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 2007-10-05 01:40:14 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 int alloc_reg(void);
138 static void free_reg(int);
140 static struct block
*root
;
143 * Value passed to gen_load_a() to indicate what the offset argument
147 OR_PACKET
, /* relative to the beginning of the packet */
148 OR_LINK
, /* relative to the link-layer header */
149 OR_NET
, /* relative to the network-layer header */
150 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
151 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
152 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
156 * We divy out chunks of memory rather than call malloc each time so
157 * we don't have to worry about leaking memory. It's probably
158 * not a big deal if all this memory was wasted but if this ever
159 * goes into a library that would probably not be a good idea.
161 * XXX - this *is* in a library....
164 #define CHUNK0SIZE 1024
170 static struct chunk chunks
[NCHUNKS
];
171 static int cur_chunk
;
173 static void *newchunk(u_int
);
174 static void freechunks(void);
175 static inline struct block
*new_block(int);
176 static inline struct slist
*new_stmt(int);
177 static struct block
*gen_retblk(int);
178 static inline void syntax(void);
180 static void backpatch(struct block
*, struct block
*);
181 static void merge(struct block
*, struct block
*);
182 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
183 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
184 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
185 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
186 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
187 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
189 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
190 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
191 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
192 static struct slist
*gen_load_llrel(u_int
, u_int
);
193 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
194 static struct slist
*gen_loadx_iphdrlen(void);
195 static struct block
*gen_uncond(int);
196 static inline struct block
*gen_true(void);
197 static inline struct block
*gen_false(void);
198 static struct block
*gen_ether_linktype(int);
199 static struct block
*gen_linux_sll_linktype(int);
200 static void insert_radiotap_load_llprefixlen(struct block
*);
201 static void insert_ppi_load_llprefixlen(struct block
*);
202 static void insert_load_llprefixlen(struct block
*);
203 static struct slist
*gen_llprefixlen(void);
204 static struct block
*gen_linktype(int);
205 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
206 static struct block
*gen_llc_linktype(int);
207 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
209 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
211 static struct block
*gen_ahostop(const u_char
*, int);
212 static struct block
*gen_ehostop(const u_char
*, int);
213 static struct block
*gen_fhostop(const u_char
*, int);
214 static struct block
*gen_thostop(const u_char
*, int);
215 static struct block
*gen_wlanhostop(const u_char
*, int);
216 static struct block
*gen_ipfchostop(const u_char
*, int);
217 static struct block
*gen_dnhostop(bpf_u_int32
, int);
218 static struct block
*gen_mpls_linktype(int);
219 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
221 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
224 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
226 static struct block
*gen_ipfrag(void);
227 static struct block
*gen_portatom(int, bpf_int32
);
228 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
230 static struct block
*gen_portatom6(int, bpf_int32
);
231 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
233 struct block
*gen_portop(int, int, int);
234 static struct block
*gen_port(int, int, int);
235 struct block
*gen_portrangeop(int, int, int, int);
236 static struct block
*gen_portrange(int, int, int, int);
238 struct block
*gen_portop6(int, int, int);
239 static struct block
*gen_port6(int, int, int);
240 struct block
*gen_portrangeop6(int, int, int, int);
241 static struct block
*gen_portrange6(int, int, int, int);
243 static int lookup_proto(const char *, int);
244 static struct block
*gen_protochain(int, int, int);
245 static struct block
*gen_proto(int, int, int);
246 static struct slist
*xfer_to_x(struct arth
*);
247 static struct slist
*xfer_to_a(struct arth
*);
248 static struct block
*gen_mac_multicast(int);
249 static struct block
*gen_len(int, int);
251 static struct block
*gen_ppi_dlt_check(void);
252 static struct block
*gen_msg_abbrev(int type
);
263 /* XXX Round up to nearest long. */
264 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
266 /* XXX Round up to structure boundary. */
270 cp
= &chunks
[cur_chunk
];
271 if (n
> cp
->n_left
) {
272 ++cp
, k
= ++cur_chunk
;
274 bpf_error("out of memory");
275 size
= CHUNK0SIZE
<< k
;
276 cp
->m
= (void *)malloc(size
);
278 bpf_error("out of memory");
279 memset((char *)cp
->m
, 0, size
);
282 bpf_error("out of memory");
285 return (void *)((char *)cp
->m
+ cp
->n_left
);
294 for (i
= 0; i
< NCHUNKS
; ++i
)
295 if (chunks
[i
].m
!= NULL
) {
302 * A strdup whose allocations are freed after code generation is over.
306 register const char *s
;
308 int n
= strlen(s
) + 1;
309 char *cp
= newchunk(n
);
315 static inline struct block
*
321 p
= (struct block
*)newchunk(sizeof(*p
));
328 static inline struct slist
*
334 p
= (struct slist
*)newchunk(sizeof(*p
));
340 static struct block
*
344 struct block
*b
= new_block(BPF_RET
|BPF_K
);
353 bpf_error("syntax error in filter expression");
356 static bpf_u_int32 netmask
;
361 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
362 const char *buf
, int optimize
, bpf_u_int32 mask
)
365 const char * volatile xbuf
= buf
;
372 if (setjmp(top_ctx
)) {
380 snaplen
= pcap_snapshot(p
);
382 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
383 "snaplen of 0 rejects all packets");
387 lex_init(xbuf
? xbuf
: "");
395 root
= gen_retblk(snaplen
);
397 if (optimize
&& !no_optimize
) {
400 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
401 bpf_error("expression rejects all packets");
403 program
->bf_insns
= icode_to_fcode(root
, &len
);
404 program
->bf_len
= len
;
412 * entry point for using the compiler with no pcap open
413 * pass in all the stuff that is needed explicitly instead.
416 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
417 struct bpf_program
*program
,
418 const char *buf
, int optimize
, bpf_u_int32 mask
)
423 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
426 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
432 * Clean up a "struct bpf_program" by freeing all the memory allocated
436 pcap_freecode(struct bpf_program
*program
)
439 if (program
->bf_insns
!= NULL
) {
440 free((char *)program
->bf_insns
);
441 program
->bf_insns
= NULL
;
446 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
447 * which of the jt and jf fields has been resolved and which is a pointer
448 * back to another unresolved block (or nil). At least one of the fields
449 * in each block is already resolved.
452 backpatch(list
, target
)
453 struct block
*list
, *target
;
470 * Merge the lists in b0 and b1, using the 'sense' field to indicate
471 * which of jt and jf is the link.
475 struct block
*b0
, *b1
;
477 register struct block
**p
= &b0
;
479 /* Find end of list. */
481 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
483 /* Concatenate the lists. */
491 struct block
*ppi_dlt_check
;
493 ppi_dlt_check
= gen_ppi_dlt_check();
495 if (ppi_dlt_check
!= NULL
)
497 gen_and(ppi_dlt_check
, p
);
500 backpatch(p
, gen_retblk(snaplen
));
501 p
->sense
= !p
->sense
;
502 backpatch(p
, gen_retblk(0));
506 * Insert before the statements of the first (root) block any
507 * statements needed to load the lengths of any variable-length
508 * headers into registers.
510 * XXX - a fancier strategy would be to insert those before the
511 * statements of all blocks that use those lengths and that
512 * have no predecessors that use them, so that we only compute
513 * the lengths if we need them. There might be even better
514 * approaches than that. However, as we're currently only
515 * handling variable-length radiotap headers, and as all
516 * filtering expressions other than raw link[M:N] tests
517 * require the length of that header, doing more for that
518 * header length isn't really worth the effort.
520 insert_load_llprefixlen(root
);
525 struct block
*b0
, *b1
;
527 backpatch(b0
, b1
->head
);
528 b0
->sense
= !b0
->sense
;
529 b1
->sense
= !b1
->sense
;
531 b1
->sense
= !b1
->sense
;
537 struct block
*b0
, *b1
;
539 b0
->sense
= !b0
->sense
;
540 backpatch(b0
, b1
->head
);
541 b0
->sense
= !b0
->sense
;
550 b
->sense
= !b
->sense
;
553 static struct block
*
554 gen_cmp(offrel
, offset
, size
, v
)
555 enum e_offrel offrel
;
559 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
562 static struct block
*
563 gen_cmp_gt(offrel
, offset
, size
, v
)
564 enum e_offrel offrel
;
568 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
571 static struct block
*
572 gen_cmp_ge(offrel
, offset
, size
, v
)
573 enum e_offrel offrel
;
577 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
580 static struct block
*
581 gen_cmp_lt(offrel
, offset
, size
, v
)
582 enum e_offrel offrel
;
586 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
589 static struct block
*
590 gen_cmp_le(offrel
, offset
, size
, v
)
591 enum e_offrel offrel
;
595 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
598 static struct block
*
599 gen_mcmp(offrel
, offset
, size
, v
, mask
)
600 enum e_offrel offrel
;
605 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
608 static struct block
*
609 gen_bcmp(offrel
, offset
, size
, v
)
610 enum e_offrel offrel
;
611 register u_int offset
, size
;
612 register const u_char
*v
;
614 register struct block
*b
, *tmp
;
618 register const u_char
*p
= &v
[size
- 4];
619 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
620 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
622 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
629 register const u_char
*p
= &v
[size
- 2];
630 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
632 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
639 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
648 * AND the field of size "size" at offset "offset" relative to the header
649 * specified by "offrel" with "mask", and compare it with the value "v"
650 * with the test specified by "jtype"; if "reverse" is true, the test
651 * should test the opposite of "jtype".
653 static struct block
*
654 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
655 enum e_offrel offrel
;
657 bpf_u_int32 offset
, size
, mask
, jtype
;
660 struct slist
*s
, *s2
;
663 s
= gen_load_a(offrel
, offset
, size
);
665 if (mask
!= 0xffffffff) {
666 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
671 b
= new_block(JMP(jtype
));
674 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
680 * Various code constructs need to know the layout of the data link
681 * layer. These variables give the necessary offsets from the beginning
682 * of the packet data.
684 * If the link layer has variable_length headers, the offsets are offsets
685 * from the end of the link-link-layer header, and "reg_ll_size" is
686 * the register number for a register containing the length of the
687 * link-layer header. Otherwise, "reg_ll_size" is -1.
689 static int reg_ll_size
;
692 * This is the offset of the beginning of the link-layer header from
693 * the beginning of the raw packet data.
695 * It's usually 0, except for 802.11 with a fixed-length radio header.
696 * (For 802.11 with a variable-length radio header, we have to generate
697 * code to compute that offset; off_ll is 0 in that case.)
702 * This is the offset of the beginning of the MAC-layer header.
703 * It's usually 0, except for ATM LANE, where it's the offset, relative
704 * to the beginning of the raw packet data, of the Ethernet header.
706 static u_int off_mac
;
709 * "off_linktype" is the offset to information in the link-layer header
710 * giving the packet type. This offset is relative to the beginning
711 * of the link-layer header (i.e., it doesn't include off_ll).
713 * For Ethernet, it's the offset of the Ethernet type field.
715 * For link-layer types that always use 802.2 headers, it's the
716 * offset of the LLC header.
718 * For PPP, it's the offset of the PPP type field.
720 * For Cisco HDLC, it's the offset of the CHDLC type field.
722 * For BSD loopback, it's the offset of the AF_ value.
724 * For Linux cooked sockets, it's the offset of the type field.
726 * It's set to -1 for no encapsulation, in which case, IP is assumed.
728 static u_int off_linktype
;
731 * TRUE if the link layer includes an ATM pseudo-header.
733 static int is_atm
= 0;
736 * TRUE if "lane" appeared in the filter; it causes us to generate
737 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
739 static int is_lane
= 0;
742 * These are offsets for the ATM pseudo-header.
744 static u_int off_vpi
;
745 static u_int off_vci
;
746 static u_int off_proto
;
749 * These are offsets for the MTP2 fields.
754 * These are offsets for the MTP3 fields.
756 static u_int off_sio
;
757 static u_int off_opc
;
758 static u_int off_dpc
;
759 static u_int off_sls
;
762 * This is the offset of the first byte after the ATM pseudo_header,
763 * or -1 if there is no ATM pseudo-header.
765 static u_int off_payload
;
768 * These are offsets to the beginning of the network-layer header.
769 * They are relative to the beginning of the link-layer header (i.e.,
770 * they don't include off_ll).
772 * If the link layer never uses 802.2 LLC:
774 * "off_nl" and "off_nl_nosnap" are the same.
776 * If the link layer always uses 802.2 LLC:
778 * "off_nl" is the offset if there's a SNAP header following
781 * "off_nl_nosnap" is the offset if there's no SNAP header.
783 * If the link layer is Ethernet:
785 * "off_nl" is the offset if the packet is an Ethernet II packet
786 * (we assume no 802.3+802.2+SNAP);
788 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
789 * with an 802.2 header following it.
792 static u_int off_nl_nosnap
;
800 linktype
= pcap_datalink(p
);
802 pcap_fddipad
= p
->fddipad
;
806 * Assume it's not raw ATM with a pseudo-header, for now.
817 * And assume we're not doing SS7.
826 * Also assume it's not 802.11 with a fixed-length radio header.
832 label_stack_depth
= 0;
840 off_nl
= 6; /* XXX in reality, variable! */
841 off_nl_nosnap
= 6; /* no 802.2 LLC */
844 case DLT_ARCNET_LINUX
:
846 off_nl
= 8; /* XXX in reality, variable! */
847 off_nl_nosnap
= 8; /* no 802.2 LLC */
852 off_nl
= 14; /* Ethernet II */
853 off_nl_nosnap
= 17; /* 802.3+802.2 */
858 * SLIP doesn't have a link level type. The 16 byte
859 * header is hacked into our SLIP driver.
863 off_nl_nosnap
= 16; /* no 802.2 LLC */
867 /* XXX this may be the same as the DLT_PPP_BSDOS case */
871 off_nl_nosnap
= 24; /* no 802.2 LLC */
878 off_nl_nosnap
= 4; /* no 802.2 LLC */
884 off_nl_nosnap
= 12; /* no 802.2 LLC */
889 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
890 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
893 off_nl_nosnap
= 4; /* no 802.2 LLC */
898 * This does no include the Ethernet header, and
899 * only covers session state.
903 off_nl_nosnap
= 8; /* no 802.2 LLC */
909 off_nl_nosnap
= 24; /* no 802.2 LLC */
914 * FDDI doesn't really have a link-level type field.
915 * We set "off_linktype" to the offset of the LLC header.
917 * To check for Ethernet types, we assume that SSAP = SNAP
918 * is being used and pick out the encapsulated Ethernet type.
919 * XXX - should we generate code to check for SNAP?
923 off_linktype
+= pcap_fddipad
;
925 off_nl
= 21; /* FDDI+802.2+SNAP */
926 off_nl_nosnap
= 16; /* FDDI+802.2 */
928 off_nl
+= pcap_fddipad
;
929 off_nl_nosnap
+= pcap_fddipad
;
935 * Token Ring doesn't really have a link-level type field.
936 * We set "off_linktype" to the offset of the LLC header.
938 * To check for Ethernet types, we assume that SSAP = SNAP
939 * is being used and pick out the encapsulated Ethernet type.
940 * XXX - should we generate code to check for SNAP?
942 * XXX - the header is actually variable-length.
943 * Some various Linux patched versions gave 38
944 * as "off_linktype" and 40 as "off_nl"; however,
945 * if a token ring packet has *no* routing
946 * information, i.e. is not source-routed, the correct
947 * values are 20 and 22, as they are in the vanilla code.
949 * A packet is source-routed iff the uppermost bit
950 * of the first byte of the source address, at an
951 * offset of 8, has the uppermost bit set. If the
952 * packet is source-routed, the total number of bytes
953 * of routing information is 2 plus bits 0x1F00 of
954 * the 16-bit value at an offset of 14 (shifted right
955 * 8 - figure out which byte that is).
958 off_nl
= 22; /* Token Ring+802.2+SNAP */
959 off_nl_nosnap
= 17; /* Token Ring+802.2 */
964 * 802.11 doesn't really have a link-level type field.
965 * We set "off_linktype" to the offset of the LLC header.
967 * To check for Ethernet types, we assume that SSAP = SNAP
968 * is being used and pick out the encapsulated Ethernet type.
969 * XXX - should we generate code to check for SNAP?
971 * XXX - the header is actually variable-length. We
972 * assume a 24-byte link-layer header, as appears in
973 * data frames in networks with no bridges. If the
974 * fromds and tods 802.11 header bits are both set,
975 * it's actually supposed to be 30 bytes.
978 off_nl
= 32; /* 802.11+802.2+SNAP */
979 off_nl_nosnap
= 27; /* 802.11+802.2 */
982 case DLT_PRISM_HEADER
:
984 * Same as 802.11, but with an additional header before
985 * the 802.11 header, containing a bunch of additional
986 * information including radio-level information.
988 * The header is 144 bytes long.
990 * XXX - same variable-length header problem; at least
991 * the Prism header is fixed-length.
995 off_nl
= 32; /* Prism+802.11+802.2+SNAP */
996 off_nl_nosnap
= 27; /* Prism+802.11+802.2 */
999 case DLT_IEEE802_11_RADIO_AVS
:
1001 * Same as 802.11, but with an additional header before
1002 * the 802.11 header, containing a bunch of additional
1003 * information including radio-level information.
1005 * The header is 64 bytes long, at least in its
1006 * current incarnation.
1008 * XXX - same variable-length header problem, only
1009 * more so; this header is also variable-length,
1010 * with the length being the 32-bit big-endian
1011 * number at an offset of 4 from the beginning
1012 * of the radio header. We should handle that the
1013 * same way we handle the length at the beginning
1014 * of the radiotap header.
1016 * XXX - in Linux, do any drivers that supply an AVS
1017 * header supply a link-layer type other than
1018 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1019 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1020 * any drivers that supply an AVS header but supply
1021 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1022 * have to check the header in the generated code to
1023 * determine whether it's Prism or AVS.
1027 off_nl
= 32; /* Radio+802.11+802.2+SNAP */
1028 off_nl_nosnap
= 27; /* Radio+802.11+802.2 */
1033 * At the moment we treat PPI as normal Radiotap encoded
1034 * packets. The difference is in the function that generates
1035 * the code at the beginning to compute the header length.
1036 * Since this code generator of PPI supports bare 802.11
1037 * encapsulation only (i.e. the encapsulated DLT should be
1038 * DLT_IEEE802_11) we generate code to check for this too.
1041 case DLT_IEEE802_11_RADIO
:
1043 * Same as 802.11, but with an additional header before
1044 * the 802.11 header, containing a bunch of additional
1045 * information including radio-level information.
1047 * The radiotap header is variable length, and we
1048 * generate code to compute its length and store it
1049 * in a register. These offsets are relative to the
1050 * beginning of the 802.11 header.
1053 off_nl
= 32; /* 802.11+802.2+SNAP */
1054 off_nl_nosnap
= 27; /* 802.11+802.2 */
1057 case DLT_ATM_RFC1483
:
1058 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1060 * assume routed, non-ISO PDUs
1061 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1063 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1064 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1065 * latter would presumably be treated the way PPPoE
1066 * should be, so you can do "pppoe and udp port 2049"
1067 * or "pppoa and tcp port 80" and have it check for
1068 * PPPo{A,E} and a PPP protocol of IP and....
1071 off_nl
= 8; /* 802.2+SNAP */
1072 off_nl_nosnap
= 3; /* 802.2 */
1077 * Full Frontal ATM; you get AALn PDUs with an ATM
1081 off_vpi
= SUNATM_VPI_POS
;
1082 off_vci
= SUNATM_VCI_POS
;
1083 off_proto
= PROTO_POS
;
1084 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1085 off_payload
= SUNATM_PKT_BEGIN_POS
;
1086 off_linktype
= off_payload
;
1087 off_nl
= off_payload
+8; /* 802.2+SNAP */
1088 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1094 off_nl_nosnap
= 0; /* no 802.2 LLC */
1097 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1100 off_nl_nosnap
= 16; /* no 802.2 LLC */
1105 * LocalTalk does have a 1-byte type field in the LLAP header,
1106 * but really it just indicates whether there is a "short" or
1107 * "long" DDP packet following.
1111 off_nl_nosnap
= 0; /* no 802.2 LLC */
1114 case DLT_IP_OVER_FC
:
1116 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1117 * link-level type field. We set "off_linktype" to the
1118 * offset of the LLC header.
1120 * To check for Ethernet types, we assume that SSAP = SNAP
1121 * is being used and pick out the encapsulated Ethernet type.
1122 * XXX - should we generate code to check for SNAP? RFC
1123 * 2625 says SNAP should be used.
1126 off_nl
= 24; /* IPFC+802.2+SNAP */
1127 off_nl_nosnap
= 19; /* IPFC+802.2 */
1132 * XXX - we should set this to handle SNAP-encapsulated
1133 * frames (NLPID of 0x80).
1137 off_nl_nosnap
= 0; /* no 802.2 LLC */
1141 * the only BPF-interesting FRF.16 frames are non-control frames;
1142 * Frame Relay has a variable length link-layer
1143 * so lets start with offset 4 for now and increments later on (FIXME);
1148 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1151 case DLT_APPLE_IP_OVER_IEEE1394
:
1154 off_nl_nosnap
= 18; /* no 802.2 LLC */
1157 case DLT_LINUX_IRDA
:
1159 * Currently, only raw "link[N:M]" filtering is supported.
1168 * Currently, only raw "link[N:M]" filtering is supported.
1175 case DLT_SYMANTEC_FIREWALL
:
1177 off_nl
= 44; /* Ethernet II */
1178 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1181 #ifdef HAVE_NET_PFVAR_H
1184 off_nl
= PFLOG_HDRLEN
;
1185 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1189 case DLT_JUNIPER_MFR
:
1190 case DLT_JUNIPER_MLFR
:
1191 case DLT_JUNIPER_MLPPP
:
1192 case DLT_JUNIPER_PPP
:
1193 case DLT_JUNIPER_CHDLC
:
1194 case DLT_JUNIPER_FRELAY
:
1197 off_nl_nosnap
= -1; /* no 802.2 LLC */
1200 case DLT_JUNIPER_ATM1
:
1201 off_linktype
= 4; /* in reality variable between 4-8 */
1206 case DLT_JUNIPER_ATM2
:
1207 off_linktype
= 8; /* in reality variable between 8-12 */
1212 /* frames captured on a Juniper PPPoE service PIC
1213 * contain raw ethernet frames */
1214 case DLT_JUNIPER_PPPOE
:
1215 case DLT_JUNIPER_ETHER
:
1217 off_nl
= 18; /* Ethernet II */
1218 off_nl_nosnap
= 21; /* 802.3+802.2 */
1221 case DLT_JUNIPER_PPPOE_ATM
:
1224 off_nl_nosnap
= -1; /* no 802.2 LLC */
1227 case DLT_JUNIPER_GGSN
:
1230 off_nl_nosnap
= -1; /* no 802.2 LLC */
1233 case DLT_JUNIPER_ES
:
1235 off_nl
= -1; /* not really a network layer but raw IP adresses */
1236 off_nl_nosnap
= -1; /* no 802.2 LLC */
1239 case DLT_JUNIPER_MONITOR
:
1241 off_nl
= 12; /* raw IP/IP6 header */
1242 off_nl_nosnap
= -1; /* no 802.2 LLC */
1245 case DLT_JUNIPER_SERVICES
:
1247 off_nl
= -1; /* L3 proto location dep. on cookie type */
1248 off_nl_nosnap
= -1; /* no 802.2 LLC */
1251 case DLT_JUNIPER_VP
:
1257 case DLT_JUNIPER_ST
:
1263 case DLT_JUNIPER_ISM
:
1280 case DLT_MTP2_WITH_PHDR
:
1299 case DLT_LINUX_LAPD
:
1301 * Currently, only raw "link[N:M]" filtering is supported.
1310 * Currently, only raw "link[N:M]" filtering is supported.
1317 case DLT_BLUETOOTH_HCI_H4
:
1319 * Currently, only raw "link[N:M]" filtering is supported.
1328 * Currently, only raw "link[N:M]" filtering is supported.
1337 * Currently, only raw "link[N:M]" filtering is supported.
1344 case DLT_IEEE802_15_4_LINUX
:
1346 * Currently, only raw "link[N:M]" filtering is supported.
1353 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1355 * Currently, only raw "link[N:M]" filtering is supported.
1362 case DLT_IEEE802_15_4
:
1364 * Currently, only raw "link[N:M]" filtering is supported.
1373 * Currently, only raw "link[N:M]" filtering is supported.
1382 * Currently, only raw "link[N:M]" filtering is supported.
1391 * Currently, only raw "link[N:M]" filtering is supported.
1398 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1400 * Currently, only raw "link[N:M]" filtering is supported.
1409 * Currently, only raw "link[N:M]" filtering is supported.
1411 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1412 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1413 off_nl_nosnap
= -1; /* no 802.2 LLC */
1414 off_mac
= 1; /* step over the kiss length byte */
1417 bpf_error("unknown data link type %d", linktype
);
1422 * Load a value relative to the beginning of the link-layer header.
1423 * The link-layer header doesn't necessarily begin at the beginning
1424 * of the packet data; there might be a variable-length prefix containing
1425 * radio information.
1427 static struct slist
*
1428 gen_load_llrel(offset
, size
)
1431 struct slist
*s
, *s2
;
1433 s
= gen_llprefixlen();
1436 * If "s" is non-null, it has code to arrange that the X register
1437 * contains the length of the prefix preceding the link-layer
1440 * Otherwise, the length of the prefix preceding the link-layer
1441 * header is "off_ll".
1445 * There's a variable-length prefix preceding the
1446 * link-layer header. "s" points to a list of statements
1447 * that put the length of that prefix into the X register.
1448 * do an indirect load, to use the X register as an offset.
1450 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1455 * There is no variable-length header preceding the
1456 * link-layer header; add in off_ll, which, if there's
1457 * a fixed-length header preceding the link-layer header,
1458 * is the length of that header.
1460 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1461 s
->s
.k
= offset
+ off_ll
;
1467 * Load a value relative to the beginning of the specified header.
1469 static struct slist
*
1470 gen_load_a(offrel
, offset
, size
)
1471 enum e_offrel offrel
;
1474 struct slist
*s
, *s2
;
1479 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1484 s
= gen_load_llrel(offset
, size
);
1488 s
= gen_load_llrel(off_nl
+ offset
, size
);
1492 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1497 * Load the X register with the length of the IPv4 header
1498 * (plus the offset of the link-layer header, if it's
1499 * preceded by a variable-length header such as a radio
1500 * header), in bytes.
1502 s
= gen_loadx_iphdrlen();
1505 * Load the item at {offset of the link-layer header} +
1506 * {offset, relative to the start of the link-layer
1507 * header, of the IPv4 header} + {length of the IPv4 header} +
1508 * {specified offset}.
1510 * (If the link-layer is variable-length, it's included
1511 * in the value in the X register, and off_ll is 0.)
1513 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1514 s2
->s
.k
= off_ll
+ off_nl
+ offset
;
1519 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1530 * Generate code to load into the X register the sum of the length of
1531 * the IPv4 header and any variable-length header preceding the link-layer
1534 static struct slist
*
1535 gen_loadx_iphdrlen()
1537 struct slist
*s
, *s2
;
1539 s
= gen_llprefixlen();
1542 * There's a variable-length prefix preceding the
1543 * link-layer header. "s" points to a list of statements
1544 * that put the length of that prefix into the X register.
1545 * The 4*([k]&0xf) addressing mode can't be used, as we
1546 * don't have a constant offset, so we have to load the
1547 * value in question into the A register and add to it
1548 * the value from the X register.
1550 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1553 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1556 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1561 * The A register now contains the length of the
1562 * IP header. We need to add to it the length
1563 * of the prefix preceding the link-layer
1564 * header, which is still in the X register, and
1565 * move the result into the X register.
1567 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1568 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1571 * There is no variable-length header preceding the
1572 * link-layer header; add in off_ll, which, if there's
1573 * a fixed-length header preceding the link-layer header,
1574 * is the length of that header.
1576 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1577 s
->s
.k
= off_ll
+ off_nl
;
1582 static struct block
*
1589 s
= new_stmt(BPF_LD
|BPF_IMM
);
1591 b
= new_block(JMP(BPF_JEQ
));
1597 static inline struct block
*
1600 return gen_uncond(1);
1603 static inline struct block
*
1606 return gen_uncond(0);
1610 * Byte-swap a 32-bit number.
1611 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1612 * big-endian platforms.)
1614 #define SWAPLONG(y) \
1615 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1618 * Generate code to match a particular packet type.
1620 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1621 * value, if <= ETHERMTU. We use that to determine whether to
1622 * match the type/length field or to check the type/length field for
1623 * a value <= ETHERMTU to see whether it's a type field and then do
1624 * the appropriate test.
1626 static struct block
*
1627 gen_ether_linktype(proto
)
1630 struct block
*b0
, *b1
;
1636 case LLCSAP_NETBEUI
:
1638 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1639 * so we check the DSAP and SSAP.
1641 * LLCSAP_IP checks for IP-over-802.2, rather
1642 * than IP-over-Ethernet or IP-over-SNAP.
1644 * XXX - should we check both the DSAP and the
1645 * SSAP, like this, or should we check just the
1646 * DSAP, as we do for other types <= ETHERMTU
1647 * (i.e., other SAP values)?
1649 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1651 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1652 ((proto
<< 8) | proto
));
1660 * Ethernet_II frames, which are Ethernet
1661 * frames with a frame type of ETHERTYPE_IPX;
1663 * Ethernet_802.3 frames, which are 802.3
1664 * frames (i.e., the type/length field is
1665 * a length field, <= ETHERMTU, rather than
1666 * a type field) with the first two bytes
1667 * after the Ethernet/802.3 header being
1670 * Ethernet_802.2 frames, which are 802.3
1671 * frames with an 802.2 LLC header and
1672 * with the IPX LSAP as the DSAP in the LLC
1675 * Ethernet_SNAP frames, which are 802.3
1676 * frames with an LLC header and a SNAP
1677 * header and with an OUI of 0x000000
1678 * (encapsulated Ethernet) and a protocol
1679 * ID of ETHERTYPE_IPX in the SNAP header.
1681 * XXX - should we generate the same code both
1682 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1686 * This generates code to check both for the
1687 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1689 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1690 (bpf_int32
)LLCSAP_IPX
);
1691 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1696 * Now we add code to check for SNAP frames with
1697 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1699 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1703 * Now we generate code to check for 802.3
1704 * frames in general.
1706 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1710 * Now add the check for 802.3 frames before the
1711 * check for Ethernet_802.2 and Ethernet_802.3,
1712 * as those checks should only be done on 802.3
1713 * frames, not on Ethernet frames.
1718 * Now add the check for Ethernet_II frames, and
1719 * do that before checking for the other frame
1722 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1723 (bpf_int32
)ETHERTYPE_IPX
);
1727 case ETHERTYPE_ATALK
:
1728 case ETHERTYPE_AARP
:
1730 * EtherTalk (AppleTalk protocols on Ethernet link
1731 * layer) may use 802.2 encapsulation.
1735 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1736 * we check for an Ethernet type field less than
1737 * 1500, which means it's an 802.3 length field.
1739 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1743 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1744 * SNAP packets with an organization code of
1745 * 0x080007 (Apple, for Appletalk) and a protocol
1746 * type of ETHERTYPE_ATALK (Appletalk).
1748 * 802.2-encapsulated ETHERTYPE_AARP packets are
1749 * SNAP packets with an organization code of
1750 * 0x000000 (encapsulated Ethernet) and a protocol
1751 * type of ETHERTYPE_AARP (Appletalk ARP).
1753 if (proto
== ETHERTYPE_ATALK
)
1754 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1755 else /* proto == ETHERTYPE_AARP */
1756 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1760 * Check for Ethernet encapsulation (Ethertalk
1761 * phase 1?); we just check for the Ethernet
1764 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1770 if (proto
<= ETHERMTU
) {
1772 * This is an LLC SAP value, so the frames
1773 * that match would be 802.2 frames.
1774 * Check that the frame is an 802.2 frame
1775 * (i.e., that the length/type field is
1776 * a length field, <= ETHERMTU) and
1777 * then check the DSAP.
1779 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1781 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1787 * This is an Ethernet type, so compare
1788 * the length/type field with it (if
1789 * the frame is an 802.2 frame, the length
1790 * field will be <= ETHERMTU, and, as
1791 * "proto" is > ETHERMTU, this test
1792 * will fail and the frame won't match,
1793 * which is what we want).
1795 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1802 * Generate code to match a particular packet type.
1804 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1805 * value, if <= ETHERMTU. We use that to determine whether to
1806 * match the type field or to check the type field for the special
1807 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1809 static struct block
*
1810 gen_linux_sll_linktype(proto
)
1813 struct block
*b0
, *b1
;
1819 case LLCSAP_NETBEUI
:
1821 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1822 * so we check the DSAP and SSAP.
1824 * LLCSAP_IP checks for IP-over-802.2, rather
1825 * than IP-over-Ethernet or IP-over-SNAP.
1827 * XXX - should we check both the DSAP and the
1828 * SSAP, like this, or should we check just the
1829 * DSAP, as we do for other types <= ETHERMTU
1830 * (i.e., other SAP values)?
1832 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1833 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1834 ((proto
<< 8) | proto
));
1840 * Ethernet_II frames, which are Ethernet
1841 * frames with a frame type of ETHERTYPE_IPX;
1843 * Ethernet_802.3 frames, which have a frame
1844 * type of LINUX_SLL_P_802_3;
1846 * Ethernet_802.2 frames, which are 802.3
1847 * frames with an 802.2 LLC header (i.e, have
1848 * a frame type of LINUX_SLL_P_802_2) and
1849 * with the IPX LSAP as the DSAP in the LLC
1852 * Ethernet_SNAP frames, which are 802.3
1853 * frames with an LLC header and a SNAP
1854 * header and with an OUI of 0x000000
1855 * (encapsulated Ethernet) and a protocol
1856 * ID of ETHERTYPE_IPX in the SNAP header.
1858 * First, do the checks on LINUX_SLL_P_802_2
1859 * frames; generate the check for either
1860 * Ethernet_802.2 or Ethernet_SNAP frames, and
1861 * then put a check for LINUX_SLL_P_802_2 frames
1864 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1865 (bpf_int32
)LLCSAP_IPX
);
1866 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1869 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1873 * Now check for 802.3 frames and OR that with
1874 * the previous test.
1876 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1880 * Now add the check for Ethernet_II frames, and
1881 * do that before checking for the other frame
1884 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1885 (bpf_int32
)ETHERTYPE_IPX
);
1889 case ETHERTYPE_ATALK
:
1890 case ETHERTYPE_AARP
:
1892 * EtherTalk (AppleTalk protocols on Ethernet link
1893 * layer) may use 802.2 encapsulation.
1897 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1898 * we check for the 802.2 protocol type in the
1899 * "Ethernet type" field.
1901 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1904 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1905 * SNAP packets with an organization code of
1906 * 0x080007 (Apple, for Appletalk) and a protocol
1907 * type of ETHERTYPE_ATALK (Appletalk).
1909 * 802.2-encapsulated ETHERTYPE_AARP packets are
1910 * SNAP packets with an organization code of
1911 * 0x000000 (encapsulated Ethernet) and a protocol
1912 * type of ETHERTYPE_AARP (Appletalk ARP).
1914 if (proto
== ETHERTYPE_ATALK
)
1915 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1917 else /* proto == ETHERTYPE_AARP */
1918 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1923 * Check for Ethernet encapsulation (Ethertalk
1924 * phase 1?); we just check for the Ethernet
1927 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1933 if (proto
<= ETHERMTU
) {
1935 * This is an LLC SAP value, so the frames
1936 * that match would be 802.2 frames.
1937 * Check for the 802.2 protocol type
1938 * in the "Ethernet type" field, and
1939 * then check the DSAP.
1941 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1943 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1949 * This is an Ethernet type, so compare
1950 * the length/type field with it (if
1951 * the frame is an 802.2 frame, the length
1952 * field will be <= ETHERMTU, and, as
1953 * "proto" is > ETHERMTU, this test
1954 * will fail and the frame won't match,
1955 * which is what we want).
1957 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1964 insert_radiotap_load_llprefixlen(b
)
1967 struct slist
*s1
, *s2
;
1970 * Prepend to the statements in this block code to load the
1971 * length of the radiotap header into the register assigned
1972 * to hold that length, if one has been assigned.
1974 if (reg_ll_size
!= -1) {
1976 * The 2 bytes at offsets of 2 and 3 from the beginning
1977 * of the radiotap header are the length of the radiotap
1978 * header; unfortunately, it's little-endian, so we have
1979 * to load it a byte at a time and construct the value.
1983 * Load the high-order byte, at an offset of 3, shift it
1984 * left a byte, and put the result in the X register.
1986 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1988 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1991 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1995 * Load the next byte, at an offset of 2, and OR the
1996 * value from the X register into it.
1998 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2001 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2005 * Now allocate a register to hold that value and store
2008 s2
= new_stmt(BPF_ST
);
2009 s2
->s
.k
= reg_ll_size
;
2013 * Now move it into the X register.
2015 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2019 * Now append all the existing statements in this
2020 * block to these statements.
2022 sappend(s1
, b
->stmts
);
2028 * At the moment we treat PPI as normal Radiotap encoded
2029 * packets. The difference is in the function that generates
2030 * the code at the beginning to compute the header length.
2031 * Since this code generator of PPI supports bare 802.11
2032 * encapsulation only (i.e. the encapsulated DLT should be
2033 * DLT_IEEE802_11) we generate code to check for this too.
2036 insert_ppi_load_llprefixlen(b
)
2039 struct slist
*s1
, *s2
;
2042 * Prepend to the statements in this block code to load the
2043 * length of the radiotap header into the register assigned
2044 * to hold that length, if one has been assigned.
2046 if (reg_ll_size
!= -1) {
2048 * The 2 bytes at offsets of 2 and 3 from the beginning
2049 * of the radiotap header are the length of the radiotap
2050 * header; unfortunately, it's little-endian, so we have
2051 * to load it a byte at a time and construct the value.
2055 * Load the high-order byte, at an offset of 3, shift it
2056 * left a byte, and put the result in the X register.
2058 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2060 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2063 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2067 * Load the next byte, at an offset of 2, and OR the
2068 * value from the X register into it.
2070 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2073 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2077 * Now allocate a register to hold that value and store
2080 s2
= new_stmt(BPF_ST
);
2081 s2
->s
.k
= reg_ll_size
;
2085 * Now move it into the X register.
2087 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2091 * Now append all the existing statements in this
2092 * block to these statements.
2094 sappend(s1
, b
->stmts
);
2100 static struct block
*
2101 gen_ppi_dlt_check(void)
2103 struct slist
*s_load_dlt
;
2106 if (linktype
== DLT_PPI
)
2108 /* Create the statements that check for the DLT
2110 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2111 s_load_dlt
->s
.k
= 4;
2113 b
= new_block(JMP(BPF_JEQ
));
2115 b
->stmts
= s_load_dlt
;
2116 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2127 insert_load_llprefixlen(b
)
2133 * At the moment we treat PPI as normal Radiotap encoded
2134 * packets. The difference is in the function that generates
2135 * the code at the beginning to compute the header length.
2136 * Since this code generator of PPI supports bare 802.11
2137 * encapsulation only (i.e. the encapsulated DLT should be
2138 * DLT_IEEE802_11) we generate code to check for this too.
2141 insert_ppi_load_llprefixlen(b
);
2144 case DLT_IEEE802_11_RADIO
:
2145 insert_radiotap_load_llprefixlen(b
);
2151 static struct slist
*
2152 gen_radiotap_llprefixlen(void)
2156 if (reg_ll_size
== -1) {
2158 * We haven't yet assigned a register for the length
2159 * of the radiotap header; allocate one.
2161 reg_ll_size
= alloc_reg();
2165 * Load the register containing the radiotap length
2166 * into the X register.
2168 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2169 s
->s
.k
= reg_ll_size
;
2174 * At the moment we treat PPI as normal Radiotap encoded
2175 * packets. The difference is in the function that generates
2176 * the code at the beginning to compute the header length.
2177 * Since this code generator of PPI supports bare 802.11
2178 * encapsulation only (i.e. the encapsulated DLT should be
2179 * DLT_IEEE802_11) we generate code to check for this too.
2181 static struct slist
*
2182 gen_ppi_llprefixlen(void)
2186 if (reg_ll_size
== -1) {
2188 * We haven't yet assigned a register for the length
2189 * of the radiotap header; allocate one.
2191 reg_ll_size
= alloc_reg();
2195 * Load the register containing the radiotap length
2196 * into the X register.
2198 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2199 s
->s
.k
= reg_ll_size
;
2206 * Generate code to compute the link-layer header length, if necessary,
2207 * putting it into the X register, and to return either a pointer to a
2208 * "struct slist" for the list of statements in that code, or NULL if
2209 * no code is necessary.
2211 static struct slist
*
2212 gen_llprefixlen(void)
2217 return gen_ppi_llprefixlen();
2220 case DLT_IEEE802_11_RADIO
:
2221 return gen_radiotap_llprefixlen();
2229 * Generate code to match a particular packet type by matching the
2230 * link-layer type field or fields in the 802.2 LLC header.
2232 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2233 * value, if <= ETHERMTU.
2235 static struct block
*
2239 struct block
*b0
, *b1
, *b2
;
2241 /* are we checking MPLS-encapsulated packets? */
2242 if (label_stack_depth
> 0) {
2246 /* FIXME add other L3 proto IDs */
2247 return gen_mpls_linktype(Q_IP
);
2249 case ETHERTYPE_IPV6
:
2251 /* FIXME add other L3 proto IDs */
2252 return gen_mpls_linktype(Q_IPV6
);
2255 bpf_error("unsupported protocol over mpls");
2263 return gen_ether_linktype(proto
);
2271 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2275 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2285 case DLT_IEEE802_11
:
2286 case DLT_IEEE802_11_RADIO_AVS
:
2287 case DLT_IEEE802_11_RADIO
:
2288 case DLT_PRISM_HEADER
:
2289 case DLT_ATM_RFC1483
:
2291 case DLT_IP_OVER_FC
:
2292 return gen_llc_linktype(proto
);
2298 * If "is_lane" is set, check for a LANE-encapsulated
2299 * version of this protocol, otherwise check for an
2300 * LLC-encapsulated version of this protocol.
2302 * We assume LANE means Ethernet, not Token Ring.
2306 * Check that the packet doesn't begin with an
2307 * LE Control marker. (We've already generated
2310 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2315 * Now generate an Ethernet test.
2317 b1
= gen_ether_linktype(proto
);
2322 * Check for LLC encapsulation and then check the
2325 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2326 b1
= gen_llc_linktype(proto
);
2334 return gen_linux_sll_linktype(proto
);
2339 case DLT_SLIP_BSDOS
:
2342 * These types don't provide any type field; packets
2343 * are always IPv4 or IPv6.
2345 * XXX - for IPv4, check for a version number of 4, and,
2346 * for IPv6, check for a version number of 6?
2351 /* Check for a version number of 4. */
2352 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2354 case ETHERTYPE_IPV6
:
2355 /* Check for a version number of 6. */
2356 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2360 return gen_false(); /* always false */
2367 case DLT_PPP_SERIAL
:
2370 * We use Ethernet protocol types inside libpcap;
2371 * map them to the corresponding PPP protocol types.
2380 case ETHERTYPE_IPV6
:
2389 case ETHERTYPE_ATALK
:
2403 * I'm assuming the "Bridging PDU"s that go
2404 * over PPP are Spanning Tree Protocol
2418 * We use Ethernet protocol types inside libpcap;
2419 * map them to the corresponding PPP protocol types.
2424 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2425 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2427 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2432 case ETHERTYPE_IPV6
:
2442 case ETHERTYPE_ATALK
:
2456 * I'm assuming the "Bridging PDU"s that go
2457 * over PPP are Spanning Tree Protocol
2473 * For DLT_NULL, the link-layer header is a 32-bit
2474 * word containing an AF_ value in *host* byte order,
2475 * and for DLT_ENC, the link-layer header begins
2476 * with a 32-bit work containing an AF_ value in
2479 * In addition, if we're reading a saved capture file,
2480 * the host byte order in the capture may not be the
2481 * same as the host byte order on this machine.
2483 * For DLT_LOOP, the link-layer header is a 32-bit
2484 * word containing an AF_ value in *network* byte order.
2486 * XXX - AF_ values may, unfortunately, be platform-
2487 * dependent; for example, FreeBSD's AF_INET6 is 24
2488 * whilst NetBSD's and OpenBSD's is 26.
2490 * This means that, when reading a capture file, just
2491 * checking for our AF_INET6 value won't work if the
2492 * capture file came from another OS.
2501 case ETHERTYPE_IPV6
:
2508 * Not a type on which we support filtering.
2509 * XXX - support those that have AF_ values
2510 * #defined on this platform, at least?
2515 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2517 * The AF_ value is in host byte order, but
2518 * the BPF interpreter will convert it to
2519 * network byte order.
2521 * If this is a save file, and it's from a
2522 * machine with the opposite byte order to
2523 * ours, we byte-swap the AF_ value.
2525 * Then we run it through "htonl()", and
2526 * generate code to compare against the result.
2528 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2529 bpf_pcap
->sf
.swapped
)
2530 proto
= SWAPLONG(proto
);
2531 proto
= htonl(proto
);
2533 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2535 #ifdef HAVE_NET_PFVAR_H
2538 * af field is host byte order in contrast to the rest of
2541 if (proto
== ETHERTYPE_IP
)
2542 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2543 BPF_B
, (bpf_int32
)AF_INET
));
2545 else if (proto
== ETHERTYPE_IPV6
)
2546 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2547 BPF_B
, (bpf_int32
)AF_INET6
));
2553 #endif /* HAVE_NET_PFVAR_H */
2556 case DLT_ARCNET_LINUX
:
2558 * XXX should we check for first fragment if the protocol
2567 case ETHERTYPE_IPV6
:
2568 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2569 (bpf_int32
)ARCTYPE_INET6
));
2573 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2574 (bpf_int32
)ARCTYPE_IP
);
2575 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2576 (bpf_int32
)ARCTYPE_IP_OLD
);
2581 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2582 (bpf_int32
)ARCTYPE_ARP
);
2583 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2584 (bpf_int32
)ARCTYPE_ARP_OLD
);
2588 case ETHERTYPE_REVARP
:
2589 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2590 (bpf_int32
)ARCTYPE_REVARP
));
2592 case ETHERTYPE_ATALK
:
2593 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2594 (bpf_int32
)ARCTYPE_ATALK
));
2601 case ETHERTYPE_ATALK
:
2611 * XXX - assumes a 2-byte Frame Relay header with
2612 * DLCI and flags. What if the address is longer?
2618 * Check for the special NLPID for IP.
2620 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2623 case ETHERTYPE_IPV6
:
2625 * Check for the special NLPID for IPv6.
2627 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2632 * Check for several OSI protocols.
2634 * Frame Relay packets typically have an OSI
2635 * NLPID at the beginning; we check for each
2638 * What we check for is the NLPID and a frame
2639 * control field of UI, i.e. 0x03 followed
2642 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2643 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2644 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2655 case DLT_JUNIPER_MFR
:
2656 case DLT_JUNIPER_MLFR
:
2657 case DLT_JUNIPER_MLPPP
:
2658 case DLT_JUNIPER_ATM1
:
2659 case DLT_JUNIPER_ATM2
:
2660 case DLT_JUNIPER_PPPOE
:
2661 case DLT_JUNIPER_PPPOE_ATM
:
2662 case DLT_JUNIPER_GGSN
:
2663 case DLT_JUNIPER_ES
:
2664 case DLT_JUNIPER_MONITOR
:
2665 case DLT_JUNIPER_SERVICES
:
2666 case DLT_JUNIPER_ETHER
:
2667 case DLT_JUNIPER_PPP
:
2668 case DLT_JUNIPER_FRELAY
:
2669 case DLT_JUNIPER_CHDLC
:
2670 case DLT_JUNIPER_VP
:
2671 case DLT_JUNIPER_ST
:
2672 case DLT_JUNIPER_ISM
:
2673 /* just lets verify the magic number for now -
2674 * on ATM we may have up to 6 different encapsulations on the wire
2675 * and need a lot of heuristics to figure out that the payload
2678 * FIXME encapsulation specific BPF_ filters
2680 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2682 case DLT_LINUX_IRDA
:
2683 bpf_error("IrDA link-layer type filtering not implemented");
2686 bpf_error("DOCSIS link-layer type filtering not implemented");
2688 case DLT_LINUX_LAPD
:
2689 bpf_error("LAPD link-layer type filtering not implemented");
2692 bpf_error("AX.25 link-layer type filtering not implemented");
2696 * All the types that have no encapsulation should either be
2697 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2698 * all packets are IP packets, or should be handled in some
2699 * special case, if none of them are (if some are and some
2700 * aren't, the lack of encapsulation is a problem, as we'd
2701 * have to find some other way of determining the packet type).
2703 * Therefore, if "off_linktype" is -1, there's an error.
2705 if (off_linktype
== (u_int
)-1)
2709 * Any type not handled above should always have an Ethernet
2710 * type at an offset of "off_linktype". (PPP is partially
2711 * handled above - the protocol type is mapped from the
2712 * Ethernet and LLC types we use internally to the corresponding
2713 * PPP type - but the PPP type is always specified by a value
2714 * at "off_linktype", so we don't have to do the code generation
2717 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2721 * Check for an LLC SNAP packet with a given organization code and
2722 * protocol type; we check the entire contents of the 802.2 LLC and
2723 * snap headers, checking for DSAP and SSAP of SNAP and a control
2724 * field of 0x03 in the LLC header, and for the specified organization
2725 * code and protocol type in the SNAP header.
2727 static struct block
*
2728 gen_snap(orgcode
, ptype
, offset
)
2729 bpf_u_int32 orgcode
;
2733 u_char snapblock
[8];
2735 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2736 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2737 snapblock
[2] = 0x03; /* control = UI */
2738 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2739 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2740 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2741 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2742 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2743 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2747 * Generate code to match a particular packet type, for link-layer types
2748 * using 802.2 LLC headers.
2750 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2751 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2753 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2754 * value, if <= ETHERMTU. We use that to determine whether to
2755 * match the DSAP or both DSAP and LSAP or to check the OUI and
2756 * protocol ID in a SNAP header.
2758 static struct block
*
2759 gen_llc_linktype(proto
)
2763 * XXX - handle token-ring variable-length header.
2769 case LLCSAP_NETBEUI
:
2771 * XXX - should we check both the DSAP and the
2772 * SSAP, like this, or should we check just the
2773 * DSAP, as we do for other types <= ETHERMTU
2774 * (i.e., other SAP values)?
2776 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2777 ((proto
<< 8) | proto
));
2781 * XXX - are there ever SNAP frames for IPX on
2782 * non-Ethernet 802.x networks?
2784 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2785 (bpf_int32
)LLCSAP_IPX
);
2787 case ETHERTYPE_ATALK
:
2789 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2790 * SNAP packets with an organization code of
2791 * 0x080007 (Apple, for Appletalk) and a protocol
2792 * type of ETHERTYPE_ATALK (Appletalk).
2794 * XXX - check for an organization code of
2795 * encapsulated Ethernet as well?
2797 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2801 * XXX - we don't have to check for IPX 802.3
2802 * here, but should we check for the IPX Ethertype?
2804 if (proto
<= ETHERMTU
) {
2806 * This is an LLC SAP value, so check
2809 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2813 * This is an Ethernet type; we assume that it's
2814 * unlikely that it'll appear in the right place
2815 * at random, and therefore check only the
2816 * location that would hold the Ethernet type
2817 * in a SNAP frame with an organization code of
2818 * 0x000000 (encapsulated Ethernet).
2820 * XXX - if we were to check for the SNAP DSAP and
2821 * LSAP, as per XXX, and were also to check for an
2822 * organization code of 0x000000 (encapsulated
2823 * Ethernet), we'd do
2825 * return gen_snap(0x000000, proto,
2828 * here; for now, we don't, as per the above.
2829 * I don't know whether it's worth the extra CPU
2830 * time to do the right check or not.
2832 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2838 static struct block
*
2839 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2843 u_int src_off
, dst_off
;
2845 struct block
*b0
, *b1
;
2859 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2860 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2866 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2867 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2874 b0
= gen_linktype(proto
);
2875 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2881 static struct block
*
2882 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2883 struct in6_addr
*addr
;
2884 struct in6_addr
*mask
;
2886 u_int src_off
, dst_off
;
2888 struct block
*b0
, *b1
;
2903 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2904 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2910 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2911 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2918 /* this order is important */
2919 a
= (u_int32_t
*)addr
;
2920 m
= (u_int32_t
*)mask
;
2921 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2922 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2924 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2926 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2928 b0
= gen_linktype(proto
);
2934 static struct block
*
2935 gen_ehostop(eaddr
, dir
)
2936 register const u_char
*eaddr
;
2939 register struct block
*b0
, *b1
;
2943 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2946 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2949 b0
= gen_ehostop(eaddr
, Q_SRC
);
2950 b1
= gen_ehostop(eaddr
, Q_DST
);
2956 b0
= gen_ehostop(eaddr
, Q_SRC
);
2957 b1
= gen_ehostop(eaddr
, Q_DST
);
2966 * Like gen_ehostop, but for DLT_FDDI
2968 static struct block
*
2969 gen_fhostop(eaddr
, dir
)
2970 register const u_char
*eaddr
;
2973 struct block
*b0
, *b1
;
2978 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2980 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2985 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2987 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2991 b0
= gen_fhostop(eaddr
, Q_SRC
);
2992 b1
= gen_fhostop(eaddr
, Q_DST
);
2998 b0
= gen_fhostop(eaddr
, Q_SRC
);
2999 b1
= gen_fhostop(eaddr
, Q_DST
);
3008 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3010 static struct block
*
3011 gen_thostop(eaddr
, dir
)
3012 register const u_char
*eaddr
;
3015 register struct block
*b0
, *b1
;
3019 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3022 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3025 b0
= gen_thostop(eaddr
, Q_SRC
);
3026 b1
= gen_thostop(eaddr
, Q_DST
);
3032 b0
= gen_thostop(eaddr
, Q_SRC
);
3033 b1
= gen_thostop(eaddr
, Q_DST
);
3042 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
3044 static struct block
*
3045 gen_wlanhostop(eaddr
, dir
)
3046 register const u_char
*eaddr
;
3049 register struct block
*b0
, *b1
, *b2
;
3050 register struct slist
*s
;
3057 * For control frames, there is no SA.
3059 * For management frames, SA is at an
3060 * offset of 10 from the beginning of
3063 * For data frames, SA is at an offset
3064 * of 10 from the beginning of the packet
3065 * if From DS is clear, at an offset of
3066 * 16 from the beginning of the packet
3067 * if From DS is set and To DS is clear,
3068 * and an offset of 24 from the beginning
3069 * of the packet if From DS is set and To DS
3074 * Generate the tests to be done for data frames
3077 * First, check for To DS set, i.e. check "link[1] & 0x01".
3079 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3080 b1
= new_block(JMP(BPF_JSET
));
3081 b1
->s
.k
= 0x01; /* To DS */
3085 * If To DS is set, the SA is at 24.
3087 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3091 * Now, check for To DS not set, i.e. check
3092 * "!(link[1] & 0x01)".
3094 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3095 b2
= new_block(JMP(BPF_JSET
));
3096 b2
->s
.k
= 0x01; /* To DS */
3101 * If To DS is not set, the SA is at 16.
3103 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3107 * Now OR together the last two checks. That gives
3108 * the complete set of checks for data frames with
3114 * Now check for From DS being set, and AND that with
3115 * the ORed-together checks.
3117 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3118 b1
= new_block(JMP(BPF_JSET
));
3119 b1
->s
.k
= 0x02; /* From DS */
3124 * Now check for data frames with From DS not set.
3126 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3127 b2
= new_block(JMP(BPF_JSET
));
3128 b2
->s
.k
= 0x02; /* From DS */
3133 * If From DS isn't set, the SA is at 10.
3135 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3139 * Now OR together the checks for data frames with
3140 * From DS not set and for data frames with From DS
3141 * set; that gives the checks done for data frames.
3146 * Now check for a data frame.
3147 * I.e, check "link[0] & 0x08".
3149 gen_load_a(OR_LINK
, 0, BPF_B
);
3150 b1
= new_block(JMP(BPF_JSET
));
3155 * AND that with the checks done for data frames.
3160 * If the high-order bit of the type value is 0, this
3161 * is a management frame.
3162 * I.e, check "!(link[0] & 0x08)".
3164 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3165 b2
= new_block(JMP(BPF_JSET
));
3171 * For management frames, the SA is at 10.
3173 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3177 * OR that with the checks done for data frames.
3178 * That gives the checks done for management and
3184 * If the low-order bit of the type value is 1,
3185 * this is either a control frame or a frame
3186 * with a reserved type, and thus not a
3189 * I.e., check "!(link[0] & 0x04)".
3191 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3192 b1
= new_block(JMP(BPF_JSET
));
3198 * AND that with the checks for data and management
3208 * For control frames, there is no DA.
3210 * For management frames, DA is at an
3211 * offset of 4 from the beginning of
3214 * For data frames, DA is at an offset
3215 * of 4 from the beginning of the packet
3216 * if To DS is clear and at an offset of
3217 * 16 from the beginning of the packet
3222 * Generate the tests to be done for data frames.
3224 * First, check for To DS set, i.e. "link[1] & 0x01".
3226 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3227 b1
= new_block(JMP(BPF_JSET
));
3228 b1
->s
.k
= 0x01; /* To DS */
3232 * If To DS is set, the DA is at 16.
3234 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3238 * Now, check for To DS not set, i.e. check
3239 * "!(link[1] & 0x01)".
3241 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3242 b2
= new_block(JMP(BPF_JSET
));
3243 b2
->s
.k
= 0x01; /* To DS */
3248 * If To DS is not set, the DA is at 4.
3250 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3254 * Now OR together the last two checks. That gives
3255 * the complete set of checks for data frames.
3260 * Now check for a data frame.
3261 * I.e, check "link[0] & 0x08".
3263 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3264 b1
= new_block(JMP(BPF_JSET
));
3269 * AND that with the checks done for data frames.
3274 * If the high-order bit of the type value is 0, this
3275 * is a management frame.
3276 * I.e, check "!(link[0] & 0x08)".
3278 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3279 b2
= new_block(JMP(BPF_JSET
));
3285 * For management frames, the DA is at 4.
3287 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3291 * OR that with the checks done for data frames.
3292 * That gives the checks done for management and
3298 * If the low-order bit of the type value is 1,
3299 * this is either a control frame or a frame
3300 * with a reserved type, and thus not a
3303 * I.e., check "!(link[0] & 0x04)".
3305 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3306 b1
= new_block(JMP(BPF_JSET
));
3312 * AND that with the checks for data and management
3319 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3320 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3326 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3327 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3336 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3337 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3338 * as the RFC states.)
3340 static struct block
*
3341 gen_ipfchostop(eaddr
, dir
)
3342 register const u_char
*eaddr
;
3345 register struct block
*b0
, *b1
;
3349 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3352 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3355 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3356 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3362 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3363 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3372 * This is quite tricky because there may be pad bytes in front of the
3373 * DECNET header, and then there are two possible data packet formats that
3374 * carry both src and dst addresses, plus 5 packet types in a format that
3375 * carries only the src node, plus 2 types that use a different format and
3376 * also carry just the src node.
3380 * Instead of doing those all right, we just look for data packets with
3381 * 0 or 1 bytes of padding. If you want to look at other packets, that
3382 * will require a lot more hacking.
3384 * To add support for filtering on DECNET "areas" (network numbers)
3385 * one would want to add a "mask" argument to this routine. That would
3386 * make the filter even more inefficient, although one could be clever
3387 * and not generate masking instructions if the mask is 0xFFFF.
3389 static struct block
*
3390 gen_dnhostop(addr
, dir
)
3394 struct block
*b0
, *b1
, *b2
, *tmp
;
3395 u_int offset_lh
; /* offset if long header is received */
3396 u_int offset_sh
; /* offset if short header is received */
3401 offset_sh
= 1; /* follows flags */
3402 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3406 offset_sh
= 3; /* follows flags, dstnode */
3407 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3411 /* Inefficient because we do our Calvinball dance twice */
3412 b0
= gen_dnhostop(addr
, Q_SRC
);
3413 b1
= gen_dnhostop(addr
, Q_DST
);
3419 /* Inefficient because we do our Calvinball dance twice */
3420 b0
= gen_dnhostop(addr
, Q_SRC
);
3421 b1
= gen_dnhostop(addr
, Q_DST
);
3426 bpf_error("ISO host filtering not implemented");
3431 b0
= gen_linktype(ETHERTYPE_DN
);
3432 /* Check for pad = 1, long header case */
3433 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3434 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3435 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3436 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3438 /* Check for pad = 0, long header case */
3439 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3440 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3443 /* Check for pad = 1, short header case */
3444 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3445 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3446 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3449 /* Check for pad = 0, short header case */
3450 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3451 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3455 /* Combine with test for linktype */
3461 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3462 * test the bottom-of-stack bit, and then check the version number
3463 * field in the IP header.
3465 static struct block
*
3466 gen_mpls_linktype(proto
)
3469 struct block
*b0
, *b1
;
3474 /* match the bottom-of-stack bit */
3475 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3476 /* match the IPv4 version number */
3477 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3482 /* match the bottom-of-stack bit */
3483 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3484 /* match the IPv4 version number */
3485 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3494 static struct block
*
3495 gen_host(addr
, mask
, proto
, dir
, type
)
3502 struct block
*b0
, *b1
;
3503 const char *typestr
;
3513 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3515 * Only check for non-IPv4 addresses if we're not
3516 * checking MPLS-encapsulated packets.
3518 if (label_stack_depth
== 0) {
3519 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3521 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3527 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3530 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3533 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3536 bpf_error("'tcp' modifier applied to %s", typestr
);
3539 bpf_error("'sctp' modifier applied to %s", typestr
);
3542 bpf_error("'udp' modifier applied to %s", typestr
);
3545 bpf_error("'icmp' modifier applied to %s", typestr
);
3548 bpf_error("'igmp' modifier applied to %s", typestr
);
3551 bpf_error("'igrp' modifier applied to %s", typestr
);
3554 bpf_error("'pim' modifier applied to %s", typestr
);
3557 bpf_error("'vrrp' modifier applied to %s", typestr
);
3560 bpf_error("ATALK host filtering not implemented");
3563 bpf_error("AARP host filtering not implemented");
3566 return gen_dnhostop(addr
, dir
);
3569 bpf_error("SCA host filtering not implemented");
3572 bpf_error("LAT host filtering not implemented");
3575 bpf_error("MOPDL host filtering not implemented");
3578 bpf_error("MOPRC host filtering not implemented");
3582 bpf_error("'ip6' modifier applied to ip host");
3585 bpf_error("'icmp6' modifier applied to %s", typestr
);
3589 bpf_error("'ah' modifier applied to %s", typestr
);
3592 bpf_error("'esp' modifier applied to %s", typestr
);
3595 bpf_error("ISO host filtering not implemented");
3598 bpf_error("'esis' modifier applied to %s", typestr
);
3601 bpf_error("'isis' modifier applied to %s", typestr
);
3604 bpf_error("'clnp' modifier applied to %s", typestr
);
3607 bpf_error("'stp' modifier applied to %s", typestr
);
3610 bpf_error("IPX host filtering not implemented");
3613 bpf_error("'netbeui' modifier applied to %s", typestr
);
3616 bpf_error("'radio' modifier applied to %s", typestr
);
3625 static struct block
*
3626 gen_host6(addr
, mask
, proto
, dir
, type
)
3627 struct in6_addr
*addr
;
3628 struct in6_addr
*mask
;
3633 const char *typestr
;
3643 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
3646 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
3649 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
3652 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
3655 bpf_error("'sctp' modifier applied to %s", typestr
);
3658 bpf_error("'tcp' modifier applied to %s", typestr
);
3661 bpf_error("'udp' modifier applied to %s", typestr
);
3664 bpf_error("'icmp' modifier applied to %s", typestr
);
3667 bpf_error("'igmp' modifier applied to %s", typestr
);
3670 bpf_error("'igrp' modifier applied to %s", typestr
);
3673 bpf_error("'pim' modifier applied to %s", typestr
);
3676 bpf_error("'vrrp' modifier applied to %s", typestr
);
3679 bpf_error("ATALK host filtering not implemented");
3682 bpf_error("AARP host filtering not implemented");
3685 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
3688 bpf_error("SCA host filtering not implemented");
3691 bpf_error("LAT host filtering not implemented");
3694 bpf_error("MOPDL host filtering not implemented");
3697 bpf_error("MOPRC host filtering not implemented");
3700 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3703 bpf_error("'icmp6' modifier applied to %s", typestr
);
3706 bpf_error("'ah' modifier applied to %s", typestr
);
3709 bpf_error("'esp' modifier applied to %s", typestr
);
3712 bpf_error("ISO host filtering not implemented");
3715 bpf_error("'esis' modifier applied to %s", typestr
);
3718 bpf_error("'isis' modifier applied to %s", typestr
);
3721 bpf_error("'clnp' modifier applied to %s", typestr
);
3724 bpf_error("'stp' modifier applied to %s", typestr
);
3727 bpf_error("IPX host filtering not implemented");
3730 bpf_error("'netbeui' modifier applied to %s", typestr
);
3733 bpf_error("'radio' modifier applied to %s", typestr
);
3743 static struct block
*
3744 gen_gateway(eaddr
, alist
, proto
, dir
)
3745 const u_char
*eaddr
;
3746 bpf_u_int32
**alist
;
3750 struct block
*b0
, *b1
, *tmp
;
3753 bpf_error("direction applied to 'gateway'");
3762 b0
= gen_ehostop(eaddr
, Q_OR
);
3765 b0
= gen_fhostop(eaddr
, Q_OR
);
3768 b0
= gen_thostop(eaddr
, Q_OR
);
3770 case DLT_IEEE802_11
:
3771 case DLT_IEEE802_11_RADIO_AVS
:
3773 case DLT_IEEE802_11_RADIO
:
3774 case DLT_PRISM_HEADER
:
3775 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3780 * Check that the packet doesn't begin with an
3781 * LE Control marker. (We've already generated
3784 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
3789 * Now check the MAC address.
3791 b0
= gen_ehostop(eaddr
, Q_OR
);
3795 case DLT_IP_OVER_FC
:
3796 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3800 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3802 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
3804 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
3813 bpf_error("illegal modifier of 'gateway'");
3819 gen_proto_abbrev(proto
)
3828 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3830 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3836 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3838 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3844 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3846 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3852 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3855 #ifndef IPPROTO_IGMP
3856 #define IPPROTO_IGMP 2
3860 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3863 #ifndef IPPROTO_IGRP
3864 #define IPPROTO_IGRP 9
3867 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3871 #define IPPROTO_PIM 103
3875 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3877 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3882 #ifndef IPPROTO_VRRP
3883 #define IPPROTO_VRRP 112
3887 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3891 b1
= gen_linktype(ETHERTYPE_IP
);
3895 b1
= gen_linktype(ETHERTYPE_ARP
);
3899 b1
= gen_linktype(ETHERTYPE_REVARP
);
3903 bpf_error("link layer applied in wrong context");
3906 b1
= gen_linktype(ETHERTYPE_ATALK
);
3910 b1
= gen_linktype(ETHERTYPE_AARP
);
3914 b1
= gen_linktype(ETHERTYPE_DN
);
3918 b1
= gen_linktype(ETHERTYPE_SCA
);
3922 b1
= gen_linktype(ETHERTYPE_LAT
);
3926 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3930 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3935 b1
= gen_linktype(ETHERTYPE_IPV6
);
3938 #ifndef IPPROTO_ICMPV6
3939 #define IPPROTO_ICMPV6 58
3942 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3947 #define IPPROTO_AH 51
3950 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3952 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3958 #define IPPROTO_ESP 50
3961 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3963 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3969 b1
= gen_linktype(LLCSAP_ISONS
);
3973 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3977 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3980 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3981 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3982 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3984 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3986 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3988 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3992 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3993 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3994 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3996 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3998 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4000 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4004 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4005 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4006 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4008 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4013 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4014 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4019 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4020 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4022 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4024 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4029 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4030 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4035 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4036 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4041 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4045 b1
= gen_linktype(LLCSAP_8021D
);
4049 b1
= gen_linktype(LLCSAP_IPX
);
4053 b1
= gen_linktype(LLCSAP_NETBEUI
);
4057 bpf_error("'radio' is not a valid protocol type");
4065 static struct block
*
4072 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4073 b
= new_block(JMP(BPF_JSET
));
4082 * Generate a comparison to a port value in the transport-layer header
4083 * at the specified offset from the beginning of that header.
4085 * XXX - this handles a variable-length prefix preceding the link-layer
4086 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4087 * variable-length link-layer headers (such as Token Ring or 802.11
4090 static struct block
*
4091 gen_portatom(off
, v
)
4095 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4099 static struct block
*
4100 gen_portatom6(off
, v
)
4104 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4109 gen_portop(port
, proto
, dir
)
4110 int port
, proto
, dir
;
4112 struct block
*b0
, *b1
, *tmp
;
4114 /* ip proto 'proto' */
4115 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4121 b1
= gen_portatom(0, (bpf_int32
)port
);
4125 b1
= gen_portatom(2, (bpf_int32
)port
);
4130 tmp
= gen_portatom(0, (bpf_int32
)port
);
4131 b1
= gen_portatom(2, (bpf_int32
)port
);
4136 tmp
= gen_portatom(0, (bpf_int32
)port
);
4137 b1
= gen_portatom(2, (bpf_int32
)port
);
4149 static struct block
*
4150 gen_port(port
, ip_proto
, dir
)
4155 struct block
*b0
, *b1
, *tmp
;
4160 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4161 * not LLC encapsulation with LLCSAP_IP.
4163 * For IEEE 802 networks - which includes 802.5 token ring
4164 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4165 * says that SNAP encapsulation is used, not LLC encapsulation
4168 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4169 * RFC 2225 say that SNAP encapsulation is used, not LLC
4170 * encapsulation with LLCSAP_IP.
4172 * So we always check for ETHERTYPE_IP.
4174 b0
= gen_linktype(ETHERTYPE_IP
);
4180 b1
= gen_portop(port
, ip_proto
, dir
);
4184 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4185 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4187 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4200 gen_portop6(port
, proto
, dir
)
4201 int port
, proto
, dir
;
4203 struct block
*b0
, *b1
, *tmp
;
4205 /* ip6 proto 'proto' */
4206 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4210 b1
= gen_portatom6(0, (bpf_int32
)port
);
4214 b1
= gen_portatom6(2, (bpf_int32
)port
);
4219 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4220 b1
= gen_portatom6(2, (bpf_int32
)port
);
4225 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4226 b1
= gen_portatom6(2, (bpf_int32
)port
);
4238 static struct block
*
4239 gen_port6(port
, ip_proto
, dir
)
4244 struct block
*b0
, *b1
, *tmp
;
4246 /* link proto ip6 */
4247 b0
= gen_linktype(ETHERTYPE_IPV6
);
4253 b1
= gen_portop6(port
, ip_proto
, dir
);
4257 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4258 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4260 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4272 /* gen_portrange code */
4273 static struct block
*
4274 gen_portrangeatom(off
, v1
, v2
)
4278 struct block
*b1
, *b2
;
4282 * Reverse the order of the ports, so v1 is the lower one.
4291 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4292 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4300 gen_portrangeop(port1
, port2
, proto
, dir
)
4305 struct block
*b0
, *b1
, *tmp
;
4307 /* ip proto 'proto' */
4308 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4314 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4318 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4323 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4324 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4329 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4330 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4342 static struct block
*
4343 gen_portrange(port1
, port2
, ip_proto
, dir
)
4348 struct block
*b0
, *b1
, *tmp
;
4351 b0
= gen_linktype(ETHERTYPE_IP
);
4357 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4361 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4362 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4364 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4376 static struct block
*
4377 gen_portrangeatom6(off
, v1
, v2
)
4381 struct block
*b1
, *b2
;
4385 * Reverse the order of the ports, so v1 is the lower one.
4394 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4395 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4403 gen_portrangeop6(port1
, port2
, proto
, dir
)
4408 struct block
*b0
, *b1
, *tmp
;
4410 /* ip6 proto 'proto' */
4411 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4415 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4419 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4424 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4425 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4430 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4431 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4443 static struct block
*
4444 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4449 struct block
*b0
, *b1
, *tmp
;
4451 /* link proto ip6 */
4452 b0
= gen_linktype(ETHERTYPE_IPV6
);
4458 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4462 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4463 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4465 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4478 lookup_proto(name
, proto
)
4479 register const char *name
;
4489 v
= pcap_nametoproto(name
);
4490 if (v
== PROTO_UNDEF
)
4491 bpf_error("unknown ip proto '%s'", name
);
4495 /* XXX should look up h/w protocol type based on linktype */
4496 v
= pcap_nametoeproto(name
);
4497 if (v
== PROTO_UNDEF
) {
4498 v
= pcap_nametollc(name
);
4499 if (v
== PROTO_UNDEF
)
4500 bpf_error("unknown ether proto '%s'", name
);
4505 if (strcmp(name
, "esis") == 0)
4507 else if (strcmp(name
, "isis") == 0)
4509 else if (strcmp(name
, "clnp") == 0)
4512 bpf_error("unknown osi proto '%s'", name
);
4532 static struct block
*
4533 gen_protochain(v
, proto
, dir
)
4538 #ifdef NO_PROTOCHAIN
4539 return gen_proto(v
, proto
, dir
);
4541 struct block
*b0
, *b
;
4542 struct slist
*s
[100];
4543 int fix2
, fix3
, fix4
, fix5
;
4544 int ahcheck
, again
, end
;
4546 int reg2
= alloc_reg();
4548 memset(s
, 0, sizeof(s
));
4549 fix2
= fix3
= fix4
= fix5
= 0;
4556 b0
= gen_protochain(v
, Q_IP
, dir
);
4557 b
= gen_protochain(v
, Q_IPV6
, dir
);
4561 bpf_error("bad protocol applied for 'protochain'");
4566 * We don't handle variable-length radiotap here headers yet.
4567 * We might want to add BPF instructions to do the protochain
4568 * work, to simplify that and, on platforms that have a BPF
4569 * interpreter with the new instructions, let the filtering
4570 * be done in the kernel. (We already require a modified BPF
4571 * engine to do the protochain stuff, to support backward
4572 * branches, and backward branch support is unlikely to appear
4573 * in kernel BPF engines.)
4575 if (linktype
== DLT_IEEE802_11_RADIO
)
4576 bpf_error("'protochain' not supported with radiotap headers");
4578 if (linktype
== DLT_PPI
)
4579 bpf_error("'protochain' not supported with PPI headers");
4581 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4584 * s[0] is a dummy entry to protect other BPF insn from damage
4585 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4586 * hard to find interdependency made by jump table fixup.
4589 s
[i
] = new_stmt(0); /*dummy*/
4594 b0
= gen_linktype(ETHERTYPE_IP
);
4597 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4598 s
[i
]->s
.k
= off_ll
+ off_nl
+ 9;
4600 /* X = ip->ip_hl << 2 */
4601 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4602 s
[i
]->s
.k
= off_ll
+ off_nl
;
4607 b0
= gen_linktype(ETHERTYPE_IPV6
);
4609 /* A = ip6->ip_nxt */
4610 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4611 s
[i
]->s
.k
= off_ll
+ off_nl
+ 6;
4613 /* X = sizeof(struct ip6_hdr) */
4614 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4620 bpf_error("unsupported proto to gen_protochain");
4624 /* again: if (A == v) goto end; else fall through; */
4626 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4628 s
[i
]->s
.jt
= NULL
; /*later*/
4629 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4633 #ifndef IPPROTO_NONE
4634 #define IPPROTO_NONE 59
4636 /* if (A == IPPROTO_NONE) goto end */
4637 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4638 s
[i
]->s
.jt
= NULL
; /*later*/
4639 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4640 s
[i
]->s
.k
= IPPROTO_NONE
;
4641 s
[fix5
]->s
.jf
= s
[i
];
4646 if (proto
== Q_IPV6
) {
4647 int v6start
, v6end
, v6advance
, j
;
4650 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4651 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4652 s
[i
]->s
.jt
= NULL
; /*later*/
4653 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4654 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4655 s
[fix2
]->s
.jf
= s
[i
];
4657 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4658 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4659 s
[i
]->s
.jt
= NULL
; /*later*/
4660 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4661 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4663 /* if (A == IPPROTO_ROUTING) goto v6advance */
4664 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4665 s
[i
]->s
.jt
= NULL
; /*later*/
4666 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4667 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4669 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4670 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4671 s
[i
]->s
.jt
= NULL
; /*later*/
4672 s
[i
]->s
.jf
= NULL
; /*later*/
4673 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4684 * X = X + (P[X + 1] + 1) * 8;
4687 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4689 /* A = P[X + packet head] */
4690 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4691 s
[i
]->s
.k
= off_ll
+ off_nl
;
4694 s
[i
] = new_stmt(BPF_ST
);
4698 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4701 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4705 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4707 /* A = P[X + packet head]; */
4708 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4709 s
[i
]->s
.k
= off_ll
+ off_nl
;
4712 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4716 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4720 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4723 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4727 /* goto again; (must use BPF_JA for backward jump) */
4728 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4729 s
[i
]->s
.k
= again
- i
- 1;
4730 s
[i
- 1]->s
.jf
= s
[i
];
4734 for (j
= v6start
; j
<= v6end
; j
++)
4735 s
[j
]->s
.jt
= s
[v6advance
];
4740 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4742 s
[fix2
]->s
.jf
= s
[i
];
4748 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4749 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4750 s
[i
]->s
.jt
= NULL
; /*later*/
4751 s
[i
]->s
.jf
= NULL
; /*later*/
4752 s
[i
]->s
.k
= IPPROTO_AH
;
4754 s
[fix3
]->s
.jf
= s
[ahcheck
];
4761 * X = X + (P[X + 1] + 2) * 4;
4764 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4766 /* A = P[X + packet head]; */
4767 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4768 s
[i
]->s
.k
= off_ll
+ off_nl
;
4771 s
[i
] = new_stmt(BPF_ST
);
4775 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4778 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4782 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4784 /* A = P[X + packet head] */
4785 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4786 s
[i
]->s
.k
= off_ll
+ off_nl
;
4789 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4793 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4797 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4800 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4804 /* goto again; (must use BPF_JA for backward jump) */
4805 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4806 s
[i
]->s
.k
= again
- i
- 1;
4811 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4813 s
[fix2
]->s
.jt
= s
[end
];
4814 s
[fix4
]->s
.jf
= s
[end
];
4815 s
[fix5
]->s
.jt
= s
[end
];
4822 for (i
= 0; i
< max
- 1; i
++)
4823 s
[i
]->next
= s
[i
+ 1];
4824 s
[max
- 1]->next
= NULL
;
4829 b
= new_block(JMP(BPF_JEQ
));
4830 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4841 * Generate code that checks whether the packet is a packet for protocol
4842 * <proto> and whether the type field in that protocol's header has
4843 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4844 * IP packet and checks the protocol number in the IP header against <v>.
4846 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4847 * against Q_IP and Q_IPV6.
4849 static struct block
*
4850 gen_proto(v
, proto
, dir
)
4855 struct block
*b0
, *b1
;
4857 if (dir
!= Q_DEFAULT
)
4858 bpf_error("direction applied to 'proto'");
4863 b0
= gen_proto(v
, Q_IP
, dir
);
4864 b1
= gen_proto(v
, Q_IPV6
, dir
);
4872 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4873 * not LLC encapsulation with LLCSAP_IP.
4875 * For IEEE 802 networks - which includes 802.5 token ring
4876 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4877 * says that SNAP encapsulation is used, not LLC encapsulation
4880 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4881 * RFC 2225 say that SNAP encapsulation is used, not LLC
4882 * encapsulation with LLCSAP_IP.
4884 * So we always check for ETHERTYPE_IP.
4886 b0
= gen_linktype(ETHERTYPE_IP
);
4888 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4890 b1
= gen_protochain(v
, Q_IP
);
4900 * Frame Relay packets typically have an OSI
4901 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4902 * generates code to check for all the OSI
4903 * NLPIDs, so calling it and then adding a check
4904 * for the particular NLPID for which we're
4905 * looking is bogus, as we can just check for
4908 * What we check for is the NLPID and a frame
4909 * control field value of UI, i.e. 0x03 followed
4912 * XXX - assumes a 2-byte Frame Relay header with
4913 * DLCI and flags. What if the address is longer?
4915 * XXX - what about SNAP-encapsulated frames?
4917 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4923 * Cisco uses an Ethertype lookalike - for OSI,
4926 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4927 /* OSI in C-HDLC is stuffed with a fudge byte */
4928 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4933 b0
= gen_linktype(LLCSAP_ISONS
);
4934 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4940 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4942 * 4 is the offset of the PDU type relative to the IS-IS
4945 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4950 bpf_error("arp does not encapsulate another protocol");
4954 bpf_error("rarp does not encapsulate another protocol");
4958 bpf_error("atalk encapsulation is not specifiable");
4962 bpf_error("decnet encapsulation is not specifiable");
4966 bpf_error("sca does not encapsulate another protocol");
4970 bpf_error("lat does not encapsulate another protocol");
4974 bpf_error("moprc does not encapsulate another protocol");
4978 bpf_error("mopdl does not encapsulate another protocol");
4982 return gen_linktype(v
);
4985 bpf_error("'udp proto' is bogus");
4989 bpf_error("'tcp proto' is bogus");
4993 bpf_error("'sctp proto' is bogus");
4997 bpf_error("'icmp proto' is bogus");
5001 bpf_error("'igmp proto' is bogus");
5005 bpf_error("'igrp proto' is bogus");
5009 bpf_error("'pim proto' is bogus");
5013 bpf_error("'vrrp proto' is bogus");
5018 b0
= gen_linktype(ETHERTYPE_IPV6
);
5020 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5022 b1
= gen_protochain(v
, Q_IPV6
);
5028 bpf_error("'icmp6 proto' is bogus");
5032 bpf_error("'ah proto' is bogus");
5035 bpf_error("'ah proto' is bogus");
5038 bpf_error("'stp proto' is bogus");
5041 bpf_error("'ipx proto' is bogus");
5044 bpf_error("'netbeui proto' is bogus");
5047 bpf_error("'radio proto' is bogus");
5058 register const char *name
;
5061 int proto
= q
.proto
;
5065 bpf_u_int32 mask
, addr
;
5067 bpf_u_int32
**alist
;
5070 struct sockaddr_in
*sin4
;
5071 struct sockaddr_in6
*sin6
;
5072 struct addrinfo
*res
, *res0
;
5073 struct in6_addr mask128
;
5075 struct block
*b
, *tmp
;
5076 int port
, real_proto
;
5082 addr
= pcap_nametonetaddr(name
);
5084 bpf_error("unknown network '%s'", name
);
5085 /* Left justify network addr and calculate its network mask */
5087 while (addr
&& (addr
& 0xff000000) == 0) {
5091 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5095 if (proto
== Q_LINK
) {
5099 eaddr
= pcap_ether_hostton(name
);
5102 "unknown ether host '%s'", name
);
5103 b
= gen_ehostop(eaddr
, dir
);
5108 eaddr
= pcap_ether_hostton(name
);
5111 "unknown FDDI host '%s'", name
);
5112 b
= gen_fhostop(eaddr
, dir
);
5117 eaddr
= pcap_ether_hostton(name
);
5120 "unknown token ring host '%s'", name
);
5121 b
= gen_thostop(eaddr
, dir
);
5125 case DLT_IEEE802_11
:
5126 case DLT_IEEE802_11_RADIO_AVS
:
5127 case DLT_IEEE802_11_RADIO
:
5128 case DLT_PRISM_HEADER
:
5130 eaddr
= pcap_ether_hostton(name
);
5133 "unknown 802.11 host '%s'", name
);
5134 b
= gen_wlanhostop(eaddr
, dir
);
5138 case DLT_IP_OVER_FC
:
5139 eaddr
= pcap_ether_hostton(name
);
5142 "unknown Fibre Channel host '%s'", name
);
5143 b
= gen_ipfchostop(eaddr
, dir
);
5152 * Check that the packet doesn't begin
5153 * with an LE Control marker. (We've
5154 * already generated a test for LANE.)
5156 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5160 eaddr
= pcap_ether_hostton(name
);
5163 "unknown ether host '%s'", name
);
5164 b
= gen_ehostop(eaddr
, dir
);
5170 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5171 } else if (proto
== Q_DECNET
) {
5172 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5174 * I don't think DECNET hosts can be multihomed, so
5175 * there is no need to build up a list of addresses
5177 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5180 alist
= pcap_nametoaddr(name
);
5181 if (alist
== NULL
|| *alist
== NULL
)
5182 bpf_error("unknown host '%s'", name
);
5184 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5186 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5188 tmp
= gen_host(**alist
++, 0xffffffff,
5189 tproto
, dir
, q
.addr
);
5195 memset(&mask128
, 0xff, sizeof(mask128
));
5196 res0
= res
= pcap_nametoaddrinfo(name
);
5198 bpf_error("unknown host '%s'", name
);
5200 tproto
= tproto6
= proto
;
5201 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5205 for (res
= res0
; res
; res
= res
->ai_next
) {
5206 switch (res
->ai_family
) {
5208 if (tproto
== Q_IPV6
)
5211 sin4
= (struct sockaddr_in
*)
5213 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5214 0xffffffff, tproto
, dir
, q
.addr
);
5217 if (tproto6
== Q_IP
)
5220 sin6
= (struct sockaddr_in6
*)
5222 tmp
= gen_host6(&sin6
->sin6_addr
,
5223 &mask128
, tproto6
, dir
, q
.addr
);
5234 bpf_error("unknown host '%s'%s", name
,
5235 (proto
== Q_DEFAULT
)
5237 : " for specified address family");
5244 if (proto
!= Q_DEFAULT
&&
5245 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5246 bpf_error("illegal qualifier of 'port'");
5247 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5248 bpf_error("unknown port '%s'", name
);
5249 if (proto
== Q_UDP
) {
5250 if (real_proto
== IPPROTO_TCP
)
5251 bpf_error("port '%s' is tcp", name
);
5252 else if (real_proto
== IPPROTO_SCTP
)
5253 bpf_error("port '%s' is sctp", name
);
5255 /* override PROTO_UNDEF */
5256 real_proto
= IPPROTO_UDP
;
5258 if (proto
== Q_TCP
) {
5259 if (real_proto
== IPPROTO_UDP
)
5260 bpf_error("port '%s' is udp", name
);
5262 else if (real_proto
== IPPROTO_SCTP
)
5263 bpf_error("port '%s' is sctp", name
);
5265 /* override PROTO_UNDEF */
5266 real_proto
= IPPROTO_TCP
;
5268 if (proto
== Q_SCTP
) {
5269 if (real_proto
== IPPROTO_UDP
)
5270 bpf_error("port '%s' is udp", name
);
5272 else if (real_proto
== IPPROTO_TCP
)
5273 bpf_error("port '%s' is tcp", name
);
5275 /* override PROTO_UNDEF */
5276 real_proto
= IPPROTO_SCTP
;
5279 return gen_port(port
, real_proto
, dir
);
5281 b
= gen_port(port
, real_proto
, dir
);
5282 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5287 if (proto
!= Q_DEFAULT
&&
5288 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5289 bpf_error("illegal qualifier of 'portrange'");
5290 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5291 bpf_error("unknown port in range '%s'", name
);
5292 if (proto
== Q_UDP
) {
5293 if (real_proto
== IPPROTO_TCP
)
5294 bpf_error("port in range '%s' is tcp", name
);
5295 else if (real_proto
== IPPROTO_SCTP
)
5296 bpf_error("port in range '%s' is sctp", name
);
5298 /* override PROTO_UNDEF */
5299 real_proto
= IPPROTO_UDP
;
5301 if (proto
== Q_TCP
) {
5302 if (real_proto
== IPPROTO_UDP
)
5303 bpf_error("port in range '%s' is udp", name
);
5304 else if (real_proto
== IPPROTO_SCTP
)
5305 bpf_error("port in range '%s' is sctp", name
);
5307 /* override PROTO_UNDEF */
5308 real_proto
= IPPROTO_TCP
;
5310 if (proto
== Q_SCTP
) {
5311 if (real_proto
== IPPROTO_UDP
)
5312 bpf_error("port in range '%s' is udp", name
);
5313 else if (real_proto
== IPPROTO_TCP
)
5314 bpf_error("port in range '%s' is tcp", name
);
5316 /* override PROTO_UNDEF */
5317 real_proto
= IPPROTO_SCTP
;
5320 return gen_portrange(port1
, port2
, real_proto
, dir
);
5322 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5323 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5329 eaddr
= pcap_ether_hostton(name
);
5331 bpf_error("unknown ether host: %s", name
);
5333 alist
= pcap_nametoaddr(name
);
5334 if (alist
== NULL
|| *alist
== NULL
)
5335 bpf_error("unknown host '%s'", name
);
5336 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5340 bpf_error("'gateway' not supported in this configuration");
5344 real_proto
= lookup_proto(name
, proto
);
5345 if (real_proto
>= 0)
5346 return gen_proto(real_proto
, proto
, dir
);
5348 bpf_error("unknown protocol: %s", name
);
5351 real_proto
= lookup_proto(name
, proto
);
5352 if (real_proto
>= 0)
5353 return gen_protochain(real_proto
, proto
, dir
);
5355 bpf_error("unknown protocol: %s", name
);
5367 gen_mcode(s1
, s2
, masklen
, q
)
5368 register const char *s1
, *s2
;
5369 register int masklen
;
5372 register int nlen
, mlen
;
5375 nlen
= __pcap_atoin(s1
, &n
);
5376 /* Promote short ipaddr */
5380 mlen
= __pcap_atoin(s2
, &m
);
5381 /* Promote short ipaddr */
5384 bpf_error("non-network bits set in \"%s mask %s\"",
5387 /* Convert mask len to mask */
5389 bpf_error("mask length must be <= 32");
5392 * X << 32 is not guaranteed by C to be 0; it's
5397 m
= 0xffffffff << (32 - masklen
);
5399 bpf_error("non-network bits set in \"%s/%d\"",
5406 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5409 bpf_error("Mask syntax for networks only");
5418 register const char *s
;
5423 int proto
= q
.proto
;
5429 else if (q
.proto
== Q_DECNET
)
5430 vlen
= __pcap_atodn(s
, &v
);
5432 vlen
= __pcap_atoin(s
, &v
);
5439 if (proto
== Q_DECNET
)
5440 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5441 else if (proto
== Q_LINK
) {
5442 bpf_error("illegal link layer address");
5445 if (s
== NULL
&& q
.addr
== Q_NET
) {
5446 /* Promote short net number */
5447 while (v
&& (v
& 0xff000000) == 0) {
5452 /* Promote short ipaddr */
5456 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5461 proto
= IPPROTO_UDP
;
5462 else if (proto
== Q_TCP
)
5463 proto
= IPPROTO_TCP
;
5464 else if (proto
== Q_SCTP
)
5465 proto
= IPPROTO_SCTP
;
5466 else if (proto
== Q_DEFAULT
)
5467 proto
= PROTO_UNDEF
;
5469 bpf_error("illegal qualifier of 'port'");
5472 return gen_port((int)v
, proto
, dir
);
5476 b
= gen_port((int)v
, proto
, dir
);
5477 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5484 proto
= IPPROTO_UDP
;
5485 else if (proto
== Q_TCP
)
5486 proto
= IPPROTO_TCP
;
5487 else if (proto
== Q_SCTP
)
5488 proto
= IPPROTO_SCTP
;
5489 else if (proto
== Q_DEFAULT
)
5490 proto
= PROTO_UNDEF
;
5492 bpf_error("illegal qualifier of 'portrange'");
5495 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5499 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5500 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5506 bpf_error("'gateway' requires a name");
5510 return gen_proto((int)v
, proto
, dir
);
5513 return gen_protochain((int)v
, proto
, dir
);
5528 gen_mcode6(s1
, s2
, masklen
, q
)
5529 register const char *s1
, *s2
;
5530 register int masklen
;
5533 struct addrinfo
*res
;
5534 struct in6_addr
*addr
;
5535 struct in6_addr mask
;
5540 bpf_error("no mask %s supported", s2
);
5542 res
= pcap_nametoaddrinfo(s1
);
5544 bpf_error("invalid ip6 address %s", s1
);
5546 bpf_error("%s resolved to multiple address", s1
);
5547 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5549 if (sizeof(mask
) * 8 < masklen
)
5550 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5551 memset(&mask
, 0, sizeof(mask
));
5552 memset(&mask
, 0xff, masklen
/ 8);
5554 mask
.s6_addr
[masklen
/ 8] =
5555 (0xff << (8 - masklen
% 8)) & 0xff;
5558 a
= (u_int32_t
*)addr
;
5559 m
= (u_int32_t
*)&mask
;
5560 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5561 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5562 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5570 bpf_error("Mask syntax for networks only");
5574 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
5579 bpf_error("invalid qualifier against IPv6 address");
5588 register const u_char
*eaddr
;
5591 struct block
*b
, *tmp
;
5593 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5596 return gen_ehostop(eaddr
, (int)q
.dir
);
5598 return gen_fhostop(eaddr
, (int)q
.dir
);
5600 return gen_thostop(eaddr
, (int)q
.dir
);
5601 case DLT_IEEE802_11
:
5602 case DLT_IEEE802_11_RADIO_AVS
:
5603 case DLT_IEEE802_11_RADIO
:
5604 case DLT_PRISM_HEADER
:
5606 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5610 * Check that the packet doesn't begin with an
5611 * LE Control marker. (We've already generated
5614 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5619 * Now check the MAC address.
5621 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5626 case DLT_IP_OVER_FC
:
5627 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5629 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5633 bpf_error("ethernet address used in non-ether expression");
5640 struct slist
*s0
, *s1
;
5643 * This is definitely not the best way to do this, but the
5644 * lists will rarely get long.
5651 static struct slist
*
5657 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5662 static struct slist
*
5668 s
= new_stmt(BPF_LD
|BPF_MEM
);
5674 * Modify "index" to use the value stored into its register as an
5675 * offset relative to the beginning of the header for the protocol
5676 * "proto", and allocate a register and put an item "size" bytes long
5677 * (1, 2, or 4) at that offset into that register, making it the register
5681 gen_load(proto
, inst
, size
)
5686 struct slist
*s
, *tmp
;
5688 int regno
= alloc_reg();
5690 free_reg(inst
->regno
);
5694 bpf_error("data size must be 1, 2, or 4");
5710 bpf_error("unsupported index operation");
5714 * The offset is relative to the beginning of the packet
5715 * data, if we have a radio header. (If we don't, this
5718 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5719 linktype
!= DLT_IEEE802_11_RADIO
&&
5720 linktype
!= DLT_PRISM_HEADER
)
5721 bpf_error("radio information not present in capture");
5724 * Load into the X register the offset computed into the
5725 * register specifed by "index".
5727 s
= xfer_to_x(inst
);
5730 * Load the item at that offset.
5732 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5734 sappend(inst
->s
, s
);
5739 * The offset is relative to the beginning of
5740 * the link-layer header.
5742 * XXX - what about ATM LANE? Should the index be
5743 * relative to the beginning of the AAL5 frame, so
5744 * that 0 refers to the beginning of the LE Control
5745 * field, or relative to the beginning of the LAN
5746 * frame, so that 0 refers, for Ethernet LANE, to
5747 * the beginning of the destination address?
5749 s
= gen_llprefixlen();
5752 * If "s" is non-null, it has code to arrange that the
5753 * X register contains the length of the prefix preceding
5754 * the link-layer header. Add to it the offset computed
5755 * into the register specified by "index", and move that
5756 * into the X register. Otherwise, just load into the X
5757 * register the offset computed into the register specifed
5761 sappend(s
, xfer_to_a(inst
));
5762 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5763 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5765 s
= xfer_to_x(inst
);
5768 * Load the item at the sum of the offset we've put in the
5769 * X register and the offset of the start of the link
5770 * layer header (which is 0 if the radio header is
5771 * variable-length; that header length is what we put
5772 * into the X register and then added to the index).
5774 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5777 sappend(inst
->s
, s
);
5793 * The offset is relative to the beginning of
5794 * the network-layer header.
5795 * XXX - are there any cases where we want
5798 s
= gen_llprefixlen();
5801 * If "s" is non-null, it has code to arrange that the
5802 * X register contains the length of the prefix preceding
5803 * the link-layer header. Add to it the offset computed
5804 * into the register specified by "index", and move that
5805 * into the X register. Otherwise, just load into the X
5806 * register the offset computed into the register specifed
5810 sappend(s
, xfer_to_a(inst
));
5811 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5812 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5814 s
= xfer_to_x(inst
);
5817 * Load the item at the sum of the offset we've put in the
5818 * X register, the offset of the start of the network
5819 * layer header, and the offset of the start of the link
5820 * layer header (which is 0 if the radio header is
5821 * variable-length; that header length is what we put
5822 * into the X register and then added to the index).
5824 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5825 tmp
->s
.k
= off_ll
+ off_nl
;
5827 sappend(inst
->s
, s
);
5830 * Do the computation only if the packet contains
5831 * the protocol in question.
5833 b
= gen_proto_abbrev(proto
);
5835 gen_and(inst
->b
, b
);
5848 * The offset is relative to the beginning of
5849 * the transport-layer header.
5851 * Load the X register with the length of the IPv4 header
5852 * (plus the offset of the link-layer header, if it's
5853 * a variable-length header), in bytes.
5855 * XXX - are there any cases where we want
5857 * XXX - we should, if we're built with
5858 * IPv6 support, generate code to load either
5859 * IPv4, IPv6, or both, as appropriate.
5861 s
= gen_loadx_iphdrlen();
5864 * The X register now contains the sum of the length
5865 * of any variable-length header preceding the link-layer
5866 * header and the length of the network-layer header.
5867 * Load into the A register the offset relative to
5868 * the beginning of the transport layer header,
5869 * add the X register to that, move that to the
5870 * X register, and load with an offset from the
5871 * X register equal to the offset of the network
5872 * layer header relative to the beginning of
5873 * the link-layer header plus the length of any
5874 * fixed-length header preceding the link-layer
5877 sappend(s
, xfer_to_a(inst
));
5878 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5879 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5880 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5881 tmp
->s
.k
= off_ll
+ off_nl
;
5882 sappend(inst
->s
, s
);
5885 * Do the computation only if the packet contains
5886 * the protocol in question - which is true only
5887 * if this is an IP datagram and is the first or
5888 * only fragment of that datagram.
5890 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5892 gen_and(inst
->b
, b
);
5894 gen_and(gen_proto_abbrev(Q_IP
), b
);
5900 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5904 inst
->regno
= regno
;
5905 s
= new_stmt(BPF_ST
);
5907 sappend(inst
->s
, s
);
5913 gen_relation(code
, a0
, a1
, reversed
)
5915 struct arth
*a0
, *a1
;
5918 struct slist
*s0
, *s1
, *s2
;
5919 struct block
*b
, *tmp
;
5923 if (code
== BPF_JEQ
) {
5924 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5925 b
= new_block(JMP(code
));
5929 b
= new_block(BPF_JMP
|code
|BPF_X
);
5935 sappend(a0
->s
, a1
->s
);
5939 free_reg(a0
->regno
);
5940 free_reg(a1
->regno
);
5942 /* 'and' together protocol checks */
5945 gen_and(a0
->b
, tmp
= a1
->b
);
5961 int regno
= alloc_reg();
5962 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5965 s
= new_stmt(BPF_LD
|BPF_LEN
);
5966 s
->next
= new_stmt(BPF_ST
);
5967 s
->next
->s
.k
= regno
;
5982 a
= (struct arth
*)newchunk(sizeof(*a
));
5986 s
= new_stmt(BPF_LD
|BPF_IMM
);
5988 s
->next
= new_stmt(BPF_ST
);
6004 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6007 s
= new_stmt(BPF_ST
);
6015 gen_arth(code
, a0
, a1
)
6017 struct arth
*a0
, *a1
;
6019 struct slist
*s0
, *s1
, *s2
;
6023 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6028 sappend(a0
->s
, a1
->s
);
6030 free_reg(a0
->regno
);
6031 free_reg(a1
->regno
);
6033 s0
= new_stmt(BPF_ST
);
6034 a0
->regno
= s0
->s
.k
= alloc_reg();
6041 * Here we handle simple allocation of the scratch registers.
6042 * If too many registers are alloc'd, the allocator punts.
6044 static int regused
[BPF_MEMWORDS
];
6048 * Return the next free register.
6053 int n
= BPF_MEMWORDS
;
6056 if (regused
[curreg
])
6057 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6059 regused
[curreg
] = 1;
6063 bpf_error("too many registers needed to evaluate expression");
6069 * Return a register to the table so it can
6079 static struct block
*
6086 s
= new_stmt(BPF_LD
|BPF_LEN
);
6087 b
= new_block(JMP(jmp
));
6098 return gen_len(BPF_JGE
, n
);
6102 * Actually, this is less than or equal.
6110 b
= gen_len(BPF_JGT
, n
);
6117 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6118 * the beginning of the link-layer header.
6119 * XXX - that means you can't test values in the radiotap header, but
6120 * as that header is difficult if not impossible to parse generally
6121 * without a loop, that might not be a severe problem. A new keyword
6122 * "radio" could be added for that, although what you'd really want
6123 * would be a way of testing particular radio header values, which
6124 * would generate code appropriate to the radio header in question.
6127 gen_byteop(op
, idx
, val
)
6138 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6141 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6145 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6149 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6153 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6157 b
= new_block(JMP(BPF_JEQ
));
6164 static u_char abroadcast
[] = { 0x0 };
6167 gen_broadcast(proto
)
6170 bpf_u_int32 hostmask
;
6171 struct block
*b0
, *b1
, *b2
;
6172 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6180 case DLT_ARCNET_LINUX
:
6181 return gen_ahostop(abroadcast
, Q_DST
);
6183 return gen_ehostop(ebroadcast
, Q_DST
);
6185 return gen_fhostop(ebroadcast
, Q_DST
);
6187 return gen_thostop(ebroadcast
, Q_DST
);
6188 case DLT_IEEE802_11
:
6189 case DLT_IEEE802_11_RADIO_AVS
:
6190 case DLT_IEEE802_11_RADIO
:
6192 case DLT_PRISM_HEADER
:
6193 return gen_wlanhostop(ebroadcast
, Q_DST
);
6194 case DLT_IP_OVER_FC
:
6195 return gen_ipfchostop(ebroadcast
, Q_DST
);
6199 * Check that the packet doesn't begin with an
6200 * LE Control marker. (We've already generated
6203 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6208 * Now check the MAC address.
6210 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6216 bpf_error("not a broadcast link");
6221 b0
= gen_linktype(ETHERTYPE_IP
);
6222 hostmask
= ~netmask
;
6223 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6224 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6225 (bpf_int32
)(~0 & hostmask
), hostmask
);
6230 bpf_error("only link-layer/IP broadcast filters supported");
6236 * Generate code to test the low-order bit of a MAC address (that's
6237 * the bottom bit of the *first* byte).
6239 static struct block
*
6240 gen_mac_multicast(offset
)
6243 register struct block
*b0
;
6244 register struct slist
*s
;
6246 /* link[offset] & 1 != 0 */
6247 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6248 b0
= new_block(JMP(BPF_JSET
));
6255 gen_multicast(proto
)
6258 register struct block
*b0
, *b1
, *b2
;
6259 register struct slist
*s
;
6267 case DLT_ARCNET_LINUX
:
6268 /* all ARCnet multicasts use the same address */
6269 return gen_ahostop(abroadcast
, Q_DST
);
6271 /* ether[0] & 1 != 0 */
6272 return gen_mac_multicast(0);
6275 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6277 * XXX - was that referring to bit-order issues?
6279 /* fddi[1] & 1 != 0 */
6280 return gen_mac_multicast(1);
6282 /* tr[2] & 1 != 0 */
6283 return gen_mac_multicast(2);
6284 case DLT_IEEE802_11
:
6285 case DLT_IEEE802_11_RADIO_AVS
:
6287 case DLT_IEEE802_11_RADIO
:
6288 case DLT_PRISM_HEADER
:
6292 * For control frames, there is no DA.
6294 * For management frames, DA is at an
6295 * offset of 4 from the beginning of
6298 * For data frames, DA is at an offset
6299 * of 4 from the beginning of the packet
6300 * if To DS is clear and at an offset of
6301 * 16 from the beginning of the packet
6306 * Generate the tests to be done for data frames.
6308 * First, check for To DS set, i.e. "link[1] & 0x01".
6310 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6311 b1
= new_block(JMP(BPF_JSET
));
6312 b1
->s
.k
= 0x01; /* To DS */
6316 * If To DS is set, the DA is at 16.
6318 b0
= gen_mac_multicast(16);
6322 * Now, check for To DS not set, i.e. check
6323 * "!(link[1] & 0x01)".
6325 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6326 b2
= new_block(JMP(BPF_JSET
));
6327 b2
->s
.k
= 0x01; /* To DS */
6332 * If To DS is not set, the DA is at 4.
6334 b1
= gen_mac_multicast(4);
6338 * Now OR together the last two checks. That gives
6339 * the complete set of checks for data frames.
6344 * Now check for a data frame.
6345 * I.e, check "link[0] & 0x08".
6347 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6348 b1
= new_block(JMP(BPF_JSET
));
6353 * AND that with the checks done for data frames.
6358 * If the high-order bit of the type value is 0, this
6359 * is a management frame.
6360 * I.e, check "!(link[0] & 0x08)".
6362 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6363 b2
= new_block(JMP(BPF_JSET
));
6369 * For management frames, the DA is at 4.
6371 b1
= gen_mac_multicast(4);
6375 * OR that with the checks done for data frames.
6376 * That gives the checks done for management and
6382 * If the low-order bit of the type value is 1,
6383 * this is either a control frame or a frame
6384 * with a reserved type, and thus not a
6387 * I.e., check "!(link[0] & 0x04)".
6389 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6390 b1
= new_block(JMP(BPF_JSET
));
6396 * AND that with the checks for data and management
6401 case DLT_IP_OVER_FC
:
6402 b0
= gen_mac_multicast(2);
6407 * Check that the packet doesn't begin with an
6408 * LE Control marker. (We've already generated
6411 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6415 /* ether[off_mac] & 1 != 0 */
6416 b0
= gen_mac_multicast(off_mac
);
6424 /* Link not known to support multicasts */
6428 b0
= gen_linktype(ETHERTYPE_IP
);
6429 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6435 b0
= gen_linktype(ETHERTYPE_IPV6
);
6436 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6441 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6447 * generate command for inbound/outbound. It's here so we can
6448 * make it link-type specific. 'dir' = 0 implies "inbound",
6449 * = 1 implies "outbound".
6455 register struct block
*b0
;
6458 * Only some data link types support inbound/outbound qualifiers.
6462 b0
= gen_relation(BPF_JEQ
,
6463 gen_load(Q_LINK
, gen_loadi(0), 1),
6471 * Match packets sent by this machine.
6473 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6476 * Match packets sent to this machine.
6477 * (No broadcast or multicast packets, or
6478 * packets sent to some other machine and
6479 * received promiscuously.)
6481 * XXX - packets sent to other machines probably
6482 * shouldn't be matched, but what about broadcast
6483 * or multicast packets we received?
6485 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6489 #ifdef HAVE_NET_PFVAR_H
6491 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6492 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6498 /* match outgoing packets */
6499 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6501 /* match incoming packets */
6502 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6506 case DLT_JUNIPER_MFR
:
6507 case DLT_JUNIPER_MLFR
:
6508 case DLT_JUNIPER_MLPPP
:
6509 case DLT_JUNIPER_ATM1
:
6510 case DLT_JUNIPER_ATM2
:
6511 case DLT_JUNIPER_PPPOE
:
6512 case DLT_JUNIPER_PPPOE_ATM
:
6513 case DLT_JUNIPER_GGSN
:
6514 case DLT_JUNIPER_ES
:
6515 case DLT_JUNIPER_MONITOR
:
6516 case DLT_JUNIPER_SERVICES
:
6517 case DLT_JUNIPER_ETHER
:
6518 case DLT_JUNIPER_PPP
:
6519 case DLT_JUNIPER_FRELAY
:
6520 case DLT_JUNIPER_CHDLC
:
6521 case DLT_JUNIPER_VP
:
6522 case DLT_JUNIPER_ST
:
6523 case DLT_JUNIPER_ISM
:
6524 /* juniper flags (including direction) are stored
6525 * the byte after the 3-byte magic number */
6527 /* match outgoing packets */
6528 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6530 /* match incoming packets */
6531 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6536 bpf_error("inbound/outbound not supported on linktype %d",
6544 #ifdef HAVE_NET_PFVAR_H
6545 /* PF firewall log matched interface */
6547 gen_pf_ifname(const char *ifname
)
6552 if (linktype
!= DLT_PFLOG
) {
6553 bpf_error("ifname supported only on PF linktype");
6556 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6557 off
= offsetof(struct pfloghdr
, ifname
);
6558 if (strlen(ifname
) >= len
) {
6559 bpf_error("ifname interface names can only be %d characters",
6563 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6567 /* PF firewall log ruleset name */
6569 gen_pf_ruleset(char *ruleset
)
6573 if (linktype
!= DLT_PFLOG
) {
6574 bpf_error("ruleset supported only on PF linktype");
6578 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6579 bpf_error("ruleset names can only be %ld characters",
6580 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6584 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6585 strlen(ruleset
), (const u_char
*)ruleset
);
6589 /* PF firewall log rule number */
6595 if (linktype
!= DLT_PFLOG
) {
6596 bpf_error("rnr supported only on PF linktype");
6600 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6605 /* PF firewall log sub-rule number */
6607 gen_pf_srnr(int srnr
)
6611 if (linktype
!= DLT_PFLOG
) {
6612 bpf_error("srnr supported only on PF linktype");
6616 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6621 /* PF firewall log reason code */
6623 gen_pf_reason(int reason
)
6627 if (linktype
!= DLT_PFLOG
) {
6628 bpf_error("reason supported only on PF linktype");
6632 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6637 /* PF firewall log action */
6639 gen_pf_action(int action
)
6643 if (linktype
!= DLT_PFLOG
) {
6644 bpf_error("action supported only on PF linktype");
6648 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6652 #else /* !HAVE_NET_PFVAR_H */
6654 gen_pf_ifname(const char *ifname
)
6656 bpf_error("libpcap was compiled without pf support");
6662 gen_pf_ruleset(char *ruleset
)
6664 bpf_error("libpcap was compiled on a machine without pf support");
6672 bpf_error("libpcap was compiled on a machine without pf support");
6678 gen_pf_srnr(int srnr
)
6680 bpf_error("libpcap was compiled on a machine without pf support");
6686 gen_pf_reason(int reason
)
6688 bpf_error("libpcap was compiled on a machine without pf support");
6694 gen_pf_action(int action
)
6696 bpf_error("libpcap was compiled on a machine without pf support");
6700 #endif /* HAVE_NET_PFVAR_H */
6702 /* IEEE 802.11 wireless header */
6704 gen_p80211_type(int type
, int mask
)
6710 case DLT_IEEE802_11
:
6711 case DLT_PRISM_HEADER
:
6712 case DLT_IEEE802_11_RADIO_AVS
:
6713 case DLT_IEEE802_11_RADIO
:
6714 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
6719 bpf_error("802.11 link-layer types supported only on 802.11");
6727 register const u_char
*eaddr
;
6733 case DLT_ARCNET_LINUX
:
6734 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
6736 return (gen_ahostop(eaddr
, (int)q
.dir
));
6738 bpf_error("ARCnet address used in non-arc expression");
6744 bpf_error("aid supported only on ARCnet");
6747 bpf_error("ARCnet address used in non-arc expression");
6752 static struct block
*
6753 gen_ahostop(eaddr
, dir
)
6754 register const u_char
*eaddr
;
6757 register struct block
*b0
, *b1
;
6760 /* src comes first, different from Ethernet */
6762 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6765 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6768 b0
= gen_ahostop(eaddr
, Q_SRC
);
6769 b1
= gen_ahostop(eaddr
, Q_DST
);
6775 b0
= gen_ahostop(eaddr
, Q_SRC
);
6776 b1
= gen_ahostop(eaddr
, Q_DST
);
6785 * support IEEE 802.1Q VLAN trunk over ethernet
6791 struct block
*b0
, *b1
;
6793 /* can't check for VLAN-encapsulated packets inside MPLS */
6794 if (label_stack_depth
> 0)
6795 bpf_error("no VLAN match after MPLS");
6798 * Change the offsets to point to the type and data fields within
6799 * the VLAN packet. Just increment the offsets, so that we
6800 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6801 * capture VLAN 200 encapsulated within VLAN 100.
6803 * XXX - this is a bit of a kludge. If we were to split the
6804 * compiler into a parser that parses an expression and
6805 * generates an expression tree, and a code generator that
6806 * takes an expression tree (which could come from our
6807 * parser or from some other parser) and generates BPF code,
6808 * we could perhaps make the offsets parameters of routines
6809 * and, in the handler for an "AND" node, pass to subnodes
6810 * other than the VLAN node the adjusted offsets.
6812 * This would mean that "vlan" would, instead of changing the
6813 * behavior of *all* tests after it, change only the behavior
6814 * of tests ANDed with it. That would change the documented
6815 * semantics of "vlan", which might break some expressions.
6816 * However, it would mean that "(vlan and ip) or ip" would check
6817 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6818 * checking only for VLAN-encapsulated IP, so that could still
6819 * be considered worth doing; it wouldn't break expressions
6820 * that are of the form "vlan and ..." or "vlan N and ...",
6821 * which I suspect are the most common expressions involving
6822 * "vlan". "vlan or ..." doesn't necessarily do what the user
6823 * would really want, now, as all the "or ..." tests would
6824 * be done assuming a VLAN, even though the "or" could be viewed
6825 * as meaning "or, if this isn't a VLAN packet...".
6827 orig_linktype
= off_linktype
; /* save original values */
6839 bpf_error("no VLAN support for data link type %d",
6844 /* check for VLAN */
6845 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6847 /* If a specific VLAN is requested, check VLAN id */
6848 if (vlan_num
>= 0) {
6849 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6865 struct block
*b0
,*b1
;
6868 * Change the offsets to point to the type and data fields within
6869 * the MPLS packet. Just increment the offsets, so that we
6870 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6871 * capture packets with an outer label of 100000 and an inner
6874 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6878 if (label_stack_depth
> 0) {
6879 /* just match the bottom-of-stack bit clear */
6880 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6883 * Indicate that we're checking MPLS-encapsulated headers,
6884 * to make sure higher level code generators don't try to
6885 * match against IP-related protocols such as Q_ARP, Q_RARP
6890 case DLT_C_HDLC
: /* fall through */
6892 b0
= gen_linktype(ETHERTYPE_MPLS
);
6896 b0
= gen_linktype(PPP_MPLS_UCAST
);
6899 /* FIXME add other DLT_s ...
6900 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6901 * leave it for now */
6904 bpf_error("no MPLS support for data link type %d",
6912 /* If a specific MPLS label is requested, check it */
6913 if (label_num
>= 0) {
6914 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6915 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6916 0xfffff000); /* only compare the first 20 bits */
6923 label_stack_depth
++;
6928 * Support PPPOE discovery and session.
6933 /* check for PPPoE discovery */
6934 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
6943 * Test against the PPPoE session link-layer type.
6945 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
6948 * Change the offsets to point to the type and data fields within
6951 * XXX - this is a bit of a kludge. If we were to split the
6952 * compiler into a parser that parses an expression and
6953 * generates an expression tree, and a code generator that
6954 * takes an expression tree (which could come from our
6955 * parser or from some other parser) and generates BPF code,
6956 * we could perhaps make the offsets parameters of routines
6957 * and, in the handler for an "AND" node, pass to subnodes
6958 * other than the PPPoE node the adjusted offsets.
6960 * This would mean that "pppoes" would, instead of changing the
6961 * behavior of *all* tests after it, change only the behavior
6962 * of tests ANDed with it. That would change the documented
6963 * semantics of "pppoes", which might break some expressions.
6964 * However, it would mean that "(pppoes and ip) or ip" would check
6965 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6966 * checking only for VLAN-encapsulated IP, so that could still
6967 * be considered worth doing; it wouldn't break expressions
6968 * that are of the form "pppoes and ..." which I suspect are the
6969 * most common expressions involving "pppoes". "pppoes or ..."
6970 * doesn't necessarily do what the user would really want, now,
6971 * as all the "or ..." tests would be done assuming PPPoE, even
6972 * though the "or" could be viewed as meaning "or, if this isn't
6973 * a PPPoE packet...".
6975 orig_linktype
= off_linktype
; /* save original values */
6979 * The "network-layer" protocol is PPPoE, which has a 6-byte
6980 * PPPoE header, followed by PPP payload, so we set the
6981 * offsets to the network layer offset plus 6 bytes for
6982 * the PPPoE header plus the values appropriate for PPP when
6983 * encapsulated in Ethernet (which means there's no HDLC
6986 off_linktype
= orig_nl
+ 6;
6987 off_nl
= orig_nl
+ 6 + 2;
6988 off_nl_nosnap
= orig_nl
+ 6 + 2;
6991 * Set the link-layer type to PPP, as all subsequent tests will
6992 * be on the encapsulated PPP header.
7000 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7012 bpf_error("'vpi' supported only on raw ATM");
7013 if (off_vpi
== (u_int
)-1)
7015 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7021 bpf_error("'vci' supported only on raw ATM");
7022 if (off_vci
== (u_int
)-1)
7024 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7029 if (off_proto
== (u_int
)-1)
7030 abort(); /* XXX - this isn't on FreeBSD */
7031 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7036 if (off_payload
== (u_int
)-1)
7038 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7039 0xffffffff, jtype
, reverse
, jvalue
);
7044 bpf_error("'callref' supported only on raw ATM");
7045 if (off_proto
== (u_int
)-1)
7047 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7048 jtype
, reverse
, jvalue
);
7058 gen_atmtype_abbrev(type
)
7061 struct block
*b0
, *b1
;
7066 /* Get all packets in Meta signalling Circuit */
7068 bpf_error("'metac' supported only on raw ATM");
7069 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7070 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7075 /* Get all packets in Broadcast Circuit*/
7077 bpf_error("'bcc' supported only on raw ATM");
7078 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7079 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7084 /* Get all cells in Segment OAM F4 circuit*/
7086 bpf_error("'oam4sc' supported only on raw ATM");
7087 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7088 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7093 /* Get all cells in End-to-End OAM F4 Circuit*/
7095 bpf_error("'oam4ec' supported only on raw ATM");
7096 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7097 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7102 /* Get all packets in connection Signalling Circuit */
7104 bpf_error("'sc' supported only on raw ATM");
7105 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7106 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7111 /* Get all packets in ILMI Circuit */
7113 bpf_error("'ilmic' supported only on raw ATM");
7114 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7115 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7120 /* Get all LANE packets */
7122 bpf_error("'lane' supported only on raw ATM");
7123 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7126 * Arrange that all subsequent tests assume LANE
7127 * rather than LLC-encapsulated packets, and set
7128 * the offsets appropriately for LANE-encapsulated
7131 * "off_mac" is the offset of the Ethernet header,
7132 * which is 2 bytes past the ATM pseudo-header
7133 * (skipping the pseudo-header and 2-byte LE Client
7134 * field). The other offsets are Ethernet offsets
7135 * relative to "off_mac".
7138 off_mac
= off_payload
+ 2; /* MAC header */
7139 off_linktype
= off_mac
+ 12;
7140 off_nl
= off_mac
+ 14; /* Ethernet II */
7141 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
7145 /* Get all LLC-encapsulated packets */
7147 bpf_error("'llc' supported only on raw ATM");
7148 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7159 * Filtering for MTP2 messages based on li value
7160 * FISU, length is null
7161 * LSSU, length is 1 or 2
7162 * MSU, length is 3 or more
7165 gen_mtp2type_abbrev(type
)
7168 struct block
*b0
, *b1
;
7173 if ( (linktype
!= DLT_MTP2
) &&
7174 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7175 bpf_error("'fisu' supported only on MTP2");
7176 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7177 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7181 if ( (linktype
!= DLT_MTP2
) &&
7182 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7183 bpf_error("'lssu' supported only on MTP2");
7184 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7185 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7190 if ( (linktype
!= DLT_MTP2
) &&
7191 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7192 bpf_error("'msu' supported only on MTP2");
7193 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7203 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7210 bpf_u_int32 val1
, val2
, val3
;
7212 switch (mtp3field
) {
7215 if (off_sio
== (u_int
)-1)
7216 bpf_error("'sio' supported only on SS7");
7217 /* sio coded on 1 byte so max value 255 */
7219 bpf_error("sio value %u too big; max value = 255",
7221 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7222 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7226 if (off_opc
== (u_int
)-1)
7227 bpf_error("'opc' supported only on SS7");
7228 /* opc coded on 14 bits so max value 16383 */
7230 bpf_error("opc value %u too big; max value = 16383",
7232 /* the following instructions are made to convert jvalue
7233 * to the form used to write opc in an ss7 message*/
7234 val1
= jvalue
& 0x00003c00;
7236 val2
= jvalue
& 0x000003fc;
7238 val3
= jvalue
& 0x00000003;
7240 jvalue
= val1
+ val2
+ val3
;
7241 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7242 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7246 if (off_dpc
== (u_int
)-1)
7247 bpf_error("'dpc' supported only on SS7");
7248 /* dpc coded on 14 bits so max value 16383 */
7250 bpf_error("dpc value %u too big; max value = 16383",
7252 /* the following instructions are made to convert jvalue
7253 * to the forme used to write dpc in an ss7 message*/
7254 val1
= jvalue
& 0x000000ff;
7256 val2
= jvalue
& 0x00003f00;
7258 jvalue
= val1
+ val2
;
7259 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7260 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7264 if (off_sls
== (u_int
)-1)
7265 bpf_error("'sls' supported only on SS7");
7266 /* sls coded on 4 bits so max value 15 */
7268 bpf_error("sls value %u too big; max value = 15",
7270 /* the following instruction is made to convert jvalue
7271 * to the forme used to write sls in an ss7 message*/
7272 jvalue
= jvalue
<< 4;
7273 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7274 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7283 static struct block
*
7284 gen_msg_abbrev(type
)
7290 * Q.2931 signalling protocol messages for handling virtual circuits
7291 * establishment and teardown
7296 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7300 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7304 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7308 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7312 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7315 case A_RELEASE_DONE
:
7316 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7326 gen_atmmulti_abbrev(type
)
7329 struct block
*b0
, *b1
;
7335 bpf_error("'oam' supported only on raw ATM");
7336 b1
= gen_atmmulti_abbrev(A_OAMF4
);
7341 bpf_error("'oamf4' supported only on raw ATM");
7343 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7344 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7346 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7352 * Get Q.2931 signalling messages for switched
7353 * virtual connection
7356 bpf_error("'connectmsg' supported only on raw ATM");
7357 b0
= gen_msg_abbrev(A_SETUP
);
7358 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7360 b0
= gen_msg_abbrev(A_CONNECT
);
7362 b0
= gen_msg_abbrev(A_CONNECTACK
);
7364 b0
= gen_msg_abbrev(A_RELEASE
);
7366 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7368 b0
= gen_atmtype_abbrev(A_SC
);
7374 bpf_error("'metaconnect' supported only on raw ATM");
7375 b0
= gen_msg_abbrev(A_SETUP
);
7376 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7378 b0
= gen_msg_abbrev(A_CONNECT
);
7380 b0
= gen_msg_abbrev(A_RELEASE
);
7382 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7384 b0
= gen_atmtype_abbrev(A_METAC
);