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.302 2007-11-18 02:03:52 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
71 #include "ieee80211.h"
73 #include "sunatmpos.h"
77 #ifdef HAVE_NET_PFVAR_H
78 #include <sys/socket.h>
80 #include <net/pfvar.h>
81 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
88 #include <netdb.h> /* for "struct addrinfo" */
91 #include <pcap/namedb.h>
96 #define IPPROTO_SCTP 132
99 #ifdef HAVE_OS_PROTO_H
100 #include "os-proto.h"
103 #define JMP(c) ((c)|BPF_JMP|BPF_K)
106 static jmp_buf top_ctx
;
107 static pcap_t
*bpf_pcap
;
109 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
111 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
113 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
118 static int pcap_fddipad
;
123 bpf_error(const char *fmt
, ...)
128 if (bpf_pcap
!= NULL
)
129 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
136 static void init_linktype(pcap_t
*);
138 static void init_regs(void);
139 static int alloc_reg(void);
140 static void free_reg(int);
142 static struct block
*root
;
145 * Value passed to gen_load_a() to indicate what the offset argument
149 OR_PACKET
, /* relative to the beginning of the packet */
150 OR_LINK
, /* relative to the beginning of the link-layer header */
151 OR_MACPL
, /* relative to the end of the MAC-layer header */
152 OR_NET
, /* relative to the network-layer header */
153 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
154 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
155 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
159 * We divy out chunks of memory rather than call malloc each time so
160 * we don't have to worry about leaking memory. It's probably
161 * not a big deal if all this memory was wasted but if this ever
162 * goes into a library that would probably not be a good idea.
164 * XXX - this *is* in a library....
167 #define CHUNK0SIZE 1024
173 static struct chunk chunks
[NCHUNKS
];
174 static int cur_chunk
;
176 static void *newchunk(u_int
);
177 static void freechunks(void);
178 static inline struct block
*new_block(int);
179 static inline struct slist
*new_stmt(int);
180 static struct block
*gen_retblk(int);
181 static inline void syntax(void);
183 static void backpatch(struct block
*, struct block
*);
184 static void merge(struct block
*, struct block
*);
185 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
186 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
187 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
188 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
189 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
190 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
192 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
193 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
194 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
195 static struct slist
*gen_load_llrel(u_int
, u_int
);
196 static struct slist
*gen_load_macplrel(u_int
, u_int
);
197 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
198 static struct slist
*gen_loadx_iphdrlen(void);
199 static struct block
*gen_uncond(int);
200 static inline struct block
*gen_true(void);
201 static inline struct block
*gen_false(void);
202 static struct block
*gen_ether_linktype(int);
203 static struct block
*gen_linux_sll_linktype(int);
204 static struct slist
*gen_load_radiotap_llprefixlen(void);
205 static struct slist
*gen_load_ppi_llprefixlen(void);
206 static void insert_compute_vloffsets(struct block
*);
207 static struct slist
*gen_llprefixlen(void);
208 static struct slist
*gen_off_macpl(void);
209 static int ethertype_to_ppptype(int);
210 static struct block
*gen_linktype(int);
211 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
212 static struct block
*gen_llc_linktype(int);
213 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
215 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
217 static struct block
*gen_ahostop(const u_char
*, int);
218 static struct block
*gen_ehostop(const u_char
*, int);
219 static struct block
*gen_fhostop(const u_char
*, int);
220 static struct block
*gen_thostop(const u_char
*, int);
221 static struct block
*gen_wlanhostop(const u_char
*, int);
222 static struct block
*gen_ipfchostop(const u_char
*, int);
223 static struct block
*gen_dnhostop(bpf_u_int32
, int);
224 static struct block
*gen_mpls_linktype(int);
225 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
227 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
230 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
232 static struct block
*gen_ipfrag(void);
233 static struct block
*gen_portatom(int, bpf_int32
);
234 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
236 static struct block
*gen_portatom6(int, bpf_int32
);
237 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
239 struct block
*gen_portop(int, int, int);
240 static struct block
*gen_port(int, int, int);
241 struct block
*gen_portrangeop(int, int, int, int);
242 static struct block
*gen_portrange(int, int, int, int);
244 struct block
*gen_portop6(int, int, int);
245 static struct block
*gen_port6(int, int, int);
246 struct block
*gen_portrangeop6(int, int, int, int);
247 static struct block
*gen_portrange6(int, int, int, int);
249 static int lookup_proto(const char *, int);
250 static struct block
*gen_protochain(int, int, int);
251 static struct block
*gen_proto(int, int, int);
252 static struct slist
*xfer_to_x(struct arth
*);
253 static struct slist
*xfer_to_a(struct arth
*);
254 static struct block
*gen_mac_multicast(int);
255 static struct block
*gen_len(int, int);
256 static struct block
*gen_check_802_11_data_frame(void);
258 static struct block
*gen_ppi_dlt_check(void);
259 static struct block
*gen_msg_abbrev(int type
);
270 /* XXX Round up to nearest long. */
271 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
273 /* XXX Round up to structure boundary. */
277 cp
= &chunks
[cur_chunk
];
278 if (n
> cp
->n_left
) {
279 ++cp
, k
= ++cur_chunk
;
281 bpf_error("out of memory");
282 size
= CHUNK0SIZE
<< k
;
283 cp
->m
= (void *)malloc(size
);
285 bpf_error("out of memory");
286 memset((char *)cp
->m
, 0, size
);
289 bpf_error("out of memory");
292 return (void *)((char *)cp
->m
+ cp
->n_left
);
301 for (i
= 0; i
< NCHUNKS
; ++i
)
302 if (chunks
[i
].m
!= NULL
) {
309 * A strdup whose allocations are freed after code generation is over.
313 register const char *s
;
315 int n
= strlen(s
) + 1;
316 char *cp
= newchunk(n
);
322 static inline struct block
*
328 p
= (struct block
*)newchunk(sizeof(*p
));
335 static inline struct slist
*
341 p
= (struct slist
*)newchunk(sizeof(*p
));
347 static struct block
*
351 struct block
*b
= new_block(BPF_RET
|BPF_K
);
360 bpf_error("syntax error in filter expression");
363 static bpf_u_int32 netmask
;
368 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
369 const char *buf
, int optimize
, bpf_u_int32 mask
)
372 const char * volatile xbuf
= buf
;
380 if (setjmp(top_ctx
)) {
388 snaplen
= pcap_snapshot(p
);
390 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
391 "snaplen of 0 rejects all packets");
395 lex_init(xbuf
? xbuf
: "");
403 root
= gen_retblk(snaplen
);
405 if (optimize
&& !no_optimize
) {
408 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
409 bpf_error("expression rejects all packets");
411 program
->bf_insns
= icode_to_fcode(root
, &len
);
412 program
->bf_len
= len
;
420 * entry point for using the compiler with no pcap open
421 * pass in all the stuff that is needed explicitly instead.
424 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
425 struct bpf_program
*program
,
426 const char *buf
, int optimize
, bpf_u_int32 mask
)
431 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
434 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
440 * Clean up a "struct bpf_program" by freeing all the memory allocated
444 pcap_freecode(struct bpf_program
*program
)
447 if (program
->bf_insns
!= NULL
) {
448 free((char *)program
->bf_insns
);
449 program
->bf_insns
= NULL
;
454 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
455 * which of the jt and jf fields has been resolved and which is a pointer
456 * back to another unresolved block (or nil). At least one of the fields
457 * in each block is already resolved.
460 backpatch(list
, target
)
461 struct block
*list
, *target
;
478 * Merge the lists in b0 and b1, using the 'sense' field to indicate
479 * which of jt and jf is the link.
483 struct block
*b0
, *b1
;
485 register struct block
**p
= &b0
;
487 /* Find end of list. */
489 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
491 /* Concatenate the lists. */
499 struct block
*ppi_dlt_check
;
502 * Insert before the statements of the first (root) block any
503 * statements needed to load the lengths of any variable-length
504 * headers into registers.
506 * XXX - a fancier strategy would be to insert those before the
507 * statements of all blocks that use those lengths and that
508 * have no predecessors that use them, so that we only compute
509 * the lengths if we need them. There might be even better
510 * approaches than that.
512 * However, those strategies would be more complicated, and
513 * as we don't generate code to compute a length if the
514 * program has no tests that use the length, and as most
515 * tests will probably use those lengths, we would just
516 * postpone computing the lengths so that it's not done
517 * for tests that fail early, and it's not clear that's
520 insert_compute_vloffsets(p
->head
);
523 * For DLT_PPI captures, generate a check of the per-packet
524 * DLT value to make sure it's DLT_IEEE802_11.
526 ppi_dlt_check
= gen_ppi_dlt_check();
527 if (ppi_dlt_check
!= NULL
)
528 gen_and(ppi_dlt_check
, p
);
530 backpatch(p
, gen_retblk(snaplen
));
531 p
->sense
= !p
->sense
;
532 backpatch(p
, gen_retblk(0));
538 struct block
*b0
, *b1
;
540 backpatch(b0
, b1
->head
);
541 b0
->sense
= !b0
->sense
;
542 b1
->sense
= !b1
->sense
;
544 b1
->sense
= !b1
->sense
;
550 struct block
*b0
, *b1
;
552 b0
->sense
= !b0
->sense
;
553 backpatch(b0
, b1
->head
);
554 b0
->sense
= !b0
->sense
;
563 b
->sense
= !b
->sense
;
566 static struct block
*
567 gen_cmp(offrel
, offset
, size
, v
)
568 enum e_offrel offrel
;
572 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
575 static struct block
*
576 gen_cmp_gt(offrel
, offset
, size
, v
)
577 enum e_offrel offrel
;
581 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
584 static struct block
*
585 gen_cmp_ge(offrel
, offset
, size
, v
)
586 enum e_offrel offrel
;
590 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
593 static struct block
*
594 gen_cmp_lt(offrel
, offset
, size
, v
)
595 enum e_offrel offrel
;
599 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
602 static struct block
*
603 gen_cmp_le(offrel
, offset
, size
, v
)
604 enum e_offrel offrel
;
608 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
611 static struct block
*
612 gen_mcmp(offrel
, offset
, size
, v
, mask
)
613 enum e_offrel offrel
;
618 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
621 static struct block
*
622 gen_bcmp(offrel
, offset
, size
, v
)
623 enum e_offrel offrel
;
624 register u_int offset
, size
;
625 register const u_char
*v
;
627 register struct block
*b
, *tmp
;
631 register const u_char
*p
= &v
[size
- 4];
632 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
633 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
635 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
642 register const u_char
*p
= &v
[size
- 2];
643 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
645 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
652 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
661 * AND the field of size "size" at offset "offset" relative to the header
662 * specified by "offrel" with "mask", and compare it with the value "v"
663 * with the test specified by "jtype"; if "reverse" is true, the test
664 * should test the opposite of "jtype".
666 static struct block
*
667 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
668 enum e_offrel offrel
;
670 bpf_u_int32 offset
, size
, mask
, jtype
;
673 struct slist
*s
, *s2
;
676 s
= gen_load_a(offrel
, offset
, size
);
678 if (mask
!= 0xffffffff) {
679 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
684 b
= new_block(JMP(jtype
));
687 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
693 * Various code constructs need to know the layout of the data link
694 * layer. These variables give the necessary offsets from the beginning
695 * of the packet data.
699 * This is the offset of the beginning of the link-layer header from
700 * the beginning of the raw packet data.
702 * It's usually 0, except for 802.11 with a fixed-length radio header.
703 * (For 802.11 with a variable-length radio header, we have to generate
704 * code to compute that offset; off_ll is 0 in that case.)
709 * If there's a variable-length header preceding the link-layer header,
710 * "reg_off_ll" is the register number for a register containing the
711 * length of that header, and therefore the offset of the link-layer
712 * header from the beginning of the raw packet data. Otherwise,
713 * "reg_off_ll" is -1.
715 static int reg_off_ll
;
718 * This is the offset of the beginning of the MAC-layer header from
719 * the beginning of the link-layer header.
720 * It's usually 0, except for ATM LANE, where it's the offset, relative
721 * to the beginning of the raw packet data, of the Ethernet header.
723 static u_int off_mac
;
726 * This is the offset of the beginning of the MAC-layer payload,
727 * from the beginning of the raw packet data.
729 * I.e., it's the sum of the length of the link-layer header (without,
730 * for example, any 802.2 LLC header, so it's the MAC-layer
731 * portion of that header), plus any prefix preceding the
734 static u_int off_macpl
;
737 * This is 1 if the offset of the beginning of the MAC-layer payload
738 * from the beginning of the link-layer header is variable-length.
740 static int off_macpl_is_variable
;
743 * If the link layer has variable_length headers, "reg_off_macpl"
744 * is the register number for a register containing the length of the
745 * link-layer header plus the length of any variable-length header
746 * preceding the link-layer header. Otherwise, "reg_off_macpl"
749 static int reg_off_macpl
;
752 * "off_linktype" is the offset to information in the link-layer header
753 * giving the packet type. This offset is relative to the beginning
754 * of the link-layer header (i.e., it doesn't include off_ll).
756 * For Ethernet, it's the offset of the Ethernet type field.
758 * For link-layer types that always use 802.2 headers, it's the
759 * offset of the LLC header.
761 * For PPP, it's the offset of the PPP type field.
763 * For Cisco HDLC, it's the offset of the CHDLC type field.
765 * For BSD loopback, it's the offset of the AF_ value.
767 * For Linux cooked sockets, it's the offset of the type field.
769 * It's set to -1 for no encapsulation, in which case, IP is assumed.
771 static u_int off_linktype
;
774 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
775 * checks to check the PPP header, assumed to follow a LAN-style link-
776 * layer header and a PPPoE session header.
778 static int is_pppoes
= 0;
781 * TRUE if the link layer includes an ATM pseudo-header.
783 static int is_atm
= 0;
786 * TRUE if "lane" appeared in the filter; it causes us to generate
787 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
789 static int is_lane
= 0;
792 * These are offsets for the ATM pseudo-header.
794 static u_int off_vpi
;
795 static u_int off_vci
;
796 static u_int off_proto
;
799 * These are offsets for the MTP2 fields.
804 * These are offsets for the MTP3 fields.
806 static u_int off_sio
;
807 static u_int off_opc
;
808 static u_int off_dpc
;
809 static u_int off_sls
;
812 * This is the offset of the first byte after the ATM pseudo_header,
813 * or -1 if there is no ATM pseudo-header.
815 static u_int off_payload
;
818 * These are offsets to the beginning of the network-layer header.
819 * They are relative to the beginning of the MAC-layer payload (i.e.,
820 * they don't include off_ll or off_macpl).
822 * If the link layer never uses 802.2 LLC:
824 * "off_nl" and "off_nl_nosnap" are the same.
826 * If the link layer always uses 802.2 LLC:
828 * "off_nl" is the offset if there's a SNAP header following
831 * "off_nl_nosnap" is the offset if there's no SNAP header.
833 * If the link layer is Ethernet:
835 * "off_nl" is the offset if the packet is an Ethernet II packet
836 * (we assume no 802.3+802.2+SNAP);
838 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
839 * with an 802.2 header following it.
842 static u_int off_nl_nosnap
;
850 linktype
= pcap_datalink(p
);
852 pcap_fddipad
= p
->fddipad
;
856 * Assume it's not raw ATM with a pseudo-header, for now.
867 * And that we're not doing PPPoE.
872 * And assume we're not doing SS7.
881 * Also assume it's not 802.11.
885 off_macpl_is_variable
= 0;
889 label_stack_depth
= 0;
899 off_nl
= 0; /* XXX in reality, variable! */
900 off_nl_nosnap
= 0; /* no 802.2 LLC */
903 case DLT_ARCNET_LINUX
:
906 off_nl
= 0; /* XXX in reality, variable! */
907 off_nl_nosnap
= 0; /* no 802.2 LLC */
912 off_macpl
= 14; /* Ethernet header length */
913 off_nl
= 0; /* Ethernet II */
914 off_nl_nosnap
= 3; /* 802.3+802.2 */
919 * SLIP doesn't have a link level type. The 16 byte
920 * header is hacked into our SLIP driver.
925 off_nl_nosnap
= 0; /* no 802.2 LLC */
929 /* XXX this may be the same as the DLT_PPP_BSDOS case */
934 off_nl_nosnap
= 0; /* no 802.2 LLC */
942 off_nl_nosnap
= 0; /* no 802.2 LLC */
949 off_nl_nosnap
= 0; /* no 802.2 LLC */
954 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
955 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
959 off_nl_nosnap
= 0; /* no 802.2 LLC */
964 * This does no include the Ethernet header, and
965 * only covers session state.
970 off_nl_nosnap
= 0; /* no 802.2 LLC */
977 off_nl_nosnap
= 0; /* no 802.2 LLC */
982 * FDDI doesn't really have a link-level type field.
983 * We set "off_linktype" to the offset of the LLC header.
985 * To check for Ethernet types, we assume that SSAP = SNAP
986 * is being used and pick out the encapsulated Ethernet type.
987 * XXX - should we generate code to check for SNAP?
991 off_linktype
+= pcap_fddipad
;
993 off_macpl
= 13; /* FDDI MAC header length */
995 off_macpl
+= pcap_fddipad
;
997 off_nl
= 8; /* 802.2+SNAP */
998 off_nl_nosnap
= 3; /* 802.2 */
1003 * Token Ring doesn't really have a link-level type field.
1004 * We set "off_linktype" to the offset of the LLC header.
1006 * To check for Ethernet types, we assume that SSAP = SNAP
1007 * is being used and pick out the encapsulated Ethernet type.
1008 * XXX - should we generate code to check for SNAP?
1010 * XXX - the header is actually variable-length.
1011 * Some various Linux patched versions gave 38
1012 * as "off_linktype" and 40 as "off_nl"; however,
1013 * if a token ring packet has *no* routing
1014 * information, i.e. is not source-routed, the correct
1015 * values are 20 and 22, as they are in the vanilla code.
1017 * A packet is source-routed iff the uppermost bit
1018 * of the first byte of the source address, at an
1019 * offset of 8, has the uppermost bit set. If the
1020 * packet is source-routed, the total number of bytes
1021 * of routing information is 2 plus bits 0x1F00 of
1022 * the 16-bit value at an offset of 14 (shifted right
1023 * 8 - figure out which byte that is).
1026 off_macpl
= 14; /* Token Ring MAC header length */
1027 off_nl
= 8; /* 802.2+SNAP */
1028 off_nl_nosnap
= 3; /* 802.2 */
1031 case DLT_IEEE802_11
:
1033 * 802.11 doesn't really have a link-level type field.
1034 * We set "off_linktype" to the offset of the LLC header.
1036 * To check for Ethernet types, we assume that SSAP = SNAP
1037 * is being used and pick out the encapsulated Ethernet type.
1038 * XXX - should we generate code to check for SNAP?
1040 * XXX - the header is actually variable-length. We
1041 * assume a 24-byte link-layer header, as appears in
1042 * data frames in networks with no bridges. If the
1043 * fromds and tods 802.11 header bits are both set,
1044 * it's actually supposed to be 30 bytes.
1047 off_macpl
= 0; /* link-layer header is variable-length */
1048 off_macpl_is_variable
= 1;
1049 off_nl
= 8; /* 802.2+SNAP */
1050 off_nl_nosnap
= 3; /* 802.2 */
1053 case DLT_PRISM_HEADER
:
1055 * Same as 802.11, but with an additional header before
1056 * the 802.11 header, containing a bunch of additional
1057 * information including radio-level information.
1059 * The header is 144 bytes long.
1061 * XXX - same variable-length header problem; at least
1062 * the Prism header is fixed-length.
1066 off_macpl
= 0; /* link-layer header is variable-length */
1067 off_macpl_is_variable
= 1;
1068 off_nl
= 8; /* 802.2+SNAP */
1069 off_nl_nosnap
= 3; /* 802.2 */
1072 case DLT_IEEE802_11_RADIO_AVS
:
1074 * Same as 802.11, but with an additional header before
1075 * the 802.11 header, containing a bunch of additional
1076 * information including radio-level information.
1078 * The header is 64 bytes long, at least in its
1079 * current incarnation.
1081 * XXX - same variable-length header problem, only
1082 * more so; this header is also variable-length,
1083 * with the length being the 32-bit big-endian
1084 * number at an offset of 4 from the beginning
1085 * of the radio header. We should handle that the
1086 * same way we handle the length at the beginning
1087 * of the radiotap header.
1089 * XXX - in Linux, do any drivers that supply an AVS
1090 * header supply a link-layer type other than
1091 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1092 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1093 * any drivers that supply an AVS header but supply
1094 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1095 * have to check the header in the generated code to
1096 * determine whether it's Prism or AVS.
1100 off_macpl
= 0; /* link-layer header is variable-length */
1101 off_macpl_is_variable
= 1;
1102 off_nl
= 8; /* 802.2+SNAP */
1103 off_nl_nosnap
= 3; /* 802.2 */
1107 * At the moment we treat PPI as normal Radiotap encoded
1108 * packets. The difference is in the function that generates
1109 * the code at the beginning to compute the header length.
1110 * Since this code generator of PPI supports bare 802.11
1111 * encapsulation only (i.e. the encapsulated DLT should be
1112 * DLT_IEEE802_11) we generate code to check for this too.
1115 case DLT_IEEE802_11_RADIO
:
1117 * Same as 802.11, but with an additional header before
1118 * the 802.11 header, containing a bunch of additional
1119 * information including radio-level information.
1121 * The radiotap header is variable length, and we
1122 * generate code to compute its length and store it
1123 * in a register. These offsets are relative to the
1124 * beginning of the 802.11 header.
1127 off_macpl
= 0; /* link-layer header is variable-length */
1128 off_macpl_is_variable
= 1;
1129 off_nl
= 8; /* 802.2+SNAP */
1130 off_nl_nosnap
= 3; /* 802.2 */
1133 case DLT_ATM_RFC1483
:
1134 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1136 * assume routed, non-ISO PDUs
1137 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1139 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1140 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1141 * latter would presumably be treated the way PPPoE
1142 * should be, so you can do "pppoe and udp port 2049"
1143 * or "pppoa and tcp port 80" and have it check for
1144 * PPPo{A,E} and a PPP protocol of IP and....
1147 off_macpl
= 0; /* packet begins with LLC header */
1148 off_nl
= 8; /* 802.2+SNAP */
1149 off_nl_nosnap
= 3; /* 802.2 */
1154 * Full Frontal ATM; you get AALn PDUs with an ATM
1158 off_vpi
= SUNATM_VPI_POS
;
1159 off_vci
= SUNATM_VCI_POS
;
1160 off_proto
= PROTO_POS
;
1161 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1162 off_payload
= SUNATM_PKT_BEGIN_POS
;
1163 off_linktype
= off_payload
;
1164 off_macpl
= off_payload
; /* if LLC-encapsulated */
1165 off_nl
= 8; /* 802.2+SNAP */
1166 off_nl_nosnap
= 3; /* 802.2 */
1173 off_nl_nosnap
= 0; /* no 802.2 LLC */
1176 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1180 off_nl_nosnap
= 0; /* no 802.2 LLC */
1185 * LocalTalk does have a 1-byte type field in the LLAP header,
1186 * but really it just indicates whether there is a "short" or
1187 * "long" DDP packet following.
1192 off_nl_nosnap
= 0; /* no 802.2 LLC */
1195 case DLT_IP_OVER_FC
:
1197 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1198 * link-level type field. We set "off_linktype" to the
1199 * offset of the LLC header.
1201 * To check for Ethernet types, we assume that SSAP = SNAP
1202 * is being used and pick out the encapsulated Ethernet type.
1203 * XXX - should we generate code to check for SNAP? RFC
1204 * 2625 says SNAP should be used.
1208 off_nl
= 8; /* 802.2+SNAP */
1209 off_nl_nosnap
= 3; /* 802.2 */
1214 * XXX - we should set this to handle SNAP-encapsulated
1215 * frames (NLPID of 0x80).
1220 off_nl_nosnap
= 0; /* no 802.2 LLC */
1224 * the only BPF-interesting FRF.16 frames are non-control frames;
1225 * Frame Relay has a variable length link-layer
1226 * so lets start with offset 4 for now and increments later on (FIXME);
1232 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1235 case DLT_APPLE_IP_OVER_IEEE1394
:
1239 off_nl_nosnap
= 0; /* no 802.2 LLC */
1242 case DLT_LINUX_IRDA
:
1244 * Currently, only raw "link[N:M]" filtering is supported.
1254 * Currently, only raw "link[N:M]" filtering is supported.
1262 case DLT_SYMANTEC_FIREWALL
:
1265 off_nl
= 0; /* Ethernet II */
1266 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1269 #ifdef HAVE_NET_PFVAR_H
1272 off_macpl
= PFLOG_HDRLEN
;
1274 off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 case DLT_JUNIPER_MFR
:
1279 case DLT_JUNIPER_MLFR
:
1280 case DLT_JUNIPER_MLPPP
:
1281 case DLT_JUNIPER_PPP
:
1282 case DLT_JUNIPER_CHDLC
:
1283 case DLT_JUNIPER_FRELAY
:
1287 off_nl_nosnap
= -1; /* no 802.2 LLC */
1290 case DLT_JUNIPER_ATM1
:
1291 off_linktype
= 4; /* in reality variable between 4-8 */
1292 off_macpl
= 4; /* in reality variable between 4-8 */
1297 case DLT_JUNIPER_ATM2
:
1298 off_linktype
= 8; /* in reality variable between 8-12 */
1299 off_macpl
= 8; /* in reality variable between 8-12 */
1304 /* frames captured on a Juniper PPPoE service PIC
1305 * contain raw ethernet frames */
1306 case DLT_JUNIPER_PPPOE
:
1307 case DLT_JUNIPER_ETHER
:
1310 off_nl
= 18; /* Ethernet II */
1311 off_nl_nosnap
= 21; /* 802.3+802.2 */
1314 case DLT_JUNIPER_PPPOE_ATM
:
1318 off_nl_nosnap
= -1; /* no 802.2 LLC */
1321 case DLT_JUNIPER_GGSN
:
1325 off_nl_nosnap
= -1; /* no 802.2 LLC */
1328 case DLT_JUNIPER_ES
:
1330 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl
= -1; /* not really a network layer but raw IP addresses */
1332 off_nl_nosnap
= -1; /* no 802.2 LLC */
1335 case DLT_JUNIPER_MONITOR
:
1338 off_nl
= 0; /* raw IP/IP6 header */
1339 off_nl_nosnap
= -1; /* no 802.2 LLC */
1342 case DLT_JUNIPER_SERVICES
:
1344 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl
= -1; /* L3 proto location dep. on cookie type */
1346 off_nl_nosnap
= -1; /* no 802.2 LLC */
1349 case DLT_JUNIPER_VP
:
1356 case DLT_JUNIPER_ST
:
1363 case DLT_JUNIPER_ISM
:
1382 case DLT_MTP2_WITH_PHDR
:
1415 case DLT_LINUX_LAPD
:
1417 * Currently, only raw "link[N:M]" filtering is supported.
1427 * Currently, only raw "link[N:M]" filtering is supported.
1435 case DLT_BLUETOOTH_HCI_H4
:
1437 * Currently, only raw "link[N:M]" filtering is supported.
1447 * Currently, only raw "link[N:M]" filtering is supported.
1457 * Currently, only raw "link[N:M]" filtering is supported.
1465 case DLT_IEEE802_15_4_LINUX
:
1467 * Currently, only raw "link[N:M]" filtering is supported.
1475 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1477 * Currently, only raw "link[N:M]" filtering is supported.
1485 case DLT_IEEE802_15_4
:
1487 * Currently, only raw "link[N:M]" filtering is supported.
1497 * Currently, only raw "link[N:M]" filtering is supported.
1507 * Currently, only raw "link[N:M]" filtering is supported.
1517 * Currently, only raw "link[N:M]" filtering is supported.
1525 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1527 * Currently, only raw "link[N:M]" filtering is supported.
1537 * Currently, only raw "link[N:M]" filtering is supported.
1539 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1541 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1542 off_nl_nosnap
= -1; /* no 802.2 LLC */
1543 off_mac
= 1; /* step over the kiss length byte */
1546 bpf_error("unknown data link type %d", linktype
);
1551 * Load a value relative to the beginning of the link-layer header.
1552 * The link-layer header doesn't necessarily begin at the beginning
1553 * of the packet data; there might be a variable-length prefix containing
1554 * radio information.
1556 static struct slist
*
1557 gen_load_llrel(offset
, size
)
1560 struct slist
*s
, *s2
;
1562 s
= gen_llprefixlen();
1565 * If "s" is non-null, it has code to arrange that the X register
1566 * contains the length of the prefix preceding the link-layer
1569 * Otherwise, the length of the prefix preceding the link-layer
1570 * header is "off_ll".
1574 * There's a variable-length prefix preceding the
1575 * link-layer header. "s" points to a list of statements
1576 * that put the length of that prefix into the X register.
1577 * do an indirect load, to use the X register as an offset.
1579 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1584 * There is no variable-length header preceding the
1585 * link-layer header; add in off_ll, which, if there's
1586 * a fixed-length header preceding the link-layer header,
1587 * is the length of that header.
1589 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1590 s
->s
.k
= offset
+ off_ll
;
1596 * Load a value relative to the beginning of the MAC-layer payload.
1598 static struct slist
*
1599 gen_load_macplrel(offset
, size
)
1602 struct slist
*s
, *s2
;
1604 s
= gen_off_macpl();
1607 * If s is non-null, the offset of the MAC-layer payload is
1608 * variable, and s points to a list of instructions that
1609 * arrange that the X register contains that offset.
1611 * Otherwise, the offset of the MAC-layer payload is constant,
1612 * and is in off_macpl.
1616 * The offset of the MAC-layer payload is in the X
1617 * register. Do an indirect load, to use the X register
1620 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1625 * The offset of the MAC-layer payload is constant,
1626 * and is in off_macpl; load the value at that offset
1627 * plus the specified offset.
1629 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1630 s
->s
.k
= off_macpl
+ offset
;
1636 * Load a value relative to the beginning of the specified header.
1638 static struct slist
*
1639 gen_load_a(offrel
, offset
, size
)
1640 enum e_offrel offrel
;
1643 struct slist
*s
, *s2
;
1648 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1653 s
= gen_load_llrel(offset
, size
);
1657 s
= gen_load_macplrel(offset
, size
);
1661 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1665 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1670 * Load the X register with the length of the IPv4 header
1671 * (plus the offset of the link-layer header, if it's
1672 * preceded by a variable-length header such as a radio
1673 * header), in bytes.
1675 s
= gen_loadx_iphdrlen();
1678 * Load the item at {offset of the MAC-layer payload} +
1679 * {offset, relative to the start of the MAC-layer
1680 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1681 * {specified offset}.
1683 * (If the offset of the MAC-layer payload is variable,
1684 * it's included in the value in the X register, and
1687 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1688 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1693 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1704 * Generate code to load into the X register the sum of the length of
1705 * the IPv4 header and any variable-length header preceding the link-layer
1708 static struct slist
*
1709 gen_loadx_iphdrlen()
1711 struct slist
*s
, *s2
;
1713 s
= gen_off_macpl();
1716 * There's a variable-length prefix preceding the
1717 * link-layer header, or the link-layer header is itself
1718 * variable-length. "s" points to a list of statements
1719 * that put the offset of the MAC-layer payload into
1722 * The 4*([k]&0xf) addressing mode can't be used, as we
1723 * don't have a constant offset, so we have to load the
1724 * value in question into the A register and add to it
1725 * the value from the X register.
1727 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1730 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1733 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1738 * The A register now contains the length of the
1739 * IP header. We need to add to it the offset of
1740 * the MAC-layer payload, which is still in the X
1741 * register, and move the result into the X register.
1743 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1744 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1747 * There is no variable-length header preceding the
1748 * link-layer header, and the link-layer header is
1749 * fixed-length; load the length of the IPv4 header,
1750 * which is at an offset of off_nl from the beginning
1751 * of the MAC-layer payload, and thus at an offset
1752 * of off_mac_pl + off_nl from the beginning of the
1755 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1756 s
->s
.k
= off_macpl
+ off_nl
;
1761 static struct block
*
1768 s
= new_stmt(BPF_LD
|BPF_IMM
);
1770 b
= new_block(JMP(BPF_JEQ
));
1776 static inline struct block
*
1779 return gen_uncond(1);
1782 static inline struct block
*
1785 return gen_uncond(0);
1789 * Byte-swap a 32-bit number.
1790 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1791 * big-endian platforms.)
1793 #define SWAPLONG(y) \
1794 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1797 * Generate code to match a particular packet type.
1799 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1800 * value, if <= ETHERMTU. We use that to determine whether to
1801 * match the type/length field or to check the type/length field for
1802 * a value <= ETHERMTU to see whether it's a type field and then do
1803 * the appropriate test.
1805 static struct block
*
1806 gen_ether_linktype(proto
)
1809 struct block
*b0
, *b1
;
1815 case LLCSAP_NETBEUI
:
1817 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1818 * so we check the DSAP and SSAP.
1820 * LLCSAP_IP checks for IP-over-802.2, rather
1821 * than IP-over-Ethernet or IP-over-SNAP.
1823 * XXX - should we check both the DSAP and the
1824 * SSAP, like this, or should we check just the
1825 * DSAP, as we do for other types <= ETHERMTU
1826 * (i.e., other SAP values)?
1828 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1830 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1831 ((proto
<< 8) | proto
));
1839 * Ethernet_II frames, which are Ethernet
1840 * frames with a frame type of ETHERTYPE_IPX;
1842 * Ethernet_802.3 frames, which are 802.3
1843 * frames (i.e., the type/length field is
1844 * a length field, <= ETHERMTU, rather than
1845 * a type field) with the first two bytes
1846 * after the Ethernet/802.3 header being
1849 * Ethernet_802.2 frames, which are 802.3
1850 * frames with an 802.2 LLC header and
1851 * with the IPX LSAP as the DSAP in the LLC
1854 * Ethernet_SNAP frames, which are 802.3
1855 * frames with an LLC header and a SNAP
1856 * header and with an OUI of 0x000000
1857 * (encapsulated Ethernet) and a protocol
1858 * ID of ETHERTYPE_IPX in the SNAP header.
1860 * XXX - should we generate the same code both
1861 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1865 * This generates code to check both for the
1866 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1868 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1869 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1873 * Now we add code to check for SNAP frames with
1874 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1876 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1880 * Now we generate code to check for 802.3
1881 * frames in general.
1883 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1887 * Now add the check for 802.3 frames before the
1888 * check for Ethernet_802.2 and Ethernet_802.3,
1889 * as those checks should only be done on 802.3
1890 * frames, not on Ethernet frames.
1895 * Now add the check for Ethernet_II frames, and
1896 * do that before checking for the other frame
1899 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1900 (bpf_int32
)ETHERTYPE_IPX
);
1904 case ETHERTYPE_ATALK
:
1905 case ETHERTYPE_AARP
:
1907 * EtherTalk (AppleTalk protocols on Ethernet link
1908 * layer) may use 802.2 encapsulation.
1912 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1913 * we check for an Ethernet type field less than
1914 * 1500, which means it's an 802.3 length field.
1916 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1920 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1921 * SNAP packets with an organization code of
1922 * 0x080007 (Apple, for Appletalk) and a protocol
1923 * type of ETHERTYPE_ATALK (Appletalk).
1925 * 802.2-encapsulated ETHERTYPE_AARP packets are
1926 * SNAP packets with an organization code of
1927 * 0x000000 (encapsulated Ethernet) and a protocol
1928 * type of ETHERTYPE_AARP (Appletalk ARP).
1930 if (proto
== ETHERTYPE_ATALK
)
1931 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1932 else /* proto == ETHERTYPE_AARP */
1933 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1937 * Check for Ethernet encapsulation (Ethertalk
1938 * phase 1?); we just check for the Ethernet
1941 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1947 if (proto
<= ETHERMTU
) {
1949 * This is an LLC SAP value, so the frames
1950 * that match would be 802.2 frames.
1951 * Check that the frame is an 802.2 frame
1952 * (i.e., that the length/type field is
1953 * a length field, <= ETHERMTU) and
1954 * then check the DSAP.
1956 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1958 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1964 * This is an Ethernet type, so compare
1965 * the length/type field with it (if
1966 * the frame is an 802.2 frame, the length
1967 * field will be <= ETHERMTU, and, as
1968 * "proto" is > ETHERMTU, this test
1969 * will fail and the frame won't match,
1970 * which is what we want).
1972 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1979 * Generate code to match a particular packet type.
1981 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1982 * value, if <= ETHERMTU. We use that to determine whether to
1983 * match the type field or to check the type field for the special
1984 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1986 static struct block
*
1987 gen_linux_sll_linktype(proto
)
1990 struct block
*b0
, *b1
;
1996 case LLCSAP_NETBEUI
:
1998 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1999 * so we check the DSAP and SSAP.
2001 * LLCSAP_IP checks for IP-over-802.2, rather
2002 * than IP-over-Ethernet or IP-over-SNAP.
2004 * XXX - should we check both the DSAP and the
2005 * SSAP, like this, or should we check just the
2006 * DSAP, as we do for other types <= ETHERMTU
2007 * (i.e., other SAP values)?
2009 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2010 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2011 ((proto
<< 8) | proto
));
2017 * Ethernet_II frames, which are Ethernet
2018 * frames with a frame type of ETHERTYPE_IPX;
2020 * Ethernet_802.3 frames, which have a frame
2021 * type of LINUX_SLL_P_802_3;
2023 * Ethernet_802.2 frames, which are 802.3
2024 * frames with an 802.2 LLC header (i.e, have
2025 * a frame type of LINUX_SLL_P_802_2) and
2026 * with the IPX LSAP as the DSAP in the LLC
2029 * Ethernet_SNAP frames, which are 802.3
2030 * frames with an LLC header and a SNAP
2031 * header and with an OUI of 0x000000
2032 * (encapsulated Ethernet) and a protocol
2033 * ID of ETHERTYPE_IPX in the SNAP header.
2035 * First, do the checks on LINUX_SLL_P_802_2
2036 * frames; generate the check for either
2037 * Ethernet_802.2 or Ethernet_SNAP frames, and
2038 * then put a check for LINUX_SLL_P_802_2 frames
2041 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2042 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2044 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2048 * Now check for 802.3 frames and OR that with
2049 * the previous test.
2051 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2055 * Now add the check for Ethernet_II frames, and
2056 * do that before checking for the other frame
2059 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2060 (bpf_int32
)ETHERTYPE_IPX
);
2064 case ETHERTYPE_ATALK
:
2065 case ETHERTYPE_AARP
:
2067 * EtherTalk (AppleTalk protocols on Ethernet link
2068 * layer) may use 802.2 encapsulation.
2072 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2073 * we check for the 802.2 protocol type in the
2074 * "Ethernet type" field.
2076 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2079 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2080 * SNAP packets with an organization code of
2081 * 0x080007 (Apple, for Appletalk) and a protocol
2082 * type of ETHERTYPE_ATALK (Appletalk).
2084 * 802.2-encapsulated ETHERTYPE_AARP packets are
2085 * SNAP packets with an organization code of
2086 * 0x000000 (encapsulated Ethernet) and a protocol
2087 * type of ETHERTYPE_AARP (Appletalk ARP).
2089 if (proto
== ETHERTYPE_ATALK
)
2090 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2091 else /* proto == ETHERTYPE_AARP */
2092 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2096 * Check for Ethernet encapsulation (Ethertalk
2097 * phase 1?); we just check for the Ethernet
2100 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2106 if (proto
<= ETHERMTU
) {
2108 * This is an LLC SAP value, so the frames
2109 * that match would be 802.2 frames.
2110 * Check for the 802.2 protocol type
2111 * in the "Ethernet type" field, and
2112 * then check the DSAP.
2114 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2116 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2122 * This is an Ethernet type, so compare
2123 * the length/type field with it (if
2124 * the frame is an 802.2 frame, the length
2125 * field will be <= ETHERMTU, and, as
2126 * "proto" is > ETHERMTU, this test
2127 * will fail and the frame won't match,
2128 * which is what we want).
2130 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2136 static struct slist
*
2137 gen_load_radiotap_llprefixlen()
2139 struct slist
*s1
, *s2
;
2142 * Generate code to load the length of the radiotap header into
2143 * the register assigned to hold that length, if one has been
2144 * assigned. (If one hasn't been assigned, no code we've
2145 * generated uses that prefix, so we don't need to generate any
2148 if (reg_off_ll
!= -1) {
2150 * The 2 bytes at offsets of 2 and 3 from the beginning
2151 * of the radiotap header are the length of the radiotap
2152 * header; unfortunately, it's little-endian, so we have
2153 * to load it a byte at a time and construct the value.
2157 * Load the high-order byte, at an offset of 3, shift it
2158 * left a byte, and put the result in the X register.
2160 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2162 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2165 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2169 * Load the next byte, at an offset of 2, and OR the
2170 * value from the X register into it.
2172 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2175 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2179 * Now allocate a register to hold that value and store
2182 s2
= new_stmt(BPF_ST
);
2183 s2
->s
.k
= reg_off_ll
;
2187 * Now move it into the X register.
2189 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2198 * At the moment we treat PPI as normal Radiotap encoded
2199 * packets. The difference is in the function that generates
2200 * the code at the beginning to compute the header length.
2201 * Since this code generator of PPI supports bare 802.11
2202 * encapsulation only (i.e. the encapsulated DLT should be
2203 * DLT_IEEE802_11) we generate code to check for this too;
2204 * that's done in finish_parse().
2206 static struct slist
*
2207 gen_load_ppi_llprefixlen()
2209 struct slist
*s1
, *s2
;
2212 * Generate code to load the length of the radiotap header
2213 * into the register assigned to hold that length, if one has
2216 if (reg_off_ll
!= -1) {
2218 * The 2 bytes at offsets of 2 and 3 from the beginning
2219 * of the radiotap header are the length of the radiotap
2220 * header; unfortunately, it's little-endian, so we have
2221 * to load it a byte at a time and construct the value.
2225 * Load the high-order byte, at an offset of 3, shift it
2226 * left a byte, and put the result in the X register.
2228 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2230 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2233 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2237 * Load the next byte, at an offset of 2, and OR the
2238 * value from the X register into it.
2240 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2243 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2247 * Now allocate a register to hold that value and store
2250 s2
= new_stmt(BPF_ST
);
2251 s2
->s
.k
= reg_off_ll
;
2255 * Now move it into the X register.
2257 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2266 * Load a value relative to the beginning of the link-layer header after the 802.11
2267 * header, i.e. LLC_SNAP.
2268 * The link-layer header doesn't necessarily begin at the beginning
2269 * of the packet data; there might be a variable-length prefix containing
2270 * radio information.
2272 static struct slist
*
2273 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2276 struct slist
*sjset_data_frame_1
;
2277 struct slist
*sjset_data_frame_2
;
2278 struct slist
*sjset_qos
;
2279 struct slist
*sjset_radiotap_flags
;
2280 struct slist
*sjset_radiotap_tsft
;
2281 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2282 struct slist
*s_roundup
;
2284 if (reg_off_macpl
== -1) {
2286 * No register has been assigned to the offset of
2287 * the MAC-layer payload, which means nobody needs
2288 * it; don't bother computing it - just return
2289 * what we already have.
2295 * This code is not compatible with the optimizer, as
2296 * we are generating jmp instructions within a normal
2297 * slist of instructions
2302 * If "s" is non-null, it has code to arrange that the X register
2303 * contains the length of the prefix preceding the link-layer
2306 * Otherwise, the length of the prefix preceding the link-layer
2307 * header is "off_ll".
2311 * There is no variable-length header preceding the
2312 * link-layer header.
2314 * Load the length of the fixed-length prefix preceding
2315 * the link-layer header (if any) into the X register,
2316 * and store it in the reg_off_macpl register.
2317 * That length is off_ll.
2319 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2324 * The X register contains the offset of the beginning of the
2325 * link-layer header; add 24, which is the minimum length
2326 * of the MAC header for a data frame, to that, and store it
2327 * in reg_off_macpl, and then load the Frame Control field,
2328 * which is at the offset in the X register, with an indexed load.
2330 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2332 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2335 s2
= new_stmt(BPF_ST
);
2336 s2
->s
.k
= reg_off_macpl
;
2339 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2344 * Check the Frame Control field to see if this is a data frame;
2345 * a data frame has the 0x08 bit (b3) in that field set and the
2346 * 0x04 bit (b2) clear.
2348 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2349 sjset_data_frame_1
->s
.k
= 0x08;
2350 sappend(s
, sjset_data_frame_1
);
2353 * If b3 is set, test b2, otherwise go to the first statement of
2354 * the rest of the program.
2356 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2357 sjset_data_frame_2
->s
.k
= 0x04;
2358 sappend(s
, sjset_data_frame_2
);
2359 sjset_data_frame_1
->s
.jf
= snext
;
2362 * If b2 is not set, this is a data frame; test the QoS bit.
2363 * Otherwise, go to the first statement of the rest of the
2366 sjset_data_frame_2
->s
.jt
= snext
;
2367 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2368 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2369 sappend(s
, sjset_qos
);
2372 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2374 * Otherwise, go to the first statement of the rest of the
2377 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2378 s2
->s
.k
= reg_off_macpl
;
2380 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2383 s2
= new_stmt(BPF_ST
);
2384 s2
->s
.k
= reg_off_macpl
;
2388 * If we have a radiotap header, look at it to see whether
2389 * there's Atheros padding between the MAC-layer header
2392 * Note: all of the fields in the radiotap header are
2393 * little-endian, so we byte-swap all of the values
2394 * we test against, as they will be loaded as big-endian
2397 if (linktype
== DLT_IEEE802_11_RADIO
) {
2399 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2400 * in the presence flag?
2402 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2406 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2407 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2408 sappend(s
, sjset_radiotap_flags
);
2411 * If not, skip all of this.
2413 sjset_radiotap_flags
->s
.jf
= snext
;
2416 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2418 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2419 new_stmt(JMP(BPF_JSET
));
2420 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2421 sappend(s
, sjset_radiotap_tsft
);
2424 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2425 * at an offset of 16 from the beginning of the raw packet
2426 * data (8 bytes for the radiotap header and 8 bytes for
2429 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2432 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2436 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2437 sjset_tsft_datapad
->s
.k
= 0x20;
2438 sappend(s
, sjset_tsft_datapad
);
2441 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2442 * at an offset of 8 from the beginning of the raw packet
2443 * data (8 bytes for the radiotap header).
2445 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2448 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2452 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2453 sjset_notsft_datapad
->s
.k
= 0x20;
2454 sappend(s
, sjset_notsft_datapad
);
2457 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2458 * set, round the length of the 802.11 header to
2459 * a multiple of 4. Do that by adding 3 and then
2460 * dividing by and multiplying by 4, which we do by
2463 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2464 s_roundup
->s
.k
= reg_off_macpl
;
2465 sappend(s
, s_roundup
);
2466 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2469 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2472 s2
= new_stmt(BPF_ST
);
2473 s2
->s
.k
= reg_off_macpl
;
2476 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2477 sjset_tsft_datapad
->s
.jf
= snext
;
2478 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2479 sjset_notsft_datapad
->s
.jf
= snext
;
2481 sjset_qos
->s
.jf
= snext
;
2487 insert_compute_vloffsets(b
)
2493 * For link-layer types that have a variable-length header
2494 * preceding the link-layer header, generate code to load
2495 * the offset of the link-layer header into the register
2496 * assigned to that offset, if any.
2500 case DLT_IEEE802_11_RADIO
:
2501 s
= gen_load_radiotap_llprefixlen();
2505 s
= gen_load_ppi_llprefixlen();
2514 * For link-layer types that have a variable-length link-layer
2515 * header, generate code to load the offset of the MAC-layer
2516 * payload into the register assigned to that offset, if any.
2520 case DLT_IEEE802_11
:
2521 case DLT_IEEE802_11_RADIO
:
2522 case DLT_IEEE802_11_RADIO_AVS
:
2523 case DLT_PRISM_HEADER
:
2525 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2530 * If we have any offset-loading code, append all the
2531 * existing statements in the block to those statements,
2532 * and make the resulting list the list of statements
2536 sappend(s
, b
->stmts
);
2541 static struct block
*
2542 gen_ppi_dlt_check(void)
2544 struct slist
*s_load_dlt
;
2547 if (linktype
== DLT_PPI
)
2549 /* Create the statements that check for the DLT
2551 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2552 s_load_dlt
->s
.k
= 4;
2554 b
= new_block(JMP(BPF_JEQ
));
2556 b
->stmts
= s_load_dlt
;
2557 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2567 static struct slist
*
2568 gen_radiotap_llprefixlen(void)
2572 if (reg_off_ll
== -1) {
2574 * We haven't yet assigned a register for the length
2575 * of the radiotap header; allocate one.
2577 reg_off_ll
= alloc_reg();
2581 * Load the register containing the radiotap length
2582 * into the X register.
2584 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2585 s
->s
.k
= reg_off_ll
;
2590 * At the moment we treat PPI as normal Radiotap encoded
2591 * packets. The difference is in the function that generates
2592 * the code at the beginning to compute the header length.
2593 * Since this code generator of PPI supports bare 802.11
2594 * encapsulation only (i.e. the encapsulated DLT should be
2595 * DLT_IEEE802_11) we generate code to check for this too.
2597 static struct slist
*
2598 gen_ppi_llprefixlen(void)
2602 if (reg_off_ll
== -1) {
2604 * We haven't yet assigned a register for the length
2605 * of the radiotap header; allocate one.
2607 reg_off_ll
= alloc_reg();
2611 * Load the register containing the radiotap length
2612 * into the X register.
2614 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2615 s
->s
.k
= reg_off_ll
;
2620 * Generate code to compute the link-layer header length, if necessary,
2621 * putting it into the X register, and to return either a pointer to a
2622 * "struct slist" for the list of statements in that code, or NULL if
2623 * no code is necessary.
2625 static struct slist
*
2626 gen_llprefixlen(void)
2631 return gen_ppi_llprefixlen();
2633 case DLT_IEEE802_11_RADIO
:
2634 return gen_radiotap_llprefixlen();
2642 * Generate code to load the register containing the offset of the
2643 * MAC-layer payload into the X register; if no register for that offset
2644 * has been allocated, allocate it first.
2646 static struct slist
*
2651 if (off_macpl_is_variable
) {
2652 if (reg_off_macpl
== -1) {
2654 * We haven't yet assigned a register for the offset
2655 * of the MAC-layer payload; allocate one.
2657 reg_off_macpl
= alloc_reg();
2661 * Load the register containing the offset of the MAC-layer
2662 * payload into the X register.
2664 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2665 s
->s
.k
= reg_off_macpl
;
2669 * That offset isn't variable, so we don't need to
2670 * generate any code.
2677 * Map an Ethernet type to the equivalent PPP type.
2680 ethertype_to_ppptype(proto
)
2690 case ETHERTYPE_IPV6
:
2699 case ETHERTYPE_ATALK
:
2713 * I'm assuming the "Bridging PDU"s that go
2714 * over PPP are Spanning Tree Protocol
2728 * Generate code to match a particular packet type by matching the
2729 * link-layer type field or fields in the 802.2 LLC header.
2731 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2732 * value, if <= ETHERMTU.
2734 static struct block
*
2738 struct block
*b0
, *b1
, *b2
;
2740 /* are we checking MPLS-encapsulated packets? */
2741 if (label_stack_depth
> 0) {
2745 /* FIXME add other L3 proto IDs */
2746 return gen_mpls_linktype(Q_IP
);
2748 case ETHERTYPE_IPV6
:
2750 /* FIXME add other L3 proto IDs */
2751 return gen_mpls_linktype(Q_IPV6
);
2754 bpf_error("unsupported protocol over mpls");
2760 * Are we testing PPPoE packets?
2764 * The PPPoE session header is part of the
2765 * MAC-layer payload, so all references
2766 * should be relative to the beginning of
2771 * We use Ethernet protocol types inside libpcap;
2772 * map them to the corresponding PPP protocol types.
2774 proto
= ethertype_to_ppptype(proto
);
2775 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2781 return gen_ether_linktype(proto
);
2789 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2793 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2801 case DLT_IEEE802_11
:
2802 case DLT_IEEE802_11_RADIO
:
2803 case DLT_IEEE802_11_RADIO_AVS
:
2804 case DLT_PRISM_HEADER
:
2806 * Check that we have a data frame.
2808 b0
= gen_check_802_11_data_frame();
2811 * Now check for the specified link-layer type.
2813 b1
= gen_llc_linktype(proto
);
2821 * XXX - check for asynchronous frames, as per RFC 1103.
2823 return gen_llc_linktype(proto
);
2829 * XXX - check for LLC PDUs, as per IEEE 802.5.
2831 return gen_llc_linktype(proto
);
2835 case DLT_ATM_RFC1483
:
2837 case DLT_IP_OVER_FC
:
2838 return gen_llc_linktype(proto
);
2844 * If "is_lane" is set, check for a LANE-encapsulated
2845 * version of this protocol, otherwise check for an
2846 * LLC-encapsulated version of this protocol.
2848 * We assume LANE means Ethernet, not Token Ring.
2852 * Check that the packet doesn't begin with an
2853 * LE Control marker. (We've already generated
2856 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2861 * Now generate an Ethernet test.
2863 b1
= gen_ether_linktype(proto
);
2868 * Check for LLC encapsulation and then check the
2871 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2872 b1
= gen_llc_linktype(proto
);
2880 return gen_linux_sll_linktype(proto
);
2885 case DLT_SLIP_BSDOS
:
2888 * These types don't provide any type field; packets
2889 * are always IPv4 or IPv6.
2891 * XXX - for IPv4, check for a version number of 4, and,
2892 * for IPv6, check for a version number of 6?
2897 /* Check for a version number of 4. */
2898 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2900 case ETHERTYPE_IPV6
:
2901 /* Check for a version number of 6. */
2902 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2906 return gen_false(); /* always false */
2913 case DLT_PPP_SERIAL
:
2916 * We use Ethernet protocol types inside libpcap;
2917 * map them to the corresponding PPP protocol types.
2919 proto
= ethertype_to_ppptype(proto
);
2920 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2926 * We use Ethernet protocol types inside libpcap;
2927 * map them to the corresponding PPP protocol types.
2933 * Also check for Van Jacobson-compressed IP.
2934 * XXX - do this for other forms of PPP?
2936 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2937 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2939 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2944 proto
= ethertype_to_ppptype(proto
);
2945 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2955 * For DLT_NULL, the link-layer header is a 32-bit
2956 * word containing an AF_ value in *host* byte order,
2957 * and for DLT_ENC, the link-layer header begins
2958 * with a 32-bit work containing an AF_ value in
2961 * In addition, if we're reading a saved capture file,
2962 * the host byte order in the capture may not be the
2963 * same as the host byte order on this machine.
2965 * For DLT_LOOP, the link-layer header is a 32-bit
2966 * word containing an AF_ value in *network* byte order.
2968 * XXX - AF_ values may, unfortunately, be platform-
2969 * dependent; for example, FreeBSD's AF_INET6 is 24
2970 * whilst NetBSD's and OpenBSD's is 26.
2972 * This means that, when reading a capture file, just
2973 * checking for our AF_INET6 value won't work if the
2974 * capture file came from another OS.
2983 case ETHERTYPE_IPV6
:
2990 * Not a type on which we support filtering.
2991 * XXX - support those that have AF_ values
2992 * #defined on this platform, at least?
2997 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2999 * The AF_ value is in host byte order, but
3000 * the BPF interpreter will convert it to
3001 * network byte order.
3003 * If this is a save file, and it's from a
3004 * machine with the opposite byte order to
3005 * ours, we byte-swap the AF_ value.
3007 * Then we run it through "htonl()", and
3008 * generate code to compare against the result.
3010 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3011 bpf_pcap
->sf
.swapped
)
3012 proto
= SWAPLONG(proto
);
3013 proto
= htonl(proto
);
3015 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3017 #ifdef HAVE_NET_PFVAR_H
3020 * af field is host byte order in contrast to the rest of
3023 if (proto
== ETHERTYPE_IP
)
3024 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3025 BPF_B
, (bpf_int32
)AF_INET
));
3027 else if (proto
== ETHERTYPE_IPV6
)
3028 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3029 BPF_B
, (bpf_int32
)AF_INET6
));
3035 #endif /* HAVE_NET_PFVAR_H */
3038 case DLT_ARCNET_LINUX
:
3040 * XXX should we check for first fragment if the protocol
3049 case ETHERTYPE_IPV6
:
3050 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3051 (bpf_int32
)ARCTYPE_INET6
));
3055 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3056 (bpf_int32
)ARCTYPE_IP
);
3057 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3058 (bpf_int32
)ARCTYPE_IP_OLD
);
3063 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3064 (bpf_int32
)ARCTYPE_ARP
);
3065 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3066 (bpf_int32
)ARCTYPE_ARP_OLD
);
3070 case ETHERTYPE_REVARP
:
3071 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3072 (bpf_int32
)ARCTYPE_REVARP
));
3074 case ETHERTYPE_ATALK
:
3075 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3076 (bpf_int32
)ARCTYPE_ATALK
));
3083 case ETHERTYPE_ATALK
:
3093 * XXX - assumes a 2-byte Frame Relay header with
3094 * DLCI and flags. What if the address is longer?
3100 * Check for the special NLPID for IP.
3102 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3105 case ETHERTYPE_IPV6
:
3107 * Check for the special NLPID for IPv6.
3109 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3114 * Check for several OSI protocols.
3116 * Frame Relay packets typically have an OSI
3117 * NLPID at the beginning; we check for each
3120 * What we check for is the NLPID and a frame
3121 * control field of UI, i.e. 0x03 followed
3124 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3125 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3126 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3138 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3140 case DLT_JUNIPER_MFR
:
3141 case DLT_JUNIPER_MLFR
:
3142 case DLT_JUNIPER_MLPPP
:
3143 case DLT_JUNIPER_ATM1
:
3144 case DLT_JUNIPER_ATM2
:
3145 case DLT_JUNIPER_PPPOE
:
3146 case DLT_JUNIPER_PPPOE_ATM
:
3147 case DLT_JUNIPER_GGSN
:
3148 case DLT_JUNIPER_ES
:
3149 case DLT_JUNIPER_MONITOR
:
3150 case DLT_JUNIPER_SERVICES
:
3151 case DLT_JUNIPER_ETHER
:
3152 case DLT_JUNIPER_PPP
:
3153 case DLT_JUNIPER_FRELAY
:
3154 case DLT_JUNIPER_CHDLC
:
3155 case DLT_JUNIPER_VP
:
3156 case DLT_JUNIPER_ST
:
3157 case DLT_JUNIPER_ISM
:
3158 /* just lets verify the magic number for now -
3159 * on ATM we may have up to 6 different encapsulations on the wire
3160 * and need a lot of heuristics to figure out that the payload
3163 * FIXME encapsulation specific BPF_ filters
3165 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3167 case DLT_LINUX_IRDA
:
3168 bpf_error("IrDA link-layer type filtering not implemented");
3171 bpf_error("DOCSIS link-layer type filtering not implemented");
3174 case DLT_MTP2_WITH_PHDR
:
3175 bpf_error("MTP2 link-layer type filtering not implemented");
3178 bpf_error("ERF link-layer type filtering not implemented");
3182 bpf_error("PFSYNC link-layer type filtering not implemented");
3185 case DLT_LINUX_LAPD
:
3186 bpf_error("LAPD link-layer type filtering not implemented");
3190 bpf_error("USB link-layer type filtering not implemented");
3192 case DLT_BLUETOOTH_HCI_H4
:
3193 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3194 bpf_error("Bluetooth link-layer type filtering not implemented");
3197 bpf_error("CAN20B link-layer type filtering not implemented");
3199 case DLT_IEEE802_15_4
:
3200 case DLT_IEEE802_15_4_LINUX
:
3201 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3203 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3204 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3207 bpf_error("SITA link-layer type filtering not implemented");
3210 bpf_error("RAIF1 link-layer type filtering not implemented");
3213 bpf_error("IPMB link-layer type filtering not implemented");
3216 bpf_error("AX.25 link-layer type filtering not implemented");
3220 * All the types that have no encapsulation should either be
3221 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3222 * all packets are IP packets, or should be handled in some
3223 * special case, if none of them are (if some are and some
3224 * aren't, the lack of encapsulation is a problem, as we'd
3225 * have to find some other way of determining the packet type).
3227 * Therefore, if "off_linktype" is -1, there's an error.
3229 if (off_linktype
== (u_int
)-1)
3233 * Any type not handled above should always have an Ethernet
3234 * type at an offset of "off_linktype".
3236 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3240 * Check for an LLC SNAP packet with a given organization code and
3241 * protocol type; we check the entire contents of the 802.2 LLC and
3242 * snap headers, checking for DSAP and SSAP of SNAP and a control
3243 * field of 0x03 in the LLC header, and for the specified organization
3244 * code and protocol type in the SNAP header.
3246 static struct block
*
3247 gen_snap(orgcode
, ptype
)
3248 bpf_u_int32 orgcode
;
3251 u_char snapblock
[8];
3253 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3254 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3255 snapblock
[2] = 0x03; /* control = UI */
3256 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3257 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3258 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3259 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3260 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3261 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3265 * Generate code to match a particular packet type, for link-layer types
3266 * using 802.2 LLC headers.
3268 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3269 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3271 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3272 * value, if <= ETHERMTU. We use that to determine whether to
3273 * match the DSAP or both DSAP and LSAP or to check the OUI and
3274 * protocol ID in a SNAP header.
3276 static struct block
*
3277 gen_llc_linktype(proto
)
3281 * XXX - handle token-ring variable-length header.
3287 case LLCSAP_NETBEUI
:
3289 * XXX - should we check both the DSAP and the
3290 * SSAP, like this, or should we check just the
3291 * DSAP, as we do for other types <= ETHERMTU
3292 * (i.e., other SAP values)?
3294 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3295 ((proto
<< 8) | proto
));
3299 * XXX - are there ever SNAP frames for IPX on
3300 * non-Ethernet 802.x networks?
3302 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3303 (bpf_int32
)LLCSAP_IPX
);
3305 case ETHERTYPE_ATALK
:
3307 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3308 * SNAP packets with an organization code of
3309 * 0x080007 (Apple, for Appletalk) and a protocol
3310 * type of ETHERTYPE_ATALK (Appletalk).
3312 * XXX - check for an organization code of
3313 * encapsulated Ethernet as well?
3315 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3319 * XXX - we don't have to check for IPX 802.3
3320 * here, but should we check for the IPX Ethertype?
3322 if (proto
<= ETHERMTU
) {
3324 * This is an LLC SAP value, so check
3327 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3330 * This is an Ethernet type; we assume that it's
3331 * unlikely that it'll appear in the right place
3332 * at random, and therefore check only the
3333 * location that would hold the Ethernet type
3334 * in a SNAP frame with an organization code of
3335 * 0x000000 (encapsulated Ethernet).
3337 * XXX - if we were to check for the SNAP DSAP and
3338 * LSAP, as per XXX, and were also to check for an
3339 * organization code of 0x000000 (encapsulated
3340 * Ethernet), we'd do
3342 * return gen_snap(0x000000, proto);
3344 * here; for now, we don't, as per the above.
3345 * I don't know whether it's worth the extra CPU
3346 * time to do the right check or not.
3348 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3353 static struct block
*
3354 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3358 u_int src_off
, dst_off
;
3360 struct block
*b0
, *b1
;
3374 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3375 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3381 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3382 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3389 b0
= gen_linktype(proto
);
3390 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3396 static struct block
*
3397 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3398 struct in6_addr
*addr
;
3399 struct in6_addr
*mask
;
3401 u_int src_off
, dst_off
;
3403 struct block
*b0
, *b1
;
3418 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3419 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3425 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3426 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3433 /* this order is important */
3434 a
= (u_int32_t
*)addr
;
3435 m
= (u_int32_t
*)mask
;
3436 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3437 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3439 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3441 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3443 b0
= gen_linktype(proto
);
3449 static struct block
*
3450 gen_ehostop(eaddr
, dir
)
3451 register const u_char
*eaddr
;
3454 register struct block
*b0
, *b1
;
3458 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3461 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3464 b0
= gen_ehostop(eaddr
, Q_SRC
);
3465 b1
= gen_ehostop(eaddr
, Q_DST
);
3471 b0
= gen_ehostop(eaddr
, Q_SRC
);
3472 b1
= gen_ehostop(eaddr
, Q_DST
);
3481 * Like gen_ehostop, but for DLT_FDDI
3483 static struct block
*
3484 gen_fhostop(eaddr
, dir
)
3485 register const u_char
*eaddr
;
3488 struct block
*b0
, *b1
;
3493 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3495 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3500 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3502 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3506 b0
= gen_fhostop(eaddr
, Q_SRC
);
3507 b1
= gen_fhostop(eaddr
, Q_DST
);
3513 b0
= gen_fhostop(eaddr
, Q_SRC
);
3514 b1
= gen_fhostop(eaddr
, Q_DST
);
3523 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3525 static struct block
*
3526 gen_thostop(eaddr
, dir
)
3527 register const u_char
*eaddr
;
3530 register struct block
*b0
, *b1
;
3534 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3537 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3540 b0
= gen_thostop(eaddr
, Q_SRC
);
3541 b1
= gen_thostop(eaddr
, Q_DST
);
3547 b0
= gen_thostop(eaddr
, Q_SRC
);
3548 b1
= gen_thostop(eaddr
, Q_DST
);
3557 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3558 * various 802.11 + radio headers.
3560 static struct block
*
3561 gen_wlanhostop(eaddr
, dir
)
3562 register const u_char
*eaddr
;
3565 register struct block
*b0
, *b1
, *b2
;
3566 register struct slist
*s
;
3568 #ifdef ENABLE_WLAN_FILTERING_PATCH
3571 * We need to disable the optimizer because the optimizer is buggy
3572 * and wipes out some LD instructions generated by the below
3573 * code to validate the Frame Control bits
3576 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3583 * For control frames, there is no SA.
3585 * For management frames, SA is at an
3586 * offset of 10 from the beginning of
3589 * For data frames, SA is at an offset
3590 * of 10 from the beginning of the packet
3591 * if From DS is clear, at an offset of
3592 * 16 from the beginning of the packet
3593 * if From DS is set and To DS is clear,
3594 * and an offset of 24 from the beginning
3595 * of the packet if From DS is set and To DS
3600 * Generate the tests to be done for data frames
3603 * First, check for To DS set, i.e. check "link[1] & 0x01".
3605 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3606 b1
= new_block(JMP(BPF_JSET
));
3607 b1
->s
.k
= 0x01; /* To DS */
3611 * If To DS is set, the SA is at 24.
3613 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3617 * Now, check for To DS not set, i.e. check
3618 * "!(link[1] & 0x01)".
3620 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3621 b2
= new_block(JMP(BPF_JSET
));
3622 b2
->s
.k
= 0x01; /* To DS */
3627 * If To DS is not set, the SA is at 16.
3629 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3633 * Now OR together the last two checks. That gives
3634 * the complete set of checks for data frames with
3640 * Now check for From DS being set, and AND that with
3641 * the ORed-together checks.
3643 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3644 b1
= new_block(JMP(BPF_JSET
));
3645 b1
->s
.k
= 0x02; /* From DS */
3650 * Now check for data frames with From DS not set.
3652 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3653 b2
= new_block(JMP(BPF_JSET
));
3654 b2
->s
.k
= 0x02; /* From DS */
3659 * If From DS isn't set, the SA is at 10.
3661 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3665 * Now OR together the checks for data frames with
3666 * From DS not set and for data frames with From DS
3667 * set; that gives the checks done for data frames.
3672 * Now check for a data frame.
3673 * I.e, check "link[0] & 0x08".
3675 gen_load_a(OR_LINK
, 0, BPF_B
);
3676 b1
= new_block(JMP(BPF_JSET
));
3681 * AND that with the checks done for data frames.
3686 * If the high-order bit of the type value is 0, this
3687 * is a management frame.
3688 * I.e, check "!(link[0] & 0x08)".
3690 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3691 b2
= new_block(JMP(BPF_JSET
));
3697 * For management frames, the SA is at 10.
3699 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3703 * OR that with the checks done for data frames.
3704 * That gives the checks done for management and
3710 * If the low-order bit of the type value is 1,
3711 * this is either a control frame or a frame
3712 * with a reserved type, and thus not a
3715 * I.e., check "!(link[0] & 0x04)".
3717 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3718 b1
= new_block(JMP(BPF_JSET
));
3724 * AND that with the checks for data and management
3734 * For control frames, there is no DA.
3736 * For management frames, DA is at an
3737 * offset of 4 from the beginning of
3740 * For data frames, DA is at an offset
3741 * of 4 from the beginning of the packet
3742 * if To DS is clear and at an offset of
3743 * 16 from the beginning of the packet
3748 * Generate the tests to be done for data frames.
3750 * First, check for To DS set, i.e. "link[1] & 0x01".
3752 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3753 b1
= new_block(JMP(BPF_JSET
));
3754 b1
->s
.k
= 0x01; /* To DS */
3758 * If To DS is set, the DA is at 16.
3760 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3764 * Now, check for To DS not set, i.e. check
3765 * "!(link[1] & 0x01)".
3767 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3768 b2
= new_block(JMP(BPF_JSET
));
3769 b2
->s
.k
= 0x01; /* To DS */
3774 * If To DS is not set, the DA is at 4.
3776 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3780 * Now OR together the last two checks. That gives
3781 * the complete set of checks for data frames.
3786 * Now check for a data frame.
3787 * I.e, check "link[0] & 0x08".
3789 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3790 b1
= new_block(JMP(BPF_JSET
));
3795 * AND that with the checks done for data frames.
3800 * If the high-order bit of the type value is 0, this
3801 * is a management frame.
3802 * I.e, check "!(link[0] & 0x08)".
3804 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3805 b2
= new_block(JMP(BPF_JSET
));
3811 * For management frames, the DA is at 4.
3813 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3817 * OR that with the checks done for data frames.
3818 * That gives the checks done for management and
3824 * If the low-order bit of the type value is 1,
3825 * this is either a control frame or a frame
3826 * with a reserved type, and thus not a
3829 * I.e., check "!(link[0] & 0x04)".
3831 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3832 b1
= new_block(JMP(BPF_JSET
));
3838 * AND that with the checks for data and management
3845 * XXX - add RA, TA, and BSSID keywords?
3848 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
3852 * Not present in CTS or ACK control frames.
3854 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
3855 IEEE80211_FC0_TYPE_MASK
);
3857 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
3858 IEEE80211_FC0_SUBTYPE_MASK
);
3860 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
3861 IEEE80211_FC0_SUBTYPE_MASK
);
3865 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3871 * Not present in control frames.
3873 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
3874 IEEE80211_FC0_TYPE_MASK
);
3876 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3882 * Present only if the direction mask has both "From DS"
3883 * and "To DS" set. Neither control frames nor management
3884 * frames should have both of those set, so we don't
3885 * check the frame type.
3887 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
3888 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
3889 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3894 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3895 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3901 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3902 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3911 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3912 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3913 * as the RFC states.)
3915 static struct block
*
3916 gen_ipfchostop(eaddr
, dir
)
3917 register const u_char
*eaddr
;
3920 register struct block
*b0
, *b1
;
3924 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3927 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3930 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3931 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3937 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3938 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3947 * This is quite tricky because there may be pad bytes in front of the
3948 * DECNET header, and then there are two possible data packet formats that
3949 * carry both src and dst addresses, plus 5 packet types in a format that
3950 * carries only the src node, plus 2 types that use a different format and
3951 * also carry just the src node.
3955 * Instead of doing those all right, we just look for data packets with
3956 * 0 or 1 bytes of padding. If you want to look at other packets, that
3957 * will require a lot more hacking.
3959 * To add support for filtering on DECNET "areas" (network numbers)
3960 * one would want to add a "mask" argument to this routine. That would
3961 * make the filter even more inefficient, although one could be clever
3962 * and not generate masking instructions if the mask is 0xFFFF.
3964 static struct block
*
3965 gen_dnhostop(addr
, dir
)
3969 struct block
*b0
, *b1
, *b2
, *tmp
;
3970 u_int offset_lh
; /* offset if long header is received */
3971 u_int offset_sh
; /* offset if short header is received */
3976 offset_sh
= 1; /* follows flags */
3977 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3981 offset_sh
= 3; /* follows flags, dstnode */
3982 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3986 /* Inefficient because we do our Calvinball dance twice */
3987 b0
= gen_dnhostop(addr
, Q_SRC
);
3988 b1
= gen_dnhostop(addr
, Q_DST
);
3994 /* Inefficient because we do our Calvinball dance twice */
3995 b0
= gen_dnhostop(addr
, Q_SRC
);
3996 b1
= gen_dnhostop(addr
, Q_DST
);
4001 bpf_error("ISO host filtering not implemented");
4006 b0
= gen_linktype(ETHERTYPE_DN
);
4007 /* Check for pad = 1, long header case */
4008 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4009 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4010 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4011 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4013 /* Check for pad = 0, long header case */
4014 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4015 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4018 /* Check for pad = 1, short header case */
4019 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4020 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4021 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4024 /* Check for pad = 0, short header case */
4025 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4026 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4030 /* Combine with test for linktype */
4036 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4037 * test the bottom-of-stack bit, and then check the version number
4038 * field in the IP header.
4040 static struct block
*
4041 gen_mpls_linktype(proto
)
4044 struct block
*b0
, *b1
;
4049 /* match the bottom-of-stack bit */
4050 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4051 /* match the IPv4 version number */
4052 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4057 /* match the bottom-of-stack bit */
4058 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4059 /* match the IPv4 version number */
4060 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4069 static struct block
*
4070 gen_host(addr
, mask
, proto
, dir
, type
)
4077 struct block
*b0
, *b1
;
4078 const char *typestr
;
4088 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4090 * Only check for non-IPv4 addresses if we're not
4091 * checking MPLS-encapsulated packets.
4093 if (label_stack_depth
== 0) {
4094 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4096 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4102 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4105 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4108 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4111 bpf_error("'tcp' modifier applied to %s", typestr
);
4114 bpf_error("'sctp' modifier applied to %s", typestr
);
4117 bpf_error("'udp' modifier applied to %s", typestr
);
4120 bpf_error("'icmp' modifier applied to %s", typestr
);
4123 bpf_error("'igmp' modifier applied to %s", typestr
);
4126 bpf_error("'igrp' modifier applied to %s", typestr
);
4129 bpf_error("'pim' modifier applied to %s", typestr
);
4132 bpf_error("'vrrp' modifier applied to %s", typestr
);
4135 bpf_error("ATALK host filtering not implemented");
4138 bpf_error("AARP host filtering not implemented");
4141 return gen_dnhostop(addr
, dir
);
4144 bpf_error("SCA host filtering not implemented");
4147 bpf_error("LAT host filtering not implemented");
4150 bpf_error("MOPDL host filtering not implemented");
4153 bpf_error("MOPRC host filtering not implemented");
4157 bpf_error("'ip6' modifier applied to ip host");
4160 bpf_error("'icmp6' modifier applied to %s", typestr
);
4164 bpf_error("'ah' modifier applied to %s", typestr
);
4167 bpf_error("'esp' modifier applied to %s", typestr
);
4170 bpf_error("ISO host filtering not implemented");
4173 bpf_error("'esis' modifier applied to %s", typestr
);
4176 bpf_error("'isis' modifier applied to %s", typestr
);
4179 bpf_error("'clnp' modifier applied to %s", typestr
);
4182 bpf_error("'stp' modifier applied to %s", typestr
);
4185 bpf_error("IPX host filtering not implemented");
4188 bpf_error("'netbeui' modifier applied to %s", typestr
);
4191 bpf_error("'radio' modifier applied to %s", typestr
);
4200 static struct block
*
4201 gen_host6(addr
, mask
, proto
, dir
, type
)
4202 struct in6_addr
*addr
;
4203 struct in6_addr
*mask
;
4208 const char *typestr
;
4218 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4221 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4224 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4227 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4230 bpf_error("'sctp' modifier applied to %s", typestr
);
4233 bpf_error("'tcp' modifier applied to %s", typestr
);
4236 bpf_error("'udp' modifier applied to %s", typestr
);
4239 bpf_error("'icmp' modifier applied to %s", typestr
);
4242 bpf_error("'igmp' modifier applied to %s", typestr
);
4245 bpf_error("'igrp' modifier applied to %s", typestr
);
4248 bpf_error("'pim' modifier applied to %s", typestr
);
4251 bpf_error("'vrrp' modifier applied to %s", typestr
);
4254 bpf_error("ATALK host filtering not implemented");
4257 bpf_error("AARP host filtering not implemented");
4260 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4263 bpf_error("SCA host filtering not implemented");
4266 bpf_error("LAT host filtering not implemented");
4269 bpf_error("MOPDL host filtering not implemented");
4272 bpf_error("MOPRC host filtering not implemented");
4275 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4278 bpf_error("'icmp6' modifier applied to %s", typestr
);
4281 bpf_error("'ah' modifier applied to %s", typestr
);
4284 bpf_error("'esp' modifier applied to %s", typestr
);
4287 bpf_error("ISO host filtering not implemented");
4290 bpf_error("'esis' modifier applied to %s", typestr
);
4293 bpf_error("'isis' modifier applied to %s", typestr
);
4296 bpf_error("'clnp' modifier applied to %s", typestr
);
4299 bpf_error("'stp' modifier applied to %s", typestr
);
4302 bpf_error("IPX host filtering not implemented");
4305 bpf_error("'netbeui' modifier applied to %s", typestr
);
4308 bpf_error("'radio' modifier applied to %s", typestr
);
4318 static struct block
*
4319 gen_gateway(eaddr
, alist
, proto
, dir
)
4320 const u_char
*eaddr
;
4321 bpf_u_int32
**alist
;
4325 struct block
*b0
, *b1
, *tmp
;
4328 bpf_error("direction applied to 'gateway'");
4337 b0
= gen_ehostop(eaddr
, Q_OR
);
4340 b0
= gen_fhostop(eaddr
, Q_OR
);
4343 b0
= gen_thostop(eaddr
, Q_OR
);
4345 case DLT_IEEE802_11
:
4346 case DLT_IEEE802_11_RADIO_AVS
:
4348 case DLT_IEEE802_11_RADIO
:
4349 case DLT_PRISM_HEADER
:
4350 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4355 * Check that the packet doesn't begin with an
4356 * LE Control marker. (We've already generated
4359 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4364 * Now check the MAC address.
4366 b0
= gen_ehostop(eaddr
, Q_OR
);
4370 case DLT_IP_OVER_FC
:
4371 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4375 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4377 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4379 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4388 bpf_error("illegal modifier of 'gateway'");
4394 gen_proto_abbrev(proto
)
4403 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4405 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4411 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4413 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4419 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4421 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4427 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4430 #ifndef IPPROTO_IGMP
4431 #define IPPROTO_IGMP 2
4435 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4438 #ifndef IPPROTO_IGRP
4439 #define IPPROTO_IGRP 9
4442 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4446 #define IPPROTO_PIM 103
4450 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4452 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4457 #ifndef IPPROTO_VRRP
4458 #define IPPROTO_VRRP 112
4462 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4466 b1
= gen_linktype(ETHERTYPE_IP
);
4470 b1
= gen_linktype(ETHERTYPE_ARP
);
4474 b1
= gen_linktype(ETHERTYPE_REVARP
);
4478 bpf_error("link layer applied in wrong context");
4481 b1
= gen_linktype(ETHERTYPE_ATALK
);
4485 b1
= gen_linktype(ETHERTYPE_AARP
);
4489 b1
= gen_linktype(ETHERTYPE_DN
);
4493 b1
= gen_linktype(ETHERTYPE_SCA
);
4497 b1
= gen_linktype(ETHERTYPE_LAT
);
4501 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4505 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4510 b1
= gen_linktype(ETHERTYPE_IPV6
);
4513 #ifndef IPPROTO_ICMPV6
4514 #define IPPROTO_ICMPV6 58
4517 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4522 #define IPPROTO_AH 51
4525 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4527 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4533 #define IPPROTO_ESP 50
4536 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4538 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4544 b1
= gen_linktype(LLCSAP_ISONS
);
4548 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4552 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4555 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4556 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4557 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4559 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4561 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4563 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4567 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4568 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4569 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4571 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4573 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4575 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4579 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4580 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4581 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4583 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4588 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4589 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4594 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4595 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4597 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4599 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4604 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4605 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4610 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4611 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4616 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4620 b1
= gen_linktype(LLCSAP_8021D
);
4624 b1
= gen_linktype(LLCSAP_IPX
);
4628 b1
= gen_linktype(LLCSAP_NETBEUI
);
4632 bpf_error("'radio' is not a valid protocol type");
4640 static struct block
*
4647 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4648 b
= new_block(JMP(BPF_JSET
));
4657 * Generate a comparison to a port value in the transport-layer header
4658 * at the specified offset from the beginning of that header.
4660 * XXX - this handles a variable-length prefix preceding the link-layer
4661 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4662 * variable-length link-layer headers (such as Token Ring or 802.11
4665 static struct block
*
4666 gen_portatom(off
, v
)
4670 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4674 static struct block
*
4675 gen_portatom6(off
, v
)
4679 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4684 gen_portop(port
, proto
, dir
)
4685 int port
, proto
, dir
;
4687 struct block
*b0
, *b1
, *tmp
;
4689 /* ip proto 'proto' */
4690 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4696 b1
= gen_portatom(0, (bpf_int32
)port
);
4700 b1
= gen_portatom(2, (bpf_int32
)port
);
4705 tmp
= gen_portatom(0, (bpf_int32
)port
);
4706 b1
= gen_portatom(2, (bpf_int32
)port
);
4711 tmp
= gen_portatom(0, (bpf_int32
)port
);
4712 b1
= gen_portatom(2, (bpf_int32
)port
);
4724 static struct block
*
4725 gen_port(port
, ip_proto
, dir
)
4730 struct block
*b0
, *b1
, *tmp
;
4735 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4736 * not LLC encapsulation with LLCSAP_IP.
4738 * For IEEE 802 networks - which includes 802.5 token ring
4739 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4740 * says that SNAP encapsulation is used, not LLC encapsulation
4743 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4744 * RFC 2225 say that SNAP encapsulation is used, not LLC
4745 * encapsulation with LLCSAP_IP.
4747 * So we always check for ETHERTYPE_IP.
4749 b0
= gen_linktype(ETHERTYPE_IP
);
4755 b1
= gen_portop(port
, ip_proto
, dir
);
4759 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4760 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4762 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4775 gen_portop6(port
, proto
, dir
)
4776 int port
, proto
, dir
;
4778 struct block
*b0
, *b1
, *tmp
;
4780 /* ip6 proto 'proto' */
4781 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4785 b1
= gen_portatom6(0, (bpf_int32
)port
);
4789 b1
= gen_portatom6(2, (bpf_int32
)port
);
4794 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4795 b1
= gen_portatom6(2, (bpf_int32
)port
);
4800 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4801 b1
= gen_portatom6(2, (bpf_int32
)port
);
4813 static struct block
*
4814 gen_port6(port
, ip_proto
, dir
)
4819 struct block
*b0
, *b1
, *tmp
;
4821 /* link proto ip6 */
4822 b0
= gen_linktype(ETHERTYPE_IPV6
);
4828 b1
= gen_portop6(port
, ip_proto
, dir
);
4832 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4833 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4835 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4847 /* gen_portrange code */
4848 static struct block
*
4849 gen_portrangeatom(off
, v1
, v2
)
4853 struct block
*b1
, *b2
;
4857 * Reverse the order of the ports, so v1 is the lower one.
4866 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4867 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4875 gen_portrangeop(port1
, port2
, proto
, dir
)
4880 struct block
*b0
, *b1
, *tmp
;
4882 /* ip proto 'proto' */
4883 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4889 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4893 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4898 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4899 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4904 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4905 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4917 static struct block
*
4918 gen_portrange(port1
, port2
, ip_proto
, dir
)
4923 struct block
*b0
, *b1
, *tmp
;
4926 b0
= gen_linktype(ETHERTYPE_IP
);
4932 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4936 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4937 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4939 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4951 static struct block
*
4952 gen_portrangeatom6(off
, v1
, v2
)
4956 struct block
*b1
, *b2
;
4960 * Reverse the order of the ports, so v1 is the lower one.
4969 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4970 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4978 gen_portrangeop6(port1
, port2
, proto
, dir
)
4983 struct block
*b0
, *b1
, *tmp
;
4985 /* ip6 proto 'proto' */
4986 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4990 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4994 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4999 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5000 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5005 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5006 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5018 static struct block
*
5019 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5024 struct block
*b0
, *b1
, *tmp
;
5026 /* link proto ip6 */
5027 b0
= gen_linktype(ETHERTYPE_IPV6
);
5033 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5037 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5038 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5040 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5053 lookup_proto(name
, proto
)
5054 register const char *name
;
5064 v
= pcap_nametoproto(name
);
5065 if (v
== PROTO_UNDEF
)
5066 bpf_error("unknown ip proto '%s'", name
);
5070 /* XXX should look up h/w protocol type based on linktype */
5071 v
= pcap_nametoeproto(name
);
5072 if (v
== PROTO_UNDEF
) {
5073 v
= pcap_nametollc(name
);
5074 if (v
== PROTO_UNDEF
)
5075 bpf_error("unknown ether proto '%s'", name
);
5080 if (strcmp(name
, "esis") == 0)
5082 else if (strcmp(name
, "isis") == 0)
5084 else if (strcmp(name
, "clnp") == 0)
5087 bpf_error("unknown osi proto '%s'", name
);
5107 static struct block
*
5108 gen_protochain(v
, proto
, dir
)
5113 #ifdef NO_PROTOCHAIN
5114 return gen_proto(v
, proto
, dir
);
5116 struct block
*b0
, *b
;
5117 struct slist
*s
[100];
5118 int fix2
, fix3
, fix4
, fix5
;
5119 int ahcheck
, again
, end
;
5121 int reg2
= alloc_reg();
5123 memset(s
, 0, sizeof(s
));
5124 fix2
= fix3
= fix4
= fix5
= 0;
5131 b0
= gen_protochain(v
, Q_IP
, dir
);
5132 b
= gen_protochain(v
, Q_IPV6
, dir
);
5136 bpf_error("bad protocol applied for 'protochain'");
5141 * We don't handle variable-length prefixes before the link-layer
5142 * header, or variable-length link-layer headers, here yet.
5143 * We might want to add BPF instructions to do the protochain
5144 * work, to simplify that and, on platforms that have a BPF
5145 * interpreter with the new instructions, let the filtering
5146 * be done in the kernel. (We already require a modified BPF
5147 * engine to do the protochain stuff, to support backward
5148 * branches, and backward branch support is unlikely to appear
5149 * in kernel BPF engines.)
5153 case DLT_IEEE802_11
:
5154 case DLT_IEEE802_11_RADIO
:
5155 case DLT_IEEE802_11_RADIO_AVS
:
5156 case DLT_PRISM_HEADER
:
5158 bpf_error("'protochain' not supported with 802.11");
5161 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5164 * s[0] is a dummy entry to protect other BPF insn from damage
5165 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5166 * hard to find interdependency made by jump table fixup.
5169 s
[i
] = new_stmt(0); /*dummy*/
5174 b0
= gen_linktype(ETHERTYPE_IP
);
5177 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5178 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5180 /* X = ip->ip_hl << 2 */
5181 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5182 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5187 b0
= gen_linktype(ETHERTYPE_IPV6
);
5189 /* A = ip6->ip_nxt */
5190 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5191 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5193 /* X = sizeof(struct ip6_hdr) */
5194 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5200 bpf_error("unsupported proto to gen_protochain");
5204 /* again: if (A == v) goto end; else fall through; */
5206 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5208 s
[i
]->s
.jt
= NULL
; /*later*/
5209 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5213 #ifndef IPPROTO_NONE
5214 #define IPPROTO_NONE 59
5216 /* if (A == IPPROTO_NONE) goto end */
5217 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5218 s
[i
]->s
.jt
= NULL
; /*later*/
5219 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5220 s
[i
]->s
.k
= IPPROTO_NONE
;
5221 s
[fix5
]->s
.jf
= s
[i
];
5226 if (proto
== Q_IPV6
) {
5227 int v6start
, v6end
, v6advance
, j
;
5230 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5231 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5232 s
[i
]->s
.jt
= NULL
; /*later*/
5233 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5234 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5235 s
[fix2
]->s
.jf
= s
[i
];
5237 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5238 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5239 s
[i
]->s
.jt
= NULL
; /*later*/
5240 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5241 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5243 /* if (A == IPPROTO_ROUTING) goto v6advance */
5244 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5245 s
[i
]->s
.jt
= NULL
; /*later*/
5246 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5247 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5249 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5250 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5251 s
[i
]->s
.jt
= NULL
; /*later*/
5252 s
[i
]->s
.jf
= NULL
; /*later*/
5253 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5264 * X = X + (P[X + 1] + 1) * 8;
5267 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5269 /* A = P[X + packet head] */
5270 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5271 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5274 s
[i
] = new_stmt(BPF_ST
);
5278 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5281 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5285 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5287 /* A = P[X + packet head]; */
5288 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5289 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5292 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5296 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5300 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5303 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5307 /* goto again; (must use BPF_JA for backward jump) */
5308 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5309 s
[i
]->s
.k
= again
- i
- 1;
5310 s
[i
- 1]->s
.jf
= s
[i
];
5314 for (j
= v6start
; j
<= v6end
; j
++)
5315 s
[j
]->s
.jt
= s
[v6advance
];
5320 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5322 s
[fix2
]->s
.jf
= s
[i
];
5328 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5329 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5330 s
[i
]->s
.jt
= NULL
; /*later*/
5331 s
[i
]->s
.jf
= NULL
; /*later*/
5332 s
[i
]->s
.k
= IPPROTO_AH
;
5334 s
[fix3
]->s
.jf
= s
[ahcheck
];
5341 * X = X + (P[X + 1] + 2) * 4;
5344 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5346 /* A = P[X + packet head]; */
5347 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5348 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5351 s
[i
] = new_stmt(BPF_ST
);
5355 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5358 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5362 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5364 /* A = P[X + packet head] */
5365 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5366 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5369 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5373 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5377 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5380 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5384 /* goto again; (must use BPF_JA for backward jump) */
5385 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5386 s
[i
]->s
.k
= again
- i
- 1;
5391 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5393 s
[fix2
]->s
.jt
= s
[end
];
5394 s
[fix4
]->s
.jf
= s
[end
];
5395 s
[fix5
]->s
.jt
= s
[end
];
5402 for (i
= 0; i
< max
- 1; i
++)
5403 s
[i
]->next
= s
[i
+ 1];
5404 s
[max
- 1]->next
= NULL
;
5409 b
= new_block(JMP(BPF_JEQ
));
5410 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5420 static struct block
*
5421 gen_check_802_11_data_frame()
5424 struct block
*b0
, *b1
;
5427 * A data frame has the 0x08 bit (b3) in the frame control field set
5428 * and the 0x04 bit (b2) clear.
5430 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5431 b0
= new_block(JMP(BPF_JSET
));
5435 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5436 b1
= new_block(JMP(BPF_JSET
));
5447 * Generate code that checks whether the packet is a packet for protocol
5448 * <proto> and whether the type field in that protocol's header has
5449 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5450 * IP packet and checks the protocol number in the IP header against <v>.
5452 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5453 * against Q_IP and Q_IPV6.
5455 static struct block
*
5456 gen_proto(v
, proto
, dir
)
5461 struct block
*b0
, *b1
;
5463 if (dir
!= Q_DEFAULT
)
5464 bpf_error("direction applied to 'proto'");
5469 b0
= gen_proto(v
, Q_IP
, dir
);
5470 b1
= gen_proto(v
, Q_IPV6
, dir
);
5478 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5479 * not LLC encapsulation with LLCSAP_IP.
5481 * For IEEE 802 networks - which includes 802.5 token ring
5482 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5483 * says that SNAP encapsulation is used, not LLC encapsulation
5486 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5487 * RFC 2225 say that SNAP encapsulation is used, not LLC
5488 * encapsulation with LLCSAP_IP.
5490 * So we always check for ETHERTYPE_IP.
5492 b0
= gen_linktype(ETHERTYPE_IP
);
5494 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5496 b1
= gen_protochain(v
, Q_IP
);
5506 * Frame Relay packets typically have an OSI
5507 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5508 * generates code to check for all the OSI
5509 * NLPIDs, so calling it and then adding a check
5510 * for the particular NLPID for which we're
5511 * looking is bogus, as we can just check for
5514 * What we check for is the NLPID and a frame
5515 * control field value of UI, i.e. 0x03 followed
5518 * XXX - assumes a 2-byte Frame Relay header with
5519 * DLCI and flags. What if the address is longer?
5521 * XXX - what about SNAP-encapsulated frames?
5523 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5529 * Cisco uses an Ethertype lookalike - for OSI,
5532 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5533 /* OSI in C-HDLC is stuffed with a fudge byte */
5534 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5539 b0
= gen_linktype(LLCSAP_ISONS
);
5540 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5546 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5548 * 4 is the offset of the PDU type relative to the IS-IS
5551 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5556 bpf_error("arp does not encapsulate another protocol");
5560 bpf_error("rarp does not encapsulate another protocol");
5564 bpf_error("atalk encapsulation is not specifiable");
5568 bpf_error("decnet encapsulation is not specifiable");
5572 bpf_error("sca does not encapsulate another protocol");
5576 bpf_error("lat does not encapsulate another protocol");
5580 bpf_error("moprc does not encapsulate another protocol");
5584 bpf_error("mopdl does not encapsulate another protocol");
5588 return gen_linktype(v
);
5591 bpf_error("'udp proto' is bogus");
5595 bpf_error("'tcp proto' is bogus");
5599 bpf_error("'sctp proto' is bogus");
5603 bpf_error("'icmp proto' is bogus");
5607 bpf_error("'igmp proto' is bogus");
5611 bpf_error("'igrp proto' is bogus");
5615 bpf_error("'pim proto' is bogus");
5619 bpf_error("'vrrp proto' is bogus");
5624 b0
= gen_linktype(ETHERTYPE_IPV6
);
5626 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5628 b1
= gen_protochain(v
, Q_IPV6
);
5634 bpf_error("'icmp6 proto' is bogus");
5638 bpf_error("'ah proto' is bogus");
5641 bpf_error("'ah proto' is bogus");
5644 bpf_error("'stp proto' is bogus");
5647 bpf_error("'ipx proto' is bogus");
5650 bpf_error("'netbeui proto' is bogus");
5653 bpf_error("'radio proto' is bogus");
5664 register const char *name
;
5667 int proto
= q
.proto
;
5671 bpf_u_int32 mask
, addr
;
5673 bpf_u_int32
**alist
;
5676 struct sockaddr_in
*sin4
;
5677 struct sockaddr_in6
*sin6
;
5678 struct addrinfo
*res
, *res0
;
5679 struct in6_addr mask128
;
5681 struct block
*b
, *tmp
;
5682 int port
, real_proto
;
5688 addr
= pcap_nametonetaddr(name
);
5690 bpf_error("unknown network '%s'", name
);
5691 /* Left justify network addr and calculate its network mask */
5693 while (addr
&& (addr
& 0xff000000) == 0) {
5697 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5701 if (proto
== Q_LINK
) {
5705 eaddr
= pcap_ether_hostton(name
);
5708 "unknown ether host '%s'", name
);
5709 b
= gen_ehostop(eaddr
, dir
);
5714 eaddr
= pcap_ether_hostton(name
);
5717 "unknown FDDI host '%s'", name
);
5718 b
= gen_fhostop(eaddr
, dir
);
5723 eaddr
= pcap_ether_hostton(name
);
5726 "unknown token ring host '%s'", name
);
5727 b
= gen_thostop(eaddr
, dir
);
5731 case DLT_IEEE802_11
:
5732 case DLT_IEEE802_11_RADIO_AVS
:
5733 case DLT_IEEE802_11_RADIO
:
5734 case DLT_PRISM_HEADER
:
5736 eaddr
= pcap_ether_hostton(name
);
5739 "unknown 802.11 host '%s'", name
);
5740 b
= gen_wlanhostop(eaddr
, dir
);
5744 case DLT_IP_OVER_FC
:
5745 eaddr
= pcap_ether_hostton(name
);
5748 "unknown Fibre Channel host '%s'", name
);
5749 b
= gen_ipfchostop(eaddr
, dir
);
5758 * Check that the packet doesn't begin
5759 * with an LE Control marker. (We've
5760 * already generated a test for LANE.)
5762 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5766 eaddr
= pcap_ether_hostton(name
);
5769 "unknown ether host '%s'", name
);
5770 b
= gen_ehostop(eaddr
, dir
);
5776 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5777 } else if (proto
== Q_DECNET
) {
5778 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5780 * I don't think DECNET hosts can be multihomed, so
5781 * there is no need to build up a list of addresses
5783 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5786 alist
= pcap_nametoaddr(name
);
5787 if (alist
== NULL
|| *alist
== NULL
)
5788 bpf_error("unknown host '%s'", name
);
5790 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5792 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5794 tmp
= gen_host(**alist
++, 0xffffffff,
5795 tproto
, dir
, q
.addr
);
5801 memset(&mask128
, 0xff, sizeof(mask128
));
5802 res0
= res
= pcap_nametoaddrinfo(name
);
5804 bpf_error("unknown host '%s'", name
);
5806 tproto
= tproto6
= proto
;
5807 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5811 for (res
= res0
; res
; res
= res
->ai_next
) {
5812 switch (res
->ai_family
) {
5814 if (tproto
== Q_IPV6
)
5817 sin4
= (struct sockaddr_in
*)
5819 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5820 0xffffffff, tproto
, dir
, q
.addr
);
5823 if (tproto6
== Q_IP
)
5826 sin6
= (struct sockaddr_in6
*)
5828 tmp
= gen_host6(&sin6
->sin6_addr
,
5829 &mask128
, tproto6
, dir
, q
.addr
);
5840 bpf_error("unknown host '%s'%s", name
,
5841 (proto
== Q_DEFAULT
)
5843 : " for specified address family");
5850 if (proto
!= Q_DEFAULT
&&
5851 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5852 bpf_error("illegal qualifier of 'port'");
5853 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5854 bpf_error("unknown port '%s'", name
);
5855 if (proto
== Q_UDP
) {
5856 if (real_proto
== IPPROTO_TCP
)
5857 bpf_error("port '%s' is tcp", name
);
5858 else if (real_proto
== IPPROTO_SCTP
)
5859 bpf_error("port '%s' is sctp", name
);
5861 /* override PROTO_UNDEF */
5862 real_proto
= IPPROTO_UDP
;
5864 if (proto
== Q_TCP
) {
5865 if (real_proto
== IPPROTO_UDP
)
5866 bpf_error("port '%s' is udp", name
);
5868 else if (real_proto
== IPPROTO_SCTP
)
5869 bpf_error("port '%s' is sctp", name
);
5871 /* override PROTO_UNDEF */
5872 real_proto
= IPPROTO_TCP
;
5874 if (proto
== Q_SCTP
) {
5875 if (real_proto
== IPPROTO_UDP
)
5876 bpf_error("port '%s' is udp", name
);
5878 else if (real_proto
== IPPROTO_TCP
)
5879 bpf_error("port '%s' is tcp", name
);
5881 /* override PROTO_UNDEF */
5882 real_proto
= IPPROTO_SCTP
;
5885 return gen_port(port
, real_proto
, dir
);
5887 b
= gen_port(port
, real_proto
, dir
);
5888 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5893 if (proto
!= Q_DEFAULT
&&
5894 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5895 bpf_error("illegal qualifier of 'portrange'");
5896 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5897 bpf_error("unknown port in range '%s'", name
);
5898 if (proto
== Q_UDP
) {
5899 if (real_proto
== IPPROTO_TCP
)
5900 bpf_error("port in range '%s' is tcp", name
);
5901 else if (real_proto
== IPPROTO_SCTP
)
5902 bpf_error("port in range '%s' is sctp", name
);
5904 /* override PROTO_UNDEF */
5905 real_proto
= IPPROTO_UDP
;
5907 if (proto
== Q_TCP
) {
5908 if (real_proto
== IPPROTO_UDP
)
5909 bpf_error("port in range '%s' is udp", name
);
5910 else if (real_proto
== IPPROTO_SCTP
)
5911 bpf_error("port in range '%s' is sctp", name
);
5913 /* override PROTO_UNDEF */
5914 real_proto
= IPPROTO_TCP
;
5916 if (proto
== Q_SCTP
) {
5917 if (real_proto
== IPPROTO_UDP
)
5918 bpf_error("port in range '%s' is udp", name
);
5919 else if (real_proto
== IPPROTO_TCP
)
5920 bpf_error("port in range '%s' is tcp", name
);
5922 /* override PROTO_UNDEF */
5923 real_proto
= IPPROTO_SCTP
;
5926 return gen_portrange(port1
, port2
, real_proto
, dir
);
5928 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5929 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5935 eaddr
= pcap_ether_hostton(name
);
5937 bpf_error("unknown ether host: %s", name
);
5939 alist
= pcap_nametoaddr(name
);
5940 if (alist
== NULL
|| *alist
== NULL
)
5941 bpf_error("unknown host '%s'", name
);
5942 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5946 bpf_error("'gateway' not supported in this configuration");
5950 real_proto
= lookup_proto(name
, proto
);
5951 if (real_proto
>= 0)
5952 return gen_proto(real_proto
, proto
, dir
);
5954 bpf_error("unknown protocol: %s", name
);
5957 real_proto
= lookup_proto(name
, proto
);
5958 if (real_proto
>= 0)
5959 return gen_protochain(real_proto
, proto
, dir
);
5961 bpf_error("unknown protocol: %s", name
);
5972 gen_mcode(s1
, s2
, masklen
, q
)
5973 register const char *s1
, *s2
;
5974 register int masklen
;
5977 register int nlen
, mlen
;
5980 nlen
= __pcap_atoin(s1
, &n
);
5981 /* Promote short ipaddr */
5985 mlen
= __pcap_atoin(s2
, &m
);
5986 /* Promote short ipaddr */
5989 bpf_error("non-network bits set in \"%s mask %s\"",
5992 /* Convert mask len to mask */
5994 bpf_error("mask length must be <= 32");
5997 * X << 32 is not guaranteed by C to be 0; it's
6002 m
= 0xffffffff << (32 - masklen
);
6004 bpf_error("non-network bits set in \"%s/%d\"",
6011 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6014 bpf_error("Mask syntax for networks only");
6023 register const char *s
;
6028 int proto
= q
.proto
;
6034 else if (q
.proto
== Q_DECNET
)
6035 vlen
= __pcap_atodn(s
, &v
);
6037 vlen
= __pcap_atoin(s
, &v
);
6044 if (proto
== Q_DECNET
)
6045 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6046 else if (proto
== Q_LINK
) {
6047 bpf_error("illegal link layer address");
6050 if (s
== NULL
&& q
.addr
== Q_NET
) {
6051 /* Promote short net number */
6052 while (v
&& (v
& 0xff000000) == 0) {
6057 /* Promote short ipaddr */
6061 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6066 proto
= IPPROTO_UDP
;
6067 else if (proto
== Q_TCP
)
6068 proto
= IPPROTO_TCP
;
6069 else if (proto
== Q_SCTP
)
6070 proto
= IPPROTO_SCTP
;
6071 else if (proto
== Q_DEFAULT
)
6072 proto
= PROTO_UNDEF
;
6074 bpf_error("illegal qualifier of 'port'");
6077 return gen_port((int)v
, proto
, dir
);
6081 b
= gen_port((int)v
, proto
, dir
);
6082 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6089 proto
= IPPROTO_UDP
;
6090 else if (proto
== Q_TCP
)
6091 proto
= IPPROTO_TCP
;
6092 else if (proto
== Q_SCTP
)
6093 proto
= IPPROTO_SCTP
;
6094 else if (proto
== Q_DEFAULT
)
6095 proto
= PROTO_UNDEF
;
6097 bpf_error("illegal qualifier of 'portrange'");
6100 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6104 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6105 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6111 bpf_error("'gateway' requires a name");
6115 return gen_proto((int)v
, proto
, dir
);
6118 return gen_protochain((int)v
, proto
, dir
);
6133 gen_mcode6(s1
, s2
, masklen
, q
)
6134 register const char *s1
, *s2
;
6135 register int masklen
;
6138 struct addrinfo
*res
;
6139 struct in6_addr
*addr
;
6140 struct in6_addr mask
;
6145 bpf_error("no mask %s supported", s2
);
6147 res
= pcap_nametoaddrinfo(s1
);
6149 bpf_error("invalid ip6 address %s", s1
);
6151 bpf_error("%s resolved to multiple address", s1
);
6152 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6154 if (sizeof(mask
) * 8 < masklen
)
6155 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6156 memset(&mask
, 0, sizeof(mask
));
6157 memset(&mask
, 0xff, masklen
/ 8);
6159 mask
.s6_addr
[masklen
/ 8] =
6160 (0xff << (8 - masklen
% 8)) & 0xff;
6163 a
= (u_int32_t
*)addr
;
6164 m
= (u_int32_t
*)&mask
;
6165 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6166 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6167 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6175 bpf_error("Mask syntax for networks only");
6179 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6184 bpf_error("invalid qualifier against IPv6 address");
6193 register const u_char
*eaddr
;
6196 struct block
*b
, *tmp
;
6198 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6201 return gen_ehostop(eaddr
, (int)q
.dir
);
6203 return gen_fhostop(eaddr
, (int)q
.dir
);
6205 return gen_thostop(eaddr
, (int)q
.dir
);
6206 case DLT_IEEE802_11
:
6207 case DLT_IEEE802_11_RADIO_AVS
:
6208 case DLT_IEEE802_11_RADIO
:
6209 case DLT_PRISM_HEADER
:
6211 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6215 * Check that the packet doesn't begin with an
6216 * LE Control marker. (We've already generated
6219 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6224 * Now check the MAC address.
6226 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6231 case DLT_IP_OVER_FC
:
6232 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6234 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6238 bpf_error("ethernet address used in non-ether expression");
6245 struct slist
*s0
, *s1
;
6248 * This is definitely not the best way to do this, but the
6249 * lists will rarely get long.
6256 static struct slist
*
6262 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6267 static struct slist
*
6273 s
= new_stmt(BPF_LD
|BPF_MEM
);
6279 * Modify "index" to use the value stored into its register as an
6280 * offset relative to the beginning of the header for the protocol
6281 * "proto", and allocate a register and put an item "size" bytes long
6282 * (1, 2, or 4) at that offset into that register, making it the register
6286 gen_load(proto
, inst
, size
)
6291 struct slist
*s
, *tmp
;
6293 int regno
= alloc_reg();
6295 free_reg(inst
->regno
);
6299 bpf_error("data size must be 1, 2, or 4");
6315 bpf_error("unsupported index operation");
6319 * The offset is relative to the beginning of the packet
6320 * data, if we have a radio header. (If we don't, this
6323 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6324 linktype
!= DLT_IEEE802_11_RADIO
&&
6325 linktype
!= DLT_PRISM_HEADER
)
6326 bpf_error("radio information not present in capture");
6329 * Load into the X register the offset computed into the
6330 * register specifed by "index".
6332 s
= xfer_to_x(inst
);
6335 * Load the item at that offset.
6337 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6339 sappend(inst
->s
, s
);
6344 * The offset is relative to the beginning of
6345 * the link-layer header.
6347 * XXX - what about ATM LANE? Should the index be
6348 * relative to the beginning of the AAL5 frame, so
6349 * that 0 refers to the beginning of the LE Control
6350 * field, or relative to the beginning of the LAN
6351 * frame, so that 0 refers, for Ethernet LANE, to
6352 * the beginning of the destination address?
6354 s
= gen_llprefixlen();
6357 * If "s" is non-null, it has code to arrange that the
6358 * X register contains the length of the prefix preceding
6359 * the link-layer header. Add to it the offset computed
6360 * into the register specified by "index", and move that
6361 * into the X register. Otherwise, just load into the X
6362 * register the offset computed into the register specifed
6366 sappend(s
, xfer_to_a(inst
));
6367 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6368 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6370 s
= xfer_to_x(inst
);
6373 * Load the item at the sum of the offset we've put in the
6374 * X register and the offset of the start of the link
6375 * layer header (which is 0 if the radio header is
6376 * variable-length; that header length is what we put
6377 * into the X register and then added to the index).
6379 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6382 sappend(inst
->s
, s
);
6398 * The offset is relative to the beginning of
6399 * the network-layer header.
6400 * XXX - are there any cases where we want
6403 s
= gen_off_macpl();
6406 * If "s" is non-null, it has code to arrange that the
6407 * X register contains the offset of the MAC-layer
6408 * payload. Add to it the offset computed into the
6409 * register specified by "index", and move that into
6410 * the X register. Otherwise, just load into the X
6411 * register the offset computed into the register specifed
6415 sappend(s
, xfer_to_a(inst
));
6416 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6417 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6419 s
= xfer_to_x(inst
);
6422 * Load the item at the sum of the offset we've put in the
6423 * X register, the offset of the start of the network
6424 * layer header from the beginning of the MAC-layer
6425 * payload, and the purported offset of the start of the
6426 * MAC-layer payload (which might be 0 if there's a
6427 * variable-length prefix before the link-layer header
6428 * or the link-layer header itself is variable-length;
6429 * the variable-length offset of the start of the
6430 * MAC-layer payload is what we put into the X register
6431 * and then added to the index).
6433 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6434 tmp
->s
.k
= off_macpl
+ off_nl
;
6436 sappend(inst
->s
, s
);
6439 * Do the computation only if the packet contains
6440 * the protocol in question.
6442 b
= gen_proto_abbrev(proto
);
6444 gen_and(inst
->b
, b
);
6457 * The offset is relative to the beginning of
6458 * the transport-layer header.
6460 * Load the X register with the length of the IPv4 header
6461 * (plus the offset of the link-layer header, if it's
6462 * a variable-length header), in bytes.
6464 * XXX - are there any cases where we want
6466 * XXX - we should, if we're built with
6467 * IPv6 support, generate code to load either
6468 * IPv4, IPv6, or both, as appropriate.
6470 s
= gen_loadx_iphdrlen();
6473 * The X register now contains the sum of the length
6474 * of any variable-length header preceding the link-layer
6475 * header, any variable-length link-layer header, and the
6476 * length of the network-layer header.
6478 * Load into the A register the offset relative to
6479 * the beginning of the transport layer header,
6480 * add the X register to that, move that to the
6481 * X register, and load with an offset from the
6482 * X register equal to the offset of the network
6483 * layer header relative to the beginning of
6484 * the MAC-layer payload plus the fixed-length
6485 * portion of the offset of the MAC-layer payload
6486 * from the beginning of the raw packet data.
6488 sappend(s
, xfer_to_a(inst
));
6489 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6490 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6491 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6492 tmp
->s
.k
= off_macpl
+ off_nl
;
6493 sappend(inst
->s
, s
);
6496 * Do the computation only if the packet contains
6497 * the protocol in question - which is true only
6498 * if this is an IP datagram and is the first or
6499 * only fragment of that datagram.
6501 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6503 gen_and(inst
->b
, b
);
6505 gen_and(gen_proto_abbrev(Q_IP
), b
);
6511 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6515 inst
->regno
= regno
;
6516 s
= new_stmt(BPF_ST
);
6518 sappend(inst
->s
, s
);
6524 gen_relation(code
, a0
, a1
, reversed
)
6526 struct arth
*a0
, *a1
;
6529 struct slist
*s0
, *s1
, *s2
;
6530 struct block
*b
, *tmp
;
6534 if (code
== BPF_JEQ
) {
6535 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6536 b
= new_block(JMP(code
));
6540 b
= new_block(BPF_JMP
|code
|BPF_X
);
6546 sappend(a0
->s
, a1
->s
);
6550 free_reg(a0
->regno
);
6551 free_reg(a1
->regno
);
6553 /* 'and' together protocol checks */
6556 gen_and(a0
->b
, tmp
= a1
->b
);
6572 int regno
= alloc_reg();
6573 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6576 s
= new_stmt(BPF_LD
|BPF_LEN
);
6577 s
->next
= new_stmt(BPF_ST
);
6578 s
->next
->s
.k
= regno
;
6593 a
= (struct arth
*)newchunk(sizeof(*a
));
6597 s
= new_stmt(BPF_LD
|BPF_IMM
);
6599 s
->next
= new_stmt(BPF_ST
);
6615 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6618 s
= new_stmt(BPF_ST
);
6626 gen_arth(code
, a0
, a1
)
6628 struct arth
*a0
, *a1
;
6630 struct slist
*s0
, *s1
, *s2
;
6634 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6639 sappend(a0
->s
, a1
->s
);
6641 free_reg(a0
->regno
);
6642 free_reg(a1
->regno
);
6644 s0
= new_stmt(BPF_ST
);
6645 a0
->regno
= s0
->s
.k
= alloc_reg();
6652 * Here we handle simple allocation of the scratch registers.
6653 * If too many registers are alloc'd, the allocator punts.
6655 static int regused
[BPF_MEMWORDS
];
6659 * Initialize the table of used registers and the current register.
6665 memset(regused
, 0, sizeof regused
);
6669 * Return the next free register.
6674 int n
= BPF_MEMWORDS
;
6677 if (regused
[curreg
])
6678 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6680 regused
[curreg
] = 1;
6684 bpf_error("too many registers needed to evaluate expression");
6690 * Return a register to the table so it can
6700 static struct block
*
6707 s
= new_stmt(BPF_LD
|BPF_LEN
);
6708 b
= new_block(JMP(jmp
));
6719 return gen_len(BPF_JGE
, n
);
6723 * Actually, this is less than or equal.
6731 b
= gen_len(BPF_JGT
, n
);
6738 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6739 * the beginning of the link-layer header.
6740 * XXX - that means you can't test values in the radiotap header, but
6741 * as that header is difficult if not impossible to parse generally
6742 * without a loop, that might not be a severe problem. A new keyword
6743 * "radio" could be added for that, although what you'd really want
6744 * would be a way of testing particular radio header values, which
6745 * would generate code appropriate to the radio header in question.
6748 gen_byteop(op
, idx
, val
)
6759 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6762 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6766 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6770 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6774 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6778 b
= new_block(JMP(BPF_JEQ
));
6785 static u_char abroadcast
[] = { 0x0 };
6788 gen_broadcast(proto
)
6791 bpf_u_int32 hostmask
;
6792 struct block
*b0
, *b1
, *b2
;
6793 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6801 case DLT_ARCNET_LINUX
:
6802 return gen_ahostop(abroadcast
, Q_DST
);
6804 return gen_ehostop(ebroadcast
, Q_DST
);
6806 return gen_fhostop(ebroadcast
, Q_DST
);
6808 return gen_thostop(ebroadcast
, Q_DST
);
6809 case DLT_IEEE802_11
:
6810 case DLT_IEEE802_11_RADIO_AVS
:
6811 case DLT_IEEE802_11_RADIO
:
6813 case DLT_PRISM_HEADER
:
6814 return gen_wlanhostop(ebroadcast
, Q_DST
);
6815 case DLT_IP_OVER_FC
:
6816 return gen_ipfchostop(ebroadcast
, Q_DST
);
6820 * Check that the packet doesn't begin with an
6821 * LE Control marker. (We've already generated
6824 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6829 * Now check the MAC address.
6831 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6837 bpf_error("not a broadcast link");
6842 b0
= gen_linktype(ETHERTYPE_IP
);
6843 hostmask
= ~netmask
;
6844 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6845 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6846 (bpf_int32
)(~0 & hostmask
), hostmask
);
6851 bpf_error("only link-layer/IP broadcast filters supported");
6857 * Generate code to test the low-order bit of a MAC address (that's
6858 * the bottom bit of the *first* byte).
6860 static struct block
*
6861 gen_mac_multicast(offset
)
6864 register struct block
*b0
;
6865 register struct slist
*s
;
6867 /* link[offset] & 1 != 0 */
6868 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6869 b0
= new_block(JMP(BPF_JSET
));
6876 gen_multicast(proto
)
6879 register struct block
*b0
, *b1
, *b2
;
6880 register struct slist
*s
;
6888 case DLT_ARCNET_LINUX
:
6889 /* all ARCnet multicasts use the same address */
6890 return gen_ahostop(abroadcast
, Q_DST
);
6892 /* ether[0] & 1 != 0 */
6893 return gen_mac_multicast(0);
6896 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6898 * XXX - was that referring to bit-order issues?
6900 /* fddi[1] & 1 != 0 */
6901 return gen_mac_multicast(1);
6903 /* tr[2] & 1 != 0 */
6904 return gen_mac_multicast(2);
6905 case DLT_IEEE802_11
:
6906 case DLT_IEEE802_11_RADIO_AVS
:
6908 case DLT_IEEE802_11_RADIO
:
6909 case DLT_PRISM_HEADER
:
6913 * For control frames, there is no DA.
6915 * For management frames, DA is at an
6916 * offset of 4 from the beginning of
6919 * For data frames, DA is at an offset
6920 * of 4 from the beginning of the packet
6921 * if To DS is clear and at an offset of
6922 * 16 from the beginning of the packet
6927 * Generate the tests to be done for data frames.
6929 * First, check for To DS set, i.e. "link[1] & 0x01".
6931 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6932 b1
= new_block(JMP(BPF_JSET
));
6933 b1
->s
.k
= 0x01; /* To DS */
6937 * If To DS is set, the DA is at 16.
6939 b0
= gen_mac_multicast(16);
6943 * Now, check for To DS not set, i.e. check
6944 * "!(link[1] & 0x01)".
6946 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6947 b2
= new_block(JMP(BPF_JSET
));
6948 b2
->s
.k
= 0x01; /* To DS */
6953 * If To DS is not set, the DA is at 4.
6955 b1
= gen_mac_multicast(4);
6959 * Now OR together the last two checks. That gives
6960 * the complete set of checks for data frames.
6965 * Now check for a data frame.
6966 * I.e, check "link[0] & 0x08".
6968 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6969 b1
= new_block(JMP(BPF_JSET
));
6974 * AND that with the checks done for data frames.
6979 * If the high-order bit of the type value is 0, this
6980 * is a management frame.
6981 * I.e, check "!(link[0] & 0x08)".
6983 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6984 b2
= new_block(JMP(BPF_JSET
));
6990 * For management frames, the DA is at 4.
6992 b1
= gen_mac_multicast(4);
6996 * OR that with the checks done for data frames.
6997 * That gives the checks done for management and
7003 * If the low-order bit of the type value is 1,
7004 * this is either a control frame or a frame
7005 * with a reserved type, and thus not a
7008 * I.e., check "!(link[0] & 0x04)".
7010 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7011 b1
= new_block(JMP(BPF_JSET
));
7017 * AND that with the checks for data and management
7022 case DLT_IP_OVER_FC
:
7023 b0
= gen_mac_multicast(2);
7028 * Check that the packet doesn't begin with an
7029 * LE Control marker. (We've already generated
7032 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7036 /* ether[off_mac] & 1 != 0 */
7037 b0
= gen_mac_multicast(off_mac
);
7045 /* Link not known to support multicasts */
7049 b0
= gen_linktype(ETHERTYPE_IP
);
7050 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7056 b0
= gen_linktype(ETHERTYPE_IPV6
);
7057 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7062 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7068 * generate command for inbound/outbound. It's here so we can
7069 * make it link-type specific. 'dir' = 0 implies "inbound",
7070 * = 1 implies "outbound".
7076 register struct block
*b0
;
7079 * Only some data link types support inbound/outbound qualifiers.
7083 b0
= gen_relation(BPF_JEQ
,
7084 gen_load(Q_LINK
, gen_loadi(0), 1),
7092 * Match packets sent by this machine.
7094 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7097 * Match packets sent to this machine.
7098 * (No broadcast or multicast packets, or
7099 * packets sent to some other machine and
7100 * received promiscuously.)
7102 * XXX - packets sent to other machines probably
7103 * shouldn't be matched, but what about broadcast
7104 * or multicast packets we received?
7106 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7110 #ifdef HAVE_NET_PFVAR_H
7112 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7113 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7119 /* match outgoing packets */
7120 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7122 /* match incoming packets */
7123 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7127 case DLT_JUNIPER_MFR
:
7128 case DLT_JUNIPER_MLFR
:
7129 case DLT_JUNIPER_MLPPP
:
7130 case DLT_JUNIPER_ATM1
:
7131 case DLT_JUNIPER_ATM2
:
7132 case DLT_JUNIPER_PPPOE
:
7133 case DLT_JUNIPER_PPPOE_ATM
:
7134 case DLT_JUNIPER_GGSN
:
7135 case DLT_JUNIPER_ES
:
7136 case DLT_JUNIPER_MONITOR
:
7137 case DLT_JUNIPER_SERVICES
:
7138 case DLT_JUNIPER_ETHER
:
7139 case DLT_JUNIPER_PPP
:
7140 case DLT_JUNIPER_FRELAY
:
7141 case DLT_JUNIPER_CHDLC
:
7142 case DLT_JUNIPER_VP
:
7143 case DLT_JUNIPER_ST
:
7144 case DLT_JUNIPER_ISM
:
7145 /* juniper flags (including direction) are stored
7146 * the byte after the 3-byte magic number */
7148 /* match outgoing packets */
7149 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7151 /* match incoming packets */
7152 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7157 bpf_error("inbound/outbound not supported on linktype %d",
7165 #ifdef HAVE_NET_PFVAR_H
7166 /* PF firewall log matched interface */
7168 gen_pf_ifname(const char *ifname
)
7173 if (linktype
!= DLT_PFLOG
) {
7174 bpf_error("ifname supported only on PF linktype");
7177 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7178 off
= offsetof(struct pfloghdr
, ifname
);
7179 if (strlen(ifname
) >= len
) {
7180 bpf_error("ifname interface names can only be %d characters",
7184 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7188 /* PF firewall log ruleset name */
7190 gen_pf_ruleset(char *ruleset
)
7194 if (linktype
!= DLT_PFLOG
) {
7195 bpf_error("ruleset supported only on PF linktype");
7199 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7200 bpf_error("ruleset names can only be %ld characters",
7201 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7205 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7206 strlen(ruleset
), (const u_char
*)ruleset
);
7210 /* PF firewall log rule number */
7216 if (linktype
!= DLT_PFLOG
) {
7217 bpf_error("rnr supported only on PF linktype");
7221 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7226 /* PF firewall log sub-rule number */
7228 gen_pf_srnr(int srnr
)
7232 if (linktype
!= DLT_PFLOG
) {
7233 bpf_error("srnr supported only on PF linktype");
7237 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7242 /* PF firewall log reason code */
7244 gen_pf_reason(int reason
)
7248 if (linktype
!= DLT_PFLOG
) {
7249 bpf_error("reason supported only on PF linktype");
7253 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7258 /* PF firewall log action */
7260 gen_pf_action(int action
)
7264 if (linktype
!= DLT_PFLOG
) {
7265 bpf_error("action supported only on PF linktype");
7269 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7273 #else /* !HAVE_NET_PFVAR_H */
7275 gen_pf_ifname(const char *ifname
)
7277 bpf_error("libpcap was compiled without pf support");
7283 gen_pf_ruleset(char *ruleset
)
7285 bpf_error("libpcap was compiled on a machine without pf support");
7293 bpf_error("libpcap was compiled on a machine without pf support");
7299 gen_pf_srnr(int srnr
)
7301 bpf_error("libpcap was compiled on a machine without pf support");
7307 gen_pf_reason(int reason
)
7309 bpf_error("libpcap was compiled on a machine without pf support");
7315 gen_pf_action(int action
)
7317 bpf_error("libpcap was compiled on a machine without pf support");
7321 #endif /* HAVE_NET_PFVAR_H */
7323 /* IEEE 802.11 wireless header */
7325 gen_p80211_type(int type
, int mask
)
7331 case DLT_IEEE802_11
:
7332 case DLT_PRISM_HEADER
:
7333 case DLT_IEEE802_11_RADIO_AVS
:
7334 case DLT_IEEE802_11_RADIO
:
7335 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7340 bpf_error("802.11 link-layer types supported only on 802.11");
7348 gen_p80211_fcdir(int fcdir
)
7354 case DLT_IEEE802_11
:
7355 case DLT_IEEE802_11_RADIO_AVS
:
7356 case DLT_IEEE802_11_RADIO
:
7357 case DLT_PRISM_HEADER
:
7361 bpf_error("frame direction supported only with 802.11 headers");
7365 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7366 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7373 register const u_char
*eaddr
;
7379 case DLT_ARCNET_LINUX
:
7380 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7382 return (gen_ahostop(eaddr
, (int)q
.dir
));
7384 bpf_error("ARCnet address used in non-arc expression");
7390 bpf_error("aid supported only on ARCnet");
7393 bpf_error("ARCnet address used in non-arc expression");
7398 static struct block
*
7399 gen_ahostop(eaddr
, dir
)
7400 register const u_char
*eaddr
;
7403 register struct block
*b0
, *b1
;
7406 /* src comes first, different from Ethernet */
7408 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7411 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7414 b0
= gen_ahostop(eaddr
, Q_SRC
);
7415 b1
= gen_ahostop(eaddr
, Q_DST
);
7421 b0
= gen_ahostop(eaddr
, Q_SRC
);
7422 b1
= gen_ahostop(eaddr
, Q_DST
);
7431 * support IEEE 802.1Q VLAN trunk over ethernet
7437 struct block
*b0
, *b1
;
7439 /* can't check for VLAN-encapsulated packets inside MPLS */
7440 if (label_stack_depth
> 0)
7441 bpf_error("no VLAN match after MPLS");
7444 * Check for a VLAN packet, and then change the offsets to point
7445 * to the type and data fields within the VLAN packet. Just
7446 * increment the offsets, so that we can support a hierarchy, e.g.
7447 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7450 * XXX - this is a bit of a kludge. If we were to split the
7451 * compiler into a parser that parses an expression and
7452 * generates an expression tree, and a code generator that
7453 * takes an expression tree (which could come from our
7454 * parser or from some other parser) and generates BPF code,
7455 * we could perhaps make the offsets parameters of routines
7456 * and, in the handler for an "AND" node, pass to subnodes
7457 * other than the VLAN node the adjusted offsets.
7459 * This would mean that "vlan" would, instead of changing the
7460 * behavior of *all* tests after it, change only the behavior
7461 * of tests ANDed with it. That would change the documented
7462 * semantics of "vlan", which might break some expressions.
7463 * However, it would mean that "(vlan and ip) or ip" would check
7464 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7465 * checking only for VLAN-encapsulated IP, so that could still
7466 * be considered worth doing; it wouldn't break expressions
7467 * that are of the form "vlan and ..." or "vlan N and ...",
7468 * which I suspect are the most common expressions involving
7469 * "vlan". "vlan or ..." doesn't necessarily do what the user
7470 * would really want, now, as all the "or ..." tests would
7471 * be done assuming a VLAN, even though the "or" could be viewed
7472 * as meaning "or, if this isn't a VLAN packet...".
7479 /* check for VLAN */
7480 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7481 (bpf_int32
)ETHERTYPE_8021Q
);
7483 /* If a specific VLAN is requested, check VLAN id */
7484 if (vlan_num
>= 0) {
7485 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7486 (bpf_int32
)vlan_num
, 0x0fff);
7500 bpf_error("no VLAN support for data link type %d",
7515 struct block
*b0
,*b1
;
7518 * Change the offsets to point to the type and data fields within
7519 * the MPLS packet. Just increment the offsets, so that we
7520 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7521 * capture packets with an outer label of 100000 and an inner
7524 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7528 if (label_stack_depth
> 0) {
7529 /* just match the bottom-of-stack bit clear */
7530 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7533 * Indicate that we're checking MPLS-encapsulated headers,
7534 * to make sure higher level code generators don't try to
7535 * match against IP-related protocols such as Q_ARP, Q_RARP
7540 case DLT_C_HDLC
: /* fall through */
7542 b0
= gen_linktype(ETHERTYPE_MPLS
);
7546 b0
= gen_linktype(PPP_MPLS_UCAST
);
7549 /* FIXME add other DLT_s ...
7550 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7551 * leave it for now */
7554 bpf_error("no MPLS support for data link type %d",
7562 /* If a specific MPLS label is requested, check it */
7563 if (label_num
>= 0) {
7564 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7565 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7566 0xfffff000); /* only compare the first 20 bits */
7573 label_stack_depth
++;
7578 * Support PPPOE discovery and session.
7583 /* check for PPPoE discovery */
7584 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7593 * Test against the PPPoE session link-layer type.
7595 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7598 * Change the offsets to point to the type and data fields within
7599 * the PPP packet, and note that this is PPPoE rather than
7602 * XXX - this is a bit of a kludge. If we were to split the
7603 * compiler into a parser that parses an expression and
7604 * generates an expression tree, and a code generator that
7605 * takes an expression tree (which could come from our
7606 * parser or from some other parser) and generates BPF code,
7607 * we could perhaps make the offsets parameters of routines
7608 * and, in the handler for an "AND" node, pass to subnodes
7609 * other than the PPPoE node the adjusted offsets.
7611 * This would mean that "pppoes" would, instead of changing the
7612 * behavior of *all* tests after it, change only the behavior
7613 * of tests ANDed with it. That would change the documented
7614 * semantics of "pppoes", which might break some expressions.
7615 * However, it would mean that "(pppoes and ip) or ip" would check
7616 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7617 * checking only for VLAN-encapsulated IP, so that could still
7618 * be considered worth doing; it wouldn't break expressions
7619 * that are of the form "pppoes and ..." which I suspect are the
7620 * most common expressions involving "pppoes". "pppoes or ..."
7621 * doesn't necessarily do what the user would really want, now,
7622 * as all the "or ..." tests would be done assuming PPPoE, even
7623 * though the "or" could be viewed as meaning "or, if this isn't
7624 * a PPPoE packet...".
7626 orig_linktype
= off_linktype
; /* save original values */
7631 * The "network-layer" protocol is PPPoE, which has a 6-byte
7632 * PPPoE header, followed by a PPP packet.
7634 * There is no HDLC encapsulation for the PPP packet (it's
7635 * encapsulated in PPPoES instead), so the link-layer type
7636 * starts at the first byte of the PPP packet. For PPPoE,
7637 * that offset is relative to the beginning of the total
7638 * link-layer payload, including any 802.2 LLC header, so
7639 * it's 6 bytes past off_nl.
7641 off_linktype
= off_nl
+ 6;
7644 * The network-layer offsets are relative to the beginning
7645 * of the MAC-layer payload; that's past the 6-byte
7646 * PPPoE header and the 2-byte PPP header.
7649 off_nl_nosnap
= 6+2;
7655 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7667 bpf_error("'vpi' supported only on raw ATM");
7668 if (off_vpi
== (u_int
)-1)
7670 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7676 bpf_error("'vci' supported only on raw ATM");
7677 if (off_vci
== (u_int
)-1)
7679 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7684 if (off_proto
== (u_int
)-1)
7685 abort(); /* XXX - this isn't on FreeBSD */
7686 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7691 if (off_payload
== (u_int
)-1)
7693 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7694 0xffffffff, jtype
, reverse
, jvalue
);
7699 bpf_error("'callref' supported only on raw ATM");
7700 if (off_proto
== (u_int
)-1)
7702 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7703 jtype
, reverse
, jvalue
);
7713 gen_atmtype_abbrev(type
)
7716 struct block
*b0
, *b1
;
7721 /* Get all packets in Meta signalling Circuit */
7723 bpf_error("'metac' supported only on raw ATM");
7724 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7725 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7730 /* Get all packets in Broadcast Circuit*/
7732 bpf_error("'bcc' supported only on raw ATM");
7733 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7734 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7739 /* Get all cells in Segment OAM F4 circuit*/
7741 bpf_error("'oam4sc' supported only on raw ATM");
7742 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7743 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7748 /* Get all cells in End-to-End OAM F4 Circuit*/
7750 bpf_error("'oam4ec' supported only on raw ATM");
7751 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7752 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7757 /* Get all packets in connection Signalling Circuit */
7759 bpf_error("'sc' supported only on raw ATM");
7760 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7761 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7766 /* Get all packets in ILMI Circuit */
7768 bpf_error("'ilmic' supported only on raw ATM");
7769 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7770 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7775 /* Get all LANE packets */
7777 bpf_error("'lane' supported only on raw ATM");
7778 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7781 * Arrange that all subsequent tests assume LANE
7782 * rather than LLC-encapsulated packets, and set
7783 * the offsets appropriately for LANE-encapsulated
7786 * "off_mac" is the offset of the Ethernet header,
7787 * which is 2 bytes past the ATM pseudo-header
7788 * (skipping the pseudo-header and 2-byte LE Client
7789 * field). The other offsets are Ethernet offsets
7790 * relative to "off_mac".
7793 off_mac
= off_payload
+ 2; /* MAC header */
7794 off_linktype
= off_mac
+ 12;
7795 off_macpl
= off_mac
+ 14; /* Ethernet */
7796 off_nl
= 0; /* Ethernet II */
7797 off_nl_nosnap
= 3; /* 802.3+802.2 */
7801 /* Get all LLC-encapsulated packets */
7803 bpf_error("'llc' supported only on raw ATM");
7804 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7815 * Filtering for MTP2 messages based on li value
7816 * FISU, length is null
7817 * LSSU, length is 1 or 2
7818 * MSU, length is 3 or more
7821 gen_mtp2type_abbrev(type
)
7824 struct block
*b0
, *b1
;
7829 if ( (linktype
!= DLT_MTP2
) &&
7830 (linktype
!= DLT_ERF
) &&
7831 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7832 bpf_error("'fisu' supported only on MTP2");
7833 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7834 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7838 if ( (linktype
!= DLT_MTP2
) &&
7839 (linktype
!= DLT_ERF
) &&
7840 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7841 bpf_error("'lssu' supported only on MTP2");
7842 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7843 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7848 if ( (linktype
!= DLT_MTP2
) &&
7849 (linktype
!= DLT_ERF
) &&
7850 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7851 bpf_error("'msu' supported only on MTP2");
7852 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7862 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7869 bpf_u_int32 val1
, val2
, val3
;
7871 switch (mtp3field
) {
7874 if (off_sio
== (u_int
)-1)
7875 bpf_error("'sio' supported only on SS7");
7876 /* sio coded on 1 byte so max value 255 */
7878 bpf_error("sio value %u too big; max value = 255",
7880 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7881 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7885 if (off_opc
== (u_int
)-1)
7886 bpf_error("'opc' supported only on SS7");
7887 /* opc coded on 14 bits so max value 16383 */
7889 bpf_error("opc value %u too big; max value = 16383",
7891 /* the following instructions are made to convert jvalue
7892 * to the form used to write opc in an ss7 message*/
7893 val1
= jvalue
& 0x00003c00;
7895 val2
= jvalue
& 0x000003fc;
7897 val3
= jvalue
& 0x00000003;
7899 jvalue
= val1
+ val2
+ val3
;
7900 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7901 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7905 if (off_dpc
== (u_int
)-1)
7906 bpf_error("'dpc' supported only on SS7");
7907 /* dpc coded on 14 bits so max value 16383 */
7909 bpf_error("dpc value %u too big; max value = 16383",
7911 /* the following instructions are made to convert jvalue
7912 * to the forme used to write dpc in an ss7 message*/
7913 val1
= jvalue
& 0x000000ff;
7915 val2
= jvalue
& 0x00003f00;
7917 jvalue
= val1
+ val2
;
7918 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7919 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7923 if (off_sls
== (u_int
)-1)
7924 bpf_error("'sls' supported only on SS7");
7925 /* sls coded on 4 bits so max value 15 */
7927 bpf_error("sls value %u too big; max value = 15",
7929 /* the following instruction is made to convert jvalue
7930 * to the forme used to write sls in an ss7 message*/
7931 jvalue
= jvalue
<< 4;
7932 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7933 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7942 static struct block
*
7943 gen_msg_abbrev(type
)
7949 * Q.2931 signalling protocol messages for handling virtual circuits
7950 * establishment and teardown
7955 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7959 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7963 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7967 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7971 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7974 case A_RELEASE_DONE
:
7975 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7985 gen_atmmulti_abbrev(type
)
7988 struct block
*b0
, *b1
;
7994 bpf_error("'oam' supported only on raw ATM");
7995 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8000 bpf_error("'oamf4' supported only on raw ATM");
8002 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8003 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8005 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8011 * Get Q.2931 signalling messages for switched
8012 * virtual connection
8015 bpf_error("'connectmsg' supported only on raw ATM");
8016 b0
= gen_msg_abbrev(A_SETUP
);
8017 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8019 b0
= gen_msg_abbrev(A_CONNECT
);
8021 b0
= gen_msg_abbrev(A_CONNECTACK
);
8023 b0
= gen_msg_abbrev(A_RELEASE
);
8025 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8027 b0
= gen_atmtype_abbrev(A_SC
);
8033 bpf_error("'metaconnect' supported only on raw ATM");
8034 b0
= gen_msg_abbrev(A_SETUP
);
8035 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8037 b0
= gen_msg_abbrev(A_CONNECT
);
8039 b0
= gen_msg_abbrev(A_RELEASE
);
8041 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8043 b0
= gen_atmtype_abbrev(A_METAC
);