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.221.2.15 2005-05-01 19:33:11 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 offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -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_LINK
, /* relative to the link-layer header */
139 OR_NET
, /* relative to the network-layer header */
140 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
141 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
142 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
146 * We divy out chunks of memory rather than call malloc each time so
147 * we don't have to worry about leaking memory. It's probably
148 * not a big deal if all this memory was wasted but if this ever
149 * goes into a library that would probably not be a good idea.
151 * XXX - this *is* in a library....
154 #define CHUNK0SIZE 1024
160 static struct chunk chunks
[NCHUNKS
];
161 static int cur_chunk
;
163 static void *newchunk(u_int
);
164 static void freechunks(void);
165 static inline struct block
*new_block(int);
166 static inline struct slist
*new_stmt(int);
167 static struct block
*gen_retblk(int);
168 static inline void syntax(void);
170 static void backpatch(struct block
*, struct block
*);
171 static void merge(struct block
*, struct block
*);
172 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
173 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
179 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
180 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
181 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
182 static struct slist
*gen_load_llrel(u_int
, u_int
);
183 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
184 static struct slist
*gen_loadx_iphdrlen(void);
185 static struct block
*gen_uncond(int);
186 static inline struct block
*gen_true(void);
187 static inline struct block
*gen_false(void);
188 static struct block
*gen_ether_linktype(int);
189 static struct block
*gen_linux_sll_linktype(int);
190 static void insert_radiotap_load_llprefixlen(struct block
*);
191 static void insert_load_llprefixlen(struct block
*);
192 static struct slist
*gen_llprefixlen(void);
193 static struct block
*gen_linktype(int);
194 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
195 static struct block
*gen_llc_linktype(int);
196 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
198 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
200 static struct block
*gen_ahostop(const u_char
*, int);
201 static struct block
*gen_ehostop(const u_char
*, int);
202 static struct block
*gen_fhostop(const u_char
*, int);
203 static struct block
*gen_thostop(const u_char
*, int);
204 static struct block
*gen_wlanhostop(const u_char
*, int);
205 static struct block
*gen_ipfchostop(const u_char
*, int);
206 static struct block
*gen_dnhostop(bpf_u_int32
, int);
207 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
209 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
212 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
214 static struct block
*gen_ipfrag(void);
215 static struct block
*gen_portatom(int, bpf_int32
);
216 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
218 static struct block
*gen_portatom6(int, bpf_int32
);
219 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
221 struct block
*gen_portop(int, int, int);
222 static struct block
*gen_port(int, int, int);
223 struct block
*gen_portrangeop(int, int, int, int);
224 static struct block
*gen_portrange(int, int, int, int);
226 struct block
*gen_portop6(int, int, int);
227 static struct block
*gen_port6(int, int, int);
228 struct block
*gen_portrangeop6(int, int, int, int);
229 static struct block
*gen_portrange6(int, int, int, int);
231 static int lookup_proto(const char *, int);
232 static struct block
*gen_protochain(int, int, int);
233 static struct block
*gen_proto(int, int, int);
234 static struct slist
*xfer_to_x(struct arth
*);
235 static struct slist
*xfer_to_a(struct arth
*);
236 static struct block
*gen_mac_multicast(int);
237 static struct block
*gen_len(int, int);
239 static struct block
*gen_msg_abbrev(int type
);
250 /* XXX Round up to nearest long. */
251 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
253 /* XXX Round up to structure boundary. */
257 cp
= &chunks
[cur_chunk
];
258 if (n
> cp
->n_left
) {
259 ++cp
, k
= ++cur_chunk
;
261 bpf_error("out of memory");
262 size
= CHUNK0SIZE
<< k
;
263 cp
->m
= (void *)malloc(size
);
265 bpf_error("out of memory");
266 memset((char *)cp
->m
, 0, size
);
269 bpf_error("out of memory");
272 return (void *)((char *)cp
->m
+ cp
->n_left
);
281 for (i
= 0; i
< NCHUNKS
; ++i
)
282 if (chunks
[i
].m
!= NULL
) {
289 * A strdup whose allocations are freed after code generation is over.
293 register const char *s
;
295 int n
= strlen(s
) + 1;
296 char *cp
= newchunk(n
);
302 static inline struct block
*
308 p
= (struct block
*)newchunk(sizeof(*p
));
315 static inline struct slist
*
321 p
= (struct slist
*)newchunk(sizeof(*p
));
327 static struct block
*
331 struct block
*b
= new_block(BPF_RET
|BPF_K
);
340 bpf_error("syntax error in filter expression");
343 static bpf_u_int32 netmask
;
348 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
349 char *buf
, int optimize
, bpf_u_int32 mask
)
358 if (setjmp(top_ctx
)) {
366 snaplen
= pcap_snapshot(p
);
368 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
369 "snaplen of 0 rejects all packets");
373 lex_init(buf
? buf
: "");
381 root
= gen_retblk(snaplen
);
383 if (optimize
&& !no_optimize
) {
386 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
387 bpf_error("expression rejects all packets");
389 program
->bf_insns
= icode_to_fcode(root
, &len
);
390 program
->bf_len
= len
;
398 * entry point for using the compiler with no pcap open
399 * pass in all the stuff that is needed explicitly instead.
402 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
403 struct bpf_program
*program
,
404 char *buf
, int optimize
, bpf_u_int32 mask
)
409 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
412 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
418 * Clean up a "struct bpf_program" by freeing all the memory allocated
422 pcap_freecode(struct bpf_program
*program
)
425 if (program
->bf_insns
!= NULL
) {
426 free((char *)program
->bf_insns
);
427 program
->bf_insns
= NULL
;
432 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
433 * which of the jt and jf fields has been resolved and which is a pointer
434 * back to another unresolved block (or nil). At least one of the fields
435 * in each block is already resolved.
438 backpatch(list
, target
)
439 struct block
*list
, *target
;
456 * Merge the lists in b0 and b1, using the 'sense' field to indicate
457 * which of jt and jf is the link.
461 struct block
*b0
, *b1
;
463 register struct block
**p
= &b0
;
465 /* Find end of list. */
467 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
469 /* Concatenate the lists. */
477 backpatch(p
, gen_retblk(snaplen
));
478 p
->sense
= !p
->sense
;
479 backpatch(p
, gen_retblk(0));
483 * Insert before the statements of the first (root) block any
484 * statements needed to load the lengths of any variable-length
485 * headers into registers.
487 * XXX - a fancier strategy would be to insert those before the
488 * statements of all blocks that use those lengths and that
489 * have no predecessors that use them, so that we only compute
490 * the lengths if we need them. There might be even better
491 * approaches than that. However, as we're currently only
492 * handling variable-length radiotap headers, and as all
493 * filtering expressions other than raw link[M:N] tests
494 * require the length of that header, doing more for that
495 * header length isn't really worth the effort.
497 insert_load_llprefixlen(root
);
502 struct block
*b0
, *b1
;
504 backpatch(b0
, b1
->head
);
505 b0
->sense
= !b0
->sense
;
506 b1
->sense
= !b1
->sense
;
508 b1
->sense
= !b1
->sense
;
514 struct block
*b0
, *b1
;
516 b0
->sense
= !b0
->sense
;
517 backpatch(b0
, b1
->head
);
518 b0
->sense
= !b0
->sense
;
527 b
->sense
= !b
->sense
;
530 static struct block
*
531 gen_cmp(offrel
, offset
, size
, v
)
532 enum e_offrel offrel
;
536 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
539 static struct block
*
540 gen_cmp_gt(offrel
, offset
, size
, v
)
541 enum e_offrel offrel
;
545 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
548 static struct block
*
549 gen_cmp_ge(offrel
, offset
, size
, v
)
550 enum e_offrel offrel
;
554 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
557 static struct block
*
558 gen_cmp_lt(offrel
, offset
, size
, v
)
559 enum e_offrel offrel
;
563 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
566 static struct block
*
567 gen_cmp_le(offrel
, offset
, size
, v
)
568 enum e_offrel offrel
;
572 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
575 static struct block
*
576 gen_mcmp(offrel
, offset
, size
, v
, mask
)
577 enum e_offrel offrel
;
582 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
585 static struct block
*
586 gen_bcmp(offrel
, offset
, size
, v
)
587 enum e_offrel offrel
;
588 register u_int offset
, size
;
589 register const u_char
*v
;
591 register struct block
*b
, *tmp
;
595 register const u_char
*p
= &v
[size
- 4];
596 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
597 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
599 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
606 register const u_char
*p
= &v
[size
- 2];
607 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
609 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
616 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
625 * AND the field of size "size" at offset "offset" relative to the header
626 * specified by "offrel" with "mask", and compare it with the value "v"
627 * with the test specified by "jtype"; if "reverse" is true, the test
628 * should test the opposite of "jtype".
630 static struct block
*
631 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
632 enum e_offrel offrel
;
634 bpf_u_int32 offset
, size
, mask
, jtype
;
637 struct slist
*s
, *s2
;
640 s
= gen_load_a(offrel
, offset
, size
);
642 if (mask
!= 0xffffffff) {
643 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
648 b
= new_block(JMP(jtype
));
651 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
657 * Various code constructs need to know the layout of the data link
658 * layer. These variables give the necessary offsets from the beginning
659 * of the packet data.
661 * If the link layer has variable_length headers, the offsets are offsets
662 * from the end of the link-link-layer header, and "reg_ll_size" is
663 * the register number for a register containing the length of the
664 * link-layer header. Otherwise, "reg_ll_size" is -1.
666 static int reg_ll_size
;
669 * This is the offset of the beginning of the MAC-layer header.
670 * It's usually 0, except for ATM LANE.
672 static u_int off_mac
;
675 * "off_linktype" is the offset to information in the link-layer header
676 * giving the packet type.
678 * For Ethernet, it's the offset of the Ethernet type field.
680 * For link-layer types that always use 802.2 headers, it's the
681 * offset of the LLC header.
683 * For PPP, it's the offset of the PPP type field.
685 * For Cisco HDLC, it's the offset of the CHDLC type field.
687 * For BSD loopback, it's the offset of the AF_ value.
689 * For Linux cooked sockets, it's the offset of the type field.
691 * It's set to -1 for no encapsulation, in which case, IP is assumed.
693 static u_int off_linktype
;
696 * TRUE if the link layer includes an ATM pseudo-header.
698 static int is_atm
= 0;
701 * TRUE if "lane" appeared in the filter; it causes us to generate
702 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
704 static int is_lane
= 0;
707 * These are offsets for the ATM pseudo-header.
709 static u_int off_vpi
;
710 static u_int off_vci
;
711 static u_int off_proto
;
714 * This is the offset of the first byte after the ATM pseudo_header,
715 * or -1 if there is no ATM pseudo-header.
717 static u_int off_payload
;
720 * These are offsets to the beginning of the network-layer header.
722 * If the link layer never uses 802.2 LLC:
724 * "off_nl" and "off_nl_nosnap" are the same.
726 * If the link layer always uses 802.2 LLC:
728 * "off_nl" is the offset if there's a SNAP header following
731 * "off_nl_nosnap" is the offset if there's no SNAP header.
733 * If the link layer is Ethernet:
735 * "off_nl" is the offset if the packet is an Ethernet II packet
736 * (we assume no 802.3+802.2+SNAP);
738 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
739 * with an 802.2 header following it.
742 static u_int off_nl_nosnap
;
750 linktype
= pcap_datalink(p
);
752 pcap_fddipad
= p
->fddipad
;
756 * Assume it's not raw ATM with a pseudo-header, for now.
775 off_nl
= 6; /* XXX in reality, variable! */
776 off_nl_nosnap
= 6; /* no 802.2 LLC */
779 case DLT_ARCNET_LINUX
:
781 off_nl
= 8; /* XXX in reality, variable! */
782 off_nl_nosnap
= 8; /* no 802.2 LLC */
787 off_nl
= 14; /* Ethernet II */
788 off_nl_nosnap
= 17; /* 802.3+802.2 */
793 * SLIP doesn't have a link level type. The 16 byte
794 * header is hacked into our SLIP driver.
798 off_nl_nosnap
= 16; /* no 802.2 LLC */
802 /* XXX this may be the same as the DLT_PPP_BSDOS case */
806 off_nl_nosnap
= 24; /* no 802.2 LLC */
813 off_nl_nosnap
= 4; /* no 802.2 LLC */
819 off_nl_nosnap
= 12; /* no 802.2 LLC */
824 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
825 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
828 off_nl_nosnap
= 4; /* no 802.2 LLC */
833 * This does no include the Ethernet header, and
834 * only covers session state.
838 off_nl_nosnap
= 8; /* no 802.2 LLC */
844 off_nl_nosnap
= 24; /* no 802.2 LLC */
849 * FDDI doesn't really have a link-level type field.
850 * We set "off_linktype" to the offset of the LLC header.
852 * To check for Ethernet types, we assume that SSAP = SNAP
853 * is being used and pick out the encapsulated Ethernet type.
854 * XXX - should we generate code to check for SNAP?
858 off_linktype
+= pcap_fddipad
;
860 off_nl
= 21; /* FDDI+802.2+SNAP */
861 off_nl_nosnap
= 16; /* FDDI+802.2 */
863 off_nl
+= pcap_fddipad
;
864 off_nl_nosnap
+= pcap_fddipad
;
870 * Token Ring doesn't really have a link-level type field.
871 * We set "off_linktype" to the offset of the LLC header.
873 * To check for Ethernet types, we assume that SSAP = SNAP
874 * is being used and pick out the encapsulated Ethernet type.
875 * XXX - should we generate code to check for SNAP?
877 * XXX - the header is actually variable-length.
878 * Some various Linux patched versions gave 38
879 * as "off_linktype" and 40 as "off_nl"; however,
880 * if a token ring packet has *no* routing
881 * information, i.e. is not source-routed, the correct
882 * values are 20 and 22, as they are in the vanilla code.
884 * A packet is source-routed iff the uppermost bit
885 * of the first byte of the source address, at an
886 * offset of 8, has the uppermost bit set. If the
887 * packet is source-routed, the total number of bytes
888 * of routing information is 2 plus bits 0x1F00 of
889 * the 16-bit value at an offset of 14 (shifted right
890 * 8 - figure out which byte that is).
893 off_nl
= 22; /* Token Ring+802.2+SNAP */
894 off_nl_nosnap
= 17; /* Token Ring+802.2 */
899 * 802.11 doesn't really have a link-level type field.
900 * We set "off_linktype" to the offset of the LLC header.
902 * To check for Ethernet types, we assume that SSAP = SNAP
903 * is being used and pick out the encapsulated Ethernet type.
904 * XXX - should we generate code to check for SNAP?
906 * XXX - the header is actually variable-length. We
907 * assume a 24-byte link-layer header, as appears in
908 * data frames in networks with no bridges. If the
909 * fromds and tods 802.11 header bits are both set,
910 * it's actually supposed to be 30 bytes.
913 off_nl
= 32; /* 802.11+802.2+SNAP */
914 off_nl_nosnap
= 27; /* 802.11+802.2 */
917 case DLT_PRISM_HEADER
:
919 * Same as 802.11, but with an additional header before
920 * the 802.11 header, containing a bunch of additional
921 * information including radio-level information.
923 * The header is 144 bytes long.
925 * XXX - same variable-length header problem; at least
926 * the Prism header is fixed-length.
928 off_linktype
= 144+24;
929 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
930 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
933 case DLT_IEEE802_11_RADIO_AVS
:
935 * Same as 802.11, but with an additional header before
936 * the 802.11 header, containing a bunch of additional
937 * information including radio-level information.
939 * The header is 64 bytes long, at least in its
940 * current incarnation.
942 * XXX - same variable-length header problem, only
943 * more so; this header is also variable-length,
944 * with the length being the 32-bit big-endian
945 * number at an offset of 4 from the beginning
946 * of the radio header.
948 off_linktype
= 64+24;
949 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
950 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
953 case DLT_IEEE802_11_RADIO
:
955 * Same as 802.11, but with an additional header before
956 * the 802.11 header, containing a bunch of additional
957 * information including radio-level information.
959 * The radiotap header is variable length, and we
960 * generate code to compute its length and store it
961 * in a register. These offsets are relative to the
962 * beginning of the 802.11 header.
965 off_nl
= 32; /* 802.11+802.2+SNAP */
966 off_nl_nosnap
= 27; /* 802.11+802.2 */
969 case DLT_ATM_RFC1483
:
970 case DLT_ATM_CLIP
: /* Linux ATM defines this */
972 * assume routed, non-ISO PDUs
973 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
976 off_nl
= 8; /* 802.2+SNAP */
977 off_nl_nosnap
= 3; /* 802.2 */
982 * Full Frontal ATM; you get AALn PDUs with an ATM
986 off_vpi
= SUNATM_VPI_POS
;
987 off_vci
= SUNATM_VCI_POS
;
988 off_proto
= PROTO_POS
;
989 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
990 off_payload
= SUNATM_PKT_BEGIN_POS
;
991 off_linktype
= off_payload
;
992 off_nl
= off_payload
+8; /* 802.2+SNAP */
993 off_nl_nosnap
= off_payload
+3; /* 802.2 */
999 off_nl_nosnap
= 0; /* no 802.2 LLC */
1002 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1005 off_nl_nosnap
= 16; /* no 802.2 LLC */
1010 * LocalTalk does have a 1-byte type field in the LLAP header,
1011 * but really it just indicates whether there is a "short" or
1012 * "long" DDP packet following.
1016 off_nl_nosnap
= 0; /* no 802.2 LLC */
1019 case DLT_IP_OVER_FC
:
1021 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1022 * link-level type field. We set "off_linktype" to the
1023 * offset of the LLC header.
1025 * To check for Ethernet types, we assume that SSAP = SNAP
1026 * is being used and pick out the encapsulated Ethernet type.
1027 * XXX - should we generate code to check for SNAP? RFC
1028 * 2625 says SNAP should be used.
1031 off_nl
= 24; /* IPFC+802.2+SNAP */
1032 off_nl_nosnap
= 19; /* IPFC+802.2 */
1037 * XXX - we should set this to handle SNAP-encapsulated
1038 * frames (NLPID of 0x80).
1042 off_nl_nosnap
= 0; /* no 802.2 LLC */
1045 case DLT_APPLE_IP_OVER_IEEE1394
:
1048 off_nl_nosnap
= 18; /* no 802.2 LLC */
1051 case DLT_LINUX_IRDA
:
1053 * Currently, only raw "link[N:M]" filtering is supported.
1062 * Currently, only raw "link[N:M]" filtering is supported.
1069 case DLT_SYMANTEC_FIREWALL
:
1071 off_nl
= 44; /* Ethernet II */
1072 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1077 /* XXX read this from pf.h? */
1078 off_nl
= PFLOG_HDRLEN
;
1079 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1082 case DLT_JUNIPER_MLFR
:
1083 case DLT_JUNIPER_MLPPP
:
1086 off_nl_nosnap
= -1; /* no 802.2 LLC */
1089 case DLT_JUNIPER_ATM1
:
1090 off_linktype
= 4; /* in reality variable between 4-8 */
1095 case DLT_JUNIPER_ATM2
:
1096 off_linktype
= 8; /* in reality variable between 8-12 */
1109 case DLT_LINUX_LAPD
:
1111 * Currently, only raw "link[N:M]" filtering is supported.
1118 bpf_error("unknown data link type %d", linktype
);
1123 * Load a value relative to the beginning of the link-layer header.
1124 * The link-layer header doesn't necessarily begin at the beginning
1125 * of the packet data; there might be a variable-length prefix containing
1126 * radio information.
1128 static struct slist
*
1129 gen_load_llrel(offset
, size
)
1132 struct slist
*s
, *s2
;
1134 s
= gen_llprefixlen();
1137 * If "s" is non-null, it has code to arrange that the X register
1138 * contains the length of the prefix preceding the link-layer
1142 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1146 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1153 * Load a value relative to the beginning of the specified header.
1155 static struct slist
*
1156 gen_load_a(offrel
, offset
, size
)
1157 enum e_offrel offrel
;
1160 struct slist
*s
, *s2
;
1165 s
= gen_load_llrel(offset
, size
);
1169 s
= gen_load_llrel(off_nl
+ offset
, size
);
1173 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1178 * Load the X register with the length of the IPv4 header,
1181 s
= gen_loadx_iphdrlen();
1184 * Load the item at {length of the link-layer header} +
1185 * {length of the IPv4 header} + {specified offset}.
1187 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1188 s2
->s
.k
= off_nl
+ offset
;
1193 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1204 * Generate code to load into the X register the sum of the length of
1205 * the IPv4 header and any variable-length header preceding the link-layer
1208 static struct slist
*
1209 gen_loadx_iphdrlen()
1211 struct slist
*s
, *s2
;
1213 s
= gen_llprefixlen();
1216 * There's a variable-length prefix preceding the
1217 * link-layer header. "s" points to a list of statements
1218 * that put the length of that prefix into the X register.
1219 * The 4*([k]&0xf) addressing mode can't be used, as we
1220 * don't have a constant offset, so we have to load the
1221 * value in question into the A register and add to it
1222 * the value from the X register.
1224 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1227 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1230 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1235 * The A register now contains the length of the
1236 * IP header. We need to add to it the length
1237 * of the prefix preceding the link-layer
1238 * header, which is still in the X register, and
1239 * move the result into the X register.
1241 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1242 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1245 * There is no variable-length header preceding the
1246 * link-layer header; if there's a fixed-length
1247 * header preceding it, its length is included in
1248 * the off_ variables, so it doesn't need to be added.
1250 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1256 static struct block
*
1263 s
= new_stmt(BPF_LD
|BPF_IMM
);
1265 b
= new_block(JMP(BPF_JEQ
));
1271 static inline struct block
*
1274 return gen_uncond(1);
1277 static inline struct block
*
1280 return gen_uncond(0);
1284 * Byte-swap a 32-bit number.
1285 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1286 * big-endian platforms.)
1288 #define SWAPLONG(y) \
1289 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1292 * Generate code to match a particular packet type.
1294 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1295 * value, if <= ETHERMTU. We use that to determine whether to
1296 * match the type/length field or to check the type/length field for
1297 * a value <= ETHERMTU to see whether it's a type field and then do
1298 * the appropriate test.
1300 static struct block
*
1301 gen_ether_linktype(proto
)
1304 struct block
*b0
, *b1
;
1310 case LLCSAP_NETBEUI
:
1312 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1313 * so we check the DSAP and SSAP.
1315 * LLCSAP_IP checks for IP-over-802.2, rather
1316 * than IP-over-Ethernet or IP-over-SNAP.
1318 * XXX - should we check both the DSAP and the
1319 * SSAP, like this, or should we check just the
1320 * DSAP, as we do for other types <= ETHERMTU
1321 * (i.e., other SAP values)?
1323 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1325 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1326 ((proto
<< 8) | proto
));
1334 * Ethernet_II frames, which are Ethernet
1335 * frames with a frame type of ETHERTYPE_IPX;
1337 * Ethernet_802.3 frames, which are 802.3
1338 * frames (i.e., the type/length field is
1339 * a length field, <= ETHERMTU, rather than
1340 * a type field) with the first two bytes
1341 * after the Ethernet/802.3 header being
1344 * Ethernet_802.2 frames, which are 802.3
1345 * frames with an 802.2 LLC header and
1346 * with the IPX LSAP as the DSAP in the LLC
1349 * Ethernet_SNAP frames, which are 802.3
1350 * frames with an LLC header and a SNAP
1351 * header and with an OUI of 0x000000
1352 * (encapsulated Ethernet) and a protocol
1353 * ID of ETHERTYPE_IPX in the SNAP header.
1355 * XXX - should we generate the same code both
1356 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1360 * This generates code to check both for the
1361 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1363 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1364 (bpf_int32
)LLCSAP_IPX
);
1365 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1370 * Now we add code to check for SNAP frames with
1371 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1373 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1377 * Now we generate code to check for 802.3
1378 * frames in general.
1380 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1384 * Now add the check for 802.3 frames before the
1385 * check for Ethernet_802.2 and Ethernet_802.3,
1386 * as those checks should only be done on 802.3
1387 * frames, not on Ethernet frames.
1392 * Now add the check for Ethernet_II frames, and
1393 * do that before checking for the other frame
1396 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1397 (bpf_int32
)ETHERTYPE_IPX
);
1401 case ETHERTYPE_ATALK
:
1402 case ETHERTYPE_AARP
:
1404 * EtherTalk (AppleTalk protocols on Ethernet link
1405 * layer) may use 802.2 encapsulation.
1409 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1410 * we check for an Ethernet type field less than
1411 * 1500, which means it's an 802.3 length field.
1413 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1417 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1418 * SNAP packets with an organization code of
1419 * 0x080007 (Apple, for Appletalk) and a protocol
1420 * type of ETHERTYPE_ATALK (Appletalk).
1422 * 802.2-encapsulated ETHERTYPE_AARP packets are
1423 * SNAP packets with an organization code of
1424 * 0x000000 (encapsulated Ethernet) and a protocol
1425 * type of ETHERTYPE_AARP (Appletalk ARP).
1427 if (proto
== ETHERTYPE_ATALK
)
1428 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1429 else /* proto == ETHERTYPE_AARP */
1430 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1434 * Check for Ethernet encapsulation (Ethertalk
1435 * phase 1?); we just check for the Ethernet
1438 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1444 if (proto
<= ETHERMTU
) {
1446 * This is an LLC SAP value, so the frames
1447 * that match would be 802.2 frames.
1448 * Check that the frame is an 802.2 frame
1449 * (i.e., that the length/type field is
1450 * a length field, <= ETHERMTU) and
1451 * then check the DSAP.
1453 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1455 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1461 * This is an Ethernet type, so compare
1462 * the length/type field with it (if
1463 * the frame is an 802.2 frame, the length
1464 * field will be <= ETHERMTU, and, as
1465 * "proto" is > ETHERMTU, this test
1466 * will fail and the frame won't match,
1467 * which is what we want).
1469 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1476 * Generate code to match a particular packet type.
1478 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1479 * value, if <= ETHERMTU. We use that to determine whether to
1480 * match the type field or to check the type field for the special
1481 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1483 static struct block
*
1484 gen_linux_sll_linktype(proto
)
1487 struct block
*b0
, *b1
;
1493 case LLCSAP_NETBEUI
:
1495 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1496 * so we check the DSAP and SSAP.
1498 * LLCSAP_IP checks for IP-over-802.2, rather
1499 * than IP-over-Ethernet or IP-over-SNAP.
1501 * XXX - should we check both the DSAP and the
1502 * SSAP, like this, or should we check just the
1503 * DSAP, as we do for other types <= ETHERMTU
1504 * (i.e., other SAP values)?
1506 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1507 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1508 ((proto
<< 8) | proto
));
1514 * Ethernet_II frames, which are Ethernet
1515 * frames with a frame type of ETHERTYPE_IPX;
1517 * Ethernet_802.3 frames, which have a frame
1518 * type of LINUX_SLL_P_802_3;
1520 * Ethernet_802.2 frames, which are 802.3
1521 * frames with an 802.2 LLC header (i.e, have
1522 * a frame type of LINUX_SLL_P_802_2) and
1523 * with the IPX LSAP as the DSAP in the LLC
1526 * Ethernet_SNAP frames, which are 802.3
1527 * frames with an LLC header and a SNAP
1528 * header and with an OUI of 0x000000
1529 * (encapsulated Ethernet) and a protocol
1530 * ID of ETHERTYPE_IPX in the SNAP header.
1532 * First, do the checks on LINUX_SLL_P_802_2
1533 * frames; generate the check for either
1534 * Ethernet_802.2 or Ethernet_SNAP frames, and
1535 * then put a check for LINUX_SLL_P_802_2 frames
1538 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1539 (bpf_int32
)LLCSAP_IPX
);
1540 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1543 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1547 * Now check for 802.3 frames and OR that with
1548 * the previous test.
1550 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1554 * Now add the check for Ethernet_II frames, and
1555 * do that before checking for the other frame
1558 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1559 (bpf_int32
)ETHERTYPE_IPX
);
1563 case ETHERTYPE_ATALK
:
1564 case ETHERTYPE_AARP
:
1566 * EtherTalk (AppleTalk protocols on Ethernet link
1567 * layer) may use 802.2 encapsulation.
1571 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1572 * we check for the 802.2 protocol type in the
1573 * "Ethernet type" field.
1575 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1578 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1579 * SNAP packets with an organization code of
1580 * 0x080007 (Apple, for Appletalk) and a protocol
1581 * type of ETHERTYPE_ATALK (Appletalk).
1583 * 802.2-encapsulated ETHERTYPE_AARP packets are
1584 * SNAP packets with an organization code of
1585 * 0x000000 (encapsulated Ethernet) and a protocol
1586 * type of ETHERTYPE_AARP (Appletalk ARP).
1588 if (proto
== ETHERTYPE_ATALK
)
1589 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1591 else /* proto == ETHERTYPE_AARP */
1592 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1597 * Check for Ethernet encapsulation (Ethertalk
1598 * phase 1?); we just check for the Ethernet
1601 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1607 if (proto
<= ETHERMTU
) {
1609 * This is an LLC SAP value, so the frames
1610 * that match would be 802.2 frames.
1611 * Check for the 802.2 protocol type
1612 * in the "Ethernet type" field, and
1613 * then check the DSAP.
1615 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1617 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1623 * This is an Ethernet type, so compare
1624 * the length/type field with it (if
1625 * the frame is an 802.2 frame, the length
1626 * field will be <= ETHERMTU, and, as
1627 * "proto" is > ETHERMTU, this test
1628 * will fail and the frame won't match,
1629 * which is what we want).
1631 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1638 insert_radiotap_load_llprefixlen(b
)
1641 struct slist
*s1
, *s2
;
1644 * Prepend to the statements in this block code to load the
1645 * length of the radiotap header into the register assigned
1646 * to hold that length, if one has been assigned.
1648 if (reg_ll_size
!= -1) {
1650 * The 2 bytes at offsets of 2 and 3 from the beginning
1651 * of the radiotap header are the length of the radiotap
1652 * header; unfortunately, it's little-endian, so we have
1653 * to load it a byte at a time and construct the value.
1657 * Load the high-order byte, at an offset of 3, shift it
1658 * left a byte, and put the result in the X register.
1660 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1662 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1665 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1669 * Load the next byte, at an offset of 2, and OR the
1670 * value from the X register into it.
1672 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1675 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1679 * Now allocate a register to hold that value and store
1682 s2
= new_stmt(BPF_ST
);
1683 s2
->s
.k
= reg_ll_size
;
1687 * Now move it into the X register.
1689 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1693 * Now append all the existing statements in this
1694 * block to these statements.
1696 sappend(s1
, b
->stmts
);
1703 insert_load_llprefixlen(b
)
1708 case DLT_IEEE802_11_RADIO
:
1709 insert_radiotap_load_llprefixlen(b
);
1714 static struct slist
*
1715 gen_radiotap_llprefixlen(void)
1719 if (reg_ll_size
== -1) {
1721 * We haven't yet assigned a register for the length
1722 * of the radiotap header; allocate one.
1724 reg_ll_size
= alloc_reg();
1728 * Load the register containing the radiotap length
1729 * into the X register.
1731 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1732 s
->s
.k
= reg_ll_size
;
1737 * Generate code to compute the link-layer header length, if necessary,
1738 * putting it into the X register, and to return either a pointer to a
1739 * "struct slist" for the list of statements in that code, or NULL if
1740 * no code is necessary.
1742 static struct slist
*
1743 gen_llprefixlen(void)
1747 case DLT_IEEE802_11_RADIO
:
1748 return gen_radiotap_llprefixlen();
1756 * Generate code to match a particular packet type by matching the
1757 * link-layer type field or fields in the 802.2 LLC header.
1759 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1760 * value, if <= ETHERMTU.
1762 static struct block
*
1766 struct block
*b0
, *b1
, *b2
;
1771 return gen_ether_linktype(proto
);
1779 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1783 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1790 case DLT_IEEE802_11
:
1791 case DLT_PRISM_HEADER
:
1794 case DLT_IEEE802_11_RADIO
:
1795 case DLT_ATM_RFC1483
:
1797 case DLT_IP_OVER_FC
:
1798 return gen_llc_linktype(proto
);
1804 * If "is_lane" is set, check for a LANE-encapsulated
1805 * version of this protocol, otherwise check for an
1806 * LLC-encapsulated version of this protocol.
1808 * We assume LANE means Ethernet, not Token Ring.
1812 * Check that the packet doesn't begin with an
1813 * LE Control marker. (We've already generated
1816 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1821 * Now generate an Ethernet test.
1823 b1
= gen_ether_linktype(proto
);
1828 * Check for LLC encapsulation and then check the
1831 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1832 b1
= gen_llc_linktype(proto
);
1840 return gen_linux_sll_linktype(proto
);
1845 case DLT_SLIP_BSDOS
:
1848 * These types don't provide any type field; packets
1851 * XXX - for IPv4, check for a version number of 4, and,
1852 * for IPv6, check for a version number of 6?
1858 case ETHERTYPE_IPV6
:
1860 return gen_true(); /* always true */
1863 return gen_false(); /* always false */
1870 case DLT_PPP_SERIAL
:
1873 * We use Ethernet protocol types inside libpcap;
1874 * map them to the corresponding PPP protocol types.
1883 case ETHERTYPE_IPV6
:
1892 case ETHERTYPE_ATALK
:
1906 * I'm assuming the "Bridging PDU"s that go
1907 * over PPP are Spanning Tree Protocol
1921 * We use Ethernet protocol types inside libpcap;
1922 * map them to the corresponding PPP protocol types.
1927 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
1928 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
1930 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
1935 case ETHERTYPE_IPV6
:
1945 case ETHERTYPE_ATALK
:
1959 * I'm assuming the "Bridging PDU"s that go
1960 * over PPP are Spanning Tree Protocol
1976 * For DLT_NULL, the link-layer header is a 32-bit
1977 * word containing an AF_ value in *host* byte order,
1978 * and for DLT_ENC, the link-layer header begins
1979 * with a 32-bit work containing an AF_ value in
1982 * In addition, if we're reading a saved capture file,
1983 * the host byte order in the capture may not be the
1984 * same as the host byte order on this machine.
1986 * For DLT_LOOP, the link-layer header is a 32-bit
1987 * word containing an AF_ value in *network* byte order.
1989 * XXX - AF_ values may, unfortunately, be platform-
1990 * dependent; for example, FreeBSD's AF_INET6 is 24
1991 * whilst NetBSD's and OpenBSD's is 26.
1993 * This means that, when reading a capture file, just
1994 * checking for our AF_INET6 value won't work if the
1995 * capture file came from another OS.
2004 case ETHERTYPE_IPV6
:
2011 * Not a type on which we support filtering.
2012 * XXX - support those that have AF_ values
2013 * #defined on this platform, at least?
2018 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2020 * The AF_ value is in host byte order, but
2021 * the BPF interpreter will convert it to
2022 * network byte order.
2024 * If this is a save file, and it's from a
2025 * machine with the opposite byte order to
2026 * ours, we byte-swap the AF_ value.
2028 * Then we run it through "htonl()", and
2029 * generate code to compare against the result.
2031 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2032 bpf_pcap
->sf
.swapped
)
2033 proto
= SWAPLONG(proto
);
2034 proto
= htonl(proto
);
2036 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2040 * af field is host byte order in contrast to the rest of
2043 if (proto
== ETHERTYPE_IP
)
2044 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2045 BPF_B
, (bpf_int32
)AF_INET
));
2047 else if (proto
== ETHERTYPE_IPV6
)
2048 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2049 BPF_B
, (bpf_int32
)AF_INET6
));
2057 case DLT_ARCNET_LINUX
:
2059 * XXX should we check for first fragment if the protocol
2068 case ETHERTYPE_IPV6
:
2069 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2070 (bpf_int32
)ARCTYPE_INET6
));
2074 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2075 (bpf_int32
)ARCTYPE_IP
);
2076 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2077 (bpf_int32
)ARCTYPE_IP_OLD
);
2082 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2083 (bpf_int32
)ARCTYPE_ARP
);
2084 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2085 (bpf_int32
)ARCTYPE_ARP_OLD
);
2089 case ETHERTYPE_REVARP
:
2090 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2091 (bpf_int32
)ARCTYPE_REVARP
));
2093 case ETHERTYPE_ATALK
:
2094 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2095 (bpf_int32
)ARCTYPE_ATALK
));
2102 case ETHERTYPE_ATALK
:
2112 * XXX - assumes a 2-byte Frame Relay header with
2113 * DLCI and flags. What if the address is longer?
2119 * Check for the special NLPID for IP.
2121 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2124 case ETHERTYPE_IPV6
:
2126 * Check for the special NLPID for IPv6.
2128 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2133 * Check for several OSI protocols.
2135 * Frame Relay packets typically have an OSI
2136 * NLPID at the beginning; we check for each
2139 * What we check for is the NLPID and a frame
2140 * control field of UI, i.e. 0x03 followed
2143 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2144 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2145 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2156 case DLT_JUNIPER_MLFR
:
2157 case DLT_JUNIPER_MLPPP
:
2158 case DLT_JUNIPER_ATM1
:
2159 case DLT_JUNIPER_ATM2
:
2160 /* just lets verify the magic number for now -
2161 * on ATM we may have up to 6 different encapsulations on the wire
2162 * and need a lot of heuristics to figure out that the payload
2165 * FIXME encapsulation specific BPF_ filters
2167 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2169 case DLT_LINUX_IRDA
:
2170 bpf_error("IrDA link-layer type filtering not implemented");
2173 bpf_error("DOCSIS link-layer type filtering not implemented");
2175 case DLT_LINUX_LAPD
:
2176 bpf_error("LAPD link-layer type filtering not implemented");
2180 * All the types that have no encapsulation should either be
2181 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2182 * all packets are IP packets, or should be handled in some
2183 * special case, if none of them are (if some are and some
2184 * aren't, the lack of encapsulation is a problem, as we'd
2185 * have to find some other way of determining the packet type).
2187 * Therefore, if "off_linktype" is -1, there's an error.
2189 if (off_linktype
== (u_int
)-1)
2193 * Any type not handled above should always have an Ethernet
2194 * type at an offset of "off_linktype". (PPP is partially
2195 * handled above - the protocol type is mapped from the
2196 * Ethernet and LLC types we use internally to the corresponding
2197 * PPP type - but the PPP type is always specified by a value
2198 * at "off_linktype", so we don't have to do the code generation
2201 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2205 * Check for an LLC SNAP packet with a given organization code and
2206 * protocol type; we check the entire contents of the 802.2 LLC and
2207 * snap headers, checking for DSAP and SSAP of SNAP and a control
2208 * field of 0x03 in the LLC header, and for the specified organization
2209 * code and protocol type in the SNAP header.
2211 static struct block
*
2212 gen_snap(orgcode
, ptype
, offset
)
2213 bpf_u_int32 orgcode
;
2217 u_char snapblock
[8];
2219 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2220 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2221 snapblock
[2] = 0x03; /* control = UI */
2222 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2223 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2224 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2225 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2226 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2227 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2231 * Generate code to match a particular packet type, for link-layer types
2232 * using 802.2 LLC headers.
2234 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2235 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2237 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2238 * value, if <= ETHERMTU. We use that to determine whether to
2239 * match the DSAP or both DSAP and LSAP or to check the OUI and
2240 * protocol ID in a SNAP header.
2242 static struct block
*
2243 gen_llc_linktype(proto
)
2247 * XXX - handle token-ring variable-length header.
2253 case LLCSAP_NETBEUI
:
2255 * XXX - should we check both the DSAP and the
2256 * SSAP, like this, or should we check just the
2257 * DSAP, as we do for other types <= ETHERMTU
2258 * (i.e., other SAP values)?
2260 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2261 ((proto
<< 8) | proto
));
2265 * XXX - are there ever SNAP frames for IPX on
2266 * non-Ethernet 802.x networks?
2268 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2269 (bpf_int32
)LLCSAP_IPX
);
2271 case ETHERTYPE_ATALK
:
2273 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2274 * SNAP packets with an organization code of
2275 * 0x080007 (Apple, for Appletalk) and a protocol
2276 * type of ETHERTYPE_ATALK (Appletalk).
2278 * XXX - check for an organization code of
2279 * encapsulated Ethernet as well?
2281 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2285 * XXX - we don't have to check for IPX 802.3
2286 * here, but should we check for the IPX Ethertype?
2288 if (proto
<= ETHERMTU
) {
2290 * This is an LLC SAP value, so check
2293 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2297 * This is an Ethernet type; we assume that it's
2298 * unlikely that it'll appear in the right place
2299 * at random, and therefore check only the
2300 * location that would hold the Ethernet type
2301 * in a SNAP frame with an organization code of
2302 * 0x000000 (encapsulated Ethernet).
2304 * XXX - if we were to check for the SNAP DSAP and
2305 * LSAP, as per XXX, and were also to check for an
2306 * organization code of 0x000000 (encapsulated
2307 * Ethernet), we'd do
2309 * return gen_snap(0x000000, proto,
2312 * here; for now, we don't, as per the above.
2313 * I don't know whether it's worth the extra CPU
2314 * time to do the right check or not.
2316 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2322 static struct block
*
2323 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2327 u_int src_off
, dst_off
;
2329 struct block
*b0
, *b1
;
2343 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2344 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2350 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2351 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2358 b0
= gen_linktype(proto
);
2359 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2365 static struct block
*
2366 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2367 struct in6_addr
*addr
;
2368 struct in6_addr
*mask
;
2370 u_int src_off
, dst_off
;
2372 struct block
*b0
, *b1
;
2387 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2388 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2394 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2395 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2402 /* this order is important */
2403 a
= (u_int32_t
*)addr
;
2404 m
= (u_int32_t
*)mask
;
2405 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2406 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2408 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2410 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2412 b0
= gen_linktype(proto
);
2418 static struct block
*
2419 gen_ehostop(eaddr
, dir
)
2420 register const u_char
*eaddr
;
2423 register struct block
*b0
, *b1
;
2427 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2430 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2433 b0
= gen_ehostop(eaddr
, Q_SRC
);
2434 b1
= gen_ehostop(eaddr
, Q_DST
);
2440 b0
= gen_ehostop(eaddr
, Q_SRC
);
2441 b1
= gen_ehostop(eaddr
, Q_DST
);
2450 * Like gen_ehostop, but for DLT_FDDI
2452 static struct block
*
2453 gen_fhostop(eaddr
, dir
)
2454 register const u_char
*eaddr
;
2457 struct block
*b0
, *b1
;
2462 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2464 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2469 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2471 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2475 b0
= gen_fhostop(eaddr
, Q_SRC
);
2476 b1
= gen_fhostop(eaddr
, Q_DST
);
2482 b0
= gen_fhostop(eaddr
, Q_SRC
);
2483 b1
= gen_fhostop(eaddr
, Q_DST
);
2492 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2494 static struct block
*
2495 gen_thostop(eaddr
, dir
)
2496 register const u_char
*eaddr
;
2499 register struct block
*b0
, *b1
;
2503 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2506 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2509 b0
= gen_thostop(eaddr
, Q_SRC
);
2510 b1
= gen_thostop(eaddr
, Q_DST
);
2516 b0
= gen_thostop(eaddr
, Q_SRC
);
2517 b1
= gen_thostop(eaddr
, Q_DST
);
2526 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2528 static struct block
*
2529 gen_wlanhostop(eaddr
, dir
)
2530 register const u_char
*eaddr
;
2533 register struct block
*b0
, *b1
, *b2
;
2534 register struct slist
*s
;
2541 * For control frames, there is no SA.
2543 * For management frames, SA is at an
2544 * offset of 10 from the beginning of
2547 * For data frames, SA is at an offset
2548 * of 10 from the beginning of the packet
2549 * if From DS is clear, at an offset of
2550 * 16 from the beginning of the packet
2551 * if From DS is set and To DS is clear,
2552 * and an offset of 24 from the beginning
2553 * of the packet if From DS is set and To DS
2558 * Generate the tests to be done for data frames
2561 * First, check for To DS set, i.e. check "link[1] & 0x01".
2563 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2564 b1
= new_block(JMP(BPF_JSET
));
2565 b1
->s
.k
= 0x01; /* To DS */
2569 * If To DS is set, the SA is at 24.
2571 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2575 * Now, check for To DS not set, i.e. check
2576 * "!(link[1] & 0x01)".
2578 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2579 b2
= new_block(JMP(BPF_JSET
));
2580 b2
->s
.k
= 0x01; /* To DS */
2585 * If To DS is not set, the SA is at 16.
2587 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2591 * Now OR together the last two checks. That gives
2592 * the complete set of checks for data frames with
2598 * Now check for From DS being set, and AND that with
2599 * the ORed-together checks.
2601 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2602 b1
= new_block(JMP(BPF_JSET
));
2603 b1
->s
.k
= 0x02; /* From DS */
2608 * Now check for data frames with From DS not set.
2610 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2611 b2
= new_block(JMP(BPF_JSET
));
2612 b2
->s
.k
= 0x02; /* From DS */
2617 * If From DS isn't set, the SA is at 10.
2619 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2623 * Now OR together the checks for data frames with
2624 * From DS not set and for data frames with From DS
2625 * set; that gives the checks done for data frames.
2630 * Now check for a data frame.
2631 * I.e, check "link[0] & 0x08".
2633 gen_load_a(OR_LINK
, 0, BPF_B
);
2634 b1
= new_block(JMP(BPF_JSET
));
2639 * AND that with the checks done for data frames.
2644 * If the high-order bit of the type value is 0, this
2645 * is a management frame.
2646 * I.e, check "!(link[0] & 0x08)".
2648 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2649 b2
= new_block(JMP(BPF_JSET
));
2655 * For management frames, the SA is at 10.
2657 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2661 * OR that with the checks done for data frames.
2662 * That gives the checks done for management and
2668 * If the low-order bit of the type value is 1,
2669 * this is either a control frame or a frame
2670 * with a reserved type, and thus not a
2673 * I.e., check "!(link[0] & 0x04)".
2675 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2676 b1
= new_block(JMP(BPF_JSET
));
2682 * AND that with the checks for data and management
2692 * For control frames, there is no DA.
2694 * For management frames, DA is at an
2695 * offset of 4 from the beginning of
2698 * For data frames, DA is at an offset
2699 * of 4 from the beginning of the packet
2700 * if To DS is clear and at an offset of
2701 * 16 from the beginning of the packet
2706 * Generate the tests to be done for data frames.
2708 * First, check for To DS set, i.e. "link[1] & 0x01".
2710 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2711 b1
= new_block(JMP(BPF_JSET
));
2712 b1
->s
.k
= 0x01; /* To DS */
2716 * If To DS is set, the DA is at 16.
2718 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2722 * Now, check for To DS not set, i.e. check
2723 * "!(link[1] & 0x01)".
2725 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2726 b2
= new_block(JMP(BPF_JSET
));
2727 b2
->s
.k
= 0x01; /* To DS */
2732 * If To DS is not set, the DA is at 4.
2734 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2738 * Now OR together the last two checks. That gives
2739 * the complete set of checks for data frames.
2744 * Now check for a data frame.
2745 * I.e, check "link[0] & 0x08".
2747 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2748 b1
= new_block(JMP(BPF_JSET
));
2753 * AND that with the checks done for data frames.
2758 * If the high-order bit of the type value is 0, this
2759 * is a management frame.
2760 * I.e, check "!(link[0] & 0x08)".
2762 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2763 b2
= new_block(JMP(BPF_JSET
));
2769 * For management frames, the DA is at 4.
2771 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2775 * OR that with the checks done for data frames.
2776 * That gives the checks done for management and
2782 * If the low-order bit of the type value is 1,
2783 * this is either a control frame or a frame
2784 * with a reserved type, and thus not a
2787 * I.e., check "!(link[0] & 0x04)".
2789 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2790 b1
= new_block(JMP(BPF_JSET
));
2796 * AND that with the checks for data and management
2803 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2804 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2810 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2811 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2820 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2821 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2822 * as the RFC states.)
2824 static struct block
*
2825 gen_ipfchostop(eaddr
, dir
)
2826 register const u_char
*eaddr
;
2829 register struct block
*b0
, *b1
;
2833 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2836 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2839 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2840 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2846 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2847 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2856 * This is quite tricky because there may be pad bytes in front of the
2857 * DECNET header, and then there are two possible data packet formats that
2858 * carry both src and dst addresses, plus 5 packet types in a format that
2859 * carries only the src node, plus 2 types that use a different format and
2860 * also carry just the src node.
2864 * Instead of doing those all right, we just look for data packets with
2865 * 0 or 1 bytes of padding. If you want to look at other packets, that
2866 * will require a lot more hacking.
2868 * To add support for filtering on DECNET "areas" (network numbers)
2869 * one would want to add a "mask" argument to this routine. That would
2870 * make the filter even more inefficient, although one could be clever
2871 * and not generate masking instructions if the mask is 0xFFFF.
2873 static struct block
*
2874 gen_dnhostop(addr
, dir
)
2878 struct block
*b0
, *b1
, *b2
, *tmp
;
2879 u_int offset_lh
; /* offset if long header is received */
2880 u_int offset_sh
; /* offset if short header is received */
2885 offset_sh
= 1; /* follows flags */
2886 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2890 offset_sh
= 3; /* follows flags, dstnode */
2891 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2895 /* Inefficient because we do our Calvinball dance twice */
2896 b0
= gen_dnhostop(addr
, Q_SRC
);
2897 b1
= gen_dnhostop(addr
, Q_DST
);
2903 /* Inefficient because we do our Calvinball dance twice */
2904 b0
= gen_dnhostop(addr
, Q_SRC
);
2905 b1
= gen_dnhostop(addr
, Q_DST
);
2910 bpf_error("ISO host filtering not implemented");
2915 b0
= gen_linktype(ETHERTYPE_DN
);
2916 /* Check for pad = 1, long header case */
2917 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2918 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2919 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
2920 BPF_H
, (bpf_int32
)ntohs(addr
));
2922 /* Check for pad = 0, long header case */
2923 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2924 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2927 /* Check for pad = 1, short header case */
2928 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2929 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2930 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2933 /* Check for pad = 0, short header case */
2934 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2935 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2939 /* Combine with test for linktype */
2944 static struct block
*
2945 gen_host(addr
, mask
, proto
, dir
)
2951 struct block
*b0
, *b1
;
2956 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2957 if (off_linktype
!= (u_int
)-1) {
2958 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2960 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2966 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
2969 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
2972 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
2975 bpf_error("'tcp' modifier applied to host");
2978 bpf_error("'sctp' modifier applied to host");
2981 bpf_error("'udp' modifier applied to host");
2984 bpf_error("'icmp' modifier applied to host");
2987 bpf_error("'igmp' modifier applied to host");
2990 bpf_error("'igrp' modifier applied to host");
2993 bpf_error("'pim' modifier applied to host");
2996 bpf_error("'vrrp' modifier applied to host");
2999 bpf_error("ATALK host filtering not implemented");
3002 bpf_error("AARP host filtering not implemented");
3005 return gen_dnhostop(addr
, dir
);
3008 bpf_error("SCA host filtering not implemented");
3011 bpf_error("LAT host filtering not implemented");
3014 bpf_error("MOPDL host filtering not implemented");
3017 bpf_error("MOPRC host filtering not implemented");
3021 bpf_error("'ip6' modifier applied to ip host");
3024 bpf_error("'icmp6' modifier applied to host");
3028 bpf_error("'ah' modifier applied to host");
3031 bpf_error("'esp' modifier applied to host");
3034 bpf_error("ISO host filtering not implemented");
3037 bpf_error("'esis' modifier applied to host");
3040 bpf_error("'isis' modifier applied to host");
3043 bpf_error("'clnp' modifier applied to host");
3046 bpf_error("'stp' modifier applied to host");
3049 bpf_error("IPX host filtering not implemented");
3052 bpf_error("'netbeui' modifier applied to host");
3061 static struct block
*
3062 gen_host6(addr
, mask
, proto
, dir
)
3063 struct in6_addr
*addr
;
3064 struct in6_addr
*mask
;
3071 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3074 bpf_error("'ip' modifier applied to ip6 host");
3077 bpf_error("'rarp' modifier applied to ip6 host");
3080 bpf_error("'arp' modifier applied to ip6 host");
3083 bpf_error("'sctp' modifier applied to host");
3086 bpf_error("'tcp' modifier applied to host");
3089 bpf_error("'udp' modifier applied to host");
3092 bpf_error("'icmp' modifier applied to host");
3095 bpf_error("'igmp' modifier applied to host");
3098 bpf_error("'igrp' modifier applied to host");
3101 bpf_error("'pim' modifier applied to host");
3104 bpf_error("'vrrp' modifier applied to host");
3107 bpf_error("ATALK host filtering not implemented");
3110 bpf_error("AARP host filtering not implemented");
3113 bpf_error("'decnet' modifier applied to ip6 host");
3116 bpf_error("SCA host filtering not implemented");
3119 bpf_error("LAT host filtering not implemented");
3122 bpf_error("MOPDL host filtering not implemented");
3125 bpf_error("MOPRC host filtering not implemented");
3128 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3131 bpf_error("'icmp6' modifier applied to host");
3134 bpf_error("'ah' modifier applied to host");
3137 bpf_error("'esp' modifier applied to host");
3140 bpf_error("ISO host filtering not implemented");
3143 bpf_error("'esis' modifier applied to host");
3146 bpf_error("'isis' modifier applied to host");
3149 bpf_error("'clnp' modifier applied to host");
3152 bpf_error("'stp' modifier applied to host");
3155 bpf_error("IPX host filtering not implemented");
3158 bpf_error("'netbeui' modifier applied to host");
3168 static struct block
*
3169 gen_gateway(eaddr
, alist
, proto
, dir
)
3170 const u_char
*eaddr
;
3171 bpf_u_int32
**alist
;
3175 struct block
*b0
, *b1
, *tmp
;
3178 bpf_error("direction applied to 'gateway'");
3185 if (linktype
== DLT_EN10MB
)
3186 b0
= gen_ehostop(eaddr
, Q_OR
);
3187 else if (linktype
== DLT_FDDI
)
3188 b0
= gen_fhostop(eaddr
, Q_OR
);
3189 else if (linktype
== DLT_IEEE802
)
3190 b0
= gen_thostop(eaddr
, Q_OR
);
3191 else if (linktype
== DLT_IEEE802_11
)
3192 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3193 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3195 * Check that the packet doesn't begin with an
3196 * LE Control marker. (We've already generated
3199 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3204 * Now check the MAC address.
3206 b0
= gen_ehostop(eaddr
, Q_OR
);
3208 } else if (linktype
== DLT_IP_OVER_FC
)
3209 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3212 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3214 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3216 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3224 bpf_error("illegal modifier of 'gateway'");
3230 gen_proto_abbrev(proto
)
3239 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3241 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3247 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3249 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3255 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3257 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3263 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3266 #ifndef IPPROTO_IGMP
3267 #define IPPROTO_IGMP 2
3271 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3274 #ifndef IPPROTO_IGRP
3275 #define IPPROTO_IGRP 9
3278 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3282 #define IPPROTO_PIM 103
3286 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3288 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3293 #ifndef IPPROTO_VRRP
3294 #define IPPROTO_VRRP 112
3298 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3302 b1
= gen_linktype(ETHERTYPE_IP
);
3306 b1
= gen_linktype(ETHERTYPE_ARP
);
3310 b1
= gen_linktype(ETHERTYPE_REVARP
);
3314 bpf_error("link layer applied in wrong context");
3317 b1
= gen_linktype(ETHERTYPE_ATALK
);
3321 b1
= gen_linktype(ETHERTYPE_AARP
);
3325 b1
= gen_linktype(ETHERTYPE_DN
);
3329 b1
= gen_linktype(ETHERTYPE_SCA
);
3333 b1
= gen_linktype(ETHERTYPE_LAT
);
3337 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3341 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3346 b1
= gen_linktype(ETHERTYPE_IPV6
);
3349 #ifndef IPPROTO_ICMPV6
3350 #define IPPROTO_ICMPV6 58
3353 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3358 #define IPPROTO_AH 51
3361 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3363 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3369 #define IPPROTO_ESP 50
3372 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3374 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3380 b1
= gen_linktype(LLCSAP_ISONS
);
3384 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3388 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3391 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3392 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3393 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3395 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3397 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3399 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3403 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3404 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3405 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3407 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3409 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3411 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3415 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3416 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3417 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3419 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3424 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3425 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3430 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3431 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3433 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3435 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3440 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3441 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3446 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3447 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3452 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3456 b1
= gen_linktype(LLCSAP_8021D
);
3460 b1
= gen_linktype(LLCSAP_IPX
);
3464 b1
= gen_linktype(LLCSAP_NETBEUI
);
3473 static struct block
*
3480 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3481 b
= new_block(JMP(BPF_JSET
));
3490 * Generate a comparison to a port value in the transport-layer header
3491 * at the specified offset from the beginning of that header.
3493 * XXX - this handles a variable-length prefix preceding the link-layer
3494 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3495 * variable-length link-layer headers (such as Token Ring or 802.11
3498 static struct block
*
3499 gen_portatom(off
, v
)
3503 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3507 static struct block
*
3508 gen_portatom6(off
, v
)
3512 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3517 gen_portop(port
, proto
, dir
)
3518 int port
, proto
, dir
;
3520 struct block
*b0
, *b1
, *tmp
;
3522 /* ip proto 'proto' */
3523 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3529 b1
= gen_portatom(0, (bpf_int32
)port
);
3533 b1
= gen_portatom(2, (bpf_int32
)port
);
3538 tmp
= gen_portatom(0, (bpf_int32
)port
);
3539 b1
= gen_portatom(2, (bpf_int32
)port
);
3544 tmp
= gen_portatom(0, (bpf_int32
)port
);
3545 b1
= gen_portatom(2, (bpf_int32
)port
);
3557 static struct block
*
3558 gen_port(port
, ip_proto
, dir
)
3563 struct block
*b0
, *b1
, *tmp
;
3568 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3569 * not LLC encapsulation with LLCSAP_IP.
3571 * For IEEE 802 networks - which includes 802.5 token ring
3572 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3573 * says that SNAP encapsulation is used, not LLC encapsulation
3576 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3577 * RFC 2225 say that SNAP encapsulation is used, not LLC
3578 * encapsulation with LLCSAP_IP.
3580 * So we always check for ETHERTYPE_IP.
3582 b0
= gen_linktype(ETHERTYPE_IP
);
3588 b1
= gen_portop(port
, ip_proto
, dir
);
3592 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3593 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3595 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3608 gen_portop6(port
, proto
, dir
)
3609 int port
, proto
, dir
;
3611 struct block
*b0
, *b1
, *tmp
;
3613 /* ip6 proto 'proto' */
3614 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3618 b1
= gen_portatom6(0, (bpf_int32
)port
);
3622 b1
= gen_portatom6(2, (bpf_int32
)port
);
3627 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3628 b1
= gen_portatom6(2, (bpf_int32
)port
);
3633 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3634 b1
= gen_portatom6(2, (bpf_int32
)port
);
3646 static struct block
*
3647 gen_port6(port
, ip_proto
, dir
)
3652 struct block
*b0
, *b1
, *tmp
;
3654 /* link proto ip6 */
3655 b0
= gen_linktype(ETHERTYPE_IPV6
);
3661 b1
= gen_portop6(port
, ip_proto
, dir
);
3665 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3666 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3668 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3680 /* gen_portrange code */
3681 static struct block
*
3682 gen_portrangeatom(off
, v1
, v2
)
3686 struct block
*b1
, *b2
;
3690 * Reverse the order of the ports, so v1 is the lower one.
3699 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3700 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3708 gen_portrangeop(port1
, port2
, proto
, dir
)
3713 struct block
*b0
, *b1
, *tmp
;
3715 /* ip proto 'proto' */
3716 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3722 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3726 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3731 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3732 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3737 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3738 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3750 static struct block
*
3751 gen_portrange(port1
, port2
, ip_proto
, dir
)
3756 struct block
*b0
, *b1
, *tmp
;
3759 b0
= gen_linktype(ETHERTYPE_IP
);
3765 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3769 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3770 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3772 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3784 static struct block
*
3785 gen_portrangeatom6(off
, v1
, v2
)
3789 struct block
*b1
, *b2
;
3793 * Reverse the order of the ports, so v1 is the lower one.
3802 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3803 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3811 gen_portrangeop6(port1
, port2
, proto
, dir
)
3816 struct block
*b0
, *b1
, *tmp
;
3818 /* ip6 proto 'proto' */
3819 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3823 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3827 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3832 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3833 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3838 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3839 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3851 static struct block
*
3852 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3857 struct block
*b0
, *b1
, *tmp
;
3859 /* link proto ip6 */
3860 b0
= gen_linktype(ETHERTYPE_IPV6
);
3866 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3870 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3871 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3873 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3886 lookup_proto(name
, proto
)
3887 register const char *name
;
3897 v
= pcap_nametoproto(name
);
3898 if (v
== PROTO_UNDEF
)
3899 bpf_error("unknown ip proto '%s'", name
);
3903 /* XXX should look up h/w protocol type based on linktype */
3904 v
= pcap_nametoeproto(name
);
3905 if (v
== PROTO_UNDEF
) {
3906 v
= pcap_nametollc(name
);
3907 if (v
== PROTO_UNDEF
)
3908 bpf_error("unknown ether proto '%s'", name
);
3913 if (strcmp(name
, "esis") == 0)
3915 else if (strcmp(name
, "isis") == 0)
3917 else if (strcmp(name
, "clnp") == 0)
3920 bpf_error("unknown osi proto '%s'", name
);
3940 static struct block
*
3941 gen_protochain(v
, proto
, dir
)
3946 #ifdef NO_PROTOCHAIN
3947 return gen_proto(v
, proto
, dir
);
3949 struct block
*b0
, *b
;
3950 struct slist
*s
[100];
3951 int fix2
, fix3
, fix4
, fix5
;
3952 int ahcheck
, again
, end
;
3954 int reg2
= alloc_reg();
3956 memset(s
, 0, sizeof(s
));
3957 fix2
= fix3
= fix4
= fix5
= 0;
3964 b0
= gen_protochain(v
, Q_IP
, dir
);
3965 b
= gen_protochain(v
, Q_IPV6
, dir
);
3969 bpf_error("bad protocol applied for 'protochain'");
3974 * We don't handle variable-length radiotap here headers yet.
3975 * We might want to add BPF instructions to do the protochain
3976 * work, to simplify that and, on platforms that have a BPF
3977 * interpreter with the new instructions, let the filtering
3978 * be done in the kernel. (We already require a modified BPF
3979 * engine to do the protochain stuff, to support backward
3980 * branches, and backward branch support is unlikely to appear
3981 * in kernel BPF engines.)
3983 if (linktype
== DLT_IEEE802_11_RADIO
)
3984 bpf_error("'protochain' not supported with radiotap headers");
3986 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3989 * s[0] is a dummy entry to protect other BPF insn from damage
3990 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3991 * hard to find interdependency made by jump table fixup.
3994 s
[i
] = new_stmt(0); /*dummy*/
3999 b0
= gen_linktype(ETHERTYPE_IP
);
4002 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4003 s
[i
]->s
.k
= off_nl
+ 9;
4005 /* X = ip->ip_hl << 2 */
4006 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4012 b0
= gen_linktype(ETHERTYPE_IPV6
);
4014 /* A = ip6->ip_nxt */
4015 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4016 s
[i
]->s
.k
= off_nl
+ 6;
4018 /* X = sizeof(struct ip6_hdr) */
4019 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4025 bpf_error("unsupported proto to gen_protochain");
4029 /* again: if (A == v) goto end; else fall through; */
4031 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4033 s
[i
]->s
.jt
= NULL
; /*later*/
4034 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4038 #ifndef IPPROTO_NONE
4039 #define IPPROTO_NONE 59
4041 /* if (A == IPPROTO_NONE) goto end */
4042 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4043 s
[i
]->s
.jt
= NULL
; /*later*/
4044 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4045 s
[i
]->s
.k
= IPPROTO_NONE
;
4046 s
[fix5
]->s
.jf
= s
[i
];
4051 if (proto
== Q_IPV6
) {
4052 int v6start
, v6end
, v6advance
, j
;
4055 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4056 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4057 s
[i
]->s
.jt
= NULL
; /*later*/
4058 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4059 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4060 s
[fix2
]->s
.jf
= s
[i
];
4062 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4063 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4064 s
[i
]->s
.jt
= NULL
; /*later*/
4065 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4066 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4068 /* if (A == IPPROTO_ROUTING) goto v6advance */
4069 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4070 s
[i
]->s
.jt
= NULL
; /*later*/
4071 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4072 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4074 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4075 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4076 s
[i
]->s
.jt
= NULL
; /*later*/
4077 s
[i
]->s
.jf
= NULL
; /*later*/
4078 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4089 * X = X + (P[X + 1] + 1) * 8;
4092 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4094 /* A = P[X + packet head] */
4095 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4099 s
[i
] = new_stmt(BPF_ST
);
4103 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4106 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4110 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4112 /* A = P[X + packet head]; */
4113 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4117 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4121 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4125 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4128 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4132 /* goto again; (must use BPF_JA for backward jump) */
4133 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4134 s
[i
]->s
.k
= again
- i
- 1;
4135 s
[i
- 1]->s
.jf
= s
[i
];
4139 for (j
= v6start
; j
<= v6end
; j
++)
4140 s
[j
]->s
.jt
= s
[v6advance
];
4145 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4147 s
[fix2
]->s
.jf
= s
[i
];
4153 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4154 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4155 s
[i
]->s
.jt
= NULL
; /*later*/
4156 s
[i
]->s
.jf
= NULL
; /*later*/
4157 s
[i
]->s
.k
= IPPROTO_AH
;
4159 s
[fix3
]->s
.jf
= s
[ahcheck
];
4166 * X = X + (P[X + 1] + 2) * 4;
4169 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4171 /* A = P[X + packet head]; */
4172 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4176 s
[i
] = new_stmt(BPF_ST
);
4180 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4183 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4187 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4189 /* A = P[X + packet head] */
4190 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4194 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4198 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4202 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4205 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4209 /* goto again; (must use BPF_JA for backward jump) */
4210 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4211 s
[i
]->s
.k
= again
- i
- 1;
4216 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4218 s
[fix2
]->s
.jt
= s
[end
];
4219 s
[fix4
]->s
.jf
= s
[end
];
4220 s
[fix5
]->s
.jt
= s
[end
];
4227 for (i
= 0; i
< max
- 1; i
++)
4228 s
[i
]->next
= s
[i
+ 1];
4229 s
[max
- 1]->next
= NULL
;
4234 b
= new_block(JMP(BPF_JEQ
));
4235 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4246 * Generate code that checks whether the packet is a packet for protocol
4247 * <proto> and whether the type field in that protocol's header has
4248 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4249 * IP packet and checks the protocol number in the IP header against <v>.
4251 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4252 * against Q_IP and Q_IPV6.
4254 static struct block
*
4255 gen_proto(v
, proto
, dir
)
4260 struct block
*b0
, *b1
;
4262 if (dir
!= Q_DEFAULT
)
4263 bpf_error("direction applied to 'proto'");
4268 b0
= gen_proto(v
, Q_IP
, dir
);
4269 b1
= gen_proto(v
, Q_IPV6
, dir
);
4277 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4278 * not LLC encapsulation with LLCSAP_IP.
4280 * For IEEE 802 networks - which includes 802.5 token ring
4281 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4282 * says that SNAP encapsulation is used, not LLC encapsulation
4285 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4286 * RFC 2225 say that SNAP encapsulation is used, not LLC
4287 * encapsulation with LLCSAP_IP.
4289 * So we always check for ETHERTYPE_IP.
4291 b0
= gen_linktype(ETHERTYPE_IP
);
4293 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4295 b1
= gen_protochain(v
, Q_IP
);
4305 * Frame Relay packets typically have an OSI
4306 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4307 * generates code to check for all the OSI
4308 * NLPIDs, so calling it and then adding a check
4309 * for the particular NLPID for which we're
4310 * looking is bogus, as we can just check for
4313 * What we check for is the NLPID and a frame
4314 * control field value of UI, i.e. 0x03 followed
4317 * XXX - assumes a 2-byte Frame Relay header with
4318 * DLCI and flags. What if the address is longer?
4320 * XXX - what about SNAP-encapsulated frames?
4322 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4328 * Cisco uses an Ethertype lookalike - for OSI,
4331 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4332 /* OSI in C-HDLC is stuffed with a fudge byte */
4333 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4338 b0
= gen_linktype(LLCSAP_ISONS
);
4339 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4345 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4347 * 4 is the offset of the PDU type relative to the IS-IS
4350 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4355 bpf_error("arp does not encapsulate another protocol");
4359 bpf_error("rarp does not encapsulate another protocol");
4363 bpf_error("atalk encapsulation is not specifiable");
4367 bpf_error("decnet encapsulation is not specifiable");
4371 bpf_error("sca does not encapsulate another protocol");
4375 bpf_error("lat does not encapsulate another protocol");
4379 bpf_error("moprc does not encapsulate another protocol");
4383 bpf_error("mopdl does not encapsulate another protocol");
4387 return gen_linktype(v
);
4390 bpf_error("'udp proto' is bogus");
4394 bpf_error("'tcp proto' is bogus");
4398 bpf_error("'sctp proto' is bogus");
4402 bpf_error("'icmp proto' is bogus");
4406 bpf_error("'igmp proto' is bogus");
4410 bpf_error("'igrp proto' is bogus");
4414 bpf_error("'pim proto' is bogus");
4418 bpf_error("'vrrp proto' is bogus");
4423 b0
= gen_linktype(ETHERTYPE_IPV6
);
4425 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4427 b1
= gen_protochain(v
, Q_IPV6
);
4433 bpf_error("'icmp6 proto' is bogus");
4437 bpf_error("'ah proto' is bogus");
4440 bpf_error("'ah proto' is bogus");
4443 bpf_error("'stp proto' is bogus");
4446 bpf_error("'ipx proto' is bogus");
4449 bpf_error("'netbeui proto' is bogus");
4460 register const char *name
;
4463 int proto
= q
.proto
;
4467 bpf_u_int32 mask
, addr
;
4469 bpf_u_int32
**alist
;
4472 struct sockaddr_in
*sin
;
4473 struct sockaddr_in6
*sin6
;
4474 struct addrinfo
*res
, *res0
;
4475 struct in6_addr mask128
;
4477 struct block
*b
, *tmp
;
4478 int port
, real_proto
;
4484 addr
= pcap_nametonetaddr(name
);
4486 bpf_error("unknown network '%s'", name
);
4487 /* Left justify network addr and calculate its network mask */
4489 while (addr
&& (addr
& 0xff000000) == 0) {
4493 return gen_host(addr
, mask
, proto
, dir
);
4497 if (proto
== Q_LINK
) {
4501 eaddr
= pcap_ether_hostton(name
);
4504 "unknown ether host '%s'", name
);
4505 b
= gen_ehostop(eaddr
, dir
);
4510 eaddr
= pcap_ether_hostton(name
);
4513 "unknown FDDI host '%s'", name
);
4514 b
= gen_fhostop(eaddr
, dir
);
4519 eaddr
= pcap_ether_hostton(name
);
4522 "unknown token ring host '%s'", name
);
4523 b
= gen_thostop(eaddr
, dir
);
4527 case DLT_IEEE802_11
:
4528 eaddr
= pcap_ether_hostton(name
);
4531 "unknown 802.11 host '%s'", name
);
4532 b
= gen_wlanhostop(eaddr
, dir
);
4536 case DLT_IP_OVER_FC
:
4537 eaddr
= pcap_ether_hostton(name
);
4540 "unknown Fibre Channel host '%s'", name
);
4541 b
= gen_ipfchostop(eaddr
, dir
);
4550 * Check that the packet doesn't begin
4551 * with an LE Control marker. (We've
4552 * already generated a test for LANE.)
4554 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4558 eaddr
= pcap_ether_hostton(name
);
4561 "unknown ether host '%s'", name
);
4562 b
= gen_ehostop(eaddr
, dir
);
4568 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4569 } else if (proto
== Q_DECNET
) {
4570 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4572 * I don't think DECNET hosts can be multihomed, so
4573 * there is no need to build up a list of addresses
4575 return (gen_host(dn_addr
, 0, proto
, dir
));
4578 alist
= pcap_nametoaddr(name
);
4579 if (alist
== NULL
|| *alist
== NULL
)
4580 bpf_error("unknown host '%s'", name
);
4582 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4584 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4586 tmp
= gen_host(**alist
++, 0xffffffff,
4593 memset(&mask128
, 0xff, sizeof(mask128
));
4594 res0
= res
= pcap_nametoaddrinfo(name
);
4596 bpf_error("unknown host '%s'", name
);
4598 tproto
= tproto6
= proto
;
4599 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4603 for (res
= res0
; res
; res
= res
->ai_next
) {
4604 switch (res
->ai_family
) {
4606 if (tproto
== Q_IPV6
)
4609 sin
= (struct sockaddr_in
*)
4611 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4612 0xffffffff, tproto
, dir
);
4615 if (tproto6
== Q_IP
)
4618 sin6
= (struct sockaddr_in6
*)
4620 tmp
= gen_host6(&sin6
->sin6_addr
,
4621 &mask128
, tproto6
, dir
);
4632 bpf_error("unknown host '%s'%s", name
,
4633 (proto
== Q_DEFAULT
)
4635 : " for specified address family");
4642 if (proto
!= Q_DEFAULT
&&
4643 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4644 bpf_error("illegal qualifier of 'port'");
4645 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4646 bpf_error("unknown port '%s'", name
);
4647 if (proto
== Q_UDP
) {
4648 if (real_proto
== IPPROTO_TCP
)
4649 bpf_error("port '%s' is tcp", name
);
4650 else if (real_proto
== IPPROTO_SCTP
)
4651 bpf_error("port '%s' is sctp", name
);
4653 /* override PROTO_UNDEF */
4654 real_proto
= IPPROTO_UDP
;
4656 if (proto
== Q_TCP
) {
4657 if (real_proto
== IPPROTO_UDP
)
4658 bpf_error("port '%s' is udp", name
);
4660 else if (real_proto
== IPPROTO_SCTP
)
4661 bpf_error("port '%s' is sctp", name
);
4663 /* override PROTO_UNDEF */
4664 real_proto
= IPPROTO_TCP
;
4666 if (proto
== Q_SCTP
) {
4667 if (real_proto
== IPPROTO_UDP
)
4668 bpf_error("port '%s' is udp", name
);
4670 else if (real_proto
== IPPROTO_TCP
)
4671 bpf_error("port '%s' is tcp", name
);
4673 /* override PROTO_UNDEF */
4674 real_proto
= IPPROTO_SCTP
;
4677 return gen_port(port
, real_proto
, dir
);
4681 b
= gen_port(port
, real_proto
, dir
);
4682 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4688 if (proto
!= Q_DEFAULT
&&
4689 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4690 bpf_error("illegal qualifier of 'portrange'");
4691 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4692 bpf_error("unknown port in range '%s'", name
);
4693 if (proto
== Q_UDP
) {
4694 if (real_proto
== IPPROTO_TCP
)
4695 bpf_error("port in range '%s' is tcp", name
);
4696 else if (real_proto
== IPPROTO_SCTP
)
4697 bpf_error("port in range '%s' is sctp", name
);
4699 /* override PROTO_UNDEF */
4700 real_proto
= IPPROTO_UDP
;
4702 if (proto
== Q_TCP
) {
4703 if (real_proto
== IPPROTO_UDP
)
4704 bpf_error("port in range '%s' is udp", name
);
4705 else if (real_proto
== IPPROTO_SCTP
)
4706 bpf_error("port in range '%s' is sctp", name
);
4708 /* override PROTO_UNDEF */
4709 real_proto
= IPPROTO_TCP
;
4711 if (proto
== Q_SCTP
) {
4712 if (real_proto
== IPPROTO_UDP
)
4713 bpf_error("port in range '%s' is udp", name
);
4714 else if (real_proto
== IPPROTO_TCP
)
4715 bpf_error("port in range '%s' is tcp", name
);
4717 /* override PROTO_UNDEF */
4718 real_proto
= IPPROTO_SCTP
;
4721 return gen_portrange(port1
, port2
, real_proto
, dir
);
4725 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4726 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4733 eaddr
= pcap_ether_hostton(name
);
4735 bpf_error("unknown ether host: %s", name
);
4737 alist
= pcap_nametoaddr(name
);
4738 if (alist
== NULL
|| *alist
== NULL
)
4739 bpf_error("unknown host '%s'", name
);
4740 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4744 bpf_error("'gateway' not supported in this configuration");
4748 real_proto
= lookup_proto(name
, proto
);
4749 if (real_proto
>= 0)
4750 return gen_proto(real_proto
, proto
, dir
);
4752 bpf_error("unknown protocol: %s", name
);
4755 real_proto
= lookup_proto(name
, proto
);
4756 if (real_proto
>= 0)
4757 return gen_protochain(real_proto
, proto
, dir
);
4759 bpf_error("unknown protocol: %s", name
);
4771 gen_mcode(s1
, s2
, masklen
, q
)
4772 register const char *s1
, *s2
;
4773 register int masklen
;
4776 register int nlen
, mlen
;
4779 nlen
= __pcap_atoin(s1
, &n
);
4780 /* Promote short ipaddr */
4784 mlen
= __pcap_atoin(s2
, &m
);
4785 /* Promote short ipaddr */
4788 bpf_error("non-network bits set in \"%s mask %s\"",
4791 /* Convert mask len to mask */
4793 bpf_error("mask length must be <= 32");
4794 m
= 0xffffffff << (32 - masklen
);
4796 bpf_error("non-network bits set in \"%s/%d\"",
4803 return gen_host(n
, m
, q
.proto
, q
.dir
);
4806 bpf_error("Mask syntax for networks only");
4814 register const char *s
;
4819 int proto
= q
.proto
;
4825 else if (q
.proto
== Q_DECNET
)
4826 vlen
= __pcap_atodn(s
, &v
);
4828 vlen
= __pcap_atoin(s
, &v
);
4835 if (proto
== Q_DECNET
)
4836 return gen_host(v
, 0, proto
, dir
);
4837 else if (proto
== Q_LINK
) {
4838 bpf_error("illegal link layer address");
4841 if (s
== NULL
&& q
.addr
== Q_NET
) {
4842 /* Promote short net number */
4843 while (v
&& (v
& 0xff000000) == 0) {
4848 /* Promote short ipaddr */
4852 return gen_host(v
, mask
, proto
, dir
);
4857 proto
= IPPROTO_UDP
;
4858 else if (proto
== Q_TCP
)
4859 proto
= IPPROTO_TCP
;
4860 else if (proto
== Q_SCTP
)
4861 proto
= IPPROTO_SCTP
;
4862 else if (proto
== Q_DEFAULT
)
4863 proto
= PROTO_UNDEF
;
4865 bpf_error("illegal qualifier of 'port'");
4868 return gen_port((int)v
, proto
, dir
);
4872 b
= gen_port((int)v
, proto
, dir
);
4873 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4880 proto
= IPPROTO_UDP
;
4881 else if (proto
== Q_TCP
)
4882 proto
= IPPROTO_TCP
;
4883 else if (proto
== Q_SCTP
)
4884 proto
= IPPROTO_SCTP
;
4885 else if (proto
== Q_DEFAULT
)
4886 proto
= PROTO_UNDEF
;
4888 bpf_error("illegal qualifier of 'portrange'");
4891 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4895 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4896 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4902 bpf_error("'gateway' requires a name");
4906 return gen_proto((int)v
, proto
, dir
);
4909 return gen_protochain((int)v
, proto
, dir
);
4924 gen_mcode6(s1
, s2
, masklen
, q
)
4925 register const char *s1
, *s2
;
4926 register int masklen
;
4929 struct addrinfo
*res
;
4930 struct in6_addr
*addr
;
4931 struct in6_addr mask
;
4936 bpf_error("no mask %s supported", s2
);
4938 res
= pcap_nametoaddrinfo(s1
);
4940 bpf_error("invalid ip6 address %s", s1
);
4942 bpf_error("%s resolved to multiple address", s1
);
4943 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4945 if (sizeof(mask
) * 8 < masklen
)
4946 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4947 memset(&mask
, 0, sizeof(mask
));
4948 memset(&mask
, 0xff, masklen
/ 8);
4950 mask
.s6_addr
[masklen
/ 8] =
4951 (0xff << (8 - masklen
% 8)) & 0xff;
4954 a
= (u_int32_t
*)addr
;
4955 m
= (u_int32_t
*)&mask
;
4956 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4957 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4958 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4966 bpf_error("Mask syntax for networks only");
4970 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4975 bpf_error("invalid qualifier against IPv6 address");
4983 register const u_char
*eaddr
;
4986 struct block
*b
, *tmp
;
4988 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4989 if (linktype
== DLT_EN10MB
)
4990 return gen_ehostop(eaddr
, (int)q
.dir
);
4991 if (linktype
== DLT_FDDI
)
4992 return gen_fhostop(eaddr
, (int)q
.dir
);
4993 if (linktype
== DLT_IEEE802
)
4994 return gen_thostop(eaddr
, (int)q
.dir
);
4995 if (linktype
== DLT_IEEE802_11
)
4996 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4997 if (linktype
== DLT_SUNATM
&& is_lane
) {
4999 * Check that the packet doesn't begin with an
5000 * LE Control marker. (We've already generated
5003 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5008 * Now check the MAC address.
5010 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5014 if (linktype
== DLT_IP_OVER_FC
)
5015 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5016 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5018 bpf_error("ethernet address used in non-ether expression");
5024 struct slist
*s0
, *s1
;
5027 * This is definitely not the best way to do this, but the
5028 * lists will rarely get long.
5035 static struct slist
*
5041 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5046 static struct slist
*
5052 s
= new_stmt(BPF_LD
|BPF_MEM
);
5058 * Modify "index" to use the value stored into its register as an
5059 * offset relative to the beginning of the header for the protocol
5060 * "proto", and allocate a register and put an item "size" bytes long
5061 * (1, 2, or 4) at that offset into that register, making it the register
5065 gen_load(proto
, index
, size
)
5070 struct slist
*s
, *tmp
;
5072 int regno
= alloc_reg();
5074 free_reg(index
->regno
);
5078 bpf_error("data size must be 1, 2, or 4");
5094 bpf_error("unsupported index operation");
5098 * The offset is relative to the beginning of
5099 * the link-layer header.
5101 * XXX - what about ATM LANE? Should the index be
5102 * relative to the beginning of the AAL5 frame, so
5103 * that 0 refers to the beginning of the LE Control
5104 * field, or relative to the beginning of the LAN
5105 * frame, so that 0 refers, for Ethernet LANE, to
5106 * the beginning of the destination address?
5108 s
= gen_llprefixlen();
5111 * If "s" is non-null, it has code to arrange that the
5112 * X register contains the length of the prefix preceding
5113 * the link-layer header. Add to it the offset computed
5114 * into the register specified by "index", and move that
5115 * into the X register. Otherwise, just load into the X
5116 * register the offset computed into the register specifed
5120 sappend(s
, xfer_to_a(index
));
5121 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5122 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5124 s
= xfer_to_x(index
);
5127 * Load the item at the offset we've put in the X register.
5129 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5131 sappend(index
->s
, s
);
5147 * The offset is relative to the beginning of
5148 * the network-layer header.
5149 * XXX - are there any cases where we want
5152 s
= gen_llprefixlen();
5155 * If "s" is non-null, it has code to arrange that the
5156 * X register contains the length of the prefix preceding
5157 * the link-layer header. Add to it the offset computed
5158 * into the register specified by "index", and move that
5159 * into the X register. Otherwise, just load into the X
5160 * register the offset computed into the register specifed
5164 sappend(s
, xfer_to_a(index
));
5165 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5166 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5168 s
= xfer_to_x(index
);
5171 * Load the item at the sum of the offset we've put in the
5172 * X register and the offset of the start of the network
5175 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5178 sappend(index
->s
, s
);
5181 * Do the computation only if the packet contains
5182 * the protocol in question.
5184 b
= gen_proto_abbrev(proto
);
5186 gen_and(index
->b
, b
);
5199 * The offset is relative to the beginning of
5200 * the transport-layer header.
5201 * XXX - are there any cases where we want
5203 * XXX - we should, if we're built with
5204 * IPv6 support, generate code to load either
5205 * IPv4, IPv6, or both, as appropriate.
5207 s
= gen_loadx_iphdrlen();
5210 * The X register now contains the sum of the offset
5211 * of the beginning of the link-layer header and
5212 * the length of the network-layer header. Load
5213 * into the A register the offset relative to
5214 * the beginning of the transport layer header,
5215 * add the X register to that, move that to the
5216 * X register, and load with an offset from the
5217 * X register equal to the offset of the network
5218 * layer header relative to the beginning of
5219 * the link-layer header.
5221 sappend(s
, xfer_to_a(index
));
5222 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5223 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5224 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5226 sappend(index
->s
, s
);
5229 * Do the computation only if the packet contains
5230 * the protocol in question - which is true only
5231 * if this is an IP datagram and is the first or
5232 * only fragment of that datagram.
5234 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5236 gen_and(index
->b
, b
);
5238 gen_and(gen_proto_abbrev(Q_IP
), b
);
5244 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5248 index
->regno
= regno
;
5249 s
= new_stmt(BPF_ST
);
5251 sappend(index
->s
, s
);
5257 gen_relation(code
, a0
, a1
, reversed
)
5259 struct arth
*a0
, *a1
;
5262 struct slist
*s0
, *s1
, *s2
;
5263 struct block
*b
, *tmp
;
5267 if (code
== BPF_JEQ
) {
5268 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5269 b
= new_block(JMP(code
));
5273 b
= new_block(BPF_JMP
|code
|BPF_X
);
5279 sappend(a0
->s
, a1
->s
);
5283 free_reg(a0
->regno
);
5284 free_reg(a1
->regno
);
5286 /* 'and' together protocol checks */
5289 gen_and(a0
->b
, tmp
= a1
->b
);
5305 int regno
= alloc_reg();
5306 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5309 s
= new_stmt(BPF_LD
|BPF_LEN
);
5310 s
->next
= new_stmt(BPF_ST
);
5311 s
->next
->s
.k
= regno
;
5326 a
= (struct arth
*)newchunk(sizeof(*a
));
5330 s
= new_stmt(BPF_LD
|BPF_IMM
);
5332 s
->next
= new_stmt(BPF_ST
);
5348 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5351 s
= new_stmt(BPF_ST
);
5359 gen_arth(code
, a0
, a1
)
5361 struct arth
*a0
, *a1
;
5363 struct slist
*s0
, *s1
, *s2
;
5367 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5372 sappend(a0
->s
, a1
->s
);
5374 free_reg(a0
->regno
);
5375 free_reg(a1
->regno
);
5377 s0
= new_stmt(BPF_ST
);
5378 a0
->regno
= s0
->s
.k
= alloc_reg();
5385 * Here we handle simple allocation of the scratch registers.
5386 * If too many registers are alloc'd, the allocator punts.
5388 static int regused
[BPF_MEMWORDS
];
5392 * Return the next free register.
5397 int n
= BPF_MEMWORDS
;
5400 if (regused
[curreg
])
5401 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5403 regused
[curreg
] = 1;
5407 bpf_error("too many registers needed to evaluate expression");
5412 * Return a register to the table so it can
5422 static struct block
*
5429 s
= new_stmt(BPF_LD
|BPF_LEN
);
5430 b
= new_block(JMP(jmp
));
5441 return gen_len(BPF_JGE
, n
);
5445 * Actually, this is less than or equal.
5453 b
= gen_len(BPF_JGT
, n
);
5460 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5461 * the beginning of the link-layer header.
5462 * XXX - that means you can't test values in the radiotap header, but
5463 * as that header is difficult if not impossible to parse generally
5464 * without a loop, that might not be a severe problem. A new keyword
5465 * "radio" could be added for that, although what you'd really want
5466 * would be a way of testing particular radio header values, which
5467 * would generate code appropriate to the radio header in question.
5470 gen_byteop(op
, idx
, val
)
5481 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5484 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5488 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5492 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5496 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5500 b
= new_block(JMP(BPF_JEQ
));
5507 static u_char abroadcast
[] = { 0x0 };
5510 gen_broadcast(proto
)
5513 bpf_u_int32 hostmask
;
5514 struct block
*b0
, *b1
, *b2
;
5515 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5521 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5522 return gen_ahostop(abroadcast
, Q_DST
);
5523 if (linktype
== DLT_EN10MB
)
5524 return gen_ehostop(ebroadcast
, Q_DST
);
5525 if (linktype
== DLT_FDDI
)
5526 return gen_fhostop(ebroadcast
, Q_DST
);
5527 if (linktype
== DLT_IEEE802
)
5528 return gen_thostop(ebroadcast
, Q_DST
);
5529 if (linktype
== DLT_IEEE802_11
||
5530 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5531 linktype
== DLT_IEEE802_11_RADIO
||
5532 linktype
== DLT_PRISM_HEADER
)
5533 return gen_wlanhostop(ebroadcast
, Q_DST
);
5534 if (linktype
== DLT_IP_OVER_FC
)
5535 return gen_ipfchostop(ebroadcast
, Q_DST
);
5536 if (linktype
== DLT_SUNATM
&& is_lane
) {
5538 * Check that the packet doesn't begin with an
5539 * LE Control marker. (We've already generated
5542 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5547 * Now check the MAC address.
5549 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5553 bpf_error("not a broadcast link");
5557 b0
= gen_linktype(ETHERTYPE_IP
);
5558 hostmask
= ~netmask
;
5559 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5560 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5561 (bpf_int32
)(~0 & hostmask
), hostmask
);
5566 bpf_error("only link-layer/IP broadcast filters supported");
5571 * Generate code to test the low-order bit of a MAC address (that's
5572 * the bottom bit of the *first* byte).
5574 static struct block
*
5575 gen_mac_multicast(offset
)
5578 register struct block
*b0
;
5579 register struct slist
*s
;
5581 /* link[offset] & 1 != 0 */
5582 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5583 b0
= new_block(JMP(BPF_JSET
));
5590 gen_multicast(proto
)
5593 register struct block
*b0
, *b1
, *b2
;
5594 register struct slist
*s
;
5600 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5601 /* all ARCnet multicasts use the same address */
5602 return gen_ahostop(abroadcast
, Q_DST
);
5604 if (linktype
== DLT_EN10MB
) {
5605 /* ether[0] & 1 != 0 */
5606 return gen_mac_multicast(0);
5609 if (linktype
== DLT_FDDI
) {
5611 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5613 * XXX - was that referring to bit-order issues?
5615 /* fddi[1] & 1 != 0 */
5616 return gen_mac_multicast(1);
5619 if (linktype
== DLT_IEEE802
) {
5620 /* tr[2] & 1 != 0 */
5621 return gen_mac_multicast(2);
5624 if (linktype
== DLT_IEEE802_11
) {
5628 * For control frames, there is no DA.
5630 * For management frames, DA is at an
5631 * offset of 4 from the beginning of
5634 * For data frames, DA is at an offset
5635 * of 4 from the beginning of the packet
5636 * if To DS is clear and at an offset of
5637 * 16 from the beginning of the packet
5642 * Generate the tests to be done for data frames.
5644 * First, check for To DS set, i.e. "link[1] & 0x01".
5646 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5647 b1
= new_block(JMP(BPF_JSET
));
5648 b1
->s
.k
= 0x01; /* To DS */
5652 * If To DS is set, the DA is at 16.
5654 b0
= gen_mac_multicast(16);
5658 * Now, check for To DS not set, i.e. check
5659 * "!(link[1] & 0x01)".
5661 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5662 b2
= new_block(JMP(BPF_JSET
));
5663 b2
->s
.k
= 0x01; /* To DS */
5668 * If To DS is not set, the DA is at 4.
5670 b1
= gen_mac_multicast(4);
5674 * Now OR together the last two checks. That gives
5675 * the complete set of checks for data frames.
5680 * Now check for a data frame.
5681 * I.e, check "link[0] & 0x08".
5683 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5684 b1
= new_block(JMP(BPF_JSET
));
5689 * AND that with the checks done for data frames.
5694 * If the high-order bit of the type value is 0, this
5695 * is a management frame.
5696 * I.e, check "!(link[0] & 0x08)".
5698 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5699 b2
= new_block(JMP(BPF_JSET
));
5705 * For management frames, the DA is at 4.
5707 b1
= gen_mac_multicast(4);
5711 * OR that with the checks done for data frames.
5712 * That gives the checks done for management and
5718 * If the low-order bit of the type value is 1,
5719 * this is either a control frame or a frame
5720 * with a reserved type, and thus not a
5723 * I.e., check "!(link[0] & 0x04)".
5725 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5726 b1
= new_block(JMP(BPF_JSET
));
5732 * AND that with the checks for data and management
5739 if (linktype
== DLT_IP_OVER_FC
) {
5740 b0
= gen_mac_multicast(2);
5744 if (linktype
== DLT_SUNATM
&& is_lane
) {
5746 * Check that the packet doesn't begin with an
5747 * LE Control marker. (We've already generated
5750 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5754 /* ether[off_mac] & 1 != 0 */
5755 b0
= gen_mac_multicast(off_mac
);
5760 /* Link not known to support multicasts */
5764 b0
= gen_linktype(ETHERTYPE_IP
);
5765 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5771 b0
= gen_linktype(ETHERTYPE_IPV6
);
5772 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5777 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5782 * generate command for inbound/outbound. It's here so we can
5783 * make it link-type specific. 'dir' = 0 implies "inbound",
5784 * = 1 implies "outbound".
5790 register struct block
*b0
;
5793 * Only some data link types support inbound/outbound qualifiers.
5797 b0
= gen_relation(BPF_JEQ
,
5798 gen_load(Q_LINK
, gen_loadi(0), 1),
5806 * Match packets sent by this machine.
5808 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
5811 * Match packets sent to this machine.
5812 * (No broadcast or multicast packets, or
5813 * packets sent to some other machine and
5814 * received promiscuously.)
5816 * XXX - packets sent to other machines probably
5817 * shouldn't be matched, but what about broadcast
5818 * or multicast packets we received?
5820 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
5825 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
5826 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5831 /* match outgoing packets */
5832 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
5834 /* match incoming packets */
5835 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
5839 case DLT_JUNIPER_MLFR
:
5840 case DLT_JUNIPER_MLPPP
:
5841 case DLT_JUNIPER_ATM1
:
5842 case DLT_JUNIPER_ATM2
:
5843 /* juniper flags (including direction) are stored
5844 * the byte after the 3-byte magic number */
5846 /* match outgoing packets */
5847 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
5849 /* match incoming packets */
5850 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
5855 bpf_error("inbound/outbound not supported on linktype %d",
5863 /* PF firewall log matched interface */
5865 gen_pf_ifname(const char *ifname
)
5870 if (linktype
== DLT_PFLOG
) {
5871 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5872 off
= offsetof(struct pfloghdr
, ifname
);
5874 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5877 if (strlen(ifname
) >= len
) {
5878 bpf_error("ifname interface names can only be %d characters",
5882 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
5886 /* PF firewall log matched interface */
5888 gen_pf_ruleset(char *ruleset
)
5892 if (linktype
!= DLT_PFLOG
) {
5893 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5896 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5897 bpf_error("ruleset names can only be %ld characters",
5898 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5901 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
5902 strlen(ruleset
), (const u_char
*)ruleset
);
5906 /* PF firewall log rule number */
5912 if (linktype
== DLT_PFLOG
) {
5913 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5916 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5923 /* PF firewall log sub-rule number */
5925 gen_pf_srnr(int srnr
)
5929 if (linktype
!= DLT_PFLOG
) {
5930 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5934 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5939 /* PF firewall log reason code */
5941 gen_pf_reason(int reason
)
5945 if (linktype
== DLT_PFLOG
) {
5946 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
5949 bpf_error("reason not supported on linktype 0x%x", linktype
);
5956 /* PF firewall log action */
5958 gen_pf_action(int action
)
5962 if (linktype
== DLT_PFLOG
) {
5963 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
5966 bpf_error("action not supported on linktype 0x%x", linktype
);
5975 register const u_char
*eaddr
;
5978 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5979 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5980 return gen_ahostop(eaddr
, (int)q
.dir
);
5982 bpf_error("ARCnet address used in non-arc expression");
5986 static struct block
*
5987 gen_ahostop(eaddr
, dir
)
5988 register const u_char
*eaddr
;
5991 register struct block
*b0
, *b1
;
5994 /* src comes first, different from Ethernet */
5996 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
5999 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6002 b0
= gen_ahostop(eaddr
, Q_SRC
);
6003 b1
= gen_ahostop(eaddr
, Q_DST
);
6009 b0
= gen_ahostop(eaddr
, Q_SRC
);
6010 b1
= gen_ahostop(eaddr
, Q_DST
);
6019 * support IEEE 802.1Q VLAN trunk over ethernet
6028 * Change the offsets to point to the type and data fields within
6029 * the VLAN packet. Just increment the offsets, so that we
6030 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6031 * capture VLAN 200 encapsulated within VLAN 100.
6033 * XXX - this is a bit of a kludge. If we were to split the
6034 * compiler into a parser that parses an expression and
6035 * generates an expression tree, and a code generator that
6036 * takes an expression tree (which could come from our
6037 * parser or from some other parser) and generates BPF code,
6038 * we could perhaps make the offsets parameters of routines
6039 * and, in the handler for an "AND" node, pass to subnodes
6040 * other than the VLAN node the adjusted offsets.
6042 * This would mean that "vlan" would, instead of changing the
6043 * behavior of *all* tests after it, change only the behavior
6044 * of tests ANDed with it. That would change the documented
6045 * semantics of "vlan", which might break some expressions.
6046 * However, it would mean that "(vlan and ip) or ip" would check
6047 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6048 * checking only for VLAN-encapsulated IP, so that could still
6049 * be considered worth doing; it wouldn't break expressions
6050 * that are of the form "vlan and ..." or "vlan N and ...",
6051 * which I suspect are the most common expressions involving
6052 * "vlan". "vlan or ..." doesn't necessarily do what the user
6053 * would really want, now, as all the "or ..." tests would
6054 * be done assuming a VLAN, even though the "or" could be viewed
6055 * as meaning "or, if this isn't a VLAN packet...".
6057 orig_linktype
= off_linktype
; /* save original values */
6069 bpf_error("no VLAN support for data link type %d",
6074 /* check for VLAN */
6075 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6077 /* If a specific VLAN is requested, check VLAN id */
6078 if (vlan_num
>= 0) {
6081 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6100 * Change the offsets to point to the type and data fields within
6101 * the MPLS packet. Just increment the offsets, so that we
6102 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6103 * capture packets with an outer label of 100000 and an inner
6106 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6108 orig_linktype
= off_linktype
; /* save original values */
6113 case DLT_C_HDLC
: /* fall through */
6119 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6120 (bpf_int32
)ETHERTYPE_MPLS
);
6128 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6129 (bpf_int32
)PPP_MPLS_UCAST
);
6132 /* FIXME add other DLT_s ...
6133 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6134 * leave it for now */
6137 bpf_error("no MPLS support for data link type %d",
6144 /* If a specific MPLS label is requested, check it */
6145 if (label_num
>= 0) {
6148 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6149 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6150 0xfffff000); /* only compare the first 20 bits */
6159 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6171 bpf_error("'vpi' supported only on raw ATM");
6172 if (off_vpi
== (u_int
)-1)
6174 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6180 bpf_error("'vci' supported only on raw ATM");
6181 if (off_vci
== (u_int
)-1)
6183 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6188 if (off_proto
== (u_int
)-1)
6189 abort(); /* XXX - this isn't on FreeBSD */
6190 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6195 if (off_payload
== (u_int
)-1)
6197 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6198 0xffffffff, jtype
, reverse
, jvalue
);
6203 bpf_error("'callref' supported only on raw ATM");
6204 if (off_proto
== (u_int
)-1)
6206 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6207 jtype
, reverse
, jvalue
);
6217 gen_atmtype_abbrev(type
)
6220 struct block
*b0
, *b1
;
6225 /* Get all packets in Meta signalling Circuit */
6227 bpf_error("'metac' supported only on raw ATM");
6228 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6229 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6234 /* Get all packets in Broadcast Circuit*/
6236 bpf_error("'bcc' supported only on raw ATM");
6237 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6238 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6243 /* Get all cells in Segment OAM F4 circuit*/
6245 bpf_error("'oam4sc' supported only on raw ATM");
6246 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6247 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6252 /* Get all cells in End-to-End OAM F4 Circuit*/
6254 bpf_error("'oam4ec' supported only on raw ATM");
6255 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6256 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6261 /* Get all packets in connection Signalling Circuit */
6263 bpf_error("'sc' supported only on raw ATM");
6264 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6265 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6270 /* Get all packets in ILMI Circuit */
6272 bpf_error("'ilmic' supported only on raw ATM");
6273 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6274 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6279 /* Get all LANE packets */
6281 bpf_error("'lane' supported only on raw ATM");
6282 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6285 * Arrange that all subsequent tests assume LANE
6286 * rather than LLC-encapsulated packets, and set
6287 * the offsets appropriately for LANE-encapsulated
6290 * "off_mac" is the offset of the Ethernet header,
6291 * which is 2 bytes past the ATM pseudo-header
6292 * (skipping the pseudo-header and 2-byte LE Client
6293 * field). The other offsets are Ethernet offsets
6294 * relative to "off_mac".
6297 off_mac
= off_payload
+ 2; /* MAC header */
6298 off_linktype
= off_mac
+ 12;
6299 off_nl
= off_mac
+ 14; /* Ethernet II */
6300 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6304 /* Get all LLC-encapsulated packets */
6306 bpf_error("'llc' supported only on raw ATM");
6307 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6318 static struct block
*
6319 gen_msg_abbrev(type
)
6325 * Q.2931 signalling protocol messages for handling virtual circuits
6326 * establishment and teardown
6331 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6335 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6339 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6343 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6347 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6350 case A_RELEASE_DONE
:
6351 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6361 gen_atmmulti_abbrev(type
)
6364 struct block
*b0
, *b1
;
6370 bpf_error("'oam' supported only on raw ATM");
6371 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6376 bpf_error("'oamf4' supported only on raw ATM");
6378 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6379 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6381 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6387 * Get Q.2931 signalling messages for switched
6388 * virtual connection
6391 bpf_error("'connectmsg' supported only on raw ATM");
6392 b0
= gen_msg_abbrev(A_SETUP
);
6393 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6395 b0
= gen_msg_abbrev(A_CONNECT
);
6397 b0
= gen_msg_abbrev(A_CONNECTACK
);
6399 b0
= gen_msg_abbrev(A_RELEASE
);
6401 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6403 b0
= gen_atmtype_abbrev(A_SC
);
6409 bpf_error("'metaconnect' supported only on raw ATM");
6410 b0
= gen_msg_abbrev(A_SETUP
);
6411 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6413 b0
= gen_msg_abbrev(A_CONNECT
);
6415 b0
= gen_msg_abbrev(A_RELEASE
);
6417 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6419 b0
= gen_atmtype_abbrev(A_METAC
);