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.41 2006-09-13 07:02:15 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 */
1231 case DLT_LINUX_LAPD
:
1233 * Currently, only raw "link[N:M]" filtering is supported.
1240 bpf_error("unknown data link type %d", linktype
);
1245 * Load a value relative to the beginning of the link-layer header.
1246 * The link-layer header doesn't necessarily begin at the beginning
1247 * of the packet data; there might be a variable-length prefix containing
1248 * radio information.
1250 static struct slist
*
1251 gen_load_llrel(offset
, size
)
1254 struct slist
*s
, *s2
;
1256 s
= gen_llprefixlen();
1259 * If "s" is non-null, it has code to arrange that the X register
1260 * contains the length of the prefix preceding the link-layer
1263 * Otherwise, the length of the prefix preceding the link-layer
1264 * header is "off_ll".
1267 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1271 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1272 s
->s
.k
= offset
+ off_ll
;
1278 * Load a value relative to the beginning of the specified header.
1280 static struct slist
*
1281 gen_load_a(offrel
, offset
, size
)
1282 enum e_offrel offrel
;
1285 struct slist
*s
, *s2
;
1290 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1295 s
= gen_load_llrel(offset
, size
);
1299 s
= gen_load_llrel(off_nl
+ offset
, size
);
1303 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1308 * Load the X register with the length of the IPv4 header,
1311 s
= gen_loadx_iphdrlen();
1314 * Load the item at {length of the link-layer header} +
1315 * {length of the IPv4 header} + {specified offset}.
1317 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1318 s2
->s
.k
= off_nl
+ offset
;
1323 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1334 * Generate code to load into the X register the sum of the length of
1335 * the IPv4 header and any variable-length header preceding the link-layer
1338 static struct slist
*
1339 gen_loadx_iphdrlen()
1341 struct slist
*s
, *s2
;
1343 s
= gen_llprefixlen();
1346 * There's a variable-length prefix preceding the
1347 * link-layer header. "s" points to a list of statements
1348 * that put the length of that prefix into the X register.
1349 * The 4*([k]&0xf) addressing mode can't be used, as we
1350 * don't have a constant offset, so we have to load the
1351 * value in question into the A register and add to it
1352 * the value from the X register.
1354 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1357 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1360 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1365 * The A register now contains the length of the
1366 * IP header. We need to add to it the length
1367 * of the prefix preceding the link-layer
1368 * header, which is still in the X register, and
1369 * move the result into the X register.
1371 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1372 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1375 * There is no variable-length header preceding the
1376 * link-layer header; if there's a fixed-length
1377 * header preceding it, its length is included in
1378 * the off_ variables, so it doesn't need to be added.
1380 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1386 static struct block
*
1393 s
= new_stmt(BPF_LD
|BPF_IMM
);
1395 b
= new_block(JMP(BPF_JEQ
));
1401 static inline struct block
*
1404 return gen_uncond(1);
1407 static inline struct block
*
1410 return gen_uncond(0);
1414 * Byte-swap a 32-bit number.
1415 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1416 * big-endian platforms.)
1418 #define SWAPLONG(y) \
1419 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1422 * Generate code to match a particular packet type.
1424 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1425 * value, if <= ETHERMTU. We use that to determine whether to
1426 * match the type/length field or to check the type/length field for
1427 * a value <= ETHERMTU to see whether it's a type field and then do
1428 * the appropriate test.
1430 static struct block
*
1431 gen_ether_linktype(proto
)
1434 struct block
*b0
, *b1
;
1440 case LLCSAP_NETBEUI
:
1442 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1443 * so we check the DSAP and SSAP.
1445 * LLCSAP_IP checks for IP-over-802.2, rather
1446 * than IP-over-Ethernet or IP-over-SNAP.
1448 * XXX - should we check both the DSAP and the
1449 * SSAP, like this, or should we check just the
1450 * DSAP, as we do for other types <= ETHERMTU
1451 * (i.e., other SAP values)?
1453 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1455 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1456 ((proto
<< 8) | proto
));
1464 * Ethernet_II frames, which are Ethernet
1465 * frames with a frame type of ETHERTYPE_IPX;
1467 * Ethernet_802.3 frames, which are 802.3
1468 * frames (i.e., the type/length field is
1469 * a length field, <= ETHERMTU, rather than
1470 * a type field) with the first two bytes
1471 * after the Ethernet/802.3 header being
1474 * Ethernet_802.2 frames, which are 802.3
1475 * frames with an 802.2 LLC header and
1476 * with the IPX LSAP as the DSAP in the LLC
1479 * Ethernet_SNAP frames, which are 802.3
1480 * frames with an LLC header and a SNAP
1481 * header and with an OUI of 0x000000
1482 * (encapsulated Ethernet) and a protocol
1483 * ID of ETHERTYPE_IPX in the SNAP header.
1485 * XXX - should we generate the same code both
1486 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1490 * This generates code to check both for the
1491 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1493 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1494 (bpf_int32
)LLCSAP_IPX
);
1495 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1500 * Now we add code to check for SNAP frames with
1501 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1503 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1507 * Now we generate code to check for 802.3
1508 * frames in general.
1510 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1514 * Now add the check for 802.3 frames before the
1515 * check for Ethernet_802.2 and Ethernet_802.3,
1516 * as those checks should only be done on 802.3
1517 * frames, not on Ethernet frames.
1522 * Now add the check for Ethernet_II frames, and
1523 * do that before checking for the other frame
1526 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1527 (bpf_int32
)ETHERTYPE_IPX
);
1531 case ETHERTYPE_ATALK
:
1532 case ETHERTYPE_AARP
:
1534 * EtherTalk (AppleTalk protocols on Ethernet link
1535 * layer) may use 802.2 encapsulation.
1539 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1540 * we check for an Ethernet type field less than
1541 * 1500, which means it's an 802.3 length field.
1543 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1547 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1548 * SNAP packets with an organization code of
1549 * 0x080007 (Apple, for Appletalk) and a protocol
1550 * type of ETHERTYPE_ATALK (Appletalk).
1552 * 802.2-encapsulated ETHERTYPE_AARP packets are
1553 * SNAP packets with an organization code of
1554 * 0x000000 (encapsulated Ethernet) and a protocol
1555 * type of ETHERTYPE_AARP (Appletalk ARP).
1557 if (proto
== ETHERTYPE_ATALK
)
1558 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1559 else /* proto == ETHERTYPE_AARP */
1560 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1564 * Check for Ethernet encapsulation (Ethertalk
1565 * phase 1?); we just check for the Ethernet
1568 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1574 if (proto
<= ETHERMTU
) {
1576 * This is an LLC SAP value, so the frames
1577 * that match would be 802.2 frames.
1578 * Check that the frame is an 802.2 frame
1579 * (i.e., that the length/type field is
1580 * a length field, <= ETHERMTU) and
1581 * then check the DSAP.
1583 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1585 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1591 * This is an Ethernet type, so compare
1592 * the length/type field with it (if
1593 * the frame is an 802.2 frame, the length
1594 * field will be <= ETHERMTU, and, as
1595 * "proto" is > ETHERMTU, this test
1596 * will fail and the frame won't match,
1597 * which is what we want).
1599 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1606 * Generate code to match a particular packet type.
1608 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1609 * value, if <= ETHERMTU. We use that to determine whether to
1610 * match the type field or to check the type field for the special
1611 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1613 static struct block
*
1614 gen_linux_sll_linktype(proto
)
1617 struct block
*b0
, *b1
;
1623 case LLCSAP_NETBEUI
:
1625 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1626 * so we check the DSAP and SSAP.
1628 * LLCSAP_IP checks for IP-over-802.2, rather
1629 * than IP-over-Ethernet or IP-over-SNAP.
1631 * XXX - should we check both the DSAP and the
1632 * SSAP, like this, or should we check just the
1633 * DSAP, as we do for other types <= ETHERMTU
1634 * (i.e., other SAP values)?
1636 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1637 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1638 ((proto
<< 8) | proto
));
1644 * Ethernet_II frames, which are Ethernet
1645 * frames with a frame type of ETHERTYPE_IPX;
1647 * Ethernet_802.3 frames, which have a frame
1648 * type of LINUX_SLL_P_802_3;
1650 * Ethernet_802.2 frames, which are 802.3
1651 * frames with an 802.2 LLC header (i.e, have
1652 * a frame type of LINUX_SLL_P_802_2) and
1653 * with the IPX LSAP as the DSAP in the LLC
1656 * Ethernet_SNAP frames, which are 802.3
1657 * frames with an LLC header and a SNAP
1658 * header and with an OUI of 0x000000
1659 * (encapsulated Ethernet) and a protocol
1660 * ID of ETHERTYPE_IPX in the SNAP header.
1662 * First, do the checks on LINUX_SLL_P_802_2
1663 * frames; generate the check for either
1664 * Ethernet_802.2 or Ethernet_SNAP frames, and
1665 * then put a check for LINUX_SLL_P_802_2 frames
1668 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1669 (bpf_int32
)LLCSAP_IPX
);
1670 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1673 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1677 * Now check for 802.3 frames and OR that with
1678 * the previous test.
1680 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1684 * Now add the check for Ethernet_II frames, and
1685 * do that before checking for the other frame
1688 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1689 (bpf_int32
)ETHERTYPE_IPX
);
1693 case ETHERTYPE_ATALK
:
1694 case ETHERTYPE_AARP
:
1696 * EtherTalk (AppleTalk protocols on Ethernet link
1697 * layer) may use 802.2 encapsulation.
1701 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1702 * we check for the 802.2 protocol type in the
1703 * "Ethernet type" field.
1705 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1708 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1709 * SNAP packets with an organization code of
1710 * 0x080007 (Apple, for Appletalk) and a protocol
1711 * type of ETHERTYPE_ATALK (Appletalk).
1713 * 802.2-encapsulated ETHERTYPE_AARP packets are
1714 * SNAP packets with an organization code of
1715 * 0x000000 (encapsulated Ethernet) and a protocol
1716 * type of ETHERTYPE_AARP (Appletalk ARP).
1718 if (proto
== ETHERTYPE_ATALK
)
1719 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1721 else /* proto == ETHERTYPE_AARP */
1722 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1727 * Check for Ethernet encapsulation (Ethertalk
1728 * phase 1?); we just check for the Ethernet
1731 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1737 if (proto
<= ETHERMTU
) {
1739 * This is an LLC SAP value, so the frames
1740 * that match would be 802.2 frames.
1741 * Check for the 802.2 protocol type
1742 * in the "Ethernet type" field, and
1743 * then check the DSAP.
1745 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1747 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1753 * This is an Ethernet type, so compare
1754 * the length/type field with it (if
1755 * the frame is an 802.2 frame, the length
1756 * field will be <= ETHERMTU, and, as
1757 * "proto" is > ETHERMTU, this test
1758 * will fail and the frame won't match,
1759 * which is what we want).
1761 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1768 insert_radiotap_load_llprefixlen(b
)
1771 struct slist
*s1
, *s2
;
1774 * Prepend to the statements in this block code to load the
1775 * length of the radiotap header into the register assigned
1776 * to hold that length, if one has been assigned.
1778 if (reg_ll_size
!= -1) {
1780 * The 2 bytes at offsets of 2 and 3 from the beginning
1781 * of the radiotap header are the length of the radiotap
1782 * header; unfortunately, it's little-endian, so we have
1783 * to load it a byte at a time and construct the value.
1787 * Load the high-order byte, at an offset of 3, shift it
1788 * left a byte, and put the result in the X register.
1790 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1792 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1795 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1799 * Load the next byte, at an offset of 2, and OR the
1800 * value from the X register into it.
1802 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1805 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1809 * Now allocate a register to hold that value and store
1812 s2
= new_stmt(BPF_ST
);
1813 s2
->s
.k
= reg_ll_size
;
1817 * Now move it into the X register.
1819 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1823 * Now append all the existing statements in this
1824 * block to these statements.
1826 sappend(s1
, b
->stmts
);
1833 insert_load_llprefixlen(b
)
1838 case DLT_IEEE802_11_RADIO
:
1839 insert_radiotap_load_llprefixlen(b
);
1844 static struct slist
*
1845 gen_radiotap_llprefixlen(void)
1849 if (reg_ll_size
== -1) {
1851 * We haven't yet assigned a register for the length
1852 * of the radiotap header; allocate one.
1854 reg_ll_size
= alloc_reg();
1858 * Load the register containing the radiotap length
1859 * into the X register.
1861 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1862 s
->s
.k
= reg_ll_size
;
1867 * Generate code to compute the link-layer header length, if necessary,
1868 * putting it into the X register, and to return either a pointer to a
1869 * "struct slist" for the list of statements in that code, or NULL if
1870 * no code is necessary.
1872 static struct slist
*
1873 gen_llprefixlen(void)
1877 case DLT_IEEE802_11_RADIO
:
1878 return gen_radiotap_llprefixlen();
1886 * Generate code to match a particular packet type by matching the
1887 * link-layer type field or fields in the 802.2 LLC header.
1889 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1890 * value, if <= ETHERMTU.
1892 static struct block
*
1896 struct block
*b0
, *b1
, *b2
;
1898 /* are we checking MPLS-encapsulated packets? */
1899 if (label_stack_depth
> 0) {
1903 /* FIXME add other L3 proto IDs */
1904 return gen_mpls_linktype(Q_IP
);
1906 case ETHERTYPE_IPV6
:
1908 /* FIXME add other L3 proto IDs */
1909 return gen_mpls_linktype(Q_IPV6
);
1912 bpf_error("unsupported protocol over mpls");
1920 return gen_ether_linktype(proto
);
1928 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1932 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1941 case DLT_IEEE802_11
:
1942 case DLT_IEEE802_11_RADIO_AVS
:
1943 case DLT_IEEE802_11_RADIO
:
1944 case DLT_PRISM_HEADER
:
1945 case DLT_ATM_RFC1483
:
1947 case DLT_IP_OVER_FC
:
1948 return gen_llc_linktype(proto
);
1954 * If "is_lane" is set, check for a LANE-encapsulated
1955 * version of this protocol, otherwise check for an
1956 * LLC-encapsulated version of this protocol.
1958 * We assume LANE means Ethernet, not Token Ring.
1962 * Check that the packet doesn't begin with an
1963 * LE Control marker. (We've already generated
1966 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1971 * Now generate an Ethernet test.
1973 b1
= gen_ether_linktype(proto
);
1978 * Check for LLC encapsulation and then check the
1981 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1982 b1
= gen_llc_linktype(proto
);
1990 return gen_linux_sll_linktype(proto
);
1995 case DLT_SLIP_BSDOS
:
1998 * These types don't provide any type field; packets
2001 * XXX - for IPv4, check for a version number of 4, and,
2002 * for IPv6, check for a version number of 6?
2008 case ETHERTYPE_IPV6
:
2010 return gen_true(); /* always true */
2013 return gen_false(); /* always false */
2020 case DLT_PPP_SERIAL
:
2023 * We use Ethernet protocol types inside libpcap;
2024 * map them to the corresponding PPP protocol types.
2033 case ETHERTYPE_IPV6
:
2042 case ETHERTYPE_ATALK
:
2056 * I'm assuming the "Bridging PDU"s that go
2057 * over PPP are Spanning Tree Protocol
2071 * We use Ethernet protocol types inside libpcap;
2072 * map them to the corresponding PPP protocol types.
2077 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2078 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2080 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2085 case ETHERTYPE_IPV6
:
2095 case ETHERTYPE_ATALK
:
2109 * I'm assuming the "Bridging PDU"s that go
2110 * over PPP are Spanning Tree Protocol
2126 * For DLT_NULL, the link-layer header is a 32-bit
2127 * word containing an AF_ value in *host* byte order,
2128 * and for DLT_ENC, the link-layer header begins
2129 * with a 32-bit work containing an AF_ value in
2132 * In addition, if we're reading a saved capture file,
2133 * the host byte order in the capture may not be the
2134 * same as the host byte order on this machine.
2136 * For DLT_LOOP, the link-layer header is a 32-bit
2137 * word containing an AF_ value in *network* byte order.
2139 * XXX - AF_ values may, unfortunately, be platform-
2140 * dependent; for example, FreeBSD's AF_INET6 is 24
2141 * whilst NetBSD's and OpenBSD's is 26.
2143 * This means that, when reading a capture file, just
2144 * checking for our AF_INET6 value won't work if the
2145 * capture file came from another OS.
2154 case ETHERTYPE_IPV6
:
2161 * Not a type on which we support filtering.
2162 * XXX - support those that have AF_ values
2163 * #defined on this platform, at least?
2168 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2170 * The AF_ value is in host byte order, but
2171 * the BPF interpreter will convert it to
2172 * network byte order.
2174 * If this is a save file, and it's from a
2175 * machine with the opposite byte order to
2176 * ours, we byte-swap the AF_ value.
2178 * Then we run it through "htonl()", and
2179 * generate code to compare against the result.
2181 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2182 bpf_pcap
->sf
.swapped
)
2183 proto
= SWAPLONG(proto
);
2184 proto
= htonl(proto
);
2186 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2190 * af field is host byte order in contrast to the rest of
2193 if (proto
== ETHERTYPE_IP
)
2194 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2195 BPF_B
, (bpf_int32
)AF_INET
));
2197 else if (proto
== ETHERTYPE_IPV6
)
2198 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2199 BPF_B
, (bpf_int32
)AF_INET6
));
2207 case DLT_ARCNET_LINUX
:
2209 * XXX should we check for first fragment if the protocol
2218 case ETHERTYPE_IPV6
:
2219 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2220 (bpf_int32
)ARCTYPE_INET6
));
2224 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2225 (bpf_int32
)ARCTYPE_IP
);
2226 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2227 (bpf_int32
)ARCTYPE_IP_OLD
);
2232 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2233 (bpf_int32
)ARCTYPE_ARP
);
2234 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2235 (bpf_int32
)ARCTYPE_ARP_OLD
);
2239 case ETHERTYPE_REVARP
:
2240 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2241 (bpf_int32
)ARCTYPE_REVARP
));
2243 case ETHERTYPE_ATALK
:
2244 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2245 (bpf_int32
)ARCTYPE_ATALK
));
2252 case ETHERTYPE_ATALK
:
2262 * XXX - assumes a 2-byte Frame Relay header with
2263 * DLCI and flags. What if the address is longer?
2269 * Check for the special NLPID for IP.
2271 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2274 case ETHERTYPE_IPV6
:
2276 * Check for the special NLPID for IPv6.
2278 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2283 * Check for several OSI protocols.
2285 * Frame Relay packets typically have an OSI
2286 * NLPID at the beginning; we check for each
2289 * What we check for is the NLPID and a frame
2290 * control field of UI, i.e. 0x03 followed
2293 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2294 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2295 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2306 case DLT_JUNIPER_MFR
:
2307 case DLT_JUNIPER_MLFR
:
2308 case DLT_JUNIPER_MLPPP
:
2309 case DLT_JUNIPER_ATM1
:
2310 case DLT_JUNIPER_ATM2
:
2311 case DLT_JUNIPER_PPPOE
:
2312 case DLT_JUNIPER_PPPOE_ATM
:
2313 case DLT_JUNIPER_GGSN
:
2314 case DLT_JUNIPER_ES
:
2315 case DLT_JUNIPER_MONITOR
:
2316 case DLT_JUNIPER_SERVICES
:
2317 case DLT_JUNIPER_ETHER
:
2318 case DLT_JUNIPER_PPP
:
2319 case DLT_JUNIPER_FRELAY
:
2320 case DLT_JUNIPER_CHDLC
:
2321 /* just lets verify the magic number for now -
2322 * on ATM we may have up to 6 different encapsulations on the wire
2323 * and need a lot of heuristics to figure out that the payload
2326 * FIXME encapsulation specific BPF_ filters
2328 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2330 case DLT_LINUX_IRDA
:
2331 bpf_error("IrDA link-layer type filtering not implemented");
2334 bpf_error("DOCSIS link-layer type filtering not implemented");
2336 case DLT_LINUX_LAPD
:
2337 bpf_error("LAPD link-layer type filtering not implemented");
2341 * All the types that have no encapsulation should either be
2342 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2343 * all packets are IP packets, or should be handled in some
2344 * special case, if none of them are (if some are and some
2345 * aren't, the lack of encapsulation is a problem, as we'd
2346 * have to find some other way of determining the packet type).
2348 * Therefore, if "off_linktype" is -1, there's an error.
2350 if (off_linktype
== (u_int
)-1)
2354 * Any type not handled above should always have an Ethernet
2355 * type at an offset of "off_linktype". (PPP is partially
2356 * handled above - the protocol type is mapped from the
2357 * Ethernet and LLC types we use internally to the corresponding
2358 * PPP type - but the PPP type is always specified by a value
2359 * at "off_linktype", so we don't have to do the code generation
2362 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2366 * Check for an LLC SNAP packet with a given organization code and
2367 * protocol type; we check the entire contents of the 802.2 LLC and
2368 * snap headers, checking for DSAP and SSAP of SNAP and a control
2369 * field of 0x03 in the LLC header, and for the specified organization
2370 * code and protocol type in the SNAP header.
2372 static struct block
*
2373 gen_snap(orgcode
, ptype
, offset
)
2374 bpf_u_int32 orgcode
;
2378 u_char snapblock
[8];
2380 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2381 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2382 snapblock
[2] = 0x03; /* control = UI */
2383 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2384 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2385 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2386 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2387 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2388 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2392 * Generate code to match a particular packet type, for link-layer types
2393 * using 802.2 LLC headers.
2395 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2396 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2398 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2399 * value, if <= ETHERMTU. We use that to determine whether to
2400 * match the DSAP or both DSAP and LSAP or to check the OUI and
2401 * protocol ID in a SNAP header.
2403 static struct block
*
2404 gen_llc_linktype(proto
)
2408 * XXX - handle token-ring variable-length header.
2414 case LLCSAP_NETBEUI
:
2416 * XXX - should we check both the DSAP and the
2417 * SSAP, like this, or should we check just the
2418 * DSAP, as we do for other types <= ETHERMTU
2419 * (i.e., other SAP values)?
2421 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2422 ((proto
<< 8) | proto
));
2426 * XXX - are there ever SNAP frames for IPX on
2427 * non-Ethernet 802.x networks?
2429 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2430 (bpf_int32
)LLCSAP_IPX
);
2432 case ETHERTYPE_ATALK
:
2434 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2435 * SNAP packets with an organization code of
2436 * 0x080007 (Apple, for Appletalk) and a protocol
2437 * type of ETHERTYPE_ATALK (Appletalk).
2439 * XXX - check for an organization code of
2440 * encapsulated Ethernet as well?
2442 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2446 * XXX - we don't have to check for IPX 802.3
2447 * here, but should we check for the IPX Ethertype?
2449 if (proto
<= ETHERMTU
) {
2451 * This is an LLC SAP value, so check
2454 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2458 * This is an Ethernet type; we assume that it's
2459 * unlikely that it'll appear in the right place
2460 * at random, and therefore check only the
2461 * location that would hold the Ethernet type
2462 * in a SNAP frame with an organization code of
2463 * 0x000000 (encapsulated Ethernet).
2465 * XXX - if we were to check for the SNAP DSAP and
2466 * LSAP, as per XXX, and were also to check for an
2467 * organization code of 0x000000 (encapsulated
2468 * Ethernet), we'd do
2470 * return gen_snap(0x000000, proto,
2473 * here; for now, we don't, as per the above.
2474 * I don't know whether it's worth the extra CPU
2475 * time to do the right check or not.
2477 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2483 static struct block
*
2484 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2488 u_int src_off
, dst_off
;
2490 struct block
*b0
, *b1
;
2504 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2505 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2511 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2512 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2519 b0
= gen_linktype(proto
);
2520 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2526 static struct block
*
2527 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2528 struct in6_addr
*addr
;
2529 struct in6_addr
*mask
;
2531 u_int src_off
, dst_off
;
2533 struct block
*b0
, *b1
;
2548 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2549 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2555 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2556 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2563 /* this order is important */
2564 a
= (u_int32_t
*)addr
;
2565 m
= (u_int32_t
*)mask
;
2566 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2567 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2569 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2571 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2573 b0
= gen_linktype(proto
);
2579 static struct block
*
2580 gen_ehostop(eaddr
, dir
)
2581 register const u_char
*eaddr
;
2584 register struct block
*b0
, *b1
;
2588 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2591 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2594 b0
= gen_ehostop(eaddr
, Q_SRC
);
2595 b1
= gen_ehostop(eaddr
, Q_DST
);
2601 b0
= gen_ehostop(eaddr
, Q_SRC
);
2602 b1
= gen_ehostop(eaddr
, Q_DST
);
2611 * Like gen_ehostop, but for DLT_FDDI
2613 static struct block
*
2614 gen_fhostop(eaddr
, dir
)
2615 register const u_char
*eaddr
;
2618 struct block
*b0
, *b1
;
2623 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2625 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2630 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2632 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2636 b0
= gen_fhostop(eaddr
, Q_SRC
);
2637 b1
= gen_fhostop(eaddr
, Q_DST
);
2643 b0
= gen_fhostop(eaddr
, Q_SRC
);
2644 b1
= gen_fhostop(eaddr
, Q_DST
);
2653 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2655 static struct block
*
2656 gen_thostop(eaddr
, dir
)
2657 register const u_char
*eaddr
;
2660 register struct block
*b0
, *b1
;
2664 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2667 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2670 b0
= gen_thostop(eaddr
, Q_SRC
);
2671 b1
= gen_thostop(eaddr
, Q_DST
);
2677 b0
= gen_thostop(eaddr
, Q_SRC
);
2678 b1
= gen_thostop(eaddr
, Q_DST
);
2687 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2689 static struct block
*
2690 gen_wlanhostop(eaddr
, dir
)
2691 register const u_char
*eaddr
;
2694 register struct block
*b0
, *b1
, *b2
;
2695 register struct slist
*s
;
2702 * For control frames, there is no SA.
2704 * For management frames, SA is at an
2705 * offset of 10 from the beginning of
2708 * For data frames, SA is at an offset
2709 * of 10 from the beginning of the packet
2710 * if From DS is clear, at an offset of
2711 * 16 from the beginning of the packet
2712 * if From DS is set and To DS is clear,
2713 * and an offset of 24 from the beginning
2714 * of the packet if From DS is set and To DS
2719 * Generate the tests to be done for data frames
2722 * First, check for To DS set, i.e. check "link[1] & 0x01".
2724 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2725 b1
= new_block(JMP(BPF_JSET
));
2726 b1
->s
.k
= 0x01; /* To DS */
2730 * If To DS is set, the SA is at 24.
2732 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2736 * Now, check for To DS not set, i.e. check
2737 * "!(link[1] & 0x01)".
2739 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2740 b2
= new_block(JMP(BPF_JSET
));
2741 b2
->s
.k
= 0x01; /* To DS */
2746 * If To DS is not set, the SA is at 16.
2748 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2752 * Now OR together the last two checks. That gives
2753 * the complete set of checks for data frames with
2759 * Now check for From DS being set, and AND that with
2760 * the ORed-together checks.
2762 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2763 b1
= new_block(JMP(BPF_JSET
));
2764 b1
->s
.k
= 0x02; /* From DS */
2769 * Now check for data frames with From DS not set.
2771 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2772 b2
= new_block(JMP(BPF_JSET
));
2773 b2
->s
.k
= 0x02; /* From DS */
2778 * If From DS isn't set, the SA is at 10.
2780 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2784 * Now OR together the checks for data frames with
2785 * From DS not set and for data frames with From DS
2786 * set; that gives the checks done for data frames.
2791 * Now check for a data frame.
2792 * I.e, check "link[0] & 0x08".
2794 gen_load_a(OR_LINK
, 0, BPF_B
);
2795 b1
= new_block(JMP(BPF_JSET
));
2800 * AND that with the checks done for data frames.
2805 * If the high-order bit of the type value is 0, this
2806 * is a management frame.
2807 * I.e, check "!(link[0] & 0x08)".
2809 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2810 b2
= new_block(JMP(BPF_JSET
));
2816 * For management frames, the SA is at 10.
2818 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2822 * OR that with the checks done for data frames.
2823 * That gives the checks done for management and
2829 * If the low-order bit of the type value is 1,
2830 * this is either a control frame or a frame
2831 * with a reserved type, and thus not a
2834 * I.e., check "!(link[0] & 0x04)".
2836 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2837 b1
= new_block(JMP(BPF_JSET
));
2843 * AND that with the checks for data and management
2853 * For control frames, there is no DA.
2855 * For management frames, DA is at an
2856 * offset of 4 from the beginning of
2859 * For data frames, DA is at an offset
2860 * of 4 from the beginning of the packet
2861 * if To DS is clear and at an offset of
2862 * 16 from the beginning of the packet
2867 * Generate the tests to be done for data frames.
2869 * First, check for To DS set, i.e. "link[1] & 0x01".
2871 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2872 b1
= new_block(JMP(BPF_JSET
));
2873 b1
->s
.k
= 0x01; /* To DS */
2877 * If To DS is set, the DA is at 16.
2879 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2883 * Now, check for To DS not set, i.e. check
2884 * "!(link[1] & 0x01)".
2886 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2887 b2
= new_block(JMP(BPF_JSET
));
2888 b2
->s
.k
= 0x01; /* To DS */
2893 * If To DS is not set, the DA is at 4.
2895 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2899 * Now OR together the last two checks. That gives
2900 * the complete set of checks for data frames.
2905 * Now check for a data frame.
2906 * I.e, check "link[0] & 0x08".
2908 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2909 b1
= new_block(JMP(BPF_JSET
));
2914 * AND that with the checks done for data frames.
2919 * If the high-order bit of the type value is 0, this
2920 * is a management frame.
2921 * I.e, check "!(link[0] & 0x08)".
2923 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2924 b2
= new_block(JMP(BPF_JSET
));
2930 * For management frames, the DA is at 4.
2932 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2936 * OR that with the checks done for data frames.
2937 * That gives the checks done for management and
2943 * If the low-order bit of the type value is 1,
2944 * this is either a control frame or a frame
2945 * with a reserved type, and thus not a
2948 * I.e., check "!(link[0] & 0x04)".
2950 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2951 b1
= new_block(JMP(BPF_JSET
));
2957 * AND that with the checks for data and management
2964 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2965 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2971 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2972 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2981 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2982 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2983 * as the RFC states.)
2985 static struct block
*
2986 gen_ipfchostop(eaddr
, dir
)
2987 register const u_char
*eaddr
;
2990 register struct block
*b0
, *b1
;
2994 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2997 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3000 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3001 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3007 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3008 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3017 * This is quite tricky because there may be pad bytes in front of the
3018 * DECNET header, and then there are two possible data packet formats that
3019 * carry both src and dst addresses, plus 5 packet types in a format that
3020 * carries only the src node, plus 2 types that use a different format and
3021 * also carry just the src node.
3025 * Instead of doing those all right, we just look for data packets with
3026 * 0 or 1 bytes of padding. If you want to look at other packets, that
3027 * will require a lot more hacking.
3029 * To add support for filtering on DECNET "areas" (network numbers)
3030 * one would want to add a "mask" argument to this routine. That would
3031 * make the filter even more inefficient, although one could be clever
3032 * and not generate masking instructions if the mask is 0xFFFF.
3034 static struct block
*
3035 gen_dnhostop(addr
, dir
)
3039 struct block
*b0
, *b1
, *b2
, *tmp
;
3040 u_int offset_lh
; /* offset if long header is received */
3041 u_int offset_sh
; /* offset if short header is received */
3046 offset_sh
= 1; /* follows flags */
3047 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3051 offset_sh
= 3; /* follows flags, dstnode */
3052 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3056 /* Inefficient because we do our Calvinball dance twice */
3057 b0
= gen_dnhostop(addr
, Q_SRC
);
3058 b1
= gen_dnhostop(addr
, Q_DST
);
3064 /* Inefficient because we do our Calvinball dance twice */
3065 b0
= gen_dnhostop(addr
, Q_SRC
);
3066 b1
= gen_dnhostop(addr
, Q_DST
);
3071 bpf_error("ISO host filtering not implemented");
3076 b0
= gen_linktype(ETHERTYPE_DN
);
3077 /* Check for pad = 1, long header case */
3078 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3079 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3080 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3081 BPF_H
, (bpf_int32
)ntohs(addr
));
3083 /* Check for pad = 0, long header case */
3084 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3085 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3088 /* Check for pad = 1, short header case */
3089 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3090 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3091 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3094 /* Check for pad = 0, short header case */
3095 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3096 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3100 /* Combine with test for linktype */
3106 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3107 * test the bottom-of-stack bit, and then check the version number
3108 * field in the IP header.
3110 static struct block
*
3111 gen_mpls_linktype(proto
)
3114 struct block
*b0
, *b1
;
3119 /* match the bottom-of-stack bit */
3120 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3121 /* match the IPv4 version number */
3122 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3127 /* match the bottom-of-stack bit */
3128 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3129 /* match the IPv4 version number */
3130 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3139 static struct block
*
3140 gen_host(addr
, mask
, proto
, dir
, type
)
3147 struct block
*b0
, *b1
;
3148 const char *typestr
;
3158 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3160 * Only check for non-IPv4 addresses if we're not
3161 * checking MPLS-encapsulated packets.
3163 if (label_stack_depth
== 0) {
3164 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3166 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3172 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3175 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3178 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3181 bpf_error("'tcp' modifier applied to %s", typestr
);
3184 bpf_error("'sctp' modifier applied to %s", typestr
);
3187 bpf_error("'udp' modifier applied to %s", typestr
);
3190 bpf_error("'icmp' modifier applied to %s", typestr
);
3193 bpf_error("'igmp' modifier applied to %s", typestr
);
3196 bpf_error("'igrp' modifier applied to %s", typestr
);
3199 bpf_error("'pim' modifier applied to %s", typestr
);
3202 bpf_error("'vrrp' modifier applied to %s", typestr
);
3205 bpf_error("ATALK host filtering not implemented");
3208 bpf_error("AARP host filtering not implemented");
3211 return gen_dnhostop(addr
, dir
);
3214 bpf_error("SCA host filtering not implemented");
3217 bpf_error("LAT host filtering not implemented");
3220 bpf_error("MOPDL host filtering not implemented");
3223 bpf_error("MOPRC host filtering not implemented");
3227 bpf_error("'ip6' modifier applied to ip host");
3230 bpf_error("'icmp6' modifier applied to %s", typestr
);
3234 bpf_error("'ah' modifier applied to %s", typestr
);
3237 bpf_error("'esp' modifier applied to %s", typestr
);
3240 bpf_error("ISO host filtering not implemented");
3243 bpf_error("'esis' modifier applied to %s", typestr
);
3246 bpf_error("'isis' modifier applied to %s", typestr
);
3249 bpf_error("'clnp' modifier applied to %s", typestr
);
3252 bpf_error("'stp' modifier applied to %s", typestr
);
3255 bpf_error("IPX host filtering not implemented");
3258 bpf_error("'netbeui' modifier applied to %s", typestr
);
3261 bpf_error("'radio' modifier applied to %s", typestr
);
3270 static struct block
*
3271 gen_host6(addr
, mask
, proto
, dir
, type
)
3272 struct in6_addr
*addr
;
3273 struct in6_addr
*mask
;
3278 const char *typestr
;
3288 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3291 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3294 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3297 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3300 bpf_error("'sctp' modifier applied to %s", typestr
);
3303 bpf_error("'tcp' modifier applied to %s", typestr
);
3306 bpf_error("'udp' modifier applied to %s", typestr
);
3309 bpf_error("'icmp' modifier applied to %s", typestr
);
3312 bpf_error("'igmp' modifier applied to %s", typestr
);
3315 bpf_error("'igrp' modifier applied to %s", typestr
);
3318 bpf_error("'pim' modifier applied to %s", typestr
);
3321 bpf_error("'vrrp' modifier applied to %s", typestr
);
3324 bpf_error("ATALK host filtering not implemented");
3327 bpf_error("AARP host filtering not implemented");
3330 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3333 bpf_error("SCA host filtering not implemented");
3336 bpf_error("LAT host filtering not implemented");
3339 bpf_error("MOPDL host filtering not implemented");
3342 bpf_error("MOPRC host filtering not implemented");
3345 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3348 bpf_error("'icmp6' modifier applied to %s", typestr
);
3351 bpf_error("'ah' modifier applied to %s", typestr
);
3354 bpf_error("'esp' modifier applied to %s", typestr
);
3357 bpf_error("ISO host filtering not implemented");
3360 bpf_error("'esis' modifier applied to %s", typestr
);
3363 bpf_error("'isis' modifier applied to %s", typestr
);
3366 bpf_error("'clnp' modifier applied to %s", typestr
);
3369 bpf_error("'stp' modifier applied to %s", typestr
);
3372 bpf_error("IPX host filtering not implemented");
3375 bpf_error("'netbeui' modifier applied to %s", typestr
);
3378 bpf_error("'radio' modifier applied to %s", typestr
);
3388 static struct block
*
3389 gen_gateway(eaddr
, alist
, proto
, dir
)
3390 const u_char
*eaddr
;
3391 bpf_u_int32
**alist
;
3395 struct block
*b0
, *b1
, *tmp
;
3398 bpf_error("direction applied to 'gateway'");
3407 b0
= gen_ehostop(eaddr
, Q_OR
);
3410 b0
= gen_fhostop(eaddr
, Q_OR
);
3413 b0
= gen_thostop(eaddr
, Q_OR
);
3415 case DLT_IEEE802_11
:
3416 case DLT_IEEE802_11_RADIO_AVS
:
3417 case DLT_IEEE802_11_RADIO
:
3418 case DLT_PRISM_HEADER
:
3419 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3424 * Check that the packet doesn't begin with an
3425 * LE Control marker. (We've already generated
3428 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3433 * Now check the MAC address.
3435 b0
= gen_ehostop(eaddr
, Q_OR
);
3439 case DLT_IP_OVER_FC
:
3440 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3444 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3446 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3448 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3457 bpf_error("illegal modifier of 'gateway'");
3463 gen_proto_abbrev(proto
)
3472 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3474 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3480 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3482 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3488 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3490 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3496 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3499 #ifndef IPPROTO_IGMP
3500 #define IPPROTO_IGMP 2
3504 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3507 #ifndef IPPROTO_IGRP
3508 #define IPPROTO_IGRP 9
3511 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3515 #define IPPROTO_PIM 103
3519 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3521 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3526 #ifndef IPPROTO_VRRP
3527 #define IPPROTO_VRRP 112
3531 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3535 b1
= gen_linktype(ETHERTYPE_IP
);
3539 b1
= gen_linktype(ETHERTYPE_ARP
);
3543 b1
= gen_linktype(ETHERTYPE_REVARP
);
3547 bpf_error("link layer applied in wrong context");
3550 b1
= gen_linktype(ETHERTYPE_ATALK
);
3554 b1
= gen_linktype(ETHERTYPE_AARP
);
3558 b1
= gen_linktype(ETHERTYPE_DN
);
3562 b1
= gen_linktype(ETHERTYPE_SCA
);
3566 b1
= gen_linktype(ETHERTYPE_LAT
);
3570 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3574 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3579 b1
= gen_linktype(ETHERTYPE_IPV6
);
3582 #ifndef IPPROTO_ICMPV6
3583 #define IPPROTO_ICMPV6 58
3586 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3591 #define IPPROTO_AH 51
3594 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3596 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3602 #define IPPROTO_ESP 50
3605 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3607 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3613 b1
= gen_linktype(LLCSAP_ISONS
);
3617 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3621 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3624 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3625 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3626 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3628 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3630 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3632 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3636 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3637 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3638 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3640 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3642 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3644 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3648 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3649 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3650 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3652 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3657 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3658 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3663 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3664 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3666 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3668 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3673 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3674 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3679 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3680 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3685 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3689 b1
= gen_linktype(LLCSAP_8021D
);
3693 b1
= gen_linktype(LLCSAP_IPX
);
3697 b1
= gen_linktype(LLCSAP_NETBEUI
);
3701 bpf_error("'radio' is not a valid protocol type");
3709 static struct block
*
3716 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3717 b
= new_block(JMP(BPF_JSET
));
3726 * Generate a comparison to a port value in the transport-layer header
3727 * at the specified offset from the beginning of that header.
3729 * XXX - this handles a variable-length prefix preceding the link-layer
3730 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3731 * variable-length link-layer headers (such as Token Ring or 802.11
3734 static struct block
*
3735 gen_portatom(off
, v
)
3739 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3743 static struct block
*
3744 gen_portatom6(off
, v
)
3748 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3753 gen_portop(port
, proto
, dir
)
3754 int port
, proto
, dir
;
3756 struct block
*b0
, *b1
, *tmp
;
3758 /* ip proto 'proto' */
3759 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3765 b1
= gen_portatom(0, (bpf_int32
)port
);
3769 b1
= gen_portatom(2, (bpf_int32
)port
);
3774 tmp
= gen_portatom(0, (bpf_int32
)port
);
3775 b1
= gen_portatom(2, (bpf_int32
)port
);
3780 tmp
= gen_portatom(0, (bpf_int32
)port
);
3781 b1
= gen_portatom(2, (bpf_int32
)port
);
3793 static struct block
*
3794 gen_port(port
, ip_proto
, dir
)
3799 struct block
*b0
, *b1
, *tmp
;
3804 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3805 * not LLC encapsulation with LLCSAP_IP.
3807 * For IEEE 802 networks - which includes 802.5 token ring
3808 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3809 * says that SNAP encapsulation is used, not LLC encapsulation
3812 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3813 * RFC 2225 say that SNAP encapsulation is used, not LLC
3814 * encapsulation with LLCSAP_IP.
3816 * So we always check for ETHERTYPE_IP.
3818 b0
= gen_linktype(ETHERTYPE_IP
);
3824 b1
= gen_portop(port
, ip_proto
, dir
);
3828 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3829 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3831 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3844 gen_portop6(port
, proto
, dir
)
3845 int port
, proto
, dir
;
3847 struct block
*b0
, *b1
, *tmp
;
3849 /* ip6 proto 'proto' */
3850 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3854 b1
= gen_portatom6(0, (bpf_int32
)port
);
3858 b1
= gen_portatom6(2, (bpf_int32
)port
);
3863 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3864 b1
= gen_portatom6(2, (bpf_int32
)port
);
3869 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3870 b1
= gen_portatom6(2, (bpf_int32
)port
);
3882 static struct block
*
3883 gen_port6(port
, ip_proto
, dir
)
3888 struct block
*b0
, *b1
, *tmp
;
3890 /* link proto ip6 */
3891 b0
= gen_linktype(ETHERTYPE_IPV6
);
3897 b1
= gen_portop6(port
, ip_proto
, dir
);
3901 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3902 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3904 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3916 /* gen_portrange code */
3917 static struct block
*
3918 gen_portrangeatom(off
, v1
, v2
)
3922 struct block
*b1
, *b2
;
3926 * Reverse the order of the ports, so v1 is the lower one.
3935 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3936 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3944 gen_portrangeop(port1
, port2
, proto
, dir
)
3949 struct block
*b0
, *b1
, *tmp
;
3951 /* ip proto 'proto' */
3952 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3958 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3962 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3967 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3968 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3973 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3974 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3986 static struct block
*
3987 gen_portrange(port1
, port2
, ip_proto
, dir
)
3992 struct block
*b0
, *b1
, *tmp
;
3995 b0
= gen_linktype(ETHERTYPE_IP
);
4001 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4005 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4006 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4008 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4020 static struct block
*
4021 gen_portrangeatom6(off
, v1
, v2
)
4025 struct block
*b1
, *b2
;
4029 * Reverse the order of the ports, so v1 is the lower one.
4038 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4039 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4047 gen_portrangeop6(port1
, port2
, proto
, dir
)
4052 struct block
*b0
, *b1
, *tmp
;
4054 /* ip6 proto 'proto' */
4055 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4059 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4063 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4068 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4069 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4074 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4075 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4087 static struct block
*
4088 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4093 struct block
*b0
, *b1
, *tmp
;
4095 /* link proto ip6 */
4096 b0
= gen_linktype(ETHERTYPE_IPV6
);
4102 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4106 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4107 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4109 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4122 lookup_proto(name
, proto
)
4123 register const char *name
;
4133 v
= pcap_nametoproto(name
);
4134 if (v
== PROTO_UNDEF
)
4135 bpf_error("unknown ip proto '%s'", name
);
4139 /* XXX should look up h/w protocol type based on linktype */
4140 v
= pcap_nametoeproto(name
);
4141 if (v
== PROTO_UNDEF
) {
4142 v
= pcap_nametollc(name
);
4143 if (v
== PROTO_UNDEF
)
4144 bpf_error("unknown ether proto '%s'", name
);
4149 if (strcmp(name
, "esis") == 0)
4151 else if (strcmp(name
, "isis") == 0)
4153 else if (strcmp(name
, "clnp") == 0)
4156 bpf_error("unknown osi proto '%s'", name
);
4176 static struct block
*
4177 gen_protochain(v
, proto
, dir
)
4182 #ifdef NO_PROTOCHAIN
4183 return gen_proto(v
, proto
, dir
);
4185 struct block
*b0
, *b
;
4186 struct slist
*s
[100];
4187 int fix2
, fix3
, fix4
, fix5
;
4188 int ahcheck
, again
, end
;
4190 int reg2
= alloc_reg();
4192 memset(s
, 0, sizeof(s
));
4193 fix2
= fix3
= fix4
= fix5
= 0;
4200 b0
= gen_protochain(v
, Q_IP
, dir
);
4201 b
= gen_protochain(v
, Q_IPV6
, dir
);
4205 bpf_error("bad protocol applied for 'protochain'");
4210 * We don't handle variable-length radiotap here headers yet.
4211 * We might want to add BPF instructions to do the protochain
4212 * work, to simplify that and, on platforms that have a BPF
4213 * interpreter with the new instructions, let the filtering
4214 * be done in the kernel. (We already require a modified BPF
4215 * engine to do the protochain stuff, to support backward
4216 * branches, and backward branch support is unlikely to appear
4217 * in kernel BPF engines.)
4219 if (linktype
== DLT_IEEE802_11_RADIO
)
4220 bpf_error("'protochain' not supported with radiotap headers");
4222 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4225 * s[0] is a dummy entry to protect other BPF insn from damage
4226 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4227 * hard to find interdependency made by jump table fixup.
4230 s
[i
] = new_stmt(0); /*dummy*/
4235 b0
= gen_linktype(ETHERTYPE_IP
);
4238 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4239 s
[i
]->s
.k
= off_nl
+ 9;
4241 /* X = ip->ip_hl << 2 */
4242 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4248 b0
= gen_linktype(ETHERTYPE_IPV6
);
4250 /* A = ip6->ip_nxt */
4251 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4252 s
[i
]->s
.k
= off_nl
+ 6;
4254 /* X = sizeof(struct ip6_hdr) */
4255 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4261 bpf_error("unsupported proto to gen_protochain");
4265 /* again: if (A == v) goto end; else fall through; */
4267 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4269 s
[i
]->s
.jt
= NULL
; /*later*/
4270 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4274 #ifndef IPPROTO_NONE
4275 #define IPPROTO_NONE 59
4277 /* if (A == IPPROTO_NONE) goto end */
4278 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4279 s
[i
]->s
.jt
= NULL
; /*later*/
4280 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4281 s
[i
]->s
.k
= IPPROTO_NONE
;
4282 s
[fix5
]->s
.jf
= s
[i
];
4287 if (proto
== Q_IPV6
) {
4288 int v6start
, v6end
, v6advance
, j
;
4291 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4292 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4293 s
[i
]->s
.jt
= NULL
; /*later*/
4294 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4295 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4296 s
[fix2
]->s
.jf
= s
[i
];
4298 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4299 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4300 s
[i
]->s
.jt
= NULL
; /*later*/
4301 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4302 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4304 /* if (A == IPPROTO_ROUTING) goto v6advance */
4305 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4306 s
[i
]->s
.jt
= NULL
; /*later*/
4307 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4308 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4310 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4311 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4312 s
[i
]->s
.jt
= NULL
; /*later*/
4313 s
[i
]->s
.jf
= NULL
; /*later*/
4314 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4325 * X = X + (P[X + 1] + 1) * 8;
4328 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4330 /* A = P[X + packet head] */
4331 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4335 s
[i
] = new_stmt(BPF_ST
);
4339 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4342 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4346 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4348 /* A = P[X + packet head]; */
4349 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4353 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4357 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4361 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4364 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4368 /* goto again; (must use BPF_JA for backward jump) */
4369 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4370 s
[i
]->s
.k
= again
- i
- 1;
4371 s
[i
- 1]->s
.jf
= s
[i
];
4375 for (j
= v6start
; j
<= v6end
; j
++)
4376 s
[j
]->s
.jt
= s
[v6advance
];
4381 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4383 s
[fix2
]->s
.jf
= s
[i
];
4389 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4390 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4391 s
[i
]->s
.jt
= NULL
; /*later*/
4392 s
[i
]->s
.jf
= NULL
; /*later*/
4393 s
[i
]->s
.k
= IPPROTO_AH
;
4395 s
[fix3
]->s
.jf
= s
[ahcheck
];
4402 * X = X + (P[X + 1] + 2) * 4;
4405 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4407 /* A = P[X + packet head]; */
4408 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4412 s
[i
] = new_stmt(BPF_ST
);
4416 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4419 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4423 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4425 /* A = P[X + packet head] */
4426 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4430 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4434 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4438 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4441 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4445 /* goto again; (must use BPF_JA for backward jump) */
4446 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4447 s
[i
]->s
.k
= again
- i
- 1;
4452 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4454 s
[fix2
]->s
.jt
= s
[end
];
4455 s
[fix4
]->s
.jf
= s
[end
];
4456 s
[fix5
]->s
.jt
= s
[end
];
4463 for (i
= 0; i
< max
- 1; i
++)
4464 s
[i
]->next
= s
[i
+ 1];
4465 s
[max
- 1]->next
= NULL
;
4470 b
= new_block(JMP(BPF_JEQ
));
4471 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4482 * Generate code that checks whether the packet is a packet for protocol
4483 * <proto> and whether the type field in that protocol's header has
4484 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4485 * IP packet and checks the protocol number in the IP header against <v>.
4487 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4488 * against Q_IP and Q_IPV6.
4490 static struct block
*
4491 gen_proto(v
, proto
, dir
)
4496 struct block
*b0
, *b1
;
4498 if (dir
!= Q_DEFAULT
)
4499 bpf_error("direction applied to 'proto'");
4504 b0
= gen_proto(v
, Q_IP
, dir
);
4505 b1
= gen_proto(v
, Q_IPV6
, dir
);
4513 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4514 * not LLC encapsulation with LLCSAP_IP.
4516 * For IEEE 802 networks - which includes 802.5 token ring
4517 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4518 * says that SNAP encapsulation is used, not LLC encapsulation
4521 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4522 * RFC 2225 say that SNAP encapsulation is used, not LLC
4523 * encapsulation with LLCSAP_IP.
4525 * So we always check for ETHERTYPE_IP.
4528 b0
= gen_linktype(ETHERTYPE_IP
);
4530 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4532 b1
= gen_protochain(v
, Q_IP
);
4542 * Frame Relay packets typically have an OSI
4543 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4544 * generates code to check for all the OSI
4545 * NLPIDs, so calling it and then adding a check
4546 * for the particular NLPID for which we're
4547 * looking is bogus, as we can just check for
4550 * What we check for is the NLPID and a frame
4551 * control field value of UI, i.e. 0x03 followed
4554 * XXX - assumes a 2-byte Frame Relay header with
4555 * DLCI and flags. What if the address is longer?
4557 * XXX - what about SNAP-encapsulated frames?
4559 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4565 * Cisco uses an Ethertype lookalike - for OSI,
4568 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4569 /* OSI in C-HDLC is stuffed with a fudge byte */
4570 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4575 b0
= gen_linktype(LLCSAP_ISONS
);
4576 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4582 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4584 * 4 is the offset of the PDU type relative to the IS-IS
4587 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4592 bpf_error("arp does not encapsulate another protocol");
4596 bpf_error("rarp does not encapsulate another protocol");
4600 bpf_error("atalk encapsulation is not specifiable");
4604 bpf_error("decnet encapsulation is not specifiable");
4608 bpf_error("sca does not encapsulate another protocol");
4612 bpf_error("lat does not encapsulate another protocol");
4616 bpf_error("moprc does not encapsulate another protocol");
4620 bpf_error("mopdl does not encapsulate another protocol");
4624 return gen_linktype(v
);
4627 bpf_error("'udp proto' is bogus");
4631 bpf_error("'tcp proto' is bogus");
4635 bpf_error("'sctp proto' is bogus");
4639 bpf_error("'icmp proto' is bogus");
4643 bpf_error("'igmp proto' is bogus");
4647 bpf_error("'igrp proto' is bogus");
4651 bpf_error("'pim proto' is bogus");
4655 bpf_error("'vrrp proto' is bogus");
4660 b0
= gen_linktype(ETHERTYPE_IPV6
);
4662 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4664 b1
= gen_protochain(v
, Q_IPV6
);
4670 bpf_error("'icmp6 proto' is bogus");
4674 bpf_error("'ah proto' is bogus");
4677 bpf_error("'ah proto' is bogus");
4680 bpf_error("'stp proto' is bogus");
4683 bpf_error("'ipx proto' is bogus");
4686 bpf_error("'netbeui proto' is bogus");
4689 bpf_error("'radio proto' is bogus");
4700 register const char *name
;
4703 int proto
= q
.proto
;
4707 bpf_u_int32 mask
, addr
;
4709 bpf_u_int32
**alist
;
4712 struct sockaddr_in
*sin
;
4713 struct sockaddr_in6
*sin6
;
4714 struct addrinfo
*res
, *res0
;
4715 struct in6_addr mask128
;
4717 struct block
*b
, *tmp
;
4718 int port
, real_proto
;
4724 addr
= pcap_nametonetaddr(name
);
4726 bpf_error("unknown network '%s'", name
);
4727 /* Left justify network addr and calculate its network mask */
4729 while (addr
&& (addr
& 0xff000000) == 0) {
4733 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
4737 if (proto
== Q_LINK
) {
4741 eaddr
= pcap_ether_hostton(name
);
4744 "unknown ether host '%s'", name
);
4745 b
= gen_ehostop(eaddr
, dir
);
4750 eaddr
= pcap_ether_hostton(name
);
4753 "unknown FDDI host '%s'", name
);
4754 b
= gen_fhostop(eaddr
, dir
);
4759 eaddr
= pcap_ether_hostton(name
);
4762 "unknown token ring host '%s'", name
);
4763 b
= gen_thostop(eaddr
, dir
);
4767 case DLT_IEEE802_11
:
4768 case DLT_IEEE802_11_RADIO_AVS
:
4769 case DLT_IEEE802_11_RADIO
:
4770 case DLT_PRISM_HEADER
:
4771 eaddr
= pcap_ether_hostton(name
);
4774 "unknown 802.11 host '%s'", name
);
4775 b
= gen_wlanhostop(eaddr
, dir
);
4779 case DLT_IP_OVER_FC
:
4780 eaddr
= pcap_ether_hostton(name
);
4783 "unknown Fibre Channel host '%s'", name
);
4784 b
= gen_ipfchostop(eaddr
, dir
);
4793 * Check that the packet doesn't begin
4794 * with an LE Control marker. (We've
4795 * already generated a test for LANE.)
4797 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4801 eaddr
= pcap_ether_hostton(name
);
4804 "unknown ether host '%s'", name
);
4805 b
= gen_ehostop(eaddr
, dir
);
4811 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4812 } else if (proto
== Q_DECNET
) {
4813 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4815 * I don't think DECNET hosts can be multihomed, so
4816 * there is no need to build up a list of addresses
4818 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
4821 alist
= pcap_nametoaddr(name
);
4822 if (alist
== NULL
|| *alist
== NULL
)
4823 bpf_error("unknown host '%s'", name
);
4825 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4827 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
4829 tmp
= gen_host(**alist
++, 0xffffffff,
4830 tproto
, dir
, q
.addr
);
4836 memset(&mask128
, 0xff, sizeof(mask128
));
4837 res0
= res
= pcap_nametoaddrinfo(name
);
4839 bpf_error("unknown host '%s'", name
);
4841 tproto
= tproto6
= proto
;
4842 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4846 for (res
= res0
; res
; res
= res
->ai_next
) {
4847 switch (res
->ai_family
) {
4849 if (tproto
== Q_IPV6
)
4852 sin
= (struct sockaddr_in
*)
4854 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4855 0xffffffff, tproto
, dir
, q
.addr
);
4858 if (tproto6
== Q_IP
)
4861 sin6
= (struct sockaddr_in6
*)
4863 tmp
= gen_host6(&sin6
->sin6_addr
,
4864 &mask128
, tproto6
, dir
, q
.addr
);
4875 bpf_error("unknown host '%s'%s", name
,
4876 (proto
== Q_DEFAULT
)
4878 : " for specified address family");
4885 if (proto
!= Q_DEFAULT
&&
4886 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4887 bpf_error("illegal qualifier of 'port'");
4888 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4889 bpf_error("unknown port '%s'", name
);
4890 if (proto
== Q_UDP
) {
4891 if (real_proto
== IPPROTO_TCP
)
4892 bpf_error("port '%s' is tcp", name
);
4893 else if (real_proto
== IPPROTO_SCTP
)
4894 bpf_error("port '%s' is sctp", name
);
4896 /* override PROTO_UNDEF */
4897 real_proto
= IPPROTO_UDP
;
4899 if (proto
== Q_TCP
) {
4900 if (real_proto
== IPPROTO_UDP
)
4901 bpf_error("port '%s' is udp", name
);
4903 else if (real_proto
== IPPROTO_SCTP
)
4904 bpf_error("port '%s' is sctp", name
);
4906 /* override PROTO_UNDEF */
4907 real_proto
= IPPROTO_TCP
;
4909 if (proto
== Q_SCTP
) {
4910 if (real_proto
== IPPROTO_UDP
)
4911 bpf_error("port '%s' is udp", name
);
4913 else if (real_proto
== IPPROTO_TCP
)
4914 bpf_error("port '%s' is tcp", name
);
4916 /* override PROTO_UNDEF */
4917 real_proto
= IPPROTO_SCTP
;
4920 return gen_port(port
, real_proto
, dir
);
4924 b
= gen_port(port
, real_proto
, dir
);
4925 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4931 if (proto
!= Q_DEFAULT
&&
4932 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4933 bpf_error("illegal qualifier of 'portrange'");
4934 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4935 bpf_error("unknown port in range '%s'", name
);
4936 if (proto
== Q_UDP
) {
4937 if (real_proto
== IPPROTO_TCP
)
4938 bpf_error("port in range '%s' is tcp", name
);
4939 else if (real_proto
== IPPROTO_SCTP
)
4940 bpf_error("port in range '%s' is sctp", name
);
4942 /* override PROTO_UNDEF */
4943 real_proto
= IPPROTO_UDP
;
4945 if (proto
== Q_TCP
) {
4946 if (real_proto
== IPPROTO_UDP
)
4947 bpf_error("port in range '%s' is udp", name
);
4948 else if (real_proto
== IPPROTO_SCTP
)
4949 bpf_error("port in range '%s' is sctp", name
);
4951 /* override PROTO_UNDEF */
4952 real_proto
= IPPROTO_TCP
;
4954 if (proto
== Q_SCTP
) {
4955 if (real_proto
== IPPROTO_UDP
)
4956 bpf_error("port in range '%s' is udp", name
);
4957 else if (real_proto
== IPPROTO_TCP
)
4958 bpf_error("port in range '%s' is tcp", name
);
4960 /* override PROTO_UNDEF */
4961 real_proto
= IPPROTO_SCTP
;
4964 return gen_portrange(port1
, port2
, real_proto
, dir
);
4968 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4969 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4976 eaddr
= pcap_ether_hostton(name
);
4978 bpf_error("unknown ether host: %s", name
);
4980 alist
= pcap_nametoaddr(name
);
4981 if (alist
== NULL
|| *alist
== NULL
)
4982 bpf_error("unknown host '%s'", name
);
4983 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4987 bpf_error("'gateway' not supported in this configuration");
4991 real_proto
= lookup_proto(name
, proto
);
4992 if (real_proto
>= 0)
4993 return gen_proto(real_proto
, proto
, dir
);
4995 bpf_error("unknown protocol: %s", name
);
4998 real_proto
= lookup_proto(name
, proto
);
4999 if (real_proto
>= 0)
5000 return gen_protochain(real_proto
, proto
, dir
);
5002 bpf_error("unknown protocol: %s", name
);
5014 gen_mcode(s1
, s2
, masklen
, q
)
5015 register const char *s1
, *s2
;
5016 register int masklen
;
5019 register int nlen
, mlen
;
5022 nlen
= __pcap_atoin(s1
, &n
);
5023 /* Promote short ipaddr */
5027 mlen
= __pcap_atoin(s2
, &m
);
5028 /* Promote short ipaddr */
5031 bpf_error("non-network bits set in \"%s mask %s\"",
5034 /* Convert mask len to mask */
5036 bpf_error("mask length must be <= 32");
5039 * X << 32 is not guaranteed by C to be 0; it's
5044 m
= 0xffffffff << (32 - masklen
);
5046 bpf_error("non-network bits set in \"%s/%d\"",
5053 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5056 bpf_error("Mask syntax for networks only");
5064 register const char *s
;
5069 int proto
= q
.proto
;
5075 else if (q
.proto
== Q_DECNET
)
5076 vlen
= __pcap_atodn(s
, &v
);
5078 vlen
= __pcap_atoin(s
, &v
);
5085 if (proto
== Q_DECNET
)
5086 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5087 else if (proto
== Q_LINK
) {
5088 bpf_error("illegal link layer address");
5091 if (s
== NULL
&& q
.addr
== Q_NET
) {
5092 /* Promote short net number */
5093 while (v
&& (v
& 0xff000000) == 0) {
5098 /* Promote short ipaddr */
5102 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5107 proto
= IPPROTO_UDP
;
5108 else if (proto
== Q_TCP
)
5109 proto
= IPPROTO_TCP
;
5110 else if (proto
== Q_SCTP
)
5111 proto
= IPPROTO_SCTP
;
5112 else if (proto
== Q_DEFAULT
)
5113 proto
= PROTO_UNDEF
;
5115 bpf_error("illegal qualifier of 'port'");
5118 return gen_port((int)v
, proto
, dir
);
5122 b
= gen_port((int)v
, proto
, dir
);
5123 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5130 proto
= IPPROTO_UDP
;
5131 else if (proto
== Q_TCP
)
5132 proto
= IPPROTO_TCP
;
5133 else if (proto
== Q_SCTP
)
5134 proto
= IPPROTO_SCTP
;
5135 else if (proto
== Q_DEFAULT
)
5136 proto
= PROTO_UNDEF
;
5138 bpf_error("illegal qualifier of 'portrange'");
5141 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5145 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5146 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5152 bpf_error("'gateway' requires a name");
5156 return gen_proto((int)v
, proto
, dir
);
5159 return gen_protochain((int)v
, proto
, dir
);
5174 gen_mcode6(s1
, s2
, masklen
, q
)
5175 register const char *s1
, *s2
;
5176 register int masklen
;
5179 struct addrinfo
*res
;
5180 struct in6_addr
*addr
;
5181 struct in6_addr mask
;
5186 bpf_error("no mask %s supported", s2
);
5188 res
= pcap_nametoaddrinfo(s1
);
5190 bpf_error("invalid ip6 address %s", s1
);
5192 bpf_error("%s resolved to multiple address", s1
);
5193 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5195 if (sizeof(mask
) * 8 < masklen
)
5196 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5197 memset(&mask
, 0, sizeof(mask
));
5198 memset(&mask
, 0xff, masklen
/ 8);
5200 mask
.s6_addr
[masklen
/ 8] =
5201 (0xff << (8 - masklen
% 8)) & 0xff;
5204 a
= (u_int32_t
*)addr
;
5205 m
= (u_int32_t
*)&mask
;
5206 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5207 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5208 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5216 bpf_error("Mask syntax for networks only");
5220 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5225 bpf_error("invalid qualifier against IPv6 address");
5233 register const u_char
*eaddr
;
5236 struct block
*b
, *tmp
;
5238 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5241 return gen_ehostop(eaddr
, (int)q
.dir
);
5243 return gen_fhostop(eaddr
, (int)q
.dir
);
5245 return gen_thostop(eaddr
, (int)q
.dir
);
5246 case DLT_IEEE802_11
:
5247 case DLT_IEEE802_11_RADIO_AVS
:
5248 case DLT_IEEE802_11_RADIO
:
5249 case DLT_PRISM_HEADER
:
5250 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5254 * Check that the packet doesn't begin with an
5255 * LE Control marker. (We've already generated
5258 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5263 * Now check the MAC address.
5265 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5270 case DLT_IP_OVER_FC
:
5271 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5273 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5277 bpf_error("ethernet address used in non-ether expression");
5283 struct slist
*s0
, *s1
;
5286 * This is definitely not the best way to do this, but the
5287 * lists will rarely get long.
5294 static struct slist
*
5300 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5305 static struct slist
*
5311 s
= new_stmt(BPF_LD
|BPF_MEM
);
5317 * Modify "index" to use the value stored into its register as an
5318 * offset relative to the beginning of the header for the protocol
5319 * "proto", and allocate a register and put an item "size" bytes long
5320 * (1, 2, or 4) at that offset into that register, making it the register
5324 gen_load(proto
, index
, size
)
5329 struct slist
*s
, *tmp
;
5331 int regno
= alloc_reg();
5333 free_reg(index
->regno
);
5337 bpf_error("data size must be 1, 2, or 4");
5353 bpf_error("unsupported index operation");
5357 * The offset is relative to the beginning of the packet
5358 * data, if we have a radio header. (If we don't, this
5361 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5362 linktype
!= DLT_IEEE802_11_RADIO
&&
5363 linktype
!= DLT_PRISM_HEADER
)
5364 bpf_error("radio information not present in capture");
5367 * Load into the X register the offset computed into the
5368 * register specifed by "index".
5370 s
= xfer_to_x(index
);
5373 * Load the item at that offset.
5375 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5377 sappend(index
->s
, s
);
5382 * The offset is relative to the beginning of
5383 * the link-layer header.
5385 * XXX - what about ATM LANE? Should the index be
5386 * relative to the beginning of the AAL5 frame, so
5387 * that 0 refers to the beginning of the LE Control
5388 * field, or relative to the beginning of the LAN
5389 * frame, so that 0 refers, for Ethernet LANE, to
5390 * the beginning of the destination address?
5392 s
= gen_llprefixlen();
5395 * If "s" is non-null, it has code to arrange that the
5396 * X register contains the length of the prefix preceding
5397 * the link-layer header. Add to it the offset computed
5398 * into the register specified by "index", and move that
5399 * into the X register. Otherwise, just load into the X
5400 * register the offset computed into the register specifed
5404 sappend(s
, xfer_to_a(index
));
5405 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5406 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5408 s
= xfer_to_x(index
);
5411 * Load the item at the sum of the offset we've put in the
5412 * X register and the offset of the start of the link
5413 * layer header (which is 0 if the radio header is
5414 * variable-length; that header length is what we put
5415 * into the X register and then added to the index).
5417 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5420 sappend(index
->s
, s
);
5436 * The offset is relative to the beginning of
5437 * the network-layer header.
5438 * XXX - are there any cases where we want
5441 s
= gen_llprefixlen();
5444 * If "s" is non-null, it has code to arrange that the
5445 * X register contains the length of the prefix preceding
5446 * the link-layer header. Add to it the offset computed
5447 * into the register specified by "index", and move that
5448 * into the X register. Otherwise, just load into the X
5449 * register the offset computed into the register specifed
5453 sappend(s
, xfer_to_a(index
));
5454 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5455 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5457 s
= xfer_to_x(index
);
5460 * Load the item at the sum of the offset we've put in the
5461 * X register and the offset of the start of the network
5464 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5467 sappend(index
->s
, s
);
5470 * Do the computation only if the packet contains
5471 * the protocol in question.
5473 b
= gen_proto_abbrev(proto
);
5475 gen_and(index
->b
, b
);
5488 * The offset is relative to the beginning of
5489 * the transport-layer header.
5490 * XXX - are there any cases where we want
5492 * XXX - we should, if we're built with
5493 * IPv6 support, generate code to load either
5494 * IPv4, IPv6, or both, as appropriate.
5496 s
= gen_loadx_iphdrlen();
5499 * The X register now contains the sum of the offset
5500 * of the beginning of the link-layer header and
5501 * the length of the network-layer header. Load
5502 * into the A register the offset relative to
5503 * the beginning of the transport layer header,
5504 * add the X register to that, move that to the
5505 * X register, and load with an offset from the
5506 * X register equal to the offset of the network
5507 * layer header relative to the beginning of
5508 * the link-layer header.
5510 sappend(s
, xfer_to_a(index
));
5511 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5512 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5513 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5515 sappend(index
->s
, s
);
5518 * Do the computation only if the packet contains
5519 * the protocol in question - which is true only
5520 * if this is an IP datagram and is the first or
5521 * only fragment of that datagram.
5523 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5525 gen_and(index
->b
, b
);
5527 gen_and(gen_proto_abbrev(Q_IP
), b
);
5533 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5537 index
->regno
= regno
;
5538 s
= new_stmt(BPF_ST
);
5540 sappend(index
->s
, s
);
5546 gen_relation(code
, a0
, a1
, reversed
)
5548 struct arth
*a0
, *a1
;
5551 struct slist
*s0
, *s1
, *s2
;
5552 struct block
*b
, *tmp
;
5556 if (code
== BPF_JEQ
) {
5557 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5558 b
= new_block(JMP(code
));
5562 b
= new_block(BPF_JMP
|code
|BPF_X
);
5568 sappend(a0
->s
, a1
->s
);
5572 free_reg(a0
->regno
);
5573 free_reg(a1
->regno
);
5575 /* 'and' together protocol checks */
5578 gen_and(a0
->b
, tmp
= a1
->b
);
5594 int regno
= alloc_reg();
5595 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5598 s
= new_stmt(BPF_LD
|BPF_LEN
);
5599 s
->next
= new_stmt(BPF_ST
);
5600 s
->next
->s
.k
= regno
;
5615 a
= (struct arth
*)newchunk(sizeof(*a
));
5619 s
= new_stmt(BPF_LD
|BPF_IMM
);
5621 s
->next
= new_stmt(BPF_ST
);
5637 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5640 s
= new_stmt(BPF_ST
);
5648 gen_arth(code
, a0
, a1
)
5650 struct arth
*a0
, *a1
;
5652 struct slist
*s0
, *s1
, *s2
;
5656 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5661 sappend(a0
->s
, a1
->s
);
5663 free_reg(a0
->regno
);
5664 free_reg(a1
->regno
);
5666 s0
= new_stmt(BPF_ST
);
5667 a0
->regno
= s0
->s
.k
= alloc_reg();
5674 * Here we handle simple allocation of the scratch registers.
5675 * If too many registers are alloc'd, the allocator punts.
5677 static int regused
[BPF_MEMWORDS
];
5681 * Return the next free register.
5686 int n
= BPF_MEMWORDS
;
5689 if (regused
[curreg
])
5690 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5692 regused
[curreg
] = 1;
5696 bpf_error("too many registers needed to evaluate expression");
5701 * Return a register to the table so it can
5711 static struct block
*
5718 s
= new_stmt(BPF_LD
|BPF_LEN
);
5719 b
= new_block(JMP(jmp
));
5730 return gen_len(BPF_JGE
, n
);
5734 * Actually, this is less than or equal.
5742 b
= gen_len(BPF_JGT
, n
);
5749 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5750 * the beginning of the link-layer header.
5751 * XXX - that means you can't test values in the radiotap header, but
5752 * as that header is difficult if not impossible to parse generally
5753 * without a loop, that might not be a severe problem. A new keyword
5754 * "radio" could be added for that, although what you'd really want
5755 * would be a way of testing particular radio header values, which
5756 * would generate code appropriate to the radio header in question.
5759 gen_byteop(op
, idx
, val
)
5770 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5773 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5777 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5781 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5785 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5789 b
= new_block(JMP(BPF_JEQ
));
5796 static u_char abroadcast
[] = { 0x0 };
5799 gen_broadcast(proto
)
5802 bpf_u_int32 hostmask
;
5803 struct block
*b0
, *b1
, *b2
;
5804 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5812 case DLT_ARCNET_LINUX
:
5813 return gen_ahostop(abroadcast
, Q_DST
);
5815 return gen_ehostop(ebroadcast
, Q_DST
);
5817 return gen_fhostop(ebroadcast
, Q_DST
);
5819 return gen_thostop(ebroadcast
, Q_DST
);
5820 case DLT_IEEE802_11
:
5821 case DLT_IEEE802_11_RADIO_AVS
:
5822 case DLT_IEEE802_11_RADIO
:
5823 case DLT_PRISM_HEADER
:
5824 return gen_wlanhostop(ebroadcast
, Q_DST
);
5825 case DLT_IP_OVER_FC
:
5826 return gen_ipfchostop(ebroadcast
, Q_DST
);
5830 * Check that the packet doesn't begin with an
5831 * LE Control marker. (We've already generated
5834 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5839 * Now check the MAC address.
5841 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5847 bpf_error("not a broadcast link");
5852 b0
= gen_linktype(ETHERTYPE_IP
);
5853 hostmask
= ~netmask
;
5854 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5855 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5856 (bpf_int32
)(~0 & hostmask
), hostmask
);
5861 bpf_error("only link-layer/IP broadcast filters supported");
5866 * Generate code to test the low-order bit of a MAC address (that's
5867 * the bottom bit of the *first* byte).
5869 static struct block
*
5870 gen_mac_multicast(offset
)
5873 register struct block
*b0
;
5874 register struct slist
*s
;
5876 /* link[offset] & 1 != 0 */
5877 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5878 b0
= new_block(JMP(BPF_JSET
));
5885 gen_multicast(proto
)
5888 register struct block
*b0
, *b1
, *b2
;
5889 register struct slist
*s
;
5897 case DLT_ARCNET_LINUX
:
5898 /* all ARCnet multicasts use the same address */
5899 return gen_ahostop(abroadcast
, Q_DST
);
5901 /* ether[0] & 1 != 0 */
5902 return gen_mac_multicast(0);
5905 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5907 * XXX - was that referring to bit-order issues?
5909 /* fddi[1] & 1 != 0 */
5910 return gen_mac_multicast(1);
5912 /* tr[2] & 1 != 0 */
5913 return gen_mac_multicast(2);
5914 case DLT_IEEE802_11
:
5915 case DLT_IEEE802_11_RADIO_AVS
:
5916 case DLT_IEEE802_11_RADIO
:
5917 case DLT_PRISM_HEADER
:
5921 * For control frames, there is no DA.
5923 * For management frames, DA is at an
5924 * offset of 4 from the beginning of
5927 * For data frames, DA is at an offset
5928 * of 4 from the beginning of the packet
5929 * if To DS is clear and at an offset of
5930 * 16 from the beginning of the packet
5935 * Generate the tests to be done for data frames.
5937 * First, check for To DS set, i.e. "link[1] & 0x01".
5939 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5940 b1
= new_block(JMP(BPF_JSET
));
5941 b1
->s
.k
= 0x01; /* To DS */
5945 * If To DS is set, the DA is at 16.
5947 b0
= gen_mac_multicast(16);
5951 * Now, check for To DS not set, i.e. check
5952 * "!(link[1] & 0x01)".
5954 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5955 b2
= new_block(JMP(BPF_JSET
));
5956 b2
->s
.k
= 0x01; /* To DS */
5961 * If To DS is not set, the DA is at 4.
5963 b1
= gen_mac_multicast(4);
5967 * Now OR together the last two checks. That gives
5968 * the complete set of checks for data frames.
5973 * Now check for a data frame.
5974 * I.e, check "link[0] & 0x08".
5976 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5977 b1
= new_block(JMP(BPF_JSET
));
5982 * AND that with the checks done for data frames.
5987 * If the high-order bit of the type value is 0, this
5988 * is a management frame.
5989 * I.e, check "!(link[0] & 0x08)".
5991 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5992 b2
= new_block(JMP(BPF_JSET
));
5998 * For management frames, the DA is at 4.
6000 b1
= gen_mac_multicast(4);
6004 * OR that with the checks done for data frames.
6005 * That gives the checks done for management and
6011 * If the low-order bit of the type value is 1,
6012 * this is either a control frame or a frame
6013 * with a reserved type, and thus not a
6016 * I.e., check "!(link[0] & 0x04)".
6018 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6019 b1
= new_block(JMP(BPF_JSET
));
6025 * AND that with the checks for data and management
6030 case DLT_IP_OVER_FC
:
6031 b0
= gen_mac_multicast(2);
6036 * Check that the packet doesn't begin with an
6037 * LE Control marker. (We've already generated
6040 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6044 /* ether[off_mac] & 1 != 0 */
6045 b0
= gen_mac_multicast(off_mac
);
6053 /* Link not known to support multicasts */
6057 b0
= gen_linktype(ETHERTYPE_IP
);
6058 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6064 b0
= gen_linktype(ETHERTYPE_IPV6
);
6065 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6070 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6075 * generate command for inbound/outbound. It's here so we can
6076 * make it link-type specific. 'dir' = 0 implies "inbound",
6077 * = 1 implies "outbound".
6083 register struct block
*b0
;
6086 * Only some data link types support inbound/outbound qualifiers.
6090 b0
= gen_relation(BPF_JEQ
,
6091 gen_load(Q_LINK
, gen_loadi(0), 1),
6099 * Match packets sent by this machine.
6101 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6104 * Match packets sent to this machine.
6105 * (No broadcast or multicast packets, or
6106 * packets sent to some other machine and
6107 * received promiscuously.)
6109 * XXX - packets sent to other machines probably
6110 * shouldn't be matched, but what about broadcast
6111 * or multicast packets we received?
6113 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6118 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6119 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6124 /* match outgoing packets */
6125 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6127 /* match incoming packets */
6128 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6132 case DLT_JUNIPER_MFR
:
6133 case DLT_JUNIPER_MLFR
:
6134 case DLT_JUNIPER_MLPPP
:
6135 case DLT_JUNIPER_ATM1
:
6136 case DLT_JUNIPER_ATM2
:
6137 case DLT_JUNIPER_PPPOE
:
6138 case DLT_JUNIPER_PPPOE_ATM
:
6139 case DLT_JUNIPER_GGSN
:
6140 case DLT_JUNIPER_ES
:
6141 case DLT_JUNIPER_MONITOR
:
6142 case DLT_JUNIPER_SERVICES
:
6143 case DLT_JUNIPER_ETHER
:
6144 case DLT_JUNIPER_PPP
:
6145 case DLT_JUNIPER_FRELAY
:
6146 case DLT_JUNIPER_CHDLC
:
6147 /* juniper flags (including direction) are stored
6148 * the byte after the 3-byte magic number */
6150 /* match outgoing packets */
6151 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6153 /* match incoming packets */
6154 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6159 bpf_error("inbound/outbound not supported on linktype %d",
6167 /* PF firewall log matched interface */
6169 gen_pf_ifname(const char *ifname
)
6174 if (linktype
== DLT_PFLOG
) {
6175 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6176 off
= offsetof(struct pfloghdr
, ifname
);
6178 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6181 if (strlen(ifname
) >= len
) {
6182 bpf_error("ifname interface names can only be %d characters",
6186 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6190 /* PF firewall log ruleset name */
6192 gen_pf_ruleset(char *ruleset
)
6196 if (linktype
!= DLT_PFLOG
) {
6197 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6200 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6201 bpf_error("ruleset names can only be %ld characters",
6202 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6205 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6206 strlen(ruleset
), (const u_char
*)ruleset
);
6210 /* PF firewall log rule number */
6216 if (linktype
== DLT_PFLOG
) {
6217 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6220 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6227 /* PF firewall log sub-rule number */
6229 gen_pf_srnr(int srnr
)
6233 if (linktype
!= DLT_PFLOG
) {
6234 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6238 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6243 /* PF firewall log reason code */
6245 gen_pf_reason(int reason
)
6249 if (linktype
== DLT_PFLOG
) {
6250 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6253 bpf_error("reason not supported on linktype 0x%x", linktype
);
6260 /* PF firewall log action */
6262 gen_pf_action(int action
)
6266 if (linktype
== DLT_PFLOG
) {
6267 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6270 bpf_error("action not supported on linktype 0x%x", linktype
);
6279 register const u_char
*eaddr
;
6282 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6283 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6284 return gen_ahostop(eaddr
, (int)q
.dir
);
6286 bpf_error("ARCnet address used in non-arc expression");
6290 static struct block
*
6291 gen_ahostop(eaddr
, dir
)
6292 register const u_char
*eaddr
;
6295 register struct block
*b0
, *b1
;
6298 /* src comes first, different from Ethernet */
6300 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6303 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6306 b0
= gen_ahostop(eaddr
, Q_SRC
);
6307 b1
= gen_ahostop(eaddr
, Q_DST
);
6313 b0
= gen_ahostop(eaddr
, Q_SRC
);
6314 b1
= gen_ahostop(eaddr
, Q_DST
);
6323 * support IEEE 802.1Q VLAN trunk over ethernet
6329 struct block
*b0
, *b1
;
6331 /* can't check for VLAN-encapsulated packets inside MPLS */
6332 if (label_stack_depth
> 0)
6333 bpf_error("no VLAN match after MPLS");
6336 * Change the offsets to point to the type and data fields within
6337 * the VLAN packet. Just increment the offsets, so that we
6338 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6339 * capture VLAN 200 encapsulated within VLAN 100.
6341 * XXX - this is a bit of a kludge. If we were to split the
6342 * compiler into a parser that parses an expression and
6343 * generates an expression tree, and a code generator that
6344 * takes an expression tree (which could come from our
6345 * parser or from some other parser) and generates BPF code,
6346 * we could perhaps make the offsets parameters of routines
6347 * and, in the handler for an "AND" node, pass to subnodes
6348 * other than the VLAN node the adjusted offsets.
6350 * This would mean that "vlan" would, instead of changing the
6351 * behavior of *all* tests after it, change only the behavior
6352 * of tests ANDed with it. That would change the documented
6353 * semantics of "vlan", which might break some expressions.
6354 * However, it would mean that "(vlan and ip) or ip" would check
6355 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6356 * checking only for VLAN-encapsulated IP, so that could still
6357 * be considered worth doing; it wouldn't break expressions
6358 * that are of the form "vlan and ..." or "vlan N and ...",
6359 * which I suspect are the most common expressions involving
6360 * "vlan". "vlan or ..." doesn't necessarily do what the user
6361 * would really want, now, as all the "or ..." tests would
6362 * be done assuming a VLAN, even though the "or" could be viewed
6363 * as meaning "or, if this isn't a VLAN packet...".
6365 orig_linktype
= off_linktype
; /* save original values */
6377 bpf_error("no VLAN support for data link type %d",
6382 /* check for VLAN */
6383 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6385 /* If a specific VLAN is requested, check VLAN id */
6386 if (vlan_num
>= 0) {
6387 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6403 struct block
*b0
,*b1
;
6406 * Change the offsets to point to the type and data fields within
6407 * the MPLS packet. Just increment the offsets, so that we
6408 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6409 * capture packets with an outer label of 100000 and an inner
6412 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6416 if (label_stack_depth
> 0) {
6417 /* just match the bottom-of-stack bit clear */
6418 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6421 * Indicate that we're checking MPLS-encapsulated headers,
6422 * to make sure higher level code generators don't try to
6423 * match against IP-related protocols such as Q_ARP, Q_RARP
6428 case DLT_C_HDLC
: /* fall through */
6430 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
6431 (bpf_int32
)ETHERTYPE_MPLS
);
6435 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
6436 (bpf_int32
)PPP_MPLS_UCAST
);
6439 /* FIXME add other DLT_s ...
6440 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6441 * leave it for now */
6444 bpf_error("no MPLS support for data link type %d",
6452 /* If a specific MPLS label is requested, check it */
6453 if (label_num
>= 0) {
6454 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6455 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6456 0xfffff000); /* only compare the first 20 bits */
6463 label_stack_depth
++;
6468 * Support PPPOE discovery and session.
6473 /* check for PPPoE discovery */
6474 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6483 * Test against the PPPoE session link-layer type.
6485 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6488 * Change the offsets to point to the type and data fields within
6491 * XXX - this is a bit of a kludge. If we were to split the
6492 * compiler into a parser that parses an expression and
6493 * generates an expression tree, and a code generator that
6494 * takes an expression tree (which could come from our
6495 * parser or from some other parser) and generates BPF code,
6496 * we could perhaps make the offsets parameters of routines
6497 * and, in the handler for an "AND" node, pass to subnodes
6498 * other than the PPPoE node the adjusted offsets.
6500 * This would mean that "pppoes" would, instead of changing the
6501 * behavior of *all* tests after it, change only the behavior
6502 * of tests ANDed with it. That would change the documented
6503 * semantics of "pppoes", which might break some expressions.
6504 * However, it would mean that "(pppoes and ip) or ip" would check
6505 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6506 * checking only for VLAN-encapsulated IP, so that could still
6507 * be considered worth doing; it wouldn't break expressions
6508 * that are of the form "pppoes and ..." which I suspect are the
6509 * most common expressions involving "pppoes". "pppoes or ..."
6510 * doesn't necessarily do what the user would really want, now,
6511 * as all the "or ..." tests would be done assuming PPPoE, even
6512 * though the "or" could be viewed as meaning "or, if this isn't
6513 * a PPPoE packet...".
6515 orig_linktype
= off_linktype
; /* save original values */
6519 * The "network-layer" protocol is PPPoE, which has a 6-byte
6520 * PPPoE header, followed by PPP payload, so we set the
6521 * offsets to the network layer offset plus 6 bytes for
6522 * the PPPoE header plus the values appropriate for PPP when
6523 * encapsulated in Ethernet (which means there's no HDLC
6526 off_linktype
= orig_nl
+ 6;
6527 off_nl
= orig_nl
+ 6 + 2;
6528 off_nl_nosnap
= orig_nl
+ 6 + 2;
6531 * Set the link-layer type to PPP, as all subsequent tests will
6532 * be on the encapsulated PPP header.
6540 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6552 bpf_error("'vpi' supported only on raw ATM");
6553 if (off_vpi
== (u_int
)-1)
6555 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6561 bpf_error("'vci' supported only on raw ATM");
6562 if (off_vci
== (u_int
)-1)
6564 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6569 if (off_proto
== (u_int
)-1)
6570 abort(); /* XXX - this isn't on FreeBSD */
6571 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6576 if (off_payload
== (u_int
)-1)
6578 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6579 0xffffffff, jtype
, reverse
, jvalue
);
6584 bpf_error("'callref' supported only on raw ATM");
6585 if (off_proto
== (u_int
)-1)
6587 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6588 jtype
, reverse
, jvalue
);
6598 gen_atmtype_abbrev(type
)
6601 struct block
*b0
, *b1
;
6606 /* Get all packets in Meta signalling Circuit */
6608 bpf_error("'metac' supported only on raw ATM");
6609 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6610 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6615 /* Get all packets in Broadcast Circuit*/
6617 bpf_error("'bcc' supported only on raw ATM");
6618 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6619 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6624 /* Get all cells in Segment OAM F4 circuit*/
6626 bpf_error("'oam4sc' supported only on raw ATM");
6627 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6628 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6633 /* Get all cells in End-to-End OAM F4 Circuit*/
6635 bpf_error("'oam4ec' supported only on raw ATM");
6636 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6637 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6642 /* Get all packets in connection Signalling Circuit */
6644 bpf_error("'sc' supported only on raw ATM");
6645 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6646 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6651 /* Get all packets in ILMI Circuit */
6653 bpf_error("'ilmic' supported only on raw ATM");
6654 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6655 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6660 /* Get all LANE packets */
6662 bpf_error("'lane' supported only on raw ATM");
6663 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6666 * Arrange that all subsequent tests assume LANE
6667 * rather than LLC-encapsulated packets, and set
6668 * the offsets appropriately for LANE-encapsulated
6671 * "off_mac" is the offset of the Ethernet header,
6672 * which is 2 bytes past the ATM pseudo-header
6673 * (skipping the pseudo-header and 2-byte LE Client
6674 * field). The other offsets are Ethernet offsets
6675 * relative to "off_mac".
6678 off_mac
= off_payload
+ 2; /* MAC header */
6679 off_linktype
= off_mac
+ 12;
6680 off_nl
= off_mac
+ 14; /* Ethernet II */
6681 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6685 /* Get all LLC-encapsulated packets */
6687 bpf_error("'llc' supported only on raw ATM");
6688 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6699 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6706 bpf_u_int32 val1
, val2
, val3
;
6708 switch (mtp3field
) {
6711 if (off_sio
== (u_int
)-1)
6712 bpf_error("'sio' supported only on SS7");
6713 /* sio coded on 1 byte so max value 255 */
6715 bpf_error("sio value %u too big; max value = 255",
6717 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6718 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6722 if (off_opc
== (u_int
)-1)
6723 bpf_error("'opc' supported only on SS7");
6724 /* opc coded on 14 bits so max value 16383 */
6726 bpf_error("opc value %u too big; max value = 16383",
6728 /* the following instructions are made to convert jvalue
6729 * to the form used to write opc in an ss7 message*/
6730 val1
= jvalue
& 0x00003c00;
6732 val2
= jvalue
& 0x000003fc;
6734 val3
= jvalue
& 0x00000003;
6736 jvalue
= val1
+ val2
+ val3
;
6737 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6738 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6742 if (off_dpc
== (u_int
)-1)
6743 bpf_error("'dpc' supported only on SS7");
6744 /* dpc coded on 14 bits so max value 16383 */
6746 bpf_error("dpc value %u too big; max value = 16383",
6748 /* the following instructions are made to convert jvalue
6749 * to the forme used to write dpc in an ss7 message*/
6750 val1
= jvalue
& 0x000000ff;
6752 val2
= jvalue
& 0x00003f00;
6754 jvalue
= val1
+ val2
;
6755 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6756 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6760 if (off_sls
== (u_int
)-1)
6761 bpf_error("'sls' supported only on SS7");
6762 /* sls coded on 4 bits so max value 15 */
6764 bpf_error("sls value %u too big; max value = 15",
6766 /* the following instruction is made to convert jvalue
6767 * to the forme used to write sls in an ss7 message*/
6768 jvalue
= jvalue
<< 4;
6769 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6770 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6779 static struct block
*
6780 gen_msg_abbrev(type
)
6786 * Q.2931 signalling protocol messages for handling virtual circuits
6787 * establishment and teardown
6792 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6796 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6800 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6804 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6808 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6811 case A_RELEASE_DONE
:
6812 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6822 gen_atmmulti_abbrev(type
)
6825 struct block
*b0
, *b1
;
6831 bpf_error("'oam' supported only on raw ATM");
6832 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6837 bpf_error("'oamf4' supported only on raw ATM");
6839 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6840 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6842 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6848 * Get Q.2931 signalling messages for switched
6849 * virtual connection
6852 bpf_error("'connectmsg' supported only on raw ATM");
6853 b0
= gen_msg_abbrev(A_SETUP
);
6854 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6856 b0
= gen_msg_abbrev(A_CONNECT
);
6858 b0
= gen_msg_abbrev(A_CONNECTACK
);
6860 b0
= gen_msg_abbrev(A_RELEASE
);
6862 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6864 b0
= gen_atmtype_abbrev(A_SC
);
6870 bpf_error("'metaconnect' supported only on raw ATM");
6871 b0
= gen_msg_abbrev(A_SETUP
);
6872 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6874 b0
= gen_msg_abbrev(A_CONNECT
);
6876 b0
= gen_msg_abbrev(A_RELEASE
);
6878 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6880 b0
= gen_atmtype_abbrev(A_METAC
);