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.279 2007-05-30 18:05:21 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"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap/namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_PACKET
, /* relative to the beginning of the packet */
139 OR_LINK
, /* relative to the link-layer header */
140 OR_NET
, /* relative to the network-layer header */
141 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
155 #define CHUNK0SIZE 1024
161 static struct chunk chunks
[NCHUNKS
];
162 static int cur_chunk
;
164 static void *newchunk(u_int
);
165 static void freechunks(void);
166 static inline struct block
*new_block(int);
167 static inline struct slist
*new_stmt(int);
168 static struct block
*gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block
*, struct block
*);
172 static void merge(struct block
*, struct block
*);
173 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
178 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
180 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
181 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
182 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
183 static struct slist
*gen_load_llrel(u_int
, u_int
);
184 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
185 static struct slist
*gen_loadx_iphdrlen(void);
186 static struct block
*gen_uncond(int);
187 static inline struct block
*gen_true(void);
188 static inline struct block
*gen_false(void);
189 static struct block
*gen_ether_linktype(int);
190 static struct block
*gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block
*);
192 static void insert_load_llprefixlen(struct block
*);
193 static struct slist
*gen_llprefixlen(void);
194 static struct block
*gen_linktype(int);
195 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
196 static struct block
*gen_llc_linktype(int);
197 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
199 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
201 static struct block
*gen_ahostop(const u_char
*, int);
202 static struct block
*gen_ehostop(const u_char
*, int);
203 static struct block
*gen_fhostop(const u_char
*, int);
204 static struct block
*gen_thostop(const u_char
*, int);
205 static struct block
*gen_wlanhostop(const u_char
*, int);
206 static struct block
*gen_ipfchostop(const u_char
*, int);
207 static struct block
*gen_dnhostop(bpf_u_int32
, int);
208 static struct block
*gen_mpls_linktype(int);
209 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
211 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
214 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
216 static struct block
*gen_ipfrag(void);
217 static struct block
*gen_portatom(int, bpf_int32
);
218 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
220 static struct block
*gen_portatom6(int, bpf_int32
);
221 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
223 struct block
*gen_portop(int, int, int);
224 static struct block
*gen_port(int, int, int);
225 struct block
*gen_portrangeop(int, int, int, int);
226 static struct block
*gen_portrange(int, int, int, int);
228 struct block
*gen_portop6(int, int, int);
229 static struct block
*gen_port6(int, int, int);
230 struct block
*gen_portrangeop6(int, int, int, int);
231 static struct block
*gen_portrange6(int, int, int, int);
233 static int lookup_proto(const char *, int);
234 static struct block
*gen_protochain(int, int, int);
235 static struct block
*gen_proto(int, int, int);
236 static struct slist
*xfer_to_x(struct arth
*);
237 static struct slist
*xfer_to_a(struct arth
*);
238 static struct block
*gen_mac_multicast(int);
239 static struct block
*gen_len(int, int);
241 static struct block
*gen_msg_abbrev(int type
);
252 /* XXX Round up to nearest long. */
253 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
255 /* XXX Round up to structure boundary. */
259 cp
= &chunks
[cur_chunk
];
260 if (n
> cp
->n_left
) {
261 ++cp
, k
= ++cur_chunk
;
263 bpf_error("out of memory");
264 size
= CHUNK0SIZE
<< k
;
265 cp
->m
= (void *)malloc(size
);
267 bpf_error("out of memory");
268 memset((char *)cp
->m
, 0, size
);
271 bpf_error("out of memory");
274 return (void *)((char *)cp
->m
+ cp
->n_left
);
283 for (i
= 0; i
< NCHUNKS
; ++i
)
284 if (chunks
[i
].m
!= NULL
) {
291 * A strdup whose allocations are freed after code generation is over.
295 register const char *s
;
297 int n
= strlen(s
) + 1;
298 char *cp
= newchunk(n
);
304 static inline struct block
*
310 p
= (struct block
*)newchunk(sizeof(*p
));
317 static inline struct slist
*
323 p
= (struct slist
*)newchunk(sizeof(*p
));
329 static struct block
*
333 struct block
*b
= new_block(BPF_RET
|BPF_K
);
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask
;
350 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
351 char *buf
, int optimize
, bpf_u_int32 mask
)
360 if (setjmp(top_ctx
)) {
368 snaplen
= pcap_snapshot(p
);
370 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
371 "snaplen of 0 rejects all packets");
375 lex_init(buf
? buf
: "");
383 root
= gen_retblk(snaplen
);
385 if (optimize
&& !no_optimize
) {
388 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
389 bpf_error("expression rejects all packets");
391 program
->bf_insns
= icode_to_fcode(root
, &len
);
392 program
->bf_len
= len
;
400 * entry point for using the compiler with no pcap open
401 * pass in all the stuff that is needed explicitly instead.
404 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
405 struct bpf_program
*program
,
406 char *buf
, int optimize
, bpf_u_int32 mask
)
411 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
414 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
420 * Clean up a "struct bpf_program" by freeing all the memory allocated
424 pcap_freecode(struct bpf_program
*program
)
427 if (program
->bf_insns
!= NULL
) {
428 free((char *)program
->bf_insns
);
429 program
->bf_insns
= NULL
;
434 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
435 * which of the jt and jf fields has been resolved and which is a pointer
436 * back to another unresolved block (or nil). At least one of the fields
437 * in each block is already resolved.
440 backpatch(list
, target
)
441 struct block
*list
, *target
;
458 * Merge the lists in b0 and b1, using the 'sense' field to indicate
459 * which of jt and jf is the link.
463 struct block
*b0
, *b1
;
465 register struct block
**p
= &b0
;
467 /* Find end of list. */
469 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
471 /* Concatenate the lists. */
479 backpatch(p
, gen_retblk(snaplen
));
480 p
->sense
= !p
->sense
;
481 backpatch(p
, gen_retblk(0));
485 * Insert before the statements of the first (root) block any
486 * statements needed to load the lengths of any variable-length
487 * headers into registers.
489 * XXX - a fancier strategy would be to insert those before the
490 * statements of all blocks that use those lengths and that
491 * have no predecessors that use them, so that we only compute
492 * the lengths if we need them. There might be even better
493 * approaches than that. However, as we're currently only
494 * handling variable-length radiotap headers, and as all
495 * filtering expressions other than raw link[M:N] tests
496 * require the length of that header, doing more for that
497 * header length isn't really worth the effort.
499 insert_load_llprefixlen(root
);
504 struct block
*b0
, *b1
;
506 backpatch(b0
, b1
->head
);
507 b0
->sense
= !b0
->sense
;
508 b1
->sense
= !b1
->sense
;
510 b1
->sense
= !b1
->sense
;
516 struct block
*b0
, *b1
;
518 b0
->sense
= !b0
->sense
;
519 backpatch(b0
, b1
->head
);
520 b0
->sense
= !b0
->sense
;
529 b
->sense
= !b
->sense
;
532 static struct block
*
533 gen_cmp(offrel
, offset
, size
, v
)
534 enum e_offrel offrel
;
538 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
541 static struct block
*
542 gen_cmp_gt(offrel
, offset
, size
, v
)
543 enum e_offrel offrel
;
547 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
550 static struct block
*
551 gen_cmp_ge(offrel
, offset
, size
, v
)
552 enum e_offrel offrel
;
556 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
559 static struct block
*
560 gen_cmp_lt(offrel
, offset
, size
, v
)
561 enum e_offrel offrel
;
565 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
568 static struct block
*
569 gen_cmp_le(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
577 static struct block
*
578 gen_mcmp(offrel
, offset
, size
, v
, mask
)
579 enum e_offrel offrel
;
584 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
587 static struct block
*
588 gen_bcmp(offrel
, offset
, size
, v
)
589 enum e_offrel offrel
;
590 register u_int offset
, size
;
591 register const u_char
*v
;
593 register struct block
*b
, *tmp
;
597 register const u_char
*p
= &v
[size
- 4];
598 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
599 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
601 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
608 register const u_char
*p
= &v
[size
- 2];
609 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
611 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
618 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
627 * AND the field of size "size" at offset "offset" relative to the header
628 * specified by "offrel" with "mask", and compare it with the value "v"
629 * with the test specified by "jtype"; if "reverse" is true, the test
630 * should test the opposite of "jtype".
632 static struct block
*
633 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
634 enum e_offrel offrel
;
636 bpf_u_int32 offset
, size
, mask
, jtype
;
639 struct slist
*s
, *s2
;
642 s
= gen_load_a(offrel
, offset
, size
);
644 if (mask
!= 0xffffffff) {
645 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
650 b
= new_block(JMP(jtype
));
653 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
659 * Various code constructs need to know the layout of the data link
660 * layer. These variables give the necessary offsets from the beginning
661 * of the packet data.
663 * If the link layer has variable_length headers, the offsets are offsets
664 * from the end of the link-link-layer header, and "reg_ll_size" is
665 * the register number for a register containing the length of the
666 * link-layer header. Otherwise, "reg_ll_size" is -1.
668 static int reg_ll_size
;
671 * This is the offset of the beginning of the link-layer header from
672 * the beginning of the raw packet data.
674 * It's usually 0, except for 802.11 with a fixed-length radio header.
675 * (For 802.11 with a variable-length radio header, we have to generate
676 * code to compute that offset; off_ll is 0 in that case.)
681 * This is the offset of the beginning of the MAC-layer header.
682 * It's usually 0, except for ATM LANE, where it's the offset, relative
683 * to the beginning of the raw packet data, of the Ethernet header.
685 static u_int off_mac
;
688 * "off_linktype" is the offset to information in the link-layer header
689 * giving the packet type. This offset is relative to the beginning
690 * of the link-layer header (i.e., it doesn't include off_ll).
692 * For Ethernet, it's the offset of the Ethernet type field.
694 * For link-layer types that always use 802.2 headers, it's the
695 * offset of the LLC header.
697 * For PPP, it's the offset of the PPP type field.
699 * For Cisco HDLC, it's the offset of the CHDLC type field.
701 * For BSD loopback, it's the offset of the AF_ value.
703 * For Linux cooked sockets, it's the offset of the type field.
705 * It's set to -1 for no encapsulation, in which case, IP is assumed.
707 static u_int off_linktype
;
710 * TRUE if the link layer includes an ATM pseudo-header.
712 static int is_atm
= 0;
715 * TRUE if "lane" appeared in the filter; it causes us to generate
716 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
718 static int is_lane
= 0;
721 * These are offsets for the ATM pseudo-header.
723 static u_int off_vpi
;
724 static u_int off_vci
;
725 static u_int off_proto
;
728 * These are offsets for the MTP2 fields.
733 * These are offsets for the MTP3 fields.
735 static u_int off_sio
;
736 static u_int off_opc
;
737 static u_int off_dpc
;
738 static u_int off_sls
;
741 * This is the offset of the first byte after the ATM pseudo_header,
742 * or -1 if there is no ATM pseudo-header.
744 static u_int off_payload
;
747 * These are offsets to the beginning of the network-layer header.
748 * They are relative to the beginning of the link-layer header (i.e.,
749 * they don't include off_ll).
751 * If the link layer never uses 802.2 LLC:
753 * "off_nl" and "off_nl_nosnap" are the same.
755 * If the link layer always uses 802.2 LLC:
757 * "off_nl" is the offset if there's a SNAP header following
760 * "off_nl_nosnap" is the offset if there's no SNAP header.
762 * If the link layer is Ethernet:
764 * "off_nl" is the offset if the packet is an Ethernet II packet
765 * (we assume no 802.3+802.2+SNAP);
767 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
768 * with an 802.2 header following it.
771 static u_int off_nl_nosnap
;
779 linktype
= pcap_datalink(p
);
781 pcap_fddipad
= p
->fddipad
;
785 * Assume it's not raw ATM with a pseudo-header, for now.
796 * And assume we're not doing SS7.
805 * Also assume it's not 802.11 with a fixed-length radio header.
811 label_stack_depth
= 0;
819 off_nl
= 6; /* XXX in reality, variable! */
820 off_nl_nosnap
= 6; /* no 802.2 LLC */
823 case DLT_ARCNET_LINUX
:
825 off_nl
= 8; /* XXX in reality, variable! */
826 off_nl_nosnap
= 8; /* no 802.2 LLC */
831 off_nl
= 14; /* Ethernet II */
832 off_nl_nosnap
= 17; /* 802.3+802.2 */
837 * SLIP doesn't have a link level type. The 16 byte
838 * header is hacked into our SLIP driver.
842 off_nl_nosnap
= 16; /* no 802.2 LLC */
846 /* XXX this may be the same as the DLT_PPP_BSDOS case */
850 off_nl_nosnap
= 24; /* no 802.2 LLC */
857 off_nl_nosnap
= 4; /* no 802.2 LLC */
863 off_nl_nosnap
= 12; /* no 802.2 LLC */
868 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
869 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
872 off_nl_nosnap
= 4; /* no 802.2 LLC */
877 * This does no include the Ethernet header, and
878 * only covers session state.
882 off_nl_nosnap
= 8; /* no 802.2 LLC */
888 off_nl_nosnap
= 24; /* no 802.2 LLC */
893 * FDDI doesn't really have a link-level type field.
894 * We set "off_linktype" to the offset of the LLC header.
896 * To check for Ethernet types, we assume that SSAP = SNAP
897 * is being used and pick out the encapsulated Ethernet type.
898 * XXX - should we generate code to check for SNAP?
902 off_linktype
+= pcap_fddipad
;
904 off_nl
= 21; /* FDDI+802.2+SNAP */
905 off_nl_nosnap
= 16; /* FDDI+802.2 */
907 off_nl
+= pcap_fddipad
;
908 off_nl_nosnap
+= pcap_fddipad
;
914 * Token Ring doesn't really have a link-level type field.
915 * We set "off_linktype" to the offset of the LLC header.
917 * To check for Ethernet types, we assume that SSAP = SNAP
918 * is being used and pick out the encapsulated Ethernet type.
919 * XXX - should we generate code to check for SNAP?
921 * XXX - the header is actually variable-length.
922 * Some various Linux patched versions gave 38
923 * as "off_linktype" and 40 as "off_nl"; however,
924 * if a token ring packet has *no* routing
925 * information, i.e. is not source-routed, the correct
926 * values are 20 and 22, as they are in the vanilla code.
928 * A packet is source-routed iff the uppermost bit
929 * of the first byte of the source address, at an
930 * offset of 8, has the uppermost bit set. If the
931 * packet is source-routed, the total number of bytes
932 * of routing information is 2 plus bits 0x1F00 of
933 * the 16-bit value at an offset of 14 (shifted right
934 * 8 - figure out which byte that is).
937 off_nl
= 22; /* Token Ring+802.2+SNAP */
938 off_nl_nosnap
= 17; /* Token Ring+802.2 */
943 * 802.11 doesn't really have a link-level type field.
944 * We set "off_linktype" to the offset of the LLC header.
946 * To check for Ethernet types, we assume that SSAP = SNAP
947 * is being used and pick out the encapsulated Ethernet type.
948 * XXX - should we generate code to check for SNAP?
950 * XXX - the header is actually variable-length. We
951 * assume a 24-byte link-layer header, as appears in
952 * data frames in networks with no bridges. If the
953 * fromds and tods 802.11 header bits are both set,
954 * it's actually supposed to be 30 bytes.
957 off_nl
= 32; /* 802.11+802.2+SNAP */
958 off_nl_nosnap
= 27; /* 802.11+802.2 */
961 case DLT_PRISM_HEADER
:
963 * Same as 802.11, but with an additional header before
964 * the 802.11 header, containing a bunch of additional
965 * information including radio-level information.
967 * The header is 144 bytes long.
969 * XXX - same variable-length header problem; at least
970 * the Prism header is fixed-length.
974 off_nl
= 32; /* Prism+802.11+802.2+SNAP */
975 off_nl_nosnap
= 27; /* Prism+802.11+802.2 */
978 case DLT_IEEE802_11_RADIO_AVS
:
980 * Same as 802.11, but with an additional header before
981 * the 802.11 header, containing a bunch of additional
982 * information including radio-level information.
984 * The header is 64 bytes long, at least in its
985 * current incarnation.
987 * XXX - same variable-length header problem, only
988 * more so; this header is also variable-length,
989 * with the length being the 32-bit big-endian
990 * number at an offset of 4 from the beginning
991 * of the radio header. We should handle that the
992 * same way we handle the length at the beginning
993 * of the radiotap header.
995 * XXX - in Linux, do any drivers that supply an AVS
996 * header supply a link-layer type other than
997 * ARPHRD_IEEE80211_PRISM? If so, we should map that
998 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
999 * any drivers that supply an AVS header but supply
1000 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1001 * have to check the header in the generated code to
1002 * determine whether it's Prism or AVS.
1006 off_nl
= 32; /* Radio+802.11+802.2+SNAP */
1007 off_nl_nosnap
= 27; /* Radio+802.11+802.2 */
1010 case DLT_IEEE802_11_RADIO
:
1012 * Same as 802.11, but with an additional header before
1013 * the 802.11 header, containing a bunch of additional
1014 * information including radio-level information.
1016 * The radiotap header is variable length, and we
1017 * generate code to compute its length and store it
1018 * in a register. These offsets are relative to the
1019 * beginning of the 802.11 header.
1022 off_nl
= 32; /* 802.11+802.2+SNAP */
1023 off_nl_nosnap
= 27; /* 802.11+802.2 */
1026 case DLT_ATM_RFC1483
:
1027 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1029 * assume routed, non-ISO PDUs
1030 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1032 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1033 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1034 * latter would presumably be treated the way PPPoE
1035 * should be, so you can do "pppoe and udp port 2049"
1036 * or "pppoa and tcp port 80" and have it check for
1037 * PPPo{A,E} and a PPP protocol of IP and....
1040 off_nl
= 8; /* 802.2+SNAP */
1041 off_nl_nosnap
= 3; /* 802.2 */
1046 * Full Frontal ATM; you get AALn PDUs with an ATM
1050 off_vpi
= SUNATM_VPI_POS
;
1051 off_vci
= SUNATM_VCI_POS
;
1052 off_proto
= PROTO_POS
;
1053 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1054 off_payload
= SUNATM_PKT_BEGIN_POS
;
1055 off_linktype
= off_payload
;
1056 off_nl
= off_payload
+8; /* 802.2+SNAP */
1057 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1063 off_nl_nosnap
= 0; /* no 802.2 LLC */
1066 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1069 off_nl_nosnap
= 16; /* no 802.2 LLC */
1074 * LocalTalk does have a 1-byte type field in the LLAP header,
1075 * but really it just indicates whether there is a "short" or
1076 * "long" DDP packet following.
1080 off_nl_nosnap
= 0; /* no 802.2 LLC */
1083 case DLT_IP_OVER_FC
:
1085 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1086 * link-level type field. We set "off_linktype" to the
1087 * offset of the LLC header.
1089 * To check for Ethernet types, we assume that SSAP = SNAP
1090 * is being used and pick out the encapsulated Ethernet type.
1091 * XXX - should we generate code to check for SNAP? RFC
1092 * 2625 says SNAP should be used.
1095 off_nl
= 24; /* IPFC+802.2+SNAP */
1096 off_nl_nosnap
= 19; /* IPFC+802.2 */
1101 * XXX - we should set this to handle SNAP-encapsulated
1102 * frames (NLPID of 0x80).
1106 off_nl_nosnap
= 0; /* no 802.2 LLC */
1110 * the only BPF-interesting FRF.16 frames are non-control frames;
1111 * Frame Relay has a variable length link-layer
1112 * so lets start with offset 4 for now and increments later on (FIXME);
1117 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1120 case DLT_APPLE_IP_OVER_IEEE1394
:
1123 off_nl_nosnap
= 18; /* no 802.2 LLC */
1126 case DLT_LINUX_IRDA
:
1128 * Currently, only raw "link[N:M]" filtering is supported.
1137 * Currently, only raw "link[N:M]" filtering is supported.
1144 case DLT_SYMANTEC_FIREWALL
:
1146 off_nl
= 44; /* Ethernet II */
1147 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1152 /* XXX read this from pf.h? */
1153 off_nl
= PFLOG_HDRLEN
;
1154 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1157 case DLT_JUNIPER_MFR
:
1158 case DLT_JUNIPER_MLFR
:
1159 case DLT_JUNIPER_MLPPP
:
1160 case DLT_JUNIPER_PPP
:
1161 case DLT_JUNIPER_CHDLC
:
1162 case DLT_JUNIPER_FRELAY
:
1165 off_nl_nosnap
= -1; /* no 802.2 LLC */
1168 case DLT_JUNIPER_ATM1
:
1169 off_linktype
= 4; /* in reality variable between 4-8 */
1174 case DLT_JUNIPER_ATM2
:
1175 off_linktype
= 8; /* in reality variable between 8-12 */
1180 /* frames captured on a Juniper PPPoE service PIC
1181 * contain raw ethernet frames */
1182 case DLT_JUNIPER_PPPOE
:
1183 case DLT_JUNIPER_ETHER
:
1185 off_nl
= 18; /* Ethernet II */
1186 off_nl_nosnap
= 21; /* 802.3+802.2 */
1189 case DLT_JUNIPER_PPPOE_ATM
:
1192 off_nl_nosnap
= -1; /* no 802.2 LLC */
1195 case DLT_JUNIPER_GGSN
:
1198 off_nl_nosnap
= -1; /* no 802.2 LLC */
1201 case DLT_JUNIPER_ES
:
1203 off_nl
= -1; /* not really a network layer but raw IP adresses */
1204 off_nl_nosnap
= -1; /* no 802.2 LLC */
1207 case DLT_JUNIPER_MONITOR
:
1209 off_nl
= 12; /* raw IP/IP6 header */
1210 off_nl_nosnap
= -1; /* no 802.2 LLC */
1213 case DLT_JUNIPER_SERVICES
:
1215 off_nl
= -1; /* L3 proto location dep. on cookie type */
1216 off_nl_nosnap
= -1; /* no 802.2 LLC */
1219 case DLT_JUNIPER_VP
:
1225 case DLT_JUNIPER_ISM
:
1250 case DLT_LINUX_LAPD
:
1252 * Currently, only raw "link[N:M]" filtering is supported.
1261 * Currently, only raw "link[N:M]" filtering is supported.
1268 case DLT_BLUETOOTH_HCI_H4
:
1270 * Currently, only raw "link[N:M]" filtering is supported.
1279 * Currently, only raw "link[N:M]" filtering is supported.
1286 bpf_error("unknown data link type %d", linktype
);
1291 * Load a value relative to the beginning of the link-layer header.
1292 * The link-layer header doesn't necessarily begin at the beginning
1293 * of the packet data; there might be a variable-length prefix containing
1294 * radio information.
1296 static struct slist
*
1297 gen_load_llrel(offset
, size
)
1300 struct slist
*s
, *s2
;
1302 s
= gen_llprefixlen();
1305 * If "s" is non-null, it has code to arrange that the X register
1306 * contains the length of the prefix preceding the link-layer
1309 * Otherwise, the length of the prefix preceding the link-layer
1310 * header is "off_ll".
1314 * There's a variable-length prefix preceding the
1315 * link-layer header. "s" points to a list of statements
1316 * that put the length of that prefix into the X register.
1317 * do an indirect load, to use the X register as an offset.
1319 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1324 * There is no variable-length header preceding the
1325 * link-layer header; add in off_ll, which, if there's
1326 * a fixed-length header preceding the link-layer header,
1327 * is the length of that header.
1329 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1330 s
->s
.k
= offset
+ off_ll
;
1336 * Load a value relative to the beginning of the specified header.
1338 static struct slist
*
1339 gen_load_a(offrel
, offset
, size
)
1340 enum e_offrel offrel
;
1343 struct slist
*s
, *s2
;
1348 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1353 s
= gen_load_llrel(offset
, size
);
1357 s
= gen_load_llrel(off_nl
+ offset
, size
);
1361 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1366 * Load the X register with the length of the IPv4 header
1367 * (plus the offset of the link-layer header, if it's
1368 * preceded by a variable-length header such as a radio
1369 * header), in bytes.
1371 s
= gen_loadx_iphdrlen();
1374 * Load the item at {offset of the link-layer header} +
1375 * {offset, relative to the start of the link-layer
1376 * header, of the IPv4 header} + {length of the IPv4 header} +
1377 * {specified offset}.
1379 * (If the link-layer is variable-length, it's included
1380 * in the value in the X register, and off_ll is 0.)
1382 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1383 s2
->s
.k
= off_ll
+ off_nl
+ offset
;
1388 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1399 * Generate code to load into the X register the sum of the length of
1400 * the IPv4 header and any variable-length header preceding the link-layer
1403 static struct slist
*
1404 gen_loadx_iphdrlen()
1406 struct slist
*s
, *s2
;
1408 s
= gen_llprefixlen();
1411 * There's a variable-length prefix preceding the
1412 * link-layer header. "s" points to a list of statements
1413 * that put the length of that prefix into the X register.
1414 * The 4*([k]&0xf) addressing mode can't be used, as we
1415 * don't have a constant offset, so we have to load the
1416 * value in question into the A register and add to it
1417 * the value from the X register.
1419 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1422 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1425 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1430 * The A register now contains the length of the
1431 * IP header. We need to add to it the length
1432 * of the prefix preceding the link-layer
1433 * header, which is still in the X register, and
1434 * move the result into the X register.
1436 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1437 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1440 * There is no variable-length header preceding the
1441 * link-layer header; add in off_ll, which, if there's
1442 * a fixed-length header preceding the link-layer header,
1443 * is the length of that header.
1445 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1446 s
->s
.k
= off_ll
+ off_nl
;
1451 static struct block
*
1458 s
= new_stmt(BPF_LD
|BPF_IMM
);
1460 b
= new_block(JMP(BPF_JEQ
));
1466 static inline struct block
*
1469 return gen_uncond(1);
1472 static inline struct block
*
1475 return gen_uncond(0);
1479 * Byte-swap a 32-bit number.
1480 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1481 * big-endian platforms.)
1483 #define SWAPLONG(y) \
1484 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1487 * Generate code to match a particular packet type.
1489 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1490 * value, if <= ETHERMTU. We use that to determine whether to
1491 * match the type/length field or to check the type/length field for
1492 * a value <= ETHERMTU to see whether it's a type field and then do
1493 * the appropriate test.
1495 static struct block
*
1496 gen_ether_linktype(proto
)
1499 struct block
*b0
, *b1
;
1505 case LLCSAP_NETBEUI
:
1507 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1508 * so we check the DSAP and SSAP.
1510 * LLCSAP_IP checks for IP-over-802.2, rather
1511 * than IP-over-Ethernet or IP-over-SNAP.
1513 * XXX - should we check both the DSAP and the
1514 * SSAP, like this, or should we check just the
1515 * DSAP, as we do for other types <= ETHERMTU
1516 * (i.e., other SAP values)?
1518 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1520 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1521 ((proto
<< 8) | proto
));
1529 * Ethernet_II frames, which are Ethernet
1530 * frames with a frame type of ETHERTYPE_IPX;
1532 * Ethernet_802.3 frames, which are 802.3
1533 * frames (i.e., the type/length field is
1534 * a length field, <= ETHERMTU, rather than
1535 * a type field) with the first two bytes
1536 * after the Ethernet/802.3 header being
1539 * Ethernet_802.2 frames, which are 802.3
1540 * frames with an 802.2 LLC header and
1541 * with the IPX LSAP as the DSAP in the LLC
1544 * Ethernet_SNAP frames, which are 802.3
1545 * frames with an LLC header and a SNAP
1546 * header and with an OUI of 0x000000
1547 * (encapsulated Ethernet) and a protocol
1548 * ID of ETHERTYPE_IPX in the SNAP header.
1550 * XXX - should we generate the same code both
1551 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1555 * This generates code to check both for the
1556 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1558 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1559 (bpf_int32
)LLCSAP_IPX
);
1560 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1565 * Now we add code to check for SNAP frames with
1566 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1568 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1572 * Now we generate code to check for 802.3
1573 * frames in general.
1575 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1579 * Now add the check for 802.3 frames before the
1580 * check for Ethernet_802.2 and Ethernet_802.3,
1581 * as those checks should only be done on 802.3
1582 * frames, not on Ethernet frames.
1587 * Now add the check for Ethernet_II frames, and
1588 * do that before checking for the other frame
1591 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1592 (bpf_int32
)ETHERTYPE_IPX
);
1596 case ETHERTYPE_ATALK
:
1597 case ETHERTYPE_AARP
:
1599 * EtherTalk (AppleTalk protocols on Ethernet link
1600 * layer) may use 802.2 encapsulation.
1604 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1605 * we check for an Ethernet type field less than
1606 * 1500, which means it's an 802.3 length field.
1608 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1612 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1613 * SNAP packets with an organization code of
1614 * 0x080007 (Apple, for Appletalk) and a protocol
1615 * type of ETHERTYPE_ATALK (Appletalk).
1617 * 802.2-encapsulated ETHERTYPE_AARP packets are
1618 * SNAP packets with an organization code of
1619 * 0x000000 (encapsulated Ethernet) and a protocol
1620 * type of ETHERTYPE_AARP (Appletalk ARP).
1622 if (proto
== ETHERTYPE_ATALK
)
1623 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1624 else /* proto == ETHERTYPE_AARP */
1625 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1629 * Check for Ethernet encapsulation (Ethertalk
1630 * phase 1?); we just check for the Ethernet
1633 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1639 if (proto
<= ETHERMTU
) {
1641 * This is an LLC SAP value, so the frames
1642 * that match would be 802.2 frames.
1643 * Check that the frame is an 802.2 frame
1644 * (i.e., that the length/type field is
1645 * a length field, <= ETHERMTU) and
1646 * then check the DSAP.
1648 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1650 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1656 * This is an Ethernet type, so compare
1657 * the length/type field with it (if
1658 * the frame is an 802.2 frame, the length
1659 * field will be <= ETHERMTU, and, as
1660 * "proto" is > ETHERMTU, this test
1661 * will fail and the frame won't match,
1662 * which is what we want).
1664 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1671 * Generate code to match a particular packet type.
1673 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1674 * value, if <= ETHERMTU. We use that to determine whether to
1675 * match the type field or to check the type field for the special
1676 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1678 static struct block
*
1679 gen_linux_sll_linktype(proto
)
1682 struct block
*b0
, *b1
;
1688 case LLCSAP_NETBEUI
:
1690 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1691 * so we check the DSAP and SSAP.
1693 * LLCSAP_IP checks for IP-over-802.2, rather
1694 * than IP-over-Ethernet or IP-over-SNAP.
1696 * XXX - should we check both the DSAP and the
1697 * SSAP, like this, or should we check just the
1698 * DSAP, as we do for other types <= ETHERMTU
1699 * (i.e., other SAP values)?
1701 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1702 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1703 ((proto
<< 8) | proto
));
1709 * Ethernet_II frames, which are Ethernet
1710 * frames with a frame type of ETHERTYPE_IPX;
1712 * Ethernet_802.3 frames, which have a frame
1713 * type of LINUX_SLL_P_802_3;
1715 * Ethernet_802.2 frames, which are 802.3
1716 * frames with an 802.2 LLC header (i.e, have
1717 * a frame type of LINUX_SLL_P_802_2) and
1718 * with the IPX LSAP as the DSAP in the LLC
1721 * Ethernet_SNAP frames, which are 802.3
1722 * frames with an LLC header and a SNAP
1723 * header and with an OUI of 0x000000
1724 * (encapsulated Ethernet) and a protocol
1725 * ID of ETHERTYPE_IPX in the SNAP header.
1727 * First, do the checks on LINUX_SLL_P_802_2
1728 * frames; generate the check for either
1729 * Ethernet_802.2 or Ethernet_SNAP frames, and
1730 * then put a check for LINUX_SLL_P_802_2 frames
1733 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1734 (bpf_int32
)LLCSAP_IPX
);
1735 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1738 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1742 * Now check for 802.3 frames and OR that with
1743 * the previous test.
1745 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1749 * Now add the check for Ethernet_II frames, and
1750 * do that before checking for the other frame
1753 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1754 (bpf_int32
)ETHERTYPE_IPX
);
1758 case ETHERTYPE_ATALK
:
1759 case ETHERTYPE_AARP
:
1761 * EtherTalk (AppleTalk protocols on Ethernet link
1762 * layer) may use 802.2 encapsulation.
1766 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1767 * we check for the 802.2 protocol type in the
1768 * "Ethernet type" field.
1770 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1773 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1774 * SNAP packets with an organization code of
1775 * 0x080007 (Apple, for Appletalk) and a protocol
1776 * type of ETHERTYPE_ATALK (Appletalk).
1778 * 802.2-encapsulated ETHERTYPE_AARP packets are
1779 * SNAP packets with an organization code of
1780 * 0x000000 (encapsulated Ethernet) and a protocol
1781 * type of ETHERTYPE_AARP (Appletalk ARP).
1783 if (proto
== ETHERTYPE_ATALK
)
1784 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1786 else /* proto == ETHERTYPE_AARP */
1787 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1792 * Check for Ethernet encapsulation (Ethertalk
1793 * phase 1?); we just check for the Ethernet
1796 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1802 if (proto
<= ETHERMTU
) {
1804 * This is an LLC SAP value, so the frames
1805 * that match would be 802.2 frames.
1806 * Check for the 802.2 protocol type
1807 * in the "Ethernet type" field, and
1808 * then check the DSAP.
1810 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1812 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1818 * This is an Ethernet type, so compare
1819 * the length/type field with it (if
1820 * the frame is an 802.2 frame, the length
1821 * field will be <= ETHERMTU, and, as
1822 * "proto" is > ETHERMTU, this test
1823 * will fail and the frame won't match,
1824 * which is what we want).
1826 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1833 insert_radiotap_load_llprefixlen(b
)
1836 struct slist
*s1
, *s2
;
1839 * Prepend to the statements in this block code to load the
1840 * length of the radiotap header into the register assigned
1841 * to hold that length, if one has been assigned.
1843 if (reg_ll_size
!= -1) {
1845 * The 2 bytes at offsets of 2 and 3 from the beginning
1846 * of the radiotap header are the length of the radiotap
1847 * header; unfortunately, it's little-endian, so we have
1848 * to load it a byte at a time and construct the value.
1852 * Load the high-order byte, at an offset of 3, shift it
1853 * left a byte, and put the result in the X register.
1855 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1857 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1860 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1864 * Load the next byte, at an offset of 2, and OR the
1865 * value from the X register into it.
1867 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1870 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1874 * Now allocate a register to hold that value and store
1877 s2
= new_stmt(BPF_ST
);
1878 s2
->s
.k
= reg_ll_size
;
1882 * Now move it into the X register.
1884 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1888 * Now append all the existing statements in this
1889 * block to these statements.
1891 sappend(s1
, b
->stmts
);
1898 insert_load_llprefixlen(b
)
1903 case DLT_IEEE802_11_RADIO
:
1904 insert_radiotap_load_llprefixlen(b
);
1909 static struct slist
*
1910 gen_radiotap_llprefixlen(void)
1914 if (reg_ll_size
== -1) {
1916 * We haven't yet assigned a register for the length
1917 * of the radiotap header; allocate one.
1919 reg_ll_size
= alloc_reg();
1923 * Load the register containing the radiotap length
1924 * into the X register.
1926 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1927 s
->s
.k
= reg_ll_size
;
1932 * Generate code to compute the link-layer header length, if necessary,
1933 * putting it into the X register, and to return either a pointer to a
1934 * "struct slist" for the list of statements in that code, or NULL if
1935 * no code is necessary.
1937 static struct slist
*
1938 gen_llprefixlen(void)
1942 case DLT_IEEE802_11_RADIO
:
1943 return gen_radiotap_llprefixlen();
1951 * Generate code to match a particular packet type by matching the
1952 * link-layer type field or fields in the 802.2 LLC header.
1954 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1955 * value, if <= ETHERMTU.
1957 static struct block
*
1961 struct block
*b0
, *b1
, *b2
;
1963 /* are we checking MPLS-encapsulated packets? */
1964 if (label_stack_depth
> 0) {
1968 /* FIXME add other L3 proto IDs */
1969 return gen_mpls_linktype(Q_IP
);
1971 case ETHERTYPE_IPV6
:
1973 /* FIXME add other L3 proto IDs */
1974 return gen_mpls_linktype(Q_IPV6
);
1977 bpf_error("unsupported protocol over mpls");
1985 return gen_ether_linktype(proto
);
1993 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1997 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2006 case DLT_IEEE802_11
:
2007 case DLT_IEEE802_11_RADIO_AVS
:
2008 case DLT_IEEE802_11_RADIO
:
2009 case DLT_PRISM_HEADER
:
2010 case DLT_ATM_RFC1483
:
2012 case DLT_IP_OVER_FC
:
2013 return gen_llc_linktype(proto
);
2019 * If "is_lane" is set, check for a LANE-encapsulated
2020 * version of this protocol, otherwise check for an
2021 * LLC-encapsulated version of this protocol.
2023 * We assume LANE means Ethernet, not Token Ring.
2027 * Check that the packet doesn't begin with an
2028 * LE Control marker. (We've already generated
2031 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2036 * Now generate an Ethernet test.
2038 b1
= gen_ether_linktype(proto
);
2043 * Check for LLC encapsulation and then check the
2046 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2047 b1
= gen_llc_linktype(proto
);
2055 return gen_linux_sll_linktype(proto
);
2060 case DLT_SLIP_BSDOS
:
2063 * These types don't provide any type field; packets
2064 * are always IPv4 or IPv6.
2066 * XXX - for IPv4, check for a version number of 4, and,
2067 * for IPv6, check for a version number of 6?
2072 /* Check for a version number of 4. */
2073 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2075 case ETHERTYPE_IPV6
:
2076 /* Check for a version number of 6. */
2077 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2081 return gen_false(); /* always false */
2088 case DLT_PPP_SERIAL
:
2091 * We use Ethernet protocol types inside libpcap;
2092 * map them to the corresponding PPP protocol types.
2101 case ETHERTYPE_IPV6
:
2110 case ETHERTYPE_ATALK
:
2124 * I'm assuming the "Bridging PDU"s that go
2125 * over PPP are Spanning Tree Protocol
2139 * We use Ethernet protocol types inside libpcap;
2140 * map them to the corresponding PPP protocol types.
2145 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2146 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2148 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2153 case ETHERTYPE_IPV6
:
2163 case ETHERTYPE_ATALK
:
2177 * I'm assuming the "Bridging PDU"s that go
2178 * over PPP are Spanning Tree Protocol
2194 * For DLT_NULL, the link-layer header is a 32-bit
2195 * word containing an AF_ value in *host* byte order,
2196 * and for DLT_ENC, the link-layer header begins
2197 * with a 32-bit work containing an AF_ value in
2200 * In addition, if we're reading a saved capture file,
2201 * the host byte order in the capture may not be the
2202 * same as the host byte order on this machine.
2204 * For DLT_LOOP, the link-layer header is a 32-bit
2205 * word containing an AF_ value in *network* byte order.
2207 * XXX - AF_ values may, unfortunately, be platform-
2208 * dependent; for example, FreeBSD's AF_INET6 is 24
2209 * whilst NetBSD's and OpenBSD's is 26.
2211 * This means that, when reading a capture file, just
2212 * checking for our AF_INET6 value won't work if the
2213 * capture file came from another OS.
2222 case ETHERTYPE_IPV6
:
2229 * Not a type on which we support filtering.
2230 * XXX - support those that have AF_ values
2231 * #defined on this platform, at least?
2236 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2238 * The AF_ value is in host byte order, but
2239 * the BPF interpreter will convert it to
2240 * network byte order.
2242 * If this is a save file, and it's from a
2243 * machine with the opposite byte order to
2244 * ours, we byte-swap the AF_ value.
2246 * Then we run it through "htonl()", and
2247 * generate code to compare against the result.
2249 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2250 bpf_pcap
->sf
.swapped
)
2251 proto
= SWAPLONG(proto
);
2252 proto
= htonl(proto
);
2254 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2258 * af field is host byte order in contrast to the rest of
2261 if (proto
== ETHERTYPE_IP
)
2262 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2263 BPF_B
, (bpf_int32
)AF_INET
));
2265 else if (proto
== ETHERTYPE_IPV6
)
2266 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2267 BPF_B
, (bpf_int32
)AF_INET6
));
2275 case DLT_ARCNET_LINUX
:
2277 * XXX should we check for first fragment if the protocol
2286 case ETHERTYPE_IPV6
:
2287 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2288 (bpf_int32
)ARCTYPE_INET6
));
2292 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2293 (bpf_int32
)ARCTYPE_IP
);
2294 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2295 (bpf_int32
)ARCTYPE_IP_OLD
);
2300 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2301 (bpf_int32
)ARCTYPE_ARP
);
2302 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2303 (bpf_int32
)ARCTYPE_ARP_OLD
);
2307 case ETHERTYPE_REVARP
:
2308 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2309 (bpf_int32
)ARCTYPE_REVARP
));
2311 case ETHERTYPE_ATALK
:
2312 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2313 (bpf_int32
)ARCTYPE_ATALK
));
2320 case ETHERTYPE_ATALK
:
2330 * XXX - assumes a 2-byte Frame Relay header with
2331 * DLCI and flags. What if the address is longer?
2337 * Check for the special NLPID for IP.
2339 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2342 case ETHERTYPE_IPV6
:
2344 * Check for the special NLPID for IPv6.
2346 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2351 * Check for several OSI protocols.
2353 * Frame Relay packets typically have an OSI
2354 * NLPID at the beginning; we check for each
2357 * What we check for is the NLPID and a frame
2358 * control field of UI, i.e. 0x03 followed
2361 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2362 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2363 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2374 case DLT_JUNIPER_MFR
:
2375 case DLT_JUNIPER_MLFR
:
2376 case DLT_JUNIPER_MLPPP
:
2377 case DLT_JUNIPER_ATM1
:
2378 case DLT_JUNIPER_ATM2
:
2379 case DLT_JUNIPER_PPPOE
:
2380 case DLT_JUNIPER_PPPOE_ATM
:
2381 case DLT_JUNIPER_GGSN
:
2382 case DLT_JUNIPER_ES
:
2383 case DLT_JUNIPER_MONITOR
:
2384 case DLT_JUNIPER_SERVICES
:
2385 case DLT_JUNIPER_ETHER
:
2386 case DLT_JUNIPER_PPP
:
2387 case DLT_JUNIPER_FRELAY
:
2388 case DLT_JUNIPER_CHDLC
:
2389 case DLT_JUNIPER_VP
:
2390 case DLT_JUNIPER_ISM
:
2391 /* just lets verify the magic number for now -
2392 * on ATM we may have up to 6 different encapsulations on the wire
2393 * and need a lot of heuristics to figure out that the payload
2396 * FIXME encapsulation specific BPF_ filters
2398 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2400 case DLT_LINUX_IRDA
:
2401 bpf_error("IrDA link-layer type filtering not implemented");
2404 bpf_error("DOCSIS link-layer type filtering not implemented");
2406 case DLT_LINUX_LAPD
:
2407 bpf_error("LAPD link-layer type filtering not implemented");
2411 * All the types that have no encapsulation should either be
2412 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2413 * all packets are IP packets, or should be handled in some
2414 * special case, if none of them are (if some are and some
2415 * aren't, the lack of encapsulation is a problem, as we'd
2416 * have to find some other way of determining the packet type).
2418 * Therefore, if "off_linktype" is -1, there's an error.
2420 if (off_linktype
== (u_int
)-1)
2424 * Any type not handled above should always have an Ethernet
2425 * type at an offset of "off_linktype". (PPP is partially
2426 * handled above - the protocol type is mapped from the
2427 * Ethernet and LLC types we use internally to the corresponding
2428 * PPP type - but the PPP type is always specified by a value
2429 * at "off_linktype", so we don't have to do the code generation
2432 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2436 * Check for an LLC SNAP packet with a given organization code and
2437 * protocol type; we check the entire contents of the 802.2 LLC and
2438 * snap headers, checking for DSAP and SSAP of SNAP and a control
2439 * field of 0x03 in the LLC header, and for the specified organization
2440 * code and protocol type in the SNAP header.
2442 static struct block
*
2443 gen_snap(orgcode
, ptype
, offset
)
2444 bpf_u_int32 orgcode
;
2448 u_char snapblock
[8];
2450 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2451 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2452 snapblock
[2] = 0x03; /* control = UI */
2453 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2454 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2455 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2456 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2457 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2458 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2462 * Generate code to match a particular packet type, for link-layer types
2463 * using 802.2 LLC headers.
2465 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2466 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2468 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2469 * value, if <= ETHERMTU. We use that to determine whether to
2470 * match the DSAP or both DSAP and LSAP or to check the OUI and
2471 * protocol ID in a SNAP header.
2473 static struct block
*
2474 gen_llc_linktype(proto
)
2478 * XXX - handle token-ring variable-length header.
2484 case LLCSAP_NETBEUI
:
2486 * XXX - should we check both the DSAP and the
2487 * SSAP, like this, or should we check just the
2488 * DSAP, as we do for other types <= ETHERMTU
2489 * (i.e., other SAP values)?
2491 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2492 ((proto
<< 8) | proto
));
2496 * XXX - are there ever SNAP frames for IPX on
2497 * non-Ethernet 802.x networks?
2499 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2500 (bpf_int32
)LLCSAP_IPX
);
2502 case ETHERTYPE_ATALK
:
2504 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2505 * SNAP packets with an organization code of
2506 * 0x080007 (Apple, for Appletalk) and a protocol
2507 * type of ETHERTYPE_ATALK (Appletalk).
2509 * XXX - check for an organization code of
2510 * encapsulated Ethernet as well?
2512 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2516 * XXX - we don't have to check for IPX 802.3
2517 * here, but should we check for the IPX Ethertype?
2519 if (proto
<= ETHERMTU
) {
2521 * This is an LLC SAP value, so check
2524 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2528 * This is an Ethernet type; we assume that it's
2529 * unlikely that it'll appear in the right place
2530 * at random, and therefore check only the
2531 * location that would hold the Ethernet type
2532 * in a SNAP frame with an organization code of
2533 * 0x000000 (encapsulated Ethernet).
2535 * XXX - if we were to check for the SNAP DSAP and
2536 * LSAP, as per XXX, and were also to check for an
2537 * organization code of 0x000000 (encapsulated
2538 * Ethernet), we'd do
2540 * return gen_snap(0x000000, proto,
2543 * here; for now, we don't, as per the above.
2544 * I don't know whether it's worth the extra CPU
2545 * time to do the right check or not.
2547 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2553 static struct block
*
2554 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2558 u_int src_off
, dst_off
;
2560 struct block
*b0
, *b1
;
2574 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2575 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2581 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2582 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2589 b0
= gen_linktype(proto
);
2590 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2596 static struct block
*
2597 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2598 struct in6_addr
*addr
;
2599 struct in6_addr
*mask
;
2601 u_int src_off
, dst_off
;
2603 struct block
*b0
, *b1
;
2618 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2619 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2625 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2626 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2633 /* this order is important */
2634 a
= (u_int32_t
*)addr
;
2635 m
= (u_int32_t
*)mask
;
2636 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2637 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2639 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2641 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2643 b0
= gen_linktype(proto
);
2649 static struct block
*
2650 gen_ehostop(eaddr
, dir
)
2651 register const u_char
*eaddr
;
2654 register struct block
*b0
, *b1
;
2658 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2661 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2664 b0
= gen_ehostop(eaddr
, Q_SRC
);
2665 b1
= gen_ehostop(eaddr
, Q_DST
);
2671 b0
= gen_ehostop(eaddr
, Q_SRC
);
2672 b1
= gen_ehostop(eaddr
, Q_DST
);
2681 * Like gen_ehostop, but for DLT_FDDI
2683 static struct block
*
2684 gen_fhostop(eaddr
, dir
)
2685 register const u_char
*eaddr
;
2688 struct block
*b0
, *b1
;
2693 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2695 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2700 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2702 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2706 b0
= gen_fhostop(eaddr
, Q_SRC
);
2707 b1
= gen_fhostop(eaddr
, Q_DST
);
2713 b0
= gen_fhostop(eaddr
, Q_SRC
);
2714 b1
= gen_fhostop(eaddr
, Q_DST
);
2723 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2725 static struct block
*
2726 gen_thostop(eaddr
, dir
)
2727 register const u_char
*eaddr
;
2730 register struct block
*b0
, *b1
;
2734 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2737 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2740 b0
= gen_thostop(eaddr
, Q_SRC
);
2741 b1
= gen_thostop(eaddr
, Q_DST
);
2747 b0
= gen_thostop(eaddr
, Q_SRC
);
2748 b1
= gen_thostop(eaddr
, Q_DST
);
2757 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2759 static struct block
*
2760 gen_wlanhostop(eaddr
, dir
)
2761 register const u_char
*eaddr
;
2764 register struct block
*b0
, *b1
, *b2
;
2765 register struct slist
*s
;
2772 * For control frames, there is no SA.
2774 * For management frames, SA is at an
2775 * offset of 10 from the beginning of
2778 * For data frames, SA is at an offset
2779 * of 10 from the beginning of the packet
2780 * if From DS is clear, at an offset of
2781 * 16 from the beginning of the packet
2782 * if From DS is set and To DS is clear,
2783 * and an offset of 24 from the beginning
2784 * of the packet if From DS is set and To DS
2789 * Generate the tests to be done for data frames
2792 * First, check for To DS set, i.e. check "link[1] & 0x01".
2794 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2795 b1
= new_block(JMP(BPF_JSET
));
2796 b1
->s
.k
= 0x01; /* To DS */
2800 * If To DS is set, the SA is at 24.
2802 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2806 * Now, check for To DS not set, i.e. check
2807 * "!(link[1] & 0x01)".
2809 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2810 b2
= new_block(JMP(BPF_JSET
));
2811 b2
->s
.k
= 0x01; /* To DS */
2816 * If To DS is not set, the SA is at 16.
2818 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2822 * Now OR together the last two checks. That gives
2823 * the complete set of checks for data frames with
2829 * Now check for From DS being set, and AND that with
2830 * the ORed-together checks.
2832 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2833 b1
= new_block(JMP(BPF_JSET
));
2834 b1
->s
.k
= 0x02; /* From DS */
2839 * Now check for data frames with From DS not set.
2841 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2842 b2
= new_block(JMP(BPF_JSET
));
2843 b2
->s
.k
= 0x02; /* From DS */
2848 * If From DS isn't set, the SA is at 10.
2850 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2854 * Now OR together the checks for data frames with
2855 * From DS not set and for data frames with From DS
2856 * set; that gives the checks done for data frames.
2861 * Now check for a data frame.
2862 * I.e, check "link[0] & 0x08".
2864 gen_load_a(OR_LINK
, 0, BPF_B
);
2865 b1
= new_block(JMP(BPF_JSET
));
2870 * AND that with the checks done for data frames.
2875 * If the high-order bit of the type value is 0, this
2876 * is a management frame.
2877 * I.e, check "!(link[0] & 0x08)".
2879 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2880 b2
= new_block(JMP(BPF_JSET
));
2886 * For management frames, the SA is at 10.
2888 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2892 * OR that with the checks done for data frames.
2893 * That gives the checks done for management and
2899 * If the low-order bit of the type value is 1,
2900 * this is either a control frame or a frame
2901 * with a reserved type, and thus not a
2904 * I.e., check "!(link[0] & 0x04)".
2906 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2907 b1
= new_block(JMP(BPF_JSET
));
2913 * AND that with the checks for data and management
2923 * For control frames, there is no DA.
2925 * For management frames, DA is at an
2926 * offset of 4 from the beginning of
2929 * For data frames, DA is at an offset
2930 * of 4 from the beginning of the packet
2931 * if To DS is clear and at an offset of
2932 * 16 from the beginning of the packet
2937 * Generate the tests to be done for data frames.
2939 * First, check for To DS set, i.e. "link[1] & 0x01".
2941 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2942 b1
= new_block(JMP(BPF_JSET
));
2943 b1
->s
.k
= 0x01; /* To DS */
2947 * If To DS is set, the DA is at 16.
2949 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2953 * Now, check for To DS not set, i.e. check
2954 * "!(link[1] & 0x01)".
2956 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2957 b2
= new_block(JMP(BPF_JSET
));
2958 b2
->s
.k
= 0x01; /* To DS */
2963 * If To DS is not set, the DA is at 4.
2965 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2969 * Now OR together the last two checks. That gives
2970 * the complete set of checks for data frames.
2975 * Now check for a data frame.
2976 * I.e, check "link[0] & 0x08".
2978 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2979 b1
= new_block(JMP(BPF_JSET
));
2984 * AND that with the checks done for data frames.
2989 * If the high-order bit of the type value is 0, this
2990 * is a management frame.
2991 * I.e, check "!(link[0] & 0x08)".
2993 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2994 b2
= new_block(JMP(BPF_JSET
));
3000 * For management frames, the DA is at 4.
3002 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3006 * OR that with the checks done for data frames.
3007 * That gives the checks done for management and
3013 * If the low-order bit of the type value is 1,
3014 * this is either a control frame or a frame
3015 * with a reserved type, and thus not a
3018 * I.e., check "!(link[0] & 0x04)".
3020 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3021 b1
= new_block(JMP(BPF_JSET
));
3027 * AND that with the checks for data and management
3034 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3035 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3041 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3042 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3051 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3052 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3053 * as the RFC states.)
3055 static struct block
*
3056 gen_ipfchostop(eaddr
, dir
)
3057 register const u_char
*eaddr
;
3060 register struct block
*b0
, *b1
;
3064 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3067 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3070 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3071 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3077 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3078 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3087 * This is quite tricky because there may be pad bytes in front of the
3088 * DECNET header, and then there are two possible data packet formats that
3089 * carry both src and dst addresses, plus 5 packet types in a format that
3090 * carries only the src node, plus 2 types that use a different format and
3091 * also carry just the src node.
3095 * Instead of doing those all right, we just look for data packets with
3096 * 0 or 1 bytes of padding. If you want to look at other packets, that
3097 * will require a lot more hacking.
3099 * To add support for filtering on DECNET "areas" (network numbers)
3100 * one would want to add a "mask" argument to this routine. That would
3101 * make the filter even more inefficient, although one could be clever
3102 * and not generate masking instructions if the mask is 0xFFFF.
3104 static struct block
*
3105 gen_dnhostop(addr
, dir
)
3109 struct block
*b0
, *b1
, *b2
, *tmp
;
3110 u_int offset_lh
; /* offset if long header is received */
3111 u_int offset_sh
; /* offset if short header is received */
3116 offset_sh
= 1; /* follows flags */
3117 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3121 offset_sh
= 3; /* follows flags, dstnode */
3122 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3126 /* Inefficient because we do our Calvinball dance twice */
3127 b0
= gen_dnhostop(addr
, Q_SRC
);
3128 b1
= gen_dnhostop(addr
, Q_DST
);
3134 /* Inefficient because we do our Calvinball dance twice */
3135 b0
= gen_dnhostop(addr
, Q_SRC
);
3136 b1
= gen_dnhostop(addr
, Q_DST
);
3141 bpf_error("ISO host filtering not implemented");
3146 b0
= gen_linktype(ETHERTYPE_DN
);
3147 /* Check for pad = 1, long header case */
3148 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3149 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3150 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3151 BPF_H
, (bpf_int32
)ntohs(addr
));
3153 /* Check for pad = 0, long header case */
3154 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3155 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3158 /* Check for pad = 1, short header case */
3159 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3160 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3161 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3164 /* Check for pad = 0, short header case */
3165 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3166 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3170 /* Combine with test for linktype */
3176 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3177 * test the bottom-of-stack bit, and then check the version number
3178 * field in the IP header.
3180 static struct block
*
3181 gen_mpls_linktype(proto
)
3184 struct block
*b0
, *b1
;
3189 /* match the bottom-of-stack bit */
3190 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3191 /* match the IPv4 version number */
3192 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3197 /* match the bottom-of-stack bit */
3198 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3199 /* match the IPv4 version number */
3200 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3209 static struct block
*
3210 gen_host(addr
, mask
, proto
, dir
, type
)
3217 struct block
*b0
, *b1
;
3218 const char *typestr
;
3228 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3230 * Only check for non-IPv4 addresses if we're not
3231 * checking MPLS-encapsulated packets.
3233 if (label_stack_depth
== 0) {
3234 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3236 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3242 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3245 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3248 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3251 bpf_error("'tcp' modifier applied to %s", typestr
);
3254 bpf_error("'sctp' modifier applied to %s", typestr
);
3257 bpf_error("'udp' modifier applied to %s", typestr
);
3260 bpf_error("'icmp' modifier applied to %s", typestr
);
3263 bpf_error("'igmp' modifier applied to %s", typestr
);
3266 bpf_error("'igrp' modifier applied to %s", typestr
);
3269 bpf_error("'pim' modifier applied to %s", typestr
);
3272 bpf_error("'vrrp' modifier applied to %s", typestr
);
3275 bpf_error("ATALK host filtering not implemented");
3278 bpf_error("AARP host filtering not implemented");
3281 return gen_dnhostop(addr
, dir
);
3284 bpf_error("SCA host filtering not implemented");
3287 bpf_error("LAT host filtering not implemented");
3290 bpf_error("MOPDL host filtering not implemented");
3293 bpf_error("MOPRC host filtering not implemented");
3297 bpf_error("'ip6' modifier applied to ip host");
3300 bpf_error("'icmp6' modifier applied to %s", typestr
);
3304 bpf_error("'ah' modifier applied to %s", typestr
);
3307 bpf_error("'esp' modifier applied to %s", typestr
);
3310 bpf_error("ISO host filtering not implemented");
3313 bpf_error("'esis' modifier applied to %s", typestr
);
3316 bpf_error("'isis' modifier applied to %s", typestr
);
3319 bpf_error("'clnp' modifier applied to %s", typestr
);
3322 bpf_error("'stp' modifier applied to %s", typestr
);
3325 bpf_error("IPX host filtering not implemented");
3328 bpf_error("'netbeui' modifier applied to %s", typestr
);
3331 bpf_error("'radio' modifier applied to %s", typestr
);
3340 static struct block
*
3341 gen_host6(addr
, mask
, proto
, dir
, type
)
3342 struct in6_addr
*addr
;
3343 struct in6_addr
*mask
;
3348 const char *typestr
;
3358 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3361 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3364 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3367 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3370 bpf_error("'sctp' modifier applied to %s", typestr
);
3373 bpf_error("'tcp' modifier applied to %s", typestr
);
3376 bpf_error("'udp' modifier applied to %s", typestr
);
3379 bpf_error("'icmp' modifier applied to %s", typestr
);
3382 bpf_error("'igmp' modifier applied to %s", typestr
);
3385 bpf_error("'igrp' modifier applied to %s", typestr
);
3388 bpf_error("'pim' modifier applied to %s", typestr
);
3391 bpf_error("'vrrp' modifier applied to %s", typestr
);
3394 bpf_error("ATALK host filtering not implemented");
3397 bpf_error("AARP host filtering not implemented");
3400 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3403 bpf_error("SCA host filtering not implemented");
3406 bpf_error("LAT host filtering not implemented");
3409 bpf_error("MOPDL host filtering not implemented");
3412 bpf_error("MOPRC host filtering not implemented");
3415 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3418 bpf_error("'icmp6' modifier applied to %s", typestr
);
3421 bpf_error("'ah' modifier applied to %s", typestr
);
3424 bpf_error("'esp' modifier applied to %s", typestr
);
3427 bpf_error("ISO host filtering not implemented");
3430 bpf_error("'esis' modifier applied to %s", typestr
);
3433 bpf_error("'isis' modifier applied to %s", typestr
);
3436 bpf_error("'clnp' modifier applied to %s", typestr
);
3439 bpf_error("'stp' modifier applied to %s", typestr
);
3442 bpf_error("IPX host filtering not implemented");
3445 bpf_error("'netbeui' modifier applied to %s", typestr
);
3448 bpf_error("'radio' modifier applied to %s", typestr
);
3458 static struct block
*
3459 gen_gateway(eaddr
, alist
, proto
, dir
)
3460 const u_char
*eaddr
;
3461 bpf_u_int32
**alist
;
3465 struct block
*b0
, *b1
, *tmp
;
3468 bpf_error("direction applied to 'gateway'");
3477 b0
= gen_ehostop(eaddr
, Q_OR
);
3480 b0
= gen_fhostop(eaddr
, Q_OR
);
3483 b0
= gen_thostop(eaddr
, Q_OR
);
3485 case DLT_IEEE802_11
:
3486 case DLT_IEEE802_11_RADIO_AVS
:
3487 case DLT_IEEE802_11_RADIO
:
3488 case DLT_PRISM_HEADER
:
3489 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3494 * Check that the packet doesn't begin with an
3495 * LE Control marker. (We've already generated
3498 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3503 * Now check the MAC address.
3505 b0
= gen_ehostop(eaddr
, Q_OR
);
3509 case DLT_IP_OVER_FC
:
3510 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3514 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3516 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3518 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3527 bpf_error("illegal modifier of 'gateway'");
3533 gen_proto_abbrev(proto
)
3542 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3544 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3550 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3552 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3558 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3560 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3566 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3569 #ifndef IPPROTO_IGMP
3570 #define IPPROTO_IGMP 2
3574 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3577 #ifndef IPPROTO_IGRP
3578 #define IPPROTO_IGRP 9
3581 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3585 #define IPPROTO_PIM 103
3589 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3591 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3596 #ifndef IPPROTO_VRRP
3597 #define IPPROTO_VRRP 112
3601 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3605 b1
= gen_linktype(ETHERTYPE_IP
);
3609 b1
= gen_linktype(ETHERTYPE_ARP
);
3613 b1
= gen_linktype(ETHERTYPE_REVARP
);
3617 bpf_error("link layer applied in wrong context");
3620 b1
= gen_linktype(ETHERTYPE_ATALK
);
3624 b1
= gen_linktype(ETHERTYPE_AARP
);
3628 b1
= gen_linktype(ETHERTYPE_DN
);
3632 b1
= gen_linktype(ETHERTYPE_SCA
);
3636 b1
= gen_linktype(ETHERTYPE_LAT
);
3640 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3644 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3649 b1
= gen_linktype(ETHERTYPE_IPV6
);
3652 #ifndef IPPROTO_ICMPV6
3653 #define IPPROTO_ICMPV6 58
3656 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3661 #define IPPROTO_AH 51
3664 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3666 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3672 #define IPPROTO_ESP 50
3675 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3677 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3683 b1
= gen_linktype(LLCSAP_ISONS
);
3687 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3691 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3694 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3695 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3696 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3698 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3700 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3702 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3706 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3707 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3708 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3710 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3712 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3714 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3718 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3719 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3720 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3722 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3727 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3728 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3733 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3734 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3736 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3738 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3743 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3744 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3749 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3750 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3755 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3759 b1
= gen_linktype(LLCSAP_8021D
);
3763 b1
= gen_linktype(LLCSAP_IPX
);
3767 b1
= gen_linktype(LLCSAP_NETBEUI
);
3771 bpf_error("'radio' is not a valid protocol type");
3779 static struct block
*
3786 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3787 b
= new_block(JMP(BPF_JSET
));
3796 * Generate a comparison to a port value in the transport-layer header
3797 * at the specified offset from the beginning of that header.
3799 * XXX - this handles a variable-length prefix preceding the link-layer
3800 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3801 * variable-length link-layer headers (such as Token Ring or 802.11
3804 static struct block
*
3805 gen_portatom(off
, v
)
3809 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3813 static struct block
*
3814 gen_portatom6(off
, v
)
3818 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3823 gen_portop(port
, proto
, dir
)
3824 int port
, proto
, dir
;
3826 struct block
*b0
, *b1
, *tmp
;
3828 /* ip proto 'proto' */
3829 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3835 b1
= gen_portatom(0, (bpf_int32
)port
);
3839 b1
= gen_portatom(2, (bpf_int32
)port
);
3844 tmp
= gen_portatom(0, (bpf_int32
)port
);
3845 b1
= gen_portatom(2, (bpf_int32
)port
);
3850 tmp
= gen_portatom(0, (bpf_int32
)port
);
3851 b1
= gen_portatom(2, (bpf_int32
)port
);
3863 static struct block
*
3864 gen_port(port
, ip_proto
, dir
)
3869 struct block
*b0
, *b1
, *tmp
;
3874 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3875 * not LLC encapsulation with LLCSAP_IP.
3877 * For IEEE 802 networks - which includes 802.5 token ring
3878 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3879 * says that SNAP encapsulation is used, not LLC encapsulation
3882 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3883 * RFC 2225 say that SNAP encapsulation is used, not LLC
3884 * encapsulation with LLCSAP_IP.
3886 * So we always check for ETHERTYPE_IP.
3888 b0
= gen_linktype(ETHERTYPE_IP
);
3894 b1
= gen_portop(port
, ip_proto
, dir
);
3898 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3899 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3901 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3914 gen_portop6(port
, proto
, dir
)
3915 int port
, proto
, dir
;
3917 struct block
*b0
, *b1
, *tmp
;
3919 /* ip6 proto 'proto' */
3920 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3924 b1
= gen_portatom6(0, (bpf_int32
)port
);
3928 b1
= gen_portatom6(2, (bpf_int32
)port
);
3933 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3934 b1
= gen_portatom6(2, (bpf_int32
)port
);
3939 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3940 b1
= gen_portatom6(2, (bpf_int32
)port
);
3952 static struct block
*
3953 gen_port6(port
, ip_proto
, dir
)
3958 struct block
*b0
, *b1
, *tmp
;
3960 /* link proto ip6 */
3961 b0
= gen_linktype(ETHERTYPE_IPV6
);
3967 b1
= gen_portop6(port
, ip_proto
, dir
);
3971 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3972 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3974 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3986 /* gen_portrange code */
3987 static struct block
*
3988 gen_portrangeatom(off
, v1
, v2
)
3992 struct block
*b1
, *b2
;
3996 * Reverse the order of the ports, so v1 is the lower one.
4005 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4006 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4014 gen_portrangeop(port1
, port2
, proto
, dir
)
4019 struct block
*b0
, *b1
, *tmp
;
4021 /* ip proto 'proto' */
4022 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4028 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4032 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4037 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4038 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4043 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4044 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4056 static struct block
*
4057 gen_portrange(port1
, port2
, ip_proto
, dir
)
4062 struct block
*b0
, *b1
, *tmp
;
4065 b0
= gen_linktype(ETHERTYPE_IP
);
4071 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4075 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4076 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4078 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4090 static struct block
*
4091 gen_portrangeatom6(off
, v1
, v2
)
4095 struct block
*b1
, *b2
;
4099 * Reverse the order of the ports, so v1 is the lower one.
4108 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4109 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4117 gen_portrangeop6(port1
, port2
, proto
, dir
)
4122 struct block
*b0
, *b1
, *tmp
;
4124 /* ip6 proto 'proto' */
4125 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4129 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4133 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4138 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4139 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4144 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4145 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4157 static struct block
*
4158 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4163 struct block
*b0
, *b1
, *tmp
;
4165 /* link proto ip6 */
4166 b0
= gen_linktype(ETHERTYPE_IPV6
);
4172 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4176 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4177 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4179 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4192 lookup_proto(name
, proto
)
4193 register const char *name
;
4203 v
= pcap_nametoproto(name
);
4204 if (v
== PROTO_UNDEF
)
4205 bpf_error("unknown ip proto '%s'", name
);
4209 /* XXX should look up h/w protocol type based on linktype */
4210 v
= pcap_nametoeproto(name
);
4211 if (v
== PROTO_UNDEF
) {
4212 v
= pcap_nametollc(name
);
4213 if (v
== PROTO_UNDEF
)
4214 bpf_error("unknown ether proto '%s'", name
);
4219 if (strcmp(name
, "esis") == 0)
4221 else if (strcmp(name
, "isis") == 0)
4223 else if (strcmp(name
, "clnp") == 0)
4226 bpf_error("unknown osi proto '%s'", name
);
4246 static struct block
*
4247 gen_protochain(v
, proto
, dir
)
4252 #ifdef NO_PROTOCHAIN
4253 return gen_proto(v
, proto
, dir
);
4255 struct block
*b0
, *b
;
4256 struct slist
*s
[100];
4257 int fix2
, fix3
, fix4
, fix5
;
4258 int ahcheck
, again
, end
;
4260 int reg2
= alloc_reg();
4262 memset(s
, 0, sizeof(s
));
4263 fix2
= fix3
= fix4
= fix5
= 0;
4270 b0
= gen_protochain(v
, Q_IP
, dir
);
4271 b
= gen_protochain(v
, Q_IPV6
, dir
);
4275 bpf_error("bad protocol applied for 'protochain'");
4280 * We don't handle variable-length radiotap here headers yet.
4281 * We might want to add BPF instructions to do the protochain
4282 * work, to simplify that and, on platforms that have a BPF
4283 * interpreter with the new instructions, let the filtering
4284 * be done in the kernel. (We already require a modified BPF
4285 * engine to do the protochain stuff, to support backward
4286 * branches, and backward branch support is unlikely to appear
4287 * in kernel BPF engines.)
4289 if (linktype
== DLT_IEEE802_11_RADIO
)
4290 bpf_error("'protochain' not supported with radiotap headers");
4292 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4295 * s[0] is a dummy entry to protect other BPF insn from damage
4296 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4297 * hard to find interdependency made by jump table fixup.
4300 s
[i
] = new_stmt(0); /*dummy*/
4305 b0
= gen_linktype(ETHERTYPE_IP
);
4308 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4309 s
[i
]->s
.k
= off_ll
+ off_nl
+ 9;
4311 /* X = ip->ip_hl << 2 */
4312 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4313 s
[i
]->s
.k
= off_ll
+ off_nl
;
4318 b0
= gen_linktype(ETHERTYPE_IPV6
);
4320 /* A = ip6->ip_nxt */
4321 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4322 s
[i
]->s
.k
= off_ll
+ off_nl
+ 6;
4324 /* X = sizeof(struct ip6_hdr) */
4325 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4331 bpf_error("unsupported proto to gen_protochain");
4335 /* again: if (A == v) goto end; else fall through; */
4337 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4339 s
[i
]->s
.jt
= NULL
; /*later*/
4340 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4344 #ifndef IPPROTO_NONE
4345 #define IPPROTO_NONE 59
4347 /* if (A == IPPROTO_NONE) goto end */
4348 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4349 s
[i
]->s
.jt
= NULL
; /*later*/
4350 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4351 s
[i
]->s
.k
= IPPROTO_NONE
;
4352 s
[fix5
]->s
.jf
= s
[i
];
4357 if (proto
== Q_IPV6
) {
4358 int v6start
, v6end
, v6advance
, j
;
4361 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4362 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4363 s
[i
]->s
.jt
= NULL
; /*later*/
4364 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4365 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4366 s
[fix2
]->s
.jf
= s
[i
];
4368 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4369 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4370 s
[i
]->s
.jt
= NULL
; /*later*/
4371 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4372 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4374 /* if (A == IPPROTO_ROUTING) goto v6advance */
4375 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4376 s
[i
]->s
.jt
= NULL
; /*later*/
4377 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4378 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4380 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4381 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4382 s
[i
]->s
.jt
= NULL
; /*later*/
4383 s
[i
]->s
.jf
= NULL
; /*later*/
4384 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4395 * X = X + (P[X + 1] + 1) * 8;
4398 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4400 /* A = P[X + packet head] */
4401 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4402 s
[i
]->s
.k
= off_ll
+ off_nl
;
4405 s
[i
] = new_stmt(BPF_ST
);
4409 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4412 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4416 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4418 /* A = P[X + packet head]; */
4419 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4420 s
[i
]->s
.k
= off_ll
+ off_nl
;
4423 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4427 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4431 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4434 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4438 /* goto again; (must use BPF_JA for backward jump) */
4439 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4440 s
[i
]->s
.k
= again
- i
- 1;
4441 s
[i
- 1]->s
.jf
= s
[i
];
4445 for (j
= v6start
; j
<= v6end
; j
++)
4446 s
[j
]->s
.jt
= s
[v6advance
];
4451 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4453 s
[fix2
]->s
.jf
= s
[i
];
4459 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4460 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4461 s
[i
]->s
.jt
= NULL
; /*later*/
4462 s
[i
]->s
.jf
= NULL
; /*later*/
4463 s
[i
]->s
.k
= IPPROTO_AH
;
4465 s
[fix3
]->s
.jf
= s
[ahcheck
];
4472 * X = X + (P[X + 1] + 2) * 4;
4475 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4477 /* A = P[X + packet head]; */
4478 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4479 s
[i
]->s
.k
= off_ll
+ off_nl
;
4482 s
[i
] = new_stmt(BPF_ST
);
4486 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4489 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4493 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4495 /* A = P[X + packet head] */
4496 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4497 s
[i
]->s
.k
= off_ll
+ off_nl
;
4500 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4504 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4508 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4511 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4515 /* goto again; (must use BPF_JA for backward jump) */
4516 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4517 s
[i
]->s
.k
= again
- i
- 1;
4522 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4524 s
[fix2
]->s
.jt
= s
[end
];
4525 s
[fix4
]->s
.jf
= s
[end
];
4526 s
[fix5
]->s
.jt
= s
[end
];
4533 for (i
= 0; i
< max
- 1; i
++)
4534 s
[i
]->next
= s
[i
+ 1];
4535 s
[max
- 1]->next
= NULL
;
4540 b
= new_block(JMP(BPF_JEQ
));
4541 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4552 * Generate code that checks whether the packet is a packet for protocol
4553 * <proto> and whether the type field in that protocol's header has
4554 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4555 * IP packet and checks the protocol number in the IP header against <v>.
4557 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4558 * against Q_IP and Q_IPV6.
4560 static struct block
*
4561 gen_proto(v
, proto
, dir
)
4566 struct block
*b0
, *b1
;
4568 if (dir
!= Q_DEFAULT
)
4569 bpf_error("direction applied to 'proto'");
4574 b0
= gen_proto(v
, Q_IP
, dir
);
4575 b1
= gen_proto(v
, Q_IPV6
, dir
);
4583 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4584 * not LLC encapsulation with LLCSAP_IP.
4586 * For IEEE 802 networks - which includes 802.5 token ring
4587 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4588 * says that SNAP encapsulation is used, not LLC encapsulation
4591 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4592 * RFC 2225 say that SNAP encapsulation is used, not LLC
4593 * encapsulation with LLCSAP_IP.
4595 * So we always check for ETHERTYPE_IP.
4597 b0
= gen_linktype(ETHERTYPE_IP
);
4599 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4601 b1
= gen_protochain(v
, Q_IP
);
4611 * Frame Relay packets typically have an OSI
4612 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4613 * generates code to check for all the OSI
4614 * NLPIDs, so calling it and then adding a check
4615 * for the particular NLPID for which we're
4616 * looking is bogus, as we can just check for
4619 * What we check for is the NLPID and a frame
4620 * control field value of UI, i.e. 0x03 followed
4623 * XXX - assumes a 2-byte Frame Relay header with
4624 * DLCI and flags. What if the address is longer?
4626 * XXX - what about SNAP-encapsulated frames?
4628 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4634 * Cisco uses an Ethertype lookalike - for OSI,
4637 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4638 /* OSI in C-HDLC is stuffed with a fudge byte */
4639 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4644 b0
= gen_linktype(LLCSAP_ISONS
);
4645 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4651 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4653 * 4 is the offset of the PDU type relative to the IS-IS
4656 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4661 bpf_error("arp does not encapsulate another protocol");
4665 bpf_error("rarp does not encapsulate another protocol");
4669 bpf_error("atalk encapsulation is not specifiable");
4673 bpf_error("decnet encapsulation is not specifiable");
4677 bpf_error("sca does not encapsulate another protocol");
4681 bpf_error("lat does not encapsulate another protocol");
4685 bpf_error("moprc does not encapsulate another protocol");
4689 bpf_error("mopdl does not encapsulate another protocol");
4693 return gen_linktype(v
);
4696 bpf_error("'udp proto' is bogus");
4700 bpf_error("'tcp proto' is bogus");
4704 bpf_error("'sctp proto' is bogus");
4708 bpf_error("'icmp proto' is bogus");
4712 bpf_error("'igmp proto' is bogus");
4716 bpf_error("'igrp proto' is bogus");
4720 bpf_error("'pim proto' is bogus");
4724 bpf_error("'vrrp proto' is bogus");
4729 b0
= gen_linktype(ETHERTYPE_IPV6
);
4731 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4733 b1
= gen_protochain(v
, Q_IPV6
);
4739 bpf_error("'icmp6 proto' is bogus");
4743 bpf_error("'ah proto' is bogus");
4746 bpf_error("'ah proto' is bogus");
4749 bpf_error("'stp proto' is bogus");
4752 bpf_error("'ipx proto' is bogus");
4755 bpf_error("'netbeui proto' is bogus");
4758 bpf_error("'radio proto' is bogus");
4769 register const char *name
;
4772 int proto
= q
.proto
;
4776 bpf_u_int32 mask
, addr
;
4778 bpf_u_int32
**alist
;
4781 struct sockaddr_in
*sin
;
4782 struct sockaddr_in6
*sin6
;
4783 struct addrinfo
*res
, *res0
;
4784 struct in6_addr mask128
;
4786 struct block
*b
, *tmp
;
4787 int port
, real_proto
;
4793 addr
= pcap_nametonetaddr(name
);
4795 bpf_error("unknown network '%s'", name
);
4796 /* Left justify network addr and calculate its network mask */
4798 while (addr
&& (addr
& 0xff000000) == 0) {
4802 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
4806 if (proto
== Q_LINK
) {
4810 eaddr
= pcap_ether_hostton(name
);
4813 "unknown ether host '%s'", name
);
4814 b
= gen_ehostop(eaddr
, dir
);
4819 eaddr
= pcap_ether_hostton(name
);
4822 "unknown FDDI host '%s'", name
);
4823 b
= gen_fhostop(eaddr
, dir
);
4828 eaddr
= pcap_ether_hostton(name
);
4831 "unknown token ring host '%s'", name
);
4832 b
= gen_thostop(eaddr
, dir
);
4836 case DLT_IEEE802_11
:
4837 case DLT_IEEE802_11_RADIO_AVS
:
4838 case DLT_IEEE802_11_RADIO
:
4839 case DLT_PRISM_HEADER
:
4840 eaddr
= pcap_ether_hostton(name
);
4843 "unknown 802.11 host '%s'", name
);
4844 b
= gen_wlanhostop(eaddr
, dir
);
4848 case DLT_IP_OVER_FC
:
4849 eaddr
= pcap_ether_hostton(name
);
4852 "unknown Fibre Channel host '%s'", name
);
4853 b
= gen_ipfchostop(eaddr
, dir
);
4862 * Check that the packet doesn't begin
4863 * with an LE Control marker. (We've
4864 * already generated a test for LANE.)
4866 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4870 eaddr
= pcap_ether_hostton(name
);
4873 "unknown ether host '%s'", name
);
4874 b
= gen_ehostop(eaddr
, dir
);
4880 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4881 } else if (proto
== Q_DECNET
) {
4882 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4884 * I don't think DECNET hosts can be multihomed, so
4885 * there is no need to build up a list of addresses
4887 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
4890 alist
= pcap_nametoaddr(name
);
4891 if (alist
== NULL
|| *alist
== NULL
)
4892 bpf_error("unknown host '%s'", name
);
4894 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4896 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
4898 tmp
= gen_host(**alist
++, 0xffffffff,
4899 tproto
, dir
, q
.addr
);
4905 memset(&mask128
, 0xff, sizeof(mask128
));
4906 res0
= res
= pcap_nametoaddrinfo(name
);
4908 bpf_error("unknown host '%s'", name
);
4910 tproto
= tproto6
= proto
;
4911 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4915 for (res
= res0
; res
; res
= res
->ai_next
) {
4916 switch (res
->ai_family
) {
4918 if (tproto
== Q_IPV6
)
4921 sin
= (struct sockaddr_in
*)
4923 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4924 0xffffffff, tproto
, dir
, q
.addr
);
4927 if (tproto6
== Q_IP
)
4930 sin6
= (struct sockaddr_in6
*)
4932 tmp
= gen_host6(&sin6
->sin6_addr
,
4933 &mask128
, tproto6
, dir
, q
.addr
);
4944 bpf_error("unknown host '%s'%s", name
,
4945 (proto
== Q_DEFAULT
)
4947 : " for specified address family");
4954 if (proto
!= Q_DEFAULT
&&
4955 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4956 bpf_error("illegal qualifier of 'port'");
4957 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4958 bpf_error("unknown port '%s'", name
);
4959 if (proto
== Q_UDP
) {
4960 if (real_proto
== IPPROTO_TCP
)
4961 bpf_error("port '%s' is tcp", name
);
4962 else if (real_proto
== IPPROTO_SCTP
)
4963 bpf_error("port '%s' is sctp", name
);
4965 /* override PROTO_UNDEF */
4966 real_proto
= IPPROTO_UDP
;
4968 if (proto
== Q_TCP
) {
4969 if (real_proto
== IPPROTO_UDP
)
4970 bpf_error("port '%s' is udp", name
);
4972 else if (real_proto
== IPPROTO_SCTP
)
4973 bpf_error("port '%s' is sctp", name
);
4975 /* override PROTO_UNDEF */
4976 real_proto
= IPPROTO_TCP
;
4978 if (proto
== Q_SCTP
) {
4979 if (real_proto
== IPPROTO_UDP
)
4980 bpf_error("port '%s' is udp", name
);
4982 else if (real_proto
== IPPROTO_TCP
)
4983 bpf_error("port '%s' is tcp", name
);
4985 /* override PROTO_UNDEF */
4986 real_proto
= IPPROTO_SCTP
;
4989 return gen_port(port
, real_proto
, dir
);
4993 b
= gen_port(port
, real_proto
, dir
);
4994 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5000 if (proto
!= Q_DEFAULT
&&
5001 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5002 bpf_error("illegal qualifier of 'portrange'");
5003 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5004 bpf_error("unknown port in range '%s'", name
);
5005 if (proto
== Q_UDP
) {
5006 if (real_proto
== IPPROTO_TCP
)
5007 bpf_error("port in range '%s' is tcp", name
);
5008 else if (real_proto
== IPPROTO_SCTP
)
5009 bpf_error("port in range '%s' is sctp", name
);
5011 /* override PROTO_UNDEF */
5012 real_proto
= IPPROTO_UDP
;
5014 if (proto
== Q_TCP
) {
5015 if (real_proto
== IPPROTO_UDP
)
5016 bpf_error("port in range '%s' is udp", name
);
5017 else if (real_proto
== IPPROTO_SCTP
)
5018 bpf_error("port in range '%s' is sctp", name
);
5020 /* override PROTO_UNDEF */
5021 real_proto
= IPPROTO_TCP
;
5023 if (proto
== Q_SCTP
) {
5024 if (real_proto
== IPPROTO_UDP
)
5025 bpf_error("port in range '%s' is udp", name
);
5026 else if (real_proto
== IPPROTO_TCP
)
5027 bpf_error("port in range '%s' is tcp", name
);
5029 /* override PROTO_UNDEF */
5030 real_proto
= IPPROTO_SCTP
;
5033 return gen_portrange(port1
, port2
, real_proto
, dir
);
5037 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5038 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5045 eaddr
= pcap_ether_hostton(name
);
5047 bpf_error("unknown ether host: %s", name
);
5049 alist
= pcap_nametoaddr(name
);
5050 if (alist
== NULL
|| *alist
== NULL
)
5051 bpf_error("unknown host '%s'", name
);
5052 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5056 bpf_error("'gateway' not supported in this configuration");
5060 real_proto
= lookup_proto(name
, proto
);
5061 if (real_proto
>= 0)
5062 return gen_proto(real_proto
, proto
, dir
);
5064 bpf_error("unknown protocol: %s", name
);
5067 real_proto
= lookup_proto(name
, proto
);
5068 if (real_proto
>= 0)
5069 return gen_protochain(real_proto
, proto
, dir
);
5071 bpf_error("unknown protocol: %s", name
);
5083 gen_mcode(s1
, s2
, masklen
, q
)
5084 register const char *s1
, *s2
;
5085 register int masklen
;
5088 register int nlen
, mlen
;
5091 nlen
= __pcap_atoin(s1
, &n
);
5092 /* Promote short ipaddr */
5096 mlen
= __pcap_atoin(s2
, &m
);
5097 /* Promote short ipaddr */
5100 bpf_error("non-network bits set in \"%s mask %s\"",
5103 /* Convert mask len to mask */
5105 bpf_error("mask length must be <= 32");
5108 * X << 32 is not guaranteed by C to be 0; it's
5113 m
= 0xffffffff << (32 - masklen
);
5115 bpf_error("non-network bits set in \"%s/%d\"",
5122 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5125 bpf_error("Mask syntax for networks only");
5133 register const char *s
;
5138 int proto
= q
.proto
;
5144 else if (q
.proto
== Q_DECNET
)
5145 vlen
= __pcap_atodn(s
, &v
);
5147 vlen
= __pcap_atoin(s
, &v
);
5154 if (proto
== Q_DECNET
)
5155 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5156 else if (proto
== Q_LINK
) {
5157 bpf_error("illegal link layer address");
5160 if (s
== NULL
&& q
.addr
== Q_NET
) {
5161 /* Promote short net number */
5162 while (v
&& (v
& 0xff000000) == 0) {
5167 /* Promote short ipaddr */
5171 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5176 proto
= IPPROTO_UDP
;
5177 else if (proto
== Q_TCP
)
5178 proto
= IPPROTO_TCP
;
5179 else if (proto
== Q_SCTP
)
5180 proto
= IPPROTO_SCTP
;
5181 else if (proto
== Q_DEFAULT
)
5182 proto
= PROTO_UNDEF
;
5184 bpf_error("illegal qualifier of 'port'");
5187 return gen_port((int)v
, proto
, dir
);
5191 b
= gen_port((int)v
, proto
, dir
);
5192 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5199 proto
= IPPROTO_UDP
;
5200 else if (proto
== Q_TCP
)
5201 proto
= IPPROTO_TCP
;
5202 else if (proto
== Q_SCTP
)
5203 proto
= IPPROTO_SCTP
;
5204 else if (proto
== Q_DEFAULT
)
5205 proto
= PROTO_UNDEF
;
5207 bpf_error("illegal qualifier of 'portrange'");
5210 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5214 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5215 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5221 bpf_error("'gateway' requires a name");
5225 return gen_proto((int)v
, proto
, dir
);
5228 return gen_protochain((int)v
, proto
, dir
);
5243 gen_mcode6(s1
, s2
, masklen
, q
)
5244 register const char *s1
, *s2
;
5245 register int masklen
;
5248 struct addrinfo
*res
;
5249 struct in6_addr
*addr
;
5250 struct in6_addr mask
;
5255 bpf_error("no mask %s supported", s2
);
5257 res
= pcap_nametoaddrinfo(s1
);
5259 bpf_error("invalid ip6 address %s", s1
);
5261 bpf_error("%s resolved to multiple address", s1
);
5262 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5264 if (sizeof(mask
) * 8 < masklen
)
5265 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5266 memset(&mask
, 0, sizeof(mask
));
5267 memset(&mask
, 0xff, masklen
/ 8);
5269 mask
.s6_addr
[masklen
/ 8] =
5270 (0xff << (8 - masklen
% 8)) & 0xff;
5273 a
= (u_int32_t
*)addr
;
5274 m
= (u_int32_t
*)&mask
;
5275 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5276 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5277 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5285 bpf_error("Mask syntax for networks only");
5289 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5294 bpf_error("invalid qualifier against IPv6 address");
5302 register const u_char
*eaddr
;
5305 struct block
*b
, *tmp
;
5307 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5310 return gen_ehostop(eaddr
, (int)q
.dir
);
5312 return gen_fhostop(eaddr
, (int)q
.dir
);
5314 return gen_thostop(eaddr
, (int)q
.dir
);
5315 case DLT_IEEE802_11
:
5316 case DLT_IEEE802_11_RADIO_AVS
:
5317 case DLT_IEEE802_11_RADIO
:
5318 case DLT_PRISM_HEADER
:
5319 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5323 * Check that the packet doesn't begin with an
5324 * LE Control marker. (We've already generated
5327 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5332 * Now check the MAC address.
5334 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5339 case DLT_IP_OVER_FC
:
5340 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5342 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5346 bpf_error("ethernet address used in non-ether expression");
5352 struct slist
*s0
, *s1
;
5355 * This is definitely not the best way to do this, but the
5356 * lists will rarely get long.
5363 static struct slist
*
5369 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5374 static struct slist
*
5380 s
= new_stmt(BPF_LD
|BPF_MEM
);
5386 * Modify "index" to use the value stored into its register as an
5387 * offset relative to the beginning of the header for the protocol
5388 * "proto", and allocate a register and put an item "size" bytes long
5389 * (1, 2, or 4) at that offset into that register, making it the register
5393 gen_load(proto
, index
, size
)
5398 struct slist
*s
, *tmp
;
5400 int regno
= alloc_reg();
5402 free_reg(index
->regno
);
5406 bpf_error("data size must be 1, 2, or 4");
5422 bpf_error("unsupported index operation");
5426 * The offset is relative to the beginning of the packet
5427 * data, if we have a radio header. (If we don't, this
5430 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5431 linktype
!= DLT_IEEE802_11_RADIO
&&
5432 linktype
!= DLT_PRISM_HEADER
)
5433 bpf_error("radio information not present in capture");
5436 * Load into the X register the offset computed into the
5437 * register specifed by "index".
5439 s
= xfer_to_x(index
);
5442 * Load the item at that offset.
5444 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5446 sappend(index
->s
, s
);
5451 * The offset is relative to the beginning of
5452 * the link-layer header.
5454 * XXX - what about ATM LANE? Should the index be
5455 * relative to the beginning of the AAL5 frame, so
5456 * that 0 refers to the beginning of the LE Control
5457 * field, or relative to the beginning of the LAN
5458 * frame, so that 0 refers, for Ethernet LANE, to
5459 * the beginning of the destination address?
5461 s
= gen_llprefixlen();
5464 * If "s" is non-null, it has code to arrange that the
5465 * X register contains the length of the prefix preceding
5466 * the link-layer header. Add to it the offset computed
5467 * into the register specified by "index", and move that
5468 * into the X register. Otherwise, just load into the X
5469 * register the offset computed into the register specifed
5473 sappend(s
, xfer_to_a(index
));
5474 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5475 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5477 s
= xfer_to_x(index
);
5480 * Load the item at the sum of the offset we've put in the
5481 * X register and the offset of the start of the link
5482 * layer header (which is 0 if the radio header is
5483 * variable-length; that header length is what we put
5484 * into the X register and then added to the index).
5486 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5489 sappend(index
->s
, s
);
5505 * The offset is relative to the beginning of
5506 * the network-layer header.
5507 * XXX - are there any cases where we want
5510 s
= gen_llprefixlen();
5513 * If "s" is non-null, it has code to arrange that the
5514 * X register contains the length of the prefix preceding
5515 * the link-layer header. Add to it the offset computed
5516 * into the register specified by "index", and move that
5517 * into the X register. Otherwise, just load into the X
5518 * register the offset computed into the register specifed
5522 sappend(s
, xfer_to_a(index
));
5523 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5524 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5526 s
= xfer_to_x(index
);
5529 * Load the item at the sum of the offset we've put in the
5530 * X register, the offset of the start of the network
5531 * layer header, and the offset of the start of the link
5532 * layer header (which is 0 if the radio header is
5533 * variable-length; that header length is what we put
5534 * into the X register and then added to the index).
5536 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5537 tmp
->s
.k
= off_ll
+ off_nl
;
5539 sappend(index
->s
, s
);
5542 * Do the computation only if the packet contains
5543 * the protocol in question.
5545 b
= gen_proto_abbrev(proto
);
5547 gen_and(index
->b
, b
);
5560 * The offset is relative to the beginning of
5561 * the transport-layer header.
5563 * Load the X register with the length of the IPv4 header
5564 * (plus the offset of the link-layer header, if it's
5565 * a variable-length header), in bytes.
5567 * XXX - are there any cases where we want
5569 * XXX - we should, if we're built with
5570 * IPv6 support, generate code to load either
5571 * IPv4, IPv6, or both, as appropriate.
5573 s
= gen_loadx_iphdrlen();
5576 * The X register now contains the sum of the length
5577 * of any variable-length header preceding the link-layer
5578 * header and the length of the network-layer header.
5579 * Load into the A register the offset relative to
5580 * the beginning of the transport layer header,
5581 * add the X register to that, move that to the
5582 * X register, and load with an offset from the
5583 * X register equal to the offset of the network
5584 * layer header relative to the beginning of
5585 * the link-layer header plus the length of any
5586 * fixed-length header preceding the link-layer
5589 sappend(s
, xfer_to_a(index
));
5590 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5591 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5592 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5593 tmp
->s
.k
= off_ll
+ off_nl
;
5594 sappend(index
->s
, s
);
5597 * Do the computation only if the packet contains
5598 * the protocol in question - which is true only
5599 * if this is an IP datagram and is the first or
5600 * only fragment of that datagram.
5602 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5604 gen_and(index
->b
, b
);
5606 gen_and(gen_proto_abbrev(Q_IP
), b
);
5612 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5616 index
->regno
= regno
;
5617 s
= new_stmt(BPF_ST
);
5619 sappend(index
->s
, s
);
5625 gen_relation(code
, a0
, a1
, reversed
)
5627 struct arth
*a0
, *a1
;
5630 struct slist
*s0
, *s1
, *s2
;
5631 struct block
*b
, *tmp
;
5635 if (code
== BPF_JEQ
) {
5636 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5637 b
= new_block(JMP(code
));
5641 b
= new_block(BPF_JMP
|code
|BPF_X
);
5647 sappend(a0
->s
, a1
->s
);
5651 free_reg(a0
->regno
);
5652 free_reg(a1
->regno
);
5654 /* 'and' together protocol checks */
5657 gen_and(a0
->b
, tmp
= a1
->b
);
5673 int regno
= alloc_reg();
5674 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5677 s
= new_stmt(BPF_LD
|BPF_LEN
);
5678 s
->next
= new_stmt(BPF_ST
);
5679 s
->next
->s
.k
= regno
;
5694 a
= (struct arth
*)newchunk(sizeof(*a
));
5698 s
= new_stmt(BPF_LD
|BPF_IMM
);
5700 s
->next
= new_stmt(BPF_ST
);
5716 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5719 s
= new_stmt(BPF_ST
);
5727 gen_arth(code
, a0
, a1
)
5729 struct arth
*a0
, *a1
;
5731 struct slist
*s0
, *s1
, *s2
;
5735 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5740 sappend(a0
->s
, a1
->s
);
5742 free_reg(a0
->regno
);
5743 free_reg(a1
->regno
);
5745 s0
= new_stmt(BPF_ST
);
5746 a0
->regno
= s0
->s
.k
= alloc_reg();
5753 * Here we handle simple allocation of the scratch registers.
5754 * If too many registers are alloc'd, the allocator punts.
5756 static int regused
[BPF_MEMWORDS
];
5760 * Return the next free register.
5765 int n
= BPF_MEMWORDS
;
5768 if (regused
[curreg
])
5769 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5771 regused
[curreg
] = 1;
5775 bpf_error("too many registers needed to evaluate expression");
5780 * Return a register to the table so it can
5790 static struct block
*
5797 s
= new_stmt(BPF_LD
|BPF_LEN
);
5798 b
= new_block(JMP(jmp
));
5809 return gen_len(BPF_JGE
, n
);
5813 * Actually, this is less than or equal.
5821 b
= gen_len(BPF_JGT
, n
);
5828 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5829 * the beginning of the link-layer header.
5830 * XXX - that means you can't test values in the radiotap header, but
5831 * as that header is difficult if not impossible to parse generally
5832 * without a loop, that might not be a severe problem. A new keyword
5833 * "radio" could be added for that, although what you'd really want
5834 * would be a way of testing particular radio header values, which
5835 * would generate code appropriate to the radio header in question.
5838 gen_byteop(op
, idx
, val
)
5849 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5852 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5856 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5860 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5864 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5868 b
= new_block(JMP(BPF_JEQ
));
5875 static u_char abroadcast
[] = { 0x0 };
5878 gen_broadcast(proto
)
5881 bpf_u_int32 hostmask
;
5882 struct block
*b0
, *b1
, *b2
;
5883 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5891 case DLT_ARCNET_LINUX
:
5892 return gen_ahostop(abroadcast
, Q_DST
);
5894 return gen_ehostop(ebroadcast
, Q_DST
);
5896 return gen_fhostop(ebroadcast
, Q_DST
);
5898 return gen_thostop(ebroadcast
, Q_DST
);
5899 case DLT_IEEE802_11
:
5900 case DLT_IEEE802_11_RADIO_AVS
:
5901 case DLT_IEEE802_11_RADIO
:
5902 case DLT_PRISM_HEADER
:
5903 return gen_wlanhostop(ebroadcast
, Q_DST
);
5904 case DLT_IP_OVER_FC
:
5905 return gen_ipfchostop(ebroadcast
, Q_DST
);
5909 * Check that the packet doesn't begin with an
5910 * LE Control marker. (We've already generated
5913 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5918 * Now check the MAC address.
5920 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5926 bpf_error("not a broadcast link");
5931 b0
= gen_linktype(ETHERTYPE_IP
);
5932 hostmask
= ~netmask
;
5933 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5934 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5935 (bpf_int32
)(~0 & hostmask
), hostmask
);
5940 bpf_error("only link-layer/IP broadcast filters supported");
5945 * Generate code to test the low-order bit of a MAC address (that's
5946 * the bottom bit of the *first* byte).
5948 static struct block
*
5949 gen_mac_multicast(offset
)
5952 register struct block
*b0
;
5953 register struct slist
*s
;
5955 /* link[offset] & 1 != 0 */
5956 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5957 b0
= new_block(JMP(BPF_JSET
));
5964 gen_multicast(proto
)
5967 register struct block
*b0
, *b1
, *b2
;
5968 register struct slist
*s
;
5976 case DLT_ARCNET_LINUX
:
5977 /* all ARCnet multicasts use the same address */
5978 return gen_ahostop(abroadcast
, Q_DST
);
5980 /* ether[0] & 1 != 0 */
5981 return gen_mac_multicast(0);
5984 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5986 * XXX - was that referring to bit-order issues?
5988 /* fddi[1] & 1 != 0 */
5989 return gen_mac_multicast(1);
5991 /* tr[2] & 1 != 0 */
5992 return gen_mac_multicast(2);
5993 case DLT_IEEE802_11
:
5994 case DLT_IEEE802_11_RADIO_AVS
:
5995 case DLT_IEEE802_11_RADIO
:
5996 case DLT_PRISM_HEADER
:
6000 * For control frames, there is no DA.
6002 * For management frames, DA is at an
6003 * offset of 4 from the beginning of
6006 * For data frames, DA is at an offset
6007 * of 4 from the beginning of the packet
6008 * if To DS is clear and at an offset of
6009 * 16 from the beginning of the packet
6014 * Generate the tests to be done for data frames.
6016 * First, check for To DS set, i.e. "link[1] & 0x01".
6018 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6019 b1
= new_block(JMP(BPF_JSET
));
6020 b1
->s
.k
= 0x01; /* To DS */
6024 * If To DS is set, the DA is at 16.
6026 b0
= gen_mac_multicast(16);
6030 * Now, check for To DS not set, i.e. check
6031 * "!(link[1] & 0x01)".
6033 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6034 b2
= new_block(JMP(BPF_JSET
));
6035 b2
->s
.k
= 0x01; /* To DS */
6040 * If To DS is not set, the DA is at 4.
6042 b1
= gen_mac_multicast(4);
6046 * Now OR together the last two checks. That gives
6047 * the complete set of checks for data frames.
6052 * Now check for a data frame.
6053 * I.e, check "link[0] & 0x08".
6055 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6056 b1
= new_block(JMP(BPF_JSET
));
6061 * AND that with the checks done for data frames.
6066 * If the high-order bit of the type value is 0, this
6067 * is a management frame.
6068 * I.e, check "!(link[0] & 0x08)".
6070 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6071 b2
= new_block(JMP(BPF_JSET
));
6077 * For management frames, the DA is at 4.
6079 b1
= gen_mac_multicast(4);
6083 * OR that with the checks done for data frames.
6084 * That gives the checks done for management and
6090 * If the low-order bit of the type value is 1,
6091 * this is either a control frame or a frame
6092 * with a reserved type, and thus not a
6095 * I.e., check "!(link[0] & 0x04)".
6097 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6098 b1
= new_block(JMP(BPF_JSET
));
6104 * AND that with the checks for data and management
6109 case DLT_IP_OVER_FC
:
6110 b0
= gen_mac_multicast(2);
6115 * Check that the packet doesn't begin with an
6116 * LE Control marker. (We've already generated
6119 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6123 /* ether[off_mac] & 1 != 0 */
6124 b0
= gen_mac_multicast(off_mac
);
6132 /* Link not known to support multicasts */
6136 b0
= gen_linktype(ETHERTYPE_IP
);
6137 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6143 b0
= gen_linktype(ETHERTYPE_IPV6
);
6144 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6149 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6154 * generate command for inbound/outbound. It's here so we can
6155 * make it link-type specific. 'dir' = 0 implies "inbound",
6156 * = 1 implies "outbound".
6162 register struct block
*b0
;
6165 * Only some data link types support inbound/outbound qualifiers.
6169 b0
= gen_relation(BPF_JEQ
,
6170 gen_load(Q_LINK
, gen_loadi(0), 1),
6178 * Match packets sent by this machine.
6180 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6183 * Match packets sent to this machine.
6184 * (No broadcast or multicast packets, or
6185 * packets sent to some other machine and
6186 * received promiscuously.)
6188 * XXX - packets sent to other machines probably
6189 * shouldn't be matched, but what about broadcast
6190 * or multicast packets we received?
6192 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6197 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6198 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6203 /* match outgoing packets */
6204 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6206 /* match incoming packets */
6207 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6211 case DLT_JUNIPER_MFR
:
6212 case DLT_JUNIPER_MLFR
:
6213 case DLT_JUNIPER_MLPPP
:
6214 case DLT_JUNIPER_ATM1
:
6215 case DLT_JUNIPER_ATM2
:
6216 case DLT_JUNIPER_PPPOE
:
6217 case DLT_JUNIPER_PPPOE_ATM
:
6218 case DLT_JUNIPER_GGSN
:
6219 case DLT_JUNIPER_ES
:
6220 case DLT_JUNIPER_MONITOR
:
6221 case DLT_JUNIPER_SERVICES
:
6222 case DLT_JUNIPER_ETHER
:
6223 case DLT_JUNIPER_PPP
:
6224 case DLT_JUNIPER_FRELAY
:
6225 case DLT_JUNIPER_CHDLC
:
6226 case DLT_JUNIPER_VP
:
6227 case DLT_JUNIPER_ISM
:
6228 /* juniper flags (including direction) are stored
6229 * the byte after the 3-byte magic number */
6231 /* match outgoing packets */
6232 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6234 /* match incoming packets */
6235 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6240 bpf_error("inbound/outbound not supported on linktype %d",
6248 /* PF firewall log matched interface */
6250 gen_pf_ifname(const char *ifname
)
6255 if (linktype
!= DLT_PFLOG
) {
6256 bpf_error("ifname supported only on PF linktype");
6259 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6260 off
= offsetof(struct pfloghdr
, ifname
);
6261 if (strlen(ifname
) >= len
) {
6262 bpf_error("ifname interface names can only be %d characters",
6266 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6270 /* PF firewall log ruleset name */
6272 gen_pf_ruleset(char *ruleset
)
6276 if (linktype
!= DLT_PFLOG
) {
6277 bpf_error("ruleset supported only on PF linktype");
6281 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6282 bpf_error("ruleset names can only be %ld characters",
6283 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6287 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6288 strlen(ruleset
), (const u_char
*)ruleset
);
6292 /* PF firewall log rule number */
6298 if (linktype
!= DLT_PFLOG
) {
6299 bpf_error("rnr supported only on PF linktype");
6303 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6308 /* PF firewall log sub-rule number */
6310 gen_pf_srnr(int srnr
)
6314 if (linktype
!= DLT_PFLOG
) {
6315 bpf_error("srnr supported only on PF linktype");
6319 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6324 /* PF firewall log reason code */
6326 gen_pf_reason(int reason
)
6330 if (linktype
!= DLT_PFLOG
) {
6331 bpf_error("reason supported only on PF linktype");
6335 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6340 /* PF firewall log action */
6342 gen_pf_action(int action
)
6346 if (linktype
!= DLT_PFLOG
) {
6347 bpf_error("action supported only on PF linktype");
6351 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6356 /* IEEE 802.11 wireless header */
6358 gen_p80211_type(int type
, int mask
)
6364 case DLT_IEEE802_11
:
6365 case DLT_PRISM_HEADER
:
6366 case DLT_IEEE802_11_RADIO_AVS
:
6367 case DLT_IEEE802_11_RADIO
:
6368 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
6373 bpf_error("802.11 link-layer types supported only on 802.11");
6381 register const u_char
*eaddr
;
6387 case DLT_ARCNET_LINUX
:
6388 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
6390 return (gen_ahostop(eaddr
, (int)q
.dir
));
6392 bpf_error("ARCnet address used in non-arc expression");
6398 bpf_error("aid supported only on ARCnet");
6404 static struct block
*
6405 gen_ahostop(eaddr
, dir
)
6406 register const u_char
*eaddr
;
6409 register struct block
*b0
, *b1
;
6412 /* src comes first, different from Ethernet */
6414 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6417 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6420 b0
= gen_ahostop(eaddr
, Q_SRC
);
6421 b1
= gen_ahostop(eaddr
, Q_DST
);
6427 b0
= gen_ahostop(eaddr
, Q_SRC
);
6428 b1
= gen_ahostop(eaddr
, Q_DST
);
6437 * support IEEE 802.1Q VLAN trunk over ethernet
6443 struct block
*b0
, *b1
;
6445 /* can't check for VLAN-encapsulated packets inside MPLS */
6446 if (label_stack_depth
> 0)
6447 bpf_error("no VLAN match after MPLS");
6450 * Change the offsets to point to the type and data fields within
6451 * the VLAN packet. Just increment the offsets, so that we
6452 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6453 * capture VLAN 200 encapsulated within VLAN 100.
6455 * XXX - this is a bit of a kludge. If we were to split the
6456 * compiler into a parser that parses an expression and
6457 * generates an expression tree, and a code generator that
6458 * takes an expression tree (which could come from our
6459 * parser or from some other parser) and generates BPF code,
6460 * we could perhaps make the offsets parameters of routines
6461 * and, in the handler for an "AND" node, pass to subnodes
6462 * other than the VLAN node the adjusted offsets.
6464 * This would mean that "vlan" would, instead of changing the
6465 * behavior of *all* tests after it, change only the behavior
6466 * of tests ANDed with it. That would change the documented
6467 * semantics of "vlan", which might break some expressions.
6468 * However, it would mean that "(vlan and ip) or ip" would check
6469 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6470 * checking only for VLAN-encapsulated IP, so that could still
6471 * be considered worth doing; it wouldn't break expressions
6472 * that are of the form "vlan and ..." or "vlan N and ...",
6473 * which I suspect are the most common expressions involving
6474 * "vlan". "vlan or ..." doesn't necessarily do what the user
6475 * would really want, now, as all the "or ..." tests would
6476 * be done assuming a VLAN, even though the "or" could be viewed
6477 * as meaning "or, if this isn't a VLAN packet...".
6479 orig_linktype
= off_linktype
; /* save original values */
6491 bpf_error("no VLAN support for data link type %d",
6496 /* check for VLAN */
6497 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6499 /* If a specific VLAN is requested, check VLAN id */
6500 if (vlan_num
>= 0) {
6501 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6517 struct block
*b0
,*b1
;
6520 * Change the offsets to point to the type and data fields within
6521 * the MPLS packet. Just increment the offsets, so that we
6522 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6523 * capture packets with an outer label of 100000 and an inner
6526 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6530 if (label_stack_depth
> 0) {
6531 /* just match the bottom-of-stack bit clear */
6532 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6535 * Indicate that we're checking MPLS-encapsulated headers,
6536 * to make sure higher level code generators don't try to
6537 * match against IP-related protocols such as Q_ARP, Q_RARP
6542 case DLT_C_HDLC
: /* fall through */
6544 b0
= gen_linktype(ETHERTYPE_MPLS
);
6548 b0
= gen_linktype(PPP_MPLS_UCAST
);
6551 /* FIXME add other DLT_s ...
6552 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6553 * leave it for now */
6556 bpf_error("no MPLS support for data link type %d",
6564 /* If a specific MPLS label is requested, check it */
6565 if (label_num
>= 0) {
6566 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6567 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6568 0xfffff000); /* only compare the first 20 bits */
6575 label_stack_depth
++;
6580 * Support PPPOE discovery and session.
6585 /* check for PPPoE discovery */
6586 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6595 * Test against the PPPoE session link-layer type.
6597 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6600 * Change the offsets to point to the type and data fields within
6603 * XXX - this is a bit of a kludge. If we were to split the
6604 * compiler into a parser that parses an expression and
6605 * generates an expression tree, and a code generator that
6606 * takes an expression tree (which could come from our
6607 * parser or from some other parser) and generates BPF code,
6608 * we could perhaps make the offsets parameters of routines
6609 * and, in the handler for an "AND" node, pass to subnodes
6610 * other than the PPPoE node the adjusted offsets.
6612 * This would mean that "pppoes" would, instead of changing the
6613 * behavior of *all* tests after it, change only the behavior
6614 * of tests ANDed with it. That would change the documented
6615 * semantics of "pppoes", which might break some expressions.
6616 * However, it would mean that "(pppoes and ip) or ip" would check
6617 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6618 * checking only for VLAN-encapsulated IP, so that could still
6619 * be considered worth doing; it wouldn't break expressions
6620 * that are of the form "pppoes and ..." which I suspect are the
6621 * most common expressions involving "pppoes". "pppoes or ..."
6622 * doesn't necessarily do what the user would really want, now,
6623 * as all the "or ..." tests would be done assuming PPPoE, even
6624 * though the "or" could be viewed as meaning "or, if this isn't
6625 * a PPPoE packet...".
6627 orig_linktype
= off_linktype
; /* save original values */
6631 * The "network-layer" protocol is PPPoE, which has a 6-byte
6632 * PPPoE header, followed by PPP payload, so we set the
6633 * offsets to the network layer offset plus 6 bytes for
6634 * the PPPoE header plus the values appropriate for PPP when
6635 * encapsulated in Ethernet (which means there's no HDLC
6638 off_linktype
= orig_nl
+ 6;
6639 off_nl
= orig_nl
+ 6 + 2;
6640 off_nl_nosnap
= orig_nl
+ 6 + 2;
6643 * Set the link-layer type to PPP, as all subsequent tests will
6644 * be on the encapsulated PPP header.
6652 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6664 bpf_error("'vpi' supported only on raw ATM");
6665 if (off_vpi
== (u_int
)-1)
6667 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6673 bpf_error("'vci' supported only on raw ATM");
6674 if (off_vci
== (u_int
)-1)
6676 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6681 if (off_proto
== (u_int
)-1)
6682 abort(); /* XXX - this isn't on FreeBSD */
6683 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6688 if (off_payload
== (u_int
)-1)
6690 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6691 0xffffffff, jtype
, reverse
, jvalue
);
6696 bpf_error("'callref' supported only on raw ATM");
6697 if (off_proto
== (u_int
)-1)
6699 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6700 jtype
, reverse
, jvalue
);
6710 gen_atmtype_abbrev(type
)
6713 struct block
*b0
, *b1
;
6718 /* Get all packets in Meta signalling Circuit */
6720 bpf_error("'metac' supported only on raw ATM");
6721 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6722 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6727 /* Get all packets in Broadcast Circuit*/
6729 bpf_error("'bcc' supported only on raw ATM");
6730 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6731 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6736 /* Get all cells in Segment OAM F4 circuit*/
6738 bpf_error("'oam4sc' supported only on raw ATM");
6739 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6740 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6745 /* Get all cells in End-to-End OAM F4 Circuit*/
6747 bpf_error("'oam4ec' supported only on raw ATM");
6748 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6749 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6754 /* Get all packets in connection Signalling Circuit */
6756 bpf_error("'sc' supported only on raw ATM");
6757 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6758 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6763 /* Get all packets in ILMI Circuit */
6765 bpf_error("'ilmic' supported only on raw ATM");
6766 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6767 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6772 /* Get all LANE packets */
6774 bpf_error("'lane' supported only on raw ATM");
6775 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6778 * Arrange that all subsequent tests assume LANE
6779 * rather than LLC-encapsulated packets, and set
6780 * the offsets appropriately for LANE-encapsulated
6783 * "off_mac" is the offset of the Ethernet header,
6784 * which is 2 bytes past the ATM pseudo-header
6785 * (skipping the pseudo-header and 2-byte LE Client
6786 * field). The other offsets are Ethernet offsets
6787 * relative to "off_mac".
6790 off_mac
= off_payload
+ 2; /* MAC header */
6791 off_linktype
= off_mac
+ 12;
6792 off_nl
= off_mac
+ 14; /* Ethernet II */
6793 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6797 /* Get all LLC-encapsulated packets */
6799 bpf_error("'llc' supported only on raw ATM");
6800 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6811 * Filtering for MTP2 messages based on li value
6812 * FISU, length is null
6813 * LSSU, length is 1 or 2
6814 * MSU, length is 3 or more
6817 gen_mtp2type_abbrev(type
)
6820 struct block
*b0
, *b1
;
6825 if (linktype
!= DLT_MTP2
)
6826 bpf_error("'fisu' supported only on MTP2");
6827 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
6828 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
6832 if (linktype
!= DLT_MTP2
)
6833 bpf_error("'lssu' supported only on MTP2");
6834 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
6835 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
6840 if (linktype
!= DLT_MTP2
)
6841 bpf_error("'msu' supported only on MTP2");
6842 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
6852 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6859 bpf_u_int32 val1
, val2
, val3
;
6861 switch (mtp3field
) {
6864 if (off_sio
== (u_int
)-1)
6865 bpf_error("'sio' supported only on SS7");
6866 /* sio coded on 1 byte so max value 255 */
6868 bpf_error("sio value %u too big; max value = 255",
6870 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6871 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6875 if (off_opc
== (u_int
)-1)
6876 bpf_error("'opc' supported only on SS7");
6877 /* opc coded on 14 bits so max value 16383 */
6879 bpf_error("opc value %u too big; max value = 16383",
6881 /* the following instructions are made to convert jvalue
6882 * to the form used to write opc in an ss7 message*/
6883 val1
= jvalue
& 0x00003c00;
6885 val2
= jvalue
& 0x000003fc;
6887 val3
= jvalue
& 0x00000003;
6889 jvalue
= val1
+ val2
+ val3
;
6890 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6891 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6895 if (off_dpc
== (u_int
)-1)
6896 bpf_error("'dpc' supported only on SS7");
6897 /* dpc coded on 14 bits so max value 16383 */
6899 bpf_error("dpc value %u too big; max value = 16383",
6901 /* the following instructions are made to convert jvalue
6902 * to the forme used to write dpc in an ss7 message*/
6903 val1
= jvalue
& 0x000000ff;
6905 val2
= jvalue
& 0x00003f00;
6907 jvalue
= val1
+ val2
;
6908 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6909 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6913 if (off_sls
== (u_int
)-1)
6914 bpf_error("'sls' supported only on SS7");
6915 /* sls coded on 4 bits so max value 15 */
6917 bpf_error("sls value %u too big; max value = 15",
6919 /* the following instruction is made to convert jvalue
6920 * to the forme used to write sls in an ss7 message*/
6921 jvalue
= jvalue
<< 4;
6922 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6923 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6932 static struct block
*
6933 gen_msg_abbrev(type
)
6939 * Q.2931 signalling protocol messages for handling virtual circuits
6940 * establishment and teardown
6945 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6949 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6953 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6957 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6961 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6964 case A_RELEASE_DONE
:
6965 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6975 gen_atmmulti_abbrev(type
)
6978 struct block
*b0
, *b1
;
6984 bpf_error("'oam' supported only on raw ATM");
6985 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6990 bpf_error("'oamf4' supported only on raw ATM");
6992 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6993 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6995 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7001 * Get Q.2931 signalling messages for switched
7002 * virtual connection
7005 bpf_error("'connectmsg' supported only on raw ATM");
7006 b0
= gen_msg_abbrev(A_SETUP
);
7007 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7009 b0
= gen_msg_abbrev(A_CONNECT
);
7011 b0
= gen_msg_abbrev(A_CONNECTACK
);
7013 b0
= gen_msg_abbrev(A_RELEASE
);
7015 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7017 b0
= gen_atmtype_abbrev(A_SC
);
7023 bpf_error("'metaconnect' supported only on raw ATM");
7024 b0
= gen_msg_abbrev(A_SETUP
);
7025 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7027 b0
= gen_msg_abbrev(A_CONNECT
);
7029 b0
= gen_msg_abbrev(A_RELEASE
);
7031 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7033 b0
= gen_atmtype_abbrev(A_METAC
);