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.47 2007-05-30 18:06:38 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_PACKET
, /* relative to the beginning of the packet */
139 OR_LINK
, /* relative to the link-layer header */
140 OR_NET
, /* relative to the network-layer header */
141 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
155 #define CHUNK0SIZE 1024
161 static struct chunk chunks
[NCHUNKS
];
162 static int cur_chunk
;
164 static void *newchunk(u_int
);
165 static void freechunks(void);
166 static inline struct block
*new_block(int);
167 static inline struct slist
*new_stmt(int);
168 static struct block
*gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block
*, struct block
*);
172 static void merge(struct block
*, struct block
*);
173 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
178 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
180 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
181 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
182 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
183 static struct slist
*gen_load_llrel(u_int
, u_int
);
184 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
185 static struct slist
*gen_loadx_iphdrlen(void);
186 static struct block
*gen_uncond(int);
187 static inline struct block
*gen_true(void);
188 static inline struct block
*gen_false(void);
189 static struct block
*gen_ether_linktype(int);
190 static struct block
*gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block
*);
192 static void insert_load_llprefixlen(struct block
*);
193 static struct slist
*gen_llprefixlen(void);
194 static struct block
*gen_linktype(int);
195 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
196 static struct block
*gen_llc_linktype(int);
197 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
199 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
201 static struct block
*gen_ahostop(const u_char
*, int);
202 static struct block
*gen_ehostop(const u_char
*, int);
203 static struct block
*gen_fhostop(const u_char
*, int);
204 static struct block
*gen_thostop(const u_char
*, int);
205 static struct block
*gen_wlanhostop(const u_char
*, int);
206 static struct block
*gen_ipfchostop(const u_char
*, int);
207 static struct block
*gen_dnhostop(bpf_u_int32
, int);
208 static struct block
*gen_mpls_linktype(int);
209 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
211 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
214 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
216 static struct block
*gen_ipfrag(void);
217 static struct block
*gen_portatom(int, bpf_int32
);
218 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
220 static struct block
*gen_portatom6(int, bpf_int32
);
221 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
223 struct block
*gen_portop(int, int, int);
224 static struct block
*gen_port(int, int, int);
225 struct block
*gen_portrangeop(int, int, int, int);
226 static struct block
*gen_portrange(int, int, int, int);
228 struct block
*gen_portop6(int, int, int);
229 static struct block
*gen_port6(int, int, int);
230 struct block
*gen_portrangeop6(int, int, int, int);
231 static struct block
*gen_portrange6(int, int, int, int);
233 static int lookup_proto(const char *, int);
234 static struct block
*gen_protochain(int, int, int);
235 static struct block
*gen_proto(int, int, int);
236 static struct slist
*xfer_to_x(struct arth
*);
237 static struct slist
*xfer_to_a(struct arth
*);
238 static struct block
*gen_mac_multicast(int);
239 static struct block
*gen_len(int, int);
241 static struct block
*gen_msg_abbrev(int type
);
252 /* XXX Round up to nearest long. */
253 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
255 /* XXX Round up to structure boundary. */
259 cp
= &chunks
[cur_chunk
];
260 if (n
> cp
->n_left
) {
261 ++cp
, k
= ++cur_chunk
;
263 bpf_error("out of memory");
264 size
= CHUNK0SIZE
<< k
;
265 cp
->m
= (void *)malloc(size
);
267 bpf_error("out of memory");
268 memset((char *)cp
->m
, 0, size
);
271 bpf_error("out of memory");
274 return (void *)((char *)cp
->m
+ cp
->n_left
);
283 for (i
= 0; i
< NCHUNKS
; ++i
)
284 if (chunks
[i
].m
!= NULL
) {
291 * A strdup whose allocations are freed after code generation is over.
295 register const char *s
;
297 int n
= strlen(s
) + 1;
298 char *cp
= newchunk(n
);
304 static inline struct block
*
310 p
= (struct block
*)newchunk(sizeof(*p
));
317 static inline struct slist
*
323 p
= (struct slist
*)newchunk(sizeof(*p
));
329 static struct block
*
333 struct block
*b
= new_block(BPF_RET
|BPF_K
);
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask
;
350 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
351 char *buf
, int optimize
, bpf_u_int32 mask
)
360 if (setjmp(top_ctx
)) {
368 snaplen
= pcap_snapshot(p
);
370 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
371 "snaplen of 0 rejects all packets");
375 lex_init(buf
? buf
: "");
383 root
= gen_retblk(snaplen
);
385 if (optimize
&& !no_optimize
) {
388 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
389 bpf_error("expression rejects all packets");
391 program
->bf_insns
= icode_to_fcode(root
, &len
);
392 program
->bf_len
= len
;
400 * entry point for using the compiler with no pcap open
401 * pass in all the stuff that is needed explicitly instead.
404 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
405 struct bpf_program
*program
,
406 char *buf
, int optimize
, bpf_u_int32 mask
)
411 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
414 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
420 * Clean up a "struct bpf_program" by freeing all the memory allocated
424 pcap_freecode(struct bpf_program
*program
)
427 if (program
->bf_insns
!= NULL
) {
428 free((char *)program
->bf_insns
);
429 program
->bf_insns
= NULL
;
434 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
435 * which of the jt and jf fields has been resolved and which is a pointer
436 * back to another unresolved block (or nil). At least one of the fields
437 * in each block is already resolved.
440 backpatch(list
, target
)
441 struct block
*list
, *target
;
458 * Merge the lists in b0 and b1, using the 'sense' field to indicate
459 * which of jt and jf is the link.
463 struct block
*b0
, *b1
;
465 register struct block
**p
= &b0
;
467 /* Find end of list. */
469 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
471 /* Concatenate the lists. */
479 backpatch(p
, gen_retblk(snaplen
));
480 p
->sense
= !p
->sense
;
481 backpatch(p
, gen_retblk(0));
485 * Insert before the statements of the first (root) block any
486 * statements needed to load the lengths of any variable-length
487 * headers into registers.
489 * XXX - a fancier strategy would be to insert those before the
490 * statements of all blocks that use those lengths and that
491 * have no predecessors that use them, so that we only compute
492 * the lengths if we need them. There might be even better
493 * approaches than that. However, as we're currently only
494 * handling variable-length radiotap headers, and as all
495 * filtering expressions other than raw link[M:N] tests
496 * require the length of that header, doing more for that
497 * header length isn't really worth the effort.
499 insert_load_llprefixlen(root
);
504 struct block
*b0
, *b1
;
506 backpatch(b0
, b1
->head
);
507 b0
->sense
= !b0
->sense
;
508 b1
->sense
= !b1
->sense
;
510 b1
->sense
= !b1
->sense
;
516 struct block
*b0
, *b1
;
518 b0
->sense
= !b0
->sense
;
519 backpatch(b0
, b1
->head
);
520 b0
->sense
= !b0
->sense
;
529 b
->sense
= !b
->sense
;
532 static struct block
*
533 gen_cmp(offrel
, offset
, size
, v
)
534 enum e_offrel offrel
;
538 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
541 static struct block
*
542 gen_cmp_gt(offrel
, offset
, size
, v
)
543 enum e_offrel offrel
;
547 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
550 static struct block
*
551 gen_cmp_ge(offrel
, offset
, size
, v
)
552 enum e_offrel offrel
;
556 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
559 static struct block
*
560 gen_cmp_lt(offrel
, offset
, size
, v
)
561 enum e_offrel offrel
;
565 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
568 static struct block
*
569 gen_cmp_le(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
577 static struct block
*
578 gen_mcmp(offrel
, offset
, size
, v
, mask
)
579 enum e_offrel offrel
;
584 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
587 static struct block
*
588 gen_bcmp(offrel
, offset
, size
, v
)
589 enum e_offrel offrel
;
590 register u_int offset
, size
;
591 register const u_char
*v
;
593 register struct block
*b
, *tmp
;
597 register const u_char
*p
= &v
[size
- 4];
598 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
599 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
601 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
608 register const u_char
*p
= &v
[size
- 2];
609 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
611 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
618 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
627 * AND the field of size "size" at offset "offset" relative to the header
628 * specified by "offrel" with "mask", and compare it with the value "v"
629 * with the test specified by "jtype"; if "reverse" is true, the test
630 * should test the opposite of "jtype".
632 static struct block
*
633 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
634 enum e_offrel offrel
;
636 bpf_u_int32 offset
, size
, mask
, jtype
;
639 struct slist
*s
, *s2
;
642 s
= gen_load_a(offrel
, offset
, size
);
644 if (mask
!= 0xffffffff) {
645 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
650 b
= new_block(JMP(jtype
));
653 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
659 * Various code constructs need to know the layout of the data link
660 * layer. These variables give the necessary offsets from the beginning
661 * of the packet data.
663 * If the link layer has variable_length headers, the offsets are offsets
664 * from the end of the link-link-layer header, and "reg_ll_size" is
665 * the register number for a register containing the length of the
666 * link-layer header. Otherwise, "reg_ll_size" is -1.
668 static int reg_ll_size
;
671 * This is the offset of the beginning of the link-layer header from
672 * the beginning of the raw packet data.
674 * It's usually 0, except for 802.11 with a fixed-length radio header.
675 * (For 802.11 with a variable-length radio header, we have to generate
676 * code to compute that offset; off_ll is 0 in that case.)
681 * This is the offset of the beginning of the MAC-layer header.
682 * It's usually 0, except for ATM LANE, where it's the offset, relative
683 * to the beginning of the raw packet data, of the Ethernet header.
685 static u_int off_mac
;
688 * "off_linktype" is the offset to information in the link-layer header
689 * giving the packet type. This offset is relative to the beginning
690 * of the link-layer header (i.e., it doesn't include off_ll).
692 * For Ethernet, it's the offset of the Ethernet type field.
694 * For link-layer types that always use 802.2 headers, it's the
695 * offset of the LLC header.
697 * For PPP, it's the offset of the PPP type field.
699 * For Cisco HDLC, it's the offset of the CHDLC type field.
701 * For BSD loopback, it's the offset of the AF_ value.
703 * For Linux cooked sockets, it's the offset of the type field.
705 * It's set to -1 for no encapsulation, in which case, IP is assumed.
707 static u_int off_linktype
;
710 * TRUE if the link layer includes an ATM pseudo-header.
712 static int is_atm
= 0;
715 * TRUE if "lane" appeared in the filter; it causes us to generate
716 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
718 static int is_lane
= 0;
721 * These are offsets for the ATM pseudo-header.
723 static u_int off_vpi
;
724 static u_int off_vci
;
725 static u_int off_proto
;
728 * These are offsets for the MTP2 fields.
733 * These are offsets for the MTP3 fields.
735 static u_int off_sio
;
736 static u_int off_opc
;
737 static u_int off_dpc
;
738 static u_int off_sls
;
741 * This is the offset of the first byte after the ATM pseudo_header,
742 * or -1 if there is no ATM pseudo-header.
744 static u_int off_payload
;
747 * These are offsets to the beginning of the network-layer header.
748 * They are relative to the beginning of the link-layer header (i.e.,
749 * they don't include off_ll).
751 * If the link layer never uses 802.2 LLC:
753 * "off_nl" and "off_nl_nosnap" are the same.
755 * If the link layer always uses 802.2 LLC:
757 * "off_nl" is the offset if there's a SNAP header following
760 * "off_nl_nosnap" is the offset if there's no SNAP header.
762 * If the link layer is Ethernet:
764 * "off_nl" is the offset if the packet is an Ethernet II packet
765 * (we assume no 802.3+802.2+SNAP);
767 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
768 * with an 802.2 header following it.
771 static u_int off_nl_nosnap
;
779 linktype
= pcap_datalink(p
);
781 pcap_fddipad
= p
->fddipad
;
785 * Assume it's not raw ATM with a pseudo-header, for now.
796 * And assume we're not doing SS7.
805 * Also assume it's not 802.11 with a fixed-length radio header.
811 label_stack_depth
= 0;
819 off_nl
= 6; /* XXX in reality, variable! */
820 off_nl_nosnap
= 6; /* no 802.2 LLC */
823 case DLT_ARCNET_LINUX
:
825 off_nl
= 8; /* XXX in reality, variable! */
826 off_nl_nosnap
= 8; /* no 802.2 LLC */
831 off_nl
= 14; /* Ethernet II */
832 off_nl_nosnap
= 17; /* 802.3+802.2 */
837 * SLIP doesn't have a link level type. The 16 byte
838 * header is hacked into our SLIP driver.
842 off_nl_nosnap
= 16; /* no 802.2 LLC */
846 /* XXX this may be the same as the DLT_PPP_BSDOS case */
850 off_nl_nosnap
= 24; /* no 802.2 LLC */
857 off_nl_nosnap
= 4; /* no 802.2 LLC */
863 off_nl_nosnap
= 12; /* no 802.2 LLC */
868 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
869 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
872 off_nl_nosnap
= 4; /* no 802.2 LLC */
877 * This does no include the Ethernet header, and
878 * only covers session state.
882 off_nl_nosnap
= 8; /* no 802.2 LLC */
888 off_nl_nosnap
= 24; /* no 802.2 LLC */
893 * FDDI doesn't really have a link-level type field.
894 * We set "off_linktype" to the offset of the LLC header.
896 * To check for Ethernet types, we assume that SSAP = SNAP
897 * is being used and pick out the encapsulated Ethernet type.
898 * XXX - should we generate code to check for SNAP?
902 off_linktype
+= pcap_fddipad
;
904 off_nl
= 21; /* FDDI+802.2+SNAP */
905 off_nl_nosnap
= 16; /* FDDI+802.2 */
907 off_nl
+= pcap_fddipad
;
908 off_nl_nosnap
+= pcap_fddipad
;
914 * Token Ring doesn't really have a link-level type field.
915 * We set "off_linktype" to the offset of the LLC header.
917 * To check for Ethernet types, we assume that SSAP = SNAP
918 * is being used and pick out the encapsulated Ethernet type.
919 * XXX - should we generate code to check for SNAP?
921 * XXX - the header is actually variable-length.
922 * Some various Linux patched versions gave 38
923 * as "off_linktype" and 40 as "off_nl"; however,
924 * if a token ring packet has *no* routing
925 * information, i.e. is not source-routed, the correct
926 * values are 20 and 22, as they are in the vanilla code.
928 * A packet is source-routed iff the uppermost bit
929 * of the first byte of the source address, at an
930 * offset of 8, has the uppermost bit set. If the
931 * packet is source-routed, the total number of bytes
932 * of routing information is 2 plus bits 0x1F00 of
933 * the 16-bit value at an offset of 14 (shifted right
934 * 8 - figure out which byte that is).
937 off_nl
= 22; /* Token Ring+802.2+SNAP */
938 off_nl_nosnap
= 17; /* Token Ring+802.2 */
943 * 802.11 doesn't really have a link-level type field.
944 * We set "off_linktype" to the offset of the LLC header.
946 * To check for Ethernet types, we assume that SSAP = SNAP
947 * is being used and pick out the encapsulated Ethernet type.
948 * XXX - should we generate code to check for SNAP?
950 * XXX - the header is actually variable-length. We
951 * assume a 24-byte link-layer header, as appears in
952 * data frames in networks with no bridges. If the
953 * fromds and tods 802.11 header bits are both set,
954 * it's actually supposed to be 30 bytes.
957 off_nl
= 32; /* 802.11+802.2+SNAP */
958 off_nl_nosnap
= 27; /* 802.11+802.2 */
961 case DLT_PRISM_HEADER
:
963 * Same as 802.11, but with an additional header before
964 * the 802.11 header, containing a bunch of additional
965 * information including radio-level information.
967 * The header is 144 bytes long.
969 * XXX - same variable-length header problem; at least
970 * the Prism header is fixed-length.
974 off_nl
= 32; /* Prism+802.11+802.2+SNAP */
975 off_nl_nosnap
= 27; /* Prism+802.11+802.2 */
978 case DLT_IEEE802_11_RADIO_AVS
:
980 * Same as 802.11, but with an additional header before
981 * the 802.11 header, containing a bunch of additional
982 * information including radio-level information.
984 * The header is 64 bytes long, at least in its
985 * current incarnation.
987 * XXX - same variable-length header problem, only
988 * more so; this header is also variable-length,
989 * with the length being the 32-bit big-endian
990 * number at an offset of 4 from the beginning
991 * of the radio header. We should handle that the
992 * same way we handle the length at the beginning
993 * of the radiotap header.
995 * XXX - in Linux, do any drivers that supply an AVS
996 * header supply a link-layer type other than
997 * ARPHRD_IEEE80211_PRISM? If so, we should map that
998 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
999 * any drivers that supply an AVS header but supply
1000 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1001 * have to check the header in the generated code to
1002 * determine whether it's Prism or AVS.
1006 off_nl
= 32; /* Radio+802.11+802.2+SNAP */
1007 off_nl_nosnap
= 27; /* Radio+802.11+802.2 */
1010 case DLT_IEEE802_11_RADIO
:
1012 * Same as 802.11, but with an additional header before
1013 * the 802.11 header, containing a bunch of additional
1014 * information including radio-level information.
1016 * The radiotap header is variable length, and we
1017 * generate code to compute its length and store it
1018 * in a register. These offsets are relative to the
1019 * beginning of the 802.11 header.
1022 off_nl
= 32; /* 802.11+802.2+SNAP */
1023 off_nl_nosnap
= 27; /* 802.11+802.2 */
1026 case DLT_ATM_RFC1483
:
1027 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1029 * assume routed, non-ISO PDUs
1030 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1032 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1033 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1034 * latter would presumably be treated the way PPPoE
1035 * should be, so you can do "pppoe and udp port 2049"
1036 * or "pppoa and tcp port 80" and have it check for
1037 * PPPo{A,E} and a PPP protocol of IP and....
1040 off_nl
= 8; /* 802.2+SNAP */
1041 off_nl_nosnap
= 3; /* 802.2 */
1046 * Full Frontal ATM; you get AALn PDUs with an ATM
1050 off_vpi
= SUNATM_VPI_POS
;
1051 off_vci
= SUNATM_VCI_POS
;
1052 off_proto
= PROTO_POS
;
1053 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1054 off_payload
= SUNATM_PKT_BEGIN_POS
;
1055 off_linktype
= off_payload
;
1056 off_nl
= off_payload
+8; /* 802.2+SNAP */
1057 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1063 off_nl_nosnap
= 0; /* no 802.2 LLC */
1066 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1069 off_nl_nosnap
= 16; /* no 802.2 LLC */
1074 * LocalTalk does have a 1-byte type field in the LLAP header,
1075 * but really it just indicates whether there is a "short" or
1076 * "long" DDP packet following.
1080 off_nl_nosnap
= 0; /* no 802.2 LLC */
1083 case DLT_IP_OVER_FC
:
1085 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1086 * link-level type field. We set "off_linktype" to the
1087 * offset of the LLC header.
1089 * To check for Ethernet types, we assume that SSAP = SNAP
1090 * is being used and pick out the encapsulated Ethernet type.
1091 * XXX - should we generate code to check for SNAP? RFC
1092 * 2625 says SNAP should be used.
1095 off_nl
= 24; /* IPFC+802.2+SNAP */
1096 off_nl_nosnap
= 19; /* IPFC+802.2 */
1101 * XXX - we should set this to handle SNAP-encapsulated
1102 * frames (NLPID of 0x80).
1106 off_nl_nosnap
= 0; /* no 802.2 LLC */
1110 * the only BPF-interesting FRF.16 frames are non-control frames;
1111 * Frame Relay has a variable length link-layer
1112 * so lets start with offset 4 for now and increments later on (FIXME);
1117 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1120 case DLT_APPLE_IP_OVER_IEEE1394
:
1123 off_nl_nosnap
= 18; /* no 802.2 LLC */
1126 case DLT_LINUX_IRDA
:
1128 * Currently, only raw "link[N:M]" filtering is supported.
1137 * Currently, only raw "link[N:M]" filtering is supported.
1144 case DLT_SYMANTEC_FIREWALL
:
1146 off_nl
= 44; /* Ethernet II */
1147 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1152 /* XXX read this from pf.h? */
1153 off_nl
= PFLOG_HDRLEN
;
1154 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1157 case DLT_JUNIPER_MFR
:
1158 case DLT_JUNIPER_MLFR
:
1159 case DLT_JUNIPER_MLPPP
:
1160 case DLT_JUNIPER_PPP
:
1161 case DLT_JUNIPER_CHDLC
:
1162 case DLT_JUNIPER_FRELAY
:
1165 off_nl_nosnap
= -1; /* no 802.2 LLC */
1168 case DLT_JUNIPER_ATM1
:
1169 off_linktype
= 4; /* in reality variable between 4-8 */
1174 case DLT_JUNIPER_ATM2
:
1175 off_linktype
= 8; /* in reality variable between 8-12 */
1180 /* frames captured on a Juniper PPPoE service PIC
1181 * contain raw ethernet frames */
1182 case DLT_JUNIPER_PPPOE
:
1183 case DLT_JUNIPER_ETHER
:
1185 off_nl
= 18; /* Ethernet II */
1186 off_nl_nosnap
= 21; /* 802.3+802.2 */
1189 case DLT_JUNIPER_PPPOE_ATM
:
1192 off_nl_nosnap
= -1; /* no 802.2 LLC */
1195 case DLT_JUNIPER_GGSN
:
1198 off_nl_nosnap
= -1; /* no 802.2 LLC */
1201 case DLT_JUNIPER_ES
:
1203 off_nl
= -1; /* not really a network layer but raw IP adresses */
1204 off_nl_nosnap
= -1; /* no 802.2 LLC */
1207 case DLT_JUNIPER_MONITOR
:
1209 off_nl
= 12; /* raw IP/IP6 header */
1210 off_nl_nosnap
= -1; /* no 802.2 LLC */
1213 case DLT_JUNIPER_SERVICES
:
1215 off_nl
= -1; /* L3 proto location dep. on cookie type */
1216 off_nl_nosnap
= -1; /* no 802.2 LLC */
1219 case DLT_JUNIPER_VP
:
1244 case DLT_LINUX_LAPD
:
1246 * Currently, only raw "link[N:M]" filtering is supported.
1255 * Currently, only raw "link[N:M]" filtering is supported.
1262 case DLT_BLUETOOTH_HCI_H4
:
1264 * Currently, only raw "link[N:M]" filtering is supported.
1271 bpf_error("unknown data link type %d", linktype
);
1276 * Load a value relative to the beginning of the link-layer header.
1277 * The link-layer header doesn't necessarily begin at the beginning
1278 * of the packet data; there might be a variable-length prefix containing
1279 * radio information.
1281 static struct slist
*
1282 gen_load_llrel(offset
, size
)
1285 struct slist
*s
, *s2
;
1287 s
= gen_llprefixlen();
1290 * If "s" is non-null, it has code to arrange that the X register
1291 * contains the length of the prefix preceding the link-layer
1294 * Otherwise, the length of the prefix preceding the link-layer
1295 * header is "off_ll".
1299 * There's a variable-length prefix preceding the
1300 * link-layer header. "s" points to a list of statements
1301 * that put the length of that prefix into the X register.
1302 * do an indirect load, to use the X register as an offset.
1304 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1309 * There is no variable-length header preceding the
1310 * link-layer header; add in off_ll, which, if there's
1311 * a fixed-length header preceding the link-layer header,
1312 * is the length of that header.
1314 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1315 s
->s
.k
= offset
+ off_ll
;
1321 * Load a value relative to the beginning of the specified header.
1323 static struct slist
*
1324 gen_load_a(offrel
, offset
, size
)
1325 enum e_offrel offrel
;
1328 struct slist
*s
, *s2
;
1333 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1338 s
= gen_load_llrel(offset
, size
);
1342 s
= gen_load_llrel(off_nl
+ offset
, size
);
1346 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1351 * Load the X register with the length of the IPv4 header
1352 * (plus the offset of the link-layer header, if it's
1353 * preceded by a variable-length header such as a radio
1354 * header), in bytes.
1356 s
= gen_loadx_iphdrlen();
1359 * Load the item at {offset of the link-layer header} +
1360 * {offset, relative to the start of the link-layer
1361 * header, of the IPv4 header} + {length of the IPv4 header} +
1362 * {specified offset}.
1364 * (If the link-layer is variable-length, it's included
1365 * in the value in the X register, and off_ll is 0.)
1367 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1368 s2
->s
.k
= off_ll
+ off_nl
+ offset
;
1373 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1384 * Generate code to load into the X register the sum of the length of
1385 * the IPv4 header and any variable-length header preceding the link-layer
1388 static struct slist
*
1389 gen_loadx_iphdrlen()
1391 struct slist
*s
, *s2
;
1393 s
= gen_llprefixlen();
1396 * There's a variable-length prefix preceding the
1397 * link-layer header. "s" points to a list of statements
1398 * that put the length of that prefix into the X register.
1399 * The 4*([k]&0xf) addressing mode can't be used, as we
1400 * don't have a constant offset, so we have to load the
1401 * value in question into the A register and add to it
1402 * the value from the X register.
1404 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1407 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1410 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1415 * The A register now contains the length of the
1416 * IP header. We need to add to it the length
1417 * of the prefix preceding the link-layer
1418 * header, which is still in the X register, and
1419 * move the result into the X register.
1421 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1422 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1425 * There is no variable-length header preceding the
1426 * link-layer header; add in off_ll, which, if there's
1427 * a fixed-length header preceding the link-layer header,
1428 * is the length of that header.
1430 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1431 s
->s
.k
= off_ll
+ off_nl
;
1436 static struct block
*
1443 s
= new_stmt(BPF_LD
|BPF_IMM
);
1445 b
= new_block(JMP(BPF_JEQ
));
1451 static inline struct block
*
1454 return gen_uncond(1);
1457 static inline struct block
*
1460 return gen_uncond(0);
1464 * Byte-swap a 32-bit number.
1465 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1466 * big-endian platforms.)
1468 #define SWAPLONG(y) \
1469 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1472 * Generate code to match a particular packet type.
1474 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1475 * value, if <= ETHERMTU. We use that to determine whether to
1476 * match the type/length field or to check the type/length field for
1477 * a value <= ETHERMTU to see whether it's a type field and then do
1478 * the appropriate test.
1480 static struct block
*
1481 gen_ether_linktype(proto
)
1484 struct block
*b0
, *b1
;
1490 case LLCSAP_NETBEUI
:
1492 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1493 * so we check the DSAP and SSAP.
1495 * LLCSAP_IP checks for IP-over-802.2, rather
1496 * than IP-over-Ethernet or IP-over-SNAP.
1498 * XXX - should we check both the DSAP and the
1499 * SSAP, like this, or should we check just the
1500 * DSAP, as we do for other types <= ETHERMTU
1501 * (i.e., other SAP values)?
1503 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1505 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1506 ((proto
<< 8) | proto
));
1514 * Ethernet_II frames, which are Ethernet
1515 * frames with a frame type of ETHERTYPE_IPX;
1517 * Ethernet_802.3 frames, which are 802.3
1518 * frames (i.e., the type/length field is
1519 * a length field, <= ETHERMTU, rather than
1520 * a type field) with the first two bytes
1521 * after the Ethernet/802.3 header being
1524 * Ethernet_802.2 frames, which are 802.3
1525 * frames with an 802.2 LLC header and
1526 * with the IPX LSAP as the DSAP in the LLC
1529 * Ethernet_SNAP frames, which are 802.3
1530 * frames with an LLC header and a SNAP
1531 * header and with an OUI of 0x000000
1532 * (encapsulated Ethernet) and a protocol
1533 * ID of ETHERTYPE_IPX in the SNAP header.
1535 * XXX - should we generate the same code both
1536 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1540 * This generates code to check both for the
1541 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1543 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1544 (bpf_int32
)LLCSAP_IPX
);
1545 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1550 * Now we add code to check for SNAP frames with
1551 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1553 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1557 * Now we generate code to check for 802.3
1558 * frames in general.
1560 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1564 * Now add the check for 802.3 frames before the
1565 * check for Ethernet_802.2 and Ethernet_802.3,
1566 * as those checks should only be done on 802.3
1567 * frames, not on Ethernet frames.
1572 * Now add the check for Ethernet_II frames, and
1573 * do that before checking for the other frame
1576 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1577 (bpf_int32
)ETHERTYPE_IPX
);
1581 case ETHERTYPE_ATALK
:
1582 case ETHERTYPE_AARP
:
1584 * EtherTalk (AppleTalk protocols on Ethernet link
1585 * layer) may use 802.2 encapsulation.
1589 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1590 * we check for an Ethernet type field less than
1591 * 1500, which means it's an 802.3 length field.
1593 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1597 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1598 * SNAP packets with an organization code of
1599 * 0x080007 (Apple, for Appletalk) and a protocol
1600 * type of ETHERTYPE_ATALK (Appletalk).
1602 * 802.2-encapsulated ETHERTYPE_AARP packets are
1603 * SNAP packets with an organization code of
1604 * 0x000000 (encapsulated Ethernet) and a protocol
1605 * type of ETHERTYPE_AARP (Appletalk ARP).
1607 if (proto
== ETHERTYPE_ATALK
)
1608 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1609 else /* proto == ETHERTYPE_AARP */
1610 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1614 * Check for Ethernet encapsulation (Ethertalk
1615 * phase 1?); we just check for the Ethernet
1618 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1624 if (proto
<= ETHERMTU
) {
1626 * This is an LLC SAP value, so the frames
1627 * that match would be 802.2 frames.
1628 * Check that the frame is an 802.2 frame
1629 * (i.e., that the length/type field is
1630 * a length field, <= ETHERMTU) and
1631 * then check the DSAP.
1633 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1635 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1641 * This is an Ethernet type, so compare
1642 * the length/type field with it (if
1643 * the frame is an 802.2 frame, the length
1644 * field will be <= ETHERMTU, and, as
1645 * "proto" is > ETHERMTU, this test
1646 * will fail and the frame won't match,
1647 * which is what we want).
1649 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1656 * Generate code to match a particular packet type.
1658 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1659 * value, if <= ETHERMTU. We use that to determine whether to
1660 * match the type field or to check the type field for the special
1661 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1663 static struct block
*
1664 gen_linux_sll_linktype(proto
)
1667 struct block
*b0
, *b1
;
1673 case LLCSAP_NETBEUI
:
1675 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1676 * so we check the DSAP and SSAP.
1678 * LLCSAP_IP checks for IP-over-802.2, rather
1679 * than IP-over-Ethernet or IP-over-SNAP.
1681 * XXX - should we check both the DSAP and the
1682 * SSAP, like this, or should we check just the
1683 * DSAP, as we do for other types <= ETHERMTU
1684 * (i.e., other SAP values)?
1686 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1687 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1688 ((proto
<< 8) | proto
));
1694 * Ethernet_II frames, which are Ethernet
1695 * frames with a frame type of ETHERTYPE_IPX;
1697 * Ethernet_802.3 frames, which have a frame
1698 * type of LINUX_SLL_P_802_3;
1700 * Ethernet_802.2 frames, which are 802.3
1701 * frames with an 802.2 LLC header (i.e, have
1702 * a frame type of LINUX_SLL_P_802_2) and
1703 * with the IPX LSAP as the DSAP in the LLC
1706 * Ethernet_SNAP frames, which are 802.3
1707 * frames with an LLC header and a SNAP
1708 * header and with an OUI of 0x000000
1709 * (encapsulated Ethernet) and a protocol
1710 * ID of ETHERTYPE_IPX in the SNAP header.
1712 * First, do the checks on LINUX_SLL_P_802_2
1713 * frames; generate the check for either
1714 * Ethernet_802.2 or Ethernet_SNAP frames, and
1715 * then put a check for LINUX_SLL_P_802_2 frames
1718 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1719 (bpf_int32
)LLCSAP_IPX
);
1720 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1723 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1727 * Now check for 802.3 frames and OR that with
1728 * the previous test.
1730 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1734 * Now add the check for Ethernet_II frames, and
1735 * do that before checking for the other frame
1738 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1739 (bpf_int32
)ETHERTYPE_IPX
);
1743 case ETHERTYPE_ATALK
:
1744 case ETHERTYPE_AARP
:
1746 * EtherTalk (AppleTalk protocols on Ethernet link
1747 * layer) may use 802.2 encapsulation.
1751 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1752 * we check for the 802.2 protocol type in the
1753 * "Ethernet type" field.
1755 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1758 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1759 * SNAP packets with an organization code of
1760 * 0x080007 (Apple, for Appletalk) and a protocol
1761 * type of ETHERTYPE_ATALK (Appletalk).
1763 * 802.2-encapsulated ETHERTYPE_AARP packets are
1764 * SNAP packets with an organization code of
1765 * 0x000000 (encapsulated Ethernet) and a protocol
1766 * type of ETHERTYPE_AARP (Appletalk ARP).
1768 if (proto
== ETHERTYPE_ATALK
)
1769 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1771 else /* proto == ETHERTYPE_AARP */
1772 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1777 * Check for Ethernet encapsulation (Ethertalk
1778 * phase 1?); we just check for the Ethernet
1781 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1787 if (proto
<= ETHERMTU
) {
1789 * This is an LLC SAP value, so the frames
1790 * that match would be 802.2 frames.
1791 * Check for the 802.2 protocol type
1792 * in the "Ethernet type" field, and
1793 * then check the DSAP.
1795 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1797 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1803 * This is an Ethernet type, so compare
1804 * the length/type field with it (if
1805 * the frame is an 802.2 frame, the length
1806 * field will be <= ETHERMTU, and, as
1807 * "proto" is > ETHERMTU, this test
1808 * will fail and the frame won't match,
1809 * which is what we want).
1811 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1818 insert_radiotap_load_llprefixlen(b
)
1821 struct slist
*s1
, *s2
;
1824 * Prepend to the statements in this block code to load the
1825 * length of the radiotap header into the register assigned
1826 * to hold that length, if one has been assigned.
1828 if (reg_ll_size
!= -1) {
1830 * The 2 bytes at offsets of 2 and 3 from the beginning
1831 * of the radiotap header are the length of the radiotap
1832 * header; unfortunately, it's little-endian, so we have
1833 * to load it a byte at a time and construct the value.
1837 * Load the high-order byte, at an offset of 3, shift it
1838 * left a byte, and put the result in the X register.
1840 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1842 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1845 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1849 * Load the next byte, at an offset of 2, and OR the
1850 * value from the X register into it.
1852 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1855 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1859 * Now allocate a register to hold that value and store
1862 s2
= new_stmt(BPF_ST
);
1863 s2
->s
.k
= reg_ll_size
;
1867 * Now move it into the X register.
1869 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1873 * Now append all the existing statements in this
1874 * block to these statements.
1876 sappend(s1
, b
->stmts
);
1883 insert_load_llprefixlen(b
)
1888 case DLT_IEEE802_11_RADIO
:
1889 insert_radiotap_load_llprefixlen(b
);
1894 static struct slist
*
1895 gen_radiotap_llprefixlen(void)
1899 if (reg_ll_size
== -1) {
1901 * We haven't yet assigned a register for the length
1902 * of the radiotap header; allocate one.
1904 reg_ll_size
= alloc_reg();
1908 * Load the register containing the radiotap length
1909 * into the X register.
1911 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1912 s
->s
.k
= reg_ll_size
;
1917 * Generate code to compute the link-layer header length, if necessary,
1918 * putting it into the X register, and to return either a pointer to a
1919 * "struct slist" for the list of statements in that code, or NULL if
1920 * no code is necessary.
1922 static struct slist
*
1923 gen_llprefixlen(void)
1927 case DLT_IEEE802_11_RADIO
:
1928 return gen_radiotap_llprefixlen();
1936 * Generate code to match a particular packet type by matching the
1937 * link-layer type field or fields in the 802.2 LLC header.
1939 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1940 * value, if <= ETHERMTU.
1942 static struct block
*
1946 struct block
*b0
, *b1
, *b2
;
1948 /* are we checking MPLS-encapsulated packets? */
1949 if (label_stack_depth
> 0) {
1953 /* FIXME add other L3 proto IDs */
1954 return gen_mpls_linktype(Q_IP
);
1956 case ETHERTYPE_IPV6
:
1958 /* FIXME add other L3 proto IDs */
1959 return gen_mpls_linktype(Q_IPV6
);
1962 bpf_error("unsupported protocol over mpls");
1970 return gen_ether_linktype(proto
);
1978 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1982 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1991 case DLT_IEEE802_11
:
1992 case DLT_IEEE802_11_RADIO_AVS
:
1993 case DLT_IEEE802_11_RADIO
:
1994 case DLT_PRISM_HEADER
:
1995 case DLT_ATM_RFC1483
:
1997 case DLT_IP_OVER_FC
:
1998 return gen_llc_linktype(proto
);
2004 * If "is_lane" is set, check for a LANE-encapsulated
2005 * version of this protocol, otherwise check for an
2006 * LLC-encapsulated version of this protocol.
2008 * We assume LANE means Ethernet, not Token Ring.
2012 * Check that the packet doesn't begin with an
2013 * LE Control marker. (We've already generated
2016 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2021 * Now generate an Ethernet test.
2023 b1
= gen_ether_linktype(proto
);
2028 * Check for LLC encapsulation and then check the
2031 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2032 b1
= gen_llc_linktype(proto
);
2040 return gen_linux_sll_linktype(proto
);
2045 case DLT_SLIP_BSDOS
:
2048 * These types don't provide any type field; packets
2049 * are always IPv4 or IPv6.
2051 * XXX - for IPv4, check for a version number of 4, and,
2052 * for IPv6, check for a version number of 6?
2057 /* Check for a version number of 4. */
2058 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2060 case ETHERTYPE_IPV6
:
2061 /* Check for a version number of 6. */
2062 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2066 return gen_false(); /* always false */
2073 case DLT_PPP_SERIAL
:
2076 * We use Ethernet protocol types inside libpcap;
2077 * map them to the corresponding PPP protocol types.
2086 case ETHERTYPE_IPV6
:
2095 case ETHERTYPE_ATALK
:
2109 * I'm assuming the "Bridging PDU"s that go
2110 * over PPP are Spanning Tree Protocol
2124 * We use Ethernet protocol types inside libpcap;
2125 * map them to the corresponding PPP protocol types.
2130 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2131 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2133 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2138 case ETHERTYPE_IPV6
:
2148 case ETHERTYPE_ATALK
:
2162 * I'm assuming the "Bridging PDU"s that go
2163 * over PPP are Spanning Tree Protocol
2179 * For DLT_NULL, the link-layer header is a 32-bit
2180 * word containing an AF_ value in *host* byte order,
2181 * and for DLT_ENC, the link-layer header begins
2182 * with a 32-bit work containing an AF_ value in
2185 * In addition, if we're reading a saved capture file,
2186 * the host byte order in the capture may not be the
2187 * same as the host byte order on this machine.
2189 * For DLT_LOOP, the link-layer header is a 32-bit
2190 * word containing an AF_ value in *network* byte order.
2192 * XXX - AF_ values may, unfortunately, be platform-
2193 * dependent; for example, FreeBSD's AF_INET6 is 24
2194 * whilst NetBSD's and OpenBSD's is 26.
2196 * This means that, when reading a capture file, just
2197 * checking for our AF_INET6 value won't work if the
2198 * capture file came from another OS.
2207 case ETHERTYPE_IPV6
:
2214 * Not a type on which we support filtering.
2215 * XXX - support those that have AF_ values
2216 * #defined on this platform, at least?
2221 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2223 * The AF_ value is in host byte order, but
2224 * the BPF interpreter will convert it to
2225 * network byte order.
2227 * If this is a save file, and it's from a
2228 * machine with the opposite byte order to
2229 * ours, we byte-swap the AF_ value.
2231 * Then we run it through "htonl()", and
2232 * generate code to compare against the result.
2234 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2235 bpf_pcap
->sf
.swapped
)
2236 proto
= SWAPLONG(proto
);
2237 proto
= htonl(proto
);
2239 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2243 * af field is host byte order in contrast to the rest of
2246 if (proto
== ETHERTYPE_IP
)
2247 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2248 BPF_B
, (bpf_int32
)AF_INET
));
2250 else if (proto
== ETHERTYPE_IPV6
)
2251 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2252 BPF_B
, (bpf_int32
)AF_INET6
));
2260 case DLT_ARCNET_LINUX
:
2262 * XXX should we check for first fragment if the protocol
2271 case ETHERTYPE_IPV6
:
2272 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2273 (bpf_int32
)ARCTYPE_INET6
));
2277 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2278 (bpf_int32
)ARCTYPE_IP
);
2279 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2280 (bpf_int32
)ARCTYPE_IP_OLD
);
2285 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2286 (bpf_int32
)ARCTYPE_ARP
);
2287 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2288 (bpf_int32
)ARCTYPE_ARP_OLD
);
2292 case ETHERTYPE_REVARP
:
2293 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2294 (bpf_int32
)ARCTYPE_REVARP
));
2296 case ETHERTYPE_ATALK
:
2297 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2298 (bpf_int32
)ARCTYPE_ATALK
));
2305 case ETHERTYPE_ATALK
:
2315 * XXX - assumes a 2-byte Frame Relay header with
2316 * DLCI and flags. What if the address is longer?
2322 * Check for the special NLPID for IP.
2324 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2327 case ETHERTYPE_IPV6
:
2329 * Check for the special NLPID for IPv6.
2331 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2336 * Check for several OSI protocols.
2338 * Frame Relay packets typically have an OSI
2339 * NLPID at the beginning; we check for each
2342 * What we check for is the NLPID and a frame
2343 * control field of UI, i.e. 0x03 followed
2346 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2347 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2348 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2359 case DLT_JUNIPER_MFR
:
2360 case DLT_JUNIPER_MLFR
:
2361 case DLT_JUNIPER_MLPPP
:
2362 case DLT_JUNIPER_ATM1
:
2363 case DLT_JUNIPER_ATM2
:
2364 case DLT_JUNIPER_PPPOE
:
2365 case DLT_JUNIPER_PPPOE_ATM
:
2366 case DLT_JUNIPER_GGSN
:
2367 case DLT_JUNIPER_ES
:
2368 case DLT_JUNIPER_MONITOR
:
2369 case DLT_JUNIPER_SERVICES
:
2370 case DLT_JUNIPER_ETHER
:
2371 case DLT_JUNIPER_PPP
:
2372 case DLT_JUNIPER_FRELAY
:
2373 case DLT_JUNIPER_CHDLC
:
2374 case DLT_JUNIPER_VP
:
2375 /* just lets verify the magic number for now -
2376 * on ATM we may have up to 6 different encapsulations on the wire
2377 * and need a lot of heuristics to figure out that the payload
2380 * FIXME encapsulation specific BPF_ filters
2382 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2384 case DLT_LINUX_IRDA
:
2385 bpf_error("IrDA link-layer type filtering not implemented");
2388 bpf_error("DOCSIS link-layer type filtering not implemented");
2390 case DLT_LINUX_LAPD
:
2391 bpf_error("LAPD link-layer type filtering not implemented");
2395 * All the types that have no encapsulation should either be
2396 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2397 * all packets are IP packets, or should be handled in some
2398 * special case, if none of them are (if some are and some
2399 * aren't, the lack of encapsulation is a problem, as we'd
2400 * have to find some other way of determining the packet type).
2402 * Therefore, if "off_linktype" is -1, there's an error.
2404 if (off_linktype
== (u_int
)-1)
2408 * Any type not handled above should always have an Ethernet
2409 * type at an offset of "off_linktype". (PPP is partially
2410 * handled above - the protocol type is mapped from the
2411 * Ethernet and LLC types we use internally to the corresponding
2412 * PPP type - but the PPP type is always specified by a value
2413 * at "off_linktype", so we don't have to do the code generation
2416 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2420 * Check for an LLC SNAP packet with a given organization code and
2421 * protocol type; we check the entire contents of the 802.2 LLC and
2422 * snap headers, checking for DSAP and SSAP of SNAP and a control
2423 * field of 0x03 in the LLC header, and for the specified organization
2424 * code and protocol type in the SNAP header.
2426 static struct block
*
2427 gen_snap(orgcode
, ptype
, offset
)
2428 bpf_u_int32 orgcode
;
2432 u_char snapblock
[8];
2434 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2435 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2436 snapblock
[2] = 0x03; /* control = UI */
2437 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2438 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2439 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2440 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2441 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2442 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2446 * Generate code to match a particular packet type, for link-layer types
2447 * using 802.2 LLC headers.
2449 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2450 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2452 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2453 * value, if <= ETHERMTU. We use that to determine whether to
2454 * match the DSAP or both DSAP and LSAP or to check the OUI and
2455 * protocol ID in a SNAP header.
2457 static struct block
*
2458 gen_llc_linktype(proto
)
2462 * XXX - handle token-ring variable-length header.
2468 case LLCSAP_NETBEUI
:
2470 * XXX - should we check both the DSAP and the
2471 * SSAP, like this, or should we check just the
2472 * DSAP, as we do for other types <= ETHERMTU
2473 * (i.e., other SAP values)?
2475 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2476 ((proto
<< 8) | proto
));
2480 * XXX - are there ever SNAP frames for IPX on
2481 * non-Ethernet 802.x networks?
2483 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2484 (bpf_int32
)LLCSAP_IPX
);
2486 case ETHERTYPE_ATALK
:
2488 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2489 * SNAP packets with an organization code of
2490 * 0x080007 (Apple, for Appletalk) and a protocol
2491 * type of ETHERTYPE_ATALK (Appletalk).
2493 * XXX - check for an organization code of
2494 * encapsulated Ethernet as well?
2496 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2500 * XXX - we don't have to check for IPX 802.3
2501 * here, but should we check for the IPX Ethertype?
2503 if (proto
<= ETHERMTU
) {
2505 * This is an LLC SAP value, so check
2508 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2512 * This is an Ethernet type; we assume that it's
2513 * unlikely that it'll appear in the right place
2514 * at random, and therefore check only the
2515 * location that would hold the Ethernet type
2516 * in a SNAP frame with an organization code of
2517 * 0x000000 (encapsulated Ethernet).
2519 * XXX - if we were to check for the SNAP DSAP and
2520 * LSAP, as per XXX, and were also to check for an
2521 * organization code of 0x000000 (encapsulated
2522 * Ethernet), we'd do
2524 * return gen_snap(0x000000, proto,
2527 * here; for now, we don't, as per the above.
2528 * I don't know whether it's worth the extra CPU
2529 * time to do the right check or not.
2531 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2537 static struct block
*
2538 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2542 u_int src_off
, dst_off
;
2544 struct block
*b0
, *b1
;
2558 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2559 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2565 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2566 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2573 b0
= gen_linktype(proto
);
2574 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2580 static struct block
*
2581 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2582 struct in6_addr
*addr
;
2583 struct in6_addr
*mask
;
2585 u_int src_off
, dst_off
;
2587 struct block
*b0
, *b1
;
2602 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2603 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2609 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2610 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2617 /* this order is important */
2618 a
= (u_int32_t
*)addr
;
2619 m
= (u_int32_t
*)mask
;
2620 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2621 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2623 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2625 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2627 b0
= gen_linktype(proto
);
2633 static struct block
*
2634 gen_ehostop(eaddr
, dir
)
2635 register const u_char
*eaddr
;
2638 register struct block
*b0
, *b1
;
2642 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2645 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2648 b0
= gen_ehostop(eaddr
, Q_SRC
);
2649 b1
= gen_ehostop(eaddr
, Q_DST
);
2655 b0
= gen_ehostop(eaddr
, Q_SRC
);
2656 b1
= gen_ehostop(eaddr
, Q_DST
);
2665 * Like gen_ehostop, but for DLT_FDDI
2667 static struct block
*
2668 gen_fhostop(eaddr
, dir
)
2669 register const u_char
*eaddr
;
2672 struct block
*b0
, *b1
;
2677 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2679 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2684 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2686 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2690 b0
= gen_fhostop(eaddr
, Q_SRC
);
2691 b1
= gen_fhostop(eaddr
, Q_DST
);
2697 b0
= gen_fhostop(eaddr
, Q_SRC
);
2698 b1
= gen_fhostop(eaddr
, Q_DST
);
2707 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2709 static struct block
*
2710 gen_thostop(eaddr
, dir
)
2711 register const u_char
*eaddr
;
2714 register struct block
*b0
, *b1
;
2718 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2721 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2724 b0
= gen_thostop(eaddr
, Q_SRC
);
2725 b1
= gen_thostop(eaddr
, Q_DST
);
2731 b0
= gen_thostop(eaddr
, Q_SRC
);
2732 b1
= gen_thostop(eaddr
, Q_DST
);
2741 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2743 static struct block
*
2744 gen_wlanhostop(eaddr
, dir
)
2745 register const u_char
*eaddr
;
2748 register struct block
*b0
, *b1
, *b2
;
2749 register struct slist
*s
;
2756 * For control frames, there is no SA.
2758 * For management frames, SA is at an
2759 * offset of 10 from the beginning of
2762 * For data frames, SA is at an offset
2763 * of 10 from the beginning of the packet
2764 * if From DS is clear, at an offset of
2765 * 16 from the beginning of the packet
2766 * if From DS is set and To DS is clear,
2767 * and an offset of 24 from the beginning
2768 * of the packet if From DS is set and To DS
2773 * Generate the tests to be done for data frames
2776 * First, check for To DS set, i.e. check "link[1] & 0x01".
2778 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2779 b1
= new_block(JMP(BPF_JSET
));
2780 b1
->s
.k
= 0x01; /* To DS */
2784 * If To DS is set, the SA is at 24.
2786 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2790 * Now, check for To DS not set, i.e. check
2791 * "!(link[1] & 0x01)".
2793 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2794 b2
= new_block(JMP(BPF_JSET
));
2795 b2
->s
.k
= 0x01; /* To DS */
2800 * If To DS is not set, the SA is at 16.
2802 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2806 * Now OR together the last two checks. That gives
2807 * the complete set of checks for data frames with
2813 * Now check for From DS being set, and AND that with
2814 * the ORed-together checks.
2816 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2817 b1
= new_block(JMP(BPF_JSET
));
2818 b1
->s
.k
= 0x02; /* From DS */
2823 * Now check for data frames with From DS not set.
2825 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2826 b2
= new_block(JMP(BPF_JSET
));
2827 b2
->s
.k
= 0x02; /* From DS */
2832 * If From DS isn't set, the SA is at 10.
2834 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2838 * Now OR together the checks for data frames with
2839 * From DS not set and for data frames with From DS
2840 * set; that gives the checks done for data frames.
2845 * Now check for a data frame.
2846 * I.e, check "link[0] & 0x08".
2848 gen_load_a(OR_LINK
, 0, BPF_B
);
2849 b1
= new_block(JMP(BPF_JSET
));
2854 * AND that with the checks done for data frames.
2859 * If the high-order bit of the type value is 0, this
2860 * is a management frame.
2861 * I.e, check "!(link[0] & 0x08)".
2863 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2864 b2
= new_block(JMP(BPF_JSET
));
2870 * For management frames, the SA is at 10.
2872 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2876 * OR that with the checks done for data frames.
2877 * That gives the checks done for management and
2883 * If the low-order bit of the type value is 1,
2884 * this is either a control frame or a frame
2885 * with a reserved type, and thus not a
2888 * I.e., check "!(link[0] & 0x04)".
2890 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2891 b1
= new_block(JMP(BPF_JSET
));
2897 * AND that with the checks for data and management
2907 * For control frames, there is no DA.
2909 * For management frames, DA is at an
2910 * offset of 4 from the beginning of
2913 * For data frames, DA is at an offset
2914 * of 4 from the beginning of the packet
2915 * if To DS is clear and at an offset of
2916 * 16 from the beginning of the packet
2921 * Generate the tests to be done for data frames.
2923 * First, check for To DS set, i.e. "link[1] & 0x01".
2925 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2926 b1
= new_block(JMP(BPF_JSET
));
2927 b1
->s
.k
= 0x01; /* To DS */
2931 * If To DS is set, the DA is at 16.
2933 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2937 * Now, check for To DS not set, i.e. check
2938 * "!(link[1] & 0x01)".
2940 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2941 b2
= new_block(JMP(BPF_JSET
));
2942 b2
->s
.k
= 0x01; /* To DS */
2947 * If To DS is not set, the DA is at 4.
2949 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2953 * Now OR together the last two checks. That gives
2954 * the complete set of checks for data frames.
2959 * Now check for a data frame.
2960 * I.e, check "link[0] & 0x08".
2962 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2963 b1
= new_block(JMP(BPF_JSET
));
2968 * AND that with the checks done for data frames.
2973 * If the high-order bit of the type value is 0, this
2974 * is a management frame.
2975 * I.e, check "!(link[0] & 0x08)".
2977 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2978 b2
= new_block(JMP(BPF_JSET
));
2984 * For management frames, the DA is at 4.
2986 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2990 * OR that with the checks done for data frames.
2991 * That gives the checks done for management and
2997 * If the low-order bit of the type value is 1,
2998 * this is either a control frame or a frame
2999 * with a reserved type, and thus not a
3002 * I.e., check "!(link[0] & 0x04)".
3004 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3005 b1
= new_block(JMP(BPF_JSET
));
3011 * AND that with the checks for data and management
3018 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3019 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3025 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3026 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3035 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3036 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3037 * as the RFC states.)
3039 static struct block
*
3040 gen_ipfchostop(eaddr
, dir
)
3041 register const u_char
*eaddr
;
3044 register struct block
*b0
, *b1
;
3048 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3051 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3054 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3055 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3061 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3062 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3071 * This is quite tricky because there may be pad bytes in front of the
3072 * DECNET header, and then there are two possible data packet formats that
3073 * carry both src and dst addresses, plus 5 packet types in a format that
3074 * carries only the src node, plus 2 types that use a different format and
3075 * also carry just the src node.
3079 * Instead of doing those all right, we just look for data packets with
3080 * 0 or 1 bytes of padding. If you want to look at other packets, that
3081 * will require a lot more hacking.
3083 * To add support for filtering on DECNET "areas" (network numbers)
3084 * one would want to add a "mask" argument to this routine. That would
3085 * make the filter even more inefficient, although one could be clever
3086 * and not generate masking instructions if the mask is 0xFFFF.
3088 static struct block
*
3089 gen_dnhostop(addr
, dir
)
3093 struct block
*b0
, *b1
, *b2
, *tmp
;
3094 u_int offset_lh
; /* offset if long header is received */
3095 u_int offset_sh
; /* offset if short header is received */
3100 offset_sh
= 1; /* follows flags */
3101 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3105 offset_sh
= 3; /* follows flags, dstnode */
3106 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3110 /* Inefficient because we do our Calvinball dance twice */
3111 b0
= gen_dnhostop(addr
, Q_SRC
);
3112 b1
= gen_dnhostop(addr
, Q_DST
);
3118 /* Inefficient because we do our Calvinball dance twice */
3119 b0
= gen_dnhostop(addr
, Q_SRC
);
3120 b1
= gen_dnhostop(addr
, Q_DST
);
3125 bpf_error("ISO host filtering not implemented");
3130 b0
= gen_linktype(ETHERTYPE_DN
);
3131 /* Check for pad = 1, long header case */
3132 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3133 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3134 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3135 BPF_H
, (bpf_int32
)ntohs(addr
));
3137 /* Check for pad = 0, long header case */
3138 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3139 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3142 /* Check for pad = 1, short header case */
3143 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3144 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3145 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3148 /* Check for pad = 0, short header case */
3149 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3150 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3154 /* Combine with test for linktype */
3160 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3161 * test the bottom-of-stack bit, and then check the version number
3162 * field in the IP header.
3164 static struct block
*
3165 gen_mpls_linktype(proto
)
3168 struct block
*b0
, *b1
;
3173 /* match the bottom-of-stack bit */
3174 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3175 /* match the IPv4 version number */
3176 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3181 /* match the bottom-of-stack bit */
3182 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3183 /* match the IPv4 version number */
3184 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3193 static struct block
*
3194 gen_host(addr
, mask
, proto
, dir
, type
)
3201 struct block
*b0
, *b1
;
3202 const char *typestr
;
3212 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3214 * Only check for non-IPv4 addresses if we're not
3215 * checking MPLS-encapsulated packets.
3217 if (label_stack_depth
== 0) {
3218 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3220 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3226 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3229 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3232 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3235 bpf_error("'tcp' modifier applied to %s", typestr
);
3238 bpf_error("'sctp' modifier applied to %s", typestr
);
3241 bpf_error("'udp' modifier applied to %s", typestr
);
3244 bpf_error("'icmp' modifier applied to %s", typestr
);
3247 bpf_error("'igmp' modifier applied to %s", typestr
);
3250 bpf_error("'igrp' modifier applied to %s", typestr
);
3253 bpf_error("'pim' modifier applied to %s", typestr
);
3256 bpf_error("'vrrp' modifier applied to %s", typestr
);
3259 bpf_error("ATALK host filtering not implemented");
3262 bpf_error("AARP host filtering not implemented");
3265 return gen_dnhostop(addr
, dir
);
3268 bpf_error("SCA host filtering not implemented");
3271 bpf_error("LAT host filtering not implemented");
3274 bpf_error("MOPDL host filtering not implemented");
3277 bpf_error("MOPRC host filtering not implemented");
3281 bpf_error("'ip6' modifier applied to ip host");
3284 bpf_error("'icmp6' modifier applied to %s", typestr
);
3288 bpf_error("'ah' modifier applied to %s", typestr
);
3291 bpf_error("'esp' modifier applied to %s", typestr
);
3294 bpf_error("ISO host filtering not implemented");
3297 bpf_error("'esis' modifier applied to %s", typestr
);
3300 bpf_error("'isis' modifier applied to %s", typestr
);
3303 bpf_error("'clnp' modifier applied to %s", typestr
);
3306 bpf_error("'stp' modifier applied to %s", typestr
);
3309 bpf_error("IPX host filtering not implemented");
3312 bpf_error("'netbeui' modifier applied to %s", typestr
);
3315 bpf_error("'radio' modifier applied to %s", typestr
);
3324 static struct block
*
3325 gen_host6(addr
, mask
, proto
, dir
, type
)
3326 struct in6_addr
*addr
;
3327 struct in6_addr
*mask
;
3332 const char *typestr
;
3342 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3345 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3348 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3351 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3354 bpf_error("'sctp' modifier applied to %s", typestr
);
3357 bpf_error("'tcp' modifier applied to %s", typestr
);
3360 bpf_error("'udp' modifier applied to %s", typestr
);
3363 bpf_error("'icmp' modifier applied to %s", typestr
);
3366 bpf_error("'igmp' modifier applied to %s", typestr
);
3369 bpf_error("'igrp' modifier applied to %s", typestr
);
3372 bpf_error("'pim' modifier applied to %s", typestr
);
3375 bpf_error("'vrrp' modifier applied to %s", typestr
);
3378 bpf_error("ATALK host filtering not implemented");
3381 bpf_error("AARP host filtering not implemented");
3384 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3387 bpf_error("SCA host filtering not implemented");
3390 bpf_error("LAT host filtering not implemented");
3393 bpf_error("MOPDL host filtering not implemented");
3396 bpf_error("MOPRC host filtering not implemented");
3399 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3402 bpf_error("'icmp6' modifier applied to %s", typestr
);
3405 bpf_error("'ah' modifier applied to %s", typestr
);
3408 bpf_error("'esp' modifier applied to %s", typestr
);
3411 bpf_error("ISO host filtering not implemented");
3414 bpf_error("'esis' modifier applied to %s", typestr
);
3417 bpf_error("'isis' modifier applied to %s", typestr
);
3420 bpf_error("'clnp' modifier applied to %s", typestr
);
3423 bpf_error("'stp' modifier applied to %s", typestr
);
3426 bpf_error("IPX host filtering not implemented");
3429 bpf_error("'netbeui' modifier applied to %s", typestr
);
3432 bpf_error("'radio' modifier applied to %s", typestr
);
3442 static struct block
*
3443 gen_gateway(eaddr
, alist
, proto
, dir
)
3444 const u_char
*eaddr
;
3445 bpf_u_int32
**alist
;
3449 struct block
*b0
, *b1
, *tmp
;
3452 bpf_error("direction applied to 'gateway'");
3461 b0
= gen_ehostop(eaddr
, Q_OR
);
3464 b0
= gen_fhostop(eaddr
, Q_OR
);
3467 b0
= gen_thostop(eaddr
, Q_OR
);
3469 case DLT_IEEE802_11
:
3470 case DLT_IEEE802_11_RADIO_AVS
:
3471 case DLT_IEEE802_11_RADIO
:
3472 case DLT_PRISM_HEADER
:
3473 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3478 * Check that the packet doesn't begin with an
3479 * LE Control marker. (We've already generated
3482 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3487 * Now check the MAC address.
3489 b0
= gen_ehostop(eaddr
, Q_OR
);
3493 case DLT_IP_OVER_FC
:
3494 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3498 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3500 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3502 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3511 bpf_error("illegal modifier of 'gateway'");
3517 gen_proto_abbrev(proto
)
3526 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3528 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3534 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3536 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3542 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3544 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3550 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3553 #ifndef IPPROTO_IGMP
3554 #define IPPROTO_IGMP 2
3558 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3561 #ifndef IPPROTO_IGRP
3562 #define IPPROTO_IGRP 9
3565 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3569 #define IPPROTO_PIM 103
3573 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3575 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3580 #ifndef IPPROTO_VRRP
3581 #define IPPROTO_VRRP 112
3585 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3589 b1
= gen_linktype(ETHERTYPE_IP
);
3593 b1
= gen_linktype(ETHERTYPE_ARP
);
3597 b1
= gen_linktype(ETHERTYPE_REVARP
);
3601 bpf_error("link layer applied in wrong context");
3604 b1
= gen_linktype(ETHERTYPE_ATALK
);
3608 b1
= gen_linktype(ETHERTYPE_AARP
);
3612 b1
= gen_linktype(ETHERTYPE_DN
);
3616 b1
= gen_linktype(ETHERTYPE_SCA
);
3620 b1
= gen_linktype(ETHERTYPE_LAT
);
3624 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3628 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3633 b1
= gen_linktype(ETHERTYPE_IPV6
);
3636 #ifndef IPPROTO_ICMPV6
3637 #define IPPROTO_ICMPV6 58
3640 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3645 #define IPPROTO_AH 51
3648 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3650 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3656 #define IPPROTO_ESP 50
3659 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3661 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3667 b1
= gen_linktype(LLCSAP_ISONS
);
3671 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3675 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3678 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3679 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3680 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3682 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3684 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3686 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3690 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3691 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3692 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3694 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3696 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3698 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3702 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3703 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3704 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3706 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3711 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3712 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3717 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3718 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3720 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3722 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3727 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3728 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3733 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3734 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3739 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3743 b1
= gen_linktype(LLCSAP_8021D
);
3747 b1
= gen_linktype(LLCSAP_IPX
);
3751 b1
= gen_linktype(LLCSAP_NETBEUI
);
3755 bpf_error("'radio' is not a valid protocol type");
3763 static struct block
*
3770 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3771 b
= new_block(JMP(BPF_JSET
));
3780 * Generate a comparison to a port value in the transport-layer header
3781 * at the specified offset from the beginning of that header.
3783 * XXX - this handles a variable-length prefix preceding the link-layer
3784 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3785 * variable-length link-layer headers (such as Token Ring or 802.11
3788 static struct block
*
3789 gen_portatom(off
, v
)
3793 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3797 static struct block
*
3798 gen_portatom6(off
, v
)
3802 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3807 gen_portop(port
, proto
, dir
)
3808 int port
, proto
, dir
;
3810 struct block
*b0
, *b1
, *tmp
;
3812 /* ip proto 'proto' */
3813 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3819 b1
= gen_portatom(0, (bpf_int32
)port
);
3823 b1
= gen_portatom(2, (bpf_int32
)port
);
3828 tmp
= gen_portatom(0, (bpf_int32
)port
);
3829 b1
= gen_portatom(2, (bpf_int32
)port
);
3834 tmp
= gen_portatom(0, (bpf_int32
)port
);
3835 b1
= gen_portatom(2, (bpf_int32
)port
);
3847 static struct block
*
3848 gen_port(port
, ip_proto
, dir
)
3853 struct block
*b0
, *b1
, *tmp
;
3858 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3859 * not LLC encapsulation with LLCSAP_IP.
3861 * For IEEE 802 networks - which includes 802.5 token ring
3862 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3863 * says that SNAP encapsulation is used, not LLC encapsulation
3866 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3867 * RFC 2225 say that SNAP encapsulation is used, not LLC
3868 * encapsulation with LLCSAP_IP.
3870 * So we always check for ETHERTYPE_IP.
3872 b0
= gen_linktype(ETHERTYPE_IP
);
3878 b1
= gen_portop(port
, ip_proto
, dir
);
3882 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3883 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3885 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3898 gen_portop6(port
, proto
, dir
)
3899 int port
, proto
, dir
;
3901 struct block
*b0
, *b1
, *tmp
;
3903 /* ip6 proto 'proto' */
3904 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3908 b1
= gen_portatom6(0, (bpf_int32
)port
);
3912 b1
= gen_portatom6(2, (bpf_int32
)port
);
3917 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3918 b1
= gen_portatom6(2, (bpf_int32
)port
);
3923 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3924 b1
= gen_portatom6(2, (bpf_int32
)port
);
3936 static struct block
*
3937 gen_port6(port
, ip_proto
, dir
)
3942 struct block
*b0
, *b1
, *tmp
;
3944 /* link proto ip6 */
3945 b0
= gen_linktype(ETHERTYPE_IPV6
);
3951 b1
= gen_portop6(port
, ip_proto
, dir
);
3955 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3956 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3958 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3970 /* gen_portrange code */
3971 static struct block
*
3972 gen_portrangeatom(off
, v1
, v2
)
3976 struct block
*b1
, *b2
;
3980 * Reverse the order of the ports, so v1 is the lower one.
3989 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3990 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3998 gen_portrangeop(port1
, port2
, proto
, dir
)
4003 struct block
*b0
, *b1
, *tmp
;
4005 /* ip proto 'proto' */
4006 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4012 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4016 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4021 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4022 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4027 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4028 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4040 static struct block
*
4041 gen_portrange(port1
, port2
, ip_proto
, dir
)
4046 struct block
*b0
, *b1
, *tmp
;
4049 b0
= gen_linktype(ETHERTYPE_IP
);
4055 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4059 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4060 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4062 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4074 static struct block
*
4075 gen_portrangeatom6(off
, v1
, v2
)
4079 struct block
*b1
, *b2
;
4083 * Reverse the order of the ports, so v1 is the lower one.
4092 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4093 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4101 gen_portrangeop6(port1
, port2
, proto
, dir
)
4106 struct block
*b0
, *b1
, *tmp
;
4108 /* ip6 proto 'proto' */
4109 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4113 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4117 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4122 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4123 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4128 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4129 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4141 static struct block
*
4142 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4147 struct block
*b0
, *b1
, *tmp
;
4149 /* link proto ip6 */
4150 b0
= gen_linktype(ETHERTYPE_IPV6
);
4156 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4160 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4161 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4163 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4176 lookup_proto(name
, proto
)
4177 register const char *name
;
4187 v
= pcap_nametoproto(name
);
4188 if (v
== PROTO_UNDEF
)
4189 bpf_error("unknown ip proto '%s'", name
);
4193 /* XXX should look up h/w protocol type based on linktype */
4194 v
= pcap_nametoeproto(name
);
4195 if (v
== PROTO_UNDEF
) {
4196 v
= pcap_nametollc(name
);
4197 if (v
== PROTO_UNDEF
)
4198 bpf_error("unknown ether proto '%s'", name
);
4203 if (strcmp(name
, "esis") == 0)
4205 else if (strcmp(name
, "isis") == 0)
4207 else if (strcmp(name
, "clnp") == 0)
4210 bpf_error("unknown osi proto '%s'", name
);
4230 static struct block
*
4231 gen_protochain(v
, proto
, dir
)
4236 #ifdef NO_PROTOCHAIN
4237 return gen_proto(v
, proto
, dir
);
4239 struct block
*b0
, *b
;
4240 struct slist
*s
[100];
4241 int fix2
, fix3
, fix4
, fix5
;
4242 int ahcheck
, again
, end
;
4244 int reg2
= alloc_reg();
4246 memset(s
, 0, sizeof(s
));
4247 fix2
= fix3
= fix4
= fix5
= 0;
4254 b0
= gen_protochain(v
, Q_IP
, dir
);
4255 b
= gen_protochain(v
, Q_IPV6
, dir
);
4259 bpf_error("bad protocol applied for 'protochain'");
4264 * We don't handle variable-length radiotap here headers yet.
4265 * We might want to add BPF instructions to do the protochain
4266 * work, to simplify that and, on platforms that have a BPF
4267 * interpreter with the new instructions, let the filtering
4268 * be done in the kernel. (We already require a modified BPF
4269 * engine to do the protochain stuff, to support backward
4270 * branches, and backward branch support is unlikely to appear
4271 * in kernel BPF engines.)
4273 if (linktype
== DLT_IEEE802_11_RADIO
)
4274 bpf_error("'protochain' not supported with radiotap headers");
4276 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4279 * s[0] is a dummy entry to protect other BPF insn from damage
4280 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4281 * hard to find interdependency made by jump table fixup.
4284 s
[i
] = new_stmt(0); /*dummy*/
4289 b0
= gen_linktype(ETHERTYPE_IP
);
4292 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4293 s
[i
]->s
.k
= off_ll
+ off_nl
+ 9;
4295 /* X = ip->ip_hl << 2 */
4296 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4297 s
[i
]->s
.k
= off_ll
+ off_nl
;
4302 b0
= gen_linktype(ETHERTYPE_IPV6
);
4304 /* A = ip6->ip_nxt */
4305 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4306 s
[i
]->s
.k
= off_ll
+ off_nl
+ 6;
4308 /* X = sizeof(struct ip6_hdr) */
4309 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4315 bpf_error("unsupported proto to gen_protochain");
4319 /* again: if (A == v) goto end; else fall through; */
4321 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4323 s
[i
]->s
.jt
= NULL
; /*later*/
4324 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4328 #ifndef IPPROTO_NONE
4329 #define IPPROTO_NONE 59
4331 /* if (A == IPPROTO_NONE) goto end */
4332 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4333 s
[i
]->s
.jt
= NULL
; /*later*/
4334 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4335 s
[i
]->s
.k
= IPPROTO_NONE
;
4336 s
[fix5
]->s
.jf
= s
[i
];
4341 if (proto
== Q_IPV6
) {
4342 int v6start
, v6end
, v6advance
, j
;
4345 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4346 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4347 s
[i
]->s
.jt
= NULL
; /*later*/
4348 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4349 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4350 s
[fix2
]->s
.jf
= s
[i
];
4352 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4353 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4354 s
[i
]->s
.jt
= NULL
; /*later*/
4355 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4356 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4358 /* if (A == IPPROTO_ROUTING) goto v6advance */
4359 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4360 s
[i
]->s
.jt
= NULL
; /*later*/
4361 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4362 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4364 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4365 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4366 s
[i
]->s
.jt
= NULL
; /*later*/
4367 s
[i
]->s
.jf
= NULL
; /*later*/
4368 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4379 * X = X + (P[X + 1] + 1) * 8;
4382 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4384 /* A = P[X + packet head] */
4385 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4386 s
[i
]->s
.k
= off_ll
+ off_nl
;
4389 s
[i
] = new_stmt(BPF_ST
);
4393 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4396 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4400 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4402 /* A = P[X + packet head]; */
4403 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4404 s
[i
]->s
.k
= off_ll
+ off_nl
;
4407 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4411 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4415 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4418 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4422 /* goto again; (must use BPF_JA for backward jump) */
4423 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4424 s
[i
]->s
.k
= again
- i
- 1;
4425 s
[i
- 1]->s
.jf
= s
[i
];
4429 for (j
= v6start
; j
<= v6end
; j
++)
4430 s
[j
]->s
.jt
= s
[v6advance
];
4435 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4437 s
[fix2
]->s
.jf
= s
[i
];
4443 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4444 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4445 s
[i
]->s
.jt
= NULL
; /*later*/
4446 s
[i
]->s
.jf
= NULL
; /*later*/
4447 s
[i
]->s
.k
= IPPROTO_AH
;
4449 s
[fix3
]->s
.jf
= s
[ahcheck
];
4456 * X = X + (P[X + 1] + 2) * 4;
4459 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4461 /* A = P[X + packet head]; */
4462 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4463 s
[i
]->s
.k
= off_ll
+ off_nl
;
4466 s
[i
] = new_stmt(BPF_ST
);
4470 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4473 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4477 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4479 /* A = P[X + packet head] */
4480 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4481 s
[i
]->s
.k
= off_ll
+ off_nl
;
4484 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4488 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4492 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4495 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4499 /* goto again; (must use BPF_JA for backward jump) */
4500 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4501 s
[i
]->s
.k
= again
- i
- 1;
4506 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4508 s
[fix2
]->s
.jt
= s
[end
];
4509 s
[fix4
]->s
.jf
= s
[end
];
4510 s
[fix5
]->s
.jt
= s
[end
];
4517 for (i
= 0; i
< max
- 1; i
++)
4518 s
[i
]->next
= s
[i
+ 1];
4519 s
[max
- 1]->next
= NULL
;
4524 b
= new_block(JMP(BPF_JEQ
));
4525 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4536 * Generate code that checks whether the packet is a packet for protocol
4537 * <proto> and whether the type field in that protocol's header has
4538 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4539 * IP packet and checks the protocol number in the IP header against <v>.
4541 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4542 * against Q_IP and Q_IPV6.
4544 static struct block
*
4545 gen_proto(v
, proto
, dir
)
4550 struct block
*b0
, *b1
;
4552 if (dir
!= Q_DEFAULT
)
4553 bpf_error("direction applied to 'proto'");
4558 b0
= gen_proto(v
, Q_IP
, dir
);
4559 b1
= gen_proto(v
, Q_IPV6
, dir
);
4567 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4568 * not LLC encapsulation with LLCSAP_IP.
4570 * For IEEE 802 networks - which includes 802.5 token ring
4571 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4572 * says that SNAP encapsulation is used, not LLC encapsulation
4575 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4576 * RFC 2225 say that SNAP encapsulation is used, not LLC
4577 * encapsulation with LLCSAP_IP.
4579 * So we always check for ETHERTYPE_IP.
4581 b0
= gen_linktype(ETHERTYPE_IP
);
4583 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4585 b1
= gen_protochain(v
, Q_IP
);
4595 * Frame Relay packets typically have an OSI
4596 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4597 * generates code to check for all the OSI
4598 * NLPIDs, so calling it and then adding a check
4599 * for the particular NLPID for which we're
4600 * looking is bogus, as we can just check for
4603 * What we check for is the NLPID and a frame
4604 * control field value of UI, i.e. 0x03 followed
4607 * XXX - assumes a 2-byte Frame Relay header with
4608 * DLCI and flags. What if the address is longer?
4610 * XXX - what about SNAP-encapsulated frames?
4612 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4618 * Cisco uses an Ethertype lookalike - for OSI,
4621 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4622 /* OSI in C-HDLC is stuffed with a fudge byte */
4623 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4628 b0
= gen_linktype(LLCSAP_ISONS
);
4629 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4635 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4637 * 4 is the offset of the PDU type relative to the IS-IS
4640 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4645 bpf_error("arp does not encapsulate another protocol");
4649 bpf_error("rarp does not encapsulate another protocol");
4653 bpf_error("atalk encapsulation is not specifiable");
4657 bpf_error("decnet encapsulation is not specifiable");
4661 bpf_error("sca does not encapsulate another protocol");
4665 bpf_error("lat does not encapsulate another protocol");
4669 bpf_error("moprc does not encapsulate another protocol");
4673 bpf_error("mopdl does not encapsulate another protocol");
4677 return gen_linktype(v
);
4680 bpf_error("'udp proto' is bogus");
4684 bpf_error("'tcp proto' is bogus");
4688 bpf_error("'sctp proto' is bogus");
4692 bpf_error("'icmp proto' is bogus");
4696 bpf_error("'igmp proto' is bogus");
4700 bpf_error("'igrp proto' is bogus");
4704 bpf_error("'pim proto' is bogus");
4708 bpf_error("'vrrp proto' is bogus");
4713 b0
= gen_linktype(ETHERTYPE_IPV6
);
4715 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4717 b1
= gen_protochain(v
, Q_IPV6
);
4723 bpf_error("'icmp6 proto' is bogus");
4727 bpf_error("'ah proto' is bogus");
4730 bpf_error("'ah proto' is bogus");
4733 bpf_error("'stp proto' is bogus");
4736 bpf_error("'ipx proto' is bogus");
4739 bpf_error("'netbeui proto' is bogus");
4742 bpf_error("'radio proto' is bogus");
4753 register const char *name
;
4756 int proto
= q
.proto
;
4760 bpf_u_int32 mask
, addr
;
4762 bpf_u_int32
**alist
;
4765 struct sockaddr_in
*sin
;
4766 struct sockaddr_in6
*sin6
;
4767 struct addrinfo
*res
, *res0
;
4768 struct in6_addr mask128
;
4770 struct block
*b
, *tmp
;
4771 int port
, real_proto
;
4777 addr
= pcap_nametonetaddr(name
);
4779 bpf_error("unknown network '%s'", name
);
4780 /* Left justify network addr and calculate its network mask */
4782 while (addr
&& (addr
& 0xff000000) == 0) {
4786 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
4790 if (proto
== Q_LINK
) {
4794 eaddr
= pcap_ether_hostton(name
);
4797 "unknown ether host '%s'", name
);
4798 b
= gen_ehostop(eaddr
, dir
);
4803 eaddr
= pcap_ether_hostton(name
);
4806 "unknown FDDI host '%s'", name
);
4807 b
= gen_fhostop(eaddr
, dir
);
4812 eaddr
= pcap_ether_hostton(name
);
4815 "unknown token ring host '%s'", name
);
4816 b
= gen_thostop(eaddr
, dir
);
4820 case DLT_IEEE802_11
:
4821 case DLT_IEEE802_11_RADIO_AVS
:
4822 case DLT_IEEE802_11_RADIO
:
4823 case DLT_PRISM_HEADER
:
4824 eaddr
= pcap_ether_hostton(name
);
4827 "unknown 802.11 host '%s'", name
);
4828 b
= gen_wlanhostop(eaddr
, dir
);
4832 case DLT_IP_OVER_FC
:
4833 eaddr
= pcap_ether_hostton(name
);
4836 "unknown Fibre Channel host '%s'", name
);
4837 b
= gen_ipfchostop(eaddr
, dir
);
4846 * Check that the packet doesn't begin
4847 * with an LE Control marker. (We've
4848 * already generated a test for LANE.)
4850 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4854 eaddr
= pcap_ether_hostton(name
);
4857 "unknown ether host '%s'", name
);
4858 b
= gen_ehostop(eaddr
, dir
);
4864 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4865 } else if (proto
== Q_DECNET
) {
4866 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4868 * I don't think DECNET hosts can be multihomed, so
4869 * there is no need to build up a list of addresses
4871 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
4874 alist
= pcap_nametoaddr(name
);
4875 if (alist
== NULL
|| *alist
== NULL
)
4876 bpf_error("unknown host '%s'", name
);
4878 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4880 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
4882 tmp
= gen_host(**alist
++, 0xffffffff,
4883 tproto
, dir
, q
.addr
);
4889 memset(&mask128
, 0xff, sizeof(mask128
));
4890 res0
= res
= pcap_nametoaddrinfo(name
);
4892 bpf_error("unknown host '%s'", name
);
4894 tproto
= tproto6
= proto
;
4895 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4899 for (res
= res0
; res
; res
= res
->ai_next
) {
4900 switch (res
->ai_family
) {
4902 if (tproto
== Q_IPV6
)
4905 sin
= (struct sockaddr_in
*)
4907 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4908 0xffffffff, tproto
, dir
, q
.addr
);
4911 if (tproto6
== Q_IP
)
4914 sin6
= (struct sockaddr_in6
*)
4916 tmp
= gen_host6(&sin6
->sin6_addr
,
4917 &mask128
, tproto6
, dir
, q
.addr
);
4928 bpf_error("unknown host '%s'%s", name
,
4929 (proto
== Q_DEFAULT
)
4931 : " for specified address family");
4938 if (proto
!= Q_DEFAULT
&&
4939 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4940 bpf_error("illegal qualifier of 'port'");
4941 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4942 bpf_error("unknown port '%s'", name
);
4943 if (proto
== Q_UDP
) {
4944 if (real_proto
== IPPROTO_TCP
)
4945 bpf_error("port '%s' is tcp", name
);
4946 else if (real_proto
== IPPROTO_SCTP
)
4947 bpf_error("port '%s' is sctp", name
);
4949 /* override PROTO_UNDEF */
4950 real_proto
= IPPROTO_UDP
;
4952 if (proto
== Q_TCP
) {
4953 if (real_proto
== IPPROTO_UDP
)
4954 bpf_error("port '%s' is udp", name
);
4956 else if (real_proto
== IPPROTO_SCTP
)
4957 bpf_error("port '%s' is sctp", name
);
4959 /* override PROTO_UNDEF */
4960 real_proto
= IPPROTO_TCP
;
4962 if (proto
== Q_SCTP
) {
4963 if (real_proto
== IPPROTO_UDP
)
4964 bpf_error("port '%s' is udp", name
);
4966 else if (real_proto
== IPPROTO_TCP
)
4967 bpf_error("port '%s' is tcp", name
);
4969 /* override PROTO_UNDEF */
4970 real_proto
= IPPROTO_SCTP
;
4973 return gen_port(port
, real_proto
, dir
);
4977 b
= gen_port(port
, real_proto
, dir
);
4978 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4984 if (proto
!= Q_DEFAULT
&&
4985 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4986 bpf_error("illegal qualifier of 'portrange'");
4987 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4988 bpf_error("unknown port in range '%s'", name
);
4989 if (proto
== Q_UDP
) {
4990 if (real_proto
== IPPROTO_TCP
)
4991 bpf_error("port in range '%s' is tcp", name
);
4992 else if (real_proto
== IPPROTO_SCTP
)
4993 bpf_error("port in range '%s' is sctp", name
);
4995 /* override PROTO_UNDEF */
4996 real_proto
= IPPROTO_UDP
;
4998 if (proto
== Q_TCP
) {
4999 if (real_proto
== IPPROTO_UDP
)
5000 bpf_error("port in range '%s' is udp", name
);
5001 else if (real_proto
== IPPROTO_SCTP
)
5002 bpf_error("port in range '%s' is sctp", name
);
5004 /* override PROTO_UNDEF */
5005 real_proto
= IPPROTO_TCP
;
5007 if (proto
== Q_SCTP
) {
5008 if (real_proto
== IPPROTO_UDP
)
5009 bpf_error("port in range '%s' is udp", name
);
5010 else if (real_proto
== IPPROTO_TCP
)
5011 bpf_error("port in range '%s' is tcp", name
);
5013 /* override PROTO_UNDEF */
5014 real_proto
= IPPROTO_SCTP
;
5017 return gen_portrange(port1
, port2
, real_proto
, dir
);
5021 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5022 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5029 eaddr
= pcap_ether_hostton(name
);
5031 bpf_error("unknown ether host: %s", name
);
5033 alist
= pcap_nametoaddr(name
);
5034 if (alist
== NULL
|| *alist
== NULL
)
5035 bpf_error("unknown host '%s'", name
);
5036 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5040 bpf_error("'gateway' not supported in this configuration");
5044 real_proto
= lookup_proto(name
, proto
);
5045 if (real_proto
>= 0)
5046 return gen_proto(real_proto
, proto
, dir
);
5048 bpf_error("unknown protocol: %s", name
);
5051 real_proto
= lookup_proto(name
, proto
);
5052 if (real_proto
>= 0)
5053 return gen_protochain(real_proto
, proto
, dir
);
5055 bpf_error("unknown protocol: %s", name
);
5067 gen_mcode(s1
, s2
, masklen
, q
)
5068 register const char *s1
, *s2
;
5069 register int masklen
;
5072 register int nlen
, mlen
;
5075 nlen
= __pcap_atoin(s1
, &n
);
5076 /* Promote short ipaddr */
5080 mlen
= __pcap_atoin(s2
, &m
);
5081 /* Promote short ipaddr */
5084 bpf_error("non-network bits set in \"%s mask %s\"",
5087 /* Convert mask len to mask */
5089 bpf_error("mask length must be <= 32");
5092 * X << 32 is not guaranteed by C to be 0; it's
5097 m
= 0xffffffff << (32 - masklen
);
5099 bpf_error("non-network bits set in \"%s/%d\"",
5106 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5109 bpf_error("Mask syntax for networks only");
5117 register const char *s
;
5122 int proto
= q
.proto
;
5128 else if (q
.proto
== Q_DECNET
)
5129 vlen
= __pcap_atodn(s
, &v
);
5131 vlen
= __pcap_atoin(s
, &v
);
5138 if (proto
== Q_DECNET
)
5139 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5140 else if (proto
== Q_LINK
) {
5141 bpf_error("illegal link layer address");
5144 if (s
== NULL
&& q
.addr
== Q_NET
) {
5145 /* Promote short net number */
5146 while (v
&& (v
& 0xff000000) == 0) {
5151 /* Promote short ipaddr */
5155 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5160 proto
= IPPROTO_UDP
;
5161 else if (proto
== Q_TCP
)
5162 proto
= IPPROTO_TCP
;
5163 else if (proto
== Q_SCTP
)
5164 proto
= IPPROTO_SCTP
;
5165 else if (proto
== Q_DEFAULT
)
5166 proto
= PROTO_UNDEF
;
5168 bpf_error("illegal qualifier of 'port'");
5171 return gen_port((int)v
, proto
, dir
);
5175 b
= gen_port((int)v
, proto
, dir
);
5176 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5183 proto
= IPPROTO_UDP
;
5184 else if (proto
== Q_TCP
)
5185 proto
= IPPROTO_TCP
;
5186 else if (proto
== Q_SCTP
)
5187 proto
= IPPROTO_SCTP
;
5188 else if (proto
== Q_DEFAULT
)
5189 proto
= PROTO_UNDEF
;
5191 bpf_error("illegal qualifier of 'portrange'");
5194 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5198 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5199 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5205 bpf_error("'gateway' requires a name");
5209 return gen_proto((int)v
, proto
, dir
);
5212 return gen_protochain((int)v
, proto
, dir
);
5227 gen_mcode6(s1
, s2
, masklen
, q
)
5228 register const char *s1
, *s2
;
5229 register int masklen
;
5232 struct addrinfo
*res
;
5233 struct in6_addr
*addr
;
5234 struct in6_addr mask
;
5239 bpf_error("no mask %s supported", s2
);
5241 res
= pcap_nametoaddrinfo(s1
);
5243 bpf_error("invalid ip6 address %s", s1
);
5245 bpf_error("%s resolved to multiple address", s1
);
5246 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5248 if (sizeof(mask
) * 8 < masklen
)
5249 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5250 memset(&mask
, 0, sizeof(mask
));
5251 memset(&mask
, 0xff, masklen
/ 8);
5253 mask
.s6_addr
[masklen
/ 8] =
5254 (0xff << (8 - masklen
% 8)) & 0xff;
5257 a
= (u_int32_t
*)addr
;
5258 m
= (u_int32_t
*)&mask
;
5259 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5260 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5261 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5269 bpf_error("Mask syntax for networks only");
5273 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5278 bpf_error("invalid qualifier against IPv6 address");
5286 register const u_char
*eaddr
;
5289 struct block
*b
, *tmp
;
5291 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5294 return gen_ehostop(eaddr
, (int)q
.dir
);
5296 return gen_fhostop(eaddr
, (int)q
.dir
);
5298 return gen_thostop(eaddr
, (int)q
.dir
);
5299 case DLT_IEEE802_11
:
5300 case DLT_IEEE802_11_RADIO_AVS
:
5301 case DLT_IEEE802_11_RADIO
:
5302 case DLT_PRISM_HEADER
:
5303 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5307 * Check that the packet doesn't begin with an
5308 * LE Control marker. (We've already generated
5311 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5316 * Now check the MAC address.
5318 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5323 case DLT_IP_OVER_FC
:
5324 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5326 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5330 bpf_error("ethernet address used in non-ether expression");
5336 struct slist
*s0
, *s1
;
5339 * This is definitely not the best way to do this, but the
5340 * lists will rarely get long.
5347 static struct slist
*
5353 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5358 static struct slist
*
5364 s
= new_stmt(BPF_LD
|BPF_MEM
);
5370 * Modify "index" to use the value stored into its register as an
5371 * offset relative to the beginning of the header for the protocol
5372 * "proto", and allocate a register and put an item "size" bytes long
5373 * (1, 2, or 4) at that offset into that register, making it the register
5377 gen_load(proto
, index
, size
)
5382 struct slist
*s
, *tmp
;
5384 int regno
= alloc_reg();
5386 free_reg(index
->regno
);
5390 bpf_error("data size must be 1, 2, or 4");
5406 bpf_error("unsupported index operation");
5410 * The offset is relative to the beginning of the packet
5411 * data, if we have a radio header. (If we don't, this
5414 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5415 linktype
!= DLT_IEEE802_11_RADIO
&&
5416 linktype
!= DLT_PRISM_HEADER
)
5417 bpf_error("radio information not present in capture");
5420 * Load into the X register the offset computed into the
5421 * register specifed by "index".
5423 s
= xfer_to_x(index
);
5426 * Load the item at that offset.
5428 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5430 sappend(index
->s
, s
);
5435 * The offset is relative to the beginning of
5436 * the link-layer header.
5438 * XXX - what about ATM LANE? Should the index be
5439 * relative to the beginning of the AAL5 frame, so
5440 * that 0 refers to the beginning of the LE Control
5441 * field, or relative to the beginning of the LAN
5442 * frame, so that 0 refers, for Ethernet LANE, to
5443 * the beginning of the destination address?
5445 s
= gen_llprefixlen();
5448 * If "s" is non-null, it has code to arrange that the
5449 * X register contains the length of the prefix preceding
5450 * the link-layer header. Add to it the offset computed
5451 * into the register specified by "index", and move that
5452 * into the X register. Otherwise, just load into the X
5453 * register the offset computed into the register specifed
5457 sappend(s
, xfer_to_a(index
));
5458 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5459 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5461 s
= xfer_to_x(index
);
5464 * Load the item at the sum of the offset we've put in the
5465 * X register and the offset of the start of the link
5466 * layer header (which is 0 if the radio header is
5467 * variable-length; that header length is what we put
5468 * into the X register and then added to the index).
5470 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5473 sappend(index
->s
, s
);
5489 * The offset is relative to the beginning of
5490 * the network-layer header.
5491 * XXX - are there any cases where we want
5494 s
= gen_llprefixlen();
5497 * If "s" is non-null, it has code to arrange that the
5498 * X register contains the length of the prefix preceding
5499 * the link-layer header. Add to it the offset computed
5500 * into the register specified by "index", and move that
5501 * into the X register. Otherwise, just load into the X
5502 * register the offset computed into the register specifed
5506 sappend(s
, xfer_to_a(index
));
5507 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5508 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5510 s
= xfer_to_x(index
);
5513 * Load the item at the sum of the offset we've put in the
5514 * X register, the offset of the start of the network
5515 * layer header, and the offset of the start of the link
5516 * layer header (which is 0 if the radio header is
5517 * variable-length; that header length is what we put
5518 * into the X register and then added to the index).
5520 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5521 tmp
->s
.k
= off_ll
+ off_nl
;
5523 sappend(index
->s
, s
);
5526 * Do the computation only if the packet contains
5527 * the protocol in question.
5529 b
= gen_proto_abbrev(proto
);
5531 gen_and(index
->b
, b
);
5544 * The offset is relative to the beginning of
5545 * the transport-layer header.
5547 * Load the X register with the length of the IPv4 header
5548 * (plus the offset of the link-layer header, if it's
5549 * a variable-length header), in bytes.
5551 * XXX - are there any cases where we want
5553 * XXX - we should, if we're built with
5554 * IPv6 support, generate code to load either
5555 * IPv4, IPv6, or both, as appropriate.
5557 s
= gen_loadx_iphdrlen();
5560 * The X register now contains the sum of the length
5561 * of any variable-length header preceding the link-layer
5562 * header and the length of the network-layer header.
5563 * Load into the A register the offset relative to
5564 * the beginning of the transport layer header,
5565 * add the X register to that, move that to the
5566 * X register, and load with an offset from the
5567 * X register equal to the offset of the network
5568 * layer header relative to the beginning of
5569 * the link-layer header plus the length of any
5570 * fixed-length header preceding the link-layer
5573 sappend(s
, xfer_to_a(index
));
5574 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5575 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5576 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5577 tmp
->s
.k
= off_ll
+ off_nl
;
5578 sappend(index
->s
, s
);
5581 * Do the computation only if the packet contains
5582 * the protocol in question - which is true only
5583 * if this is an IP datagram and is the first or
5584 * only fragment of that datagram.
5586 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5588 gen_and(index
->b
, b
);
5590 gen_and(gen_proto_abbrev(Q_IP
), b
);
5596 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5600 index
->regno
= regno
;
5601 s
= new_stmt(BPF_ST
);
5603 sappend(index
->s
, s
);
5609 gen_relation(code
, a0
, a1
, reversed
)
5611 struct arth
*a0
, *a1
;
5614 struct slist
*s0
, *s1
, *s2
;
5615 struct block
*b
, *tmp
;
5619 if (code
== BPF_JEQ
) {
5620 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5621 b
= new_block(JMP(code
));
5625 b
= new_block(BPF_JMP
|code
|BPF_X
);
5631 sappend(a0
->s
, a1
->s
);
5635 free_reg(a0
->regno
);
5636 free_reg(a1
->regno
);
5638 /* 'and' together protocol checks */
5641 gen_and(a0
->b
, tmp
= a1
->b
);
5657 int regno
= alloc_reg();
5658 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5661 s
= new_stmt(BPF_LD
|BPF_LEN
);
5662 s
->next
= new_stmt(BPF_ST
);
5663 s
->next
->s
.k
= regno
;
5678 a
= (struct arth
*)newchunk(sizeof(*a
));
5682 s
= new_stmt(BPF_LD
|BPF_IMM
);
5684 s
->next
= new_stmt(BPF_ST
);
5700 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5703 s
= new_stmt(BPF_ST
);
5711 gen_arth(code
, a0
, a1
)
5713 struct arth
*a0
, *a1
;
5715 struct slist
*s0
, *s1
, *s2
;
5719 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5724 sappend(a0
->s
, a1
->s
);
5726 free_reg(a0
->regno
);
5727 free_reg(a1
->regno
);
5729 s0
= new_stmt(BPF_ST
);
5730 a0
->regno
= s0
->s
.k
= alloc_reg();
5737 * Here we handle simple allocation of the scratch registers.
5738 * If too many registers are alloc'd, the allocator punts.
5740 static int regused
[BPF_MEMWORDS
];
5744 * Return the next free register.
5749 int n
= BPF_MEMWORDS
;
5752 if (regused
[curreg
])
5753 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5755 regused
[curreg
] = 1;
5759 bpf_error("too many registers needed to evaluate expression");
5764 * Return a register to the table so it can
5774 static struct block
*
5781 s
= new_stmt(BPF_LD
|BPF_LEN
);
5782 b
= new_block(JMP(jmp
));
5793 return gen_len(BPF_JGE
, n
);
5797 * Actually, this is less than or equal.
5805 b
= gen_len(BPF_JGT
, n
);
5812 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5813 * the beginning of the link-layer header.
5814 * XXX - that means you can't test values in the radiotap header, but
5815 * as that header is difficult if not impossible to parse generally
5816 * without a loop, that might not be a severe problem. A new keyword
5817 * "radio" could be added for that, although what you'd really want
5818 * would be a way of testing particular radio header values, which
5819 * would generate code appropriate to the radio header in question.
5822 gen_byteop(op
, idx
, val
)
5833 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5836 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5840 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5844 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5848 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5852 b
= new_block(JMP(BPF_JEQ
));
5859 static u_char abroadcast
[] = { 0x0 };
5862 gen_broadcast(proto
)
5865 bpf_u_int32 hostmask
;
5866 struct block
*b0
, *b1
, *b2
;
5867 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5875 case DLT_ARCNET_LINUX
:
5876 return gen_ahostop(abroadcast
, Q_DST
);
5878 return gen_ehostop(ebroadcast
, Q_DST
);
5880 return gen_fhostop(ebroadcast
, Q_DST
);
5882 return gen_thostop(ebroadcast
, Q_DST
);
5883 case DLT_IEEE802_11
:
5884 case DLT_IEEE802_11_RADIO_AVS
:
5885 case DLT_IEEE802_11_RADIO
:
5886 case DLT_PRISM_HEADER
:
5887 return gen_wlanhostop(ebroadcast
, Q_DST
);
5888 case DLT_IP_OVER_FC
:
5889 return gen_ipfchostop(ebroadcast
, Q_DST
);
5893 * Check that the packet doesn't begin with an
5894 * LE Control marker. (We've already generated
5897 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5902 * Now check the MAC address.
5904 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5910 bpf_error("not a broadcast link");
5915 b0
= gen_linktype(ETHERTYPE_IP
);
5916 hostmask
= ~netmask
;
5917 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5918 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5919 (bpf_int32
)(~0 & hostmask
), hostmask
);
5924 bpf_error("only link-layer/IP broadcast filters supported");
5929 * Generate code to test the low-order bit of a MAC address (that's
5930 * the bottom bit of the *first* byte).
5932 static struct block
*
5933 gen_mac_multicast(offset
)
5936 register struct block
*b0
;
5937 register struct slist
*s
;
5939 /* link[offset] & 1 != 0 */
5940 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5941 b0
= new_block(JMP(BPF_JSET
));
5948 gen_multicast(proto
)
5951 register struct block
*b0
, *b1
, *b2
;
5952 register struct slist
*s
;
5960 case DLT_ARCNET_LINUX
:
5961 /* all ARCnet multicasts use the same address */
5962 return gen_ahostop(abroadcast
, Q_DST
);
5964 /* ether[0] & 1 != 0 */
5965 return gen_mac_multicast(0);
5968 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5970 * XXX - was that referring to bit-order issues?
5972 /* fddi[1] & 1 != 0 */
5973 return gen_mac_multicast(1);
5975 /* tr[2] & 1 != 0 */
5976 return gen_mac_multicast(2);
5977 case DLT_IEEE802_11
:
5978 case DLT_IEEE802_11_RADIO_AVS
:
5979 case DLT_IEEE802_11_RADIO
:
5980 case DLT_PRISM_HEADER
:
5984 * For control frames, there is no DA.
5986 * For management frames, DA is at an
5987 * offset of 4 from the beginning of
5990 * For data frames, DA is at an offset
5991 * of 4 from the beginning of the packet
5992 * if To DS is clear and at an offset of
5993 * 16 from the beginning of the packet
5998 * Generate the tests to be done for data frames.
6000 * First, check for To DS set, i.e. "link[1] & 0x01".
6002 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6003 b1
= new_block(JMP(BPF_JSET
));
6004 b1
->s
.k
= 0x01; /* To DS */
6008 * If To DS is set, the DA is at 16.
6010 b0
= gen_mac_multicast(16);
6014 * Now, check for To DS not set, i.e. check
6015 * "!(link[1] & 0x01)".
6017 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6018 b2
= new_block(JMP(BPF_JSET
));
6019 b2
->s
.k
= 0x01; /* To DS */
6024 * If To DS is not set, the DA is at 4.
6026 b1
= gen_mac_multicast(4);
6030 * Now OR together the last two checks. That gives
6031 * the complete set of checks for data frames.
6036 * Now check for a data frame.
6037 * I.e, check "link[0] & 0x08".
6039 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6040 b1
= new_block(JMP(BPF_JSET
));
6045 * AND that with the checks done for data frames.
6050 * If the high-order bit of the type value is 0, this
6051 * is a management frame.
6052 * I.e, check "!(link[0] & 0x08)".
6054 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6055 b2
= new_block(JMP(BPF_JSET
));
6061 * For management frames, the DA is at 4.
6063 b1
= gen_mac_multicast(4);
6067 * OR that with the checks done for data frames.
6068 * That gives the checks done for management and
6074 * If the low-order bit of the type value is 1,
6075 * this is either a control frame or a frame
6076 * with a reserved type, and thus not a
6079 * I.e., check "!(link[0] & 0x04)".
6081 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6082 b1
= new_block(JMP(BPF_JSET
));
6088 * AND that with the checks for data and management
6093 case DLT_IP_OVER_FC
:
6094 b0
= gen_mac_multicast(2);
6099 * Check that the packet doesn't begin with an
6100 * LE Control marker. (We've already generated
6103 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6107 /* ether[off_mac] & 1 != 0 */
6108 b0
= gen_mac_multicast(off_mac
);
6116 /* Link not known to support multicasts */
6120 b0
= gen_linktype(ETHERTYPE_IP
);
6121 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6127 b0
= gen_linktype(ETHERTYPE_IPV6
);
6128 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6133 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6138 * generate command for inbound/outbound. It's here so we can
6139 * make it link-type specific. 'dir' = 0 implies "inbound",
6140 * = 1 implies "outbound".
6146 register struct block
*b0
;
6149 * Only some data link types support inbound/outbound qualifiers.
6153 b0
= gen_relation(BPF_JEQ
,
6154 gen_load(Q_LINK
, gen_loadi(0), 1),
6162 * Match packets sent by this machine.
6164 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6167 * Match packets sent to this machine.
6168 * (No broadcast or multicast packets, or
6169 * packets sent to some other machine and
6170 * received promiscuously.)
6172 * XXX - packets sent to other machines probably
6173 * shouldn't be matched, but what about broadcast
6174 * or multicast packets we received?
6176 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6181 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6182 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6187 /* match outgoing packets */
6188 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6190 /* match incoming packets */
6191 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6195 case DLT_JUNIPER_MFR
:
6196 case DLT_JUNIPER_MLFR
:
6197 case DLT_JUNIPER_MLPPP
:
6198 case DLT_JUNIPER_ATM1
:
6199 case DLT_JUNIPER_ATM2
:
6200 case DLT_JUNIPER_PPPOE
:
6201 case DLT_JUNIPER_PPPOE_ATM
:
6202 case DLT_JUNIPER_GGSN
:
6203 case DLT_JUNIPER_ES
:
6204 case DLT_JUNIPER_MONITOR
:
6205 case DLT_JUNIPER_SERVICES
:
6206 case DLT_JUNIPER_ETHER
:
6207 case DLT_JUNIPER_PPP
:
6208 case DLT_JUNIPER_FRELAY
:
6209 case DLT_JUNIPER_CHDLC
:
6210 case DLT_JUNIPER_VP
:
6211 /* juniper flags (including direction) are stored
6212 * the byte after the 3-byte magic number */
6214 /* match outgoing packets */
6215 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6217 /* match incoming packets */
6218 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6223 bpf_error("inbound/outbound not supported on linktype %d",
6231 /* PF firewall log matched interface */
6233 gen_pf_ifname(const char *ifname
)
6238 if (linktype
== DLT_PFLOG
) {
6239 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6240 off
= offsetof(struct pfloghdr
, ifname
);
6242 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6245 if (strlen(ifname
) >= len
) {
6246 bpf_error("ifname interface names can only be %d characters",
6250 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6254 /* PF firewall log ruleset name */
6256 gen_pf_ruleset(char *ruleset
)
6260 if (linktype
!= DLT_PFLOG
) {
6261 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6264 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6265 bpf_error("ruleset names can only be %ld characters",
6266 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6269 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6270 strlen(ruleset
), (const u_char
*)ruleset
);
6274 /* PF firewall log rule number */
6280 if (linktype
== DLT_PFLOG
) {
6281 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6284 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6291 /* PF firewall log sub-rule number */
6293 gen_pf_srnr(int srnr
)
6297 if (linktype
!= DLT_PFLOG
) {
6298 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6302 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6307 /* PF firewall log reason code */
6309 gen_pf_reason(int reason
)
6313 if (linktype
== DLT_PFLOG
) {
6314 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6317 bpf_error("reason not supported on linktype 0x%x", linktype
);
6324 /* PF firewall log action */
6326 gen_pf_action(int action
)
6330 if (linktype
== DLT_PFLOG
) {
6331 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6334 bpf_error("action not supported on linktype 0x%x", linktype
);
6343 register const u_char
*eaddr
;
6346 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6347 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6348 return gen_ahostop(eaddr
, (int)q
.dir
);
6350 bpf_error("ARCnet address used in non-arc expression");
6354 static struct block
*
6355 gen_ahostop(eaddr
, dir
)
6356 register const u_char
*eaddr
;
6359 register struct block
*b0
, *b1
;
6362 /* src comes first, different from Ethernet */
6364 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6367 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6370 b0
= gen_ahostop(eaddr
, Q_SRC
);
6371 b1
= gen_ahostop(eaddr
, Q_DST
);
6377 b0
= gen_ahostop(eaddr
, Q_SRC
);
6378 b1
= gen_ahostop(eaddr
, Q_DST
);
6387 * support IEEE 802.1Q VLAN trunk over ethernet
6393 struct block
*b0
, *b1
;
6395 /* can't check for VLAN-encapsulated packets inside MPLS */
6396 if (label_stack_depth
> 0)
6397 bpf_error("no VLAN match after MPLS");
6400 * Change the offsets to point to the type and data fields within
6401 * the VLAN packet. Just increment the offsets, so that we
6402 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6403 * capture VLAN 200 encapsulated within VLAN 100.
6405 * XXX - this is a bit of a kludge. If we were to split the
6406 * compiler into a parser that parses an expression and
6407 * generates an expression tree, and a code generator that
6408 * takes an expression tree (which could come from our
6409 * parser or from some other parser) and generates BPF code,
6410 * we could perhaps make the offsets parameters of routines
6411 * and, in the handler for an "AND" node, pass to subnodes
6412 * other than the VLAN node the adjusted offsets.
6414 * This would mean that "vlan" would, instead of changing the
6415 * behavior of *all* tests after it, change only the behavior
6416 * of tests ANDed with it. That would change the documented
6417 * semantics of "vlan", which might break some expressions.
6418 * However, it would mean that "(vlan and ip) or ip" would check
6419 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6420 * checking only for VLAN-encapsulated IP, so that could still
6421 * be considered worth doing; it wouldn't break expressions
6422 * that are of the form "vlan and ..." or "vlan N and ...",
6423 * which I suspect are the most common expressions involving
6424 * "vlan". "vlan or ..." doesn't necessarily do what the user
6425 * would really want, now, as all the "or ..." tests would
6426 * be done assuming a VLAN, even though the "or" could be viewed
6427 * as meaning "or, if this isn't a VLAN packet...".
6429 orig_linktype
= off_linktype
; /* save original values */
6441 bpf_error("no VLAN support for data link type %d",
6446 /* check for VLAN */
6447 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6449 /* If a specific VLAN is requested, check VLAN id */
6450 if (vlan_num
>= 0) {
6451 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6467 struct block
*b0
,*b1
;
6470 * Change the offsets to point to the type and data fields within
6471 * the MPLS packet. Just increment the offsets, so that we
6472 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6473 * capture packets with an outer label of 100000 and an inner
6476 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6480 if (label_stack_depth
> 0) {
6481 /* just match the bottom-of-stack bit clear */
6482 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6485 * Indicate that we're checking MPLS-encapsulated headers,
6486 * to make sure higher level code generators don't try to
6487 * match against IP-related protocols such as Q_ARP, Q_RARP
6492 case DLT_C_HDLC
: /* fall through */
6494 b0
= gen_linktype(ETHERTYPE_MPLS
);
6498 b0
= gen_linktype(PPP_MPLS_UCAST
);
6501 /* FIXME add other DLT_s ...
6502 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6503 * leave it for now */
6506 bpf_error("no MPLS support for data link type %d",
6514 /* If a specific MPLS label is requested, check it */
6515 if (label_num
>= 0) {
6516 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6517 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6518 0xfffff000); /* only compare the first 20 bits */
6525 label_stack_depth
++;
6530 * Support PPPOE discovery and session.
6535 /* check for PPPoE discovery */
6536 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6545 * Test against the PPPoE session link-layer type.
6547 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6550 * Change the offsets to point to the type and data fields within
6553 * XXX - this is a bit of a kludge. If we were to split the
6554 * compiler into a parser that parses an expression and
6555 * generates an expression tree, and a code generator that
6556 * takes an expression tree (which could come from our
6557 * parser or from some other parser) and generates BPF code,
6558 * we could perhaps make the offsets parameters of routines
6559 * and, in the handler for an "AND" node, pass to subnodes
6560 * other than the PPPoE node the adjusted offsets.
6562 * This would mean that "pppoes" would, instead of changing the
6563 * behavior of *all* tests after it, change only the behavior
6564 * of tests ANDed with it. That would change the documented
6565 * semantics of "pppoes", which might break some expressions.
6566 * However, it would mean that "(pppoes and ip) or ip" would check
6567 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6568 * checking only for VLAN-encapsulated IP, so that could still
6569 * be considered worth doing; it wouldn't break expressions
6570 * that are of the form "pppoes and ..." which I suspect are the
6571 * most common expressions involving "pppoes". "pppoes or ..."
6572 * doesn't necessarily do what the user would really want, now,
6573 * as all the "or ..." tests would be done assuming PPPoE, even
6574 * though the "or" could be viewed as meaning "or, if this isn't
6575 * a PPPoE packet...".
6577 orig_linktype
= off_linktype
; /* save original values */
6581 * The "network-layer" protocol is PPPoE, which has a 6-byte
6582 * PPPoE header, followed by PPP payload, so we set the
6583 * offsets to the network layer offset plus 6 bytes for
6584 * the PPPoE header plus the values appropriate for PPP when
6585 * encapsulated in Ethernet (which means there's no HDLC
6588 off_linktype
= orig_nl
+ 6;
6589 off_nl
= orig_nl
+ 6 + 2;
6590 off_nl_nosnap
= orig_nl
+ 6 + 2;
6593 * Set the link-layer type to PPP, as all subsequent tests will
6594 * be on the encapsulated PPP header.
6602 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6614 bpf_error("'vpi' supported only on raw ATM");
6615 if (off_vpi
== (u_int
)-1)
6617 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6623 bpf_error("'vci' supported only on raw ATM");
6624 if (off_vci
== (u_int
)-1)
6626 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6631 if (off_proto
== (u_int
)-1)
6632 abort(); /* XXX - this isn't on FreeBSD */
6633 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6638 if (off_payload
== (u_int
)-1)
6640 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6641 0xffffffff, jtype
, reverse
, jvalue
);
6646 bpf_error("'callref' supported only on raw ATM");
6647 if (off_proto
== (u_int
)-1)
6649 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6650 jtype
, reverse
, jvalue
);
6660 gen_atmtype_abbrev(type
)
6663 struct block
*b0
, *b1
;
6668 /* Get all packets in Meta signalling Circuit */
6670 bpf_error("'metac' supported only on raw ATM");
6671 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6672 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6677 /* Get all packets in Broadcast Circuit*/
6679 bpf_error("'bcc' supported only on raw ATM");
6680 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6681 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6686 /* Get all cells in Segment OAM F4 circuit*/
6688 bpf_error("'oam4sc' supported only on raw ATM");
6689 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6690 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6695 /* Get all cells in End-to-End OAM F4 Circuit*/
6697 bpf_error("'oam4ec' supported only on raw ATM");
6698 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6699 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6704 /* Get all packets in connection Signalling Circuit */
6706 bpf_error("'sc' supported only on raw ATM");
6707 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6708 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6713 /* Get all packets in ILMI Circuit */
6715 bpf_error("'ilmic' supported only on raw ATM");
6716 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6717 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6722 /* Get all LANE packets */
6724 bpf_error("'lane' supported only on raw ATM");
6725 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6728 * Arrange that all subsequent tests assume LANE
6729 * rather than LLC-encapsulated packets, and set
6730 * the offsets appropriately for LANE-encapsulated
6733 * "off_mac" is the offset of the Ethernet header,
6734 * which is 2 bytes past the ATM pseudo-header
6735 * (skipping the pseudo-header and 2-byte LE Client
6736 * field). The other offsets are Ethernet offsets
6737 * relative to "off_mac".
6740 off_mac
= off_payload
+ 2; /* MAC header */
6741 off_linktype
= off_mac
+ 12;
6742 off_nl
= off_mac
+ 14; /* Ethernet II */
6743 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6747 /* Get all LLC-encapsulated packets */
6749 bpf_error("'llc' supported only on raw ATM");
6750 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6761 * Filtering for MTP2 messages based on li value
6762 * FISU, length is null
6763 * LSSU, length is 1 or 2
6764 * MSU, length is 3 or more
6767 gen_mtp2type_abbrev(type
)
6770 struct block
*b0
, *b1
;
6775 if (linktype
!= DLT_MTP2
)
6776 bpf_error("'fisu' supported only on MTP2");
6777 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
6778 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
6782 if (linktype
!= DLT_MTP2
)
6783 bpf_error("'lssu' supported only on MTP2");
6784 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
6785 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
6790 if (linktype
!= DLT_MTP2
)
6791 bpf_error("'msu' supported only on MTP2");
6792 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
6802 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6809 bpf_u_int32 val1
, val2
, val3
;
6811 switch (mtp3field
) {
6814 if (off_sio
== (u_int
)-1)
6815 bpf_error("'sio' supported only on SS7");
6816 /* sio coded on 1 byte so max value 255 */
6818 bpf_error("sio value %u too big; max value = 255",
6820 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6821 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6825 if (off_opc
== (u_int
)-1)
6826 bpf_error("'opc' supported only on SS7");
6827 /* opc coded on 14 bits so max value 16383 */
6829 bpf_error("opc value %u too big; max value = 16383",
6831 /* the following instructions are made to convert jvalue
6832 * to the form used to write opc in an ss7 message*/
6833 val1
= jvalue
& 0x00003c00;
6835 val2
= jvalue
& 0x000003fc;
6837 val3
= jvalue
& 0x00000003;
6839 jvalue
= val1
+ val2
+ val3
;
6840 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6841 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6845 if (off_dpc
== (u_int
)-1)
6846 bpf_error("'dpc' supported only on SS7");
6847 /* dpc coded on 14 bits so max value 16383 */
6849 bpf_error("dpc value %u too big; max value = 16383",
6851 /* the following instructions are made to convert jvalue
6852 * to the forme used to write dpc in an ss7 message*/
6853 val1
= jvalue
& 0x000000ff;
6855 val2
= jvalue
& 0x00003f00;
6857 jvalue
= val1
+ val2
;
6858 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6859 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6863 if (off_sls
== (u_int
)-1)
6864 bpf_error("'sls' supported only on SS7");
6865 /* sls coded on 4 bits so max value 15 */
6867 bpf_error("sls value %u too big; max value = 15",
6869 /* the following instruction is made to convert jvalue
6870 * to the forme used to write sls in an ss7 message*/
6871 jvalue
= jvalue
<< 4;
6872 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6873 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6882 static struct block
*
6883 gen_msg_abbrev(type
)
6889 * Q.2931 signalling protocol messages for handling virtual circuits
6890 * establishment and teardown
6895 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6899 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6903 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6907 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6911 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6914 case A_RELEASE_DONE
:
6915 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6925 gen_atmmulti_abbrev(type
)
6928 struct block
*b0
, *b1
;
6934 bpf_error("'oam' supported only on raw ATM");
6935 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6940 bpf_error("'oamf4' supported only on raw ATM");
6942 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6943 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6945 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6951 * Get Q.2931 signalling messages for switched
6952 * virtual connection
6955 bpf_error("'connectmsg' supported only on raw ATM");
6956 b0
= gen_msg_abbrev(A_SETUP
);
6957 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6959 b0
= gen_msg_abbrev(A_CONNECT
);
6961 b0
= gen_msg_abbrev(A_CONNECTACK
);
6963 b0
= gen_msg_abbrev(A_RELEASE
);
6965 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6967 b0
= gen_atmtype_abbrev(A_SC
);
6973 bpf_error("'metaconnect' supported only on raw ATM");
6974 b0
= gen_msg_abbrev(A_SETUP
);
6975 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6977 b0
= gen_msg_abbrev(A_CONNECT
);
6979 b0
= gen_msg_abbrev(A_RELEASE
);
6981 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6983 b0
= gen_atmtype_abbrev(A_METAC
);