1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.221.2.24 2005-06-20 21:52:53 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_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_host(bpf_u_int32
, bpf_u_int32
, int, int);
210 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
213 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
215 static struct block
*gen_ipfrag(void);
216 static struct block
*gen_portatom(int, bpf_int32
);
217 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
219 static struct block
*gen_portatom6(int, bpf_int32
);
220 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
222 struct block
*gen_portop(int, int, int);
223 static struct block
*gen_port(int, int, int);
224 struct block
*gen_portrangeop(int, int, int, int);
225 static struct block
*gen_portrange(int, int, int, int);
227 struct block
*gen_portop6(int, int, int);
228 static struct block
*gen_port6(int, int, int);
229 struct block
*gen_portrangeop6(int, int, int, int);
230 static struct block
*gen_portrange6(int, int, int, int);
232 static int lookup_proto(const char *, int);
233 static struct block
*gen_protochain(int, int, int);
234 static struct block
*gen_proto(int, int, int);
235 static struct slist
*xfer_to_x(struct arth
*);
236 static struct slist
*xfer_to_a(struct arth
*);
237 static struct block
*gen_mac_multicast(int);
238 static struct block
*gen_len(int, int);
240 static struct block
*gen_msg_abbrev(int type
);
251 /* XXX Round up to nearest long. */
252 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
254 /* XXX Round up to structure boundary. */
258 cp
= &chunks
[cur_chunk
];
259 if (n
> cp
->n_left
) {
260 ++cp
, k
= ++cur_chunk
;
262 bpf_error("out of memory");
263 size
= CHUNK0SIZE
<< k
;
264 cp
->m
= (void *)malloc(size
);
266 bpf_error("out of memory");
267 memset((char *)cp
->m
, 0, size
);
270 bpf_error("out of memory");
273 return (void *)((char *)cp
->m
+ cp
->n_left
);
282 for (i
= 0; i
< NCHUNKS
; ++i
)
283 if (chunks
[i
].m
!= NULL
) {
290 * A strdup whose allocations are freed after code generation is over.
294 register const char *s
;
296 int n
= strlen(s
) + 1;
297 char *cp
= newchunk(n
);
303 static inline struct block
*
309 p
= (struct block
*)newchunk(sizeof(*p
));
316 static inline struct slist
*
322 p
= (struct slist
*)newchunk(sizeof(*p
));
328 static struct block
*
332 struct block
*b
= new_block(BPF_RET
|BPF_K
);
341 bpf_error("syntax error in filter expression");
344 static bpf_u_int32 netmask
;
349 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
350 char *buf
, int optimize
, bpf_u_int32 mask
)
359 if (setjmp(top_ctx
)) {
367 snaplen
= pcap_snapshot(p
);
369 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
370 "snaplen of 0 rejects all packets");
374 lex_init(buf
? buf
: "");
382 root
= gen_retblk(snaplen
);
384 if (optimize
&& !no_optimize
) {
387 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
388 bpf_error("expression rejects all packets");
390 program
->bf_insns
= icode_to_fcode(root
, &len
);
391 program
->bf_len
= len
;
399 * entry point for using the compiler with no pcap open
400 * pass in all the stuff that is needed explicitly instead.
403 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
404 struct bpf_program
*program
,
405 char *buf
, int optimize
, bpf_u_int32 mask
)
410 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
413 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
419 * Clean up a "struct bpf_program" by freeing all the memory allocated
423 pcap_freecode(struct bpf_program
*program
)
426 if (program
->bf_insns
!= NULL
) {
427 free((char *)program
->bf_insns
);
428 program
->bf_insns
= NULL
;
433 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
434 * which of the jt and jf fields has been resolved and which is a pointer
435 * back to another unresolved block (or nil). At least one of the fields
436 * in each block is already resolved.
439 backpatch(list
, target
)
440 struct block
*list
, *target
;
457 * Merge the lists in b0 and b1, using the 'sense' field to indicate
458 * which of jt and jf is the link.
462 struct block
*b0
, *b1
;
464 register struct block
**p
= &b0
;
466 /* Find end of list. */
468 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
470 /* Concatenate the lists. */
478 backpatch(p
, gen_retblk(snaplen
));
479 p
->sense
= !p
->sense
;
480 backpatch(p
, gen_retblk(0));
484 * Insert before the statements of the first (root) block any
485 * statements needed to load the lengths of any variable-length
486 * headers into registers.
488 * XXX - a fancier strategy would be to insert those before the
489 * statements of all blocks that use those lengths and that
490 * have no predecessors that use them, so that we only compute
491 * the lengths if we need them. There might be even better
492 * approaches than that. However, as we're currently only
493 * handling variable-length radiotap headers, and as all
494 * filtering expressions other than raw link[M:N] tests
495 * require the length of that header, doing more for that
496 * header length isn't really worth the effort.
498 insert_load_llprefixlen(root
);
503 struct block
*b0
, *b1
;
505 backpatch(b0
, b1
->head
);
506 b0
->sense
= !b0
->sense
;
507 b1
->sense
= !b1
->sense
;
509 b1
->sense
= !b1
->sense
;
515 struct block
*b0
, *b1
;
517 b0
->sense
= !b0
->sense
;
518 backpatch(b0
, b1
->head
);
519 b0
->sense
= !b0
->sense
;
528 b
->sense
= !b
->sense
;
531 static struct block
*
532 gen_cmp(offrel
, offset
, size
, v
)
533 enum e_offrel offrel
;
537 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
540 static struct block
*
541 gen_cmp_gt(offrel
, offset
, size
, v
)
542 enum e_offrel offrel
;
546 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
549 static struct block
*
550 gen_cmp_ge(offrel
, offset
, size
, v
)
551 enum e_offrel offrel
;
555 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
558 static struct block
*
559 gen_cmp_lt(offrel
, offset
, size
, v
)
560 enum e_offrel offrel
;
564 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
567 static struct block
*
568 gen_cmp_le(offrel
, offset
, size
, v
)
569 enum e_offrel offrel
;
573 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
576 static struct block
*
577 gen_mcmp(offrel
, offset
, size
, v
, mask
)
578 enum e_offrel offrel
;
583 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
586 static struct block
*
587 gen_bcmp(offrel
, offset
, size
, v
)
588 enum e_offrel offrel
;
589 register u_int offset
, size
;
590 register const u_char
*v
;
592 register struct block
*b
, *tmp
;
596 register const u_char
*p
= &v
[size
- 4];
597 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
598 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
600 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
607 register const u_char
*p
= &v
[size
- 2];
608 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
610 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
617 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
626 * AND the field of size "size" at offset "offset" relative to the header
627 * specified by "offrel" with "mask", and compare it with the value "v"
628 * with the test specified by "jtype"; if "reverse" is true, the test
629 * should test the opposite of "jtype".
631 static struct block
*
632 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
633 enum e_offrel offrel
;
635 bpf_u_int32 offset
, size
, mask
, jtype
;
638 struct slist
*s
, *s2
;
641 s
= gen_load_a(offrel
, offset
, size
);
643 if (mask
!= 0xffffffff) {
644 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
649 b
= new_block(JMP(jtype
));
652 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
658 * Various code constructs need to know the layout of the data link
659 * layer. These variables give the necessary offsets from the beginning
660 * of the packet data.
662 * If the link layer has variable_length headers, the offsets are offsets
663 * from the end of the link-link-layer header, and "reg_ll_size" is
664 * the register number for a register containing the length of the
665 * link-layer header. Otherwise, "reg_ll_size" is -1.
667 static int reg_ll_size
;
670 * This is the offset of the beginning of the link-layer header.
671 * It's usually 0, except for 802.11 with a fixed-length radio header.
676 * This is the offset of the beginning of the MAC-layer header.
677 * It's usually 0, except for ATM LANE.
679 static u_int off_mac
;
682 * "off_linktype" is the offset to information in the link-layer header
683 * giving the packet type.
685 * For Ethernet, it's the offset of the Ethernet type field.
687 * For link-layer types that always use 802.2 headers, it's the
688 * offset of the LLC header.
690 * For PPP, it's the offset of the PPP type field.
692 * For Cisco HDLC, it's the offset of the CHDLC type field.
694 * For BSD loopback, it's the offset of the AF_ value.
696 * For Linux cooked sockets, it's the offset of the type field.
698 * It's set to -1 for no encapsulation, in which case, IP is assumed.
700 static u_int off_linktype
;
703 * TRUE if the link layer includes an ATM pseudo-header.
705 static int is_atm
= 0;
708 * TRUE if "lane" appeared in the filter; it causes us to generate
709 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
711 static int is_lane
= 0;
714 * These are offsets for the ATM pseudo-header.
716 static u_int off_vpi
;
717 static u_int off_vci
;
718 static u_int off_proto
;
721 * These are offsets for the MTP3 fields.
723 static u_int off_sio
;
724 static u_int off_opc
;
725 static u_int off_dpc
;
726 static u_int off_sls
;
729 * This is the offset of the first byte after the ATM pseudo_header,
730 * or -1 if there is no ATM pseudo-header.
732 static u_int off_payload
;
735 * These are offsets to the beginning of the network-layer header.
737 * If the link layer never uses 802.2 LLC:
739 * "off_nl" and "off_nl_nosnap" are the same.
741 * If the link layer always uses 802.2 LLC:
743 * "off_nl" is the offset if there's a SNAP header following
746 * "off_nl_nosnap" is the offset if there's no SNAP header.
748 * If the link layer is Ethernet:
750 * "off_nl" is the offset if the packet is an Ethernet II packet
751 * (we assume no 802.3+802.2+SNAP);
753 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
754 * with an 802.2 header following it.
757 static u_int off_nl_nosnap
;
765 linktype
= pcap_datalink(p
);
767 pcap_fddipad
= p
->fddipad
;
771 * Assume it's not raw ATM with a pseudo-header, for now.
787 * Also assume it's not 802.11 with a fixed-length radio header.
800 off_nl
= 6; /* XXX in reality, variable! */
801 off_nl_nosnap
= 6; /* no 802.2 LLC */
804 case DLT_ARCNET_LINUX
:
806 off_nl
= 8; /* XXX in reality, variable! */
807 off_nl_nosnap
= 8; /* no 802.2 LLC */
812 off_nl
= 14; /* Ethernet II */
813 off_nl_nosnap
= 17; /* 802.3+802.2 */
818 * SLIP doesn't have a link level type. The 16 byte
819 * header is hacked into our SLIP driver.
823 off_nl_nosnap
= 16; /* no 802.2 LLC */
827 /* XXX this may be the same as the DLT_PPP_BSDOS case */
831 off_nl_nosnap
= 24; /* no 802.2 LLC */
838 off_nl_nosnap
= 4; /* no 802.2 LLC */
844 off_nl_nosnap
= 12; /* no 802.2 LLC */
849 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
850 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
853 off_nl_nosnap
= 4; /* no 802.2 LLC */
858 * This does no include the Ethernet header, and
859 * only covers session state.
863 off_nl_nosnap
= 8; /* no 802.2 LLC */
869 off_nl_nosnap
= 24; /* no 802.2 LLC */
874 * FDDI doesn't really have a link-level type field.
875 * We set "off_linktype" to the offset of the LLC header.
877 * To check for Ethernet types, we assume that SSAP = SNAP
878 * is being used and pick out the encapsulated Ethernet type.
879 * XXX - should we generate code to check for SNAP?
883 off_linktype
+= pcap_fddipad
;
885 off_nl
= 21; /* FDDI+802.2+SNAP */
886 off_nl_nosnap
= 16; /* FDDI+802.2 */
888 off_nl
+= pcap_fddipad
;
889 off_nl_nosnap
+= pcap_fddipad
;
895 * Token Ring doesn't really have a link-level type field.
896 * We set "off_linktype" to the offset of the LLC header.
898 * To check for Ethernet types, we assume that SSAP = SNAP
899 * is being used and pick out the encapsulated Ethernet type.
900 * XXX - should we generate code to check for SNAP?
902 * XXX - the header is actually variable-length.
903 * Some various Linux patched versions gave 38
904 * as "off_linktype" and 40 as "off_nl"; however,
905 * if a token ring packet has *no* routing
906 * information, i.e. is not source-routed, the correct
907 * values are 20 and 22, as they are in the vanilla code.
909 * A packet is source-routed iff the uppermost bit
910 * of the first byte of the source address, at an
911 * offset of 8, has the uppermost bit set. If the
912 * packet is source-routed, the total number of bytes
913 * of routing information is 2 plus bits 0x1F00 of
914 * the 16-bit value at an offset of 14 (shifted right
915 * 8 - figure out which byte that is).
918 off_nl
= 22; /* Token Ring+802.2+SNAP */
919 off_nl_nosnap
= 17; /* Token Ring+802.2 */
924 * 802.11 doesn't really have a link-level type field.
925 * We set "off_linktype" to the offset of the LLC header.
927 * To check for Ethernet types, we assume that SSAP = SNAP
928 * is being used and pick out the encapsulated Ethernet type.
929 * XXX - should we generate code to check for SNAP?
931 * XXX - the header is actually variable-length. We
932 * assume a 24-byte link-layer header, as appears in
933 * data frames in networks with no bridges. If the
934 * fromds and tods 802.11 header bits are both set,
935 * it's actually supposed to be 30 bytes.
938 off_nl
= 32; /* 802.11+802.2+SNAP */
939 off_nl_nosnap
= 27; /* 802.11+802.2 */
942 case DLT_PRISM_HEADER
:
944 * Same as 802.11, but with an additional header before
945 * the 802.11 header, containing a bunch of additional
946 * information including radio-level information.
948 * The header is 144 bytes long.
950 * XXX - same variable-length header problem; at least
951 * the Prism header is fixed-length.
954 off_linktype
= 144+24;
955 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
956 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
959 case DLT_IEEE802_11_RADIO_AVS
:
961 * Same as 802.11, but with an additional header before
962 * the 802.11 header, containing a bunch of additional
963 * information including radio-level information.
965 * The header is 64 bytes long, at least in its
966 * current incarnation.
968 * XXX - same variable-length header problem, only
969 * more so; this header is also variable-length,
970 * with the length being the 32-bit big-endian
971 * number at an offset of 4 from the beginning
972 * of the radio header.
975 off_linktype
= 64+24;
976 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
977 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
980 case DLT_IEEE802_11_RADIO
:
982 * Same as 802.11, but with an additional header before
983 * the 802.11 header, containing a bunch of additional
984 * information including radio-level information.
986 * The radiotap header is variable length, and we
987 * generate code to compute its length and store it
988 * in a register. These offsets are relative to the
989 * beginning of the 802.11 header.
992 off_nl
= 32; /* 802.11+802.2+SNAP */
993 off_nl_nosnap
= 27; /* 802.11+802.2 */
996 case DLT_ATM_RFC1483
:
997 case DLT_ATM_CLIP
: /* Linux ATM defines this */
999 * assume routed, non-ISO PDUs
1000 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1002 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1003 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1004 * latter would presumably be treated the way PPPoE
1005 * should be, so you can do "pppoe and udp port 2049"
1006 * or "pppoa and tcp port 80" and have it check for
1007 * PPPo{A,E} and a PPP protocol of IP and....
1010 off_nl
= 8; /* 802.2+SNAP */
1011 off_nl_nosnap
= 3; /* 802.2 */
1016 * Full Frontal ATM; you get AALn PDUs with an ATM
1020 off_vpi
= SUNATM_VPI_POS
;
1021 off_vci
= SUNATM_VCI_POS
;
1022 off_proto
= PROTO_POS
;
1023 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1024 off_payload
= SUNATM_PKT_BEGIN_POS
;
1025 off_linktype
= off_payload
;
1026 off_nl
= off_payload
+8; /* 802.2+SNAP */
1027 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1033 off_nl_nosnap
= 0; /* no 802.2 LLC */
1036 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1039 off_nl_nosnap
= 16; /* no 802.2 LLC */
1044 * LocalTalk does have a 1-byte type field in the LLAP header,
1045 * but really it just indicates whether there is a "short" or
1046 * "long" DDP packet following.
1050 off_nl_nosnap
= 0; /* no 802.2 LLC */
1053 case DLT_IP_OVER_FC
:
1055 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1056 * link-level type field. We set "off_linktype" to the
1057 * offset of the LLC header.
1059 * To check for Ethernet types, we assume that SSAP = SNAP
1060 * is being used and pick out the encapsulated Ethernet type.
1061 * XXX - should we generate code to check for SNAP? RFC
1062 * 2625 says SNAP should be used.
1065 off_nl
= 24; /* IPFC+802.2+SNAP */
1066 off_nl_nosnap
= 19; /* IPFC+802.2 */
1071 * XXX - we should set this to handle SNAP-encapsulated
1072 * frames (NLPID of 0x80).
1076 off_nl_nosnap
= 0; /* no 802.2 LLC */
1079 case DLT_APPLE_IP_OVER_IEEE1394
:
1082 off_nl_nosnap
= 18; /* no 802.2 LLC */
1085 case DLT_LINUX_IRDA
:
1087 * Currently, only raw "link[N:M]" filtering is supported.
1096 * Currently, only raw "link[N:M]" filtering is supported.
1103 case DLT_SYMANTEC_FIREWALL
:
1105 off_nl
= 44; /* Ethernet II */
1106 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1111 /* XXX read this from pf.h? */
1112 off_nl
= PFLOG_HDRLEN
;
1113 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1116 case DLT_JUNIPER_MLFR
:
1117 case DLT_JUNIPER_MLPPP
:
1120 off_nl_nosnap
= -1; /* no 802.2 LLC */
1123 case DLT_JUNIPER_ATM1
:
1124 off_linktype
= 4; /* in reality variable between 4-8 */
1129 case DLT_JUNIPER_ATM2
:
1130 off_linktype
= 8; /* in reality variable between 8-12 */
1135 /* frames captured on a Juniper PPPoE service PIC
1136 * contain raw ethernet frames */
1137 case DLT_JUNIPER_PPPOE
:
1139 off_nl
= 18; /* Ethernet II */
1140 off_nl_nosnap
= 21; /* 802.3+802.2 */
1143 case DLT_JUNIPER_PPPOE_ATM
:
1146 off_nl_nosnap
= -1; /* no 802.2 LLC */
1149 case DLT_JUNIPER_GGSN
:
1152 off_nl_nosnap
= -1; /* no 802.2 LLC */
1155 case DLT_JUNIPER_ES
:
1157 off_nl
= -1; /* not really a network layer but raw IP adresses */
1158 off_nl_nosnap
= -1; /* no 802.2 LLC */
1161 case DLT_JUNIPER_MONITOR
:
1163 off_nl
= 12; /* raw IP/IP6 header */
1164 off_nl_nosnap
= -1; /* no 802.2 LLC */
1167 case DLT_JUNIPER_SERVICES
:
1169 off_nl
= -1; /* L3 proto location dep. on cookie type */
1170 off_nl_nosnap
= -1; /* no 802.2 LLC */
1191 case DLT_LINUX_LAPD
:
1193 * Currently, only raw "link[N:M]" filtering is supported.
1200 bpf_error("unknown data link type %d", linktype
);
1205 * Load a value relative to the beginning of the link-layer header.
1206 * The link-layer header doesn't necessarily begin at the beginning
1207 * of the packet data; there might be a variable-length prefix containing
1208 * radio information.
1210 static struct slist
*
1211 gen_load_llrel(offset
, size
)
1214 struct slist
*s
, *s2
;
1216 s
= gen_llprefixlen();
1219 * If "s" is non-null, it has code to arrange that the X register
1220 * contains the length of the prefix preceding the link-layer
1224 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1228 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1235 * Load a value relative to the beginning of the specified header.
1237 static struct slist
*
1238 gen_load_a(offrel
, offset
, size
)
1239 enum e_offrel offrel
;
1242 struct slist
*s
, *s2
;
1247 s
= gen_load_llrel(offset
, size
);
1251 s
= gen_load_llrel(off_ll
+ offset
, size
);
1255 s
= gen_load_llrel(off_nl
+ offset
, size
);
1259 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1264 * Load the X register with the length of the IPv4 header,
1267 s
= gen_loadx_iphdrlen();
1270 * Load the item at {length of the link-layer header} +
1271 * {length of the IPv4 header} + {specified offset}.
1273 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1274 s2
->s
.k
= off_nl
+ offset
;
1279 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1290 * Generate code to load into the X register the sum of the length of
1291 * the IPv4 header and any variable-length header preceding the link-layer
1294 static struct slist
*
1295 gen_loadx_iphdrlen()
1297 struct slist
*s
, *s2
;
1299 s
= gen_llprefixlen();
1302 * There's a variable-length prefix preceding the
1303 * link-layer header. "s" points to a list of statements
1304 * that put the length of that prefix into the X register.
1305 * The 4*([k]&0xf) addressing mode can't be used, as we
1306 * don't have a constant offset, so we have to load the
1307 * value in question into the A register and add to it
1308 * the value from the X register.
1310 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1313 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1316 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1321 * The A register now contains the length of the
1322 * IP header. We need to add to it the length
1323 * of the prefix preceding the link-layer
1324 * header, which is still in the X register, and
1325 * move the result into the X register.
1327 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1328 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1331 * There is no variable-length header preceding the
1332 * link-layer header; if there's a fixed-length
1333 * header preceding it, its length is included in
1334 * the off_ variables, so it doesn't need to be added.
1336 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1342 static struct block
*
1349 s
= new_stmt(BPF_LD
|BPF_IMM
);
1351 b
= new_block(JMP(BPF_JEQ
));
1357 static inline struct block
*
1360 return gen_uncond(1);
1363 static inline struct block
*
1366 return gen_uncond(0);
1370 * Byte-swap a 32-bit number.
1371 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1372 * big-endian platforms.)
1374 #define SWAPLONG(y) \
1375 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1378 * Generate code to match a particular packet type.
1380 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1381 * value, if <= ETHERMTU. We use that to determine whether to
1382 * match the type/length field or to check the type/length field for
1383 * a value <= ETHERMTU to see whether it's a type field and then do
1384 * the appropriate test.
1386 static struct block
*
1387 gen_ether_linktype(proto
)
1390 struct block
*b0
, *b1
;
1396 case LLCSAP_NETBEUI
:
1398 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1399 * so we check the DSAP and SSAP.
1401 * LLCSAP_IP checks for IP-over-802.2, rather
1402 * than IP-over-Ethernet or IP-over-SNAP.
1404 * XXX - should we check both the DSAP and the
1405 * SSAP, like this, or should we check just the
1406 * DSAP, as we do for other types <= ETHERMTU
1407 * (i.e., other SAP values)?
1409 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1411 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1412 ((proto
<< 8) | proto
));
1420 * Ethernet_II frames, which are Ethernet
1421 * frames with a frame type of ETHERTYPE_IPX;
1423 * Ethernet_802.3 frames, which are 802.3
1424 * frames (i.e., the type/length field is
1425 * a length field, <= ETHERMTU, rather than
1426 * a type field) with the first two bytes
1427 * after the Ethernet/802.3 header being
1430 * Ethernet_802.2 frames, which are 802.3
1431 * frames with an 802.2 LLC header and
1432 * with the IPX LSAP as the DSAP in the LLC
1435 * Ethernet_SNAP frames, which are 802.3
1436 * frames with an LLC header and a SNAP
1437 * header and with an OUI of 0x000000
1438 * (encapsulated Ethernet) and a protocol
1439 * ID of ETHERTYPE_IPX in the SNAP header.
1441 * XXX - should we generate the same code both
1442 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1446 * This generates code to check both for the
1447 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1449 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1450 (bpf_int32
)LLCSAP_IPX
);
1451 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1456 * Now we add code to check for SNAP frames with
1457 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1459 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1463 * Now we generate code to check for 802.3
1464 * frames in general.
1466 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1470 * Now add the check for 802.3 frames before the
1471 * check for Ethernet_802.2 and Ethernet_802.3,
1472 * as those checks should only be done on 802.3
1473 * frames, not on Ethernet frames.
1478 * Now add the check for Ethernet_II frames, and
1479 * do that before checking for the other frame
1482 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1483 (bpf_int32
)ETHERTYPE_IPX
);
1487 case ETHERTYPE_ATALK
:
1488 case ETHERTYPE_AARP
:
1490 * EtherTalk (AppleTalk protocols on Ethernet link
1491 * layer) may use 802.2 encapsulation.
1495 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1496 * we check for an Ethernet type field less than
1497 * 1500, which means it's an 802.3 length field.
1499 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1503 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1504 * SNAP packets with an organization code of
1505 * 0x080007 (Apple, for Appletalk) and a protocol
1506 * type of ETHERTYPE_ATALK (Appletalk).
1508 * 802.2-encapsulated ETHERTYPE_AARP packets are
1509 * SNAP packets with an organization code of
1510 * 0x000000 (encapsulated Ethernet) and a protocol
1511 * type of ETHERTYPE_AARP (Appletalk ARP).
1513 if (proto
== ETHERTYPE_ATALK
)
1514 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1515 else /* proto == ETHERTYPE_AARP */
1516 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1520 * Check for Ethernet encapsulation (Ethertalk
1521 * phase 1?); we just check for the Ethernet
1524 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1530 if (proto
<= ETHERMTU
) {
1532 * This is an LLC SAP value, so the frames
1533 * that match would be 802.2 frames.
1534 * Check that the frame is an 802.2 frame
1535 * (i.e., that the length/type field is
1536 * a length field, <= ETHERMTU) and
1537 * then check the DSAP.
1539 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1541 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1547 * This is an Ethernet type, so compare
1548 * the length/type field with it (if
1549 * the frame is an 802.2 frame, the length
1550 * field will be <= ETHERMTU, and, as
1551 * "proto" is > ETHERMTU, this test
1552 * will fail and the frame won't match,
1553 * which is what we want).
1555 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1562 * Generate code to match a particular packet type.
1564 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1565 * value, if <= ETHERMTU. We use that to determine whether to
1566 * match the type field or to check the type field for the special
1567 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1569 static struct block
*
1570 gen_linux_sll_linktype(proto
)
1573 struct block
*b0
, *b1
;
1579 case LLCSAP_NETBEUI
:
1581 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1582 * so we check the DSAP and SSAP.
1584 * LLCSAP_IP checks for IP-over-802.2, rather
1585 * than IP-over-Ethernet or IP-over-SNAP.
1587 * XXX - should we check both the DSAP and the
1588 * SSAP, like this, or should we check just the
1589 * DSAP, as we do for other types <= ETHERMTU
1590 * (i.e., other SAP values)?
1592 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1593 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1594 ((proto
<< 8) | proto
));
1600 * Ethernet_II frames, which are Ethernet
1601 * frames with a frame type of ETHERTYPE_IPX;
1603 * Ethernet_802.3 frames, which have a frame
1604 * type of LINUX_SLL_P_802_3;
1606 * Ethernet_802.2 frames, which are 802.3
1607 * frames with an 802.2 LLC header (i.e, have
1608 * a frame type of LINUX_SLL_P_802_2) and
1609 * with the IPX LSAP as the DSAP in the LLC
1612 * Ethernet_SNAP frames, which are 802.3
1613 * frames with an LLC header and a SNAP
1614 * header and with an OUI of 0x000000
1615 * (encapsulated Ethernet) and a protocol
1616 * ID of ETHERTYPE_IPX in the SNAP header.
1618 * First, do the checks on LINUX_SLL_P_802_2
1619 * frames; generate the check for either
1620 * Ethernet_802.2 or Ethernet_SNAP frames, and
1621 * then put a check for LINUX_SLL_P_802_2 frames
1624 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1625 (bpf_int32
)LLCSAP_IPX
);
1626 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1629 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1633 * Now check for 802.3 frames and OR that with
1634 * the previous test.
1636 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1640 * Now add the check for Ethernet_II frames, and
1641 * do that before checking for the other frame
1644 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1645 (bpf_int32
)ETHERTYPE_IPX
);
1649 case ETHERTYPE_ATALK
:
1650 case ETHERTYPE_AARP
:
1652 * EtherTalk (AppleTalk protocols on Ethernet link
1653 * layer) may use 802.2 encapsulation.
1657 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1658 * we check for the 802.2 protocol type in the
1659 * "Ethernet type" field.
1661 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1664 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1665 * SNAP packets with an organization code of
1666 * 0x080007 (Apple, for Appletalk) and a protocol
1667 * type of ETHERTYPE_ATALK (Appletalk).
1669 * 802.2-encapsulated ETHERTYPE_AARP packets are
1670 * SNAP packets with an organization code of
1671 * 0x000000 (encapsulated Ethernet) and a protocol
1672 * type of ETHERTYPE_AARP (Appletalk ARP).
1674 if (proto
== ETHERTYPE_ATALK
)
1675 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1677 else /* proto == ETHERTYPE_AARP */
1678 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1683 * Check for Ethernet encapsulation (Ethertalk
1684 * phase 1?); we just check for the Ethernet
1687 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1693 if (proto
<= ETHERMTU
) {
1695 * This is an LLC SAP value, so the frames
1696 * that match would be 802.2 frames.
1697 * Check for the 802.2 protocol type
1698 * in the "Ethernet type" field, and
1699 * then check the DSAP.
1701 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1703 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1709 * This is an Ethernet type, so compare
1710 * the length/type field with it (if
1711 * the frame is an 802.2 frame, the length
1712 * field will be <= ETHERMTU, and, as
1713 * "proto" is > ETHERMTU, this test
1714 * will fail and the frame won't match,
1715 * which is what we want).
1717 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1724 insert_radiotap_load_llprefixlen(b
)
1727 struct slist
*s1
, *s2
;
1730 * Prepend to the statements in this block code to load the
1731 * length of the radiotap header into the register assigned
1732 * to hold that length, if one has been assigned.
1734 if (reg_ll_size
!= -1) {
1736 * The 2 bytes at offsets of 2 and 3 from the beginning
1737 * of the radiotap header are the length of the radiotap
1738 * header; unfortunately, it's little-endian, so we have
1739 * to load it a byte at a time and construct the value.
1743 * Load the high-order byte, at an offset of 3, shift it
1744 * left a byte, and put the result in the X register.
1746 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1748 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1751 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1755 * Load the next byte, at an offset of 2, and OR the
1756 * value from the X register into it.
1758 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1761 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1765 * Now allocate a register to hold that value and store
1768 s2
= new_stmt(BPF_ST
);
1769 s2
->s
.k
= reg_ll_size
;
1773 * Now move it into the X register.
1775 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1779 * Now append all the existing statements in this
1780 * block to these statements.
1782 sappend(s1
, b
->stmts
);
1789 insert_load_llprefixlen(b
)
1794 case DLT_IEEE802_11_RADIO
:
1795 insert_radiotap_load_llprefixlen(b
);
1800 static struct slist
*
1801 gen_radiotap_llprefixlen(void)
1805 if (reg_ll_size
== -1) {
1807 * We haven't yet assigned a register for the length
1808 * of the radiotap header; allocate one.
1810 reg_ll_size
= alloc_reg();
1814 * Load the register containing the radiotap length
1815 * into the X register.
1817 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1818 s
->s
.k
= reg_ll_size
;
1823 * Generate code to compute the link-layer header length, if necessary,
1824 * putting it into the X register, and to return either a pointer to a
1825 * "struct slist" for the list of statements in that code, or NULL if
1826 * no code is necessary.
1828 static struct slist
*
1829 gen_llprefixlen(void)
1833 case DLT_IEEE802_11_RADIO
:
1834 return gen_radiotap_llprefixlen();
1842 * Generate code to match a particular packet type by matching the
1843 * link-layer type field or fields in the 802.2 LLC header.
1845 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1846 * value, if <= ETHERMTU.
1848 static struct block
*
1852 struct block
*b0
, *b1
, *b2
;
1857 return gen_ether_linktype(proto
);
1865 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1869 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1878 case DLT_IEEE802_11
:
1879 case DLT_IEEE802_11_RADIO_AVS
:
1880 case DLT_IEEE802_11_RADIO
:
1881 case DLT_PRISM_HEADER
:
1882 case DLT_ATM_RFC1483
:
1884 case DLT_IP_OVER_FC
:
1885 return gen_llc_linktype(proto
);
1891 * If "is_lane" is set, check for a LANE-encapsulated
1892 * version of this protocol, otherwise check for an
1893 * LLC-encapsulated version of this protocol.
1895 * We assume LANE means Ethernet, not Token Ring.
1899 * Check that the packet doesn't begin with an
1900 * LE Control marker. (We've already generated
1903 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1908 * Now generate an Ethernet test.
1910 b1
= gen_ether_linktype(proto
);
1915 * Check for LLC encapsulation and then check the
1918 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1919 b1
= gen_llc_linktype(proto
);
1927 return gen_linux_sll_linktype(proto
);
1932 case DLT_SLIP_BSDOS
:
1935 * These types don't provide any type field; packets
1938 * XXX - for IPv4, check for a version number of 4, and,
1939 * for IPv6, check for a version number of 6?
1945 case ETHERTYPE_IPV6
:
1947 return gen_true(); /* always true */
1950 return gen_false(); /* always false */
1957 case DLT_PPP_SERIAL
:
1960 * We use Ethernet protocol types inside libpcap;
1961 * map them to the corresponding PPP protocol types.
1970 case ETHERTYPE_IPV6
:
1979 case ETHERTYPE_ATALK
:
1993 * I'm assuming the "Bridging PDU"s that go
1994 * over PPP are Spanning Tree Protocol
2008 * We use Ethernet protocol types inside libpcap;
2009 * map them to the corresponding PPP protocol types.
2014 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2015 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2017 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2022 case ETHERTYPE_IPV6
:
2032 case ETHERTYPE_ATALK
:
2046 * I'm assuming the "Bridging PDU"s that go
2047 * over PPP are Spanning Tree Protocol
2063 * For DLT_NULL, the link-layer header is a 32-bit
2064 * word containing an AF_ value in *host* byte order,
2065 * and for DLT_ENC, the link-layer header begins
2066 * with a 32-bit work containing an AF_ value in
2069 * In addition, if we're reading a saved capture file,
2070 * the host byte order in the capture may not be the
2071 * same as the host byte order on this machine.
2073 * For DLT_LOOP, the link-layer header is a 32-bit
2074 * word containing an AF_ value in *network* byte order.
2076 * XXX - AF_ values may, unfortunately, be platform-
2077 * dependent; for example, FreeBSD's AF_INET6 is 24
2078 * whilst NetBSD's and OpenBSD's is 26.
2080 * This means that, when reading a capture file, just
2081 * checking for our AF_INET6 value won't work if the
2082 * capture file came from another OS.
2091 case ETHERTYPE_IPV6
:
2098 * Not a type on which we support filtering.
2099 * XXX - support those that have AF_ values
2100 * #defined on this platform, at least?
2105 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2107 * The AF_ value is in host byte order, but
2108 * the BPF interpreter will convert it to
2109 * network byte order.
2111 * If this is a save file, and it's from a
2112 * machine with the opposite byte order to
2113 * ours, we byte-swap the AF_ value.
2115 * Then we run it through "htonl()", and
2116 * generate code to compare against the result.
2118 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2119 bpf_pcap
->sf
.swapped
)
2120 proto
= SWAPLONG(proto
);
2121 proto
= htonl(proto
);
2123 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2127 * af field is host byte order in contrast to the rest of
2130 if (proto
== ETHERTYPE_IP
)
2131 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2132 BPF_B
, (bpf_int32
)AF_INET
));
2134 else if (proto
== ETHERTYPE_IPV6
)
2135 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2136 BPF_B
, (bpf_int32
)AF_INET6
));
2144 case DLT_ARCNET_LINUX
:
2146 * XXX should we check for first fragment if the protocol
2155 case ETHERTYPE_IPV6
:
2156 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2157 (bpf_int32
)ARCTYPE_INET6
));
2161 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2162 (bpf_int32
)ARCTYPE_IP
);
2163 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2164 (bpf_int32
)ARCTYPE_IP_OLD
);
2169 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2170 (bpf_int32
)ARCTYPE_ARP
);
2171 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2172 (bpf_int32
)ARCTYPE_ARP_OLD
);
2176 case ETHERTYPE_REVARP
:
2177 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2178 (bpf_int32
)ARCTYPE_REVARP
));
2180 case ETHERTYPE_ATALK
:
2181 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2182 (bpf_int32
)ARCTYPE_ATALK
));
2189 case ETHERTYPE_ATALK
:
2199 * XXX - assumes a 2-byte Frame Relay header with
2200 * DLCI and flags. What if the address is longer?
2206 * Check for the special NLPID for IP.
2208 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2211 case ETHERTYPE_IPV6
:
2213 * Check for the special NLPID for IPv6.
2215 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2220 * Check for several OSI protocols.
2222 * Frame Relay packets typically have an OSI
2223 * NLPID at the beginning; we check for each
2226 * What we check for is the NLPID and a frame
2227 * control field of UI, i.e. 0x03 followed
2230 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2231 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2232 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2243 case DLT_JUNIPER_MLFR
:
2244 case DLT_JUNIPER_MLPPP
:
2245 case DLT_JUNIPER_ATM1
:
2246 case DLT_JUNIPER_ATM2
:
2247 case DLT_JUNIPER_PPPOE
:
2248 case DLT_JUNIPER_PPPOE_ATM
:
2249 case DLT_JUNIPER_GGSN
:
2250 case DLT_JUNIPER_ES
:
2251 case DLT_JUNIPER_MONITOR
:
2252 case DLT_JUNIPER_SERVICES
:
2253 /* just lets verify the magic number for now -
2254 * on ATM we may have up to 6 different encapsulations on the wire
2255 * and need a lot of heuristics to figure out that the payload
2258 * FIXME encapsulation specific BPF_ filters
2260 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2262 case DLT_LINUX_IRDA
:
2263 bpf_error("IrDA link-layer type filtering not implemented");
2266 bpf_error("DOCSIS link-layer type filtering not implemented");
2268 case DLT_LINUX_LAPD
:
2269 bpf_error("LAPD link-layer type filtering not implemented");
2273 * All the types that have no encapsulation should either be
2274 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2275 * all packets are IP packets, or should be handled in some
2276 * special case, if none of them are (if some are and some
2277 * aren't, the lack of encapsulation is a problem, as we'd
2278 * have to find some other way of determining the packet type).
2280 * Therefore, if "off_linktype" is -1, there's an error.
2282 if (off_linktype
== (u_int
)-1)
2286 * Any type not handled above should always have an Ethernet
2287 * type at an offset of "off_linktype". (PPP is partially
2288 * handled above - the protocol type is mapped from the
2289 * Ethernet and LLC types we use internally to the corresponding
2290 * PPP type - but the PPP type is always specified by a value
2291 * at "off_linktype", so we don't have to do the code generation
2294 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2298 * Check for an LLC SNAP packet with a given organization code and
2299 * protocol type; we check the entire contents of the 802.2 LLC and
2300 * snap headers, checking for DSAP and SSAP of SNAP and a control
2301 * field of 0x03 in the LLC header, and for the specified organization
2302 * code and protocol type in the SNAP header.
2304 static struct block
*
2305 gen_snap(orgcode
, ptype
, offset
)
2306 bpf_u_int32 orgcode
;
2310 u_char snapblock
[8];
2312 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2313 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2314 snapblock
[2] = 0x03; /* control = UI */
2315 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2316 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2317 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2318 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2319 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2320 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2324 * Generate code to match a particular packet type, for link-layer types
2325 * using 802.2 LLC headers.
2327 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2328 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2330 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2331 * value, if <= ETHERMTU. We use that to determine whether to
2332 * match the DSAP or both DSAP and LSAP or to check the OUI and
2333 * protocol ID in a SNAP header.
2335 static struct block
*
2336 gen_llc_linktype(proto
)
2340 * XXX - handle token-ring variable-length header.
2346 case LLCSAP_NETBEUI
:
2348 * XXX - should we check both the DSAP and the
2349 * SSAP, like this, or should we check just the
2350 * DSAP, as we do for other types <= ETHERMTU
2351 * (i.e., other SAP values)?
2353 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2354 ((proto
<< 8) | proto
));
2358 * XXX - are there ever SNAP frames for IPX on
2359 * non-Ethernet 802.x networks?
2361 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2362 (bpf_int32
)LLCSAP_IPX
);
2364 case ETHERTYPE_ATALK
:
2366 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2367 * SNAP packets with an organization code of
2368 * 0x080007 (Apple, for Appletalk) and a protocol
2369 * type of ETHERTYPE_ATALK (Appletalk).
2371 * XXX - check for an organization code of
2372 * encapsulated Ethernet as well?
2374 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2378 * XXX - we don't have to check for IPX 802.3
2379 * here, but should we check for the IPX Ethertype?
2381 if (proto
<= ETHERMTU
) {
2383 * This is an LLC SAP value, so check
2386 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2390 * This is an Ethernet type; we assume that it's
2391 * unlikely that it'll appear in the right place
2392 * at random, and therefore check only the
2393 * location that would hold the Ethernet type
2394 * in a SNAP frame with an organization code of
2395 * 0x000000 (encapsulated Ethernet).
2397 * XXX - if we were to check for the SNAP DSAP and
2398 * LSAP, as per XXX, and were also to check for an
2399 * organization code of 0x000000 (encapsulated
2400 * Ethernet), we'd do
2402 * return gen_snap(0x000000, proto,
2405 * here; for now, we don't, as per the above.
2406 * I don't know whether it's worth the extra CPU
2407 * time to do the right check or not.
2409 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2415 static struct block
*
2416 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2420 u_int src_off
, dst_off
;
2422 struct block
*b0
, *b1
;
2436 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2437 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2443 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2444 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2451 b0
= gen_linktype(proto
);
2452 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2458 static struct block
*
2459 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2460 struct in6_addr
*addr
;
2461 struct in6_addr
*mask
;
2463 u_int src_off
, dst_off
;
2465 struct block
*b0
, *b1
;
2480 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2481 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2487 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2488 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2495 /* this order is important */
2496 a
= (u_int32_t
*)addr
;
2497 m
= (u_int32_t
*)mask
;
2498 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2499 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2501 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2503 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2505 b0
= gen_linktype(proto
);
2511 static struct block
*
2512 gen_ehostop(eaddr
, dir
)
2513 register const u_char
*eaddr
;
2516 register struct block
*b0
, *b1
;
2520 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2523 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2526 b0
= gen_ehostop(eaddr
, Q_SRC
);
2527 b1
= gen_ehostop(eaddr
, Q_DST
);
2533 b0
= gen_ehostop(eaddr
, Q_SRC
);
2534 b1
= gen_ehostop(eaddr
, Q_DST
);
2543 * Like gen_ehostop, but for DLT_FDDI
2545 static struct block
*
2546 gen_fhostop(eaddr
, dir
)
2547 register const u_char
*eaddr
;
2550 struct block
*b0
, *b1
;
2555 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2557 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2562 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2564 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2568 b0
= gen_fhostop(eaddr
, Q_SRC
);
2569 b1
= gen_fhostop(eaddr
, Q_DST
);
2575 b0
= gen_fhostop(eaddr
, Q_SRC
);
2576 b1
= gen_fhostop(eaddr
, Q_DST
);
2585 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2587 static struct block
*
2588 gen_thostop(eaddr
, dir
)
2589 register const u_char
*eaddr
;
2592 register struct block
*b0
, *b1
;
2596 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2599 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2602 b0
= gen_thostop(eaddr
, Q_SRC
);
2603 b1
= gen_thostop(eaddr
, Q_DST
);
2609 b0
= gen_thostop(eaddr
, Q_SRC
);
2610 b1
= gen_thostop(eaddr
, Q_DST
);
2619 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2621 static struct block
*
2622 gen_wlanhostop(eaddr
, dir
)
2623 register const u_char
*eaddr
;
2626 register struct block
*b0
, *b1
, *b2
;
2627 register struct slist
*s
;
2634 * For control frames, there is no SA.
2636 * For management frames, SA is at an
2637 * offset of 10 from the beginning of
2640 * For data frames, SA is at an offset
2641 * of 10 from the beginning of the packet
2642 * if From DS is clear, at an offset of
2643 * 16 from the beginning of the packet
2644 * if From DS is set and To DS is clear,
2645 * and an offset of 24 from the beginning
2646 * of the packet if From DS is set and To DS
2651 * Generate the tests to be done for data frames
2654 * First, check for To DS set, i.e. check "link[1] & 0x01".
2656 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2657 b1
= new_block(JMP(BPF_JSET
));
2658 b1
->s
.k
= 0x01; /* To DS */
2662 * If To DS is set, the SA is at 24.
2664 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2668 * Now, check for To DS not set, i.e. check
2669 * "!(link[1] & 0x01)".
2671 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2672 b2
= new_block(JMP(BPF_JSET
));
2673 b2
->s
.k
= 0x01; /* To DS */
2678 * If To DS is not set, the SA is at 16.
2680 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2684 * Now OR together the last two checks. That gives
2685 * the complete set of checks for data frames with
2691 * Now check for From DS being set, and AND that with
2692 * the ORed-together checks.
2694 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2695 b1
= new_block(JMP(BPF_JSET
));
2696 b1
->s
.k
= 0x02; /* From DS */
2701 * Now check for data frames with From DS not set.
2703 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2704 b2
= new_block(JMP(BPF_JSET
));
2705 b2
->s
.k
= 0x02; /* From DS */
2710 * If From DS isn't set, the SA is at 10.
2712 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2716 * Now OR together the checks for data frames with
2717 * From DS not set and for data frames with From DS
2718 * set; that gives the checks done for data frames.
2723 * Now check for a data frame.
2724 * I.e, check "link[0] & 0x08".
2726 gen_load_a(OR_LINK
, 0, BPF_B
);
2727 b1
= new_block(JMP(BPF_JSET
));
2732 * AND that with the checks done for data frames.
2737 * If the high-order bit of the type value is 0, this
2738 * is a management frame.
2739 * I.e, check "!(link[0] & 0x08)".
2741 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2742 b2
= new_block(JMP(BPF_JSET
));
2748 * For management frames, the SA is at 10.
2750 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2754 * OR that with the checks done for data frames.
2755 * That gives the checks done for management and
2761 * If the low-order bit of the type value is 1,
2762 * this is either a control frame or a frame
2763 * with a reserved type, and thus not a
2766 * I.e., check "!(link[0] & 0x04)".
2768 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2769 b1
= new_block(JMP(BPF_JSET
));
2775 * AND that with the checks for data and management
2785 * For control frames, there is no DA.
2787 * For management frames, DA is at an
2788 * offset of 4 from the beginning of
2791 * For data frames, DA is at an offset
2792 * of 4 from the beginning of the packet
2793 * if To DS is clear and at an offset of
2794 * 16 from the beginning of the packet
2799 * Generate the tests to be done for data frames.
2801 * First, check for To DS set, i.e. "link[1] & 0x01".
2803 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2804 b1
= new_block(JMP(BPF_JSET
));
2805 b1
->s
.k
= 0x01; /* To DS */
2809 * If To DS is set, the DA is at 16.
2811 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2815 * Now, check for To DS not set, i.e. check
2816 * "!(link[1] & 0x01)".
2818 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2819 b2
= new_block(JMP(BPF_JSET
));
2820 b2
->s
.k
= 0x01; /* To DS */
2825 * If To DS is not set, the DA is at 4.
2827 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2831 * Now OR together the last two checks. That gives
2832 * the complete set of checks for data frames.
2837 * Now check for a data frame.
2838 * I.e, check "link[0] & 0x08".
2840 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2841 b1
= new_block(JMP(BPF_JSET
));
2846 * AND that with the checks done for data frames.
2851 * If the high-order bit of the type value is 0, this
2852 * is a management frame.
2853 * I.e, check "!(link[0] & 0x08)".
2855 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2856 b2
= new_block(JMP(BPF_JSET
));
2862 * For management frames, the DA is at 4.
2864 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2868 * OR that with the checks done for data frames.
2869 * That gives the checks done for management and
2875 * If the low-order bit of the type value is 1,
2876 * this is either a control frame or a frame
2877 * with a reserved type, and thus not a
2880 * I.e., check "!(link[0] & 0x04)".
2882 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2883 b1
= new_block(JMP(BPF_JSET
));
2889 * AND that with the checks for data and management
2896 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2897 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2903 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2904 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2913 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2914 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2915 * as the RFC states.)
2917 static struct block
*
2918 gen_ipfchostop(eaddr
, dir
)
2919 register const u_char
*eaddr
;
2922 register struct block
*b0
, *b1
;
2926 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2929 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2932 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2933 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2939 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2940 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2949 * This is quite tricky because there may be pad bytes in front of the
2950 * DECNET header, and then there are two possible data packet formats that
2951 * carry both src and dst addresses, plus 5 packet types in a format that
2952 * carries only the src node, plus 2 types that use a different format and
2953 * also carry just the src node.
2957 * Instead of doing those all right, we just look for data packets with
2958 * 0 or 1 bytes of padding. If you want to look at other packets, that
2959 * will require a lot more hacking.
2961 * To add support for filtering on DECNET "areas" (network numbers)
2962 * one would want to add a "mask" argument to this routine. That would
2963 * make the filter even more inefficient, although one could be clever
2964 * and not generate masking instructions if the mask is 0xFFFF.
2966 static struct block
*
2967 gen_dnhostop(addr
, dir
)
2971 struct block
*b0
, *b1
, *b2
, *tmp
;
2972 u_int offset_lh
; /* offset if long header is received */
2973 u_int offset_sh
; /* offset if short header is received */
2978 offset_sh
= 1; /* follows flags */
2979 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2983 offset_sh
= 3; /* follows flags, dstnode */
2984 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2988 /* Inefficient because we do our Calvinball dance twice */
2989 b0
= gen_dnhostop(addr
, Q_SRC
);
2990 b1
= gen_dnhostop(addr
, Q_DST
);
2996 /* Inefficient because we do our Calvinball dance twice */
2997 b0
= gen_dnhostop(addr
, Q_SRC
);
2998 b1
= gen_dnhostop(addr
, Q_DST
);
3003 bpf_error("ISO host filtering not implemented");
3008 b0
= gen_linktype(ETHERTYPE_DN
);
3009 /* Check for pad = 1, long header case */
3010 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3011 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3012 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3013 BPF_H
, (bpf_int32
)ntohs(addr
));
3015 /* Check for pad = 0, long header case */
3016 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3017 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3020 /* Check for pad = 1, short header case */
3021 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3022 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3023 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3026 /* Check for pad = 0, short header case */
3027 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3028 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3032 /* Combine with test for linktype */
3037 static struct block
*
3038 gen_host(addr
, mask
, proto
, dir
)
3044 struct block
*b0
, *b1
;
3049 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
3050 if (off_linktype
!= (u_int
)-1) {
3051 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
3053 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
3059 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3062 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3065 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3068 bpf_error("'tcp' modifier applied to host");
3071 bpf_error("'sctp' modifier applied to host");
3074 bpf_error("'udp' modifier applied to host");
3077 bpf_error("'icmp' modifier applied to host");
3080 bpf_error("'igmp' modifier applied to host");
3083 bpf_error("'igrp' modifier applied to host");
3086 bpf_error("'pim' modifier applied to host");
3089 bpf_error("'vrrp' modifier applied to host");
3092 bpf_error("ATALK host filtering not implemented");
3095 bpf_error("AARP host filtering not implemented");
3098 return gen_dnhostop(addr
, dir
);
3101 bpf_error("SCA host filtering not implemented");
3104 bpf_error("LAT host filtering not implemented");
3107 bpf_error("MOPDL host filtering not implemented");
3110 bpf_error("MOPRC host filtering not implemented");
3114 bpf_error("'ip6' modifier applied to ip host");
3117 bpf_error("'icmp6' modifier applied to host");
3121 bpf_error("'ah' modifier applied to host");
3124 bpf_error("'esp' modifier applied to host");
3127 bpf_error("ISO host filtering not implemented");
3130 bpf_error("'esis' modifier applied to host");
3133 bpf_error("'isis' modifier applied to host");
3136 bpf_error("'clnp' modifier applied to host");
3139 bpf_error("'stp' modifier applied to host");
3142 bpf_error("IPX host filtering not implemented");
3145 bpf_error("'netbeui' modifier applied to host");
3148 bpf_error("'radio' modifier applied to host");
3157 static struct block
*
3158 gen_host6(addr
, mask
, proto
, dir
)
3159 struct in6_addr
*addr
;
3160 struct in6_addr
*mask
;
3167 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3170 bpf_error("'ip' modifier applied to ip6 host");
3173 bpf_error("'rarp' modifier applied to ip6 host");
3176 bpf_error("'arp' modifier applied to ip6 host");
3179 bpf_error("'sctp' modifier applied to host");
3182 bpf_error("'tcp' modifier applied to host");
3185 bpf_error("'udp' modifier applied to host");
3188 bpf_error("'icmp' modifier applied to host");
3191 bpf_error("'igmp' modifier applied to host");
3194 bpf_error("'igrp' modifier applied to host");
3197 bpf_error("'pim' modifier applied to host");
3200 bpf_error("'vrrp' modifier applied to host");
3203 bpf_error("ATALK host filtering not implemented");
3206 bpf_error("AARP host filtering not implemented");
3209 bpf_error("'decnet' modifier applied to ip6 host");
3212 bpf_error("SCA host filtering not implemented");
3215 bpf_error("LAT host filtering not implemented");
3218 bpf_error("MOPDL host filtering not implemented");
3221 bpf_error("MOPRC host filtering not implemented");
3224 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3227 bpf_error("'icmp6' modifier applied to host");
3230 bpf_error("'ah' modifier applied to host");
3233 bpf_error("'esp' modifier applied to host");
3236 bpf_error("ISO host filtering not implemented");
3239 bpf_error("'esis' modifier applied to host");
3242 bpf_error("'isis' modifier applied to host");
3245 bpf_error("'clnp' modifier applied to host");
3248 bpf_error("'stp' modifier applied to host");
3251 bpf_error("IPX host filtering not implemented");
3254 bpf_error("'netbeui' modifier applied to host");
3257 bpf_error("'radio' modifier applied to host");
3267 static struct block
*
3268 gen_gateway(eaddr
, alist
, proto
, dir
)
3269 const u_char
*eaddr
;
3270 bpf_u_int32
**alist
;
3274 struct block
*b0
, *b1
, *tmp
;
3277 bpf_error("direction applied to 'gateway'");
3284 if (linktype
== DLT_EN10MB
)
3285 b0
= gen_ehostop(eaddr
, Q_OR
);
3286 else if (linktype
== DLT_FDDI
)
3287 b0
= gen_fhostop(eaddr
, Q_OR
);
3288 else if (linktype
== DLT_IEEE802
)
3289 b0
= gen_thostop(eaddr
, Q_OR
);
3290 else if (linktype
== DLT_IEEE802_11
||
3291 linktype
== DLT_IEEE802_11_RADIO_AVS
||
3292 linktype
== DLT_IEEE802_11_RADIO
||
3293 linktype
== DLT_PRISM_HEADER
)
3294 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3295 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3297 * Check that the packet doesn't begin with an
3298 * LE Control marker. (We've already generated
3301 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3306 * Now check the MAC address.
3308 b0
= gen_ehostop(eaddr
, Q_OR
);
3310 } else if (linktype
== DLT_IP_OVER_FC
)
3311 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3314 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3316 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3318 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3326 bpf_error("illegal modifier of 'gateway'");
3332 gen_proto_abbrev(proto
)
3341 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3343 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3349 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3351 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3357 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3359 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3365 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3368 #ifndef IPPROTO_IGMP
3369 #define IPPROTO_IGMP 2
3373 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3376 #ifndef IPPROTO_IGRP
3377 #define IPPROTO_IGRP 9
3380 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3384 #define IPPROTO_PIM 103
3388 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3390 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3395 #ifndef IPPROTO_VRRP
3396 #define IPPROTO_VRRP 112
3400 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3404 b1
= gen_linktype(ETHERTYPE_IP
);
3408 b1
= gen_linktype(ETHERTYPE_ARP
);
3412 b1
= gen_linktype(ETHERTYPE_REVARP
);
3416 bpf_error("link layer applied in wrong context");
3419 b1
= gen_linktype(ETHERTYPE_ATALK
);
3423 b1
= gen_linktype(ETHERTYPE_AARP
);
3427 b1
= gen_linktype(ETHERTYPE_DN
);
3431 b1
= gen_linktype(ETHERTYPE_SCA
);
3435 b1
= gen_linktype(ETHERTYPE_LAT
);
3439 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3443 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3448 b1
= gen_linktype(ETHERTYPE_IPV6
);
3451 #ifndef IPPROTO_ICMPV6
3452 #define IPPROTO_ICMPV6 58
3455 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3460 #define IPPROTO_AH 51
3463 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3465 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3471 #define IPPROTO_ESP 50
3474 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3476 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3482 b1
= gen_linktype(LLCSAP_ISONS
);
3486 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3490 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3493 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3494 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3495 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3497 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3499 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3501 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3505 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3506 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3507 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3509 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3511 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3513 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3517 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3518 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3519 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3521 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3526 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3527 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3532 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3533 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3535 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3537 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3542 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3543 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3548 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3549 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3554 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3558 b1
= gen_linktype(LLCSAP_8021D
);
3562 b1
= gen_linktype(LLCSAP_IPX
);
3566 b1
= gen_linktype(LLCSAP_NETBEUI
);
3570 bpf_error("'radio' is not a valid protocol type");
3578 static struct block
*
3585 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3586 b
= new_block(JMP(BPF_JSET
));
3595 * Generate a comparison to a port value in the transport-layer header
3596 * at the specified offset from the beginning of that header.
3598 * XXX - this handles a variable-length prefix preceding the link-layer
3599 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3600 * variable-length link-layer headers (such as Token Ring or 802.11
3603 static struct block
*
3604 gen_portatom(off
, v
)
3608 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3612 static struct block
*
3613 gen_portatom6(off
, v
)
3617 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3622 gen_portop(port
, proto
, dir
)
3623 int port
, proto
, dir
;
3625 struct block
*b0
, *b1
, *tmp
;
3627 /* ip proto 'proto' */
3628 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3634 b1
= gen_portatom(0, (bpf_int32
)port
);
3638 b1
= gen_portatom(2, (bpf_int32
)port
);
3643 tmp
= gen_portatom(0, (bpf_int32
)port
);
3644 b1
= gen_portatom(2, (bpf_int32
)port
);
3649 tmp
= gen_portatom(0, (bpf_int32
)port
);
3650 b1
= gen_portatom(2, (bpf_int32
)port
);
3662 static struct block
*
3663 gen_port(port
, ip_proto
, dir
)
3668 struct block
*b0
, *b1
, *tmp
;
3673 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3674 * not LLC encapsulation with LLCSAP_IP.
3676 * For IEEE 802 networks - which includes 802.5 token ring
3677 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3678 * says that SNAP encapsulation is used, not LLC encapsulation
3681 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3682 * RFC 2225 say that SNAP encapsulation is used, not LLC
3683 * encapsulation with LLCSAP_IP.
3685 * So we always check for ETHERTYPE_IP.
3687 b0
= gen_linktype(ETHERTYPE_IP
);
3693 b1
= gen_portop(port
, ip_proto
, dir
);
3697 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3698 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3700 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3713 gen_portop6(port
, proto
, dir
)
3714 int port
, proto
, dir
;
3716 struct block
*b0
, *b1
, *tmp
;
3718 /* ip6 proto 'proto' */
3719 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3723 b1
= gen_portatom6(0, (bpf_int32
)port
);
3727 b1
= gen_portatom6(2, (bpf_int32
)port
);
3732 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3733 b1
= gen_portatom6(2, (bpf_int32
)port
);
3738 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3739 b1
= gen_portatom6(2, (bpf_int32
)port
);
3751 static struct block
*
3752 gen_port6(port
, ip_proto
, dir
)
3757 struct block
*b0
, *b1
, *tmp
;
3759 /* link proto ip6 */
3760 b0
= gen_linktype(ETHERTYPE_IPV6
);
3766 b1
= gen_portop6(port
, ip_proto
, dir
);
3770 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3771 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3773 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3785 /* gen_portrange code */
3786 static struct block
*
3787 gen_portrangeatom(off
, v1
, v2
)
3791 struct block
*b1
, *b2
;
3795 * Reverse the order of the ports, so v1 is the lower one.
3804 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3805 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3813 gen_portrangeop(port1
, port2
, proto
, dir
)
3818 struct block
*b0
, *b1
, *tmp
;
3820 /* ip proto 'proto' */
3821 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3827 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3831 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3836 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3837 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3842 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3843 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3855 static struct block
*
3856 gen_portrange(port1
, port2
, ip_proto
, dir
)
3861 struct block
*b0
, *b1
, *tmp
;
3864 b0
= gen_linktype(ETHERTYPE_IP
);
3870 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3874 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3875 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3877 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3889 static struct block
*
3890 gen_portrangeatom6(off
, v1
, v2
)
3894 struct block
*b1
, *b2
;
3898 * Reverse the order of the ports, so v1 is the lower one.
3907 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3908 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3916 gen_portrangeop6(port1
, port2
, proto
, dir
)
3921 struct block
*b0
, *b1
, *tmp
;
3923 /* ip6 proto 'proto' */
3924 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3928 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3932 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3937 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3938 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3943 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3944 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3956 static struct block
*
3957 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3962 struct block
*b0
, *b1
, *tmp
;
3964 /* link proto ip6 */
3965 b0
= gen_linktype(ETHERTYPE_IPV6
);
3971 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3975 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3976 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3978 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3991 lookup_proto(name
, proto
)
3992 register const char *name
;
4002 v
= pcap_nametoproto(name
);
4003 if (v
== PROTO_UNDEF
)
4004 bpf_error("unknown ip proto '%s'", name
);
4008 /* XXX should look up h/w protocol type based on linktype */
4009 v
= pcap_nametoeproto(name
);
4010 if (v
== PROTO_UNDEF
) {
4011 v
= pcap_nametollc(name
);
4012 if (v
== PROTO_UNDEF
)
4013 bpf_error("unknown ether proto '%s'", name
);
4018 if (strcmp(name
, "esis") == 0)
4020 else if (strcmp(name
, "isis") == 0)
4022 else if (strcmp(name
, "clnp") == 0)
4025 bpf_error("unknown osi proto '%s'", name
);
4045 static struct block
*
4046 gen_protochain(v
, proto
, dir
)
4051 #ifdef NO_PROTOCHAIN
4052 return gen_proto(v
, proto
, dir
);
4054 struct block
*b0
, *b
;
4055 struct slist
*s
[100];
4056 int fix2
, fix3
, fix4
, fix5
;
4057 int ahcheck
, again
, end
;
4059 int reg2
= alloc_reg();
4061 memset(s
, 0, sizeof(s
));
4062 fix2
= fix3
= fix4
= fix5
= 0;
4069 b0
= gen_protochain(v
, Q_IP
, dir
);
4070 b
= gen_protochain(v
, Q_IPV6
, dir
);
4074 bpf_error("bad protocol applied for 'protochain'");
4079 * We don't handle variable-length radiotap here headers yet.
4080 * We might want to add BPF instructions to do the protochain
4081 * work, to simplify that and, on platforms that have a BPF
4082 * interpreter with the new instructions, let the filtering
4083 * be done in the kernel. (We already require a modified BPF
4084 * engine to do the protochain stuff, to support backward
4085 * branches, and backward branch support is unlikely to appear
4086 * in kernel BPF engines.)
4088 if (linktype
== DLT_IEEE802_11_RADIO
)
4089 bpf_error("'protochain' not supported with radiotap headers");
4091 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4094 * s[0] is a dummy entry to protect other BPF insn from damage
4095 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4096 * hard to find interdependency made by jump table fixup.
4099 s
[i
] = new_stmt(0); /*dummy*/
4104 b0
= gen_linktype(ETHERTYPE_IP
);
4107 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4108 s
[i
]->s
.k
= off_nl
+ 9;
4110 /* X = ip->ip_hl << 2 */
4111 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4117 b0
= gen_linktype(ETHERTYPE_IPV6
);
4119 /* A = ip6->ip_nxt */
4120 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4121 s
[i
]->s
.k
= off_nl
+ 6;
4123 /* X = sizeof(struct ip6_hdr) */
4124 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4130 bpf_error("unsupported proto to gen_protochain");
4134 /* again: if (A == v) goto end; else fall through; */
4136 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4138 s
[i
]->s
.jt
= NULL
; /*later*/
4139 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4143 #ifndef IPPROTO_NONE
4144 #define IPPROTO_NONE 59
4146 /* if (A == IPPROTO_NONE) goto end */
4147 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4148 s
[i
]->s
.jt
= NULL
; /*later*/
4149 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4150 s
[i
]->s
.k
= IPPROTO_NONE
;
4151 s
[fix5
]->s
.jf
= s
[i
];
4156 if (proto
== Q_IPV6
) {
4157 int v6start
, v6end
, v6advance
, j
;
4160 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4161 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4162 s
[i
]->s
.jt
= NULL
; /*later*/
4163 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4164 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4165 s
[fix2
]->s
.jf
= s
[i
];
4167 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4168 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4169 s
[i
]->s
.jt
= NULL
; /*later*/
4170 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4171 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4173 /* if (A == IPPROTO_ROUTING) goto v6advance */
4174 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4175 s
[i
]->s
.jt
= NULL
; /*later*/
4176 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4177 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4179 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4180 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4181 s
[i
]->s
.jt
= NULL
; /*later*/
4182 s
[i
]->s
.jf
= NULL
; /*later*/
4183 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4194 * X = X + (P[X + 1] + 1) * 8;
4197 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4199 /* A = P[X + packet head] */
4200 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4204 s
[i
] = new_stmt(BPF_ST
);
4208 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4211 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4215 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4217 /* A = P[X + packet head]; */
4218 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4222 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4226 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4230 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4233 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4237 /* goto again; (must use BPF_JA for backward jump) */
4238 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4239 s
[i
]->s
.k
= again
- i
- 1;
4240 s
[i
- 1]->s
.jf
= s
[i
];
4244 for (j
= v6start
; j
<= v6end
; j
++)
4245 s
[j
]->s
.jt
= s
[v6advance
];
4250 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4252 s
[fix2
]->s
.jf
= s
[i
];
4258 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4259 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4260 s
[i
]->s
.jt
= NULL
; /*later*/
4261 s
[i
]->s
.jf
= NULL
; /*later*/
4262 s
[i
]->s
.k
= IPPROTO_AH
;
4264 s
[fix3
]->s
.jf
= s
[ahcheck
];
4271 * X = X + (P[X + 1] + 2) * 4;
4274 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4276 /* A = P[X + packet head]; */
4277 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4281 s
[i
] = new_stmt(BPF_ST
);
4285 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4288 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4292 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4294 /* A = P[X + packet head] */
4295 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4299 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4303 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4307 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4310 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4314 /* goto again; (must use BPF_JA for backward jump) */
4315 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4316 s
[i
]->s
.k
= again
- i
- 1;
4321 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4323 s
[fix2
]->s
.jt
= s
[end
];
4324 s
[fix4
]->s
.jf
= s
[end
];
4325 s
[fix5
]->s
.jt
= s
[end
];
4332 for (i
= 0; i
< max
- 1; i
++)
4333 s
[i
]->next
= s
[i
+ 1];
4334 s
[max
- 1]->next
= NULL
;
4339 b
= new_block(JMP(BPF_JEQ
));
4340 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4351 * Generate code that checks whether the packet is a packet for protocol
4352 * <proto> and whether the type field in that protocol's header has
4353 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4354 * IP packet and checks the protocol number in the IP header against <v>.
4356 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4357 * against Q_IP and Q_IPV6.
4359 static struct block
*
4360 gen_proto(v
, proto
, dir
)
4365 struct block
*b0
, *b1
;
4367 if (dir
!= Q_DEFAULT
)
4368 bpf_error("direction applied to 'proto'");
4373 b0
= gen_proto(v
, Q_IP
, dir
);
4374 b1
= gen_proto(v
, Q_IPV6
, dir
);
4382 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4383 * not LLC encapsulation with LLCSAP_IP.
4385 * For IEEE 802 networks - which includes 802.5 token ring
4386 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4387 * says that SNAP encapsulation is used, not LLC encapsulation
4390 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4391 * RFC 2225 say that SNAP encapsulation is used, not LLC
4392 * encapsulation with LLCSAP_IP.
4394 * So we always check for ETHERTYPE_IP.
4396 b0
= gen_linktype(ETHERTYPE_IP
);
4398 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4400 b1
= gen_protochain(v
, Q_IP
);
4410 * Frame Relay packets typically have an OSI
4411 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4412 * generates code to check for all the OSI
4413 * NLPIDs, so calling it and then adding a check
4414 * for the particular NLPID for which we're
4415 * looking is bogus, as we can just check for
4418 * What we check for is the NLPID and a frame
4419 * control field value of UI, i.e. 0x03 followed
4422 * XXX - assumes a 2-byte Frame Relay header with
4423 * DLCI and flags. What if the address is longer?
4425 * XXX - what about SNAP-encapsulated frames?
4427 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4433 * Cisco uses an Ethertype lookalike - for OSI,
4436 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4437 /* OSI in C-HDLC is stuffed with a fudge byte */
4438 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4443 b0
= gen_linktype(LLCSAP_ISONS
);
4444 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4450 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4452 * 4 is the offset of the PDU type relative to the IS-IS
4455 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4460 bpf_error("arp does not encapsulate another protocol");
4464 bpf_error("rarp does not encapsulate another protocol");
4468 bpf_error("atalk encapsulation is not specifiable");
4472 bpf_error("decnet encapsulation is not specifiable");
4476 bpf_error("sca does not encapsulate another protocol");
4480 bpf_error("lat does not encapsulate another protocol");
4484 bpf_error("moprc does not encapsulate another protocol");
4488 bpf_error("mopdl does not encapsulate another protocol");
4492 return gen_linktype(v
);
4495 bpf_error("'udp proto' is bogus");
4499 bpf_error("'tcp proto' is bogus");
4503 bpf_error("'sctp proto' is bogus");
4507 bpf_error("'icmp proto' is bogus");
4511 bpf_error("'igmp proto' is bogus");
4515 bpf_error("'igrp proto' is bogus");
4519 bpf_error("'pim proto' is bogus");
4523 bpf_error("'vrrp proto' is bogus");
4528 b0
= gen_linktype(ETHERTYPE_IPV6
);
4530 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4532 b1
= gen_protochain(v
, Q_IPV6
);
4538 bpf_error("'icmp6 proto' is bogus");
4542 bpf_error("'ah proto' is bogus");
4545 bpf_error("'ah proto' is bogus");
4548 bpf_error("'stp proto' is bogus");
4551 bpf_error("'ipx proto' is bogus");
4554 bpf_error("'netbeui proto' is bogus");
4557 bpf_error("'radio proto' is bogus");
4568 register const char *name
;
4571 int proto
= q
.proto
;
4575 bpf_u_int32 mask
, addr
;
4577 bpf_u_int32
**alist
;
4580 struct sockaddr_in
*sin
;
4581 struct sockaddr_in6
*sin6
;
4582 struct addrinfo
*res
, *res0
;
4583 struct in6_addr mask128
;
4585 struct block
*b
, *tmp
;
4586 int port
, real_proto
;
4592 addr
= pcap_nametonetaddr(name
);
4594 bpf_error("unknown network '%s'", name
);
4595 /* Left justify network addr and calculate its network mask */
4597 while (addr
&& (addr
& 0xff000000) == 0) {
4601 return gen_host(addr
, mask
, proto
, dir
);
4605 if (proto
== Q_LINK
) {
4609 eaddr
= pcap_ether_hostton(name
);
4612 "unknown ether host '%s'", name
);
4613 b
= gen_ehostop(eaddr
, dir
);
4618 eaddr
= pcap_ether_hostton(name
);
4621 "unknown FDDI host '%s'", name
);
4622 b
= gen_fhostop(eaddr
, dir
);
4627 eaddr
= pcap_ether_hostton(name
);
4630 "unknown token ring host '%s'", name
);
4631 b
= gen_thostop(eaddr
, dir
);
4635 case DLT_IEEE802_11
:
4636 case DLT_IEEE802_11_RADIO_AVS
:
4637 case DLT_IEEE802_11_RADIO
:
4638 case DLT_PRISM_HEADER
:
4639 eaddr
= pcap_ether_hostton(name
);
4642 "unknown 802.11 host '%s'", name
);
4643 b
= gen_wlanhostop(eaddr
, dir
);
4647 case DLT_IP_OVER_FC
:
4648 eaddr
= pcap_ether_hostton(name
);
4651 "unknown Fibre Channel host '%s'", name
);
4652 b
= gen_ipfchostop(eaddr
, dir
);
4661 * Check that the packet doesn't begin
4662 * with an LE Control marker. (We've
4663 * already generated a test for LANE.)
4665 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4669 eaddr
= pcap_ether_hostton(name
);
4672 "unknown ether host '%s'", name
);
4673 b
= gen_ehostop(eaddr
, dir
);
4679 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4680 } else if (proto
== Q_DECNET
) {
4681 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4683 * I don't think DECNET hosts can be multihomed, so
4684 * there is no need to build up a list of addresses
4686 return (gen_host(dn_addr
, 0, proto
, dir
));
4689 alist
= pcap_nametoaddr(name
);
4690 if (alist
== NULL
|| *alist
== NULL
)
4691 bpf_error("unknown host '%s'", name
);
4693 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4695 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4697 tmp
= gen_host(**alist
++, 0xffffffff,
4704 memset(&mask128
, 0xff, sizeof(mask128
));
4705 res0
= res
= pcap_nametoaddrinfo(name
);
4707 bpf_error("unknown host '%s'", name
);
4709 tproto
= tproto6
= proto
;
4710 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4714 for (res
= res0
; res
; res
= res
->ai_next
) {
4715 switch (res
->ai_family
) {
4717 if (tproto
== Q_IPV6
)
4720 sin
= (struct sockaddr_in
*)
4722 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4723 0xffffffff, tproto
, dir
);
4726 if (tproto6
== Q_IP
)
4729 sin6
= (struct sockaddr_in6
*)
4731 tmp
= gen_host6(&sin6
->sin6_addr
,
4732 &mask128
, tproto6
, dir
);
4743 bpf_error("unknown host '%s'%s", name
,
4744 (proto
== Q_DEFAULT
)
4746 : " for specified address family");
4753 if (proto
!= Q_DEFAULT
&&
4754 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4755 bpf_error("illegal qualifier of 'port'");
4756 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4757 bpf_error("unknown port '%s'", name
);
4758 if (proto
== Q_UDP
) {
4759 if (real_proto
== IPPROTO_TCP
)
4760 bpf_error("port '%s' is tcp", name
);
4761 else if (real_proto
== IPPROTO_SCTP
)
4762 bpf_error("port '%s' is sctp", name
);
4764 /* override PROTO_UNDEF */
4765 real_proto
= IPPROTO_UDP
;
4767 if (proto
== Q_TCP
) {
4768 if (real_proto
== IPPROTO_UDP
)
4769 bpf_error("port '%s' is udp", name
);
4771 else if (real_proto
== IPPROTO_SCTP
)
4772 bpf_error("port '%s' is sctp", name
);
4774 /* override PROTO_UNDEF */
4775 real_proto
= IPPROTO_TCP
;
4777 if (proto
== Q_SCTP
) {
4778 if (real_proto
== IPPROTO_UDP
)
4779 bpf_error("port '%s' is udp", name
);
4781 else if (real_proto
== IPPROTO_TCP
)
4782 bpf_error("port '%s' is tcp", name
);
4784 /* override PROTO_UNDEF */
4785 real_proto
= IPPROTO_SCTP
;
4788 return gen_port(port
, real_proto
, dir
);
4792 b
= gen_port(port
, real_proto
, dir
);
4793 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4799 if (proto
!= Q_DEFAULT
&&
4800 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4801 bpf_error("illegal qualifier of 'portrange'");
4802 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4803 bpf_error("unknown port in range '%s'", name
);
4804 if (proto
== Q_UDP
) {
4805 if (real_proto
== IPPROTO_TCP
)
4806 bpf_error("port in range '%s' is tcp", name
);
4807 else if (real_proto
== IPPROTO_SCTP
)
4808 bpf_error("port in range '%s' is sctp", name
);
4810 /* override PROTO_UNDEF */
4811 real_proto
= IPPROTO_UDP
;
4813 if (proto
== Q_TCP
) {
4814 if (real_proto
== IPPROTO_UDP
)
4815 bpf_error("port in range '%s' is udp", name
);
4816 else if (real_proto
== IPPROTO_SCTP
)
4817 bpf_error("port in range '%s' is sctp", name
);
4819 /* override PROTO_UNDEF */
4820 real_proto
= IPPROTO_TCP
;
4822 if (proto
== Q_SCTP
) {
4823 if (real_proto
== IPPROTO_UDP
)
4824 bpf_error("port in range '%s' is udp", name
);
4825 else if (real_proto
== IPPROTO_TCP
)
4826 bpf_error("port in range '%s' is tcp", name
);
4828 /* override PROTO_UNDEF */
4829 real_proto
= IPPROTO_SCTP
;
4832 return gen_portrange(port1
, port2
, real_proto
, dir
);
4836 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4837 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4844 eaddr
= pcap_ether_hostton(name
);
4846 bpf_error("unknown ether host: %s", name
);
4848 alist
= pcap_nametoaddr(name
);
4849 if (alist
== NULL
|| *alist
== NULL
)
4850 bpf_error("unknown host '%s'", name
);
4851 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4855 bpf_error("'gateway' not supported in this configuration");
4859 real_proto
= lookup_proto(name
, proto
);
4860 if (real_proto
>= 0)
4861 return gen_proto(real_proto
, proto
, dir
);
4863 bpf_error("unknown protocol: %s", name
);
4866 real_proto
= lookup_proto(name
, proto
);
4867 if (real_proto
>= 0)
4868 return gen_protochain(real_proto
, proto
, dir
);
4870 bpf_error("unknown protocol: %s", name
);
4882 gen_mcode(s1
, s2
, masklen
, q
)
4883 register const char *s1
, *s2
;
4884 register int masklen
;
4887 register int nlen
, mlen
;
4890 nlen
= __pcap_atoin(s1
, &n
);
4891 /* Promote short ipaddr */
4895 mlen
= __pcap_atoin(s2
, &m
);
4896 /* Promote short ipaddr */
4899 bpf_error("non-network bits set in \"%s mask %s\"",
4902 /* Convert mask len to mask */
4904 bpf_error("mask length must be <= 32");
4905 m
= 0xffffffff << (32 - masklen
);
4907 bpf_error("non-network bits set in \"%s/%d\"",
4914 return gen_host(n
, m
, q
.proto
, q
.dir
);
4917 bpf_error("Mask syntax for networks only");
4925 register const char *s
;
4930 int proto
= q
.proto
;
4936 else if (q
.proto
== Q_DECNET
)
4937 vlen
= __pcap_atodn(s
, &v
);
4939 vlen
= __pcap_atoin(s
, &v
);
4946 if (proto
== Q_DECNET
)
4947 return gen_host(v
, 0, proto
, dir
);
4948 else if (proto
== Q_LINK
) {
4949 bpf_error("illegal link layer address");
4952 if (s
== NULL
&& q
.addr
== Q_NET
) {
4953 /* Promote short net number */
4954 while (v
&& (v
& 0xff000000) == 0) {
4959 /* Promote short ipaddr */
4963 return gen_host(v
, mask
, proto
, dir
);
4968 proto
= IPPROTO_UDP
;
4969 else if (proto
== Q_TCP
)
4970 proto
= IPPROTO_TCP
;
4971 else if (proto
== Q_SCTP
)
4972 proto
= IPPROTO_SCTP
;
4973 else if (proto
== Q_DEFAULT
)
4974 proto
= PROTO_UNDEF
;
4976 bpf_error("illegal qualifier of 'port'");
4979 return gen_port((int)v
, proto
, dir
);
4983 b
= gen_port((int)v
, proto
, dir
);
4984 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4991 proto
= IPPROTO_UDP
;
4992 else if (proto
== Q_TCP
)
4993 proto
= IPPROTO_TCP
;
4994 else if (proto
== Q_SCTP
)
4995 proto
= IPPROTO_SCTP
;
4996 else if (proto
== Q_DEFAULT
)
4997 proto
= PROTO_UNDEF
;
4999 bpf_error("illegal qualifier of 'portrange'");
5002 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5006 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5007 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5013 bpf_error("'gateway' requires a name");
5017 return gen_proto((int)v
, proto
, dir
);
5020 return gen_protochain((int)v
, proto
, dir
);
5035 gen_mcode6(s1
, s2
, masklen
, q
)
5036 register const char *s1
, *s2
;
5037 register int masklen
;
5040 struct addrinfo
*res
;
5041 struct in6_addr
*addr
;
5042 struct in6_addr mask
;
5047 bpf_error("no mask %s supported", s2
);
5049 res
= pcap_nametoaddrinfo(s1
);
5051 bpf_error("invalid ip6 address %s", s1
);
5053 bpf_error("%s resolved to multiple address", s1
);
5054 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5056 if (sizeof(mask
) * 8 < masklen
)
5057 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5058 memset(&mask
, 0, sizeof(mask
));
5059 memset(&mask
, 0xff, masklen
/ 8);
5061 mask
.s6_addr
[masklen
/ 8] =
5062 (0xff << (8 - masklen
% 8)) & 0xff;
5065 a
= (u_int32_t
*)addr
;
5066 m
= (u_int32_t
*)&mask
;
5067 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5068 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5069 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5077 bpf_error("Mask syntax for networks only");
5081 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
5086 bpf_error("invalid qualifier against IPv6 address");
5094 register const u_char
*eaddr
;
5097 struct block
*b
, *tmp
;
5099 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5100 if (linktype
== DLT_EN10MB
)
5101 return gen_ehostop(eaddr
, (int)q
.dir
);
5102 if (linktype
== DLT_FDDI
)
5103 return gen_fhostop(eaddr
, (int)q
.dir
);
5104 if (linktype
== DLT_IEEE802
)
5105 return gen_thostop(eaddr
, (int)q
.dir
);
5106 if (linktype
== DLT_IEEE802_11
||
5107 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5108 linktype
== DLT_IEEE802_11_RADIO
||
5109 linktype
== DLT_PRISM_HEADER
)
5110 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5111 if (linktype
== DLT_SUNATM
&& is_lane
) {
5113 * Check that the packet doesn't begin with an
5114 * LE Control marker. (We've already generated
5117 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5122 * Now check the MAC address.
5124 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5128 if (linktype
== DLT_IP_OVER_FC
)
5129 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5130 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5132 bpf_error("ethernet address used in non-ether expression");
5138 struct slist
*s0
, *s1
;
5141 * This is definitely not the best way to do this, but the
5142 * lists will rarely get long.
5149 static struct slist
*
5155 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5160 static struct slist
*
5166 s
= new_stmt(BPF_LD
|BPF_MEM
);
5172 * Modify "index" to use the value stored into its register as an
5173 * offset relative to the beginning of the header for the protocol
5174 * "proto", and allocate a register and put an item "size" bytes long
5175 * (1, 2, or 4) at that offset into that register, making it the register
5179 gen_load(proto
, index
, size
)
5184 struct slist
*s
, *tmp
;
5186 int regno
= alloc_reg();
5188 free_reg(index
->regno
);
5192 bpf_error("data size must be 1, 2, or 4");
5208 bpf_error("unsupported index operation");
5212 * The offset is relative to the beginning of the packet
5213 * data, if we have a radio header. (If we don't, this
5216 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5217 linktype
!= DLT_IEEE802_11_RADIO
&&
5218 linktype
!= DLT_PRISM_HEADER
)
5219 bpf_error("radio information not present in capture");
5222 * Load into the X register the offset computed into the
5223 * register specifed by "index".
5225 s
= xfer_to_x(index
);
5228 * Load the item at that offset.
5230 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5232 sappend(index
->s
, s
);
5237 * The offset is relative to the beginning of
5238 * the link-layer header.
5240 * XXX - what about ATM LANE? Should the index be
5241 * relative to the beginning of the AAL5 frame, so
5242 * that 0 refers to the beginning of the LE Control
5243 * field, or relative to the beginning of the LAN
5244 * frame, so that 0 refers, for Ethernet LANE, to
5245 * the beginning of the destination address?
5247 s
= gen_llprefixlen();
5250 * If "s" is non-null, it has code to arrange that the
5251 * X register contains the length of the prefix preceding
5252 * the link-layer header. Add to it the offset computed
5253 * into the register specified by "index", and move that
5254 * into the X register. Otherwise, just load into the X
5255 * register the offset computed into the register specifed
5259 sappend(s
, xfer_to_a(index
));
5260 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5261 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5263 s
= xfer_to_x(index
);
5266 * Load the item at the sum of the offset we've put in the
5267 * X register and the offset of the start of the link
5268 * layer header (which is 0 if the radio header is
5269 * variable-length; that header length is what we put
5270 * into the X register and then added to the index).
5272 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5275 sappend(index
->s
, s
);
5291 * The offset is relative to the beginning of
5292 * the network-layer header.
5293 * XXX - are there any cases where we want
5296 s
= gen_llprefixlen();
5299 * If "s" is non-null, it has code to arrange that the
5300 * X register contains the length of the prefix preceding
5301 * the link-layer header. Add to it the offset computed
5302 * into the register specified by "index", and move that
5303 * into the X register. Otherwise, just load into the X
5304 * register the offset computed into the register specifed
5308 sappend(s
, xfer_to_a(index
));
5309 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5310 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5312 s
= xfer_to_x(index
);
5315 * Load the item at the sum of the offset we've put in the
5316 * X register and the offset of the start of the network
5319 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5322 sappend(index
->s
, s
);
5325 * Do the computation only if the packet contains
5326 * the protocol in question.
5328 b
= gen_proto_abbrev(proto
);
5330 gen_and(index
->b
, b
);
5343 * The offset is relative to the beginning of
5344 * the transport-layer header.
5345 * XXX - are there any cases where we want
5347 * XXX - we should, if we're built with
5348 * IPv6 support, generate code to load either
5349 * IPv4, IPv6, or both, as appropriate.
5351 s
= gen_loadx_iphdrlen();
5354 * The X register now contains the sum of the offset
5355 * of the beginning of the link-layer header and
5356 * the length of the network-layer header. Load
5357 * into the A register the offset relative to
5358 * the beginning of the transport layer header,
5359 * add the X register to that, move that to the
5360 * X register, and load with an offset from the
5361 * X register equal to the offset of the network
5362 * layer header relative to the beginning of
5363 * the link-layer header.
5365 sappend(s
, xfer_to_a(index
));
5366 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5367 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5368 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5370 sappend(index
->s
, s
);
5373 * Do the computation only if the packet contains
5374 * the protocol in question - which is true only
5375 * if this is an IP datagram and is the first or
5376 * only fragment of that datagram.
5378 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5380 gen_and(index
->b
, b
);
5382 gen_and(gen_proto_abbrev(Q_IP
), b
);
5388 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5392 index
->regno
= regno
;
5393 s
= new_stmt(BPF_ST
);
5395 sappend(index
->s
, s
);
5401 gen_relation(code
, a0
, a1
, reversed
)
5403 struct arth
*a0
, *a1
;
5406 struct slist
*s0
, *s1
, *s2
;
5407 struct block
*b
, *tmp
;
5411 if (code
== BPF_JEQ
) {
5412 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5413 b
= new_block(JMP(code
));
5417 b
= new_block(BPF_JMP
|code
|BPF_X
);
5423 sappend(a0
->s
, a1
->s
);
5427 free_reg(a0
->regno
);
5428 free_reg(a1
->regno
);
5430 /* 'and' together protocol checks */
5433 gen_and(a0
->b
, tmp
= a1
->b
);
5449 int regno
= alloc_reg();
5450 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5453 s
= new_stmt(BPF_LD
|BPF_LEN
);
5454 s
->next
= new_stmt(BPF_ST
);
5455 s
->next
->s
.k
= regno
;
5470 a
= (struct arth
*)newchunk(sizeof(*a
));
5474 s
= new_stmt(BPF_LD
|BPF_IMM
);
5476 s
->next
= new_stmt(BPF_ST
);
5492 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5495 s
= new_stmt(BPF_ST
);
5503 gen_arth(code
, a0
, a1
)
5505 struct arth
*a0
, *a1
;
5507 struct slist
*s0
, *s1
, *s2
;
5511 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5516 sappend(a0
->s
, a1
->s
);
5518 free_reg(a0
->regno
);
5519 free_reg(a1
->regno
);
5521 s0
= new_stmt(BPF_ST
);
5522 a0
->regno
= s0
->s
.k
= alloc_reg();
5529 * Here we handle simple allocation of the scratch registers.
5530 * If too many registers are alloc'd, the allocator punts.
5532 static int regused
[BPF_MEMWORDS
];
5536 * Return the next free register.
5541 int n
= BPF_MEMWORDS
;
5544 if (regused
[curreg
])
5545 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5547 regused
[curreg
] = 1;
5551 bpf_error("too many registers needed to evaluate expression");
5556 * Return a register to the table so it can
5566 static struct block
*
5573 s
= new_stmt(BPF_LD
|BPF_LEN
);
5574 b
= new_block(JMP(jmp
));
5585 return gen_len(BPF_JGE
, n
);
5589 * Actually, this is less than or equal.
5597 b
= gen_len(BPF_JGT
, n
);
5604 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5605 * the beginning of the link-layer header.
5606 * XXX - that means you can't test values in the radiotap header, but
5607 * as that header is difficult if not impossible to parse generally
5608 * without a loop, that might not be a severe problem. A new keyword
5609 * "radio" could be added for that, although what you'd really want
5610 * would be a way of testing particular radio header values, which
5611 * would generate code appropriate to the radio header in question.
5614 gen_byteop(op
, idx
, val
)
5625 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5628 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5632 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5636 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5640 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5644 b
= new_block(JMP(BPF_JEQ
));
5651 static u_char abroadcast
[] = { 0x0 };
5654 gen_broadcast(proto
)
5657 bpf_u_int32 hostmask
;
5658 struct block
*b0
, *b1
, *b2
;
5659 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5665 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5666 return gen_ahostop(abroadcast
, Q_DST
);
5667 if (linktype
== DLT_EN10MB
)
5668 return gen_ehostop(ebroadcast
, Q_DST
);
5669 if (linktype
== DLT_FDDI
)
5670 return gen_fhostop(ebroadcast
, Q_DST
);
5671 if (linktype
== DLT_IEEE802
)
5672 return gen_thostop(ebroadcast
, Q_DST
);
5673 if (linktype
== DLT_IEEE802_11
||
5674 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5675 linktype
== DLT_IEEE802_11_RADIO
||
5676 linktype
== DLT_PRISM_HEADER
)
5677 return gen_wlanhostop(ebroadcast
, Q_DST
);
5678 if (linktype
== DLT_IP_OVER_FC
)
5679 return gen_ipfchostop(ebroadcast
, Q_DST
);
5680 if (linktype
== DLT_SUNATM
&& is_lane
) {
5682 * Check that the packet doesn't begin with an
5683 * LE Control marker. (We've already generated
5686 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5691 * Now check the MAC address.
5693 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5697 bpf_error("not a broadcast link");
5701 b0
= gen_linktype(ETHERTYPE_IP
);
5702 hostmask
= ~netmask
;
5703 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5704 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5705 (bpf_int32
)(~0 & hostmask
), hostmask
);
5710 bpf_error("only link-layer/IP broadcast filters supported");
5715 * Generate code to test the low-order bit of a MAC address (that's
5716 * the bottom bit of the *first* byte).
5718 static struct block
*
5719 gen_mac_multicast(offset
)
5722 register struct block
*b0
;
5723 register struct slist
*s
;
5725 /* link[offset] & 1 != 0 */
5726 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5727 b0
= new_block(JMP(BPF_JSET
));
5734 gen_multicast(proto
)
5737 register struct block
*b0
, *b1
, *b2
;
5738 register struct slist
*s
;
5744 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5745 /* all ARCnet multicasts use the same address */
5746 return gen_ahostop(abroadcast
, Q_DST
);
5748 if (linktype
== DLT_EN10MB
) {
5749 /* ether[0] & 1 != 0 */
5750 return gen_mac_multicast(0);
5753 if (linktype
== DLT_FDDI
) {
5755 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5757 * XXX - was that referring to bit-order issues?
5759 /* fddi[1] & 1 != 0 */
5760 return gen_mac_multicast(1);
5763 if (linktype
== DLT_IEEE802
) {
5764 /* tr[2] & 1 != 0 */
5765 return gen_mac_multicast(2);
5768 if (linktype
== DLT_IEEE802_11
||
5769 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5770 linktype
== DLT_IEEE802_11_RADIO
||
5771 linktype
== DLT_PRISM_HEADER
) {
5775 * For control frames, there is no DA.
5777 * For management frames, DA is at an
5778 * offset of 4 from the beginning of
5781 * For data frames, DA is at an offset
5782 * of 4 from the beginning of the packet
5783 * if To DS is clear and at an offset of
5784 * 16 from the beginning of the packet
5789 * Generate the tests to be done for data frames.
5791 * First, check for To DS set, i.e. "link[1] & 0x01".
5793 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5794 b1
= new_block(JMP(BPF_JSET
));
5795 b1
->s
.k
= 0x01; /* To DS */
5799 * If To DS is set, the DA is at 16.
5801 b0
= gen_mac_multicast(16);
5805 * Now, check for To DS not set, i.e. check
5806 * "!(link[1] & 0x01)".
5808 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5809 b2
= new_block(JMP(BPF_JSET
));
5810 b2
->s
.k
= 0x01; /* To DS */
5815 * If To DS is not set, the DA is at 4.
5817 b1
= gen_mac_multicast(4);
5821 * Now OR together the last two checks. That gives
5822 * the complete set of checks for data frames.
5827 * Now check for a data frame.
5828 * I.e, check "link[0] & 0x08".
5830 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5831 b1
= new_block(JMP(BPF_JSET
));
5836 * AND that with the checks done for data frames.
5841 * If the high-order bit of the type value is 0, this
5842 * is a management frame.
5843 * I.e, check "!(link[0] & 0x08)".
5845 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5846 b2
= new_block(JMP(BPF_JSET
));
5852 * For management frames, the DA is at 4.
5854 b1
= gen_mac_multicast(4);
5858 * OR that with the checks done for data frames.
5859 * That gives the checks done for management and
5865 * If the low-order bit of the type value is 1,
5866 * this is either a control frame or a frame
5867 * with a reserved type, and thus not a
5870 * I.e., check "!(link[0] & 0x04)".
5872 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5873 b1
= new_block(JMP(BPF_JSET
));
5879 * AND that with the checks for data and management
5886 if (linktype
== DLT_IP_OVER_FC
) {
5887 b0
= gen_mac_multicast(2);
5891 if (linktype
== DLT_SUNATM
&& is_lane
) {
5893 * Check that the packet doesn't begin with an
5894 * LE Control marker. (We've already generated
5897 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5901 /* ether[off_mac] & 1 != 0 */
5902 b0
= gen_mac_multicast(off_mac
);
5907 /* Link not known to support multicasts */
5911 b0
= gen_linktype(ETHERTYPE_IP
);
5912 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5918 b0
= gen_linktype(ETHERTYPE_IPV6
);
5919 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5924 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5929 * generate command for inbound/outbound. It's here so we can
5930 * make it link-type specific. 'dir' = 0 implies "inbound",
5931 * = 1 implies "outbound".
5937 register struct block
*b0
;
5940 * Only some data link types support inbound/outbound qualifiers.
5944 b0
= gen_relation(BPF_JEQ
,
5945 gen_load(Q_LINK
, gen_loadi(0), 1),
5953 * Match packets sent by this machine.
5955 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
5958 * Match packets sent to this machine.
5959 * (No broadcast or multicast packets, or
5960 * packets sent to some other machine and
5961 * received promiscuously.)
5963 * XXX - packets sent to other machines probably
5964 * shouldn't be matched, but what about broadcast
5965 * or multicast packets we received?
5967 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
5972 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
5973 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5978 /* match outgoing packets */
5979 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
5981 /* match incoming packets */
5982 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
5986 case DLT_JUNIPER_MLFR
:
5987 case DLT_JUNIPER_MLPPP
:
5988 case DLT_JUNIPER_ATM1
:
5989 case DLT_JUNIPER_ATM2
:
5990 case DLT_JUNIPER_PPPOE
:
5991 case DLT_JUNIPER_PPPOE_ATM
:
5992 case DLT_JUNIPER_GGSN
:
5993 case DLT_JUNIPER_ES
:
5994 case DLT_JUNIPER_MONITOR
:
5995 case DLT_JUNIPER_SERVICES
:
5996 /* juniper flags (including direction) are stored
5997 * the byte after the 3-byte magic number */
5999 /* match outgoing packets */
6000 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6002 /* match incoming packets */
6003 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6008 bpf_error("inbound/outbound not supported on linktype %d",
6016 /* PF firewall log matched interface */
6018 gen_pf_ifname(const char *ifname
)
6023 if (linktype
== DLT_PFLOG
) {
6024 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6025 off
= offsetof(struct pfloghdr
, ifname
);
6027 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6030 if (strlen(ifname
) >= len
) {
6031 bpf_error("ifname interface names can only be %d characters",
6035 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6039 /* PF firewall log matched interface */
6041 gen_pf_ruleset(char *ruleset
)
6045 if (linktype
!= DLT_PFLOG
) {
6046 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6049 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6050 bpf_error("ruleset names can only be %ld characters",
6051 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6054 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6055 strlen(ruleset
), (const u_char
*)ruleset
);
6059 /* PF firewall log rule number */
6065 if (linktype
== DLT_PFLOG
) {
6066 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6069 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6076 /* PF firewall log sub-rule number */
6078 gen_pf_srnr(int srnr
)
6082 if (linktype
!= DLT_PFLOG
) {
6083 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6087 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6092 /* PF firewall log reason code */
6094 gen_pf_reason(int reason
)
6098 if (linktype
== DLT_PFLOG
) {
6099 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6102 bpf_error("reason not supported on linktype 0x%x", linktype
);
6109 /* PF firewall log action */
6111 gen_pf_action(int action
)
6115 if (linktype
== DLT_PFLOG
) {
6116 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6119 bpf_error("action not supported on linktype 0x%x", linktype
);
6128 register const u_char
*eaddr
;
6131 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6132 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6133 return gen_ahostop(eaddr
, (int)q
.dir
);
6135 bpf_error("ARCnet address used in non-arc expression");
6139 static struct block
*
6140 gen_ahostop(eaddr
, dir
)
6141 register const u_char
*eaddr
;
6144 register struct block
*b0
, *b1
;
6147 /* src comes first, different from Ethernet */
6149 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6152 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6155 b0
= gen_ahostop(eaddr
, Q_SRC
);
6156 b1
= gen_ahostop(eaddr
, Q_DST
);
6162 b0
= gen_ahostop(eaddr
, Q_SRC
);
6163 b1
= gen_ahostop(eaddr
, Q_DST
);
6172 * support IEEE 802.1Q VLAN trunk over ethernet
6181 * Change the offsets to point to the type and data fields within
6182 * the VLAN packet. Just increment the offsets, so that we
6183 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6184 * capture VLAN 200 encapsulated within VLAN 100.
6186 * XXX - this is a bit of a kludge. If we were to split the
6187 * compiler into a parser that parses an expression and
6188 * generates an expression tree, and a code generator that
6189 * takes an expression tree (which could come from our
6190 * parser or from some other parser) and generates BPF code,
6191 * we could perhaps make the offsets parameters of routines
6192 * and, in the handler for an "AND" node, pass to subnodes
6193 * other than the VLAN node the adjusted offsets.
6195 * This would mean that "vlan" would, instead of changing the
6196 * behavior of *all* tests after it, change only the behavior
6197 * of tests ANDed with it. That would change the documented
6198 * semantics of "vlan", which might break some expressions.
6199 * However, it would mean that "(vlan and ip) or ip" would check
6200 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6201 * checking only for VLAN-encapsulated IP, so that could still
6202 * be considered worth doing; it wouldn't break expressions
6203 * that are of the form "vlan and ..." or "vlan N and ...",
6204 * which I suspect are the most common expressions involving
6205 * "vlan". "vlan or ..." doesn't necessarily do what the user
6206 * would really want, now, as all the "or ..." tests would
6207 * be done assuming a VLAN, even though the "or" could be viewed
6208 * as meaning "or, if this isn't a VLAN packet...".
6210 orig_linktype
= off_linktype
; /* save original values */
6222 bpf_error("no VLAN support for data link type %d",
6227 /* check for VLAN */
6228 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6230 /* If a specific VLAN is requested, check VLAN id */
6231 if (vlan_num
>= 0) {
6234 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6253 * Change the offsets to point to the type and data fields within
6254 * the MPLS packet. Just increment the offsets, so that we
6255 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6256 * capture packets with an outer label of 100000 and an inner
6259 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6261 orig_linktype
= off_linktype
; /* save original values */
6266 case DLT_C_HDLC
: /* fall through */
6271 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6272 (bpf_int32
)ETHERTYPE_MPLS
);
6279 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6280 (bpf_int32
)PPP_MPLS_UCAST
);
6283 /* FIXME add other DLT_s ...
6284 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6285 * leave it for now */
6288 bpf_error("no MPLS support for data link type %d",
6295 /* If a specific MPLS label is requested, check it */
6296 if (label_num
>= 0) {
6299 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6300 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6301 0xfffff000); /* only compare the first 20 bits */
6310 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6322 bpf_error("'vpi' supported only on raw ATM");
6323 if (off_vpi
== (u_int
)-1)
6325 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6331 bpf_error("'vci' supported only on raw ATM");
6332 if (off_vci
== (u_int
)-1)
6334 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6339 if (off_proto
== (u_int
)-1)
6340 abort(); /* XXX - this isn't on FreeBSD */
6341 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6346 if (off_payload
== (u_int
)-1)
6348 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6349 0xffffffff, jtype
, reverse
, jvalue
);
6354 bpf_error("'callref' supported only on raw ATM");
6355 if (off_proto
== (u_int
)-1)
6357 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6358 jtype
, reverse
, jvalue
);
6368 gen_atmtype_abbrev(type
)
6371 struct block
*b0
, *b1
;
6376 /* Get all packets in Meta signalling Circuit */
6378 bpf_error("'metac' supported only on raw ATM");
6379 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6380 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6385 /* Get all packets in Broadcast Circuit*/
6387 bpf_error("'bcc' supported only on raw ATM");
6388 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6389 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6394 /* Get all cells in Segment OAM F4 circuit*/
6396 bpf_error("'oam4sc' supported only on raw ATM");
6397 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6398 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6403 /* Get all cells in End-to-End OAM F4 Circuit*/
6405 bpf_error("'oam4ec' supported only on raw ATM");
6406 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6407 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6412 /* Get all packets in connection Signalling Circuit */
6414 bpf_error("'sc' supported only on raw ATM");
6415 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6416 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6421 /* Get all packets in ILMI Circuit */
6423 bpf_error("'ilmic' supported only on raw ATM");
6424 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6425 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6430 /* Get all LANE packets */
6432 bpf_error("'lane' supported only on raw ATM");
6433 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6436 * Arrange that all subsequent tests assume LANE
6437 * rather than LLC-encapsulated packets, and set
6438 * the offsets appropriately for LANE-encapsulated
6441 * "off_mac" is the offset of the Ethernet header,
6442 * which is 2 bytes past the ATM pseudo-header
6443 * (skipping the pseudo-header and 2-byte LE Client
6444 * field). The other offsets are Ethernet offsets
6445 * relative to "off_mac".
6448 off_mac
= off_payload
+ 2; /* MAC header */
6449 off_linktype
= off_mac
+ 12;
6450 off_nl
= off_mac
+ 14; /* Ethernet II */
6451 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6455 /* Get all LLC-encapsulated packets */
6457 bpf_error("'llc' supported only on raw ATM");
6458 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6469 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6476 bpf_u_int32 val1
, val2
, val3
;
6478 switch (mtp3field
) {
6481 if (off_sio
== (u_int
)-1)
6482 bpf_error("'sio' supported only on SS7");
6483 /* sio coded on 1 byte so max value 255 */
6485 bpf_error("sio value %u too big; max value = 255",
6487 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6488 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6492 if (off_opc
== (u_int
)-1)
6493 bpf_error("'opc' supported only on SS7");
6494 /* opc coded on 14 bits so max value 16383 */
6496 bpf_error("opc value %u too big; max value = 16383",
6498 /* the following instructions are made to convert jvalue
6499 * to the form used to write opc in an ss7 message*/
6500 val1
= jvalue
& 0x00003c00;
6502 val2
= jvalue
& 0x000003fc;
6504 val3
= jvalue
& 0x00000003;
6506 jvalue
= val1
+ val2
+ val3
;
6507 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6508 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6512 if (off_dpc
== (u_int
)-1)
6513 bpf_error("'dpc' supported only on SS7");
6514 /* dpc coded on 14 bits so max value 16383 */
6516 bpf_error("dpc value %u too big; max value = 16383",
6518 /* the following instructions are made to convert jvalue
6519 * to the forme used to write dpc in an ss7 message*/
6520 val1
= jvalue
& 0x000000ff;
6522 val2
= jvalue
& 0x00003f00;
6524 jvalue
= val1
+ val2
;
6525 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6526 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6530 if (off_sls
== (u_int
)-1)
6531 bpf_error("'sls' supported only on SS7");
6532 /* sls coded on 4 bits so max value 15 */
6534 bpf_error("sls value %u too big; max value = 15",
6536 /* the following instruction is made to convert jvalue
6537 * to the forme used to write sls in an ss7 message*/
6538 jvalue
= jvalue
<< 4;
6539 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6540 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6549 static struct block
*
6550 gen_msg_abbrev(type
)
6556 * Q.2931 signalling protocol messages for handling virtual circuits
6557 * establishment and teardown
6562 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6566 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6570 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6574 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6578 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6581 case A_RELEASE_DONE
:
6582 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6592 gen_atmmulti_abbrev(type
)
6595 struct block
*b0
, *b1
;
6601 bpf_error("'oam' supported only on raw ATM");
6602 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6607 bpf_error("'oamf4' supported only on raw ATM");
6609 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6610 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6612 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6618 * Get Q.2931 signalling messages for switched
6619 * virtual connection
6622 bpf_error("'connectmsg' supported only on raw ATM");
6623 b0
= gen_msg_abbrev(A_SETUP
);
6624 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6626 b0
= gen_msg_abbrev(A_CONNECT
);
6628 b0
= gen_msg_abbrev(A_CONNECTACK
);
6630 b0
= gen_msg_abbrev(A_RELEASE
);
6632 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6634 b0
= gen_atmtype_abbrev(A_SC
);
6640 bpf_error("'metaconnect' supported only on raw ATM");
6641 b0
= gen_msg_abbrev(A_SETUP
);
6642 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6644 b0
= gen_msg_abbrev(A_CONNECT
);
6646 b0
= gen_msg_abbrev(A_RELEASE
);
6648 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6650 b0
= gen_atmtype_abbrev(A_METAC
);