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.1 2007-10-26 00:45:33 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
:
1301 case DLT_LINUX_LAPD
:
1303 * Currently, only raw "link[N:M]" filtering is supported.
1312 * Currently, only raw "link[N:M]" filtering is supported.
1319 case DLT_BLUETOOTH_HCI_H4
:
1321 * Currently, only raw "link[N:M]" filtering is supported.
1330 * Currently, only raw "link[N:M]" filtering is supported.
1339 * Currently, only raw "link[N:M]" filtering is supported.
1346 case DLT_IEEE802_15_4_LINUX
:
1348 * Currently, only raw "link[N:M]" filtering is supported.
1355 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1357 * Currently, only raw "link[N:M]" filtering is supported.
1364 case DLT_IEEE802_15_4
:
1366 * Currently, only raw "link[N:M]" filtering is supported.
1375 * Currently, only raw "link[N:M]" filtering is supported.
1384 * Currently, only raw "link[N:M]" filtering is supported.
1393 * Currently, only raw "link[N:M]" filtering is supported.
1400 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1402 * Currently, only raw "link[N:M]" filtering is supported.
1411 * Currently, only raw "link[N:M]" filtering is supported.
1413 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1414 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1415 off_nl_nosnap
= -1; /* no 802.2 LLC */
1416 off_mac
= 1; /* step over the kiss length byte */
1419 bpf_error("unknown data link type %d", linktype
);
1424 * Load a value relative to the beginning of the link-layer header.
1425 * The link-layer header doesn't necessarily begin at the beginning
1426 * of the packet data; there might be a variable-length prefix containing
1427 * radio information.
1429 static struct slist
*
1430 gen_load_llrel(offset
, size
)
1433 struct slist
*s
, *s2
;
1435 s
= gen_llprefixlen();
1438 * If "s" is non-null, it has code to arrange that the X register
1439 * contains the length of the prefix preceding the link-layer
1442 * Otherwise, the length of the prefix preceding the link-layer
1443 * header is "off_ll".
1447 * There's a variable-length prefix preceding the
1448 * link-layer header. "s" points to a list of statements
1449 * that put the length of that prefix into the X register.
1450 * do an indirect load, to use the X register as an offset.
1452 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1457 * There is no variable-length header preceding the
1458 * link-layer header; add in off_ll, which, if there's
1459 * a fixed-length header preceding the link-layer header,
1460 * is the length of that header.
1462 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1463 s
->s
.k
= offset
+ off_ll
;
1469 * Load a value relative to the beginning of the specified header.
1471 static struct slist
*
1472 gen_load_a(offrel
, offset
, size
)
1473 enum e_offrel offrel
;
1476 struct slist
*s
, *s2
;
1481 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1486 s
= gen_load_llrel(offset
, size
);
1490 s
= gen_load_llrel(off_nl
+ offset
, size
);
1494 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1499 * Load the X register with the length of the IPv4 header
1500 * (plus the offset of the link-layer header, if it's
1501 * preceded by a variable-length header such as a radio
1502 * header), in bytes.
1504 s
= gen_loadx_iphdrlen();
1507 * Load the item at {offset of the link-layer header} +
1508 * {offset, relative to the start of the link-layer
1509 * header, of the IPv4 header} + {length of the IPv4 header} +
1510 * {specified offset}.
1512 * (If the link-layer is variable-length, it's included
1513 * in the value in the X register, and off_ll is 0.)
1515 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1516 s2
->s
.k
= off_ll
+ off_nl
+ offset
;
1521 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1532 * Generate code to load into the X register the sum of the length of
1533 * the IPv4 header and any variable-length header preceding the link-layer
1536 static struct slist
*
1537 gen_loadx_iphdrlen()
1539 struct slist
*s
, *s2
;
1541 s
= gen_llprefixlen();
1544 * There's a variable-length prefix preceding the
1545 * link-layer header. "s" points to a list of statements
1546 * that put the length of that prefix into the X register.
1547 * The 4*([k]&0xf) addressing mode can't be used, as we
1548 * don't have a constant offset, so we have to load the
1549 * value in question into the A register and add to it
1550 * the value from the X register.
1552 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1555 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1558 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1563 * The A register now contains the length of the
1564 * IP header. We need to add to it the length
1565 * of the prefix preceding the link-layer
1566 * header, which is still in the X register, and
1567 * move the result into the X register.
1569 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1570 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1573 * There is no variable-length header preceding the
1574 * link-layer header; add in off_ll, which, if there's
1575 * a fixed-length header preceding the link-layer header,
1576 * is the length of that header.
1578 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1579 s
->s
.k
= off_ll
+ off_nl
;
1584 static struct block
*
1591 s
= new_stmt(BPF_LD
|BPF_IMM
);
1593 b
= new_block(JMP(BPF_JEQ
));
1599 static inline struct block
*
1602 return gen_uncond(1);
1605 static inline struct block
*
1608 return gen_uncond(0);
1612 * Byte-swap a 32-bit number.
1613 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1614 * big-endian platforms.)
1616 #define SWAPLONG(y) \
1617 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1620 * Generate code to match a particular packet type.
1622 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1623 * value, if <= ETHERMTU. We use that to determine whether to
1624 * match the type/length field or to check the type/length field for
1625 * a value <= ETHERMTU to see whether it's a type field and then do
1626 * the appropriate test.
1628 static struct block
*
1629 gen_ether_linktype(proto
)
1632 struct block
*b0
, *b1
;
1638 case LLCSAP_NETBEUI
:
1640 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1641 * so we check the DSAP and SSAP.
1643 * LLCSAP_IP checks for IP-over-802.2, rather
1644 * than IP-over-Ethernet or IP-over-SNAP.
1646 * XXX - should we check both the DSAP and the
1647 * SSAP, like this, or should we check just the
1648 * DSAP, as we do for other types <= ETHERMTU
1649 * (i.e., other SAP values)?
1651 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1653 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1654 ((proto
<< 8) | proto
));
1662 * Ethernet_II frames, which are Ethernet
1663 * frames with a frame type of ETHERTYPE_IPX;
1665 * Ethernet_802.3 frames, which are 802.3
1666 * frames (i.e., the type/length field is
1667 * a length field, <= ETHERMTU, rather than
1668 * a type field) with the first two bytes
1669 * after the Ethernet/802.3 header being
1672 * Ethernet_802.2 frames, which are 802.3
1673 * frames with an 802.2 LLC header and
1674 * with the IPX LSAP as the DSAP in the LLC
1677 * Ethernet_SNAP frames, which are 802.3
1678 * frames with an LLC header and a SNAP
1679 * header and with an OUI of 0x000000
1680 * (encapsulated Ethernet) and a protocol
1681 * ID of ETHERTYPE_IPX in the SNAP header.
1683 * XXX - should we generate the same code both
1684 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1688 * This generates code to check both for the
1689 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1691 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1692 (bpf_int32
)LLCSAP_IPX
);
1693 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1698 * Now we add code to check for SNAP frames with
1699 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1701 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1705 * Now we generate code to check for 802.3
1706 * frames in general.
1708 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1712 * Now add the check for 802.3 frames before the
1713 * check for Ethernet_802.2 and Ethernet_802.3,
1714 * as those checks should only be done on 802.3
1715 * frames, not on Ethernet frames.
1720 * Now add the check for Ethernet_II frames, and
1721 * do that before checking for the other frame
1724 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1725 (bpf_int32
)ETHERTYPE_IPX
);
1729 case ETHERTYPE_ATALK
:
1730 case ETHERTYPE_AARP
:
1732 * EtherTalk (AppleTalk protocols on Ethernet link
1733 * layer) may use 802.2 encapsulation.
1737 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1738 * we check for an Ethernet type field less than
1739 * 1500, which means it's an 802.3 length field.
1741 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1745 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1746 * SNAP packets with an organization code of
1747 * 0x080007 (Apple, for Appletalk) and a protocol
1748 * type of ETHERTYPE_ATALK (Appletalk).
1750 * 802.2-encapsulated ETHERTYPE_AARP packets are
1751 * SNAP packets with an organization code of
1752 * 0x000000 (encapsulated Ethernet) and a protocol
1753 * type of ETHERTYPE_AARP (Appletalk ARP).
1755 if (proto
== ETHERTYPE_ATALK
)
1756 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1757 else /* proto == ETHERTYPE_AARP */
1758 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1762 * Check for Ethernet encapsulation (Ethertalk
1763 * phase 1?); we just check for the Ethernet
1766 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1772 if (proto
<= ETHERMTU
) {
1774 * This is an LLC SAP value, so the frames
1775 * that match would be 802.2 frames.
1776 * Check that the frame is an 802.2 frame
1777 * (i.e., that the length/type field is
1778 * a length field, <= ETHERMTU) and
1779 * then check the DSAP.
1781 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1783 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1789 * This is an Ethernet type, so compare
1790 * the length/type field with it (if
1791 * the frame is an 802.2 frame, the length
1792 * field will be <= ETHERMTU, and, as
1793 * "proto" is > ETHERMTU, this test
1794 * will fail and the frame won't match,
1795 * which is what we want).
1797 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1804 * Generate code to match a particular packet type.
1806 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1807 * value, if <= ETHERMTU. We use that to determine whether to
1808 * match the type field or to check the type field for the special
1809 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1811 static struct block
*
1812 gen_linux_sll_linktype(proto
)
1815 struct block
*b0
, *b1
;
1821 case LLCSAP_NETBEUI
:
1823 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1824 * so we check the DSAP and SSAP.
1826 * LLCSAP_IP checks for IP-over-802.2, rather
1827 * than IP-over-Ethernet or IP-over-SNAP.
1829 * XXX - should we check both the DSAP and the
1830 * SSAP, like this, or should we check just the
1831 * DSAP, as we do for other types <= ETHERMTU
1832 * (i.e., other SAP values)?
1834 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1835 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1836 ((proto
<< 8) | proto
));
1842 * Ethernet_II frames, which are Ethernet
1843 * frames with a frame type of ETHERTYPE_IPX;
1845 * Ethernet_802.3 frames, which have a frame
1846 * type of LINUX_SLL_P_802_3;
1848 * Ethernet_802.2 frames, which are 802.3
1849 * frames with an 802.2 LLC header (i.e, have
1850 * a frame type of LINUX_SLL_P_802_2) and
1851 * with the IPX LSAP as the DSAP in the LLC
1854 * Ethernet_SNAP frames, which are 802.3
1855 * frames with an LLC header and a SNAP
1856 * header and with an OUI of 0x000000
1857 * (encapsulated Ethernet) and a protocol
1858 * ID of ETHERTYPE_IPX in the SNAP header.
1860 * First, do the checks on LINUX_SLL_P_802_2
1861 * frames; generate the check for either
1862 * Ethernet_802.2 or Ethernet_SNAP frames, and
1863 * then put a check for LINUX_SLL_P_802_2 frames
1866 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1867 (bpf_int32
)LLCSAP_IPX
);
1868 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1871 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1875 * Now check for 802.3 frames and OR that with
1876 * the previous test.
1878 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1882 * Now add the check for Ethernet_II frames, and
1883 * do that before checking for the other frame
1886 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1887 (bpf_int32
)ETHERTYPE_IPX
);
1891 case ETHERTYPE_ATALK
:
1892 case ETHERTYPE_AARP
:
1894 * EtherTalk (AppleTalk protocols on Ethernet link
1895 * layer) may use 802.2 encapsulation.
1899 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1900 * we check for the 802.2 protocol type in the
1901 * "Ethernet type" field.
1903 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1906 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1907 * SNAP packets with an organization code of
1908 * 0x080007 (Apple, for Appletalk) and a protocol
1909 * type of ETHERTYPE_ATALK (Appletalk).
1911 * 802.2-encapsulated ETHERTYPE_AARP packets are
1912 * SNAP packets with an organization code of
1913 * 0x000000 (encapsulated Ethernet) and a protocol
1914 * type of ETHERTYPE_AARP (Appletalk ARP).
1916 if (proto
== ETHERTYPE_ATALK
)
1917 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1919 else /* proto == ETHERTYPE_AARP */
1920 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1925 * Check for Ethernet encapsulation (Ethertalk
1926 * phase 1?); we just check for the Ethernet
1929 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1935 if (proto
<= ETHERMTU
) {
1937 * This is an LLC SAP value, so the frames
1938 * that match would be 802.2 frames.
1939 * Check for the 802.2 protocol type
1940 * in the "Ethernet type" field, and
1941 * then check the DSAP.
1943 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1945 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1951 * This is an Ethernet type, so compare
1952 * the length/type field with it (if
1953 * the frame is an 802.2 frame, the length
1954 * field will be <= ETHERMTU, and, as
1955 * "proto" is > ETHERMTU, this test
1956 * will fail and the frame won't match,
1957 * which is what we want).
1959 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1966 insert_radiotap_load_llprefixlen(b
)
1969 struct slist
*s1
, *s2
;
1972 * Prepend to the statements in this block code to load the
1973 * length of the radiotap header into the register assigned
1974 * to hold that length, if one has been assigned.
1976 if (reg_ll_size
!= -1) {
1978 * The 2 bytes at offsets of 2 and 3 from the beginning
1979 * of the radiotap header are the length of the radiotap
1980 * header; unfortunately, it's little-endian, so we have
1981 * to load it a byte at a time and construct the value.
1985 * Load the high-order byte, at an offset of 3, shift it
1986 * left a byte, and put the result in the X register.
1988 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1990 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1993 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1997 * Load the next byte, at an offset of 2, and OR the
1998 * value from the X register into it.
2000 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2003 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2007 * Now allocate a register to hold that value and store
2010 s2
= new_stmt(BPF_ST
);
2011 s2
->s
.k
= reg_ll_size
;
2015 * Now move it into the X register.
2017 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2021 * Now append all the existing statements in this
2022 * block to these statements.
2024 sappend(s1
, b
->stmts
);
2030 * At the moment we treat PPI as normal Radiotap encoded
2031 * packets. The difference is in the function that generates
2032 * the code at the beginning to compute the header length.
2033 * Since this code generator of PPI supports bare 802.11
2034 * encapsulation only (i.e. the encapsulated DLT should be
2035 * DLT_IEEE802_11) we generate code to check for this too.
2038 insert_ppi_load_llprefixlen(b
)
2041 struct slist
*s1
, *s2
;
2044 * Prepend to the statements in this block code to load the
2045 * length of the radiotap header into the register assigned
2046 * to hold that length, if one has been assigned.
2048 if (reg_ll_size
!= -1) {
2050 * The 2 bytes at offsets of 2 and 3 from the beginning
2051 * of the radiotap header are the length of the radiotap
2052 * header; unfortunately, it's little-endian, so we have
2053 * to load it a byte at a time and construct the value.
2057 * Load the high-order byte, at an offset of 3, shift it
2058 * left a byte, and put the result in the X register.
2060 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2062 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2065 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2069 * Load the next byte, at an offset of 2, and OR the
2070 * value from the X register into it.
2072 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2075 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2079 * Now allocate a register to hold that value and store
2082 s2
= new_stmt(BPF_ST
);
2083 s2
->s
.k
= reg_ll_size
;
2087 * Now move it into the X register.
2089 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2093 * Now append all the existing statements in this
2094 * block to these statements.
2096 sappend(s1
, b
->stmts
);
2102 static struct block
*
2103 gen_ppi_dlt_check(void)
2105 struct slist
*s_load_dlt
;
2108 if (linktype
== DLT_PPI
)
2110 /* Create the statements that check for the DLT
2112 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2113 s_load_dlt
->s
.k
= 4;
2115 b
= new_block(JMP(BPF_JEQ
));
2117 b
->stmts
= s_load_dlt
;
2118 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2129 insert_load_llprefixlen(b
)
2135 * At the moment we treat PPI as normal Radiotap encoded
2136 * packets. The difference is in the function that generates
2137 * the code at the beginning to compute the header length.
2138 * Since this code generator of PPI supports bare 802.11
2139 * encapsulation only (i.e. the encapsulated DLT should be
2140 * DLT_IEEE802_11) we generate code to check for this too.
2143 insert_ppi_load_llprefixlen(b
);
2146 case DLT_IEEE802_11_RADIO
:
2147 insert_radiotap_load_llprefixlen(b
);
2153 static struct slist
*
2154 gen_radiotap_llprefixlen(void)
2158 if (reg_ll_size
== -1) {
2160 * We haven't yet assigned a register for the length
2161 * of the radiotap header; allocate one.
2163 reg_ll_size
= alloc_reg();
2167 * Load the register containing the radiotap length
2168 * into the X register.
2170 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2171 s
->s
.k
= reg_ll_size
;
2176 * At the moment we treat PPI as normal Radiotap encoded
2177 * packets. The difference is in the function that generates
2178 * the code at the beginning to compute the header length.
2179 * Since this code generator of PPI supports bare 802.11
2180 * encapsulation only (i.e. the encapsulated DLT should be
2181 * DLT_IEEE802_11) we generate code to check for this too.
2183 static struct slist
*
2184 gen_ppi_llprefixlen(void)
2188 if (reg_ll_size
== -1) {
2190 * We haven't yet assigned a register for the length
2191 * of the radiotap header; allocate one.
2193 reg_ll_size
= alloc_reg();
2197 * Load the register containing the radiotap length
2198 * into the X register.
2200 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2201 s
->s
.k
= reg_ll_size
;
2208 * Generate code to compute the link-layer header length, if necessary,
2209 * putting it into the X register, and to return either a pointer to a
2210 * "struct slist" for the list of statements in that code, or NULL if
2211 * no code is necessary.
2213 static struct slist
*
2214 gen_llprefixlen(void)
2219 return gen_ppi_llprefixlen();
2222 case DLT_IEEE802_11_RADIO
:
2223 return gen_radiotap_llprefixlen();
2231 * Generate code to match a particular packet type by matching the
2232 * link-layer type field or fields in the 802.2 LLC header.
2234 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2235 * value, if <= ETHERMTU.
2237 static struct block
*
2241 struct block
*b0
, *b1
, *b2
;
2243 /* are we checking MPLS-encapsulated packets? */
2244 if (label_stack_depth
> 0) {
2248 /* FIXME add other L3 proto IDs */
2249 return gen_mpls_linktype(Q_IP
);
2251 case ETHERTYPE_IPV6
:
2253 /* FIXME add other L3 proto IDs */
2254 return gen_mpls_linktype(Q_IPV6
);
2257 bpf_error("unsupported protocol over mpls");
2265 return gen_ether_linktype(proto
);
2273 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2277 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2287 case DLT_IEEE802_11
:
2288 case DLT_IEEE802_11_RADIO_AVS
:
2289 case DLT_IEEE802_11_RADIO
:
2290 case DLT_PRISM_HEADER
:
2291 case DLT_ATM_RFC1483
:
2293 case DLT_IP_OVER_FC
:
2294 return gen_llc_linktype(proto
);
2300 * If "is_lane" is set, check for a LANE-encapsulated
2301 * version of this protocol, otherwise check for an
2302 * LLC-encapsulated version of this protocol.
2304 * We assume LANE means Ethernet, not Token Ring.
2308 * Check that the packet doesn't begin with an
2309 * LE Control marker. (We've already generated
2312 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2317 * Now generate an Ethernet test.
2319 b1
= gen_ether_linktype(proto
);
2324 * Check for LLC encapsulation and then check the
2327 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2328 b1
= gen_llc_linktype(proto
);
2336 return gen_linux_sll_linktype(proto
);
2341 case DLT_SLIP_BSDOS
:
2344 * These types don't provide any type field; packets
2345 * are always IPv4 or IPv6.
2347 * XXX - for IPv4, check for a version number of 4, and,
2348 * for IPv6, check for a version number of 6?
2353 /* Check for a version number of 4. */
2354 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2356 case ETHERTYPE_IPV6
:
2357 /* Check for a version number of 6. */
2358 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2362 return gen_false(); /* always false */
2369 case DLT_PPP_SERIAL
:
2372 * We use Ethernet protocol types inside libpcap;
2373 * map them to the corresponding PPP protocol types.
2382 case ETHERTYPE_IPV6
:
2391 case ETHERTYPE_ATALK
:
2405 * I'm assuming the "Bridging PDU"s that go
2406 * over PPP are Spanning Tree Protocol
2420 * We use Ethernet protocol types inside libpcap;
2421 * map them to the corresponding PPP protocol types.
2426 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2427 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2429 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2434 case ETHERTYPE_IPV6
:
2444 case ETHERTYPE_ATALK
:
2458 * I'm assuming the "Bridging PDU"s that go
2459 * over PPP are Spanning Tree Protocol
2475 * For DLT_NULL, the link-layer header is a 32-bit
2476 * word containing an AF_ value in *host* byte order,
2477 * and for DLT_ENC, the link-layer header begins
2478 * with a 32-bit work containing an AF_ value in
2481 * In addition, if we're reading a saved capture file,
2482 * the host byte order in the capture may not be the
2483 * same as the host byte order on this machine.
2485 * For DLT_LOOP, the link-layer header is a 32-bit
2486 * word containing an AF_ value in *network* byte order.
2488 * XXX - AF_ values may, unfortunately, be platform-
2489 * dependent; for example, FreeBSD's AF_INET6 is 24
2490 * whilst NetBSD's and OpenBSD's is 26.
2492 * This means that, when reading a capture file, just
2493 * checking for our AF_INET6 value won't work if the
2494 * capture file came from another OS.
2503 case ETHERTYPE_IPV6
:
2510 * Not a type on which we support filtering.
2511 * XXX - support those that have AF_ values
2512 * #defined on this platform, at least?
2517 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2519 * The AF_ value is in host byte order, but
2520 * the BPF interpreter will convert it to
2521 * network byte order.
2523 * If this is a save file, and it's from a
2524 * machine with the opposite byte order to
2525 * ours, we byte-swap the AF_ value.
2527 * Then we run it through "htonl()", and
2528 * generate code to compare against the result.
2530 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2531 bpf_pcap
->sf
.swapped
)
2532 proto
= SWAPLONG(proto
);
2533 proto
= htonl(proto
);
2535 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2537 #ifdef HAVE_NET_PFVAR_H
2540 * af field is host byte order in contrast to the rest of
2543 if (proto
== ETHERTYPE_IP
)
2544 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2545 BPF_B
, (bpf_int32
)AF_INET
));
2547 else if (proto
== ETHERTYPE_IPV6
)
2548 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2549 BPF_B
, (bpf_int32
)AF_INET6
));
2555 #endif /* HAVE_NET_PFVAR_H */
2558 case DLT_ARCNET_LINUX
:
2560 * XXX should we check for first fragment if the protocol
2569 case ETHERTYPE_IPV6
:
2570 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2571 (bpf_int32
)ARCTYPE_INET6
));
2575 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2576 (bpf_int32
)ARCTYPE_IP
);
2577 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2578 (bpf_int32
)ARCTYPE_IP_OLD
);
2583 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2584 (bpf_int32
)ARCTYPE_ARP
);
2585 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2586 (bpf_int32
)ARCTYPE_ARP_OLD
);
2590 case ETHERTYPE_REVARP
:
2591 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2592 (bpf_int32
)ARCTYPE_REVARP
));
2594 case ETHERTYPE_ATALK
:
2595 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2596 (bpf_int32
)ARCTYPE_ATALK
));
2603 case ETHERTYPE_ATALK
:
2613 * XXX - assumes a 2-byte Frame Relay header with
2614 * DLCI and flags. What if the address is longer?
2620 * Check for the special NLPID for IP.
2622 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2625 case ETHERTYPE_IPV6
:
2627 * Check for the special NLPID for IPv6.
2629 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2634 * Check for several OSI protocols.
2636 * Frame Relay packets typically have an OSI
2637 * NLPID at the beginning; we check for each
2640 * What we check for is the NLPID and a frame
2641 * control field of UI, i.e. 0x03 followed
2644 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2645 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2646 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2657 case DLT_JUNIPER_MFR
:
2658 case DLT_JUNIPER_MLFR
:
2659 case DLT_JUNIPER_MLPPP
:
2660 case DLT_JUNIPER_ATM1
:
2661 case DLT_JUNIPER_ATM2
:
2662 case DLT_JUNIPER_PPPOE
:
2663 case DLT_JUNIPER_PPPOE_ATM
:
2664 case DLT_JUNIPER_GGSN
:
2665 case DLT_JUNIPER_ES
:
2666 case DLT_JUNIPER_MONITOR
:
2667 case DLT_JUNIPER_SERVICES
:
2668 case DLT_JUNIPER_ETHER
:
2669 case DLT_JUNIPER_PPP
:
2670 case DLT_JUNIPER_FRELAY
:
2671 case DLT_JUNIPER_CHDLC
:
2672 case DLT_JUNIPER_VP
:
2673 case DLT_JUNIPER_ST
:
2674 case DLT_JUNIPER_ISM
:
2675 /* just lets verify the magic number for now -
2676 * on ATM we may have up to 6 different encapsulations on the wire
2677 * and need a lot of heuristics to figure out that the payload
2680 * FIXME encapsulation specific BPF_ filters
2682 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2684 case DLT_LINUX_IRDA
:
2685 bpf_error("IrDA link-layer type filtering not implemented");
2688 bpf_error("DOCSIS link-layer type filtering not implemented");
2690 case DLT_LINUX_LAPD
:
2691 bpf_error("LAPD link-layer type filtering not implemented");
2694 bpf_error("AX.25 link-layer type filtering not implemented");
2698 * All the types that have no encapsulation should either be
2699 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2700 * all packets are IP packets, or should be handled in some
2701 * special case, if none of them are (if some are and some
2702 * aren't, the lack of encapsulation is a problem, as we'd
2703 * have to find some other way of determining the packet type).
2705 * Therefore, if "off_linktype" is -1, there's an error.
2707 if (off_linktype
== (u_int
)-1)
2711 * Any type not handled above should always have an Ethernet
2712 * type at an offset of "off_linktype". (PPP is partially
2713 * handled above - the protocol type is mapped from the
2714 * Ethernet and LLC types we use internally to the corresponding
2715 * PPP type - but the PPP type is always specified by a value
2716 * at "off_linktype", so we don't have to do the code generation
2719 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2723 * Check for an LLC SNAP packet with a given organization code and
2724 * protocol type; we check the entire contents of the 802.2 LLC and
2725 * snap headers, checking for DSAP and SSAP of SNAP and a control
2726 * field of 0x03 in the LLC header, and for the specified organization
2727 * code and protocol type in the SNAP header.
2729 static struct block
*
2730 gen_snap(orgcode
, ptype
, offset
)
2731 bpf_u_int32 orgcode
;
2735 u_char snapblock
[8];
2737 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2738 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2739 snapblock
[2] = 0x03; /* control = UI */
2740 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2741 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2742 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2743 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2744 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2745 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2749 * Generate code to match a particular packet type, for link-layer types
2750 * using 802.2 LLC headers.
2752 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2753 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2755 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2756 * value, if <= ETHERMTU. We use that to determine whether to
2757 * match the DSAP or both DSAP and LSAP or to check the OUI and
2758 * protocol ID in a SNAP header.
2760 static struct block
*
2761 gen_llc_linktype(proto
)
2765 * XXX - handle token-ring variable-length header.
2771 case LLCSAP_NETBEUI
:
2773 * XXX - should we check both the DSAP and the
2774 * SSAP, like this, or should we check just the
2775 * DSAP, as we do for other types <= ETHERMTU
2776 * (i.e., other SAP values)?
2778 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2779 ((proto
<< 8) | proto
));
2783 * XXX - are there ever SNAP frames for IPX on
2784 * non-Ethernet 802.x networks?
2786 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2787 (bpf_int32
)LLCSAP_IPX
);
2789 case ETHERTYPE_ATALK
:
2791 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2792 * SNAP packets with an organization code of
2793 * 0x080007 (Apple, for Appletalk) and a protocol
2794 * type of ETHERTYPE_ATALK (Appletalk).
2796 * XXX - check for an organization code of
2797 * encapsulated Ethernet as well?
2799 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2803 * XXX - we don't have to check for IPX 802.3
2804 * here, but should we check for the IPX Ethertype?
2806 if (proto
<= ETHERMTU
) {
2808 * This is an LLC SAP value, so check
2811 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2815 * This is an Ethernet type; we assume that it's
2816 * unlikely that it'll appear in the right place
2817 * at random, and therefore check only the
2818 * location that would hold the Ethernet type
2819 * in a SNAP frame with an organization code of
2820 * 0x000000 (encapsulated Ethernet).
2822 * XXX - if we were to check for the SNAP DSAP and
2823 * LSAP, as per XXX, and were also to check for an
2824 * organization code of 0x000000 (encapsulated
2825 * Ethernet), we'd do
2827 * return gen_snap(0x000000, proto,
2830 * here; for now, we don't, as per the above.
2831 * I don't know whether it's worth the extra CPU
2832 * time to do the right check or not.
2834 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2840 static struct block
*
2841 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2845 u_int src_off
, dst_off
;
2847 struct block
*b0
, *b1
;
2861 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2862 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2868 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2869 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2876 b0
= gen_linktype(proto
);
2877 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2883 static struct block
*
2884 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2885 struct in6_addr
*addr
;
2886 struct in6_addr
*mask
;
2888 u_int src_off
, dst_off
;
2890 struct block
*b0
, *b1
;
2905 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2906 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2912 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2913 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2920 /* this order is important */
2921 a
= (u_int32_t
*)addr
;
2922 m
= (u_int32_t
*)mask
;
2923 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2924 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2926 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2928 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2930 b0
= gen_linktype(proto
);
2936 static struct block
*
2937 gen_ehostop(eaddr
, dir
)
2938 register const u_char
*eaddr
;
2941 register struct block
*b0
, *b1
;
2945 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2948 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2951 b0
= gen_ehostop(eaddr
, Q_SRC
);
2952 b1
= gen_ehostop(eaddr
, Q_DST
);
2958 b0
= gen_ehostop(eaddr
, Q_SRC
);
2959 b1
= gen_ehostop(eaddr
, Q_DST
);
2968 * Like gen_ehostop, but for DLT_FDDI
2970 static struct block
*
2971 gen_fhostop(eaddr
, dir
)
2972 register const u_char
*eaddr
;
2975 struct block
*b0
, *b1
;
2980 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2982 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2987 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2989 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2993 b0
= gen_fhostop(eaddr
, Q_SRC
);
2994 b1
= gen_fhostop(eaddr
, Q_DST
);
3000 b0
= gen_fhostop(eaddr
, Q_SRC
);
3001 b1
= gen_fhostop(eaddr
, Q_DST
);
3010 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3012 static struct block
*
3013 gen_thostop(eaddr
, dir
)
3014 register const u_char
*eaddr
;
3017 register struct block
*b0
, *b1
;
3021 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3024 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3027 b0
= gen_thostop(eaddr
, Q_SRC
);
3028 b1
= gen_thostop(eaddr
, Q_DST
);
3034 b0
= gen_thostop(eaddr
, Q_SRC
);
3035 b1
= gen_thostop(eaddr
, Q_DST
);
3044 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
3046 static struct block
*
3047 gen_wlanhostop(eaddr
, dir
)
3048 register const u_char
*eaddr
;
3051 register struct block
*b0
, *b1
, *b2
;
3052 register struct slist
*s
;
3059 * For control frames, there is no SA.
3061 * For management frames, SA is at an
3062 * offset of 10 from the beginning of
3065 * For data frames, SA is at an offset
3066 * of 10 from the beginning of the packet
3067 * if From DS is clear, at an offset of
3068 * 16 from the beginning of the packet
3069 * if From DS is set and To DS is clear,
3070 * and an offset of 24 from the beginning
3071 * of the packet if From DS is set and To DS
3076 * Generate the tests to be done for data frames
3079 * First, check for To DS set, i.e. check "link[1] & 0x01".
3081 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3082 b1
= new_block(JMP(BPF_JSET
));
3083 b1
->s
.k
= 0x01; /* To DS */
3087 * If To DS is set, the SA is at 24.
3089 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3093 * Now, check for To DS not set, i.e. check
3094 * "!(link[1] & 0x01)".
3096 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3097 b2
= new_block(JMP(BPF_JSET
));
3098 b2
->s
.k
= 0x01; /* To DS */
3103 * If To DS is not set, the SA is at 16.
3105 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3109 * Now OR together the last two checks. That gives
3110 * the complete set of checks for data frames with
3116 * Now check for From DS being set, and AND that with
3117 * the ORed-together checks.
3119 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3120 b1
= new_block(JMP(BPF_JSET
));
3121 b1
->s
.k
= 0x02; /* From DS */
3126 * Now check for data frames with From DS not set.
3128 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3129 b2
= new_block(JMP(BPF_JSET
));
3130 b2
->s
.k
= 0x02; /* From DS */
3135 * If From DS isn't set, the SA is at 10.
3137 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3141 * Now OR together the checks for data frames with
3142 * From DS not set and for data frames with From DS
3143 * set; that gives the checks done for data frames.
3148 * Now check for a data frame.
3149 * I.e, check "link[0] & 0x08".
3151 gen_load_a(OR_LINK
, 0, BPF_B
);
3152 b1
= new_block(JMP(BPF_JSET
));
3157 * AND that with the checks done for data frames.
3162 * If the high-order bit of the type value is 0, this
3163 * is a management frame.
3164 * I.e, check "!(link[0] & 0x08)".
3166 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3167 b2
= new_block(JMP(BPF_JSET
));
3173 * For management frames, the SA is at 10.
3175 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3179 * OR that with the checks done for data frames.
3180 * That gives the checks done for management and
3186 * If the low-order bit of the type value is 1,
3187 * this is either a control frame or a frame
3188 * with a reserved type, and thus not a
3191 * I.e., check "!(link[0] & 0x04)".
3193 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3194 b1
= new_block(JMP(BPF_JSET
));
3200 * AND that with the checks for data and management
3210 * For control frames, there is no DA.
3212 * For management frames, DA is at an
3213 * offset of 4 from the beginning of
3216 * For data frames, DA is at an offset
3217 * of 4 from the beginning of the packet
3218 * if To DS is clear and at an offset of
3219 * 16 from the beginning of the packet
3224 * Generate the tests to be done for data frames.
3226 * First, check for To DS set, i.e. "link[1] & 0x01".
3228 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3229 b1
= new_block(JMP(BPF_JSET
));
3230 b1
->s
.k
= 0x01; /* To DS */
3234 * If To DS is set, the DA is at 16.
3236 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3240 * Now, check for To DS not set, i.e. check
3241 * "!(link[1] & 0x01)".
3243 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3244 b2
= new_block(JMP(BPF_JSET
));
3245 b2
->s
.k
= 0x01; /* To DS */
3250 * If To DS is not set, the DA is at 4.
3252 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3256 * Now OR together the last two checks. That gives
3257 * the complete set of checks for data frames.
3262 * Now check for a data frame.
3263 * I.e, check "link[0] & 0x08".
3265 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3266 b1
= new_block(JMP(BPF_JSET
));
3271 * AND that with the checks done for data frames.
3276 * If the high-order bit of the type value is 0, this
3277 * is a management frame.
3278 * I.e, check "!(link[0] & 0x08)".
3280 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3281 b2
= new_block(JMP(BPF_JSET
));
3287 * For management frames, the DA is at 4.
3289 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3293 * OR that with the checks done for data frames.
3294 * That gives the checks done for management and
3300 * If the low-order bit of the type value is 1,
3301 * this is either a control frame or a frame
3302 * with a reserved type, and thus not a
3305 * I.e., check "!(link[0] & 0x04)".
3307 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3308 b1
= new_block(JMP(BPF_JSET
));
3314 * AND that with the checks for data and management
3321 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3322 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3328 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3329 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3338 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3339 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3340 * as the RFC states.)
3342 static struct block
*
3343 gen_ipfchostop(eaddr
, dir
)
3344 register const u_char
*eaddr
;
3347 register struct block
*b0
, *b1
;
3351 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3354 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3357 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3358 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3364 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3365 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3374 * This is quite tricky because there may be pad bytes in front of the
3375 * DECNET header, and then there are two possible data packet formats that
3376 * carry both src and dst addresses, plus 5 packet types in a format that
3377 * carries only the src node, plus 2 types that use a different format and
3378 * also carry just the src node.
3382 * Instead of doing those all right, we just look for data packets with
3383 * 0 or 1 bytes of padding. If you want to look at other packets, that
3384 * will require a lot more hacking.
3386 * To add support for filtering on DECNET "areas" (network numbers)
3387 * one would want to add a "mask" argument to this routine. That would
3388 * make the filter even more inefficient, although one could be clever
3389 * and not generate masking instructions if the mask is 0xFFFF.
3391 static struct block
*
3392 gen_dnhostop(addr
, dir
)
3396 struct block
*b0
, *b1
, *b2
, *tmp
;
3397 u_int offset_lh
; /* offset if long header is received */
3398 u_int offset_sh
; /* offset if short header is received */
3403 offset_sh
= 1; /* follows flags */
3404 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3408 offset_sh
= 3; /* follows flags, dstnode */
3409 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3413 /* Inefficient because we do our Calvinball dance twice */
3414 b0
= gen_dnhostop(addr
, Q_SRC
);
3415 b1
= gen_dnhostop(addr
, Q_DST
);
3421 /* Inefficient because we do our Calvinball dance twice */
3422 b0
= gen_dnhostop(addr
, Q_SRC
);
3423 b1
= gen_dnhostop(addr
, Q_DST
);
3428 bpf_error("ISO host filtering not implemented");
3433 b0
= gen_linktype(ETHERTYPE_DN
);
3434 /* Check for pad = 1, long header case */
3435 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3436 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3437 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3438 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3440 /* Check for pad = 0, long header case */
3441 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3442 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3445 /* Check for pad = 1, short header case */
3446 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3447 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3448 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3451 /* Check for pad = 0, short header case */
3452 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3453 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3457 /* Combine with test for linktype */
3463 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3464 * test the bottom-of-stack bit, and then check the version number
3465 * field in the IP header.
3467 static struct block
*
3468 gen_mpls_linktype(proto
)
3471 struct block
*b0
, *b1
;
3476 /* match the bottom-of-stack bit */
3477 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3478 /* match the IPv4 version number */
3479 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3484 /* match the bottom-of-stack bit */
3485 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3486 /* match the IPv4 version number */
3487 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3496 static struct block
*
3497 gen_host(addr
, mask
, proto
, dir
, type
)
3504 struct block
*b0
, *b1
;
3505 const char *typestr
;
3515 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3517 * Only check for non-IPv4 addresses if we're not
3518 * checking MPLS-encapsulated packets.
3520 if (label_stack_depth
== 0) {
3521 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3523 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3529 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3532 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3535 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3538 bpf_error("'tcp' modifier applied to %s", typestr
);
3541 bpf_error("'sctp' modifier applied to %s", typestr
);
3544 bpf_error("'udp' modifier applied to %s", typestr
);
3547 bpf_error("'icmp' modifier applied to %s", typestr
);
3550 bpf_error("'igmp' modifier applied to %s", typestr
);
3553 bpf_error("'igrp' modifier applied to %s", typestr
);
3556 bpf_error("'pim' modifier applied to %s", typestr
);
3559 bpf_error("'vrrp' modifier applied to %s", typestr
);
3562 bpf_error("ATALK host filtering not implemented");
3565 bpf_error("AARP host filtering not implemented");
3568 return gen_dnhostop(addr
, dir
);
3571 bpf_error("SCA host filtering not implemented");
3574 bpf_error("LAT host filtering not implemented");
3577 bpf_error("MOPDL host filtering not implemented");
3580 bpf_error("MOPRC host filtering not implemented");
3584 bpf_error("'ip6' modifier applied to ip host");
3587 bpf_error("'icmp6' modifier applied to %s", typestr
);
3591 bpf_error("'ah' modifier applied to %s", typestr
);
3594 bpf_error("'esp' modifier applied to %s", typestr
);
3597 bpf_error("ISO host filtering not implemented");
3600 bpf_error("'esis' modifier applied to %s", typestr
);
3603 bpf_error("'isis' modifier applied to %s", typestr
);
3606 bpf_error("'clnp' modifier applied to %s", typestr
);
3609 bpf_error("'stp' modifier applied to %s", typestr
);
3612 bpf_error("IPX host filtering not implemented");
3615 bpf_error("'netbeui' modifier applied to %s", typestr
);
3618 bpf_error("'radio' modifier applied to %s", typestr
);
3627 static struct block
*
3628 gen_host6(addr
, mask
, proto
, dir
, type
)
3629 struct in6_addr
*addr
;
3630 struct in6_addr
*mask
;
3635 const char *typestr
;
3645 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3648 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3651 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3654 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3657 bpf_error("'sctp' modifier applied to %s", typestr
);
3660 bpf_error("'tcp' modifier applied to %s", typestr
);
3663 bpf_error("'udp' modifier applied to %s", typestr
);
3666 bpf_error("'icmp' modifier applied to %s", typestr
);
3669 bpf_error("'igmp' modifier applied to %s", typestr
);
3672 bpf_error("'igrp' modifier applied to %s", typestr
);
3675 bpf_error("'pim' modifier applied to %s", typestr
);
3678 bpf_error("'vrrp' modifier applied to %s", typestr
);
3681 bpf_error("ATALK host filtering not implemented");
3684 bpf_error("AARP host filtering not implemented");
3687 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3690 bpf_error("SCA host filtering not implemented");
3693 bpf_error("LAT host filtering not implemented");
3696 bpf_error("MOPDL host filtering not implemented");
3699 bpf_error("MOPRC host filtering not implemented");
3702 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3705 bpf_error("'icmp6' modifier applied to %s", typestr
);
3708 bpf_error("'ah' modifier applied to %s", typestr
);
3711 bpf_error("'esp' modifier applied to %s", typestr
);
3714 bpf_error("ISO host filtering not implemented");
3717 bpf_error("'esis' modifier applied to %s", typestr
);
3720 bpf_error("'isis' modifier applied to %s", typestr
);
3723 bpf_error("'clnp' modifier applied to %s", typestr
);
3726 bpf_error("'stp' modifier applied to %s", typestr
);
3729 bpf_error("IPX host filtering not implemented");
3732 bpf_error("'netbeui' modifier applied to %s", typestr
);
3735 bpf_error("'radio' modifier applied to %s", typestr
);
3745 static struct block
*
3746 gen_gateway(eaddr
, alist
, proto
, dir
)
3747 const u_char
*eaddr
;
3748 bpf_u_int32
**alist
;
3752 struct block
*b0
, *b1
, *tmp
;
3755 bpf_error("direction applied to 'gateway'");
3764 b0
= gen_ehostop(eaddr
, Q_OR
);
3767 b0
= gen_fhostop(eaddr
, Q_OR
);
3770 b0
= gen_thostop(eaddr
, Q_OR
);
3772 case DLT_IEEE802_11
:
3773 case DLT_IEEE802_11_RADIO_AVS
:
3775 case DLT_IEEE802_11_RADIO
:
3776 case DLT_PRISM_HEADER
:
3777 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3782 * Check that the packet doesn't begin with an
3783 * LE Control marker. (We've already generated
3786 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
3791 * Now check the MAC address.
3793 b0
= gen_ehostop(eaddr
, Q_OR
);
3797 case DLT_IP_OVER_FC
:
3798 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3802 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3804 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3806 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3815 bpf_error("illegal modifier of 'gateway'");
3821 gen_proto_abbrev(proto
)
3830 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3832 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3838 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3840 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3846 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3848 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3854 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3857 #ifndef IPPROTO_IGMP
3858 #define IPPROTO_IGMP 2
3862 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3865 #ifndef IPPROTO_IGRP
3866 #define IPPROTO_IGRP 9
3869 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3873 #define IPPROTO_PIM 103
3877 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3879 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3884 #ifndef IPPROTO_VRRP
3885 #define IPPROTO_VRRP 112
3889 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3893 b1
= gen_linktype(ETHERTYPE_IP
);
3897 b1
= gen_linktype(ETHERTYPE_ARP
);
3901 b1
= gen_linktype(ETHERTYPE_REVARP
);
3905 bpf_error("link layer applied in wrong context");
3908 b1
= gen_linktype(ETHERTYPE_ATALK
);
3912 b1
= gen_linktype(ETHERTYPE_AARP
);
3916 b1
= gen_linktype(ETHERTYPE_DN
);
3920 b1
= gen_linktype(ETHERTYPE_SCA
);
3924 b1
= gen_linktype(ETHERTYPE_LAT
);
3928 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3932 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3937 b1
= gen_linktype(ETHERTYPE_IPV6
);
3940 #ifndef IPPROTO_ICMPV6
3941 #define IPPROTO_ICMPV6 58
3944 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3949 #define IPPROTO_AH 51
3952 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3954 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3960 #define IPPROTO_ESP 50
3963 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3965 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3971 b1
= gen_linktype(LLCSAP_ISONS
);
3975 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3979 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3982 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3983 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3984 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3986 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3988 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3990 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3994 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3995 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3996 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3998 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4000 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4002 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4006 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4007 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4008 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4010 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4015 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4016 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4021 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4022 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4024 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4026 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4031 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4032 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4037 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4038 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4043 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4047 b1
= gen_linktype(LLCSAP_8021D
);
4051 b1
= gen_linktype(LLCSAP_IPX
);
4055 b1
= gen_linktype(LLCSAP_NETBEUI
);
4059 bpf_error("'radio' is not a valid protocol type");
4067 static struct block
*
4074 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4075 b
= new_block(JMP(BPF_JSET
));
4084 * Generate a comparison to a port value in the transport-layer header
4085 * at the specified offset from the beginning of that header.
4087 * XXX - this handles a variable-length prefix preceding the link-layer
4088 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4089 * variable-length link-layer headers (such as Token Ring or 802.11
4092 static struct block
*
4093 gen_portatom(off
, v
)
4097 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4101 static struct block
*
4102 gen_portatom6(off
, v
)
4106 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4111 gen_portop(port
, proto
, dir
)
4112 int port
, proto
, dir
;
4114 struct block
*b0
, *b1
, *tmp
;
4116 /* ip proto 'proto' */
4117 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4123 b1
= gen_portatom(0, (bpf_int32
)port
);
4127 b1
= gen_portatom(2, (bpf_int32
)port
);
4132 tmp
= gen_portatom(0, (bpf_int32
)port
);
4133 b1
= gen_portatom(2, (bpf_int32
)port
);
4138 tmp
= gen_portatom(0, (bpf_int32
)port
);
4139 b1
= gen_portatom(2, (bpf_int32
)port
);
4151 static struct block
*
4152 gen_port(port
, ip_proto
, dir
)
4157 struct block
*b0
, *b1
, *tmp
;
4162 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4163 * not LLC encapsulation with LLCSAP_IP.
4165 * For IEEE 802 networks - which includes 802.5 token ring
4166 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4167 * says that SNAP encapsulation is used, not LLC encapsulation
4170 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4171 * RFC 2225 say that SNAP encapsulation is used, not LLC
4172 * encapsulation with LLCSAP_IP.
4174 * So we always check for ETHERTYPE_IP.
4176 b0
= gen_linktype(ETHERTYPE_IP
);
4182 b1
= gen_portop(port
, ip_proto
, dir
);
4186 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4187 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4189 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4202 gen_portop6(port
, proto
, dir
)
4203 int port
, proto
, dir
;
4205 struct block
*b0
, *b1
, *tmp
;
4207 /* ip6 proto 'proto' */
4208 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4212 b1
= gen_portatom6(0, (bpf_int32
)port
);
4216 b1
= gen_portatom6(2, (bpf_int32
)port
);
4221 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4222 b1
= gen_portatom6(2, (bpf_int32
)port
);
4227 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4228 b1
= gen_portatom6(2, (bpf_int32
)port
);
4240 static struct block
*
4241 gen_port6(port
, ip_proto
, dir
)
4246 struct block
*b0
, *b1
, *tmp
;
4248 /* link proto ip6 */
4249 b0
= gen_linktype(ETHERTYPE_IPV6
);
4255 b1
= gen_portop6(port
, ip_proto
, dir
);
4259 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4260 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4262 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4274 /* gen_portrange code */
4275 static struct block
*
4276 gen_portrangeatom(off
, v1
, v2
)
4280 struct block
*b1
, *b2
;
4284 * Reverse the order of the ports, so v1 is the lower one.
4293 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4294 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4302 gen_portrangeop(port1
, port2
, proto
, dir
)
4307 struct block
*b0
, *b1
, *tmp
;
4309 /* ip proto 'proto' */
4310 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4316 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4320 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4325 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4326 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4331 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4332 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4344 static struct block
*
4345 gen_portrange(port1
, port2
, ip_proto
, dir
)
4350 struct block
*b0
, *b1
, *tmp
;
4353 b0
= gen_linktype(ETHERTYPE_IP
);
4359 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4363 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4364 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4366 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4378 static struct block
*
4379 gen_portrangeatom6(off
, v1
, v2
)
4383 struct block
*b1
, *b2
;
4387 * Reverse the order of the ports, so v1 is the lower one.
4396 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4397 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4405 gen_portrangeop6(port1
, port2
, proto
, dir
)
4410 struct block
*b0
, *b1
, *tmp
;
4412 /* ip6 proto 'proto' */
4413 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4417 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4421 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4426 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4427 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4432 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4433 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4445 static struct block
*
4446 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4451 struct block
*b0
, *b1
, *tmp
;
4453 /* link proto ip6 */
4454 b0
= gen_linktype(ETHERTYPE_IPV6
);
4460 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4464 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4465 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4467 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4480 lookup_proto(name
, proto
)
4481 register const char *name
;
4491 v
= pcap_nametoproto(name
);
4492 if (v
== PROTO_UNDEF
)
4493 bpf_error("unknown ip proto '%s'", name
);
4497 /* XXX should look up h/w protocol type based on linktype */
4498 v
= pcap_nametoeproto(name
);
4499 if (v
== PROTO_UNDEF
) {
4500 v
= pcap_nametollc(name
);
4501 if (v
== PROTO_UNDEF
)
4502 bpf_error("unknown ether proto '%s'", name
);
4507 if (strcmp(name
, "esis") == 0)
4509 else if (strcmp(name
, "isis") == 0)
4511 else if (strcmp(name
, "clnp") == 0)
4514 bpf_error("unknown osi proto '%s'", name
);
4534 static struct block
*
4535 gen_protochain(v
, proto
, dir
)
4540 #ifdef NO_PROTOCHAIN
4541 return gen_proto(v
, proto
, dir
);
4543 struct block
*b0
, *b
;
4544 struct slist
*s
[100];
4545 int fix2
, fix3
, fix4
, fix5
;
4546 int ahcheck
, again
, end
;
4548 int reg2
= alloc_reg();
4550 memset(s
, 0, sizeof(s
));
4551 fix2
= fix3
= fix4
= fix5
= 0;
4558 b0
= gen_protochain(v
, Q_IP
, dir
);
4559 b
= gen_protochain(v
, Q_IPV6
, dir
);
4563 bpf_error("bad protocol applied for 'protochain'");
4568 * We don't handle variable-length radiotap here headers yet.
4569 * We might want to add BPF instructions to do the protochain
4570 * work, to simplify that and, on platforms that have a BPF
4571 * interpreter with the new instructions, let the filtering
4572 * be done in the kernel. (We already require a modified BPF
4573 * engine to do the protochain stuff, to support backward
4574 * branches, and backward branch support is unlikely to appear
4575 * in kernel BPF engines.)
4577 if (linktype
== DLT_IEEE802_11_RADIO
)
4578 bpf_error("'protochain' not supported with radiotap headers");
4580 if (linktype
== DLT_PPI
)
4581 bpf_error("'protochain' not supported with PPI headers");
4583 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4586 * s[0] is a dummy entry to protect other BPF insn from damage
4587 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4588 * hard to find interdependency made by jump table fixup.
4591 s
[i
] = new_stmt(0); /*dummy*/
4596 b0
= gen_linktype(ETHERTYPE_IP
);
4599 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4600 s
[i
]->s
.k
= off_ll
+ off_nl
+ 9;
4602 /* X = ip->ip_hl << 2 */
4603 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4604 s
[i
]->s
.k
= off_ll
+ off_nl
;
4609 b0
= gen_linktype(ETHERTYPE_IPV6
);
4611 /* A = ip6->ip_nxt */
4612 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4613 s
[i
]->s
.k
= off_ll
+ off_nl
+ 6;
4615 /* X = sizeof(struct ip6_hdr) */
4616 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4622 bpf_error("unsupported proto to gen_protochain");
4626 /* again: if (A == v) goto end; else fall through; */
4628 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4630 s
[i
]->s
.jt
= NULL
; /*later*/
4631 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4635 #ifndef IPPROTO_NONE
4636 #define IPPROTO_NONE 59
4638 /* if (A == IPPROTO_NONE) goto end */
4639 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4640 s
[i
]->s
.jt
= NULL
; /*later*/
4641 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4642 s
[i
]->s
.k
= IPPROTO_NONE
;
4643 s
[fix5
]->s
.jf
= s
[i
];
4648 if (proto
== Q_IPV6
) {
4649 int v6start
, v6end
, v6advance
, j
;
4652 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4653 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4654 s
[i
]->s
.jt
= NULL
; /*later*/
4655 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4656 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4657 s
[fix2
]->s
.jf
= s
[i
];
4659 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4660 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4661 s
[i
]->s
.jt
= NULL
; /*later*/
4662 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4663 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4665 /* if (A == IPPROTO_ROUTING) goto v6advance */
4666 s
[i
- 1]->s
.jf
= 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_ROUTING
;
4671 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4672 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4673 s
[i
]->s
.jt
= NULL
; /*later*/
4674 s
[i
]->s
.jf
= NULL
; /*later*/
4675 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4686 * X = X + (P[X + 1] + 1) * 8;
4689 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4691 /* A = P[X + packet head] */
4692 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4693 s
[i
]->s
.k
= off_ll
+ off_nl
;
4696 s
[i
] = new_stmt(BPF_ST
);
4700 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4703 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4707 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4709 /* A = P[X + packet head]; */
4710 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4711 s
[i
]->s
.k
= off_ll
+ off_nl
;
4714 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4718 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4722 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4725 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4729 /* goto again; (must use BPF_JA for backward jump) */
4730 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4731 s
[i
]->s
.k
= again
- i
- 1;
4732 s
[i
- 1]->s
.jf
= s
[i
];
4736 for (j
= v6start
; j
<= v6end
; j
++)
4737 s
[j
]->s
.jt
= s
[v6advance
];
4742 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4744 s
[fix2
]->s
.jf
= s
[i
];
4750 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4751 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4752 s
[i
]->s
.jt
= NULL
; /*later*/
4753 s
[i
]->s
.jf
= NULL
; /*later*/
4754 s
[i
]->s
.k
= IPPROTO_AH
;
4756 s
[fix3
]->s
.jf
= s
[ahcheck
];
4763 * X = X + (P[X + 1] + 2) * 4;
4766 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4768 /* A = P[X + packet head]; */
4769 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4770 s
[i
]->s
.k
= off_ll
+ off_nl
;
4773 s
[i
] = new_stmt(BPF_ST
);
4777 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4780 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4784 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4786 /* A = P[X + packet head] */
4787 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4788 s
[i
]->s
.k
= off_ll
+ off_nl
;
4791 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4795 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4799 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4802 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4806 /* goto again; (must use BPF_JA for backward jump) */
4807 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4808 s
[i
]->s
.k
= again
- i
- 1;
4813 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4815 s
[fix2
]->s
.jt
= s
[end
];
4816 s
[fix4
]->s
.jf
= s
[end
];
4817 s
[fix5
]->s
.jt
= s
[end
];
4824 for (i
= 0; i
< max
- 1; i
++)
4825 s
[i
]->next
= s
[i
+ 1];
4826 s
[max
- 1]->next
= NULL
;
4831 b
= new_block(JMP(BPF_JEQ
));
4832 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4843 * Generate code that checks whether the packet is a packet for protocol
4844 * <proto> and whether the type field in that protocol's header has
4845 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4846 * IP packet and checks the protocol number in the IP header against <v>.
4848 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4849 * against Q_IP and Q_IPV6.
4851 static struct block
*
4852 gen_proto(v
, proto
, dir
)
4857 struct block
*b0
, *b1
;
4859 if (dir
!= Q_DEFAULT
)
4860 bpf_error("direction applied to 'proto'");
4865 b0
= gen_proto(v
, Q_IP
, dir
);
4866 b1
= gen_proto(v
, Q_IPV6
, dir
);
4874 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4875 * not LLC encapsulation with LLCSAP_IP.
4877 * For IEEE 802 networks - which includes 802.5 token ring
4878 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4879 * says that SNAP encapsulation is used, not LLC encapsulation
4882 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4883 * RFC 2225 say that SNAP encapsulation is used, not LLC
4884 * encapsulation with LLCSAP_IP.
4886 * So we always check for ETHERTYPE_IP.
4888 b0
= gen_linktype(ETHERTYPE_IP
);
4890 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4892 b1
= gen_protochain(v
, Q_IP
);
4902 * Frame Relay packets typically have an OSI
4903 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4904 * generates code to check for all the OSI
4905 * NLPIDs, so calling it and then adding a check
4906 * for the particular NLPID for which we're
4907 * looking is bogus, as we can just check for
4910 * What we check for is the NLPID and a frame
4911 * control field value of UI, i.e. 0x03 followed
4914 * XXX - assumes a 2-byte Frame Relay header with
4915 * DLCI and flags. What if the address is longer?
4917 * XXX - what about SNAP-encapsulated frames?
4919 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4925 * Cisco uses an Ethertype lookalike - for OSI,
4928 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4929 /* OSI in C-HDLC is stuffed with a fudge byte */
4930 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4935 b0
= gen_linktype(LLCSAP_ISONS
);
4936 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4942 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4944 * 4 is the offset of the PDU type relative to the IS-IS
4947 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4952 bpf_error("arp does not encapsulate another protocol");
4956 bpf_error("rarp does not encapsulate another protocol");
4960 bpf_error("atalk encapsulation is not specifiable");
4964 bpf_error("decnet encapsulation is not specifiable");
4968 bpf_error("sca does not encapsulate another protocol");
4972 bpf_error("lat does not encapsulate another protocol");
4976 bpf_error("moprc does not encapsulate another protocol");
4980 bpf_error("mopdl does not encapsulate another protocol");
4984 return gen_linktype(v
);
4987 bpf_error("'udp proto' is bogus");
4991 bpf_error("'tcp proto' is bogus");
4995 bpf_error("'sctp proto' is bogus");
4999 bpf_error("'icmp proto' is bogus");
5003 bpf_error("'igmp proto' is bogus");
5007 bpf_error("'igrp proto' is bogus");
5011 bpf_error("'pim proto' is bogus");
5015 bpf_error("'vrrp proto' is bogus");
5020 b0
= gen_linktype(ETHERTYPE_IPV6
);
5022 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5024 b1
= gen_protochain(v
, Q_IPV6
);
5030 bpf_error("'icmp6 proto' is bogus");
5034 bpf_error("'ah proto' is bogus");
5037 bpf_error("'ah proto' is bogus");
5040 bpf_error("'stp proto' is bogus");
5043 bpf_error("'ipx proto' is bogus");
5046 bpf_error("'netbeui proto' is bogus");
5049 bpf_error("'radio proto' is bogus");
5060 register const char *name
;
5063 int proto
= q
.proto
;
5067 bpf_u_int32 mask
, addr
;
5069 bpf_u_int32
**alist
;
5072 struct sockaddr_in
*sin4
;
5073 struct sockaddr_in6
*sin6
;
5074 struct addrinfo
*res
, *res0
;
5075 struct in6_addr mask128
;
5077 struct block
*b
, *tmp
;
5078 int port
, real_proto
;
5084 addr
= pcap_nametonetaddr(name
);
5086 bpf_error("unknown network '%s'", name
);
5087 /* Left justify network addr and calculate its network mask */
5089 while (addr
&& (addr
& 0xff000000) == 0) {
5093 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5097 if (proto
== Q_LINK
) {
5101 eaddr
= pcap_ether_hostton(name
);
5104 "unknown ether host '%s'", name
);
5105 b
= gen_ehostop(eaddr
, dir
);
5110 eaddr
= pcap_ether_hostton(name
);
5113 "unknown FDDI host '%s'", name
);
5114 b
= gen_fhostop(eaddr
, dir
);
5119 eaddr
= pcap_ether_hostton(name
);
5122 "unknown token ring host '%s'", name
);
5123 b
= gen_thostop(eaddr
, dir
);
5127 case DLT_IEEE802_11
:
5128 case DLT_IEEE802_11_RADIO_AVS
:
5129 case DLT_IEEE802_11_RADIO
:
5130 case DLT_PRISM_HEADER
:
5132 eaddr
= pcap_ether_hostton(name
);
5135 "unknown 802.11 host '%s'", name
);
5136 b
= gen_wlanhostop(eaddr
, dir
);
5140 case DLT_IP_OVER_FC
:
5141 eaddr
= pcap_ether_hostton(name
);
5144 "unknown Fibre Channel host '%s'", name
);
5145 b
= gen_ipfchostop(eaddr
, dir
);
5154 * Check that the packet doesn't begin
5155 * with an LE Control marker. (We've
5156 * already generated a test for LANE.)
5158 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5162 eaddr
= pcap_ether_hostton(name
);
5165 "unknown ether host '%s'", name
);
5166 b
= gen_ehostop(eaddr
, dir
);
5172 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5173 } else if (proto
== Q_DECNET
) {
5174 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5176 * I don't think DECNET hosts can be multihomed, so
5177 * there is no need to build up a list of addresses
5179 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5182 alist
= pcap_nametoaddr(name
);
5183 if (alist
== NULL
|| *alist
== NULL
)
5184 bpf_error("unknown host '%s'", name
);
5186 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5188 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5190 tmp
= gen_host(**alist
++, 0xffffffff,
5191 tproto
, dir
, q
.addr
);
5197 memset(&mask128
, 0xff, sizeof(mask128
));
5198 res0
= res
= pcap_nametoaddrinfo(name
);
5200 bpf_error("unknown host '%s'", name
);
5202 tproto
= tproto6
= proto
;
5203 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5207 for (res
= res0
; res
; res
= res
->ai_next
) {
5208 switch (res
->ai_family
) {
5210 if (tproto
== Q_IPV6
)
5213 sin4
= (struct sockaddr_in
*)
5215 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5216 0xffffffff, tproto
, dir
, q
.addr
);
5219 if (tproto6
== Q_IP
)
5222 sin6
= (struct sockaddr_in6
*)
5224 tmp
= gen_host6(&sin6
->sin6_addr
,
5225 &mask128
, tproto6
, dir
, q
.addr
);
5236 bpf_error("unknown host '%s'%s", name
,
5237 (proto
== Q_DEFAULT
)
5239 : " for specified address family");
5246 if (proto
!= Q_DEFAULT
&&
5247 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5248 bpf_error("illegal qualifier of 'port'");
5249 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5250 bpf_error("unknown port '%s'", name
);
5251 if (proto
== Q_UDP
) {
5252 if (real_proto
== IPPROTO_TCP
)
5253 bpf_error("port '%s' is tcp", name
);
5254 else if (real_proto
== IPPROTO_SCTP
)
5255 bpf_error("port '%s' is sctp", name
);
5257 /* override PROTO_UNDEF */
5258 real_proto
= IPPROTO_UDP
;
5260 if (proto
== Q_TCP
) {
5261 if (real_proto
== IPPROTO_UDP
)
5262 bpf_error("port '%s' is udp", name
);
5264 else if (real_proto
== IPPROTO_SCTP
)
5265 bpf_error("port '%s' is sctp", name
);
5267 /* override PROTO_UNDEF */
5268 real_proto
= IPPROTO_TCP
;
5270 if (proto
== Q_SCTP
) {
5271 if (real_proto
== IPPROTO_UDP
)
5272 bpf_error("port '%s' is udp", name
);
5274 else if (real_proto
== IPPROTO_TCP
)
5275 bpf_error("port '%s' is tcp", name
);
5277 /* override PROTO_UNDEF */
5278 real_proto
= IPPROTO_SCTP
;
5281 return gen_port(port
, real_proto
, dir
);
5283 b
= gen_port(port
, real_proto
, dir
);
5284 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5289 if (proto
!= Q_DEFAULT
&&
5290 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5291 bpf_error("illegal qualifier of 'portrange'");
5292 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5293 bpf_error("unknown port in range '%s'", name
);
5294 if (proto
== Q_UDP
) {
5295 if (real_proto
== IPPROTO_TCP
)
5296 bpf_error("port in range '%s' is tcp", name
);
5297 else if (real_proto
== IPPROTO_SCTP
)
5298 bpf_error("port in range '%s' is sctp", name
);
5300 /* override PROTO_UNDEF */
5301 real_proto
= IPPROTO_UDP
;
5303 if (proto
== Q_TCP
) {
5304 if (real_proto
== IPPROTO_UDP
)
5305 bpf_error("port in range '%s' is udp", name
);
5306 else if (real_proto
== IPPROTO_SCTP
)
5307 bpf_error("port in range '%s' is sctp", name
);
5309 /* override PROTO_UNDEF */
5310 real_proto
= IPPROTO_TCP
;
5312 if (proto
== Q_SCTP
) {
5313 if (real_proto
== IPPROTO_UDP
)
5314 bpf_error("port in range '%s' is udp", name
);
5315 else if (real_proto
== IPPROTO_TCP
)
5316 bpf_error("port in range '%s' is tcp", name
);
5318 /* override PROTO_UNDEF */
5319 real_proto
= IPPROTO_SCTP
;
5322 return gen_portrange(port1
, port2
, real_proto
, dir
);
5324 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5325 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5331 eaddr
= pcap_ether_hostton(name
);
5333 bpf_error("unknown ether host: %s", name
);
5335 alist
= pcap_nametoaddr(name
);
5336 if (alist
== NULL
|| *alist
== NULL
)
5337 bpf_error("unknown host '%s'", name
);
5338 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5342 bpf_error("'gateway' not supported in this configuration");
5346 real_proto
= lookup_proto(name
, proto
);
5347 if (real_proto
>= 0)
5348 return gen_proto(real_proto
, proto
, dir
);
5350 bpf_error("unknown protocol: %s", name
);
5353 real_proto
= lookup_proto(name
, proto
);
5354 if (real_proto
>= 0)
5355 return gen_protochain(real_proto
, proto
, dir
);
5357 bpf_error("unknown protocol: %s", name
);
5369 gen_mcode(s1
, s2
, masklen
, q
)
5370 register const char *s1
, *s2
;
5371 register int masklen
;
5374 register int nlen
, mlen
;
5377 nlen
= __pcap_atoin(s1
, &n
);
5378 /* Promote short ipaddr */
5382 mlen
= __pcap_atoin(s2
, &m
);
5383 /* Promote short ipaddr */
5386 bpf_error("non-network bits set in \"%s mask %s\"",
5389 /* Convert mask len to mask */
5391 bpf_error("mask length must be <= 32");
5394 * X << 32 is not guaranteed by C to be 0; it's
5399 m
= 0xffffffff << (32 - masklen
);
5401 bpf_error("non-network bits set in \"%s/%d\"",
5408 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5411 bpf_error("Mask syntax for networks only");
5420 register const char *s
;
5425 int proto
= q
.proto
;
5431 else if (q
.proto
== Q_DECNET
)
5432 vlen
= __pcap_atodn(s
, &v
);
5434 vlen
= __pcap_atoin(s
, &v
);
5441 if (proto
== Q_DECNET
)
5442 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5443 else if (proto
== Q_LINK
) {
5444 bpf_error("illegal link layer address");
5447 if (s
== NULL
&& q
.addr
== Q_NET
) {
5448 /* Promote short net number */
5449 while (v
&& (v
& 0xff000000) == 0) {
5454 /* Promote short ipaddr */
5458 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5463 proto
= IPPROTO_UDP
;
5464 else if (proto
== Q_TCP
)
5465 proto
= IPPROTO_TCP
;
5466 else if (proto
== Q_SCTP
)
5467 proto
= IPPROTO_SCTP
;
5468 else if (proto
== Q_DEFAULT
)
5469 proto
= PROTO_UNDEF
;
5471 bpf_error("illegal qualifier of 'port'");
5474 return gen_port((int)v
, proto
, dir
);
5478 b
= gen_port((int)v
, proto
, dir
);
5479 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5486 proto
= IPPROTO_UDP
;
5487 else if (proto
== Q_TCP
)
5488 proto
= IPPROTO_TCP
;
5489 else if (proto
== Q_SCTP
)
5490 proto
= IPPROTO_SCTP
;
5491 else if (proto
== Q_DEFAULT
)
5492 proto
= PROTO_UNDEF
;
5494 bpf_error("illegal qualifier of 'portrange'");
5497 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5501 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5502 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5508 bpf_error("'gateway' requires a name");
5512 return gen_proto((int)v
, proto
, dir
);
5515 return gen_protochain((int)v
, proto
, dir
);
5530 gen_mcode6(s1
, s2
, masklen
, q
)
5531 register const char *s1
, *s2
;
5532 register int masklen
;
5535 struct addrinfo
*res
;
5536 struct in6_addr
*addr
;
5537 struct in6_addr mask
;
5542 bpf_error("no mask %s supported", s2
);
5544 res
= pcap_nametoaddrinfo(s1
);
5546 bpf_error("invalid ip6 address %s", s1
);
5548 bpf_error("%s resolved to multiple address", s1
);
5549 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5551 if (sizeof(mask
) * 8 < masklen
)
5552 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5553 memset(&mask
, 0, sizeof(mask
));
5554 memset(&mask
, 0xff, masklen
/ 8);
5556 mask
.s6_addr
[masklen
/ 8] =
5557 (0xff << (8 - masklen
% 8)) & 0xff;
5560 a
= (u_int32_t
*)addr
;
5561 m
= (u_int32_t
*)&mask
;
5562 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5563 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5564 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5572 bpf_error("Mask syntax for networks only");
5576 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5581 bpf_error("invalid qualifier against IPv6 address");
5590 register const u_char
*eaddr
;
5593 struct block
*b
, *tmp
;
5595 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5598 return gen_ehostop(eaddr
, (int)q
.dir
);
5600 return gen_fhostop(eaddr
, (int)q
.dir
);
5602 return gen_thostop(eaddr
, (int)q
.dir
);
5603 case DLT_IEEE802_11
:
5604 case DLT_IEEE802_11_RADIO_AVS
:
5605 case DLT_IEEE802_11_RADIO
:
5606 case DLT_PRISM_HEADER
:
5608 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5612 * Check that the packet doesn't begin with an
5613 * LE Control marker. (We've already generated
5616 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5621 * Now check the MAC address.
5623 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5628 case DLT_IP_OVER_FC
:
5629 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5631 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5635 bpf_error("ethernet address used in non-ether expression");
5642 struct slist
*s0
, *s1
;
5645 * This is definitely not the best way to do this, but the
5646 * lists will rarely get long.
5653 static struct slist
*
5659 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5664 static struct slist
*
5670 s
= new_stmt(BPF_LD
|BPF_MEM
);
5676 * Modify "index" to use the value stored into its register as an
5677 * offset relative to the beginning of the header for the protocol
5678 * "proto", and allocate a register and put an item "size" bytes long
5679 * (1, 2, or 4) at that offset into that register, making it the register
5683 gen_load(proto
, inst
, size
)
5688 struct slist
*s
, *tmp
;
5690 int regno
= alloc_reg();
5692 free_reg(inst
->regno
);
5696 bpf_error("data size must be 1, 2, or 4");
5712 bpf_error("unsupported index operation");
5716 * The offset is relative to the beginning of the packet
5717 * data, if we have a radio header. (If we don't, this
5720 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5721 linktype
!= DLT_IEEE802_11_RADIO
&&
5722 linktype
!= DLT_PRISM_HEADER
)
5723 bpf_error("radio information not present in capture");
5726 * Load into the X register the offset computed into the
5727 * register specifed by "index".
5729 s
= xfer_to_x(inst
);
5732 * Load the item at that offset.
5734 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5736 sappend(inst
->s
, s
);
5741 * The offset is relative to the beginning of
5742 * the link-layer header.
5744 * XXX - what about ATM LANE? Should the index be
5745 * relative to the beginning of the AAL5 frame, so
5746 * that 0 refers to the beginning of the LE Control
5747 * field, or relative to the beginning of the LAN
5748 * frame, so that 0 refers, for Ethernet LANE, to
5749 * the beginning of the destination address?
5751 s
= gen_llprefixlen();
5754 * If "s" is non-null, it has code to arrange that the
5755 * X register contains the length of the prefix preceding
5756 * the link-layer header. Add to it the offset computed
5757 * into the register specified by "index", and move that
5758 * into the X register. Otherwise, just load into the X
5759 * register the offset computed into the register specifed
5763 sappend(s
, xfer_to_a(inst
));
5764 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5765 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5767 s
= xfer_to_x(inst
);
5770 * Load the item at the sum of the offset we've put in the
5771 * X register and the offset of the start of the link
5772 * layer header (which is 0 if the radio header is
5773 * variable-length; that header length is what we put
5774 * into the X register and then added to the index).
5776 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5779 sappend(inst
->s
, s
);
5795 * The offset is relative to the beginning of
5796 * the network-layer header.
5797 * XXX - are there any cases where we want
5800 s
= gen_llprefixlen();
5803 * If "s" is non-null, it has code to arrange that the
5804 * X register contains the length of the prefix preceding
5805 * the link-layer header. Add to it the offset computed
5806 * into the register specified by "index", and move that
5807 * into the X register. Otherwise, just load into the X
5808 * register the offset computed into the register specifed
5812 sappend(s
, xfer_to_a(inst
));
5813 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5814 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5816 s
= xfer_to_x(inst
);
5819 * Load the item at the sum of the offset we've put in the
5820 * X register, the offset of the start of the network
5821 * layer header, and the offset of the start of the link
5822 * layer header (which is 0 if the radio header is
5823 * variable-length; that header length is what we put
5824 * into the X register and then added to the index).
5826 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5827 tmp
->s
.k
= off_ll
+ off_nl
;
5829 sappend(inst
->s
, s
);
5832 * Do the computation only if the packet contains
5833 * the protocol in question.
5835 b
= gen_proto_abbrev(proto
);
5837 gen_and(inst
->b
, b
);
5850 * The offset is relative to the beginning of
5851 * the transport-layer header.
5853 * Load the X register with the length of the IPv4 header
5854 * (plus the offset of the link-layer header, if it's
5855 * a variable-length header), in bytes.
5857 * XXX - are there any cases where we want
5859 * XXX - we should, if we're built with
5860 * IPv6 support, generate code to load either
5861 * IPv4, IPv6, or both, as appropriate.
5863 s
= gen_loadx_iphdrlen();
5866 * The X register now contains the sum of the length
5867 * of any variable-length header preceding the link-layer
5868 * header and the length of the network-layer header.
5869 * Load into the A register the offset relative to
5870 * the beginning of the transport layer header,
5871 * add the X register to that, move that to the
5872 * X register, and load with an offset from the
5873 * X register equal to the offset of the network
5874 * layer header relative to the beginning of
5875 * the link-layer header plus the length of any
5876 * fixed-length header preceding the link-layer
5879 sappend(s
, xfer_to_a(inst
));
5880 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5881 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5882 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5883 tmp
->s
.k
= off_ll
+ off_nl
;
5884 sappend(inst
->s
, s
);
5887 * Do the computation only if the packet contains
5888 * the protocol in question - which is true only
5889 * if this is an IP datagram and is the first or
5890 * only fragment of that datagram.
5892 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5894 gen_and(inst
->b
, b
);
5896 gen_and(gen_proto_abbrev(Q_IP
), b
);
5902 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5906 inst
->regno
= regno
;
5907 s
= new_stmt(BPF_ST
);
5909 sappend(inst
->s
, s
);
5915 gen_relation(code
, a0
, a1
, reversed
)
5917 struct arth
*a0
, *a1
;
5920 struct slist
*s0
, *s1
, *s2
;
5921 struct block
*b
, *tmp
;
5925 if (code
== BPF_JEQ
) {
5926 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5927 b
= new_block(JMP(code
));
5931 b
= new_block(BPF_JMP
|code
|BPF_X
);
5937 sappend(a0
->s
, a1
->s
);
5941 free_reg(a0
->regno
);
5942 free_reg(a1
->regno
);
5944 /* 'and' together protocol checks */
5947 gen_and(a0
->b
, tmp
= a1
->b
);
5963 int regno
= alloc_reg();
5964 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5967 s
= new_stmt(BPF_LD
|BPF_LEN
);
5968 s
->next
= new_stmt(BPF_ST
);
5969 s
->next
->s
.k
= regno
;
5984 a
= (struct arth
*)newchunk(sizeof(*a
));
5988 s
= new_stmt(BPF_LD
|BPF_IMM
);
5990 s
->next
= new_stmt(BPF_ST
);
6006 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6009 s
= new_stmt(BPF_ST
);
6017 gen_arth(code
, a0
, a1
)
6019 struct arth
*a0
, *a1
;
6021 struct slist
*s0
, *s1
, *s2
;
6025 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6030 sappend(a0
->s
, a1
->s
);
6032 free_reg(a0
->regno
);
6033 free_reg(a1
->regno
);
6035 s0
= new_stmt(BPF_ST
);
6036 a0
->regno
= s0
->s
.k
= alloc_reg();
6043 * Here we handle simple allocation of the scratch registers.
6044 * If too many registers are alloc'd, the allocator punts.
6046 static int regused
[BPF_MEMWORDS
];
6050 * Initialize the table of used registers and the current register.
6056 memset(regused
, 0, sizeof regused
);
6060 * Return the next free register.
6065 int n
= BPF_MEMWORDS
;
6068 if (regused
[curreg
])
6069 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6071 regused
[curreg
] = 1;
6075 bpf_error("too many registers needed to evaluate expression");
6081 * Return a register to the table so it can
6091 static struct block
*
6098 s
= new_stmt(BPF_LD
|BPF_LEN
);
6099 b
= new_block(JMP(jmp
));
6110 return gen_len(BPF_JGE
, n
);
6114 * Actually, this is less than or equal.
6122 b
= gen_len(BPF_JGT
, n
);
6129 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6130 * the beginning of the link-layer header.
6131 * XXX - that means you can't test values in the radiotap header, but
6132 * as that header is difficult if not impossible to parse generally
6133 * without a loop, that might not be a severe problem. A new keyword
6134 * "radio" could be added for that, although what you'd really want
6135 * would be a way of testing particular radio header values, which
6136 * would generate code appropriate to the radio header in question.
6139 gen_byteop(op
, idx
, val
)
6150 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6153 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6157 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6161 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6165 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6169 b
= new_block(JMP(BPF_JEQ
));
6176 static u_char abroadcast
[] = { 0x0 };
6179 gen_broadcast(proto
)
6182 bpf_u_int32 hostmask
;
6183 struct block
*b0
, *b1
, *b2
;
6184 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6192 case DLT_ARCNET_LINUX
:
6193 return gen_ahostop(abroadcast
, Q_DST
);
6195 return gen_ehostop(ebroadcast
, Q_DST
);
6197 return gen_fhostop(ebroadcast
, Q_DST
);
6199 return gen_thostop(ebroadcast
, Q_DST
);
6200 case DLT_IEEE802_11
:
6201 case DLT_IEEE802_11_RADIO_AVS
:
6202 case DLT_IEEE802_11_RADIO
:
6204 case DLT_PRISM_HEADER
:
6205 return gen_wlanhostop(ebroadcast
, Q_DST
);
6206 case DLT_IP_OVER_FC
:
6207 return gen_ipfchostop(ebroadcast
, Q_DST
);
6211 * Check that the packet doesn't begin with an
6212 * LE Control marker. (We've already generated
6215 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6220 * Now check the MAC address.
6222 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6228 bpf_error("not a broadcast link");
6233 b0
= gen_linktype(ETHERTYPE_IP
);
6234 hostmask
= ~netmask
;
6235 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6236 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6237 (bpf_int32
)(~0 & hostmask
), hostmask
);
6242 bpf_error("only link-layer/IP broadcast filters supported");
6248 * Generate code to test the low-order bit of a MAC address (that's
6249 * the bottom bit of the *first* byte).
6251 static struct block
*
6252 gen_mac_multicast(offset
)
6255 register struct block
*b0
;
6256 register struct slist
*s
;
6258 /* link[offset] & 1 != 0 */
6259 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6260 b0
= new_block(JMP(BPF_JSET
));
6267 gen_multicast(proto
)
6270 register struct block
*b0
, *b1
, *b2
;
6271 register struct slist
*s
;
6279 case DLT_ARCNET_LINUX
:
6280 /* all ARCnet multicasts use the same address */
6281 return gen_ahostop(abroadcast
, Q_DST
);
6283 /* ether[0] & 1 != 0 */
6284 return gen_mac_multicast(0);
6287 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6289 * XXX - was that referring to bit-order issues?
6291 /* fddi[1] & 1 != 0 */
6292 return gen_mac_multicast(1);
6294 /* tr[2] & 1 != 0 */
6295 return gen_mac_multicast(2);
6296 case DLT_IEEE802_11
:
6297 case DLT_IEEE802_11_RADIO_AVS
:
6299 case DLT_IEEE802_11_RADIO
:
6300 case DLT_PRISM_HEADER
:
6304 * For control frames, there is no DA.
6306 * For management frames, DA is at an
6307 * offset of 4 from the beginning of
6310 * For data frames, DA is at an offset
6311 * of 4 from the beginning of the packet
6312 * if To DS is clear and at an offset of
6313 * 16 from the beginning of the packet
6318 * Generate the tests to be done for data frames.
6320 * First, check for To DS set, i.e. "link[1] & 0x01".
6322 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6323 b1
= new_block(JMP(BPF_JSET
));
6324 b1
->s
.k
= 0x01; /* To DS */
6328 * If To DS is set, the DA is at 16.
6330 b0
= gen_mac_multicast(16);
6334 * Now, check for To DS not set, i.e. check
6335 * "!(link[1] & 0x01)".
6337 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6338 b2
= new_block(JMP(BPF_JSET
));
6339 b2
->s
.k
= 0x01; /* To DS */
6344 * If To DS is not set, the DA is at 4.
6346 b1
= gen_mac_multicast(4);
6350 * Now OR together the last two checks. That gives
6351 * the complete set of checks for data frames.
6356 * Now check for a data frame.
6357 * I.e, check "link[0] & 0x08".
6359 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6360 b1
= new_block(JMP(BPF_JSET
));
6365 * AND that with the checks done for data frames.
6370 * If the high-order bit of the type value is 0, this
6371 * is a management frame.
6372 * I.e, check "!(link[0] & 0x08)".
6374 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6375 b2
= new_block(JMP(BPF_JSET
));
6381 * For management frames, the DA is at 4.
6383 b1
= gen_mac_multicast(4);
6387 * OR that with the checks done for data frames.
6388 * That gives the checks done for management and
6394 * If the low-order bit of the type value is 1,
6395 * this is either a control frame or a frame
6396 * with a reserved type, and thus not a
6399 * I.e., check "!(link[0] & 0x04)".
6401 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6402 b1
= new_block(JMP(BPF_JSET
));
6408 * AND that with the checks for data and management
6413 case DLT_IP_OVER_FC
:
6414 b0
= gen_mac_multicast(2);
6419 * Check that the packet doesn't begin with an
6420 * LE Control marker. (We've already generated
6423 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6427 /* ether[off_mac] & 1 != 0 */
6428 b0
= gen_mac_multicast(off_mac
);
6436 /* Link not known to support multicasts */
6440 b0
= gen_linktype(ETHERTYPE_IP
);
6441 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6447 b0
= gen_linktype(ETHERTYPE_IPV6
);
6448 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6453 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6459 * generate command for inbound/outbound. It's here so we can
6460 * make it link-type specific. 'dir' = 0 implies "inbound",
6461 * = 1 implies "outbound".
6467 register struct block
*b0
;
6470 * Only some data link types support inbound/outbound qualifiers.
6474 b0
= gen_relation(BPF_JEQ
,
6475 gen_load(Q_LINK
, gen_loadi(0), 1),
6483 * Match packets sent by this machine.
6485 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6488 * Match packets sent to this machine.
6489 * (No broadcast or multicast packets, or
6490 * packets sent to some other machine and
6491 * received promiscuously.)
6493 * XXX - packets sent to other machines probably
6494 * shouldn't be matched, but what about broadcast
6495 * or multicast packets we received?
6497 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6501 #ifdef HAVE_NET_PFVAR_H
6503 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6504 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6510 /* match outgoing packets */
6511 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6513 /* match incoming packets */
6514 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6518 case DLT_JUNIPER_MFR
:
6519 case DLT_JUNIPER_MLFR
:
6520 case DLT_JUNIPER_MLPPP
:
6521 case DLT_JUNIPER_ATM1
:
6522 case DLT_JUNIPER_ATM2
:
6523 case DLT_JUNIPER_PPPOE
:
6524 case DLT_JUNIPER_PPPOE_ATM
:
6525 case DLT_JUNIPER_GGSN
:
6526 case DLT_JUNIPER_ES
:
6527 case DLT_JUNIPER_MONITOR
:
6528 case DLT_JUNIPER_SERVICES
:
6529 case DLT_JUNIPER_ETHER
:
6530 case DLT_JUNIPER_PPP
:
6531 case DLT_JUNIPER_FRELAY
:
6532 case DLT_JUNIPER_CHDLC
:
6533 case DLT_JUNIPER_VP
:
6534 case DLT_JUNIPER_ST
:
6535 case DLT_JUNIPER_ISM
:
6536 /* juniper flags (including direction) are stored
6537 * the byte after the 3-byte magic number */
6539 /* match outgoing packets */
6540 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6542 /* match incoming packets */
6543 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6548 bpf_error("inbound/outbound not supported on linktype %d",
6556 #ifdef HAVE_NET_PFVAR_H
6557 /* PF firewall log matched interface */
6559 gen_pf_ifname(const char *ifname
)
6564 if (linktype
!= DLT_PFLOG
) {
6565 bpf_error("ifname supported only on PF linktype");
6568 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6569 off
= offsetof(struct pfloghdr
, ifname
);
6570 if (strlen(ifname
) >= len
) {
6571 bpf_error("ifname interface names can only be %d characters",
6575 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6579 /* PF firewall log ruleset name */
6581 gen_pf_ruleset(char *ruleset
)
6585 if (linktype
!= DLT_PFLOG
) {
6586 bpf_error("ruleset supported only on PF linktype");
6590 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6591 bpf_error("ruleset names can only be %ld characters",
6592 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6596 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6597 strlen(ruleset
), (const u_char
*)ruleset
);
6601 /* PF firewall log rule number */
6607 if (linktype
!= DLT_PFLOG
) {
6608 bpf_error("rnr supported only on PF linktype");
6612 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6617 /* PF firewall log sub-rule number */
6619 gen_pf_srnr(int srnr
)
6623 if (linktype
!= DLT_PFLOG
) {
6624 bpf_error("srnr supported only on PF linktype");
6628 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6633 /* PF firewall log reason code */
6635 gen_pf_reason(int reason
)
6639 if (linktype
!= DLT_PFLOG
) {
6640 bpf_error("reason supported only on PF linktype");
6644 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6649 /* PF firewall log action */
6651 gen_pf_action(int action
)
6655 if (linktype
!= DLT_PFLOG
) {
6656 bpf_error("action supported only on PF linktype");
6660 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6664 #else /* !HAVE_NET_PFVAR_H */
6666 gen_pf_ifname(const char *ifname
)
6668 bpf_error("libpcap was compiled without pf support");
6674 gen_pf_ruleset(char *ruleset
)
6676 bpf_error("libpcap was compiled on a machine without pf support");
6684 bpf_error("libpcap was compiled on a machine without pf support");
6690 gen_pf_srnr(int srnr
)
6692 bpf_error("libpcap was compiled on a machine without pf support");
6698 gen_pf_reason(int reason
)
6700 bpf_error("libpcap was compiled on a machine without pf support");
6706 gen_pf_action(int action
)
6708 bpf_error("libpcap was compiled on a machine without pf support");
6712 #endif /* HAVE_NET_PFVAR_H */
6714 /* IEEE 802.11 wireless header */
6716 gen_p80211_type(int type
, int mask
)
6722 case DLT_IEEE802_11
:
6723 case DLT_PRISM_HEADER
:
6724 case DLT_IEEE802_11_RADIO_AVS
:
6725 case DLT_IEEE802_11_RADIO
:
6726 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
6731 bpf_error("802.11 link-layer types supported only on 802.11");
6739 register const u_char
*eaddr
;
6745 case DLT_ARCNET_LINUX
:
6746 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
6748 return (gen_ahostop(eaddr
, (int)q
.dir
));
6750 bpf_error("ARCnet address used in non-arc expression");
6756 bpf_error("aid supported only on ARCnet");
6759 bpf_error("ARCnet address used in non-arc expression");
6764 static struct block
*
6765 gen_ahostop(eaddr
, dir
)
6766 register const u_char
*eaddr
;
6769 register struct block
*b0
, *b1
;
6772 /* src comes first, different from Ethernet */
6774 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6777 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6780 b0
= gen_ahostop(eaddr
, Q_SRC
);
6781 b1
= gen_ahostop(eaddr
, Q_DST
);
6787 b0
= gen_ahostop(eaddr
, Q_SRC
);
6788 b1
= gen_ahostop(eaddr
, Q_DST
);
6797 * support IEEE 802.1Q VLAN trunk over ethernet
6803 struct block
*b0
, *b1
;
6805 /* can't check for VLAN-encapsulated packets inside MPLS */
6806 if (label_stack_depth
> 0)
6807 bpf_error("no VLAN match after MPLS");
6810 * Change the offsets to point to the type and data fields within
6811 * the VLAN packet. Just increment the offsets, so that we
6812 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6813 * capture VLAN 200 encapsulated within VLAN 100.
6815 * XXX - this is a bit of a kludge. If we were to split the
6816 * compiler into a parser that parses an expression and
6817 * generates an expression tree, and a code generator that
6818 * takes an expression tree (which could come from our
6819 * parser or from some other parser) and generates BPF code,
6820 * we could perhaps make the offsets parameters of routines
6821 * and, in the handler for an "AND" node, pass to subnodes
6822 * other than the VLAN node the adjusted offsets.
6824 * This would mean that "vlan" would, instead of changing the
6825 * behavior of *all* tests after it, change only the behavior
6826 * of tests ANDed with it. That would change the documented
6827 * semantics of "vlan", which might break some expressions.
6828 * However, it would mean that "(vlan and ip) or ip" would check
6829 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6830 * checking only for VLAN-encapsulated IP, so that could still
6831 * be considered worth doing; it wouldn't break expressions
6832 * that are of the form "vlan and ..." or "vlan N and ...",
6833 * which I suspect are the most common expressions involving
6834 * "vlan". "vlan or ..." doesn't necessarily do what the user
6835 * would really want, now, as all the "or ..." tests would
6836 * be done assuming a VLAN, even though the "or" could be viewed
6837 * as meaning "or, if this isn't a VLAN packet...".
6839 orig_linktype
= off_linktype
; /* save original values */
6851 bpf_error("no VLAN support for data link type %d",
6856 /* check for VLAN */
6857 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6859 /* If a specific VLAN is requested, check VLAN id */
6860 if (vlan_num
>= 0) {
6861 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6877 struct block
*b0
,*b1
;
6880 * Change the offsets to point to the type and data fields within
6881 * the MPLS packet. Just increment the offsets, so that we
6882 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6883 * capture packets with an outer label of 100000 and an inner
6886 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6890 if (label_stack_depth
> 0) {
6891 /* just match the bottom-of-stack bit clear */
6892 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6895 * Indicate that we're checking MPLS-encapsulated headers,
6896 * to make sure higher level code generators don't try to
6897 * match against IP-related protocols such as Q_ARP, Q_RARP
6902 case DLT_C_HDLC
: /* fall through */
6904 b0
= gen_linktype(ETHERTYPE_MPLS
);
6908 b0
= gen_linktype(PPP_MPLS_UCAST
);
6911 /* FIXME add other DLT_s ...
6912 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6913 * leave it for now */
6916 bpf_error("no MPLS support for data link type %d",
6924 /* If a specific MPLS label is requested, check it */
6925 if (label_num
>= 0) {
6926 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6927 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6928 0xfffff000); /* only compare the first 20 bits */
6935 label_stack_depth
++;
6940 * Support PPPOE discovery and session.
6945 /* check for PPPoE discovery */
6946 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6955 * Test against the PPPoE session link-layer type.
6957 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6960 * Change the offsets to point to the type and data fields within
6963 * XXX - this is a bit of a kludge. If we were to split the
6964 * compiler into a parser that parses an expression and
6965 * generates an expression tree, and a code generator that
6966 * takes an expression tree (which could come from our
6967 * parser or from some other parser) and generates BPF code,
6968 * we could perhaps make the offsets parameters of routines
6969 * and, in the handler for an "AND" node, pass to subnodes
6970 * other than the PPPoE node the adjusted offsets.
6972 * This would mean that "pppoes" would, instead of changing the
6973 * behavior of *all* tests after it, change only the behavior
6974 * of tests ANDed with it. That would change the documented
6975 * semantics of "pppoes", which might break some expressions.
6976 * However, it would mean that "(pppoes and ip) or ip" would check
6977 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6978 * checking only for VLAN-encapsulated IP, so that could still
6979 * be considered worth doing; it wouldn't break expressions
6980 * that are of the form "pppoes and ..." which I suspect are the
6981 * most common expressions involving "pppoes". "pppoes or ..."
6982 * doesn't necessarily do what the user would really want, now,
6983 * as all the "or ..." tests would be done assuming PPPoE, even
6984 * though the "or" could be viewed as meaning "or, if this isn't
6985 * a PPPoE packet...".
6987 orig_linktype
= off_linktype
; /* save original values */
6991 * The "network-layer" protocol is PPPoE, which has a 6-byte
6992 * PPPoE header, followed by PPP payload, so we set the
6993 * offsets to the network layer offset plus 6 bytes for
6994 * the PPPoE header plus the values appropriate for PPP when
6995 * encapsulated in Ethernet (which means there's no HDLC
6998 off_linktype
= orig_nl
+ 6;
6999 off_nl
= orig_nl
+ 6 + 2;
7000 off_nl_nosnap
= orig_nl
+ 6 + 2;
7003 * Set the link-layer type to PPP, as all subsequent tests will
7004 * be on the encapsulated PPP header.
7012 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7024 bpf_error("'vpi' supported only on raw ATM");
7025 if (off_vpi
== (u_int
)-1)
7027 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7033 bpf_error("'vci' supported only on raw ATM");
7034 if (off_vci
== (u_int
)-1)
7036 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7041 if (off_proto
== (u_int
)-1)
7042 abort(); /* XXX - this isn't on FreeBSD */
7043 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7048 if (off_payload
== (u_int
)-1)
7050 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7051 0xffffffff, jtype
, reverse
, jvalue
);
7056 bpf_error("'callref' supported only on raw ATM");
7057 if (off_proto
== (u_int
)-1)
7059 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7060 jtype
, reverse
, jvalue
);
7070 gen_atmtype_abbrev(type
)
7073 struct block
*b0
, *b1
;
7078 /* Get all packets in Meta signalling Circuit */
7080 bpf_error("'metac' supported only on raw ATM");
7081 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7082 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7087 /* Get all packets in Broadcast Circuit*/
7089 bpf_error("'bcc' supported only on raw ATM");
7090 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7091 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7096 /* Get all cells in Segment OAM F4 circuit*/
7098 bpf_error("'oam4sc' supported only on raw ATM");
7099 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7100 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7105 /* Get all cells in End-to-End OAM F4 Circuit*/
7107 bpf_error("'oam4ec' supported only on raw ATM");
7108 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7109 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7114 /* Get all packets in connection Signalling Circuit */
7116 bpf_error("'sc' supported only on raw ATM");
7117 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7118 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7123 /* Get all packets in ILMI Circuit */
7125 bpf_error("'ilmic' supported only on raw ATM");
7126 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7127 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7132 /* Get all LANE packets */
7134 bpf_error("'lane' supported only on raw ATM");
7135 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7138 * Arrange that all subsequent tests assume LANE
7139 * rather than LLC-encapsulated packets, and set
7140 * the offsets appropriately for LANE-encapsulated
7143 * "off_mac" is the offset of the Ethernet header,
7144 * which is 2 bytes past the ATM pseudo-header
7145 * (skipping the pseudo-header and 2-byte LE Client
7146 * field). The other offsets are Ethernet offsets
7147 * relative to "off_mac".
7150 off_mac
= off_payload
+ 2; /* MAC header */
7151 off_linktype
= off_mac
+ 12;
7152 off_nl
= off_mac
+ 14; /* Ethernet II */
7153 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
7157 /* Get all LLC-encapsulated packets */
7159 bpf_error("'llc' supported only on raw ATM");
7160 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7171 * Filtering for MTP2 messages based on li value
7172 * FISU, length is null
7173 * LSSU, length is 1 or 2
7174 * MSU, length is 3 or more
7177 gen_mtp2type_abbrev(type
)
7180 struct block
*b0
, *b1
;
7185 if ( (linktype
!= DLT_MTP2
) &&
7186 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7187 bpf_error("'fisu' supported only on MTP2");
7188 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7189 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7193 if ( (linktype
!= DLT_MTP2
) &&
7194 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7195 bpf_error("'lssu' supported only on MTP2");
7196 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7197 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7202 if ( (linktype
!= DLT_MTP2
) &&
7203 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7204 bpf_error("'msu' supported only on MTP2");
7205 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7215 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7222 bpf_u_int32 val1
, val2
, val3
;
7224 switch (mtp3field
) {
7227 if (off_sio
== (u_int
)-1)
7228 bpf_error("'sio' supported only on SS7");
7229 /* sio coded on 1 byte so max value 255 */
7231 bpf_error("sio value %u too big; max value = 255",
7233 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7234 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7238 if (off_opc
== (u_int
)-1)
7239 bpf_error("'opc' supported only on SS7");
7240 /* opc coded on 14 bits so max value 16383 */
7242 bpf_error("opc value %u too big; max value = 16383",
7244 /* the following instructions are made to convert jvalue
7245 * to the form used to write opc in an ss7 message*/
7246 val1
= jvalue
& 0x00003c00;
7248 val2
= jvalue
& 0x000003fc;
7250 val3
= jvalue
& 0x00000003;
7252 jvalue
= val1
+ val2
+ val3
;
7253 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7254 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7258 if (off_dpc
== (u_int
)-1)
7259 bpf_error("'dpc' supported only on SS7");
7260 /* dpc coded on 14 bits so max value 16383 */
7262 bpf_error("dpc value %u too big; max value = 16383",
7264 /* the following instructions are made to convert jvalue
7265 * to the forme used to write dpc in an ss7 message*/
7266 val1
= jvalue
& 0x000000ff;
7268 val2
= jvalue
& 0x00003f00;
7270 jvalue
= val1
+ val2
;
7271 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7272 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7276 if (off_sls
== (u_int
)-1)
7277 bpf_error("'sls' supported only on SS7");
7278 /* sls coded on 4 bits so max value 15 */
7280 bpf_error("sls value %u too big; max value = 15",
7282 /* the following instruction is made to convert jvalue
7283 * to the forme used to write sls in an ss7 message*/
7284 jvalue
= jvalue
<< 4;
7285 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7286 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7295 static struct block
*
7296 gen_msg_abbrev(type
)
7302 * Q.2931 signalling protocol messages for handling virtual circuits
7303 * establishment and teardown
7308 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7312 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7316 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7320 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7324 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7327 case A_RELEASE_DONE
:
7328 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7338 gen_atmmulti_abbrev(type
)
7341 struct block
*b0
, *b1
;
7347 bpf_error("'oam' supported only on raw ATM");
7348 b1
= gen_atmmulti_abbrev(A_OAMF4
);
7353 bpf_error("'oamf4' supported only on raw ATM");
7355 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7356 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7358 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7364 * Get Q.2931 signalling messages for switched
7365 * virtual connection
7368 bpf_error("'connectmsg' supported only on raw ATM");
7369 b0
= gen_msg_abbrev(A_SETUP
);
7370 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7372 b0
= gen_msg_abbrev(A_CONNECT
);
7374 b0
= gen_msg_abbrev(A_CONNECTACK
);
7376 b0
= gen_msg_abbrev(A_RELEASE
);
7378 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7380 b0
= gen_atmtype_abbrev(A_SC
);
7386 bpf_error("'metaconnect' supported only on raw ATM");
7387 b0
= gen_msg_abbrev(A_SETUP
);
7388 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7390 b0
= gen_msg_abbrev(A_CONNECT
);
7392 b0
= gen_msg_abbrev(A_RELEASE
);
7394 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7396 b0
= gen_atmtype_abbrev(A_METAC
);