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.290.2.4 2007-11-04 22:29:35 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"
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
83 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #include <netdb.h> /* for "struct addrinfo" */
90 #include <pcap/namedb.h>
95 #define IPPROTO_SCTP 132
98 #ifdef HAVE_OS_PROTO_H
102 #define JMP(c) ((c)|BPF_JMP|BPF_K)
105 static jmp_buf top_ctx
;
106 static pcap_t
*bpf_pcap
;
108 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
110 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
112 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
117 static int pcap_fddipad
;
122 bpf_error(const char *fmt
, ...)
127 if (bpf_pcap
!= NULL
)
128 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
135 static void init_linktype(pcap_t
*);
137 static void init_regs(void);
138 static int alloc_reg(void);
139 static void free_reg(int);
141 static struct block
*root
;
144 * Value passed to gen_load_a() to indicate what the offset argument
148 OR_PACKET
, /* relative to the beginning of the packet */
149 OR_LINK
, /* relative to the link-layer header */
150 OR_NET
, /* relative to the network-layer header */
151 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
152 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
153 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
157 * We divy out chunks of memory rather than call malloc each time so
158 * we don't have to worry about leaking memory. It's probably
159 * not a big deal if all this memory was wasted but if this ever
160 * goes into a library that would probably not be a good idea.
162 * XXX - this *is* in a library....
165 #define CHUNK0SIZE 1024
171 static struct chunk chunks
[NCHUNKS
];
172 static int cur_chunk
;
174 static void *newchunk(u_int
);
175 static void freechunks(void);
176 static inline struct block
*new_block(int);
177 static inline struct slist
*new_stmt(int);
178 static struct block
*gen_retblk(int);
179 static inline void syntax(void);
181 static void backpatch(struct block
*, struct block
*);
182 static void merge(struct block
*, struct block
*);
183 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
184 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
185 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
186 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
187 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
188 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
190 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
191 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
192 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
193 static struct slist
*gen_load_llrel(u_int
, u_int
);
194 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
195 static struct slist
*gen_loadx_iphdrlen(void);
196 static struct block
*gen_uncond(int);
197 static inline struct block
*gen_true(void);
198 static inline struct block
*gen_false(void);
199 static struct block
*gen_ether_linktype(int);
200 static struct block
*gen_linux_sll_linktype(int);
201 static void insert_radiotap_load_llprefixlen(struct block
*);
202 static void insert_ppi_load_llprefixlen(struct block
*);
203 static void insert_load_llprefixlen(struct block
*);
204 static struct slist
*gen_llprefixlen(void);
205 static struct block
*gen_linktype(int);
206 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
207 static struct block
*gen_llc_linktype(int);
208 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
210 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
212 static struct block
*gen_ahostop(const u_char
*, int);
213 static struct block
*gen_ehostop(const u_char
*, int);
214 static struct block
*gen_fhostop(const u_char
*, int);
215 static struct block
*gen_thostop(const u_char
*, int);
216 static struct block
*gen_wlanhostop(const u_char
*, int);
217 static struct block
*gen_ipfchostop(const u_char
*, int);
218 static struct block
*gen_dnhostop(bpf_u_int32
, int);
219 static struct block
*gen_mpls_linktype(int);
220 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
222 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
225 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
227 static struct block
*gen_ipfrag(void);
228 static struct block
*gen_portatom(int, bpf_int32
);
229 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
231 static struct block
*gen_portatom6(int, bpf_int32
);
232 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
234 struct block
*gen_portop(int, int, int);
235 static struct block
*gen_port(int, int, int);
236 struct block
*gen_portrangeop(int, int, int, int);
237 static struct block
*gen_portrange(int, int, int, int);
239 struct block
*gen_portop6(int, int, int);
240 static struct block
*gen_port6(int, int, int);
241 struct block
*gen_portrangeop6(int, int, int, int);
242 static struct block
*gen_portrange6(int, int, int, int);
244 static int lookup_proto(const char *, int);
245 static struct block
*gen_protochain(int, int, int);
246 static struct block
*gen_proto(int, int, int);
247 static struct slist
*xfer_to_x(struct arth
*);
248 static struct slist
*xfer_to_a(struct arth
*);
249 static struct block
*gen_mac_multicast(int);
250 static struct block
*gen_len(int, int);
252 static struct block
*gen_ppi_dlt_check(void);
253 static struct block
*gen_msg_abbrev(int type
);
264 /* XXX Round up to nearest long. */
265 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
267 /* XXX Round up to structure boundary. */
271 cp
= &chunks
[cur_chunk
];
272 if (n
> cp
->n_left
) {
273 ++cp
, k
= ++cur_chunk
;
275 bpf_error("out of memory");
276 size
= CHUNK0SIZE
<< k
;
277 cp
->m
= (void *)malloc(size
);
279 bpf_error("out of memory");
280 memset((char *)cp
->m
, 0, size
);
283 bpf_error("out of memory");
286 return (void *)((char *)cp
->m
+ cp
->n_left
);
295 for (i
= 0; i
< NCHUNKS
; ++i
)
296 if (chunks
[i
].m
!= NULL
) {
303 * A strdup whose allocations are freed after code generation is over.
307 register const char *s
;
309 int n
= strlen(s
) + 1;
310 char *cp
= newchunk(n
);
316 static inline struct block
*
322 p
= (struct block
*)newchunk(sizeof(*p
));
329 static inline struct slist
*
335 p
= (struct slist
*)newchunk(sizeof(*p
));
341 static struct block
*
345 struct block
*b
= new_block(BPF_RET
|BPF_K
);
354 bpf_error("syntax error in filter expression");
357 static bpf_u_int32 netmask
;
362 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
363 const char *buf
, int optimize
, bpf_u_int32 mask
)
366 const char * volatile xbuf
= buf
;
374 if (setjmp(top_ctx
)) {
382 snaplen
= pcap_snapshot(p
);
384 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
385 "snaplen of 0 rejects all packets");
389 lex_init(xbuf
? xbuf
: "");
397 root
= gen_retblk(snaplen
);
399 if (optimize
&& !no_optimize
) {
402 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
403 bpf_error("expression rejects all packets");
405 program
->bf_insns
= icode_to_fcode(root
, &len
);
406 program
->bf_len
= len
;
414 * entry point for using the compiler with no pcap open
415 * pass in all the stuff that is needed explicitly instead.
418 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
419 struct bpf_program
*program
,
420 const char *buf
, int optimize
, bpf_u_int32 mask
)
425 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
428 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
434 * Clean up a "struct bpf_program" by freeing all the memory allocated
438 pcap_freecode(struct bpf_program
*program
)
441 if (program
->bf_insns
!= NULL
) {
442 free((char *)program
->bf_insns
);
443 program
->bf_insns
= NULL
;
448 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
449 * which of the jt and jf fields has been resolved and which is a pointer
450 * back to another unresolved block (or nil). At least one of the fields
451 * in each block is already resolved.
454 backpatch(list
, target
)
455 struct block
*list
, *target
;
472 * Merge the lists in b0 and b1, using the 'sense' field to indicate
473 * which of jt and jf is the link.
477 struct block
*b0
, *b1
;
479 register struct block
**p
= &b0
;
481 /* Find end of list. */
483 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
485 /* Concatenate the lists. */
493 struct block
*ppi_dlt_check
;
495 ppi_dlt_check
= gen_ppi_dlt_check();
497 if (ppi_dlt_check
!= NULL
)
499 gen_and(ppi_dlt_check
, p
);
502 backpatch(p
, gen_retblk(snaplen
));
503 p
->sense
= !p
->sense
;
504 backpatch(p
, gen_retblk(0));
508 * Insert before the statements of the first (root) block any
509 * statements needed to load the lengths of any variable-length
510 * headers into registers.
512 * XXX - a fancier strategy would be to insert those before the
513 * statements of all blocks that use those lengths and that
514 * have no predecessors that use them, so that we only compute
515 * the lengths if we need them. There might be even better
516 * approaches than that. However, as we're currently only
517 * handling variable-length radiotap headers, and as all
518 * filtering expressions other than raw link[M:N] tests
519 * require the length of that header, doing more for that
520 * header length isn't really worth the effort.
522 insert_load_llprefixlen(root
);
527 struct block
*b0
, *b1
;
529 backpatch(b0
, b1
->head
);
530 b0
->sense
= !b0
->sense
;
531 b1
->sense
= !b1
->sense
;
533 b1
->sense
= !b1
->sense
;
539 struct block
*b0
, *b1
;
541 b0
->sense
= !b0
->sense
;
542 backpatch(b0
, b1
->head
);
543 b0
->sense
= !b0
->sense
;
552 b
->sense
= !b
->sense
;
555 static struct block
*
556 gen_cmp(offrel
, offset
, size
, v
)
557 enum e_offrel offrel
;
561 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
564 static struct block
*
565 gen_cmp_gt(offrel
, offset
, size
, v
)
566 enum e_offrel offrel
;
570 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
573 static struct block
*
574 gen_cmp_ge(offrel
, offset
, size
, v
)
575 enum e_offrel offrel
;
579 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
582 static struct block
*
583 gen_cmp_lt(offrel
, offset
, size
, v
)
584 enum e_offrel offrel
;
588 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
591 static struct block
*
592 gen_cmp_le(offrel
, offset
, size
, v
)
593 enum e_offrel offrel
;
597 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
600 static struct block
*
601 gen_mcmp(offrel
, offset
, size
, v
, mask
)
602 enum e_offrel offrel
;
607 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
610 static struct block
*
611 gen_bcmp(offrel
, offset
, size
, v
)
612 enum e_offrel offrel
;
613 register u_int offset
, size
;
614 register const u_char
*v
;
616 register struct block
*b
, *tmp
;
620 register const u_char
*p
= &v
[size
- 4];
621 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
622 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
624 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
631 register const u_char
*p
= &v
[size
- 2];
632 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
634 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
641 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
650 * AND the field of size "size" at offset "offset" relative to the header
651 * specified by "offrel" with "mask", and compare it with the value "v"
652 * with the test specified by "jtype"; if "reverse" is true, the test
653 * should test the opposite of "jtype".
655 static struct block
*
656 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
657 enum e_offrel offrel
;
659 bpf_u_int32 offset
, size
, mask
, jtype
;
662 struct slist
*s
, *s2
;
665 s
= gen_load_a(offrel
, offset
, size
);
667 if (mask
!= 0xffffffff) {
668 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
673 b
= new_block(JMP(jtype
));
676 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
682 * Various code constructs need to know the layout of the data link
683 * layer. These variables give the necessary offsets from the beginning
684 * of the packet data.
686 * If the link layer has variable_length headers, the offsets are offsets
687 * from the end of the link-link-layer header, and "reg_ll_size" is
688 * the register number for a register containing the length of the
689 * link-layer header. Otherwise, "reg_ll_size" is -1.
691 static int reg_ll_size
;
694 * This is the offset of the beginning of the link-layer header from
695 * the beginning of the raw packet data.
697 * It's usually 0, except for 802.11 with a fixed-length radio header.
698 * (For 802.11 with a variable-length radio header, we have to generate
699 * code to compute that offset; off_ll is 0 in that case.)
704 * This is the offset of the beginning of the MAC-layer header.
705 * It's usually 0, except for ATM LANE, where it's the offset, relative
706 * to the beginning of the raw packet data, of the Ethernet header.
708 static u_int off_mac
;
711 * "off_linktype" is the offset to information in the link-layer header
712 * giving the packet type. This offset is relative to the beginning
713 * of the link-layer header (i.e., it doesn't include off_ll).
715 * For Ethernet, it's the offset of the Ethernet type field.
717 * For link-layer types that always use 802.2 headers, it's the
718 * offset of the LLC header.
720 * For PPP, it's the offset of the PPP type field.
722 * For Cisco HDLC, it's the offset of the CHDLC type field.
724 * For BSD loopback, it's the offset of the AF_ value.
726 * For Linux cooked sockets, it's the offset of the type field.
728 * It's set to -1 for no encapsulation, in which case, IP is assumed.
730 static u_int off_linktype
;
733 * TRUE if the link layer includes an ATM pseudo-header.
735 static int is_atm
= 0;
738 * TRUE if "lane" appeared in the filter; it causes us to generate
739 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
741 static int is_lane
= 0;
744 * These are offsets for the ATM pseudo-header.
746 static u_int off_vpi
;
747 static u_int off_vci
;
748 static u_int off_proto
;
751 * These are offsets for the MTP2 fields.
756 * These are offsets for the MTP3 fields.
758 static u_int off_sio
;
759 static u_int off_opc
;
760 static u_int off_dpc
;
761 static u_int off_sls
;
764 * This is the offset of the first byte after the ATM pseudo_header,
765 * or -1 if there is no ATM pseudo-header.
767 static u_int off_payload
;
770 * These are offsets to the beginning of the network-layer header.
771 * They are relative to the beginning of the link-layer header (i.e.,
772 * they don't include off_ll).
774 * If the link layer never uses 802.2 LLC:
776 * "off_nl" and "off_nl_nosnap" are the same.
778 * If the link layer always uses 802.2 LLC:
780 * "off_nl" is the offset if there's a SNAP header following
783 * "off_nl_nosnap" is the offset if there's no SNAP header.
785 * If the link layer is Ethernet:
787 * "off_nl" is the offset if the packet is an Ethernet II packet
788 * (we assume no 802.3+802.2+SNAP);
790 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
791 * with an 802.2 header following it.
794 static u_int off_nl_nosnap
;
802 linktype
= pcap_datalink(p
);
804 pcap_fddipad
= p
->fddipad
;
808 * Assume it's not raw ATM with a pseudo-header, for now.
819 * And assume we're not doing SS7.
828 * Also assume it's not 802.11 with a fixed-length radio header.
834 label_stack_depth
= 0;
842 off_nl
= 6; /* XXX in reality, variable! */
843 off_nl_nosnap
= 6; /* no 802.2 LLC */
846 case DLT_ARCNET_LINUX
:
848 off_nl
= 8; /* XXX in reality, variable! */
849 off_nl_nosnap
= 8; /* no 802.2 LLC */
854 off_nl
= 14; /* Ethernet II */
855 off_nl_nosnap
= 17; /* 802.3+802.2 */
860 * SLIP doesn't have a link level type. The 16 byte
861 * header is hacked into our SLIP driver.
865 off_nl_nosnap
= 16; /* no 802.2 LLC */
869 /* XXX this may be the same as the DLT_PPP_BSDOS case */
873 off_nl_nosnap
= 24; /* no 802.2 LLC */
880 off_nl_nosnap
= 4; /* no 802.2 LLC */
886 off_nl_nosnap
= 12; /* no 802.2 LLC */
891 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
892 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
895 off_nl_nosnap
= 4; /* no 802.2 LLC */
900 * This does no include the Ethernet header, and
901 * only covers session state.
905 off_nl_nosnap
= 8; /* no 802.2 LLC */
911 off_nl_nosnap
= 24; /* no 802.2 LLC */
916 * FDDI doesn't really have a link-level type field.
917 * We set "off_linktype" to the offset of the LLC header.
919 * To check for Ethernet types, we assume that SSAP = SNAP
920 * is being used and pick out the encapsulated Ethernet type.
921 * XXX - should we generate code to check for SNAP?
925 off_linktype
+= pcap_fddipad
;
927 off_nl
= 21; /* FDDI+802.2+SNAP */
928 off_nl_nosnap
= 16; /* FDDI+802.2 */
930 off_nl
+= pcap_fddipad
;
931 off_nl_nosnap
+= pcap_fddipad
;
937 * Token Ring doesn't really have a link-level type field.
938 * We set "off_linktype" to the offset of the LLC header.
940 * To check for Ethernet types, we assume that SSAP = SNAP
941 * is being used and pick out the encapsulated Ethernet type.
942 * XXX - should we generate code to check for SNAP?
944 * XXX - the header is actually variable-length.
945 * Some various Linux patched versions gave 38
946 * as "off_linktype" and 40 as "off_nl"; however,
947 * if a token ring packet has *no* routing
948 * information, i.e. is not source-routed, the correct
949 * values are 20 and 22, as they are in the vanilla code.
951 * A packet is source-routed iff the uppermost bit
952 * of the first byte of the source address, at an
953 * offset of 8, has the uppermost bit set. If the
954 * packet is source-routed, the total number of bytes
955 * of routing information is 2 plus bits 0x1F00 of
956 * the 16-bit value at an offset of 14 (shifted right
957 * 8 - figure out which byte that is).
960 off_nl
= 22; /* Token Ring+802.2+SNAP */
961 off_nl_nosnap
= 17; /* Token Ring+802.2 */
966 * 802.11 doesn't really have a link-level type field.
967 * We set "off_linktype" to the offset of the LLC header.
969 * To check for Ethernet types, we assume that SSAP = SNAP
970 * is being used and pick out the encapsulated Ethernet type.
971 * XXX - should we generate code to check for SNAP?
973 * XXX - the header is actually variable-length. We
974 * assume a 24-byte link-layer header, as appears in
975 * data frames in networks with no bridges. If the
976 * fromds and tods 802.11 header bits are both set,
977 * it's actually supposed to be 30 bytes.
980 off_nl
= 32; /* 802.11+802.2+SNAP */
981 off_nl_nosnap
= 27; /* 802.11+802.2 */
984 case DLT_PRISM_HEADER
:
986 * Same as 802.11, but with an additional header before
987 * the 802.11 header, containing a bunch of additional
988 * information including radio-level information.
990 * The header is 144 bytes long.
992 * XXX - same variable-length header problem; at least
993 * the Prism header is fixed-length.
997 off_nl
= 32; /* Prism+802.11+802.2+SNAP */
998 off_nl_nosnap
= 27; /* Prism+802.11+802.2 */
1001 case DLT_IEEE802_11_RADIO_AVS
:
1003 * Same as 802.11, but with an additional header before
1004 * the 802.11 header, containing a bunch of additional
1005 * information including radio-level information.
1007 * The header is 64 bytes long, at least in its
1008 * current incarnation.
1010 * XXX - same variable-length header problem, only
1011 * more so; this header is also variable-length,
1012 * with the length being the 32-bit big-endian
1013 * number at an offset of 4 from the beginning
1014 * of the radio header. We should handle that the
1015 * same way we handle the length at the beginning
1016 * of the radiotap header.
1018 * XXX - in Linux, do any drivers that supply an AVS
1019 * header supply a link-layer type other than
1020 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1021 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1022 * any drivers that supply an AVS header but supply
1023 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1024 * have to check the header in the generated code to
1025 * determine whether it's Prism or AVS.
1029 off_nl
= 32; /* Radio+802.11+802.2+SNAP */
1030 off_nl_nosnap
= 27; /* Radio+802.11+802.2 */
1035 * At the moment we treat PPI as normal Radiotap encoded
1036 * packets. The difference is in the function that generates
1037 * the code at the beginning to compute the header length.
1038 * Since this code generator of PPI supports bare 802.11
1039 * encapsulation only (i.e. the encapsulated DLT should be
1040 * DLT_IEEE802_11) we generate code to check for this too.
1043 case DLT_IEEE802_11_RADIO
:
1045 * Same as 802.11, but with an additional header before
1046 * the 802.11 header, containing a bunch of additional
1047 * information including radio-level information.
1049 * The radiotap header is variable length, and we
1050 * generate code to compute its length and store it
1051 * in a register. These offsets are relative to the
1052 * beginning of the 802.11 header.
1055 off_nl
= 32; /* 802.11+802.2+SNAP */
1056 off_nl_nosnap
= 27; /* 802.11+802.2 */
1059 case DLT_ATM_RFC1483
:
1060 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1062 * assume routed, non-ISO PDUs
1063 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1065 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1066 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1067 * latter would presumably be treated the way PPPoE
1068 * should be, so you can do "pppoe and udp port 2049"
1069 * or "pppoa and tcp port 80" and have it check for
1070 * PPPo{A,E} and a PPP protocol of IP and....
1073 off_nl
= 8; /* 802.2+SNAP */
1074 off_nl_nosnap
= 3; /* 802.2 */
1079 * Full Frontal ATM; you get AALn PDUs with an ATM
1083 off_vpi
= SUNATM_VPI_POS
;
1084 off_vci
= SUNATM_VCI_POS
;
1085 off_proto
= PROTO_POS
;
1086 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1087 off_payload
= SUNATM_PKT_BEGIN_POS
;
1088 off_linktype
= off_payload
;
1089 off_nl
= off_payload
+8; /* 802.2+SNAP */
1090 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1096 off_nl_nosnap
= 0; /* no 802.2 LLC */
1099 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1102 off_nl_nosnap
= 16; /* no 802.2 LLC */
1107 * LocalTalk does have a 1-byte type field in the LLAP header,
1108 * but really it just indicates whether there is a "short" or
1109 * "long" DDP packet following.
1113 off_nl_nosnap
= 0; /* no 802.2 LLC */
1116 case DLT_IP_OVER_FC
:
1118 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1119 * link-level type field. We set "off_linktype" to the
1120 * offset of the LLC header.
1122 * To check for Ethernet types, we assume that SSAP = SNAP
1123 * is being used and pick out the encapsulated Ethernet type.
1124 * XXX - should we generate code to check for SNAP? RFC
1125 * 2625 says SNAP should be used.
1128 off_nl
= 24; /* IPFC+802.2+SNAP */
1129 off_nl_nosnap
= 19; /* IPFC+802.2 */
1134 * XXX - we should set this to handle SNAP-encapsulated
1135 * frames (NLPID of 0x80).
1139 off_nl_nosnap
= 0; /* no 802.2 LLC */
1143 * the only BPF-interesting FRF.16 frames are non-control frames;
1144 * Frame Relay has a variable length link-layer
1145 * so lets start with offset 4 for now and increments later on (FIXME);
1150 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1153 case DLT_APPLE_IP_OVER_IEEE1394
:
1156 off_nl_nosnap
= 18; /* no 802.2 LLC */
1159 case DLT_LINUX_IRDA
:
1161 * Currently, only raw "link[N:M]" filtering is supported.
1170 * Currently, only raw "link[N:M]" filtering is supported.
1177 case DLT_SYMANTEC_FIREWALL
:
1179 off_nl
= 44; /* Ethernet II */
1180 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1183 #ifdef HAVE_NET_PFVAR_H
1186 off_nl
= PFLOG_HDRLEN
;
1187 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1191 case DLT_JUNIPER_MFR
:
1192 case DLT_JUNIPER_MLFR
:
1193 case DLT_JUNIPER_MLPPP
:
1194 case DLT_JUNIPER_PPP
:
1195 case DLT_JUNIPER_CHDLC
:
1196 case DLT_JUNIPER_FRELAY
:
1199 off_nl_nosnap
= -1; /* no 802.2 LLC */
1202 case DLT_JUNIPER_ATM1
:
1203 off_linktype
= 4; /* in reality variable between 4-8 */
1208 case DLT_JUNIPER_ATM2
:
1209 off_linktype
= 8; /* in reality variable between 8-12 */
1214 /* frames captured on a Juniper PPPoE service PIC
1215 * contain raw ethernet frames */
1216 case DLT_JUNIPER_PPPOE
:
1217 case DLT_JUNIPER_ETHER
:
1219 off_nl
= 18; /* Ethernet II */
1220 off_nl_nosnap
= 21; /* 802.3+802.2 */
1223 case DLT_JUNIPER_PPPOE_ATM
:
1226 off_nl_nosnap
= -1; /* no 802.2 LLC */
1229 case DLT_JUNIPER_GGSN
:
1232 off_nl_nosnap
= -1; /* no 802.2 LLC */
1235 case DLT_JUNIPER_ES
:
1237 off_nl
= -1; /* not really a network layer but raw IP adresses */
1238 off_nl_nosnap
= -1; /* no 802.2 LLC */
1241 case DLT_JUNIPER_MONITOR
:
1243 off_nl
= 12; /* raw IP/IP6 header */
1244 off_nl_nosnap
= -1; /* no 802.2 LLC */
1247 case DLT_JUNIPER_SERVICES
:
1249 off_nl
= -1; /* L3 proto location dep. on cookie type */
1250 off_nl_nosnap
= -1; /* no 802.2 LLC */
1253 case DLT_JUNIPER_VP
:
1259 case DLT_JUNIPER_ST
:
1265 case DLT_JUNIPER_ISM
:
1282 case DLT_MTP2_WITH_PHDR
:
1312 case DLT_LINUX_LAPD
:
1314 * Currently, only raw "link[N:M]" filtering is supported.
1323 * Currently, only raw "link[N:M]" filtering is supported.
1330 case DLT_BLUETOOTH_HCI_H4
:
1332 * Currently, only raw "link[N:M]" filtering is supported.
1341 * Currently, only raw "link[N:M]" filtering is supported.
1350 * Currently, only raw "link[N:M]" filtering is supported.
1357 case DLT_IEEE802_15_4_LINUX
:
1359 * Currently, only raw "link[N:M]" filtering is supported.
1366 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1368 * Currently, only raw "link[N:M]" filtering is supported.
1375 case DLT_IEEE802_15_4
:
1377 * Currently, only raw "link[N:M]" filtering is supported.
1386 * Currently, only raw "link[N:M]" filtering is supported.
1395 * Currently, only raw "link[N:M]" filtering is supported.
1404 * Currently, only raw "link[N:M]" filtering is supported.
1411 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1413 * Currently, only raw "link[N:M]" filtering is supported.
1422 * Currently, only raw "link[N:M]" filtering is supported.
1424 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1425 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1426 off_nl_nosnap
= -1; /* no 802.2 LLC */
1427 off_mac
= 1; /* step over the kiss length byte */
1430 bpf_error("unknown data link type %d", linktype
);
1435 * Load a value relative to the beginning of the link-layer header.
1436 * The link-layer header doesn't necessarily begin at the beginning
1437 * of the packet data; there might be a variable-length prefix containing
1438 * radio information.
1440 static struct slist
*
1441 gen_load_llrel(offset
, size
)
1444 struct slist
*s
, *s2
;
1446 s
= gen_llprefixlen();
1449 * If "s" is non-null, it has code to arrange that the X register
1450 * contains the length of the prefix preceding the link-layer
1453 * Otherwise, the length of the prefix preceding the link-layer
1454 * header is "off_ll".
1458 * There's a variable-length prefix preceding the
1459 * link-layer header. "s" points to a list of statements
1460 * that put the length of that prefix into the X register.
1461 * do an indirect load, to use the X register as an offset.
1463 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1468 * There is no variable-length header preceding the
1469 * link-layer header; add in off_ll, which, if there's
1470 * a fixed-length header preceding the link-layer header,
1471 * is the length of that header.
1473 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1474 s
->s
.k
= offset
+ off_ll
;
1480 * Load a value relative to the beginning of the specified header.
1482 static struct slist
*
1483 gen_load_a(offrel
, offset
, size
)
1484 enum e_offrel offrel
;
1487 struct slist
*s
, *s2
;
1492 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1497 s
= gen_load_llrel(offset
, size
);
1501 s
= gen_load_llrel(off_nl
+ offset
, size
);
1505 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1510 * Load the X register with the length of the IPv4 header
1511 * (plus the offset of the link-layer header, if it's
1512 * preceded by a variable-length header such as a radio
1513 * header), in bytes.
1515 s
= gen_loadx_iphdrlen();
1518 * Load the item at {offset of the link-layer header} +
1519 * {offset, relative to the start of the link-layer
1520 * header, of the IPv4 header} + {length of the IPv4 header} +
1521 * {specified offset}.
1523 * (If the link-layer is variable-length, it's included
1524 * in the value in the X register, and off_ll is 0.)
1526 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1527 s2
->s
.k
= off_ll
+ off_nl
+ offset
;
1532 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1543 * Generate code to load into the X register the sum of the length of
1544 * the IPv4 header and any variable-length header preceding the link-layer
1547 static struct slist
*
1548 gen_loadx_iphdrlen()
1550 struct slist
*s
, *s2
;
1552 s
= gen_llprefixlen();
1555 * There's a variable-length prefix preceding the
1556 * link-layer header. "s" points to a list of statements
1557 * that put the length of that prefix into the X register.
1558 * The 4*([k]&0xf) addressing mode can't be used, as we
1559 * don't have a constant offset, so we have to load the
1560 * value in question into the A register and add to it
1561 * the value from the X register.
1563 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1566 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1569 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1574 * The A register now contains the length of the
1575 * IP header. We need to add to it the length
1576 * of the prefix preceding the link-layer
1577 * header, which is still in the X register, and
1578 * move the result into the X register.
1580 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1581 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1584 * There is no variable-length header preceding the
1585 * link-layer header; add in off_ll, which, if there's
1586 * a fixed-length header preceding the link-layer header,
1587 * is the length of that header.
1589 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1590 s
->s
.k
= off_ll
+ off_nl
;
1595 static struct block
*
1602 s
= new_stmt(BPF_LD
|BPF_IMM
);
1604 b
= new_block(JMP(BPF_JEQ
));
1610 static inline struct block
*
1613 return gen_uncond(1);
1616 static inline struct block
*
1619 return gen_uncond(0);
1623 * Byte-swap a 32-bit number.
1624 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1625 * big-endian platforms.)
1627 #define SWAPLONG(y) \
1628 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1631 * Generate code to match a particular packet type.
1633 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1634 * value, if <= ETHERMTU. We use that to determine whether to
1635 * match the type/length field or to check the type/length field for
1636 * a value <= ETHERMTU to see whether it's a type field and then do
1637 * the appropriate test.
1639 static struct block
*
1640 gen_ether_linktype(proto
)
1643 struct block
*b0
, *b1
;
1649 case LLCSAP_NETBEUI
:
1651 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1652 * so we check the DSAP and SSAP.
1654 * LLCSAP_IP checks for IP-over-802.2, rather
1655 * than IP-over-Ethernet or IP-over-SNAP.
1657 * XXX - should we check both the DSAP and the
1658 * SSAP, like this, or should we check just the
1659 * DSAP, as we do for other types <= ETHERMTU
1660 * (i.e., other SAP values)?
1662 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1664 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1665 ((proto
<< 8) | proto
));
1673 * Ethernet_II frames, which are Ethernet
1674 * frames with a frame type of ETHERTYPE_IPX;
1676 * Ethernet_802.3 frames, which are 802.3
1677 * frames (i.e., the type/length field is
1678 * a length field, <= ETHERMTU, rather than
1679 * a type field) with the first two bytes
1680 * after the Ethernet/802.3 header being
1683 * Ethernet_802.2 frames, which are 802.3
1684 * frames with an 802.2 LLC header and
1685 * with the IPX LSAP as the DSAP in the LLC
1688 * Ethernet_SNAP frames, which are 802.3
1689 * frames with an LLC header and a SNAP
1690 * header and with an OUI of 0x000000
1691 * (encapsulated Ethernet) and a protocol
1692 * ID of ETHERTYPE_IPX in the SNAP header.
1694 * XXX - should we generate the same code both
1695 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1699 * This generates code to check both for the
1700 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1702 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1703 (bpf_int32
)LLCSAP_IPX
);
1704 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1709 * Now we add code to check for SNAP frames with
1710 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1712 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, off_linktype
+ 2);
1716 * Now we generate code to check for 802.3
1717 * frames in general.
1719 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1723 * Now add the check for 802.3 frames before the
1724 * check for Ethernet_802.2 and Ethernet_802.3,
1725 * as those checks should only be done on 802.3
1726 * frames, not on Ethernet frames.
1731 * Now add the check for Ethernet_II frames, and
1732 * do that before checking for the other frame
1735 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1736 (bpf_int32
)ETHERTYPE_IPX
);
1740 case ETHERTYPE_ATALK
:
1741 case ETHERTYPE_AARP
:
1743 * EtherTalk (AppleTalk protocols on Ethernet link
1744 * layer) may use 802.2 encapsulation.
1748 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1749 * we check for an Ethernet type field less than
1750 * 1500, which means it's an 802.3 length field.
1752 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1756 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1757 * SNAP packets with an organization code of
1758 * 0x080007 (Apple, for Appletalk) and a protocol
1759 * type of ETHERTYPE_ATALK (Appletalk).
1761 * 802.2-encapsulated ETHERTYPE_AARP packets are
1762 * SNAP packets with an organization code of
1763 * 0x000000 (encapsulated Ethernet) and a protocol
1764 * type of ETHERTYPE_AARP (Appletalk ARP).
1766 if (proto
== ETHERTYPE_ATALK
)
1767 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1769 else /* proto == ETHERTYPE_AARP */
1770 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1775 * Check for Ethernet encapsulation (Ethertalk
1776 * phase 1?); we just check for the Ethernet
1779 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1785 if (proto
<= ETHERMTU
) {
1787 * This is an LLC SAP value, so the frames
1788 * that match would be 802.2 frames.
1789 * Check that the frame is an 802.2 frame
1790 * (i.e., that the length/type field is
1791 * a length field, <= ETHERMTU) and
1792 * then check the DSAP.
1794 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1796 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1802 * This is an Ethernet type, so compare
1803 * the length/type field with it (if
1804 * the frame is an 802.2 frame, the length
1805 * field will be <= ETHERMTU, and, as
1806 * "proto" is > ETHERMTU, this test
1807 * will fail and the frame won't match,
1808 * which is what we want).
1810 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1817 * Generate code to match a particular packet type.
1819 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1820 * value, if <= ETHERMTU. We use that to determine whether to
1821 * match the type field or to check the type field for the special
1822 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1824 static struct block
*
1825 gen_linux_sll_linktype(proto
)
1828 struct block
*b0
, *b1
;
1834 case LLCSAP_NETBEUI
:
1836 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1837 * so we check the DSAP and SSAP.
1839 * LLCSAP_IP checks for IP-over-802.2, rather
1840 * than IP-over-Ethernet or IP-over-SNAP.
1842 * XXX - should we check both the DSAP and the
1843 * SSAP, like this, or should we check just the
1844 * DSAP, as we do for other types <= ETHERMTU
1845 * (i.e., other SAP values)?
1847 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1848 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1849 ((proto
<< 8) | proto
));
1855 * Ethernet_II frames, which are Ethernet
1856 * frames with a frame type of ETHERTYPE_IPX;
1858 * Ethernet_802.3 frames, which have a frame
1859 * type of LINUX_SLL_P_802_3;
1861 * Ethernet_802.2 frames, which are 802.3
1862 * frames with an 802.2 LLC header (i.e, have
1863 * a frame type of LINUX_SLL_P_802_2) and
1864 * with the IPX LSAP as the DSAP in the LLC
1867 * Ethernet_SNAP frames, which are 802.3
1868 * frames with an LLC header and a SNAP
1869 * header and with an OUI of 0x000000
1870 * (encapsulated Ethernet) and a protocol
1871 * ID of ETHERTYPE_IPX in the SNAP header.
1873 * First, do the checks on LINUX_SLL_P_802_2
1874 * frames; generate the check for either
1875 * Ethernet_802.2 or Ethernet_SNAP frames, and
1876 * then put a check for LINUX_SLL_P_802_2 frames
1879 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1880 (bpf_int32
)LLCSAP_IPX
);
1881 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1884 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1888 * Now check for 802.3 frames and OR that with
1889 * the previous test.
1891 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1895 * Now add the check for Ethernet_II frames, and
1896 * do that before checking for the other frame
1899 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1900 (bpf_int32
)ETHERTYPE_IPX
);
1904 case ETHERTYPE_ATALK
:
1905 case ETHERTYPE_AARP
:
1907 * EtherTalk (AppleTalk protocols on Ethernet link
1908 * layer) may use 802.2 encapsulation.
1912 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1913 * we check for the 802.2 protocol type in the
1914 * "Ethernet type" field.
1916 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1919 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1920 * SNAP packets with an organization code of
1921 * 0x080007 (Apple, for Appletalk) and a protocol
1922 * type of ETHERTYPE_ATALK (Appletalk).
1924 * 802.2-encapsulated ETHERTYPE_AARP packets are
1925 * SNAP packets with an organization code of
1926 * 0x000000 (encapsulated Ethernet) and a protocol
1927 * type of ETHERTYPE_AARP (Appletalk ARP).
1929 if (proto
== ETHERTYPE_ATALK
)
1930 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1932 else /* proto == ETHERTYPE_AARP */
1933 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1938 * Check for Ethernet encapsulation (Ethertalk
1939 * phase 1?); we just check for the Ethernet
1942 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1948 if (proto
<= ETHERMTU
) {
1950 * This is an LLC SAP value, so the frames
1951 * that match would be 802.2 frames.
1952 * Check for the 802.2 protocol type
1953 * in the "Ethernet type" field, and
1954 * then check the DSAP.
1956 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1958 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1964 * This is an Ethernet type, so compare
1965 * the length/type field with it (if
1966 * the frame is an 802.2 frame, the length
1967 * field will be <= ETHERMTU, and, as
1968 * "proto" is > ETHERMTU, this test
1969 * will fail and the frame won't match,
1970 * which is what we want).
1972 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1979 insert_radiotap_load_llprefixlen(b
)
1982 struct slist
*s1
, *s2
;
1985 * Prepend to the statements in this block code to load the
1986 * length of the radiotap header into the register assigned
1987 * to hold that length, if one has been assigned.
1989 if (reg_ll_size
!= -1) {
1991 * The 2 bytes at offsets of 2 and 3 from the beginning
1992 * of the radiotap header are the length of the radiotap
1993 * header; unfortunately, it's little-endian, so we have
1994 * to load it a byte at a time and construct the value.
1998 * Load the high-order byte, at an offset of 3, shift it
1999 * left a byte, and put the result in the X register.
2001 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2003 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2006 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2010 * Load the next byte, at an offset of 2, and OR the
2011 * value from the X register into it.
2013 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2016 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2020 * Now allocate a register to hold that value and store
2023 s2
= new_stmt(BPF_ST
);
2024 s2
->s
.k
= reg_ll_size
;
2028 * Now move it into the X register.
2030 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2034 * Now append all the existing statements in this
2035 * block to these statements.
2037 sappend(s1
, b
->stmts
);
2043 * At the moment we treat PPI as normal Radiotap encoded
2044 * packets. The difference is in the function that generates
2045 * the code at the beginning to compute the header length.
2046 * Since this code generator of PPI supports bare 802.11
2047 * encapsulation only (i.e. the encapsulated DLT should be
2048 * DLT_IEEE802_11) we generate code to check for this too.
2051 insert_ppi_load_llprefixlen(b
)
2054 struct slist
*s1
, *s2
;
2057 * Prepend to the statements in this block code to load the
2058 * length of the radiotap header into the register assigned
2059 * to hold that length, if one has been assigned.
2061 if (reg_ll_size
!= -1) {
2063 * The 2 bytes at offsets of 2 and 3 from the beginning
2064 * of the radiotap header are the length of the radiotap
2065 * header; unfortunately, it's little-endian, so we have
2066 * to load it a byte at a time and construct the value.
2070 * Load the high-order byte, at an offset of 3, shift it
2071 * left a byte, and put the result in the X register.
2073 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2075 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2078 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2082 * Load the next byte, at an offset of 2, and OR the
2083 * value from the X register into it.
2085 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2088 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2092 * Now allocate a register to hold that value and store
2095 s2
= new_stmt(BPF_ST
);
2096 s2
->s
.k
= reg_ll_size
;
2100 * Now move it into the X register.
2102 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2106 * Now append all the existing statements in this
2107 * block to these statements.
2109 sappend(s1
, b
->stmts
);
2115 static struct block
*
2116 gen_ppi_dlt_check(void)
2118 struct slist
*s_load_dlt
;
2121 if (linktype
== DLT_PPI
)
2123 /* Create the statements that check for the DLT
2125 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2126 s_load_dlt
->s
.k
= 4;
2128 b
= new_block(JMP(BPF_JEQ
));
2130 b
->stmts
= s_load_dlt
;
2131 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2142 insert_load_llprefixlen(b
)
2148 * At the moment we treat PPI as normal Radiotap encoded
2149 * packets. The difference is in the function that generates
2150 * the code at the beginning to compute the header length.
2151 * Since this code generator of PPI supports bare 802.11
2152 * encapsulation only (i.e. the encapsulated DLT should be
2153 * DLT_IEEE802_11) we generate code to check for this too.
2156 insert_ppi_load_llprefixlen(b
);
2159 case DLT_IEEE802_11_RADIO
:
2160 insert_radiotap_load_llprefixlen(b
);
2166 static struct slist
*
2167 gen_radiotap_llprefixlen(void)
2171 if (reg_ll_size
== -1) {
2173 * We haven't yet assigned a register for the length
2174 * of the radiotap header; allocate one.
2176 reg_ll_size
= alloc_reg();
2180 * Load the register containing the radiotap length
2181 * into the X register.
2183 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2184 s
->s
.k
= reg_ll_size
;
2189 * At the moment we treat PPI as normal Radiotap encoded
2190 * packets. The difference is in the function that generates
2191 * the code at the beginning to compute the header length.
2192 * Since this code generator of PPI supports bare 802.11
2193 * encapsulation only (i.e. the encapsulated DLT should be
2194 * DLT_IEEE802_11) we generate code to check for this too.
2196 static struct slist
*
2197 gen_ppi_llprefixlen(void)
2201 if (reg_ll_size
== -1) {
2203 * We haven't yet assigned a register for the length
2204 * of the radiotap header; allocate one.
2206 reg_ll_size
= alloc_reg();
2210 * Load the register containing the radiotap length
2211 * into the X register.
2213 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2214 s
->s
.k
= reg_ll_size
;
2221 * Generate code to compute the link-layer header length, if necessary,
2222 * putting it into the X register, and to return either a pointer to a
2223 * "struct slist" for the list of statements in that code, or NULL if
2224 * no code is necessary.
2226 static struct slist
*
2227 gen_llprefixlen(void)
2232 return gen_ppi_llprefixlen();
2235 case DLT_IEEE802_11_RADIO
:
2236 return gen_radiotap_llprefixlen();
2244 * Generate code to match a particular packet type by matching the
2245 * link-layer type field or fields in the 802.2 LLC header.
2247 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2248 * value, if <= ETHERMTU.
2250 static struct block
*
2254 struct block
*b0
, *b1
, *b2
;
2256 /* are we checking MPLS-encapsulated packets? */
2257 if (label_stack_depth
> 0) {
2261 /* FIXME add other L3 proto IDs */
2262 return gen_mpls_linktype(Q_IP
);
2264 case ETHERTYPE_IPV6
:
2266 /* FIXME add other L3 proto IDs */
2267 return gen_mpls_linktype(Q_IPV6
);
2270 bpf_error("unsupported protocol over mpls");
2278 return gen_ether_linktype(proto
);
2286 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2290 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2300 case DLT_IEEE802_11
:
2301 case DLT_IEEE802_11_RADIO_AVS
:
2302 case DLT_IEEE802_11_RADIO
:
2303 case DLT_PRISM_HEADER
:
2304 case DLT_ATM_RFC1483
:
2306 case DLT_IP_OVER_FC
:
2307 return gen_llc_linktype(proto
);
2313 * If "is_lane" is set, check for a LANE-encapsulated
2314 * version of this protocol, otherwise check for an
2315 * LLC-encapsulated version of this protocol.
2317 * We assume LANE means Ethernet, not Token Ring.
2321 * Check that the packet doesn't begin with an
2322 * LE Control marker. (We've already generated
2325 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2330 * Now generate an Ethernet test.
2332 b1
= gen_ether_linktype(proto
);
2337 * Check for LLC encapsulation and then check the
2340 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2341 b1
= gen_llc_linktype(proto
);
2349 return gen_linux_sll_linktype(proto
);
2354 case DLT_SLIP_BSDOS
:
2357 * These types don't provide any type field; packets
2358 * are always IPv4 or IPv6.
2360 * XXX - for IPv4, check for a version number of 4, and,
2361 * for IPv6, check for a version number of 6?
2366 /* Check for a version number of 4. */
2367 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2369 case ETHERTYPE_IPV6
:
2370 /* Check for a version number of 6. */
2371 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2375 return gen_false(); /* always false */
2382 case DLT_PPP_SERIAL
:
2385 * We use Ethernet protocol types inside libpcap;
2386 * map them to the corresponding PPP protocol types.
2395 case ETHERTYPE_IPV6
:
2404 case ETHERTYPE_ATALK
:
2418 * I'm assuming the "Bridging PDU"s that go
2419 * over PPP are Spanning Tree Protocol
2433 * We use Ethernet protocol types inside libpcap;
2434 * map them to the corresponding PPP protocol types.
2439 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2440 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2442 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2447 case ETHERTYPE_IPV6
:
2457 case ETHERTYPE_ATALK
:
2471 * I'm assuming the "Bridging PDU"s that go
2472 * over PPP are Spanning Tree Protocol
2488 * For DLT_NULL, the link-layer header is a 32-bit
2489 * word containing an AF_ value in *host* byte order,
2490 * and for DLT_ENC, the link-layer header begins
2491 * with a 32-bit work containing an AF_ value in
2494 * In addition, if we're reading a saved capture file,
2495 * the host byte order in the capture may not be the
2496 * same as the host byte order on this machine.
2498 * For DLT_LOOP, the link-layer header is a 32-bit
2499 * word containing an AF_ value in *network* byte order.
2501 * XXX - AF_ values may, unfortunately, be platform-
2502 * dependent; for example, FreeBSD's AF_INET6 is 24
2503 * whilst NetBSD's and OpenBSD's is 26.
2505 * This means that, when reading a capture file, just
2506 * checking for our AF_INET6 value won't work if the
2507 * capture file came from another OS.
2516 case ETHERTYPE_IPV6
:
2523 * Not a type on which we support filtering.
2524 * XXX - support those that have AF_ values
2525 * #defined on this platform, at least?
2530 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2532 * The AF_ value is in host byte order, but
2533 * the BPF interpreter will convert it to
2534 * network byte order.
2536 * If this is a save file, and it's from a
2537 * machine with the opposite byte order to
2538 * ours, we byte-swap the AF_ value.
2540 * Then we run it through "htonl()", and
2541 * generate code to compare against the result.
2543 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2544 bpf_pcap
->sf
.swapped
)
2545 proto
= SWAPLONG(proto
);
2546 proto
= htonl(proto
);
2548 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2550 #ifdef HAVE_NET_PFVAR_H
2553 * af field is host byte order in contrast to the rest of
2556 if (proto
== ETHERTYPE_IP
)
2557 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2558 BPF_B
, (bpf_int32
)AF_INET
));
2560 else if (proto
== ETHERTYPE_IPV6
)
2561 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2562 BPF_B
, (bpf_int32
)AF_INET6
));
2568 #endif /* HAVE_NET_PFVAR_H */
2571 case DLT_ARCNET_LINUX
:
2573 * XXX should we check for first fragment if the protocol
2582 case ETHERTYPE_IPV6
:
2583 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2584 (bpf_int32
)ARCTYPE_INET6
));
2588 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2589 (bpf_int32
)ARCTYPE_IP
);
2590 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2591 (bpf_int32
)ARCTYPE_IP_OLD
);
2596 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2597 (bpf_int32
)ARCTYPE_ARP
);
2598 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2599 (bpf_int32
)ARCTYPE_ARP_OLD
);
2603 case ETHERTYPE_REVARP
:
2604 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2605 (bpf_int32
)ARCTYPE_REVARP
));
2607 case ETHERTYPE_ATALK
:
2608 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2609 (bpf_int32
)ARCTYPE_ATALK
));
2616 case ETHERTYPE_ATALK
:
2626 * XXX - assumes a 2-byte Frame Relay header with
2627 * DLCI and flags. What if the address is longer?
2633 * Check for the special NLPID for IP.
2635 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2638 case ETHERTYPE_IPV6
:
2640 * Check for the special NLPID for IPv6.
2642 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2647 * Check for several OSI protocols.
2649 * Frame Relay packets typically have an OSI
2650 * NLPID at the beginning; we check for each
2653 * What we check for is the NLPID and a frame
2654 * control field of UI, i.e. 0x03 followed
2657 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2658 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2659 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2670 case DLT_JUNIPER_MFR
:
2671 case DLT_JUNIPER_MLFR
:
2672 case DLT_JUNIPER_MLPPP
:
2673 case DLT_JUNIPER_ATM1
:
2674 case DLT_JUNIPER_ATM2
:
2675 case DLT_JUNIPER_PPPOE
:
2676 case DLT_JUNIPER_PPPOE_ATM
:
2677 case DLT_JUNIPER_GGSN
:
2678 case DLT_JUNIPER_ES
:
2679 case DLT_JUNIPER_MONITOR
:
2680 case DLT_JUNIPER_SERVICES
:
2681 case DLT_JUNIPER_ETHER
:
2682 case DLT_JUNIPER_PPP
:
2683 case DLT_JUNIPER_FRELAY
:
2684 case DLT_JUNIPER_CHDLC
:
2685 case DLT_JUNIPER_VP
:
2686 case DLT_JUNIPER_ST
:
2687 case DLT_JUNIPER_ISM
:
2688 /* just lets verify the magic number for now -
2689 * on ATM we may have up to 6 different encapsulations on the wire
2690 * and need a lot of heuristics to figure out that the payload
2693 * FIXME encapsulation specific BPF_ filters
2695 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2697 case DLT_LINUX_IRDA
:
2698 bpf_error("IrDA link-layer type filtering not implemented");
2701 bpf_error("DOCSIS link-layer type filtering not implemented");
2703 case DLT_LINUX_LAPD
:
2704 bpf_error("LAPD link-layer type filtering not implemented");
2707 bpf_error("AX.25 link-layer type filtering not implemented");
2711 * All the types that have no encapsulation should either be
2712 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2713 * all packets are IP packets, or should be handled in some
2714 * special case, if none of them are (if some are and some
2715 * aren't, the lack of encapsulation is a problem, as we'd
2716 * have to find some other way of determining the packet type).
2718 * Therefore, if "off_linktype" is -1, there's an error.
2720 if (off_linktype
== (u_int
)-1)
2724 * Any type not handled above should always have an Ethernet
2725 * type at an offset of "off_linktype". (PPP is partially
2726 * handled above - the protocol type is mapped from the
2727 * Ethernet and LLC types we use internally to the corresponding
2728 * PPP type - but the PPP type is always specified by a value
2729 * at "off_linktype", so we don't have to do the code generation
2732 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2736 * Check for an LLC SNAP packet with a given organization code and
2737 * protocol type; we check the entire contents of the 802.2 LLC and
2738 * snap headers, checking for DSAP and SSAP of SNAP and a control
2739 * field of 0x03 in the LLC header, and for the specified organization
2740 * code and protocol type in the SNAP header.
2742 static struct block
*
2743 gen_snap(orgcode
, ptype
, offset
)
2744 bpf_u_int32 orgcode
;
2748 u_char snapblock
[8];
2750 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2751 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2752 snapblock
[2] = 0x03; /* control = UI */
2753 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2754 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2755 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2756 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2757 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2758 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2762 * Generate code to match a particular packet type, for link-layer types
2763 * using 802.2 LLC headers.
2765 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2766 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2768 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2769 * value, if <= ETHERMTU. We use that to determine whether to
2770 * match the DSAP or both DSAP and LSAP or to check the OUI and
2771 * protocol ID in a SNAP header.
2773 static struct block
*
2774 gen_llc_linktype(proto
)
2778 * XXX - handle token-ring variable-length header.
2784 case LLCSAP_NETBEUI
:
2786 * XXX - should we check both the DSAP and the
2787 * SSAP, like this, or should we check just the
2788 * DSAP, as we do for other types <= ETHERMTU
2789 * (i.e., other SAP values)?
2791 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2792 ((proto
<< 8) | proto
));
2796 * XXX - are there ever SNAP frames for IPX on
2797 * non-Ethernet 802.x networks?
2799 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2800 (bpf_int32
)LLCSAP_IPX
);
2802 case ETHERTYPE_ATALK
:
2804 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2805 * SNAP packets with an organization code of
2806 * 0x080007 (Apple, for Appletalk) and a protocol
2807 * type of ETHERTYPE_ATALK (Appletalk).
2809 * XXX - check for an organization code of
2810 * encapsulated Ethernet as well?
2812 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2816 * XXX - we don't have to check for IPX 802.3
2817 * here, but should we check for the IPX Ethertype?
2819 if (proto
<= ETHERMTU
) {
2821 * This is an LLC SAP value, so check
2824 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2828 * This is an Ethernet type; we assume that it's
2829 * unlikely that it'll appear in the right place
2830 * at random, and therefore check only the
2831 * location that would hold the Ethernet type
2832 * in a SNAP frame with an organization code of
2833 * 0x000000 (encapsulated Ethernet).
2835 * XXX - if we were to check for the SNAP DSAP and
2836 * LSAP, as per XXX, and were also to check for an
2837 * organization code of 0x000000 (encapsulated
2838 * Ethernet), we'd do
2840 * return gen_snap(0x000000, proto,
2843 * here; for now, we don't, as per the above.
2844 * I don't know whether it's worth the extra CPU
2845 * time to do the right check or not.
2847 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2853 static struct block
*
2854 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2858 u_int src_off
, dst_off
;
2860 struct block
*b0
, *b1
;
2874 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2875 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2881 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2882 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2889 b0
= gen_linktype(proto
);
2890 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2896 static struct block
*
2897 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2898 struct in6_addr
*addr
;
2899 struct in6_addr
*mask
;
2901 u_int src_off
, dst_off
;
2903 struct block
*b0
, *b1
;
2918 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2919 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2925 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2926 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2933 /* this order is important */
2934 a
= (u_int32_t
*)addr
;
2935 m
= (u_int32_t
*)mask
;
2936 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2937 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2939 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2941 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2943 b0
= gen_linktype(proto
);
2949 static struct block
*
2950 gen_ehostop(eaddr
, dir
)
2951 register const u_char
*eaddr
;
2954 register struct block
*b0
, *b1
;
2958 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2961 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2964 b0
= gen_ehostop(eaddr
, Q_SRC
);
2965 b1
= gen_ehostop(eaddr
, Q_DST
);
2971 b0
= gen_ehostop(eaddr
, Q_SRC
);
2972 b1
= gen_ehostop(eaddr
, Q_DST
);
2981 * Like gen_ehostop, but for DLT_FDDI
2983 static struct block
*
2984 gen_fhostop(eaddr
, dir
)
2985 register const u_char
*eaddr
;
2988 struct block
*b0
, *b1
;
2993 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2995 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3000 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3002 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3006 b0
= gen_fhostop(eaddr
, Q_SRC
);
3007 b1
= gen_fhostop(eaddr
, Q_DST
);
3013 b0
= gen_fhostop(eaddr
, Q_SRC
);
3014 b1
= gen_fhostop(eaddr
, Q_DST
);
3023 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3025 static struct block
*
3026 gen_thostop(eaddr
, dir
)
3027 register const u_char
*eaddr
;
3030 register struct block
*b0
, *b1
;
3034 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3037 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3040 b0
= gen_thostop(eaddr
, Q_SRC
);
3041 b1
= gen_thostop(eaddr
, Q_DST
);
3047 b0
= gen_thostop(eaddr
, Q_SRC
);
3048 b1
= gen_thostop(eaddr
, Q_DST
);
3057 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
3059 static struct block
*
3060 gen_wlanhostop(eaddr
, dir
)
3061 register const u_char
*eaddr
;
3064 register struct block
*b0
, *b1
, *b2
;
3065 register struct slist
*s
;
3072 * For control frames, there is no SA.
3074 * For management frames, SA is at an
3075 * offset of 10 from the beginning of
3078 * For data frames, SA is at an offset
3079 * of 10 from the beginning of the packet
3080 * if From DS is clear, at an offset of
3081 * 16 from the beginning of the packet
3082 * if From DS is set and To DS is clear,
3083 * and an offset of 24 from the beginning
3084 * of the packet if From DS is set and To DS
3089 * Generate the tests to be done for data frames
3092 * First, check for To DS set, i.e. check "link[1] & 0x01".
3094 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3095 b1
= new_block(JMP(BPF_JSET
));
3096 b1
->s
.k
= 0x01; /* To DS */
3100 * If To DS is set, the SA is at 24.
3102 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3106 * Now, check for To DS not set, i.e. check
3107 * "!(link[1] & 0x01)".
3109 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3110 b2
= new_block(JMP(BPF_JSET
));
3111 b2
->s
.k
= 0x01; /* To DS */
3116 * If To DS is not set, the SA is at 16.
3118 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3122 * Now OR together the last two checks. That gives
3123 * the complete set of checks for data frames with
3129 * Now check for From DS being set, and AND that with
3130 * the ORed-together checks.
3132 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3133 b1
= new_block(JMP(BPF_JSET
));
3134 b1
->s
.k
= 0x02; /* From DS */
3139 * Now check for data frames with From DS not set.
3141 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3142 b2
= new_block(JMP(BPF_JSET
));
3143 b2
->s
.k
= 0x02; /* From DS */
3148 * If From DS isn't set, the SA is at 10.
3150 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3154 * Now OR together the checks for data frames with
3155 * From DS not set and for data frames with From DS
3156 * set; that gives the checks done for data frames.
3161 * Now check for a data frame.
3162 * I.e, check "link[0] & 0x08".
3164 gen_load_a(OR_LINK
, 0, BPF_B
);
3165 b1
= new_block(JMP(BPF_JSET
));
3170 * AND that with the checks done for data frames.
3175 * If the high-order bit of the type value is 0, this
3176 * is a management frame.
3177 * I.e, check "!(link[0] & 0x08)".
3179 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3180 b2
= new_block(JMP(BPF_JSET
));
3186 * For management frames, the SA is at 10.
3188 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3192 * OR that with the checks done for data frames.
3193 * That gives the checks done for management and
3199 * If the low-order bit of the type value is 1,
3200 * this is either a control frame or a frame
3201 * with a reserved type, and thus not a
3204 * I.e., check "!(link[0] & 0x04)".
3206 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3207 b1
= new_block(JMP(BPF_JSET
));
3213 * AND that with the checks for data and management
3223 * For control frames, there is no DA.
3225 * For management frames, DA is at an
3226 * offset of 4 from the beginning of
3229 * For data frames, DA is at an offset
3230 * of 4 from the beginning of the packet
3231 * if To DS is clear and at an offset of
3232 * 16 from the beginning of the packet
3237 * Generate the tests to be done for data frames.
3239 * First, check for To DS set, i.e. "link[1] & 0x01".
3241 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3242 b1
= new_block(JMP(BPF_JSET
));
3243 b1
->s
.k
= 0x01; /* To DS */
3247 * If To DS is set, the DA is at 16.
3249 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3253 * Now, check for To DS not set, i.e. check
3254 * "!(link[1] & 0x01)".
3256 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3257 b2
= new_block(JMP(BPF_JSET
));
3258 b2
->s
.k
= 0x01; /* To DS */
3263 * If To DS is not set, the DA is at 4.
3265 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3269 * Now OR together the last two checks. That gives
3270 * the complete set of checks for data frames.
3275 * Now check for a data frame.
3276 * I.e, check "link[0] & 0x08".
3278 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3279 b1
= new_block(JMP(BPF_JSET
));
3284 * AND that with the checks done for data frames.
3289 * If the high-order bit of the type value is 0, this
3290 * is a management frame.
3291 * I.e, check "!(link[0] & 0x08)".
3293 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3294 b2
= new_block(JMP(BPF_JSET
));
3300 * For management frames, the DA is at 4.
3302 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3306 * OR that with the checks done for data frames.
3307 * That gives the checks done for management and
3313 * If the low-order bit of the type value is 1,
3314 * this is either a control frame or a frame
3315 * with a reserved type, and thus not a
3318 * I.e., check "!(link[0] & 0x04)".
3320 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3321 b1
= new_block(JMP(BPF_JSET
));
3327 * AND that with the checks for data and management
3334 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3335 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3341 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3342 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3351 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3352 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3353 * as the RFC states.)
3355 static struct block
*
3356 gen_ipfchostop(eaddr
, dir
)
3357 register const u_char
*eaddr
;
3360 register struct block
*b0
, *b1
;
3364 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3367 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3370 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3371 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3377 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3378 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3387 * This is quite tricky because there may be pad bytes in front of the
3388 * DECNET header, and then there are two possible data packet formats that
3389 * carry both src and dst addresses, plus 5 packet types in a format that
3390 * carries only the src node, plus 2 types that use a different format and
3391 * also carry just the src node.
3395 * Instead of doing those all right, we just look for data packets with
3396 * 0 or 1 bytes of padding. If you want to look at other packets, that
3397 * will require a lot more hacking.
3399 * To add support for filtering on DECNET "areas" (network numbers)
3400 * one would want to add a "mask" argument to this routine. That would
3401 * make the filter even more inefficient, although one could be clever
3402 * and not generate masking instructions if the mask is 0xFFFF.
3404 static struct block
*
3405 gen_dnhostop(addr
, dir
)
3409 struct block
*b0
, *b1
, *b2
, *tmp
;
3410 u_int offset_lh
; /* offset if long header is received */
3411 u_int offset_sh
; /* offset if short header is received */
3416 offset_sh
= 1; /* follows flags */
3417 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3421 offset_sh
= 3; /* follows flags, dstnode */
3422 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3426 /* Inefficient because we do our Calvinball dance twice */
3427 b0
= gen_dnhostop(addr
, Q_SRC
);
3428 b1
= gen_dnhostop(addr
, Q_DST
);
3434 /* Inefficient because we do our Calvinball dance twice */
3435 b0
= gen_dnhostop(addr
, Q_SRC
);
3436 b1
= gen_dnhostop(addr
, Q_DST
);
3441 bpf_error("ISO host filtering not implemented");
3446 b0
= gen_linktype(ETHERTYPE_DN
);
3447 /* Check for pad = 1, long header case */
3448 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3449 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3450 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3451 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3453 /* Check for pad = 0, long header case */
3454 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3455 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3458 /* Check for pad = 1, short header case */
3459 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3460 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3461 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3464 /* Check for pad = 0, short header case */
3465 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3466 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3470 /* Combine with test for linktype */
3476 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3477 * test the bottom-of-stack bit, and then check the version number
3478 * field in the IP header.
3480 static struct block
*
3481 gen_mpls_linktype(proto
)
3484 struct block
*b0
, *b1
;
3489 /* match the bottom-of-stack bit */
3490 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3491 /* match the IPv4 version number */
3492 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3497 /* match the bottom-of-stack bit */
3498 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3499 /* match the IPv4 version number */
3500 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3509 static struct block
*
3510 gen_host(addr
, mask
, proto
, dir
, type
)
3517 struct block
*b0
, *b1
;
3518 const char *typestr
;
3528 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3530 * Only check for non-IPv4 addresses if we're not
3531 * checking MPLS-encapsulated packets.
3533 if (label_stack_depth
== 0) {
3534 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3536 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3542 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3545 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3548 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3551 bpf_error("'tcp' modifier applied to %s", typestr
);
3554 bpf_error("'sctp' modifier applied to %s", typestr
);
3557 bpf_error("'udp' modifier applied to %s", typestr
);
3560 bpf_error("'icmp' modifier applied to %s", typestr
);
3563 bpf_error("'igmp' modifier applied to %s", typestr
);
3566 bpf_error("'igrp' modifier applied to %s", typestr
);
3569 bpf_error("'pim' modifier applied to %s", typestr
);
3572 bpf_error("'vrrp' modifier applied to %s", typestr
);
3575 bpf_error("ATALK host filtering not implemented");
3578 bpf_error("AARP host filtering not implemented");
3581 return gen_dnhostop(addr
, dir
);
3584 bpf_error("SCA host filtering not implemented");
3587 bpf_error("LAT host filtering not implemented");
3590 bpf_error("MOPDL host filtering not implemented");
3593 bpf_error("MOPRC host filtering not implemented");
3597 bpf_error("'ip6' modifier applied to ip host");
3600 bpf_error("'icmp6' modifier applied to %s", typestr
);
3604 bpf_error("'ah' modifier applied to %s", typestr
);
3607 bpf_error("'esp' modifier applied to %s", typestr
);
3610 bpf_error("ISO host filtering not implemented");
3613 bpf_error("'esis' modifier applied to %s", typestr
);
3616 bpf_error("'isis' modifier applied to %s", typestr
);
3619 bpf_error("'clnp' modifier applied to %s", typestr
);
3622 bpf_error("'stp' modifier applied to %s", typestr
);
3625 bpf_error("IPX host filtering not implemented");
3628 bpf_error("'netbeui' modifier applied to %s", typestr
);
3631 bpf_error("'radio' modifier applied to %s", typestr
);
3640 static struct block
*
3641 gen_host6(addr
, mask
, proto
, dir
, type
)
3642 struct in6_addr
*addr
;
3643 struct in6_addr
*mask
;
3648 const char *typestr
;
3658 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3661 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3664 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3667 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3670 bpf_error("'sctp' modifier applied to %s", typestr
);
3673 bpf_error("'tcp' modifier applied to %s", typestr
);
3676 bpf_error("'udp' modifier applied to %s", typestr
);
3679 bpf_error("'icmp' modifier applied to %s", typestr
);
3682 bpf_error("'igmp' modifier applied to %s", typestr
);
3685 bpf_error("'igrp' modifier applied to %s", typestr
);
3688 bpf_error("'pim' modifier applied to %s", typestr
);
3691 bpf_error("'vrrp' modifier applied to %s", typestr
);
3694 bpf_error("ATALK host filtering not implemented");
3697 bpf_error("AARP host filtering not implemented");
3700 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3703 bpf_error("SCA host filtering not implemented");
3706 bpf_error("LAT host filtering not implemented");
3709 bpf_error("MOPDL host filtering not implemented");
3712 bpf_error("MOPRC host filtering not implemented");
3715 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3718 bpf_error("'icmp6' modifier applied to %s", typestr
);
3721 bpf_error("'ah' modifier applied to %s", typestr
);
3724 bpf_error("'esp' modifier applied to %s", typestr
);
3727 bpf_error("ISO host filtering not implemented");
3730 bpf_error("'esis' modifier applied to %s", typestr
);
3733 bpf_error("'isis' modifier applied to %s", typestr
);
3736 bpf_error("'clnp' modifier applied to %s", typestr
);
3739 bpf_error("'stp' modifier applied to %s", typestr
);
3742 bpf_error("IPX host filtering not implemented");
3745 bpf_error("'netbeui' modifier applied to %s", typestr
);
3748 bpf_error("'radio' modifier applied to %s", typestr
);
3758 static struct block
*
3759 gen_gateway(eaddr
, alist
, proto
, dir
)
3760 const u_char
*eaddr
;
3761 bpf_u_int32
**alist
;
3765 struct block
*b0
, *b1
, *tmp
;
3768 bpf_error("direction applied to 'gateway'");
3777 b0
= gen_ehostop(eaddr
, Q_OR
);
3780 b0
= gen_fhostop(eaddr
, Q_OR
);
3783 b0
= gen_thostop(eaddr
, Q_OR
);
3785 case DLT_IEEE802_11
:
3786 case DLT_IEEE802_11_RADIO_AVS
:
3788 case DLT_IEEE802_11_RADIO
:
3789 case DLT_PRISM_HEADER
:
3790 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3795 * Check that the packet doesn't begin with an
3796 * LE Control marker. (We've already generated
3799 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
3804 * Now check the MAC address.
3806 b0
= gen_ehostop(eaddr
, Q_OR
);
3810 case DLT_IP_OVER_FC
:
3811 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3815 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3817 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3819 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3828 bpf_error("illegal modifier of 'gateway'");
3834 gen_proto_abbrev(proto
)
3843 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3845 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3851 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3853 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3859 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3861 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3867 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3870 #ifndef IPPROTO_IGMP
3871 #define IPPROTO_IGMP 2
3875 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3878 #ifndef IPPROTO_IGRP
3879 #define IPPROTO_IGRP 9
3882 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3886 #define IPPROTO_PIM 103
3890 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3892 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3897 #ifndef IPPROTO_VRRP
3898 #define IPPROTO_VRRP 112
3902 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3906 b1
= gen_linktype(ETHERTYPE_IP
);
3910 b1
= gen_linktype(ETHERTYPE_ARP
);
3914 b1
= gen_linktype(ETHERTYPE_REVARP
);
3918 bpf_error("link layer applied in wrong context");
3921 b1
= gen_linktype(ETHERTYPE_ATALK
);
3925 b1
= gen_linktype(ETHERTYPE_AARP
);
3929 b1
= gen_linktype(ETHERTYPE_DN
);
3933 b1
= gen_linktype(ETHERTYPE_SCA
);
3937 b1
= gen_linktype(ETHERTYPE_LAT
);
3941 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3945 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3950 b1
= gen_linktype(ETHERTYPE_IPV6
);
3953 #ifndef IPPROTO_ICMPV6
3954 #define IPPROTO_ICMPV6 58
3957 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3962 #define IPPROTO_AH 51
3965 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3967 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3973 #define IPPROTO_ESP 50
3976 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3978 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3984 b1
= gen_linktype(LLCSAP_ISONS
);
3988 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3992 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3995 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3996 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3997 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3999 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4001 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4003 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4007 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4008 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4009 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4011 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4013 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4015 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4019 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4020 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4021 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4023 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4028 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4029 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4034 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4035 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4037 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4039 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4044 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4045 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4050 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4051 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4056 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4060 b1
= gen_linktype(LLCSAP_8021D
);
4064 b1
= gen_linktype(LLCSAP_IPX
);
4068 b1
= gen_linktype(LLCSAP_NETBEUI
);
4072 bpf_error("'radio' is not a valid protocol type");
4080 static struct block
*
4087 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4088 b
= new_block(JMP(BPF_JSET
));
4097 * Generate a comparison to a port value in the transport-layer header
4098 * at the specified offset from the beginning of that header.
4100 * XXX - this handles a variable-length prefix preceding the link-layer
4101 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4102 * variable-length link-layer headers (such as Token Ring or 802.11
4105 static struct block
*
4106 gen_portatom(off
, v
)
4110 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4114 static struct block
*
4115 gen_portatom6(off
, v
)
4119 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4124 gen_portop(port
, proto
, dir
)
4125 int port
, proto
, dir
;
4127 struct block
*b0
, *b1
, *tmp
;
4129 /* ip proto 'proto' */
4130 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4136 b1
= gen_portatom(0, (bpf_int32
)port
);
4140 b1
= gen_portatom(2, (bpf_int32
)port
);
4145 tmp
= gen_portatom(0, (bpf_int32
)port
);
4146 b1
= gen_portatom(2, (bpf_int32
)port
);
4151 tmp
= gen_portatom(0, (bpf_int32
)port
);
4152 b1
= gen_portatom(2, (bpf_int32
)port
);
4164 static struct block
*
4165 gen_port(port
, ip_proto
, dir
)
4170 struct block
*b0
, *b1
, *tmp
;
4175 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4176 * not LLC encapsulation with LLCSAP_IP.
4178 * For IEEE 802 networks - which includes 802.5 token ring
4179 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4180 * says that SNAP encapsulation is used, not LLC encapsulation
4183 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4184 * RFC 2225 say that SNAP encapsulation is used, not LLC
4185 * encapsulation with LLCSAP_IP.
4187 * So we always check for ETHERTYPE_IP.
4189 b0
= gen_linktype(ETHERTYPE_IP
);
4195 b1
= gen_portop(port
, ip_proto
, dir
);
4199 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4200 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4202 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4215 gen_portop6(port
, proto
, dir
)
4216 int port
, proto
, dir
;
4218 struct block
*b0
, *b1
, *tmp
;
4220 /* ip6 proto 'proto' */
4221 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4225 b1
= gen_portatom6(0, (bpf_int32
)port
);
4229 b1
= gen_portatom6(2, (bpf_int32
)port
);
4234 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4235 b1
= gen_portatom6(2, (bpf_int32
)port
);
4240 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4241 b1
= gen_portatom6(2, (bpf_int32
)port
);
4253 static struct block
*
4254 gen_port6(port
, ip_proto
, dir
)
4259 struct block
*b0
, *b1
, *tmp
;
4261 /* link proto ip6 */
4262 b0
= gen_linktype(ETHERTYPE_IPV6
);
4268 b1
= gen_portop6(port
, ip_proto
, dir
);
4272 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4273 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4275 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4287 /* gen_portrange code */
4288 static struct block
*
4289 gen_portrangeatom(off
, v1
, v2
)
4293 struct block
*b1
, *b2
;
4297 * Reverse the order of the ports, so v1 is the lower one.
4306 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4307 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4315 gen_portrangeop(port1
, port2
, proto
, dir
)
4320 struct block
*b0
, *b1
, *tmp
;
4322 /* ip proto 'proto' */
4323 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4329 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4333 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4338 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4339 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4344 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4345 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4357 static struct block
*
4358 gen_portrange(port1
, port2
, ip_proto
, dir
)
4363 struct block
*b0
, *b1
, *tmp
;
4366 b0
= gen_linktype(ETHERTYPE_IP
);
4372 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4376 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4377 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4379 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4391 static struct block
*
4392 gen_portrangeatom6(off
, v1
, v2
)
4396 struct block
*b1
, *b2
;
4400 * Reverse the order of the ports, so v1 is the lower one.
4409 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4410 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4418 gen_portrangeop6(port1
, port2
, proto
, dir
)
4423 struct block
*b0
, *b1
, *tmp
;
4425 /* ip6 proto 'proto' */
4426 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4430 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4434 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4439 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4440 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4445 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4446 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4458 static struct block
*
4459 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4464 struct block
*b0
, *b1
, *tmp
;
4466 /* link proto ip6 */
4467 b0
= gen_linktype(ETHERTYPE_IPV6
);
4473 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4477 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4478 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4480 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4493 lookup_proto(name
, proto
)
4494 register const char *name
;
4504 v
= pcap_nametoproto(name
);
4505 if (v
== PROTO_UNDEF
)
4506 bpf_error("unknown ip proto '%s'", name
);
4510 /* XXX should look up h/w protocol type based on linktype */
4511 v
= pcap_nametoeproto(name
);
4512 if (v
== PROTO_UNDEF
) {
4513 v
= pcap_nametollc(name
);
4514 if (v
== PROTO_UNDEF
)
4515 bpf_error("unknown ether proto '%s'", name
);
4520 if (strcmp(name
, "esis") == 0)
4522 else if (strcmp(name
, "isis") == 0)
4524 else if (strcmp(name
, "clnp") == 0)
4527 bpf_error("unknown osi proto '%s'", name
);
4547 static struct block
*
4548 gen_protochain(v
, proto
, dir
)
4553 #ifdef NO_PROTOCHAIN
4554 return gen_proto(v
, proto
, dir
);
4556 struct block
*b0
, *b
;
4557 struct slist
*s
[100];
4558 int fix2
, fix3
, fix4
, fix5
;
4559 int ahcheck
, again
, end
;
4561 int reg2
= alloc_reg();
4563 memset(s
, 0, sizeof(s
));
4564 fix2
= fix3
= fix4
= fix5
= 0;
4571 b0
= gen_protochain(v
, Q_IP
, dir
);
4572 b
= gen_protochain(v
, Q_IPV6
, dir
);
4576 bpf_error("bad protocol applied for 'protochain'");
4581 * We don't handle variable-length radiotap here headers yet.
4582 * We might want to add BPF instructions to do the protochain
4583 * work, to simplify that and, on platforms that have a BPF
4584 * interpreter with the new instructions, let the filtering
4585 * be done in the kernel. (We already require a modified BPF
4586 * engine to do the protochain stuff, to support backward
4587 * branches, and backward branch support is unlikely to appear
4588 * in kernel BPF engines.)
4590 if (linktype
== DLT_IEEE802_11_RADIO
)
4591 bpf_error("'protochain' not supported with radiotap headers");
4593 if (linktype
== DLT_PPI
)
4594 bpf_error("'protochain' not supported with PPI headers");
4596 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4599 * s[0] is a dummy entry to protect other BPF insn from damage
4600 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4601 * hard to find interdependency made by jump table fixup.
4604 s
[i
] = new_stmt(0); /*dummy*/
4609 b0
= gen_linktype(ETHERTYPE_IP
);
4612 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4613 s
[i
]->s
.k
= off_ll
+ off_nl
+ 9;
4615 /* X = ip->ip_hl << 2 */
4616 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4617 s
[i
]->s
.k
= off_ll
+ off_nl
;
4622 b0
= gen_linktype(ETHERTYPE_IPV6
);
4624 /* A = ip6->ip_nxt */
4625 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4626 s
[i
]->s
.k
= off_ll
+ off_nl
+ 6;
4628 /* X = sizeof(struct ip6_hdr) */
4629 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4635 bpf_error("unsupported proto to gen_protochain");
4639 /* again: if (A == v) goto end; else fall through; */
4641 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4643 s
[i
]->s
.jt
= NULL
; /*later*/
4644 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4648 #ifndef IPPROTO_NONE
4649 #define IPPROTO_NONE 59
4651 /* if (A == IPPROTO_NONE) goto end */
4652 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4653 s
[i
]->s
.jt
= NULL
; /*later*/
4654 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4655 s
[i
]->s
.k
= IPPROTO_NONE
;
4656 s
[fix5
]->s
.jf
= s
[i
];
4661 if (proto
== Q_IPV6
) {
4662 int v6start
, v6end
, v6advance
, j
;
4665 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4666 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4667 s
[i
]->s
.jt
= NULL
; /*later*/
4668 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4669 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4670 s
[fix2
]->s
.jf
= s
[i
];
4672 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4673 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4674 s
[i
]->s
.jt
= NULL
; /*later*/
4675 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4676 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4678 /* if (A == IPPROTO_ROUTING) goto v6advance */
4679 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4680 s
[i
]->s
.jt
= NULL
; /*later*/
4681 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4682 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4684 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4685 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4686 s
[i
]->s
.jt
= NULL
; /*later*/
4687 s
[i
]->s
.jf
= NULL
; /*later*/
4688 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4699 * X = X + (P[X + 1] + 1) * 8;
4702 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4704 /* A = P[X + packet head] */
4705 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4706 s
[i
]->s
.k
= off_ll
+ off_nl
;
4709 s
[i
] = new_stmt(BPF_ST
);
4713 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4716 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4720 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4722 /* A = P[X + packet head]; */
4723 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4724 s
[i
]->s
.k
= off_ll
+ off_nl
;
4727 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4731 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4735 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4738 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4742 /* goto again; (must use BPF_JA for backward jump) */
4743 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4744 s
[i
]->s
.k
= again
- i
- 1;
4745 s
[i
- 1]->s
.jf
= s
[i
];
4749 for (j
= v6start
; j
<= v6end
; j
++)
4750 s
[j
]->s
.jt
= s
[v6advance
];
4755 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4757 s
[fix2
]->s
.jf
= s
[i
];
4763 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4764 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4765 s
[i
]->s
.jt
= NULL
; /*later*/
4766 s
[i
]->s
.jf
= NULL
; /*later*/
4767 s
[i
]->s
.k
= IPPROTO_AH
;
4769 s
[fix3
]->s
.jf
= s
[ahcheck
];
4776 * X = X + (P[X + 1] + 2) * 4;
4779 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4781 /* A = P[X + packet head]; */
4782 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4783 s
[i
]->s
.k
= off_ll
+ off_nl
;
4786 s
[i
] = new_stmt(BPF_ST
);
4790 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4793 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4797 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4799 /* A = P[X + packet head] */
4800 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4801 s
[i
]->s
.k
= off_ll
+ off_nl
;
4804 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4808 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4812 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4815 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4819 /* goto again; (must use BPF_JA for backward jump) */
4820 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4821 s
[i
]->s
.k
= again
- i
- 1;
4826 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4828 s
[fix2
]->s
.jt
= s
[end
];
4829 s
[fix4
]->s
.jf
= s
[end
];
4830 s
[fix5
]->s
.jt
= s
[end
];
4837 for (i
= 0; i
< max
- 1; i
++)
4838 s
[i
]->next
= s
[i
+ 1];
4839 s
[max
- 1]->next
= NULL
;
4844 b
= new_block(JMP(BPF_JEQ
));
4845 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4856 * Generate code that checks whether the packet is a packet for protocol
4857 * <proto> and whether the type field in that protocol's header has
4858 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4859 * IP packet and checks the protocol number in the IP header against <v>.
4861 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4862 * against Q_IP and Q_IPV6.
4864 static struct block
*
4865 gen_proto(v
, proto
, dir
)
4870 struct block
*b0
, *b1
;
4872 if (dir
!= Q_DEFAULT
)
4873 bpf_error("direction applied to 'proto'");
4878 b0
= gen_proto(v
, Q_IP
, dir
);
4879 b1
= gen_proto(v
, Q_IPV6
, dir
);
4887 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4888 * not LLC encapsulation with LLCSAP_IP.
4890 * For IEEE 802 networks - which includes 802.5 token ring
4891 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4892 * says that SNAP encapsulation is used, not LLC encapsulation
4895 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4896 * RFC 2225 say that SNAP encapsulation is used, not LLC
4897 * encapsulation with LLCSAP_IP.
4899 * So we always check for ETHERTYPE_IP.
4901 b0
= gen_linktype(ETHERTYPE_IP
);
4903 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4905 b1
= gen_protochain(v
, Q_IP
);
4915 * Frame Relay packets typically have an OSI
4916 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4917 * generates code to check for all the OSI
4918 * NLPIDs, so calling it and then adding a check
4919 * for the particular NLPID for which we're
4920 * looking is bogus, as we can just check for
4923 * What we check for is the NLPID and a frame
4924 * control field value of UI, i.e. 0x03 followed
4927 * XXX - assumes a 2-byte Frame Relay header with
4928 * DLCI and flags. What if the address is longer?
4930 * XXX - what about SNAP-encapsulated frames?
4932 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4938 * Cisco uses an Ethertype lookalike - for OSI,
4941 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4942 /* OSI in C-HDLC is stuffed with a fudge byte */
4943 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4948 b0
= gen_linktype(LLCSAP_ISONS
);
4949 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4955 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4957 * 4 is the offset of the PDU type relative to the IS-IS
4960 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4965 bpf_error("arp does not encapsulate another protocol");
4969 bpf_error("rarp does not encapsulate another protocol");
4973 bpf_error("atalk encapsulation is not specifiable");
4977 bpf_error("decnet encapsulation is not specifiable");
4981 bpf_error("sca does not encapsulate another protocol");
4985 bpf_error("lat does not encapsulate another protocol");
4989 bpf_error("moprc does not encapsulate another protocol");
4993 bpf_error("mopdl does not encapsulate another protocol");
4997 return gen_linktype(v
);
5000 bpf_error("'udp proto' is bogus");
5004 bpf_error("'tcp proto' is bogus");
5008 bpf_error("'sctp proto' is bogus");
5012 bpf_error("'icmp proto' is bogus");
5016 bpf_error("'igmp proto' is bogus");
5020 bpf_error("'igrp proto' is bogus");
5024 bpf_error("'pim proto' is bogus");
5028 bpf_error("'vrrp proto' is bogus");
5033 b0
= gen_linktype(ETHERTYPE_IPV6
);
5035 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5037 b1
= gen_protochain(v
, Q_IPV6
);
5043 bpf_error("'icmp6 proto' is bogus");
5047 bpf_error("'ah proto' is bogus");
5050 bpf_error("'ah proto' is bogus");
5053 bpf_error("'stp proto' is bogus");
5056 bpf_error("'ipx proto' is bogus");
5059 bpf_error("'netbeui proto' is bogus");
5062 bpf_error("'radio proto' is bogus");
5073 register const char *name
;
5076 int proto
= q
.proto
;
5080 bpf_u_int32 mask
, addr
;
5082 bpf_u_int32
**alist
;
5085 struct sockaddr_in
*sin4
;
5086 struct sockaddr_in6
*sin6
;
5087 struct addrinfo
*res
, *res0
;
5088 struct in6_addr mask128
;
5090 struct block
*b
, *tmp
;
5091 int port
, real_proto
;
5097 addr
= pcap_nametonetaddr(name
);
5099 bpf_error("unknown network '%s'", name
);
5100 /* Left justify network addr and calculate its network mask */
5102 while (addr
&& (addr
& 0xff000000) == 0) {
5106 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5110 if (proto
== Q_LINK
) {
5114 eaddr
= pcap_ether_hostton(name
);
5117 "unknown ether host '%s'", name
);
5118 b
= gen_ehostop(eaddr
, dir
);
5123 eaddr
= pcap_ether_hostton(name
);
5126 "unknown FDDI host '%s'", name
);
5127 b
= gen_fhostop(eaddr
, dir
);
5132 eaddr
= pcap_ether_hostton(name
);
5135 "unknown token ring host '%s'", name
);
5136 b
= gen_thostop(eaddr
, dir
);
5140 case DLT_IEEE802_11
:
5141 case DLT_IEEE802_11_RADIO_AVS
:
5142 case DLT_IEEE802_11_RADIO
:
5143 case DLT_PRISM_HEADER
:
5145 eaddr
= pcap_ether_hostton(name
);
5148 "unknown 802.11 host '%s'", name
);
5149 b
= gen_wlanhostop(eaddr
, dir
);
5153 case DLT_IP_OVER_FC
:
5154 eaddr
= pcap_ether_hostton(name
);
5157 "unknown Fibre Channel host '%s'", name
);
5158 b
= gen_ipfchostop(eaddr
, dir
);
5167 * Check that the packet doesn't begin
5168 * with an LE Control marker. (We've
5169 * already generated a test for LANE.)
5171 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5175 eaddr
= pcap_ether_hostton(name
);
5178 "unknown ether host '%s'", name
);
5179 b
= gen_ehostop(eaddr
, dir
);
5185 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5186 } else if (proto
== Q_DECNET
) {
5187 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5189 * I don't think DECNET hosts can be multihomed, so
5190 * there is no need to build up a list of addresses
5192 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5195 alist
= pcap_nametoaddr(name
);
5196 if (alist
== NULL
|| *alist
== NULL
)
5197 bpf_error("unknown host '%s'", name
);
5199 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5201 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5203 tmp
= gen_host(**alist
++, 0xffffffff,
5204 tproto
, dir
, q
.addr
);
5210 memset(&mask128
, 0xff, sizeof(mask128
));
5211 res0
= res
= pcap_nametoaddrinfo(name
);
5213 bpf_error("unknown host '%s'", name
);
5215 tproto
= tproto6
= proto
;
5216 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5220 for (res
= res0
; res
; res
= res
->ai_next
) {
5221 switch (res
->ai_family
) {
5223 if (tproto
== Q_IPV6
)
5226 sin4
= (struct sockaddr_in
*)
5228 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5229 0xffffffff, tproto
, dir
, q
.addr
);
5232 if (tproto6
== Q_IP
)
5235 sin6
= (struct sockaddr_in6
*)
5237 tmp
= gen_host6(&sin6
->sin6_addr
,
5238 &mask128
, tproto6
, dir
, q
.addr
);
5249 bpf_error("unknown host '%s'%s", name
,
5250 (proto
== Q_DEFAULT
)
5252 : " for specified address family");
5259 if (proto
!= Q_DEFAULT
&&
5260 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5261 bpf_error("illegal qualifier of 'port'");
5262 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5263 bpf_error("unknown port '%s'", name
);
5264 if (proto
== Q_UDP
) {
5265 if (real_proto
== IPPROTO_TCP
)
5266 bpf_error("port '%s' is tcp", name
);
5267 else if (real_proto
== IPPROTO_SCTP
)
5268 bpf_error("port '%s' is sctp", name
);
5270 /* override PROTO_UNDEF */
5271 real_proto
= IPPROTO_UDP
;
5273 if (proto
== Q_TCP
) {
5274 if (real_proto
== IPPROTO_UDP
)
5275 bpf_error("port '%s' is udp", name
);
5277 else if (real_proto
== IPPROTO_SCTP
)
5278 bpf_error("port '%s' is sctp", name
);
5280 /* override PROTO_UNDEF */
5281 real_proto
= IPPROTO_TCP
;
5283 if (proto
== Q_SCTP
) {
5284 if (real_proto
== IPPROTO_UDP
)
5285 bpf_error("port '%s' is udp", name
);
5287 else if (real_proto
== IPPROTO_TCP
)
5288 bpf_error("port '%s' is tcp", name
);
5290 /* override PROTO_UNDEF */
5291 real_proto
= IPPROTO_SCTP
;
5294 return gen_port(port
, real_proto
, dir
);
5296 b
= gen_port(port
, real_proto
, dir
);
5297 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5302 if (proto
!= Q_DEFAULT
&&
5303 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5304 bpf_error("illegal qualifier of 'portrange'");
5305 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5306 bpf_error("unknown port in range '%s'", name
);
5307 if (proto
== Q_UDP
) {
5308 if (real_proto
== IPPROTO_TCP
)
5309 bpf_error("port in range '%s' is tcp", name
);
5310 else if (real_proto
== IPPROTO_SCTP
)
5311 bpf_error("port in range '%s' is sctp", name
);
5313 /* override PROTO_UNDEF */
5314 real_proto
= IPPROTO_UDP
;
5316 if (proto
== Q_TCP
) {
5317 if (real_proto
== IPPROTO_UDP
)
5318 bpf_error("port in range '%s' is udp", name
);
5319 else if (real_proto
== IPPROTO_SCTP
)
5320 bpf_error("port in range '%s' is sctp", name
);
5322 /* override PROTO_UNDEF */
5323 real_proto
= IPPROTO_TCP
;
5325 if (proto
== Q_SCTP
) {
5326 if (real_proto
== IPPROTO_UDP
)
5327 bpf_error("port in range '%s' is udp", name
);
5328 else if (real_proto
== IPPROTO_TCP
)
5329 bpf_error("port in range '%s' is tcp", name
);
5331 /* override PROTO_UNDEF */
5332 real_proto
= IPPROTO_SCTP
;
5335 return gen_portrange(port1
, port2
, real_proto
, dir
);
5337 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5338 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5344 eaddr
= pcap_ether_hostton(name
);
5346 bpf_error("unknown ether host: %s", name
);
5348 alist
= pcap_nametoaddr(name
);
5349 if (alist
== NULL
|| *alist
== NULL
)
5350 bpf_error("unknown host '%s'", name
);
5351 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5355 bpf_error("'gateway' not supported in this configuration");
5359 real_proto
= lookup_proto(name
, proto
);
5360 if (real_proto
>= 0)
5361 return gen_proto(real_proto
, proto
, dir
);
5363 bpf_error("unknown protocol: %s", name
);
5366 real_proto
= lookup_proto(name
, proto
);
5367 if (real_proto
>= 0)
5368 return gen_protochain(real_proto
, proto
, dir
);
5370 bpf_error("unknown protocol: %s", name
);
5382 gen_mcode(s1
, s2
, masklen
, q
)
5383 register const char *s1
, *s2
;
5384 register int masklen
;
5387 register int nlen
, mlen
;
5390 nlen
= __pcap_atoin(s1
, &n
);
5391 /* Promote short ipaddr */
5395 mlen
= __pcap_atoin(s2
, &m
);
5396 /* Promote short ipaddr */
5399 bpf_error("non-network bits set in \"%s mask %s\"",
5402 /* Convert mask len to mask */
5404 bpf_error("mask length must be <= 32");
5407 * X << 32 is not guaranteed by C to be 0; it's
5412 m
= 0xffffffff << (32 - masklen
);
5414 bpf_error("non-network bits set in \"%s/%d\"",
5421 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5424 bpf_error("Mask syntax for networks only");
5433 register const char *s
;
5438 int proto
= q
.proto
;
5444 else if (q
.proto
== Q_DECNET
)
5445 vlen
= __pcap_atodn(s
, &v
);
5447 vlen
= __pcap_atoin(s
, &v
);
5454 if (proto
== Q_DECNET
)
5455 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5456 else if (proto
== Q_LINK
) {
5457 bpf_error("illegal link layer address");
5460 if (s
== NULL
&& q
.addr
== Q_NET
) {
5461 /* Promote short net number */
5462 while (v
&& (v
& 0xff000000) == 0) {
5467 /* Promote short ipaddr */
5471 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5476 proto
= IPPROTO_UDP
;
5477 else if (proto
== Q_TCP
)
5478 proto
= IPPROTO_TCP
;
5479 else if (proto
== Q_SCTP
)
5480 proto
= IPPROTO_SCTP
;
5481 else if (proto
== Q_DEFAULT
)
5482 proto
= PROTO_UNDEF
;
5484 bpf_error("illegal qualifier of 'port'");
5487 return gen_port((int)v
, proto
, dir
);
5491 b
= gen_port((int)v
, proto
, dir
);
5492 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5499 proto
= IPPROTO_UDP
;
5500 else if (proto
== Q_TCP
)
5501 proto
= IPPROTO_TCP
;
5502 else if (proto
== Q_SCTP
)
5503 proto
= IPPROTO_SCTP
;
5504 else if (proto
== Q_DEFAULT
)
5505 proto
= PROTO_UNDEF
;
5507 bpf_error("illegal qualifier of 'portrange'");
5510 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5514 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5515 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5521 bpf_error("'gateway' requires a name");
5525 return gen_proto((int)v
, proto
, dir
);
5528 return gen_protochain((int)v
, proto
, dir
);
5543 gen_mcode6(s1
, s2
, masklen
, q
)
5544 register const char *s1
, *s2
;
5545 register int masklen
;
5548 struct addrinfo
*res
;
5549 struct in6_addr
*addr
;
5550 struct in6_addr mask
;
5555 bpf_error("no mask %s supported", s2
);
5557 res
= pcap_nametoaddrinfo(s1
);
5559 bpf_error("invalid ip6 address %s", s1
);
5561 bpf_error("%s resolved to multiple address", s1
);
5562 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5564 if (sizeof(mask
) * 8 < masklen
)
5565 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5566 memset(&mask
, 0, sizeof(mask
));
5567 memset(&mask
, 0xff, masklen
/ 8);
5569 mask
.s6_addr
[masklen
/ 8] =
5570 (0xff << (8 - masklen
% 8)) & 0xff;
5573 a
= (u_int32_t
*)addr
;
5574 m
= (u_int32_t
*)&mask
;
5575 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5576 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5577 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5585 bpf_error("Mask syntax for networks only");
5589 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5594 bpf_error("invalid qualifier against IPv6 address");
5603 register const u_char
*eaddr
;
5606 struct block
*b
, *tmp
;
5608 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5611 return gen_ehostop(eaddr
, (int)q
.dir
);
5613 return gen_fhostop(eaddr
, (int)q
.dir
);
5615 return gen_thostop(eaddr
, (int)q
.dir
);
5616 case DLT_IEEE802_11
:
5617 case DLT_IEEE802_11_RADIO_AVS
:
5618 case DLT_IEEE802_11_RADIO
:
5619 case DLT_PRISM_HEADER
:
5621 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5625 * Check that the packet doesn't begin with an
5626 * LE Control marker. (We've already generated
5629 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5634 * Now check the MAC address.
5636 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5641 case DLT_IP_OVER_FC
:
5642 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5644 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5648 bpf_error("ethernet address used in non-ether expression");
5655 struct slist
*s0
, *s1
;
5658 * This is definitely not the best way to do this, but the
5659 * lists will rarely get long.
5666 static struct slist
*
5672 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5677 static struct slist
*
5683 s
= new_stmt(BPF_LD
|BPF_MEM
);
5689 * Modify "index" to use the value stored into its register as an
5690 * offset relative to the beginning of the header for the protocol
5691 * "proto", and allocate a register and put an item "size" bytes long
5692 * (1, 2, or 4) at that offset into that register, making it the register
5696 gen_load(proto
, inst
, size
)
5701 struct slist
*s
, *tmp
;
5703 int regno
= alloc_reg();
5705 free_reg(inst
->regno
);
5709 bpf_error("data size must be 1, 2, or 4");
5725 bpf_error("unsupported index operation");
5729 * The offset is relative to the beginning of the packet
5730 * data, if we have a radio header. (If we don't, this
5733 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5734 linktype
!= DLT_IEEE802_11_RADIO
&&
5735 linktype
!= DLT_PRISM_HEADER
)
5736 bpf_error("radio information not present in capture");
5739 * Load into the X register the offset computed into the
5740 * register specifed by "index".
5742 s
= xfer_to_x(inst
);
5745 * Load the item at that offset.
5747 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5749 sappend(inst
->s
, s
);
5754 * The offset is relative to the beginning of
5755 * the link-layer header.
5757 * XXX - what about ATM LANE? Should the index be
5758 * relative to the beginning of the AAL5 frame, so
5759 * that 0 refers to the beginning of the LE Control
5760 * field, or relative to the beginning of the LAN
5761 * frame, so that 0 refers, for Ethernet LANE, to
5762 * the beginning of the destination address?
5764 s
= gen_llprefixlen();
5767 * If "s" is non-null, it has code to arrange that the
5768 * X register contains the length of the prefix preceding
5769 * the link-layer header. Add to it the offset computed
5770 * into the register specified by "index", and move that
5771 * into the X register. Otherwise, just load into the X
5772 * register the offset computed into the register specifed
5776 sappend(s
, xfer_to_a(inst
));
5777 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5778 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5780 s
= xfer_to_x(inst
);
5783 * Load the item at the sum of the offset we've put in the
5784 * X register and the offset of the start of the link
5785 * layer header (which is 0 if the radio header is
5786 * variable-length; that header length is what we put
5787 * into the X register and then added to the index).
5789 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5792 sappend(inst
->s
, s
);
5808 * The offset is relative to the beginning of
5809 * the network-layer header.
5810 * XXX - are there any cases where we want
5813 s
= gen_llprefixlen();
5816 * If "s" is non-null, it has code to arrange that the
5817 * X register contains the length of the prefix preceding
5818 * the link-layer header. Add to it the offset computed
5819 * into the register specified by "index", and move that
5820 * into the X register. Otherwise, just load into the X
5821 * register the offset computed into the register specifed
5825 sappend(s
, xfer_to_a(inst
));
5826 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5827 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5829 s
= xfer_to_x(inst
);
5832 * Load the item at the sum of the offset we've put in the
5833 * X register, the offset of the start of the network
5834 * layer header, and the offset of the start of the link
5835 * layer header (which is 0 if the radio header is
5836 * variable-length; that header length is what we put
5837 * into the X register and then added to the index).
5839 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5840 tmp
->s
.k
= off_ll
+ off_nl
;
5842 sappend(inst
->s
, s
);
5845 * Do the computation only if the packet contains
5846 * the protocol in question.
5848 b
= gen_proto_abbrev(proto
);
5850 gen_and(inst
->b
, b
);
5863 * The offset is relative to the beginning of
5864 * the transport-layer header.
5866 * Load the X register with the length of the IPv4 header
5867 * (plus the offset of the link-layer header, if it's
5868 * a variable-length header), in bytes.
5870 * XXX - are there any cases where we want
5872 * XXX - we should, if we're built with
5873 * IPv6 support, generate code to load either
5874 * IPv4, IPv6, or both, as appropriate.
5876 s
= gen_loadx_iphdrlen();
5879 * The X register now contains the sum of the length
5880 * of any variable-length header preceding the link-layer
5881 * header and the length of the network-layer header.
5882 * Load into the A register the offset relative to
5883 * the beginning of the transport layer header,
5884 * add the X register to that, move that to the
5885 * X register, and load with an offset from the
5886 * X register equal to the offset of the network
5887 * layer header relative to the beginning of
5888 * the link-layer header plus the length of any
5889 * fixed-length header preceding the link-layer
5892 sappend(s
, xfer_to_a(inst
));
5893 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5894 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5895 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5896 tmp
->s
.k
= off_ll
+ off_nl
;
5897 sappend(inst
->s
, s
);
5900 * Do the computation only if the packet contains
5901 * the protocol in question - which is true only
5902 * if this is an IP datagram and is the first or
5903 * only fragment of that datagram.
5905 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5907 gen_and(inst
->b
, b
);
5909 gen_and(gen_proto_abbrev(Q_IP
), b
);
5915 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5919 inst
->regno
= regno
;
5920 s
= new_stmt(BPF_ST
);
5922 sappend(inst
->s
, s
);
5928 gen_relation(code
, a0
, a1
, reversed
)
5930 struct arth
*a0
, *a1
;
5933 struct slist
*s0
, *s1
, *s2
;
5934 struct block
*b
, *tmp
;
5938 if (code
== BPF_JEQ
) {
5939 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5940 b
= new_block(JMP(code
));
5944 b
= new_block(BPF_JMP
|code
|BPF_X
);
5950 sappend(a0
->s
, a1
->s
);
5954 free_reg(a0
->regno
);
5955 free_reg(a1
->regno
);
5957 /* 'and' together protocol checks */
5960 gen_and(a0
->b
, tmp
= a1
->b
);
5976 int regno
= alloc_reg();
5977 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5980 s
= new_stmt(BPF_LD
|BPF_LEN
);
5981 s
->next
= new_stmt(BPF_ST
);
5982 s
->next
->s
.k
= regno
;
5997 a
= (struct arth
*)newchunk(sizeof(*a
));
6001 s
= new_stmt(BPF_LD
|BPF_IMM
);
6003 s
->next
= new_stmt(BPF_ST
);
6019 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6022 s
= new_stmt(BPF_ST
);
6030 gen_arth(code
, a0
, a1
)
6032 struct arth
*a0
, *a1
;
6034 struct slist
*s0
, *s1
, *s2
;
6038 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6043 sappend(a0
->s
, a1
->s
);
6045 free_reg(a0
->regno
);
6046 free_reg(a1
->regno
);
6048 s0
= new_stmt(BPF_ST
);
6049 a0
->regno
= s0
->s
.k
= alloc_reg();
6056 * Here we handle simple allocation of the scratch registers.
6057 * If too many registers are alloc'd, the allocator punts.
6059 static int regused
[BPF_MEMWORDS
];
6063 * Initialize the table of used registers and the current register.
6069 memset(regused
, 0, sizeof regused
);
6073 * Return the next free register.
6078 int n
= BPF_MEMWORDS
;
6081 if (regused
[curreg
])
6082 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6084 regused
[curreg
] = 1;
6088 bpf_error("too many registers needed to evaluate expression");
6094 * Return a register to the table so it can
6104 static struct block
*
6111 s
= new_stmt(BPF_LD
|BPF_LEN
);
6112 b
= new_block(JMP(jmp
));
6123 return gen_len(BPF_JGE
, n
);
6127 * Actually, this is less than or equal.
6135 b
= gen_len(BPF_JGT
, n
);
6142 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6143 * the beginning of the link-layer header.
6144 * XXX - that means you can't test values in the radiotap header, but
6145 * as that header is difficult if not impossible to parse generally
6146 * without a loop, that might not be a severe problem. A new keyword
6147 * "radio" could be added for that, although what you'd really want
6148 * would be a way of testing particular radio header values, which
6149 * would generate code appropriate to the radio header in question.
6152 gen_byteop(op
, idx
, val
)
6163 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6166 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6170 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6174 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6178 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6182 b
= new_block(JMP(BPF_JEQ
));
6189 static u_char abroadcast
[] = { 0x0 };
6192 gen_broadcast(proto
)
6195 bpf_u_int32 hostmask
;
6196 struct block
*b0
, *b1
, *b2
;
6197 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6205 case DLT_ARCNET_LINUX
:
6206 return gen_ahostop(abroadcast
, Q_DST
);
6208 return gen_ehostop(ebroadcast
, Q_DST
);
6210 return gen_fhostop(ebroadcast
, Q_DST
);
6212 return gen_thostop(ebroadcast
, Q_DST
);
6213 case DLT_IEEE802_11
:
6214 case DLT_IEEE802_11_RADIO_AVS
:
6215 case DLT_IEEE802_11_RADIO
:
6217 case DLT_PRISM_HEADER
:
6218 return gen_wlanhostop(ebroadcast
, Q_DST
);
6219 case DLT_IP_OVER_FC
:
6220 return gen_ipfchostop(ebroadcast
, Q_DST
);
6224 * Check that the packet doesn't begin with an
6225 * LE Control marker. (We've already generated
6228 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6233 * Now check the MAC address.
6235 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6241 bpf_error("not a broadcast link");
6246 b0
= gen_linktype(ETHERTYPE_IP
);
6247 hostmask
= ~netmask
;
6248 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6249 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6250 (bpf_int32
)(~0 & hostmask
), hostmask
);
6255 bpf_error("only link-layer/IP broadcast filters supported");
6261 * Generate code to test the low-order bit of a MAC address (that's
6262 * the bottom bit of the *first* byte).
6264 static struct block
*
6265 gen_mac_multicast(offset
)
6268 register struct block
*b0
;
6269 register struct slist
*s
;
6271 /* link[offset] & 1 != 0 */
6272 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6273 b0
= new_block(JMP(BPF_JSET
));
6280 gen_multicast(proto
)
6283 register struct block
*b0
, *b1
, *b2
;
6284 register struct slist
*s
;
6292 case DLT_ARCNET_LINUX
:
6293 /* all ARCnet multicasts use the same address */
6294 return gen_ahostop(abroadcast
, Q_DST
);
6296 /* ether[0] & 1 != 0 */
6297 return gen_mac_multicast(0);
6300 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6302 * XXX - was that referring to bit-order issues?
6304 /* fddi[1] & 1 != 0 */
6305 return gen_mac_multicast(1);
6307 /* tr[2] & 1 != 0 */
6308 return gen_mac_multicast(2);
6309 case DLT_IEEE802_11
:
6310 case DLT_IEEE802_11_RADIO_AVS
:
6312 case DLT_IEEE802_11_RADIO
:
6313 case DLT_PRISM_HEADER
:
6317 * For control frames, there is no DA.
6319 * For management frames, DA is at an
6320 * offset of 4 from the beginning of
6323 * For data frames, DA is at an offset
6324 * of 4 from the beginning of the packet
6325 * if To DS is clear and at an offset of
6326 * 16 from the beginning of the packet
6331 * Generate the tests to be done for data frames.
6333 * First, check for To DS set, i.e. "link[1] & 0x01".
6335 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6336 b1
= new_block(JMP(BPF_JSET
));
6337 b1
->s
.k
= 0x01; /* To DS */
6341 * If To DS is set, the DA is at 16.
6343 b0
= gen_mac_multicast(16);
6347 * Now, check for To DS not set, i.e. check
6348 * "!(link[1] & 0x01)".
6350 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6351 b2
= new_block(JMP(BPF_JSET
));
6352 b2
->s
.k
= 0x01; /* To DS */
6357 * If To DS is not set, the DA is at 4.
6359 b1
= gen_mac_multicast(4);
6363 * Now OR together the last two checks. That gives
6364 * the complete set of checks for data frames.
6369 * Now check for a data frame.
6370 * I.e, check "link[0] & 0x08".
6372 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6373 b1
= new_block(JMP(BPF_JSET
));
6378 * AND that with the checks done for data frames.
6383 * If the high-order bit of the type value is 0, this
6384 * is a management frame.
6385 * I.e, check "!(link[0] & 0x08)".
6387 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6388 b2
= new_block(JMP(BPF_JSET
));
6394 * For management frames, the DA is at 4.
6396 b1
= gen_mac_multicast(4);
6400 * OR that with the checks done for data frames.
6401 * That gives the checks done for management and
6407 * If the low-order bit of the type value is 1,
6408 * this is either a control frame or a frame
6409 * with a reserved type, and thus not a
6412 * I.e., check "!(link[0] & 0x04)".
6414 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6415 b1
= new_block(JMP(BPF_JSET
));
6421 * AND that with the checks for data and management
6426 case DLT_IP_OVER_FC
:
6427 b0
= gen_mac_multicast(2);
6432 * Check that the packet doesn't begin with an
6433 * LE Control marker. (We've already generated
6436 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6440 /* ether[off_mac] & 1 != 0 */
6441 b0
= gen_mac_multicast(off_mac
);
6449 /* Link not known to support multicasts */
6453 b0
= gen_linktype(ETHERTYPE_IP
);
6454 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6460 b0
= gen_linktype(ETHERTYPE_IPV6
);
6461 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6466 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6472 * generate command for inbound/outbound. It's here so we can
6473 * make it link-type specific. 'dir' = 0 implies "inbound",
6474 * = 1 implies "outbound".
6480 register struct block
*b0
;
6483 * Only some data link types support inbound/outbound qualifiers.
6487 b0
= gen_relation(BPF_JEQ
,
6488 gen_load(Q_LINK
, gen_loadi(0), 1),
6496 * Match packets sent by this machine.
6498 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6501 * Match packets sent to this machine.
6502 * (No broadcast or multicast packets, or
6503 * packets sent to some other machine and
6504 * received promiscuously.)
6506 * XXX - packets sent to other machines probably
6507 * shouldn't be matched, but what about broadcast
6508 * or multicast packets we received?
6510 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6514 #ifdef HAVE_NET_PFVAR_H
6516 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6517 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6523 /* match outgoing packets */
6524 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6526 /* match incoming packets */
6527 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6531 case DLT_JUNIPER_MFR
:
6532 case DLT_JUNIPER_MLFR
:
6533 case DLT_JUNIPER_MLPPP
:
6534 case DLT_JUNIPER_ATM1
:
6535 case DLT_JUNIPER_ATM2
:
6536 case DLT_JUNIPER_PPPOE
:
6537 case DLT_JUNIPER_PPPOE_ATM
:
6538 case DLT_JUNIPER_GGSN
:
6539 case DLT_JUNIPER_ES
:
6540 case DLT_JUNIPER_MONITOR
:
6541 case DLT_JUNIPER_SERVICES
:
6542 case DLT_JUNIPER_ETHER
:
6543 case DLT_JUNIPER_PPP
:
6544 case DLT_JUNIPER_FRELAY
:
6545 case DLT_JUNIPER_CHDLC
:
6546 case DLT_JUNIPER_VP
:
6547 case DLT_JUNIPER_ST
:
6548 case DLT_JUNIPER_ISM
:
6549 /* juniper flags (including direction) are stored
6550 * the byte after the 3-byte magic number */
6552 /* match outgoing packets */
6553 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6555 /* match incoming packets */
6556 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6561 bpf_error("inbound/outbound not supported on linktype %d",
6569 #ifdef HAVE_NET_PFVAR_H
6570 /* PF firewall log matched interface */
6572 gen_pf_ifname(const char *ifname
)
6577 if (linktype
!= DLT_PFLOG
) {
6578 bpf_error("ifname supported only on PF linktype");
6581 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6582 off
= offsetof(struct pfloghdr
, ifname
);
6583 if (strlen(ifname
) >= len
) {
6584 bpf_error("ifname interface names can only be %d characters",
6588 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6592 /* PF firewall log ruleset name */
6594 gen_pf_ruleset(char *ruleset
)
6598 if (linktype
!= DLT_PFLOG
) {
6599 bpf_error("ruleset supported only on PF linktype");
6603 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6604 bpf_error("ruleset names can only be %ld characters",
6605 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6609 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6610 strlen(ruleset
), (const u_char
*)ruleset
);
6614 /* PF firewall log rule number */
6620 if (linktype
!= DLT_PFLOG
) {
6621 bpf_error("rnr supported only on PF linktype");
6625 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6630 /* PF firewall log sub-rule number */
6632 gen_pf_srnr(int srnr
)
6636 if (linktype
!= DLT_PFLOG
) {
6637 bpf_error("srnr supported only on PF linktype");
6641 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6646 /* PF firewall log reason code */
6648 gen_pf_reason(int reason
)
6652 if (linktype
!= DLT_PFLOG
) {
6653 bpf_error("reason supported only on PF linktype");
6657 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6662 /* PF firewall log action */
6664 gen_pf_action(int action
)
6668 if (linktype
!= DLT_PFLOG
) {
6669 bpf_error("action supported only on PF linktype");
6673 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6677 #else /* !HAVE_NET_PFVAR_H */
6679 gen_pf_ifname(const char *ifname
)
6681 bpf_error("libpcap was compiled without pf support");
6687 gen_pf_ruleset(char *ruleset
)
6689 bpf_error("libpcap was compiled on a machine without pf support");
6697 bpf_error("libpcap was compiled on a machine without pf support");
6703 gen_pf_srnr(int srnr
)
6705 bpf_error("libpcap was compiled on a machine without pf support");
6711 gen_pf_reason(int reason
)
6713 bpf_error("libpcap was compiled on a machine without pf support");
6719 gen_pf_action(int action
)
6721 bpf_error("libpcap was compiled on a machine without pf support");
6725 #endif /* HAVE_NET_PFVAR_H */
6727 /* IEEE 802.11 wireless header */
6729 gen_p80211_type(int type
, int mask
)
6735 case DLT_IEEE802_11
:
6736 case DLT_PRISM_HEADER
:
6737 case DLT_IEEE802_11_RADIO_AVS
:
6738 case DLT_IEEE802_11_RADIO
:
6739 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
6744 bpf_error("802.11 link-layer types supported only on 802.11");
6752 register const u_char
*eaddr
;
6758 case DLT_ARCNET_LINUX
:
6759 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
6761 return (gen_ahostop(eaddr
, (int)q
.dir
));
6763 bpf_error("ARCnet address used in non-arc expression");
6769 bpf_error("aid supported only on ARCnet");
6772 bpf_error("ARCnet address used in non-arc expression");
6777 static struct block
*
6778 gen_ahostop(eaddr
, dir
)
6779 register const u_char
*eaddr
;
6782 register struct block
*b0
, *b1
;
6785 /* src comes first, different from Ethernet */
6787 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6790 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6793 b0
= gen_ahostop(eaddr
, Q_SRC
);
6794 b1
= gen_ahostop(eaddr
, Q_DST
);
6800 b0
= gen_ahostop(eaddr
, Q_SRC
);
6801 b1
= gen_ahostop(eaddr
, Q_DST
);
6810 * support IEEE 802.1Q VLAN trunk over ethernet
6816 struct block
*b0
, *b1
;
6818 /* can't check for VLAN-encapsulated packets inside MPLS */
6819 if (label_stack_depth
> 0)
6820 bpf_error("no VLAN match after MPLS");
6823 * Change the offsets to point to the type and data fields within
6824 * the VLAN packet. Just increment the offsets, so that we
6825 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6826 * capture VLAN 200 encapsulated within VLAN 100.
6828 * XXX - this is a bit of a kludge. If we were to split the
6829 * compiler into a parser that parses an expression and
6830 * generates an expression tree, and a code generator that
6831 * takes an expression tree (which could come from our
6832 * parser or from some other parser) and generates BPF code,
6833 * we could perhaps make the offsets parameters of routines
6834 * and, in the handler for an "AND" node, pass to subnodes
6835 * other than the VLAN node the adjusted offsets.
6837 * This would mean that "vlan" would, instead of changing the
6838 * behavior of *all* tests after it, change only the behavior
6839 * of tests ANDed with it. That would change the documented
6840 * semantics of "vlan", which might break some expressions.
6841 * However, it would mean that "(vlan and ip) or ip" would check
6842 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6843 * checking only for VLAN-encapsulated IP, so that could still
6844 * be considered worth doing; it wouldn't break expressions
6845 * that are of the form "vlan and ..." or "vlan N and ...",
6846 * which I suspect are the most common expressions involving
6847 * "vlan". "vlan or ..." doesn't necessarily do what the user
6848 * would really want, now, as all the "or ..." tests would
6849 * be done assuming a VLAN, even though the "or" could be viewed
6850 * as meaning "or, if this isn't a VLAN packet...".
6852 orig_linktype
= off_linktype
; /* save original values */
6864 bpf_error("no VLAN support for data link type %d",
6869 /* check for VLAN */
6870 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6872 /* If a specific VLAN is requested, check VLAN id */
6873 if (vlan_num
>= 0) {
6874 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6890 struct block
*b0
,*b1
;
6893 * Change the offsets to point to the type and data fields within
6894 * the MPLS packet. Just increment the offsets, so that we
6895 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6896 * capture packets with an outer label of 100000 and an inner
6899 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6903 if (label_stack_depth
> 0) {
6904 /* just match the bottom-of-stack bit clear */
6905 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6908 * Indicate that we're checking MPLS-encapsulated headers,
6909 * to make sure higher level code generators don't try to
6910 * match against IP-related protocols such as Q_ARP, Q_RARP
6915 case DLT_C_HDLC
: /* fall through */
6917 b0
= gen_linktype(ETHERTYPE_MPLS
);
6921 b0
= gen_linktype(PPP_MPLS_UCAST
);
6924 /* FIXME add other DLT_s ...
6925 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6926 * leave it for now */
6929 bpf_error("no MPLS support for data link type %d",
6937 /* If a specific MPLS label is requested, check it */
6938 if (label_num
>= 0) {
6939 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6940 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6941 0xfffff000); /* only compare the first 20 bits */
6948 label_stack_depth
++;
6953 * Support PPPOE discovery and session.
6958 /* check for PPPoE discovery */
6959 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6968 * Test against the PPPoE session link-layer type.
6970 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6973 * Change the offsets to point to the type and data fields within
6976 * XXX - this is a bit of a kludge. If we were to split the
6977 * compiler into a parser that parses an expression and
6978 * generates an expression tree, and a code generator that
6979 * takes an expression tree (which could come from our
6980 * parser or from some other parser) and generates BPF code,
6981 * we could perhaps make the offsets parameters of routines
6982 * and, in the handler for an "AND" node, pass to subnodes
6983 * other than the PPPoE node the adjusted offsets.
6985 * This would mean that "pppoes" would, instead of changing the
6986 * behavior of *all* tests after it, change only the behavior
6987 * of tests ANDed with it. That would change the documented
6988 * semantics of "pppoes", which might break some expressions.
6989 * However, it would mean that "(pppoes and ip) or ip" would check
6990 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6991 * checking only for VLAN-encapsulated IP, so that could still
6992 * be considered worth doing; it wouldn't break expressions
6993 * that are of the form "pppoes and ..." which I suspect are the
6994 * most common expressions involving "pppoes". "pppoes or ..."
6995 * doesn't necessarily do what the user would really want, now,
6996 * as all the "or ..." tests would be done assuming PPPoE, even
6997 * though the "or" could be viewed as meaning "or, if this isn't
6998 * a PPPoE packet...".
7000 orig_linktype
= off_linktype
; /* save original values */
7004 * The "network-layer" protocol is PPPoE, which has a 6-byte
7005 * PPPoE header, followed by PPP payload, so we set the
7006 * offsets to the network layer offset plus 6 bytes for
7007 * the PPPoE header plus the values appropriate for PPP when
7008 * encapsulated in Ethernet (which means there's no HDLC
7011 off_linktype
= orig_nl
+ 6;
7012 off_nl
= orig_nl
+ 6 + 2;
7013 off_nl_nosnap
= orig_nl
+ 6 + 2;
7016 * Set the link-layer type to PPP, as all subsequent tests will
7017 * be on the encapsulated PPP header.
7025 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7037 bpf_error("'vpi' supported only on raw ATM");
7038 if (off_vpi
== (u_int
)-1)
7040 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7046 bpf_error("'vci' supported only on raw ATM");
7047 if (off_vci
== (u_int
)-1)
7049 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7054 if (off_proto
== (u_int
)-1)
7055 abort(); /* XXX - this isn't on FreeBSD */
7056 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7061 if (off_payload
== (u_int
)-1)
7063 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7064 0xffffffff, jtype
, reverse
, jvalue
);
7069 bpf_error("'callref' supported only on raw ATM");
7070 if (off_proto
== (u_int
)-1)
7072 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7073 jtype
, reverse
, jvalue
);
7083 gen_atmtype_abbrev(type
)
7086 struct block
*b0
, *b1
;
7091 /* Get all packets in Meta signalling Circuit */
7093 bpf_error("'metac' supported only on raw ATM");
7094 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7095 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7100 /* Get all packets in Broadcast Circuit*/
7102 bpf_error("'bcc' supported only on raw ATM");
7103 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7104 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7109 /* Get all cells in Segment OAM F4 circuit*/
7111 bpf_error("'oam4sc' supported only on raw ATM");
7112 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7113 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7118 /* Get all cells in End-to-End OAM F4 Circuit*/
7120 bpf_error("'oam4ec' supported only on raw ATM");
7121 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7122 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7127 /* Get all packets in connection Signalling Circuit */
7129 bpf_error("'sc' supported only on raw ATM");
7130 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7131 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7136 /* Get all packets in ILMI Circuit */
7138 bpf_error("'ilmic' supported only on raw ATM");
7139 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7140 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7145 /* Get all LANE packets */
7147 bpf_error("'lane' supported only on raw ATM");
7148 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7151 * Arrange that all subsequent tests assume LANE
7152 * rather than LLC-encapsulated packets, and set
7153 * the offsets appropriately for LANE-encapsulated
7156 * "off_mac" is the offset of the Ethernet header,
7157 * which is 2 bytes past the ATM pseudo-header
7158 * (skipping the pseudo-header and 2-byte LE Client
7159 * field). The other offsets are Ethernet offsets
7160 * relative to "off_mac".
7163 off_mac
= off_payload
+ 2; /* MAC header */
7164 off_linktype
= off_mac
+ 12;
7165 off_nl
= off_mac
+ 14; /* Ethernet II */
7166 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
7170 /* Get all LLC-encapsulated packets */
7172 bpf_error("'llc' supported only on raw ATM");
7173 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7184 * Filtering for MTP2 messages based on li value
7185 * FISU, length is null
7186 * LSSU, length is 1 or 2
7187 * MSU, length is 3 or more
7190 gen_mtp2type_abbrev(type
)
7193 struct block
*b0
, *b1
;
7198 if ( (linktype
!= DLT_MTP2
) &&
7199 (linktype
!= DLT_ERF
) &&
7200 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7201 bpf_error("'fisu' supported only on MTP2");
7202 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7203 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7207 if ( (linktype
!= DLT_MTP2
) &&
7208 (linktype
!= DLT_ERF
) &&
7209 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7210 bpf_error("'lssu' supported only on MTP2");
7211 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7212 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7217 if ( (linktype
!= DLT_MTP2
) &&
7218 (linktype
!= DLT_ERF
) &&
7219 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7220 bpf_error("'msu' supported only on MTP2");
7221 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7231 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7238 bpf_u_int32 val1
, val2
, val3
;
7240 switch (mtp3field
) {
7243 if (off_sio
== (u_int
)-1)
7244 bpf_error("'sio' supported only on SS7");
7245 /* sio coded on 1 byte so max value 255 */
7247 bpf_error("sio value %u too big; max value = 255",
7249 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7250 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7254 if (off_opc
== (u_int
)-1)
7255 bpf_error("'opc' supported only on SS7");
7256 /* opc coded on 14 bits so max value 16383 */
7258 bpf_error("opc value %u too big; max value = 16383",
7260 /* the following instructions are made to convert jvalue
7261 * to the form used to write opc in an ss7 message*/
7262 val1
= jvalue
& 0x00003c00;
7264 val2
= jvalue
& 0x000003fc;
7266 val3
= jvalue
& 0x00000003;
7268 jvalue
= val1
+ val2
+ val3
;
7269 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7270 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7274 if (off_dpc
== (u_int
)-1)
7275 bpf_error("'dpc' supported only on SS7");
7276 /* dpc coded on 14 bits so max value 16383 */
7278 bpf_error("dpc value %u too big; max value = 16383",
7280 /* the following instructions are made to convert jvalue
7281 * to the forme used to write dpc in an ss7 message*/
7282 val1
= jvalue
& 0x000000ff;
7284 val2
= jvalue
& 0x00003f00;
7286 jvalue
= val1
+ val2
;
7287 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7288 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7292 if (off_sls
== (u_int
)-1)
7293 bpf_error("'sls' supported only on SS7");
7294 /* sls coded on 4 bits so max value 15 */
7296 bpf_error("sls value %u too big; max value = 15",
7298 /* the following instruction is made to convert jvalue
7299 * to the forme used to write sls in an ss7 message*/
7300 jvalue
= jvalue
<< 4;
7301 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7302 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7311 static struct block
*
7312 gen_msg_abbrev(type
)
7318 * Q.2931 signalling protocol messages for handling virtual circuits
7319 * establishment and teardown
7324 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7328 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7332 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7336 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7340 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7343 case A_RELEASE_DONE
:
7344 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7354 gen_atmmulti_abbrev(type
)
7357 struct block
*b0
, *b1
;
7363 bpf_error("'oam' supported only on raw ATM");
7364 b1
= gen_atmmulti_abbrev(A_OAMF4
);
7369 bpf_error("'oamf4' supported only on raw ATM");
7371 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7372 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7374 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7380 * Get Q.2931 signalling messages for switched
7381 * virtual connection
7384 bpf_error("'connectmsg' supported only on raw ATM");
7385 b0
= gen_msg_abbrev(A_SETUP
);
7386 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7388 b0
= gen_msg_abbrev(A_CONNECT
);
7390 b0
= gen_msg_abbrev(A_CONNECTACK
);
7392 b0
= gen_msg_abbrev(A_RELEASE
);
7394 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7396 b0
= gen_atmtype_abbrev(A_SC
);
7402 bpf_error("'metaconnect' supported only on raw ATM");
7403 b0
= gen_msg_abbrev(A_SETUP
);
7404 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7406 b0
= gen_msg_abbrev(A_CONNECT
);
7408 b0
= gen_msg_abbrev(A_RELEASE
);
7410 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7412 b0
= gen_atmtype_abbrev(A_METAC
);