1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
88 #include <linux/types.h>
89 #include <linux/if_packet.h>
90 #include <linux/filter.h>
92 #ifdef HAVE_NET_PFVAR_H
93 #include <sys/socket.h>
95 #include <net/pfvar.h>
96 #include <net/if_pflog.h>
99 #define offsetof(s, e) ((size_t)&((s *)0)->e)
103 #include <netdb.h> /* for "struct addrinfo" */
106 #include <pcap/namedb.h>
108 #define ETHERMTU 1500
110 #ifndef IPPROTO_HOPOPTS
111 #define IPPROTO_HOPOPTS 0
113 #ifndef IPPROTO_ROUTING
114 #define IPPROTO_ROUTING 43
116 #ifndef IPPROTO_FRAGMENT
117 #define IPPROTO_FRAGMENT 44
119 #ifndef IPPROTO_DSTOPTS
120 #define IPPROTO_DSTOPTS 60
123 #define IPPROTO_SCTP 132
126 #ifdef HAVE_OS_PROTO_H
127 #include "os-proto.h"
130 #define JMP(c) ((c)|BPF_JMP|BPF_K)
133 static jmp_buf top_ctx
;
134 static pcap_t
*bpf_pcap
;
136 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
138 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
140 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
144 static int pcap_fddipad
;
148 bpf_error(const char *fmt
, ...)
153 if (bpf_pcap
!= NULL
)
154 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
161 static void init_linktype(pcap_t
*);
163 static void init_regs(void);
164 static int alloc_reg(void);
165 static void free_reg(int);
167 static struct block
*root
;
170 * Value passed to gen_load_a() to indicate what the offset argument
174 OR_PACKET
, /* relative to the beginning of the packet */
175 OR_LINK
, /* relative to the beginning of the link-layer header */
176 OR_MACPL
, /* relative to the end of the MAC-layer header */
177 OR_NET
, /* relative to the network-layer header */
178 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
179 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
180 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
185 * As errors are handled by a longjmp, anything allocated must be freed
186 * in the longjmp handler, so it must be reachable from that handler.
187 * One thing that's allocated is the result of pcap_nametoaddrinfo();
188 * it must be freed with freeaddrinfo(). This variable points to any
189 * addrinfo structure that would need to be freed.
191 static struct addrinfo
*ai
;
195 * We divy out chunks of memory rather than call malloc each time so
196 * we don't have to worry about leaking memory. It's probably
197 * not a big deal if all this memory was wasted but if this ever
198 * goes into a library that would probably not be a good idea.
200 * XXX - this *is* in a library....
203 #define CHUNK0SIZE 1024
209 static struct chunk chunks
[NCHUNKS
];
210 static int cur_chunk
;
212 static void *newchunk(u_int
);
213 static void freechunks(void);
214 static inline struct block
*new_block(int);
215 static inline struct slist
*new_stmt(int);
216 static struct block
*gen_retblk(int);
217 static inline void syntax(void);
219 static void backpatch(struct block
*, struct block
*);
220 static void merge(struct block
*, struct block
*);
221 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
222 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
223 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
224 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
225 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
226 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
228 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
229 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
230 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
231 static struct slist
*gen_load_llrel(u_int
, u_int
);
232 static struct slist
*gen_load_macplrel(u_int
, u_int
);
233 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
234 static struct slist
*gen_loadx_iphdrlen(void);
235 static struct block
*gen_uncond(int);
236 static inline struct block
*gen_true(void);
237 static inline struct block
*gen_false(void);
238 static struct block
*gen_ether_linktype(int);
239 static struct block
*gen_ipnet_linktype(int);
240 static struct block
*gen_linux_sll_linktype(int);
241 static struct slist
*gen_load_prism_llprefixlen(void);
242 static struct slist
*gen_load_avs_llprefixlen(void);
243 static struct slist
*gen_load_radiotap_llprefixlen(void);
244 static struct slist
*gen_load_ppi_llprefixlen(void);
245 static void insert_compute_vloffsets(struct block
*);
246 static struct slist
*gen_llprefixlen(void);
247 static struct slist
*gen_off_macpl(void);
248 static int ethertype_to_ppptype(int);
249 static struct block
*gen_linktype(int);
250 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
251 static struct block
*gen_llc_linktype(int);
252 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
254 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
256 static struct block
*gen_ahostop(const u_char
*, int);
257 static struct block
*gen_ehostop(const u_char
*, int);
258 static struct block
*gen_fhostop(const u_char
*, int);
259 static struct block
*gen_thostop(const u_char
*, int);
260 static struct block
*gen_wlanhostop(const u_char
*, int);
261 static struct block
*gen_ipfchostop(const u_char
*, int);
262 static struct block
*gen_dnhostop(bpf_u_int32
, int);
263 static struct block
*gen_mpls_linktype(int);
264 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
266 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
269 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
271 static struct block
*gen_ipfrag(void);
272 static struct block
*gen_portatom(int, bpf_int32
);
273 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
274 static struct block
*gen_portatom6(int, bpf_int32
);
275 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
276 struct block
*gen_portop(int, int, int);
277 static struct block
*gen_port(int, int, int);
278 struct block
*gen_portrangeop(int, int, int, int);
279 static struct block
*gen_portrange(int, int, int, int);
280 struct block
*gen_portop6(int, int, int);
281 static struct block
*gen_port6(int, int, int);
282 struct block
*gen_portrangeop6(int, int, int, int);
283 static struct block
*gen_portrange6(int, int, int, int);
284 static int lookup_proto(const char *, int);
285 static struct block
*gen_protochain(int, int, int);
286 static struct block
*gen_proto(int, int, int);
287 static struct slist
*xfer_to_x(struct arth
*);
288 static struct slist
*xfer_to_a(struct arth
*);
289 static struct block
*gen_mac_multicast(int);
290 static struct block
*gen_len(int, int);
291 static struct block
*gen_check_802_11_data_frame(void);
293 static struct block
*gen_ppi_dlt_check(void);
294 static struct block
*gen_msg_abbrev(int type
);
305 /* XXX Round up to nearest long. */
306 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
308 /* XXX Round up to structure boundary. */
312 cp
= &chunks
[cur_chunk
];
313 if (n
> cp
->n_left
) {
314 ++cp
, k
= ++cur_chunk
;
316 bpf_error("out of memory");
317 size
= CHUNK0SIZE
<< k
;
318 cp
->m
= (void *)malloc(size
);
320 bpf_error("out of memory");
321 memset((char *)cp
->m
, 0, size
);
324 bpf_error("out of memory");
327 return (void *)((char *)cp
->m
+ cp
->n_left
);
336 for (i
= 0; i
< NCHUNKS
; ++i
)
337 if (chunks
[i
].m
!= NULL
) {
344 * A strdup whose allocations are freed after code generation is over.
348 register const char *s
;
350 int n
= strlen(s
) + 1;
351 char *cp
= newchunk(n
);
357 static inline struct block
*
363 p
= (struct block
*)newchunk(sizeof(*p
));
370 static inline struct slist
*
376 p
= (struct slist
*)newchunk(sizeof(*p
));
382 static struct block
*
386 struct block
*b
= new_block(BPF_RET
|BPF_K
);
395 bpf_error("syntax error in filter expression");
398 static bpf_u_int32 netmask
;
403 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
404 const char *buf
, int optimize
, bpf_u_int32 mask
);
407 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
408 const char *buf
, int optimize
, bpf_u_int32 mask
)
412 EnterCriticalSection(&g_PcapCompileCriticalSection
);
414 result
= pcap_compile_unsafe(p
, program
, buf
, optimize
, mask
);
416 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
422 pcap_compile_unsafe(pcap_t
*p
, struct bpf_program
*program
,
423 const char *buf
, int optimize
, bpf_u_int32 mask
)
426 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
427 const char *buf
, int optimize
, bpf_u_int32 mask
)
431 const char * volatile xbuf
= buf
;
435 * If this pcap_t hasn't been activated, it doesn't have a
436 * link-layer type, so we can't use it.
439 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
440 "not-yet-activated pcap_t passed to pcap_compile");
448 if (setjmp(top_ctx
)) {
462 snaplen
= pcap_snapshot(p
);
464 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
465 "snaplen of 0 rejects all packets");
469 lex_init(xbuf
? xbuf
: "");
477 root
= gen_retblk(snaplen
);
479 if (optimize
&& !no_optimize
) {
482 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
483 bpf_error("expression rejects all packets");
485 program
->bf_insns
= icode_to_fcode(root
, &len
);
486 program
->bf_len
= len
;
494 * entry point for using the compiler with no pcap open
495 * pass in all the stuff that is needed explicitly instead.
498 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
499 struct bpf_program
*program
,
500 const char *buf
, int optimize
, bpf_u_int32 mask
)
505 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
508 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
514 * Clean up a "struct bpf_program" by freeing all the memory allocated
518 pcap_freecode(struct bpf_program
*program
)
521 if (program
->bf_insns
!= NULL
) {
522 free((char *)program
->bf_insns
);
523 program
->bf_insns
= NULL
;
528 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
529 * which of the jt and jf fields has been resolved and which is a pointer
530 * back to another unresolved block (or nil). At least one of the fields
531 * in each block is already resolved.
534 backpatch(list
, target
)
535 struct block
*list
, *target
;
552 * Merge the lists in b0 and b1, using the 'sense' field to indicate
553 * which of jt and jf is the link.
557 struct block
*b0
, *b1
;
559 register struct block
**p
= &b0
;
561 /* Find end of list. */
563 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
565 /* Concatenate the lists. */
573 struct block
*ppi_dlt_check
;
576 * Insert before the statements of the first (root) block any
577 * statements needed to load the lengths of any variable-length
578 * headers into registers.
580 * XXX - a fancier strategy would be to insert those before the
581 * statements of all blocks that use those lengths and that
582 * have no predecessors that use them, so that we only compute
583 * the lengths if we need them. There might be even better
584 * approaches than that.
586 * However, those strategies would be more complicated, and
587 * as we don't generate code to compute a length if the
588 * program has no tests that use the length, and as most
589 * tests will probably use those lengths, we would just
590 * postpone computing the lengths so that it's not done
591 * for tests that fail early, and it's not clear that's
594 insert_compute_vloffsets(p
->head
);
597 * For DLT_PPI captures, generate a check of the per-packet
598 * DLT value to make sure it's DLT_IEEE802_11.
600 ppi_dlt_check
= gen_ppi_dlt_check();
601 if (ppi_dlt_check
!= NULL
)
602 gen_and(ppi_dlt_check
, p
);
604 backpatch(p
, gen_retblk(snaplen
));
605 p
->sense
= !p
->sense
;
606 backpatch(p
, gen_retblk(0));
612 struct block
*b0
, *b1
;
614 backpatch(b0
, b1
->head
);
615 b0
->sense
= !b0
->sense
;
616 b1
->sense
= !b1
->sense
;
618 b1
->sense
= !b1
->sense
;
624 struct block
*b0
, *b1
;
626 b0
->sense
= !b0
->sense
;
627 backpatch(b0
, b1
->head
);
628 b0
->sense
= !b0
->sense
;
637 b
->sense
= !b
->sense
;
640 static struct block
*
641 gen_cmp(offrel
, offset
, size
, v
)
642 enum e_offrel offrel
;
646 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
649 static struct block
*
650 gen_cmp_gt(offrel
, offset
, size
, v
)
651 enum e_offrel offrel
;
655 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
658 static struct block
*
659 gen_cmp_ge(offrel
, offset
, size
, v
)
660 enum e_offrel offrel
;
664 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
667 static struct block
*
668 gen_cmp_lt(offrel
, offset
, size
, v
)
669 enum e_offrel offrel
;
673 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
676 static struct block
*
677 gen_cmp_le(offrel
, offset
, size
, v
)
678 enum e_offrel offrel
;
682 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
685 static struct block
*
686 gen_mcmp(offrel
, offset
, size
, v
, mask
)
687 enum e_offrel offrel
;
692 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
695 static struct block
*
696 gen_bcmp(offrel
, offset
, size
, v
)
697 enum e_offrel offrel
;
698 register u_int offset
, size
;
699 register const u_char
*v
;
701 register struct block
*b
, *tmp
;
705 register const u_char
*p
= &v
[size
- 4];
706 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
707 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
709 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
716 register const u_char
*p
= &v
[size
- 2];
717 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
719 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
726 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
735 * AND the field of size "size" at offset "offset" relative to the header
736 * specified by "offrel" with "mask", and compare it with the value "v"
737 * with the test specified by "jtype"; if "reverse" is true, the test
738 * should test the opposite of "jtype".
740 static struct block
*
741 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
742 enum e_offrel offrel
;
744 bpf_u_int32 offset
, size
, mask
, jtype
;
747 struct slist
*s
, *s2
;
750 s
= gen_load_a(offrel
, offset
, size
);
752 if (mask
!= 0xffffffff) {
753 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
758 b
= new_block(JMP(jtype
));
761 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
767 * Various code constructs need to know the layout of the data link
768 * layer. These variables give the necessary offsets from the beginning
769 * of the packet data.
773 * This is the offset of the beginning of the link-layer header from
774 * the beginning of the raw packet data.
776 * It's usually 0, except for 802.11 with a fixed-length radio header.
777 * (For 802.11 with a variable-length radio header, we have to generate
778 * code to compute that offset; off_ll is 0 in that case.)
783 * If there's a variable-length header preceding the link-layer header,
784 * "reg_off_ll" is the register number for a register containing the
785 * length of that header, and therefore the offset of the link-layer
786 * header from the beginning of the raw packet data. Otherwise,
787 * "reg_off_ll" is -1.
789 static int reg_off_ll
;
792 * This is the offset of the beginning of the MAC-layer header from
793 * the beginning of the link-layer header.
794 * It's usually 0, except for ATM LANE, where it's the offset, relative
795 * to the beginning of the raw packet data, of the Ethernet header, and
796 * for Ethernet with various additional information.
798 static u_int off_mac
;
801 * This is the offset of the beginning of the MAC-layer payload,
802 * from the beginning of the raw packet data.
804 * I.e., it's the sum of the length of the link-layer header (without,
805 * for example, any 802.2 LLC header, so it's the MAC-layer
806 * portion of that header), plus any prefix preceding the
809 static u_int off_macpl
;
812 * This is 1 if the offset of the beginning of the MAC-layer payload
813 * from the beginning of the link-layer header is variable-length.
815 static int off_macpl_is_variable
;
818 * If the link layer has variable_length headers, "reg_off_macpl"
819 * is the register number for a register containing the length of the
820 * link-layer header plus the length of any variable-length header
821 * preceding the link-layer header. Otherwise, "reg_off_macpl"
824 static int reg_off_macpl
;
827 * "off_linktype" is the offset to information in the link-layer header
828 * giving the packet type. This offset is relative to the beginning
829 * of the link-layer header (i.e., it doesn't include off_ll).
831 * For Ethernet, it's the offset of the Ethernet type field.
833 * For link-layer types that always use 802.2 headers, it's the
834 * offset of the LLC header.
836 * For PPP, it's the offset of the PPP type field.
838 * For Cisco HDLC, it's the offset of the CHDLC type field.
840 * For BSD loopback, it's the offset of the AF_ value.
842 * For Linux cooked sockets, it's the offset of the type field.
844 * It's set to -1 for no encapsulation, in which case, IP is assumed.
846 static u_int off_linktype
;
849 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
850 * checks to check the PPP header, assumed to follow a LAN-style link-
851 * layer header and a PPPoE session header.
853 static int is_pppoes
= 0;
856 * TRUE if the link layer includes an ATM pseudo-header.
858 static int is_atm
= 0;
861 * TRUE if "lane" appeared in the filter; it causes us to generate
862 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
864 static int is_lane
= 0;
867 * These are offsets for the ATM pseudo-header.
869 static u_int off_vpi
;
870 static u_int off_vci
;
871 static u_int off_proto
;
874 * These are offsets for the MTP2 fields.
877 static u_int off_li_hsl
;
880 * These are offsets for the MTP3 fields.
882 static u_int off_sio
;
883 static u_int off_opc
;
884 static u_int off_dpc
;
885 static u_int off_sls
;
888 * This is the offset of the first byte after the ATM pseudo_header,
889 * or -1 if there is no ATM pseudo-header.
891 static u_int off_payload
;
894 * These are offsets to the beginning of the network-layer header.
895 * They are relative to the beginning of the MAC-layer payload (i.e.,
896 * they don't include off_ll or off_macpl).
898 * If the link layer never uses 802.2 LLC:
900 * "off_nl" and "off_nl_nosnap" are the same.
902 * If the link layer always uses 802.2 LLC:
904 * "off_nl" is the offset if there's a SNAP header following
907 * "off_nl_nosnap" is the offset if there's no SNAP header.
909 * If the link layer is Ethernet:
911 * "off_nl" is the offset if the packet is an Ethernet II packet
912 * (we assume no 802.3+802.2+SNAP);
914 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
915 * with an 802.2 header following it.
918 static u_int off_nl_nosnap
;
926 linktype
= pcap_datalink(p
);
927 pcap_fddipad
= p
->fddipad
;
930 * Assume it's not raw ATM with a pseudo-header, for now.
941 * And that we're not doing PPPoE.
946 * And assume we're not doing SS7.
956 * Also assume it's not 802.11.
960 off_macpl_is_variable
= 0;
964 label_stack_depth
= 0;
974 off_nl
= 0; /* XXX in reality, variable! */
975 off_nl_nosnap
= 0; /* no 802.2 LLC */
978 case DLT_ARCNET_LINUX
:
981 off_nl
= 0; /* XXX in reality, variable! */
982 off_nl_nosnap
= 0; /* no 802.2 LLC */
987 off_macpl
= 14; /* Ethernet header length */
988 off_nl
= 0; /* Ethernet II */
989 off_nl_nosnap
= 3; /* 802.3+802.2 */
994 * SLIP doesn't have a link level type. The 16 byte
995 * header is hacked into our SLIP driver.
1000 off_nl_nosnap
= 0; /* no 802.2 LLC */
1003 case DLT_SLIP_BSDOS
:
1004 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1009 off_nl_nosnap
= 0; /* no 802.2 LLC */
1017 off_nl_nosnap
= 0; /* no 802.2 LLC */
1024 off_nl_nosnap
= 0; /* no 802.2 LLC */
1029 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1030 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1034 off_nl_nosnap
= 0; /* no 802.2 LLC */
1039 * This does no include the Ethernet header, and
1040 * only covers session state.
1045 off_nl_nosnap
= 0; /* no 802.2 LLC */
1052 off_nl_nosnap
= 0; /* no 802.2 LLC */
1057 * FDDI doesn't really have a link-level type field.
1058 * We set "off_linktype" to the offset of the LLC header.
1060 * To check for Ethernet types, we assume that SSAP = SNAP
1061 * is being used and pick out the encapsulated Ethernet type.
1062 * XXX - should we generate code to check for SNAP?
1065 off_linktype
+= pcap_fddipad
;
1066 off_macpl
= 13; /* FDDI MAC header length */
1067 off_macpl
+= pcap_fddipad
;
1068 off_nl
= 8; /* 802.2+SNAP */
1069 off_nl_nosnap
= 3; /* 802.2 */
1074 * Token Ring doesn't really have a link-level type field.
1075 * We set "off_linktype" to the offset of the LLC header.
1077 * To check for Ethernet types, we assume that SSAP = SNAP
1078 * is being used and pick out the encapsulated Ethernet type.
1079 * XXX - should we generate code to check for SNAP?
1081 * XXX - the header is actually variable-length.
1082 * Some various Linux patched versions gave 38
1083 * as "off_linktype" and 40 as "off_nl"; however,
1084 * if a token ring packet has *no* routing
1085 * information, i.e. is not source-routed, the correct
1086 * values are 20 and 22, as they are in the vanilla code.
1088 * A packet is source-routed iff the uppermost bit
1089 * of the first byte of the source address, at an
1090 * offset of 8, has the uppermost bit set. If the
1091 * packet is source-routed, the total number of bytes
1092 * of routing information is 2 plus bits 0x1F00 of
1093 * the 16-bit value at an offset of 14 (shifted right
1094 * 8 - figure out which byte that is).
1097 off_macpl
= 14; /* Token Ring MAC header length */
1098 off_nl
= 8; /* 802.2+SNAP */
1099 off_nl_nosnap
= 3; /* 802.2 */
1102 case DLT_IEEE802_11
:
1103 case DLT_PRISM_HEADER
:
1104 case DLT_IEEE802_11_RADIO_AVS
:
1105 case DLT_IEEE802_11_RADIO
:
1107 * 802.11 doesn't really have a link-level type field.
1108 * We set "off_linktype" to the offset of the LLC header.
1110 * To check for Ethernet types, we assume that SSAP = SNAP
1111 * is being used and pick out the encapsulated Ethernet type.
1112 * XXX - should we generate code to check for SNAP?
1114 * We also handle variable-length radio headers here.
1115 * The Prism header is in theory variable-length, but in
1116 * practice it's always 144 bytes long. However, some
1117 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1118 * sometimes or always supply an AVS header, so we
1119 * have to check whether the radio header is a Prism
1120 * header or an AVS header, so, in practice, it's
1124 off_macpl
= 0; /* link-layer header is variable-length */
1125 off_macpl_is_variable
= 1;
1126 off_nl
= 8; /* 802.2+SNAP */
1127 off_nl_nosnap
= 3; /* 802.2 */
1132 * At the moment we treat PPI the same way that we treat
1133 * normal Radiotap encoded packets. The difference is in
1134 * the function that generates the code at the beginning
1135 * to compute the header length. Since this code generator
1136 * of PPI supports bare 802.11 encapsulation only (i.e.
1137 * the encapsulated DLT should be DLT_IEEE802_11) we
1138 * generate code to check for this too.
1141 off_macpl
= 0; /* link-layer header is variable-length */
1142 off_macpl_is_variable
= 1;
1143 off_nl
= 8; /* 802.2+SNAP */
1144 off_nl_nosnap
= 3; /* 802.2 */
1147 case DLT_ATM_RFC1483
:
1148 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1150 * assume routed, non-ISO PDUs
1151 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1153 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1154 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1155 * latter would presumably be treated the way PPPoE
1156 * should be, so you can do "pppoe and udp port 2049"
1157 * or "pppoa and tcp port 80" and have it check for
1158 * PPPo{A,E} and a PPP protocol of IP and....
1161 off_macpl
= 0; /* packet begins with LLC header */
1162 off_nl
= 8; /* 802.2+SNAP */
1163 off_nl_nosnap
= 3; /* 802.2 */
1168 * Full Frontal ATM; you get AALn PDUs with an ATM
1172 off_vpi
= SUNATM_VPI_POS
;
1173 off_vci
= SUNATM_VCI_POS
;
1174 off_proto
= PROTO_POS
;
1175 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1176 off_payload
= SUNATM_PKT_BEGIN_POS
;
1177 off_linktype
= off_payload
;
1178 off_macpl
= off_payload
; /* if LLC-encapsulated */
1179 off_nl
= 8; /* 802.2+SNAP */
1180 off_nl_nosnap
= 3; /* 802.2 */
1189 off_nl_nosnap
= 0; /* no 802.2 LLC */
1192 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1196 off_nl_nosnap
= 0; /* no 802.2 LLC */
1201 * LocalTalk does have a 1-byte type field in the LLAP header,
1202 * but really it just indicates whether there is a "short" or
1203 * "long" DDP packet following.
1208 off_nl_nosnap
= 0; /* no 802.2 LLC */
1211 case DLT_IP_OVER_FC
:
1213 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1214 * link-level type field. We set "off_linktype" to the
1215 * offset of the LLC header.
1217 * To check for Ethernet types, we assume that SSAP = SNAP
1218 * is being used and pick out the encapsulated Ethernet type.
1219 * XXX - should we generate code to check for SNAP? RFC
1220 * 2625 says SNAP should be used.
1224 off_nl
= 8; /* 802.2+SNAP */
1225 off_nl_nosnap
= 3; /* 802.2 */
1230 * XXX - we should set this to handle SNAP-encapsulated
1231 * frames (NLPID of 0x80).
1236 off_nl_nosnap
= 0; /* no 802.2 LLC */
1240 * the only BPF-interesting FRF.16 frames are non-control frames;
1241 * Frame Relay has a variable length link-layer
1242 * so lets start with offset 4 for now and increments later on (FIXME);
1248 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1251 case DLT_APPLE_IP_OVER_IEEE1394
:
1255 off_nl_nosnap
= 0; /* no 802.2 LLC */
1258 case DLT_SYMANTEC_FIREWALL
:
1261 off_nl
= 0; /* Ethernet II */
1262 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1265 #ifdef HAVE_NET_PFVAR_H
1268 off_macpl
= PFLOG_HDRLEN
;
1270 off_nl_nosnap
= 0; /* no 802.2 LLC */
1274 case DLT_JUNIPER_MFR
:
1275 case DLT_JUNIPER_MLFR
:
1276 case DLT_JUNIPER_MLPPP
:
1277 case DLT_JUNIPER_PPP
:
1278 case DLT_JUNIPER_CHDLC
:
1279 case DLT_JUNIPER_FRELAY
:
1283 off_nl_nosnap
= -1; /* no 802.2 LLC */
1286 case DLT_JUNIPER_ATM1
:
1287 off_linktype
= 4; /* in reality variable between 4-8 */
1288 off_macpl
= 4; /* in reality variable between 4-8 */
1293 case DLT_JUNIPER_ATM2
:
1294 off_linktype
= 8; /* in reality variable between 8-12 */
1295 off_macpl
= 8; /* in reality variable between 8-12 */
1300 /* frames captured on a Juniper PPPoE service PIC
1301 * contain raw ethernet frames */
1302 case DLT_JUNIPER_PPPOE
:
1303 case DLT_JUNIPER_ETHER
:
1306 off_nl
= 18; /* Ethernet II */
1307 off_nl_nosnap
= 21; /* 802.3+802.2 */
1310 case DLT_JUNIPER_PPPOE_ATM
:
1314 off_nl_nosnap
= -1; /* no 802.2 LLC */
1317 case DLT_JUNIPER_GGSN
:
1321 off_nl_nosnap
= -1; /* no 802.2 LLC */
1324 case DLT_JUNIPER_ES
:
1326 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1327 off_nl
= -1; /* not really a network layer but raw IP addresses */
1328 off_nl_nosnap
= -1; /* no 802.2 LLC */
1331 case DLT_JUNIPER_MONITOR
:
1334 off_nl
= 0; /* raw IP/IP6 header */
1335 off_nl_nosnap
= -1; /* no 802.2 LLC */
1338 case DLT_BACNET_MS_TP
:
1345 case DLT_JUNIPER_SERVICES
:
1347 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1348 off_nl
= -1; /* L3 proto location dep. on cookie type */
1349 off_nl_nosnap
= -1; /* no 802.2 LLC */
1352 case DLT_JUNIPER_VP
:
1359 case DLT_JUNIPER_ST
:
1366 case DLT_JUNIPER_ISM
:
1373 case DLT_JUNIPER_VS
:
1374 case DLT_JUNIPER_SRX_E2E
:
1375 case DLT_JUNIPER_FIBRECHANNEL
:
1376 case DLT_JUNIPER_ATM_CEMIC
:
1396 case DLT_MTP2_WITH_PHDR
:
1431 * Currently, only raw "link[N:M]" filtering is supported.
1433 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1435 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1436 off_nl_nosnap
= -1; /* no 802.2 LLC */
1437 off_mac
= 1; /* step over the kiss length byte */
1442 off_macpl
= 24; /* ipnet header length */
1447 case DLT_NETANALYZER
:
1448 off_mac
= 4; /* MAC header is past 4-byte pseudo-header */
1449 off_linktype
= 16; /* includes 4-byte pseudo-header */
1450 off_macpl
= 18; /* pseudo-header+Ethernet header length */
1451 off_nl
= 0; /* Ethernet II */
1452 off_nl_nosnap
= 3; /* 802.3+802.2 */
1455 case DLT_NETANALYZER_TRANSPARENT
:
1456 off_mac
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1457 off_linktype
= 24; /* includes 4-byte pseudo-header+preamble+SFD */
1458 off_macpl
= 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1459 off_nl
= 0; /* Ethernet II */
1460 off_nl_nosnap
= 3; /* 802.3+802.2 */
1465 * For values in the range in which we've assigned new
1466 * DLT_ values, only raw "link[N:M]" filtering is supported.
1468 if (linktype
>= DLT_MATCHING_MIN
&&
1469 linktype
<= DLT_MATCHING_MAX
) {
1478 bpf_error("unknown data link type %d", linktype
);
1483 * Load a value relative to the beginning of the link-layer header.
1484 * The link-layer header doesn't necessarily begin at the beginning
1485 * of the packet data; there might be a variable-length prefix containing
1486 * radio information.
1488 static struct slist
*
1489 gen_load_llrel(offset
, size
)
1492 struct slist
*s
, *s2
;
1494 s
= gen_llprefixlen();
1497 * If "s" is non-null, it has code to arrange that the X register
1498 * contains the length of the prefix preceding the link-layer
1501 * Otherwise, the length of the prefix preceding the link-layer
1502 * header is "off_ll".
1506 * There's a variable-length prefix preceding the
1507 * link-layer header. "s" points to a list of statements
1508 * that put the length of that prefix into the X register.
1509 * do an indirect load, to use the X register as an offset.
1511 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1516 * There is no variable-length header preceding the
1517 * link-layer header; add in off_ll, which, if there's
1518 * a fixed-length header preceding the link-layer header,
1519 * is the length of that header.
1521 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1522 s
->s
.k
= offset
+ off_ll
;
1528 * Load a value relative to the beginning of the MAC-layer payload.
1530 static struct slist
*
1531 gen_load_macplrel(offset
, size
)
1534 struct slist
*s
, *s2
;
1536 s
= gen_off_macpl();
1539 * If s is non-null, the offset of the MAC-layer payload is
1540 * variable, and s points to a list of instructions that
1541 * arrange that the X register contains that offset.
1543 * Otherwise, the offset of the MAC-layer payload is constant,
1544 * and is in off_macpl.
1548 * The offset of the MAC-layer payload is in the X
1549 * register. Do an indirect load, to use the X register
1552 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1557 * The offset of the MAC-layer payload is constant,
1558 * and is in off_macpl; load the value at that offset
1559 * plus the specified offset.
1561 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1562 s
->s
.k
= off_macpl
+ offset
;
1568 * Load a value relative to the beginning of the specified header.
1570 static struct slist
*
1571 gen_load_a(offrel
, offset
, size
)
1572 enum e_offrel offrel
;
1575 struct slist
*s
, *s2
;
1580 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1585 s
= gen_load_llrel(offset
, size
);
1589 s
= gen_load_macplrel(offset
, size
);
1593 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1597 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1602 * Load the X register with the length of the IPv4 header
1603 * (plus the offset of the link-layer header, if it's
1604 * preceded by a variable-length header such as a radio
1605 * header), in bytes.
1607 s
= gen_loadx_iphdrlen();
1610 * Load the item at {offset of the MAC-layer payload} +
1611 * {offset, relative to the start of the MAC-layer
1612 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1613 * {specified offset}.
1615 * (If the offset of the MAC-layer payload is variable,
1616 * it's included in the value in the X register, and
1619 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1620 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1625 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1636 * Generate code to load into the X register the sum of the length of
1637 * the IPv4 header and any variable-length header preceding the link-layer
1640 static struct slist
*
1641 gen_loadx_iphdrlen()
1643 struct slist
*s
, *s2
;
1645 s
= gen_off_macpl();
1648 * There's a variable-length prefix preceding the
1649 * link-layer header, or the link-layer header is itself
1650 * variable-length. "s" points to a list of statements
1651 * that put the offset of the MAC-layer payload into
1654 * The 4*([k]&0xf) addressing mode can't be used, as we
1655 * don't have a constant offset, so we have to load the
1656 * value in question into the A register and add to it
1657 * the value from the X register.
1659 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1662 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1665 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1670 * The A register now contains the length of the
1671 * IP header. We need to add to it the offset of
1672 * the MAC-layer payload, which is still in the X
1673 * register, and move the result into the X register.
1675 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1676 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1679 * There is no variable-length header preceding the
1680 * link-layer header, and the link-layer header is
1681 * fixed-length; load the length of the IPv4 header,
1682 * which is at an offset of off_nl from the beginning
1683 * of the MAC-layer payload, and thus at an offset
1684 * of off_mac_pl + off_nl from the beginning of the
1687 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1688 s
->s
.k
= off_macpl
+ off_nl
;
1693 static struct block
*
1700 s
= new_stmt(BPF_LD
|BPF_IMM
);
1702 b
= new_block(JMP(BPF_JEQ
));
1708 static inline struct block
*
1711 return gen_uncond(1);
1714 static inline struct block
*
1717 return gen_uncond(0);
1721 * Byte-swap a 32-bit number.
1722 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1723 * big-endian platforms.)
1725 #define SWAPLONG(y) \
1726 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1729 * Generate code to match a particular packet type.
1731 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1732 * value, if <= ETHERMTU. We use that to determine whether to
1733 * match the type/length field or to check the type/length field for
1734 * a value <= ETHERMTU to see whether it's a type field and then do
1735 * the appropriate test.
1737 static struct block
*
1738 gen_ether_linktype(proto
)
1741 struct block
*b0
, *b1
;
1747 case LLCSAP_NETBEUI
:
1749 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1750 * so we check the DSAP and SSAP.
1752 * LLCSAP_IP checks for IP-over-802.2, rather
1753 * than IP-over-Ethernet or IP-over-SNAP.
1755 * XXX - should we check both the DSAP and the
1756 * SSAP, like this, or should we check just the
1757 * DSAP, as we do for other types <= ETHERMTU
1758 * (i.e., other SAP values)?
1760 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1762 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1763 ((proto
<< 8) | proto
));
1771 * Ethernet_II frames, which are Ethernet
1772 * frames with a frame type of ETHERTYPE_IPX;
1774 * Ethernet_802.3 frames, which are 802.3
1775 * frames (i.e., the type/length field is
1776 * a length field, <= ETHERMTU, rather than
1777 * a type field) with the first two bytes
1778 * after the Ethernet/802.3 header being
1781 * Ethernet_802.2 frames, which are 802.3
1782 * frames with an 802.2 LLC header and
1783 * with the IPX LSAP as the DSAP in the LLC
1786 * Ethernet_SNAP frames, which are 802.3
1787 * frames with an LLC header and a SNAP
1788 * header and with an OUI of 0x000000
1789 * (encapsulated Ethernet) and a protocol
1790 * ID of ETHERTYPE_IPX in the SNAP header.
1792 * XXX - should we generate the same code both
1793 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1797 * This generates code to check both for the
1798 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1800 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1801 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1805 * Now we add code to check for SNAP frames with
1806 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1808 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1812 * Now we generate code to check for 802.3
1813 * frames in general.
1815 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1819 * Now add the check for 802.3 frames before the
1820 * check for Ethernet_802.2 and Ethernet_802.3,
1821 * as those checks should only be done on 802.3
1822 * frames, not on Ethernet frames.
1827 * Now add the check for Ethernet_II frames, and
1828 * do that before checking for the other frame
1831 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1832 (bpf_int32
)ETHERTYPE_IPX
);
1836 case ETHERTYPE_ATALK
:
1837 case ETHERTYPE_AARP
:
1839 * EtherTalk (AppleTalk protocols on Ethernet link
1840 * layer) may use 802.2 encapsulation.
1844 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1845 * we check for an Ethernet type field less than
1846 * 1500, which means it's an 802.3 length field.
1848 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1852 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1853 * SNAP packets with an organization code of
1854 * 0x080007 (Apple, for Appletalk) and a protocol
1855 * type of ETHERTYPE_ATALK (Appletalk).
1857 * 802.2-encapsulated ETHERTYPE_AARP packets are
1858 * SNAP packets with an organization code of
1859 * 0x000000 (encapsulated Ethernet) and a protocol
1860 * type of ETHERTYPE_AARP (Appletalk ARP).
1862 if (proto
== ETHERTYPE_ATALK
)
1863 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1864 else /* proto == ETHERTYPE_AARP */
1865 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1869 * Check for Ethernet encapsulation (Ethertalk
1870 * phase 1?); we just check for the Ethernet
1873 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1879 if (proto
<= ETHERMTU
) {
1881 * This is an LLC SAP value, so the frames
1882 * that match would be 802.2 frames.
1883 * Check that the frame is an 802.2 frame
1884 * (i.e., that the length/type field is
1885 * a length field, <= ETHERMTU) and
1886 * then check the DSAP.
1888 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1890 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1896 * This is an Ethernet type, so compare
1897 * the length/type field with it (if
1898 * the frame is an 802.2 frame, the length
1899 * field will be <= ETHERMTU, and, as
1900 * "proto" is > ETHERMTU, this test
1901 * will fail and the frame won't match,
1902 * which is what we want).
1904 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1911 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1912 * or IPv6 then we have an error.
1914 static struct block
*
1915 gen_ipnet_linktype(proto
)
1921 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1922 (bpf_int32
)IPH_AF_INET
);
1925 case ETHERTYPE_IPV6
:
1926 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1927 (bpf_int32
)IPH_AF_INET6
);
1938 * Generate code to match a particular packet type.
1940 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1941 * value, if <= ETHERMTU. We use that to determine whether to
1942 * match the type field or to check the type field for the special
1943 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1945 static struct block
*
1946 gen_linux_sll_linktype(proto
)
1949 struct block
*b0
, *b1
;
1955 case LLCSAP_NETBEUI
:
1957 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1958 * so we check the DSAP and SSAP.
1960 * LLCSAP_IP checks for IP-over-802.2, rather
1961 * than IP-over-Ethernet or IP-over-SNAP.
1963 * XXX - should we check both the DSAP and the
1964 * SSAP, like this, or should we check just the
1965 * DSAP, as we do for other types <= ETHERMTU
1966 * (i.e., other SAP values)?
1968 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1969 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1970 ((proto
<< 8) | proto
));
1976 * Ethernet_II frames, which are Ethernet
1977 * frames with a frame type of ETHERTYPE_IPX;
1979 * Ethernet_802.3 frames, which have a frame
1980 * type of LINUX_SLL_P_802_3;
1982 * Ethernet_802.2 frames, which are 802.3
1983 * frames with an 802.2 LLC header (i.e, have
1984 * a frame type of LINUX_SLL_P_802_2) and
1985 * with the IPX LSAP as the DSAP in the LLC
1988 * Ethernet_SNAP frames, which are 802.3
1989 * frames with an LLC header and a SNAP
1990 * header and with an OUI of 0x000000
1991 * (encapsulated Ethernet) and a protocol
1992 * ID of ETHERTYPE_IPX in the SNAP header.
1994 * First, do the checks on LINUX_SLL_P_802_2
1995 * frames; generate the check for either
1996 * Ethernet_802.2 or Ethernet_SNAP frames, and
1997 * then put a check for LINUX_SLL_P_802_2 frames
2000 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2001 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2003 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2007 * Now check for 802.3 frames and OR that with
2008 * the previous test.
2010 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2014 * Now add the check for Ethernet_II frames, and
2015 * do that before checking for the other frame
2018 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2019 (bpf_int32
)ETHERTYPE_IPX
);
2023 case ETHERTYPE_ATALK
:
2024 case ETHERTYPE_AARP
:
2026 * EtherTalk (AppleTalk protocols on Ethernet link
2027 * layer) may use 802.2 encapsulation.
2031 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2032 * we check for the 802.2 protocol type in the
2033 * "Ethernet type" field.
2035 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2038 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2039 * SNAP packets with an organization code of
2040 * 0x080007 (Apple, for Appletalk) and a protocol
2041 * type of ETHERTYPE_ATALK (Appletalk).
2043 * 802.2-encapsulated ETHERTYPE_AARP packets are
2044 * SNAP packets with an organization code of
2045 * 0x000000 (encapsulated Ethernet) and a protocol
2046 * type of ETHERTYPE_AARP (Appletalk ARP).
2048 if (proto
== ETHERTYPE_ATALK
)
2049 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2050 else /* proto == ETHERTYPE_AARP */
2051 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2055 * Check for Ethernet encapsulation (Ethertalk
2056 * phase 1?); we just check for the Ethernet
2059 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2065 if (proto
<= ETHERMTU
) {
2067 * This is an LLC SAP value, so the frames
2068 * that match would be 802.2 frames.
2069 * Check for the 802.2 protocol type
2070 * in the "Ethernet type" field, and
2071 * then check the DSAP.
2073 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2075 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2081 * This is an Ethernet type, so compare
2082 * the length/type field with it (if
2083 * the frame is an 802.2 frame, the length
2084 * field will be <= ETHERMTU, and, as
2085 * "proto" is > ETHERMTU, this test
2086 * will fail and the frame won't match,
2087 * which is what we want).
2089 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2095 static struct slist
*
2096 gen_load_prism_llprefixlen()
2098 struct slist
*s1
, *s2
;
2099 struct slist
*sjeq_avs_cookie
;
2100 struct slist
*sjcommon
;
2103 * This code is not compatible with the optimizer, as
2104 * we are generating jmp instructions within a normal
2105 * slist of instructions
2110 * Generate code to load the length of the radio header into
2111 * the register assigned to hold that length, if one has been
2112 * assigned. (If one hasn't been assigned, no code we've
2113 * generated uses that prefix, so we don't need to generate any
2116 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2117 * or always use the AVS header rather than the Prism header.
2118 * We load a 4-byte big-endian value at the beginning of the
2119 * raw packet data, and see whether, when masked with 0xFFFFF000,
2120 * it's equal to 0x80211000. If so, that indicates that it's
2121 * an AVS header (the masked-out bits are the version number).
2122 * Otherwise, it's a Prism header.
2124 * XXX - the Prism header is also, in theory, variable-length,
2125 * but no known software generates headers that aren't 144
2128 if (reg_off_ll
!= -1) {
2132 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2136 * AND it with 0xFFFFF000.
2138 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2139 s2
->s
.k
= 0xFFFFF000;
2143 * Compare with 0x80211000.
2145 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2146 sjeq_avs_cookie
->s
.k
= 0x80211000;
2147 sappend(s1
, sjeq_avs_cookie
);
2152 * The 4 bytes at an offset of 4 from the beginning of
2153 * the AVS header are the length of the AVS header.
2154 * That field is big-endian.
2156 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2159 sjeq_avs_cookie
->s
.jt
= s2
;
2162 * Now jump to the code to allocate a register
2163 * into which to save the header length and
2164 * store the length there. (The "jump always"
2165 * instruction needs to have the k field set;
2166 * it's added to the PC, so, as we're jumping
2167 * over a single instruction, it should be 1.)
2169 sjcommon
= new_stmt(JMP(BPF_JA
));
2171 sappend(s1
, sjcommon
);
2174 * Now for the code that handles the Prism header.
2175 * Just load the length of the Prism header (144)
2176 * into the A register. Have the test for an AVS
2177 * header branch here if we don't have an AVS header.
2179 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2182 sjeq_avs_cookie
->s
.jf
= s2
;
2185 * Now allocate a register to hold that value and store
2186 * it. The code for the AVS header will jump here after
2187 * loading the length of the AVS header.
2189 s2
= new_stmt(BPF_ST
);
2190 s2
->s
.k
= reg_off_ll
;
2192 sjcommon
->s
.jf
= s2
;
2195 * Now move it into the X register.
2197 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2205 static struct slist
*
2206 gen_load_avs_llprefixlen()
2208 struct slist
*s1
, *s2
;
2211 * Generate code to load the length of the AVS header into
2212 * the register assigned to hold that length, if one has been
2213 * assigned. (If one hasn't been assigned, no code we've
2214 * generated uses that prefix, so we don't need to generate any
2217 if (reg_off_ll
!= -1) {
2219 * The 4 bytes at an offset of 4 from the beginning of
2220 * the AVS header are the length of the AVS header.
2221 * That field is big-endian.
2223 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2227 * Now allocate a register to hold that value and store
2230 s2
= new_stmt(BPF_ST
);
2231 s2
->s
.k
= reg_off_ll
;
2235 * Now move it into the X register.
2237 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2245 static struct slist
*
2246 gen_load_radiotap_llprefixlen()
2248 struct slist
*s1
, *s2
;
2251 * Generate code to load the length of the radiotap header into
2252 * the register assigned to hold that length, if one has been
2253 * assigned. (If one hasn't been assigned, no code we've
2254 * generated uses that prefix, so we don't need to generate any
2257 if (reg_off_ll
!= -1) {
2259 * The 2 bytes at offsets of 2 and 3 from the beginning
2260 * of the radiotap header are the length of the radiotap
2261 * header; unfortunately, it's little-endian, so we have
2262 * to load it a byte at a time and construct the value.
2266 * Load the high-order byte, at an offset of 3, shift it
2267 * left a byte, and put the result in the X register.
2269 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2271 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2274 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2278 * Load the next byte, at an offset of 2, and OR the
2279 * value from the X register into it.
2281 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2284 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2288 * Now allocate a register to hold that value and store
2291 s2
= new_stmt(BPF_ST
);
2292 s2
->s
.k
= reg_off_ll
;
2296 * Now move it into the X register.
2298 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2307 * At the moment we treat PPI as normal Radiotap encoded
2308 * packets. The difference is in the function that generates
2309 * the code at the beginning to compute the header length.
2310 * Since this code generator of PPI supports bare 802.11
2311 * encapsulation only (i.e. the encapsulated DLT should be
2312 * DLT_IEEE802_11) we generate code to check for this too;
2313 * that's done in finish_parse().
2315 static struct slist
*
2316 gen_load_ppi_llprefixlen()
2318 struct slist
*s1
, *s2
;
2321 * Generate code to load the length of the radiotap header
2322 * into the register assigned to hold that length, if one has
2325 if (reg_off_ll
!= -1) {
2327 * The 2 bytes at offsets of 2 and 3 from the beginning
2328 * of the radiotap header are the length of the radiotap
2329 * header; unfortunately, it's little-endian, so we have
2330 * to load it a byte at a time and construct the value.
2334 * Load the high-order byte, at an offset of 3, shift it
2335 * left a byte, and put the result in the X register.
2337 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2339 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2342 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2346 * Load the next byte, at an offset of 2, and OR the
2347 * value from the X register into it.
2349 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2352 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2356 * Now allocate a register to hold that value and store
2359 s2
= new_stmt(BPF_ST
);
2360 s2
->s
.k
= reg_off_ll
;
2364 * Now move it into the X register.
2366 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2375 * Load a value relative to the beginning of the link-layer header after the 802.11
2376 * header, i.e. LLC_SNAP.
2377 * The link-layer header doesn't necessarily begin at the beginning
2378 * of the packet data; there might be a variable-length prefix containing
2379 * radio information.
2381 static struct slist
*
2382 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2385 struct slist
*sjset_data_frame_1
;
2386 struct slist
*sjset_data_frame_2
;
2387 struct slist
*sjset_qos
;
2388 struct slist
*sjset_radiotap_flags
;
2389 struct slist
*sjset_radiotap_tsft
;
2390 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2391 struct slist
*s_roundup
;
2393 if (reg_off_macpl
== -1) {
2395 * No register has been assigned to the offset of
2396 * the MAC-layer payload, which means nobody needs
2397 * it; don't bother computing it - just return
2398 * what we already have.
2404 * This code is not compatible with the optimizer, as
2405 * we are generating jmp instructions within a normal
2406 * slist of instructions
2411 * If "s" is non-null, it has code to arrange that the X register
2412 * contains the length of the prefix preceding the link-layer
2415 * Otherwise, the length of the prefix preceding the link-layer
2416 * header is "off_ll".
2420 * There is no variable-length header preceding the
2421 * link-layer header.
2423 * Load the length of the fixed-length prefix preceding
2424 * the link-layer header (if any) into the X register,
2425 * and store it in the reg_off_macpl register.
2426 * That length is off_ll.
2428 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2433 * The X register contains the offset of the beginning of the
2434 * link-layer header; add 24, which is the minimum length
2435 * of the MAC header for a data frame, to that, and store it
2436 * in reg_off_macpl, and then load the Frame Control field,
2437 * which is at the offset in the X register, with an indexed load.
2439 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2441 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2444 s2
= new_stmt(BPF_ST
);
2445 s2
->s
.k
= reg_off_macpl
;
2448 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2453 * Check the Frame Control field to see if this is a data frame;
2454 * a data frame has the 0x08 bit (b3) in that field set and the
2455 * 0x04 bit (b2) clear.
2457 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2458 sjset_data_frame_1
->s
.k
= 0x08;
2459 sappend(s
, sjset_data_frame_1
);
2462 * If b3 is set, test b2, otherwise go to the first statement of
2463 * the rest of the program.
2465 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2466 sjset_data_frame_2
->s
.k
= 0x04;
2467 sappend(s
, sjset_data_frame_2
);
2468 sjset_data_frame_1
->s
.jf
= snext
;
2471 * If b2 is not set, this is a data frame; test the QoS bit.
2472 * Otherwise, go to the first statement of the rest of the
2475 sjset_data_frame_2
->s
.jt
= snext
;
2476 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2477 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2478 sappend(s
, sjset_qos
);
2481 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2483 * Otherwise, go to the first statement of the rest of the
2486 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2487 s2
->s
.k
= reg_off_macpl
;
2489 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2492 s2
= new_stmt(BPF_ST
);
2493 s2
->s
.k
= reg_off_macpl
;
2497 * If we have a radiotap header, look at it to see whether
2498 * there's Atheros padding between the MAC-layer header
2501 * Note: all of the fields in the radiotap header are
2502 * little-endian, so we byte-swap all of the values
2503 * we test against, as they will be loaded as big-endian
2506 if (linktype
== DLT_IEEE802_11_RADIO
) {
2508 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2509 * in the presence flag?
2511 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2515 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2516 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2517 sappend(s
, sjset_radiotap_flags
);
2520 * If not, skip all of this.
2522 sjset_radiotap_flags
->s
.jf
= snext
;
2525 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2527 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2528 new_stmt(JMP(BPF_JSET
));
2529 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2530 sappend(s
, sjset_radiotap_tsft
);
2533 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2534 * at an offset of 16 from the beginning of the raw packet
2535 * data (8 bytes for the radiotap header and 8 bytes for
2538 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2541 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2545 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2546 sjset_tsft_datapad
->s
.k
= 0x20;
2547 sappend(s
, sjset_tsft_datapad
);
2550 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2551 * at an offset of 8 from the beginning of the raw packet
2552 * data (8 bytes for the radiotap header).
2554 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2557 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2561 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2562 sjset_notsft_datapad
->s
.k
= 0x20;
2563 sappend(s
, sjset_notsft_datapad
);
2566 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2567 * set, round the length of the 802.11 header to
2568 * a multiple of 4. Do that by adding 3 and then
2569 * dividing by and multiplying by 4, which we do by
2572 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2573 s_roundup
->s
.k
= reg_off_macpl
;
2574 sappend(s
, s_roundup
);
2575 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2578 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2581 s2
= new_stmt(BPF_ST
);
2582 s2
->s
.k
= reg_off_macpl
;
2585 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2586 sjset_tsft_datapad
->s
.jf
= snext
;
2587 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2588 sjset_notsft_datapad
->s
.jf
= snext
;
2590 sjset_qos
->s
.jf
= snext
;
2596 insert_compute_vloffsets(b
)
2602 * For link-layer types that have a variable-length header
2603 * preceding the link-layer header, generate code to load
2604 * the offset of the link-layer header into the register
2605 * assigned to that offset, if any.
2609 case DLT_PRISM_HEADER
:
2610 s
= gen_load_prism_llprefixlen();
2613 case DLT_IEEE802_11_RADIO_AVS
:
2614 s
= gen_load_avs_llprefixlen();
2617 case DLT_IEEE802_11_RADIO
:
2618 s
= gen_load_radiotap_llprefixlen();
2622 s
= gen_load_ppi_llprefixlen();
2631 * For link-layer types that have a variable-length link-layer
2632 * header, generate code to load the offset of the MAC-layer
2633 * payload into the register assigned to that offset, if any.
2637 case DLT_IEEE802_11
:
2638 case DLT_PRISM_HEADER
:
2639 case DLT_IEEE802_11_RADIO_AVS
:
2640 case DLT_IEEE802_11_RADIO
:
2642 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2647 * If we have any offset-loading code, append all the
2648 * existing statements in the block to those statements,
2649 * and make the resulting list the list of statements
2653 sappend(s
, b
->stmts
);
2658 static struct block
*
2659 gen_ppi_dlt_check(void)
2661 struct slist
*s_load_dlt
;
2664 if (linktype
== DLT_PPI
)
2666 /* Create the statements that check for the DLT
2668 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2669 s_load_dlt
->s
.k
= 4;
2671 b
= new_block(JMP(BPF_JEQ
));
2673 b
->stmts
= s_load_dlt
;
2674 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2684 static struct slist
*
2685 gen_prism_llprefixlen(void)
2689 if (reg_off_ll
== -1) {
2691 * We haven't yet assigned a register for the length
2692 * of the radio header; allocate one.
2694 reg_off_ll
= alloc_reg();
2698 * Load the register containing the radio length
2699 * into the X register.
2701 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2702 s
->s
.k
= reg_off_ll
;
2706 static struct slist
*
2707 gen_avs_llprefixlen(void)
2711 if (reg_off_ll
== -1) {
2713 * We haven't yet assigned a register for the length
2714 * of the AVS header; allocate one.
2716 reg_off_ll
= alloc_reg();
2720 * Load the register containing the AVS length
2721 * into the X register.
2723 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2724 s
->s
.k
= reg_off_ll
;
2728 static struct slist
*
2729 gen_radiotap_llprefixlen(void)
2733 if (reg_off_ll
== -1) {
2735 * We haven't yet assigned a register for the length
2736 * of the radiotap header; allocate one.
2738 reg_off_ll
= alloc_reg();
2742 * Load the register containing the radiotap length
2743 * into the X register.
2745 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2746 s
->s
.k
= reg_off_ll
;
2751 * At the moment we treat PPI as normal Radiotap encoded
2752 * packets. The difference is in the function that generates
2753 * the code at the beginning to compute the header length.
2754 * Since this code generator of PPI supports bare 802.11
2755 * encapsulation only (i.e. the encapsulated DLT should be
2756 * DLT_IEEE802_11) we generate code to check for this too.
2758 static struct slist
*
2759 gen_ppi_llprefixlen(void)
2763 if (reg_off_ll
== -1) {
2765 * We haven't yet assigned a register for the length
2766 * of the radiotap header; allocate one.
2768 reg_off_ll
= alloc_reg();
2772 * Load the register containing the PPI length
2773 * into the X register.
2775 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2776 s
->s
.k
= reg_off_ll
;
2781 * Generate code to compute the link-layer header length, if necessary,
2782 * putting it into the X register, and to return either a pointer to a
2783 * "struct slist" for the list of statements in that code, or NULL if
2784 * no code is necessary.
2786 static struct slist
*
2787 gen_llprefixlen(void)
2791 case DLT_PRISM_HEADER
:
2792 return gen_prism_llprefixlen();
2794 case DLT_IEEE802_11_RADIO_AVS
:
2795 return gen_avs_llprefixlen();
2797 case DLT_IEEE802_11_RADIO
:
2798 return gen_radiotap_llprefixlen();
2801 return gen_ppi_llprefixlen();
2809 * Generate code to load the register containing the offset of the
2810 * MAC-layer payload into the X register; if no register for that offset
2811 * has been allocated, allocate it first.
2813 static struct slist
*
2818 if (off_macpl_is_variable
) {
2819 if (reg_off_macpl
== -1) {
2821 * We haven't yet assigned a register for the offset
2822 * of the MAC-layer payload; allocate one.
2824 reg_off_macpl
= alloc_reg();
2828 * Load the register containing the offset of the MAC-layer
2829 * payload into the X register.
2831 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2832 s
->s
.k
= reg_off_macpl
;
2836 * That offset isn't variable, so we don't need to
2837 * generate any code.
2844 * Map an Ethernet type to the equivalent PPP type.
2847 ethertype_to_ppptype(proto
)
2856 case ETHERTYPE_IPV6
:
2864 case ETHERTYPE_ATALK
:
2878 * I'm assuming the "Bridging PDU"s that go
2879 * over PPP are Spanning Tree Protocol
2893 * Generate code to match a particular packet type by matching the
2894 * link-layer type field or fields in the 802.2 LLC header.
2896 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2897 * value, if <= ETHERMTU.
2899 static struct block
*
2903 struct block
*b0
, *b1
, *b2
;
2905 /* are we checking MPLS-encapsulated packets? */
2906 if (label_stack_depth
> 0) {
2910 /* FIXME add other L3 proto IDs */
2911 return gen_mpls_linktype(Q_IP
);
2913 case ETHERTYPE_IPV6
:
2915 /* FIXME add other L3 proto IDs */
2916 return gen_mpls_linktype(Q_IPV6
);
2919 bpf_error("unsupported protocol over mpls");
2925 * Are we testing PPPoE packets?
2929 * The PPPoE session header is part of the
2930 * MAC-layer payload, so all references
2931 * should be relative to the beginning of
2936 * We use Ethernet protocol types inside libpcap;
2937 * map them to the corresponding PPP protocol types.
2939 proto
= ethertype_to_ppptype(proto
);
2940 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2946 case DLT_NETANALYZER
:
2947 case DLT_NETANALYZER_TRANSPARENT
:
2948 return gen_ether_linktype(proto
);
2956 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2960 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2967 case DLT_IEEE802_11
:
2968 case DLT_PRISM_HEADER
:
2969 case DLT_IEEE802_11_RADIO_AVS
:
2970 case DLT_IEEE802_11_RADIO
:
2973 * Check that we have a data frame.
2975 b0
= gen_check_802_11_data_frame();
2978 * Now check for the specified link-layer type.
2980 b1
= gen_llc_linktype(proto
);
2988 * XXX - check for asynchronous frames, as per RFC 1103.
2990 return gen_llc_linktype(proto
);
2996 * XXX - check for LLC PDUs, as per IEEE 802.5.
2998 return gen_llc_linktype(proto
);
3002 case DLT_ATM_RFC1483
:
3004 case DLT_IP_OVER_FC
:
3005 return gen_llc_linktype(proto
);
3011 * If "is_lane" is set, check for a LANE-encapsulated
3012 * version of this protocol, otherwise check for an
3013 * LLC-encapsulated version of this protocol.
3015 * We assume LANE means Ethernet, not Token Ring.
3019 * Check that the packet doesn't begin with an
3020 * LE Control marker. (We've already generated
3023 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3028 * Now generate an Ethernet test.
3030 b1
= gen_ether_linktype(proto
);
3035 * Check for LLC encapsulation and then check the
3038 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3039 b1
= gen_llc_linktype(proto
);
3047 return gen_linux_sll_linktype(proto
);
3052 case DLT_SLIP_BSDOS
:
3055 * These types don't provide any type field; packets
3056 * are always IPv4 or IPv6.
3058 * XXX - for IPv4, check for a version number of 4, and,
3059 * for IPv6, check for a version number of 6?
3064 /* Check for a version number of 4. */
3065 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3067 case ETHERTYPE_IPV6
:
3068 /* Check for a version number of 6. */
3069 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3072 return gen_false(); /* always false */
3079 * Raw IPv4, so no type field.
3081 if (proto
== ETHERTYPE_IP
)
3082 return gen_true(); /* always true */
3084 /* Checking for something other than IPv4; always false */
3091 * Raw IPv6, so no type field.
3093 if (proto
== ETHERTYPE_IPV6
)
3094 return gen_true(); /* always true */
3096 /* Checking for something other than IPv6; always false */
3103 case DLT_PPP_SERIAL
:
3106 * We use Ethernet protocol types inside libpcap;
3107 * map them to the corresponding PPP protocol types.
3109 proto
= ethertype_to_ppptype(proto
);
3110 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3116 * We use Ethernet protocol types inside libpcap;
3117 * map them to the corresponding PPP protocol types.
3123 * Also check for Van Jacobson-compressed IP.
3124 * XXX - do this for other forms of PPP?
3126 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3127 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3129 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3134 proto
= ethertype_to_ppptype(proto
);
3135 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3145 * For DLT_NULL, the link-layer header is a 32-bit
3146 * word containing an AF_ value in *host* byte order,
3147 * and for DLT_ENC, the link-layer header begins
3148 * with a 32-bit work containing an AF_ value in
3151 * In addition, if we're reading a saved capture file,
3152 * the host byte order in the capture may not be the
3153 * same as the host byte order on this machine.
3155 * For DLT_LOOP, the link-layer header is a 32-bit
3156 * word containing an AF_ value in *network* byte order.
3158 * XXX - AF_ values may, unfortunately, be platform-
3159 * dependent; for example, FreeBSD's AF_INET6 is 24
3160 * whilst NetBSD's and OpenBSD's is 26.
3162 * This means that, when reading a capture file, just
3163 * checking for our AF_INET6 value won't work if the
3164 * capture file came from another OS.
3173 case ETHERTYPE_IPV6
:
3180 * Not a type on which we support filtering.
3181 * XXX - support those that have AF_ values
3182 * #defined on this platform, at least?
3187 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3189 * The AF_ value is in host byte order, but
3190 * the BPF interpreter will convert it to
3191 * network byte order.
3193 * If this is a save file, and it's from a
3194 * machine with the opposite byte order to
3195 * ours, we byte-swap the AF_ value.
3197 * Then we run it through "htonl()", and
3198 * generate code to compare against the result.
3200 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3201 proto
= SWAPLONG(proto
);
3202 proto
= htonl(proto
);
3204 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3206 #ifdef HAVE_NET_PFVAR_H
3209 * af field is host byte order in contrast to the rest of
3212 if (proto
== ETHERTYPE_IP
)
3213 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3214 BPF_B
, (bpf_int32
)AF_INET
));
3215 else if (proto
== ETHERTYPE_IPV6
)
3216 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3217 BPF_B
, (bpf_int32
)AF_INET6
));
3222 #endif /* HAVE_NET_PFVAR_H */
3225 case DLT_ARCNET_LINUX
:
3227 * XXX should we check for first fragment if the protocol
3235 case ETHERTYPE_IPV6
:
3236 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3237 (bpf_int32
)ARCTYPE_INET6
));
3240 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3241 (bpf_int32
)ARCTYPE_IP
);
3242 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3243 (bpf_int32
)ARCTYPE_IP_OLD
);
3248 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3249 (bpf_int32
)ARCTYPE_ARP
);
3250 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3251 (bpf_int32
)ARCTYPE_ARP_OLD
);
3255 case ETHERTYPE_REVARP
:
3256 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3257 (bpf_int32
)ARCTYPE_REVARP
));
3259 case ETHERTYPE_ATALK
:
3260 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3261 (bpf_int32
)ARCTYPE_ATALK
));
3268 case ETHERTYPE_ATALK
:
3278 * XXX - assumes a 2-byte Frame Relay header with
3279 * DLCI and flags. What if the address is longer?
3285 * Check for the special NLPID for IP.
3287 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3289 case ETHERTYPE_IPV6
:
3291 * Check for the special NLPID for IPv6.
3293 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3297 * Check for several OSI protocols.
3299 * Frame Relay packets typically have an OSI
3300 * NLPID at the beginning; we check for each
3303 * What we check for is the NLPID and a frame
3304 * control field of UI, i.e. 0x03 followed
3307 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3308 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3309 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3321 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3323 case DLT_JUNIPER_MFR
:
3324 case DLT_JUNIPER_MLFR
:
3325 case DLT_JUNIPER_MLPPP
:
3326 case DLT_JUNIPER_ATM1
:
3327 case DLT_JUNIPER_ATM2
:
3328 case DLT_JUNIPER_PPPOE
:
3329 case DLT_JUNIPER_PPPOE_ATM
:
3330 case DLT_JUNIPER_GGSN
:
3331 case DLT_JUNIPER_ES
:
3332 case DLT_JUNIPER_MONITOR
:
3333 case DLT_JUNIPER_SERVICES
:
3334 case DLT_JUNIPER_ETHER
:
3335 case DLT_JUNIPER_PPP
:
3336 case DLT_JUNIPER_FRELAY
:
3337 case DLT_JUNIPER_CHDLC
:
3338 case DLT_JUNIPER_VP
:
3339 case DLT_JUNIPER_ST
:
3340 case DLT_JUNIPER_ISM
:
3341 case DLT_JUNIPER_VS
:
3342 case DLT_JUNIPER_SRX_E2E
:
3343 case DLT_JUNIPER_FIBRECHANNEL
:
3344 case DLT_JUNIPER_ATM_CEMIC
:
3346 /* just lets verify the magic number for now -
3347 * on ATM we may have up to 6 different encapsulations on the wire
3348 * and need a lot of heuristics to figure out that the payload
3351 * FIXME encapsulation specific BPF_ filters
3353 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3355 case DLT_BACNET_MS_TP
:
3356 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3359 return gen_ipnet_linktype(proto
);
3361 case DLT_LINUX_IRDA
:
3362 bpf_error("IrDA link-layer type filtering not implemented");
3365 bpf_error("DOCSIS link-layer type filtering not implemented");
3368 case DLT_MTP2_WITH_PHDR
:
3369 bpf_error("MTP2 link-layer type filtering not implemented");
3372 bpf_error("ERF link-layer type filtering not implemented");
3375 bpf_error("PFSYNC link-layer type filtering not implemented");
3377 case DLT_LINUX_LAPD
:
3378 bpf_error("LAPD link-layer type filtering not implemented");
3382 case DLT_USB_LINUX_MMAPPED
:
3383 bpf_error("USB link-layer type filtering not implemented");
3385 case DLT_BLUETOOTH_HCI_H4
:
3386 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3387 bpf_error("Bluetooth link-layer type filtering not implemented");
3390 case DLT_CAN_SOCKETCAN
:
3391 bpf_error("CAN link-layer type filtering not implemented");
3393 case DLT_IEEE802_15_4
:
3394 case DLT_IEEE802_15_4_LINUX
:
3395 case DLT_IEEE802_15_4_NONASK_PHY
:
3396 case DLT_IEEE802_15_4_NOFCS
:
3397 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3399 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3400 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3403 bpf_error("SITA link-layer type filtering not implemented");
3406 bpf_error("RAIF1 link-layer type filtering not implemented");
3409 bpf_error("IPMB link-layer type filtering not implemented");
3412 bpf_error("AX.25 link-layer type filtering not implemented");
3416 * All the types that have no encapsulation should either be
3417 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3418 * all packets are IP packets, or should be handled in some
3419 * special case, if none of them are (if some are and some
3420 * aren't, the lack of encapsulation is a problem, as we'd
3421 * have to find some other way of determining the packet type).
3423 * Therefore, if "off_linktype" is -1, there's an error.
3425 if (off_linktype
== (u_int
)-1)
3429 * Any type not handled above should always have an Ethernet
3430 * type at an offset of "off_linktype".
3432 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3436 * Check for an LLC SNAP packet with a given organization code and
3437 * protocol type; we check the entire contents of the 802.2 LLC and
3438 * snap headers, checking for DSAP and SSAP of SNAP and a control
3439 * field of 0x03 in the LLC header, and for the specified organization
3440 * code and protocol type in the SNAP header.
3442 static struct block
*
3443 gen_snap(orgcode
, ptype
)
3444 bpf_u_int32 orgcode
;
3447 u_char snapblock
[8];
3449 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3450 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3451 snapblock
[2] = 0x03; /* control = UI */
3452 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3453 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3454 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3455 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3456 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3457 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3461 * Generate code to match a particular packet type, for link-layer types
3462 * using 802.2 LLC headers.
3464 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3465 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3467 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3468 * value, if <= ETHERMTU. We use that to determine whether to
3469 * match the DSAP or both DSAP and LSAP or to check the OUI and
3470 * protocol ID in a SNAP header.
3472 static struct block
*
3473 gen_llc_linktype(proto
)
3477 * XXX - handle token-ring variable-length header.
3483 case LLCSAP_NETBEUI
:
3485 * XXX - should we check both the DSAP and the
3486 * SSAP, like this, or should we check just the
3487 * DSAP, as we do for other types <= ETHERMTU
3488 * (i.e., other SAP values)?
3490 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3491 ((proto
<< 8) | proto
));
3495 * XXX - are there ever SNAP frames for IPX on
3496 * non-Ethernet 802.x networks?
3498 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3499 (bpf_int32
)LLCSAP_IPX
);
3501 case ETHERTYPE_ATALK
:
3503 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3504 * SNAP packets with an organization code of
3505 * 0x080007 (Apple, for Appletalk) and a protocol
3506 * type of ETHERTYPE_ATALK (Appletalk).
3508 * XXX - check for an organization code of
3509 * encapsulated Ethernet as well?
3511 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3515 * XXX - we don't have to check for IPX 802.3
3516 * here, but should we check for the IPX Ethertype?
3518 if (proto
<= ETHERMTU
) {
3520 * This is an LLC SAP value, so check
3523 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3526 * This is an Ethernet type; we assume that it's
3527 * unlikely that it'll appear in the right place
3528 * at random, and therefore check only the
3529 * location that would hold the Ethernet type
3530 * in a SNAP frame with an organization code of
3531 * 0x000000 (encapsulated Ethernet).
3533 * XXX - if we were to check for the SNAP DSAP and
3534 * LSAP, as per XXX, and were also to check for an
3535 * organization code of 0x000000 (encapsulated
3536 * Ethernet), we'd do
3538 * return gen_snap(0x000000, proto);
3540 * here; for now, we don't, as per the above.
3541 * I don't know whether it's worth the extra CPU
3542 * time to do the right check or not.
3544 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3549 static struct block
*
3550 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3554 u_int src_off
, dst_off
;
3556 struct block
*b0
, *b1
;
3570 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3571 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3577 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3578 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3585 b0
= gen_linktype(proto
);
3586 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3592 static struct block
*
3593 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3594 struct in6_addr
*addr
;
3595 struct in6_addr
*mask
;
3597 u_int src_off
, dst_off
;
3599 struct block
*b0
, *b1
;
3614 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3615 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3621 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3622 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3629 /* this order is important */
3630 a
= (u_int32_t
*)addr
;
3631 m
= (u_int32_t
*)mask
;
3632 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3633 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3635 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3637 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3639 b0
= gen_linktype(proto
);
3645 static struct block
*
3646 gen_ehostop(eaddr
, dir
)
3647 register const u_char
*eaddr
;
3650 register struct block
*b0
, *b1
;
3654 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3657 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3660 b0
= gen_ehostop(eaddr
, Q_SRC
);
3661 b1
= gen_ehostop(eaddr
, Q_DST
);
3667 b0
= gen_ehostop(eaddr
, Q_SRC
);
3668 b1
= gen_ehostop(eaddr
, Q_DST
);
3673 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3677 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3681 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3685 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3689 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3693 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3701 * Like gen_ehostop, but for DLT_FDDI
3703 static struct block
*
3704 gen_fhostop(eaddr
, dir
)
3705 register const u_char
*eaddr
;
3708 struct block
*b0
, *b1
;
3712 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3715 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3718 b0
= gen_fhostop(eaddr
, Q_SRC
);
3719 b1
= gen_fhostop(eaddr
, Q_DST
);
3725 b0
= gen_fhostop(eaddr
, Q_SRC
);
3726 b1
= gen_fhostop(eaddr
, Q_DST
);
3731 bpf_error("'addr1' is only supported on 802.11");
3735 bpf_error("'addr2' is only supported on 802.11");
3739 bpf_error("'addr3' is only supported on 802.11");
3743 bpf_error("'addr4' is only supported on 802.11");
3747 bpf_error("'ra' is only supported on 802.11");
3751 bpf_error("'ta' is only supported on 802.11");
3759 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3761 static struct block
*
3762 gen_thostop(eaddr
, dir
)
3763 register const u_char
*eaddr
;
3766 register struct block
*b0
, *b1
;
3770 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3773 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3776 b0
= gen_thostop(eaddr
, Q_SRC
);
3777 b1
= gen_thostop(eaddr
, Q_DST
);
3783 b0
= gen_thostop(eaddr
, Q_SRC
);
3784 b1
= gen_thostop(eaddr
, Q_DST
);
3789 bpf_error("'addr1' is only supported on 802.11");
3793 bpf_error("'addr2' is only supported on 802.11");
3797 bpf_error("'addr3' is only supported on 802.11");
3801 bpf_error("'addr4' is only supported on 802.11");
3805 bpf_error("'ra' is only supported on 802.11");
3809 bpf_error("'ta' is only supported on 802.11");
3817 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3818 * various 802.11 + radio headers.
3820 static struct block
*
3821 gen_wlanhostop(eaddr
, dir
)
3822 register const u_char
*eaddr
;
3825 register struct block
*b0
, *b1
, *b2
;
3826 register struct slist
*s
;
3828 #ifdef ENABLE_WLAN_FILTERING_PATCH
3831 * We need to disable the optimizer because the optimizer is buggy
3832 * and wipes out some LD instructions generated by the below
3833 * code to validate the Frame Control bits
3836 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3843 * For control frames, there is no SA.
3845 * For management frames, SA is at an
3846 * offset of 10 from the beginning of
3849 * For data frames, SA is at an offset
3850 * of 10 from the beginning of the packet
3851 * if From DS is clear, at an offset of
3852 * 16 from the beginning of the packet
3853 * if From DS is set and To DS is clear,
3854 * and an offset of 24 from the beginning
3855 * of the packet if From DS is set and To DS
3860 * Generate the tests to be done for data frames
3863 * First, check for To DS set, i.e. check "link[1] & 0x01".
3865 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3866 b1
= new_block(JMP(BPF_JSET
));
3867 b1
->s
.k
= 0x01; /* To DS */
3871 * If To DS is set, the SA is at 24.
3873 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3877 * Now, check for To DS not set, i.e. check
3878 * "!(link[1] & 0x01)".
3880 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3881 b2
= new_block(JMP(BPF_JSET
));
3882 b2
->s
.k
= 0x01; /* To DS */
3887 * If To DS is not set, the SA is at 16.
3889 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3893 * Now OR together the last two checks. That gives
3894 * the complete set of checks for data frames with
3900 * Now check for From DS being set, and AND that with
3901 * the ORed-together checks.
3903 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3904 b1
= new_block(JMP(BPF_JSET
));
3905 b1
->s
.k
= 0x02; /* From DS */
3910 * Now check for data frames with From DS not set.
3912 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3913 b2
= new_block(JMP(BPF_JSET
));
3914 b2
->s
.k
= 0x02; /* From DS */
3919 * If From DS isn't set, the SA is at 10.
3921 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3925 * Now OR together the checks for data frames with
3926 * From DS not set and for data frames with From DS
3927 * set; that gives the checks done for data frames.
3932 * Now check for a data frame.
3933 * I.e, check "link[0] & 0x08".
3935 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3936 b1
= new_block(JMP(BPF_JSET
));
3941 * AND that with the checks done for data frames.
3946 * If the high-order bit of the type value is 0, this
3947 * is a management frame.
3948 * I.e, check "!(link[0] & 0x08)".
3950 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3951 b2
= new_block(JMP(BPF_JSET
));
3957 * For management frames, the SA is at 10.
3959 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3963 * OR that with the checks done for data frames.
3964 * That gives the checks done for management and
3970 * If the low-order bit of the type value is 1,
3971 * this is either a control frame or a frame
3972 * with a reserved type, and thus not a
3975 * I.e., check "!(link[0] & 0x04)".
3977 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3978 b1
= new_block(JMP(BPF_JSET
));
3984 * AND that with the checks for data and management
3994 * For control frames, there is no DA.
3996 * For management frames, DA is at an
3997 * offset of 4 from the beginning of
4000 * For data frames, DA is at an offset
4001 * of 4 from the beginning of the packet
4002 * if To DS is clear and at an offset of
4003 * 16 from the beginning of the packet
4008 * Generate the tests to be done for data frames.
4010 * First, check for To DS set, i.e. "link[1] & 0x01".
4012 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4013 b1
= new_block(JMP(BPF_JSET
));
4014 b1
->s
.k
= 0x01; /* To DS */
4018 * If To DS is set, the DA is at 16.
4020 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4024 * Now, check for To DS not set, i.e. check
4025 * "!(link[1] & 0x01)".
4027 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
4028 b2
= new_block(JMP(BPF_JSET
));
4029 b2
->s
.k
= 0x01; /* To DS */
4034 * If To DS is not set, the DA is at 4.
4036 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4040 * Now OR together the last two checks. That gives
4041 * the complete set of checks for data frames.
4046 * Now check for a data frame.
4047 * I.e, check "link[0] & 0x08".
4049 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4050 b1
= new_block(JMP(BPF_JSET
));
4055 * AND that with the checks done for data frames.
4060 * If the high-order bit of the type value is 0, this
4061 * is a management frame.
4062 * I.e, check "!(link[0] & 0x08)".
4064 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4065 b2
= new_block(JMP(BPF_JSET
));
4071 * For management frames, the DA is at 4.
4073 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4077 * OR that with the checks done for data frames.
4078 * That gives the checks done for management and
4084 * If the low-order bit of the type value is 1,
4085 * this is either a control frame or a frame
4086 * with a reserved type, and thus not a
4089 * I.e., check "!(link[0] & 0x04)".
4091 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4092 b1
= new_block(JMP(BPF_JSET
));
4098 * AND that with the checks for data and management
4106 * Not present in management frames; addr1 in other
4111 * If the high-order bit of the type value is 0, this
4112 * is a management frame.
4113 * I.e, check "(link[0] & 0x08)".
4115 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4116 b1
= new_block(JMP(BPF_JSET
));
4123 b0
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4126 * AND that with the check of addr1.
4133 * Not present in management frames; addr2, if present,
4138 * Not present in CTS or ACK control frames.
4140 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4141 IEEE80211_FC0_TYPE_MASK
);
4143 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4144 IEEE80211_FC0_SUBTYPE_MASK
);
4146 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4147 IEEE80211_FC0_SUBTYPE_MASK
);
4153 * If the high-order bit of the type value is 0, this
4154 * is a management frame.
4155 * I.e, check "(link[0] & 0x08)".
4157 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4158 b1
= new_block(JMP(BPF_JSET
));
4163 * AND that with the check for frames other than
4164 * CTS and ACK frames.
4171 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4176 * XXX - add BSSID keyword?
4179 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4183 * Not present in CTS or ACK control frames.
4185 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4186 IEEE80211_FC0_TYPE_MASK
);
4188 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4189 IEEE80211_FC0_SUBTYPE_MASK
);
4191 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4192 IEEE80211_FC0_SUBTYPE_MASK
);
4196 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4202 * Not present in control frames.
4204 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4205 IEEE80211_FC0_TYPE_MASK
);
4207 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4213 * Present only if the direction mask has both "From DS"
4214 * and "To DS" set. Neither control frames nor management
4215 * frames should have both of those set, so we don't
4216 * check the frame type.
4218 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4219 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4220 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4225 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4226 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4232 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4233 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4242 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4243 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4244 * as the RFC states.)
4246 static struct block
*
4247 gen_ipfchostop(eaddr
, dir
)
4248 register const u_char
*eaddr
;
4251 register struct block
*b0
, *b1
;
4255 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4258 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4261 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4262 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4268 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4269 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4274 bpf_error("'addr1' is only supported on 802.11");
4278 bpf_error("'addr2' is only supported on 802.11");
4282 bpf_error("'addr3' is only supported on 802.11");
4286 bpf_error("'addr4' is only supported on 802.11");
4290 bpf_error("'ra' is only supported on 802.11");
4294 bpf_error("'ta' is only supported on 802.11");
4302 * This is quite tricky because there may be pad bytes in front of the
4303 * DECNET header, and then there are two possible data packet formats that
4304 * carry both src and dst addresses, plus 5 packet types in a format that
4305 * carries only the src node, plus 2 types that use a different format and
4306 * also carry just the src node.
4310 * Instead of doing those all right, we just look for data packets with
4311 * 0 or 1 bytes of padding. If you want to look at other packets, that
4312 * will require a lot more hacking.
4314 * To add support for filtering on DECNET "areas" (network numbers)
4315 * one would want to add a "mask" argument to this routine. That would
4316 * make the filter even more inefficient, although one could be clever
4317 * and not generate masking instructions if the mask is 0xFFFF.
4319 static struct block
*
4320 gen_dnhostop(addr
, dir
)
4324 struct block
*b0
, *b1
, *b2
, *tmp
;
4325 u_int offset_lh
; /* offset if long header is received */
4326 u_int offset_sh
; /* offset if short header is received */
4331 offset_sh
= 1; /* follows flags */
4332 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4336 offset_sh
= 3; /* follows flags, dstnode */
4337 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4341 /* Inefficient because we do our Calvinball dance twice */
4342 b0
= gen_dnhostop(addr
, Q_SRC
);
4343 b1
= gen_dnhostop(addr
, Q_DST
);
4349 /* Inefficient because we do our Calvinball dance twice */
4350 b0
= gen_dnhostop(addr
, Q_SRC
);
4351 b1
= gen_dnhostop(addr
, Q_DST
);
4356 bpf_error("ISO host filtering not implemented");
4361 b0
= gen_linktype(ETHERTYPE_DN
);
4362 /* Check for pad = 1, long header case */
4363 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4364 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4365 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4366 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4368 /* Check for pad = 0, long header case */
4369 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4370 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4373 /* Check for pad = 1, short header case */
4374 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4375 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4376 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4379 /* Check for pad = 0, short header case */
4380 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4381 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4385 /* Combine with test for linktype */
4391 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4392 * test the bottom-of-stack bit, and then check the version number
4393 * field in the IP header.
4395 static struct block
*
4396 gen_mpls_linktype(proto
)
4399 struct block
*b0
, *b1
;
4404 /* match the bottom-of-stack bit */
4405 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4406 /* match the IPv4 version number */
4407 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4412 /* match the bottom-of-stack bit */
4413 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4414 /* match the IPv4 version number */
4415 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4424 static struct block
*
4425 gen_host(addr
, mask
, proto
, dir
, type
)
4432 struct block
*b0
, *b1
;
4433 const char *typestr
;
4443 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4445 * Only check for non-IPv4 addresses if we're not
4446 * checking MPLS-encapsulated packets.
4448 if (label_stack_depth
== 0) {
4449 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4451 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4457 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4460 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4463 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4466 bpf_error("'tcp' modifier applied to %s", typestr
);
4469 bpf_error("'sctp' modifier applied to %s", typestr
);
4472 bpf_error("'udp' modifier applied to %s", typestr
);
4475 bpf_error("'icmp' modifier applied to %s", typestr
);
4478 bpf_error("'igmp' modifier applied to %s", typestr
);
4481 bpf_error("'igrp' modifier applied to %s", typestr
);
4484 bpf_error("'pim' modifier applied to %s", typestr
);
4487 bpf_error("'vrrp' modifier applied to %s", typestr
);
4490 bpf_error("'carp' modifier applied to %s", typestr
);
4493 bpf_error("ATALK host filtering not implemented");
4496 bpf_error("AARP host filtering not implemented");
4499 return gen_dnhostop(addr
, dir
);
4502 bpf_error("SCA host filtering not implemented");
4505 bpf_error("LAT host filtering not implemented");
4508 bpf_error("MOPDL host filtering not implemented");
4511 bpf_error("MOPRC host filtering not implemented");
4514 bpf_error("'ip6' modifier applied to ip host");
4517 bpf_error("'icmp6' modifier applied to %s", typestr
);
4520 bpf_error("'ah' modifier applied to %s", typestr
);
4523 bpf_error("'esp' modifier applied to %s", typestr
);
4526 bpf_error("ISO host filtering not implemented");
4529 bpf_error("'esis' modifier applied to %s", typestr
);
4532 bpf_error("'isis' modifier applied to %s", typestr
);
4535 bpf_error("'clnp' modifier applied to %s", typestr
);
4538 bpf_error("'stp' modifier applied to %s", typestr
);
4541 bpf_error("IPX host filtering not implemented");
4544 bpf_error("'netbeui' modifier applied to %s", typestr
);
4547 bpf_error("'radio' modifier applied to %s", typestr
);
4556 static struct block
*
4557 gen_host6(addr
, mask
, proto
, dir
, type
)
4558 struct in6_addr
*addr
;
4559 struct in6_addr
*mask
;
4564 const char *typestr
;
4574 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4577 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4580 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4583 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4586 bpf_error("'sctp' modifier applied to %s", typestr
);
4589 bpf_error("'tcp' modifier applied to %s", typestr
);
4592 bpf_error("'udp' modifier applied to %s", typestr
);
4595 bpf_error("'icmp' modifier applied to %s", typestr
);
4598 bpf_error("'igmp' modifier applied to %s", typestr
);
4601 bpf_error("'igrp' modifier applied to %s", typestr
);
4604 bpf_error("'pim' modifier applied to %s", typestr
);
4607 bpf_error("'vrrp' modifier applied to %s", typestr
);
4610 bpf_error("'carp' modifier applied to %s", typestr
);
4613 bpf_error("ATALK host filtering not implemented");
4616 bpf_error("AARP host filtering not implemented");
4619 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4622 bpf_error("SCA host filtering not implemented");
4625 bpf_error("LAT host filtering not implemented");
4628 bpf_error("MOPDL host filtering not implemented");
4631 bpf_error("MOPRC host filtering not implemented");
4634 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4637 bpf_error("'icmp6' modifier applied to %s", typestr
);
4640 bpf_error("'ah' modifier applied to %s", typestr
);
4643 bpf_error("'esp' modifier applied to %s", typestr
);
4646 bpf_error("ISO host filtering not implemented");
4649 bpf_error("'esis' modifier applied to %s", typestr
);
4652 bpf_error("'isis' modifier applied to %s", typestr
);
4655 bpf_error("'clnp' modifier applied to %s", typestr
);
4658 bpf_error("'stp' modifier applied to %s", typestr
);
4661 bpf_error("IPX host filtering not implemented");
4664 bpf_error("'netbeui' modifier applied to %s", typestr
);
4667 bpf_error("'radio' modifier applied to %s", typestr
);
4677 static struct block
*
4678 gen_gateway(eaddr
, alist
, proto
, dir
)
4679 const u_char
*eaddr
;
4680 bpf_u_int32
**alist
;
4684 struct block
*b0
, *b1
, *tmp
;
4687 bpf_error("direction applied to 'gateway'");
4696 case DLT_NETANALYZER
:
4697 case DLT_NETANALYZER_TRANSPARENT
:
4698 b0
= gen_ehostop(eaddr
, Q_OR
);
4701 b0
= gen_fhostop(eaddr
, Q_OR
);
4704 b0
= gen_thostop(eaddr
, Q_OR
);
4706 case DLT_IEEE802_11
:
4707 case DLT_PRISM_HEADER
:
4708 case DLT_IEEE802_11_RADIO_AVS
:
4709 case DLT_IEEE802_11_RADIO
:
4711 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4716 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4718 * Check that the packet doesn't begin with an
4719 * LE Control marker. (We've already generated
4722 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4727 * Now check the MAC address.
4729 b0
= gen_ehostop(eaddr
, Q_OR
);
4732 case DLT_IP_OVER_FC
:
4733 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4737 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4739 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4741 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4750 bpf_error("illegal modifier of 'gateway'");
4756 gen_proto_abbrev(proto
)
4765 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4766 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4771 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4772 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4777 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4778 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4783 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4786 #ifndef IPPROTO_IGMP
4787 #define IPPROTO_IGMP 2
4791 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4794 #ifndef IPPROTO_IGRP
4795 #define IPPROTO_IGRP 9
4798 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4802 #define IPPROTO_PIM 103
4806 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4807 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4811 #ifndef IPPROTO_VRRP
4812 #define IPPROTO_VRRP 112
4816 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4819 #ifndef IPPROTO_CARP
4820 #define IPPROTO_CARP 112
4824 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4828 b1
= gen_linktype(ETHERTYPE_IP
);
4832 b1
= gen_linktype(ETHERTYPE_ARP
);
4836 b1
= gen_linktype(ETHERTYPE_REVARP
);
4840 bpf_error("link layer applied in wrong context");
4843 b1
= gen_linktype(ETHERTYPE_ATALK
);
4847 b1
= gen_linktype(ETHERTYPE_AARP
);
4851 b1
= gen_linktype(ETHERTYPE_DN
);
4855 b1
= gen_linktype(ETHERTYPE_SCA
);
4859 b1
= gen_linktype(ETHERTYPE_LAT
);
4863 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4867 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4871 b1
= gen_linktype(ETHERTYPE_IPV6
);
4874 #ifndef IPPROTO_ICMPV6
4875 #define IPPROTO_ICMPV6 58
4878 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4882 #define IPPROTO_AH 51
4885 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4886 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4891 #define IPPROTO_ESP 50
4894 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4895 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4900 b1
= gen_linktype(LLCSAP_ISONS
);
4904 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4908 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4911 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4912 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4913 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4915 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4917 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4919 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4923 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4924 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4925 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4927 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4929 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4931 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4935 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4936 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4937 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4939 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4944 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4945 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4950 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4951 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4953 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4955 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4960 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4961 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4966 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4967 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4972 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4976 b1
= gen_linktype(LLCSAP_8021D
);
4980 b1
= gen_linktype(LLCSAP_IPX
);
4984 b1
= gen_linktype(LLCSAP_NETBEUI
);
4988 bpf_error("'radio' is not a valid protocol type");
4996 static struct block
*
5002 /* not IPv4 frag other than the first frag */
5003 s
= gen_load_a(OR_NET
, 6, BPF_H
);
5004 b
= new_block(JMP(BPF_JSET
));
5013 * Generate a comparison to a port value in the transport-layer header
5014 * at the specified offset from the beginning of that header.
5016 * XXX - this handles a variable-length prefix preceding the link-layer
5017 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5018 * variable-length link-layer headers (such as Token Ring or 802.11
5021 static struct block
*
5022 gen_portatom(off
, v
)
5026 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5029 static struct block
*
5030 gen_portatom6(off
, v
)
5034 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5038 gen_portop(port
, proto
, dir
)
5039 int port
, proto
, dir
;
5041 struct block
*b0
, *b1
, *tmp
;
5043 /* ip proto 'proto' and not a fragment other than the first fragment */
5044 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5050 b1
= gen_portatom(0, (bpf_int32
)port
);
5054 b1
= gen_portatom(2, (bpf_int32
)port
);
5059 tmp
= 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
);
5078 static struct block
*
5079 gen_port(port
, ip_proto
, dir
)
5084 struct block
*b0
, *b1
, *tmp
;
5089 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5090 * not LLC encapsulation with LLCSAP_IP.
5092 * For IEEE 802 networks - which includes 802.5 token ring
5093 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5094 * says that SNAP encapsulation is used, not LLC encapsulation
5097 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5098 * RFC 2225 say that SNAP encapsulation is used, not LLC
5099 * encapsulation with LLCSAP_IP.
5101 * So we always check for ETHERTYPE_IP.
5103 b0
= gen_linktype(ETHERTYPE_IP
);
5109 b1
= gen_portop(port
, ip_proto
, dir
);
5113 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5114 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5116 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5128 gen_portop6(port
, proto
, dir
)
5129 int port
, proto
, dir
;
5131 struct block
*b0
, *b1
, *tmp
;
5133 /* ip6 proto 'proto' */
5134 /* XXX - catch the first fragment of a fragmented packet? */
5135 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5139 b1
= gen_portatom6(0, (bpf_int32
)port
);
5143 b1
= gen_portatom6(2, (bpf_int32
)port
);
5148 tmp
= 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
);
5167 static struct block
*
5168 gen_port6(port
, ip_proto
, dir
)
5173 struct block
*b0
, *b1
, *tmp
;
5175 /* link proto ip6 */
5176 b0
= gen_linktype(ETHERTYPE_IPV6
);
5182 b1
= gen_portop6(port
, ip_proto
, dir
);
5186 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5187 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5189 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5200 /* gen_portrange code */
5201 static struct block
*
5202 gen_portrangeatom(off
, v1
, v2
)
5206 struct block
*b1
, *b2
;
5210 * Reverse the order of the ports, so v1 is the lower one.
5219 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5220 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5228 gen_portrangeop(port1
, port2
, proto
, dir
)
5233 struct block
*b0
, *b1
, *tmp
;
5235 /* ip proto 'proto' and not a fragment other than the first fragment */
5236 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5242 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5246 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5251 tmp
= 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
);
5270 static struct block
*
5271 gen_portrange(port1
, port2
, ip_proto
, dir
)
5276 struct block
*b0
, *b1
, *tmp
;
5279 b0
= gen_linktype(ETHERTYPE_IP
);
5285 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5289 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5290 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5292 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5303 static struct block
*
5304 gen_portrangeatom6(off
, v1
, v2
)
5308 struct block
*b1
, *b2
;
5312 * Reverse the order of the ports, so v1 is the lower one.
5321 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5322 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5330 gen_portrangeop6(port1
, port2
, proto
, dir
)
5335 struct block
*b0
, *b1
, *tmp
;
5337 /* ip6 proto 'proto' */
5338 /* XXX - catch the first fragment of a fragmented packet? */
5339 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5343 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5347 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5352 tmp
= 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
);
5371 static struct block
*
5372 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5377 struct block
*b0
, *b1
, *tmp
;
5379 /* link proto ip6 */
5380 b0
= gen_linktype(ETHERTYPE_IPV6
);
5386 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5390 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5391 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5393 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5405 lookup_proto(name
, proto
)
5406 register const char *name
;
5416 v
= pcap_nametoproto(name
);
5417 if (v
== PROTO_UNDEF
)
5418 bpf_error("unknown ip proto '%s'", name
);
5422 /* XXX should look up h/w protocol type based on linktype */
5423 v
= pcap_nametoeproto(name
);
5424 if (v
== PROTO_UNDEF
) {
5425 v
= pcap_nametollc(name
);
5426 if (v
== PROTO_UNDEF
)
5427 bpf_error("unknown ether proto '%s'", name
);
5432 if (strcmp(name
, "esis") == 0)
5434 else if (strcmp(name
, "isis") == 0)
5436 else if (strcmp(name
, "clnp") == 0)
5439 bpf_error("unknown osi proto '%s'", name
);
5459 static struct block
*
5460 gen_protochain(v
, proto
, dir
)
5465 #ifdef NO_PROTOCHAIN
5466 return gen_proto(v
, proto
, dir
);
5468 struct block
*b0
, *b
;
5469 struct slist
*s
[100];
5470 int fix2
, fix3
, fix4
, fix5
;
5471 int ahcheck
, again
, end
;
5473 int reg2
= alloc_reg();
5475 memset(s
, 0, sizeof(s
));
5476 fix2
= fix3
= fix4
= fix5
= 0;
5483 b0
= gen_protochain(v
, Q_IP
, dir
);
5484 b
= gen_protochain(v
, Q_IPV6
, dir
);
5488 bpf_error("bad protocol applied for 'protochain'");
5493 * We don't handle variable-length prefixes before the link-layer
5494 * header, or variable-length link-layer headers, here yet.
5495 * We might want to add BPF instructions to do the protochain
5496 * work, to simplify that and, on platforms that have a BPF
5497 * interpreter with the new instructions, let the filtering
5498 * be done in the kernel. (We already require a modified BPF
5499 * engine to do the protochain stuff, to support backward
5500 * branches, and backward branch support is unlikely to appear
5501 * in kernel BPF engines.)
5505 case DLT_IEEE802_11
:
5506 case DLT_PRISM_HEADER
:
5507 case DLT_IEEE802_11_RADIO_AVS
:
5508 case DLT_IEEE802_11_RADIO
:
5510 bpf_error("'protochain' not supported with 802.11");
5513 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5516 * s[0] is a dummy entry to protect other BPF insn from damage
5517 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5518 * hard to find interdependency made by jump table fixup.
5521 s
[i
] = new_stmt(0); /*dummy*/
5526 b0
= gen_linktype(ETHERTYPE_IP
);
5529 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5530 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5532 /* X = ip->ip_hl << 2 */
5533 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5534 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5539 b0
= gen_linktype(ETHERTYPE_IPV6
);
5541 /* A = ip6->ip_nxt */
5542 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5543 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5545 /* X = sizeof(struct ip6_hdr) */
5546 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5552 bpf_error("unsupported proto to gen_protochain");
5556 /* again: if (A == v) goto end; else fall through; */
5558 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5560 s
[i
]->s
.jt
= NULL
; /*later*/
5561 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5565 #ifndef IPPROTO_NONE
5566 #define IPPROTO_NONE 59
5568 /* if (A == IPPROTO_NONE) goto end */
5569 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5570 s
[i
]->s
.jt
= NULL
; /*later*/
5571 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5572 s
[i
]->s
.k
= IPPROTO_NONE
;
5573 s
[fix5
]->s
.jf
= s
[i
];
5577 if (proto
== Q_IPV6
) {
5578 int v6start
, v6end
, v6advance
, j
;
5581 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5582 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5583 s
[i
]->s
.jt
= NULL
; /*later*/
5584 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5585 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5586 s
[fix2
]->s
.jf
= s
[i
];
5588 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5589 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5590 s
[i
]->s
.jt
= NULL
; /*later*/
5591 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5592 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5594 /* if (A == IPPROTO_ROUTING) 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_ROUTING
;
5600 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
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
; /*later*/
5604 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5614 * A = P[X + packet head];
5615 * X = X + (P[X + packet head + 1] + 1) * 8;
5617 /* A = P[X + packet head] */
5618 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5619 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5622 s
[i
] = new_stmt(BPF_ST
);
5625 /* A = P[X + packet head + 1]; */
5626 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5627 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 1;
5630 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5634 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5638 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5642 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5645 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5649 /* goto again; (must use BPF_JA for backward jump) */
5650 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5651 s
[i
]->s
.k
= again
- i
- 1;
5652 s
[i
- 1]->s
.jf
= s
[i
];
5656 for (j
= v6start
; j
<= v6end
; j
++)
5657 s
[j
]->s
.jt
= s
[v6advance
];
5660 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5662 s
[fix2
]->s
.jf
= s
[i
];
5668 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5669 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5670 s
[i
]->s
.jt
= NULL
; /*later*/
5671 s
[i
]->s
.jf
= NULL
; /*later*/
5672 s
[i
]->s
.k
= IPPROTO_AH
;
5674 s
[fix3
]->s
.jf
= s
[ahcheck
];
5681 * X = X + (P[X + 1] + 2) * 4;
5684 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5686 /* A = P[X + packet head]; */
5687 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5688 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5691 s
[i
] = new_stmt(BPF_ST
);
5695 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5698 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5702 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5704 /* A = P[X + packet head] */
5705 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5706 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5709 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5713 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5717 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5720 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5724 /* goto again; (must use BPF_JA for backward jump) */
5725 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5726 s
[i
]->s
.k
= again
- i
- 1;
5731 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5733 s
[fix2
]->s
.jt
= s
[end
];
5734 s
[fix4
]->s
.jf
= s
[end
];
5735 s
[fix5
]->s
.jt
= s
[end
];
5742 for (i
= 0; i
< max
- 1; i
++)
5743 s
[i
]->next
= s
[i
+ 1];
5744 s
[max
- 1]->next
= NULL
;
5749 b
= new_block(JMP(BPF_JEQ
));
5750 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5760 static struct block
*
5761 gen_check_802_11_data_frame()
5764 struct block
*b0
, *b1
;
5767 * A data frame has the 0x08 bit (b3) in the frame control field set
5768 * and the 0x04 bit (b2) clear.
5770 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5771 b0
= new_block(JMP(BPF_JSET
));
5775 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5776 b1
= new_block(JMP(BPF_JSET
));
5787 * Generate code that checks whether the packet is a packet for protocol
5788 * <proto> and whether the type field in that protocol's header has
5789 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5790 * IP packet and checks the protocol number in the IP header against <v>.
5792 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5793 * against Q_IP and Q_IPV6.
5795 static struct block
*
5796 gen_proto(v
, proto
, dir
)
5801 struct block
*b0
, *b1
;
5806 if (dir
!= Q_DEFAULT
)
5807 bpf_error("direction applied to 'proto'");
5811 b0
= gen_proto(v
, Q_IP
, dir
);
5812 b1
= gen_proto(v
, Q_IPV6
, dir
);
5818 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5819 * not LLC encapsulation with LLCSAP_IP.
5821 * For IEEE 802 networks - which includes 802.5 token ring
5822 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5823 * says that SNAP encapsulation is used, not LLC encapsulation
5826 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5827 * RFC 2225 say that SNAP encapsulation is used, not LLC
5828 * encapsulation with LLCSAP_IP.
5830 * So we always check for ETHERTYPE_IP.
5832 b0
= gen_linktype(ETHERTYPE_IP
);
5834 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5836 b1
= gen_protochain(v
, Q_IP
);
5846 * Frame Relay packets typically have an OSI
5847 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5848 * generates code to check for all the OSI
5849 * NLPIDs, so calling it and then adding a check
5850 * for the particular NLPID for which we're
5851 * looking is bogus, as we can just check for
5854 * What we check for is the NLPID and a frame
5855 * control field value of UI, i.e. 0x03 followed
5858 * XXX - assumes a 2-byte Frame Relay header with
5859 * DLCI and flags. What if the address is longer?
5861 * XXX - what about SNAP-encapsulated frames?
5863 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5869 * Cisco uses an Ethertype lookalike - for OSI,
5872 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5873 /* OSI in C-HDLC is stuffed with a fudge byte */
5874 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5879 b0
= gen_linktype(LLCSAP_ISONS
);
5880 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5886 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5888 * 4 is the offset of the PDU type relative to the IS-IS
5891 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5896 bpf_error("arp does not encapsulate another protocol");
5900 bpf_error("rarp does not encapsulate another protocol");
5904 bpf_error("atalk encapsulation is not specifiable");
5908 bpf_error("decnet encapsulation is not specifiable");
5912 bpf_error("sca does not encapsulate another protocol");
5916 bpf_error("lat does not encapsulate another protocol");
5920 bpf_error("moprc does not encapsulate another protocol");
5924 bpf_error("mopdl does not encapsulate another protocol");
5928 return gen_linktype(v
);
5931 bpf_error("'udp proto' is bogus");
5935 bpf_error("'tcp proto' is bogus");
5939 bpf_error("'sctp proto' is bogus");
5943 bpf_error("'icmp proto' is bogus");
5947 bpf_error("'igmp proto' is bogus");
5951 bpf_error("'igrp proto' is bogus");
5955 bpf_error("'pim proto' is bogus");
5959 bpf_error("'vrrp proto' is bogus");
5963 bpf_error("'carp proto' is bogus");
5967 b0
= gen_linktype(ETHERTYPE_IPV6
);
5970 * Also check for a fragment header before the final
5973 b2
= gen_cmp(OR_NET
, 6, BPF_B
, IPPROTO_FRAGMENT
);
5974 b1
= gen_cmp(OR_NET
, 40, BPF_B
, (bpf_int32
)v
);
5976 b2
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5979 b1
= gen_protochain(v
, Q_IPV6
);
5985 bpf_error("'icmp6 proto' is bogus");
5988 bpf_error("'ah proto' is bogus");
5991 bpf_error("'ah proto' is bogus");
5994 bpf_error("'stp proto' is bogus");
5997 bpf_error("'ipx proto' is bogus");
6000 bpf_error("'netbeui proto' is bogus");
6003 bpf_error("'radio proto' is bogus");
6014 register const char *name
;
6017 int proto
= q
.proto
;
6021 bpf_u_int32 mask
, addr
;
6023 bpf_u_int32
**alist
;
6026 struct sockaddr_in
*sin4
;
6027 struct sockaddr_in6
*sin6
;
6028 struct addrinfo
*res
, *res0
;
6029 struct in6_addr mask128
;
6031 struct block
*b
, *tmp
;
6032 int port
, real_proto
;
6038 addr
= pcap_nametonetaddr(name
);
6040 bpf_error("unknown network '%s'", name
);
6041 /* Left justify network addr and calculate its network mask */
6043 while (addr
&& (addr
& 0xff000000) == 0) {
6047 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6051 if (proto
== Q_LINK
) {
6055 case DLT_NETANALYZER
:
6056 case DLT_NETANALYZER_TRANSPARENT
:
6057 eaddr
= pcap_ether_hostton(name
);
6060 "unknown ether host '%s'", name
);
6061 b
= gen_ehostop(eaddr
, dir
);
6066 eaddr
= pcap_ether_hostton(name
);
6069 "unknown FDDI host '%s'", name
);
6070 b
= gen_fhostop(eaddr
, dir
);
6075 eaddr
= pcap_ether_hostton(name
);
6078 "unknown token ring host '%s'", name
);
6079 b
= gen_thostop(eaddr
, dir
);
6083 case DLT_IEEE802_11
:
6084 case DLT_PRISM_HEADER
:
6085 case DLT_IEEE802_11_RADIO_AVS
:
6086 case DLT_IEEE802_11_RADIO
:
6088 eaddr
= pcap_ether_hostton(name
);
6091 "unknown 802.11 host '%s'", name
);
6092 b
= gen_wlanhostop(eaddr
, dir
);
6096 case DLT_IP_OVER_FC
:
6097 eaddr
= pcap_ether_hostton(name
);
6100 "unknown Fibre Channel host '%s'", name
);
6101 b
= gen_ipfchostop(eaddr
, dir
);
6110 * Check that the packet doesn't begin
6111 * with an LE Control marker. (We've
6112 * already generated a test for LANE.)
6114 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6118 eaddr
= pcap_ether_hostton(name
);
6121 "unknown ether host '%s'", name
);
6122 b
= gen_ehostop(eaddr
, dir
);
6128 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6129 } else if (proto
== Q_DECNET
) {
6130 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6132 * I don't think DECNET hosts can be multihomed, so
6133 * there is no need to build up a list of addresses
6135 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6138 alist
= pcap_nametoaddr(name
);
6139 if (alist
== NULL
|| *alist
== NULL
)
6140 bpf_error("unknown host '%s'", name
);
6142 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
6144 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6146 tmp
= gen_host(**alist
++, 0xffffffff,
6147 tproto
, dir
, q
.addr
);
6153 memset(&mask128
, 0xff, sizeof(mask128
));
6154 res0
= res
= pcap_nametoaddrinfo(name
);
6156 bpf_error("unknown host '%s'", name
);
6159 tproto
= tproto6
= proto
;
6160 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
6164 for (res
= res0
; res
; res
= res
->ai_next
) {
6165 switch (res
->ai_family
) {
6167 if (tproto
== Q_IPV6
)
6170 sin4
= (struct sockaddr_in
*)
6172 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6173 0xffffffff, tproto
, dir
, q
.addr
);
6176 if (tproto6
== Q_IP
)
6179 sin6
= (struct sockaddr_in6
*)
6181 tmp
= gen_host6(&sin6
->sin6_addr
,
6182 &mask128
, tproto6
, dir
, q
.addr
);
6194 bpf_error("unknown host '%s'%s", name
,
6195 (proto
== Q_DEFAULT
)
6197 : " for specified address family");
6204 if (proto
!= Q_DEFAULT
&&
6205 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6206 bpf_error("illegal qualifier of 'port'");
6207 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6208 bpf_error("unknown port '%s'", name
);
6209 if (proto
== Q_UDP
) {
6210 if (real_proto
== IPPROTO_TCP
)
6211 bpf_error("port '%s' is tcp", name
);
6212 else if (real_proto
== IPPROTO_SCTP
)
6213 bpf_error("port '%s' is sctp", name
);
6215 /* override PROTO_UNDEF */
6216 real_proto
= IPPROTO_UDP
;
6218 if (proto
== Q_TCP
) {
6219 if (real_proto
== IPPROTO_UDP
)
6220 bpf_error("port '%s' is udp", name
);
6222 else if (real_proto
== IPPROTO_SCTP
)
6223 bpf_error("port '%s' is sctp", name
);
6225 /* override PROTO_UNDEF */
6226 real_proto
= IPPROTO_TCP
;
6228 if (proto
== Q_SCTP
) {
6229 if (real_proto
== IPPROTO_UDP
)
6230 bpf_error("port '%s' is udp", name
);
6232 else if (real_proto
== IPPROTO_TCP
)
6233 bpf_error("port '%s' is tcp", name
);
6235 /* override PROTO_UNDEF */
6236 real_proto
= IPPROTO_SCTP
;
6239 bpf_error("illegal port number %d < 0", port
);
6241 bpf_error("illegal port number %d > 65535", port
);
6242 b
= gen_port(port
, real_proto
, dir
);
6243 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6247 if (proto
!= Q_DEFAULT
&&
6248 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6249 bpf_error("illegal qualifier of 'portrange'");
6250 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6251 bpf_error("unknown port in range '%s'", name
);
6252 if (proto
== Q_UDP
) {
6253 if (real_proto
== IPPROTO_TCP
)
6254 bpf_error("port in range '%s' is tcp", name
);
6255 else if (real_proto
== IPPROTO_SCTP
)
6256 bpf_error("port in range '%s' is sctp", name
);
6258 /* override PROTO_UNDEF */
6259 real_proto
= IPPROTO_UDP
;
6261 if (proto
== Q_TCP
) {
6262 if (real_proto
== IPPROTO_UDP
)
6263 bpf_error("port in range '%s' is udp", name
);
6264 else if (real_proto
== IPPROTO_SCTP
)
6265 bpf_error("port in range '%s' is sctp", name
);
6267 /* override PROTO_UNDEF */
6268 real_proto
= IPPROTO_TCP
;
6270 if (proto
== Q_SCTP
) {
6271 if (real_proto
== IPPROTO_UDP
)
6272 bpf_error("port in range '%s' is udp", name
);
6273 else if (real_proto
== IPPROTO_TCP
)
6274 bpf_error("port in range '%s' is tcp", name
);
6276 /* override PROTO_UNDEF */
6277 real_proto
= IPPROTO_SCTP
;
6280 bpf_error("illegal port number %d < 0", port1
);
6282 bpf_error("illegal port number %d > 65535", port1
);
6284 bpf_error("illegal port number %d < 0", port2
);
6286 bpf_error("illegal port number %d > 65535", port2
);
6288 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6289 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6294 eaddr
= pcap_ether_hostton(name
);
6296 bpf_error("unknown ether host: %s", name
);
6298 alist
= pcap_nametoaddr(name
);
6299 if (alist
== NULL
|| *alist
== NULL
)
6300 bpf_error("unknown host '%s'", name
);
6301 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6305 bpf_error("'gateway' not supported in this configuration");
6309 real_proto
= lookup_proto(name
, proto
);
6310 if (real_proto
>= 0)
6311 return gen_proto(real_proto
, proto
, dir
);
6313 bpf_error("unknown protocol: %s", name
);
6316 real_proto
= lookup_proto(name
, proto
);
6317 if (real_proto
>= 0)
6318 return gen_protochain(real_proto
, proto
, dir
);
6320 bpf_error("unknown protocol: %s", name
);
6331 gen_mcode(s1
, s2
, masklen
, q
)
6332 register const char *s1
, *s2
;
6333 register int masklen
;
6336 register int nlen
, mlen
;
6339 nlen
= __pcap_atoin(s1
, &n
);
6340 /* Promote short ipaddr */
6344 mlen
= __pcap_atoin(s2
, &m
);
6345 /* Promote short ipaddr */
6348 bpf_error("non-network bits set in \"%s mask %s\"",
6351 /* Convert mask len to mask */
6353 bpf_error("mask length must be <= 32");
6356 * X << 32 is not guaranteed by C to be 0; it's
6361 m
= 0xffffffff << (32 - masklen
);
6363 bpf_error("non-network bits set in \"%s/%d\"",
6370 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6373 bpf_error("Mask syntax for networks only");
6382 register const char *s
;
6387 int proto
= q
.proto
;
6393 else if (q
.proto
== Q_DECNET
)
6394 vlen
= __pcap_atodn(s
, &v
);
6396 vlen
= __pcap_atoin(s
, &v
);
6403 if (proto
== Q_DECNET
)
6404 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6405 else if (proto
== Q_LINK
) {
6406 bpf_error("illegal link layer address");
6409 if (s
== NULL
&& q
.addr
== Q_NET
) {
6410 /* Promote short net number */
6411 while (v
&& (v
& 0xff000000) == 0) {
6416 /* Promote short ipaddr */
6420 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6425 proto
= IPPROTO_UDP
;
6426 else if (proto
== Q_TCP
)
6427 proto
= IPPROTO_TCP
;
6428 else if (proto
== Q_SCTP
)
6429 proto
= IPPROTO_SCTP
;
6430 else if (proto
== Q_DEFAULT
)
6431 proto
= PROTO_UNDEF
;
6433 bpf_error("illegal qualifier of 'port'");
6436 bpf_error("illegal port number %u > 65535", v
);
6440 b
= gen_port((int)v
, proto
, dir
);
6441 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6447 proto
= IPPROTO_UDP
;
6448 else if (proto
== Q_TCP
)
6449 proto
= IPPROTO_TCP
;
6450 else if (proto
== Q_SCTP
)
6451 proto
= IPPROTO_SCTP
;
6452 else if (proto
== Q_DEFAULT
)
6453 proto
= PROTO_UNDEF
;
6455 bpf_error("illegal qualifier of 'portrange'");
6458 bpf_error("illegal port number %u > 65535", v
);
6462 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6463 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6468 bpf_error("'gateway' requires a name");
6472 return gen_proto((int)v
, proto
, dir
);
6475 return gen_protochain((int)v
, proto
, dir
);
6490 gen_mcode6(s1
, s2
, masklen
, q
)
6491 register const char *s1
, *s2
;
6492 register int masklen
;
6495 struct addrinfo
*res
;
6496 struct in6_addr
*addr
;
6497 struct in6_addr mask
;
6502 bpf_error("no mask %s supported", s2
);
6504 res
= pcap_nametoaddrinfo(s1
);
6506 bpf_error("invalid ip6 address %s", s1
);
6509 bpf_error("%s resolved to multiple address", s1
);
6510 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6512 if (sizeof(mask
) * 8 < masklen
)
6513 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6514 memset(&mask
, 0, sizeof(mask
));
6515 memset(&mask
, 0xff, masklen
/ 8);
6517 mask
.s6_addr
[masklen
/ 8] =
6518 (0xff << (8 - masklen
% 8)) & 0xff;
6521 a
= (u_int32_t
*)addr
;
6522 m
= (u_int32_t
*)&mask
;
6523 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6524 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6525 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6533 bpf_error("Mask syntax for networks only");
6537 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6543 bpf_error("invalid qualifier against IPv6 address");
6552 register const u_char
*eaddr
;
6555 struct block
*b
, *tmp
;
6557 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6560 case DLT_NETANALYZER
:
6561 case DLT_NETANALYZER_TRANSPARENT
:
6562 return gen_ehostop(eaddr
, (int)q
.dir
);
6564 return gen_fhostop(eaddr
, (int)q
.dir
);
6566 return gen_thostop(eaddr
, (int)q
.dir
);
6567 case DLT_IEEE802_11
:
6568 case DLT_PRISM_HEADER
:
6569 case DLT_IEEE802_11_RADIO_AVS
:
6570 case DLT_IEEE802_11_RADIO
:
6572 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6576 * Check that the packet doesn't begin with an
6577 * LE Control marker. (We've already generated
6580 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6585 * Now check the MAC address.
6587 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6592 case DLT_IP_OVER_FC
:
6593 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6595 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6599 bpf_error("ethernet address used in non-ether expression");
6606 struct slist
*s0
, *s1
;
6609 * This is definitely not the best way to do this, but the
6610 * lists will rarely get long.
6617 static struct slist
*
6623 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6628 static struct slist
*
6634 s
= new_stmt(BPF_LD
|BPF_MEM
);
6640 * Modify "index" to use the value stored into its register as an
6641 * offset relative to the beginning of the header for the protocol
6642 * "proto", and allocate a register and put an item "size" bytes long
6643 * (1, 2, or 4) at that offset into that register, making it the register
6647 gen_load(proto
, inst
, size
)
6652 struct slist
*s
, *tmp
;
6654 int regno
= alloc_reg();
6656 free_reg(inst
->regno
);
6660 bpf_error("data size must be 1, 2, or 4");
6676 bpf_error("unsupported index operation");
6680 * The offset is relative to the beginning of the packet
6681 * data, if we have a radio header. (If we don't, this
6684 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6685 linktype
!= DLT_IEEE802_11_RADIO
&&
6686 linktype
!= DLT_PRISM_HEADER
)
6687 bpf_error("radio information not present in capture");
6690 * Load into the X register the offset computed into the
6691 * register specified by "index".
6693 s
= xfer_to_x(inst
);
6696 * Load the item at that offset.
6698 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6700 sappend(inst
->s
, s
);
6705 * The offset is relative to the beginning of
6706 * the link-layer header.
6708 * XXX - what about ATM LANE? Should the index be
6709 * relative to the beginning of the AAL5 frame, so
6710 * that 0 refers to the beginning of the LE Control
6711 * field, or relative to the beginning of the LAN
6712 * frame, so that 0 refers, for Ethernet LANE, to
6713 * the beginning of the destination address?
6715 s
= gen_llprefixlen();
6718 * If "s" is non-null, it has code to arrange that the
6719 * X register contains the length of the prefix preceding
6720 * the link-layer header. Add to it the offset computed
6721 * into the register specified by "index", and move that
6722 * into the X register. Otherwise, just load into the X
6723 * register the offset computed into the register specified
6727 sappend(s
, xfer_to_a(inst
));
6728 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6729 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6731 s
= xfer_to_x(inst
);
6734 * Load the item at the sum of the offset we've put in the
6735 * X register and the offset of the start of the link
6736 * layer header (which is 0 if the radio header is
6737 * variable-length; that header length is what we put
6738 * into the X register and then added to the index).
6740 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6743 sappend(inst
->s
, s
);
6757 * The offset is relative to the beginning of
6758 * the network-layer header.
6759 * XXX - are there any cases where we want
6762 s
= gen_off_macpl();
6765 * If "s" is non-null, it has code to arrange that the
6766 * X register contains the offset of the MAC-layer
6767 * payload. Add to it the offset computed into the
6768 * register specified by "index", and move that into
6769 * the X register. Otherwise, just load into the X
6770 * register the offset computed into the register specified
6774 sappend(s
, xfer_to_a(inst
));
6775 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6776 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6778 s
= xfer_to_x(inst
);
6781 * Load the item at the sum of the offset we've put in the
6782 * X register, the offset of the start of the network
6783 * layer header from the beginning of the MAC-layer
6784 * payload, and the purported offset of the start of the
6785 * MAC-layer payload (which might be 0 if there's a
6786 * variable-length prefix before the link-layer header
6787 * or the link-layer header itself is variable-length;
6788 * the variable-length offset of the start of the
6789 * MAC-layer payload is what we put into the X register
6790 * and then added to the index).
6792 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6793 tmp
->s
.k
= off_macpl
+ off_nl
;
6795 sappend(inst
->s
, s
);
6798 * Do the computation only if the packet contains
6799 * the protocol in question.
6801 b
= gen_proto_abbrev(proto
);
6803 gen_and(inst
->b
, b
);
6817 * The offset is relative to the beginning of
6818 * the transport-layer header.
6820 * Load the X register with the length of the IPv4 header
6821 * (plus the offset of the link-layer header, if it's
6822 * a variable-length header), in bytes.
6824 * XXX - are there any cases where we want
6826 * XXX - we should, if we're built with
6827 * IPv6 support, generate code to load either
6828 * IPv4, IPv6, or both, as appropriate.
6830 s
= gen_loadx_iphdrlen();
6833 * The X register now contains the sum of the length
6834 * of any variable-length header preceding the link-layer
6835 * header, any variable-length link-layer header, and the
6836 * length of the network-layer header.
6838 * Load into the A register the offset relative to
6839 * the beginning of the transport layer header,
6840 * add the X register to that, move that to the
6841 * X register, and load with an offset from the
6842 * X register equal to the offset of the network
6843 * layer header relative to the beginning of
6844 * the MAC-layer payload plus the fixed-length
6845 * portion of the offset of the MAC-layer payload
6846 * from the beginning of the raw packet data.
6848 sappend(s
, xfer_to_a(inst
));
6849 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6850 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6851 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6852 tmp
->s
.k
= off_macpl
+ off_nl
;
6853 sappend(inst
->s
, s
);
6856 * Do the computation only if the packet contains
6857 * the protocol in question - which is true only
6858 * if this is an IP datagram and is the first or
6859 * only fragment of that datagram.
6861 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6863 gen_and(inst
->b
, b
);
6864 gen_and(gen_proto_abbrev(Q_IP
), b
);
6868 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6871 inst
->regno
= regno
;
6872 s
= new_stmt(BPF_ST
);
6874 sappend(inst
->s
, s
);
6880 gen_relation(code
, a0
, a1
, reversed
)
6882 struct arth
*a0
, *a1
;
6885 struct slist
*s0
, *s1
, *s2
;
6886 struct block
*b
, *tmp
;
6890 if (code
== BPF_JEQ
) {
6891 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6892 b
= new_block(JMP(code
));
6896 b
= new_block(BPF_JMP
|code
|BPF_X
);
6902 sappend(a0
->s
, a1
->s
);
6906 free_reg(a0
->regno
);
6907 free_reg(a1
->regno
);
6909 /* 'and' together protocol checks */
6912 gen_and(a0
->b
, tmp
= a1
->b
);
6928 int regno
= alloc_reg();
6929 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6932 s
= new_stmt(BPF_LD
|BPF_LEN
);
6933 s
->next
= new_stmt(BPF_ST
);
6934 s
->next
->s
.k
= regno
;
6949 a
= (struct arth
*)newchunk(sizeof(*a
));
6953 s
= new_stmt(BPF_LD
|BPF_IMM
);
6955 s
->next
= new_stmt(BPF_ST
);
6971 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6974 s
= new_stmt(BPF_ST
);
6982 gen_arth(code
, a0
, a1
)
6984 struct arth
*a0
, *a1
;
6986 struct slist
*s0
, *s1
, *s2
;
6990 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6995 sappend(a0
->s
, a1
->s
);
6997 free_reg(a0
->regno
);
6998 free_reg(a1
->regno
);
7000 s0
= new_stmt(BPF_ST
);
7001 a0
->regno
= s0
->s
.k
= alloc_reg();
7008 * Here we handle simple allocation of the scratch registers.
7009 * If too many registers are alloc'd, the allocator punts.
7011 static int regused
[BPF_MEMWORDS
];
7015 * Initialize the table of used registers and the current register.
7021 memset(regused
, 0, sizeof regused
);
7025 * Return the next free register.
7030 int n
= BPF_MEMWORDS
;
7033 if (regused
[curreg
])
7034 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7036 regused
[curreg
] = 1;
7040 bpf_error("too many registers needed to evaluate expression");
7046 * Return a register to the table so it can
7056 static struct block
*
7063 s
= new_stmt(BPF_LD
|BPF_LEN
);
7064 b
= new_block(JMP(jmp
));
7075 return gen_len(BPF_JGE
, n
);
7079 * Actually, this is less than or equal.
7087 b
= gen_len(BPF_JGT
, n
);
7094 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7095 * the beginning of the link-layer header.
7096 * XXX - that means you can't test values in the radiotap header, but
7097 * as that header is difficult if not impossible to parse generally
7098 * without a loop, that might not be a severe problem. A new keyword
7099 * "radio" could be added for that, although what you'd really want
7100 * would be a way of testing particular radio header values, which
7101 * would generate code appropriate to the radio header in question.
7104 gen_byteop(op
, idx
, val
)
7115 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7118 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7122 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7126 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7130 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7134 b
= new_block(JMP(BPF_JEQ
));
7141 static u_char abroadcast
[] = { 0x0 };
7144 gen_broadcast(proto
)
7147 bpf_u_int32 hostmask
;
7148 struct block
*b0
, *b1
, *b2
;
7149 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7157 case DLT_ARCNET_LINUX
:
7158 return gen_ahostop(abroadcast
, Q_DST
);
7160 case DLT_NETANALYZER
:
7161 case DLT_NETANALYZER_TRANSPARENT
:
7162 return gen_ehostop(ebroadcast
, Q_DST
);
7164 return gen_fhostop(ebroadcast
, Q_DST
);
7166 return gen_thostop(ebroadcast
, Q_DST
);
7167 case DLT_IEEE802_11
:
7168 case DLT_PRISM_HEADER
:
7169 case DLT_IEEE802_11_RADIO_AVS
:
7170 case DLT_IEEE802_11_RADIO
:
7172 return gen_wlanhostop(ebroadcast
, Q_DST
);
7173 case DLT_IP_OVER_FC
:
7174 return gen_ipfchostop(ebroadcast
, Q_DST
);
7178 * Check that the packet doesn't begin with an
7179 * LE Control marker. (We've already generated
7182 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7187 * Now check the MAC address.
7189 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7195 bpf_error("not a broadcast link");
7201 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7202 * as an indication that we don't know the netmask, and fail
7205 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7206 bpf_error("netmask not known, so 'ip broadcast' not supported");
7207 b0
= gen_linktype(ETHERTYPE_IP
);
7208 hostmask
= ~netmask
;
7209 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7210 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7211 (bpf_int32
)(~0 & hostmask
), hostmask
);
7216 bpf_error("only link-layer/IP broadcast filters supported");
7222 * Generate code to test the low-order bit of a MAC address (that's
7223 * the bottom bit of the *first* byte).
7225 static struct block
*
7226 gen_mac_multicast(offset
)
7229 register struct block
*b0
;
7230 register struct slist
*s
;
7232 /* link[offset] & 1 != 0 */
7233 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7234 b0
= new_block(JMP(BPF_JSET
));
7241 gen_multicast(proto
)
7244 register struct block
*b0
, *b1
, *b2
;
7245 register struct slist
*s
;
7253 case DLT_ARCNET_LINUX
:
7254 /* all ARCnet multicasts use the same address */
7255 return gen_ahostop(abroadcast
, Q_DST
);
7257 case DLT_NETANALYZER
:
7258 case DLT_NETANALYZER_TRANSPARENT
:
7259 /* ether[0] & 1 != 0 */
7260 return gen_mac_multicast(0);
7263 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7265 * XXX - was that referring to bit-order issues?
7267 /* fddi[1] & 1 != 0 */
7268 return gen_mac_multicast(1);
7270 /* tr[2] & 1 != 0 */
7271 return gen_mac_multicast(2);
7272 case DLT_IEEE802_11
:
7273 case DLT_PRISM_HEADER
:
7274 case DLT_IEEE802_11_RADIO_AVS
:
7275 case DLT_IEEE802_11_RADIO
:
7280 * For control frames, there is no DA.
7282 * For management frames, DA is at an
7283 * offset of 4 from the beginning of
7286 * For data frames, DA is at an offset
7287 * of 4 from the beginning of the packet
7288 * if To DS is clear and at an offset of
7289 * 16 from the beginning of the packet
7294 * Generate the tests to be done for data frames.
7296 * First, check for To DS set, i.e. "link[1] & 0x01".
7298 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7299 b1
= new_block(JMP(BPF_JSET
));
7300 b1
->s
.k
= 0x01; /* To DS */
7304 * If To DS is set, the DA is at 16.
7306 b0
= gen_mac_multicast(16);
7310 * Now, check for To DS not set, i.e. check
7311 * "!(link[1] & 0x01)".
7313 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7314 b2
= new_block(JMP(BPF_JSET
));
7315 b2
->s
.k
= 0x01; /* To DS */
7320 * If To DS is not set, the DA is at 4.
7322 b1
= gen_mac_multicast(4);
7326 * Now OR together the last two checks. That gives
7327 * the complete set of checks for data frames.
7332 * Now check for a data frame.
7333 * I.e, check "link[0] & 0x08".
7335 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7336 b1
= new_block(JMP(BPF_JSET
));
7341 * AND that with the checks done for data frames.
7346 * If the high-order bit of the type value is 0, this
7347 * is a management frame.
7348 * I.e, check "!(link[0] & 0x08)".
7350 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7351 b2
= new_block(JMP(BPF_JSET
));
7357 * For management frames, the DA is at 4.
7359 b1
= gen_mac_multicast(4);
7363 * OR that with the checks done for data frames.
7364 * That gives the checks done for management and
7370 * If the low-order bit of the type value is 1,
7371 * this is either a control frame or a frame
7372 * with a reserved type, and thus not a
7375 * I.e., check "!(link[0] & 0x04)".
7377 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7378 b1
= new_block(JMP(BPF_JSET
));
7384 * AND that with the checks for data and management
7389 case DLT_IP_OVER_FC
:
7390 b0
= gen_mac_multicast(2);
7395 * Check that the packet doesn't begin with an
7396 * LE Control marker. (We've already generated
7399 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7403 /* ether[off_mac] & 1 != 0 */
7404 b0
= gen_mac_multicast(off_mac
);
7412 /* Link not known to support multicasts */
7416 b0
= gen_linktype(ETHERTYPE_IP
);
7417 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7422 b0
= gen_linktype(ETHERTYPE_IPV6
);
7423 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7427 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7433 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7434 * Outbound traffic is sent by this machine, while inbound traffic is
7435 * sent by a remote machine (and may include packets destined for a
7436 * unicast or multicast link-layer address we are not subscribing to).
7437 * These are the same definitions implemented by pcap_setdirection().
7438 * Capturing only unicast traffic destined for this host is probably
7439 * better accomplished using a higher-layer filter.
7445 register struct block
*b0
;
7448 * Only some data link types support inbound/outbound qualifiers.
7452 b0
= gen_relation(BPF_JEQ
,
7453 gen_load(Q_LINK
, gen_loadi(0), 1),
7460 /* match outgoing packets */
7461 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_OUTBOUND
);
7463 /* match incoming packets */
7464 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, IPNET_INBOUND
);
7469 /* match outgoing packets */
7470 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7472 /* to filter on inbound traffic, invert the match */
7477 #ifdef HAVE_NET_PFVAR_H
7479 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7480 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7486 /* match outgoing packets */
7487 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7489 /* match incoming packets */
7490 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7494 case DLT_JUNIPER_MFR
:
7495 case DLT_JUNIPER_MLFR
:
7496 case DLT_JUNIPER_MLPPP
:
7497 case DLT_JUNIPER_ATM1
:
7498 case DLT_JUNIPER_ATM2
:
7499 case DLT_JUNIPER_PPPOE
:
7500 case DLT_JUNIPER_PPPOE_ATM
:
7501 case DLT_JUNIPER_GGSN
:
7502 case DLT_JUNIPER_ES
:
7503 case DLT_JUNIPER_MONITOR
:
7504 case DLT_JUNIPER_SERVICES
:
7505 case DLT_JUNIPER_ETHER
:
7506 case DLT_JUNIPER_PPP
:
7507 case DLT_JUNIPER_FRELAY
:
7508 case DLT_JUNIPER_CHDLC
:
7509 case DLT_JUNIPER_VP
:
7510 case DLT_JUNIPER_ST
:
7511 case DLT_JUNIPER_ISM
:
7512 case DLT_JUNIPER_VS
:
7513 case DLT_JUNIPER_SRX_E2E
:
7514 case DLT_JUNIPER_FIBRECHANNEL
:
7515 case DLT_JUNIPER_ATM_CEMIC
:
7517 /* juniper flags (including direction) are stored
7518 * the byte after the 3-byte magic number */
7520 /* match outgoing packets */
7521 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7523 /* match incoming packets */
7524 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7530 * If we have packet meta-data indicating a direction,
7531 * check it, otherwise give up as this link-layer type
7532 * has nothing in the packet data.
7534 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7536 * This is Linux with PF_PACKET support.
7537 * If this is a *live* capture, we can look at
7538 * special meta-data in the filter expression;
7539 * if it's a savefile, we can't.
7541 if (bpf_pcap
->rfile
!= NULL
) {
7542 /* We have a FILE *, so this is a savefile */
7543 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7548 /* match outgoing packets */
7549 b0
= gen_cmp(OR_LINK
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7552 /* to filter on inbound traffic, invert the match */
7555 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7556 bpf_error("inbound/outbound not supported on linktype %d",
7560 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7565 #ifdef HAVE_NET_PFVAR_H
7566 /* PF firewall log matched interface */
7568 gen_pf_ifname(const char *ifname
)
7573 if (linktype
!= DLT_PFLOG
) {
7574 bpf_error("ifname supported only on PF linktype");
7577 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7578 off
= offsetof(struct pfloghdr
, ifname
);
7579 if (strlen(ifname
) >= len
) {
7580 bpf_error("ifname interface names can only be %d characters",
7584 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7588 /* PF firewall log ruleset name */
7590 gen_pf_ruleset(char *ruleset
)
7594 if (linktype
!= DLT_PFLOG
) {
7595 bpf_error("ruleset supported only on PF linktype");
7599 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7600 bpf_error("ruleset names can only be %ld characters",
7601 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7605 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7606 strlen(ruleset
), (const u_char
*)ruleset
);
7610 /* PF firewall log rule number */
7616 if (linktype
!= DLT_PFLOG
) {
7617 bpf_error("rnr supported only on PF linktype");
7621 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7626 /* PF firewall log sub-rule number */
7628 gen_pf_srnr(int srnr
)
7632 if (linktype
!= DLT_PFLOG
) {
7633 bpf_error("srnr supported only on PF linktype");
7637 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7642 /* PF firewall log reason code */
7644 gen_pf_reason(int reason
)
7648 if (linktype
!= DLT_PFLOG
) {
7649 bpf_error("reason supported only on PF linktype");
7653 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7658 /* PF firewall log action */
7660 gen_pf_action(int action
)
7664 if (linktype
!= DLT_PFLOG
) {
7665 bpf_error("action supported only on PF linktype");
7669 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7673 #else /* !HAVE_NET_PFVAR_H */
7675 gen_pf_ifname(const char *ifname
)
7677 bpf_error("libpcap was compiled without pf support");
7683 gen_pf_ruleset(char *ruleset
)
7685 bpf_error("libpcap was compiled on a machine without pf support");
7693 bpf_error("libpcap was compiled on a machine without pf support");
7699 gen_pf_srnr(int srnr
)
7701 bpf_error("libpcap was compiled on a machine without pf support");
7707 gen_pf_reason(int reason
)
7709 bpf_error("libpcap was compiled on a machine without pf support");
7715 gen_pf_action(int action
)
7717 bpf_error("libpcap was compiled on a machine without pf support");
7721 #endif /* HAVE_NET_PFVAR_H */
7723 /* IEEE 802.11 wireless header */
7725 gen_p80211_type(int type
, int mask
)
7731 case DLT_IEEE802_11
:
7732 case DLT_PRISM_HEADER
:
7733 case DLT_IEEE802_11_RADIO_AVS
:
7734 case DLT_IEEE802_11_RADIO
:
7735 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7740 bpf_error("802.11 link-layer types supported only on 802.11");
7748 gen_p80211_fcdir(int fcdir
)
7754 case DLT_IEEE802_11
:
7755 case DLT_PRISM_HEADER
:
7756 case DLT_IEEE802_11_RADIO_AVS
:
7757 case DLT_IEEE802_11_RADIO
:
7761 bpf_error("frame direction supported only with 802.11 headers");
7765 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7766 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7773 register const u_char
*eaddr
;
7779 case DLT_ARCNET_LINUX
:
7780 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7782 return (gen_ahostop(eaddr
, (int)q
.dir
));
7784 bpf_error("ARCnet address used in non-arc expression");
7790 bpf_error("aid supported only on ARCnet");
7793 bpf_error("ARCnet address used in non-arc expression");
7798 static struct block
*
7799 gen_ahostop(eaddr
, dir
)
7800 register const u_char
*eaddr
;
7803 register struct block
*b0
, *b1
;
7806 /* src comes first, different from Ethernet */
7808 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7811 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7814 b0
= gen_ahostop(eaddr
, Q_SRC
);
7815 b1
= gen_ahostop(eaddr
, Q_DST
);
7821 b0
= gen_ahostop(eaddr
, Q_SRC
);
7822 b1
= gen_ahostop(eaddr
, Q_DST
);
7827 bpf_error("'addr1' is only supported on 802.11");
7831 bpf_error("'addr2' is only supported on 802.11");
7835 bpf_error("'addr3' is only supported on 802.11");
7839 bpf_error("'addr4' is only supported on 802.11");
7843 bpf_error("'ra' is only supported on 802.11");
7847 bpf_error("'ta' is only supported on 802.11");
7855 * support IEEE 802.1Q VLAN trunk over ethernet
7861 struct block
*b0
, *b1
;
7863 /* can't check for VLAN-encapsulated packets inside MPLS */
7864 if (label_stack_depth
> 0)
7865 bpf_error("no VLAN match after MPLS");
7868 * Check for a VLAN packet, and then change the offsets to point
7869 * to the type and data fields within the VLAN packet. Just
7870 * increment the offsets, so that we can support a hierarchy, e.g.
7871 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7874 * XXX - this is a bit of a kludge. If we were to split the
7875 * compiler into a parser that parses an expression and
7876 * generates an expression tree, and a code generator that
7877 * takes an expression tree (which could come from our
7878 * parser or from some other parser) and generates BPF code,
7879 * we could perhaps make the offsets parameters of routines
7880 * and, in the handler for an "AND" node, pass to subnodes
7881 * other than the VLAN node the adjusted offsets.
7883 * This would mean that "vlan" would, instead of changing the
7884 * behavior of *all* tests after it, change only the behavior
7885 * of tests ANDed with it. That would change the documented
7886 * semantics of "vlan", which might break some expressions.
7887 * However, it would mean that "(vlan and ip) or ip" would check
7888 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7889 * checking only for VLAN-encapsulated IP, so that could still
7890 * be considered worth doing; it wouldn't break expressions
7891 * that are of the form "vlan and ..." or "vlan N and ...",
7892 * which I suspect are the most common expressions involving
7893 * "vlan". "vlan or ..." doesn't necessarily do what the user
7894 * would really want, now, as all the "or ..." tests would
7895 * be done assuming a VLAN, even though the "or" could be viewed
7896 * as meaning "or, if this isn't a VLAN packet...".
7903 case DLT_NETANALYZER
:
7904 case DLT_NETANALYZER_TRANSPARENT
:
7905 /* check for VLAN, including QinQ */
7906 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7907 (bpf_int32
)ETHERTYPE_8021Q
);
7908 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7909 (bpf_int32
)ETHERTYPE_8021QINQ
);
7913 /* If a specific VLAN is requested, check VLAN id */
7914 if (vlan_num
>= 0) {
7915 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7916 (bpf_int32
)vlan_num
, 0x0fff);
7930 bpf_error("no VLAN support for data link type %d",
7945 struct block
*b0
,*b1
;
7948 * Change the offsets to point to the type and data fields within
7949 * the MPLS packet. Just increment the offsets, so that we
7950 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7951 * capture packets with an outer label of 100000 and an inner
7954 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7958 if (label_stack_depth
> 0) {
7959 /* just match the bottom-of-stack bit clear */
7960 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7963 * Indicate that we're checking MPLS-encapsulated headers,
7964 * to make sure higher level code generators don't try to
7965 * match against IP-related protocols such as Q_ARP, Q_RARP
7970 case DLT_C_HDLC
: /* fall through */
7972 case DLT_NETANALYZER
:
7973 case DLT_NETANALYZER_TRANSPARENT
:
7974 b0
= gen_linktype(ETHERTYPE_MPLS
);
7978 b0
= gen_linktype(PPP_MPLS_UCAST
);
7981 /* FIXME add other DLT_s ...
7982 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7983 * leave it for now */
7986 bpf_error("no MPLS support for data link type %d",
7994 /* If a specific MPLS label is requested, check it */
7995 if (label_num
>= 0) {
7996 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7997 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7998 0xfffff000); /* only compare the first 20 bits */
8005 label_stack_depth
++;
8010 * Support PPPOE discovery and session.
8015 /* check for PPPoE discovery */
8016 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8025 * Test against the PPPoE session link-layer type.
8027 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8030 * Change the offsets to point to the type and data fields within
8031 * the PPP packet, and note that this is PPPoE rather than
8034 * XXX - this is a bit of a kludge. If we were to split the
8035 * compiler into a parser that parses an expression and
8036 * generates an expression tree, and a code generator that
8037 * takes an expression tree (which could come from our
8038 * parser or from some other parser) and generates BPF code,
8039 * we could perhaps make the offsets parameters of routines
8040 * and, in the handler for an "AND" node, pass to subnodes
8041 * other than the PPPoE node the adjusted offsets.
8043 * This would mean that "pppoes" would, instead of changing the
8044 * behavior of *all* tests after it, change only the behavior
8045 * of tests ANDed with it. That would change the documented
8046 * semantics of "pppoes", which might break some expressions.
8047 * However, it would mean that "(pppoes and ip) or ip" would check
8048 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8049 * checking only for VLAN-encapsulated IP, so that could still
8050 * be considered worth doing; it wouldn't break expressions
8051 * that are of the form "pppoes and ..." which I suspect are the
8052 * most common expressions involving "pppoes". "pppoes or ..."
8053 * doesn't necessarily do what the user would really want, now,
8054 * as all the "or ..." tests would be done assuming PPPoE, even
8055 * though the "or" could be viewed as meaning "or, if this isn't
8056 * a PPPoE packet...".
8058 orig_linktype
= off_linktype
; /* save original values */
8063 * The "network-layer" protocol is PPPoE, which has a 6-byte
8064 * PPPoE header, followed by a PPP packet.
8066 * There is no HDLC encapsulation for the PPP packet (it's
8067 * encapsulated in PPPoES instead), so the link-layer type
8068 * starts at the first byte of the PPP packet. For PPPoE,
8069 * that offset is relative to the beginning of the total
8070 * link-layer payload, including any 802.2 LLC header, so
8071 * it's 6 bytes past off_nl.
8073 off_linktype
= off_nl
+ 6;
8076 * The network-layer offsets are relative to the beginning
8077 * of the MAC-layer payload; that's past the 6-byte
8078 * PPPoE header and the 2-byte PPP header.
8081 off_nl_nosnap
= 6+2;
8087 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8099 bpf_error("'vpi' supported only on raw ATM");
8100 if (off_vpi
== (u_int
)-1)
8102 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8108 bpf_error("'vci' supported only on raw ATM");
8109 if (off_vci
== (u_int
)-1)
8111 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8116 if (off_proto
== (u_int
)-1)
8117 abort(); /* XXX - this isn't on FreeBSD */
8118 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
8123 if (off_payload
== (u_int
)-1)
8125 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8126 0xffffffff, jtype
, reverse
, jvalue
);
8131 bpf_error("'callref' supported only on raw ATM");
8132 if (off_proto
== (u_int
)-1)
8134 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
8135 jtype
, reverse
, jvalue
);
8145 gen_atmtype_abbrev(type
)
8148 struct block
*b0
, *b1
;
8153 /* Get all packets in Meta signalling Circuit */
8155 bpf_error("'metac' supported only on raw ATM");
8156 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8157 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8162 /* Get all packets in Broadcast Circuit*/
8164 bpf_error("'bcc' supported only on raw ATM");
8165 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8166 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8171 /* Get all cells in Segment OAM F4 circuit*/
8173 bpf_error("'oam4sc' supported only on raw ATM");
8174 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8175 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8180 /* Get all cells in End-to-End OAM F4 Circuit*/
8182 bpf_error("'oam4ec' supported only on raw ATM");
8183 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8184 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8189 /* Get all packets in connection Signalling Circuit */
8191 bpf_error("'sc' supported only on raw ATM");
8192 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8193 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8198 /* Get all packets in ILMI Circuit */
8200 bpf_error("'ilmic' supported only on raw ATM");
8201 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8202 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8207 /* Get all LANE packets */
8209 bpf_error("'lane' supported only on raw ATM");
8210 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8213 * Arrange that all subsequent tests assume LANE
8214 * rather than LLC-encapsulated packets, and set
8215 * the offsets appropriately for LANE-encapsulated
8218 * "off_mac" is the offset of the Ethernet header,
8219 * which is 2 bytes past the ATM pseudo-header
8220 * (skipping the pseudo-header and 2-byte LE Client
8221 * field). The other offsets are Ethernet offsets
8222 * relative to "off_mac".
8225 off_mac
= off_payload
+ 2; /* MAC header */
8226 off_linktype
= off_mac
+ 12;
8227 off_macpl
= off_mac
+ 14; /* Ethernet */
8228 off_nl
= 0; /* Ethernet II */
8229 off_nl_nosnap
= 3; /* 802.3+802.2 */
8233 /* Get all LLC-encapsulated packets */
8235 bpf_error("'llc' supported only on raw ATM");
8236 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8247 * Filtering for MTP2 messages based on li value
8248 * FISU, length is null
8249 * LSSU, length is 1 or 2
8250 * MSU, length is 3 or more
8251 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8254 gen_mtp2type_abbrev(type
)
8257 struct block
*b0
, *b1
;
8262 if ( (linktype
!= DLT_MTP2
) &&
8263 (linktype
!= DLT_ERF
) &&
8264 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8265 bpf_error("'fisu' supported only on MTP2");
8266 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8267 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8271 if ( (linktype
!= DLT_MTP2
) &&
8272 (linktype
!= DLT_ERF
) &&
8273 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8274 bpf_error("'lssu' supported only on MTP2");
8275 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8276 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8281 if ( (linktype
!= DLT_MTP2
) &&
8282 (linktype
!= DLT_ERF
) &&
8283 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8284 bpf_error("'msu' supported only on MTP2");
8285 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8289 if ( (linktype
!= DLT_MTP2
) &&
8290 (linktype
!= DLT_ERF
) &&
8291 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8292 bpf_error("'hfisu' supported only on MTP2_HSL");
8293 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8294 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8298 if ( (linktype
!= DLT_MTP2
) &&
8299 (linktype
!= DLT_ERF
) &&
8300 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8301 bpf_error("'hlssu' supported only on MTP2_HSL");
8302 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8303 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8308 if ( (linktype
!= DLT_MTP2
) &&
8309 (linktype
!= DLT_ERF
) &&
8310 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8311 bpf_error("'hmsu' supported only on MTP2_HSL");
8312 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8322 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8329 bpf_u_int32 val1
, val2
, val3
;
8330 u_int newoff_sio
=off_sio
;
8331 u_int newoff_opc
=off_opc
;
8332 u_int newoff_dpc
=off_dpc
;
8333 u_int newoff_sls
=off_sls
;
8335 switch (mtp3field
) {
8338 newoff_sio
+= 3; /* offset for MTP2_HSL */
8342 if (off_sio
== (u_int
)-1)
8343 bpf_error("'sio' supported only on SS7");
8344 /* sio coded on 1 byte so max value 255 */
8346 bpf_error("sio value %u too big; max value = 255",
8348 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8349 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8355 if (off_opc
== (u_int
)-1)
8356 bpf_error("'opc' supported only on SS7");
8357 /* opc coded on 14 bits so max value 16383 */
8359 bpf_error("opc value %u too big; max value = 16383",
8361 /* the following instructions are made to convert jvalue
8362 * to the form used to write opc in an ss7 message*/
8363 val1
= jvalue
& 0x00003c00;
8365 val2
= jvalue
& 0x000003fc;
8367 val3
= jvalue
& 0x00000003;
8369 jvalue
= val1
+ val2
+ val3
;
8370 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8371 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8379 if (off_dpc
== (u_int
)-1)
8380 bpf_error("'dpc' supported only on SS7");
8381 /* dpc coded on 14 bits so max value 16383 */
8383 bpf_error("dpc value %u too big; max value = 16383",
8385 /* the following instructions are made to convert jvalue
8386 * to the forme used to write dpc in an ss7 message*/
8387 val1
= jvalue
& 0x000000ff;
8389 val2
= jvalue
& 0x00003f00;
8391 jvalue
= val1
+ val2
;
8392 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8393 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8399 if (off_sls
== (u_int
)-1)
8400 bpf_error("'sls' supported only on SS7");
8401 /* sls coded on 4 bits so max value 15 */
8403 bpf_error("sls value %u too big; max value = 15",
8405 /* the following instruction is made to convert jvalue
8406 * to the forme used to write sls in an ss7 message*/
8407 jvalue
= jvalue
<< 4;
8408 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8409 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8418 static struct block
*
8419 gen_msg_abbrev(type
)
8425 * Q.2931 signalling protocol messages for handling virtual circuits
8426 * establishment and teardown
8431 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8435 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8439 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8443 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8447 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8450 case A_RELEASE_DONE
:
8451 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8461 gen_atmmulti_abbrev(type
)
8464 struct block
*b0
, *b1
;
8470 bpf_error("'oam' supported only on raw ATM");
8471 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8476 bpf_error("'oamf4' supported only on raw ATM");
8478 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8479 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8481 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8487 * Get Q.2931 signalling messages for switched
8488 * virtual connection
8491 bpf_error("'connectmsg' supported only on raw ATM");
8492 b0
= gen_msg_abbrev(A_SETUP
);
8493 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8495 b0
= gen_msg_abbrev(A_CONNECT
);
8497 b0
= gen_msg_abbrev(A_CONNECTACK
);
8499 b0
= gen_msg_abbrev(A_RELEASE
);
8501 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8503 b0
= gen_atmtype_abbrev(A_SC
);
8509 bpf_error("'metaconnect' supported only on raw ATM");
8510 b0
= gen_msg_abbrev(A_SETUP
);
8511 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8513 b0
= gen_msg_abbrev(A_CONNECT
);
8515 b0
= gen_msg_abbrev(A_RELEASE
);
8517 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8519 b0
= gen_atmtype_abbrev(A_METAC
);