1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.290.2.17 2008-12-21 19:29:49 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"
71 #include "ieee80211.h"
73 #include "sunatmpos.h"
77 #ifdef HAVE_NET_PFVAR_H
78 #include <sys/socket.h>
80 #include <net/pfvar.h>
81 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
88 #include <netdb.h> /* for "struct addrinfo" */
91 #include <pcap/namedb.h>
96 #define IPPROTO_SCTP 132
99 #ifdef HAVE_OS_PROTO_H
100 #include "os-proto.h"
103 #define JMP(c) ((c)|BPF_JMP|BPF_K)
106 static jmp_buf top_ctx
;
107 static pcap_t
*bpf_pcap
;
109 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
111 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
113 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
118 static int pcap_fddipad
;
123 bpf_error(const char *fmt
, ...)
128 if (bpf_pcap
!= NULL
)
129 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
136 static void init_linktype(pcap_t
*);
138 static void init_regs(void);
139 static int alloc_reg(void);
140 static void free_reg(int);
142 static struct block
*root
;
145 * Value passed to gen_load_a() to indicate what the offset argument
149 OR_PACKET
, /* relative to the beginning of the packet */
150 OR_LINK
, /* relative to the beginning of the link-layer header */
151 OR_MACPL
, /* relative to the end of the MAC-layer header */
152 OR_NET
, /* relative to the network-layer header */
153 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
154 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
155 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
159 * We divy out chunks of memory rather than call malloc each time so
160 * we don't have to worry about leaking memory. It's probably
161 * not a big deal if all this memory was wasted but if this ever
162 * goes into a library that would probably not be a good idea.
164 * XXX - this *is* in a library....
167 #define CHUNK0SIZE 1024
173 static struct chunk chunks
[NCHUNKS
];
174 static int cur_chunk
;
176 static void *newchunk(u_int
);
177 static void freechunks(void);
178 static inline struct block
*new_block(int);
179 static inline struct slist
*new_stmt(int);
180 static struct block
*gen_retblk(int);
181 static inline void syntax(void);
183 static void backpatch(struct block
*, struct block
*);
184 static void merge(struct block
*, struct block
*);
185 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
186 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
187 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
188 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
189 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
190 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
192 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
193 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
194 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
195 static struct slist
*gen_load_llrel(u_int
, u_int
);
196 static struct slist
*gen_load_macplrel(u_int
, u_int
);
197 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
198 static struct slist
*gen_loadx_iphdrlen(void);
199 static struct block
*gen_uncond(int);
200 static inline struct block
*gen_true(void);
201 static inline struct block
*gen_false(void);
202 static struct block
*gen_ether_linktype(int);
203 static struct block
*gen_linux_sll_linktype(int);
204 static struct slist
*gen_load_prism_llprefixlen(void);
205 static struct slist
*gen_load_avs_llprefixlen(void);
206 static struct slist
*gen_load_radiotap_llprefixlen(void);
207 static struct slist
*gen_load_ppi_llprefixlen(void);
208 static void insert_compute_vloffsets(struct block
*);
209 static struct slist
*gen_llprefixlen(void);
210 static struct slist
*gen_off_macpl(void);
211 static int ethertype_to_ppptype(int);
212 static struct block
*gen_linktype(int);
213 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
214 static struct block
*gen_llc_linktype(int);
215 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
217 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
219 static struct block
*gen_ahostop(const u_char
*, int);
220 static struct block
*gen_ehostop(const u_char
*, int);
221 static struct block
*gen_fhostop(const u_char
*, int);
222 static struct block
*gen_thostop(const u_char
*, int);
223 static struct block
*gen_wlanhostop(const u_char
*, int);
224 static struct block
*gen_ipfchostop(const u_char
*, int);
225 static struct block
*gen_dnhostop(bpf_u_int32
, int);
226 static struct block
*gen_mpls_linktype(int);
227 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
229 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
232 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
234 static struct block
*gen_ipfrag(void);
235 static struct block
*gen_portatom(int, bpf_int32
);
236 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
238 static struct block
*gen_portatom6(int, bpf_int32
);
239 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
241 struct block
*gen_portop(int, int, int);
242 static struct block
*gen_port(int, int, int);
243 struct block
*gen_portrangeop(int, int, int, int);
244 static struct block
*gen_portrange(int, int, int, int);
246 struct block
*gen_portop6(int, int, int);
247 static struct block
*gen_port6(int, int, int);
248 struct block
*gen_portrangeop6(int, int, int, int);
249 static struct block
*gen_portrange6(int, int, int, int);
251 static int lookup_proto(const char *, int);
252 static struct block
*gen_protochain(int, int, int);
253 static struct block
*gen_proto(int, int, int);
254 static struct slist
*xfer_to_x(struct arth
*);
255 static struct slist
*xfer_to_a(struct arth
*);
256 static struct block
*gen_mac_multicast(int);
257 static struct block
*gen_len(int, int);
258 static struct block
*gen_check_802_11_data_frame(void);
260 static struct block
*gen_ppi_dlt_check(void);
261 static struct block
*gen_msg_abbrev(int type
);
272 /* XXX Round up to nearest long. */
273 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
275 /* XXX Round up to structure boundary. */
279 cp
= &chunks
[cur_chunk
];
280 if (n
> cp
->n_left
) {
281 ++cp
, k
= ++cur_chunk
;
283 bpf_error("out of memory");
284 size
= CHUNK0SIZE
<< k
;
285 cp
->m
= (void *)malloc(size
);
287 bpf_error("out of memory");
288 memset((char *)cp
->m
, 0, size
);
291 bpf_error("out of memory");
294 return (void *)((char *)cp
->m
+ cp
->n_left
);
303 for (i
= 0; i
< NCHUNKS
; ++i
)
304 if (chunks
[i
].m
!= NULL
) {
311 * A strdup whose allocations are freed after code generation is over.
315 register const char *s
;
317 int n
= strlen(s
) + 1;
318 char *cp
= newchunk(n
);
324 static inline struct block
*
330 p
= (struct block
*)newchunk(sizeof(*p
));
337 static inline struct slist
*
343 p
= (struct slist
*)newchunk(sizeof(*p
));
349 static struct block
*
353 struct block
*b
= new_block(BPF_RET
|BPF_K
);
362 bpf_error("syntax error in filter expression");
365 static bpf_u_int32 netmask
;
370 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
371 const char *buf
, int optimize
, bpf_u_int32 mask
)
374 const char * volatile xbuf
= buf
;
382 if (setjmp(top_ctx
)) {
390 snaplen
= pcap_snapshot(p
);
392 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
393 "snaplen of 0 rejects all packets");
397 lex_init(xbuf
? xbuf
: "");
405 root
= gen_retblk(snaplen
);
407 if (optimize
&& !no_optimize
) {
410 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
411 bpf_error("expression rejects all packets");
413 program
->bf_insns
= icode_to_fcode(root
, &len
);
414 program
->bf_len
= len
;
422 * entry point for using the compiler with no pcap open
423 * pass in all the stuff that is needed explicitly instead.
426 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
427 struct bpf_program
*program
,
428 const char *buf
, int optimize
, bpf_u_int32 mask
)
433 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
436 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
442 * Clean up a "struct bpf_program" by freeing all the memory allocated
446 pcap_freecode(struct bpf_program
*program
)
449 if (program
->bf_insns
!= NULL
) {
450 free((char *)program
->bf_insns
);
451 program
->bf_insns
= NULL
;
456 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
457 * which of the jt and jf fields has been resolved and which is a pointer
458 * back to another unresolved block (or nil). At least one of the fields
459 * in each block is already resolved.
462 backpatch(list
, target
)
463 struct block
*list
, *target
;
480 * Merge the lists in b0 and b1, using the 'sense' field to indicate
481 * which of jt and jf is the link.
485 struct block
*b0
, *b1
;
487 register struct block
**p
= &b0
;
489 /* Find end of list. */
491 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
493 /* Concatenate the lists. */
501 struct block
*ppi_dlt_check
;
504 * Insert before the statements of the first (root) block any
505 * statements needed to load the lengths of any variable-length
506 * headers into registers.
508 * XXX - a fancier strategy would be to insert those before the
509 * statements of all blocks that use those lengths and that
510 * have no predecessors that use them, so that we only compute
511 * the lengths if we need them. There might be even better
512 * approaches than that.
514 * However, those strategies would be more complicated, and
515 * as we don't generate code to compute a length if the
516 * program has no tests that use the length, and as most
517 * tests will probably use those lengths, we would just
518 * postpone computing the lengths so that it's not done
519 * for tests that fail early, and it's not clear that's
522 insert_compute_vloffsets(p
->head
);
525 * For DLT_PPI captures, generate a check of the per-packet
526 * DLT value to make sure it's DLT_IEEE802_11.
528 ppi_dlt_check
= gen_ppi_dlt_check();
529 if (ppi_dlt_check
!= NULL
)
530 gen_and(ppi_dlt_check
, p
);
532 backpatch(p
, gen_retblk(snaplen
));
533 p
->sense
= !p
->sense
;
534 backpatch(p
, gen_retblk(0));
540 struct block
*b0
, *b1
;
542 backpatch(b0
, b1
->head
);
543 b0
->sense
= !b0
->sense
;
544 b1
->sense
= !b1
->sense
;
546 b1
->sense
= !b1
->sense
;
552 struct block
*b0
, *b1
;
554 b0
->sense
= !b0
->sense
;
555 backpatch(b0
, b1
->head
);
556 b0
->sense
= !b0
->sense
;
565 b
->sense
= !b
->sense
;
568 static struct block
*
569 gen_cmp(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
577 static struct block
*
578 gen_cmp_gt(offrel
, offset
, size
, v
)
579 enum e_offrel offrel
;
583 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
586 static struct block
*
587 gen_cmp_ge(offrel
, offset
, size
, v
)
588 enum e_offrel offrel
;
592 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
595 static struct block
*
596 gen_cmp_lt(offrel
, offset
, size
, v
)
597 enum e_offrel offrel
;
601 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
604 static struct block
*
605 gen_cmp_le(offrel
, offset
, size
, v
)
606 enum e_offrel offrel
;
610 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
613 static struct block
*
614 gen_mcmp(offrel
, offset
, size
, v
, mask
)
615 enum e_offrel offrel
;
620 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
623 static struct block
*
624 gen_bcmp(offrel
, offset
, size
, v
)
625 enum e_offrel offrel
;
626 register u_int offset
, size
;
627 register const u_char
*v
;
629 register struct block
*b
, *tmp
;
633 register const u_char
*p
= &v
[size
- 4];
634 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
635 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
637 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
644 register const u_char
*p
= &v
[size
- 2];
645 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
647 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
654 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
663 * AND the field of size "size" at offset "offset" relative to the header
664 * specified by "offrel" with "mask", and compare it with the value "v"
665 * with the test specified by "jtype"; if "reverse" is true, the test
666 * should test the opposite of "jtype".
668 static struct block
*
669 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
670 enum e_offrel offrel
;
672 bpf_u_int32 offset
, size
, mask
, jtype
;
675 struct slist
*s
, *s2
;
678 s
= gen_load_a(offrel
, offset
, size
);
680 if (mask
!= 0xffffffff) {
681 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
686 b
= new_block(JMP(jtype
));
689 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
695 * Various code constructs need to know the layout of the data link
696 * layer. These variables give the necessary offsets from the beginning
697 * of the packet data.
701 * This is the offset of the beginning of the link-layer header from
702 * the beginning of the raw packet data.
704 * It's usually 0, except for 802.11 with a fixed-length radio header.
705 * (For 802.11 with a variable-length radio header, we have to generate
706 * code to compute that offset; off_ll is 0 in that case.)
711 * If there's a variable-length header preceding the link-layer header,
712 * "reg_off_ll" is the register number for a register containing the
713 * length of that header, and therefore the offset of the link-layer
714 * header from the beginning of the raw packet data. Otherwise,
715 * "reg_off_ll" is -1.
717 static int reg_off_ll
;
720 * This is the offset of the beginning of the MAC-layer header from
721 * the beginning of the link-layer header.
722 * It's usually 0, except for ATM LANE, where it's the offset, relative
723 * to the beginning of the raw packet data, of the Ethernet header.
725 static u_int off_mac
;
728 * This is the offset of the beginning of the MAC-layer payload,
729 * from the beginning of the raw packet data.
731 * I.e., it's the sum of the length of the link-layer header (without,
732 * for example, any 802.2 LLC header, so it's the MAC-layer
733 * portion of that header), plus any prefix preceding the
736 static u_int off_macpl
;
739 * This is 1 if the offset of the beginning of the MAC-layer payload
740 * from the beginning of the link-layer header is variable-length.
742 static int off_macpl_is_variable
;
745 * If the link layer has variable_length headers, "reg_off_macpl"
746 * is the register number for a register containing the length of the
747 * link-layer header plus the length of any variable-length header
748 * preceding the link-layer header. Otherwise, "reg_off_macpl"
751 static int reg_off_macpl
;
754 * "off_linktype" is the offset to information in the link-layer header
755 * giving the packet type. This offset is relative to the beginning
756 * of the link-layer header (i.e., it doesn't include off_ll).
758 * For Ethernet, it's the offset of the Ethernet type field.
760 * For link-layer types that always use 802.2 headers, it's the
761 * offset of the LLC header.
763 * For PPP, it's the offset of the PPP type field.
765 * For Cisco HDLC, it's the offset of the CHDLC type field.
767 * For BSD loopback, it's the offset of the AF_ value.
769 * For Linux cooked sockets, it's the offset of the type field.
771 * It's set to -1 for no encapsulation, in which case, IP is assumed.
773 static u_int off_linktype
;
776 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
777 * checks to check the PPP header, assumed to follow a LAN-style link-
778 * layer header and a PPPoE session header.
780 static int is_pppoes
= 0;
783 * TRUE if the link layer includes an ATM pseudo-header.
785 static int is_atm
= 0;
788 * TRUE if "lane" appeared in the filter; it causes us to generate
789 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
791 static int is_lane
= 0;
794 * These are offsets for the ATM pseudo-header.
796 static u_int off_vpi
;
797 static u_int off_vci
;
798 static u_int off_proto
;
801 * These are offsets for the MTP2 fields.
806 * These are offsets for the MTP3 fields.
808 static u_int off_sio
;
809 static u_int off_opc
;
810 static u_int off_dpc
;
811 static u_int off_sls
;
814 * This is the offset of the first byte after the ATM pseudo_header,
815 * or -1 if there is no ATM pseudo-header.
817 static u_int off_payload
;
820 * These are offsets to the beginning of the network-layer header.
821 * They are relative to the beginning of the MAC-layer payload (i.e.,
822 * they don't include off_ll or off_macpl).
824 * If the link layer never uses 802.2 LLC:
826 * "off_nl" and "off_nl_nosnap" are the same.
828 * If the link layer always uses 802.2 LLC:
830 * "off_nl" is the offset if there's a SNAP header following
833 * "off_nl_nosnap" is the offset if there's no SNAP header.
835 * If the link layer is Ethernet:
837 * "off_nl" is the offset if the packet is an Ethernet II packet
838 * (we assume no 802.3+802.2+SNAP);
840 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
841 * with an 802.2 header following it.
844 static u_int off_nl_nosnap
;
852 linktype
= pcap_datalink(p
);
854 pcap_fddipad
= p
->fddipad
;
858 * Assume it's not raw ATM with a pseudo-header, for now.
869 * And that we're not doing PPPoE.
874 * And assume we're not doing SS7.
883 * Also assume it's not 802.11.
887 off_macpl_is_variable
= 0;
891 label_stack_depth
= 0;
901 off_nl
= 0; /* XXX in reality, variable! */
902 off_nl_nosnap
= 0; /* no 802.2 LLC */
905 case DLT_ARCNET_LINUX
:
908 off_nl
= 0; /* XXX in reality, variable! */
909 off_nl_nosnap
= 0; /* no 802.2 LLC */
914 off_macpl
= 14; /* Ethernet header length */
915 off_nl
= 0; /* Ethernet II */
916 off_nl_nosnap
= 3; /* 802.3+802.2 */
921 * SLIP doesn't have a link level type. The 16 byte
922 * header is hacked into our SLIP driver.
927 off_nl_nosnap
= 0; /* no 802.2 LLC */
931 /* XXX this may be the same as the DLT_PPP_BSDOS case */
936 off_nl_nosnap
= 0; /* no 802.2 LLC */
944 off_nl_nosnap
= 0; /* no 802.2 LLC */
951 off_nl_nosnap
= 0; /* no 802.2 LLC */
956 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
957 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 * This does no include the Ethernet header, and
967 * only covers session state.
972 off_nl_nosnap
= 0; /* no 802.2 LLC */
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
984 * FDDI doesn't really have a link-level type field.
985 * We set "off_linktype" to the offset of the LLC header.
987 * To check for Ethernet types, we assume that SSAP = SNAP
988 * is being used and pick out the encapsulated Ethernet type.
989 * XXX - should we generate code to check for SNAP?
993 off_linktype
+= pcap_fddipad
;
995 off_macpl
= 13; /* FDDI MAC header length */
997 off_macpl
+= pcap_fddipad
;
999 off_nl
= 8; /* 802.2+SNAP */
1000 off_nl_nosnap
= 3; /* 802.2 */
1005 * Token Ring doesn't really have a link-level type field.
1006 * We set "off_linktype" to the offset of the LLC header.
1008 * To check for Ethernet types, we assume that SSAP = SNAP
1009 * is being used and pick out the encapsulated Ethernet type.
1010 * XXX - should we generate code to check for SNAP?
1012 * XXX - the header is actually variable-length.
1013 * Some various Linux patched versions gave 38
1014 * as "off_linktype" and 40 as "off_nl"; however,
1015 * if a token ring packet has *no* routing
1016 * information, i.e. is not source-routed, the correct
1017 * values are 20 and 22, as they are in the vanilla code.
1019 * A packet is source-routed iff the uppermost bit
1020 * of the first byte of the source address, at an
1021 * offset of 8, has the uppermost bit set. If the
1022 * packet is source-routed, the total number of bytes
1023 * of routing information is 2 plus bits 0x1F00 of
1024 * the 16-bit value at an offset of 14 (shifted right
1025 * 8 - figure out which byte that is).
1028 off_macpl
= 14; /* Token Ring MAC header length */
1029 off_nl
= 8; /* 802.2+SNAP */
1030 off_nl_nosnap
= 3; /* 802.2 */
1033 case DLT_IEEE802_11
:
1034 case DLT_PRISM_HEADER
:
1035 case DLT_IEEE802_11_RADIO_AVS
:
1036 case DLT_IEEE802_11_RADIO
:
1038 * 802.11 doesn't really have a link-level type field.
1039 * We set "off_linktype" to the offset of the LLC header.
1041 * To check for Ethernet types, we assume that SSAP = SNAP
1042 * is being used and pick out the encapsulated Ethernet type.
1043 * XXX - should we generate code to check for SNAP?
1045 * We also handle variable-length radio headers here.
1046 * The Prism header is in theory variable-length, but in
1047 * practice it's always 144 bytes long. However, some
1048 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1049 * sometimes or always supply an AVS header, so we
1050 * have to check whether the radio header is a Prism
1051 * header or an AVS header, so, in practice, it's
1055 off_macpl
= 0; /* link-layer header is variable-length */
1056 off_macpl_is_variable
= 1;
1057 off_nl
= 8; /* 802.2+SNAP */
1058 off_nl_nosnap
= 3; /* 802.2 */
1063 * At the moment we treat PPI the same way that we treat
1064 * normal Radiotap encoded packets. The difference is in
1065 * the function that generates the code at the beginning
1066 * to compute the header length. Since this code generator
1067 * of PPI supports bare 802.11 encapsulation only (i.e.
1068 * the encapsulated DLT should be DLT_IEEE802_11) we
1069 * generate code to check for this too.
1072 off_macpl
= 0; /* link-layer header is variable-length */
1073 off_macpl_is_variable
= 1;
1074 off_nl
= 8; /* 802.2+SNAP */
1075 off_nl_nosnap
= 3; /* 802.2 */
1078 case DLT_ATM_RFC1483
:
1079 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1081 * assume routed, non-ISO PDUs
1082 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1084 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1085 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1086 * latter would presumably be treated the way PPPoE
1087 * should be, so you can do "pppoe and udp port 2049"
1088 * or "pppoa and tcp port 80" and have it check for
1089 * PPPo{A,E} and a PPP protocol of IP and....
1092 off_macpl
= 0; /* packet begins with LLC header */
1093 off_nl
= 8; /* 802.2+SNAP */
1094 off_nl_nosnap
= 3; /* 802.2 */
1099 * Full Frontal ATM; you get AALn PDUs with an ATM
1103 off_vpi
= SUNATM_VPI_POS
;
1104 off_vci
= SUNATM_VCI_POS
;
1105 off_proto
= PROTO_POS
;
1106 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1107 off_payload
= SUNATM_PKT_BEGIN_POS
;
1108 off_linktype
= off_payload
;
1109 off_macpl
= off_payload
; /* if LLC-encapsulated */
1110 off_nl
= 8; /* 802.2+SNAP */
1111 off_nl_nosnap
= 3; /* 802.2 */
1118 off_nl_nosnap
= 0; /* no 802.2 LLC */
1121 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1125 off_nl_nosnap
= 0; /* no 802.2 LLC */
1130 * LocalTalk does have a 1-byte type field in the LLAP header,
1131 * but really it just indicates whether there is a "short" or
1132 * "long" DDP packet following.
1137 off_nl_nosnap
= 0; /* no 802.2 LLC */
1140 case DLT_IP_OVER_FC
:
1142 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1143 * link-level type field. We set "off_linktype" to the
1144 * offset of the LLC header.
1146 * To check for Ethernet types, we assume that SSAP = SNAP
1147 * is being used and pick out the encapsulated Ethernet type.
1148 * XXX - should we generate code to check for SNAP? RFC
1149 * 2625 says SNAP should be used.
1153 off_nl
= 8; /* 802.2+SNAP */
1154 off_nl_nosnap
= 3; /* 802.2 */
1159 * XXX - we should set this to handle SNAP-encapsulated
1160 * frames (NLPID of 0x80).
1165 off_nl_nosnap
= 0; /* no 802.2 LLC */
1169 * the only BPF-interesting FRF.16 frames are non-control frames;
1170 * Frame Relay has a variable length link-layer
1171 * so lets start with offset 4 for now and increments later on (FIXME);
1177 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1180 case DLT_APPLE_IP_OVER_IEEE1394
:
1184 off_nl_nosnap
= 0; /* no 802.2 LLC */
1187 case DLT_LINUX_IRDA
:
1189 * Currently, only raw "link[N:M]" filtering is supported.
1199 * Currently, only raw "link[N:M]" filtering is supported.
1207 case DLT_SYMANTEC_FIREWALL
:
1210 off_nl
= 0; /* Ethernet II */
1211 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1214 #ifdef HAVE_NET_PFVAR_H
1217 off_macpl
= PFLOG_HDRLEN
;
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 case DLT_JUNIPER_MFR
:
1224 case DLT_JUNIPER_MLFR
:
1225 case DLT_JUNIPER_MLPPP
:
1226 case DLT_JUNIPER_PPP
:
1227 case DLT_JUNIPER_CHDLC
:
1228 case DLT_JUNIPER_FRELAY
:
1232 off_nl_nosnap
= -1; /* no 802.2 LLC */
1235 case DLT_JUNIPER_ATM1
:
1236 off_linktype
= 4; /* in reality variable between 4-8 */
1237 off_macpl
= 4; /* in reality variable between 4-8 */
1242 case DLT_JUNIPER_ATM2
:
1243 off_linktype
= 8; /* in reality variable between 8-12 */
1244 off_macpl
= 8; /* in reality variable between 8-12 */
1249 /* frames captured on a Juniper PPPoE service PIC
1250 * contain raw ethernet frames */
1251 case DLT_JUNIPER_PPPOE
:
1252 case DLT_JUNIPER_ETHER
:
1255 off_nl
= 18; /* Ethernet II */
1256 off_nl_nosnap
= 21; /* 802.3+802.2 */
1259 case DLT_JUNIPER_PPPOE_ATM
:
1263 off_nl_nosnap
= -1; /* no 802.2 LLC */
1266 case DLT_JUNIPER_GGSN
:
1270 off_nl_nosnap
= -1; /* no 802.2 LLC */
1273 case DLT_JUNIPER_ES
:
1275 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1276 off_nl
= -1; /* not really a network layer but raw IP addresses */
1277 off_nl_nosnap
= -1; /* no 802.2 LLC */
1280 case DLT_JUNIPER_MONITOR
:
1283 off_nl
= 0; /* raw IP/IP6 header */
1284 off_nl_nosnap
= -1; /* no 802.2 LLC */
1287 case DLT_JUNIPER_SERVICES
:
1289 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1290 off_nl
= -1; /* L3 proto location dep. on cookie type */
1291 off_nl_nosnap
= -1; /* no 802.2 LLC */
1294 case DLT_JUNIPER_VP
:
1301 case DLT_JUNIPER_ST
:
1308 case DLT_JUNIPER_ISM
:
1327 case DLT_MTP2_WITH_PHDR
:
1360 case DLT_LINUX_LAPD
:
1362 * Currently, only raw "link[N:M]" filtering is supported.
1372 * Currently, only raw "link[N:M]" filtering is supported.
1380 case DLT_BLUETOOTH_HCI_H4
:
1382 * Currently, only raw "link[N:M]" filtering is supported.
1392 * Currently, only raw "link[N:M]" filtering is supported.
1402 * Currently, only raw "link[N:M]" filtering is supported.
1410 case DLT_IEEE802_15_4_LINUX
:
1412 * Currently, only raw "link[N:M]" filtering is supported.
1420 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1422 * Currently, only raw "link[N:M]" filtering is supported.
1430 case DLT_IEEE802_15_4
:
1432 * Currently, only raw "link[N:M]" filtering is supported.
1442 * Currently, only raw "link[N:M]" filtering is supported.
1452 * Currently, only raw "link[N:M]" filtering is supported.
1462 * Currently, only raw "link[N:M]" filtering is supported.
1470 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1472 * Currently, only raw "link[N:M]" filtering is supported.
1482 * Currently, only raw "link[N:M]" filtering is supported.
1484 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1486 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1487 off_nl_nosnap
= -1; /* no 802.2 LLC */
1488 off_mac
= 1; /* step over the kiss length byte */
1491 case DLT_IEEE802_15_4_NONASK_PHY
:
1493 * Currently, only raw "link[N:M]" filtering is supported.
1503 * Currently, only raw "link[N:M]" filtering is supported.
1511 bpf_error("unknown data link type %d", linktype
);
1516 * Load a value relative to the beginning of the link-layer header.
1517 * The link-layer header doesn't necessarily begin at the beginning
1518 * of the packet data; there might be a variable-length prefix containing
1519 * radio information.
1521 static struct slist
*
1522 gen_load_llrel(offset
, size
)
1525 struct slist
*s
, *s2
;
1527 s
= gen_llprefixlen();
1530 * If "s" is non-null, it has code to arrange that the X register
1531 * contains the length of the prefix preceding the link-layer
1534 * Otherwise, the length of the prefix preceding the link-layer
1535 * header is "off_ll".
1539 * There's a variable-length prefix preceding the
1540 * link-layer header. "s" points to a list of statements
1541 * that put the length of that prefix into the X register.
1542 * do an indirect load, to use the X register as an offset.
1544 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1549 * There is no variable-length header preceding the
1550 * link-layer header; add in off_ll, which, if there's
1551 * a fixed-length header preceding the link-layer header,
1552 * is the length of that header.
1554 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1555 s
->s
.k
= offset
+ off_ll
;
1561 * Load a value relative to the beginning of the MAC-layer payload.
1563 static struct slist
*
1564 gen_load_macplrel(offset
, size
)
1567 struct slist
*s
, *s2
;
1569 s
= gen_off_macpl();
1572 * If s is non-null, the offset of the MAC-layer payload is
1573 * variable, and s points to a list of instructions that
1574 * arrange that the X register contains that offset.
1576 * Otherwise, the offset of the MAC-layer payload is constant,
1577 * and is in off_macpl.
1581 * The offset of the MAC-layer payload is in the X
1582 * register. Do an indirect load, to use the X register
1585 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1590 * The offset of the MAC-layer payload is constant,
1591 * and is in off_macpl; load the value at that offset
1592 * plus the specified offset.
1594 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1595 s
->s
.k
= off_macpl
+ offset
;
1601 * Load a value relative to the beginning of the specified header.
1603 static struct slist
*
1604 gen_load_a(offrel
, offset
, size
)
1605 enum e_offrel offrel
;
1608 struct slist
*s
, *s2
;
1613 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1618 s
= gen_load_llrel(offset
, size
);
1622 s
= gen_load_macplrel(offset
, size
);
1626 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1630 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1635 * Load the X register with the length of the IPv4 header
1636 * (plus the offset of the link-layer header, if it's
1637 * preceded by a variable-length header such as a radio
1638 * header), in bytes.
1640 s
= gen_loadx_iphdrlen();
1643 * Load the item at {offset of the MAC-layer payload} +
1644 * {offset, relative to the start of the MAC-layer
1645 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1646 * {specified offset}.
1648 * (If the offset of the MAC-layer payload is variable,
1649 * it's included in the value in the X register, and
1652 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1653 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1658 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1669 * Generate code to load into the X register the sum of the length of
1670 * the IPv4 header and any variable-length header preceding the link-layer
1673 static struct slist
*
1674 gen_loadx_iphdrlen()
1676 struct slist
*s
, *s2
;
1678 s
= gen_off_macpl();
1681 * There's a variable-length prefix preceding the
1682 * link-layer header, or the link-layer header is itself
1683 * variable-length. "s" points to a list of statements
1684 * that put the offset of the MAC-layer payload into
1687 * The 4*([k]&0xf) addressing mode can't be used, as we
1688 * don't have a constant offset, so we have to load the
1689 * value in question into the A register and add to it
1690 * the value from the X register.
1692 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1695 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1698 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1703 * The A register now contains the length of the
1704 * IP header. We need to add to it the offset of
1705 * the MAC-layer payload, which is still in the X
1706 * register, and move the result into the X register.
1708 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1709 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1712 * There is no variable-length header preceding the
1713 * link-layer header, and the link-layer header is
1714 * fixed-length; load the length of the IPv4 header,
1715 * which is at an offset of off_nl from the beginning
1716 * of the MAC-layer payload, and thus at an offset
1717 * of off_mac_pl + off_nl from the beginning of the
1720 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1721 s
->s
.k
= off_macpl
+ off_nl
;
1726 static struct block
*
1733 s
= new_stmt(BPF_LD
|BPF_IMM
);
1735 b
= new_block(JMP(BPF_JEQ
));
1741 static inline struct block
*
1744 return gen_uncond(1);
1747 static inline struct block
*
1750 return gen_uncond(0);
1754 * Byte-swap a 32-bit number.
1755 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1756 * big-endian platforms.)
1758 #define SWAPLONG(y) \
1759 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1762 * Generate code to match a particular packet type.
1764 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1765 * value, if <= ETHERMTU. We use that to determine whether to
1766 * match the type/length field or to check the type/length field for
1767 * a value <= ETHERMTU to see whether it's a type field and then do
1768 * the appropriate test.
1770 static struct block
*
1771 gen_ether_linktype(proto
)
1774 struct block
*b0
, *b1
;
1780 case LLCSAP_NETBEUI
:
1782 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1783 * so we check the DSAP and SSAP.
1785 * LLCSAP_IP checks for IP-over-802.2, rather
1786 * than IP-over-Ethernet or IP-over-SNAP.
1788 * XXX - should we check both the DSAP and the
1789 * SSAP, like this, or should we check just the
1790 * DSAP, as we do for other types <= ETHERMTU
1791 * (i.e., other SAP values)?
1793 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1795 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1796 ((proto
<< 8) | proto
));
1804 * Ethernet_II frames, which are Ethernet
1805 * frames with a frame type of ETHERTYPE_IPX;
1807 * Ethernet_802.3 frames, which are 802.3
1808 * frames (i.e., the type/length field is
1809 * a length field, <= ETHERMTU, rather than
1810 * a type field) with the first two bytes
1811 * after the Ethernet/802.3 header being
1814 * Ethernet_802.2 frames, which are 802.3
1815 * frames with an 802.2 LLC header and
1816 * with the IPX LSAP as the DSAP in the LLC
1819 * Ethernet_SNAP frames, which are 802.3
1820 * frames with an LLC header and a SNAP
1821 * header and with an OUI of 0x000000
1822 * (encapsulated Ethernet) and a protocol
1823 * ID of ETHERTYPE_IPX in the SNAP header.
1825 * XXX - should we generate the same code both
1826 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1830 * This generates code to check both for the
1831 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1833 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1834 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1838 * Now we add code to check for SNAP frames with
1839 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1841 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1845 * Now we generate code to check for 802.3
1846 * frames in general.
1848 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1852 * Now add the check for 802.3 frames before the
1853 * check for Ethernet_802.2 and Ethernet_802.3,
1854 * as those checks should only be done on 802.3
1855 * frames, not on Ethernet frames.
1860 * Now add the check for Ethernet_II frames, and
1861 * do that before checking for the other frame
1864 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1865 (bpf_int32
)ETHERTYPE_IPX
);
1869 case ETHERTYPE_ATALK
:
1870 case ETHERTYPE_AARP
:
1872 * EtherTalk (AppleTalk protocols on Ethernet link
1873 * layer) may use 802.2 encapsulation.
1877 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1878 * we check for an Ethernet type field less than
1879 * 1500, which means it's an 802.3 length field.
1881 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1885 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1886 * SNAP packets with an organization code of
1887 * 0x080007 (Apple, for Appletalk) and a protocol
1888 * type of ETHERTYPE_ATALK (Appletalk).
1890 * 802.2-encapsulated ETHERTYPE_AARP packets are
1891 * SNAP packets with an organization code of
1892 * 0x000000 (encapsulated Ethernet) and a protocol
1893 * type of ETHERTYPE_AARP (Appletalk ARP).
1895 if (proto
== ETHERTYPE_ATALK
)
1896 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1897 else /* proto == ETHERTYPE_AARP */
1898 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1902 * Check for Ethernet encapsulation (Ethertalk
1903 * phase 1?); we just check for the Ethernet
1906 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1912 if (proto
<= ETHERMTU
) {
1914 * This is an LLC SAP value, so the frames
1915 * that match would be 802.2 frames.
1916 * Check that the frame is an 802.2 frame
1917 * (i.e., that the length/type field is
1918 * a length field, <= ETHERMTU) and
1919 * then check the DSAP.
1921 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1923 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1929 * This is an Ethernet type, so compare
1930 * the length/type field with it (if
1931 * the frame is an 802.2 frame, the length
1932 * field will be <= ETHERMTU, and, as
1933 * "proto" is > ETHERMTU, this test
1934 * will fail and the frame won't match,
1935 * which is what we want).
1937 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1944 * Generate code to match a particular packet type.
1946 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1947 * value, if <= ETHERMTU. We use that to determine whether to
1948 * match the type field or to check the type field for the special
1949 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1951 static struct block
*
1952 gen_linux_sll_linktype(proto
)
1955 struct block
*b0
, *b1
;
1961 case LLCSAP_NETBEUI
:
1963 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1964 * so we check the DSAP and SSAP.
1966 * LLCSAP_IP checks for IP-over-802.2, rather
1967 * than IP-over-Ethernet or IP-over-SNAP.
1969 * XXX - should we check both the DSAP and the
1970 * SSAP, like this, or should we check just the
1971 * DSAP, as we do for other types <= ETHERMTU
1972 * (i.e., other SAP values)?
1974 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1975 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1976 ((proto
<< 8) | proto
));
1982 * Ethernet_II frames, which are Ethernet
1983 * frames with a frame type of ETHERTYPE_IPX;
1985 * Ethernet_802.3 frames, which have a frame
1986 * type of LINUX_SLL_P_802_3;
1988 * Ethernet_802.2 frames, which are 802.3
1989 * frames with an 802.2 LLC header (i.e, have
1990 * a frame type of LINUX_SLL_P_802_2) and
1991 * with the IPX LSAP as the DSAP in the LLC
1994 * Ethernet_SNAP frames, which are 802.3
1995 * frames with an LLC header and a SNAP
1996 * header and with an OUI of 0x000000
1997 * (encapsulated Ethernet) and a protocol
1998 * ID of ETHERTYPE_IPX in the SNAP header.
2000 * First, do the checks on LINUX_SLL_P_802_2
2001 * frames; generate the check for either
2002 * Ethernet_802.2 or Ethernet_SNAP frames, and
2003 * then put a check for LINUX_SLL_P_802_2 frames
2006 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2007 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2009 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2013 * Now check for 802.3 frames and OR that with
2014 * the previous test.
2016 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2020 * Now add the check for Ethernet_II frames, and
2021 * do that before checking for the other frame
2024 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2025 (bpf_int32
)ETHERTYPE_IPX
);
2029 case ETHERTYPE_ATALK
:
2030 case ETHERTYPE_AARP
:
2032 * EtherTalk (AppleTalk protocols on Ethernet link
2033 * layer) may use 802.2 encapsulation.
2037 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2038 * we check for the 802.2 protocol type in the
2039 * "Ethernet type" field.
2041 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2044 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2045 * SNAP packets with an organization code of
2046 * 0x080007 (Apple, for Appletalk) and a protocol
2047 * type of ETHERTYPE_ATALK (Appletalk).
2049 * 802.2-encapsulated ETHERTYPE_AARP packets are
2050 * SNAP packets with an organization code of
2051 * 0x000000 (encapsulated Ethernet) and a protocol
2052 * type of ETHERTYPE_AARP (Appletalk ARP).
2054 if (proto
== ETHERTYPE_ATALK
)
2055 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2056 else /* proto == ETHERTYPE_AARP */
2057 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2061 * Check for Ethernet encapsulation (Ethertalk
2062 * phase 1?); we just check for the Ethernet
2065 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2071 if (proto
<= ETHERMTU
) {
2073 * This is an LLC SAP value, so the frames
2074 * that match would be 802.2 frames.
2075 * Check for the 802.2 protocol type
2076 * in the "Ethernet type" field, and
2077 * then check the DSAP.
2079 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2081 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2087 * This is an Ethernet type, so compare
2088 * the length/type field with it (if
2089 * the frame is an 802.2 frame, the length
2090 * field will be <= ETHERMTU, and, as
2091 * "proto" is > ETHERMTU, this test
2092 * will fail and the frame won't match,
2093 * which is what we want).
2095 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2101 static struct slist
*
2102 gen_load_prism_llprefixlen()
2104 struct slist
*s1
, *s2
;
2105 struct slist
*sjeq_avs_cookie
;
2106 struct slist
*sjcommon
;
2109 * This code is not compatible with the optimizer, as
2110 * we are generating jmp instructions within a normal
2111 * slist of instructions
2116 * Generate code to load the length of the radio header into
2117 * the register assigned to hold that length, if one has been
2118 * assigned. (If one hasn't been assigned, no code we've
2119 * generated uses that prefix, so we don't need to generate any
2122 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2123 * or always use the AVS header rather than the Prism header.
2124 * We load a 4-byte big-endian value at the beginning of the
2125 * raw packet data, and see whether, when masked with 0xFFFFF000,
2126 * it's equal to 0x80211000. If so, that indicates that it's
2127 * an AVS header (the masked-out bits are the version number).
2128 * Otherwise, it's a Prism header.
2130 * XXX - the Prism header is also, in theory, variable-length,
2131 * but no known software generates headers that aren't 144
2134 if (reg_off_ll
!= -1) {
2138 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2142 * AND it with 0xFFFFF000.
2144 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2145 s2
->s
.k
= 0xFFFFF000;
2149 * Compare with 0x80211000.
2151 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2152 sjeq_avs_cookie
->s
.k
= 0x80211000;
2153 sappend(s1
, sjeq_avs_cookie
);
2158 * The 4 bytes at an offset of 4 from the beginning of
2159 * the AVS header are the length of the AVS header.
2160 * That field is big-endian.
2162 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2165 sjeq_avs_cookie
->s
.jt
= s2
;
2168 * Now jump to the code to allocate a register
2169 * into which to save the header length and
2170 * store the length there. (The "jump always"
2171 * instruction needs to have the k field set;
2172 * it's added to the PC, so, as we're jumping
2173 * over a single instruction, it should be 1.)
2175 sjcommon
= new_stmt(JMP(BPF_JA
));
2177 sappend(s1
, sjcommon
);
2180 * Now for the code that handles the Prism header.
2181 * Just load the length of the Prism header (144)
2182 * into the A register. Have the test for an AVS
2183 * header branch here if we don't have an AVS header.
2185 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2188 sjeq_avs_cookie
->s
.jf
= s2
;
2191 * Now allocate a register to hold that value and store
2192 * it. The code for the AVS header will jump here after
2193 * loading the length of the AVS header.
2195 s2
= new_stmt(BPF_ST
);
2196 s2
->s
.k
= reg_off_ll
;
2198 sjcommon
->s
.jf
= s2
;
2201 * Now move it into the X register.
2203 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2211 static struct slist
*
2212 gen_load_avs_llprefixlen()
2214 struct slist
*s1
, *s2
;
2217 * Generate code to load the length of the AVS header into
2218 * the register assigned to hold that length, if one has been
2219 * assigned. (If one hasn't been assigned, no code we've
2220 * generated uses that prefix, so we don't need to generate any
2223 if (reg_off_ll
!= -1) {
2225 * The 4 bytes at an offset of 4 from the beginning of
2226 * the AVS header are the length of the AVS header.
2227 * That field is big-endian.
2229 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2233 * Now allocate a register to hold that value and store
2236 s2
= new_stmt(BPF_ST
);
2237 s2
->s
.k
= reg_off_ll
;
2241 * Now move it into the X register.
2243 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2251 static struct slist
*
2252 gen_load_radiotap_llprefixlen()
2254 struct slist
*s1
, *s2
;
2257 * Generate code to load the length of the radiotap header into
2258 * the register assigned to hold that length, if one has been
2259 * assigned. (If one hasn't been assigned, no code we've
2260 * generated uses that prefix, so we don't need to generate any
2263 if (reg_off_ll
!= -1) {
2265 * The 2 bytes at offsets of 2 and 3 from the beginning
2266 * of the radiotap header are the length of the radiotap
2267 * header; unfortunately, it's little-endian, so we have
2268 * to load it a byte at a time and construct the value.
2272 * Load the high-order byte, at an offset of 3, shift it
2273 * left a byte, and put the result in the X register.
2275 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2277 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2280 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2284 * Load the next byte, at an offset of 2, and OR the
2285 * value from the X register into it.
2287 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2290 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2294 * Now allocate a register to hold that value and store
2297 s2
= new_stmt(BPF_ST
);
2298 s2
->s
.k
= reg_off_ll
;
2302 * Now move it into the X register.
2304 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2313 * At the moment we treat PPI as normal Radiotap encoded
2314 * packets. The difference is in the function that generates
2315 * the code at the beginning to compute the header length.
2316 * Since this code generator of PPI supports bare 802.11
2317 * encapsulation only (i.e. the encapsulated DLT should be
2318 * DLT_IEEE802_11) we generate code to check for this too;
2319 * that's done in finish_parse().
2321 static struct slist
*
2322 gen_load_ppi_llprefixlen()
2324 struct slist
*s1
, *s2
;
2327 * Generate code to load the length of the radiotap header
2328 * into the register assigned to hold that length, if one has
2331 if (reg_off_ll
!= -1) {
2333 * The 2 bytes at offsets of 2 and 3 from the beginning
2334 * of the radiotap header are the length of the radiotap
2335 * header; unfortunately, it's little-endian, so we have
2336 * to load it a byte at a time and construct the value.
2340 * Load the high-order byte, at an offset of 3, shift it
2341 * left a byte, and put the result in the X register.
2343 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2345 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2348 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2352 * Load the next byte, at an offset of 2, and OR the
2353 * value from the X register into it.
2355 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2358 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2362 * Now allocate a register to hold that value and store
2365 s2
= new_stmt(BPF_ST
);
2366 s2
->s
.k
= reg_off_ll
;
2370 * Now move it into the X register.
2372 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2381 * Load a value relative to the beginning of the link-layer header after the 802.11
2382 * header, i.e. LLC_SNAP.
2383 * The link-layer header doesn't necessarily begin at the beginning
2384 * of the packet data; there might be a variable-length prefix containing
2385 * radio information.
2387 static struct slist
*
2388 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2391 struct slist
*sjset_data_frame_1
;
2392 struct slist
*sjset_data_frame_2
;
2393 struct slist
*sjset_qos
;
2394 struct slist
*sjset_radiotap_flags
;
2395 struct slist
*sjset_radiotap_tsft
;
2396 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2397 struct slist
*s_roundup
;
2399 if (reg_off_macpl
== -1) {
2401 * No register has been assigned to the offset of
2402 * the MAC-layer payload, which means nobody needs
2403 * it; don't bother computing it - just return
2404 * what we already have.
2410 * This code is not compatible with the optimizer, as
2411 * we are generating jmp instructions within a normal
2412 * slist of instructions
2417 * If "s" is non-null, it has code to arrange that the X register
2418 * contains the length of the prefix preceding the link-layer
2421 * Otherwise, the length of the prefix preceding the link-layer
2422 * header is "off_ll".
2426 * There is no variable-length header preceding the
2427 * link-layer header.
2429 * Load the length of the fixed-length prefix preceding
2430 * the link-layer header (if any) into the X register,
2431 * and store it in the reg_off_macpl register.
2432 * That length is off_ll.
2434 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2439 * The X register contains the offset of the beginning of the
2440 * link-layer header; add 24, which is the minimum length
2441 * of the MAC header for a data frame, to that, and store it
2442 * in reg_off_macpl, and then load the Frame Control field,
2443 * which is at the offset in the X register, with an indexed load.
2445 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2447 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2450 s2
= new_stmt(BPF_ST
);
2451 s2
->s
.k
= reg_off_macpl
;
2454 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2459 * Check the Frame Control field to see if this is a data frame;
2460 * a data frame has the 0x08 bit (b3) in that field set and the
2461 * 0x04 bit (b2) clear.
2463 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2464 sjset_data_frame_1
->s
.k
= 0x08;
2465 sappend(s
, sjset_data_frame_1
);
2468 * If b3 is set, test b2, otherwise go to the first statement of
2469 * the rest of the program.
2471 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2472 sjset_data_frame_2
->s
.k
= 0x04;
2473 sappend(s
, sjset_data_frame_2
);
2474 sjset_data_frame_1
->s
.jf
= snext
;
2477 * If b2 is not set, this is a data frame; test the QoS bit.
2478 * Otherwise, go to the first statement of the rest of the
2481 sjset_data_frame_2
->s
.jt
= snext
;
2482 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2483 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2484 sappend(s
, sjset_qos
);
2487 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2489 * Otherwise, go to the first statement of the rest of the
2492 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2493 s2
->s
.k
= reg_off_macpl
;
2495 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2498 s2
= new_stmt(BPF_ST
);
2499 s2
->s
.k
= reg_off_macpl
;
2503 * If we have a radiotap header, look at it to see whether
2504 * there's Atheros padding between the MAC-layer header
2507 * Note: all of the fields in the radiotap header are
2508 * little-endian, so we byte-swap all of the values
2509 * we test against, as they will be loaded as big-endian
2512 if (linktype
== DLT_IEEE802_11_RADIO
) {
2514 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2515 * in the presence flag?
2517 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2521 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2522 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2523 sappend(s
, sjset_radiotap_flags
);
2526 * If not, skip all of this.
2528 sjset_radiotap_flags
->s
.jf
= snext
;
2531 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2533 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2534 new_stmt(JMP(BPF_JSET
));
2535 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2536 sappend(s
, sjset_radiotap_tsft
);
2539 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2540 * at an offset of 16 from the beginning of the raw packet
2541 * data (8 bytes for the radiotap header and 8 bytes for
2544 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2547 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2551 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2552 sjset_tsft_datapad
->s
.k
= 0x20;
2553 sappend(s
, sjset_tsft_datapad
);
2556 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2557 * at an offset of 8 from the beginning of the raw packet
2558 * data (8 bytes for the radiotap header).
2560 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2563 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2567 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2568 sjset_notsft_datapad
->s
.k
= 0x20;
2569 sappend(s
, sjset_notsft_datapad
);
2572 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2573 * set, round the length of the 802.11 header to
2574 * a multiple of 4. Do that by adding 3 and then
2575 * dividing by and multiplying by 4, which we do by
2578 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2579 s_roundup
->s
.k
= reg_off_macpl
;
2580 sappend(s
, s_roundup
);
2581 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2584 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2587 s2
= new_stmt(BPF_ST
);
2588 s2
->s
.k
= reg_off_macpl
;
2591 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2592 sjset_tsft_datapad
->s
.jf
= snext
;
2593 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2594 sjset_notsft_datapad
->s
.jf
= snext
;
2596 sjset_qos
->s
.jf
= snext
;
2602 insert_compute_vloffsets(b
)
2608 * For link-layer types that have a variable-length header
2609 * preceding the link-layer header, generate code to load
2610 * the offset of the link-layer header into the register
2611 * assigned to that offset, if any.
2615 case DLT_PRISM_HEADER
:
2616 s
= gen_load_prism_llprefixlen();
2619 case DLT_IEEE802_11_RADIO_AVS
:
2620 s
= gen_load_avs_llprefixlen();
2623 case DLT_IEEE802_11_RADIO
:
2624 s
= gen_load_radiotap_llprefixlen();
2628 s
= gen_load_ppi_llprefixlen();
2637 * For link-layer types that have a variable-length link-layer
2638 * header, generate code to load the offset of the MAC-layer
2639 * payload into the register assigned to that offset, if any.
2643 case DLT_IEEE802_11
:
2644 case DLT_PRISM_HEADER
:
2645 case DLT_IEEE802_11_RADIO_AVS
:
2646 case DLT_IEEE802_11_RADIO
:
2648 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2653 * If we have any offset-loading code, append all the
2654 * existing statements in the block to those statements,
2655 * and make the resulting list the list of statements
2659 sappend(s
, b
->stmts
);
2664 static struct block
*
2665 gen_ppi_dlt_check(void)
2667 struct slist
*s_load_dlt
;
2670 if (linktype
== DLT_PPI
)
2672 /* Create the statements that check for the DLT
2674 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2675 s_load_dlt
->s
.k
= 4;
2677 b
= new_block(JMP(BPF_JEQ
));
2679 b
->stmts
= s_load_dlt
;
2680 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2690 static struct slist
*
2691 gen_prism_llprefixlen(void)
2695 if (reg_off_ll
== -1) {
2697 * We haven't yet assigned a register for the length
2698 * of the radio header; allocate one.
2700 reg_off_ll
= alloc_reg();
2704 * Load the register containing the radio length
2705 * into the X register.
2707 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2708 s
->s
.k
= reg_off_ll
;
2712 static struct slist
*
2713 gen_avs_llprefixlen(void)
2717 if (reg_off_ll
== -1) {
2719 * We haven't yet assigned a register for the length
2720 * of the AVS header; allocate one.
2722 reg_off_ll
= alloc_reg();
2726 * Load the register containing the AVS length
2727 * into the X register.
2729 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2730 s
->s
.k
= reg_off_ll
;
2734 static struct slist
*
2735 gen_radiotap_llprefixlen(void)
2739 if (reg_off_ll
== -1) {
2741 * We haven't yet assigned a register for the length
2742 * of the radiotap header; allocate one.
2744 reg_off_ll
= alloc_reg();
2748 * Load the register containing the radiotap length
2749 * into the X register.
2751 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2752 s
->s
.k
= reg_off_ll
;
2757 * At the moment we treat PPI as normal Radiotap encoded
2758 * packets. The difference is in the function that generates
2759 * the code at the beginning to compute the header length.
2760 * Since this code generator of PPI supports bare 802.11
2761 * encapsulation only (i.e. the encapsulated DLT should be
2762 * DLT_IEEE802_11) we generate code to check for this too.
2764 static struct slist
*
2765 gen_ppi_llprefixlen(void)
2769 if (reg_off_ll
== -1) {
2771 * We haven't yet assigned a register for the length
2772 * of the radiotap header; allocate one.
2774 reg_off_ll
= alloc_reg();
2778 * Load the register containing the PPI length
2779 * into the X register.
2781 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2782 s
->s
.k
= reg_off_ll
;
2787 * Generate code to compute the link-layer header length, if necessary,
2788 * putting it into the X register, and to return either a pointer to a
2789 * "struct slist" for the list of statements in that code, or NULL if
2790 * no code is necessary.
2792 static struct slist
*
2793 gen_llprefixlen(void)
2797 case DLT_PRISM_HEADER
:
2798 return gen_prism_llprefixlen();
2800 case DLT_IEEE802_11_RADIO_AVS
:
2801 return gen_avs_llprefixlen();
2803 case DLT_IEEE802_11_RADIO
:
2804 return gen_radiotap_llprefixlen();
2807 return gen_ppi_llprefixlen();
2815 * Generate code to load the register containing the offset of the
2816 * MAC-layer payload into the X register; if no register for that offset
2817 * has been allocated, allocate it first.
2819 static struct slist
*
2824 if (off_macpl_is_variable
) {
2825 if (reg_off_macpl
== -1) {
2827 * We haven't yet assigned a register for the offset
2828 * of the MAC-layer payload; allocate one.
2830 reg_off_macpl
= alloc_reg();
2834 * Load the register containing the offset of the MAC-layer
2835 * payload into the X register.
2837 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2838 s
->s
.k
= reg_off_macpl
;
2842 * That offset isn't variable, so we don't need to
2843 * generate any code.
2850 * Map an Ethernet type to the equivalent PPP type.
2853 ethertype_to_ppptype(proto
)
2863 case ETHERTYPE_IPV6
:
2872 case ETHERTYPE_ATALK
:
2886 * I'm assuming the "Bridging PDU"s that go
2887 * over PPP are Spanning Tree Protocol
2901 * Generate code to match a particular packet type by matching the
2902 * link-layer type field or fields in the 802.2 LLC header.
2904 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2905 * value, if <= ETHERMTU.
2907 static struct block
*
2911 struct block
*b0
, *b1
, *b2
;
2913 /* are we checking MPLS-encapsulated packets? */
2914 if (label_stack_depth
> 0) {
2918 /* FIXME add other L3 proto IDs */
2919 return gen_mpls_linktype(Q_IP
);
2921 case ETHERTYPE_IPV6
:
2923 /* FIXME add other L3 proto IDs */
2924 return gen_mpls_linktype(Q_IPV6
);
2927 bpf_error("unsupported protocol over mpls");
2933 * Are we testing PPPoE packets?
2937 * The PPPoE session header is part of the
2938 * MAC-layer payload, so all references
2939 * should be relative to the beginning of
2944 * We use Ethernet protocol types inside libpcap;
2945 * map them to the corresponding PPP protocol types.
2947 proto
= ethertype_to_ppptype(proto
);
2948 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2954 return gen_ether_linktype(proto
);
2962 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2966 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2973 case DLT_IEEE802_11
:
2974 case DLT_PRISM_HEADER
:
2975 case DLT_IEEE802_11_RADIO_AVS
:
2976 case DLT_IEEE802_11_RADIO
:
2979 * Check that we have a data frame.
2981 b0
= gen_check_802_11_data_frame();
2984 * Now check for the specified link-layer type.
2986 b1
= gen_llc_linktype(proto
);
2994 * XXX - check for asynchronous frames, as per RFC 1103.
2996 return gen_llc_linktype(proto
);
3002 * XXX - check for LLC PDUs, as per IEEE 802.5.
3004 return gen_llc_linktype(proto
);
3008 case DLT_ATM_RFC1483
:
3010 case DLT_IP_OVER_FC
:
3011 return gen_llc_linktype(proto
);
3017 * If "is_lane" is set, check for a LANE-encapsulated
3018 * version of this protocol, otherwise check for an
3019 * LLC-encapsulated version of this protocol.
3021 * We assume LANE means Ethernet, not Token Ring.
3025 * Check that the packet doesn't begin with an
3026 * LE Control marker. (We've already generated
3029 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3034 * Now generate an Ethernet test.
3036 b1
= gen_ether_linktype(proto
);
3041 * Check for LLC encapsulation and then check the
3044 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3045 b1
= gen_llc_linktype(proto
);
3053 return gen_linux_sll_linktype(proto
);
3058 case DLT_SLIP_BSDOS
:
3061 * These types don't provide any type field; packets
3062 * are always IPv4 or IPv6.
3064 * XXX - for IPv4, check for a version number of 4, and,
3065 * for IPv6, check for a version number of 6?
3070 /* Check for a version number of 4. */
3071 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3073 case ETHERTYPE_IPV6
:
3074 /* Check for a version number of 6. */
3075 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3079 return gen_false(); /* always false */
3086 case DLT_PPP_SERIAL
:
3089 * We use Ethernet protocol types inside libpcap;
3090 * map them to the corresponding PPP protocol types.
3092 proto
= ethertype_to_ppptype(proto
);
3093 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3099 * We use Ethernet protocol types inside libpcap;
3100 * map them to the corresponding PPP protocol types.
3106 * Also check for Van Jacobson-compressed IP.
3107 * XXX - do this for other forms of PPP?
3109 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3110 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3112 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3117 proto
= ethertype_to_ppptype(proto
);
3118 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3128 * For DLT_NULL, the link-layer header is a 32-bit
3129 * word containing an AF_ value in *host* byte order,
3130 * and for DLT_ENC, the link-layer header begins
3131 * with a 32-bit work containing an AF_ value in
3134 * In addition, if we're reading a saved capture file,
3135 * the host byte order in the capture may not be the
3136 * same as the host byte order on this machine.
3138 * For DLT_LOOP, the link-layer header is a 32-bit
3139 * word containing an AF_ value in *network* byte order.
3141 * XXX - AF_ values may, unfortunately, be platform-
3142 * dependent; for example, FreeBSD's AF_INET6 is 24
3143 * whilst NetBSD's and OpenBSD's is 26.
3145 * This means that, when reading a capture file, just
3146 * checking for our AF_INET6 value won't work if the
3147 * capture file came from another OS.
3156 case ETHERTYPE_IPV6
:
3163 * Not a type on which we support filtering.
3164 * XXX - support those that have AF_ values
3165 * #defined on this platform, at least?
3170 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3172 * The AF_ value is in host byte order, but
3173 * the BPF interpreter will convert it to
3174 * network byte order.
3176 * If this is a save file, and it's from a
3177 * machine with the opposite byte order to
3178 * ours, we byte-swap the AF_ value.
3180 * Then we run it through "htonl()", and
3181 * generate code to compare against the result.
3183 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3184 bpf_pcap
->sf
.swapped
)
3185 proto
= SWAPLONG(proto
);
3186 proto
= htonl(proto
);
3188 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3190 #ifdef HAVE_NET_PFVAR_H
3193 * af field is host byte order in contrast to the rest of
3196 if (proto
== ETHERTYPE_IP
)
3197 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3198 BPF_B
, (bpf_int32
)AF_INET
));
3200 else if (proto
== ETHERTYPE_IPV6
)
3201 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3202 BPF_B
, (bpf_int32
)AF_INET6
));
3208 #endif /* HAVE_NET_PFVAR_H */
3211 case DLT_ARCNET_LINUX
:
3213 * XXX should we check for first fragment if the protocol
3222 case ETHERTYPE_IPV6
:
3223 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3224 (bpf_int32
)ARCTYPE_INET6
));
3228 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3229 (bpf_int32
)ARCTYPE_IP
);
3230 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3231 (bpf_int32
)ARCTYPE_IP_OLD
);
3236 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3237 (bpf_int32
)ARCTYPE_ARP
);
3238 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3239 (bpf_int32
)ARCTYPE_ARP_OLD
);
3243 case ETHERTYPE_REVARP
:
3244 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3245 (bpf_int32
)ARCTYPE_REVARP
));
3247 case ETHERTYPE_ATALK
:
3248 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3249 (bpf_int32
)ARCTYPE_ATALK
));
3256 case ETHERTYPE_ATALK
:
3266 * XXX - assumes a 2-byte Frame Relay header with
3267 * DLCI and flags. What if the address is longer?
3273 * Check for the special NLPID for IP.
3275 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3278 case ETHERTYPE_IPV6
:
3280 * Check for the special NLPID for IPv6.
3282 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3287 * Check for several OSI protocols.
3289 * Frame Relay packets typically have an OSI
3290 * NLPID at the beginning; we check for each
3293 * What we check for is the NLPID and a frame
3294 * control field of UI, i.e. 0x03 followed
3297 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3298 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3299 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3311 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3313 case DLT_JUNIPER_MFR
:
3314 case DLT_JUNIPER_MLFR
:
3315 case DLT_JUNIPER_MLPPP
:
3316 case DLT_JUNIPER_ATM1
:
3317 case DLT_JUNIPER_ATM2
:
3318 case DLT_JUNIPER_PPPOE
:
3319 case DLT_JUNIPER_PPPOE_ATM
:
3320 case DLT_JUNIPER_GGSN
:
3321 case DLT_JUNIPER_ES
:
3322 case DLT_JUNIPER_MONITOR
:
3323 case DLT_JUNIPER_SERVICES
:
3324 case DLT_JUNIPER_ETHER
:
3325 case DLT_JUNIPER_PPP
:
3326 case DLT_JUNIPER_FRELAY
:
3327 case DLT_JUNIPER_CHDLC
:
3328 case DLT_JUNIPER_VP
:
3329 case DLT_JUNIPER_ST
:
3330 case DLT_JUNIPER_ISM
:
3331 /* just lets verify the magic number for now -
3332 * on ATM we may have up to 6 different encapsulations on the wire
3333 * and need a lot of heuristics to figure out that the payload
3336 * FIXME encapsulation specific BPF_ filters
3338 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3340 case DLT_LINUX_IRDA
:
3341 bpf_error("IrDA link-layer type filtering not implemented");
3344 bpf_error("DOCSIS link-layer type filtering not implemented");
3347 case DLT_MTP2_WITH_PHDR
:
3348 bpf_error("MTP2 link-layer type filtering not implemented");
3351 bpf_error("ERF link-layer type filtering not implemented");
3355 bpf_error("PFSYNC link-layer type filtering not implemented");
3358 case DLT_LINUX_LAPD
:
3359 bpf_error("LAPD link-layer type filtering not implemented");
3363 bpf_error("USB link-layer type filtering not implemented");
3365 case DLT_BLUETOOTH_HCI_H4
:
3366 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3367 bpf_error("Bluetooth link-layer type filtering not implemented");
3370 bpf_error("CAN20B link-layer type filtering not implemented");
3372 case DLT_IEEE802_15_4
:
3373 case DLT_IEEE802_15_4_LINUX
:
3374 case DLT_IEEE802_15_4_NONASK_PHY
:
3375 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3377 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3378 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3381 bpf_error("SITA link-layer type filtering not implemented");
3384 bpf_error("RAIF1 link-layer type filtering not implemented");
3387 bpf_error("IPMB link-layer type filtering not implemented");
3390 bpf_error("AX.25 link-layer type filtering not implemented");
3394 * All the types that have no encapsulation should either be
3395 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3396 * all packets are IP packets, or should be handled in some
3397 * special case, if none of them are (if some are and some
3398 * aren't, the lack of encapsulation is a problem, as we'd
3399 * have to find some other way of determining the packet type).
3401 * Therefore, if "off_linktype" is -1, there's an error.
3403 if (off_linktype
== (u_int
)-1)
3407 * Any type not handled above should always have an Ethernet
3408 * type at an offset of "off_linktype".
3410 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3414 * Check for an LLC SNAP packet with a given organization code and
3415 * protocol type; we check the entire contents of the 802.2 LLC and
3416 * snap headers, checking for DSAP and SSAP of SNAP and a control
3417 * field of 0x03 in the LLC header, and for the specified organization
3418 * code and protocol type in the SNAP header.
3420 static struct block
*
3421 gen_snap(orgcode
, ptype
)
3422 bpf_u_int32 orgcode
;
3425 u_char snapblock
[8];
3427 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3428 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3429 snapblock
[2] = 0x03; /* control = UI */
3430 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3431 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3432 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3433 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3434 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3435 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3439 * Generate code to match a particular packet type, for link-layer types
3440 * using 802.2 LLC headers.
3442 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3443 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3445 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3446 * value, if <= ETHERMTU. We use that to determine whether to
3447 * match the DSAP or both DSAP and LSAP or to check the OUI and
3448 * protocol ID in a SNAP header.
3450 static struct block
*
3451 gen_llc_linktype(proto
)
3455 * XXX - handle token-ring variable-length header.
3461 case LLCSAP_NETBEUI
:
3463 * XXX - should we check both the DSAP and the
3464 * SSAP, like this, or should we check just the
3465 * DSAP, as we do for other types <= ETHERMTU
3466 * (i.e., other SAP values)?
3468 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3469 ((proto
<< 8) | proto
));
3473 * XXX - are there ever SNAP frames for IPX on
3474 * non-Ethernet 802.x networks?
3476 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3477 (bpf_int32
)LLCSAP_IPX
);
3479 case ETHERTYPE_ATALK
:
3481 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3482 * SNAP packets with an organization code of
3483 * 0x080007 (Apple, for Appletalk) and a protocol
3484 * type of ETHERTYPE_ATALK (Appletalk).
3486 * XXX - check for an organization code of
3487 * encapsulated Ethernet as well?
3489 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3493 * XXX - we don't have to check for IPX 802.3
3494 * here, but should we check for the IPX Ethertype?
3496 if (proto
<= ETHERMTU
) {
3498 * This is an LLC SAP value, so check
3501 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3504 * This is an Ethernet type; we assume that it's
3505 * unlikely that it'll appear in the right place
3506 * at random, and therefore check only the
3507 * location that would hold the Ethernet type
3508 * in a SNAP frame with an organization code of
3509 * 0x000000 (encapsulated Ethernet).
3511 * XXX - if we were to check for the SNAP DSAP and
3512 * LSAP, as per XXX, and were also to check for an
3513 * organization code of 0x000000 (encapsulated
3514 * Ethernet), we'd do
3516 * return gen_snap(0x000000, proto);
3518 * here; for now, we don't, as per the above.
3519 * I don't know whether it's worth the extra CPU
3520 * time to do the right check or not.
3522 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3527 static struct block
*
3528 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3532 u_int src_off
, dst_off
;
3534 struct block
*b0
, *b1
;
3548 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3549 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3555 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3556 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3563 b0
= gen_linktype(proto
);
3564 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3570 static struct block
*
3571 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3572 struct in6_addr
*addr
;
3573 struct in6_addr
*mask
;
3575 u_int src_off
, dst_off
;
3577 struct block
*b0
, *b1
;
3592 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3593 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3599 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3600 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3607 /* this order is important */
3608 a
= (u_int32_t
*)addr
;
3609 m
= (u_int32_t
*)mask
;
3610 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3611 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3613 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3615 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3617 b0
= gen_linktype(proto
);
3623 static struct block
*
3624 gen_ehostop(eaddr
, dir
)
3625 register const u_char
*eaddr
;
3628 register struct block
*b0
, *b1
;
3632 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3635 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3638 b0
= gen_ehostop(eaddr
, Q_SRC
);
3639 b1
= gen_ehostop(eaddr
, Q_DST
);
3645 b0
= gen_ehostop(eaddr
, Q_SRC
);
3646 b1
= gen_ehostop(eaddr
, Q_DST
);
3655 * Like gen_ehostop, but for DLT_FDDI
3657 static struct block
*
3658 gen_fhostop(eaddr
, dir
)
3659 register const u_char
*eaddr
;
3662 struct block
*b0
, *b1
;
3667 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3669 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3674 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3676 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3680 b0
= gen_fhostop(eaddr
, Q_SRC
);
3681 b1
= gen_fhostop(eaddr
, Q_DST
);
3687 b0
= gen_fhostop(eaddr
, Q_SRC
);
3688 b1
= gen_fhostop(eaddr
, Q_DST
);
3697 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3699 static struct block
*
3700 gen_thostop(eaddr
, dir
)
3701 register const u_char
*eaddr
;
3704 register struct block
*b0
, *b1
;
3708 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3711 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3714 b0
= gen_thostop(eaddr
, Q_SRC
);
3715 b1
= gen_thostop(eaddr
, Q_DST
);
3721 b0
= gen_thostop(eaddr
, Q_SRC
);
3722 b1
= gen_thostop(eaddr
, Q_DST
);
3731 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3732 * various 802.11 + radio headers.
3734 static struct block
*
3735 gen_wlanhostop(eaddr
, dir
)
3736 register const u_char
*eaddr
;
3739 register struct block
*b0
, *b1
, *b2
;
3740 register struct slist
*s
;
3742 #ifdef ENABLE_WLAN_FILTERING_PATCH
3745 * We need to disable the optimizer because the optimizer is buggy
3746 * and wipes out some LD instructions generated by the below
3747 * code to validate the Frame Control bits
3750 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3757 * For control frames, there is no SA.
3759 * For management frames, SA is at an
3760 * offset of 10 from the beginning of
3763 * For data frames, SA is at an offset
3764 * of 10 from the beginning of the packet
3765 * if From DS is clear, at an offset of
3766 * 16 from the beginning of the packet
3767 * if From DS is set and To DS is clear,
3768 * and an offset of 24 from the beginning
3769 * of the packet if From DS is set and To DS
3774 * Generate the tests to be done for data frames
3777 * First, check for To DS set, i.e. check "link[1] & 0x01".
3779 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3780 b1
= new_block(JMP(BPF_JSET
));
3781 b1
->s
.k
= 0x01; /* To DS */
3785 * If To DS is set, the SA is at 24.
3787 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3791 * Now, check for To DS not set, i.e. check
3792 * "!(link[1] & 0x01)".
3794 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3795 b2
= new_block(JMP(BPF_JSET
));
3796 b2
->s
.k
= 0x01; /* To DS */
3801 * If To DS is not set, the SA is at 16.
3803 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3807 * Now OR together the last two checks. That gives
3808 * the complete set of checks for data frames with
3814 * Now check for From DS being set, and AND that with
3815 * the ORed-together checks.
3817 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3818 b1
= new_block(JMP(BPF_JSET
));
3819 b1
->s
.k
= 0x02; /* From DS */
3824 * Now check for data frames with From DS not set.
3826 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3827 b2
= new_block(JMP(BPF_JSET
));
3828 b2
->s
.k
= 0x02; /* From DS */
3833 * If From DS isn't set, the SA is at 10.
3835 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3839 * Now OR together the checks for data frames with
3840 * From DS not set and for data frames with From DS
3841 * set; that gives the checks done for data frames.
3846 * Now check for a data frame.
3847 * I.e, check "link[0] & 0x08".
3849 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3850 b1
= new_block(JMP(BPF_JSET
));
3855 * AND that with the checks done for data frames.
3860 * If the high-order bit of the type value is 0, this
3861 * is a management frame.
3862 * I.e, check "!(link[0] & 0x08)".
3864 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3865 b2
= new_block(JMP(BPF_JSET
));
3871 * For management frames, the SA is at 10.
3873 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3877 * OR that with the checks done for data frames.
3878 * That gives the checks done for management and
3884 * If the low-order bit of the type value is 1,
3885 * this is either a control frame or a frame
3886 * with a reserved type, and thus not a
3889 * I.e., check "!(link[0] & 0x04)".
3891 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3892 b1
= new_block(JMP(BPF_JSET
));
3898 * AND that with the checks for data and management
3908 * For control frames, there is no DA.
3910 * For management frames, DA is at an
3911 * offset of 4 from the beginning of
3914 * For data frames, DA is at an offset
3915 * of 4 from the beginning of the packet
3916 * if To DS is clear and at an offset of
3917 * 16 from the beginning of the packet
3922 * Generate the tests to be done for data frames.
3924 * First, check for To DS set, i.e. "link[1] & 0x01".
3926 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3927 b1
= new_block(JMP(BPF_JSET
));
3928 b1
->s
.k
= 0x01; /* To DS */
3932 * If To DS is set, the DA is at 16.
3934 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3938 * Now, check for To DS not set, i.e. check
3939 * "!(link[1] & 0x01)".
3941 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3942 b2
= new_block(JMP(BPF_JSET
));
3943 b2
->s
.k
= 0x01; /* To DS */
3948 * If To DS is not set, the DA is at 4.
3950 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3954 * Now OR together the last two checks. That gives
3955 * the complete set of checks for data frames.
3960 * Now check for a data frame.
3961 * I.e, check "link[0] & 0x08".
3963 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3964 b1
= new_block(JMP(BPF_JSET
));
3969 * AND that with the checks done for data frames.
3974 * If the high-order bit of the type value is 0, this
3975 * is a management frame.
3976 * I.e, check "!(link[0] & 0x08)".
3978 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3979 b2
= new_block(JMP(BPF_JSET
));
3985 * For management frames, the DA is at 4.
3987 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3991 * OR that with the checks done for data frames.
3992 * That gives the checks done for management and
3998 * If the low-order bit of the type value is 1,
3999 * this is either a control frame or a frame
4000 * with a reserved type, and thus not a
4003 * I.e., check "!(link[0] & 0x04)".
4005 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4006 b1
= new_block(JMP(BPF_JSET
));
4012 * AND that with the checks for data and management
4019 * XXX - add RA, TA, and BSSID keywords?
4022 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4026 * Not present in CTS or ACK control frames.
4028 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4029 IEEE80211_FC0_TYPE_MASK
);
4031 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4032 IEEE80211_FC0_SUBTYPE_MASK
);
4034 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4035 IEEE80211_FC0_SUBTYPE_MASK
);
4039 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4045 * Not present in control frames.
4047 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4048 IEEE80211_FC0_TYPE_MASK
);
4050 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4056 * Present only if the direction mask has both "From DS"
4057 * and "To DS" set. Neither control frames nor management
4058 * frames should have both of those set, so we don't
4059 * check the frame type.
4061 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4062 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4063 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4068 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4069 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4075 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4076 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4085 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4086 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4087 * as the RFC states.)
4089 static struct block
*
4090 gen_ipfchostop(eaddr
, dir
)
4091 register const u_char
*eaddr
;
4094 register struct block
*b0
, *b1
;
4098 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4101 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4104 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4105 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4111 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4112 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4121 * This is quite tricky because there may be pad bytes in front of the
4122 * DECNET header, and then there are two possible data packet formats that
4123 * carry both src and dst addresses, plus 5 packet types in a format that
4124 * carries only the src node, plus 2 types that use a different format and
4125 * also carry just the src node.
4129 * Instead of doing those all right, we just look for data packets with
4130 * 0 or 1 bytes of padding. If you want to look at other packets, that
4131 * will require a lot more hacking.
4133 * To add support for filtering on DECNET "areas" (network numbers)
4134 * one would want to add a "mask" argument to this routine. That would
4135 * make the filter even more inefficient, although one could be clever
4136 * and not generate masking instructions if the mask is 0xFFFF.
4138 static struct block
*
4139 gen_dnhostop(addr
, dir
)
4143 struct block
*b0
, *b1
, *b2
, *tmp
;
4144 u_int offset_lh
; /* offset if long header is received */
4145 u_int offset_sh
; /* offset if short header is received */
4150 offset_sh
= 1; /* follows flags */
4151 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4155 offset_sh
= 3; /* follows flags, dstnode */
4156 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4160 /* Inefficient because we do our Calvinball dance twice */
4161 b0
= gen_dnhostop(addr
, Q_SRC
);
4162 b1
= gen_dnhostop(addr
, Q_DST
);
4168 /* Inefficient because we do our Calvinball dance twice */
4169 b0
= gen_dnhostop(addr
, Q_SRC
);
4170 b1
= gen_dnhostop(addr
, Q_DST
);
4175 bpf_error("ISO host filtering not implemented");
4180 b0
= gen_linktype(ETHERTYPE_DN
);
4181 /* Check for pad = 1, long header case */
4182 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4183 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4184 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4185 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4187 /* Check for pad = 0, long header case */
4188 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4189 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4192 /* Check for pad = 1, short header case */
4193 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4194 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4195 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4198 /* Check for pad = 0, short header case */
4199 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4200 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4204 /* Combine with test for linktype */
4210 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4211 * test the bottom-of-stack bit, and then check the version number
4212 * field in the IP header.
4214 static struct block
*
4215 gen_mpls_linktype(proto
)
4218 struct block
*b0
, *b1
;
4223 /* match the bottom-of-stack bit */
4224 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4225 /* match the IPv4 version number */
4226 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4231 /* match the bottom-of-stack bit */
4232 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4233 /* match the IPv4 version number */
4234 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4243 static struct block
*
4244 gen_host(addr
, mask
, proto
, dir
, type
)
4251 struct block
*b0
, *b1
;
4252 const char *typestr
;
4262 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4264 * Only check for non-IPv4 addresses if we're not
4265 * checking MPLS-encapsulated packets.
4267 if (label_stack_depth
== 0) {
4268 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4270 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4276 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4279 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4282 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4285 bpf_error("'tcp' modifier applied to %s", typestr
);
4288 bpf_error("'sctp' modifier applied to %s", typestr
);
4291 bpf_error("'udp' modifier applied to %s", typestr
);
4294 bpf_error("'icmp' modifier applied to %s", typestr
);
4297 bpf_error("'igmp' modifier applied to %s", typestr
);
4300 bpf_error("'igrp' modifier applied to %s", typestr
);
4303 bpf_error("'pim' modifier applied to %s", typestr
);
4306 bpf_error("'vrrp' modifier applied to %s", typestr
);
4309 bpf_error("ATALK host filtering not implemented");
4312 bpf_error("AARP host filtering not implemented");
4315 return gen_dnhostop(addr
, dir
);
4318 bpf_error("SCA host filtering not implemented");
4321 bpf_error("LAT host filtering not implemented");
4324 bpf_error("MOPDL host filtering not implemented");
4327 bpf_error("MOPRC host filtering not implemented");
4331 bpf_error("'ip6' modifier applied to ip host");
4334 bpf_error("'icmp6' modifier applied to %s", typestr
);
4338 bpf_error("'ah' modifier applied to %s", typestr
);
4341 bpf_error("'esp' modifier applied to %s", typestr
);
4344 bpf_error("ISO host filtering not implemented");
4347 bpf_error("'esis' modifier applied to %s", typestr
);
4350 bpf_error("'isis' modifier applied to %s", typestr
);
4353 bpf_error("'clnp' modifier applied to %s", typestr
);
4356 bpf_error("'stp' modifier applied to %s", typestr
);
4359 bpf_error("IPX host filtering not implemented");
4362 bpf_error("'netbeui' modifier applied to %s", typestr
);
4365 bpf_error("'radio' modifier applied to %s", typestr
);
4374 static struct block
*
4375 gen_host6(addr
, mask
, proto
, dir
, type
)
4376 struct in6_addr
*addr
;
4377 struct in6_addr
*mask
;
4382 const char *typestr
;
4392 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4395 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4398 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4401 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4404 bpf_error("'sctp' modifier applied to %s", typestr
);
4407 bpf_error("'tcp' modifier applied to %s", typestr
);
4410 bpf_error("'udp' modifier applied to %s", typestr
);
4413 bpf_error("'icmp' modifier applied to %s", typestr
);
4416 bpf_error("'igmp' modifier applied to %s", typestr
);
4419 bpf_error("'igrp' modifier applied to %s", typestr
);
4422 bpf_error("'pim' modifier applied to %s", typestr
);
4425 bpf_error("'vrrp' modifier applied to %s", typestr
);
4428 bpf_error("ATALK host filtering not implemented");
4431 bpf_error("AARP host filtering not implemented");
4434 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4437 bpf_error("SCA host filtering not implemented");
4440 bpf_error("LAT host filtering not implemented");
4443 bpf_error("MOPDL host filtering not implemented");
4446 bpf_error("MOPRC host filtering not implemented");
4449 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4452 bpf_error("'icmp6' modifier applied to %s", typestr
);
4455 bpf_error("'ah' modifier applied to %s", typestr
);
4458 bpf_error("'esp' modifier applied to %s", typestr
);
4461 bpf_error("ISO host filtering not implemented");
4464 bpf_error("'esis' modifier applied to %s", typestr
);
4467 bpf_error("'isis' modifier applied to %s", typestr
);
4470 bpf_error("'clnp' modifier applied to %s", typestr
);
4473 bpf_error("'stp' modifier applied to %s", typestr
);
4476 bpf_error("IPX host filtering not implemented");
4479 bpf_error("'netbeui' modifier applied to %s", typestr
);
4482 bpf_error("'radio' modifier applied to %s", typestr
);
4492 static struct block
*
4493 gen_gateway(eaddr
, alist
, proto
, dir
)
4494 const u_char
*eaddr
;
4495 bpf_u_int32
**alist
;
4499 struct block
*b0
, *b1
, *tmp
;
4502 bpf_error("direction applied to 'gateway'");
4511 b0
= gen_ehostop(eaddr
, Q_OR
);
4514 b0
= gen_fhostop(eaddr
, Q_OR
);
4517 b0
= gen_thostop(eaddr
, Q_OR
);
4519 case DLT_IEEE802_11
:
4520 case DLT_PRISM_HEADER
:
4521 case DLT_IEEE802_11_RADIO_AVS
:
4522 case DLT_IEEE802_11_RADIO
:
4524 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4529 * Check that the packet doesn't begin with an
4530 * LE Control marker. (We've already generated
4533 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4538 * Now check the MAC address.
4540 b0
= gen_ehostop(eaddr
, Q_OR
);
4544 case DLT_IP_OVER_FC
:
4545 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4549 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4551 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4553 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4562 bpf_error("illegal modifier of 'gateway'");
4568 gen_proto_abbrev(proto
)
4577 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4579 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4585 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4587 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4593 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4595 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4601 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4604 #ifndef IPPROTO_IGMP
4605 #define IPPROTO_IGMP 2
4609 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4612 #ifndef IPPROTO_IGRP
4613 #define IPPROTO_IGRP 9
4616 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4620 #define IPPROTO_PIM 103
4624 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4626 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4631 #ifndef IPPROTO_VRRP
4632 #define IPPROTO_VRRP 112
4636 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4640 b1
= gen_linktype(ETHERTYPE_IP
);
4644 b1
= gen_linktype(ETHERTYPE_ARP
);
4648 b1
= gen_linktype(ETHERTYPE_REVARP
);
4652 bpf_error("link layer applied in wrong context");
4655 b1
= gen_linktype(ETHERTYPE_ATALK
);
4659 b1
= gen_linktype(ETHERTYPE_AARP
);
4663 b1
= gen_linktype(ETHERTYPE_DN
);
4667 b1
= gen_linktype(ETHERTYPE_SCA
);
4671 b1
= gen_linktype(ETHERTYPE_LAT
);
4675 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4679 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4684 b1
= gen_linktype(ETHERTYPE_IPV6
);
4687 #ifndef IPPROTO_ICMPV6
4688 #define IPPROTO_ICMPV6 58
4691 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4696 #define IPPROTO_AH 51
4699 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4701 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4707 #define IPPROTO_ESP 50
4710 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4712 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4718 b1
= gen_linktype(LLCSAP_ISONS
);
4722 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4726 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4729 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4730 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4731 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4733 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4735 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4737 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4741 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4742 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4743 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4745 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4747 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4749 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4753 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4754 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4755 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4757 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4762 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4763 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4768 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4769 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4771 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4773 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4778 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4779 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4784 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4785 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4790 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4794 b1
= gen_linktype(LLCSAP_8021D
);
4798 b1
= gen_linktype(LLCSAP_IPX
);
4802 b1
= gen_linktype(LLCSAP_NETBEUI
);
4806 bpf_error("'radio' is not a valid protocol type");
4814 static struct block
*
4821 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4822 b
= new_block(JMP(BPF_JSET
));
4831 * Generate a comparison to a port value in the transport-layer header
4832 * at the specified offset from the beginning of that header.
4834 * XXX - this handles a variable-length prefix preceding the link-layer
4835 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4836 * variable-length link-layer headers (such as Token Ring or 802.11
4839 static struct block
*
4840 gen_portatom(off
, v
)
4844 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4848 static struct block
*
4849 gen_portatom6(off
, v
)
4853 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4858 gen_portop(port
, proto
, dir
)
4859 int port
, proto
, dir
;
4861 struct block
*b0
, *b1
, *tmp
;
4863 /* ip proto 'proto' */
4864 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4870 b1
= gen_portatom(0, (bpf_int32
)port
);
4874 b1
= gen_portatom(2, (bpf_int32
)port
);
4879 tmp
= gen_portatom(0, (bpf_int32
)port
);
4880 b1
= gen_portatom(2, (bpf_int32
)port
);
4885 tmp
= gen_portatom(0, (bpf_int32
)port
);
4886 b1
= gen_portatom(2, (bpf_int32
)port
);
4898 static struct block
*
4899 gen_port(port
, ip_proto
, dir
)
4904 struct block
*b0
, *b1
, *tmp
;
4909 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4910 * not LLC encapsulation with LLCSAP_IP.
4912 * For IEEE 802 networks - which includes 802.5 token ring
4913 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4914 * says that SNAP encapsulation is used, not LLC encapsulation
4917 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4918 * RFC 2225 say that SNAP encapsulation is used, not LLC
4919 * encapsulation with LLCSAP_IP.
4921 * So we always check for ETHERTYPE_IP.
4923 b0
= gen_linktype(ETHERTYPE_IP
);
4929 b1
= gen_portop(port
, ip_proto
, dir
);
4933 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4934 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4936 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4949 gen_portop6(port
, proto
, dir
)
4950 int port
, proto
, dir
;
4952 struct block
*b0
, *b1
, *tmp
;
4954 /* ip6 proto 'proto' */
4955 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4959 b1
= gen_portatom6(0, (bpf_int32
)port
);
4963 b1
= gen_portatom6(2, (bpf_int32
)port
);
4968 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4969 b1
= gen_portatom6(2, (bpf_int32
)port
);
4974 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4975 b1
= gen_portatom6(2, (bpf_int32
)port
);
4987 static struct block
*
4988 gen_port6(port
, ip_proto
, dir
)
4993 struct block
*b0
, *b1
, *tmp
;
4995 /* link proto ip6 */
4996 b0
= gen_linktype(ETHERTYPE_IPV6
);
5002 b1
= gen_portop6(port
, ip_proto
, dir
);
5006 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5007 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5009 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5021 /* gen_portrange code */
5022 static struct block
*
5023 gen_portrangeatom(off
, v1
, v2
)
5027 struct block
*b1
, *b2
;
5031 * Reverse the order of the ports, so v1 is the lower one.
5040 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5041 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5049 gen_portrangeop(port1
, port2
, proto
, dir
)
5054 struct block
*b0
, *b1
, *tmp
;
5056 /* ip proto 'proto' */
5057 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5063 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5067 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5072 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5073 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5078 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5079 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5091 static struct block
*
5092 gen_portrange(port1
, port2
, ip_proto
, dir
)
5097 struct block
*b0
, *b1
, *tmp
;
5100 b0
= gen_linktype(ETHERTYPE_IP
);
5106 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5110 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5111 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5113 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5125 static struct block
*
5126 gen_portrangeatom6(off
, v1
, v2
)
5130 struct block
*b1
, *b2
;
5134 * Reverse the order of the ports, so v1 is the lower one.
5143 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5144 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5152 gen_portrangeop6(port1
, port2
, proto
, dir
)
5157 struct block
*b0
, *b1
, *tmp
;
5159 /* ip6 proto 'proto' */
5160 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5164 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5168 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5173 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5174 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5179 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5180 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5192 static struct block
*
5193 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5198 struct block
*b0
, *b1
, *tmp
;
5200 /* link proto ip6 */
5201 b0
= gen_linktype(ETHERTYPE_IPV6
);
5207 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5211 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5212 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5214 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5227 lookup_proto(name
, proto
)
5228 register const char *name
;
5238 v
= pcap_nametoproto(name
);
5239 if (v
== PROTO_UNDEF
)
5240 bpf_error("unknown ip proto '%s'", name
);
5244 /* XXX should look up h/w protocol type based on linktype */
5245 v
= pcap_nametoeproto(name
);
5246 if (v
== PROTO_UNDEF
) {
5247 v
= pcap_nametollc(name
);
5248 if (v
== PROTO_UNDEF
)
5249 bpf_error("unknown ether proto '%s'", name
);
5254 if (strcmp(name
, "esis") == 0)
5256 else if (strcmp(name
, "isis") == 0)
5258 else if (strcmp(name
, "clnp") == 0)
5261 bpf_error("unknown osi proto '%s'", name
);
5281 static struct block
*
5282 gen_protochain(v
, proto
, dir
)
5287 #ifdef NO_PROTOCHAIN
5288 return gen_proto(v
, proto
, dir
);
5290 struct block
*b0
, *b
;
5291 struct slist
*s
[100];
5292 int fix2
, fix3
, fix4
, fix5
;
5293 int ahcheck
, again
, end
;
5295 int reg2
= alloc_reg();
5297 memset(s
, 0, sizeof(s
));
5298 fix2
= fix3
= fix4
= fix5
= 0;
5305 b0
= gen_protochain(v
, Q_IP
, dir
);
5306 b
= gen_protochain(v
, Q_IPV6
, dir
);
5310 bpf_error("bad protocol applied for 'protochain'");
5315 * We don't handle variable-length prefixes before the link-layer
5316 * header, or variable-length link-layer headers, here yet.
5317 * We might want to add BPF instructions to do the protochain
5318 * work, to simplify that and, on platforms that have a BPF
5319 * interpreter with the new instructions, let the filtering
5320 * be done in the kernel. (We already require a modified BPF
5321 * engine to do the protochain stuff, to support backward
5322 * branches, and backward branch support is unlikely to appear
5323 * in kernel BPF engines.)
5327 case DLT_IEEE802_11
:
5328 case DLT_PRISM_HEADER
:
5329 case DLT_IEEE802_11_RADIO_AVS
:
5330 case DLT_IEEE802_11_RADIO
:
5332 bpf_error("'protochain' not supported with 802.11");
5335 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5338 * s[0] is a dummy entry to protect other BPF insn from damage
5339 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5340 * hard to find interdependency made by jump table fixup.
5343 s
[i
] = new_stmt(0); /*dummy*/
5348 b0
= gen_linktype(ETHERTYPE_IP
);
5351 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5352 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5354 /* X = ip->ip_hl << 2 */
5355 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5356 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5361 b0
= gen_linktype(ETHERTYPE_IPV6
);
5363 /* A = ip6->ip_nxt */
5364 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5365 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5367 /* X = sizeof(struct ip6_hdr) */
5368 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5374 bpf_error("unsupported proto to gen_protochain");
5378 /* again: if (A == v) goto end; else fall through; */
5380 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5382 s
[i
]->s
.jt
= NULL
; /*later*/
5383 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5387 #ifndef IPPROTO_NONE
5388 #define IPPROTO_NONE 59
5390 /* if (A == IPPROTO_NONE) goto end */
5391 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5392 s
[i
]->s
.jt
= NULL
; /*later*/
5393 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5394 s
[i
]->s
.k
= IPPROTO_NONE
;
5395 s
[fix5
]->s
.jf
= s
[i
];
5400 if (proto
== Q_IPV6
) {
5401 int v6start
, v6end
, v6advance
, j
;
5404 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5405 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5406 s
[i
]->s
.jt
= NULL
; /*later*/
5407 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5408 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5409 s
[fix2
]->s
.jf
= s
[i
];
5411 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5412 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5413 s
[i
]->s
.jt
= NULL
; /*later*/
5414 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5415 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5417 /* if (A == IPPROTO_ROUTING) goto v6advance */
5418 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5419 s
[i
]->s
.jt
= NULL
; /*later*/
5420 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5421 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5423 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5424 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5425 s
[i
]->s
.jt
= NULL
; /*later*/
5426 s
[i
]->s
.jf
= NULL
; /*later*/
5427 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5438 * X = X + (P[X + 1] + 1) * 8;
5441 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5443 /* A = P[X + packet head] */
5444 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5445 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5448 s
[i
] = new_stmt(BPF_ST
);
5452 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5455 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5459 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5461 /* A = P[X + packet head]; */
5462 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5463 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5466 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5470 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5474 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5477 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5481 /* goto again; (must use BPF_JA for backward jump) */
5482 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5483 s
[i
]->s
.k
= again
- i
- 1;
5484 s
[i
- 1]->s
.jf
= s
[i
];
5488 for (j
= v6start
; j
<= v6end
; j
++)
5489 s
[j
]->s
.jt
= s
[v6advance
];
5494 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5496 s
[fix2
]->s
.jf
= s
[i
];
5502 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5503 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5504 s
[i
]->s
.jt
= NULL
; /*later*/
5505 s
[i
]->s
.jf
= NULL
; /*later*/
5506 s
[i
]->s
.k
= IPPROTO_AH
;
5508 s
[fix3
]->s
.jf
= s
[ahcheck
];
5515 * X = X + (P[X + 1] + 2) * 4;
5518 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5520 /* A = P[X + packet head]; */
5521 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5522 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5525 s
[i
] = new_stmt(BPF_ST
);
5529 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5532 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5536 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5538 /* A = P[X + packet head] */
5539 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5540 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5543 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5547 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5551 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5554 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5558 /* goto again; (must use BPF_JA for backward jump) */
5559 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5560 s
[i
]->s
.k
= again
- i
- 1;
5565 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5567 s
[fix2
]->s
.jt
= s
[end
];
5568 s
[fix4
]->s
.jf
= s
[end
];
5569 s
[fix5
]->s
.jt
= s
[end
];
5576 for (i
= 0; i
< max
- 1; i
++)
5577 s
[i
]->next
= s
[i
+ 1];
5578 s
[max
- 1]->next
= NULL
;
5583 b
= new_block(JMP(BPF_JEQ
));
5584 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5594 static struct block
*
5595 gen_check_802_11_data_frame()
5598 struct block
*b0
, *b1
;
5601 * A data frame has the 0x08 bit (b3) in the frame control field set
5602 * and the 0x04 bit (b2) clear.
5604 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5605 b0
= new_block(JMP(BPF_JSET
));
5609 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5610 b1
= new_block(JMP(BPF_JSET
));
5621 * Generate code that checks whether the packet is a packet for protocol
5622 * <proto> and whether the type field in that protocol's header has
5623 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5624 * IP packet and checks the protocol number in the IP header against <v>.
5626 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5627 * against Q_IP and Q_IPV6.
5629 static struct block
*
5630 gen_proto(v
, proto
, dir
)
5635 struct block
*b0
, *b1
;
5637 if (dir
!= Q_DEFAULT
)
5638 bpf_error("direction applied to 'proto'");
5643 b0
= gen_proto(v
, Q_IP
, dir
);
5644 b1
= gen_proto(v
, Q_IPV6
, dir
);
5652 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5653 * not LLC encapsulation with LLCSAP_IP.
5655 * For IEEE 802 networks - which includes 802.5 token ring
5656 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5657 * says that SNAP encapsulation is used, not LLC encapsulation
5660 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5661 * RFC 2225 say that SNAP encapsulation is used, not LLC
5662 * encapsulation with LLCSAP_IP.
5664 * So we always check for ETHERTYPE_IP.
5666 b0
= gen_linktype(ETHERTYPE_IP
);
5668 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5670 b1
= gen_protochain(v
, Q_IP
);
5680 * Frame Relay packets typically have an OSI
5681 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5682 * generates code to check for all the OSI
5683 * NLPIDs, so calling it and then adding a check
5684 * for the particular NLPID for which we're
5685 * looking is bogus, as we can just check for
5688 * What we check for is the NLPID and a frame
5689 * control field value of UI, i.e. 0x03 followed
5692 * XXX - assumes a 2-byte Frame Relay header with
5693 * DLCI and flags. What if the address is longer?
5695 * XXX - what about SNAP-encapsulated frames?
5697 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5703 * Cisco uses an Ethertype lookalike - for OSI,
5706 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5707 /* OSI in C-HDLC is stuffed with a fudge byte */
5708 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5713 b0
= gen_linktype(LLCSAP_ISONS
);
5714 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5720 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5722 * 4 is the offset of the PDU type relative to the IS-IS
5725 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5730 bpf_error("arp does not encapsulate another protocol");
5734 bpf_error("rarp does not encapsulate another protocol");
5738 bpf_error("atalk encapsulation is not specifiable");
5742 bpf_error("decnet encapsulation is not specifiable");
5746 bpf_error("sca does not encapsulate another protocol");
5750 bpf_error("lat does not encapsulate another protocol");
5754 bpf_error("moprc does not encapsulate another protocol");
5758 bpf_error("mopdl does not encapsulate another protocol");
5762 return gen_linktype(v
);
5765 bpf_error("'udp proto' is bogus");
5769 bpf_error("'tcp proto' is bogus");
5773 bpf_error("'sctp proto' is bogus");
5777 bpf_error("'icmp proto' is bogus");
5781 bpf_error("'igmp proto' is bogus");
5785 bpf_error("'igrp proto' is bogus");
5789 bpf_error("'pim proto' is bogus");
5793 bpf_error("'vrrp proto' is bogus");
5798 b0
= gen_linktype(ETHERTYPE_IPV6
);
5800 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5802 b1
= gen_protochain(v
, Q_IPV6
);
5808 bpf_error("'icmp6 proto' is bogus");
5812 bpf_error("'ah proto' is bogus");
5815 bpf_error("'ah proto' is bogus");
5818 bpf_error("'stp proto' is bogus");
5821 bpf_error("'ipx proto' is bogus");
5824 bpf_error("'netbeui proto' is bogus");
5827 bpf_error("'radio proto' is bogus");
5838 register const char *name
;
5841 int proto
= q
.proto
;
5845 bpf_u_int32 mask
, addr
;
5847 bpf_u_int32
**alist
;
5850 struct sockaddr_in
*sin4
;
5851 struct sockaddr_in6
*sin6
;
5852 struct addrinfo
*res
, *res0
;
5853 struct in6_addr mask128
;
5855 struct block
*b
, *tmp
;
5856 int port
, real_proto
;
5862 addr
= pcap_nametonetaddr(name
);
5864 bpf_error("unknown network '%s'", name
);
5865 /* Left justify network addr and calculate its network mask */
5867 while (addr
&& (addr
& 0xff000000) == 0) {
5871 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5875 if (proto
== Q_LINK
) {
5879 eaddr
= pcap_ether_hostton(name
);
5882 "unknown ether host '%s'", name
);
5883 b
= gen_ehostop(eaddr
, dir
);
5888 eaddr
= pcap_ether_hostton(name
);
5891 "unknown FDDI host '%s'", name
);
5892 b
= gen_fhostop(eaddr
, dir
);
5897 eaddr
= pcap_ether_hostton(name
);
5900 "unknown token ring host '%s'", name
);
5901 b
= gen_thostop(eaddr
, dir
);
5905 case DLT_IEEE802_11
:
5906 case DLT_PRISM_HEADER
:
5907 case DLT_IEEE802_11_RADIO_AVS
:
5908 case DLT_IEEE802_11_RADIO
:
5910 eaddr
= pcap_ether_hostton(name
);
5913 "unknown 802.11 host '%s'", name
);
5914 b
= gen_wlanhostop(eaddr
, dir
);
5918 case DLT_IP_OVER_FC
:
5919 eaddr
= pcap_ether_hostton(name
);
5922 "unknown Fibre Channel host '%s'", name
);
5923 b
= gen_ipfchostop(eaddr
, dir
);
5932 * Check that the packet doesn't begin
5933 * with an LE Control marker. (We've
5934 * already generated a test for LANE.)
5936 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5940 eaddr
= pcap_ether_hostton(name
);
5943 "unknown ether host '%s'", name
);
5944 b
= gen_ehostop(eaddr
, dir
);
5950 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5951 } else if (proto
== Q_DECNET
) {
5952 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5954 * I don't think DECNET hosts can be multihomed, so
5955 * there is no need to build up a list of addresses
5957 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5960 alist
= pcap_nametoaddr(name
);
5961 if (alist
== NULL
|| *alist
== NULL
)
5962 bpf_error("unknown host '%s'", name
);
5964 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5966 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5968 tmp
= gen_host(**alist
++, 0xffffffff,
5969 tproto
, dir
, q
.addr
);
5975 memset(&mask128
, 0xff, sizeof(mask128
));
5976 res0
= res
= pcap_nametoaddrinfo(name
);
5978 bpf_error("unknown host '%s'", name
);
5980 tproto
= tproto6
= proto
;
5981 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5985 for (res
= res0
; res
; res
= res
->ai_next
) {
5986 switch (res
->ai_family
) {
5988 if (tproto
== Q_IPV6
)
5991 sin4
= (struct sockaddr_in
*)
5993 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5994 0xffffffff, tproto
, dir
, q
.addr
);
5997 if (tproto6
== Q_IP
)
6000 sin6
= (struct sockaddr_in6
*)
6002 tmp
= gen_host6(&sin6
->sin6_addr
,
6003 &mask128
, tproto6
, dir
, q
.addr
);
6014 bpf_error("unknown host '%s'%s", name
,
6015 (proto
== Q_DEFAULT
)
6017 : " for specified address family");
6024 if (proto
!= Q_DEFAULT
&&
6025 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6026 bpf_error("illegal qualifier of 'port'");
6027 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6028 bpf_error("unknown port '%s'", name
);
6029 if (proto
== Q_UDP
) {
6030 if (real_proto
== IPPROTO_TCP
)
6031 bpf_error("port '%s' is tcp", name
);
6032 else if (real_proto
== IPPROTO_SCTP
)
6033 bpf_error("port '%s' is sctp", name
);
6035 /* override PROTO_UNDEF */
6036 real_proto
= IPPROTO_UDP
;
6038 if (proto
== Q_TCP
) {
6039 if (real_proto
== IPPROTO_UDP
)
6040 bpf_error("port '%s' is udp", name
);
6042 else if (real_proto
== IPPROTO_SCTP
)
6043 bpf_error("port '%s' is sctp", name
);
6045 /* override PROTO_UNDEF */
6046 real_proto
= IPPROTO_TCP
;
6048 if (proto
== Q_SCTP
) {
6049 if (real_proto
== IPPROTO_UDP
)
6050 bpf_error("port '%s' is udp", name
);
6052 else if (real_proto
== IPPROTO_TCP
)
6053 bpf_error("port '%s' is tcp", name
);
6055 /* override PROTO_UNDEF */
6056 real_proto
= IPPROTO_SCTP
;
6059 return gen_port(port
, real_proto
, dir
);
6061 b
= gen_port(port
, real_proto
, dir
);
6062 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6067 if (proto
!= Q_DEFAULT
&&
6068 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6069 bpf_error("illegal qualifier of 'portrange'");
6070 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6071 bpf_error("unknown port in range '%s'", name
);
6072 if (proto
== Q_UDP
) {
6073 if (real_proto
== IPPROTO_TCP
)
6074 bpf_error("port in range '%s' is tcp", name
);
6075 else if (real_proto
== IPPROTO_SCTP
)
6076 bpf_error("port in range '%s' is sctp", name
);
6078 /* override PROTO_UNDEF */
6079 real_proto
= IPPROTO_UDP
;
6081 if (proto
== Q_TCP
) {
6082 if (real_proto
== IPPROTO_UDP
)
6083 bpf_error("port in range '%s' is udp", name
);
6084 else if (real_proto
== IPPROTO_SCTP
)
6085 bpf_error("port in range '%s' is sctp", name
);
6087 /* override PROTO_UNDEF */
6088 real_proto
= IPPROTO_TCP
;
6090 if (proto
== Q_SCTP
) {
6091 if (real_proto
== IPPROTO_UDP
)
6092 bpf_error("port in range '%s' is udp", name
);
6093 else if (real_proto
== IPPROTO_TCP
)
6094 bpf_error("port in range '%s' is tcp", name
);
6096 /* override PROTO_UNDEF */
6097 real_proto
= IPPROTO_SCTP
;
6100 return gen_portrange(port1
, port2
, real_proto
, dir
);
6102 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6103 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6109 eaddr
= pcap_ether_hostton(name
);
6111 bpf_error("unknown ether host: %s", name
);
6113 alist
= pcap_nametoaddr(name
);
6114 if (alist
== NULL
|| *alist
== NULL
)
6115 bpf_error("unknown host '%s'", name
);
6116 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6120 bpf_error("'gateway' not supported in this configuration");
6124 real_proto
= lookup_proto(name
, proto
);
6125 if (real_proto
>= 0)
6126 return gen_proto(real_proto
, proto
, dir
);
6128 bpf_error("unknown protocol: %s", name
);
6131 real_proto
= lookup_proto(name
, proto
);
6132 if (real_proto
>= 0)
6133 return gen_protochain(real_proto
, proto
, dir
);
6135 bpf_error("unknown protocol: %s", name
);
6146 gen_mcode(s1
, s2
, masklen
, q
)
6147 register const char *s1
, *s2
;
6148 register int masklen
;
6151 register int nlen
, mlen
;
6154 nlen
= __pcap_atoin(s1
, &n
);
6155 /* Promote short ipaddr */
6159 mlen
= __pcap_atoin(s2
, &m
);
6160 /* Promote short ipaddr */
6163 bpf_error("non-network bits set in \"%s mask %s\"",
6166 /* Convert mask len to mask */
6168 bpf_error("mask length must be <= 32");
6171 * X << 32 is not guaranteed by C to be 0; it's
6176 m
= 0xffffffff << (32 - masklen
);
6178 bpf_error("non-network bits set in \"%s/%d\"",
6185 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6188 bpf_error("Mask syntax for networks only");
6197 register const char *s
;
6202 int proto
= q
.proto
;
6208 else if (q
.proto
== Q_DECNET
)
6209 vlen
= __pcap_atodn(s
, &v
);
6211 vlen
= __pcap_atoin(s
, &v
);
6218 if (proto
== Q_DECNET
)
6219 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6220 else if (proto
== Q_LINK
) {
6221 bpf_error("illegal link layer address");
6224 if (s
== NULL
&& q
.addr
== Q_NET
) {
6225 /* Promote short net number */
6226 while (v
&& (v
& 0xff000000) == 0) {
6231 /* Promote short ipaddr */
6235 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6240 proto
= IPPROTO_UDP
;
6241 else if (proto
== Q_TCP
)
6242 proto
= IPPROTO_TCP
;
6243 else if (proto
== Q_SCTP
)
6244 proto
= IPPROTO_SCTP
;
6245 else if (proto
== Q_DEFAULT
)
6246 proto
= PROTO_UNDEF
;
6248 bpf_error("illegal qualifier of 'port'");
6251 return gen_port((int)v
, proto
, dir
);
6255 b
= gen_port((int)v
, proto
, dir
);
6256 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6263 proto
= IPPROTO_UDP
;
6264 else if (proto
== Q_TCP
)
6265 proto
= IPPROTO_TCP
;
6266 else if (proto
== Q_SCTP
)
6267 proto
= IPPROTO_SCTP
;
6268 else if (proto
== Q_DEFAULT
)
6269 proto
= PROTO_UNDEF
;
6271 bpf_error("illegal qualifier of 'portrange'");
6274 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6278 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6279 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6285 bpf_error("'gateway' requires a name");
6289 return gen_proto((int)v
, proto
, dir
);
6292 return gen_protochain((int)v
, proto
, dir
);
6307 gen_mcode6(s1
, s2
, masklen
, q
)
6308 register const char *s1
, *s2
;
6309 register int masklen
;
6312 struct addrinfo
*res
;
6313 struct in6_addr
*addr
;
6314 struct in6_addr mask
;
6319 bpf_error("no mask %s supported", s2
);
6321 res
= pcap_nametoaddrinfo(s1
);
6323 bpf_error("invalid ip6 address %s", s1
);
6325 bpf_error("%s resolved to multiple address", s1
);
6326 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6328 if (sizeof(mask
) * 8 < masklen
)
6329 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6330 memset(&mask
, 0, sizeof(mask
));
6331 memset(&mask
, 0xff, masklen
/ 8);
6333 mask
.s6_addr
[masklen
/ 8] =
6334 (0xff << (8 - masklen
% 8)) & 0xff;
6337 a
= (u_int32_t
*)addr
;
6338 m
= (u_int32_t
*)&mask
;
6339 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6340 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6341 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6349 bpf_error("Mask syntax for networks only");
6353 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6358 bpf_error("invalid qualifier against IPv6 address");
6367 register const u_char
*eaddr
;
6370 struct block
*b
, *tmp
;
6372 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6375 return gen_ehostop(eaddr
, (int)q
.dir
);
6377 return gen_fhostop(eaddr
, (int)q
.dir
);
6379 return gen_thostop(eaddr
, (int)q
.dir
);
6380 case DLT_IEEE802_11
:
6381 case DLT_PRISM_HEADER
:
6382 case DLT_IEEE802_11_RADIO_AVS
:
6383 case DLT_IEEE802_11_RADIO
:
6385 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6389 * Check that the packet doesn't begin with an
6390 * LE Control marker. (We've already generated
6393 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6398 * Now check the MAC address.
6400 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6405 case DLT_IP_OVER_FC
:
6406 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6408 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6412 bpf_error("ethernet address used in non-ether expression");
6419 struct slist
*s0
, *s1
;
6422 * This is definitely not the best way to do this, but the
6423 * lists will rarely get long.
6430 static struct slist
*
6436 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6441 static struct slist
*
6447 s
= new_stmt(BPF_LD
|BPF_MEM
);
6453 * Modify "index" to use the value stored into its register as an
6454 * offset relative to the beginning of the header for the protocol
6455 * "proto", and allocate a register and put an item "size" bytes long
6456 * (1, 2, or 4) at that offset into that register, making it the register
6460 gen_load(proto
, inst
, size
)
6465 struct slist
*s
, *tmp
;
6467 int regno
= alloc_reg();
6469 free_reg(inst
->regno
);
6473 bpf_error("data size must be 1, 2, or 4");
6489 bpf_error("unsupported index operation");
6493 * The offset is relative to the beginning of the packet
6494 * data, if we have a radio header. (If we don't, this
6497 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6498 linktype
!= DLT_IEEE802_11_RADIO
&&
6499 linktype
!= DLT_PRISM_HEADER
)
6500 bpf_error("radio information not present in capture");
6503 * Load into the X register the offset computed into the
6504 * register specifed by "index".
6506 s
= xfer_to_x(inst
);
6509 * Load the item at that offset.
6511 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6513 sappend(inst
->s
, s
);
6518 * The offset is relative to the beginning of
6519 * the link-layer header.
6521 * XXX - what about ATM LANE? Should the index be
6522 * relative to the beginning of the AAL5 frame, so
6523 * that 0 refers to the beginning of the LE Control
6524 * field, or relative to the beginning of the LAN
6525 * frame, so that 0 refers, for Ethernet LANE, to
6526 * the beginning of the destination address?
6528 s
= gen_llprefixlen();
6531 * If "s" is non-null, it has code to arrange that the
6532 * X register contains the length of the prefix preceding
6533 * the link-layer header. Add to it the offset computed
6534 * into the register specified by "index", and move that
6535 * into the X register. Otherwise, just load into the X
6536 * register the offset computed into the register specifed
6540 sappend(s
, xfer_to_a(inst
));
6541 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6542 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6544 s
= xfer_to_x(inst
);
6547 * Load the item at the sum of the offset we've put in the
6548 * X register and the offset of the start of the link
6549 * layer header (which is 0 if the radio header is
6550 * variable-length; that header length is what we put
6551 * into the X register and then added to the index).
6553 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6556 sappend(inst
->s
, s
);
6572 * The offset is relative to the beginning of
6573 * the network-layer header.
6574 * XXX - are there any cases where we want
6577 s
= gen_off_macpl();
6580 * If "s" is non-null, it has code to arrange that the
6581 * X register contains the offset of the MAC-layer
6582 * payload. Add to it the offset computed into the
6583 * register specified by "index", and move that into
6584 * the X register. Otherwise, just load into the X
6585 * register the offset computed into the register specifed
6589 sappend(s
, xfer_to_a(inst
));
6590 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6591 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6593 s
= xfer_to_x(inst
);
6596 * Load the item at the sum of the offset we've put in the
6597 * X register, the offset of the start of the network
6598 * layer header from the beginning of the MAC-layer
6599 * payload, and the purported offset of the start of the
6600 * MAC-layer payload (which might be 0 if there's a
6601 * variable-length prefix before the link-layer header
6602 * or the link-layer header itself is variable-length;
6603 * the variable-length offset of the start of the
6604 * MAC-layer payload is what we put into the X register
6605 * and then added to the index).
6607 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6608 tmp
->s
.k
= off_macpl
+ off_nl
;
6610 sappend(inst
->s
, s
);
6613 * Do the computation only if the packet contains
6614 * the protocol in question.
6616 b
= gen_proto_abbrev(proto
);
6618 gen_and(inst
->b
, b
);
6631 * The offset is relative to the beginning of
6632 * the transport-layer header.
6634 * Load the X register with the length of the IPv4 header
6635 * (plus the offset of the link-layer header, if it's
6636 * a variable-length header), in bytes.
6638 * XXX - are there any cases where we want
6640 * XXX - we should, if we're built with
6641 * IPv6 support, generate code to load either
6642 * IPv4, IPv6, or both, as appropriate.
6644 s
= gen_loadx_iphdrlen();
6647 * The X register now contains the sum of the length
6648 * of any variable-length header preceding the link-layer
6649 * header, any variable-length link-layer header, and the
6650 * length of the network-layer header.
6652 * Load into the A register the offset relative to
6653 * the beginning of the transport layer header,
6654 * add the X register to that, move that to the
6655 * X register, and load with an offset from the
6656 * X register equal to the offset of the network
6657 * layer header relative to the beginning of
6658 * the MAC-layer payload plus the fixed-length
6659 * portion of the offset of the MAC-layer payload
6660 * from the beginning of the raw packet data.
6662 sappend(s
, xfer_to_a(inst
));
6663 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6664 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6665 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6666 tmp
->s
.k
= off_macpl
+ off_nl
;
6667 sappend(inst
->s
, s
);
6670 * Do the computation only if the packet contains
6671 * the protocol in question - which is true only
6672 * if this is an IP datagram and is the first or
6673 * only fragment of that datagram.
6675 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6677 gen_and(inst
->b
, b
);
6679 gen_and(gen_proto_abbrev(Q_IP
), b
);
6685 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6689 inst
->regno
= regno
;
6690 s
= new_stmt(BPF_ST
);
6692 sappend(inst
->s
, s
);
6698 gen_relation(code
, a0
, a1
, reversed
)
6700 struct arth
*a0
, *a1
;
6703 struct slist
*s0
, *s1
, *s2
;
6704 struct block
*b
, *tmp
;
6708 if (code
== BPF_JEQ
) {
6709 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6710 b
= new_block(JMP(code
));
6714 b
= new_block(BPF_JMP
|code
|BPF_X
);
6720 sappend(a0
->s
, a1
->s
);
6724 free_reg(a0
->regno
);
6725 free_reg(a1
->regno
);
6727 /* 'and' together protocol checks */
6730 gen_and(a0
->b
, tmp
= a1
->b
);
6746 int regno
= alloc_reg();
6747 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6750 s
= new_stmt(BPF_LD
|BPF_LEN
);
6751 s
->next
= new_stmt(BPF_ST
);
6752 s
->next
->s
.k
= regno
;
6767 a
= (struct arth
*)newchunk(sizeof(*a
));
6771 s
= new_stmt(BPF_LD
|BPF_IMM
);
6773 s
->next
= new_stmt(BPF_ST
);
6789 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6792 s
= new_stmt(BPF_ST
);
6800 gen_arth(code
, a0
, a1
)
6802 struct arth
*a0
, *a1
;
6804 struct slist
*s0
, *s1
, *s2
;
6808 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6813 sappend(a0
->s
, a1
->s
);
6815 free_reg(a0
->regno
);
6816 free_reg(a1
->regno
);
6818 s0
= new_stmt(BPF_ST
);
6819 a0
->regno
= s0
->s
.k
= alloc_reg();
6826 * Here we handle simple allocation of the scratch registers.
6827 * If too many registers are alloc'd, the allocator punts.
6829 static int regused
[BPF_MEMWORDS
];
6833 * Initialize the table of used registers and the current register.
6839 memset(regused
, 0, sizeof regused
);
6843 * Return the next free register.
6848 int n
= BPF_MEMWORDS
;
6851 if (regused
[curreg
])
6852 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6854 regused
[curreg
] = 1;
6858 bpf_error("too many registers needed to evaluate expression");
6864 * Return a register to the table so it can
6874 static struct block
*
6881 s
= new_stmt(BPF_LD
|BPF_LEN
);
6882 b
= new_block(JMP(jmp
));
6893 return gen_len(BPF_JGE
, n
);
6897 * Actually, this is less than or equal.
6905 b
= gen_len(BPF_JGT
, n
);
6912 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6913 * the beginning of the link-layer header.
6914 * XXX - that means you can't test values in the radiotap header, but
6915 * as that header is difficult if not impossible to parse generally
6916 * without a loop, that might not be a severe problem. A new keyword
6917 * "radio" could be added for that, although what you'd really want
6918 * would be a way of testing particular radio header values, which
6919 * would generate code appropriate to the radio header in question.
6922 gen_byteop(op
, idx
, val
)
6933 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6936 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6940 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6944 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6948 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6952 b
= new_block(JMP(BPF_JEQ
));
6959 static u_char abroadcast
[] = { 0x0 };
6962 gen_broadcast(proto
)
6965 bpf_u_int32 hostmask
;
6966 struct block
*b0
, *b1
, *b2
;
6967 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6975 case DLT_ARCNET_LINUX
:
6976 return gen_ahostop(abroadcast
, Q_DST
);
6978 return gen_ehostop(ebroadcast
, Q_DST
);
6980 return gen_fhostop(ebroadcast
, Q_DST
);
6982 return gen_thostop(ebroadcast
, Q_DST
);
6983 case DLT_IEEE802_11
:
6984 case DLT_PRISM_HEADER
:
6985 case DLT_IEEE802_11_RADIO_AVS
:
6986 case DLT_IEEE802_11_RADIO
:
6988 return gen_wlanhostop(ebroadcast
, Q_DST
);
6989 case DLT_IP_OVER_FC
:
6990 return gen_ipfchostop(ebroadcast
, Q_DST
);
6994 * Check that the packet doesn't begin with an
6995 * LE Control marker. (We've already generated
6998 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7003 * Now check the MAC address.
7005 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7011 bpf_error("not a broadcast link");
7016 b0
= gen_linktype(ETHERTYPE_IP
);
7017 hostmask
= ~netmask
;
7018 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7019 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7020 (bpf_int32
)(~0 & hostmask
), hostmask
);
7025 bpf_error("only link-layer/IP broadcast filters supported");
7031 * Generate code to test the low-order bit of a MAC address (that's
7032 * the bottom bit of the *first* byte).
7034 static struct block
*
7035 gen_mac_multicast(offset
)
7038 register struct block
*b0
;
7039 register struct slist
*s
;
7041 /* link[offset] & 1 != 0 */
7042 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7043 b0
= new_block(JMP(BPF_JSET
));
7050 gen_multicast(proto
)
7053 register struct block
*b0
, *b1
, *b2
;
7054 register struct slist
*s
;
7062 case DLT_ARCNET_LINUX
:
7063 /* all ARCnet multicasts use the same address */
7064 return gen_ahostop(abroadcast
, Q_DST
);
7066 /* ether[0] & 1 != 0 */
7067 return gen_mac_multicast(0);
7070 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7072 * XXX - was that referring to bit-order issues?
7074 /* fddi[1] & 1 != 0 */
7075 return gen_mac_multicast(1);
7077 /* tr[2] & 1 != 0 */
7078 return gen_mac_multicast(2);
7079 case DLT_IEEE802_11
:
7080 case DLT_PRISM_HEADER
:
7081 case DLT_IEEE802_11_RADIO_AVS
:
7082 case DLT_IEEE802_11_RADIO
:
7087 * For control frames, there is no DA.
7089 * For management frames, DA is at an
7090 * offset of 4 from the beginning of
7093 * For data frames, DA is at an offset
7094 * of 4 from the beginning of the packet
7095 * if To DS is clear and at an offset of
7096 * 16 from the beginning of the packet
7101 * Generate the tests to be done for data frames.
7103 * First, check for To DS set, i.e. "link[1] & 0x01".
7105 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7106 b1
= new_block(JMP(BPF_JSET
));
7107 b1
->s
.k
= 0x01; /* To DS */
7111 * If To DS is set, the DA is at 16.
7113 b0
= gen_mac_multicast(16);
7117 * Now, check for To DS not set, i.e. check
7118 * "!(link[1] & 0x01)".
7120 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7121 b2
= new_block(JMP(BPF_JSET
));
7122 b2
->s
.k
= 0x01; /* To DS */
7127 * If To DS is not set, the DA is at 4.
7129 b1
= gen_mac_multicast(4);
7133 * Now OR together the last two checks. That gives
7134 * the complete set of checks for data frames.
7139 * Now check for a data frame.
7140 * I.e, check "link[0] & 0x08".
7142 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7143 b1
= new_block(JMP(BPF_JSET
));
7148 * AND that with the checks done for data frames.
7153 * If the high-order bit of the type value is 0, this
7154 * is a management frame.
7155 * I.e, check "!(link[0] & 0x08)".
7157 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7158 b2
= new_block(JMP(BPF_JSET
));
7164 * For management frames, the DA is at 4.
7166 b1
= gen_mac_multicast(4);
7170 * OR that with the checks done for data frames.
7171 * That gives the checks done for management and
7177 * If the low-order bit of the type value is 1,
7178 * this is either a control frame or a frame
7179 * with a reserved type, and thus not a
7182 * I.e., check "!(link[0] & 0x04)".
7184 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7185 b1
= new_block(JMP(BPF_JSET
));
7191 * AND that with the checks for data and management
7196 case DLT_IP_OVER_FC
:
7197 b0
= gen_mac_multicast(2);
7202 * Check that the packet doesn't begin with an
7203 * LE Control marker. (We've already generated
7206 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7210 /* ether[off_mac] & 1 != 0 */
7211 b0
= gen_mac_multicast(off_mac
);
7219 /* Link not known to support multicasts */
7223 b0
= gen_linktype(ETHERTYPE_IP
);
7224 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7230 b0
= gen_linktype(ETHERTYPE_IPV6
);
7231 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7236 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7242 * generate command for inbound/outbound. It's here so we can
7243 * make it link-type specific. 'dir' = 0 implies "inbound",
7244 * = 1 implies "outbound".
7250 register struct block
*b0
;
7253 * Only some data link types support inbound/outbound qualifiers.
7257 b0
= gen_relation(BPF_JEQ
,
7258 gen_load(Q_LINK
, gen_loadi(0), 1),
7266 * Match packets sent by this machine.
7268 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7271 * Match packets sent to this machine.
7272 * (No broadcast or multicast packets, or
7273 * packets sent to some other machine and
7274 * received promiscuously.)
7276 * XXX - packets sent to other machines probably
7277 * shouldn't be matched, but what about broadcast
7278 * or multicast packets we received?
7280 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7284 #ifdef HAVE_NET_PFVAR_H
7286 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7287 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7293 /* match outgoing packets */
7294 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7296 /* match incoming packets */
7297 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7301 case DLT_JUNIPER_MFR
:
7302 case DLT_JUNIPER_MLFR
:
7303 case DLT_JUNIPER_MLPPP
:
7304 case DLT_JUNIPER_ATM1
:
7305 case DLT_JUNIPER_ATM2
:
7306 case DLT_JUNIPER_PPPOE
:
7307 case DLT_JUNIPER_PPPOE_ATM
:
7308 case DLT_JUNIPER_GGSN
:
7309 case DLT_JUNIPER_ES
:
7310 case DLT_JUNIPER_MONITOR
:
7311 case DLT_JUNIPER_SERVICES
:
7312 case DLT_JUNIPER_ETHER
:
7313 case DLT_JUNIPER_PPP
:
7314 case DLT_JUNIPER_FRELAY
:
7315 case DLT_JUNIPER_CHDLC
:
7316 case DLT_JUNIPER_VP
:
7317 case DLT_JUNIPER_ST
:
7318 case DLT_JUNIPER_ISM
:
7319 /* juniper flags (including direction) are stored
7320 * the byte after the 3-byte magic number */
7322 /* match outgoing packets */
7323 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7325 /* match incoming packets */
7326 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7331 bpf_error("inbound/outbound not supported on linktype %d",
7339 #ifdef HAVE_NET_PFVAR_H
7340 /* PF firewall log matched interface */
7342 gen_pf_ifname(const char *ifname
)
7347 if (linktype
!= DLT_PFLOG
) {
7348 bpf_error("ifname supported only on PF linktype");
7351 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7352 off
= offsetof(struct pfloghdr
, ifname
);
7353 if (strlen(ifname
) >= len
) {
7354 bpf_error("ifname interface names can only be %d characters",
7358 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7362 /* PF firewall log ruleset name */
7364 gen_pf_ruleset(char *ruleset
)
7368 if (linktype
!= DLT_PFLOG
) {
7369 bpf_error("ruleset supported only on PF linktype");
7373 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7374 bpf_error("ruleset names can only be %ld characters",
7375 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7379 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7380 strlen(ruleset
), (const u_char
*)ruleset
);
7384 /* PF firewall log rule number */
7390 if (linktype
!= DLT_PFLOG
) {
7391 bpf_error("rnr supported only on PF linktype");
7395 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7400 /* PF firewall log sub-rule number */
7402 gen_pf_srnr(int srnr
)
7406 if (linktype
!= DLT_PFLOG
) {
7407 bpf_error("srnr supported only on PF linktype");
7411 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7416 /* PF firewall log reason code */
7418 gen_pf_reason(int reason
)
7422 if (linktype
!= DLT_PFLOG
) {
7423 bpf_error("reason supported only on PF linktype");
7427 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7432 /* PF firewall log action */
7434 gen_pf_action(int action
)
7438 if (linktype
!= DLT_PFLOG
) {
7439 bpf_error("action supported only on PF linktype");
7443 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7447 #else /* !HAVE_NET_PFVAR_H */
7449 gen_pf_ifname(const char *ifname
)
7451 bpf_error("libpcap was compiled without pf support");
7457 gen_pf_ruleset(char *ruleset
)
7459 bpf_error("libpcap was compiled on a machine without pf support");
7467 bpf_error("libpcap was compiled on a machine without pf support");
7473 gen_pf_srnr(int srnr
)
7475 bpf_error("libpcap was compiled on a machine without pf support");
7481 gen_pf_reason(int reason
)
7483 bpf_error("libpcap was compiled on a machine without pf support");
7489 gen_pf_action(int action
)
7491 bpf_error("libpcap was compiled on a machine without pf support");
7495 #endif /* HAVE_NET_PFVAR_H */
7497 /* IEEE 802.11 wireless header */
7499 gen_p80211_type(int type
, int mask
)
7505 case DLT_IEEE802_11
:
7506 case DLT_PRISM_HEADER
:
7507 case DLT_IEEE802_11_RADIO_AVS
:
7508 case DLT_IEEE802_11_RADIO
:
7509 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7514 bpf_error("802.11 link-layer types supported only on 802.11");
7522 gen_p80211_fcdir(int fcdir
)
7528 case DLT_IEEE802_11
:
7529 case DLT_PRISM_HEADER
:
7530 case DLT_IEEE802_11_RADIO_AVS
:
7531 case DLT_IEEE802_11_RADIO
:
7535 bpf_error("frame direction supported only with 802.11 headers");
7539 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7540 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7547 register const u_char
*eaddr
;
7553 case DLT_ARCNET_LINUX
:
7554 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7556 return (gen_ahostop(eaddr
, (int)q
.dir
));
7558 bpf_error("ARCnet address used in non-arc expression");
7564 bpf_error("aid supported only on ARCnet");
7567 bpf_error("ARCnet address used in non-arc expression");
7572 static struct block
*
7573 gen_ahostop(eaddr
, dir
)
7574 register const u_char
*eaddr
;
7577 register struct block
*b0
, *b1
;
7580 /* src comes first, different from Ethernet */
7582 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7585 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7588 b0
= gen_ahostop(eaddr
, Q_SRC
);
7589 b1
= gen_ahostop(eaddr
, Q_DST
);
7595 b0
= gen_ahostop(eaddr
, Q_SRC
);
7596 b1
= gen_ahostop(eaddr
, Q_DST
);
7605 * support IEEE 802.1Q VLAN trunk over ethernet
7611 struct block
*b0
, *b1
;
7613 /* can't check for VLAN-encapsulated packets inside MPLS */
7614 if (label_stack_depth
> 0)
7615 bpf_error("no VLAN match after MPLS");
7618 * Check for a VLAN packet, and then change the offsets to point
7619 * to the type and data fields within the VLAN packet. Just
7620 * increment the offsets, so that we can support a hierarchy, e.g.
7621 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7624 * XXX - this is a bit of a kludge. If we were to split the
7625 * compiler into a parser that parses an expression and
7626 * generates an expression tree, and a code generator that
7627 * takes an expression tree (which could come from our
7628 * parser or from some other parser) and generates BPF code,
7629 * we could perhaps make the offsets parameters of routines
7630 * and, in the handler for an "AND" node, pass to subnodes
7631 * other than the VLAN node the adjusted offsets.
7633 * This would mean that "vlan" would, instead of changing the
7634 * behavior of *all* tests after it, change only the behavior
7635 * of tests ANDed with it. That would change the documented
7636 * semantics of "vlan", which might break some expressions.
7637 * However, it would mean that "(vlan and ip) or ip" would check
7638 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7639 * checking only for VLAN-encapsulated IP, so that could still
7640 * be considered worth doing; it wouldn't break expressions
7641 * that are of the form "vlan and ..." or "vlan N and ...",
7642 * which I suspect are the most common expressions involving
7643 * "vlan". "vlan or ..." doesn't necessarily do what the user
7644 * would really want, now, as all the "or ..." tests would
7645 * be done assuming a VLAN, even though the "or" could be viewed
7646 * as meaning "or, if this isn't a VLAN packet...".
7653 /* check for VLAN */
7654 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7655 (bpf_int32
)ETHERTYPE_8021Q
);
7657 /* If a specific VLAN is requested, check VLAN id */
7658 if (vlan_num
>= 0) {
7659 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7660 (bpf_int32
)vlan_num
, 0x0fff);
7674 bpf_error("no VLAN support for data link type %d",
7689 struct block
*b0
,*b1
;
7692 * Change the offsets to point to the type and data fields within
7693 * the MPLS packet. Just increment the offsets, so that we
7694 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7695 * capture packets with an outer label of 100000 and an inner
7698 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7702 if (label_stack_depth
> 0) {
7703 /* just match the bottom-of-stack bit clear */
7704 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7707 * Indicate that we're checking MPLS-encapsulated headers,
7708 * to make sure higher level code generators don't try to
7709 * match against IP-related protocols such as Q_ARP, Q_RARP
7714 case DLT_C_HDLC
: /* fall through */
7716 b0
= gen_linktype(ETHERTYPE_MPLS
);
7720 b0
= gen_linktype(PPP_MPLS_UCAST
);
7723 /* FIXME add other DLT_s ...
7724 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7725 * leave it for now */
7728 bpf_error("no MPLS support for data link type %d",
7736 /* If a specific MPLS label is requested, check it */
7737 if (label_num
>= 0) {
7738 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7739 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7740 0xfffff000); /* only compare the first 20 bits */
7747 label_stack_depth
++;
7752 * Support PPPOE discovery and session.
7757 /* check for PPPoE discovery */
7758 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7767 * Test against the PPPoE session link-layer type.
7769 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7772 * Change the offsets to point to the type and data fields within
7773 * the PPP packet, and note that this is PPPoE rather than
7776 * XXX - this is a bit of a kludge. If we were to split the
7777 * compiler into a parser that parses an expression and
7778 * generates an expression tree, and a code generator that
7779 * takes an expression tree (which could come from our
7780 * parser or from some other parser) and generates BPF code,
7781 * we could perhaps make the offsets parameters of routines
7782 * and, in the handler for an "AND" node, pass to subnodes
7783 * other than the PPPoE node the adjusted offsets.
7785 * This would mean that "pppoes" would, instead of changing the
7786 * behavior of *all* tests after it, change only the behavior
7787 * of tests ANDed with it. That would change the documented
7788 * semantics of "pppoes", which might break some expressions.
7789 * However, it would mean that "(pppoes and ip) or ip" would check
7790 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7791 * checking only for VLAN-encapsulated IP, so that could still
7792 * be considered worth doing; it wouldn't break expressions
7793 * that are of the form "pppoes and ..." which I suspect are the
7794 * most common expressions involving "pppoes". "pppoes or ..."
7795 * doesn't necessarily do what the user would really want, now,
7796 * as all the "or ..." tests would be done assuming PPPoE, even
7797 * though the "or" could be viewed as meaning "or, if this isn't
7798 * a PPPoE packet...".
7800 orig_linktype
= off_linktype
; /* save original values */
7805 * The "network-layer" protocol is PPPoE, which has a 6-byte
7806 * PPPoE header, followed by a PPP packet.
7808 * There is no HDLC encapsulation for the PPP packet (it's
7809 * encapsulated in PPPoES instead), so the link-layer type
7810 * starts at the first byte of the PPP packet. For PPPoE,
7811 * that offset is relative to the beginning of the total
7812 * link-layer payload, including any 802.2 LLC header, so
7813 * it's 6 bytes past off_nl.
7815 off_linktype
= off_nl
+ 6;
7818 * The network-layer offsets are relative to the beginning
7819 * of the MAC-layer payload; that's past the 6-byte
7820 * PPPoE header and the 2-byte PPP header.
7823 off_nl_nosnap
= 6+2;
7829 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7841 bpf_error("'vpi' supported only on raw ATM");
7842 if (off_vpi
== (u_int
)-1)
7844 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7850 bpf_error("'vci' supported only on raw ATM");
7851 if (off_vci
== (u_int
)-1)
7853 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7858 if (off_proto
== (u_int
)-1)
7859 abort(); /* XXX - this isn't on FreeBSD */
7860 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7865 if (off_payload
== (u_int
)-1)
7867 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7868 0xffffffff, jtype
, reverse
, jvalue
);
7873 bpf_error("'callref' supported only on raw ATM");
7874 if (off_proto
== (u_int
)-1)
7876 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7877 jtype
, reverse
, jvalue
);
7887 gen_atmtype_abbrev(type
)
7890 struct block
*b0
, *b1
;
7895 /* Get all packets in Meta signalling Circuit */
7897 bpf_error("'metac' supported only on raw ATM");
7898 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7899 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7904 /* Get all packets in Broadcast Circuit*/
7906 bpf_error("'bcc' supported only on raw ATM");
7907 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7908 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7913 /* Get all cells in Segment OAM F4 circuit*/
7915 bpf_error("'oam4sc' supported only on raw ATM");
7916 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7917 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7922 /* Get all cells in End-to-End OAM F4 Circuit*/
7924 bpf_error("'oam4ec' supported only on raw ATM");
7925 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7926 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7931 /* Get all packets in connection Signalling Circuit */
7933 bpf_error("'sc' supported only on raw ATM");
7934 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7935 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7940 /* Get all packets in ILMI Circuit */
7942 bpf_error("'ilmic' supported only on raw ATM");
7943 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7944 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7949 /* Get all LANE packets */
7951 bpf_error("'lane' supported only on raw ATM");
7952 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7955 * Arrange that all subsequent tests assume LANE
7956 * rather than LLC-encapsulated packets, and set
7957 * the offsets appropriately for LANE-encapsulated
7960 * "off_mac" is the offset of the Ethernet header,
7961 * which is 2 bytes past the ATM pseudo-header
7962 * (skipping the pseudo-header and 2-byte LE Client
7963 * field). The other offsets are Ethernet offsets
7964 * relative to "off_mac".
7967 off_mac
= off_payload
+ 2; /* MAC header */
7968 off_linktype
= off_mac
+ 12;
7969 off_macpl
= off_mac
+ 14; /* Ethernet */
7970 off_nl
= 0; /* Ethernet II */
7971 off_nl_nosnap
= 3; /* 802.3+802.2 */
7975 /* Get all LLC-encapsulated packets */
7977 bpf_error("'llc' supported only on raw ATM");
7978 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7989 * Filtering for MTP2 messages based on li value
7990 * FISU, length is null
7991 * LSSU, length is 1 or 2
7992 * MSU, length is 3 or more
7995 gen_mtp2type_abbrev(type
)
7998 struct block
*b0
, *b1
;
8003 if ( (linktype
!= DLT_MTP2
) &&
8004 (linktype
!= DLT_ERF
) &&
8005 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8006 bpf_error("'fisu' supported only on MTP2");
8007 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8008 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8012 if ( (linktype
!= DLT_MTP2
) &&
8013 (linktype
!= DLT_ERF
) &&
8014 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8015 bpf_error("'lssu' supported only on MTP2");
8016 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8017 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8022 if ( (linktype
!= DLT_MTP2
) &&
8023 (linktype
!= DLT_ERF
) &&
8024 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8025 bpf_error("'msu' supported only on MTP2");
8026 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8036 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8043 bpf_u_int32 val1
, val2
, val3
;
8045 switch (mtp3field
) {
8048 if (off_sio
== (u_int
)-1)
8049 bpf_error("'sio' supported only on SS7");
8050 /* sio coded on 1 byte so max value 255 */
8052 bpf_error("sio value %u too big; max value = 255",
8054 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8055 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8059 if (off_opc
== (u_int
)-1)
8060 bpf_error("'opc' supported only on SS7");
8061 /* opc coded on 14 bits so max value 16383 */
8063 bpf_error("opc value %u too big; max value = 16383",
8065 /* the following instructions are made to convert jvalue
8066 * to the form used to write opc in an ss7 message*/
8067 val1
= jvalue
& 0x00003c00;
8069 val2
= jvalue
& 0x000003fc;
8071 val3
= jvalue
& 0x00000003;
8073 jvalue
= val1
+ val2
+ val3
;
8074 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8075 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8079 if (off_dpc
== (u_int
)-1)
8080 bpf_error("'dpc' supported only on SS7");
8081 /* dpc coded on 14 bits so max value 16383 */
8083 bpf_error("dpc value %u too big; max value = 16383",
8085 /* the following instructions are made to convert jvalue
8086 * to the forme used to write dpc in an ss7 message*/
8087 val1
= jvalue
& 0x000000ff;
8089 val2
= jvalue
& 0x00003f00;
8091 jvalue
= val1
+ val2
;
8092 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8093 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8097 if (off_sls
== (u_int
)-1)
8098 bpf_error("'sls' supported only on SS7");
8099 /* sls coded on 4 bits so max value 15 */
8101 bpf_error("sls value %u too big; max value = 15",
8103 /* the following instruction is made to convert jvalue
8104 * to the forme used to write sls in an ss7 message*/
8105 jvalue
= jvalue
<< 4;
8106 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8107 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8116 static struct block
*
8117 gen_msg_abbrev(type
)
8123 * Q.2931 signalling protocol messages for handling virtual circuits
8124 * establishment and teardown
8129 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8133 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8137 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8141 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8145 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8148 case A_RELEASE_DONE
:
8149 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8159 gen_atmmulti_abbrev(type
)
8162 struct block
*b0
, *b1
;
8168 bpf_error("'oam' supported only on raw ATM");
8169 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8174 bpf_error("'oamf4' supported only on raw ATM");
8176 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8177 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8179 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8185 * Get Q.2931 signalling messages for switched
8186 * virtual connection
8189 bpf_error("'connectmsg' supported only on raw ATM");
8190 b0
= gen_msg_abbrev(A_SETUP
);
8191 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8193 b0
= gen_msg_abbrev(A_CONNECT
);
8195 b0
= gen_msg_abbrev(A_CONNECTACK
);
8197 b0
= gen_msg_abbrev(A_RELEASE
);
8199 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8201 b0
= gen_atmtype_abbrev(A_SC
);
8207 bpf_error("'metaconnect' supported only on raw ATM");
8208 b0
= gen_msg_abbrev(A_SETUP
);
8209 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8211 b0
= gen_msg_abbrev(A_CONNECT
);
8213 b0
= gen_msg_abbrev(A_RELEASE
);
8215 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8217 b0
= gen_atmtype_abbrev(A_METAC
);