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: /pub/NetBSD/misc/repositories/cvsroot/src/dist/libpcap/gencode.c,v 1.4 2006/10/15 19:27:21 christos 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);
211 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, 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 const char *buf
, int optimize
, bpf_u_int32 mask
)
354 const char * volatile xbuf
= buf
;
361 if (setjmp(top_ctx
)) {
369 snaplen
= pcap_snapshot(p
);
371 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
372 "snaplen of 0 rejects all packets");
376 lex_init(xbuf
? xbuf
: "");
384 root
= gen_retblk(snaplen
);
386 if (optimize
&& !no_optimize
) {
389 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
390 bpf_error("expression rejects all packets");
392 program
->bf_insns
= icode_to_fcode(root
, &len
);
393 program
->bf_len
= len
;
401 * entry point for using the compiler with no pcap open
402 * pass in all the stuff that is needed explicitly instead.
405 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
406 struct bpf_program
*program
,
407 const char *buf
, int optimize
, bpf_u_int32 mask
)
412 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
415 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
421 * Clean up a "struct bpf_program" by freeing all the memory allocated
425 pcap_freecode(struct bpf_program
*program
)
428 if (program
->bf_insns
!= NULL
) {
429 free((char *)program
->bf_insns
);
430 program
->bf_insns
= NULL
;
435 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
436 * which of the jt and jf fields has been resolved and which is a pointer
437 * back to another unresolved block (or nil). At least one of the fields
438 * in each block is already resolved.
441 backpatch(list
, target
)
442 struct block
*list
, *target
;
459 * Merge the lists in b0 and b1, using the 'sense' field to indicate
460 * which of jt and jf is the link.
464 struct block
*b0
, *b1
;
466 register struct block
**p
= &b0
;
468 /* Find end of list. */
470 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
472 /* Concatenate the lists. */
480 backpatch(p
, gen_retblk(snaplen
));
481 p
->sense
= !p
->sense
;
482 backpatch(p
, gen_retblk(0));
486 * Insert before the statements of the first (root) block any
487 * statements needed to load the lengths of any variable-length
488 * headers into registers.
490 * XXX - a fancier strategy would be to insert those before the
491 * statements of all blocks that use those lengths and that
492 * have no predecessors that use them, so that we only compute
493 * the lengths if we need them. There might be even better
494 * approaches than that. However, as we're currently only
495 * handling variable-length radiotap headers, and as all
496 * filtering expressions other than raw link[M:N] tests
497 * require the length of that header, doing more for that
498 * header length isn't really worth the effort.
500 insert_load_llprefixlen(root
);
505 struct block
*b0
, *b1
;
507 backpatch(b0
, b1
->head
);
508 b0
->sense
= !b0
->sense
;
509 b1
->sense
= !b1
->sense
;
511 b1
->sense
= !b1
->sense
;
517 struct block
*b0
, *b1
;
519 b0
->sense
= !b0
->sense
;
520 backpatch(b0
, b1
->head
);
521 b0
->sense
= !b0
->sense
;
530 b
->sense
= !b
->sense
;
533 static struct block
*
534 gen_cmp(offrel
, offset
, size
, v
)
535 enum e_offrel offrel
;
539 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
542 static struct block
*
543 gen_cmp_gt(offrel
, offset
, size
, v
)
544 enum e_offrel offrel
;
548 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
551 static struct block
*
552 gen_cmp_ge(offrel
, offset
, size
, v
)
553 enum e_offrel offrel
;
557 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
560 static struct block
*
561 gen_cmp_lt(offrel
, offset
, size
, v
)
562 enum e_offrel offrel
;
566 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
569 static struct block
*
570 gen_cmp_le(offrel
, offset
, size
, v
)
571 enum e_offrel offrel
;
575 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
578 static struct block
*
579 gen_mcmp(offrel
, offset
, size
, v
, mask
)
580 enum e_offrel offrel
;
585 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
588 static struct block
*
589 gen_bcmp(offrel
, offset
, size
, v
)
590 enum e_offrel offrel
;
591 register u_int offset
, size
;
592 register const u_char
*v
;
594 register struct block
*b
, *tmp
;
598 register const u_char
*p
= &v
[size
- 4];
599 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
600 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
602 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
609 register const u_char
*p
= &v
[size
- 2];
610 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
612 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
619 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
628 * AND the field of size "size" at offset "offset" relative to the header
629 * specified by "offrel" with "mask", and compare it with the value "v"
630 * with the test specified by "jtype"; if "reverse" is true, the test
631 * should test the opposite of "jtype".
633 static struct block
*
634 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
635 enum e_offrel offrel
;
637 bpf_u_int32 offset
, size
, mask
, jtype
;
640 struct slist
*s
, *s2
;
643 s
= gen_load_a(offrel
, offset
, size
);
645 if (mask
!= 0xffffffff) {
646 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
651 b
= new_block(JMP(jtype
));
654 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
660 * Various code constructs need to know the layout of the data link
661 * layer. These variables give the necessary offsets from the beginning
662 * of the packet data.
664 * If the link layer has variable_length headers, the offsets are offsets
665 * from the end of the link-link-layer header, and "reg_ll_size" is
666 * the register number for a register containing the length of the
667 * link-layer header. Otherwise, "reg_ll_size" is -1.
669 static int reg_ll_size
;
672 * This is the offset of the beginning of the link-layer header.
673 * It's usually 0, except for 802.11 with a fixed-length radio header.
678 * This is the offset of the beginning of the MAC-layer header.
679 * It's usually 0, except for ATM LANE.
681 static u_int off_mac
;
684 * "off_linktype" is the offset to information in the link-layer header
685 * giving the packet type.
687 * For Ethernet, it's the offset of the Ethernet type field.
689 * For link-layer types that always use 802.2 headers, it's the
690 * offset of the LLC header.
692 * For PPP, it's the offset of the PPP type field.
694 * For Cisco HDLC, it's the offset of the CHDLC type field.
696 * For BSD loopback, it's the offset of the AF_ value.
698 * For Linux cooked sockets, it's the offset of the type field.
700 * It's set to -1 for no encapsulation, in which case, IP is assumed.
702 static u_int off_linktype
;
705 * TRUE if the link layer includes an ATM pseudo-header.
707 static int is_atm
= 0;
710 * TRUE if "lane" appeared in the filter; it causes us to generate
711 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
713 static int is_lane
= 0;
716 * These are offsets for the ATM pseudo-header.
718 static u_int off_vpi
;
719 static u_int off_vci
;
720 static u_int off_proto
;
723 * These are offsets for the MTP3 fields.
725 static u_int off_sio
;
726 static u_int off_opc
;
727 static u_int off_dpc
;
728 static u_int off_sls
;
731 * This is the offset of the first byte after the ATM pseudo_header,
732 * or -1 if there is no ATM pseudo-header.
734 static u_int off_payload
;
737 * These are offsets to the beginning of the network-layer header.
739 * If the link layer never uses 802.2 LLC:
741 * "off_nl" and "off_nl_nosnap" are the same.
743 * If the link layer always uses 802.2 LLC:
745 * "off_nl" is the offset if there's a SNAP header following
748 * "off_nl_nosnap" is the offset if there's no SNAP header.
750 * If the link layer is Ethernet:
752 * "off_nl" is the offset if the packet is an Ethernet II packet
753 * (we assume no 802.3+802.2+SNAP);
755 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
756 * with an 802.2 header following it.
759 static u_int off_nl_nosnap
;
767 linktype
= pcap_datalink(p
);
769 pcap_fddipad
= p
->fddipad
;
773 * Assume it's not raw ATM with a pseudo-header, for now.
784 * And assume we're not doing SS7.
792 * Also assume it's not 802.11 with a fixed-length radio header.
798 label_stack_depth
= 0;
806 off_nl
= 6; /* XXX in reality, variable! */
807 off_nl_nosnap
= 6; /* no 802.2 LLC */
810 case DLT_ARCNET_LINUX
:
812 off_nl
= 8; /* XXX in reality, variable! */
813 off_nl_nosnap
= 8; /* no 802.2 LLC */
818 off_nl
= 14; /* Ethernet II */
819 off_nl_nosnap
= 17; /* 802.3+802.2 */
824 * SLIP doesn't have a link level type. The 16 byte
825 * header is hacked into our SLIP driver.
829 off_nl_nosnap
= 16; /* no 802.2 LLC */
833 /* XXX this may be the same as the DLT_PPP_BSDOS case */
837 off_nl_nosnap
= 24; /* no 802.2 LLC */
844 off_nl_nosnap
= 4; /* no 802.2 LLC */
850 off_nl_nosnap
= 12; /* no 802.2 LLC */
855 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
856 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
859 off_nl_nosnap
= 4; /* no 802.2 LLC */
864 * This does no include the Ethernet header, and
865 * only covers session state.
869 off_nl_nosnap
= 8; /* no 802.2 LLC */
875 off_nl_nosnap
= 24; /* no 802.2 LLC */
880 * FDDI doesn't really have a link-level type field.
881 * We set "off_linktype" to the offset of the LLC header.
883 * To check for Ethernet types, we assume that SSAP = SNAP
884 * is being used and pick out the encapsulated Ethernet type.
885 * XXX - should we generate code to check for SNAP?
889 off_linktype
+= pcap_fddipad
;
891 off_nl
= 21; /* FDDI+802.2+SNAP */
892 off_nl_nosnap
= 16; /* FDDI+802.2 */
894 off_nl
+= pcap_fddipad
;
895 off_nl_nosnap
+= pcap_fddipad
;
901 * Token Ring doesn't really have a link-level type field.
902 * We set "off_linktype" to the offset of the LLC header.
904 * To check for Ethernet types, we assume that SSAP = SNAP
905 * is being used and pick out the encapsulated Ethernet type.
906 * XXX - should we generate code to check for SNAP?
908 * XXX - the header is actually variable-length.
909 * Some various Linux patched versions gave 38
910 * as "off_linktype" and 40 as "off_nl"; however,
911 * if a token ring packet has *no* routing
912 * information, i.e. is not source-routed, the correct
913 * values are 20 and 22, as they are in the vanilla code.
915 * A packet is source-routed iff the uppermost bit
916 * of the first byte of the source address, at an
917 * offset of 8, has the uppermost bit set. If the
918 * packet is source-routed, the total number of bytes
919 * of routing information is 2 plus bits 0x1F00 of
920 * the 16-bit value at an offset of 14 (shifted right
921 * 8 - figure out which byte that is).
924 off_nl
= 22; /* Token Ring+802.2+SNAP */
925 off_nl_nosnap
= 17; /* Token Ring+802.2 */
930 * 802.11 doesn't really have a link-level type field.
931 * We set "off_linktype" to the offset of the LLC header.
933 * To check for Ethernet types, we assume that SSAP = SNAP
934 * is being used and pick out the encapsulated Ethernet type.
935 * XXX - should we generate code to check for SNAP?
937 * XXX - the header is actually variable-length. We
938 * assume a 24-byte link-layer header, as appears in
939 * data frames in networks with no bridges. If the
940 * fromds and tods 802.11 header bits are both set,
941 * it's actually supposed to be 30 bytes.
944 off_nl
= 32; /* 802.11+802.2+SNAP */
945 off_nl_nosnap
= 27; /* 802.11+802.2 */
948 case DLT_PRISM_HEADER
:
950 * Same as 802.11, but with an additional header before
951 * the 802.11 header, containing a bunch of additional
952 * information including radio-level information.
954 * The header is 144 bytes long.
956 * XXX - same variable-length header problem; at least
957 * the Prism header is fixed-length.
960 off_linktype
= 144+24;
961 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
962 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
965 case DLT_IEEE802_11_RADIO_AVS
:
967 * Same as 802.11, but with an additional header before
968 * the 802.11 header, containing a bunch of additional
969 * information including radio-level information.
971 * The header is 64 bytes long, at least in its
972 * current incarnation.
974 * XXX - same variable-length header problem, only
975 * more so; this header is also variable-length,
976 * with the length being the 32-bit big-endian
977 * number at an offset of 4 from the beginning
978 * of the radio header.
981 off_linktype
= 64+24;
982 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
983 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
986 case DLT_IEEE802_11_RADIO
:
988 * Same as 802.11, but with an additional header before
989 * the 802.11 header, containing a bunch of additional
990 * information including radio-level information.
992 * The radiotap header is variable length, and we
993 * generate code to compute its length and store it
994 * in a register. These offsets are relative to the
995 * beginning of the 802.11 header.
998 off_nl
= 32; /* 802.11+802.2+SNAP */
999 off_nl_nosnap
= 27; /* 802.11+802.2 */
1002 case DLT_ATM_RFC1483
:
1003 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1005 * assume routed, non-ISO PDUs
1006 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1008 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1009 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1010 * latter would presumably be treated the way PPPoE
1011 * should be, so you can do "pppoe and udp port 2049"
1012 * or "pppoa and tcp port 80" and have it check for
1013 * PPPo{A,E} and a PPP protocol of IP and....
1016 off_nl
= 8; /* 802.2+SNAP */
1017 off_nl_nosnap
= 3; /* 802.2 */
1022 * Full Frontal ATM; you get AALn PDUs with an ATM
1026 off_vpi
= SUNATM_VPI_POS
;
1027 off_vci
= SUNATM_VCI_POS
;
1028 off_proto
= PROTO_POS
;
1029 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1030 off_payload
= SUNATM_PKT_BEGIN_POS
;
1031 off_linktype
= off_payload
;
1032 off_nl
= off_payload
+8; /* 802.2+SNAP */
1033 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1039 off_nl_nosnap
= 0; /* no 802.2 LLC */
1042 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1045 off_nl_nosnap
= 16; /* no 802.2 LLC */
1050 * LocalTalk does have a 1-byte type field in the LLAP header,
1051 * but really it just indicates whether there is a "short" or
1052 * "long" DDP packet following.
1056 off_nl_nosnap
= 0; /* no 802.2 LLC */
1059 case DLT_IP_OVER_FC
:
1061 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1062 * link-level type field. We set "off_linktype" to the
1063 * offset of the LLC header.
1065 * To check for Ethernet types, we assume that SSAP = SNAP
1066 * is being used and pick out the encapsulated Ethernet type.
1067 * XXX - should we generate code to check for SNAP? RFC
1068 * 2625 says SNAP should be used.
1071 off_nl
= 24; /* IPFC+802.2+SNAP */
1072 off_nl_nosnap
= 19; /* IPFC+802.2 */
1077 * XXX - we should set this to handle SNAP-encapsulated
1078 * frames (NLPID of 0x80).
1082 off_nl_nosnap
= 0; /* no 802.2 LLC */
1085 case DLT_APPLE_IP_OVER_IEEE1394
:
1088 off_nl_nosnap
= 18; /* no 802.2 LLC */
1091 case DLT_LINUX_IRDA
:
1093 * Currently, only raw "link[N:M]" filtering is supported.
1102 * Currently, only raw "link[N:M]" filtering is supported.
1109 case DLT_SYMANTEC_FIREWALL
:
1111 off_nl
= 44; /* Ethernet II */
1112 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1117 /* XXX read this from pf.h? */
1118 off_nl
= PFLOG_HDRLEN
;
1119 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1122 case DLT_JUNIPER_MFR
:
1123 case DLT_JUNIPER_MLFR
:
1124 case DLT_JUNIPER_MLPPP
:
1125 case DLT_JUNIPER_PPP
:
1126 case DLT_JUNIPER_CHDLC
:
1127 case DLT_JUNIPER_FRELAY
:
1130 off_nl_nosnap
= -1; /* no 802.2 LLC */
1133 case DLT_JUNIPER_ATM1
:
1134 off_linktype
= 4; /* in reality variable between 4-8 */
1139 case DLT_JUNIPER_ATM2
:
1140 off_linktype
= 8; /* in reality variable between 8-12 */
1145 /* frames captured on a Juniper PPPoE service PIC
1146 * contain raw ethernet frames */
1147 case DLT_JUNIPER_PPPOE
:
1148 case DLT_JUNIPER_ETHER
:
1150 off_nl
= 18; /* Ethernet II */
1151 off_nl_nosnap
= 21; /* 802.3+802.2 */
1154 case DLT_JUNIPER_PPPOE_ATM
:
1157 off_nl_nosnap
= -1; /* no 802.2 LLC */
1160 case DLT_JUNIPER_GGSN
:
1163 off_nl_nosnap
= -1; /* no 802.2 LLC */
1166 case DLT_JUNIPER_ES
:
1168 off_nl
= -1; /* not really a network layer but raw IP adresses */
1169 off_nl_nosnap
= -1; /* no 802.2 LLC */
1172 case DLT_JUNIPER_MONITOR
:
1174 off_nl
= 12; /* raw IP/IP6 header */
1175 off_nl_nosnap
= -1; /* no 802.2 LLC */
1178 case DLT_JUNIPER_SERVICES
:
1180 off_nl
= -1; /* L3 proto location dep. on cookie type */
1181 off_nl_nosnap
= -1; /* no 802.2 LLC */
1202 case DLT_LINUX_LAPD
:
1204 * Currently, only raw "link[N:M]" filtering is supported.
1211 bpf_error("unknown data link type %d", linktype
);
1216 * Load a value relative to the beginning of the link-layer header.
1217 * The link-layer header doesn't necessarily begin at the beginning
1218 * of the packet data; there might be a variable-length prefix containing
1219 * radio information.
1221 static struct slist
*
1222 gen_load_llrel(offset
, size
)
1225 struct slist
*s
, *s2
;
1227 s
= gen_llprefixlen();
1230 * If "s" is non-null, it has code to arrange that the X register
1231 * contains the length of the prefix preceding the link-layer
1235 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1239 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1246 * Load a value relative to the beginning of the specified header.
1248 static struct slist
*
1249 gen_load_a(offrel
, offset
, size
)
1250 enum e_offrel offrel
;
1253 struct slist
*s
, *s2
;
1258 s
= gen_load_llrel(offset
, size
);
1262 s
= gen_load_llrel(off_ll
+ offset
, size
);
1266 s
= gen_load_llrel(off_nl
+ offset
, size
);
1270 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1275 * Load the X register with the length of the IPv4 header,
1278 s
= gen_loadx_iphdrlen();
1281 * Load the item at {length of the link-layer header} +
1282 * {length of the IPv4 header} + {specified offset}.
1284 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1285 s2
->s
.k
= off_nl
+ offset
;
1290 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1301 * Generate code to load into the X register the sum of the length of
1302 * the IPv4 header and any variable-length header preceding the link-layer
1305 static struct slist
*
1306 gen_loadx_iphdrlen()
1308 struct slist
*s
, *s2
;
1310 s
= gen_llprefixlen();
1313 * There's a variable-length prefix preceding the
1314 * link-layer header. "s" points to a list of statements
1315 * that put the length of that prefix into the X register.
1316 * The 4*([k]&0xf) addressing mode can't be used, as we
1317 * don't have a constant offset, so we have to load the
1318 * value in question into the A register and add to it
1319 * the value from the X register.
1321 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1324 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1327 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1332 * The A register now contains the length of the
1333 * IP header. We need to add to it the length
1334 * of the prefix preceding the link-layer
1335 * header, which is still in the X register, and
1336 * move the result into the X register.
1338 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1339 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1342 * There is no variable-length header preceding the
1343 * link-layer header; if there's a fixed-length
1344 * header preceding it, its length is included in
1345 * the off_ variables, so it doesn't need to be added.
1347 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1353 static struct block
*
1360 s
= new_stmt(BPF_LD
|BPF_IMM
);
1362 b
= new_block(JMP(BPF_JEQ
));
1368 static inline struct block
*
1371 return gen_uncond(1);
1374 static inline struct block
*
1377 return gen_uncond(0);
1381 * Byte-swap a 32-bit number.
1382 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1383 * big-endian platforms.)
1385 #define SWAPLONG(y) \
1386 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1389 * Generate code to match a particular packet type.
1391 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1392 * value, if <= ETHERMTU. We use that to determine whether to
1393 * match the type/length field or to check the type/length field for
1394 * a value <= ETHERMTU to see whether it's a type field and then do
1395 * the appropriate test.
1397 static struct block
*
1398 gen_ether_linktype(proto
)
1401 struct block
*b0
, *b1
;
1407 case LLCSAP_NETBEUI
:
1409 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1410 * so we check the DSAP and SSAP.
1412 * LLCSAP_IP checks for IP-over-802.2, rather
1413 * than IP-over-Ethernet or IP-over-SNAP.
1415 * XXX - should we check both the DSAP and the
1416 * SSAP, like this, or should we check just the
1417 * DSAP, as we do for other types <= ETHERMTU
1418 * (i.e., other SAP values)?
1420 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1422 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1423 ((proto
<< 8) | proto
));
1431 * Ethernet_II frames, which are Ethernet
1432 * frames with a frame type of ETHERTYPE_IPX;
1434 * Ethernet_802.3 frames, which are 802.3
1435 * frames (i.e., the type/length field is
1436 * a length field, <= ETHERMTU, rather than
1437 * a type field) with the first two bytes
1438 * after the Ethernet/802.3 header being
1441 * Ethernet_802.2 frames, which are 802.3
1442 * frames with an 802.2 LLC header and
1443 * with the IPX LSAP as the DSAP in the LLC
1446 * Ethernet_SNAP frames, which are 802.3
1447 * frames with an LLC header and a SNAP
1448 * header and with an OUI of 0x000000
1449 * (encapsulated Ethernet) and a protocol
1450 * ID of ETHERTYPE_IPX in the SNAP header.
1452 * XXX - should we generate the same code both
1453 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1457 * This generates code to check both for the
1458 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1460 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1461 (bpf_int32
)LLCSAP_IPX
);
1462 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1467 * Now we add code to check for SNAP frames with
1468 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1470 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1474 * Now we generate code to check for 802.3
1475 * frames in general.
1477 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1481 * Now add the check for 802.3 frames before the
1482 * check for Ethernet_802.2 and Ethernet_802.3,
1483 * as those checks should only be done on 802.3
1484 * frames, not on Ethernet frames.
1489 * Now add the check for Ethernet_II frames, and
1490 * do that before checking for the other frame
1493 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1494 (bpf_int32
)ETHERTYPE_IPX
);
1498 case ETHERTYPE_ATALK
:
1499 case ETHERTYPE_AARP
:
1501 * EtherTalk (AppleTalk protocols on Ethernet link
1502 * layer) may use 802.2 encapsulation.
1506 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1507 * we check for an Ethernet type field less than
1508 * 1500, which means it's an 802.3 length field.
1510 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1514 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1515 * SNAP packets with an organization code of
1516 * 0x080007 (Apple, for Appletalk) and a protocol
1517 * type of ETHERTYPE_ATALK (Appletalk).
1519 * 802.2-encapsulated ETHERTYPE_AARP packets are
1520 * SNAP packets with an organization code of
1521 * 0x000000 (encapsulated Ethernet) and a protocol
1522 * type of ETHERTYPE_AARP (Appletalk ARP).
1524 if (proto
== ETHERTYPE_ATALK
)
1525 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1526 else /* proto == ETHERTYPE_AARP */
1527 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1531 * Check for Ethernet encapsulation (Ethertalk
1532 * phase 1?); we just check for the Ethernet
1535 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1541 if (proto
<= ETHERMTU
) {
1543 * This is an LLC SAP value, so the frames
1544 * that match would be 802.2 frames.
1545 * Check that the frame is an 802.2 frame
1546 * (i.e., that the length/type field is
1547 * a length field, <= ETHERMTU) and
1548 * then check the DSAP.
1550 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1552 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1558 * This is an Ethernet type, so compare
1559 * the length/type field with it (if
1560 * the frame is an 802.2 frame, the length
1561 * field will be <= ETHERMTU, and, as
1562 * "proto" is > ETHERMTU, this test
1563 * will fail and the frame won't match,
1564 * which is what we want).
1566 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1573 * Generate code to match a particular packet type.
1575 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1576 * value, if <= ETHERMTU. We use that to determine whether to
1577 * match the type field or to check the type field for the special
1578 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1580 static struct block
*
1581 gen_linux_sll_linktype(proto
)
1584 struct block
*b0
, *b1
;
1590 case LLCSAP_NETBEUI
:
1592 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1593 * so we check the DSAP and SSAP.
1595 * LLCSAP_IP checks for IP-over-802.2, rather
1596 * than IP-over-Ethernet or IP-over-SNAP.
1598 * XXX - should we check both the DSAP and the
1599 * SSAP, like this, or should we check just the
1600 * DSAP, as we do for other types <= ETHERMTU
1601 * (i.e., other SAP values)?
1603 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1604 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1605 ((proto
<< 8) | proto
));
1611 * Ethernet_II frames, which are Ethernet
1612 * frames with a frame type of ETHERTYPE_IPX;
1614 * Ethernet_802.3 frames, which have a frame
1615 * type of LINUX_SLL_P_802_3;
1617 * Ethernet_802.2 frames, which are 802.3
1618 * frames with an 802.2 LLC header (i.e, have
1619 * a frame type of LINUX_SLL_P_802_2) and
1620 * with the IPX LSAP as the DSAP in the LLC
1623 * Ethernet_SNAP frames, which are 802.3
1624 * frames with an LLC header and a SNAP
1625 * header and with an OUI of 0x000000
1626 * (encapsulated Ethernet) and a protocol
1627 * ID of ETHERTYPE_IPX in the SNAP header.
1629 * First, do the checks on LINUX_SLL_P_802_2
1630 * frames; generate the check for either
1631 * Ethernet_802.2 or Ethernet_SNAP frames, and
1632 * then put a check for LINUX_SLL_P_802_2 frames
1635 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1636 (bpf_int32
)LLCSAP_IPX
);
1637 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1640 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1644 * Now check for 802.3 frames and OR that with
1645 * the previous test.
1647 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1651 * Now add the check for Ethernet_II frames, and
1652 * do that before checking for the other frame
1655 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1656 (bpf_int32
)ETHERTYPE_IPX
);
1660 case ETHERTYPE_ATALK
:
1661 case ETHERTYPE_AARP
:
1663 * EtherTalk (AppleTalk protocols on Ethernet link
1664 * layer) may use 802.2 encapsulation.
1668 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1669 * we check for the 802.2 protocol type in the
1670 * "Ethernet type" field.
1672 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1675 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1676 * SNAP packets with an organization code of
1677 * 0x080007 (Apple, for Appletalk) and a protocol
1678 * type of ETHERTYPE_ATALK (Appletalk).
1680 * 802.2-encapsulated ETHERTYPE_AARP packets are
1681 * SNAP packets with an organization code of
1682 * 0x000000 (encapsulated Ethernet) and a protocol
1683 * type of ETHERTYPE_AARP (Appletalk ARP).
1685 if (proto
== ETHERTYPE_ATALK
)
1686 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1688 else /* proto == ETHERTYPE_AARP */
1689 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1694 * Check for Ethernet encapsulation (Ethertalk
1695 * phase 1?); we just check for the Ethernet
1698 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1704 if (proto
<= ETHERMTU
) {
1706 * This is an LLC SAP value, so the frames
1707 * that match would be 802.2 frames.
1708 * Check for the 802.2 protocol type
1709 * in the "Ethernet type" field, and
1710 * then check the DSAP.
1712 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1714 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1720 * This is an Ethernet type, so compare
1721 * the length/type field with it (if
1722 * the frame is an 802.2 frame, the length
1723 * field will be <= ETHERMTU, and, as
1724 * "proto" is > ETHERMTU, this test
1725 * will fail and the frame won't match,
1726 * which is what we want).
1728 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1735 insert_radiotap_load_llprefixlen(b
)
1738 struct slist
*s1
, *s2
;
1741 * Prepend to the statements in this block code to load the
1742 * length of the radiotap header into the register assigned
1743 * to hold that length, if one has been assigned.
1745 if (reg_ll_size
!= -1) {
1747 * The 2 bytes at offsets of 2 and 3 from the beginning
1748 * of the radiotap header are the length of the radiotap
1749 * header; unfortunately, it's little-endian, so we have
1750 * to load it a byte at a time and construct the value.
1754 * Load the high-order byte, at an offset of 3, shift it
1755 * left a byte, and put the result in the X register.
1757 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1759 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1762 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1766 * Load the next byte, at an offset of 2, and OR the
1767 * value from the X register into it.
1769 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1772 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1776 * Now allocate a register to hold that value and store
1779 s2
= new_stmt(BPF_ST
);
1780 s2
->s
.k
= reg_ll_size
;
1784 * Now move it into the X register.
1786 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1790 * Now append all the existing statements in this
1791 * block to these statements.
1793 sappend(s1
, b
->stmts
);
1800 insert_load_llprefixlen(b
)
1805 case DLT_IEEE802_11_RADIO
:
1806 insert_radiotap_load_llprefixlen(b
);
1811 static struct slist
*
1812 gen_radiotap_llprefixlen(void)
1816 if (reg_ll_size
== -1) {
1818 * We haven't yet assigned a register for the length
1819 * of the radiotap header; allocate one.
1821 reg_ll_size
= alloc_reg();
1825 * Load the register containing the radiotap length
1826 * into the X register.
1828 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1829 s
->s
.k
= reg_ll_size
;
1834 * Generate code to compute the link-layer header length, if necessary,
1835 * putting it into the X register, and to return either a pointer to a
1836 * "struct slist" for the list of statements in that code, or NULL if
1837 * no code is necessary.
1839 static struct slist
*
1840 gen_llprefixlen(void)
1844 case DLT_IEEE802_11_RADIO
:
1845 return gen_radiotap_llprefixlen();
1853 * Generate code to match a particular packet type by matching the
1854 * link-layer type field or fields in the 802.2 LLC header.
1856 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1857 * value, if <= ETHERMTU.
1859 static struct block
*
1863 struct block
*b0
, *b1
, *b2
;
1865 /* are we checking MPLS-encapsulated packets? */
1866 if (label_stack_depth
> 0) {
1870 /* FIXME add other L3 proto IDs */
1871 return gen_mpls_linktype(Q_IP
);
1873 case ETHERTYPE_IPV6
:
1875 /* FIXME add other L3 proto IDs */
1876 return gen_mpls_linktype(Q_IPV6
);
1879 bpf_error("unsupported protocol over mpls");
1887 return gen_ether_linktype(proto
);
1895 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1899 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1908 case DLT_IEEE802_11
:
1909 case DLT_IEEE802_11_RADIO_AVS
:
1910 case DLT_IEEE802_11_RADIO
:
1911 case DLT_PRISM_HEADER
:
1912 case DLT_ATM_RFC1483
:
1914 case DLT_IP_OVER_FC
:
1915 return gen_llc_linktype(proto
);
1921 * If "is_lane" is set, check for a LANE-encapsulated
1922 * version of this protocol, otherwise check for an
1923 * LLC-encapsulated version of this protocol.
1925 * We assume LANE means Ethernet, not Token Ring.
1929 * Check that the packet doesn't begin with an
1930 * LE Control marker. (We've already generated
1933 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1938 * Now generate an Ethernet test.
1940 b1
= gen_ether_linktype(proto
);
1945 * Check for LLC encapsulation and then check the
1948 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1949 b1
= gen_llc_linktype(proto
);
1957 return gen_linux_sll_linktype(proto
);
1962 case DLT_SLIP_BSDOS
:
1965 * These types don't provide any type field; packets
1968 * XXX - for IPv4, check for a version number of 4, and,
1969 * for IPv6, check for a version number of 6?
1975 case ETHERTYPE_IPV6
:
1977 return gen_true(); /* always true */
1980 return gen_false(); /* always false */
1987 case DLT_PPP_SERIAL
:
1990 * We use Ethernet protocol types inside libpcap;
1991 * map them to the corresponding PPP protocol types.
2000 case ETHERTYPE_IPV6
:
2009 case ETHERTYPE_ATALK
:
2023 * I'm assuming the "Bridging PDU"s that go
2024 * over PPP are Spanning Tree Protocol
2038 * We use Ethernet protocol types inside libpcap;
2039 * map them to the corresponding PPP protocol types.
2044 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2045 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2047 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2052 case ETHERTYPE_IPV6
:
2062 case ETHERTYPE_ATALK
:
2076 * I'm assuming the "Bridging PDU"s that go
2077 * over PPP are Spanning Tree Protocol
2093 * For DLT_NULL, the link-layer header is a 32-bit
2094 * word containing an AF_ value in *host* byte order,
2095 * and for DLT_ENC, the link-layer header begins
2096 * with a 32-bit work containing an AF_ value in
2099 * In addition, if we're reading a saved capture file,
2100 * the host byte order in the capture may not be the
2101 * same as the host byte order on this machine.
2103 * For DLT_LOOP, the link-layer header is a 32-bit
2104 * word containing an AF_ value in *network* byte order.
2106 * XXX - AF_ values may, unfortunately, be platform-
2107 * dependent; for example, FreeBSD's AF_INET6 is 24
2108 * whilst NetBSD's and OpenBSD's is 26.
2110 * This means that, when reading a capture file, just
2111 * checking for our AF_INET6 value won't work if the
2112 * capture file came from another OS.
2121 case ETHERTYPE_IPV6
:
2128 * Not a type on which we support filtering.
2129 * XXX - support those that have AF_ values
2130 * #defined on this platform, at least?
2135 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2137 * The AF_ value is in host byte order, but
2138 * the BPF interpreter will convert it to
2139 * network byte order.
2141 * If this is a save file, and it's from a
2142 * machine with the opposite byte order to
2143 * ours, we byte-swap the AF_ value.
2145 * Then we run it through "htonl()", and
2146 * generate code to compare against the result.
2148 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2149 bpf_pcap
->sf
.swapped
)
2150 proto
= SWAPLONG(proto
);
2151 proto
= htonl(proto
);
2153 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2157 * af field is host byte order in contrast to the rest of
2160 if (proto
== ETHERTYPE_IP
)
2161 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2162 BPF_B
, (bpf_int32
)AF_INET
));
2164 else if (proto
== ETHERTYPE_IPV6
)
2165 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2166 BPF_B
, (bpf_int32
)AF_INET6
));
2174 case DLT_ARCNET_LINUX
:
2176 * XXX should we check for first fragment if the protocol
2185 case ETHERTYPE_IPV6
:
2186 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2187 (bpf_int32
)ARCTYPE_INET6
));
2191 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2192 (bpf_int32
)ARCTYPE_IP
);
2193 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2194 (bpf_int32
)ARCTYPE_IP_OLD
);
2199 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2200 (bpf_int32
)ARCTYPE_ARP
);
2201 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2202 (bpf_int32
)ARCTYPE_ARP_OLD
);
2206 case ETHERTYPE_REVARP
:
2207 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2208 (bpf_int32
)ARCTYPE_REVARP
));
2210 case ETHERTYPE_ATALK
:
2211 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2212 (bpf_int32
)ARCTYPE_ATALK
));
2219 case ETHERTYPE_ATALK
:
2229 * XXX - assumes a 2-byte Frame Relay header with
2230 * DLCI and flags. What if the address is longer?
2236 * Check for the special NLPID for IP.
2238 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2241 case ETHERTYPE_IPV6
:
2243 * Check for the special NLPID for IPv6.
2245 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2250 * Check for several OSI protocols.
2252 * Frame Relay packets typically have an OSI
2253 * NLPID at the beginning; we check for each
2256 * What we check for is the NLPID and a frame
2257 * control field of UI, i.e. 0x03 followed
2260 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2261 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2262 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2273 case DLT_JUNIPER_MFR
:
2274 case DLT_JUNIPER_MLFR
:
2275 case DLT_JUNIPER_MLPPP
:
2276 case DLT_JUNIPER_ATM1
:
2277 case DLT_JUNIPER_ATM2
:
2278 case DLT_JUNIPER_PPPOE
:
2279 case DLT_JUNIPER_PPPOE_ATM
:
2280 case DLT_JUNIPER_GGSN
:
2281 case DLT_JUNIPER_ES
:
2282 case DLT_JUNIPER_MONITOR
:
2283 case DLT_JUNIPER_SERVICES
:
2284 case DLT_JUNIPER_ETHER
:
2285 case DLT_JUNIPER_PPP
:
2286 case DLT_JUNIPER_FRELAY
:
2287 case DLT_JUNIPER_CHDLC
:
2288 /* just lets verify the magic number for now -
2289 * on ATM we may have up to 6 different encapsulations on the wire
2290 * and need a lot of heuristics to figure out that the payload
2293 * FIXME encapsulation specific BPF_ filters
2295 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2297 case DLT_LINUX_IRDA
:
2298 bpf_error("IrDA link-layer type filtering not implemented");
2301 bpf_error("DOCSIS link-layer type filtering not implemented");
2303 case DLT_LINUX_LAPD
:
2304 bpf_error("LAPD link-layer type filtering not implemented");
2308 * All the types that have no encapsulation should either be
2309 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2310 * all packets are IP packets, or should be handled in some
2311 * special case, if none of them are (if some are and some
2312 * aren't, the lack of encapsulation is a problem, as we'd
2313 * have to find some other way of determining the packet type).
2315 * Therefore, if "off_linktype" is -1, there's an error.
2317 if (off_linktype
== (u_int
)-1)
2321 * Any type not handled above should always have an Ethernet
2322 * type at an offset of "off_linktype". (PPP is partially
2323 * handled above - the protocol type is mapped from the
2324 * Ethernet and LLC types we use internally to the corresponding
2325 * PPP type - but the PPP type is always specified by a value
2326 * at "off_linktype", so we don't have to do the code generation
2329 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2333 * Check for an LLC SNAP packet with a given organization code and
2334 * protocol type; we check the entire contents of the 802.2 LLC and
2335 * snap headers, checking for DSAP and SSAP of SNAP and a control
2336 * field of 0x03 in the LLC header, and for the specified organization
2337 * code and protocol type in the SNAP header.
2339 static struct block
*
2340 gen_snap(orgcode
, ptype
, offset
)
2341 bpf_u_int32 orgcode
;
2345 u_char snapblock
[8];
2347 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2348 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2349 snapblock
[2] = 0x03; /* control = UI */
2350 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2351 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2352 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2353 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2354 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2355 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2359 * Generate code to match a particular packet type, for link-layer types
2360 * using 802.2 LLC headers.
2362 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2363 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2365 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2366 * value, if <= ETHERMTU. We use that to determine whether to
2367 * match the DSAP or both DSAP and LSAP or to check the OUI and
2368 * protocol ID in a SNAP header.
2370 static struct block
*
2371 gen_llc_linktype(proto
)
2375 * XXX - handle token-ring variable-length header.
2381 case LLCSAP_NETBEUI
:
2383 * XXX - should we check both the DSAP and the
2384 * SSAP, like this, or should we check just the
2385 * DSAP, as we do for other types <= ETHERMTU
2386 * (i.e., other SAP values)?
2388 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2389 ((proto
<< 8) | proto
));
2393 * XXX - are there ever SNAP frames for IPX on
2394 * non-Ethernet 802.x networks?
2396 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2397 (bpf_int32
)LLCSAP_IPX
);
2399 case ETHERTYPE_ATALK
:
2401 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2402 * SNAP packets with an organization code of
2403 * 0x080007 (Apple, for Appletalk) and a protocol
2404 * type of ETHERTYPE_ATALK (Appletalk).
2406 * XXX - check for an organization code of
2407 * encapsulated Ethernet as well?
2409 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2413 * XXX - we don't have to check for IPX 802.3
2414 * here, but should we check for the IPX Ethertype?
2416 if (proto
<= ETHERMTU
) {
2418 * This is an LLC SAP value, so check
2421 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2425 * This is an Ethernet type; we assume that it's
2426 * unlikely that it'll appear in the right place
2427 * at random, and therefore check only the
2428 * location that would hold the Ethernet type
2429 * in a SNAP frame with an organization code of
2430 * 0x000000 (encapsulated Ethernet).
2432 * XXX - if we were to check for the SNAP DSAP and
2433 * LSAP, as per XXX, and were also to check for an
2434 * organization code of 0x000000 (encapsulated
2435 * Ethernet), we'd do
2437 * return gen_snap(0x000000, proto,
2440 * here; for now, we don't, as per the above.
2441 * I don't know whether it's worth the extra CPU
2442 * time to do the right check or not.
2444 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2450 static struct block
*
2451 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2455 u_int src_off
, dst_off
;
2457 struct block
*b0
, *b1
;
2471 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2472 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2478 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2479 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2486 b0
= gen_linktype(proto
);
2487 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2493 static struct block
*
2494 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2495 struct in6_addr
*addr
;
2496 struct in6_addr
*mask
;
2498 u_int src_off
, dst_off
;
2500 struct block
*b0
, *b1
;
2515 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2516 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2522 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2523 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2530 /* this order is important */
2531 a
= (u_int32_t
*)addr
;
2532 m
= (u_int32_t
*)mask
;
2533 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2534 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2536 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2538 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2540 b0
= gen_linktype(proto
);
2546 static struct block
*
2547 gen_ehostop(eaddr
, dir
)
2548 register const u_char
*eaddr
;
2551 register struct block
*b0
, *b1
;
2555 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2558 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2561 b0
= gen_ehostop(eaddr
, Q_SRC
);
2562 b1
= gen_ehostop(eaddr
, Q_DST
);
2568 b0
= gen_ehostop(eaddr
, Q_SRC
);
2569 b1
= gen_ehostop(eaddr
, Q_DST
);
2578 * Like gen_ehostop, but for DLT_FDDI
2580 static struct block
*
2581 gen_fhostop(eaddr
, dir
)
2582 register const u_char
*eaddr
;
2585 struct block
*b0
, *b1
;
2590 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2592 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2597 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2599 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2603 b0
= gen_fhostop(eaddr
, Q_SRC
);
2604 b1
= gen_fhostop(eaddr
, Q_DST
);
2610 b0
= gen_fhostop(eaddr
, Q_SRC
);
2611 b1
= gen_fhostop(eaddr
, Q_DST
);
2620 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2622 static struct block
*
2623 gen_thostop(eaddr
, dir
)
2624 register const u_char
*eaddr
;
2627 register struct block
*b0
, *b1
;
2631 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2634 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2637 b0
= gen_thostop(eaddr
, Q_SRC
);
2638 b1
= gen_thostop(eaddr
, Q_DST
);
2644 b0
= gen_thostop(eaddr
, Q_SRC
);
2645 b1
= gen_thostop(eaddr
, Q_DST
);
2654 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2656 static struct block
*
2657 gen_wlanhostop(eaddr
, dir
)
2658 register const u_char
*eaddr
;
2661 register struct block
*b0
, *b1
, *b2
;
2662 register struct slist
*s
;
2669 * For control frames, there is no SA.
2671 * For management frames, SA is at an
2672 * offset of 10 from the beginning of
2675 * For data frames, SA is at an offset
2676 * of 10 from the beginning of the packet
2677 * if From DS is clear, at an offset of
2678 * 16 from the beginning of the packet
2679 * if From DS is set and To DS is clear,
2680 * and an offset of 24 from the beginning
2681 * of the packet if From DS is set and To DS
2686 * Generate the tests to be done for data frames
2689 * First, check for To DS set, i.e. check "link[1] & 0x01".
2691 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2692 b1
= new_block(JMP(BPF_JSET
));
2693 b1
->s
.k
= 0x01; /* To DS */
2697 * If To DS is set, the SA is at 24.
2699 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2703 * Now, check for To DS not set, i.e. check
2704 * "!(link[1] & 0x01)".
2706 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2707 b2
= new_block(JMP(BPF_JSET
));
2708 b2
->s
.k
= 0x01; /* To DS */
2713 * If To DS is not set, the SA is at 16.
2715 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2719 * Now OR together the last two checks. That gives
2720 * the complete set of checks for data frames with
2726 * Now check for From DS being set, and AND that with
2727 * the ORed-together checks.
2729 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2730 b1
= new_block(JMP(BPF_JSET
));
2731 b1
->s
.k
= 0x02; /* From DS */
2736 * Now check for data frames with From DS not set.
2738 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2739 b2
= new_block(JMP(BPF_JSET
));
2740 b2
->s
.k
= 0x02; /* From DS */
2745 * If From DS isn't set, the SA is at 10.
2747 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2751 * Now OR together the checks for data frames with
2752 * From DS not set and for data frames with From DS
2753 * set; that gives the checks done for data frames.
2758 * Now check for a data frame.
2759 * I.e, check "link[0] & 0x08".
2761 gen_load_a(OR_LINK
, 0, BPF_B
);
2762 b1
= new_block(JMP(BPF_JSET
));
2767 * AND that with the checks done for data frames.
2772 * If the high-order bit of the type value is 0, this
2773 * is a management frame.
2774 * I.e, check "!(link[0] & 0x08)".
2776 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2777 b2
= new_block(JMP(BPF_JSET
));
2783 * For management frames, the SA is at 10.
2785 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2789 * OR that with the checks done for data frames.
2790 * That gives the checks done for management and
2796 * If the low-order bit of the type value is 1,
2797 * this is either a control frame or a frame
2798 * with a reserved type, and thus not a
2801 * I.e., check "!(link[0] & 0x04)".
2803 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2804 b1
= new_block(JMP(BPF_JSET
));
2810 * AND that with the checks for data and management
2820 * For control frames, there is no DA.
2822 * For management frames, DA is at an
2823 * offset of 4 from the beginning of
2826 * For data frames, DA is at an offset
2827 * of 4 from the beginning of the packet
2828 * if To DS is clear and at an offset of
2829 * 16 from the beginning of the packet
2834 * Generate the tests to be done for data frames.
2836 * First, check for To DS set, i.e. "link[1] & 0x01".
2838 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2839 b1
= new_block(JMP(BPF_JSET
));
2840 b1
->s
.k
= 0x01; /* To DS */
2844 * If To DS is set, the DA is at 16.
2846 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2850 * Now, check for To DS not set, i.e. check
2851 * "!(link[1] & 0x01)".
2853 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2854 b2
= new_block(JMP(BPF_JSET
));
2855 b2
->s
.k
= 0x01; /* To DS */
2860 * If To DS is not set, the DA is at 4.
2862 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2866 * Now OR together the last two checks. That gives
2867 * the complete set of checks for data frames.
2872 * Now check for a data frame.
2873 * I.e, check "link[0] & 0x08".
2875 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2876 b1
= new_block(JMP(BPF_JSET
));
2881 * AND that with the checks done for data frames.
2886 * If the high-order bit of the type value is 0, this
2887 * is a management frame.
2888 * I.e, check "!(link[0] & 0x08)".
2890 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2891 b2
= new_block(JMP(BPF_JSET
));
2897 * For management frames, the DA is at 4.
2899 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2903 * OR that with the checks done for data frames.
2904 * That gives the checks done for management and
2910 * If the low-order bit of the type value is 1,
2911 * this is either a control frame or a frame
2912 * with a reserved type, and thus not a
2915 * I.e., check "!(link[0] & 0x04)".
2917 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2918 b1
= new_block(JMP(BPF_JSET
));
2924 * AND that with the checks for data and management
2931 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2932 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2938 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2939 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2948 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2949 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2950 * as the RFC states.)
2952 static struct block
*
2953 gen_ipfchostop(eaddr
, dir
)
2954 register const u_char
*eaddr
;
2957 register struct block
*b0
, *b1
;
2961 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2964 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2967 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2968 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2974 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2975 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2984 * This is quite tricky because there may be pad bytes in front of the
2985 * DECNET header, and then there are two possible data packet formats that
2986 * carry both src and dst addresses, plus 5 packet types in a format that
2987 * carries only the src node, plus 2 types that use a different format and
2988 * also carry just the src node.
2992 * Instead of doing those all right, we just look for data packets with
2993 * 0 or 1 bytes of padding. If you want to look at other packets, that
2994 * will require a lot more hacking.
2996 * To add support for filtering on DECNET "areas" (network numbers)
2997 * one would want to add a "mask" argument to this routine. That would
2998 * make the filter even more inefficient, although one could be clever
2999 * and not generate masking instructions if the mask is 0xFFFF.
3001 static struct block
*
3002 gen_dnhostop(addr
, dir
)
3006 struct block
*b0
, *b1
, *b2
, *tmp
;
3007 u_int offset_lh
; /* offset if long header is received */
3008 u_int offset_sh
; /* offset if short header is received */
3013 offset_sh
= 1; /* follows flags */
3014 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3018 offset_sh
= 3; /* follows flags, dstnode */
3019 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3023 /* Inefficient because we do our Calvinball dance twice */
3024 b0
= gen_dnhostop(addr
, Q_SRC
);
3025 b1
= gen_dnhostop(addr
, Q_DST
);
3031 /* Inefficient because we do our Calvinball dance twice */
3032 b0
= gen_dnhostop(addr
, Q_SRC
);
3033 b1
= gen_dnhostop(addr
, Q_DST
);
3038 bpf_error("ISO host filtering not implemented");
3043 b0
= gen_linktype(ETHERTYPE_DN
);
3044 /* Check for pad = 1, long header case */
3045 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3046 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3047 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3048 BPF_H
, (bpf_int32
)ntohs(addr
));
3050 /* Check for pad = 0, long header case */
3051 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3052 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3055 /* Check for pad = 1, short header case */
3056 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3057 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3058 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3061 /* Check for pad = 0, short header case */
3062 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3063 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3067 /* Combine with test for linktype */
3073 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3074 * test the bottom-of-stack bit, and then check the version number
3075 * field in the IP header.
3077 static struct block
*
3078 gen_mpls_linktype(proto
)
3081 struct block
*b0
, *b1
;
3086 /* match the bottom-of-stack bit */
3087 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3088 /* match the IPv4 version number */
3089 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3094 /* match the bottom-of-stack bit */
3095 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3096 /* match the IPv4 version number */
3097 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3106 static struct block
*
3107 gen_host(addr
, mask
, proto
, dir
)
3113 struct block
*b0
, *b1
;
3118 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
3120 * Only check for non-IPv4 addresses if we're not
3121 * checking MPLS-encapsulated packets.
3123 if (label_stack_depth
== 0) {
3124 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
3126 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
3132 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3135 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3138 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3141 bpf_error("'tcp' modifier applied to host");
3144 bpf_error("'sctp' modifier applied to host");
3147 bpf_error("'udp' modifier applied to host");
3150 bpf_error("'icmp' modifier applied to host");
3153 bpf_error("'igmp' modifier applied to host");
3156 bpf_error("'igrp' modifier applied to host");
3159 bpf_error("'pim' modifier applied to host");
3162 bpf_error("'vrrp' modifier applied to host");
3165 bpf_error("ATALK host filtering not implemented");
3168 bpf_error("AARP host filtering not implemented");
3171 return gen_dnhostop(addr
, dir
);
3174 bpf_error("SCA host filtering not implemented");
3177 bpf_error("LAT host filtering not implemented");
3180 bpf_error("MOPDL host filtering not implemented");
3183 bpf_error("MOPRC host filtering not implemented");
3187 bpf_error("'ip6' modifier applied to ip host");
3190 bpf_error("'icmp6' modifier applied to host");
3194 bpf_error("'ah' modifier applied to host");
3197 bpf_error("'esp' modifier applied to host");
3200 bpf_error("ISO host filtering not implemented");
3203 bpf_error("'esis' modifier applied to host");
3206 bpf_error("'isis' modifier applied to host");
3209 bpf_error("'clnp' modifier applied to host");
3212 bpf_error("'stp' modifier applied to host");
3215 bpf_error("IPX host filtering not implemented");
3218 bpf_error("'netbeui' modifier applied to host");
3221 bpf_error("'radio' modifier applied to host");
3230 static struct block
*
3231 gen_host6(addr
, mask
, proto
, dir
)
3232 struct in6_addr
*addr
;
3233 struct in6_addr
*mask
;
3240 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3243 bpf_error("'ip' modifier applied to ip6 host");
3246 bpf_error("'rarp' modifier applied to ip6 host");
3249 bpf_error("'arp' modifier applied to ip6 host");
3252 bpf_error("'sctp' modifier applied to host");
3255 bpf_error("'tcp' modifier applied to host");
3258 bpf_error("'udp' modifier applied to host");
3261 bpf_error("'icmp' modifier applied to host");
3264 bpf_error("'igmp' modifier applied to host");
3267 bpf_error("'igrp' modifier applied to host");
3270 bpf_error("'pim' modifier applied to host");
3273 bpf_error("'vrrp' modifier applied to host");
3276 bpf_error("ATALK host filtering not implemented");
3279 bpf_error("AARP host filtering not implemented");
3282 bpf_error("'decnet' modifier applied to ip6 host");
3285 bpf_error("SCA host filtering not implemented");
3288 bpf_error("LAT host filtering not implemented");
3291 bpf_error("MOPDL host filtering not implemented");
3294 bpf_error("MOPRC host filtering not implemented");
3297 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3300 bpf_error("'icmp6' modifier applied to host");
3303 bpf_error("'ah' modifier applied to host");
3306 bpf_error("'esp' modifier applied to host");
3309 bpf_error("ISO host filtering not implemented");
3312 bpf_error("'esis' modifier applied to host");
3315 bpf_error("'isis' modifier applied to host");
3318 bpf_error("'clnp' modifier applied to host");
3321 bpf_error("'stp' modifier applied to host");
3324 bpf_error("IPX host filtering not implemented");
3327 bpf_error("'netbeui' modifier applied to host");
3330 bpf_error("'radio' modifier applied to host");
3340 static struct block
*
3341 gen_gateway(eaddr
, alist
, proto
, dir
)
3342 const u_char
*eaddr
;
3343 bpf_u_int32
**alist
;
3347 struct block
*b0
, *b1
, *tmp
;
3350 bpf_error("direction applied to 'gateway'");
3357 if (linktype
== DLT_EN10MB
)
3358 b0
= gen_ehostop(eaddr
, Q_OR
);
3359 else if (linktype
== DLT_FDDI
)
3360 b0
= gen_fhostop(eaddr
, Q_OR
);
3361 else if (linktype
== DLT_IEEE802
)
3362 b0
= gen_thostop(eaddr
, Q_OR
);
3363 else if (linktype
== DLT_IEEE802_11
||
3364 linktype
== DLT_IEEE802_11_RADIO_AVS
||
3365 linktype
== DLT_IEEE802_11_RADIO
||
3366 linktype
== DLT_PRISM_HEADER
)
3367 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3368 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3370 * Check that the packet doesn't begin with an
3371 * LE Control marker. (We've already generated
3374 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3379 * Now check the MAC address.
3381 b0
= gen_ehostop(eaddr
, Q_OR
);
3383 } else if (linktype
== DLT_IP_OVER_FC
)
3384 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3387 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3389 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3391 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3399 bpf_error("illegal modifier of 'gateway'");
3405 gen_proto_abbrev(proto
)
3414 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3416 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3422 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3424 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3430 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3432 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3438 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3441 #ifndef IPPROTO_IGMP
3442 #define IPPROTO_IGMP 2
3446 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3449 #ifndef IPPROTO_IGRP
3450 #define IPPROTO_IGRP 9
3453 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3457 #define IPPROTO_PIM 103
3461 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3463 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3468 #ifndef IPPROTO_VRRP
3469 #define IPPROTO_VRRP 112
3473 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3477 b1
= gen_linktype(ETHERTYPE_IP
);
3481 b1
= gen_linktype(ETHERTYPE_ARP
);
3485 b1
= gen_linktype(ETHERTYPE_REVARP
);
3489 bpf_error("link layer applied in wrong context");
3492 b1
= gen_linktype(ETHERTYPE_ATALK
);
3496 b1
= gen_linktype(ETHERTYPE_AARP
);
3500 b1
= gen_linktype(ETHERTYPE_DN
);
3504 b1
= gen_linktype(ETHERTYPE_SCA
);
3508 b1
= gen_linktype(ETHERTYPE_LAT
);
3512 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3516 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3521 b1
= gen_linktype(ETHERTYPE_IPV6
);
3524 #ifndef IPPROTO_ICMPV6
3525 #define IPPROTO_ICMPV6 58
3528 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3533 #define IPPROTO_AH 51
3536 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3538 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3544 #define IPPROTO_ESP 50
3547 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3549 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3555 b1
= gen_linktype(LLCSAP_ISONS
);
3559 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3563 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3566 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3567 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3568 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3570 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3572 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3574 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3578 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3579 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3580 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3582 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3584 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3586 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3590 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3591 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3592 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3594 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3599 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3600 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3605 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3606 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3608 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3610 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3615 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3616 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3621 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3622 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3627 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3631 b1
= gen_linktype(LLCSAP_8021D
);
3635 b1
= gen_linktype(LLCSAP_IPX
);
3639 b1
= gen_linktype(LLCSAP_NETBEUI
);
3643 bpf_error("'radio' is not a valid protocol type");
3651 static struct block
*
3658 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3659 b
= new_block(JMP(BPF_JSET
));
3668 * Generate a comparison to a port value in the transport-layer header
3669 * at the specified offset from the beginning of that header.
3671 * XXX - this handles a variable-length prefix preceding the link-layer
3672 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3673 * variable-length link-layer headers (such as Token Ring or 802.11
3676 static struct block
*
3677 gen_portatom(off
, v
)
3681 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3685 static struct block
*
3686 gen_portatom6(off
, v
)
3690 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3695 gen_portop(port
, proto
, dir
)
3696 int port
, proto
, dir
;
3698 struct block
*b0
, *b1
, *tmp
;
3700 /* ip proto 'proto' */
3701 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3707 b1
= gen_portatom(0, (bpf_int32
)port
);
3711 b1
= gen_portatom(2, (bpf_int32
)port
);
3716 tmp
= gen_portatom(0, (bpf_int32
)port
);
3717 b1
= gen_portatom(2, (bpf_int32
)port
);
3722 tmp
= gen_portatom(0, (bpf_int32
)port
);
3723 b1
= gen_portatom(2, (bpf_int32
)port
);
3735 static struct block
*
3736 gen_port(port
, ip_proto
, dir
)
3741 struct block
*b0
, *b1
, *tmp
;
3746 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3747 * not LLC encapsulation with LLCSAP_IP.
3749 * For IEEE 802 networks - which includes 802.5 token ring
3750 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3751 * says that SNAP encapsulation is used, not LLC encapsulation
3754 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3755 * RFC 2225 say that SNAP encapsulation is used, not LLC
3756 * encapsulation with LLCSAP_IP.
3758 * So we always check for ETHERTYPE_IP.
3760 b0
= gen_linktype(ETHERTYPE_IP
);
3766 b1
= gen_portop(port
, ip_proto
, dir
);
3770 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3771 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3773 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3786 gen_portop6(port
, proto
, dir
)
3787 int port
, proto
, dir
;
3789 struct block
*b0
, *b1
, *tmp
;
3791 /* ip6 proto 'proto' */
3792 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3796 b1
= gen_portatom6(0, (bpf_int32
)port
);
3800 b1
= gen_portatom6(2, (bpf_int32
)port
);
3805 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3806 b1
= gen_portatom6(2, (bpf_int32
)port
);
3811 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3812 b1
= gen_portatom6(2, (bpf_int32
)port
);
3824 static struct block
*
3825 gen_port6(port
, ip_proto
, dir
)
3830 struct block
*b0
, *b1
, *tmp
;
3832 /* link proto ip6 */
3833 b0
= gen_linktype(ETHERTYPE_IPV6
);
3839 b1
= gen_portop6(port
, ip_proto
, dir
);
3843 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3844 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3846 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3858 /* gen_portrange code */
3859 static struct block
*
3860 gen_portrangeatom(off
, v1
, v2
)
3864 struct block
*b1
, *b2
;
3868 * Reverse the order of the ports, so v1 is the lower one.
3877 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3878 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3886 gen_portrangeop(port1
, port2
, proto
, dir
)
3891 struct block
*b0
, *b1
, *tmp
;
3893 /* ip proto 'proto' */
3894 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3900 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3904 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3909 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3910 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3915 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3916 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3928 static struct block
*
3929 gen_portrange(port1
, port2
, ip_proto
, dir
)
3934 struct block
*b0
, *b1
, *tmp
;
3937 b0
= gen_linktype(ETHERTYPE_IP
);
3943 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3947 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3948 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3950 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3962 static struct block
*
3963 gen_portrangeatom6(off
, v1
, v2
)
3967 struct block
*b1
, *b2
;
3971 * Reverse the order of the ports, so v1 is the lower one.
3980 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3981 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3989 gen_portrangeop6(port1
, port2
, proto
, dir
)
3994 struct block
*b0
, *b1
, *tmp
;
3996 /* ip6 proto 'proto' */
3997 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4001 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4005 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4010 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4011 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4016 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4017 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4029 static struct block
*
4030 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4035 struct block
*b0
, *b1
, *tmp
;
4037 /* link proto ip6 */
4038 b0
= gen_linktype(ETHERTYPE_IPV6
);
4044 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4048 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4049 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4051 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4064 lookup_proto(name
, proto
)
4065 register const char *name
;
4075 v
= pcap_nametoproto(name
);
4076 if (v
== PROTO_UNDEF
)
4077 bpf_error("unknown ip proto '%s'", name
);
4081 /* XXX should look up h/w protocol type based on linktype */
4082 v
= pcap_nametoeproto(name
);
4083 if (v
== PROTO_UNDEF
) {
4084 v
= pcap_nametollc(name
);
4085 if (v
== PROTO_UNDEF
)
4086 bpf_error("unknown ether proto '%s'", name
);
4091 if (strcmp(name
, "esis") == 0)
4093 else if (strcmp(name
, "isis") == 0)
4095 else if (strcmp(name
, "clnp") == 0)
4098 bpf_error("unknown osi proto '%s'", name
);
4118 static struct block
*
4119 gen_protochain(v
, proto
, dir
)
4124 #ifdef NO_PROTOCHAIN
4125 return gen_proto(v
, proto
, dir
);
4127 struct block
*b0
, *b
;
4128 struct slist
*s
[100];
4129 int fix2
, fix3
, fix4
, fix5
;
4130 int ahcheck
, again
, end
;
4132 int reg2
= alloc_reg();
4134 memset(s
, 0, sizeof(s
));
4135 fix2
= fix3
= fix4
= fix5
= 0;
4142 b0
= gen_protochain(v
, Q_IP
, dir
);
4143 b
= gen_protochain(v
, Q_IPV6
, dir
);
4147 bpf_error("bad protocol applied for 'protochain'");
4152 * We don't handle variable-length radiotap here headers yet.
4153 * We might want to add BPF instructions to do the protochain
4154 * work, to simplify that and, on platforms that have a BPF
4155 * interpreter with the new instructions, let the filtering
4156 * be done in the kernel. (We already require a modified BPF
4157 * engine to do the protochain stuff, to support backward
4158 * branches, and backward branch support is unlikely to appear
4159 * in kernel BPF engines.)
4161 if (linktype
== DLT_IEEE802_11_RADIO
)
4162 bpf_error("'protochain' not supported with radiotap headers");
4164 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4167 * s[0] is a dummy entry to protect other BPF insn from damage
4168 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4169 * hard to find interdependency made by jump table fixup.
4172 s
[i
] = new_stmt(0); /*dummy*/
4177 b0
= gen_linktype(ETHERTYPE_IP
);
4180 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4181 s
[i
]->s
.k
= off_nl
+ 9;
4183 /* X = ip->ip_hl << 2 */
4184 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4190 b0
= gen_linktype(ETHERTYPE_IPV6
);
4192 /* A = ip6->ip_nxt */
4193 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4194 s
[i
]->s
.k
= off_nl
+ 6;
4196 /* X = sizeof(struct ip6_hdr) */
4197 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4203 bpf_error("unsupported proto to gen_protochain");
4207 /* again: if (A == v) goto end; else fall through; */
4209 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4211 s
[i
]->s
.jt
= NULL
; /*later*/
4212 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4216 #ifndef IPPROTO_NONE
4217 #define IPPROTO_NONE 59
4219 /* if (A == IPPROTO_NONE) goto end */
4220 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4221 s
[i
]->s
.jt
= NULL
; /*later*/
4222 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4223 s
[i
]->s
.k
= IPPROTO_NONE
;
4224 s
[fix5
]->s
.jf
= s
[i
];
4229 if (proto
== Q_IPV6
) {
4230 int v6start
, v6end
, v6advance
, j
;
4233 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4234 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4235 s
[i
]->s
.jt
= NULL
; /*later*/
4236 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4237 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4238 s
[fix2
]->s
.jf
= s
[i
];
4240 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4241 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4242 s
[i
]->s
.jt
= NULL
; /*later*/
4243 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4244 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4246 /* if (A == IPPROTO_ROUTING) goto v6advance */
4247 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4248 s
[i
]->s
.jt
= NULL
; /*later*/
4249 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4250 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4252 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4253 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4254 s
[i
]->s
.jt
= NULL
; /*later*/
4255 s
[i
]->s
.jf
= NULL
; /*later*/
4256 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4267 * X = X + (P[X + 1] + 1) * 8;
4270 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4272 /* A = P[X + packet head] */
4273 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4277 s
[i
] = new_stmt(BPF_ST
);
4281 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4284 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4288 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4290 /* A = P[X + packet head]; */
4291 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4295 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4299 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4303 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4306 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4310 /* goto again; (must use BPF_JA for backward jump) */
4311 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4312 s
[i
]->s
.k
= again
- i
- 1;
4313 s
[i
- 1]->s
.jf
= s
[i
];
4317 for (j
= v6start
; j
<= v6end
; j
++)
4318 s
[j
]->s
.jt
= s
[v6advance
];
4323 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4325 s
[fix2
]->s
.jf
= s
[i
];
4331 /* if (A == IPPROTO_AH) then fall through; else 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
; /*later*/
4335 s
[i
]->s
.k
= IPPROTO_AH
;
4337 s
[fix3
]->s
.jf
= s
[ahcheck
];
4344 * X = X + (P[X + 1] + 2) * 4;
4347 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4349 /* A = P[X + packet head]; */
4350 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4354 s
[i
] = new_stmt(BPF_ST
);
4358 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4361 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4365 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4367 /* A = P[X + packet head] */
4368 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4372 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4376 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4380 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4383 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4387 /* goto again; (must use BPF_JA for backward jump) */
4388 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4389 s
[i
]->s
.k
= again
- i
- 1;
4394 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4396 s
[fix2
]->s
.jt
= s
[end
];
4397 s
[fix4
]->s
.jf
= s
[end
];
4398 s
[fix5
]->s
.jt
= s
[end
];
4405 for (i
= 0; i
< max
- 1; i
++)
4406 s
[i
]->next
= s
[i
+ 1];
4407 s
[max
- 1]->next
= NULL
;
4412 b
= new_block(JMP(BPF_JEQ
));
4413 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4424 * Generate code that checks whether the packet is a packet for protocol
4425 * <proto> and whether the type field in that protocol's header has
4426 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4427 * IP packet and checks the protocol number in the IP header against <v>.
4429 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4430 * against Q_IP and Q_IPV6.
4432 static struct block
*
4433 gen_proto(v
, proto
, dir
)
4438 struct block
*b0
, *b1
;
4440 if (dir
!= Q_DEFAULT
)
4441 bpf_error("direction applied to 'proto'");
4446 b0
= gen_proto(v
, Q_IP
, dir
);
4447 b1
= gen_proto(v
, Q_IPV6
, dir
);
4455 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4456 * not LLC encapsulation with LLCSAP_IP.
4458 * For IEEE 802 networks - which includes 802.5 token ring
4459 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4460 * says that SNAP encapsulation is used, not LLC encapsulation
4463 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4464 * RFC 2225 say that SNAP encapsulation is used, not LLC
4465 * encapsulation with LLCSAP_IP.
4467 * So we always check for ETHERTYPE_IP.
4470 b0
= gen_linktype(ETHERTYPE_IP
);
4472 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4474 b1
= gen_protochain(v
, Q_IP
);
4484 * Frame Relay packets typically have an OSI
4485 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4486 * generates code to check for all the OSI
4487 * NLPIDs, so calling it and then adding a check
4488 * for the particular NLPID for which we're
4489 * looking is bogus, as we can just check for
4492 * What we check for is the NLPID and a frame
4493 * control field value of UI, i.e. 0x03 followed
4496 * XXX - assumes a 2-byte Frame Relay header with
4497 * DLCI and flags. What if the address is longer?
4499 * XXX - what about SNAP-encapsulated frames?
4501 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4507 * Cisco uses an Ethertype lookalike - for OSI,
4510 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4511 /* OSI in C-HDLC is stuffed with a fudge byte */
4512 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4517 b0
= gen_linktype(LLCSAP_ISONS
);
4518 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4524 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4526 * 4 is the offset of the PDU type relative to the IS-IS
4529 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4534 bpf_error("arp does not encapsulate another protocol");
4538 bpf_error("rarp does not encapsulate another protocol");
4542 bpf_error("atalk encapsulation is not specifiable");
4546 bpf_error("decnet encapsulation is not specifiable");
4550 bpf_error("sca does not encapsulate another protocol");
4554 bpf_error("lat does not encapsulate another protocol");
4558 bpf_error("moprc does not encapsulate another protocol");
4562 bpf_error("mopdl does not encapsulate another protocol");
4566 return gen_linktype(v
);
4569 bpf_error("'udp proto' is bogus");
4573 bpf_error("'tcp proto' is bogus");
4577 bpf_error("'sctp proto' is bogus");
4581 bpf_error("'icmp proto' is bogus");
4585 bpf_error("'igmp proto' is bogus");
4589 bpf_error("'igrp proto' is bogus");
4593 bpf_error("'pim proto' is bogus");
4597 bpf_error("'vrrp proto' is bogus");
4602 b0
= gen_linktype(ETHERTYPE_IPV6
);
4604 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4606 b1
= gen_protochain(v
, Q_IPV6
);
4612 bpf_error("'icmp6 proto' is bogus");
4616 bpf_error("'ah proto' is bogus");
4619 bpf_error("'ah proto' is bogus");
4622 bpf_error("'stp proto' is bogus");
4625 bpf_error("'ipx proto' is bogus");
4628 bpf_error("'netbeui proto' is bogus");
4631 bpf_error("'radio proto' is bogus");
4642 register const char *name
;
4645 int proto
= q
.proto
;
4649 bpf_u_int32 mask
, addr
;
4651 bpf_u_int32
**alist
;
4654 struct sockaddr_in
*sin4
;
4655 struct sockaddr_in6
*sin6
;
4656 struct addrinfo
*res
, *res0
;
4657 struct in6_addr mask128
;
4659 struct block
*b
, *tmp
;
4660 int port
, real_proto
;
4666 addr
= pcap_nametonetaddr(name
);
4668 bpf_error("unknown network '%s'", name
);
4669 /* Left justify network addr and calculate its network mask */
4671 while (addr
&& (addr
& 0xff000000) == 0) {
4675 return gen_host(addr
, mask
, proto
, dir
);
4679 if (proto
== Q_LINK
) {
4683 eaddr
= pcap_ether_hostton(name
);
4686 "unknown ether host '%s'", name
);
4687 b
= gen_ehostop(eaddr
, dir
);
4692 eaddr
= pcap_ether_hostton(name
);
4695 "unknown FDDI host '%s'", name
);
4696 b
= gen_fhostop(eaddr
, dir
);
4701 eaddr
= pcap_ether_hostton(name
);
4704 "unknown token ring host '%s'", name
);
4705 b
= gen_thostop(eaddr
, dir
);
4709 case DLT_IEEE802_11
:
4710 case DLT_IEEE802_11_RADIO_AVS
:
4711 case DLT_IEEE802_11_RADIO
:
4712 case DLT_PRISM_HEADER
:
4713 eaddr
= pcap_ether_hostton(name
);
4716 "unknown 802.11 host '%s'", name
);
4717 b
= gen_wlanhostop(eaddr
, dir
);
4721 case DLT_IP_OVER_FC
:
4722 eaddr
= pcap_ether_hostton(name
);
4725 "unknown Fibre Channel host '%s'", name
);
4726 b
= gen_ipfchostop(eaddr
, dir
);
4735 * Check that the packet doesn't begin
4736 * with an LE Control marker. (We've
4737 * already generated a test for LANE.)
4739 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4743 eaddr
= pcap_ether_hostton(name
);
4746 "unknown ether host '%s'", name
);
4747 b
= gen_ehostop(eaddr
, dir
);
4753 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4754 } else if (proto
== Q_DECNET
) {
4755 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4757 * I don't think DECNET hosts can be multihomed, so
4758 * there is no need to build up a list of addresses
4760 return (gen_host(dn_addr
, 0, proto
, dir
));
4763 alist
= pcap_nametoaddr(name
);
4764 if (alist
== NULL
|| *alist
== NULL
)
4765 bpf_error("unknown host '%s'", name
);
4767 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4769 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4771 tmp
= gen_host(**alist
++, 0xffffffff,
4778 memset(&mask128
, 0xff, sizeof(mask128
));
4779 res0
= res
= pcap_nametoaddrinfo(name
);
4781 bpf_error("unknown host '%s'", name
);
4783 tproto
= tproto6
= proto
;
4784 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4788 for (res
= res0
; res
; res
= res
->ai_next
) {
4789 switch (res
->ai_family
) {
4791 if (tproto
== Q_IPV6
)
4794 sin4
= (struct sockaddr_in
*)
4796 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
4797 0xffffffff, tproto
, dir
);
4800 if (tproto6
== Q_IP
)
4803 sin6
= (struct sockaddr_in6
*)
4805 tmp
= gen_host6(&sin6
->sin6_addr
,
4806 &mask128
, tproto6
, dir
);
4817 bpf_error("unknown host '%s'%s", name
,
4818 (proto
== Q_DEFAULT
)
4820 : " for specified address family");
4827 if (proto
!= Q_DEFAULT
&&
4828 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4829 bpf_error("illegal qualifier of 'port'");
4830 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4831 bpf_error("unknown port '%s'", name
);
4832 if (proto
== Q_UDP
) {
4833 if (real_proto
== IPPROTO_TCP
)
4834 bpf_error("port '%s' is tcp", name
);
4835 else if (real_proto
== IPPROTO_SCTP
)
4836 bpf_error("port '%s' is sctp", name
);
4838 /* override PROTO_UNDEF */
4839 real_proto
= IPPROTO_UDP
;
4841 if (proto
== Q_TCP
) {
4842 if (real_proto
== IPPROTO_UDP
)
4843 bpf_error("port '%s' is udp", name
);
4845 else if (real_proto
== IPPROTO_SCTP
)
4846 bpf_error("port '%s' is sctp", name
);
4848 /* override PROTO_UNDEF */
4849 real_proto
= IPPROTO_TCP
;
4851 if (proto
== Q_SCTP
) {
4852 if (real_proto
== IPPROTO_UDP
)
4853 bpf_error("port '%s' is udp", name
);
4855 else if (real_proto
== IPPROTO_TCP
)
4856 bpf_error("port '%s' is tcp", name
);
4858 /* override PROTO_UNDEF */
4859 real_proto
= IPPROTO_SCTP
;
4862 return gen_port(port
, real_proto
, dir
);
4865 b
= gen_port(port
, real_proto
, dir
);
4866 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4872 if (proto
!= Q_DEFAULT
&&
4873 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4874 bpf_error("illegal qualifier of 'portrange'");
4875 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4876 bpf_error("unknown port in range '%s'", name
);
4877 if (proto
== Q_UDP
) {
4878 if (real_proto
== IPPROTO_TCP
)
4879 bpf_error("port in range '%s' is tcp", name
);
4880 else if (real_proto
== IPPROTO_SCTP
)
4881 bpf_error("port in range '%s' is sctp", name
);
4883 /* override PROTO_UNDEF */
4884 real_proto
= IPPROTO_UDP
;
4886 if (proto
== Q_TCP
) {
4887 if (real_proto
== IPPROTO_UDP
)
4888 bpf_error("port in range '%s' is udp", name
);
4889 else if (real_proto
== IPPROTO_SCTP
)
4890 bpf_error("port in range '%s' is sctp", name
);
4892 /* override PROTO_UNDEF */
4893 real_proto
= IPPROTO_TCP
;
4895 if (proto
== Q_SCTP
) {
4896 if (real_proto
== IPPROTO_UDP
)
4897 bpf_error("port in range '%s' is udp", name
);
4898 else if (real_proto
== IPPROTO_TCP
)
4899 bpf_error("port in range '%s' is tcp", name
);
4901 /* override PROTO_UNDEF */
4902 real_proto
= IPPROTO_SCTP
;
4905 return gen_portrange(port1
, port2
, real_proto
, dir
);
4908 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4909 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4916 eaddr
= pcap_ether_hostton(name
);
4918 bpf_error("unknown ether host: %s", name
);
4920 alist
= pcap_nametoaddr(name
);
4921 if (alist
== NULL
|| *alist
== NULL
)
4922 bpf_error("unknown host '%s'", name
);
4923 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4927 bpf_error("'gateway' not supported in this configuration");
4931 real_proto
= lookup_proto(name
, proto
);
4932 if (real_proto
>= 0)
4933 return gen_proto(real_proto
, proto
, dir
);
4935 bpf_error("unknown protocol: %s", name
);
4938 real_proto
= lookup_proto(name
, proto
);
4939 if (real_proto
>= 0)
4940 return gen_protochain(real_proto
, proto
, dir
);
4942 bpf_error("unknown protocol: %s", name
);
4954 gen_mcode(s1
, s2
, masklen
, q
)
4955 register const char *s1
, *s2
;
4956 register int masklen
;
4959 register int nlen
, mlen
;
4962 nlen
= __pcap_atoin(s1
, &n
);
4963 /* Promote short ipaddr */
4967 mlen
= __pcap_atoin(s2
, &m
);
4968 /* Promote short ipaddr */
4971 bpf_error("non-network bits set in \"%s mask %s\"",
4974 /* Convert mask len to mask */
4976 bpf_error("mask length must be <= 32");
4977 m
= 0xffffffff << (32 - masklen
);
4979 bpf_error("non-network bits set in \"%s/%d\"",
4986 return gen_host(n
, m
, q
.proto
, q
.dir
);
4989 bpf_error("Mask syntax for networks only");
4997 register const char *s
;
5002 int proto
= q
.proto
;
5008 else if (q
.proto
== Q_DECNET
)
5009 vlen
= __pcap_atodn(s
, &v
);
5011 vlen
= __pcap_atoin(s
, &v
);
5018 if (proto
== Q_DECNET
)
5019 return gen_host(v
, 0, proto
, dir
);
5020 else if (proto
== Q_LINK
) {
5021 bpf_error("illegal link layer address");
5024 if (s
== NULL
&& q
.addr
== Q_NET
) {
5025 /* Promote short net number */
5026 while (v
&& (v
& 0xff000000) == 0) {
5031 /* Promote short ipaddr */
5035 return gen_host(v
, mask
, proto
, dir
);
5040 proto
= IPPROTO_UDP
;
5041 else if (proto
== Q_TCP
)
5042 proto
= IPPROTO_TCP
;
5043 else if (proto
== Q_SCTP
)
5044 proto
= IPPROTO_SCTP
;
5045 else if (proto
== Q_DEFAULT
)
5046 proto
= PROTO_UNDEF
;
5048 bpf_error("illegal qualifier of 'port'");
5051 return gen_port((int)v
, proto
, dir
);
5055 b
= gen_port((int)v
, proto
, dir
);
5056 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5063 proto
= IPPROTO_UDP
;
5064 else if (proto
== Q_TCP
)
5065 proto
= IPPROTO_TCP
;
5066 else if (proto
== Q_SCTP
)
5067 proto
= IPPROTO_SCTP
;
5068 else if (proto
== Q_DEFAULT
)
5069 proto
= PROTO_UNDEF
;
5071 bpf_error("illegal qualifier of 'portrange'");
5074 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5078 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5079 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5085 bpf_error("'gateway' requires a name");
5089 return gen_proto((int)v
, proto
, dir
);
5092 return gen_protochain((int)v
, proto
, dir
);
5107 gen_mcode6(s1
, s2
, masklen
, q
)
5108 register const char *s1
, *s2
;
5109 register int masklen
;
5112 struct addrinfo
*res
;
5113 struct in6_addr
*addr
;
5114 struct in6_addr mask
;
5119 bpf_error("no mask %s supported", s2
);
5121 res
= pcap_nametoaddrinfo(s1
);
5123 bpf_error("invalid ip6 address %s", s1
);
5125 bpf_error("%s resolved to multiple address", s1
);
5126 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5128 if (sizeof(mask
) * 8 < masklen
)
5129 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5130 memset(&mask
, 0, sizeof(mask
));
5131 memset(&mask
, 0xff, masklen
/ 8);
5133 mask
.s6_addr
[masklen
/ 8] =
5134 (0xff << (8 - masklen
% 8)) & 0xff;
5137 a
= (u_int32_t
*)addr
;
5138 m
= (u_int32_t
*)&mask
;
5139 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5140 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5141 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5149 bpf_error("Mask syntax for networks only");
5153 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
5158 bpf_error("invalid qualifier against IPv6 address");
5166 register const u_char
*eaddr
;
5169 struct block
*b
, *tmp
;
5171 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5172 if (linktype
== DLT_EN10MB
)
5173 return gen_ehostop(eaddr
, (int)q
.dir
);
5174 if (linktype
== DLT_FDDI
)
5175 return gen_fhostop(eaddr
, (int)q
.dir
);
5176 if (linktype
== DLT_IEEE802
)
5177 return gen_thostop(eaddr
, (int)q
.dir
);
5178 if (linktype
== DLT_IEEE802_11
||
5179 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5180 linktype
== DLT_IEEE802_11_RADIO
||
5181 linktype
== DLT_PRISM_HEADER
)
5182 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5183 if (linktype
== DLT_SUNATM
&& is_lane
) {
5185 * Check that the packet doesn't begin with an
5186 * LE Control marker. (We've already generated
5189 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5194 * Now check the MAC address.
5196 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5200 if (linktype
== DLT_IP_OVER_FC
)
5201 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5202 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5204 bpf_error("ethernet address used in non-ether expression");
5210 struct slist
*s0
, *s1
;
5213 * This is definitely not the best way to do this, but the
5214 * lists will rarely get long.
5221 static struct slist
*
5227 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5232 static struct slist
*
5238 s
= new_stmt(BPF_LD
|BPF_MEM
);
5244 * Modify "index" to use the value stored into its register as an
5245 * offset relative to the beginning of the header for the protocol
5246 * "proto", and allocate a register and put an item "size" bytes long
5247 * (1, 2, or 4) at that offset into that register, making it the register
5251 gen_load(proto
, inst
, size
)
5256 struct slist
*s
, *tmp
;
5258 int regno
= alloc_reg();
5260 free_reg(inst
->regno
);
5264 bpf_error("data size must be 1, 2, or 4");
5280 bpf_error("unsupported index operation");
5284 * The offset is relative to the beginning of the packet
5285 * data, if we have a radio header. (If we don't, this
5288 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5289 linktype
!= DLT_IEEE802_11_RADIO
&&
5290 linktype
!= DLT_PRISM_HEADER
)
5291 bpf_error("radio information not present in capture");
5294 * Load into the X register the offset computed into the
5295 * register specifed by "index".
5297 s
= xfer_to_x(inst
);
5300 * Load the item at that offset.
5302 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5304 sappend(inst
->s
, s
);
5309 * The offset is relative to the beginning of
5310 * the link-layer header.
5312 * XXX - what about ATM LANE? Should the index be
5313 * relative to the beginning of the AAL5 frame, so
5314 * that 0 refers to the beginning of the LE Control
5315 * field, or relative to the beginning of the LAN
5316 * frame, so that 0 refers, for Ethernet LANE, to
5317 * the beginning of the destination address?
5319 s
= gen_llprefixlen();
5322 * If "s" is non-null, it has code to arrange that the
5323 * X register contains the length of the prefix preceding
5324 * the link-layer header. Add to it the offset computed
5325 * into the register specified by "index", and move that
5326 * into the X register. Otherwise, just load into the X
5327 * register the offset computed into the register specifed
5331 sappend(s
, xfer_to_a(inst
));
5332 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5333 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5335 s
= xfer_to_x(inst
);
5338 * Load the item at the sum of the offset we've put in the
5339 * X register and the offset of the start of the link
5340 * layer header (which is 0 if the radio header is
5341 * variable-length; that header length is what we put
5342 * into the X register and then added to the index).
5344 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5347 sappend(inst
->s
, s
);
5363 * The offset is relative to the beginning of
5364 * the network-layer header.
5365 * XXX - are there any cases where we want
5368 s
= gen_llprefixlen();
5371 * If "s" is non-null, it has code to arrange that the
5372 * X register contains the length of the prefix preceding
5373 * the link-layer header. Add to it the offset computed
5374 * into the register specified by "index", and move that
5375 * into the X register. Otherwise, just load into the X
5376 * register the offset computed into the register specifed
5380 sappend(s
, xfer_to_a(inst
));
5381 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5382 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5384 s
= xfer_to_x(inst
);
5387 * Load the item at the sum of the offset we've put in the
5388 * X register and the offset of the start of the network
5391 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5394 sappend(inst
->s
, s
);
5397 * Do the computation only if the packet contains
5398 * the protocol in question.
5400 b
= gen_proto_abbrev(proto
);
5402 gen_and(inst
->b
, b
);
5415 * The offset is relative to the beginning of
5416 * the transport-layer header.
5417 * XXX - are there any cases where we want
5419 * XXX - we should, if we're built with
5420 * IPv6 support, generate code to load either
5421 * IPv4, IPv6, or both, as appropriate.
5423 s
= gen_loadx_iphdrlen();
5426 * The X register now contains the sum of the offset
5427 * of the beginning of the link-layer header and
5428 * the length of the network-layer header. Load
5429 * into the A register the offset relative to
5430 * the beginning of the transport layer header,
5431 * add the X register to that, move that to the
5432 * X register, and load with an offset from the
5433 * X register equal to the offset of the network
5434 * layer header relative to the beginning of
5435 * the link-layer header.
5437 sappend(s
, xfer_to_a(inst
));
5438 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5439 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5440 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5442 sappend(inst
->s
, s
);
5445 * Do the computation only if the packet contains
5446 * the protocol in question - which is true only
5447 * if this is an IP datagram and is the first or
5448 * only fragment of that datagram.
5450 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5452 gen_and(inst
->b
, b
);
5454 gen_and(gen_proto_abbrev(Q_IP
), b
);
5460 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5464 inst
->regno
= regno
;
5465 s
= new_stmt(BPF_ST
);
5467 sappend(inst
->s
, s
);
5473 gen_relation(code
, a0
, a1
, reversed
)
5475 struct arth
*a0
, *a1
;
5478 struct slist
*s0
, *s1
, *s2
;
5479 struct block
*b
, *tmp
;
5483 if (code
== BPF_JEQ
) {
5484 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5485 b
= new_block(JMP(code
));
5489 b
= new_block(BPF_JMP
|code
|BPF_X
);
5495 sappend(a0
->s
, a1
->s
);
5499 free_reg(a0
->regno
);
5500 free_reg(a1
->regno
);
5502 /* 'and' together protocol checks */
5505 gen_and(a0
->b
, tmp
= a1
->b
);
5521 int regno
= alloc_reg();
5522 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5525 s
= new_stmt(BPF_LD
|BPF_LEN
);
5526 s
->next
= new_stmt(BPF_ST
);
5527 s
->next
->s
.k
= regno
;
5542 a
= (struct arth
*)newchunk(sizeof(*a
));
5546 s
= new_stmt(BPF_LD
|BPF_IMM
);
5548 s
->next
= new_stmt(BPF_ST
);
5564 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5567 s
= new_stmt(BPF_ST
);
5575 gen_arth(code
, a0
, a1
)
5577 struct arth
*a0
, *a1
;
5579 struct slist
*s0
, *s1
, *s2
;
5583 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5588 sappend(a0
->s
, a1
->s
);
5590 free_reg(a0
->regno
);
5591 free_reg(a1
->regno
);
5593 s0
= new_stmt(BPF_ST
);
5594 a0
->regno
= s0
->s
.k
= alloc_reg();
5601 * Here we handle simple allocation of the scratch registers.
5602 * If too many registers are alloc'd, the allocator punts.
5604 static int regused
[BPF_MEMWORDS
];
5608 * Return the next free register.
5613 int n
= BPF_MEMWORDS
;
5616 if (regused
[curreg
])
5617 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5619 regused
[curreg
] = 1;
5623 bpf_error("too many registers needed to evaluate expression");
5628 * Return a register to the table so it can
5638 static struct block
*
5645 s
= new_stmt(BPF_LD
|BPF_LEN
);
5646 b
= new_block(JMP(jmp
));
5657 return gen_len(BPF_JGE
, n
);
5661 * Actually, this is less than or equal.
5669 b
= gen_len(BPF_JGT
, n
);
5676 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5677 * the beginning of the link-layer header.
5678 * XXX - that means you can't test values in the radiotap header, but
5679 * as that header is difficult if not impossible to parse generally
5680 * without a loop, that might not be a severe problem. A new keyword
5681 * "radio" could be added for that, although what you'd really want
5682 * would be a way of testing particular radio header values, which
5683 * would generate code appropriate to the radio header in question.
5686 gen_byteop(op
, idx
, val
)
5697 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5700 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5704 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5708 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5712 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5716 b
= new_block(JMP(BPF_JEQ
));
5723 static u_char abroadcast
[] = { 0x0 };
5726 gen_broadcast(proto
)
5729 bpf_u_int32 hostmask
;
5730 struct block
*b0
, *b1
, *b2
;
5731 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5737 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5738 return gen_ahostop(abroadcast
, Q_DST
);
5739 if (linktype
== DLT_EN10MB
)
5740 return gen_ehostop(ebroadcast
, Q_DST
);
5741 if (linktype
== DLT_FDDI
)
5742 return gen_fhostop(ebroadcast
, Q_DST
);
5743 if (linktype
== DLT_IEEE802
)
5744 return gen_thostop(ebroadcast
, Q_DST
);
5745 if (linktype
== DLT_IEEE802_11
||
5746 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5747 linktype
== DLT_IEEE802_11_RADIO
||
5748 linktype
== DLT_PRISM_HEADER
)
5749 return gen_wlanhostop(ebroadcast
, Q_DST
);
5750 if (linktype
== DLT_IP_OVER_FC
)
5751 return gen_ipfchostop(ebroadcast
, Q_DST
);
5752 if (linktype
== DLT_SUNATM
&& is_lane
) {
5754 * Check that the packet doesn't begin with an
5755 * LE Control marker. (We've already generated
5758 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5763 * Now check the MAC address.
5765 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5769 bpf_error("not a broadcast link");
5773 b0
= gen_linktype(ETHERTYPE_IP
);
5774 hostmask
= ~netmask
;
5775 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5776 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5777 (bpf_int32
)(~0 & hostmask
), hostmask
);
5782 bpf_error("only link-layer/IP broadcast filters supported");
5787 * Generate code to test the low-order bit of a MAC address (that's
5788 * the bottom bit of the *first* byte).
5790 static struct block
*
5791 gen_mac_multicast(offset
)
5794 register struct block
*b0
;
5795 register struct slist
*s
;
5797 /* link[offset] & 1 != 0 */
5798 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5799 b0
= new_block(JMP(BPF_JSET
));
5806 gen_multicast(proto
)
5809 register struct block
*b0
, *b1
, *b2
;
5810 register struct slist
*s
;
5816 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5817 /* all ARCnet multicasts use the same address */
5818 return gen_ahostop(abroadcast
, Q_DST
);
5820 if (linktype
== DLT_EN10MB
) {
5821 /* ether[0] & 1 != 0 */
5822 return gen_mac_multicast(0);
5825 if (linktype
== DLT_FDDI
) {
5827 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5829 * XXX - was that referring to bit-order issues?
5831 /* fddi[1] & 1 != 0 */
5832 return gen_mac_multicast(1);
5835 if (linktype
== DLT_IEEE802
) {
5836 /* tr[2] & 1 != 0 */
5837 return gen_mac_multicast(2);
5840 if (linktype
== DLT_IEEE802_11
||
5841 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5842 linktype
== DLT_IEEE802_11_RADIO
||
5843 linktype
== DLT_PRISM_HEADER
) {
5847 * For control frames, there is no DA.
5849 * For management frames, DA is at an
5850 * offset of 4 from the beginning of
5853 * For data frames, DA is at an offset
5854 * of 4 from the beginning of the packet
5855 * if To DS is clear and at an offset of
5856 * 16 from the beginning of the packet
5861 * Generate the tests to be done for data frames.
5863 * First, check for To DS set, i.e. "link[1] & 0x01".
5865 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5866 b1
= new_block(JMP(BPF_JSET
));
5867 b1
->s
.k
= 0x01; /* To DS */
5871 * If To DS is set, the DA is at 16.
5873 b0
= gen_mac_multicast(16);
5877 * Now, check for To DS not set, i.e. check
5878 * "!(link[1] & 0x01)".
5880 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5881 b2
= new_block(JMP(BPF_JSET
));
5882 b2
->s
.k
= 0x01; /* To DS */
5887 * If To DS is not set, the DA is at 4.
5889 b1
= gen_mac_multicast(4);
5893 * Now OR together the last two checks. That gives
5894 * the complete set of checks for data frames.
5899 * Now check for a data frame.
5900 * I.e, check "link[0] & 0x08".
5902 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5903 b1
= new_block(JMP(BPF_JSET
));
5908 * AND that with the checks done for data frames.
5913 * If the high-order bit of the type value is 0, this
5914 * is a management frame.
5915 * I.e, check "!(link[0] & 0x08)".
5917 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5918 b2
= new_block(JMP(BPF_JSET
));
5924 * For management frames, the DA is at 4.
5926 b1
= gen_mac_multicast(4);
5930 * OR that with the checks done for data frames.
5931 * That gives the checks done for management and
5937 * If the low-order bit of the type value is 1,
5938 * this is either a control frame or a frame
5939 * with a reserved type, and thus not a
5942 * I.e., check "!(link[0] & 0x04)".
5944 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5945 b1
= new_block(JMP(BPF_JSET
));
5951 * AND that with the checks for data and management
5958 if (linktype
== DLT_IP_OVER_FC
) {
5959 b0
= gen_mac_multicast(2);
5963 if (linktype
== DLT_SUNATM
&& is_lane
) {
5965 * Check that the packet doesn't begin with an
5966 * LE Control marker. (We've already generated
5969 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5973 /* ether[off_mac] & 1 != 0 */
5974 b0
= gen_mac_multicast(off_mac
);
5979 /* Link not known to support multicasts */
5983 b0
= gen_linktype(ETHERTYPE_IP
);
5984 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5990 b0
= gen_linktype(ETHERTYPE_IPV6
);
5991 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5996 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6001 * generate command for inbound/outbound. It's here so we can
6002 * make it link-type specific. 'dir' = 0 implies "inbound",
6003 * = 1 implies "outbound".
6009 register struct block
*b0
;
6012 * Only some data link types support inbound/outbound qualifiers.
6016 b0
= gen_relation(BPF_JEQ
,
6017 gen_load(Q_LINK
, gen_loadi(0), 1),
6025 * Match packets sent by this machine.
6027 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6030 * Match packets sent to this machine.
6031 * (No broadcast or multicast packets, or
6032 * packets sent to some other machine and
6033 * received promiscuously.)
6035 * XXX - packets sent to other machines probably
6036 * shouldn't be matched, but what about broadcast
6037 * or multicast packets we received?
6039 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6044 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6045 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6050 /* match outgoing packets */
6051 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6053 /* match incoming packets */
6054 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6058 case DLT_JUNIPER_MFR
:
6059 case DLT_JUNIPER_MLFR
:
6060 case DLT_JUNIPER_MLPPP
:
6061 case DLT_JUNIPER_ATM1
:
6062 case DLT_JUNIPER_ATM2
:
6063 case DLT_JUNIPER_PPPOE
:
6064 case DLT_JUNIPER_PPPOE_ATM
:
6065 case DLT_JUNIPER_GGSN
:
6066 case DLT_JUNIPER_ES
:
6067 case DLT_JUNIPER_MONITOR
:
6068 case DLT_JUNIPER_SERVICES
:
6069 case DLT_JUNIPER_ETHER
:
6070 case DLT_JUNIPER_PPP
:
6071 case DLT_JUNIPER_FRELAY
:
6072 case DLT_JUNIPER_CHDLC
:
6073 /* juniper flags (including direction) are stored
6074 * the byte after the 3-byte magic number */
6076 /* match outgoing packets */
6077 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6079 /* match incoming packets */
6080 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6085 bpf_error("inbound/outbound not supported on linktype %d",
6093 /* PF firewall log matched interface */
6095 gen_pf_ifname(const char *ifname
)
6100 if (linktype
== DLT_PFLOG
) {
6101 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6102 off
= offsetof(struct pfloghdr
, ifname
);
6104 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6107 if (strlen(ifname
) >= len
) {
6108 bpf_error("ifname interface names can only be %d characters",
6112 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6116 /* PF firewall log ruleset name */
6118 gen_pf_ruleset(char *ruleset
)
6122 if (linktype
!= DLT_PFLOG
) {
6123 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6126 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6127 bpf_error("ruleset names can only be %ld characters",
6128 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6131 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6132 strlen(ruleset
), (const u_char
*)ruleset
);
6136 /* PF firewall log rule number */
6142 if (linktype
== DLT_PFLOG
) {
6143 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6146 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6153 /* PF firewall log sub-rule number */
6155 gen_pf_srnr(int srnr
)
6159 if (linktype
!= DLT_PFLOG
) {
6160 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6164 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6169 /* PF firewall log reason code */
6171 gen_pf_reason(int reason
)
6175 if (linktype
== DLT_PFLOG
) {
6176 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6179 bpf_error("reason not supported on linktype 0x%x", linktype
);
6186 /* PF firewall log action */
6188 gen_pf_action(int action
)
6192 if (linktype
== DLT_PFLOG
) {
6193 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6196 bpf_error("action not supported on linktype 0x%x", linktype
);
6205 register const u_char
*eaddr
;
6208 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6209 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6210 return gen_ahostop(eaddr
, (int)q
.dir
);
6212 bpf_error("ARCnet address used in non-arc expression");
6216 static struct block
*
6217 gen_ahostop(eaddr
, dir
)
6218 register const u_char
*eaddr
;
6221 register struct block
*b0
, *b1
;
6224 /* src comes first, different from Ethernet */
6226 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6229 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6232 b0
= gen_ahostop(eaddr
, Q_SRC
);
6233 b1
= gen_ahostop(eaddr
, Q_DST
);
6239 b0
= gen_ahostop(eaddr
, Q_SRC
);
6240 b1
= gen_ahostop(eaddr
, Q_DST
);
6249 * support IEEE 802.1Q VLAN trunk over ethernet
6255 struct block
*b0
, *b1
;
6257 /* can't check for VLAN-encapsulated packets inside MPLS */
6258 if (label_stack_depth
> 0)
6259 bpf_error("no VLAN match after MPLS");
6262 * Change the offsets to point to the type and data fields within
6263 * the VLAN packet. Just increment the offsets, so that we
6264 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6265 * capture VLAN 200 encapsulated within VLAN 100.
6267 * XXX - this is a bit of a kludge. If we were to split the
6268 * compiler into a parser that parses an expression and
6269 * generates an expression tree, and a code generator that
6270 * takes an expression tree (which could come from our
6271 * parser or from some other parser) and generates BPF code,
6272 * we could perhaps make the offsets parameters of routines
6273 * and, in the handler for an "AND" node, pass to subnodes
6274 * other than the VLAN node the adjusted offsets.
6276 * This would mean that "vlan" would, instead of changing the
6277 * behavior of *all* tests after it, change only the behavior
6278 * of tests ANDed with it. That would change the documented
6279 * semantics of "vlan", which might break some expressions.
6280 * However, it would mean that "(vlan and ip) or ip" would check
6281 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6282 * checking only for VLAN-encapsulated IP, so that could still
6283 * be considered worth doing; it wouldn't break expressions
6284 * that are of the form "vlan and ..." or "vlan N and ...",
6285 * which I suspect are the most common expressions involving
6286 * "vlan". "vlan or ..." doesn't necessarily do what the user
6287 * would really want, now, as all the "or ..." tests would
6288 * be done assuming a VLAN, even though the "or" could be viewed
6289 * as meaning "or, if this isn't a VLAN packet...".
6291 orig_linktype
= off_linktype
; /* save original values */
6303 bpf_error("no VLAN support for data link type %d",
6308 /* check for VLAN */
6309 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6311 /* If a specific VLAN is requested, check VLAN id */
6312 if (vlan_num
>= 0) {
6313 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6329 struct block
*b0
,*b1
;
6332 * Change the offsets to point to the type and data fields within
6333 * the MPLS packet. Just increment the offsets, so that we
6334 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6335 * capture packets with an outer label of 100000 and an inner
6338 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6342 if (label_stack_depth
> 0) {
6343 /* just match the bottom-of-stack bit clear */
6344 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6347 * Indicate that we're checking MPLS-encapsulated headers,
6348 * to make sure higher level code generators don't try to
6349 * match against IP-related protocols such as Q_ARP, Q_RARP
6354 case DLT_C_HDLC
: /* fall through */
6356 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
6357 (bpf_int32
)ETHERTYPE_MPLS
);
6361 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
6362 (bpf_int32
)PPP_MPLS_UCAST
);
6365 /* FIXME add other DLT_s ...
6366 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6367 * leave it for now */
6370 bpf_error("no MPLS support for data link type %d",
6378 /* If a specific MPLS label is requested, check it */
6379 if (label_num
>= 0) {
6380 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6381 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6382 0xfffff000); /* only compare the first 20 bits */
6389 label_stack_depth
++;
6394 * Support PPPOE discovery and session.
6399 /* check for PPPoE discovery */
6400 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6409 * Test against the PPPoE session link-layer type.
6411 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6414 * Change the offsets to point to the type and data fields within
6417 * XXX - this is a bit of a kludge. If we were to split the
6418 * compiler into a parser that parses an expression and
6419 * generates an expression tree, and a code generator that
6420 * takes an expression tree (which could come from our
6421 * parser or from some other parser) and generates BPF code,
6422 * we could perhaps make the offsets parameters of routines
6423 * and, in the handler for an "AND" node, pass to subnodes
6424 * other than the PPPoE node the adjusted offsets.
6426 * This would mean that "pppoes" would, instead of changing the
6427 * behavior of *all* tests after it, change only the behavior
6428 * of tests ANDed with it. That would change the documented
6429 * semantics of "pppoes", which might break some expressions.
6430 * However, it would mean that "(pppoes and ip) or ip" would check
6431 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6432 * checking only for VLAN-encapsulated IP, so that could still
6433 * be considered worth doing; it wouldn't break expressions
6434 * that are of the form "pppoes and ..." which I suspect are the
6435 * most common expressions involving "pppoes". "pppoes or ..."
6436 * doesn't necessarily do what the user would really want, now,
6437 * as all the "or ..." tests would be done assuming PPPoE, even
6438 * though the "or" could be viewed as meaning "or, if this isn't
6439 * a PPPoE packet...".
6441 orig_linktype
= off_linktype
; /* save original values */
6445 * The "network-layer" protocol is PPPoE, which has a 6-byte
6446 * PPPoE header, followed by PPP payload, so we set the
6447 * offsets to the network layer offset plus 6 bytes for
6448 * the PPPoE header plus the values appropriate for PPP when
6449 * encapsulated in Ethernet (which means there's no HDLC
6452 off_linktype
= orig_nl
+ 6;
6453 off_nl
= orig_nl
+ 6 + 2;
6454 off_nl_nosnap
= orig_nl
+ 6 + 2;
6457 * Set the link-layer type to PPP, as all subsequent tests will
6458 * be on the encapsulated PPP header.
6466 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6478 bpf_error("'vpi' supported only on raw ATM");
6479 if (off_vpi
== (u_int
)-1)
6481 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6487 bpf_error("'vci' supported only on raw ATM");
6488 if (off_vci
== (u_int
)-1)
6490 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6495 if (off_proto
== (u_int
)-1)
6496 abort(); /* XXX - this isn't on FreeBSD */
6497 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6502 if (off_payload
== (u_int
)-1)
6504 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6505 0xffffffff, jtype
, reverse
, jvalue
);
6510 bpf_error("'callref' supported only on raw ATM");
6511 if (off_proto
== (u_int
)-1)
6513 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6514 jtype
, reverse
, jvalue
);
6524 gen_atmtype_abbrev(type
)
6527 struct block
*b0
, *b1
;
6532 /* Get all packets in Meta signalling Circuit */
6534 bpf_error("'metac' supported only on raw ATM");
6535 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6536 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6541 /* Get all packets in Broadcast Circuit*/
6543 bpf_error("'bcc' supported only on raw ATM");
6544 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6545 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6550 /* Get all cells in Segment OAM F4 circuit*/
6552 bpf_error("'oam4sc' supported only on raw ATM");
6553 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6554 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6559 /* Get all cells in End-to-End OAM F4 Circuit*/
6561 bpf_error("'oam4ec' supported only on raw ATM");
6562 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6563 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6568 /* Get all packets in connection Signalling Circuit */
6570 bpf_error("'sc' supported only on raw ATM");
6571 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6572 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6577 /* Get all packets in ILMI Circuit */
6579 bpf_error("'ilmic' supported only on raw ATM");
6580 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6581 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6586 /* Get all LANE packets */
6588 bpf_error("'lane' supported only on raw ATM");
6589 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6592 * Arrange that all subsequent tests assume LANE
6593 * rather than LLC-encapsulated packets, and set
6594 * the offsets appropriately for LANE-encapsulated
6597 * "off_mac" is the offset of the Ethernet header,
6598 * which is 2 bytes past the ATM pseudo-header
6599 * (skipping the pseudo-header and 2-byte LE Client
6600 * field). The other offsets are Ethernet offsets
6601 * relative to "off_mac".
6604 off_mac
= off_payload
+ 2; /* MAC header */
6605 off_linktype
= off_mac
+ 12;
6606 off_nl
= off_mac
+ 14; /* Ethernet II */
6607 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6611 /* Get all LLC-encapsulated packets */
6613 bpf_error("'llc' supported only on raw ATM");
6614 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6625 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6632 bpf_u_int32 val1
, val2
, val3
;
6634 switch (mtp3field
) {
6637 if (off_sio
== (u_int
)-1)
6638 bpf_error("'sio' supported only on SS7");
6639 /* sio coded on 1 byte so max value 255 */
6641 bpf_error("sio value %u too big; max value = 255",
6643 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6644 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6648 if (off_opc
== (u_int
)-1)
6649 bpf_error("'opc' supported only on SS7");
6650 /* opc coded on 14 bits so max value 16383 */
6652 bpf_error("opc value %u too big; max value = 16383",
6654 /* the following instructions are made to convert jvalue
6655 * to the form used to write opc in an ss7 message*/
6656 val1
= jvalue
& 0x00003c00;
6658 val2
= jvalue
& 0x000003fc;
6660 val3
= jvalue
& 0x00000003;
6662 jvalue
= val1
+ val2
+ val3
;
6663 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6664 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6668 if (off_dpc
== (u_int
)-1)
6669 bpf_error("'dpc' supported only on SS7");
6670 /* dpc coded on 14 bits so max value 16383 */
6672 bpf_error("dpc value %u too big; max value = 16383",
6674 /* the following instructions are made to convert jvalue
6675 * to the forme used to write dpc in an ss7 message*/
6676 val1
= jvalue
& 0x000000ff;
6678 val2
= jvalue
& 0x00003f00;
6680 jvalue
= val1
+ val2
;
6681 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6682 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6686 if (off_sls
== (u_int
)-1)
6687 bpf_error("'sls' supported only on SS7");
6688 /* sls coded on 4 bits so max value 15 */
6690 bpf_error("sls value %u too big; max value = 15",
6692 /* the following instruction is made to convert jvalue
6693 * to the forme used to write sls in an ss7 message*/
6694 jvalue
= jvalue
<< 4;
6695 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6696 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6705 static struct block
*
6706 gen_msg_abbrev(type
)
6712 * Q.2931 signalling protocol messages for handling virtual circuits
6713 * establishment and teardown
6718 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6722 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6726 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6730 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6734 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6737 case A_RELEASE_DONE
:
6738 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6748 gen_atmmulti_abbrev(type
)
6751 struct block
*b0
, *b1
;
6757 bpf_error("'oam' supported only on raw ATM");
6758 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6763 bpf_error("'oamf4' supported only on raw ATM");
6765 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6766 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6768 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6774 * Get Q.2931 signalling messages for switched
6775 * virtual connection
6778 bpf_error("'connectmsg' supported only on raw ATM");
6779 b0
= gen_msg_abbrev(A_SETUP
);
6780 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6782 b0
= gen_msg_abbrev(A_CONNECT
);
6784 b0
= gen_msg_abbrev(A_CONNECTACK
);
6786 b0
= gen_msg_abbrev(A_RELEASE
);
6788 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6790 b0
= gen_atmtype_abbrev(A_SC
);
6796 bpf_error("'metaconnect' supported only on raw ATM");
6797 b0
= gen_msg_abbrev(A_SETUP
);
6798 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6800 b0
= gen_msg_abbrev(A_CONNECT
);
6802 b0
= gen_msg_abbrev(A_RELEASE
);
6804 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6806 b0
= gen_atmtype_abbrev(A_METAC
);