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.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
43 * XXX - why was this included even on UNIX?
52 #include <sys/param.h>
55 #include <netinet/in.h>
56 #include <arpa/inet.h>
72 #include "ethertype.h"
76 #include "ieee80211.h"
78 #include "sunatmpos.h"
81 #include "pcap/ipnet.h"
83 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
84 #include <linux/types.h>
85 #include <linux/if_packet.h>
86 #include <linux/filter.h>
88 #ifdef HAVE_NET_PFVAR_H
89 #include <sys/socket.h>
91 #include <net/pfvar.h>
92 #include <net/if_pflog.h>
95 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #include <netdb.h> /* for "struct addrinfo" */
102 #include <pcap/namedb.h>
104 #define ETHERMTU 1500
106 #ifndef IPPROTO_HOPOPTS
107 #define IPPROTO_HOPOPTS 0
109 #ifndef IPPROTO_ROUTING
110 #define IPPROTO_ROUTING 43
112 #ifndef IPPROTO_FRAGMENT
113 #define IPPROTO_FRAGMENT 44
115 #ifndef IPPROTO_DSTOPTS
116 #define IPPROTO_DSTOPTS 60
119 #define IPPROTO_SCTP 132
122 #ifdef HAVE_OS_PROTO_H
123 #include "os-proto.h"
126 #define JMP(c) ((c)|BPF_JMP|BPF_K)
129 static jmp_buf top_ctx
;
130 static pcap_t
*bpf_pcap
;
132 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
134 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
136 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
140 static int pcap_fddipad
;
144 bpf_error(const char *fmt
, ...)
149 if (bpf_pcap
!= NULL
)
150 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
157 static void init_linktype(pcap_t
*);
159 static void init_regs(void);
160 static int alloc_reg(void);
161 static void free_reg(int);
163 static struct block
*root
;
166 * Value passed to gen_load_a() to indicate what the offset argument
170 OR_PACKET
, /* relative to the beginning of the packet */
171 OR_LINK
, /* relative to the beginning of the link-layer header */
172 OR_MACPL
, /* relative to the end of the MAC-layer header */
173 OR_NET
, /* relative to the network-layer header */
174 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
175 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
176 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
181 * As errors are handled by a longjmp, anything allocated must be freed
182 * in the longjmp handler, so it must be reachable from that handler.
183 * One thing that's allocated is the result of pcap_nametoaddrinfo();
184 * it must be freed with freeaddrinfo(). This variable points to any
185 * addrinfo structure that would need to be freed.
187 static struct addrinfo
*ai
;
191 * We divy out chunks of memory rather than call malloc each time so
192 * we don't have to worry about leaking memory. It's probably
193 * not a big deal if all this memory was wasted but if this ever
194 * goes into a library that would probably not be a good idea.
196 * XXX - this *is* in a library....
199 #define CHUNK0SIZE 1024
205 static struct chunk chunks
[NCHUNKS
];
206 static int cur_chunk
;
208 static void *newchunk(u_int
);
209 static void freechunks(void);
210 static inline struct block
*new_block(int);
211 static inline struct slist
*new_stmt(int);
212 static struct block
*gen_retblk(int);
213 static inline void syntax(void);
215 static void backpatch(struct block
*, struct block
*);
216 static void merge(struct block
*, struct block
*);
217 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
218 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
219 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
220 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
221 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
222 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
224 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
225 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
226 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
227 static struct slist
*gen_load_llrel(u_int
, u_int
);
228 static struct slist
*gen_load_macplrel(u_int
, u_int
);
229 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
230 static struct slist
*gen_loadx_iphdrlen(void);
231 static struct block
*gen_uncond(int);
232 static inline struct block
*gen_true(void);
233 static inline struct block
*gen_false(void);
234 static struct block
*gen_ether_linktype(int);
235 static struct block
*gen_ipnet_linktype(int);
236 static struct block
*gen_linux_sll_linktype(int);
237 static struct slist
*gen_load_prism_llprefixlen(void);
238 static struct slist
*gen_load_avs_llprefixlen(void);
239 static struct slist
*gen_load_radiotap_llprefixlen(void);
240 static struct slist
*gen_load_ppi_llprefixlen(void);
241 static void insert_compute_vloffsets(struct block
*);
242 static struct slist
*gen_llprefixlen(void);
243 static struct slist
*gen_off_macpl(void);
244 static int ethertype_to_ppptype(int);
245 static struct block
*gen_linktype(int);
246 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
247 static struct block
*gen_llc_linktype(int);
248 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
250 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
252 static struct block
*gen_ahostop(const u_char
*, int);
253 static struct block
*gen_ehostop(const u_char
*, int);
254 static struct block
*gen_fhostop(const u_char
*, int);
255 static struct block
*gen_thostop(const u_char
*, int);
256 static struct block
*gen_wlanhostop(const u_char
*, int);
257 static struct block
*gen_ipfchostop(const u_char
*, int);
258 static struct block
*gen_dnhostop(bpf_u_int32
, int);
259 static struct block
*gen_mpls_linktype(int);
260 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
262 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
265 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
267 static struct block
*gen_ipfrag(void);
268 static struct block
*gen_portatom(int, bpf_int32
);
269 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
270 static struct block
*gen_portatom6(int, bpf_int32
);
271 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
272 struct block
*gen_portop(int, int, int);
273 static struct block
*gen_port(int, int, int);
274 struct block
*gen_portrangeop(int, int, int, int);
275 static struct block
*gen_portrange(int, int, int, int);
276 struct block
*gen_portop6(int, int, int);
277 static struct block
*gen_port6(int, int, int);
278 struct block
*gen_portrangeop6(int, int, int, int);
279 static struct block
*gen_portrange6(int, int, int, int);
280 static int lookup_proto(const char *, int);
281 static struct block
*gen_protochain(int, int, int);
282 static struct block
*gen_proto(int, int, int);
283 static struct slist
*xfer_to_x(struct arth
*);
284 static struct slist
*xfer_to_a(struct arth
*);
285 static struct block
*gen_mac_multicast(int);
286 static struct block
*gen_len(int, int);
287 static struct block
*gen_check_802_11_data_frame(void);
289 static struct block
*gen_ppi_dlt_check(void);
290 static struct block
*gen_msg_abbrev(int type
);
301 /* XXX Round up to nearest long. */
302 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
304 /* XXX Round up to structure boundary. */
308 cp
= &chunks
[cur_chunk
];
309 if (n
> cp
->n_left
) {
310 ++cp
, k
= ++cur_chunk
;
312 bpf_error("out of memory");
313 size
= CHUNK0SIZE
<< k
;
314 cp
->m
= (void *)malloc(size
);
316 bpf_error("out of memory");
317 memset((char *)cp
->m
, 0, size
);
320 bpf_error("out of memory");
323 return (void *)((char *)cp
->m
+ cp
->n_left
);
332 for (i
= 0; i
< NCHUNKS
; ++i
)
333 if (chunks
[i
].m
!= NULL
) {
340 * A strdup whose allocations are freed after code generation is over.
344 register const char *s
;
346 int n
= strlen(s
) + 1;
347 char *cp
= newchunk(n
);
353 static inline struct block
*
359 p
= (struct block
*)newchunk(sizeof(*p
));
366 static inline struct slist
*
372 p
= (struct slist
*)newchunk(sizeof(*p
));
378 static struct block
*
382 struct block
*b
= new_block(BPF_RET
|BPF_K
);
391 bpf_error("syntax error in filter expression");
394 static bpf_u_int32 netmask
;
399 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
400 const char *buf
, int optimize
, bpf_u_int32 mask
);
403 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
404 const char *buf
, int optimize
, bpf_u_int32 mask
)
408 EnterCriticalSection(&g_PcapCompileCriticalSection
);
410 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
412 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
418 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
419 const char *buf
, int optimize
, bpf_u_int32 mask
)
422 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
423 const char *buf
, int optimize
, bpf_u_int32 mask
)
427 const char * volatile xbuf
= buf
;
431 * If this pcap_t hasn't been activated, it doesn't have a
432 * link-layer type, so we can't use it.
435 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
436 "not-yet-activated pcap_t passed to pcap_compile");
444 if (setjmp(top_ctx
)) {
458 snaplen
= pcap_snapshot(p
);
460 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
461 "snaplen of 0 rejects all packets");
465 lex_init(xbuf
? xbuf
: "");
473 root
= gen_retblk(snaplen
);
475 if (optimize
&& !no_optimize
) {
478 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
479 bpf_error("expression rejects all packets");
481 program
->bf_insns
= icode_to_fcode(root
, &len
);
482 program
->bf_len
= len
;
490 * entry point for using the compiler with no pcap open
491 * pass in all the stuff that is needed explicitly instead.
494 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
495 struct bpf_program
*program
,
496 const char *buf
, int optimize
, bpf_u_int32 mask
)
501 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
504 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
510 * Clean up a "struct bpf_program" by freeing all the memory allocated
514 pcap_freecode(struct bpf_program
*program
)
517 if (program
->bf_insns
!= NULL
) {
518 free((char *)program
->bf_insns
);
519 program
->bf_insns
= NULL
;
524 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
525 * which of the jt and jf fields has been resolved and which is a pointer
526 * back to another unresolved block (or nil). At least one of the fields
527 * in each block is already resolved.
530 backpatch(list
, target
)
531 struct block
*list
, *target
;
548 * Merge the lists in b0 and b1, using the 'sense' field to indicate
549 * which of jt and jf is the link.
553 struct block
*b0
, *b1
;
555 register struct block
**p
= &b0
;
557 /* Find end of list. */
559 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
561 /* Concatenate the lists. */
569 struct block
*ppi_dlt_check
;
572 * Insert before the statements of the first (root) block any
573 * statements needed to load the lengths of any variable-length
574 * headers into registers.
576 * XXX - a fancier strategy would be to insert those before the
577 * statements of all blocks that use those lengths and that
578 * have no predecessors that use them, so that we only compute
579 * the lengths if we need them. There might be even better
580 * approaches than that.
582 * However, those strategies would be more complicated, and
583 * as we don't generate code to compute a length if the
584 * program has no tests that use the length, and as most
585 * tests will probably use those lengths, we would just
586 * postpone computing the lengths so that it's not done
587 * for tests that fail early, and it's not clear that's
590 insert_compute_vloffsets(p
->head
);
593 * For DLT_PPI captures, generate a check of the per-packet
594 * DLT value to make sure it's DLT_IEEE802_11.
596 ppi_dlt_check
= gen_ppi_dlt_check();
597 if (ppi_dlt_check
!= NULL
)
598 gen_and(ppi_dlt_check
, p
);
600 backpatch(p
, gen_retblk(snaplen
));
601 p
->sense
= !p
->sense
;
602 backpatch(p
, gen_retblk(0));
608 struct block
*b0
, *b1
;
610 backpatch(b0
, b1
->head
);
611 b0
->sense
= !b0
->sense
;
612 b1
->sense
= !b1
->sense
;
614 b1
->sense
= !b1
->sense
;
620 struct block
*b0
, *b1
;
622 b0
->sense
= !b0
->sense
;
623 backpatch(b0
, b1
->head
);
624 b0
->sense
= !b0
->sense
;
633 b
->sense
= !b
->sense
;
636 static struct block
*
637 gen_cmp(offrel
, offset
, size
, v
)
638 enum e_offrel offrel
;
642 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
645 static struct block
*
646 gen_cmp_gt(offrel
, offset
, size
, v
)
647 enum e_offrel offrel
;
651 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
654 static struct block
*
655 gen_cmp_ge(offrel
, offset
, size
, v
)
656 enum e_offrel offrel
;
660 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
663 static struct block
*
664 gen_cmp_lt(offrel
, offset
, size
, v
)
665 enum e_offrel offrel
;
669 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
672 static struct block
*
673 gen_cmp_le(offrel
, offset
, size
, v
)
674 enum e_offrel offrel
;
678 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
681 static struct block
*
682 gen_mcmp(offrel
, offset
, size
, v
, mask
)
683 enum e_offrel offrel
;
688 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
691 static struct block
*
692 gen_bcmp(offrel
, offset
, size
, v
)
693 enum e_offrel offrel
;
694 register u_int offset
, size
;
695 register const u_char
*v
;
697 register struct block
*b
, *tmp
;
701 register const u_char
*p
= &v
[size
- 4];
702 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
703 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
705 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
712 register const u_char
*p
= &v
[size
- 2];
713 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
715 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
722 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
731 * AND the field of size "size" at offset "offset" relative to the header
732 * specified by "offrel" with "mask", and compare it with the value "v"
733 * with the test specified by "jtype"; if "reverse" is true, the test
734 * should test the opposite of "jtype".
736 static struct block
*
737 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
738 enum e_offrel offrel
;
740 bpf_u_int32 offset
, size
, mask
, jtype
;
743 struct slist
*s
, *s2
;
746 s
= gen_load_a(offrel
, offset
, size
);
748 if (mask
!= 0xffffffff) {
749 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
754 b
= new_block(JMP(jtype
));
757 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
763 * Various code constructs need to know the layout of the data link
764 * layer. These variables give the necessary offsets from the beginning
765 * of the packet data.
769 * This is the offset of the beginning of the link-layer header from
770 * the beginning of the raw packet data.
772 * It's usually 0, except for 802.11 with a fixed-length radio header.
773 * (For 802.11 with a variable-length radio header, we have to generate
774 * code to compute that offset; off_ll is 0 in that case.)
779 * If there's a variable-length header preceding the link-layer header,
780 * "reg_off_ll" is the register number for a register containing the
781 * length of that header, and therefore the offset of the link-layer
782 * header from the beginning of the raw packet data. Otherwise,
783 * "reg_off_ll" is -1.
785 static int reg_off_ll
;
788 * This is the offset of the beginning of the MAC-layer header from
789 * the beginning of the link-layer header.
790 * It's usually 0, except for ATM LANE, where it's the offset, relative
791 * to the beginning of the raw packet data, of the Ethernet header, and
792 * for Ethernet with various additional information.
794 static u_int off_mac
;
797 * This is the offset of the beginning of the MAC-layer payload,
798 * from the beginning of the raw packet data.
800 * I.e., it's the sum of the length of the link-layer header (without,
801 * for example, any 802.2 LLC header, so it's the MAC-layer
802 * portion of that header), plus any prefix preceding the
805 static u_int off_macpl
;
808 * This is 1 if the offset of the beginning of the MAC-layer payload
809 * from the beginning of the link-layer header is variable-length.
811 static int off_macpl_is_variable
;
814 * If the link layer has variable_length headers, "reg_off_macpl"
815 * is the register number for a register containing the length of the
816 * link-layer header plus the length of any variable-length header
817 * preceding the link-layer header. Otherwise, "reg_off_macpl"
820 static int reg_off_macpl
;
823 * "off_linktype" is the offset to information in the link-layer header
824 * giving the packet type. This offset is relative to the beginning
825 * of the link-layer header (i.e., it doesn't include off_ll).
827 * For Ethernet, it's the offset of the Ethernet type field.
829 * For link-layer types that always use 802.2 headers, it's the
830 * offset of the LLC header.
832 * For PPP, it's the offset of the PPP type field.
834 * For Cisco HDLC, it's the offset of the CHDLC type field.
836 * For BSD loopback, it's the offset of the AF_ value.
838 * For Linux cooked sockets, it's the offset of the type field.
840 * It's set to -1 for no encapsulation, in which case, IP is assumed.
842 static u_int off_linktype
;
845 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
846 * checks to check the PPP header, assumed to follow a LAN-style link-
847 * layer header and a PPPoE session header.
849 static int is_pppoes
= 0;
852 * TRUE if the link layer includes an ATM pseudo-header.
854 static int is_atm
= 0;
857 * TRUE if "lane" appeared in the filter; it causes us to generate
858 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
860 static int is_lane
= 0;
863 * These are offsets for the ATM pseudo-header.
865 static u_int off_vpi
;
866 static u_int off_vci
;
867 static u_int off_proto
;
870 * These are offsets for the MTP2 fields.
873 static u_int off_li_hsl
;
876 * These are offsets for the MTP3 fields.
878 static u_int off_sio
;
879 static u_int off_opc
;
880 static u_int off_dpc
;
881 static u_int off_sls
;
884 * This is the offset of the first byte after the ATM pseudo_header,
885 * or -1 if there is no ATM pseudo-header.
887 static u_int off_payload
;
890 * These are offsets to the beginning of the network-layer header.
891 * They are relative to the beginning of the MAC-layer payload (i.e.,
892 * they don't include off_ll or off_macpl).
894 * If the link layer never uses 802.2 LLC:
896 * "off_nl" and "off_nl_nosnap" are the same.
898 * If the link layer always uses 802.2 LLC:
900 * "off_nl" is the offset if there's a SNAP header following
903 * "off_nl_nosnap" is the offset if there's no SNAP header.
905 * If the link layer is Ethernet:
907 * "off_nl" is the offset if the packet is an Ethernet II packet
908 * (we assume no 802.3+802.2+SNAP);
910 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
911 * with an 802.2 header following it.
914 static u_int off_nl_nosnap
;
922 linktype
= pcap_datalink(p
);
923 pcap_fddipad
= p
->fddipad
;
926 * Assume it's not raw ATM with a pseudo-header, for now.
937 * And that we're not doing PPPoE.
942 * And assume we're not doing SS7.
952 * Also assume it's not 802.11.
956 off_macpl_is_variable
= 0;
960 label_stack_depth
= 0;
970 off_nl
= 0; /* XXX in reality, variable! */
971 off_nl_nosnap
= 0; /* no 802.2 LLC */
974 case DLT_ARCNET_LINUX
:
977 off_nl
= 0; /* XXX in reality, variable! */
978 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 off_macpl
= 14; /* Ethernet header length */
984 off_nl
= 0; /* Ethernet II */
985 off_nl_nosnap
= 3; /* 802.3+802.2 */
990 * SLIP doesn't have a link level type. The 16 byte
991 * header is hacked into our SLIP driver.
996 off_nl_nosnap
= 0; /* no 802.2 LLC */
1000 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1005 off_nl_nosnap
= 0; /* no 802.2 LLC */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1020 off_nl_nosnap
= 0; /* no 802.2 LLC */
1025 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1026 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1030 off_nl_nosnap
= 0; /* no 802.2 LLC */
1035 * This does no include the Ethernet header, and
1036 * only covers session state.
1041 off_nl_nosnap
= 0; /* no 802.2 LLC */
1048 off_nl_nosnap
= 0; /* no 802.2 LLC */
1053 * FDDI doesn't really have a link-level type field.
1054 * We set "off_linktype" to the offset of the LLC header.
1056 * To check for Ethernet types, we assume that SSAP = SNAP
1057 * is being used and pick out the encapsulated Ethernet type.
1058 * XXX - should we generate code to check for SNAP?
1061 off_linktype
+= pcap_fddipad
;
1062 off_macpl
= 13; /* FDDI MAC header length */
1063 off_macpl
+= pcap_fddipad
;
1064 off_nl
= 8; /* 802.2+SNAP */
1065 off_nl_nosnap
= 3; /* 802.2 */
1070 * Token Ring doesn't really have a link-level type field.
1071 * We set "off_linktype" to the offset of the LLC header.
1073 * To check for Ethernet types, we assume that SSAP = SNAP
1074 * is being used and pick out the encapsulated Ethernet type.
1075 * XXX - should we generate code to check for SNAP?
1077 * XXX - the header is actually variable-length.
1078 * Some various Linux patched versions gave 38
1079 * as "off_linktype" and 40 as "off_nl"; however,
1080 * if a token ring packet has *no* routing
1081 * information, i.e. is not source-routed, the correct
1082 * values are 20 and 22, as they are in the vanilla code.
1084 * A packet is source-routed iff the uppermost bit
1085 * of the first byte of the source address, at an
1086 * offset of 8, has the uppermost bit set. If the
1087 * packet is source-routed, the total number of bytes
1088 * of routing information is 2 plus bits 0x1F00 of
1089 * the 16-bit value at an offset of 14 (shifted right
1090 * 8 - figure out which byte that is).
1093 off_macpl
= 14; /* Token Ring MAC header length */
1094 off_nl
= 8; /* 802.2+SNAP */
1095 off_nl_nosnap
= 3; /* 802.2 */
1098 case DLT_IEEE802_11
:
1099 case DLT_PRISM_HEADER
:
1100 case DLT_IEEE802_11_RADIO_AVS
:
1101 case DLT_IEEE802_11_RADIO
:
1103 * 802.11 doesn't really have a link-level type field.
1104 * We set "off_linktype" to the offset of the LLC header.
1106 * To check for Ethernet types, we assume that SSAP = SNAP
1107 * is being used and pick out the encapsulated Ethernet type.
1108 * XXX - should we generate code to check for SNAP?
1110 * We also handle variable-length radio headers here.
1111 * The Prism header is in theory variable-length, but in
1112 * practice it's always 144 bytes long. However, some
1113 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1114 * sometimes or always supply an AVS header, so we
1115 * have to check whether the radio header is a Prism
1116 * header or an AVS header, so, in practice, it's
1120 off_macpl
= 0; /* link-layer header is variable-length */
1121 off_macpl_is_variable
= 1;
1122 off_nl
= 8; /* 802.2+SNAP */
1123 off_nl_nosnap
= 3; /* 802.2 */
1128 * At the moment we treat PPI the same way that we treat
1129 * normal Radiotap encoded packets. The difference is in
1130 * the function that generates the code at the beginning
1131 * to compute the header length. Since this code generator
1132 * of PPI supports bare 802.11 encapsulation only (i.e.
1133 * the encapsulated DLT should be DLT_IEEE802_11) we
1134 * generate code to check for this too.
1137 off_macpl
= 0; /* link-layer header is variable-length */
1138 off_macpl_is_variable
= 1;
1139 off_nl
= 8; /* 802.2+SNAP */
1140 off_nl_nosnap
= 3; /* 802.2 */
1143 case DLT_ATM_RFC1483
:
1144 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1146 * assume routed, non-ISO PDUs
1147 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1149 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1150 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1151 * latter would presumably be treated the way PPPoE
1152 * should be, so you can do "pppoe and udp port 2049"
1153 * or "pppoa and tcp port 80" and have it check for
1154 * PPPo{A,E} and a PPP protocol of IP and....
1157 off_macpl
= 0; /* packet begins with LLC header */
1158 off_nl
= 8; /* 802.2+SNAP */
1159 off_nl_nosnap
= 3; /* 802.2 */
1164 * Full Frontal ATM; you get AALn PDUs with an ATM
1168 off_vpi
= SUNATM_VPI_POS
;
1169 off_vci
= SUNATM_VCI_POS
;
1170 off_proto
= PROTO_POS
;
1171 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1172 off_payload
= SUNATM_PKT_BEGIN_POS
;
1173 off_linktype
= off_payload
;
1174 off_macpl
= off_payload
; /* if LLC-encapsulated */
1175 off_nl
= 8; /* 802.2+SNAP */
1176 off_nl_nosnap
= 3; /* 802.2 */
1185 off_nl_nosnap
= 0; /* no 802.2 LLC */
1188 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1192 off_nl_nosnap
= 0; /* no 802.2 LLC */
1197 * LocalTalk does have a 1-byte type field in the LLAP header,
1198 * but really it just indicates whether there is a "short" or
1199 * "long" DDP packet following.
1204 off_nl_nosnap
= 0; /* no 802.2 LLC */
1207 case DLT_IP_OVER_FC
:
1209 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1210 * link-level type field. We set "off_linktype" to the
1211 * offset of the LLC header.
1213 * To check for Ethernet types, we assume that SSAP = SNAP
1214 * is being used and pick out the encapsulated Ethernet type.
1215 * XXX - should we generate code to check for SNAP? RFC
1216 * 2625 says SNAP should be used.
1220 off_nl
= 8; /* 802.2+SNAP */
1221 off_nl_nosnap
= 3; /* 802.2 */
1226 * XXX - we should set this to handle SNAP-encapsulated
1227 * frames (NLPID of 0x80).
1232 off_nl_nosnap
= 0; /* no 802.2 LLC */
1236 * the only BPF-interesting FRF.16 frames are non-control frames;
1237 * Frame Relay has a variable length link-layer
1238 * so lets start with offset 4 for now and increments later on (FIXME);
1244 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1247 case DLT_APPLE_IP_OVER_IEEE1394
:
1251 off_nl_nosnap
= 0; /* no 802.2 LLC */
1254 case DLT_SYMANTEC_FIREWALL
:
1257 off_nl
= 0; /* Ethernet II */
1258 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1261 #ifdef HAVE_NET_PFVAR_H
1264 off_macpl
= PFLOG_HDRLEN
;
1266 off_nl_nosnap
= 0; /* no 802.2 LLC */
1270 case DLT_JUNIPER_MFR
:
1271 case DLT_JUNIPER_MLFR
:
1272 case DLT_JUNIPER_MLPPP
:
1273 case DLT_JUNIPER_PPP
:
1274 case DLT_JUNIPER_CHDLC
:
1275 case DLT_JUNIPER_FRELAY
:
1279 off_nl_nosnap
= -1; /* no 802.2 LLC */
1282 case DLT_JUNIPER_ATM1
:
1283 off_linktype
= 4; /* in reality variable between 4-8 */
1284 off_macpl
= 4; /* in reality variable between 4-8 */
1289 case DLT_JUNIPER_ATM2
:
1290 off_linktype
= 8; /* in reality variable between 8-12 */
1291 off_macpl
= 8; /* in reality variable between 8-12 */
1296 /* frames captured on a Juniper PPPoE service PIC
1297 * contain raw ethernet frames */
1298 case DLT_JUNIPER_PPPOE
:
1299 case DLT_JUNIPER_ETHER
:
1302 off_nl
= 18; /* Ethernet II */
1303 off_nl_nosnap
= 21; /* 802.3+802.2 */
1306 case DLT_JUNIPER_PPPOE_ATM
:
1310 off_nl_nosnap
= -1; /* no 802.2 LLC */
1313 case DLT_JUNIPER_GGSN
:
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_ES
:
1322 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1323 off_nl
= -1; /* not really a network layer but raw IP addresses */
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_MONITOR
:
1330 off_nl
= 0; /* raw IP/IP6 header */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_BACNET_MS_TP
:
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
:
1369 case DLT_JUNIPER_VS
:
1370 case DLT_JUNIPER_SRX_E2E
:
1371 case DLT_JUNIPER_FIBRECHANNEL
:
1372 case DLT_JUNIPER_ATM_CEMIC
:
1392 case DLT_MTP2_WITH_PHDR
:
1427 * Currently, only raw "link[N:M]" filtering is supported.
1429 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1431 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1432 off_nl_nosnap
= -1; /* no 802.2 LLC */
1433 off_mac
= 1; /* step over the kiss length byte */
1438 off_macpl
= 24; /* ipnet header length */
1443 case DLT_NETANALYZER
:
1444 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1445 off_linktype
= 16; /* includes 4-byte pseudo-header */
1446 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1447 off_nl
= 0; /* Ethernet II */
1448 off_nl_nosnap
= 3; /* 802.3+802.2 */
1451 case DLT_NETANALYZER_TRANSPARENT
:
1452 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1453 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1454 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1455 off_nl
= 0; /* Ethernet II */
1456 off_nl_nosnap
= 3; /* 802.3+802.2 */
1461 * For values in the range in which we've assigned new
1462 * DLT_ values, only raw "link[N:M]" filtering is supported.
1464 if (linktype
>= DLT_MATCHING_MIN
&&
1465 linktype
<= DLT_MATCHING_MAX
) {
1474 bpf_error("unknown data link type %d", linktype
);
1479 * Load a value relative to the beginning of the link-layer header.
1480 * The link-layer header doesn't necessarily begin at the beginning
1481 * of the packet data; there might be a variable-length prefix containing
1482 * radio information.
1484 static struct slist
*
1485 gen_load_llrel(offset
, size
)
1488 struct slist
*s
, *s2
;
1490 s
= gen_llprefixlen();
1493 * If "s" is non-null, it has code to arrange that the X register
1494 * contains the length of the prefix preceding the link-layer
1497 * Otherwise, the length of the prefix preceding the link-layer
1498 * header is "off_ll".
1502 * There's a variable-length prefix preceding the
1503 * link-layer header. "s" points to a list of statements
1504 * that put the length of that prefix into the X register.
1505 * do an indirect load, to use the X register as an offset.
1507 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1512 * There is no variable-length header preceding the
1513 * link-layer header; add in off_ll, which, if there's
1514 * a fixed-length header preceding the link-layer header,
1515 * is the length of that header.
1517 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1518 s
->s
.k
= offset
+ off_ll
;
1524 * Load a value relative to the beginning of the MAC-layer payload.
1526 static struct slist
*
1527 gen_load_macplrel(offset
, size
)
1530 struct slist
*s
, *s2
;
1532 s
= gen_off_macpl();
1535 * If s is non-null, the offset of the MAC-layer payload is
1536 * variable, and s points to a list of instructions that
1537 * arrange that the X register contains that offset.
1539 * Otherwise, the offset of the MAC-layer payload is constant,
1540 * and is in off_macpl.
1544 * The offset of the MAC-layer payload is in the X
1545 * register. Do an indirect load, to use the X register
1548 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1553 * The offset of the MAC-layer payload is constant,
1554 * and is in off_macpl; load the value at that offset
1555 * plus the specified offset.
1557 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1558 s
->s
.k
= off_macpl
+ offset
;
1564 * Load a value relative to the beginning of the specified header.
1566 static struct slist
*
1567 gen_load_a(offrel
, offset
, size
)
1568 enum e_offrel offrel
;
1571 struct slist
*s
, *s2
;
1576 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1581 s
= gen_load_llrel(offset
, size
);
1585 s
= gen_load_macplrel(offset
, size
);
1589 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1593 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1598 * Load the X register with the length of the IPv4 header
1599 * (plus the offset of the link-layer header, if it's
1600 * preceded by a variable-length header such as a radio
1601 * header), in bytes.
1603 s
= gen_loadx_iphdrlen();
1606 * Load the item at {offset of the MAC-layer payload} +
1607 * {offset, relative to the start of the MAC-layer
1608 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1609 * {specified offset}.
1611 * (If the offset of the MAC-layer payload is variable,
1612 * it's included in the value in the X register, and
1615 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1616 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1621 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1632 * Generate code to load into the X register the sum of the length of
1633 * the IPv4 header and any variable-length header preceding the link-layer
1636 static struct slist
*
1637 gen_loadx_iphdrlen()
1639 struct slist
*s
, *s2
;
1641 s
= gen_off_macpl();
1644 * There's a variable-length prefix preceding the
1645 * link-layer header, or the link-layer header is itself
1646 * variable-length. "s" points to a list of statements
1647 * that put the offset of the MAC-layer payload into
1650 * The 4*([k]&0xf) addressing mode can't be used, as we
1651 * don't have a constant offset, so we have to load the
1652 * value in question into the A register and add to it
1653 * the value from the X register.
1655 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1658 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1661 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1666 * The A register now contains the length of the
1667 * IP header. We need to add to it the offset of
1668 * the MAC-layer payload, which is still in the X
1669 * register, and move the result into the X register.
1671 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1672 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1675 * There is no variable-length header preceding the
1676 * link-layer header, and the link-layer header is
1677 * fixed-length; load the length of the IPv4 header,
1678 * which is at an offset of off_nl from the beginning
1679 * of the MAC-layer payload, and thus at an offset
1680 * of off_mac_pl + off_nl from the beginning of the
1683 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1684 s
->s
.k
= off_macpl
+ off_nl
;
1689 static struct block
*
1696 s
= new_stmt(BPF_LD
|BPF_IMM
);
1698 b
= new_block(JMP(BPF_JEQ
));
1704 static inline struct block
*
1707 return gen_uncond(1);
1710 static inline struct block
*
1713 return gen_uncond(0);
1717 * Byte-swap a 32-bit number.
1718 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1719 * big-endian platforms.)
1721 #define SWAPLONG(y) \
1722 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1725 * Generate code to match a particular packet type.
1727 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1728 * value, if <= ETHERMTU. We use that to determine whether to
1729 * match the type/length field or to check the type/length field for
1730 * a value <= ETHERMTU to see whether it's a type field and then do
1731 * the appropriate test.
1733 static struct block
*
1734 gen_ether_linktype(proto
)
1737 struct block
*b0
, *b1
;
1743 case LLCSAP_NETBEUI
:
1745 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1746 * so we check the DSAP and SSAP.
1748 * LLCSAP_IP checks for IP-over-802.2, rather
1749 * than IP-over-Ethernet or IP-over-SNAP.
1751 * XXX - should we check both the DSAP and the
1752 * SSAP, like this, or should we check just the
1753 * DSAP, as we do for other types <= ETHERMTU
1754 * (i.e., other SAP values)?
1756 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1758 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1759 ((proto
<< 8) | proto
));
1767 * Ethernet_II frames, which are Ethernet
1768 * frames with a frame type of ETHERTYPE_IPX;
1770 * Ethernet_802.3 frames, which are 802.3
1771 * frames (i.e., the type/length field is
1772 * a length field, <= ETHERMTU, rather than
1773 * a type field) with the first two bytes
1774 * after the Ethernet/802.3 header being
1777 * Ethernet_802.2 frames, which are 802.3
1778 * frames with an 802.2 LLC header and
1779 * with the IPX LSAP as the DSAP in the LLC
1782 * Ethernet_SNAP frames, which are 802.3
1783 * frames with an LLC header and a SNAP
1784 * header and with an OUI of 0x000000
1785 * (encapsulated Ethernet) and a protocol
1786 * ID of ETHERTYPE_IPX in the SNAP header.
1788 * XXX - should we generate the same code both
1789 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1793 * This generates code to check both for the
1794 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1796 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1797 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1801 * Now we add code to check for SNAP frames with
1802 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1804 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1808 * Now we generate code to check for 802.3
1809 * frames in general.
1811 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1815 * Now add the check for 802.3 frames before the
1816 * check for Ethernet_802.2 and Ethernet_802.3,
1817 * as those checks should only be done on 802.3
1818 * frames, not on Ethernet frames.
1823 * Now add the check for Ethernet_II frames, and
1824 * do that before checking for the other frame
1827 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1828 (bpf_int32
)ETHERTYPE_IPX
);
1832 case ETHERTYPE_ATALK
:
1833 case ETHERTYPE_AARP
:
1835 * EtherTalk (AppleTalk protocols on Ethernet link
1836 * layer) may use 802.2 encapsulation.
1840 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1841 * we check for an Ethernet type field less than
1842 * 1500, which means it's an 802.3 length field.
1844 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1848 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1849 * SNAP packets with an organization code of
1850 * 0x080007 (Apple, for Appletalk) and a protocol
1851 * type of ETHERTYPE_ATALK (Appletalk).
1853 * 802.2-encapsulated ETHERTYPE_AARP packets are
1854 * SNAP packets with an organization code of
1855 * 0x000000 (encapsulated Ethernet) and a protocol
1856 * type of ETHERTYPE_AARP (Appletalk ARP).
1858 if (proto
== ETHERTYPE_ATALK
)
1859 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1860 else /* proto == ETHERTYPE_AARP */
1861 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1865 * Check for Ethernet encapsulation (Ethertalk
1866 * phase 1?); we just check for the Ethernet
1869 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1875 if (proto
<= ETHERMTU
) {
1877 * This is an LLC SAP value, so the frames
1878 * that match would be 802.2 frames.
1879 * Check that the frame is an 802.2 frame
1880 * (i.e., that the length/type field is
1881 * a length field, <= ETHERMTU) and
1882 * then check the DSAP.
1884 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1886 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1892 * This is an Ethernet type, so compare
1893 * the length/type field with it (if
1894 * the frame is an 802.2 frame, the length
1895 * field will be <= ETHERMTU, and, as
1896 * "proto" is > ETHERMTU, this test
1897 * will fail and the frame won't match,
1898 * which is what we want).
1900 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1907 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1908 * or IPv6 then we have an error.
1910 static struct block
*
1911 gen_ipnet_linktype(proto
)
1917 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1918 (bpf_int32
)IPH_AF_INET
);
1921 case ETHERTYPE_IPV6
:
1922 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1923 (bpf_int32
)IPH_AF_INET6
);
1934 * Generate code to match a particular packet type.
1936 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1937 * value, if <= ETHERMTU. We use that to determine whether to
1938 * match the type field or to check the type field for the special
1939 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1941 static struct block
*
1942 gen_linux_sll_linktype(proto
)
1945 struct block
*b0
, *b1
;
1951 case LLCSAP_NETBEUI
:
1953 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1954 * so we check the DSAP and SSAP.
1956 * LLCSAP_IP checks for IP-over-802.2, rather
1957 * than IP-over-Ethernet or IP-over-SNAP.
1959 * XXX - should we check both the DSAP and the
1960 * SSAP, like this, or should we check just the
1961 * DSAP, as we do for other types <= ETHERMTU
1962 * (i.e., other SAP values)?
1964 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1965 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1966 ((proto
<< 8) | proto
));
1972 * Ethernet_II frames, which are Ethernet
1973 * frames with a frame type of ETHERTYPE_IPX;
1975 * Ethernet_802.3 frames, which have a frame
1976 * type of LINUX_SLL_P_802_3;
1978 * Ethernet_802.2 frames, which are 802.3
1979 * frames with an 802.2 LLC header (i.e, have
1980 * a frame type of LINUX_SLL_P_802_2) and
1981 * with the IPX LSAP as the DSAP in the LLC
1984 * Ethernet_SNAP frames, which are 802.3
1985 * frames with an LLC header and a SNAP
1986 * header and with an OUI of 0x000000
1987 * (encapsulated Ethernet) and a protocol
1988 * ID of ETHERTYPE_IPX in the SNAP header.
1990 * First, do the checks on LINUX_SLL_P_802_2
1991 * frames; generate the check for either
1992 * Ethernet_802.2 or Ethernet_SNAP frames, and
1993 * then put a check for LINUX_SLL_P_802_2 frames
1996 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1997 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1999 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2003 * Now check for 802.3 frames and OR that with
2004 * the previous test.
2006 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2010 * Now add the check for Ethernet_II frames, and
2011 * do that before checking for the other frame
2014 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2015 (bpf_int32
)ETHERTYPE_IPX
);
2019 case ETHERTYPE_ATALK
:
2020 case ETHERTYPE_AARP
:
2022 * EtherTalk (AppleTalk protocols on Ethernet link
2023 * layer) may use 802.2 encapsulation.
2027 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2028 * we check for the 802.2 protocol type in the
2029 * "Ethernet type" field.
2031 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2034 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2035 * SNAP packets with an organization code of
2036 * 0x080007 (Apple, for Appletalk) and a protocol
2037 * type of ETHERTYPE_ATALK (Appletalk).
2039 * 802.2-encapsulated ETHERTYPE_AARP packets are
2040 * SNAP packets with an organization code of
2041 * 0x000000 (encapsulated Ethernet) and a protocol
2042 * type of ETHERTYPE_AARP (Appletalk ARP).
2044 if (proto
== ETHERTYPE_ATALK
)
2045 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2046 else /* proto == ETHERTYPE_AARP */
2047 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2051 * Check for Ethernet encapsulation (Ethertalk
2052 * phase 1?); we just check for the Ethernet
2055 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2061 if (proto
<= ETHERMTU
) {
2063 * This is an LLC SAP value, so the frames
2064 * that match would be 802.2 frames.
2065 * Check for the 802.2 protocol type
2066 * in the "Ethernet type" field, and
2067 * then check the DSAP.
2069 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2071 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2077 * This is an Ethernet type, so compare
2078 * the length/type field with it (if
2079 * the frame is an 802.2 frame, the length
2080 * field will be <= ETHERMTU, and, as
2081 * "proto" is > ETHERMTU, this test
2082 * will fail and the frame won't match,
2083 * which is what we want).
2085 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2091 static struct slist
*
2092 gen_load_prism_llprefixlen()
2094 struct slist
*s1
, *s2
;
2095 struct slist
*sjeq_avs_cookie
;
2096 struct slist
*sjcommon
;
2099 * This code is not compatible with the optimizer, as
2100 * we are generating jmp instructions within a normal
2101 * slist of instructions
2106 * Generate code to load the length of the radio header into
2107 * the register assigned to hold that length, if one has been
2108 * assigned. (If one hasn't been assigned, no code we've
2109 * generated uses that prefix, so we don't need to generate any
2112 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2113 * or always use the AVS header rather than the Prism header.
2114 * We load a 4-byte big-endian value at the beginning of the
2115 * raw packet data, and see whether, when masked with 0xFFFFF000,
2116 * it's equal to 0x80211000. If so, that indicates that it's
2117 * an AVS header (the masked-out bits are the version number).
2118 * Otherwise, it's a Prism header.
2120 * XXX - the Prism header is also, in theory, variable-length,
2121 * but no known software generates headers that aren't 144
2124 if (reg_off_ll
!= -1) {
2128 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2132 * AND it with 0xFFFFF000.
2134 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2135 s2
->s
.k
= 0xFFFFF000;
2139 * Compare with 0x80211000.
2141 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2142 sjeq_avs_cookie
->s
.k
= 0x80211000;
2143 sappend(s1
, sjeq_avs_cookie
);
2148 * The 4 bytes at an offset of 4 from the beginning of
2149 * the AVS header are the length of the AVS header.
2150 * That field is big-endian.
2152 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2155 sjeq_avs_cookie
->s
.jt
= s2
;
2158 * Now jump to the code to allocate a register
2159 * into which to save the header length and
2160 * store the length there. (The "jump always"
2161 * instruction needs to have the k field set;
2162 * it's added to the PC, so, as we're jumping
2163 * over a single instruction, it should be 1.)
2165 sjcommon
= new_stmt(JMP(BPF_JA
));
2167 sappend(s1
, sjcommon
);
2170 * Now for the code that handles the Prism header.
2171 * Just load the length of the Prism header (144)
2172 * into the A register. Have the test for an AVS
2173 * header branch here if we don't have an AVS header.
2175 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2178 sjeq_avs_cookie
->s
.jf
= s2
;
2181 * Now allocate a register to hold that value and store
2182 * it. The code for the AVS header will jump here after
2183 * loading the length of the AVS header.
2185 s2
= new_stmt(BPF_ST
);
2186 s2
->s
.k
= reg_off_ll
;
2188 sjcommon
->s
.jf
= s2
;
2191 * Now move it into the X register.
2193 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2201 static struct slist
*
2202 gen_load_avs_llprefixlen()
2204 struct slist
*s1
, *s2
;
2207 * Generate code to load the length of the AVS header into
2208 * the register assigned to hold that length, if one has been
2209 * assigned. (If one hasn't been assigned, no code we've
2210 * generated uses that prefix, so we don't need to generate any
2213 if (reg_off_ll
!= -1) {
2215 * The 4 bytes at an offset of 4 from the beginning of
2216 * the AVS header are the length of the AVS header.
2217 * That field is big-endian.
2219 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2223 * Now allocate a register to hold that value and store
2226 s2
= new_stmt(BPF_ST
);
2227 s2
->s
.k
= reg_off_ll
;
2231 * Now move it into the X register.
2233 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2241 static struct slist
*
2242 gen_load_radiotap_llprefixlen()
2244 struct slist
*s1
, *s2
;
2247 * Generate code to load the length of the radiotap header into
2248 * the register assigned to hold that length, if one has been
2249 * assigned. (If one hasn't been assigned, no code we've
2250 * generated uses that prefix, so we don't need to generate any
2253 if (reg_off_ll
!= -1) {
2255 * The 2 bytes at offsets of 2 and 3 from the beginning
2256 * of the radiotap header are the length of the radiotap
2257 * header; unfortunately, it's little-endian, so we have
2258 * to load it a byte at a time and construct the value.
2262 * Load the high-order byte, at an offset of 3, shift it
2263 * left a byte, and put the result in the X register.
2265 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2267 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2270 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2274 * Load the next byte, at an offset of 2, and OR the
2275 * value from the X register into it.
2277 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2280 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2284 * Now allocate a register to hold that value and store
2287 s2
= new_stmt(BPF_ST
);
2288 s2
->s
.k
= reg_off_ll
;
2292 * Now move it into the X register.
2294 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2303 * At the moment we treat PPI as normal Radiotap encoded
2304 * packets. The difference is in the function that generates
2305 * the code at the beginning to compute the header length.
2306 * Since this code generator of PPI supports bare 802.11
2307 * encapsulation only (i.e. the encapsulated DLT should be
2308 * DLT_IEEE802_11) we generate code to check for this too;
2309 * that's done in finish_parse().
2311 static struct slist
*
2312 gen_load_ppi_llprefixlen()
2314 struct slist
*s1
, *s2
;
2317 * Generate code to load the length of the radiotap header
2318 * into the register assigned to hold that length, if one has
2321 if (reg_off_ll
!= -1) {
2323 * The 2 bytes at offsets of 2 and 3 from the beginning
2324 * of the radiotap header are the length of the radiotap
2325 * header; unfortunately, it's little-endian, so we have
2326 * to load it a byte at a time and construct the value.
2330 * Load the high-order byte, at an offset of 3, shift it
2331 * left a byte, and put the result in the X register.
2333 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2335 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2338 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2342 * Load the next byte, at an offset of 2, and OR the
2343 * value from the X register into it.
2345 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2348 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2352 * Now allocate a register to hold that value and store
2355 s2
= new_stmt(BPF_ST
);
2356 s2
->s
.k
= reg_off_ll
;
2360 * Now move it into the X register.
2362 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2371 * Load a value relative to the beginning of the link-layer header after the 802.11
2372 * header, i.e. LLC_SNAP.
2373 * The link-layer header doesn't necessarily begin at the beginning
2374 * of the packet data; there might be a variable-length prefix containing
2375 * radio information.
2377 static struct slist
*
2378 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2381 struct slist
*sjset_data_frame_1
;
2382 struct slist
*sjset_data_frame_2
;
2383 struct slist
*sjset_qos
;
2384 struct slist
*sjset_radiotap_flags
;
2385 struct slist
*sjset_radiotap_tsft
;
2386 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2387 struct slist
*s_roundup
;
2389 if (reg_off_macpl
== -1) {
2391 * No register has been assigned to the offset of
2392 * the MAC-layer payload, which means nobody needs
2393 * it; don't bother computing it - just return
2394 * what we already have.
2400 * This code is not compatible with the optimizer, as
2401 * we are generating jmp instructions within a normal
2402 * slist of instructions
2407 * If "s" is non-null, it has code to arrange that the X register
2408 * contains the length of the prefix preceding the link-layer
2411 * Otherwise, the length of the prefix preceding the link-layer
2412 * header is "off_ll".
2416 * There is no variable-length header preceding the
2417 * link-layer header.
2419 * Load the length of the fixed-length prefix preceding
2420 * the link-layer header (if any) into the X register,
2421 * and store it in the reg_off_macpl register.
2422 * That length is off_ll.
2424 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2429 * The X register contains the offset of the beginning of the
2430 * link-layer header; add 24, which is the minimum length
2431 * of the MAC header for a data frame, to that, and store it
2432 * in reg_off_macpl, and then load the Frame Control field,
2433 * which is at the offset in the X register, with an indexed load.
2435 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2437 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2440 s2
= new_stmt(BPF_ST
);
2441 s2
->s
.k
= reg_off_macpl
;
2444 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2449 * Check the Frame Control field to see if this is a data frame;
2450 * a data frame has the 0x08 bit (b3) in that field set and the
2451 * 0x04 bit (b2) clear.
2453 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2454 sjset_data_frame_1
->s
.k
= 0x08;
2455 sappend(s
, sjset_data_frame_1
);
2458 * If b3 is set, test b2, otherwise go to the first statement of
2459 * the rest of the program.
2461 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2462 sjset_data_frame_2
->s
.k
= 0x04;
2463 sappend(s
, sjset_data_frame_2
);
2464 sjset_data_frame_1
->s
.jf
= snext
;
2467 * If b2 is not set, this is a data frame; test the QoS bit.
2468 * Otherwise, go to the first statement of the rest of the
2471 sjset_data_frame_2
->s
.jt
= snext
;
2472 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2473 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2474 sappend(s
, sjset_qos
);
2477 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2479 * Otherwise, go to the first statement of the rest of the
2482 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2483 s2
->s
.k
= reg_off_macpl
;
2485 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2488 s2
= new_stmt(BPF_ST
);
2489 s2
->s
.k
= reg_off_macpl
;
2493 * If we have a radiotap header, look at it to see whether
2494 * there's Atheros padding between the MAC-layer header
2497 * Note: all of the fields in the radiotap header are
2498 * little-endian, so we byte-swap all of the values
2499 * we test against, as they will be loaded as big-endian
2502 if (linktype
== DLT_IEEE802_11_RADIO
) {
2504 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2505 * in the presence flag?
2507 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2511 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2512 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2513 sappend(s
, sjset_radiotap_flags
);
2516 * If not, skip all of this.
2518 sjset_radiotap_flags
->s
.jf
= snext
;
2521 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2523 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2524 new_stmt(JMP(BPF_JSET
));
2525 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2526 sappend(s
, sjset_radiotap_tsft
);
2529 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2530 * at an offset of 16 from the beginning of the raw packet
2531 * data (8 bytes for the radiotap header and 8 bytes for
2534 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2537 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2541 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2542 sjset_tsft_datapad
->s
.k
= 0x20;
2543 sappend(s
, sjset_tsft_datapad
);
2546 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2547 * at an offset of 8 from the beginning of the raw packet
2548 * data (8 bytes for the radiotap header).
2550 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2553 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2557 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2558 sjset_notsft_datapad
->s
.k
= 0x20;
2559 sappend(s
, sjset_notsft_datapad
);
2562 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2563 * set, round the length of the 802.11 header to
2564 * a multiple of 4. Do that by adding 3 and then
2565 * dividing by and multiplying by 4, which we do by
2568 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2569 s_roundup
->s
.k
= reg_off_macpl
;
2570 sappend(s
, s_roundup
);
2571 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2574 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2577 s2
= new_stmt(BPF_ST
);
2578 s2
->s
.k
= reg_off_macpl
;
2581 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2582 sjset_tsft_datapad
->s
.jf
= snext
;
2583 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2584 sjset_notsft_datapad
->s
.jf
= snext
;
2586 sjset_qos
->s
.jf
= snext
;
2592 insert_compute_vloffsets(b
)
2598 * For link-layer types that have a variable-length header
2599 * preceding the link-layer header, generate code to load
2600 * the offset of the link-layer header into the register
2601 * assigned to that offset, if any.
2605 case DLT_PRISM_HEADER
:
2606 s
= gen_load_prism_llprefixlen();
2609 case DLT_IEEE802_11_RADIO_AVS
:
2610 s
= gen_load_avs_llprefixlen();
2613 case DLT_IEEE802_11_RADIO
:
2614 s
= gen_load_radiotap_llprefixlen();
2618 s
= gen_load_ppi_llprefixlen();
2627 * For link-layer types that have a variable-length link-layer
2628 * header, generate code to load the offset of the MAC-layer
2629 * payload into the register assigned to that offset, if any.
2633 case DLT_IEEE802_11
:
2634 case DLT_PRISM_HEADER
:
2635 case DLT_IEEE802_11_RADIO_AVS
:
2636 case DLT_IEEE802_11_RADIO
:
2638 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2643 * If we have any offset-loading code, append all the
2644 * existing statements in the block to those statements,
2645 * and make the resulting list the list of statements
2649 sappend(s
, b
->stmts
);
2654 static struct block
*
2655 gen_ppi_dlt_check(void)
2657 struct slist
*s_load_dlt
;
2660 if (linktype
== DLT_PPI
)
2662 /* Create the statements that check for the DLT
2664 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2665 s_load_dlt
->s
.k
= 4;
2667 b
= new_block(JMP(BPF_JEQ
));
2669 b
->stmts
= s_load_dlt
;
2670 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2680 static struct slist
*
2681 gen_prism_llprefixlen(void)
2685 if (reg_off_ll
== -1) {
2687 * We haven't yet assigned a register for the length
2688 * of the radio header; allocate one.
2690 reg_off_ll
= alloc_reg();
2694 * Load the register containing the radio length
2695 * into the X register.
2697 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2698 s
->s
.k
= reg_off_ll
;
2702 static struct slist
*
2703 gen_avs_llprefixlen(void)
2707 if (reg_off_ll
== -1) {
2709 * We haven't yet assigned a register for the length
2710 * of the AVS header; allocate one.
2712 reg_off_ll
= alloc_reg();
2716 * Load the register containing the AVS length
2717 * into the X register.
2719 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2720 s
->s
.k
= reg_off_ll
;
2724 static struct slist
*
2725 gen_radiotap_llprefixlen(void)
2729 if (reg_off_ll
== -1) {
2731 * We haven't yet assigned a register for the length
2732 * of the radiotap header; allocate one.
2734 reg_off_ll
= alloc_reg();
2738 * Load the register containing the radiotap length
2739 * into the X register.
2741 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2742 s
->s
.k
= reg_off_ll
;
2747 * At the moment we treat PPI as normal Radiotap encoded
2748 * packets. The difference is in the function that generates
2749 * the code at the beginning to compute the header length.
2750 * Since this code generator of PPI supports bare 802.11
2751 * encapsulation only (i.e. the encapsulated DLT should be
2752 * DLT_IEEE802_11) we generate code to check for this too.
2754 static struct slist
*
2755 gen_ppi_llprefixlen(void)
2759 if (reg_off_ll
== -1) {
2761 * We haven't yet assigned a register for the length
2762 * of the radiotap header; allocate one.
2764 reg_off_ll
= alloc_reg();
2768 * Load the register containing the PPI length
2769 * into the X register.
2771 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2772 s
->s
.k
= reg_off_ll
;
2777 * Generate code to compute the link-layer header length, if necessary,
2778 * putting it into the X register, and to return either a pointer to a
2779 * "struct slist" for the list of statements in that code, or NULL if
2780 * no code is necessary.
2782 static struct slist
*
2783 gen_llprefixlen(void)
2787 case DLT_PRISM_HEADER
:
2788 return gen_prism_llprefixlen();
2790 case DLT_IEEE802_11_RADIO_AVS
:
2791 return gen_avs_llprefixlen();
2793 case DLT_IEEE802_11_RADIO
:
2794 return gen_radiotap_llprefixlen();
2797 return gen_ppi_llprefixlen();
2805 * Generate code to load the register containing the offset of the
2806 * MAC-layer payload into the X register; if no register for that offset
2807 * has been allocated, allocate it first.
2809 static struct slist
*
2814 if (off_macpl_is_variable
) {
2815 if (reg_off_macpl
== -1) {
2817 * We haven't yet assigned a register for the offset
2818 * of the MAC-layer payload; allocate one.
2820 reg_off_macpl
= alloc_reg();
2824 * Load the register containing the offset of the MAC-layer
2825 * payload into the X register.
2827 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2828 s
->s
.k
= reg_off_macpl
;
2832 * That offset isn't variable, so we don't need to
2833 * generate any code.
2840 * Map an Ethernet type to the equivalent PPP type.
2843 ethertype_to_ppptype(proto
)
2852 case ETHERTYPE_IPV6
:
2860 case ETHERTYPE_ATALK
:
2874 * I'm assuming the "Bridging PDU"s that go
2875 * over PPP are Spanning Tree Protocol
2889 * Generate code to match a particular packet type by matching the
2890 * link-layer type field or fields in the 802.2 LLC header.
2892 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2893 * value, if <= ETHERMTU.
2895 static struct block
*
2899 struct block
*b0
, *b1
, *b2
;
2901 /* are we checking MPLS-encapsulated packets? */
2902 if (label_stack_depth
> 0) {
2906 /* FIXME add other L3 proto IDs */
2907 return gen_mpls_linktype(Q_IP
);
2909 case ETHERTYPE_IPV6
:
2911 /* FIXME add other L3 proto IDs */
2912 return gen_mpls_linktype(Q_IPV6
);
2915 bpf_error("unsupported protocol over mpls");
2921 * Are we testing PPPoE packets?
2925 * The PPPoE session header is part of the
2926 * MAC-layer payload, so all references
2927 * should be relative to the beginning of
2932 * We use Ethernet protocol types inside libpcap;
2933 * map them to the corresponding PPP protocol types.
2935 proto
= ethertype_to_ppptype(proto
);
2936 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2942 case DLT_NETANALYZER
:
2943 case DLT_NETANALYZER_TRANSPARENT
:
2944 return gen_ether_linktype(proto
);
2952 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2956 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2963 case DLT_IEEE802_11
:
2964 case DLT_PRISM_HEADER
:
2965 case DLT_IEEE802_11_RADIO_AVS
:
2966 case DLT_IEEE802_11_RADIO
:
2969 * Check that we have a data frame.
2971 b0
= gen_check_802_11_data_frame();
2974 * Now check for the specified link-layer type.
2976 b1
= gen_llc_linktype(proto
);
2984 * XXX - check for asynchronous frames, as per RFC 1103.
2986 return gen_llc_linktype(proto
);
2992 * XXX - check for LLC PDUs, as per IEEE 802.5.
2994 return gen_llc_linktype(proto
);
2998 case DLT_ATM_RFC1483
:
3000 case DLT_IP_OVER_FC
:
3001 return gen_llc_linktype(proto
);
3007 * If "is_lane" is set, check for a LANE-encapsulated
3008 * version of this protocol, otherwise check for an
3009 * LLC-encapsulated version of this protocol.
3011 * We assume LANE means Ethernet, not Token Ring.
3015 * Check that the packet doesn't begin with an
3016 * LE Control marker. (We've already generated
3019 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3024 * Now generate an Ethernet test.
3026 b1
= gen_ether_linktype(proto
);
3031 * Check for LLC encapsulation and then check the
3034 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3035 b1
= gen_llc_linktype(proto
);
3043 return gen_linux_sll_linktype(proto
);
3048 case DLT_SLIP_BSDOS
:
3051 * These types don't provide any type field; packets
3052 * are always IPv4 or IPv6.
3054 * XXX - for IPv4, check for a version number of 4, and,
3055 * for IPv6, check for a version number of 6?
3060 /* Check for a version number of 4. */
3061 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3063 case ETHERTYPE_IPV6
:
3064 /* Check for a version number of 6. */
3065 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3068 return gen_false(); /* always false */
3075 * Raw IPv4, so no type field.
3077 if (proto
== ETHERTYPE_IP
)
3078 return gen_true(); /* always true */
3080 /* Checking for something other than IPv4; always false */
3087 * Raw IPv6, so no type field.
3089 if (proto
== ETHERTYPE_IPV6
)
3090 return gen_true(); /* always true */
3092 /* Checking for something other than IPv6; always false */
3099 case DLT_PPP_SERIAL
:
3102 * We use Ethernet protocol types inside libpcap;
3103 * map them to the corresponding PPP protocol types.
3105 proto
= ethertype_to_ppptype(proto
);
3106 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3112 * We use Ethernet protocol types inside libpcap;
3113 * map them to the corresponding PPP protocol types.
3119 * Also check for Van Jacobson-compressed IP.
3120 * XXX - do this for other forms of PPP?
3122 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3123 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3125 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3130 proto
= ethertype_to_ppptype(proto
);
3131 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3141 * For DLT_NULL, the link-layer header is a 32-bit
3142 * word containing an AF_ value in *host* byte order,
3143 * and for DLT_ENC, the link-layer header begins
3144 * with a 32-bit work containing an AF_ value in
3147 * In addition, if we're reading a saved capture file,
3148 * the host byte order in the capture may not be the
3149 * same as the host byte order on this machine.
3151 * For DLT_LOOP, the link-layer header is a 32-bit
3152 * word containing an AF_ value in *network* byte order.
3154 * XXX - AF_ values may, unfortunately, be platform-
3155 * dependent; for example, FreeBSD's AF_INET6 is 24
3156 * whilst NetBSD's and OpenBSD's is 26.
3158 * This means that, when reading a capture file, just
3159 * checking for our AF_INET6 value won't work if the
3160 * capture file came from another OS.
3169 case ETHERTYPE_IPV6
:
3176 * Not a type on which we support filtering.
3177 * XXX - support those that have AF_ values
3178 * #defined on this platform, at least?
3183 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3185 * The AF_ value is in host byte order, but
3186 * the BPF interpreter will convert it to
3187 * network byte order.
3189 * If this is a save file, and it's from a
3190 * machine with the opposite byte order to
3191 * ours, we byte-swap the AF_ value.
3193 * Then we run it through "htonl()", and
3194 * generate code to compare against the result.
3196 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3197 proto
= SWAPLONG(proto
);
3198 proto
= htonl(proto
);
3200 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3202 #ifdef HAVE_NET_PFVAR_H
3205 * af field is host byte order in contrast to the rest of
3208 if (proto
== ETHERTYPE_IP
)
3209 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3210 BPF_B
, (bpf_int32
)AF_INET
));
3211 else if (proto
== ETHERTYPE_IPV6
)
3212 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3213 BPF_B
, (bpf_int32
)AF_INET6
));
3218 #endif /* HAVE_NET_PFVAR_H */
3221 case DLT_ARCNET_LINUX
:
3223 * XXX should we check for first fragment if the protocol
3231 case ETHERTYPE_IPV6
:
3232 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3233 (bpf_int32
)ARCTYPE_INET6
));
3236 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3237 (bpf_int32
)ARCTYPE_IP
);
3238 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3239 (bpf_int32
)ARCTYPE_IP_OLD
);
3244 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3245 (bpf_int32
)ARCTYPE_ARP
);
3246 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3247 (bpf_int32
)ARCTYPE_ARP_OLD
);
3251 case ETHERTYPE_REVARP
:
3252 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3253 (bpf_int32
)ARCTYPE_REVARP
));
3255 case ETHERTYPE_ATALK
:
3256 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3257 (bpf_int32
)ARCTYPE_ATALK
));
3264 case ETHERTYPE_ATALK
:
3274 * XXX - assumes a 2-byte Frame Relay header with
3275 * DLCI and flags. What if the address is longer?
3281 * Check for the special NLPID for IP.
3283 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3285 case ETHERTYPE_IPV6
:
3287 * Check for the special NLPID for IPv6.
3289 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3293 * Check for several OSI protocols.
3295 * Frame Relay packets typically have an OSI
3296 * NLPID at the beginning; we check for each
3299 * What we check for is the NLPID and a frame
3300 * control field of UI, i.e. 0x03 followed
3303 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3304 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3305 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3317 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3319 case DLT_JUNIPER_MFR
:
3320 case DLT_JUNIPER_MLFR
:
3321 case DLT_JUNIPER_MLPPP
:
3322 case DLT_JUNIPER_ATM1
:
3323 case DLT_JUNIPER_ATM2
:
3324 case DLT_JUNIPER_PPPOE
:
3325 case DLT_JUNIPER_PPPOE_ATM
:
3326 case DLT_JUNIPER_GGSN
:
3327 case DLT_JUNIPER_ES
:
3328 case DLT_JUNIPER_MONITOR
:
3329 case DLT_JUNIPER_SERVICES
:
3330 case DLT_JUNIPER_ETHER
:
3331 case DLT_JUNIPER_PPP
:
3332 case DLT_JUNIPER_FRELAY
:
3333 case DLT_JUNIPER_CHDLC
:
3334 case DLT_JUNIPER_VP
:
3335 case DLT_JUNIPER_ST
:
3336 case DLT_JUNIPER_ISM
:
3337 case DLT_JUNIPER_VS
:
3338 case DLT_JUNIPER_SRX_E2E
:
3339 case DLT_JUNIPER_FIBRECHANNEL
:
3340 case DLT_JUNIPER_ATM_CEMIC
:
3342 /* just lets verify the magic number for now -
3343 * on ATM we may have up to 6 different encapsulations on the wire
3344 * and need a lot of heuristics to figure out that the payload
3347 * FIXME encapsulation specific BPF_ filters
3349 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3351 case DLT_BACNET_MS_TP
:
3352 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3355 return gen_ipnet_linktype(proto
);
3357 case DLT_LINUX_IRDA
:
3358 bpf_error("IrDA link-layer type filtering not implemented");
3361 bpf_error("DOCSIS link-layer type filtering not implemented");
3364 case DLT_MTP2_WITH_PHDR
:
3365 bpf_error("MTP2 link-layer type filtering not implemented");
3368 bpf_error("ERF link-layer type filtering not implemented");
3371 bpf_error("PFSYNC link-layer type filtering not implemented");
3373 case DLT_LINUX_LAPD
:
3374 bpf_error("LAPD link-layer type filtering not implemented");
3378 case DLT_USB_LINUX_MMAPPED
:
3379 bpf_error("USB link-layer type filtering not implemented");
3381 case DLT_BLUETOOTH_HCI_H4
:
3382 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3383 bpf_error("Bluetooth link-layer type filtering not implemented");
3386 case DLT_CAN_SOCKETCAN
:
3387 bpf_error("CAN link-layer type filtering not implemented");
3389 case DLT_IEEE802_15_4
:
3390 case DLT_IEEE802_15_4_LINUX
:
3391 case DLT_IEEE802_15_4_NONASK_PHY
:
3392 case DLT_IEEE802_15_4_NOFCS
:
3393 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3395 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3396 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3399 bpf_error("SITA link-layer type filtering not implemented");
3402 bpf_error("RAIF1 link-layer type filtering not implemented");
3405 bpf_error("IPMB link-layer type filtering not implemented");
3408 bpf_error("AX.25 link-layer type filtering not implemented");
3411 /* Using the fixed-size NFLOG header it is possible to tell only
3412 * the address family of the packet, other meaningful data is
3413 * either missing or behind TLVs.
3415 bpf_error("NFLOG link-layer type filtering not implemented");
3419 * All the types that have no encapsulation should either be
3420 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3421 * all packets are IP packets, or should be handled in some
3422 * special case, if none of them are (if some are and some
3423 * aren't, the lack of encapsulation is a problem, as we'd
3424 * have to find some other way of determining the packet type).
3426 * Therefore, if "off_linktype" is -1, there's an error.
3428 if (off_linktype
== (u_int
)-1)
3432 * Any type not handled above should always have an Ethernet
3433 * type at an offset of "off_linktype".
3435 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3439 * Check for an LLC SNAP packet with a given organization code and
3440 * protocol type; we check the entire contents of the 802.2 LLC and
3441 * snap headers, checking for DSAP and SSAP of SNAP and a control
3442 * field of 0x03 in the LLC header, and for the specified organization
3443 * code and protocol type in the SNAP header.
3445 static struct block
*
3446 gen_snap(orgcode
, ptype
)
3447 bpf_u_int32 orgcode
;
3450 u_char snapblock
[8];
3452 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3453 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3454 snapblock
[2] = 0x03; /* control = UI */
3455 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3456 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3457 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3458 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3459 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3460 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3464 * Generate code to match a particular packet type, for link-layer types
3465 * using 802.2 LLC headers.
3467 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3468 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3470 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3471 * value, if <= ETHERMTU. We use that to determine whether to
3472 * match the DSAP or both DSAP and LSAP or to check the OUI and
3473 * protocol ID in a SNAP header.
3475 static struct block
*
3476 gen_llc_linktype(proto
)
3480 * XXX - handle token-ring variable-length header.
3486 case LLCSAP_NETBEUI
:
3488 * XXX - should we check both the DSAP and the
3489 * SSAP, like this, or should we check just the
3490 * DSAP, as we do for other types <= ETHERMTU
3491 * (i.e., other SAP values)?
3493 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3494 ((proto
<< 8) | proto
));
3498 * XXX - are there ever SNAP frames for IPX on
3499 * non-Ethernet 802.x networks?
3501 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3502 (bpf_int32
)LLCSAP_IPX
);
3504 case ETHERTYPE_ATALK
:
3506 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3507 * SNAP packets with an organization code of
3508 * 0x080007 (Apple, for Appletalk) and a protocol
3509 * type of ETHERTYPE_ATALK (Appletalk).
3511 * XXX - check for an organization code of
3512 * encapsulated Ethernet as well?
3514 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3518 * XXX - we don't have to check for IPX 802.3
3519 * here, but should we check for the IPX Ethertype?
3521 if (proto
<= ETHERMTU
) {
3523 * This is an LLC SAP value, so check
3526 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3529 * This is an Ethernet type; we assume that it's
3530 * unlikely that it'll appear in the right place
3531 * at random, and therefore check only the
3532 * location that would hold the Ethernet type
3533 * in a SNAP frame with an organization code of
3534 * 0x000000 (encapsulated Ethernet).
3536 * XXX - if we were to check for the SNAP DSAP and
3537 * LSAP, as per XXX, and were also to check for an
3538 * organization code of 0x000000 (encapsulated
3539 * Ethernet), we'd do
3541 * return gen_snap(0x000000, proto);
3543 * here; for now, we don't, as per the above.
3544 * I don't know whether it's worth the extra CPU
3545 * time to do the right check or not.
3547 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3552 static struct block
*
3553 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3557 u_int src_off
, dst_off
;
3559 struct block
*b0
, *b1
;
3573 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3574 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3580 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3581 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3588 b0
= gen_linktype(proto
);
3589 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3595 static struct block
*
3596 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3597 struct in6_addr
*addr
;
3598 struct in6_addr
*mask
;
3600 u_int src_off
, dst_off
;
3602 struct block
*b0
, *b1
;
3617 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3618 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3624 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3625 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3632 /* this order is important */
3633 a
= (u_int32_t
*)addr
;
3634 m
= (u_int32_t
*)mask
;
3635 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3636 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3638 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3640 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3642 b0
= gen_linktype(proto
);
3648 static struct block
*
3649 gen_ehostop(eaddr
, dir
)
3650 register const u_char
*eaddr
;
3653 register struct block
*b0
, *b1
;
3657 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3660 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3663 b0
= gen_ehostop(eaddr
, Q_SRC
);
3664 b1
= gen_ehostop(eaddr
, Q_DST
);
3670 b0
= gen_ehostop(eaddr
, Q_SRC
);
3671 b1
= gen_ehostop(eaddr
, Q_DST
);
3676 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3680 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3684 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3688 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3692 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3696 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3704 * Like gen_ehostop, but for DLT_FDDI
3706 static struct block
*
3707 gen_fhostop(eaddr
, dir
)
3708 register const u_char
*eaddr
;
3711 struct block
*b0
, *b1
;
3715 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3718 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3721 b0
= gen_fhostop(eaddr
, Q_SRC
);
3722 b1
= gen_fhostop(eaddr
, Q_DST
);
3728 b0
= gen_fhostop(eaddr
, Q_SRC
);
3729 b1
= gen_fhostop(eaddr
, Q_DST
);
3734 bpf_error("'addr1' is only supported on 802.11");
3738 bpf_error("'addr2' is only supported on 802.11");
3742 bpf_error("'addr3' is only supported on 802.11");
3746 bpf_error("'addr4' is only supported on 802.11");
3750 bpf_error("'ra' is only supported on 802.11");
3754 bpf_error("'ta' is only supported on 802.11");
3762 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3764 static struct block
*
3765 gen_thostop(eaddr
, dir
)
3766 register const u_char
*eaddr
;
3769 register struct block
*b0
, *b1
;
3773 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3776 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3779 b0
= gen_thostop(eaddr
, Q_SRC
);
3780 b1
= gen_thostop(eaddr
, Q_DST
);
3786 b0
= gen_thostop(eaddr
, Q_SRC
);
3787 b1
= gen_thostop(eaddr
, Q_DST
);
3792 bpf_error("'addr1' is only supported on 802.11");
3796 bpf_error("'addr2' is only supported on 802.11");
3800 bpf_error("'addr3' is only supported on 802.11");
3804 bpf_error("'addr4' is only supported on 802.11");
3808 bpf_error("'ra' is only supported on 802.11");
3812 bpf_error("'ta' is only supported on 802.11");
3820 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3821 * various 802.11 + radio headers.
3823 static struct block
*
3824 gen_wlanhostop(eaddr
, dir
)
3825 register const u_char
*eaddr
;
3828 register struct block
*b0
, *b1
, *b2
;
3829 register struct slist
*s
;
3831 #ifdef ENABLE_WLAN_FILTERING_PATCH
3834 * We need to disable the optimizer because the optimizer is buggy
3835 * and wipes out some LD instructions generated by the below
3836 * code to validate the Frame Control bits
3839 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3846 * For control frames, there is no SA.
3848 * For management frames, SA is at an
3849 * offset of 10 from the beginning of
3852 * For data frames, SA is at an offset
3853 * of 10 from the beginning of the packet
3854 * if From DS is clear, at an offset of
3855 * 16 from the beginning of the packet
3856 * if From DS is set and To DS is clear,
3857 * and an offset of 24 from the beginning
3858 * of the packet if From DS is set and To DS
3863 * Generate the tests to be done for data frames
3866 * First, check for To DS set, i.e. check "link[1] & 0x01".
3868 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3869 b1
= new_block(JMP(BPF_JSET
));
3870 b1
->s
.k
= 0x01; /* To DS */
3874 * If To DS is set, the SA is at 24.
3876 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3880 * Now, check for To DS not set, i.e. check
3881 * "!(link[1] & 0x01)".
3883 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3884 b2
= new_block(JMP(BPF_JSET
));
3885 b2
->s
.k
= 0x01; /* To DS */
3890 * If To DS is not set, the SA is at 16.
3892 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3896 * Now OR together the last two checks. That gives
3897 * the complete set of checks for data frames with
3903 * Now check for From DS being set, and AND that with
3904 * the ORed-together checks.
3906 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3907 b1
= new_block(JMP(BPF_JSET
));
3908 b1
->s
.k
= 0x02; /* From DS */
3913 * Now check for data frames with From DS not set.
3915 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3916 b2
= new_block(JMP(BPF_JSET
));
3917 b2
->s
.k
= 0x02; /* From DS */
3922 * If From DS isn't set, the SA is at 10.
3924 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3928 * Now OR together the checks for data frames with
3929 * From DS not set and for data frames with From DS
3930 * set; that gives the checks done for data frames.
3935 * Now check for a data frame.
3936 * I.e, check "link[0] & 0x08".
3938 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3939 b1
= new_block(JMP(BPF_JSET
));
3944 * AND that with the checks done for data frames.
3949 * If the high-order bit of the type value is 0, this
3950 * is a management frame.
3951 * I.e, check "!(link[0] & 0x08)".
3953 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3954 b2
= new_block(JMP(BPF_JSET
));
3960 * For management frames, the SA is at 10.
3962 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3966 * OR that with the checks done for data frames.
3967 * That gives the checks done for management and
3973 * If the low-order bit of the type value is 1,
3974 * this is either a control frame or a frame
3975 * with a reserved type, and thus not a
3978 * I.e., check "!(link[0] & 0x04)".
3980 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3981 b1
= new_block(JMP(BPF_JSET
));
3987 * AND that with the checks for data and management
3997 * For control frames, there is no DA.
3999 * For management frames, DA is at an
4000 * offset of 4 from the beginning of
4003 * For data frames, DA is at an offset
4004 * of 4 from the beginning of the packet
4005 * if To DS is clear and at an offset of
4006 * 16 from the beginning of the packet
4011 * Generate the tests to be done for data frames.
4013 * First, check for To DS set, i.e. "link[1] & 0x01".
4015 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4016 b1
= new_block(JMP(BPF_JSET
));
4017 b1
->s
.k
= 0x01; /* To DS */
4021 * If To DS is set, the DA is at 16.
4023 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4027 * Now, check for To DS not set, i.e. check
4028 * "!(link[1] & 0x01)".
4030 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4031 b2
= new_block(JMP(BPF_JSET
));
4032 b2
->s
.k
= 0x01; /* To DS */
4037 * If To DS is not set, the DA is at 4.
4039 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4043 * Now OR together the last two checks. That gives
4044 * the complete set of checks for data frames.
4049 * Now check for a data frame.
4050 * I.e, check "link[0] & 0x08".
4052 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4053 b1
= new_block(JMP(BPF_JSET
));
4058 * AND that with the checks done for data frames.
4063 * If the high-order bit of the type value is 0, this
4064 * is a management frame.
4065 * I.e, check "!(link[0] & 0x08)".
4067 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4068 b2
= new_block(JMP(BPF_JSET
));
4074 * For management frames, the DA is at 4.
4076 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4080 * OR that with the checks done for data frames.
4081 * That gives the checks done for management and
4087 * If the low-order bit of the type value is 1,
4088 * this is either a control frame or a frame
4089 * with a reserved type, and thus not a
4092 * I.e., check "!(link[0] & 0x04)".
4094 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4095 b1
= new_block(JMP(BPF_JSET
));
4101 * AND that with the checks for data and management
4109 * Not present in management frames; addr1 in other
4114 * If the high-order bit of the type value is 0, this
4115 * is a management frame.
4116 * I.e, check "(link[0] & 0x08)".
4118 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4119 b1
= new_block(JMP(BPF_JSET
));
4126 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4129 * AND that with the check of addr1.
4136 * Not present in management frames; addr2, if present,
4141 * Not present in CTS or ACK control frames.
4143 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4144 IEEE80211_FC0_TYPE_MASK
);
4146 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4147 IEEE80211_FC0_SUBTYPE_MASK
);
4149 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4150 IEEE80211_FC0_SUBTYPE_MASK
);
4156 * If the high-order bit of the type value is 0, this
4157 * is a management frame.
4158 * I.e, check "(link[0] & 0x08)".
4160 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4161 b1
= new_block(JMP(BPF_JSET
));
4166 * AND that with the check for frames other than
4167 * CTS and ACK frames.
4174 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4179 * XXX - add BSSID keyword?
4182 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4186 * Not present in CTS or ACK control frames.
4188 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4189 IEEE80211_FC0_TYPE_MASK
);
4191 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4192 IEEE80211_FC0_SUBTYPE_MASK
);
4194 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4195 IEEE80211_FC0_SUBTYPE_MASK
);
4199 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4205 * Not present in control frames.
4207 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4208 IEEE80211_FC0_TYPE_MASK
);
4210 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4216 * Present only if the direction mask has both "From DS"
4217 * and "To DS" set. Neither control frames nor management
4218 * frames should have both of those set, so we don't
4219 * check the frame type.
4221 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4222 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4223 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4228 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4229 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4235 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4236 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4245 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4246 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4247 * as the RFC states.)
4249 static struct block
*
4250 gen_ipfchostop(eaddr
, dir
)
4251 register const u_char
*eaddr
;
4254 register struct block
*b0
, *b1
;
4258 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4261 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4264 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4265 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4271 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4272 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4277 bpf_error("'addr1' is only supported on 802.11");
4281 bpf_error("'addr2' is only supported on 802.11");
4285 bpf_error("'addr3' is only supported on 802.11");
4289 bpf_error("'addr4' is only supported on 802.11");
4293 bpf_error("'ra' is only supported on 802.11");
4297 bpf_error("'ta' is only supported on 802.11");
4305 * This is quite tricky because there may be pad bytes in front of the
4306 * DECNET header, and then there are two possible data packet formats that
4307 * carry both src and dst addresses, plus 5 packet types in a format that
4308 * carries only the src node, plus 2 types that use a different format and
4309 * also carry just the src node.
4313 * Instead of doing those all right, we just look for data packets with
4314 * 0 or 1 bytes of padding. If you want to look at other packets, that
4315 * will require a lot more hacking.
4317 * To add support for filtering on DECNET "areas" (network numbers)
4318 * one would want to add a "mask" argument to this routine. That would
4319 * make the filter even more inefficient, although one could be clever
4320 * and not generate masking instructions if the mask is 0xFFFF.
4322 static struct block
*
4323 gen_dnhostop(addr
, dir
)
4327 struct block
*b0
, *b1
, *b2
, *tmp
;
4328 u_int offset_lh
; /* offset if long header is received */
4329 u_int offset_sh
; /* offset if short header is received */
4334 offset_sh
= 1; /* follows flags */
4335 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4339 offset_sh
= 3; /* follows flags, dstnode */
4340 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4344 /* Inefficient because we do our Calvinball dance twice */
4345 b0
= gen_dnhostop(addr
, Q_SRC
);
4346 b1
= gen_dnhostop(addr
, Q_DST
);
4352 /* Inefficient because we do our Calvinball dance twice */
4353 b0
= gen_dnhostop(addr
, Q_SRC
);
4354 b1
= gen_dnhostop(addr
, Q_DST
);
4359 bpf_error("ISO host filtering not implemented");
4364 b0
= gen_linktype(ETHERTYPE_DN
);
4365 /* Check for pad = 1, long header case */
4366 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4367 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4368 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4369 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4371 /* Check for pad = 0, long header case */
4372 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4373 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4376 /* Check for pad = 1, short header case */
4377 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4378 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4379 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4382 /* Check for pad = 0, short header case */
4383 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4384 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4388 /* Combine with test for linktype */
4394 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4395 * test the bottom-of-stack bit, and then check the version number
4396 * field in the IP header.
4398 static struct block
*
4399 gen_mpls_linktype(proto
)
4402 struct block
*b0
, *b1
;
4407 /* match the bottom-of-stack bit */
4408 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4409 /* match the IPv4 version number */
4410 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4415 /* match the bottom-of-stack bit */
4416 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4417 /* match the IPv4 version number */
4418 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4427 static struct block
*
4428 gen_host(addr
, mask
, proto
, dir
, type
)
4435 struct block
*b0
, *b1
;
4436 const char *typestr
;
4446 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4448 * Only check for non-IPv4 addresses if we're not
4449 * checking MPLS-encapsulated packets.
4451 if (label_stack_depth
== 0) {
4452 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4454 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4460 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4463 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4466 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4469 bpf_error("'tcp' modifier applied to %s", typestr
);
4472 bpf_error("'sctp' modifier applied to %s", typestr
);
4475 bpf_error("'udp' modifier applied to %s", typestr
);
4478 bpf_error("'icmp' modifier applied to %s", typestr
);
4481 bpf_error("'igmp' modifier applied to %s", typestr
);
4484 bpf_error("'igrp' modifier applied to %s", typestr
);
4487 bpf_error("'pim' modifier applied to %s", typestr
);
4490 bpf_error("'vrrp' modifier applied to %s", typestr
);
4493 bpf_error("'carp' modifier applied to %s", typestr
);
4496 bpf_error("ATALK host filtering not implemented");
4499 bpf_error("AARP host filtering not implemented");
4502 return gen_dnhostop(addr
, dir
);
4505 bpf_error("SCA host filtering not implemented");
4508 bpf_error("LAT host filtering not implemented");
4511 bpf_error("MOPDL host filtering not implemented");
4514 bpf_error("MOPRC host filtering not implemented");
4517 bpf_error("'ip6' modifier applied to ip host");
4520 bpf_error("'icmp6' modifier applied to %s", typestr
);
4523 bpf_error("'ah' modifier applied to %s", typestr
);
4526 bpf_error("'esp' modifier applied to %s", typestr
);
4529 bpf_error("ISO host filtering not implemented");
4532 bpf_error("'esis' modifier applied to %s", typestr
);
4535 bpf_error("'isis' modifier applied to %s", typestr
);
4538 bpf_error("'clnp' modifier applied to %s", typestr
);
4541 bpf_error("'stp' modifier applied to %s", typestr
);
4544 bpf_error("IPX host filtering not implemented");
4547 bpf_error("'netbeui' modifier applied to %s", typestr
);
4550 bpf_error("'radio' modifier applied to %s", typestr
);
4559 static struct block
*
4560 gen_host6(addr
, mask
, proto
, dir
, type
)
4561 struct in6_addr
*addr
;
4562 struct in6_addr
*mask
;
4567 const char *typestr
;
4577 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4580 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4583 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4586 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4589 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4592 bpf_error("'sctp' modifier applied to %s", typestr
);
4595 bpf_error("'tcp' modifier applied to %s", typestr
);
4598 bpf_error("'udp' modifier applied to %s", typestr
);
4601 bpf_error("'icmp' modifier applied to %s", typestr
);
4604 bpf_error("'igmp' modifier applied to %s", typestr
);
4607 bpf_error("'igrp' modifier applied to %s", typestr
);
4610 bpf_error("'pim' modifier applied to %s", typestr
);
4613 bpf_error("'vrrp' modifier applied to %s", typestr
);
4616 bpf_error("'carp' modifier applied to %s", typestr
);
4619 bpf_error("ATALK host filtering not implemented");
4622 bpf_error("AARP host filtering not implemented");
4625 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4628 bpf_error("SCA host filtering not implemented");
4631 bpf_error("LAT host filtering not implemented");
4634 bpf_error("MOPDL host filtering not implemented");
4637 bpf_error("MOPRC host filtering not implemented");
4640 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4643 bpf_error("'icmp6' modifier applied to %s", typestr
);
4646 bpf_error("'ah' modifier applied to %s", typestr
);
4649 bpf_error("'esp' modifier applied to %s", typestr
);
4652 bpf_error("ISO host filtering not implemented");
4655 bpf_error("'esis' modifier applied to %s", typestr
);
4658 bpf_error("'isis' modifier applied to %s", typestr
);
4661 bpf_error("'clnp' modifier applied to %s", typestr
);
4664 bpf_error("'stp' modifier applied to %s", typestr
);
4667 bpf_error("IPX host filtering not implemented");
4670 bpf_error("'netbeui' modifier applied to %s", typestr
);
4673 bpf_error("'radio' modifier applied to %s", typestr
);
4683 static struct block
*
4684 gen_gateway(eaddr
, alist
, proto
, dir
)
4685 const u_char
*eaddr
;
4686 bpf_u_int32
**alist
;
4690 struct block
*b0
, *b1
, *tmp
;
4693 bpf_error("direction applied to 'gateway'");
4702 case DLT_NETANALYZER
:
4703 case DLT_NETANALYZER_TRANSPARENT
:
4704 b0
= gen_ehostop(eaddr
, Q_OR
);
4707 b0
= gen_fhostop(eaddr
, Q_OR
);
4710 b0
= gen_thostop(eaddr
, Q_OR
);
4712 case DLT_IEEE802_11
:
4713 case DLT_PRISM_HEADER
:
4714 case DLT_IEEE802_11_RADIO_AVS
:
4715 case DLT_IEEE802_11_RADIO
:
4717 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4722 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4724 * Check that the packet doesn't begin with an
4725 * LE Control marker. (We've already generated
4728 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4733 * Now check the MAC address.
4735 b0
= gen_ehostop(eaddr
, Q_OR
);
4738 case DLT_IP_OVER_FC
:
4739 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4743 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4745 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4747 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4756 bpf_error("illegal modifier of 'gateway'");
4762 gen_proto_abbrev(proto
)
4771 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4772 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4777 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4778 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4783 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4784 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4789 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4792 #ifndef IPPROTO_IGMP
4793 #define IPPROTO_IGMP 2
4797 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4800 #ifndef IPPROTO_IGRP
4801 #define IPPROTO_IGRP 9
4804 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4808 #define IPPROTO_PIM 103
4812 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4813 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4817 #ifndef IPPROTO_VRRP
4818 #define IPPROTO_VRRP 112
4822 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4825 #ifndef IPPROTO_CARP
4826 #define IPPROTO_CARP 112
4830 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4834 b1
= gen_linktype(ETHERTYPE_IP
);
4838 b1
= gen_linktype(ETHERTYPE_ARP
);
4842 b1
= gen_linktype(ETHERTYPE_REVARP
);
4846 bpf_error("link layer applied in wrong context");
4849 b1
= gen_linktype(ETHERTYPE_ATALK
);
4853 b1
= gen_linktype(ETHERTYPE_AARP
);
4857 b1
= gen_linktype(ETHERTYPE_DN
);
4861 b1
= gen_linktype(ETHERTYPE_SCA
);
4865 b1
= gen_linktype(ETHERTYPE_LAT
);
4869 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4873 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4877 b1
= gen_linktype(ETHERTYPE_IPV6
);
4880 #ifndef IPPROTO_ICMPV6
4881 #define IPPROTO_ICMPV6 58
4884 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4888 #define IPPROTO_AH 51
4891 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4892 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4897 #define IPPROTO_ESP 50
4900 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4901 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4906 b1
= gen_linktype(LLCSAP_ISONS
);
4910 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4914 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4917 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4918 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4919 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4921 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4923 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4925 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4929 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4930 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4931 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4933 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4935 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4937 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4941 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4942 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4943 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4945 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4950 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4951 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4956 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4957 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4959 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4961 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4966 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4967 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4972 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4973 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4978 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4982 b1
= gen_linktype(LLCSAP_8021D
);
4986 b1
= gen_linktype(LLCSAP_IPX
);
4990 b1
= gen_linktype(LLCSAP_NETBEUI
);
4994 bpf_error("'radio' is not a valid protocol type");
5002 static struct block
*
5008 /* not IPv4 frag other than the first frag */
5009 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5010 b
= new_block(JMP(BPF_JSET
));
5019 * Generate a comparison to a port value in the transport-layer header
5020 * at the specified offset from the beginning of that header.
5022 * XXX - this handles a variable-length prefix preceding the link-layer
5023 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5024 * variable-length link-layer headers (such as Token Ring or 802.11
5027 static struct block
*
5028 gen_portatom(off
, v
)
5032 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5035 static struct block
*
5036 gen_portatom6(off
, v
)
5040 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5044 gen_portop(port
, proto
, dir
)
5045 int port
, proto
, dir
;
5047 struct block
*b0
, *b1
, *tmp
;
5049 /* ip proto 'proto' and not a fragment other than the first fragment */
5050 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5056 b1
= gen_portatom(0, (bpf_int32
)port
);
5060 b1
= gen_portatom(2, (bpf_int32
)port
);
5065 tmp
= gen_portatom(0, (bpf_int32
)port
);
5066 b1
= gen_portatom(2, (bpf_int32
)port
);
5071 tmp
= gen_portatom(0, (bpf_int32
)port
);
5072 b1
= gen_portatom(2, (bpf_int32
)port
);
5084 static struct block
*
5085 gen_port(port
, ip_proto
, dir
)
5090 struct block
*b0
, *b1
, *tmp
;
5095 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5096 * not LLC encapsulation with LLCSAP_IP.
5098 * For IEEE 802 networks - which includes 802.5 token ring
5099 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5100 * says that SNAP encapsulation is used, not LLC encapsulation
5103 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5104 * RFC 2225 say that SNAP encapsulation is used, not LLC
5105 * encapsulation with LLCSAP_IP.
5107 * So we always check for ETHERTYPE_IP.
5109 b0
= gen_linktype(ETHERTYPE_IP
);
5115 b1
= gen_portop(port
, ip_proto
, dir
);
5119 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5120 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5122 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5134 gen_portop6(port
, proto
, dir
)
5135 int port
, proto
, dir
;
5137 struct block
*b0
, *b1
, *tmp
;
5139 /* ip6 proto 'proto' */
5140 /* XXX - catch the first fragment of a fragmented packet? */
5141 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5145 b1
= gen_portatom6(0, (bpf_int32
)port
);
5149 b1
= gen_portatom6(2, (bpf_int32
)port
);
5154 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5155 b1
= gen_portatom6(2, (bpf_int32
)port
);
5160 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5161 b1
= gen_portatom6(2, (bpf_int32
)port
);
5173 static struct block
*
5174 gen_port6(port
, ip_proto
, dir
)
5179 struct block
*b0
, *b1
, *tmp
;
5181 /* link proto ip6 */
5182 b0
= gen_linktype(ETHERTYPE_IPV6
);
5188 b1
= gen_portop6(port
, ip_proto
, dir
);
5192 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5193 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5195 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5206 /* gen_portrange code */
5207 static struct block
*
5208 gen_portrangeatom(off
, v1
, v2
)
5212 struct block
*b1
, *b2
;
5216 * Reverse the order of the ports, so v1 is the lower one.
5225 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5226 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5234 gen_portrangeop(port1
, port2
, proto
, dir
)
5239 struct block
*b0
, *b1
, *tmp
;
5241 /* ip proto 'proto' and not a fragment other than the first fragment */
5242 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5248 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5252 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5257 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5258 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5263 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5264 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5276 static struct block
*
5277 gen_portrange(port1
, port2
, ip_proto
, dir
)
5282 struct block
*b0
, *b1
, *tmp
;
5285 b0
= gen_linktype(ETHERTYPE_IP
);
5291 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5295 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5296 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5298 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5309 static struct block
*
5310 gen_portrangeatom6(off
, v1
, v2
)
5314 struct block
*b1
, *b2
;
5318 * Reverse the order of the ports, so v1 is the lower one.
5327 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5328 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5336 gen_portrangeop6(port1
, port2
, proto
, dir
)
5341 struct block
*b0
, *b1
, *tmp
;
5343 /* ip6 proto 'proto' */
5344 /* XXX - catch the first fragment of a fragmented packet? */
5345 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5349 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5353 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5358 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5359 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5364 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5365 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5377 static struct block
*
5378 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5383 struct block
*b0
, *b1
, *tmp
;
5385 /* link proto ip6 */
5386 b0
= gen_linktype(ETHERTYPE_IPV6
);
5392 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5396 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5397 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5399 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5411 lookup_proto(name
, proto
)
5412 register const char *name
;
5422 v
= pcap_nametoproto(name
);
5423 if (v
== PROTO_UNDEF
)
5424 bpf_error("unknown ip proto '%s'", name
);
5428 /* XXX should look up h/w protocol type based on linktype */
5429 v
= pcap_nametoeproto(name
);
5430 if (v
== PROTO_UNDEF
) {
5431 v
= pcap_nametollc(name
);
5432 if (v
== PROTO_UNDEF
)
5433 bpf_error("unknown ether proto '%s'", name
);
5438 if (strcmp(name
, "esis") == 0)
5440 else if (strcmp(name
, "isis") == 0)
5442 else if (strcmp(name
, "clnp") == 0)
5445 bpf_error("unknown osi proto '%s'", name
);
5465 static struct block
*
5466 gen_protochain(v
, proto
, dir
)
5471 #ifdef NO_PROTOCHAIN
5472 return gen_proto(v
, proto
, dir
);
5474 struct block
*b0
, *b
;
5475 struct slist
*s
[100];
5476 int fix2
, fix3
, fix4
, fix5
;
5477 int ahcheck
, again
, end
;
5479 int reg2
= alloc_reg();
5481 memset(s
, 0, sizeof(s
));
5482 fix2
= fix3
= fix4
= fix5
= 0;
5489 b0
= gen_protochain(v
, Q_IP
, dir
);
5490 b
= gen_protochain(v
, Q_IPV6
, dir
);
5494 bpf_error("bad protocol applied for 'protochain'");
5499 * We don't handle variable-length prefixes before the link-layer
5500 * header, or variable-length link-layer headers, here yet.
5501 * We might want to add BPF instructions to do the protochain
5502 * work, to simplify that and, on platforms that have a BPF
5503 * interpreter with the new instructions, let the filtering
5504 * be done in the kernel. (We already require a modified BPF
5505 * engine to do the protochain stuff, to support backward
5506 * branches, and backward branch support is unlikely to appear
5507 * in kernel BPF engines.)
5511 case DLT_IEEE802_11
:
5512 case DLT_PRISM_HEADER
:
5513 case DLT_IEEE802_11_RADIO_AVS
:
5514 case DLT_IEEE802_11_RADIO
:
5516 bpf_error("'protochain' not supported with 802.11");
5519 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5522 * s[0] is a dummy entry to protect other BPF insn from damage
5523 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5524 * hard to find interdependency made by jump table fixup.
5527 s
[i
] = new_stmt(0); /*dummy*/
5532 b0
= gen_linktype(ETHERTYPE_IP
);
5535 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5536 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5538 /* X = ip->ip_hl << 2 */
5539 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5540 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5545 b0
= gen_linktype(ETHERTYPE_IPV6
);
5547 /* A = ip6->ip_nxt */
5548 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5549 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5551 /* X = sizeof(struct ip6_hdr) */
5552 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5558 bpf_error("unsupported proto to gen_protochain");
5562 /* again: if (A == v) goto end; else fall through; */
5564 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5566 s
[i
]->s
.jt
= NULL
; /*later*/
5567 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5571 #ifndef IPPROTO_NONE
5572 #define IPPROTO_NONE 59
5574 /* if (A == IPPROTO_NONE) goto end */
5575 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5576 s
[i
]->s
.jt
= NULL
; /*later*/
5577 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5578 s
[i
]->s
.k
= IPPROTO_NONE
;
5579 s
[fix5
]->s
.jf
= s
[i
];
5583 if (proto
== Q_IPV6
) {
5584 int v6start
, v6end
, v6advance
, j
;
5587 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5588 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5589 s
[i
]->s
.jt
= NULL
; /*later*/
5590 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5591 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5592 s
[fix2
]->s
.jf
= s
[i
];
5594 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5595 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5596 s
[i
]->s
.jt
= NULL
; /*later*/
5597 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5598 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5600 /* if (A == IPPROTO_ROUTING) goto v6advance */
5601 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5602 s
[i
]->s
.jt
= NULL
; /*later*/
5603 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5604 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5606 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5607 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5608 s
[i
]->s
.jt
= NULL
; /*later*/
5609 s
[i
]->s
.jf
= NULL
; /*later*/
5610 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5620 * A = P[X + packet head];
5621 * X = X + (P[X + packet head + 1] + 1) * 8;
5623 /* A = P[X + packet head] */
5624 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5625 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5628 s
[i
] = new_stmt(BPF_ST
);
5631 /* A = P[X + packet head + 1]; */
5632 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5633 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5636 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5640 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5644 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5648 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5651 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5655 /* goto again; (must use BPF_JA for backward jump) */
5656 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5657 s
[i
]->s
.k
= again
- i
- 1;
5658 s
[i
- 1]->s
.jf
= s
[i
];
5662 for (j
= v6start
; j
<= v6end
; j
++)
5663 s
[j
]->s
.jt
= s
[v6advance
];
5666 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5668 s
[fix2
]->s
.jf
= s
[i
];
5674 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5675 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5676 s
[i
]->s
.jt
= NULL
; /*later*/
5677 s
[i
]->s
.jf
= NULL
; /*later*/
5678 s
[i
]->s
.k
= IPPROTO_AH
;
5680 s
[fix3
]->s
.jf
= s
[ahcheck
];
5687 * X = X + (P[X + 1] + 2) * 4;
5690 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5692 /* A = P[X + packet head]; */
5693 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5694 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5697 s
[i
] = new_stmt(BPF_ST
);
5701 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5704 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5708 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5710 /* A = P[X + packet head] */
5711 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5712 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5715 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5719 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5723 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5726 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5730 /* goto again; (must use BPF_JA for backward jump) */
5731 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5732 s
[i
]->s
.k
= again
- i
- 1;
5737 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5739 s
[fix2
]->s
.jt
= s
[end
];
5740 s
[fix4
]->s
.jf
= s
[end
];
5741 s
[fix5
]->s
.jt
= s
[end
];
5748 for (i
= 0; i
< max
- 1; i
++)
5749 s
[i
]->next
= s
[i
+ 1];
5750 s
[max
- 1]->next
= NULL
;
5755 b
= new_block(JMP(BPF_JEQ
));
5756 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5766 static struct block
*
5767 gen_check_802_11_data_frame()
5770 struct block
*b0
, *b1
;
5773 * A data frame has the 0x08 bit (b3) in the frame control field set
5774 * and the 0x04 bit (b2) clear.
5776 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5777 b0
= new_block(JMP(BPF_JSET
));
5781 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5782 b1
= new_block(JMP(BPF_JSET
));
5793 * Generate code that checks whether the packet is a packet for protocol
5794 * <proto> and whether the type field in that protocol's header has
5795 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5796 * IP packet and checks the protocol number in the IP header against <v>.
5798 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5799 * against Q_IP and Q_IPV6.
5801 static struct block
*
5802 gen_proto(v
, proto
, dir
)
5807 struct block
*b0
, *b1
;
5812 if (dir
!= Q_DEFAULT
)
5813 bpf_error("direction applied to 'proto'");
5817 b0
= gen_proto(v
, Q_IP
, dir
);
5818 b1
= gen_proto(v
, Q_IPV6
, dir
);
5824 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5825 * not LLC encapsulation with LLCSAP_IP.
5827 * For IEEE 802 networks - which includes 802.5 token ring
5828 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5829 * says that SNAP encapsulation is used, not LLC encapsulation
5832 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5833 * RFC 2225 say that SNAP encapsulation is used, not LLC
5834 * encapsulation with LLCSAP_IP.
5836 * So we always check for ETHERTYPE_IP.
5838 b0
= gen_linktype(ETHERTYPE_IP
);
5840 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5842 b1
= gen_protochain(v
, Q_IP
);
5852 * Frame Relay packets typically have an OSI
5853 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5854 * generates code to check for all the OSI
5855 * NLPIDs, so calling it and then adding a check
5856 * for the particular NLPID for which we're
5857 * looking is bogus, as we can just check for
5860 * What we check for is the NLPID and a frame
5861 * control field value of UI, i.e. 0x03 followed
5864 * XXX - assumes a 2-byte Frame Relay header with
5865 * DLCI and flags. What if the address is longer?
5867 * XXX - what about SNAP-encapsulated frames?
5869 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5875 * Cisco uses an Ethertype lookalike - for OSI,
5878 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5879 /* OSI in C-HDLC is stuffed with a fudge byte */
5880 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5885 b0
= gen_linktype(LLCSAP_ISONS
);
5886 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5892 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5894 * 4 is the offset of the PDU type relative to the IS-IS
5897 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5902 bpf_error("arp does not encapsulate another protocol");
5906 bpf_error("rarp does not encapsulate another protocol");
5910 bpf_error("atalk encapsulation is not specifiable");
5914 bpf_error("decnet encapsulation is not specifiable");
5918 bpf_error("sca does not encapsulate another protocol");
5922 bpf_error("lat does not encapsulate another protocol");
5926 bpf_error("moprc does not encapsulate another protocol");
5930 bpf_error("mopdl does not encapsulate another protocol");
5934 return gen_linktype(v
);
5937 bpf_error("'udp proto' is bogus");
5941 bpf_error("'tcp proto' is bogus");
5945 bpf_error("'sctp proto' is bogus");
5949 bpf_error("'icmp proto' is bogus");
5953 bpf_error("'igmp proto' is bogus");
5957 bpf_error("'igrp proto' is bogus");
5961 bpf_error("'pim proto' is bogus");
5965 bpf_error("'vrrp proto' is bogus");
5969 bpf_error("'carp proto' is bogus");
5973 b0
= gen_linktype(ETHERTYPE_IPV6
);
5976 * Also check for a fragment header before the final
5979 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
5980 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
5982 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5985 b1
= gen_protochain(v
, Q_IPV6
);
5991 bpf_error("'icmp6 proto' is bogus");
5994 bpf_error("'ah proto' is bogus");
5997 bpf_error("'ah proto' is bogus");
6000 bpf_error("'stp proto' is bogus");
6003 bpf_error("'ipx proto' is bogus");
6006 bpf_error("'netbeui proto' is bogus");
6009 bpf_error("'radio proto' is bogus");
6020 register const char *name
;
6023 int proto
= q
.proto
;
6027 bpf_u_int32 mask
, addr
;
6029 bpf_u_int32
**alist
;
6032 struct sockaddr_in
*sin4
;
6033 struct sockaddr_in6
*sin6
;
6034 struct addrinfo
*res
, *res0
;
6035 struct in6_addr mask128
;
6037 struct block
*b
, *tmp
;
6038 int port
, real_proto
;
6044 addr
= pcap_nametonetaddr(name
);
6046 bpf_error("unknown network '%s'", name
);
6047 /* Left justify network addr and calculate its network mask */
6049 while (addr
&& (addr
& 0xff000000) == 0) {
6053 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6057 if (proto
== Q_LINK
) {
6061 case DLT_NETANALYZER
:
6062 case DLT_NETANALYZER_TRANSPARENT
:
6063 eaddr
= pcap_ether_hostton(name
);
6066 "unknown ether host '%s'", name
);
6067 b
= gen_ehostop(eaddr
, dir
);
6072 eaddr
= pcap_ether_hostton(name
);
6075 "unknown FDDI host '%s'", name
);
6076 b
= gen_fhostop(eaddr
, dir
);
6081 eaddr
= pcap_ether_hostton(name
);
6084 "unknown token ring host '%s'", name
);
6085 b
= gen_thostop(eaddr
, dir
);
6089 case DLT_IEEE802_11
:
6090 case DLT_PRISM_HEADER
:
6091 case DLT_IEEE802_11_RADIO_AVS
:
6092 case DLT_IEEE802_11_RADIO
:
6094 eaddr
= pcap_ether_hostton(name
);
6097 "unknown 802.11 host '%s'", name
);
6098 b
= gen_wlanhostop(eaddr
, dir
);
6102 case DLT_IP_OVER_FC
:
6103 eaddr
= pcap_ether_hostton(name
);
6106 "unknown Fibre Channel host '%s'", name
);
6107 b
= gen_ipfchostop(eaddr
, dir
);
6116 * Check that the packet doesn't begin
6117 * with an LE Control marker. (We've
6118 * already generated a test for LANE.)
6120 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6124 eaddr
= pcap_ether_hostton(name
);
6127 "unknown ether host '%s'", name
);
6128 b
= gen_ehostop(eaddr
, dir
);
6134 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6135 } else if (proto
== Q_DECNET
) {
6136 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6138 * I don't think DECNET hosts can be multihomed, so
6139 * there is no need to build up a list of addresses
6141 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6144 alist
= pcap_nametoaddr(name
);
6145 if (alist
== NULL
|| *alist
== NULL
)
6146 bpf_error("unknown host '%s'", name
);
6148 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6150 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6152 tmp
= gen_host(**alist
++, 0xffffffff,
6153 tproto
, dir
, q
.addr
);
6159 memset(&mask128
, 0xff, sizeof(mask128
));
6160 res0
= res
= pcap_nametoaddrinfo(name
);
6162 bpf_error("unknown host '%s'", name
);
6165 tproto
= tproto6
= proto
;
6166 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6170 for (res
= res0
; res
; res
= res
->ai_next
) {
6171 switch (res
->ai_family
) {
6173 if (tproto
== Q_IPV6
)
6176 sin4
= (struct sockaddr_in
*)
6178 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6179 0xffffffff, tproto
, dir
, q
.addr
);
6182 if (tproto6
== Q_IP
)
6185 sin6
= (struct sockaddr_in6
*)
6187 tmp
= gen_host6(&sin6
->sin6_addr
,
6188 &mask128
, tproto6
, dir
, q
.addr
);
6200 bpf_error("unknown host '%s'%s", name
,
6201 (proto
== Q_DEFAULT
)
6203 : " for specified address family");
6210 if (proto
!= Q_DEFAULT
&&
6211 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6212 bpf_error("illegal qualifier of 'port'");
6213 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6214 bpf_error("unknown port '%s'", name
);
6215 if (proto
== Q_UDP
) {
6216 if (real_proto
== IPPROTO_TCP
)
6217 bpf_error("port '%s' is tcp", name
);
6218 else if (real_proto
== IPPROTO_SCTP
)
6219 bpf_error("port '%s' is sctp", name
);
6221 /* override PROTO_UNDEF */
6222 real_proto
= IPPROTO_UDP
;
6224 if (proto
== Q_TCP
) {
6225 if (real_proto
== IPPROTO_UDP
)
6226 bpf_error("port '%s' is udp", name
);
6228 else if (real_proto
== IPPROTO_SCTP
)
6229 bpf_error("port '%s' is sctp", name
);
6231 /* override PROTO_UNDEF */
6232 real_proto
= IPPROTO_TCP
;
6234 if (proto
== Q_SCTP
) {
6235 if (real_proto
== IPPROTO_UDP
)
6236 bpf_error("port '%s' is udp", name
);
6238 else if (real_proto
== IPPROTO_TCP
)
6239 bpf_error("port '%s' is tcp", name
);
6241 /* override PROTO_UNDEF */
6242 real_proto
= IPPROTO_SCTP
;
6245 bpf_error("illegal port number %d < 0", port
);
6247 bpf_error("illegal port number %d > 65535", port
);
6248 b
= gen_port(port
, real_proto
, dir
);
6249 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6253 if (proto
!= Q_DEFAULT
&&
6254 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6255 bpf_error("illegal qualifier of 'portrange'");
6256 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6257 bpf_error("unknown port in range '%s'", name
);
6258 if (proto
== Q_UDP
) {
6259 if (real_proto
== IPPROTO_TCP
)
6260 bpf_error("port in range '%s' is tcp", name
);
6261 else if (real_proto
== IPPROTO_SCTP
)
6262 bpf_error("port in range '%s' is sctp", name
);
6264 /* override PROTO_UNDEF */
6265 real_proto
= IPPROTO_UDP
;
6267 if (proto
== Q_TCP
) {
6268 if (real_proto
== IPPROTO_UDP
)
6269 bpf_error("port in range '%s' is udp", name
);
6270 else if (real_proto
== IPPROTO_SCTP
)
6271 bpf_error("port in range '%s' is sctp", name
);
6273 /* override PROTO_UNDEF */
6274 real_proto
= IPPROTO_TCP
;
6276 if (proto
== Q_SCTP
) {
6277 if (real_proto
== IPPROTO_UDP
)
6278 bpf_error("port in range '%s' is udp", name
);
6279 else if (real_proto
== IPPROTO_TCP
)
6280 bpf_error("port in range '%s' is tcp", name
);
6282 /* override PROTO_UNDEF */
6283 real_proto
= IPPROTO_SCTP
;
6286 bpf_error("illegal port number %d < 0", port1
);
6288 bpf_error("illegal port number %d > 65535", port1
);
6290 bpf_error("illegal port number %d < 0", port2
);
6292 bpf_error("illegal port number %d > 65535", port2
);
6294 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6295 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6300 eaddr
= pcap_ether_hostton(name
);
6302 bpf_error("unknown ether host: %s", name
);
6304 alist
= pcap_nametoaddr(name
);
6305 if (alist
== NULL
|| *alist
== NULL
)
6306 bpf_error("unknown host '%s'", name
);
6307 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6311 bpf_error("'gateway' not supported in this configuration");
6315 real_proto
= lookup_proto(name
, proto
);
6316 if (real_proto
>= 0)
6317 return gen_proto(real_proto
, proto
, dir
);
6319 bpf_error("unknown protocol: %s", name
);
6322 real_proto
= lookup_proto(name
, proto
);
6323 if (real_proto
>= 0)
6324 return gen_protochain(real_proto
, proto
, dir
);
6326 bpf_error("unknown protocol: %s", name
);
6337 gen_mcode(s1
, s2
, masklen
, q
)
6338 register const char *s1
, *s2
;
6339 register int masklen
;
6342 register int nlen
, mlen
;
6345 nlen
= __pcap_atoin(s1
, &n
);
6346 /* Promote short ipaddr */
6350 mlen
= __pcap_atoin(s2
, &m
);
6351 /* Promote short ipaddr */
6354 bpf_error("non-network bits set in \"%s mask %s\"",
6357 /* Convert mask len to mask */
6359 bpf_error("mask length must be <= 32");
6362 * X << 32 is not guaranteed by C to be 0; it's
6367 m
= 0xffffffff << (32 - masklen
);
6369 bpf_error("non-network bits set in \"%s/%d\"",
6376 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6379 bpf_error("Mask syntax for networks only");
6388 register const char *s
;
6393 int proto
= q
.proto
;
6399 else if (q
.proto
== Q_DECNET
)
6400 vlen
= __pcap_atodn(s
, &v
);
6402 vlen
= __pcap_atoin(s
, &v
);
6409 if (proto
== Q_DECNET
)
6410 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6411 else if (proto
== Q_LINK
) {
6412 bpf_error("illegal link layer address");
6415 if (s
== NULL
&& q
.addr
== Q_NET
) {
6416 /* Promote short net number */
6417 while (v
&& (v
& 0xff000000) == 0) {
6422 /* Promote short ipaddr */
6426 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6431 proto
= IPPROTO_UDP
;
6432 else if (proto
== Q_TCP
)
6433 proto
= IPPROTO_TCP
;
6434 else if (proto
== Q_SCTP
)
6435 proto
= IPPROTO_SCTP
;
6436 else if (proto
== Q_DEFAULT
)
6437 proto
= PROTO_UNDEF
;
6439 bpf_error("illegal qualifier of 'port'");
6442 bpf_error("illegal port number %u > 65535", v
);
6446 b
= gen_port((int)v
, proto
, dir
);
6447 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6453 proto
= IPPROTO_UDP
;
6454 else if (proto
== Q_TCP
)
6455 proto
= IPPROTO_TCP
;
6456 else if (proto
== Q_SCTP
)
6457 proto
= IPPROTO_SCTP
;
6458 else if (proto
== Q_DEFAULT
)
6459 proto
= PROTO_UNDEF
;
6461 bpf_error("illegal qualifier of 'portrange'");
6464 bpf_error("illegal port number %u > 65535", v
);
6468 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6469 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6474 bpf_error("'gateway' requires a name");
6478 return gen_proto((int)v
, proto
, dir
);
6481 return gen_protochain((int)v
, proto
, dir
);
6496 gen_mcode6(s1
, s2
, masklen
, q
)
6497 register const char *s1
, *s2
;
6498 register int masklen
;
6501 struct addrinfo
*res
;
6502 struct in6_addr
*addr
;
6503 struct in6_addr mask
;
6508 bpf_error("no mask %s supported", s2
);
6510 res
= pcap_nametoaddrinfo(s1
);
6512 bpf_error("invalid ip6 address %s", s1
);
6515 bpf_error("%s resolved to multiple address", s1
);
6516 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6518 if (sizeof(mask
) * 8 < masklen
)
6519 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6520 memset(&mask
, 0, sizeof(mask
));
6521 memset(&mask
, 0xff, masklen
/ 8);
6523 mask
.s6_addr
[masklen
/ 8] =
6524 (0xff << (8 - masklen
% 8)) & 0xff;
6527 a
= (u_int32_t
*)addr
;
6528 m
= (u_int32_t
*)&mask
;
6529 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6530 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6531 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6539 bpf_error("Mask syntax for networks only");
6543 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6549 bpf_error("invalid qualifier against IPv6 address");
6558 register const u_char
*eaddr
;
6561 struct block
*b
, *tmp
;
6563 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6566 case DLT_NETANALYZER
:
6567 case DLT_NETANALYZER_TRANSPARENT
:
6568 return gen_ehostop(eaddr
, (int)q
.dir
);
6570 return gen_fhostop(eaddr
, (int)q
.dir
);
6572 return gen_thostop(eaddr
, (int)q
.dir
);
6573 case DLT_IEEE802_11
:
6574 case DLT_PRISM_HEADER
:
6575 case DLT_IEEE802_11_RADIO_AVS
:
6576 case DLT_IEEE802_11_RADIO
:
6578 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6582 * Check that the packet doesn't begin with an
6583 * LE Control marker. (We've already generated
6586 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6591 * Now check the MAC address.
6593 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6598 case DLT_IP_OVER_FC
:
6599 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6601 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6605 bpf_error("ethernet address used in non-ether expression");
6612 struct slist
*s0
, *s1
;
6615 * This is definitely not the best way to do this, but the
6616 * lists will rarely get long.
6623 static struct slist
*
6629 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6634 static struct slist
*
6640 s
= new_stmt(BPF_LD
|BPF_MEM
);
6646 * Modify "index" to use the value stored into its register as an
6647 * offset relative to the beginning of the header for the protocol
6648 * "proto", and allocate a register and put an item "size" bytes long
6649 * (1, 2, or 4) at that offset into that register, making it the register
6653 gen_load(proto
, inst
, size
)
6658 struct slist
*s
, *tmp
;
6660 int regno
= alloc_reg();
6662 free_reg(inst
->regno
);
6666 bpf_error("data size must be 1, 2, or 4");
6682 bpf_error("unsupported index operation");
6686 * The offset is relative to the beginning of the packet
6687 * data, if we have a radio header. (If we don't, this
6690 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6691 linktype
!= DLT_IEEE802_11_RADIO
&&
6692 linktype
!= DLT_PRISM_HEADER
)
6693 bpf_error("radio information not present in capture");
6696 * Load into the X register the offset computed into the
6697 * register specified by "index".
6699 s
= xfer_to_x(inst
);
6702 * Load the item at that offset.
6704 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6706 sappend(inst
->s
, s
);
6711 * The offset is relative to the beginning of
6712 * the link-layer header.
6714 * XXX - what about ATM LANE? Should the index be
6715 * relative to the beginning of the AAL5 frame, so
6716 * that 0 refers to the beginning of the LE Control
6717 * field, or relative to the beginning of the LAN
6718 * frame, so that 0 refers, for Ethernet LANE, to
6719 * the beginning of the destination address?
6721 s
= gen_llprefixlen();
6724 * If "s" is non-null, it has code to arrange that the
6725 * X register contains the length of the prefix preceding
6726 * the link-layer header. Add to it the offset computed
6727 * into the register specified by "index", and move that
6728 * into the X register. Otherwise, just load into the X
6729 * register the offset computed into the register specified
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 and the offset of the start of the link
6742 * layer header (which is 0 if the radio header is
6743 * variable-length; that header length is what we put
6744 * into the X register and then added to the index).
6746 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6749 sappend(inst
->s
, s
);
6763 * The offset is relative to the beginning of
6764 * the network-layer header.
6765 * XXX - are there any cases where we want
6768 s
= gen_off_macpl();
6771 * If "s" is non-null, it has code to arrange that the
6772 * X register contains the offset of the MAC-layer
6773 * payload. Add to it the offset computed into the
6774 * register specified by "index", and move that into
6775 * the X register. Otherwise, just load into the X
6776 * register the offset computed into the register specified
6780 sappend(s
, xfer_to_a(inst
));
6781 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6782 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6784 s
= xfer_to_x(inst
);
6787 * Load the item at the sum of the offset we've put in the
6788 * X register, the offset of the start of the network
6789 * layer header from the beginning of the MAC-layer
6790 * payload, and the purported offset of the start of the
6791 * MAC-layer payload (which might be 0 if there's a
6792 * variable-length prefix before the link-layer header
6793 * or the link-layer header itself is variable-length;
6794 * the variable-length offset of the start of the
6795 * MAC-layer payload is what we put into the X register
6796 * and then added to the index).
6798 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6799 tmp
->s
.k
= off_macpl
+ off_nl
;
6801 sappend(inst
->s
, s
);
6804 * Do the computation only if the packet contains
6805 * the protocol in question.
6807 b
= gen_proto_abbrev(proto
);
6809 gen_and(inst
->b
, b
);
6823 * The offset is relative to the beginning of
6824 * the transport-layer header.
6826 * Load the X register with the length of the IPv4 header
6827 * (plus the offset of the link-layer header, if it's
6828 * a variable-length header), in bytes.
6830 * XXX - are there any cases where we want
6832 * XXX - we should, if we're built with
6833 * IPv6 support, generate code to load either
6834 * IPv4, IPv6, or both, as appropriate.
6836 s
= gen_loadx_iphdrlen();
6839 * The X register now contains the sum of the length
6840 * of any variable-length header preceding the link-layer
6841 * header, any variable-length link-layer header, and the
6842 * length of the network-layer header.
6844 * Load into the A register the offset relative to
6845 * the beginning of the transport layer header,
6846 * add the X register to that, move that to the
6847 * X register, and load with an offset from the
6848 * X register equal to the offset of the network
6849 * layer header relative to the beginning of
6850 * the MAC-layer payload plus the fixed-length
6851 * portion of the offset of the MAC-layer payload
6852 * from the beginning of the raw packet data.
6854 sappend(s
, xfer_to_a(inst
));
6855 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6856 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6857 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6858 tmp
->s
.k
= off_macpl
+ off_nl
;
6859 sappend(inst
->s
, s
);
6862 * Do the computation only if the packet contains
6863 * the protocol in question - which is true only
6864 * if this is an IP datagram and is the first or
6865 * only fragment of that datagram.
6867 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6869 gen_and(inst
->b
, b
);
6870 gen_and(gen_proto_abbrev(Q_IP
), b
);
6874 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6877 inst
->regno
= regno
;
6878 s
= new_stmt(BPF_ST
);
6880 sappend(inst
->s
, s
);
6886 gen_relation(code
, a0
, a1
, reversed
)
6888 struct arth
*a0
, *a1
;
6891 struct slist
*s0
, *s1
, *s2
;
6892 struct block
*b
, *tmp
;
6896 if (code
== BPF_JEQ
) {
6897 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6898 b
= new_block(JMP(code
));
6902 b
= new_block(BPF_JMP
|code
|BPF_X
);
6908 sappend(a0
->s
, a1
->s
);
6912 free_reg(a0
->regno
);
6913 free_reg(a1
->regno
);
6915 /* 'and' together protocol checks */
6918 gen_and(a0
->b
, tmp
= a1
->b
);
6934 int regno
= alloc_reg();
6935 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6938 s
= new_stmt(BPF_LD
|BPF_LEN
);
6939 s
->next
= new_stmt(BPF_ST
);
6940 s
->next
->s
.k
= regno
;
6955 a
= (struct arth
*)newchunk(sizeof(*a
));
6959 s
= new_stmt(BPF_LD
|BPF_IMM
);
6961 s
->next
= new_stmt(BPF_ST
);
6977 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6980 s
= new_stmt(BPF_ST
);
6988 gen_arth(code
, a0
, a1
)
6990 struct arth
*a0
, *a1
;
6992 struct slist
*s0
, *s1
, *s2
;
6996 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7001 sappend(a0
->s
, a1
->s
);
7003 free_reg(a0
->regno
);
7004 free_reg(a1
->regno
);
7006 s0
= new_stmt(BPF_ST
);
7007 a0
->regno
= s0
->s
.k
= alloc_reg();
7014 * Here we handle simple allocation of the scratch registers.
7015 * If too many registers are alloc'd, the allocator punts.
7017 static int regused
[BPF_MEMWORDS
];
7021 * Initialize the table of used registers and the current register.
7027 memset(regused
, 0, sizeof regused
);
7031 * Return the next free register.
7036 int n
= BPF_MEMWORDS
;
7039 if (regused
[curreg
])
7040 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7042 regused
[curreg
] = 1;
7046 bpf_error("too many registers needed to evaluate expression");
7052 * Return a register to the table so it can
7062 static struct block
*
7069 s
= new_stmt(BPF_LD
|BPF_LEN
);
7070 b
= new_block(JMP(jmp
));
7081 return gen_len(BPF_JGE
, n
);
7085 * Actually, this is less than or equal.
7093 b
= gen_len(BPF_JGT
, n
);
7100 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7101 * the beginning of the link-layer header.
7102 * XXX - that means you can't test values in the radiotap header, but
7103 * as that header is difficult if not impossible to parse generally
7104 * without a loop, that might not be a severe problem. A new keyword
7105 * "radio" could be added for that, although what you'd really want
7106 * would be a way of testing particular radio header values, which
7107 * would generate code appropriate to the radio header in question.
7110 gen_byteop(op
, idx
, val
)
7121 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7124 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7128 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7132 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7136 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7140 b
= new_block(JMP(BPF_JEQ
));
7147 static u_char abroadcast
[] = { 0x0 };
7150 gen_broadcast(proto
)
7153 bpf_u_int32 hostmask
;
7154 struct block
*b0
, *b1
, *b2
;
7155 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7163 case DLT_ARCNET_LINUX
:
7164 return gen_ahostop(abroadcast
, Q_DST
);
7166 case DLT_NETANALYZER
:
7167 case DLT_NETANALYZER_TRANSPARENT
:
7168 return gen_ehostop(ebroadcast
, Q_DST
);
7170 return gen_fhostop(ebroadcast
, Q_DST
);
7172 return gen_thostop(ebroadcast
, Q_DST
);
7173 case DLT_IEEE802_11
:
7174 case DLT_PRISM_HEADER
:
7175 case DLT_IEEE802_11_RADIO_AVS
:
7176 case DLT_IEEE802_11_RADIO
:
7178 return gen_wlanhostop(ebroadcast
, Q_DST
);
7179 case DLT_IP_OVER_FC
:
7180 return gen_ipfchostop(ebroadcast
, Q_DST
);
7184 * Check that the packet doesn't begin with an
7185 * LE Control marker. (We've already generated
7188 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7193 * Now check the MAC address.
7195 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7201 bpf_error("not a broadcast link");
7207 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7208 * as an indication that we don't know the netmask, and fail
7211 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7212 bpf_error("netmask not known, so 'ip broadcast' not supported");
7213 b0
= gen_linktype(ETHERTYPE_IP
);
7214 hostmask
= ~netmask
;
7215 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7216 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7217 (bpf_int32
)(~0 & hostmask
), hostmask
);
7222 bpf_error("only link-layer/IP broadcast filters supported");
7228 * Generate code to test the low-order bit of a MAC address (that's
7229 * the bottom bit of the *first* byte).
7231 static struct block
*
7232 gen_mac_multicast(offset
)
7235 register struct block
*b0
;
7236 register struct slist
*s
;
7238 /* link[offset] & 1 != 0 */
7239 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7240 b0
= new_block(JMP(BPF_JSET
));
7247 gen_multicast(proto
)
7250 register struct block
*b0
, *b1
, *b2
;
7251 register struct slist
*s
;
7259 case DLT_ARCNET_LINUX
:
7260 /* all ARCnet multicasts use the same address */
7261 return gen_ahostop(abroadcast
, Q_DST
);
7263 case DLT_NETANALYZER
:
7264 case DLT_NETANALYZER_TRANSPARENT
:
7265 /* ether[0] & 1 != 0 */
7266 return gen_mac_multicast(0);
7269 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7271 * XXX - was that referring to bit-order issues?
7273 /* fddi[1] & 1 != 0 */
7274 return gen_mac_multicast(1);
7276 /* tr[2] & 1 != 0 */
7277 return gen_mac_multicast(2);
7278 case DLT_IEEE802_11
:
7279 case DLT_PRISM_HEADER
:
7280 case DLT_IEEE802_11_RADIO_AVS
:
7281 case DLT_IEEE802_11_RADIO
:
7286 * For control frames, there is no DA.
7288 * For management frames, DA is at an
7289 * offset of 4 from the beginning of
7292 * For data frames, DA is at an offset
7293 * of 4 from the beginning of the packet
7294 * if To DS is clear and at an offset of
7295 * 16 from the beginning of the packet
7300 * Generate the tests to be done for data frames.
7302 * First, check for To DS set, i.e. "link[1] & 0x01".
7304 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7305 b1
= new_block(JMP(BPF_JSET
));
7306 b1
->s
.k
= 0x01; /* To DS */
7310 * If To DS is set, the DA is at 16.
7312 b0
= gen_mac_multicast(16);
7316 * Now, check for To DS not set, i.e. check
7317 * "!(link[1] & 0x01)".
7319 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7320 b2
= new_block(JMP(BPF_JSET
));
7321 b2
->s
.k
= 0x01; /* To DS */
7326 * If To DS is not set, the DA is at 4.
7328 b1
= gen_mac_multicast(4);
7332 * Now OR together the last two checks. That gives
7333 * the complete set of checks for data frames.
7338 * Now check for a data frame.
7339 * I.e, check "link[0] & 0x08".
7341 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7342 b1
= new_block(JMP(BPF_JSET
));
7347 * AND that with the checks done for data frames.
7352 * If the high-order bit of the type value is 0, this
7353 * is a management frame.
7354 * I.e, check "!(link[0] & 0x08)".
7356 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7357 b2
= new_block(JMP(BPF_JSET
));
7363 * For management frames, the DA is at 4.
7365 b1
= gen_mac_multicast(4);
7369 * OR that with the checks done for data frames.
7370 * That gives the checks done for management and
7376 * If the low-order bit of the type value is 1,
7377 * this is either a control frame or a frame
7378 * with a reserved type, and thus not a
7381 * I.e., check "!(link[0] & 0x04)".
7383 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7384 b1
= new_block(JMP(BPF_JSET
));
7390 * AND that with the checks for data and management
7395 case DLT_IP_OVER_FC
:
7396 b0
= gen_mac_multicast(2);
7401 * Check that the packet doesn't begin with an
7402 * LE Control marker. (We've already generated
7405 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7409 /* ether[off_mac] & 1 != 0 */
7410 b0
= gen_mac_multicast(off_mac
);
7418 /* Link not known to support multicasts */
7422 b0
= gen_linktype(ETHERTYPE_IP
);
7423 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7428 b0
= gen_linktype(ETHERTYPE_IPV6
);
7429 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7433 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7439 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7440 * Outbound traffic is sent by this machine, while inbound traffic is
7441 * sent by a remote machine (and may include packets destined for a
7442 * unicast or multicast link-layer address we are not subscribing to).
7443 * These are the same definitions implemented by pcap_setdirection().
7444 * Capturing only unicast traffic destined for this host is probably
7445 * better accomplished using a higher-layer filter.
7451 register struct block
*b0
;
7454 * Only some data link types support inbound/outbound qualifiers.
7458 b0
= gen_relation(BPF_JEQ
,
7459 gen_load(Q_LINK
, gen_loadi(0), 1),
7466 /* match outgoing packets */
7467 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7469 /* match incoming packets */
7470 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7475 /* match outgoing packets */
7476 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7478 /* to filter on inbound traffic, invert the match */
7483 #ifdef HAVE_NET_PFVAR_H
7485 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7486 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7492 /* match outgoing packets */
7493 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7495 /* match incoming packets */
7496 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7500 case DLT_JUNIPER_MFR
:
7501 case DLT_JUNIPER_MLFR
:
7502 case DLT_JUNIPER_MLPPP
:
7503 case DLT_JUNIPER_ATM1
:
7504 case DLT_JUNIPER_ATM2
:
7505 case DLT_JUNIPER_PPPOE
:
7506 case DLT_JUNIPER_PPPOE_ATM
:
7507 case DLT_JUNIPER_GGSN
:
7508 case DLT_JUNIPER_ES
:
7509 case DLT_JUNIPER_MONITOR
:
7510 case DLT_JUNIPER_SERVICES
:
7511 case DLT_JUNIPER_ETHER
:
7512 case DLT_JUNIPER_PPP
:
7513 case DLT_JUNIPER_FRELAY
:
7514 case DLT_JUNIPER_CHDLC
:
7515 case DLT_JUNIPER_VP
:
7516 case DLT_JUNIPER_ST
:
7517 case DLT_JUNIPER_ISM
:
7518 case DLT_JUNIPER_VS
:
7519 case DLT_JUNIPER_SRX_E2E
:
7520 case DLT_JUNIPER_FIBRECHANNEL
:
7521 case DLT_JUNIPER_ATM_CEMIC
:
7523 /* juniper flags (including direction) are stored
7524 * the byte after the 3-byte magic number */
7526 /* match outgoing packets */
7527 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7529 /* match incoming packets */
7530 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7536 * If we have packet meta-data indicating a direction,
7537 * check it, otherwise give up as this link-layer type
7538 * has nothing in the packet data.
7540 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7542 * This is Linux with PF_PACKET support.
7543 * If this is a *live* capture, we can look at
7544 * special meta-data in the filter expression;
7545 * if it's a savefile, we can't.
7547 if (bpf_pcap
->rfile
!= NULL
) {
7548 /* We have a FILE *, so this is a savefile */
7549 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7554 /* match outgoing packets */
7555 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7558 /* to filter on inbound traffic, invert the match */
7561 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7562 bpf_error("inbound/outbound not supported on linktype %d",
7566 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7571 #ifdef HAVE_NET_PFVAR_H
7572 /* PF firewall log matched interface */
7574 gen_pf_ifname(const char *ifname
)
7579 if (linktype
!= DLT_PFLOG
) {
7580 bpf_error("ifname supported only on PF linktype");
7583 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7584 off
= offsetof(struct pfloghdr
, ifname
);
7585 if (strlen(ifname
) >= len
) {
7586 bpf_error("ifname interface names can only be %d characters",
7590 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7594 /* PF firewall log ruleset name */
7596 gen_pf_ruleset(char *ruleset
)
7600 if (linktype
!= DLT_PFLOG
) {
7601 bpf_error("ruleset supported only on PF linktype");
7605 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7606 bpf_error("ruleset names can only be %ld characters",
7607 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7611 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7612 strlen(ruleset
), (const u_char
*)ruleset
);
7616 /* PF firewall log rule number */
7622 if (linktype
!= DLT_PFLOG
) {
7623 bpf_error("rnr supported only on PF linktype");
7627 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7632 /* PF firewall log sub-rule number */
7634 gen_pf_srnr(int srnr
)
7638 if (linktype
!= DLT_PFLOG
) {
7639 bpf_error("srnr supported only on PF linktype");
7643 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7648 /* PF firewall log reason code */
7650 gen_pf_reason(int reason
)
7654 if (linktype
!= DLT_PFLOG
) {
7655 bpf_error("reason supported only on PF linktype");
7659 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7664 /* PF firewall log action */
7666 gen_pf_action(int action
)
7670 if (linktype
!= DLT_PFLOG
) {
7671 bpf_error("action supported only on PF linktype");
7675 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7679 #else /* !HAVE_NET_PFVAR_H */
7681 gen_pf_ifname(const char *ifname
)
7683 bpf_error("libpcap was compiled without pf support");
7689 gen_pf_ruleset(char *ruleset
)
7691 bpf_error("libpcap was compiled on a machine without pf support");
7699 bpf_error("libpcap was compiled on a machine without pf support");
7705 gen_pf_srnr(int srnr
)
7707 bpf_error("libpcap was compiled on a machine without pf support");
7713 gen_pf_reason(int reason
)
7715 bpf_error("libpcap was compiled on a machine without pf support");
7721 gen_pf_action(int action
)
7723 bpf_error("libpcap was compiled on a machine without pf support");
7727 #endif /* HAVE_NET_PFVAR_H */
7729 /* IEEE 802.11 wireless header */
7731 gen_p80211_type(int type
, int mask
)
7737 case DLT_IEEE802_11
:
7738 case DLT_PRISM_HEADER
:
7739 case DLT_IEEE802_11_RADIO_AVS
:
7740 case DLT_IEEE802_11_RADIO
:
7741 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7746 bpf_error("802.11 link-layer types supported only on 802.11");
7754 gen_p80211_fcdir(int fcdir
)
7760 case DLT_IEEE802_11
:
7761 case DLT_PRISM_HEADER
:
7762 case DLT_IEEE802_11_RADIO_AVS
:
7763 case DLT_IEEE802_11_RADIO
:
7767 bpf_error("frame direction supported only with 802.11 headers");
7771 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7772 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7779 register const u_char
*eaddr
;
7785 case DLT_ARCNET_LINUX
:
7786 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7788 return (gen_ahostop(eaddr
, (int)q
.dir
));
7790 bpf_error("ARCnet address used in non-arc expression");
7796 bpf_error("aid supported only on ARCnet");
7799 bpf_error("ARCnet address used in non-arc expression");
7804 static struct block
*
7805 gen_ahostop(eaddr
, dir
)
7806 register const u_char
*eaddr
;
7809 register struct block
*b0
, *b1
;
7812 /* src comes first, different from Ethernet */
7814 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7817 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7820 b0
= gen_ahostop(eaddr
, Q_SRC
);
7821 b1
= gen_ahostop(eaddr
, Q_DST
);
7827 b0
= gen_ahostop(eaddr
, Q_SRC
);
7828 b1
= gen_ahostop(eaddr
, Q_DST
);
7833 bpf_error("'addr1' is only supported on 802.11");
7837 bpf_error("'addr2' is only supported on 802.11");
7841 bpf_error("'addr3' is only supported on 802.11");
7845 bpf_error("'addr4' is only supported on 802.11");
7849 bpf_error("'ra' is only supported on 802.11");
7853 bpf_error("'ta' is only supported on 802.11");
7861 * support IEEE 802.1Q VLAN trunk over ethernet
7867 struct block
*b0
, *b1
;
7869 /* can't check for VLAN-encapsulated packets inside MPLS */
7870 if (label_stack_depth
> 0)
7871 bpf_error("no VLAN match after MPLS");
7874 * Check for a VLAN packet, and then change the offsets to point
7875 * to the type and data fields within the VLAN packet. Just
7876 * increment the offsets, so that we can support a hierarchy, e.g.
7877 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7880 * XXX - this is a bit of a kludge. If we were to split the
7881 * compiler into a parser that parses an expression and
7882 * generates an expression tree, and a code generator that
7883 * takes an expression tree (which could come from our
7884 * parser or from some other parser) and generates BPF code,
7885 * we could perhaps make the offsets parameters of routines
7886 * and, in the handler for an "AND" node, pass to subnodes
7887 * other than the VLAN node the adjusted offsets.
7889 * This would mean that "vlan" would, instead of changing the
7890 * behavior of *all* tests after it, change only the behavior
7891 * of tests ANDed with it. That would change the documented
7892 * semantics of "vlan", which might break some expressions.
7893 * However, it would mean that "(vlan and ip) or ip" would check
7894 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7895 * checking only for VLAN-encapsulated IP, so that could still
7896 * be considered worth doing; it wouldn't break expressions
7897 * that are of the form "vlan and ..." or "vlan N and ...",
7898 * which I suspect are the most common expressions involving
7899 * "vlan". "vlan or ..." doesn't necessarily do what the user
7900 * would really want, now, as all the "or ..." tests would
7901 * be done assuming a VLAN, even though the "or" could be viewed
7902 * as meaning "or, if this isn't a VLAN packet...".
7909 case DLT_NETANALYZER
:
7910 case DLT_NETANALYZER_TRANSPARENT
:
7911 /* check for VLAN, including QinQ */
7912 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7913 (bpf_int32
)ETHERTYPE_8021Q
);
7914 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7915 (bpf_int32
)ETHERTYPE_8021QINQ
);
7919 /* If a specific VLAN is requested, check VLAN id */
7920 if (vlan_num
>= 0) {
7921 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7922 (bpf_int32
)vlan_num
, 0x0fff);
7936 bpf_error("no VLAN support for data link type %d",
7951 struct block
*b0
,*b1
;
7954 * Change the offsets to point to the type and data fields within
7955 * the MPLS packet. Just increment the offsets, so that we
7956 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7957 * capture packets with an outer label of 100000 and an inner
7960 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7964 if (label_stack_depth
> 0) {
7965 /* just match the bottom-of-stack bit clear */
7966 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7969 * Indicate that we're checking MPLS-encapsulated headers,
7970 * to make sure higher level code generators don't try to
7971 * match against IP-related protocols such as Q_ARP, Q_RARP
7976 case DLT_C_HDLC
: /* fall through */
7978 case DLT_NETANALYZER
:
7979 case DLT_NETANALYZER_TRANSPARENT
:
7980 b0
= gen_linktype(ETHERTYPE_MPLS
);
7984 b0
= gen_linktype(PPP_MPLS_UCAST
);
7987 /* FIXME add other DLT_s ...
7988 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7989 * leave it for now */
7992 bpf_error("no MPLS support for data link type %d",
8000 /* If a specific MPLS label is requested, check it */
8001 if (label_num
>= 0) {
8002 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8003 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
8004 0xfffff000); /* only compare the first 20 bits */
8011 label_stack_depth
++;
8016 * Support PPPOE discovery and session.
8021 /* check for PPPoE discovery */
8022 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8026 gen_pppoes(sess_num
)
8029 struct block
*b0
, *b1
;
8032 * Test against the PPPoE session link-layer type.
8034 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8037 * Change the offsets to point to the type and data fields within
8038 * the PPP packet, and note that this is PPPoE rather than
8041 * XXX - this is a bit of a kludge. If we were to split the
8042 * compiler into a parser that parses an expression and
8043 * generates an expression tree, and a code generator that
8044 * takes an expression tree (which could come from our
8045 * parser or from some other parser) and generates BPF code,
8046 * we could perhaps make the offsets parameters of routines
8047 * and, in the handler for an "AND" node, pass to subnodes
8048 * other than the PPPoE node the adjusted offsets.
8050 * This would mean that "pppoes" would, instead of changing the
8051 * behavior of *all* tests after it, change only the behavior
8052 * of tests ANDed with it. That would change the documented
8053 * semantics of "pppoes", which might break some expressions.
8054 * However, it would mean that "(pppoes and ip) or ip" would check
8055 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8056 * checking only for VLAN-encapsulated IP, so that could still
8057 * be considered worth doing; it wouldn't break expressions
8058 * that are of the form "pppoes and ..." which I suspect are the
8059 * most common expressions involving "pppoes". "pppoes or ..."
8060 * doesn't necessarily do what the user would really want, now,
8061 * as all the "or ..." tests would be done assuming PPPoE, even
8062 * though the "or" could be viewed as meaning "or, if this isn't
8063 * a PPPoE packet...".
8065 orig_linktype
= off_linktype
; /* save original values */
8069 /* If a specific session is requested, check PPPoE session id */
8070 if (sess_num
>= 0) {
8071 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
,
8072 (bpf_int32
)sess_num
, 0x0000ffff);
8078 * The "network-layer" protocol is PPPoE, which has a 6-byte
8079 * PPPoE header, followed by a PPP packet.
8081 * There is no HDLC encapsulation for the PPP packet (it's
8082 * encapsulated in PPPoES instead), so the link-layer type
8083 * starts at the first byte of the PPP packet. For PPPoE,
8084 * that offset is relative to the beginning of the total
8085 * link-layer payload, including any 802.2 LLC header, so
8086 * it's 6 bytes past off_nl.
8088 off_linktype
= off_nl
+ 6;
8091 * The network-layer offsets are relative to the beginning
8092 * of the MAC-layer payload; that's past the 6-byte
8093 * PPPoE header and the 2-byte PPP header.
8096 off_nl_nosnap
= 6+2;
8102 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8114 bpf_error("'vpi' supported only on raw ATM");
8115 if (off_vpi
== (u_int
)-1)
8117 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8123 bpf_error("'vci' supported only on raw ATM");
8124 if (off_vci
== (u_int
)-1)
8126 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8131 if (off_proto
== (u_int
)-1)
8132 abort(); /* XXX - this isn't on FreeBSD */
8133 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8138 if (off_payload
== (u_int
)-1)
8140 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8141 0xffffffff, jtype
, reverse
, jvalue
);
8146 bpf_error("'callref' supported only on raw ATM");
8147 if (off_proto
== (u_int
)-1)
8149 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8150 jtype
, reverse
, jvalue
);
8160 gen_atmtype_abbrev(type
)
8163 struct block
*b0
, *b1
;
8168 /* Get all packets in Meta signalling Circuit */
8170 bpf_error("'metac' supported only on raw ATM");
8171 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8172 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8177 /* Get all packets in Broadcast Circuit*/
8179 bpf_error("'bcc' supported only on raw ATM");
8180 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8181 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8186 /* Get all cells in Segment OAM F4 circuit*/
8188 bpf_error("'oam4sc' supported only on raw ATM");
8189 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8190 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8195 /* Get all cells in End-to-End OAM F4 Circuit*/
8197 bpf_error("'oam4ec' supported only on raw ATM");
8198 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8199 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8204 /* Get all packets in connection Signalling Circuit */
8206 bpf_error("'sc' supported only on raw ATM");
8207 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8208 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8213 /* Get all packets in ILMI Circuit */
8215 bpf_error("'ilmic' supported only on raw ATM");
8216 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8217 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8222 /* Get all LANE packets */
8224 bpf_error("'lane' supported only on raw ATM");
8225 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8228 * Arrange that all subsequent tests assume LANE
8229 * rather than LLC-encapsulated packets, and set
8230 * the offsets appropriately for LANE-encapsulated
8233 * "off_mac" is the offset of the Ethernet header,
8234 * which is 2 bytes past the ATM pseudo-header
8235 * (skipping the pseudo-header and 2-byte LE Client
8236 * field). The other offsets are Ethernet offsets
8237 * relative to "off_mac".
8240 off_mac
= off_payload
+ 2; /* MAC header */
8241 off_linktype
= off_mac
+ 12;
8242 off_macpl
= off_mac
+ 14; /* Ethernet */
8243 off_nl
= 0; /* Ethernet II */
8244 off_nl_nosnap
= 3; /* 802.3+802.2 */
8248 /* Get all LLC-encapsulated packets */
8250 bpf_error("'llc' supported only on raw ATM");
8251 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8262 * Filtering for MTP2 messages based on li value
8263 * FISU, length is null
8264 * LSSU, length is 1 or 2
8265 * MSU, length is 3 or more
8266 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8269 gen_mtp2type_abbrev(type
)
8272 struct block
*b0
, *b1
;
8277 if ( (linktype
!= DLT_MTP2
) &&
8278 (linktype
!= DLT_ERF
) &&
8279 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8280 bpf_error("'fisu' supported only on MTP2");
8281 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8282 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8286 if ( (linktype
!= DLT_MTP2
) &&
8287 (linktype
!= DLT_ERF
) &&
8288 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8289 bpf_error("'lssu' supported only on MTP2");
8290 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8291 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8296 if ( (linktype
!= DLT_MTP2
) &&
8297 (linktype
!= DLT_ERF
) &&
8298 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8299 bpf_error("'msu' supported only on MTP2");
8300 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8304 if ( (linktype
!= DLT_MTP2
) &&
8305 (linktype
!= DLT_ERF
) &&
8306 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8307 bpf_error("'hfisu' supported only on MTP2_HSL");
8308 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8309 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8313 if ( (linktype
!= DLT_MTP2
) &&
8314 (linktype
!= DLT_ERF
) &&
8315 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8316 bpf_error("'hlssu' supported only on MTP2_HSL");
8317 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8318 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8323 if ( (linktype
!= DLT_MTP2
) &&
8324 (linktype
!= DLT_ERF
) &&
8325 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8326 bpf_error("'hmsu' supported only on MTP2_HSL");
8327 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8337 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8344 bpf_u_int32 val1
, val2
, val3
;
8345 u_int newoff_sio
=off_sio
;
8346 u_int newoff_opc
=off_opc
;
8347 u_int newoff_dpc
=off_dpc
;
8348 u_int newoff_sls
=off_sls
;
8350 switch (mtp3field
) {
8353 newoff_sio
+= 3; /* offset for MTP2_HSL */
8357 if (off_sio
== (u_int
)-1)
8358 bpf_error("'sio' supported only on SS7");
8359 /* sio coded on 1 byte so max value 255 */
8361 bpf_error("sio value %u too big; max value = 255",
8363 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8364 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8370 if (off_opc
== (u_int
)-1)
8371 bpf_error("'opc' supported only on SS7");
8372 /* opc coded on 14 bits so max value 16383 */
8374 bpf_error("opc value %u too big; max value = 16383",
8376 /* the following instructions are made to convert jvalue
8377 * to the form used to write opc in an ss7 message*/
8378 val1
= jvalue
& 0x00003c00;
8380 val2
= jvalue
& 0x000003fc;
8382 val3
= jvalue
& 0x00000003;
8384 jvalue
= val1
+ val2
+ val3
;
8385 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8386 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8394 if (off_dpc
== (u_int
)-1)
8395 bpf_error("'dpc' supported only on SS7");
8396 /* dpc coded on 14 bits so max value 16383 */
8398 bpf_error("dpc value %u too big; max value = 16383",
8400 /* the following instructions are made to convert jvalue
8401 * to the forme used to write dpc in an ss7 message*/
8402 val1
= jvalue
& 0x000000ff;
8404 val2
= jvalue
& 0x00003f00;
8406 jvalue
= val1
+ val2
;
8407 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8408 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8414 if (off_sls
== (u_int
)-1)
8415 bpf_error("'sls' supported only on SS7");
8416 /* sls coded on 4 bits so max value 15 */
8418 bpf_error("sls value %u too big; max value = 15",
8420 /* the following instruction is made to convert jvalue
8421 * to the forme used to write sls in an ss7 message*/
8422 jvalue
= jvalue
<< 4;
8423 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8424 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8433 static struct block
*
8434 gen_msg_abbrev(type
)
8440 * Q.2931 signalling protocol messages for handling virtual circuits
8441 * establishment and teardown
8446 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8450 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8454 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8458 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8462 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8465 case A_RELEASE_DONE
:
8466 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8476 gen_atmmulti_abbrev(type
)
8479 struct block
*b0
, *b1
;
8485 bpf_error("'oam' supported only on raw ATM");
8486 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8491 bpf_error("'oamf4' supported only on raw ATM");
8493 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8494 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8496 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8502 * Get Q.2931 signalling messages for switched
8503 * virtual connection
8506 bpf_error("'connectmsg' supported only on raw ATM");
8507 b0
= gen_msg_abbrev(A_SETUP
);
8508 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8510 b0
= gen_msg_abbrev(A_CONNECT
);
8512 b0
= gen_msg_abbrev(A_CONNECTACK
);
8514 b0
= gen_msg_abbrev(A_RELEASE
);
8516 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8518 b0
= gen_atmtype_abbrev(A_SC
);
8524 bpf_error("'metaconnect' supported only on raw ATM");
8525 b0
= gen_msg_abbrev(A_SETUP
);
8526 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8528 b0
= gen_msg_abbrev(A_CONNECT
);
8530 b0
= gen_msg_abbrev(A_RELEASE
);
8532 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8534 b0
= gen_atmtype_abbrev(A_METAC
);