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.44 2006-10-13 08:56:07 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_PACKET
, /* relative to the beginning of the packet */
139 OR_LINK
, /* relative to the link-layer header */
140 OR_NET
, /* relative to the network-layer header */
141 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
155 #define CHUNK0SIZE 1024
161 static struct chunk chunks
[NCHUNKS
];
162 static int cur_chunk
;
164 static void *newchunk(u_int
);
165 static void freechunks(void);
166 static inline struct block
*new_block(int);
167 static inline struct slist
*new_stmt(int);
168 static struct block
*gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block
*, struct block
*);
172 static void merge(struct block
*, struct block
*);
173 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
178 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
180 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
181 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
182 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
183 static struct slist
*gen_load_llrel(u_int
, u_int
);
184 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
185 static struct slist
*gen_loadx_iphdrlen(void);
186 static struct block
*gen_uncond(int);
187 static inline struct block
*gen_true(void);
188 static inline struct block
*gen_false(void);
189 static struct block
*gen_ether_linktype(int);
190 static struct block
*gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block
*);
192 static void insert_load_llprefixlen(struct block
*);
193 static struct slist
*gen_llprefixlen(void);
194 static struct block
*gen_linktype(int);
195 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
196 static struct block
*gen_llc_linktype(int);
197 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
199 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
201 static struct block
*gen_ahostop(const u_char
*, int);
202 static struct block
*gen_ehostop(const u_char
*, int);
203 static struct block
*gen_fhostop(const u_char
*, int);
204 static struct block
*gen_thostop(const u_char
*, int);
205 static struct block
*gen_wlanhostop(const u_char
*, int);
206 static struct block
*gen_ipfchostop(const u_char
*, int);
207 static struct block
*gen_dnhostop(bpf_u_int32
, int);
208 static struct block
*gen_mpls_linktype(int);
209 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
211 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
214 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
216 static struct block
*gen_ipfrag(void);
217 static struct block
*gen_portatom(int, bpf_int32
);
218 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
220 static struct block
*gen_portatom6(int, bpf_int32
);
221 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
223 struct block
*gen_portop(int, int, int);
224 static struct block
*gen_port(int, int, int);
225 struct block
*gen_portrangeop(int, int, int, int);
226 static struct block
*gen_portrange(int, int, int, int);
228 struct block
*gen_portop6(int, int, int);
229 static struct block
*gen_port6(int, int, int);
230 struct block
*gen_portrangeop6(int, int, int, int);
231 static struct block
*gen_portrange6(int, int, int, int);
233 static int lookup_proto(const char *, int);
234 static struct block
*gen_protochain(int, int, int);
235 static struct block
*gen_proto(int, int, int);
236 static struct slist
*xfer_to_x(struct arth
*);
237 static struct slist
*xfer_to_a(struct arth
*);
238 static struct block
*gen_mac_multicast(int);
239 static struct block
*gen_len(int, int);
241 static struct block
*gen_msg_abbrev(int type
);
252 /* XXX Round up to nearest long. */
253 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
255 /* XXX Round up to structure boundary. */
259 cp
= &chunks
[cur_chunk
];
260 if (n
> cp
->n_left
) {
261 ++cp
, k
= ++cur_chunk
;
263 bpf_error("out of memory");
264 size
= CHUNK0SIZE
<< k
;
265 cp
->m
= (void *)malloc(size
);
267 bpf_error("out of memory");
268 memset((char *)cp
->m
, 0, size
);
271 bpf_error("out of memory");
274 return (void *)((char *)cp
->m
+ cp
->n_left
);
283 for (i
= 0; i
< NCHUNKS
; ++i
)
284 if (chunks
[i
].m
!= NULL
) {
291 * A strdup whose allocations are freed after code generation is over.
295 register const char *s
;
297 int n
= strlen(s
) + 1;
298 char *cp
= newchunk(n
);
304 static inline struct block
*
310 p
= (struct block
*)newchunk(sizeof(*p
));
317 static inline struct slist
*
323 p
= (struct slist
*)newchunk(sizeof(*p
));
329 static struct block
*
333 struct block
*b
= new_block(BPF_RET
|BPF_K
);
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask
;
350 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
351 char *buf
, int optimize
, bpf_u_int32 mask
)
360 if (setjmp(top_ctx
)) {
368 snaplen
= pcap_snapshot(p
);
370 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
371 "snaplen of 0 rejects all packets");
375 lex_init(buf
? buf
: "");
383 root
= gen_retblk(snaplen
);
385 if (optimize
&& !no_optimize
) {
388 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
389 bpf_error("expression rejects all packets");
391 program
->bf_insns
= icode_to_fcode(root
, &len
);
392 program
->bf_len
= len
;
400 * entry point for using the compiler with no pcap open
401 * pass in all the stuff that is needed explicitly instead.
404 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
405 struct bpf_program
*program
,
406 char *buf
, int optimize
, bpf_u_int32 mask
)
411 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
414 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
420 * Clean up a "struct bpf_program" by freeing all the memory allocated
424 pcap_freecode(struct bpf_program
*program
)
427 if (program
->bf_insns
!= NULL
) {
428 free((char *)program
->bf_insns
);
429 program
->bf_insns
= NULL
;
434 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
435 * which of the jt and jf fields has been resolved and which is a pointer
436 * back to another unresolved block (or nil). At least one of the fields
437 * in each block is already resolved.
440 backpatch(list
, target
)
441 struct block
*list
, *target
;
458 * Merge the lists in b0 and b1, using the 'sense' field to indicate
459 * which of jt and jf is the link.
463 struct block
*b0
, *b1
;
465 register struct block
**p
= &b0
;
467 /* Find end of list. */
469 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
471 /* Concatenate the lists. */
479 backpatch(p
, gen_retblk(snaplen
));
480 p
->sense
= !p
->sense
;
481 backpatch(p
, gen_retblk(0));
485 * Insert before the statements of the first (root) block any
486 * statements needed to load the lengths of any variable-length
487 * headers into registers.
489 * XXX - a fancier strategy would be to insert those before the
490 * statements of all blocks that use those lengths and that
491 * have no predecessors that use them, so that we only compute
492 * the lengths if we need them. There might be even better
493 * approaches than that. However, as we're currently only
494 * handling variable-length radiotap headers, and as all
495 * filtering expressions other than raw link[M:N] tests
496 * require the length of that header, doing more for that
497 * header length isn't really worth the effort.
499 insert_load_llprefixlen(root
);
504 struct block
*b0
, *b1
;
506 backpatch(b0
, b1
->head
);
507 b0
->sense
= !b0
->sense
;
508 b1
->sense
= !b1
->sense
;
510 b1
->sense
= !b1
->sense
;
516 struct block
*b0
, *b1
;
518 b0
->sense
= !b0
->sense
;
519 backpatch(b0
, b1
->head
);
520 b0
->sense
= !b0
->sense
;
529 b
->sense
= !b
->sense
;
532 static struct block
*
533 gen_cmp(offrel
, offset
, size
, v
)
534 enum e_offrel offrel
;
538 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
541 static struct block
*
542 gen_cmp_gt(offrel
, offset
, size
, v
)
543 enum e_offrel offrel
;
547 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
550 static struct block
*
551 gen_cmp_ge(offrel
, offset
, size
, v
)
552 enum e_offrel offrel
;
556 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
559 static struct block
*
560 gen_cmp_lt(offrel
, offset
, size
, v
)
561 enum e_offrel offrel
;
565 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
568 static struct block
*
569 gen_cmp_le(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
577 static struct block
*
578 gen_mcmp(offrel
, offset
, size
, v
, mask
)
579 enum e_offrel offrel
;
584 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
587 static struct block
*
588 gen_bcmp(offrel
, offset
, size
, v
)
589 enum e_offrel offrel
;
590 register u_int offset
, size
;
591 register const u_char
*v
;
593 register struct block
*b
, *tmp
;
597 register const u_char
*p
= &v
[size
- 4];
598 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
599 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
601 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
608 register const u_char
*p
= &v
[size
- 2];
609 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
611 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
618 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
627 * AND the field of size "size" at offset "offset" relative to the header
628 * specified by "offrel" with "mask", and compare it with the value "v"
629 * with the test specified by "jtype"; if "reverse" is true, the test
630 * should test the opposite of "jtype".
632 static struct block
*
633 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
634 enum e_offrel offrel
;
636 bpf_u_int32 offset
, size
, mask
, jtype
;
639 struct slist
*s
, *s2
;
642 s
= gen_load_a(offrel
, offset
, size
);
644 if (mask
!= 0xffffffff) {
645 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
650 b
= new_block(JMP(jtype
));
653 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
659 * Various code constructs need to know the layout of the data link
660 * layer. These variables give the necessary offsets from the beginning
661 * of the packet data.
663 * If the link layer has variable_length headers, the offsets are offsets
664 * from the end of the link-link-layer header, and "reg_ll_size" is
665 * the register number for a register containing the length of the
666 * link-layer header. Otherwise, "reg_ll_size" is -1.
668 static int reg_ll_size
;
671 * This is the offset of the beginning of the link-layer header from
672 * the beginning of the raw packet data.
674 * It's usually 0, except for 802.11 with a fixed-length radio header.
675 * (For 802.11 with a variable-length radio header, we have to generate
676 * code to compute that offset; off_ll is 0 in that case.)
681 * This is the offset of the beginning of the MAC-layer header.
682 * It's usually 0, except for ATM LANE, where it's the offset, relative
683 * to the beginning of the raw packet data, of the Ethernet header.
685 static u_int off_mac
;
688 * "off_linktype" is the offset to information in the link-layer header
689 * giving the packet type. This offset is relative to the beginning
690 * of the link-layer header (i.e., it doesn't include off_ll).
692 * For Ethernet, it's the offset of the Ethernet type field.
694 * For link-layer types that always use 802.2 headers, it's the
695 * offset of the LLC header.
697 * For PPP, it's the offset of the PPP type field.
699 * For Cisco HDLC, it's the offset of the CHDLC type field.
701 * For BSD loopback, it's the offset of the AF_ value.
703 * For Linux cooked sockets, it's the offset of the type field.
705 * It's set to -1 for no encapsulation, in which case, IP is assumed.
707 static u_int off_linktype
;
710 * TRUE if the link layer includes an ATM pseudo-header.
712 static int is_atm
= 0;
715 * TRUE if "lane" appeared in the filter; it causes us to generate
716 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
718 static int is_lane
= 0;
721 * These are offsets for the ATM pseudo-header.
723 static u_int off_vpi
;
724 static u_int off_vci
;
725 static u_int off_proto
;
728 * These are offsets for the MTP3 fields.
730 static u_int off_sio
;
731 static u_int off_opc
;
732 static u_int off_dpc
;
733 static u_int off_sls
;
736 * This is the offset of the first byte after the ATM pseudo_header,
737 * or -1 if there is no ATM pseudo-header.
739 static u_int off_payload
;
742 * These are offsets to the beginning of the network-layer header.
743 * They are relative to the beginning of the link-layer header (i.e.,
744 * they don't include off_ll).
746 * If the link layer never uses 802.2 LLC:
748 * "off_nl" and "off_nl_nosnap" are the same.
750 * If the link layer always uses 802.2 LLC:
752 * "off_nl" is the offset if there's a SNAP header following
755 * "off_nl_nosnap" is the offset if there's no SNAP header.
757 * If the link layer is Ethernet:
759 * "off_nl" is the offset if the packet is an Ethernet II packet
760 * (we assume no 802.3+802.2+SNAP);
762 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
763 * with an 802.2 header following it.
766 static u_int off_nl_nosnap
;
774 linktype
= pcap_datalink(p
);
776 pcap_fddipad
= p
->fddipad
;
780 * Assume it's not raw ATM with a pseudo-header, for now.
791 * And assume we're not doing SS7.
799 * Also assume it's not 802.11 with a fixed-length radio header.
805 label_stack_depth
= 0;
813 off_nl
= 6; /* XXX in reality, variable! */
814 off_nl_nosnap
= 6; /* no 802.2 LLC */
817 case DLT_ARCNET_LINUX
:
819 off_nl
= 8; /* XXX in reality, variable! */
820 off_nl_nosnap
= 8; /* no 802.2 LLC */
825 off_nl
= 14; /* Ethernet II */
826 off_nl_nosnap
= 17; /* 802.3+802.2 */
831 * SLIP doesn't have a link level type. The 16 byte
832 * header is hacked into our SLIP driver.
836 off_nl_nosnap
= 16; /* no 802.2 LLC */
840 /* XXX this may be the same as the DLT_PPP_BSDOS case */
844 off_nl_nosnap
= 24; /* no 802.2 LLC */
851 off_nl_nosnap
= 4; /* no 802.2 LLC */
857 off_nl_nosnap
= 12; /* no 802.2 LLC */
862 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
863 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
866 off_nl_nosnap
= 4; /* no 802.2 LLC */
871 * This does no include the Ethernet header, and
872 * only covers session state.
876 off_nl_nosnap
= 8; /* no 802.2 LLC */
882 off_nl_nosnap
= 24; /* no 802.2 LLC */
887 * FDDI doesn't really have a link-level type field.
888 * We set "off_linktype" to the offset of the LLC header.
890 * To check for Ethernet types, we assume that SSAP = SNAP
891 * is being used and pick out the encapsulated Ethernet type.
892 * XXX - should we generate code to check for SNAP?
896 off_linktype
+= pcap_fddipad
;
898 off_nl
= 21; /* FDDI+802.2+SNAP */
899 off_nl_nosnap
= 16; /* FDDI+802.2 */
901 off_nl
+= pcap_fddipad
;
902 off_nl_nosnap
+= pcap_fddipad
;
908 * Token Ring doesn't really have a link-level type field.
909 * We set "off_linktype" to the offset of the LLC header.
911 * To check for Ethernet types, we assume that SSAP = SNAP
912 * is being used and pick out the encapsulated Ethernet type.
913 * XXX - should we generate code to check for SNAP?
915 * XXX - the header is actually variable-length.
916 * Some various Linux patched versions gave 38
917 * as "off_linktype" and 40 as "off_nl"; however,
918 * if a token ring packet has *no* routing
919 * information, i.e. is not source-routed, the correct
920 * values are 20 and 22, as they are in the vanilla code.
922 * A packet is source-routed iff the uppermost bit
923 * of the first byte of the source address, at an
924 * offset of 8, has the uppermost bit set. If the
925 * packet is source-routed, the total number of bytes
926 * of routing information is 2 plus bits 0x1F00 of
927 * the 16-bit value at an offset of 14 (shifted right
928 * 8 - figure out which byte that is).
931 off_nl
= 22; /* Token Ring+802.2+SNAP */
932 off_nl_nosnap
= 17; /* Token Ring+802.2 */
937 * 802.11 doesn't really have a link-level type field.
938 * We set "off_linktype" to the offset of the LLC header.
940 * To check for Ethernet types, we assume that SSAP = SNAP
941 * is being used and pick out the encapsulated Ethernet type.
942 * XXX - should we generate code to check for SNAP?
944 * XXX - the header is actually variable-length. We
945 * assume a 24-byte link-layer header, as appears in
946 * data frames in networks with no bridges. If the
947 * fromds and tods 802.11 header bits are both set,
948 * it's actually supposed to be 30 bytes.
951 off_nl
= 32; /* 802.11+802.2+SNAP */
952 off_nl_nosnap
= 27; /* 802.11+802.2 */
955 case DLT_PRISM_HEADER
:
957 * Same as 802.11, but with an additional header before
958 * the 802.11 header, containing a bunch of additional
959 * information including radio-level information.
961 * The header is 144 bytes long.
963 * XXX - same variable-length header problem; at least
964 * the Prism header is fixed-length.
968 off_nl
= 32; /* Prism+802.11+802.2+SNAP */
969 off_nl_nosnap
= 27; /* Prism+802.11+802.2 */
972 case DLT_IEEE802_11_RADIO_AVS
:
974 * Same as 802.11, but with an additional header before
975 * the 802.11 header, containing a bunch of additional
976 * information including radio-level information.
978 * The header is 64 bytes long, at least in its
979 * current incarnation.
981 * XXX - same variable-length header problem, only
982 * more so; this header is also variable-length,
983 * with the length being the 32-bit big-endian
984 * number at an offset of 4 from the beginning
985 * of the radio header. We should handle that the
986 * same way we handle the length at the beginning
987 * of the radiotap header.
989 * XXX - in Linux, do any drivers that supply an AVS
990 * header supply a link-layer type other than
991 * ARPHRD_IEEE80211_PRISM? If so, we should map that
992 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
993 * any drivers that supply an AVS header but supply
994 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
995 * have to check the header in the generated code to
996 * determine whether it's Prism or AVS.
1000 off_nl
= 32; /* Radio+802.11+802.2+SNAP */
1001 off_nl_nosnap
= 27; /* Radio+802.11+802.2 */
1004 case DLT_IEEE802_11_RADIO
:
1006 * Same as 802.11, but with an additional header before
1007 * the 802.11 header, containing a bunch of additional
1008 * information including radio-level information.
1010 * The radiotap header is variable length, and we
1011 * generate code to compute its length and store it
1012 * in a register. These offsets are relative to the
1013 * beginning of the 802.11 header.
1016 off_nl
= 32; /* 802.11+802.2+SNAP */
1017 off_nl_nosnap
= 27; /* 802.11+802.2 */
1020 case DLT_ATM_RFC1483
:
1021 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1023 * assume routed, non-ISO PDUs
1024 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1026 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1027 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1028 * latter would presumably be treated the way PPPoE
1029 * should be, so you can do "pppoe and udp port 2049"
1030 * or "pppoa and tcp port 80" and have it check for
1031 * PPPo{A,E} and a PPP protocol of IP and....
1034 off_nl
= 8; /* 802.2+SNAP */
1035 off_nl_nosnap
= 3; /* 802.2 */
1040 * Full Frontal ATM; you get AALn PDUs with an ATM
1044 off_vpi
= SUNATM_VPI_POS
;
1045 off_vci
= SUNATM_VCI_POS
;
1046 off_proto
= PROTO_POS
;
1047 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1048 off_payload
= SUNATM_PKT_BEGIN_POS
;
1049 off_linktype
= off_payload
;
1050 off_nl
= off_payload
+8; /* 802.2+SNAP */
1051 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1057 off_nl_nosnap
= 0; /* no 802.2 LLC */
1060 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1063 off_nl_nosnap
= 16; /* no 802.2 LLC */
1068 * LocalTalk does have a 1-byte type field in the LLAP header,
1069 * but really it just indicates whether there is a "short" or
1070 * "long" DDP packet following.
1074 off_nl_nosnap
= 0; /* no 802.2 LLC */
1077 case DLT_IP_OVER_FC
:
1079 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1080 * link-level type field. We set "off_linktype" to the
1081 * offset of the LLC header.
1083 * To check for Ethernet types, we assume that SSAP = SNAP
1084 * is being used and pick out the encapsulated Ethernet type.
1085 * XXX - should we generate code to check for SNAP? RFC
1086 * 2625 says SNAP should be used.
1089 off_nl
= 24; /* IPFC+802.2+SNAP */
1090 off_nl_nosnap
= 19; /* IPFC+802.2 */
1095 * XXX - we should set this to handle SNAP-encapsulated
1096 * frames (NLPID of 0x80).
1100 off_nl_nosnap
= 0; /* no 802.2 LLC */
1104 * the only BPF-interesting FRF.16 frames are non-control frames;
1105 * Frame Relay has a variable length link-layer
1106 * so lets start with offset 4 for now and increments later on (FIXME);
1111 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1114 case DLT_APPLE_IP_OVER_IEEE1394
:
1117 off_nl_nosnap
= 18; /* no 802.2 LLC */
1120 case DLT_LINUX_IRDA
:
1122 * Currently, only raw "link[N:M]" filtering is supported.
1131 * Currently, only raw "link[N:M]" filtering is supported.
1138 case DLT_SYMANTEC_FIREWALL
:
1140 off_nl
= 44; /* Ethernet II */
1141 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1146 /* XXX read this from pf.h? */
1147 off_nl
= PFLOG_HDRLEN
;
1148 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1151 case DLT_JUNIPER_MFR
:
1152 case DLT_JUNIPER_MLFR
:
1153 case DLT_JUNIPER_MLPPP
:
1154 case DLT_JUNIPER_PPP
:
1155 case DLT_JUNIPER_CHDLC
:
1156 case DLT_JUNIPER_FRELAY
:
1159 off_nl_nosnap
= -1; /* no 802.2 LLC */
1162 case DLT_JUNIPER_ATM1
:
1163 off_linktype
= 4; /* in reality variable between 4-8 */
1168 case DLT_JUNIPER_ATM2
:
1169 off_linktype
= 8; /* in reality variable between 8-12 */
1174 /* frames captured on a Juniper PPPoE service PIC
1175 * contain raw ethernet frames */
1176 case DLT_JUNIPER_PPPOE
:
1177 case DLT_JUNIPER_ETHER
:
1179 off_nl
= 18; /* Ethernet II */
1180 off_nl_nosnap
= 21; /* 802.3+802.2 */
1183 case DLT_JUNIPER_PPPOE_ATM
:
1186 off_nl_nosnap
= -1; /* no 802.2 LLC */
1189 case DLT_JUNIPER_GGSN
:
1192 off_nl_nosnap
= -1; /* no 802.2 LLC */
1195 case DLT_JUNIPER_ES
:
1197 off_nl
= -1; /* not really a network layer but raw IP adresses */
1198 off_nl_nosnap
= -1; /* no 802.2 LLC */
1201 case DLT_JUNIPER_MONITOR
:
1203 off_nl
= 12; /* raw IP/IP6 header */
1204 off_nl_nosnap
= -1; /* no 802.2 LLC */
1207 case DLT_JUNIPER_SERVICES
:
1209 off_nl
= -1; /* L3 proto location dep. on cookie type */
1210 off_nl_nosnap
= -1; /* no 802.2 LLC */
1213 case DLT_JUNIPER_VP
:
1237 case DLT_LINUX_LAPD
:
1239 * Currently, only raw "link[N:M]" filtering is supported.
1248 * Currently, only raw "link[N:M]" filtering is supported.
1255 case DLT_BLUETOOTH_HCI_H4
:
1257 * Currently, only raw "link[N:M]" filtering is supported.
1264 bpf_error("unknown data link type %d", linktype
);
1269 * Load a value relative to the beginning of the link-layer header.
1270 * The link-layer header doesn't necessarily begin at the beginning
1271 * of the packet data; there might be a variable-length prefix containing
1272 * radio information.
1274 static struct slist
*
1275 gen_load_llrel(offset
, size
)
1278 struct slist
*s
, *s2
;
1280 s
= gen_llprefixlen();
1283 * If "s" is non-null, it has code to arrange that the X register
1284 * contains the length of the prefix preceding the link-layer
1287 * Otherwise, the length of the prefix preceding the link-layer
1288 * header is "off_ll".
1292 * There's a variable-length prefix preceding the
1293 * link-layer header. "s" points to a list of statements
1294 * that put the length of that prefix into the X register.
1295 * do an indirect load, to use the X register as an offset.
1297 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1302 * There is no variable-length header preceding the
1303 * link-layer header; add in off_ll, which, if there's
1304 * a fixed-length header preceding the link-layer header,
1305 * is the length of that header.
1307 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1308 s
->s
.k
= offset
+ off_ll
;
1314 * Load a value relative to the beginning of the specified header.
1316 static struct slist
*
1317 gen_load_a(offrel
, offset
, size
)
1318 enum e_offrel offrel
;
1321 struct slist
*s
, *s2
;
1326 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1331 s
= gen_load_llrel(offset
, size
);
1335 s
= gen_load_llrel(off_nl
+ offset
, size
);
1339 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1344 * Load the X register with the length of the IPv4 header
1345 * (plus the offset of the link-layer header, if it's
1346 * preceded by a variable-length header such as a radio
1347 * header), in bytes.
1349 s
= gen_loadx_iphdrlen();
1352 * Load the item at {offset of the link-layer header} +
1353 * {offset, relative to the start of the link-layer
1354 * header, of the IPv4 header} + {length of the IPv4 header} +
1355 * {specified offset}.
1357 * (If the link-layer is variable-length, it's included
1358 * in the value in the X register, and off_ll is 0.)
1360 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1361 s2
->s
.k
= off_ll
+ off_nl
+ offset
;
1366 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1377 * Generate code to load into the X register the sum of the length of
1378 * the IPv4 header and any variable-length header preceding the link-layer
1381 static struct slist
*
1382 gen_loadx_iphdrlen()
1384 struct slist
*s
, *s2
;
1386 s
= gen_llprefixlen();
1389 * There's a variable-length prefix preceding the
1390 * link-layer header. "s" points to a list of statements
1391 * that put the length of that prefix into the X register.
1392 * The 4*([k]&0xf) addressing mode can't be used, as we
1393 * don't have a constant offset, so we have to load the
1394 * value in question into the A register and add to it
1395 * the value from the X register.
1397 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1400 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1403 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1408 * The A register now contains the length of the
1409 * IP header. We need to add to it the length
1410 * of the prefix preceding the link-layer
1411 * header, which is still in the X register, and
1412 * move the result into the X register.
1414 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1415 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1418 * There is no variable-length header preceding the
1419 * link-layer header; add in off_ll, which, if there's
1420 * a fixed-length header preceding the link-layer header,
1421 * is the length of that header.
1423 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1424 s
->s
.k
= off_ll
+ off_nl
;
1429 static struct block
*
1436 s
= new_stmt(BPF_LD
|BPF_IMM
);
1438 b
= new_block(JMP(BPF_JEQ
));
1444 static inline struct block
*
1447 return gen_uncond(1);
1450 static inline struct block
*
1453 return gen_uncond(0);
1457 * Byte-swap a 32-bit number.
1458 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1459 * big-endian platforms.)
1461 #define SWAPLONG(y) \
1462 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1465 * Generate code to match a particular packet type.
1467 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1468 * value, if <= ETHERMTU. We use that to determine whether to
1469 * match the type/length field or to check the type/length field for
1470 * a value <= ETHERMTU to see whether it's a type field and then do
1471 * the appropriate test.
1473 static struct block
*
1474 gen_ether_linktype(proto
)
1477 struct block
*b0
, *b1
;
1483 case LLCSAP_NETBEUI
:
1485 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1486 * so we check the DSAP and SSAP.
1488 * LLCSAP_IP checks for IP-over-802.2, rather
1489 * than IP-over-Ethernet or IP-over-SNAP.
1491 * XXX - should we check both the DSAP and the
1492 * SSAP, like this, or should we check just the
1493 * DSAP, as we do for other types <= ETHERMTU
1494 * (i.e., other SAP values)?
1496 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1498 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1499 ((proto
<< 8) | proto
));
1507 * Ethernet_II frames, which are Ethernet
1508 * frames with a frame type of ETHERTYPE_IPX;
1510 * Ethernet_802.3 frames, which are 802.3
1511 * frames (i.e., the type/length field is
1512 * a length field, <= ETHERMTU, rather than
1513 * a type field) with the first two bytes
1514 * after the Ethernet/802.3 header being
1517 * Ethernet_802.2 frames, which are 802.3
1518 * frames with an 802.2 LLC header and
1519 * with the IPX LSAP as the DSAP in the LLC
1522 * Ethernet_SNAP frames, which are 802.3
1523 * frames with an LLC header and a SNAP
1524 * header and with an OUI of 0x000000
1525 * (encapsulated Ethernet) and a protocol
1526 * ID of ETHERTYPE_IPX in the SNAP header.
1528 * XXX - should we generate the same code both
1529 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1533 * This generates code to check both for the
1534 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1536 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1537 (bpf_int32
)LLCSAP_IPX
);
1538 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1543 * Now we add code to check for SNAP frames with
1544 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1546 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1550 * Now we generate code to check for 802.3
1551 * frames in general.
1553 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1557 * Now add the check for 802.3 frames before the
1558 * check for Ethernet_802.2 and Ethernet_802.3,
1559 * as those checks should only be done on 802.3
1560 * frames, not on Ethernet frames.
1565 * Now add the check for Ethernet_II frames, and
1566 * do that before checking for the other frame
1569 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1570 (bpf_int32
)ETHERTYPE_IPX
);
1574 case ETHERTYPE_ATALK
:
1575 case ETHERTYPE_AARP
:
1577 * EtherTalk (AppleTalk protocols on Ethernet link
1578 * layer) may use 802.2 encapsulation.
1582 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1583 * we check for an Ethernet type field less than
1584 * 1500, which means it's an 802.3 length field.
1586 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1590 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1591 * SNAP packets with an organization code of
1592 * 0x080007 (Apple, for Appletalk) and a protocol
1593 * type of ETHERTYPE_ATALK (Appletalk).
1595 * 802.2-encapsulated ETHERTYPE_AARP packets are
1596 * SNAP packets with an organization code of
1597 * 0x000000 (encapsulated Ethernet) and a protocol
1598 * type of ETHERTYPE_AARP (Appletalk ARP).
1600 if (proto
== ETHERTYPE_ATALK
)
1601 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1602 else /* proto == ETHERTYPE_AARP */
1603 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1607 * Check for Ethernet encapsulation (Ethertalk
1608 * phase 1?); we just check for the Ethernet
1611 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1617 if (proto
<= ETHERMTU
) {
1619 * This is an LLC SAP value, so the frames
1620 * that match would be 802.2 frames.
1621 * Check that the frame is an 802.2 frame
1622 * (i.e., that the length/type field is
1623 * a length field, <= ETHERMTU) and
1624 * then check the DSAP.
1626 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1628 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1634 * This is an Ethernet type, so compare
1635 * the length/type field with it (if
1636 * the frame is an 802.2 frame, the length
1637 * field will be <= ETHERMTU, and, as
1638 * "proto" is > ETHERMTU, this test
1639 * will fail and the frame won't match,
1640 * which is what we want).
1642 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1649 * Generate code to match a particular packet type.
1651 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1652 * value, if <= ETHERMTU. We use that to determine whether to
1653 * match the type field or to check the type field for the special
1654 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1656 static struct block
*
1657 gen_linux_sll_linktype(proto
)
1660 struct block
*b0
, *b1
;
1666 case LLCSAP_NETBEUI
:
1668 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1669 * so we check the DSAP and SSAP.
1671 * LLCSAP_IP checks for IP-over-802.2, rather
1672 * than IP-over-Ethernet or IP-over-SNAP.
1674 * XXX - should we check both the DSAP and the
1675 * SSAP, like this, or should we check just the
1676 * DSAP, as we do for other types <= ETHERMTU
1677 * (i.e., other SAP values)?
1679 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1680 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1681 ((proto
<< 8) | proto
));
1687 * Ethernet_II frames, which are Ethernet
1688 * frames with a frame type of ETHERTYPE_IPX;
1690 * Ethernet_802.3 frames, which have a frame
1691 * type of LINUX_SLL_P_802_3;
1693 * Ethernet_802.2 frames, which are 802.3
1694 * frames with an 802.2 LLC header (i.e, have
1695 * a frame type of LINUX_SLL_P_802_2) and
1696 * with the IPX LSAP as the DSAP in the LLC
1699 * Ethernet_SNAP frames, which are 802.3
1700 * frames with an LLC header and a SNAP
1701 * header and with an OUI of 0x000000
1702 * (encapsulated Ethernet) and a protocol
1703 * ID of ETHERTYPE_IPX in the SNAP header.
1705 * First, do the checks on LINUX_SLL_P_802_2
1706 * frames; generate the check for either
1707 * Ethernet_802.2 or Ethernet_SNAP frames, and
1708 * then put a check for LINUX_SLL_P_802_2 frames
1711 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1712 (bpf_int32
)LLCSAP_IPX
);
1713 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1716 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1720 * Now check for 802.3 frames and OR that with
1721 * the previous test.
1723 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1727 * Now add the check for Ethernet_II frames, and
1728 * do that before checking for the other frame
1731 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1732 (bpf_int32
)ETHERTYPE_IPX
);
1736 case ETHERTYPE_ATALK
:
1737 case ETHERTYPE_AARP
:
1739 * EtherTalk (AppleTalk protocols on Ethernet link
1740 * layer) may use 802.2 encapsulation.
1744 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1745 * we check for the 802.2 protocol type in the
1746 * "Ethernet type" field.
1748 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1751 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1752 * SNAP packets with an organization code of
1753 * 0x080007 (Apple, for Appletalk) and a protocol
1754 * type of ETHERTYPE_ATALK (Appletalk).
1756 * 802.2-encapsulated ETHERTYPE_AARP packets are
1757 * SNAP packets with an organization code of
1758 * 0x000000 (encapsulated Ethernet) and a protocol
1759 * type of ETHERTYPE_AARP (Appletalk ARP).
1761 if (proto
== ETHERTYPE_ATALK
)
1762 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1764 else /* proto == ETHERTYPE_AARP */
1765 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1770 * Check for Ethernet encapsulation (Ethertalk
1771 * phase 1?); we just check for the Ethernet
1774 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1780 if (proto
<= ETHERMTU
) {
1782 * This is an LLC SAP value, so the frames
1783 * that match would be 802.2 frames.
1784 * Check for the 802.2 protocol type
1785 * in the "Ethernet type" field, and
1786 * then check the DSAP.
1788 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1790 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1796 * This is an Ethernet type, so compare
1797 * the length/type field with it (if
1798 * the frame is an 802.2 frame, the length
1799 * field will be <= ETHERMTU, and, as
1800 * "proto" is > ETHERMTU, this test
1801 * will fail and the frame won't match,
1802 * which is what we want).
1804 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1811 insert_radiotap_load_llprefixlen(b
)
1814 struct slist
*s1
, *s2
;
1817 * Prepend to the statements in this block code to load the
1818 * length of the radiotap header into the register assigned
1819 * to hold that length, if one has been assigned.
1821 if (reg_ll_size
!= -1) {
1823 * The 2 bytes at offsets of 2 and 3 from the beginning
1824 * of the radiotap header are the length of the radiotap
1825 * header; unfortunately, it's little-endian, so we have
1826 * to load it a byte at a time and construct the value.
1830 * Load the high-order byte, at an offset of 3, shift it
1831 * left a byte, and put the result in the X register.
1833 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1835 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1838 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1842 * Load the next byte, at an offset of 2, and OR the
1843 * value from the X register into it.
1845 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1848 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1852 * Now allocate a register to hold that value and store
1855 s2
= new_stmt(BPF_ST
);
1856 s2
->s
.k
= reg_ll_size
;
1860 * Now move it into the X register.
1862 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1866 * Now append all the existing statements in this
1867 * block to these statements.
1869 sappend(s1
, b
->stmts
);
1876 insert_load_llprefixlen(b
)
1881 case DLT_IEEE802_11_RADIO
:
1882 insert_radiotap_load_llprefixlen(b
);
1887 static struct slist
*
1888 gen_radiotap_llprefixlen(void)
1892 if (reg_ll_size
== -1) {
1894 * We haven't yet assigned a register for the length
1895 * of the radiotap header; allocate one.
1897 reg_ll_size
= alloc_reg();
1901 * Load the register containing the radiotap length
1902 * into the X register.
1904 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1905 s
->s
.k
= reg_ll_size
;
1910 * Generate code to compute the link-layer header length, if necessary,
1911 * putting it into the X register, and to return either a pointer to a
1912 * "struct slist" for the list of statements in that code, or NULL if
1913 * no code is necessary.
1915 static struct slist
*
1916 gen_llprefixlen(void)
1920 case DLT_IEEE802_11_RADIO
:
1921 return gen_radiotap_llprefixlen();
1929 * Generate code to match a particular packet type by matching the
1930 * link-layer type field or fields in the 802.2 LLC header.
1932 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1933 * value, if <= ETHERMTU.
1935 static struct block
*
1939 struct block
*b0
, *b1
, *b2
;
1941 /* are we checking MPLS-encapsulated packets? */
1942 if (label_stack_depth
> 0) {
1946 /* FIXME add other L3 proto IDs */
1947 return gen_mpls_linktype(Q_IP
);
1949 case ETHERTYPE_IPV6
:
1951 /* FIXME add other L3 proto IDs */
1952 return gen_mpls_linktype(Q_IPV6
);
1955 bpf_error("unsupported protocol over mpls");
1963 return gen_ether_linktype(proto
);
1971 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1975 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1984 case DLT_IEEE802_11
:
1985 case DLT_IEEE802_11_RADIO_AVS
:
1986 case DLT_IEEE802_11_RADIO
:
1987 case DLT_PRISM_HEADER
:
1988 case DLT_ATM_RFC1483
:
1990 case DLT_IP_OVER_FC
:
1991 return gen_llc_linktype(proto
);
1997 * If "is_lane" is set, check for a LANE-encapsulated
1998 * version of this protocol, otherwise check for an
1999 * LLC-encapsulated version of this protocol.
2001 * We assume LANE means Ethernet, not Token Ring.
2005 * Check that the packet doesn't begin with an
2006 * LE Control marker. (We've already generated
2009 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2014 * Now generate an Ethernet test.
2016 b1
= gen_ether_linktype(proto
);
2021 * Check for LLC encapsulation and then check the
2024 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2025 b1
= gen_llc_linktype(proto
);
2033 return gen_linux_sll_linktype(proto
);
2038 case DLT_SLIP_BSDOS
:
2041 * These types don't provide any type field; packets
2044 * XXX - for IPv4, check for a version number of 4, and,
2045 * for IPv6, check for a version number of 6?
2051 case ETHERTYPE_IPV6
:
2053 return gen_true(); /* always true */
2056 return gen_false(); /* always false */
2063 case DLT_PPP_SERIAL
:
2066 * We use Ethernet protocol types inside libpcap;
2067 * map them to the corresponding PPP protocol types.
2076 case ETHERTYPE_IPV6
:
2085 case ETHERTYPE_ATALK
:
2099 * I'm assuming the "Bridging PDU"s that go
2100 * over PPP are Spanning Tree Protocol
2114 * We use Ethernet protocol types inside libpcap;
2115 * map them to the corresponding PPP protocol types.
2120 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2121 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2123 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2128 case ETHERTYPE_IPV6
:
2138 case ETHERTYPE_ATALK
:
2152 * I'm assuming the "Bridging PDU"s that go
2153 * over PPP are Spanning Tree Protocol
2169 * For DLT_NULL, the link-layer header is a 32-bit
2170 * word containing an AF_ value in *host* byte order,
2171 * and for DLT_ENC, the link-layer header begins
2172 * with a 32-bit work containing an AF_ value in
2175 * In addition, if we're reading a saved capture file,
2176 * the host byte order in the capture may not be the
2177 * same as the host byte order on this machine.
2179 * For DLT_LOOP, the link-layer header is a 32-bit
2180 * word containing an AF_ value in *network* byte order.
2182 * XXX - AF_ values may, unfortunately, be platform-
2183 * dependent; for example, FreeBSD's AF_INET6 is 24
2184 * whilst NetBSD's and OpenBSD's is 26.
2186 * This means that, when reading a capture file, just
2187 * checking for our AF_INET6 value won't work if the
2188 * capture file came from another OS.
2197 case ETHERTYPE_IPV6
:
2204 * Not a type on which we support filtering.
2205 * XXX - support those that have AF_ values
2206 * #defined on this platform, at least?
2211 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2213 * The AF_ value is in host byte order, but
2214 * the BPF interpreter will convert it to
2215 * network byte order.
2217 * If this is a save file, and it's from a
2218 * machine with the opposite byte order to
2219 * ours, we byte-swap the AF_ value.
2221 * Then we run it through "htonl()", and
2222 * generate code to compare against the result.
2224 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2225 bpf_pcap
->sf
.swapped
)
2226 proto
= SWAPLONG(proto
);
2227 proto
= htonl(proto
);
2229 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2233 * af field is host byte order in contrast to the rest of
2236 if (proto
== ETHERTYPE_IP
)
2237 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2238 BPF_B
, (bpf_int32
)AF_INET
));
2240 else if (proto
== ETHERTYPE_IPV6
)
2241 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2242 BPF_B
, (bpf_int32
)AF_INET6
));
2250 case DLT_ARCNET_LINUX
:
2252 * XXX should we check for first fragment if the protocol
2261 case ETHERTYPE_IPV6
:
2262 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2263 (bpf_int32
)ARCTYPE_INET6
));
2267 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2268 (bpf_int32
)ARCTYPE_IP
);
2269 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2270 (bpf_int32
)ARCTYPE_IP_OLD
);
2275 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2276 (bpf_int32
)ARCTYPE_ARP
);
2277 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2278 (bpf_int32
)ARCTYPE_ARP_OLD
);
2282 case ETHERTYPE_REVARP
:
2283 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2284 (bpf_int32
)ARCTYPE_REVARP
));
2286 case ETHERTYPE_ATALK
:
2287 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2288 (bpf_int32
)ARCTYPE_ATALK
));
2295 case ETHERTYPE_ATALK
:
2305 * XXX - assumes a 2-byte Frame Relay header with
2306 * DLCI and flags. What if the address is longer?
2312 * Check for the special NLPID for IP.
2314 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2317 case ETHERTYPE_IPV6
:
2319 * Check for the special NLPID for IPv6.
2321 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2326 * Check for several OSI protocols.
2328 * Frame Relay packets typically have an OSI
2329 * NLPID at the beginning; we check for each
2332 * What we check for is the NLPID and a frame
2333 * control field of UI, i.e. 0x03 followed
2336 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2337 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2338 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2349 case DLT_JUNIPER_MFR
:
2350 case DLT_JUNIPER_MLFR
:
2351 case DLT_JUNIPER_MLPPP
:
2352 case DLT_JUNIPER_ATM1
:
2353 case DLT_JUNIPER_ATM2
:
2354 case DLT_JUNIPER_PPPOE
:
2355 case DLT_JUNIPER_PPPOE_ATM
:
2356 case DLT_JUNIPER_GGSN
:
2357 case DLT_JUNIPER_ES
:
2358 case DLT_JUNIPER_MONITOR
:
2359 case DLT_JUNIPER_SERVICES
:
2360 case DLT_JUNIPER_ETHER
:
2361 case DLT_JUNIPER_PPP
:
2362 case DLT_JUNIPER_FRELAY
:
2363 case DLT_JUNIPER_CHDLC
:
2364 case DLT_JUNIPER_VP
:
2365 /* just lets verify the magic number for now -
2366 * on ATM we may have up to 6 different encapsulations on the wire
2367 * and need a lot of heuristics to figure out that the payload
2370 * FIXME encapsulation specific BPF_ filters
2372 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2374 case DLT_LINUX_IRDA
:
2375 bpf_error("IrDA link-layer type filtering not implemented");
2378 bpf_error("DOCSIS link-layer type filtering not implemented");
2380 case DLT_LINUX_LAPD
:
2381 bpf_error("LAPD link-layer type filtering not implemented");
2385 * All the types that have no encapsulation should either be
2386 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2387 * all packets are IP packets, or should be handled in some
2388 * special case, if none of them are (if some are and some
2389 * aren't, the lack of encapsulation is a problem, as we'd
2390 * have to find some other way of determining the packet type).
2392 * Therefore, if "off_linktype" is -1, there's an error.
2394 if (off_linktype
== (u_int
)-1)
2398 * Any type not handled above should always have an Ethernet
2399 * type at an offset of "off_linktype". (PPP is partially
2400 * handled above - the protocol type is mapped from the
2401 * Ethernet and LLC types we use internally to the corresponding
2402 * PPP type - but the PPP type is always specified by a value
2403 * at "off_linktype", so we don't have to do the code generation
2406 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2410 * Check for an LLC SNAP packet with a given organization code and
2411 * protocol type; we check the entire contents of the 802.2 LLC and
2412 * snap headers, checking for DSAP and SSAP of SNAP and a control
2413 * field of 0x03 in the LLC header, and for the specified organization
2414 * code and protocol type in the SNAP header.
2416 static struct block
*
2417 gen_snap(orgcode
, ptype
, offset
)
2418 bpf_u_int32 orgcode
;
2422 u_char snapblock
[8];
2424 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2425 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2426 snapblock
[2] = 0x03; /* control = UI */
2427 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2428 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2429 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2430 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2431 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2432 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2436 * Generate code to match a particular packet type, for link-layer types
2437 * using 802.2 LLC headers.
2439 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2440 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2442 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2443 * value, if <= ETHERMTU. We use that to determine whether to
2444 * match the DSAP or both DSAP and LSAP or to check the OUI and
2445 * protocol ID in a SNAP header.
2447 static struct block
*
2448 gen_llc_linktype(proto
)
2452 * XXX - handle token-ring variable-length header.
2458 case LLCSAP_NETBEUI
:
2460 * XXX - should we check both the DSAP and the
2461 * SSAP, like this, or should we check just the
2462 * DSAP, as we do for other types <= ETHERMTU
2463 * (i.e., other SAP values)?
2465 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2466 ((proto
<< 8) | proto
));
2470 * XXX - are there ever SNAP frames for IPX on
2471 * non-Ethernet 802.x networks?
2473 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2474 (bpf_int32
)LLCSAP_IPX
);
2476 case ETHERTYPE_ATALK
:
2478 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2479 * SNAP packets with an organization code of
2480 * 0x080007 (Apple, for Appletalk) and a protocol
2481 * type of ETHERTYPE_ATALK (Appletalk).
2483 * XXX - check for an organization code of
2484 * encapsulated Ethernet as well?
2486 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2490 * XXX - we don't have to check for IPX 802.3
2491 * here, but should we check for the IPX Ethertype?
2493 if (proto
<= ETHERMTU
) {
2495 * This is an LLC SAP value, so check
2498 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2502 * This is an Ethernet type; we assume that it's
2503 * unlikely that it'll appear in the right place
2504 * at random, and therefore check only the
2505 * location that would hold the Ethernet type
2506 * in a SNAP frame with an organization code of
2507 * 0x000000 (encapsulated Ethernet).
2509 * XXX - if we were to check for the SNAP DSAP and
2510 * LSAP, as per XXX, and were also to check for an
2511 * organization code of 0x000000 (encapsulated
2512 * Ethernet), we'd do
2514 * return gen_snap(0x000000, proto,
2517 * here; for now, we don't, as per the above.
2518 * I don't know whether it's worth the extra CPU
2519 * time to do the right check or not.
2521 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2527 static struct block
*
2528 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2532 u_int src_off
, dst_off
;
2534 struct block
*b0
, *b1
;
2548 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2549 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2555 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2556 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2563 b0
= gen_linktype(proto
);
2564 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2570 static struct block
*
2571 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2572 struct in6_addr
*addr
;
2573 struct in6_addr
*mask
;
2575 u_int src_off
, dst_off
;
2577 struct block
*b0
, *b1
;
2592 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2593 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2599 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2600 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2607 /* this order is important */
2608 a
= (u_int32_t
*)addr
;
2609 m
= (u_int32_t
*)mask
;
2610 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2611 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2613 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2615 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2617 b0
= gen_linktype(proto
);
2623 static struct block
*
2624 gen_ehostop(eaddr
, dir
)
2625 register const u_char
*eaddr
;
2628 register struct block
*b0
, *b1
;
2632 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2635 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2638 b0
= gen_ehostop(eaddr
, Q_SRC
);
2639 b1
= gen_ehostop(eaddr
, Q_DST
);
2645 b0
= gen_ehostop(eaddr
, Q_SRC
);
2646 b1
= gen_ehostop(eaddr
, Q_DST
);
2655 * Like gen_ehostop, but for DLT_FDDI
2657 static struct block
*
2658 gen_fhostop(eaddr
, dir
)
2659 register const u_char
*eaddr
;
2662 struct block
*b0
, *b1
;
2667 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2669 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2674 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2676 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2680 b0
= gen_fhostop(eaddr
, Q_SRC
);
2681 b1
= gen_fhostop(eaddr
, Q_DST
);
2687 b0
= gen_fhostop(eaddr
, Q_SRC
);
2688 b1
= gen_fhostop(eaddr
, Q_DST
);
2697 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2699 static struct block
*
2700 gen_thostop(eaddr
, dir
)
2701 register const u_char
*eaddr
;
2704 register struct block
*b0
, *b1
;
2708 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2711 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2714 b0
= gen_thostop(eaddr
, Q_SRC
);
2715 b1
= gen_thostop(eaddr
, Q_DST
);
2721 b0
= gen_thostop(eaddr
, Q_SRC
);
2722 b1
= gen_thostop(eaddr
, Q_DST
);
2731 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2733 static struct block
*
2734 gen_wlanhostop(eaddr
, dir
)
2735 register const u_char
*eaddr
;
2738 register struct block
*b0
, *b1
, *b2
;
2739 register struct slist
*s
;
2746 * For control frames, there is no SA.
2748 * For management frames, SA is at an
2749 * offset of 10 from the beginning of
2752 * For data frames, SA is at an offset
2753 * of 10 from the beginning of the packet
2754 * if From DS is clear, at an offset of
2755 * 16 from the beginning of the packet
2756 * if From DS is set and To DS is clear,
2757 * and an offset of 24 from the beginning
2758 * of the packet if From DS is set and To DS
2763 * Generate the tests to be done for data frames
2766 * First, check for To DS set, i.e. check "link[1] & 0x01".
2768 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2769 b1
= new_block(JMP(BPF_JSET
));
2770 b1
->s
.k
= 0x01; /* To DS */
2774 * If To DS is set, the SA is at 24.
2776 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2780 * Now, check for To DS not set, i.e. check
2781 * "!(link[1] & 0x01)".
2783 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2784 b2
= new_block(JMP(BPF_JSET
));
2785 b2
->s
.k
= 0x01; /* To DS */
2790 * If To DS is not set, the SA is at 16.
2792 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2796 * Now OR together the last two checks. That gives
2797 * the complete set of checks for data frames with
2803 * Now check for From DS being set, and AND that with
2804 * the ORed-together checks.
2806 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2807 b1
= new_block(JMP(BPF_JSET
));
2808 b1
->s
.k
= 0x02; /* From DS */
2813 * Now check for data frames with From DS not set.
2815 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2816 b2
= new_block(JMP(BPF_JSET
));
2817 b2
->s
.k
= 0x02; /* From DS */
2822 * If From DS isn't set, the SA is at 10.
2824 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2828 * Now OR together the checks for data frames with
2829 * From DS not set and for data frames with From DS
2830 * set; that gives the checks done for data frames.
2835 * Now check for a data frame.
2836 * I.e, check "link[0] & 0x08".
2838 gen_load_a(OR_LINK
, 0, BPF_B
);
2839 b1
= new_block(JMP(BPF_JSET
));
2844 * AND that with the checks done for data frames.
2849 * If the high-order bit of the type value is 0, this
2850 * is a management frame.
2851 * I.e, check "!(link[0] & 0x08)".
2853 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2854 b2
= new_block(JMP(BPF_JSET
));
2860 * For management frames, the SA is at 10.
2862 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2866 * OR that with the checks done for data frames.
2867 * That gives the checks done for management and
2873 * If the low-order bit of the type value is 1,
2874 * this is either a control frame or a frame
2875 * with a reserved type, and thus not a
2878 * I.e., check "!(link[0] & 0x04)".
2880 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2881 b1
= new_block(JMP(BPF_JSET
));
2887 * AND that with the checks for data and management
2897 * For control frames, there is no DA.
2899 * For management frames, DA is at an
2900 * offset of 4 from the beginning of
2903 * For data frames, DA is at an offset
2904 * of 4 from the beginning of the packet
2905 * if To DS is clear and at an offset of
2906 * 16 from the beginning of the packet
2911 * Generate the tests to be done for data frames.
2913 * First, check for To DS set, i.e. "link[1] & 0x01".
2915 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2916 b1
= new_block(JMP(BPF_JSET
));
2917 b1
->s
.k
= 0x01; /* To DS */
2921 * If To DS is set, the DA is at 16.
2923 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2927 * Now, check for To DS not set, i.e. check
2928 * "!(link[1] & 0x01)".
2930 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2931 b2
= new_block(JMP(BPF_JSET
));
2932 b2
->s
.k
= 0x01; /* To DS */
2937 * If To DS is not set, the DA is at 4.
2939 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2943 * Now OR together the last two checks. That gives
2944 * the complete set of checks for data frames.
2949 * Now check for a data frame.
2950 * I.e, check "link[0] & 0x08".
2952 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2953 b1
= new_block(JMP(BPF_JSET
));
2958 * AND that with the checks done for data frames.
2963 * If the high-order bit of the type value is 0, this
2964 * is a management frame.
2965 * I.e, check "!(link[0] & 0x08)".
2967 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2968 b2
= new_block(JMP(BPF_JSET
));
2974 * For management frames, the DA is at 4.
2976 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2980 * OR that with the checks done for data frames.
2981 * That gives the checks done for management and
2987 * If the low-order bit of the type value is 1,
2988 * this is either a control frame or a frame
2989 * with a reserved type, and thus not a
2992 * I.e., check "!(link[0] & 0x04)".
2994 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2995 b1
= new_block(JMP(BPF_JSET
));
3001 * AND that with the checks for data and management
3008 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3009 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3015 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3016 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3025 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3026 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3027 * as the RFC states.)
3029 static struct block
*
3030 gen_ipfchostop(eaddr
, dir
)
3031 register const u_char
*eaddr
;
3034 register struct block
*b0
, *b1
;
3038 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3041 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3044 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3045 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3051 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3052 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3061 * This is quite tricky because there may be pad bytes in front of the
3062 * DECNET header, and then there are two possible data packet formats that
3063 * carry both src and dst addresses, plus 5 packet types in a format that
3064 * carries only the src node, plus 2 types that use a different format and
3065 * also carry just the src node.
3069 * Instead of doing those all right, we just look for data packets with
3070 * 0 or 1 bytes of padding. If you want to look at other packets, that
3071 * will require a lot more hacking.
3073 * To add support for filtering on DECNET "areas" (network numbers)
3074 * one would want to add a "mask" argument to this routine. That would
3075 * make the filter even more inefficient, although one could be clever
3076 * and not generate masking instructions if the mask is 0xFFFF.
3078 static struct block
*
3079 gen_dnhostop(addr
, dir
)
3083 struct block
*b0
, *b1
, *b2
, *tmp
;
3084 u_int offset_lh
; /* offset if long header is received */
3085 u_int offset_sh
; /* offset if short header is received */
3090 offset_sh
= 1; /* follows flags */
3091 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3095 offset_sh
= 3; /* follows flags, dstnode */
3096 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3100 /* Inefficient because we do our Calvinball dance twice */
3101 b0
= gen_dnhostop(addr
, Q_SRC
);
3102 b1
= gen_dnhostop(addr
, Q_DST
);
3108 /* Inefficient because we do our Calvinball dance twice */
3109 b0
= gen_dnhostop(addr
, Q_SRC
);
3110 b1
= gen_dnhostop(addr
, Q_DST
);
3115 bpf_error("ISO host filtering not implemented");
3120 b0
= gen_linktype(ETHERTYPE_DN
);
3121 /* Check for pad = 1, long header case */
3122 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3123 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3124 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3125 BPF_H
, (bpf_int32
)ntohs(addr
));
3127 /* Check for pad = 0, long header case */
3128 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3129 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3132 /* Check for pad = 1, short header case */
3133 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3134 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3135 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3138 /* Check for pad = 0, short header case */
3139 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3140 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3144 /* Combine with test for linktype */
3150 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3151 * test the bottom-of-stack bit, and then check the version number
3152 * field in the IP header.
3154 static struct block
*
3155 gen_mpls_linktype(proto
)
3158 struct block
*b0
, *b1
;
3163 /* match the bottom-of-stack bit */
3164 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3165 /* match the IPv4 version number */
3166 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3171 /* match the bottom-of-stack bit */
3172 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3173 /* match the IPv4 version number */
3174 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3183 static struct block
*
3184 gen_host(addr
, mask
, proto
, dir
, type
)
3191 struct block
*b0
, *b1
;
3192 const char *typestr
;
3202 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3204 * Only check for non-IPv4 addresses if we're not
3205 * checking MPLS-encapsulated packets.
3207 if (label_stack_depth
== 0) {
3208 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3210 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3216 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3219 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3222 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3225 bpf_error("'tcp' modifier applied to %s", typestr
);
3228 bpf_error("'sctp' modifier applied to %s", typestr
);
3231 bpf_error("'udp' modifier applied to %s", typestr
);
3234 bpf_error("'icmp' modifier applied to %s", typestr
);
3237 bpf_error("'igmp' modifier applied to %s", typestr
);
3240 bpf_error("'igrp' modifier applied to %s", typestr
);
3243 bpf_error("'pim' modifier applied to %s", typestr
);
3246 bpf_error("'vrrp' modifier applied to %s", typestr
);
3249 bpf_error("ATALK host filtering not implemented");
3252 bpf_error("AARP host filtering not implemented");
3255 return gen_dnhostop(addr
, dir
);
3258 bpf_error("SCA host filtering not implemented");
3261 bpf_error("LAT host filtering not implemented");
3264 bpf_error("MOPDL host filtering not implemented");
3267 bpf_error("MOPRC host filtering not implemented");
3271 bpf_error("'ip6' modifier applied to ip host");
3274 bpf_error("'icmp6' modifier applied to %s", typestr
);
3278 bpf_error("'ah' modifier applied to %s", typestr
);
3281 bpf_error("'esp' modifier applied to %s", typestr
);
3284 bpf_error("ISO host filtering not implemented");
3287 bpf_error("'esis' modifier applied to %s", typestr
);
3290 bpf_error("'isis' modifier applied to %s", typestr
);
3293 bpf_error("'clnp' modifier applied to %s", typestr
);
3296 bpf_error("'stp' modifier applied to %s", typestr
);
3299 bpf_error("IPX host filtering not implemented");
3302 bpf_error("'netbeui' modifier applied to %s", typestr
);
3305 bpf_error("'radio' modifier applied to %s", typestr
);
3314 static struct block
*
3315 gen_host6(addr
, mask
, proto
, dir
, type
)
3316 struct in6_addr
*addr
;
3317 struct in6_addr
*mask
;
3322 const char *typestr
;
3332 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3335 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3338 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3341 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3344 bpf_error("'sctp' modifier applied to %s", typestr
);
3347 bpf_error("'tcp' modifier applied to %s", typestr
);
3350 bpf_error("'udp' modifier applied to %s", typestr
);
3353 bpf_error("'icmp' modifier applied to %s", typestr
);
3356 bpf_error("'igmp' modifier applied to %s", typestr
);
3359 bpf_error("'igrp' modifier applied to %s", typestr
);
3362 bpf_error("'pim' modifier applied to %s", typestr
);
3365 bpf_error("'vrrp' modifier applied to %s", typestr
);
3368 bpf_error("ATALK host filtering not implemented");
3371 bpf_error("AARP host filtering not implemented");
3374 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3377 bpf_error("SCA host filtering not implemented");
3380 bpf_error("LAT host filtering not implemented");
3383 bpf_error("MOPDL host filtering not implemented");
3386 bpf_error("MOPRC host filtering not implemented");
3389 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3392 bpf_error("'icmp6' modifier applied to %s", typestr
);
3395 bpf_error("'ah' modifier applied to %s", typestr
);
3398 bpf_error("'esp' modifier applied to %s", typestr
);
3401 bpf_error("ISO host filtering not implemented");
3404 bpf_error("'esis' modifier applied to %s", typestr
);
3407 bpf_error("'isis' modifier applied to %s", typestr
);
3410 bpf_error("'clnp' modifier applied to %s", typestr
);
3413 bpf_error("'stp' modifier applied to %s", typestr
);
3416 bpf_error("IPX host filtering not implemented");
3419 bpf_error("'netbeui' modifier applied to %s", typestr
);
3422 bpf_error("'radio' modifier applied to %s", typestr
);
3432 static struct block
*
3433 gen_gateway(eaddr
, alist
, proto
, dir
)
3434 const u_char
*eaddr
;
3435 bpf_u_int32
**alist
;
3439 struct block
*b0
, *b1
, *tmp
;
3442 bpf_error("direction applied to 'gateway'");
3451 b0
= gen_ehostop(eaddr
, Q_OR
);
3454 b0
= gen_fhostop(eaddr
, Q_OR
);
3457 b0
= gen_thostop(eaddr
, Q_OR
);
3459 case DLT_IEEE802_11
:
3460 case DLT_IEEE802_11_RADIO_AVS
:
3461 case DLT_IEEE802_11_RADIO
:
3462 case DLT_PRISM_HEADER
:
3463 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3468 * Check that the packet doesn't begin with an
3469 * LE Control marker. (We've already generated
3472 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3477 * Now check the MAC address.
3479 b0
= gen_ehostop(eaddr
, Q_OR
);
3483 case DLT_IP_OVER_FC
:
3484 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3488 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3490 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3492 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3501 bpf_error("illegal modifier of 'gateway'");
3507 gen_proto_abbrev(proto
)
3516 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3518 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3524 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3526 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3532 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3534 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3540 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3543 #ifndef IPPROTO_IGMP
3544 #define IPPROTO_IGMP 2
3548 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3551 #ifndef IPPROTO_IGRP
3552 #define IPPROTO_IGRP 9
3555 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3559 #define IPPROTO_PIM 103
3563 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3565 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3570 #ifndef IPPROTO_VRRP
3571 #define IPPROTO_VRRP 112
3575 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3579 b1
= gen_linktype(ETHERTYPE_IP
);
3583 b1
= gen_linktype(ETHERTYPE_ARP
);
3587 b1
= gen_linktype(ETHERTYPE_REVARP
);
3591 bpf_error("link layer applied in wrong context");
3594 b1
= gen_linktype(ETHERTYPE_ATALK
);
3598 b1
= gen_linktype(ETHERTYPE_AARP
);
3602 b1
= gen_linktype(ETHERTYPE_DN
);
3606 b1
= gen_linktype(ETHERTYPE_SCA
);
3610 b1
= gen_linktype(ETHERTYPE_LAT
);
3614 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3618 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3623 b1
= gen_linktype(ETHERTYPE_IPV6
);
3626 #ifndef IPPROTO_ICMPV6
3627 #define IPPROTO_ICMPV6 58
3630 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3635 #define IPPROTO_AH 51
3638 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3640 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3646 #define IPPROTO_ESP 50
3649 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3651 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3657 b1
= gen_linktype(LLCSAP_ISONS
);
3661 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3665 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3668 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3669 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3670 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3672 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3674 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3676 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3680 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3681 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3682 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3684 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3686 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3688 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3692 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3693 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3694 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3696 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3701 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3702 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3707 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3708 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3710 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3712 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3717 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3718 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3723 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3724 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3729 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3733 b1
= gen_linktype(LLCSAP_8021D
);
3737 b1
= gen_linktype(LLCSAP_IPX
);
3741 b1
= gen_linktype(LLCSAP_NETBEUI
);
3745 bpf_error("'radio' is not a valid protocol type");
3753 static struct block
*
3760 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3761 b
= new_block(JMP(BPF_JSET
));
3770 * Generate a comparison to a port value in the transport-layer header
3771 * at the specified offset from the beginning of that header.
3773 * XXX - this handles a variable-length prefix preceding the link-layer
3774 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3775 * variable-length link-layer headers (such as Token Ring or 802.11
3778 static struct block
*
3779 gen_portatom(off
, v
)
3783 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3787 static struct block
*
3788 gen_portatom6(off
, v
)
3792 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3797 gen_portop(port
, proto
, dir
)
3798 int port
, proto
, dir
;
3800 struct block
*b0
, *b1
, *tmp
;
3802 /* ip proto 'proto' */
3803 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3809 b1
= gen_portatom(0, (bpf_int32
)port
);
3813 b1
= gen_portatom(2, (bpf_int32
)port
);
3818 tmp
= gen_portatom(0, (bpf_int32
)port
);
3819 b1
= gen_portatom(2, (bpf_int32
)port
);
3824 tmp
= gen_portatom(0, (bpf_int32
)port
);
3825 b1
= gen_portatom(2, (bpf_int32
)port
);
3837 static struct block
*
3838 gen_port(port
, ip_proto
, dir
)
3843 struct block
*b0
, *b1
, *tmp
;
3848 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3849 * not LLC encapsulation with LLCSAP_IP.
3851 * For IEEE 802 networks - which includes 802.5 token ring
3852 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3853 * says that SNAP encapsulation is used, not LLC encapsulation
3856 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3857 * RFC 2225 say that SNAP encapsulation is used, not LLC
3858 * encapsulation with LLCSAP_IP.
3860 * So we always check for ETHERTYPE_IP.
3862 b0
= gen_linktype(ETHERTYPE_IP
);
3868 b1
= gen_portop(port
, ip_proto
, dir
);
3872 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3873 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3875 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3888 gen_portop6(port
, proto
, dir
)
3889 int port
, proto
, dir
;
3891 struct block
*b0
, *b1
, *tmp
;
3893 /* ip6 proto 'proto' */
3894 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3898 b1
= gen_portatom6(0, (bpf_int32
)port
);
3902 b1
= gen_portatom6(2, (bpf_int32
)port
);
3907 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3908 b1
= gen_portatom6(2, (bpf_int32
)port
);
3913 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3914 b1
= gen_portatom6(2, (bpf_int32
)port
);
3926 static struct block
*
3927 gen_port6(port
, ip_proto
, dir
)
3932 struct block
*b0
, *b1
, *tmp
;
3934 /* link proto ip6 */
3935 b0
= gen_linktype(ETHERTYPE_IPV6
);
3941 b1
= gen_portop6(port
, ip_proto
, dir
);
3945 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3946 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3948 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3960 /* gen_portrange code */
3961 static struct block
*
3962 gen_portrangeatom(off
, v1
, v2
)
3966 struct block
*b1
, *b2
;
3970 * Reverse the order of the ports, so v1 is the lower one.
3979 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3980 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3988 gen_portrangeop(port1
, port2
, proto
, dir
)
3993 struct block
*b0
, *b1
, *tmp
;
3995 /* ip proto 'proto' */
3996 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4002 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4006 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4011 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4012 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4017 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4018 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4030 static struct block
*
4031 gen_portrange(port1
, port2
, ip_proto
, dir
)
4036 struct block
*b0
, *b1
, *tmp
;
4039 b0
= gen_linktype(ETHERTYPE_IP
);
4045 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4049 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4050 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4052 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4064 static struct block
*
4065 gen_portrangeatom6(off
, v1
, v2
)
4069 struct block
*b1
, *b2
;
4073 * Reverse the order of the ports, so v1 is the lower one.
4082 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4083 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4091 gen_portrangeop6(port1
, port2
, proto
, dir
)
4096 struct block
*b0
, *b1
, *tmp
;
4098 /* ip6 proto 'proto' */
4099 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4103 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4107 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4112 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4113 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4118 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4119 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4131 static struct block
*
4132 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4137 struct block
*b0
, *b1
, *tmp
;
4139 /* link proto ip6 */
4140 b0
= gen_linktype(ETHERTYPE_IPV6
);
4146 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4150 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4151 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4153 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4166 lookup_proto(name
, proto
)
4167 register const char *name
;
4177 v
= pcap_nametoproto(name
);
4178 if (v
== PROTO_UNDEF
)
4179 bpf_error("unknown ip proto '%s'", name
);
4183 /* XXX should look up h/w protocol type based on linktype */
4184 v
= pcap_nametoeproto(name
);
4185 if (v
== PROTO_UNDEF
) {
4186 v
= pcap_nametollc(name
);
4187 if (v
== PROTO_UNDEF
)
4188 bpf_error("unknown ether proto '%s'", name
);
4193 if (strcmp(name
, "esis") == 0)
4195 else if (strcmp(name
, "isis") == 0)
4197 else if (strcmp(name
, "clnp") == 0)
4200 bpf_error("unknown osi proto '%s'", name
);
4220 static struct block
*
4221 gen_protochain(v
, proto
, dir
)
4226 #ifdef NO_PROTOCHAIN
4227 return gen_proto(v
, proto
, dir
);
4229 struct block
*b0
, *b
;
4230 struct slist
*s
[100];
4231 int fix2
, fix3
, fix4
, fix5
;
4232 int ahcheck
, again
, end
;
4234 int reg2
= alloc_reg();
4236 memset(s
, 0, sizeof(s
));
4237 fix2
= fix3
= fix4
= fix5
= 0;
4244 b0
= gen_protochain(v
, Q_IP
, dir
);
4245 b
= gen_protochain(v
, Q_IPV6
, dir
);
4249 bpf_error("bad protocol applied for 'protochain'");
4254 * We don't handle variable-length radiotap here headers yet.
4255 * We might want to add BPF instructions to do the protochain
4256 * work, to simplify that and, on platforms that have a BPF
4257 * interpreter with the new instructions, let the filtering
4258 * be done in the kernel. (We already require a modified BPF
4259 * engine to do the protochain stuff, to support backward
4260 * branches, and backward branch support is unlikely to appear
4261 * in kernel BPF engines.)
4263 if (linktype
== DLT_IEEE802_11_RADIO
)
4264 bpf_error("'protochain' not supported with radiotap headers");
4266 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4269 * s[0] is a dummy entry to protect other BPF insn from damage
4270 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4271 * hard to find interdependency made by jump table fixup.
4274 s
[i
] = new_stmt(0); /*dummy*/
4279 b0
= gen_linktype(ETHERTYPE_IP
);
4282 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4283 s
[i
]->s
.k
= off_ll
+ off_nl
+ 9;
4285 /* X = ip->ip_hl << 2 */
4286 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4287 s
[i
]->s
.k
= off_ll
+ off_nl
;
4292 b0
= gen_linktype(ETHERTYPE_IPV6
);
4294 /* A = ip6->ip_nxt */
4295 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4296 s
[i
]->s
.k
= off_ll
+ off_nl
+ 6;
4298 /* X = sizeof(struct ip6_hdr) */
4299 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4305 bpf_error("unsupported proto to gen_protochain");
4309 /* again: if (A == v) goto end; else fall through; */
4311 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4313 s
[i
]->s
.jt
= NULL
; /*later*/
4314 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4318 #ifndef IPPROTO_NONE
4319 #define IPPROTO_NONE 59
4321 /* if (A == IPPROTO_NONE) goto end */
4322 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4323 s
[i
]->s
.jt
= NULL
; /*later*/
4324 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4325 s
[i
]->s
.k
= IPPROTO_NONE
;
4326 s
[fix5
]->s
.jf
= s
[i
];
4331 if (proto
== Q_IPV6
) {
4332 int v6start
, v6end
, v6advance
, j
;
4335 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4336 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4337 s
[i
]->s
.jt
= NULL
; /*later*/
4338 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4339 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4340 s
[fix2
]->s
.jf
= s
[i
];
4342 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4343 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4344 s
[i
]->s
.jt
= NULL
; /*later*/
4345 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4346 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4348 /* if (A == IPPROTO_ROUTING) goto v6advance */
4349 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4350 s
[i
]->s
.jt
= NULL
; /*later*/
4351 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4352 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4354 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4355 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4356 s
[i
]->s
.jt
= NULL
; /*later*/
4357 s
[i
]->s
.jf
= NULL
; /*later*/
4358 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4369 * X = X + (P[X + 1] + 1) * 8;
4372 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4374 /* A = P[X + packet head] */
4375 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4376 s
[i
]->s
.k
= off_ll
+ off_nl
;
4379 s
[i
] = new_stmt(BPF_ST
);
4383 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4386 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4390 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4392 /* A = P[X + packet head]; */
4393 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4394 s
[i
]->s
.k
= off_ll
+ off_nl
;
4397 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4401 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4405 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4408 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4412 /* goto again; (must use BPF_JA for backward jump) */
4413 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4414 s
[i
]->s
.k
= again
- i
- 1;
4415 s
[i
- 1]->s
.jf
= s
[i
];
4419 for (j
= v6start
; j
<= v6end
; j
++)
4420 s
[j
]->s
.jt
= s
[v6advance
];
4425 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4427 s
[fix2
]->s
.jf
= s
[i
];
4433 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4434 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4435 s
[i
]->s
.jt
= NULL
; /*later*/
4436 s
[i
]->s
.jf
= NULL
; /*later*/
4437 s
[i
]->s
.k
= IPPROTO_AH
;
4439 s
[fix3
]->s
.jf
= s
[ahcheck
];
4446 * X = X + (P[X + 1] + 2) * 4;
4449 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4451 /* A = P[X + packet head]; */
4452 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4453 s
[i
]->s
.k
= off_ll
+ off_nl
;
4456 s
[i
] = new_stmt(BPF_ST
);
4460 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4463 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4467 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4469 /* A = P[X + packet head] */
4470 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4471 s
[i
]->s
.k
= off_ll
+ off_nl
;
4474 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4478 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4482 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4485 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4489 /* goto again; (must use BPF_JA for backward jump) */
4490 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4491 s
[i
]->s
.k
= again
- i
- 1;
4496 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4498 s
[fix2
]->s
.jt
= s
[end
];
4499 s
[fix4
]->s
.jf
= s
[end
];
4500 s
[fix5
]->s
.jt
= s
[end
];
4507 for (i
= 0; i
< max
- 1; i
++)
4508 s
[i
]->next
= s
[i
+ 1];
4509 s
[max
- 1]->next
= NULL
;
4514 b
= new_block(JMP(BPF_JEQ
));
4515 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4526 * Generate code that checks whether the packet is a packet for protocol
4527 * <proto> and whether the type field in that protocol's header has
4528 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4529 * IP packet and checks the protocol number in the IP header against <v>.
4531 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4532 * against Q_IP and Q_IPV6.
4534 static struct block
*
4535 gen_proto(v
, proto
, dir
)
4540 struct block
*b0
, *b1
;
4542 if (dir
!= Q_DEFAULT
)
4543 bpf_error("direction applied to 'proto'");
4548 b0
= gen_proto(v
, Q_IP
, dir
);
4549 b1
= gen_proto(v
, Q_IPV6
, dir
);
4557 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4558 * not LLC encapsulation with LLCSAP_IP.
4560 * For IEEE 802 networks - which includes 802.5 token ring
4561 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4562 * says that SNAP encapsulation is used, not LLC encapsulation
4565 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4566 * RFC 2225 say that SNAP encapsulation is used, not LLC
4567 * encapsulation with LLCSAP_IP.
4569 * So we always check for ETHERTYPE_IP.
4572 b0
= gen_linktype(ETHERTYPE_IP
);
4574 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4576 b1
= gen_protochain(v
, Q_IP
);
4586 * Frame Relay packets typically have an OSI
4587 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4588 * generates code to check for all the OSI
4589 * NLPIDs, so calling it and then adding a check
4590 * for the particular NLPID for which we're
4591 * looking is bogus, as we can just check for
4594 * What we check for is the NLPID and a frame
4595 * control field value of UI, i.e. 0x03 followed
4598 * XXX - assumes a 2-byte Frame Relay header with
4599 * DLCI and flags. What if the address is longer?
4601 * XXX - what about SNAP-encapsulated frames?
4603 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4609 * Cisco uses an Ethertype lookalike - for OSI,
4612 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4613 /* OSI in C-HDLC is stuffed with a fudge byte */
4614 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4619 b0
= gen_linktype(LLCSAP_ISONS
);
4620 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4626 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4628 * 4 is the offset of the PDU type relative to the IS-IS
4631 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4636 bpf_error("arp does not encapsulate another protocol");
4640 bpf_error("rarp does not encapsulate another protocol");
4644 bpf_error("atalk encapsulation is not specifiable");
4648 bpf_error("decnet encapsulation is not specifiable");
4652 bpf_error("sca does not encapsulate another protocol");
4656 bpf_error("lat does not encapsulate another protocol");
4660 bpf_error("moprc does not encapsulate another protocol");
4664 bpf_error("mopdl does not encapsulate another protocol");
4668 return gen_linktype(v
);
4671 bpf_error("'udp proto' is bogus");
4675 bpf_error("'tcp proto' is bogus");
4679 bpf_error("'sctp proto' is bogus");
4683 bpf_error("'icmp proto' is bogus");
4687 bpf_error("'igmp proto' is bogus");
4691 bpf_error("'igrp proto' is bogus");
4695 bpf_error("'pim proto' is bogus");
4699 bpf_error("'vrrp proto' is bogus");
4704 b0
= gen_linktype(ETHERTYPE_IPV6
);
4706 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4708 b1
= gen_protochain(v
, Q_IPV6
);
4714 bpf_error("'icmp6 proto' is bogus");
4718 bpf_error("'ah proto' is bogus");
4721 bpf_error("'ah proto' is bogus");
4724 bpf_error("'stp proto' is bogus");
4727 bpf_error("'ipx proto' is bogus");
4730 bpf_error("'netbeui proto' is bogus");
4733 bpf_error("'radio proto' is bogus");
4744 register const char *name
;
4747 int proto
= q
.proto
;
4751 bpf_u_int32 mask
, addr
;
4753 bpf_u_int32
**alist
;
4756 struct sockaddr_in
*sin
;
4757 struct sockaddr_in6
*sin6
;
4758 struct addrinfo
*res
, *res0
;
4759 struct in6_addr mask128
;
4761 struct block
*b
, *tmp
;
4762 int port
, real_proto
;
4768 addr
= pcap_nametonetaddr(name
);
4770 bpf_error("unknown network '%s'", name
);
4771 /* Left justify network addr and calculate its network mask */
4773 while (addr
&& (addr
& 0xff000000) == 0) {
4777 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
4781 if (proto
== Q_LINK
) {
4785 eaddr
= pcap_ether_hostton(name
);
4788 "unknown ether host '%s'", name
);
4789 b
= gen_ehostop(eaddr
, dir
);
4794 eaddr
= pcap_ether_hostton(name
);
4797 "unknown FDDI host '%s'", name
);
4798 b
= gen_fhostop(eaddr
, dir
);
4803 eaddr
= pcap_ether_hostton(name
);
4806 "unknown token ring host '%s'", name
);
4807 b
= gen_thostop(eaddr
, dir
);
4811 case DLT_IEEE802_11
:
4812 case DLT_IEEE802_11_RADIO_AVS
:
4813 case DLT_IEEE802_11_RADIO
:
4814 case DLT_PRISM_HEADER
:
4815 eaddr
= pcap_ether_hostton(name
);
4818 "unknown 802.11 host '%s'", name
);
4819 b
= gen_wlanhostop(eaddr
, dir
);
4823 case DLT_IP_OVER_FC
:
4824 eaddr
= pcap_ether_hostton(name
);
4827 "unknown Fibre Channel host '%s'", name
);
4828 b
= gen_ipfchostop(eaddr
, dir
);
4837 * Check that the packet doesn't begin
4838 * with an LE Control marker. (We've
4839 * already generated a test for LANE.)
4841 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4845 eaddr
= pcap_ether_hostton(name
);
4848 "unknown ether host '%s'", name
);
4849 b
= gen_ehostop(eaddr
, dir
);
4855 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4856 } else if (proto
== Q_DECNET
) {
4857 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4859 * I don't think DECNET hosts can be multihomed, so
4860 * there is no need to build up a list of addresses
4862 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
4865 alist
= pcap_nametoaddr(name
);
4866 if (alist
== NULL
|| *alist
== NULL
)
4867 bpf_error("unknown host '%s'", name
);
4869 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4871 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
4873 tmp
= gen_host(**alist
++, 0xffffffff,
4874 tproto
, dir
, q
.addr
);
4880 memset(&mask128
, 0xff, sizeof(mask128
));
4881 res0
= res
= pcap_nametoaddrinfo(name
);
4883 bpf_error("unknown host '%s'", name
);
4885 tproto
= tproto6
= proto
;
4886 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4890 for (res
= res0
; res
; res
= res
->ai_next
) {
4891 switch (res
->ai_family
) {
4893 if (tproto
== Q_IPV6
)
4896 sin
= (struct sockaddr_in
*)
4898 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4899 0xffffffff, tproto
, dir
, q
.addr
);
4902 if (tproto6
== Q_IP
)
4905 sin6
= (struct sockaddr_in6
*)
4907 tmp
= gen_host6(&sin6
->sin6_addr
,
4908 &mask128
, tproto6
, dir
, q
.addr
);
4919 bpf_error("unknown host '%s'%s", name
,
4920 (proto
== Q_DEFAULT
)
4922 : " for specified address family");
4929 if (proto
!= Q_DEFAULT
&&
4930 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4931 bpf_error("illegal qualifier of 'port'");
4932 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4933 bpf_error("unknown port '%s'", name
);
4934 if (proto
== Q_UDP
) {
4935 if (real_proto
== IPPROTO_TCP
)
4936 bpf_error("port '%s' is tcp", name
);
4937 else if (real_proto
== IPPROTO_SCTP
)
4938 bpf_error("port '%s' is sctp", name
);
4940 /* override PROTO_UNDEF */
4941 real_proto
= IPPROTO_UDP
;
4943 if (proto
== Q_TCP
) {
4944 if (real_proto
== IPPROTO_UDP
)
4945 bpf_error("port '%s' is udp", name
);
4947 else if (real_proto
== IPPROTO_SCTP
)
4948 bpf_error("port '%s' is sctp", name
);
4950 /* override PROTO_UNDEF */
4951 real_proto
= IPPROTO_TCP
;
4953 if (proto
== Q_SCTP
) {
4954 if (real_proto
== IPPROTO_UDP
)
4955 bpf_error("port '%s' is udp", name
);
4957 else if (real_proto
== IPPROTO_TCP
)
4958 bpf_error("port '%s' is tcp", name
);
4960 /* override PROTO_UNDEF */
4961 real_proto
= IPPROTO_SCTP
;
4964 return gen_port(port
, real_proto
, dir
);
4968 b
= gen_port(port
, real_proto
, dir
);
4969 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4975 if (proto
!= Q_DEFAULT
&&
4976 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4977 bpf_error("illegal qualifier of 'portrange'");
4978 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4979 bpf_error("unknown port in range '%s'", name
);
4980 if (proto
== Q_UDP
) {
4981 if (real_proto
== IPPROTO_TCP
)
4982 bpf_error("port in range '%s' is tcp", name
);
4983 else if (real_proto
== IPPROTO_SCTP
)
4984 bpf_error("port in range '%s' is sctp", name
);
4986 /* override PROTO_UNDEF */
4987 real_proto
= IPPROTO_UDP
;
4989 if (proto
== Q_TCP
) {
4990 if (real_proto
== IPPROTO_UDP
)
4991 bpf_error("port in range '%s' is udp", name
);
4992 else if (real_proto
== IPPROTO_SCTP
)
4993 bpf_error("port in range '%s' is sctp", name
);
4995 /* override PROTO_UNDEF */
4996 real_proto
= IPPROTO_TCP
;
4998 if (proto
== Q_SCTP
) {
4999 if (real_proto
== IPPROTO_UDP
)
5000 bpf_error("port in range '%s' is udp", name
);
5001 else if (real_proto
== IPPROTO_TCP
)
5002 bpf_error("port in range '%s' is tcp", name
);
5004 /* override PROTO_UNDEF */
5005 real_proto
= IPPROTO_SCTP
;
5008 return gen_portrange(port1
, port2
, real_proto
, dir
);
5012 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5013 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5020 eaddr
= pcap_ether_hostton(name
);
5022 bpf_error("unknown ether host: %s", name
);
5024 alist
= pcap_nametoaddr(name
);
5025 if (alist
== NULL
|| *alist
== NULL
)
5026 bpf_error("unknown host '%s'", name
);
5027 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5031 bpf_error("'gateway' not supported in this configuration");
5035 real_proto
= lookup_proto(name
, proto
);
5036 if (real_proto
>= 0)
5037 return gen_proto(real_proto
, proto
, dir
);
5039 bpf_error("unknown protocol: %s", name
);
5042 real_proto
= lookup_proto(name
, proto
);
5043 if (real_proto
>= 0)
5044 return gen_protochain(real_proto
, proto
, dir
);
5046 bpf_error("unknown protocol: %s", name
);
5058 gen_mcode(s1
, s2
, masklen
, q
)
5059 register const char *s1
, *s2
;
5060 register int masklen
;
5063 register int nlen
, mlen
;
5066 nlen
= __pcap_atoin(s1
, &n
);
5067 /* Promote short ipaddr */
5071 mlen
= __pcap_atoin(s2
, &m
);
5072 /* Promote short ipaddr */
5075 bpf_error("non-network bits set in \"%s mask %s\"",
5078 /* Convert mask len to mask */
5080 bpf_error("mask length must be <= 32");
5083 * X << 32 is not guaranteed by C to be 0; it's
5088 m
= 0xffffffff << (32 - masklen
);
5090 bpf_error("non-network bits set in \"%s/%d\"",
5097 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5100 bpf_error("Mask syntax for networks only");
5108 register const char *s
;
5113 int proto
= q
.proto
;
5119 else if (q
.proto
== Q_DECNET
)
5120 vlen
= __pcap_atodn(s
, &v
);
5122 vlen
= __pcap_atoin(s
, &v
);
5129 if (proto
== Q_DECNET
)
5130 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5131 else if (proto
== Q_LINK
) {
5132 bpf_error("illegal link layer address");
5135 if (s
== NULL
&& q
.addr
== Q_NET
) {
5136 /* Promote short net number */
5137 while (v
&& (v
& 0xff000000) == 0) {
5142 /* Promote short ipaddr */
5146 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5151 proto
= IPPROTO_UDP
;
5152 else if (proto
== Q_TCP
)
5153 proto
= IPPROTO_TCP
;
5154 else if (proto
== Q_SCTP
)
5155 proto
= IPPROTO_SCTP
;
5156 else if (proto
== Q_DEFAULT
)
5157 proto
= PROTO_UNDEF
;
5159 bpf_error("illegal qualifier of 'port'");
5162 return gen_port((int)v
, proto
, dir
);
5166 b
= gen_port((int)v
, proto
, dir
);
5167 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5174 proto
= IPPROTO_UDP
;
5175 else if (proto
== Q_TCP
)
5176 proto
= IPPROTO_TCP
;
5177 else if (proto
== Q_SCTP
)
5178 proto
= IPPROTO_SCTP
;
5179 else if (proto
== Q_DEFAULT
)
5180 proto
= PROTO_UNDEF
;
5182 bpf_error("illegal qualifier of 'portrange'");
5185 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5189 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5190 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5196 bpf_error("'gateway' requires a name");
5200 return gen_proto((int)v
, proto
, dir
);
5203 return gen_protochain((int)v
, proto
, dir
);
5218 gen_mcode6(s1
, s2
, masklen
, q
)
5219 register const char *s1
, *s2
;
5220 register int masklen
;
5223 struct addrinfo
*res
;
5224 struct in6_addr
*addr
;
5225 struct in6_addr mask
;
5230 bpf_error("no mask %s supported", s2
);
5232 res
= pcap_nametoaddrinfo(s1
);
5234 bpf_error("invalid ip6 address %s", s1
);
5236 bpf_error("%s resolved to multiple address", s1
);
5237 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5239 if (sizeof(mask
) * 8 < masklen
)
5240 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5241 memset(&mask
, 0, sizeof(mask
));
5242 memset(&mask
, 0xff, masklen
/ 8);
5244 mask
.s6_addr
[masklen
/ 8] =
5245 (0xff << (8 - masklen
% 8)) & 0xff;
5248 a
= (u_int32_t
*)addr
;
5249 m
= (u_int32_t
*)&mask
;
5250 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5251 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5252 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5260 bpf_error("Mask syntax for networks only");
5264 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5269 bpf_error("invalid qualifier against IPv6 address");
5277 register const u_char
*eaddr
;
5280 struct block
*b
, *tmp
;
5282 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5285 return gen_ehostop(eaddr
, (int)q
.dir
);
5287 return gen_fhostop(eaddr
, (int)q
.dir
);
5289 return gen_thostop(eaddr
, (int)q
.dir
);
5290 case DLT_IEEE802_11
:
5291 case DLT_IEEE802_11_RADIO_AVS
:
5292 case DLT_IEEE802_11_RADIO
:
5293 case DLT_PRISM_HEADER
:
5294 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5298 * Check that the packet doesn't begin with an
5299 * LE Control marker. (We've already generated
5302 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5307 * Now check the MAC address.
5309 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5314 case DLT_IP_OVER_FC
:
5315 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5317 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5321 bpf_error("ethernet address used in non-ether expression");
5327 struct slist
*s0
, *s1
;
5330 * This is definitely not the best way to do this, but the
5331 * lists will rarely get long.
5338 static struct slist
*
5344 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5349 static struct slist
*
5355 s
= new_stmt(BPF_LD
|BPF_MEM
);
5361 * Modify "index" to use the value stored into its register as an
5362 * offset relative to the beginning of the header for the protocol
5363 * "proto", and allocate a register and put an item "size" bytes long
5364 * (1, 2, or 4) at that offset into that register, making it the register
5368 gen_load(proto
, index
, size
)
5373 struct slist
*s
, *tmp
;
5375 int regno
= alloc_reg();
5377 free_reg(index
->regno
);
5381 bpf_error("data size must be 1, 2, or 4");
5397 bpf_error("unsupported index operation");
5401 * The offset is relative to the beginning of the packet
5402 * data, if we have a radio header. (If we don't, this
5405 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5406 linktype
!= DLT_IEEE802_11_RADIO
&&
5407 linktype
!= DLT_PRISM_HEADER
)
5408 bpf_error("radio information not present in capture");
5411 * Load into the X register the offset computed into the
5412 * register specifed by "index".
5414 s
= xfer_to_x(index
);
5417 * Load the item at that offset.
5419 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5421 sappend(index
->s
, s
);
5426 * The offset is relative to the beginning of
5427 * the link-layer header.
5429 * XXX - what about ATM LANE? Should the index be
5430 * relative to the beginning of the AAL5 frame, so
5431 * that 0 refers to the beginning of the LE Control
5432 * field, or relative to the beginning of the LAN
5433 * frame, so that 0 refers, for Ethernet LANE, to
5434 * the beginning of the destination address?
5436 s
= gen_llprefixlen();
5439 * If "s" is non-null, it has code to arrange that the
5440 * X register contains the length of the prefix preceding
5441 * the link-layer header. Add to it the offset computed
5442 * into the register specified by "index", and move that
5443 * into the X register. Otherwise, just load into the X
5444 * register the offset computed into the register specifed
5448 sappend(s
, xfer_to_a(index
));
5449 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5450 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5452 s
= xfer_to_x(index
);
5455 * Load the item at the sum of the offset we've put in the
5456 * X register and the offset of the start of the link
5457 * layer header (which is 0 if the radio header is
5458 * variable-length; that header length is what we put
5459 * into the X register and then added to the index).
5461 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5464 sappend(index
->s
, s
);
5480 * The offset is relative to the beginning of
5481 * the network-layer header.
5482 * XXX - are there any cases where we want
5485 s
= gen_llprefixlen();
5488 * If "s" is non-null, it has code to arrange that the
5489 * X register contains the length of the prefix preceding
5490 * the link-layer header. Add to it the offset computed
5491 * into the register specified by "index", and move that
5492 * into the X register. Otherwise, just load into the X
5493 * register the offset computed into the register specifed
5497 sappend(s
, xfer_to_a(index
));
5498 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5499 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5501 s
= xfer_to_x(index
);
5504 * Load the item at the sum of the offset we've put in the
5505 * X register, the offset of the start of the network
5506 * layer header, and the offset of the start of the link
5507 * layer header (which is 0 if the radio header is
5508 * variable-length; that header length is what we put
5509 * into the X register and then added to the index).
5511 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5512 tmp
->s
.k
= off_ll
+ off_nl
;
5514 sappend(index
->s
, s
);
5517 * Do the computation only if the packet contains
5518 * the protocol in question.
5520 b
= gen_proto_abbrev(proto
);
5522 gen_and(index
->b
, b
);
5535 * The offset is relative to the beginning of
5536 * the transport-layer header.
5538 * Load the X register with the length of the IPv4 header
5539 * (plus the offset of the link-layer header, if it's
5540 * a variable-length header), in bytes.
5542 * XXX - are there any cases where we want
5544 * XXX - we should, if we're built with
5545 * IPv6 support, generate code to load either
5546 * IPv4, IPv6, or both, as appropriate.
5548 s
= gen_loadx_iphdrlen();
5551 * The X register now contains the sum of the length
5552 * of any variable-length header preceding the link-layer
5553 * header and the length of the network-layer header.
5554 * Load into the A register the offset relative to
5555 * the beginning of the transport layer header,
5556 * add the X register to that, move that to the
5557 * X register, and load with an offset from the
5558 * X register equal to the offset of the network
5559 * layer header relative to the beginning of
5560 * the link-layer header plus the length of any
5561 * fixed-length header preceding the link-layer
5564 sappend(s
, xfer_to_a(index
));
5565 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5566 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5567 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5568 tmp
->s
.k
= off_ll
+ off_nl
;
5569 sappend(index
->s
, s
);
5572 * Do the computation only if the packet contains
5573 * the protocol in question - which is true only
5574 * if this is an IP datagram and is the first or
5575 * only fragment of that datagram.
5577 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5579 gen_and(index
->b
, b
);
5581 gen_and(gen_proto_abbrev(Q_IP
), b
);
5587 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5591 index
->regno
= regno
;
5592 s
= new_stmt(BPF_ST
);
5594 sappend(index
->s
, s
);
5600 gen_relation(code
, a0
, a1
, reversed
)
5602 struct arth
*a0
, *a1
;
5605 struct slist
*s0
, *s1
, *s2
;
5606 struct block
*b
, *tmp
;
5610 if (code
== BPF_JEQ
) {
5611 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5612 b
= new_block(JMP(code
));
5616 b
= new_block(BPF_JMP
|code
|BPF_X
);
5622 sappend(a0
->s
, a1
->s
);
5626 free_reg(a0
->regno
);
5627 free_reg(a1
->regno
);
5629 /* 'and' together protocol checks */
5632 gen_and(a0
->b
, tmp
= a1
->b
);
5648 int regno
= alloc_reg();
5649 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5652 s
= new_stmt(BPF_LD
|BPF_LEN
);
5653 s
->next
= new_stmt(BPF_ST
);
5654 s
->next
->s
.k
= regno
;
5669 a
= (struct arth
*)newchunk(sizeof(*a
));
5673 s
= new_stmt(BPF_LD
|BPF_IMM
);
5675 s
->next
= new_stmt(BPF_ST
);
5691 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5694 s
= new_stmt(BPF_ST
);
5702 gen_arth(code
, a0
, a1
)
5704 struct arth
*a0
, *a1
;
5706 struct slist
*s0
, *s1
, *s2
;
5710 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5715 sappend(a0
->s
, a1
->s
);
5717 free_reg(a0
->regno
);
5718 free_reg(a1
->regno
);
5720 s0
= new_stmt(BPF_ST
);
5721 a0
->regno
= s0
->s
.k
= alloc_reg();
5728 * Here we handle simple allocation of the scratch registers.
5729 * If too many registers are alloc'd, the allocator punts.
5731 static int regused
[BPF_MEMWORDS
];
5735 * Return the next free register.
5740 int n
= BPF_MEMWORDS
;
5743 if (regused
[curreg
])
5744 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5746 regused
[curreg
] = 1;
5750 bpf_error("too many registers needed to evaluate expression");
5755 * Return a register to the table so it can
5765 static struct block
*
5772 s
= new_stmt(BPF_LD
|BPF_LEN
);
5773 b
= new_block(JMP(jmp
));
5784 return gen_len(BPF_JGE
, n
);
5788 * Actually, this is less than or equal.
5796 b
= gen_len(BPF_JGT
, n
);
5803 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5804 * the beginning of the link-layer header.
5805 * XXX - that means you can't test values in the radiotap header, but
5806 * as that header is difficult if not impossible to parse generally
5807 * without a loop, that might not be a severe problem. A new keyword
5808 * "radio" could be added for that, although what you'd really want
5809 * would be a way of testing particular radio header values, which
5810 * would generate code appropriate to the radio header in question.
5813 gen_byteop(op
, idx
, val
)
5824 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5827 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5831 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5835 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5839 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5843 b
= new_block(JMP(BPF_JEQ
));
5850 static u_char abroadcast
[] = { 0x0 };
5853 gen_broadcast(proto
)
5856 bpf_u_int32 hostmask
;
5857 struct block
*b0
, *b1
, *b2
;
5858 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5866 case DLT_ARCNET_LINUX
:
5867 return gen_ahostop(abroadcast
, Q_DST
);
5869 return gen_ehostop(ebroadcast
, Q_DST
);
5871 return gen_fhostop(ebroadcast
, Q_DST
);
5873 return gen_thostop(ebroadcast
, Q_DST
);
5874 case DLT_IEEE802_11
:
5875 case DLT_IEEE802_11_RADIO_AVS
:
5876 case DLT_IEEE802_11_RADIO
:
5877 case DLT_PRISM_HEADER
:
5878 return gen_wlanhostop(ebroadcast
, Q_DST
);
5879 case DLT_IP_OVER_FC
:
5880 return gen_ipfchostop(ebroadcast
, Q_DST
);
5884 * Check that the packet doesn't begin with an
5885 * LE Control marker. (We've already generated
5888 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5893 * Now check the MAC address.
5895 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5901 bpf_error("not a broadcast link");
5906 b0
= gen_linktype(ETHERTYPE_IP
);
5907 hostmask
= ~netmask
;
5908 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5909 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5910 (bpf_int32
)(~0 & hostmask
), hostmask
);
5915 bpf_error("only link-layer/IP broadcast filters supported");
5920 * Generate code to test the low-order bit of a MAC address (that's
5921 * the bottom bit of the *first* byte).
5923 static struct block
*
5924 gen_mac_multicast(offset
)
5927 register struct block
*b0
;
5928 register struct slist
*s
;
5930 /* link[offset] & 1 != 0 */
5931 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5932 b0
= new_block(JMP(BPF_JSET
));
5939 gen_multicast(proto
)
5942 register struct block
*b0
, *b1
, *b2
;
5943 register struct slist
*s
;
5951 case DLT_ARCNET_LINUX
:
5952 /* all ARCnet multicasts use the same address */
5953 return gen_ahostop(abroadcast
, Q_DST
);
5955 /* ether[0] & 1 != 0 */
5956 return gen_mac_multicast(0);
5959 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5961 * XXX - was that referring to bit-order issues?
5963 /* fddi[1] & 1 != 0 */
5964 return gen_mac_multicast(1);
5966 /* tr[2] & 1 != 0 */
5967 return gen_mac_multicast(2);
5968 case DLT_IEEE802_11
:
5969 case DLT_IEEE802_11_RADIO_AVS
:
5970 case DLT_IEEE802_11_RADIO
:
5971 case DLT_PRISM_HEADER
:
5975 * For control frames, there is no DA.
5977 * For management frames, DA is at an
5978 * offset of 4 from the beginning of
5981 * For data frames, DA is at an offset
5982 * of 4 from the beginning of the packet
5983 * if To DS is clear and at an offset of
5984 * 16 from the beginning of the packet
5989 * Generate the tests to be done for data frames.
5991 * First, check for To DS set, i.e. "link[1] & 0x01".
5993 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5994 b1
= new_block(JMP(BPF_JSET
));
5995 b1
->s
.k
= 0x01; /* To DS */
5999 * If To DS is set, the DA is at 16.
6001 b0
= gen_mac_multicast(16);
6005 * Now, check for To DS not set, i.e. check
6006 * "!(link[1] & 0x01)".
6008 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6009 b2
= new_block(JMP(BPF_JSET
));
6010 b2
->s
.k
= 0x01; /* To DS */
6015 * If To DS is not set, the DA is at 4.
6017 b1
= gen_mac_multicast(4);
6021 * Now OR together the last two checks. That gives
6022 * the complete set of checks for data frames.
6027 * Now check for a data frame.
6028 * I.e, check "link[0] & 0x08".
6030 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6031 b1
= new_block(JMP(BPF_JSET
));
6036 * AND that with the checks done for data frames.
6041 * If the high-order bit of the type value is 0, this
6042 * is a management frame.
6043 * I.e, check "!(link[0] & 0x08)".
6045 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6046 b2
= new_block(JMP(BPF_JSET
));
6052 * For management frames, the DA is at 4.
6054 b1
= gen_mac_multicast(4);
6058 * OR that with the checks done for data frames.
6059 * That gives the checks done for management and
6065 * If the low-order bit of the type value is 1,
6066 * this is either a control frame or a frame
6067 * with a reserved type, and thus not a
6070 * I.e., check "!(link[0] & 0x04)".
6072 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6073 b1
= new_block(JMP(BPF_JSET
));
6079 * AND that with the checks for data and management
6084 case DLT_IP_OVER_FC
:
6085 b0
= gen_mac_multicast(2);
6090 * Check that the packet doesn't begin with an
6091 * LE Control marker. (We've already generated
6094 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6098 /* ether[off_mac] & 1 != 0 */
6099 b0
= gen_mac_multicast(off_mac
);
6107 /* Link not known to support multicasts */
6111 b0
= gen_linktype(ETHERTYPE_IP
);
6112 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6118 b0
= gen_linktype(ETHERTYPE_IPV6
);
6119 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6124 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6129 * generate command for inbound/outbound. It's here so we can
6130 * make it link-type specific. 'dir' = 0 implies "inbound",
6131 * = 1 implies "outbound".
6137 register struct block
*b0
;
6140 * Only some data link types support inbound/outbound qualifiers.
6144 b0
= gen_relation(BPF_JEQ
,
6145 gen_load(Q_LINK
, gen_loadi(0), 1),
6153 * Match packets sent by this machine.
6155 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6158 * Match packets sent to this machine.
6159 * (No broadcast or multicast packets, or
6160 * packets sent to some other machine and
6161 * received promiscuously.)
6163 * XXX - packets sent to other machines probably
6164 * shouldn't be matched, but what about broadcast
6165 * or multicast packets we received?
6167 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6172 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6173 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6178 /* match outgoing packets */
6179 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6181 /* match incoming packets */
6182 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6186 case DLT_JUNIPER_MFR
:
6187 case DLT_JUNIPER_MLFR
:
6188 case DLT_JUNIPER_MLPPP
:
6189 case DLT_JUNIPER_ATM1
:
6190 case DLT_JUNIPER_ATM2
:
6191 case DLT_JUNIPER_PPPOE
:
6192 case DLT_JUNIPER_PPPOE_ATM
:
6193 case DLT_JUNIPER_GGSN
:
6194 case DLT_JUNIPER_ES
:
6195 case DLT_JUNIPER_MONITOR
:
6196 case DLT_JUNIPER_SERVICES
:
6197 case DLT_JUNIPER_ETHER
:
6198 case DLT_JUNIPER_PPP
:
6199 case DLT_JUNIPER_FRELAY
:
6200 case DLT_JUNIPER_CHDLC
:
6201 case DLT_JUNIPER_VP
:
6202 /* juniper flags (including direction) are stored
6203 * the byte after the 3-byte magic number */
6205 /* match outgoing packets */
6206 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6208 /* match incoming packets */
6209 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6214 bpf_error("inbound/outbound not supported on linktype %d",
6222 /* PF firewall log matched interface */
6224 gen_pf_ifname(const char *ifname
)
6229 if (linktype
== DLT_PFLOG
) {
6230 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6231 off
= offsetof(struct pfloghdr
, ifname
);
6233 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6236 if (strlen(ifname
) >= len
) {
6237 bpf_error("ifname interface names can only be %d characters",
6241 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6245 /* PF firewall log ruleset name */
6247 gen_pf_ruleset(char *ruleset
)
6251 if (linktype
!= DLT_PFLOG
) {
6252 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6255 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6256 bpf_error("ruleset names can only be %ld characters",
6257 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6260 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6261 strlen(ruleset
), (const u_char
*)ruleset
);
6265 /* PF firewall log rule number */
6271 if (linktype
== DLT_PFLOG
) {
6272 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6275 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6282 /* PF firewall log sub-rule number */
6284 gen_pf_srnr(int srnr
)
6288 if (linktype
!= DLT_PFLOG
) {
6289 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6293 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6298 /* PF firewall log reason code */
6300 gen_pf_reason(int reason
)
6304 if (linktype
== DLT_PFLOG
) {
6305 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6308 bpf_error("reason not supported on linktype 0x%x", linktype
);
6315 /* PF firewall log action */
6317 gen_pf_action(int action
)
6321 if (linktype
== DLT_PFLOG
) {
6322 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6325 bpf_error("action not supported on linktype 0x%x", linktype
);
6334 register const u_char
*eaddr
;
6337 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6338 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6339 return gen_ahostop(eaddr
, (int)q
.dir
);
6341 bpf_error("ARCnet address used in non-arc expression");
6345 static struct block
*
6346 gen_ahostop(eaddr
, dir
)
6347 register const u_char
*eaddr
;
6350 register struct block
*b0
, *b1
;
6353 /* src comes first, different from Ethernet */
6355 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6358 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6361 b0
= gen_ahostop(eaddr
, Q_SRC
);
6362 b1
= gen_ahostop(eaddr
, Q_DST
);
6368 b0
= gen_ahostop(eaddr
, Q_SRC
);
6369 b1
= gen_ahostop(eaddr
, Q_DST
);
6378 * support IEEE 802.1Q VLAN trunk over ethernet
6384 struct block
*b0
, *b1
;
6386 /* can't check for VLAN-encapsulated packets inside MPLS */
6387 if (label_stack_depth
> 0)
6388 bpf_error("no VLAN match after MPLS");
6391 * Change the offsets to point to the type and data fields within
6392 * the VLAN packet. Just increment the offsets, so that we
6393 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6394 * capture VLAN 200 encapsulated within VLAN 100.
6396 * XXX - this is a bit of a kludge. If we were to split the
6397 * compiler into a parser that parses an expression and
6398 * generates an expression tree, and a code generator that
6399 * takes an expression tree (which could come from our
6400 * parser or from some other parser) and generates BPF code,
6401 * we could perhaps make the offsets parameters of routines
6402 * and, in the handler for an "AND" node, pass to subnodes
6403 * other than the VLAN node the adjusted offsets.
6405 * This would mean that "vlan" would, instead of changing the
6406 * behavior of *all* tests after it, change only the behavior
6407 * of tests ANDed with it. That would change the documented
6408 * semantics of "vlan", which might break some expressions.
6409 * However, it would mean that "(vlan and ip) or ip" would check
6410 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6411 * checking only for VLAN-encapsulated IP, so that could still
6412 * be considered worth doing; it wouldn't break expressions
6413 * that are of the form "vlan and ..." or "vlan N and ...",
6414 * which I suspect are the most common expressions involving
6415 * "vlan". "vlan or ..." doesn't necessarily do what the user
6416 * would really want, now, as all the "or ..." tests would
6417 * be done assuming a VLAN, even though the "or" could be viewed
6418 * as meaning "or, if this isn't a VLAN packet...".
6420 orig_linktype
= off_linktype
; /* save original values */
6432 bpf_error("no VLAN support for data link type %d",
6437 /* check for VLAN */
6438 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6440 /* If a specific VLAN is requested, check VLAN id */
6441 if (vlan_num
>= 0) {
6442 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6458 struct block
*b0
,*b1
;
6461 * Change the offsets to point to the type and data fields within
6462 * the MPLS packet. Just increment the offsets, so that we
6463 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6464 * capture packets with an outer label of 100000 and an inner
6467 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6471 if (label_stack_depth
> 0) {
6472 /* just match the bottom-of-stack bit clear */
6473 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6476 * Indicate that we're checking MPLS-encapsulated headers,
6477 * to make sure higher level code generators don't try to
6478 * match against IP-related protocols such as Q_ARP, Q_RARP
6483 case DLT_C_HDLC
: /* fall through */
6485 b0
= gen_linktype(ETHERTYPE_MPLS
);
6489 b0
= gen_linktype(PPP_MPLS_UCAST
);
6492 /* FIXME add other DLT_s ...
6493 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6494 * leave it for now */
6497 bpf_error("no MPLS support for data link type %d",
6505 /* If a specific MPLS label is requested, check it */
6506 if (label_num
>= 0) {
6507 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6508 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6509 0xfffff000); /* only compare the first 20 bits */
6516 label_stack_depth
++;
6521 * Support PPPOE discovery and session.
6526 /* check for PPPoE discovery */
6527 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6536 * Test against the PPPoE session link-layer type.
6538 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6541 * Change the offsets to point to the type and data fields within
6544 * XXX - this is a bit of a kludge. If we were to split the
6545 * compiler into a parser that parses an expression and
6546 * generates an expression tree, and a code generator that
6547 * takes an expression tree (which could come from our
6548 * parser or from some other parser) and generates BPF code,
6549 * we could perhaps make the offsets parameters of routines
6550 * and, in the handler for an "AND" node, pass to subnodes
6551 * other than the PPPoE node the adjusted offsets.
6553 * This would mean that "pppoes" would, instead of changing the
6554 * behavior of *all* tests after it, change only the behavior
6555 * of tests ANDed with it. That would change the documented
6556 * semantics of "pppoes", which might break some expressions.
6557 * However, it would mean that "(pppoes and ip) or ip" would check
6558 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6559 * checking only for VLAN-encapsulated IP, so that could still
6560 * be considered worth doing; it wouldn't break expressions
6561 * that are of the form "pppoes and ..." which I suspect are the
6562 * most common expressions involving "pppoes". "pppoes or ..."
6563 * doesn't necessarily do what the user would really want, now,
6564 * as all the "or ..." tests would be done assuming PPPoE, even
6565 * though the "or" could be viewed as meaning "or, if this isn't
6566 * a PPPoE packet...".
6568 orig_linktype
= off_linktype
; /* save original values */
6572 * The "network-layer" protocol is PPPoE, which has a 6-byte
6573 * PPPoE header, followed by PPP payload, so we set the
6574 * offsets to the network layer offset plus 6 bytes for
6575 * the PPPoE header plus the values appropriate for PPP when
6576 * encapsulated in Ethernet (which means there's no HDLC
6579 off_linktype
= orig_nl
+ 6;
6580 off_nl
= orig_nl
+ 6 + 2;
6581 off_nl_nosnap
= orig_nl
+ 6 + 2;
6584 * Set the link-layer type to PPP, as all subsequent tests will
6585 * be on the encapsulated PPP header.
6593 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6605 bpf_error("'vpi' supported only on raw ATM");
6606 if (off_vpi
== (u_int
)-1)
6608 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6614 bpf_error("'vci' supported only on raw ATM");
6615 if (off_vci
== (u_int
)-1)
6617 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6622 if (off_proto
== (u_int
)-1)
6623 abort(); /* XXX - this isn't on FreeBSD */
6624 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6629 if (off_payload
== (u_int
)-1)
6631 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6632 0xffffffff, jtype
, reverse
, jvalue
);
6637 bpf_error("'callref' supported only on raw ATM");
6638 if (off_proto
== (u_int
)-1)
6640 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6641 jtype
, reverse
, jvalue
);
6651 gen_atmtype_abbrev(type
)
6654 struct block
*b0
, *b1
;
6659 /* Get all packets in Meta signalling Circuit */
6661 bpf_error("'metac' supported only on raw ATM");
6662 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6663 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6668 /* Get all packets in Broadcast Circuit*/
6670 bpf_error("'bcc' supported only on raw ATM");
6671 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6672 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6677 /* Get all cells in Segment OAM F4 circuit*/
6679 bpf_error("'oam4sc' supported only on raw ATM");
6680 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6681 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6686 /* Get all cells in End-to-End OAM F4 Circuit*/
6688 bpf_error("'oam4ec' supported only on raw ATM");
6689 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6690 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6695 /* Get all packets in connection Signalling Circuit */
6697 bpf_error("'sc' supported only on raw ATM");
6698 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6699 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6704 /* Get all packets in ILMI Circuit */
6706 bpf_error("'ilmic' supported only on raw ATM");
6707 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6708 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6713 /* Get all LANE packets */
6715 bpf_error("'lane' supported only on raw ATM");
6716 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6719 * Arrange that all subsequent tests assume LANE
6720 * rather than LLC-encapsulated packets, and set
6721 * the offsets appropriately for LANE-encapsulated
6724 * "off_mac" is the offset of the Ethernet header,
6725 * which is 2 bytes past the ATM pseudo-header
6726 * (skipping the pseudo-header and 2-byte LE Client
6727 * field). The other offsets are Ethernet offsets
6728 * relative to "off_mac".
6731 off_mac
= off_payload
+ 2; /* MAC header */
6732 off_linktype
= off_mac
+ 12;
6733 off_nl
= off_mac
+ 14; /* Ethernet II */
6734 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6738 /* Get all LLC-encapsulated packets */
6740 bpf_error("'llc' supported only on raw ATM");
6741 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6752 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6759 bpf_u_int32 val1
, val2
, val3
;
6761 switch (mtp3field
) {
6764 if (off_sio
== (u_int
)-1)
6765 bpf_error("'sio' supported only on SS7");
6766 /* sio coded on 1 byte so max value 255 */
6768 bpf_error("sio value %u too big; max value = 255",
6770 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6771 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6775 if (off_opc
== (u_int
)-1)
6776 bpf_error("'opc' supported only on SS7");
6777 /* opc coded on 14 bits so max value 16383 */
6779 bpf_error("opc value %u too big; max value = 16383",
6781 /* the following instructions are made to convert jvalue
6782 * to the form used to write opc in an ss7 message*/
6783 val1
= jvalue
& 0x00003c00;
6785 val2
= jvalue
& 0x000003fc;
6787 val3
= jvalue
& 0x00000003;
6789 jvalue
= val1
+ val2
+ val3
;
6790 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6791 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6795 if (off_dpc
== (u_int
)-1)
6796 bpf_error("'dpc' supported only on SS7");
6797 /* dpc coded on 14 bits so max value 16383 */
6799 bpf_error("dpc value %u too big; max value = 16383",
6801 /* the following instructions are made to convert jvalue
6802 * to the forme used to write dpc in an ss7 message*/
6803 val1
= jvalue
& 0x000000ff;
6805 val2
= jvalue
& 0x00003f00;
6807 jvalue
= val1
+ val2
;
6808 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6809 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6813 if (off_sls
== (u_int
)-1)
6814 bpf_error("'sls' supported only on SS7");
6815 /* sls coded on 4 bits so max value 15 */
6817 bpf_error("sls value %u too big; max value = 15",
6819 /* the following instruction is made to convert jvalue
6820 * to the forme used to write sls in an ss7 message*/
6821 jvalue
= jvalue
<< 4;
6822 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6823 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6832 static struct block
*
6833 gen_msg_abbrev(type
)
6839 * Q.2931 signalling protocol messages for handling virtual circuits
6840 * establishment and teardown
6845 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6849 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6853 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6857 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6861 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6864 case A_RELEASE_DONE
:
6865 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6875 gen_atmmulti_abbrev(type
)
6878 struct block
*b0
, *b1
;
6884 bpf_error("'oam' supported only on raw ATM");
6885 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6890 bpf_error("'oamf4' supported only on raw ATM");
6892 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6893 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6895 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6901 * Get Q.2931 signalling messages for switched
6902 * virtual connection
6905 bpf_error("'connectmsg' supported only on raw ATM");
6906 b0
= gen_msg_abbrev(A_SETUP
);
6907 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6909 b0
= gen_msg_abbrev(A_CONNECT
);
6911 b0
= gen_msg_abbrev(A_CONNECTACK
);
6913 b0
= gen_msg_abbrev(A_RELEASE
);
6915 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6917 b0
= gen_atmtype_abbrev(A_SC
);
6923 bpf_error("'metaconnect' supported only on raw ATM");
6924 b0
= gen_msg_abbrev(A_SETUP
);
6925 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6927 b0
= gen_msg_abbrev(A_CONNECT
);
6929 b0
= gen_msg_abbrev(A_RELEASE
);
6931 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6933 b0
= gen_atmtype_abbrev(A_METAC
);