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.15 2007-12-29 23:15:43 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 bpf_error("unknown data link type %d", linktype
);
1496 * Load a value relative to the beginning of the link-layer header.
1497 * The link-layer header doesn't necessarily begin at the beginning
1498 * of the packet data; there might be a variable-length prefix containing
1499 * radio information.
1501 static struct slist
*
1502 gen_load_llrel(offset
, size
)
1505 struct slist
*s
, *s2
;
1507 s
= gen_llprefixlen();
1510 * If "s" is non-null, it has code to arrange that the X register
1511 * contains the length of the prefix preceding the link-layer
1514 * Otherwise, the length of the prefix preceding the link-layer
1515 * header is "off_ll".
1519 * There's a variable-length prefix preceding the
1520 * link-layer header. "s" points to a list of statements
1521 * that put the length of that prefix into the X register.
1522 * do an indirect load, to use the X register as an offset.
1524 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1529 * There is no variable-length header preceding the
1530 * link-layer header; add in off_ll, which, if there's
1531 * a fixed-length header preceding the link-layer header,
1532 * is the length of that header.
1534 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1535 s
->s
.k
= offset
+ off_ll
;
1541 * Load a value relative to the beginning of the MAC-layer payload.
1543 static struct slist
*
1544 gen_load_macplrel(offset
, size
)
1547 struct slist
*s
, *s2
;
1549 s
= gen_off_macpl();
1552 * If s is non-null, the offset of the MAC-layer payload is
1553 * variable, and s points to a list of instructions that
1554 * arrange that the X register contains that offset.
1556 * Otherwise, the offset of the MAC-layer payload is constant,
1557 * and is in off_macpl.
1561 * The offset of the MAC-layer payload is in the X
1562 * register. Do an indirect load, to use the X register
1565 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1570 * The offset of the MAC-layer payload is constant,
1571 * and is in off_macpl; load the value at that offset
1572 * plus the specified offset.
1574 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1575 s
->s
.k
= off_macpl
+ offset
;
1581 * Load a value relative to the beginning of the specified header.
1583 static struct slist
*
1584 gen_load_a(offrel
, offset
, size
)
1585 enum e_offrel offrel
;
1588 struct slist
*s
, *s2
;
1593 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1598 s
= gen_load_llrel(offset
, size
);
1602 s
= gen_load_macplrel(offset
, size
);
1606 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1610 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1615 * Load the X register with the length of the IPv4 header
1616 * (plus the offset of the link-layer header, if it's
1617 * preceded by a variable-length header such as a radio
1618 * header), in bytes.
1620 s
= gen_loadx_iphdrlen();
1623 * Load the item at {offset of the MAC-layer payload} +
1624 * {offset, relative to the start of the MAC-layer
1625 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1626 * {specified offset}.
1628 * (If the offset of the MAC-layer payload is variable,
1629 * it's included in the value in the X register, and
1632 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1633 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1638 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1649 * Generate code to load into the X register the sum of the length of
1650 * the IPv4 header and any variable-length header preceding the link-layer
1653 static struct slist
*
1654 gen_loadx_iphdrlen()
1656 struct slist
*s
, *s2
;
1658 s
= gen_off_macpl();
1661 * There's a variable-length prefix preceding the
1662 * link-layer header, or the link-layer header is itself
1663 * variable-length. "s" points to a list of statements
1664 * that put the offset of the MAC-layer payload into
1667 * The 4*([k]&0xf) addressing mode can't be used, as we
1668 * don't have a constant offset, so we have to load the
1669 * value in question into the A register and add to it
1670 * the value from the X register.
1672 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1675 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1678 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1683 * The A register now contains the length of the
1684 * IP header. We need to add to it the offset of
1685 * the MAC-layer payload, which is still in the X
1686 * register, and move the result into the X register.
1688 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1689 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1692 * There is no variable-length header preceding the
1693 * link-layer header, and the link-layer header is
1694 * fixed-length; load the length of the IPv4 header,
1695 * which is at an offset of off_nl from the beginning
1696 * of the MAC-layer payload, and thus at an offset
1697 * of off_mac_pl + off_nl from the beginning of the
1700 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1701 s
->s
.k
= off_macpl
+ off_nl
;
1706 static struct block
*
1713 s
= new_stmt(BPF_LD
|BPF_IMM
);
1715 b
= new_block(JMP(BPF_JEQ
));
1721 static inline struct block
*
1724 return gen_uncond(1);
1727 static inline struct block
*
1730 return gen_uncond(0);
1734 * Byte-swap a 32-bit number.
1735 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1736 * big-endian platforms.)
1738 #define SWAPLONG(y) \
1739 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1742 * Generate code to match a particular packet type.
1744 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1745 * value, if <= ETHERMTU. We use that to determine whether to
1746 * match the type/length field or to check the type/length field for
1747 * a value <= ETHERMTU to see whether it's a type field and then do
1748 * the appropriate test.
1750 static struct block
*
1751 gen_ether_linktype(proto
)
1754 struct block
*b0
, *b1
;
1760 case LLCSAP_NETBEUI
:
1762 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1763 * so we check the DSAP and SSAP.
1765 * LLCSAP_IP checks for IP-over-802.2, rather
1766 * than IP-over-Ethernet or IP-over-SNAP.
1768 * XXX - should we check both the DSAP and the
1769 * SSAP, like this, or should we check just the
1770 * DSAP, as we do for other types <= ETHERMTU
1771 * (i.e., other SAP values)?
1773 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1775 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1776 ((proto
<< 8) | proto
));
1784 * Ethernet_II frames, which are Ethernet
1785 * frames with a frame type of ETHERTYPE_IPX;
1787 * Ethernet_802.3 frames, which are 802.3
1788 * frames (i.e., the type/length field is
1789 * a length field, <= ETHERMTU, rather than
1790 * a type field) with the first two bytes
1791 * after the Ethernet/802.3 header being
1794 * Ethernet_802.2 frames, which are 802.3
1795 * frames with an 802.2 LLC header and
1796 * with the IPX LSAP as the DSAP in the LLC
1799 * Ethernet_SNAP frames, which are 802.3
1800 * frames with an LLC header and a SNAP
1801 * header and with an OUI of 0x000000
1802 * (encapsulated Ethernet) and a protocol
1803 * ID of ETHERTYPE_IPX in the SNAP header.
1805 * XXX - should we generate the same code both
1806 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1810 * This generates code to check both for the
1811 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1813 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1814 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1818 * Now we add code to check for SNAP frames with
1819 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1821 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1825 * Now we generate code to check for 802.3
1826 * frames in general.
1828 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1832 * Now add the check for 802.3 frames before the
1833 * check for Ethernet_802.2 and Ethernet_802.3,
1834 * as those checks should only be done on 802.3
1835 * frames, not on Ethernet frames.
1840 * Now add the check for Ethernet_II frames, and
1841 * do that before checking for the other frame
1844 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1845 (bpf_int32
)ETHERTYPE_IPX
);
1849 case ETHERTYPE_ATALK
:
1850 case ETHERTYPE_AARP
:
1852 * EtherTalk (AppleTalk protocols on Ethernet link
1853 * layer) may use 802.2 encapsulation.
1857 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1858 * we check for an Ethernet type field less than
1859 * 1500, which means it's an 802.3 length field.
1861 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1865 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1866 * SNAP packets with an organization code of
1867 * 0x080007 (Apple, for Appletalk) and a protocol
1868 * type of ETHERTYPE_ATALK (Appletalk).
1870 * 802.2-encapsulated ETHERTYPE_AARP packets are
1871 * SNAP packets with an organization code of
1872 * 0x000000 (encapsulated Ethernet) and a protocol
1873 * type of ETHERTYPE_AARP (Appletalk ARP).
1875 if (proto
== ETHERTYPE_ATALK
)
1876 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1877 else /* proto == ETHERTYPE_AARP */
1878 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1882 * Check for Ethernet encapsulation (Ethertalk
1883 * phase 1?); we just check for the Ethernet
1886 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1892 if (proto
<= ETHERMTU
) {
1894 * This is an LLC SAP value, so the frames
1895 * that match would be 802.2 frames.
1896 * Check that the frame is an 802.2 frame
1897 * (i.e., that the length/type field is
1898 * a length field, <= ETHERMTU) and
1899 * then check the DSAP.
1901 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1903 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1909 * This is an Ethernet type, so compare
1910 * the length/type field with it (if
1911 * the frame is an 802.2 frame, the length
1912 * field will be <= ETHERMTU, and, as
1913 * "proto" is > ETHERMTU, this test
1914 * will fail and the frame won't match,
1915 * which is what we want).
1917 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1924 * Generate code to match a particular packet type.
1926 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1927 * value, if <= ETHERMTU. We use that to determine whether to
1928 * match the type field or to check the type field for the special
1929 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1931 static struct block
*
1932 gen_linux_sll_linktype(proto
)
1935 struct block
*b0
, *b1
;
1941 case LLCSAP_NETBEUI
:
1943 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1944 * so we check the DSAP and SSAP.
1946 * LLCSAP_IP checks for IP-over-802.2, rather
1947 * than IP-over-Ethernet or IP-over-SNAP.
1949 * XXX - should we check both the DSAP and the
1950 * SSAP, like this, or should we check just the
1951 * DSAP, as we do for other types <= ETHERMTU
1952 * (i.e., other SAP values)?
1954 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1955 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1956 ((proto
<< 8) | proto
));
1962 * Ethernet_II frames, which are Ethernet
1963 * frames with a frame type of ETHERTYPE_IPX;
1965 * Ethernet_802.3 frames, which have a frame
1966 * type of LINUX_SLL_P_802_3;
1968 * Ethernet_802.2 frames, which are 802.3
1969 * frames with an 802.2 LLC header (i.e, have
1970 * a frame type of LINUX_SLL_P_802_2) and
1971 * with the IPX LSAP as the DSAP in the LLC
1974 * Ethernet_SNAP frames, which are 802.3
1975 * frames with an LLC header and a SNAP
1976 * header and with an OUI of 0x000000
1977 * (encapsulated Ethernet) and a protocol
1978 * ID of ETHERTYPE_IPX in the SNAP header.
1980 * First, do the checks on LINUX_SLL_P_802_2
1981 * frames; generate the check for either
1982 * Ethernet_802.2 or Ethernet_SNAP frames, and
1983 * then put a check for LINUX_SLL_P_802_2 frames
1986 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1987 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
1989 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1993 * Now check for 802.3 frames and OR that with
1994 * the previous test.
1996 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2000 * Now add the check for Ethernet_II frames, and
2001 * do that before checking for the other frame
2004 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2005 (bpf_int32
)ETHERTYPE_IPX
);
2009 case ETHERTYPE_ATALK
:
2010 case ETHERTYPE_AARP
:
2012 * EtherTalk (AppleTalk protocols on Ethernet link
2013 * layer) may use 802.2 encapsulation.
2017 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2018 * we check for the 802.2 protocol type in the
2019 * "Ethernet type" field.
2021 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2024 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2025 * SNAP packets with an organization code of
2026 * 0x080007 (Apple, for Appletalk) and a protocol
2027 * type of ETHERTYPE_ATALK (Appletalk).
2029 * 802.2-encapsulated ETHERTYPE_AARP packets are
2030 * SNAP packets with an organization code of
2031 * 0x000000 (encapsulated Ethernet) and a protocol
2032 * type of ETHERTYPE_AARP (Appletalk ARP).
2034 if (proto
== ETHERTYPE_ATALK
)
2035 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2036 else /* proto == ETHERTYPE_AARP */
2037 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2041 * Check for Ethernet encapsulation (Ethertalk
2042 * phase 1?); we just check for the Ethernet
2045 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2051 if (proto
<= ETHERMTU
) {
2053 * This is an LLC SAP value, so the frames
2054 * that match would be 802.2 frames.
2055 * Check for the 802.2 protocol type
2056 * in the "Ethernet type" field, and
2057 * then check the DSAP.
2059 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2061 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2067 * This is an Ethernet type, so compare
2068 * the length/type field with it (if
2069 * the frame is an 802.2 frame, the length
2070 * field will be <= ETHERMTU, and, as
2071 * "proto" is > ETHERMTU, this test
2072 * will fail and the frame won't match,
2073 * which is what we want).
2075 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2081 static struct slist
*
2082 gen_load_prism_llprefixlen()
2084 struct slist
*s1
, *s2
;
2085 struct slist
*sjeq_avs_cookie
;
2086 struct slist
*sjcommon
;
2089 * This code is not compatible with the optimizer, as
2090 * we are generating jmp instructions within a normal
2091 * slist of instructions
2096 * Generate code to load the length of the radio header into
2097 * the register assigned to hold that length, if one has been
2098 * assigned. (If one hasn't been assigned, no code we've
2099 * generated uses that prefix, so we don't need to generate any
2102 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2103 * or always use the AVS header rather than the Prism header.
2104 * We load a 4-byte big-endian value at the beginning of the
2105 * raw packet data, and see whether, when masked with 0xFFFFF000,
2106 * it's equal to 0x80211000. If so, that indicates that it's
2107 * an AVS header (the masked-out bits are the version number).
2108 * Otherwise, it's a Prism header.
2110 * XXX - the Prism header is also, in theory, variable-length,
2111 * but no known software generates headers that aren't 144
2114 if (reg_off_ll
!= -1) {
2118 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2122 * AND it with 0xFFFFF000.
2124 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2125 s2
->s
.k
= 0xFFFFF000;
2129 * Compare with 0x80211000.
2131 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2132 sjeq_avs_cookie
->s
.k
= 0x80211000;
2133 sappend(s1
, sjeq_avs_cookie
);
2138 * The 4 bytes at an offset of 4 from the beginning of
2139 * the AVS header are the length of the AVS header.
2140 * That field is big-endian.
2142 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2145 sjeq_avs_cookie
->s
.jt
= s2
;
2148 * Now jump to the code to allocate a register
2149 * into which to save the header length and
2150 * store the length there. (The "jump always"
2151 * instruction needs to have the k field set;
2152 * it's added to the PC, so, as we're jumping
2153 * over a single instruction, it should be 1.)
2155 sjcommon
= new_stmt(JMP(BPF_JA
));
2157 sappend(s1
, sjcommon
);
2160 * Now for the code that handles the Prism header.
2161 * Just load the length of the Prism header (144)
2162 * into the A register. Have the test for an AVS
2163 * header branch here if we don't have an AVS header.
2165 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2168 sjeq_avs_cookie
->s
.jf
= s2
;
2171 * Now allocate a register to hold that value and store
2172 * it. The code for the AVS header will jump here after
2173 * loading the length of the AVS header.
2175 s2
= new_stmt(BPF_ST
);
2176 s2
->s
.k
= reg_off_ll
;
2178 sjcommon
->s
.jf
= s2
;
2181 * Now move it into the X register.
2183 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2191 static struct slist
*
2192 gen_load_avs_llprefixlen()
2194 struct slist
*s1
, *s2
;
2197 * Generate code to load the length of the AVS header into
2198 * the register assigned to hold that length, if one has been
2199 * assigned. (If one hasn't been assigned, no code we've
2200 * generated uses that prefix, so we don't need to generate any
2203 if (reg_off_ll
!= -1) {
2205 * The 4 bytes at an offset of 4 from the beginning of
2206 * the AVS header are the length of the AVS header.
2207 * That field is big-endian.
2209 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2213 * Now allocate a register to hold that value and store
2216 s2
= new_stmt(BPF_ST
);
2217 s2
->s
.k
= reg_off_ll
;
2221 * Now move it into the X register.
2223 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2231 static struct slist
*
2232 gen_load_radiotap_llprefixlen()
2234 struct slist
*s1
, *s2
;
2237 * Generate code to load the length of the radiotap header into
2238 * the register assigned to hold that length, if one has been
2239 * assigned. (If one hasn't been assigned, no code we've
2240 * generated uses that prefix, so we don't need to generate any
2243 if (reg_off_ll
!= -1) {
2245 * The 2 bytes at offsets of 2 and 3 from the beginning
2246 * of the radiotap header are the length of the radiotap
2247 * header; unfortunately, it's little-endian, so we have
2248 * to load it a byte at a time and construct the value.
2252 * Load the high-order byte, at an offset of 3, shift it
2253 * left a byte, and put the result in the X register.
2255 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2257 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2260 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2264 * Load the next byte, at an offset of 2, and OR the
2265 * value from the X register into it.
2267 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2270 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2274 * Now allocate a register to hold that value and store
2277 s2
= new_stmt(BPF_ST
);
2278 s2
->s
.k
= reg_off_ll
;
2282 * Now move it into the X register.
2284 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2293 * At the moment we treat PPI as normal Radiotap encoded
2294 * packets. The difference is in the function that generates
2295 * the code at the beginning to compute the header length.
2296 * Since this code generator of PPI supports bare 802.11
2297 * encapsulation only (i.e. the encapsulated DLT should be
2298 * DLT_IEEE802_11) we generate code to check for this too;
2299 * that's done in finish_parse().
2301 static struct slist
*
2302 gen_load_ppi_llprefixlen()
2304 struct slist
*s1
, *s2
;
2307 * Generate code to load the length of the radiotap header
2308 * into the register assigned to hold that length, if one has
2311 if (reg_off_ll
!= -1) {
2313 * The 2 bytes at offsets of 2 and 3 from the beginning
2314 * of the radiotap header are the length of the radiotap
2315 * header; unfortunately, it's little-endian, so we have
2316 * to load it a byte at a time and construct the value.
2320 * Load the high-order byte, at an offset of 3, shift it
2321 * left a byte, and put the result in the X register.
2323 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2325 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2328 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2332 * Load the next byte, at an offset of 2, and OR the
2333 * value from the X register into it.
2335 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2338 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2342 * Now allocate a register to hold that value and store
2345 s2
= new_stmt(BPF_ST
);
2346 s2
->s
.k
= reg_off_ll
;
2350 * Now move it into the X register.
2352 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2361 * Load a value relative to the beginning of the link-layer header after the 802.11
2362 * header, i.e. LLC_SNAP.
2363 * The link-layer header doesn't necessarily begin at the beginning
2364 * of the packet data; there might be a variable-length prefix containing
2365 * radio information.
2367 static struct slist
*
2368 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2371 struct slist
*sjset_data_frame_1
;
2372 struct slist
*sjset_data_frame_2
;
2373 struct slist
*sjset_qos
;
2374 struct slist
*sjset_radiotap_flags
;
2375 struct slist
*sjset_radiotap_tsft
;
2376 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2377 struct slist
*s_roundup
;
2379 if (reg_off_macpl
== -1) {
2381 * No register has been assigned to the offset of
2382 * the MAC-layer payload, which means nobody needs
2383 * it; don't bother computing it - just return
2384 * what we already have.
2390 * This code is not compatible with the optimizer, as
2391 * we are generating jmp instructions within a normal
2392 * slist of instructions
2397 * If "s" is non-null, it has code to arrange that the X register
2398 * contains the length of the prefix preceding the link-layer
2401 * Otherwise, the length of the prefix preceding the link-layer
2402 * header is "off_ll".
2406 * There is no variable-length header preceding the
2407 * link-layer header.
2409 * Load the length of the fixed-length prefix preceding
2410 * the link-layer header (if any) into the X register,
2411 * and store it in the reg_off_macpl register.
2412 * That length is off_ll.
2414 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2419 * The X register contains the offset of the beginning of the
2420 * link-layer header; add 24, which is the minimum length
2421 * of the MAC header for a data frame, to that, and store it
2422 * in reg_off_macpl, and then load the Frame Control field,
2423 * which is at the offset in the X register, with an indexed load.
2425 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2427 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2430 s2
= new_stmt(BPF_ST
);
2431 s2
->s
.k
= reg_off_macpl
;
2434 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2439 * Check the Frame Control field to see if this is a data frame;
2440 * a data frame has the 0x08 bit (b3) in that field set and the
2441 * 0x04 bit (b2) clear.
2443 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2444 sjset_data_frame_1
->s
.k
= 0x08;
2445 sappend(s
, sjset_data_frame_1
);
2448 * If b3 is set, test b2, otherwise go to the first statement of
2449 * the rest of the program.
2451 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2452 sjset_data_frame_2
->s
.k
= 0x04;
2453 sappend(s
, sjset_data_frame_2
);
2454 sjset_data_frame_1
->s
.jf
= snext
;
2457 * If b2 is not set, this is a data frame; test the QoS bit.
2458 * Otherwise, go to the first statement of the rest of the
2461 sjset_data_frame_2
->s
.jt
= snext
;
2462 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2463 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2464 sappend(s
, sjset_qos
);
2467 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2469 * Otherwise, go to the first statement of the rest of the
2472 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2473 s2
->s
.k
= reg_off_macpl
;
2475 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2478 s2
= new_stmt(BPF_ST
);
2479 s2
->s
.k
= reg_off_macpl
;
2483 * If we have a radiotap header, look at it to see whether
2484 * there's Atheros padding between the MAC-layer header
2487 * Note: all of the fields in the radiotap header are
2488 * little-endian, so we byte-swap all of the values
2489 * we test against, as they will be loaded as big-endian
2492 if (linktype
== DLT_IEEE802_11_RADIO
) {
2494 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2495 * in the presence flag?
2497 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2501 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2502 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2503 sappend(s
, sjset_radiotap_flags
);
2506 * If not, skip all of this.
2508 sjset_radiotap_flags
->s
.jf
= snext
;
2511 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2513 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2514 new_stmt(JMP(BPF_JSET
));
2515 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2516 sappend(s
, sjset_radiotap_tsft
);
2519 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2520 * at an offset of 16 from the beginning of the raw packet
2521 * data (8 bytes for the radiotap header and 8 bytes for
2524 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2527 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2531 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2532 sjset_tsft_datapad
->s
.k
= 0x20;
2533 sappend(s
, sjset_tsft_datapad
);
2536 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2537 * at an offset of 8 from the beginning of the raw packet
2538 * data (8 bytes for the radiotap header).
2540 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2543 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2547 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2548 sjset_notsft_datapad
->s
.k
= 0x20;
2549 sappend(s
, sjset_notsft_datapad
);
2552 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2553 * set, round the length of the 802.11 header to
2554 * a multiple of 4. Do that by adding 3 and then
2555 * dividing by and multiplying by 4, which we do by
2558 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2559 s_roundup
->s
.k
= reg_off_macpl
;
2560 sappend(s
, s_roundup
);
2561 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2564 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2567 s2
= new_stmt(BPF_ST
);
2568 s2
->s
.k
= reg_off_macpl
;
2571 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2572 sjset_tsft_datapad
->s
.jf
= snext
;
2573 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2574 sjset_notsft_datapad
->s
.jf
= snext
;
2576 sjset_qos
->s
.jf
= snext
;
2582 insert_compute_vloffsets(b
)
2588 * For link-layer types that have a variable-length header
2589 * preceding the link-layer header, generate code to load
2590 * the offset of the link-layer header into the register
2591 * assigned to that offset, if any.
2595 case DLT_PRISM_HEADER
:
2596 s
= gen_load_prism_llprefixlen();
2599 case DLT_IEEE802_11_RADIO_AVS
:
2600 s
= gen_load_avs_llprefixlen();
2603 case DLT_IEEE802_11_RADIO
:
2604 s
= gen_load_radiotap_llprefixlen();
2608 s
= gen_load_ppi_llprefixlen();
2617 * For link-layer types that have a variable-length link-layer
2618 * header, generate code to load the offset of the MAC-layer
2619 * payload into the register assigned to that offset, if any.
2623 case DLT_IEEE802_11
:
2624 case DLT_PRISM_HEADER
:
2625 case DLT_IEEE802_11_RADIO_AVS
:
2626 case DLT_IEEE802_11_RADIO
:
2628 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2633 * If we have any offset-loading code, append all the
2634 * existing statements in the block to those statements,
2635 * and make the resulting list the list of statements
2639 sappend(s
, b
->stmts
);
2644 static struct block
*
2645 gen_ppi_dlt_check(void)
2647 struct slist
*s_load_dlt
;
2650 if (linktype
== DLT_PPI
)
2652 /* Create the statements that check for the DLT
2654 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2655 s_load_dlt
->s
.k
= 4;
2657 b
= new_block(JMP(BPF_JEQ
));
2659 b
->stmts
= s_load_dlt
;
2660 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2670 static struct slist
*
2671 gen_prism_llprefixlen(void)
2675 if (reg_off_ll
== -1) {
2677 * We haven't yet assigned a register for the length
2678 * of the radio header; allocate one.
2680 reg_off_ll
= alloc_reg();
2684 * Load the register containing the radio length
2685 * into the X register.
2687 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2688 s
->s
.k
= reg_off_ll
;
2692 static struct slist
*
2693 gen_avs_llprefixlen(void)
2697 if (reg_off_ll
== -1) {
2699 * We haven't yet assigned a register for the length
2700 * of the AVS header; allocate one.
2702 reg_off_ll
= alloc_reg();
2706 * Load the register containing the AVS length
2707 * into the X register.
2709 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2710 s
->s
.k
= reg_off_ll
;
2714 static struct slist
*
2715 gen_radiotap_llprefixlen(void)
2719 if (reg_off_ll
== -1) {
2721 * We haven't yet assigned a register for the length
2722 * of the radiotap header; allocate one.
2724 reg_off_ll
= alloc_reg();
2728 * Load the register containing the radiotap length
2729 * into the X register.
2731 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2732 s
->s
.k
= reg_off_ll
;
2737 * At the moment we treat PPI as normal Radiotap encoded
2738 * packets. The difference is in the function that generates
2739 * the code at the beginning to compute the header length.
2740 * Since this code generator of PPI supports bare 802.11
2741 * encapsulation only (i.e. the encapsulated DLT should be
2742 * DLT_IEEE802_11) we generate code to check for this too.
2744 static struct slist
*
2745 gen_ppi_llprefixlen(void)
2749 if (reg_off_ll
== -1) {
2751 * We haven't yet assigned a register for the length
2752 * of the radiotap header; allocate one.
2754 reg_off_ll
= alloc_reg();
2758 * Load the register containing the PPI length
2759 * into the X register.
2761 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2762 s
->s
.k
= reg_off_ll
;
2767 * Generate code to compute the link-layer header length, if necessary,
2768 * putting it into the X register, and to return either a pointer to a
2769 * "struct slist" for the list of statements in that code, or NULL if
2770 * no code is necessary.
2772 static struct slist
*
2773 gen_llprefixlen(void)
2777 case DLT_PRISM_HEADER
:
2778 return gen_prism_llprefixlen();
2780 case DLT_IEEE802_11_RADIO_AVS
:
2781 return gen_avs_llprefixlen();
2783 case DLT_IEEE802_11_RADIO
:
2784 return gen_radiotap_llprefixlen();
2787 return gen_ppi_llprefixlen();
2795 * Generate code to load the register containing the offset of the
2796 * MAC-layer payload into the X register; if no register for that offset
2797 * has been allocated, allocate it first.
2799 static struct slist
*
2804 if (off_macpl_is_variable
) {
2805 if (reg_off_macpl
== -1) {
2807 * We haven't yet assigned a register for the offset
2808 * of the MAC-layer payload; allocate one.
2810 reg_off_macpl
= alloc_reg();
2814 * Load the register containing the offset of the MAC-layer
2815 * payload into the X register.
2817 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2818 s
->s
.k
= reg_off_macpl
;
2822 * That offset isn't variable, so we don't need to
2823 * generate any code.
2830 * Map an Ethernet type to the equivalent PPP type.
2833 ethertype_to_ppptype(proto
)
2843 case ETHERTYPE_IPV6
:
2852 case ETHERTYPE_ATALK
:
2866 * I'm assuming the "Bridging PDU"s that go
2867 * over PPP are Spanning Tree Protocol
2881 * Generate code to match a particular packet type by matching the
2882 * link-layer type field or fields in the 802.2 LLC header.
2884 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2885 * value, if <= ETHERMTU.
2887 static struct block
*
2891 struct block
*b0
, *b1
, *b2
;
2893 /* are we checking MPLS-encapsulated packets? */
2894 if (label_stack_depth
> 0) {
2898 /* FIXME add other L3 proto IDs */
2899 return gen_mpls_linktype(Q_IP
);
2901 case ETHERTYPE_IPV6
:
2903 /* FIXME add other L3 proto IDs */
2904 return gen_mpls_linktype(Q_IPV6
);
2907 bpf_error("unsupported protocol over mpls");
2913 * Are we testing PPPoE packets?
2917 * The PPPoE session header is part of the
2918 * MAC-layer payload, so all references
2919 * should be relative to the beginning of
2924 * We use Ethernet protocol types inside libpcap;
2925 * map them to the corresponding PPP protocol types.
2927 proto
= ethertype_to_ppptype(proto
);
2928 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2934 return gen_ether_linktype(proto
);
2942 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2946 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2953 case DLT_IEEE802_11
:
2954 case DLT_PRISM_HEADER
:
2955 case DLT_IEEE802_11_RADIO_AVS
:
2956 case DLT_IEEE802_11_RADIO
:
2959 * Check that we have a data frame.
2961 b0
= gen_check_802_11_data_frame();
2964 * Now check for the specified link-layer type.
2966 b1
= gen_llc_linktype(proto
);
2974 * XXX - check for asynchronous frames, as per RFC 1103.
2976 return gen_llc_linktype(proto
);
2982 * XXX - check for LLC PDUs, as per IEEE 802.5.
2984 return gen_llc_linktype(proto
);
2988 case DLT_ATM_RFC1483
:
2990 case DLT_IP_OVER_FC
:
2991 return gen_llc_linktype(proto
);
2997 * If "is_lane" is set, check for a LANE-encapsulated
2998 * version of this protocol, otherwise check for an
2999 * LLC-encapsulated version of this protocol.
3001 * We assume LANE means Ethernet, not Token Ring.
3005 * Check that the packet doesn't begin with an
3006 * LE Control marker. (We've already generated
3009 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3014 * Now generate an Ethernet test.
3016 b1
= gen_ether_linktype(proto
);
3021 * Check for LLC encapsulation and then check the
3024 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3025 b1
= gen_llc_linktype(proto
);
3033 return gen_linux_sll_linktype(proto
);
3038 case DLT_SLIP_BSDOS
:
3041 * These types don't provide any type field; packets
3042 * are always IPv4 or IPv6.
3044 * XXX - for IPv4, check for a version number of 4, and,
3045 * for IPv6, check for a version number of 6?
3050 /* Check for a version number of 4. */
3051 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3053 case ETHERTYPE_IPV6
:
3054 /* Check for a version number of 6. */
3055 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3059 return gen_false(); /* always false */
3066 case DLT_PPP_SERIAL
:
3069 * We use Ethernet protocol types inside libpcap;
3070 * map them to the corresponding PPP protocol types.
3072 proto
= ethertype_to_ppptype(proto
);
3073 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3079 * We use Ethernet protocol types inside libpcap;
3080 * map them to the corresponding PPP protocol types.
3086 * Also check for Van Jacobson-compressed IP.
3087 * XXX - do this for other forms of PPP?
3089 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3090 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3092 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3097 proto
= ethertype_to_ppptype(proto
);
3098 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3108 * For DLT_NULL, the link-layer header is a 32-bit
3109 * word containing an AF_ value in *host* byte order,
3110 * and for DLT_ENC, the link-layer header begins
3111 * with a 32-bit work containing an AF_ value in
3114 * In addition, if we're reading a saved capture file,
3115 * the host byte order in the capture may not be the
3116 * same as the host byte order on this machine.
3118 * For DLT_LOOP, the link-layer header is a 32-bit
3119 * word containing an AF_ value in *network* byte order.
3121 * XXX - AF_ values may, unfortunately, be platform-
3122 * dependent; for example, FreeBSD's AF_INET6 is 24
3123 * whilst NetBSD's and OpenBSD's is 26.
3125 * This means that, when reading a capture file, just
3126 * checking for our AF_INET6 value won't work if the
3127 * capture file came from another OS.
3136 case ETHERTYPE_IPV6
:
3143 * Not a type on which we support filtering.
3144 * XXX - support those that have AF_ values
3145 * #defined on this platform, at least?
3150 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3152 * The AF_ value is in host byte order, but
3153 * the BPF interpreter will convert it to
3154 * network byte order.
3156 * If this is a save file, and it's from a
3157 * machine with the opposite byte order to
3158 * ours, we byte-swap the AF_ value.
3160 * Then we run it through "htonl()", and
3161 * generate code to compare against the result.
3163 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3164 bpf_pcap
->sf
.swapped
)
3165 proto
= SWAPLONG(proto
);
3166 proto
= htonl(proto
);
3168 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3170 #ifdef HAVE_NET_PFVAR_H
3173 * af field is host byte order in contrast to the rest of
3176 if (proto
== ETHERTYPE_IP
)
3177 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3178 BPF_B
, (bpf_int32
)AF_INET
));
3180 else if (proto
== ETHERTYPE_IPV6
)
3181 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3182 BPF_B
, (bpf_int32
)AF_INET6
));
3188 #endif /* HAVE_NET_PFVAR_H */
3191 case DLT_ARCNET_LINUX
:
3193 * XXX should we check for first fragment if the protocol
3202 case ETHERTYPE_IPV6
:
3203 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3204 (bpf_int32
)ARCTYPE_INET6
));
3208 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3209 (bpf_int32
)ARCTYPE_IP
);
3210 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3211 (bpf_int32
)ARCTYPE_IP_OLD
);
3216 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3217 (bpf_int32
)ARCTYPE_ARP
);
3218 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3219 (bpf_int32
)ARCTYPE_ARP_OLD
);
3223 case ETHERTYPE_REVARP
:
3224 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3225 (bpf_int32
)ARCTYPE_REVARP
));
3227 case ETHERTYPE_ATALK
:
3228 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3229 (bpf_int32
)ARCTYPE_ATALK
));
3236 case ETHERTYPE_ATALK
:
3246 * XXX - assumes a 2-byte Frame Relay header with
3247 * DLCI and flags. What if the address is longer?
3253 * Check for the special NLPID for IP.
3255 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3258 case ETHERTYPE_IPV6
:
3260 * Check for the special NLPID for IPv6.
3262 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3267 * Check for several OSI protocols.
3269 * Frame Relay packets typically have an OSI
3270 * NLPID at the beginning; we check for each
3273 * What we check for is the NLPID and a frame
3274 * control field of UI, i.e. 0x03 followed
3277 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3278 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3279 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3291 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3293 case DLT_JUNIPER_MFR
:
3294 case DLT_JUNIPER_MLFR
:
3295 case DLT_JUNIPER_MLPPP
:
3296 case DLT_JUNIPER_ATM1
:
3297 case DLT_JUNIPER_ATM2
:
3298 case DLT_JUNIPER_PPPOE
:
3299 case DLT_JUNIPER_PPPOE_ATM
:
3300 case DLT_JUNIPER_GGSN
:
3301 case DLT_JUNIPER_ES
:
3302 case DLT_JUNIPER_MONITOR
:
3303 case DLT_JUNIPER_SERVICES
:
3304 case DLT_JUNIPER_ETHER
:
3305 case DLT_JUNIPER_PPP
:
3306 case DLT_JUNIPER_FRELAY
:
3307 case DLT_JUNIPER_CHDLC
:
3308 case DLT_JUNIPER_VP
:
3309 case DLT_JUNIPER_ST
:
3310 case DLT_JUNIPER_ISM
:
3311 /* just lets verify the magic number for now -
3312 * on ATM we may have up to 6 different encapsulations on the wire
3313 * and need a lot of heuristics to figure out that the payload
3316 * FIXME encapsulation specific BPF_ filters
3318 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3320 case DLT_LINUX_IRDA
:
3321 bpf_error("IrDA link-layer type filtering not implemented");
3324 bpf_error("DOCSIS link-layer type filtering not implemented");
3327 case DLT_MTP2_WITH_PHDR
:
3328 bpf_error("MTP2 link-layer type filtering not implemented");
3331 bpf_error("ERF link-layer type filtering not implemented");
3335 bpf_error("PFSYNC link-layer type filtering not implemented");
3338 case DLT_LINUX_LAPD
:
3339 bpf_error("LAPD link-layer type filtering not implemented");
3343 bpf_error("USB link-layer type filtering not implemented");
3345 case DLT_BLUETOOTH_HCI_H4
:
3346 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3347 bpf_error("Bluetooth link-layer type filtering not implemented");
3350 bpf_error("CAN20B link-layer type filtering not implemented");
3352 case DLT_IEEE802_15_4
:
3353 case DLT_IEEE802_15_4_LINUX
:
3354 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3356 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3357 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3360 bpf_error("SITA link-layer type filtering not implemented");
3363 bpf_error("RAIF1 link-layer type filtering not implemented");
3366 bpf_error("IPMB link-layer type filtering not implemented");
3369 bpf_error("AX.25 link-layer type filtering not implemented");
3373 * All the types that have no encapsulation should either be
3374 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3375 * all packets are IP packets, or should be handled in some
3376 * special case, if none of them are (if some are and some
3377 * aren't, the lack of encapsulation is a problem, as we'd
3378 * have to find some other way of determining the packet type).
3380 * Therefore, if "off_linktype" is -1, there's an error.
3382 if (off_linktype
== (u_int
)-1)
3386 * Any type not handled above should always have an Ethernet
3387 * type at an offset of "off_linktype".
3389 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3393 * Check for an LLC SNAP packet with a given organization code and
3394 * protocol type; we check the entire contents of the 802.2 LLC and
3395 * snap headers, checking for DSAP and SSAP of SNAP and a control
3396 * field of 0x03 in the LLC header, and for the specified organization
3397 * code and protocol type in the SNAP header.
3399 static struct block
*
3400 gen_snap(orgcode
, ptype
)
3401 bpf_u_int32 orgcode
;
3404 u_char snapblock
[8];
3406 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3407 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3408 snapblock
[2] = 0x03; /* control = UI */
3409 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3410 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3411 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3412 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3413 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3414 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3418 * Generate code to match a particular packet type, for link-layer types
3419 * using 802.2 LLC headers.
3421 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3422 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3424 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3425 * value, if <= ETHERMTU. We use that to determine whether to
3426 * match the DSAP or both DSAP and LSAP or to check the OUI and
3427 * protocol ID in a SNAP header.
3429 static struct block
*
3430 gen_llc_linktype(proto
)
3434 * XXX - handle token-ring variable-length header.
3440 case LLCSAP_NETBEUI
:
3442 * XXX - should we check both the DSAP and the
3443 * SSAP, like this, or should we check just the
3444 * DSAP, as we do for other types <= ETHERMTU
3445 * (i.e., other SAP values)?
3447 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3448 ((proto
<< 8) | proto
));
3452 * XXX - are there ever SNAP frames for IPX on
3453 * non-Ethernet 802.x networks?
3455 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3456 (bpf_int32
)LLCSAP_IPX
);
3458 case ETHERTYPE_ATALK
:
3460 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3461 * SNAP packets with an organization code of
3462 * 0x080007 (Apple, for Appletalk) and a protocol
3463 * type of ETHERTYPE_ATALK (Appletalk).
3465 * XXX - check for an organization code of
3466 * encapsulated Ethernet as well?
3468 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3472 * XXX - we don't have to check for IPX 802.3
3473 * here, but should we check for the IPX Ethertype?
3475 if (proto
<= ETHERMTU
) {
3477 * This is an LLC SAP value, so check
3480 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3483 * This is an Ethernet type; we assume that it's
3484 * unlikely that it'll appear in the right place
3485 * at random, and therefore check only the
3486 * location that would hold the Ethernet type
3487 * in a SNAP frame with an organization code of
3488 * 0x000000 (encapsulated Ethernet).
3490 * XXX - if we were to check for the SNAP DSAP and
3491 * LSAP, as per XXX, and were also to check for an
3492 * organization code of 0x000000 (encapsulated
3493 * Ethernet), we'd do
3495 * return gen_snap(0x000000, proto);
3497 * here; for now, we don't, as per the above.
3498 * I don't know whether it's worth the extra CPU
3499 * time to do the right check or not.
3501 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3506 static struct block
*
3507 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3511 u_int src_off
, dst_off
;
3513 struct block
*b0
, *b1
;
3527 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3528 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3534 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3535 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3542 b0
= gen_linktype(proto
);
3543 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3549 static struct block
*
3550 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3551 struct in6_addr
*addr
;
3552 struct in6_addr
*mask
;
3554 u_int src_off
, dst_off
;
3556 struct block
*b0
, *b1
;
3571 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3572 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3578 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3579 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3586 /* this order is important */
3587 a
= (u_int32_t
*)addr
;
3588 m
= (u_int32_t
*)mask
;
3589 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3590 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3592 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3594 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3596 b0
= gen_linktype(proto
);
3602 static struct block
*
3603 gen_ehostop(eaddr
, dir
)
3604 register const u_char
*eaddr
;
3607 register struct block
*b0
, *b1
;
3611 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3614 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3617 b0
= gen_ehostop(eaddr
, Q_SRC
);
3618 b1
= gen_ehostop(eaddr
, Q_DST
);
3624 b0
= gen_ehostop(eaddr
, Q_SRC
);
3625 b1
= gen_ehostop(eaddr
, Q_DST
);
3634 * Like gen_ehostop, but for DLT_FDDI
3636 static struct block
*
3637 gen_fhostop(eaddr
, dir
)
3638 register const u_char
*eaddr
;
3641 struct block
*b0
, *b1
;
3646 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3648 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3653 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3655 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3659 b0
= gen_fhostop(eaddr
, Q_SRC
);
3660 b1
= gen_fhostop(eaddr
, Q_DST
);
3666 b0
= gen_fhostop(eaddr
, Q_SRC
);
3667 b1
= gen_fhostop(eaddr
, Q_DST
);
3676 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3678 static struct block
*
3679 gen_thostop(eaddr
, dir
)
3680 register const u_char
*eaddr
;
3683 register struct block
*b0
, *b1
;
3687 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3690 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3693 b0
= gen_thostop(eaddr
, Q_SRC
);
3694 b1
= gen_thostop(eaddr
, Q_DST
);
3700 b0
= gen_thostop(eaddr
, Q_SRC
);
3701 b1
= gen_thostop(eaddr
, Q_DST
);
3710 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3711 * various 802.11 + radio headers.
3713 static struct block
*
3714 gen_wlanhostop(eaddr
, dir
)
3715 register const u_char
*eaddr
;
3718 register struct block
*b0
, *b1
, *b2
;
3719 register struct slist
*s
;
3721 #ifdef ENABLE_WLAN_FILTERING_PATCH
3724 * We need to disable the optimizer because the optimizer is buggy
3725 * and wipes out some LD instructions generated by the below
3726 * code to validate the Frame Control bits
3729 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3736 * For control frames, there is no SA.
3738 * For management frames, SA is at an
3739 * offset of 10 from the beginning of
3742 * For data frames, SA is at an offset
3743 * of 10 from the beginning of the packet
3744 * if From DS is clear, at an offset of
3745 * 16 from the beginning of the packet
3746 * if From DS is set and To DS is clear,
3747 * and an offset of 24 from the beginning
3748 * of the packet if From DS is set and To DS
3753 * Generate the tests to be done for data frames
3756 * First, check for To DS set, i.e. check "link[1] & 0x01".
3758 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3759 b1
= new_block(JMP(BPF_JSET
));
3760 b1
->s
.k
= 0x01; /* To DS */
3764 * If To DS is set, the SA is at 24.
3766 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3770 * Now, check for To DS not set, i.e. check
3771 * "!(link[1] & 0x01)".
3773 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3774 b2
= new_block(JMP(BPF_JSET
));
3775 b2
->s
.k
= 0x01; /* To DS */
3780 * If To DS is not set, the SA is at 16.
3782 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3786 * Now OR together the last two checks. That gives
3787 * the complete set of checks for data frames with
3793 * Now check for From DS being set, and AND that with
3794 * the ORed-together checks.
3796 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3797 b1
= new_block(JMP(BPF_JSET
));
3798 b1
->s
.k
= 0x02; /* From DS */
3803 * Now check for data frames with From DS not set.
3805 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3806 b2
= new_block(JMP(BPF_JSET
));
3807 b2
->s
.k
= 0x02; /* From DS */
3812 * If From DS isn't set, the SA is at 10.
3814 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3818 * Now OR together the checks for data frames with
3819 * From DS not set and for data frames with From DS
3820 * set; that gives the checks done for data frames.
3825 * Now check for a data frame.
3826 * I.e, check "link[0] & 0x08".
3828 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3829 b1
= new_block(JMP(BPF_JSET
));
3834 * AND that with the checks done for data frames.
3839 * If the high-order bit of the type value is 0, this
3840 * is a management frame.
3841 * I.e, check "!(link[0] & 0x08)".
3843 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3844 b2
= new_block(JMP(BPF_JSET
));
3850 * For management frames, the SA is at 10.
3852 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3856 * OR that with the checks done for data frames.
3857 * That gives the checks done for management and
3863 * If the low-order bit of the type value is 1,
3864 * this is either a control frame or a frame
3865 * with a reserved type, and thus not a
3868 * I.e., check "!(link[0] & 0x04)".
3870 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3871 b1
= new_block(JMP(BPF_JSET
));
3877 * AND that with the checks for data and management
3887 * For control frames, there is no DA.
3889 * For management frames, DA is at an
3890 * offset of 4 from the beginning of
3893 * For data frames, DA is at an offset
3894 * of 4 from the beginning of the packet
3895 * if To DS is clear and at an offset of
3896 * 16 from the beginning of the packet
3901 * Generate the tests to be done for data frames.
3903 * First, check for To DS set, i.e. "link[1] & 0x01".
3905 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3906 b1
= new_block(JMP(BPF_JSET
));
3907 b1
->s
.k
= 0x01; /* To DS */
3911 * If To DS is set, the DA is at 16.
3913 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3917 * Now, check for To DS not set, i.e. check
3918 * "!(link[1] & 0x01)".
3920 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3921 b2
= new_block(JMP(BPF_JSET
));
3922 b2
->s
.k
= 0x01; /* To DS */
3927 * If To DS is not set, the DA is at 4.
3929 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3933 * Now OR together the last two checks. That gives
3934 * the complete set of checks for data frames.
3939 * Now check for a data frame.
3940 * I.e, check "link[0] & 0x08".
3942 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3943 b1
= new_block(JMP(BPF_JSET
));
3948 * AND that with the checks done for data frames.
3953 * If the high-order bit of the type value is 0, this
3954 * is a management frame.
3955 * I.e, check "!(link[0] & 0x08)".
3957 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3958 b2
= new_block(JMP(BPF_JSET
));
3964 * For management frames, the DA is at 4.
3966 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3970 * OR that with the checks done for data frames.
3971 * That gives the checks done for management and
3977 * If the low-order bit of the type value is 1,
3978 * this is either a control frame or a frame
3979 * with a reserved type, and thus not a
3982 * I.e., check "!(link[0] & 0x04)".
3984 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3985 b1
= new_block(JMP(BPF_JSET
));
3991 * AND that with the checks for data and management
3998 * XXX - add RA, TA, and BSSID keywords?
4001 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4005 * Not present in CTS or ACK control frames.
4007 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4008 IEEE80211_FC0_TYPE_MASK
);
4010 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4011 IEEE80211_FC0_SUBTYPE_MASK
);
4013 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4014 IEEE80211_FC0_SUBTYPE_MASK
);
4018 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4024 * Not present in control frames.
4026 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4027 IEEE80211_FC0_TYPE_MASK
);
4029 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4035 * Present only if the direction mask has both "From DS"
4036 * and "To DS" set. Neither control frames nor management
4037 * frames should have both of those set, so we don't
4038 * check the frame type.
4040 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4041 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4042 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4047 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4048 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4054 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4055 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4064 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4065 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4066 * as the RFC states.)
4068 static struct block
*
4069 gen_ipfchostop(eaddr
, dir
)
4070 register const u_char
*eaddr
;
4073 register struct block
*b0
, *b1
;
4077 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4080 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4083 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4084 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4090 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4091 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4100 * This is quite tricky because there may be pad bytes in front of the
4101 * DECNET header, and then there are two possible data packet formats that
4102 * carry both src and dst addresses, plus 5 packet types in a format that
4103 * carries only the src node, plus 2 types that use a different format and
4104 * also carry just the src node.
4108 * Instead of doing those all right, we just look for data packets with
4109 * 0 or 1 bytes of padding. If you want to look at other packets, that
4110 * will require a lot more hacking.
4112 * To add support for filtering on DECNET "areas" (network numbers)
4113 * one would want to add a "mask" argument to this routine. That would
4114 * make the filter even more inefficient, although one could be clever
4115 * and not generate masking instructions if the mask is 0xFFFF.
4117 static struct block
*
4118 gen_dnhostop(addr
, dir
)
4122 struct block
*b0
, *b1
, *b2
, *tmp
;
4123 u_int offset_lh
; /* offset if long header is received */
4124 u_int offset_sh
; /* offset if short header is received */
4129 offset_sh
= 1; /* follows flags */
4130 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4134 offset_sh
= 3; /* follows flags, dstnode */
4135 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4139 /* Inefficient because we do our Calvinball dance twice */
4140 b0
= gen_dnhostop(addr
, Q_SRC
);
4141 b1
= gen_dnhostop(addr
, Q_DST
);
4147 /* Inefficient because we do our Calvinball dance twice */
4148 b0
= gen_dnhostop(addr
, Q_SRC
);
4149 b1
= gen_dnhostop(addr
, Q_DST
);
4154 bpf_error("ISO host filtering not implemented");
4159 b0
= gen_linktype(ETHERTYPE_DN
);
4160 /* Check for pad = 1, long header case */
4161 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4162 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4163 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4164 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4166 /* Check for pad = 0, long header case */
4167 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4168 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4171 /* Check for pad = 1, short header case */
4172 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4173 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4174 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4177 /* Check for pad = 0, short header case */
4178 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4179 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4183 /* Combine with test for linktype */
4189 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4190 * test the bottom-of-stack bit, and then check the version number
4191 * field in the IP header.
4193 static struct block
*
4194 gen_mpls_linktype(proto
)
4197 struct block
*b0
, *b1
;
4202 /* match the bottom-of-stack bit */
4203 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4204 /* match the IPv4 version number */
4205 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4210 /* match the bottom-of-stack bit */
4211 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4212 /* match the IPv4 version number */
4213 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4222 static struct block
*
4223 gen_host(addr
, mask
, proto
, dir
, type
)
4230 struct block
*b0
, *b1
;
4231 const char *typestr
;
4241 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4243 * Only check for non-IPv4 addresses if we're not
4244 * checking MPLS-encapsulated packets.
4246 if (label_stack_depth
== 0) {
4247 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4249 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4255 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4258 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4261 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4264 bpf_error("'tcp' modifier applied to %s", typestr
);
4267 bpf_error("'sctp' modifier applied to %s", typestr
);
4270 bpf_error("'udp' modifier applied to %s", typestr
);
4273 bpf_error("'icmp' modifier applied to %s", typestr
);
4276 bpf_error("'igmp' modifier applied to %s", typestr
);
4279 bpf_error("'igrp' modifier applied to %s", typestr
);
4282 bpf_error("'pim' modifier applied to %s", typestr
);
4285 bpf_error("'vrrp' modifier applied to %s", typestr
);
4288 bpf_error("ATALK host filtering not implemented");
4291 bpf_error("AARP host filtering not implemented");
4294 return gen_dnhostop(addr
, dir
);
4297 bpf_error("SCA host filtering not implemented");
4300 bpf_error("LAT host filtering not implemented");
4303 bpf_error("MOPDL host filtering not implemented");
4306 bpf_error("MOPRC host filtering not implemented");
4310 bpf_error("'ip6' modifier applied to ip host");
4313 bpf_error("'icmp6' modifier applied to %s", typestr
);
4317 bpf_error("'ah' modifier applied to %s", typestr
);
4320 bpf_error("'esp' modifier applied to %s", typestr
);
4323 bpf_error("ISO host filtering not implemented");
4326 bpf_error("'esis' modifier applied to %s", typestr
);
4329 bpf_error("'isis' modifier applied to %s", typestr
);
4332 bpf_error("'clnp' modifier applied to %s", typestr
);
4335 bpf_error("'stp' modifier applied to %s", typestr
);
4338 bpf_error("IPX host filtering not implemented");
4341 bpf_error("'netbeui' modifier applied to %s", typestr
);
4344 bpf_error("'radio' modifier applied to %s", typestr
);
4353 static struct block
*
4354 gen_host6(addr
, mask
, proto
, dir
, type
)
4355 struct in6_addr
*addr
;
4356 struct in6_addr
*mask
;
4361 const char *typestr
;
4371 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4374 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4377 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4380 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4383 bpf_error("'sctp' modifier applied to %s", typestr
);
4386 bpf_error("'tcp' modifier applied to %s", typestr
);
4389 bpf_error("'udp' modifier applied to %s", typestr
);
4392 bpf_error("'icmp' modifier applied to %s", typestr
);
4395 bpf_error("'igmp' modifier applied to %s", typestr
);
4398 bpf_error("'igrp' modifier applied to %s", typestr
);
4401 bpf_error("'pim' modifier applied to %s", typestr
);
4404 bpf_error("'vrrp' modifier applied to %s", typestr
);
4407 bpf_error("ATALK host filtering not implemented");
4410 bpf_error("AARP host filtering not implemented");
4413 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4416 bpf_error("SCA host filtering not implemented");
4419 bpf_error("LAT host filtering not implemented");
4422 bpf_error("MOPDL host filtering not implemented");
4425 bpf_error("MOPRC host filtering not implemented");
4428 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4431 bpf_error("'icmp6' modifier applied to %s", typestr
);
4434 bpf_error("'ah' modifier applied to %s", typestr
);
4437 bpf_error("'esp' modifier applied to %s", typestr
);
4440 bpf_error("ISO host filtering not implemented");
4443 bpf_error("'esis' modifier applied to %s", typestr
);
4446 bpf_error("'isis' modifier applied to %s", typestr
);
4449 bpf_error("'clnp' modifier applied to %s", typestr
);
4452 bpf_error("'stp' modifier applied to %s", typestr
);
4455 bpf_error("IPX host filtering not implemented");
4458 bpf_error("'netbeui' modifier applied to %s", typestr
);
4461 bpf_error("'radio' modifier applied to %s", typestr
);
4471 static struct block
*
4472 gen_gateway(eaddr
, alist
, proto
, dir
)
4473 const u_char
*eaddr
;
4474 bpf_u_int32
**alist
;
4478 struct block
*b0
, *b1
, *tmp
;
4481 bpf_error("direction applied to 'gateway'");
4490 b0
= gen_ehostop(eaddr
, Q_OR
);
4493 b0
= gen_fhostop(eaddr
, Q_OR
);
4496 b0
= gen_thostop(eaddr
, Q_OR
);
4498 case DLT_IEEE802_11
:
4499 case DLT_PRISM_HEADER
:
4500 case DLT_IEEE802_11_RADIO_AVS
:
4501 case DLT_IEEE802_11_RADIO
:
4503 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4508 * Check that the packet doesn't begin with an
4509 * LE Control marker. (We've already generated
4512 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4517 * Now check the MAC address.
4519 b0
= gen_ehostop(eaddr
, Q_OR
);
4523 case DLT_IP_OVER_FC
:
4524 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4528 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4530 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4532 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4541 bpf_error("illegal modifier of 'gateway'");
4547 gen_proto_abbrev(proto
)
4556 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4558 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4564 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4566 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4572 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4574 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4580 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4583 #ifndef IPPROTO_IGMP
4584 #define IPPROTO_IGMP 2
4588 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4591 #ifndef IPPROTO_IGRP
4592 #define IPPROTO_IGRP 9
4595 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4599 #define IPPROTO_PIM 103
4603 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4605 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4610 #ifndef IPPROTO_VRRP
4611 #define IPPROTO_VRRP 112
4615 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4619 b1
= gen_linktype(ETHERTYPE_IP
);
4623 b1
= gen_linktype(ETHERTYPE_ARP
);
4627 b1
= gen_linktype(ETHERTYPE_REVARP
);
4631 bpf_error("link layer applied in wrong context");
4634 b1
= gen_linktype(ETHERTYPE_ATALK
);
4638 b1
= gen_linktype(ETHERTYPE_AARP
);
4642 b1
= gen_linktype(ETHERTYPE_DN
);
4646 b1
= gen_linktype(ETHERTYPE_SCA
);
4650 b1
= gen_linktype(ETHERTYPE_LAT
);
4654 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4658 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4663 b1
= gen_linktype(ETHERTYPE_IPV6
);
4666 #ifndef IPPROTO_ICMPV6
4667 #define IPPROTO_ICMPV6 58
4670 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4675 #define IPPROTO_AH 51
4678 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4680 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4686 #define IPPROTO_ESP 50
4689 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4691 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4697 b1
= gen_linktype(LLCSAP_ISONS
);
4701 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4705 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4708 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4709 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4710 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4712 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4714 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4716 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4720 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4721 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4722 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4724 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4726 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4728 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4732 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4733 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4734 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4736 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4741 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4742 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4747 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4748 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4750 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4752 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4757 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4758 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4763 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4764 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4769 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4773 b1
= gen_linktype(LLCSAP_8021D
);
4777 b1
= gen_linktype(LLCSAP_IPX
);
4781 b1
= gen_linktype(LLCSAP_NETBEUI
);
4785 bpf_error("'radio' is not a valid protocol type");
4793 static struct block
*
4800 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4801 b
= new_block(JMP(BPF_JSET
));
4810 * Generate a comparison to a port value in the transport-layer header
4811 * at the specified offset from the beginning of that header.
4813 * XXX - this handles a variable-length prefix preceding the link-layer
4814 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4815 * variable-length link-layer headers (such as Token Ring or 802.11
4818 static struct block
*
4819 gen_portatom(off
, v
)
4823 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4827 static struct block
*
4828 gen_portatom6(off
, v
)
4832 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4837 gen_portop(port
, proto
, dir
)
4838 int port
, proto
, dir
;
4840 struct block
*b0
, *b1
, *tmp
;
4842 /* ip proto 'proto' */
4843 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4849 b1
= gen_portatom(0, (bpf_int32
)port
);
4853 b1
= gen_portatom(2, (bpf_int32
)port
);
4858 tmp
= gen_portatom(0, (bpf_int32
)port
);
4859 b1
= gen_portatom(2, (bpf_int32
)port
);
4864 tmp
= gen_portatom(0, (bpf_int32
)port
);
4865 b1
= gen_portatom(2, (bpf_int32
)port
);
4877 static struct block
*
4878 gen_port(port
, ip_proto
, dir
)
4883 struct block
*b0
, *b1
, *tmp
;
4888 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4889 * not LLC encapsulation with LLCSAP_IP.
4891 * For IEEE 802 networks - which includes 802.5 token ring
4892 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4893 * says that SNAP encapsulation is used, not LLC encapsulation
4896 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4897 * RFC 2225 say that SNAP encapsulation is used, not LLC
4898 * encapsulation with LLCSAP_IP.
4900 * So we always check for ETHERTYPE_IP.
4902 b0
= gen_linktype(ETHERTYPE_IP
);
4908 b1
= gen_portop(port
, ip_proto
, dir
);
4912 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4913 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4915 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4928 gen_portop6(port
, proto
, dir
)
4929 int port
, proto
, dir
;
4931 struct block
*b0
, *b1
, *tmp
;
4933 /* ip6 proto 'proto' */
4934 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4938 b1
= gen_portatom6(0, (bpf_int32
)port
);
4942 b1
= gen_portatom6(2, (bpf_int32
)port
);
4947 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4948 b1
= gen_portatom6(2, (bpf_int32
)port
);
4953 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4954 b1
= gen_portatom6(2, (bpf_int32
)port
);
4966 static struct block
*
4967 gen_port6(port
, ip_proto
, dir
)
4972 struct block
*b0
, *b1
, *tmp
;
4974 /* link proto ip6 */
4975 b0
= gen_linktype(ETHERTYPE_IPV6
);
4981 b1
= gen_portop6(port
, ip_proto
, dir
);
4985 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4986 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4988 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5000 /* gen_portrange code */
5001 static struct block
*
5002 gen_portrangeatom(off
, v1
, v2
)
5006 struct block
*b1
, *b2
;
5010 * Reverse the order of the ports, so v1 is the lower one.
5019 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5020 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5028 gen_portrangeop(port1
, port2
, proto
, dir
)
5033 struct block
*b0
, *b1
, *tmp
;
5035 /* ip proto 'proto' */
5036 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5042 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5046 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5051 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5052 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5057 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5058 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5070 static struct block
*
5071 gen_portrange(port1
, port2
, ip_proto
, dir
)
5076 struct block
*b0
, *b1
, *tmp
;
5079 b0
= gen_linktype(ETHERTYPE_IP
);
5085 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5089 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5090 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5092 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5104 static struct block
*
5105 gen_portrangeatom6(off
, v1
, v2
)
5109 struct block
*b1
, *b2
;
5113 * Reverse the order of the ports, so v1 is the lower one.
5122 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5123 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5131 gen_portrangeop6(port1
, port2
, proto
, dir
)
5136 struct block
*b0
, *b1
, *tmp
;
5138 /* ip6 proto 'proto' */
5139 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5143 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5147 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5152 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5153 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5158 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5159 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5171 static struct block
*
5172 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5177 struct block
*b0
, *b1
, *tmp
;
5179 /* link proto ip6 */
5180 b0
= gen_linktype(ETHERTYPE_IPV6
);
5186 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5190 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5191 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5193 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5206 lookup_proto(name
, proto
)
5207 register const char *name
;
5217 v
= pcap_nametoproto(name
);
5218 if (v
== PROTO_UNDEF
)
5219 bpf_error("unknown ip proto '%s'", name
);
5223 /* XXX should look up h/w protocol type based on linktype */
5224 v
= pcap_nametoeproto(name
);
5225 if (v
== PROTO_UNDEF
) {
5226 v
= pcap_nametollc(name
);
5227 if (v
== PROTO_UNDEF
)
5228 bpf_error("unknown ether proto '%s'", name
);
5233 if (strcmp(name
, "esis") == 0)
5235 else if (strcmp(name
, "isis") == 0)
5237 else if (strcmp(name
, "clnp") == 0)
5240 bpf_error("unknown osi proto '%s'", name
);
5260 static struct block
*
5261 gen_protochain(v
, proto
, dir
)
5266 #ifdef NO_PROTOCHAIN
5267 return gen_proto(v
, proto
, dir
);
5269 struct block
*b0
, *b
;
5270 struct slist
*s
[100];
5271 int fix2
, fix3
, fix4
, fix5
;
5272 int ahcheck
, again
, end
;
5274 int reg2
= alloc_reg();
5276 memset(s
, 0, sizeof(s
));
5277 fix2
= fix3
= fix4
= fix5
= 0;
5284 b0
= gen_protochain(v
, Q_IP
, dir
);
5285 b
= gen_protochain(v
, Q_IPV6
, dir
);
5289 bpf_error("bad protocol applied for 'protochain'");
5294 * We don't handle variable-length prefixes before the link-layer
5295 * header, or variable-length link-layer headers, here yet.
5296 * We might want to add BPF instructions to do the protochain
5297 * work, to simplify that and, on platforms that have a BPF
5298 * interpreter with the new instructions, let the filtering
5299 * be done in the kernel. (We already require a modified BPF
5300 * engine to do the protochain stuff, to support backward
5301 * branches, and backward branch support is unlikely to appear
5302 * in kernel BPF engines.)
5306 case DLT_IEEE802_11
:
5307 case DLT_PRISM_HEADER
:
5308 case DLT_IEEE802_11_RADIO_AVS
:
5309 case DLT_IEEE802_11_RADIO
:
5311 bpf_error("'protochain' not supported with 802.11");
5314 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5317 * s[0] is a dummy entry to protect other BPF insn from damage
5318 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5319 * hard to find interdependency made by jump table fixup.
5322 s
[i
] = new_stmt(0); /*dummy*/
5327 b0
= gen_linktype(ETHERTYPE_IP
);
5330 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5331 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5333 /* X = ip->ip_hl << 2 */
5334 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5335 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5340 b0
= gen_linktype(ETHERTYPE_IPV6
);
5342 /* A = ip6->ip_nxt */
5343 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5344 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5346 /* X = sizeof(struct ip6_hdr) */
5347 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5353 bpf_error("unsupported proto to gen_protochain");
5357 /* again: if (A == v) goto end; else fall through; */
5359 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5361 s
[i
]->s
.jt
= NULL
; /*later*/
5362 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5366 #ifndef IPPROTO_NONE
5367 #define IPPROTO_NONE 59
5369 /* if (A == IPPROTO_NONE) goto end */
5370 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5371 s
[i
]->s
.jt
= NULL
; /*later*/
5372 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5373 s
[i
]->s
.k
= IPPROTO_NONE
;
5374 s
[fix5
]->s
.jf
= s
[i
];
5379 if (proto
== Q_IPV6
) {
5380 int v6start
, v6end
, v6advance
, j
;
5383 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5384 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5385 s
[i
]->s
.jt
= NULL
; /*later*/
5386 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5387 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5388 s
[fix2
]->s
.jf
= s
[i
];
5390 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5391 s
[i
- 1]->s
.jf
= 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_DSTOPTS
;
5396 /* if (A == IPPROTO_ROUTING) goto v6advance */
5397 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5398 s
[i
]->s
.jt
= NULL
; /*later*/
5399 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5400 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5402 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5403 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5404 s
[i
]->s
.jt
= NULL
; /*later*/
5405 s
[i
]->s
.jf
= NULL
; /*later*/
5406 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5417 * X = X + (P[X + 1] + 1) * 8;
5420 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5422 /* A = P[X + packet head] */
5423 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5424 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5427 s
[i
] = new_stmt(BPF_ST
);
5431 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5434 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5438 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5440 /* A = P[X + packet head]; */
5441 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5442 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5445 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5449 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5453 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5456 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5460 /* goto again; (must use BPF_JA for backward jump) */
5461 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5462 s
[i
]->s
.k
= again
- i
- 1;
5463 s
[i
- 1]->s
.jf
= s
[i
];
5467 for (j
= v6start
; j
<= v6end
; j
++)
5468 s
[j
]->s
.jt
= s
[v6advance
];
5473 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5475 s
[fix2
]->s
.jf
= s
[i
];
5481 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5482 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5483 s
[i
]->s
.jt
= NULL
; /*later*/
5484 s
[i
]->s
.jf
= NULL
; /*later*/
5485 s
[i
]->s
.k
= IPPROTO_AH
;
5487 s
[fix3
]->s
.jf
= s
[ahcheck
];
5494 * X = X + (P[X + 1] + 2) * 4;
5497 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5499 /* A = P[X + packet head]; */
5500 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5501 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5504 s
[i
] = new_stmt(BPF_ST
);
5508 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5511 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5515 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5517 /* A = P[X + packet head] */
5518 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5519 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5522 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5526 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5530 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5533 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5537 /* goto again; (must use BPF_JA for backward jump) */
5538 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5539 s
[i
]->s
.k
= again
- i
- 1;
5544 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5546 s
[fix2
]->s
.jt
= s
[end
];
5547 s
[fix4
]->s
.jf
= s
[end
];
5548 s
[fix5
]->s
.jt
= s
[end
];
5555 for (i
= 0; i
< max
- 1; i
++)
5556 s
[i
]->next
= s
[i
+ 1];
5557 s
[max
- 1]->next
= NULL
;
5562 b
= new_block(JMP(BPF_JEQ
));
5563 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5573 static struct block
*
5574 gen_check_802_11_data_frame()
5577 struct block
*b0
, *b1
;
5580 * A data frame has the 0x08 bit (b3) in the frame control field set
5581 * and the 0x04 bit (b2) clear.
5583 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5584 b0
= new_block(JMP(BPF_JSET
));
5588 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5589 b1
= new_block(JMP(BPF_JSET
));
5600 * Generate code that checks whether the packet is a packet for protocol
5601 * <proto> and whether the type field in that protocol's header has
5602 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5603 * IP packet and checks the protocol number in the IP header against <v>.
5605 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5606 * against Q_IP and Q_IPV6.
5608 static struct block
*
5609 gen_proto(v
, proto
, dir
)
5614 struct block
*b0
, *b1
;
5616 if (dir
!= Q_DEFAULT
)
5617 bpf_error("direction applied to 'proto'");
5622 b0
= gen_proto(v
, Q_IP
, dir
);
5623 b1
= gen_proto(v
, Q_IPV6
, dir
);
5631 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5632 * not LLC encapsulation with LLCSAP_IP.
5634 * For IEEE 802 networks - which includes 802.5 token ring
5635 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5636 * says that SNAP encapsulation is used, not LLC encapsulation
5639 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5640 * RFC 2225 say that SNAP encapsulation is used, not LLC
5641 * encapsulation with LLCSAP_IP.
5643 * So we always check for ETHERTYPE_IP.
5645 b0
= gen_linktype(ETHERTYPE_IP
);
5647 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5649 b1
= gen_protochain(v
, Q_IP
);
5659 * Frame Relay packets typically have an OSI
5660 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5661 * generates code to check for all the OSI
5662 * NLPIDs, so calling it and then adding a check
5663 * for the particular NLPID for which we're
5664 * looking is bogus, as we can just check for
5667 * What we check for is the NLPID and a frame
5668 * control field value of UI, i.e. 0x03 followed
5671 * XXX - assumes a 2-byte Frame Relay header with
5672 * DLCI and flags. What if the address is longer?
5674 * XXX - what about SNAP-encapsulated frames?
5676 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5682 * Cisco uses an Ethertype lookalike - for OSI,
5685 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5686 /* OSI in C-HDLC is stuffed with a fudge byte */
5687 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5692 b0
= gen_linktype(LLCSAP_ISONS
);
5693 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5699 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5701 * 4 is the offset of the PDU type relative to the IS-IS
5704 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5709 bpf_error("arp does not encapsulate another protocol");
5713 bpf_error("rarp does not encapsulate another protocol");
5717 bpf_error("atalk encapsulation is not specifiable");
5721 bpf_error("decnet encapsulation is not specifiable");
5725 bpf_error("sca does not encapsulate another protocol");
5729 bpf_error("lat does not encapsulate another protocol");
5733 bpf_error("moprc does not encapsulate another protocol");
5737 bpf_error("mopdl does not encapsulate another protocol");
5741 return gen_linktype(v
);
5744 bpf_error("'udp proto' is bogus");
5748 bpf_error("'tcp proto' is bogus");
5752 bpf_error("'sctp proto' is bogus");
5756 bpf_error("'icmp proto' is bogus");
5760 bpf_error("'igmp proto' is bogus");
5764 bpf_error("'igrp proto' is bogus");
5768 bpf_error("'pim proto' is bogus");
5772 bpf_error("'vrrp proto' is bogus");
5777 b0
= gen_linktype(ETHERTYPE_IPV6
);
5779 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5781 b1
= gen_protochain(v
, Q_IPV6
);
5787 bpf_error("'icmp6 proto' is bogus");
5791 bpf_error("'ah proto' is bogus");
5794 bpf_error("'ah proto' is bogus");
5797 bpf_error("'stp proto' is bogus");
5800 bpf_error("'ipx proto' is bogus");
5803 bpf_error("'netbeui proto' is bogus");
5806 bpf_error("'radio proto' is bogus");
5817 register const char *name
;
5820 int proto
= q
.proto
;
5824 bpf_u_int32 mask
, addr
;
5826 bpf_u_int32
**alist
;
5829 struct sockaddr_in
*sin4
;
5830 struct sockaddr_in6
*sin6
;
5831 struct addrinfo
*res
, *res0
;
5832 struct in6_addr mask128
;
5834 struct block
*b
, *tmp
;
5835 int port
, real_proto
;
5841 addr
= pcap_nametonetaddr(name
);
5843 bpf_error("unknown network '%s'", name
);
5844 /* Left justify network addr and calculate its network mask */
5846 while (addr
&& (addr
& 0xff000000) == 0) {
5850 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5854 if (proto
== Q_LINK
) {
5858 eaddr
= pcap_ether_hostton(name
);
5861 "unknown ether host '%s'", name
);
5862 b
= gen_ehostop(eaddr
, dir
);
5867 eaddr
= pcap_ether_hostton(name
);
5870 "unknown FDDI host '%s'", name
);
5871 b
= gen_fhostop(eaddr
, dir
);
5876 eaddr
= pcap_ether_hostton(name
);
5879 "unknown token ring host '%s'", name
);
5880 b
= gen_thostop(eaddr
, dir
);
5884 case DLT_IEEE802_11
:
5885 case DLT_PRISM_HEADER
:
5886 case DLT_IEEE802_11_RADIO_AVS
:
5887 case DLT_IEEE802_11_RADIO
:
5889 eaddr
= pcap_ether_hostton(name
);
5892 "unknown 802.11 host '%s'", name
);
5893 b
= gen_wlanhostop(eaddr
, dir
);
5897 case DLT_IP_OVER_FC
:
5898 eaddr
= pcap_ether_hostton(name
);
5901 "unknown Fibre Channel host '%s'", name
);
5902 b
= gen_ipfchostop(eaddr
, dir
);
5911 * Check that the packet doesn't begin
5912 * with an LE Control marker. (We've
5913 * already generated a test for LANE.)
5915 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5919 eaddr
= pcap_ether_hostton(name
);
5922 "unknown ether host '%s'", name
);
5923 b
= gen_ehostop(eaddr
, dir
);
5929 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5930 } else if (proto
== Q_DECNET
) {
5931 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5933 * I don't think DECNET hosts can be multihomed, so
5934 * there is no need to build up a list of addresses
5936 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5939 alist
= pcap_nametoaddr(name
);
5940 if (alist
== NULL
|| *alist
== NULL
)
5941 bpf_error("unknown host '%s'", name
);
5943 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5945 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5947 tmp
= gen_host(**alist
++, 0xffffffff,
5948 tproto
, dir
, q
.addr
);
5954 memset(&mask128
, 0xff, sizeof(mask128
));
5955 res0
= res
= pcap_nametoaddrinfo(name
);
5957 bpf_error("unknown host '%s'", name
);
5959 tproto
= tproto6
= proto
;
5960 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5964 for (res
= res0
; res
; res
= res
->ai_next
) {
5965 switch (res
->ai_family
) {
5967 if (tproto
== Q_IPV6
)
5970 sin4
= (struct sockaddr_in
*)
5972 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5973 0xffffffff, tproto
, dir
, q
.addr
);
5976 if (tproto6
== Q_IP
)
5979 sin6
= (struct sockaddr_in6
*)
5981 tmp
= gen_host6(&sin6
->sin6_addr
,
5982 &mask128
, tproto6
, dir
, q
.addr
);
5993 bpf_error("unknown host '%s'%s", name
,
5994 (proto
== Q_DEFAULT
)
5996 : " for specified address family");
6003 if (proto
!= Q_DEFAULT
&&
6004 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6005 bpf_error("illegal qualifier of 'port'");
6006 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6007 bpf_error("unknown port '%s'", name
);
6008 if (proto
== Q_UDP
) {
6009 if (real_proto
== IPPROTO_TCP
)
6010 bpf_error("port '%s' is tcp", name
);
6011 else if (real_proto
== IPPROTO_SCTP
)
6012 bpf_error("port '%s' is sctp", name
);
6014 /* override PROTO_UNDEF */
6015 real_proto
= IPPROTO_UDP
;
6017 if (proto
== Q_TCP
) {
6018 if (real_proto
== IPPROTO_UDP
)
6019 bpf_error("port '%s' is udp", name
);
6021 else if (real_proto
== IPPROTO_SCTP
)
6022 bpf_error("port '%s' is sctp", name
);
6024 /* override PROTO_UNDEF */
6025 real_proto
= IPPROTO_TCP
;
6027 if (proto
== Q_SCTP
) {
6028 if (real_proto
== IPPROTO_UDP
)
6029 bpf_error("port '%s' is udp", name
);
6031 else if (real_proto
== IPPROTO_TCP
)
6032 bpf_error("port '%s' is tcp", name
);
6034 /* override PROTO_UNDEF */
6035 real_proto
= IPPROTO_SCTP
;
6038 return gen_port(port
, real_proto
, dir
);
6040 b
= gen_port(port
, real_proto
, dir
);
6041 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6046 if (proto
!= Q_DEFAULT
&&
6047 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6048 bpf_error("illegal qualifier of 'portrange'");
6049 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6050 bpf_error("unknown port in range '%s'", name
);
6051 if (proto
== Q_UDP
) {
6052 if (real_proto
== IPPROTO_TCP
)
6053 bpf_error("port in range '%s' is tcp", name
);
6054 else if (real_proto
== IPPROTO_SCTP
)
6055 bpf_error("port in range '%s' is sctp", name
);
6057 /* override PROTO_UNDEF */
6058 real_proto
= IPPROTO_UDP
;
6060 if (proto
== Q_TCP
) {
6061 if (real_proto
== IPPROTO_UDP
)
6062 bpf_error("port in range '%s' is udp", name
);
6063 else if (real_proto
== IPPROTO_SCTP
)
6064 bpf_error("port in range '%s' is sctp", name
);
6066 /* override PROTO_UNDEF */
6067 real_proto
= IPPROTO_TCP
;
6069 if (proto
== Q_SCTP
) {
6070 if (real_proto
== IPPROTO_UDP
)
6071 bpf_error("port in range '%s' is udp", name
);
6072 else if (real_proto
== IPPROTO_TCP
)
6073 bpf_error("port in range '%s' is tcp", name
);
6075 /* override PROTO_UNDEF */
6076 real_proto
= IPPROTO_SCTP
;
6079 return gen_portrange(port1
, port2
, real_proto
, dir
);
6081 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6082 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6088 eaddr
= pcap_ether_hostton(name
);
6090 bpf_error("unknown ether host: %s", name
);
6092 alist
= pcap_nametoaddr(name
);
6093 if (alist
== NULL
|| *alist
== NULL
)
6094 bpf_error("unknown host '%s'", name
);
6095 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6099 bpf_error("'gateway' not supported in this configuration");
6103 real_proto
= lookup_proto(name
, proto
);
6104 if (real_proto
>= 0)
6105 return gen_proto(real_proto
, proto
, dir
);
6107 bpf_error("unknown protocol: %s", name
);
6110 real_proto
= lookup_proto(name
, proto
);
6111 if (real_proto
>= 0)
6112 return gen_protochain(real_proto
, proto
, dir
);
6114 bpf_error("unknown protocol: %s", name
);
6125 gen_mcode(s1
, s2
, masklen
, q
)
6126 register const char *s1
, *s2
;
6127 register int masklen
;
6130 register int nlen
, mlen
;
6133 nlen
= __pcap_atoin(s1
, &n
);
6134 /* Promote short ipaddr */
6138 mlen
= __pcap_atoin(s2
, &m
);
6139 /* Promote short ipaddr */
6142 bpf_error("non-network bits set in \"%s mask %s\"",
6145 /* Convert mask len to mask */
6147 bpf_error("mask length must be <= 32");
6150 * X << 32 is not guaranteed by C to be 0; it's
6155 m
= 0xffffffff << (32 - masklen
);
6157 bpf_error("non-network bits set in \"%s/%d\"",
6164 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6167 bpf_error("Mask syntax for networks only");
6176 register const char *s
;
6181 int proto
= q
.proto
;
6187 else if (q
.proto
== Q_DECNET
)
6188 vlen
= __pcap_atodn(s
, &v
);
6190 vlen
= __pcap_atoin(s
, &v
);
6197 if (proto
== Q_DECNET
)
6198 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6199 else if (proto
== Q_LINK
) {
6200 bpf_error("illegal link layer address");
6203 if (s
== NULL
&& q
.addr
== Q_NET
) {
6204 /* Promote short net number */
6205 while (v
&& (v
& 0xff000000) == 0) {
6210 /* Promote short ipaddr */
6214 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6219 proto
= IPPROTO_UDP
;
6220 else if (proto
== Q_TCP
)
6221 proto
= IPPROTO_TCP
;
6222 else if (proto
== Q_SCTP
)
6223 proto
= IPPROTO_SCTP
;
6224 else if (proto
== Q_DEFAULT
)
6225 proto
= PROTO_UNDEF
;
6227 bpf_error("illegal qualifier of 'port'");
6230 return gen_port((int)v
, proto
, dir
);
6234 b
= gen_port((int)v
, proto
, dir
);
6235 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6242 proto
= IPPROTO_UDP
;
6243 else if (proto
== Q_TCP
)
6244 proto
= IPPROTO_TCP
;
6245 else if (proto
== Q_SCTP
)
6246 proto
= IPPROTO_SCTP
;
6247 else if (proto
== Q_DEFAULT
)
6248 proto
= PROTO_UNDEF
;
6250 bpf_error("illegal qualifier of 'portrange'");
6253 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6257 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6258 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6264 bpf_error("'gateway' requires a name");
6268 return gen_proto((int)v
, proto
, dir
);
6271 return gen_protochain((int)v
, proto
, dir
);
6286 gen_mcode6(s1
, s2
, masklen
, q
)
6287 register const char *s1
, *s2
;
6288 register int masklen
;
6291 struct addrinfo
*res
;
6292 struct in6_addr
*addr
;
6293 struct in6_addr mask
;
6298 bpf_error("no mask %s supported", s2
);
6300 res
= pcap_nametoaddrinfo(s1
);
6302 bpf_error("invalid ip6 address %s", s1
);
6304 bpf_error("%s resolved to multiple address", s1
);
6305 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6307 if (sizeof(mask
) * 8 < masklen
)
6308 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6309 memset(&mask
, 0, sizeof(mask
));
6310 memset(&mask
, 0xff, masklen
/ 8);
6312 mask
.s6_addr
[masklen
/ 8] =
6313 (0xff << (8 - masklen
% 8)) & 0xff;
6316 a
= (u_int32_t
*)addr
;
6317 m
= (u_int32_t
*)&mask
;
6318 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6319 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6320 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6328 bpf_error("Mask syntax for networks only");
6332 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6337 bpf_error("invalid qualifier against IPv6 address");
6346 register const u_char
*eaddr
;
6349 struct block
*b
, *tmp
;
6351 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6354 return gen_ehostop(eaddr
, (int)q
.dir
);
6356 return gen_fhostop(eaddr
, (int)q
.dir
);
6358 return gen_thostop(eaddr
, (int)q
.dir
);
6359 case DLT_IEEE802_11
:
6360 case DLT_PRISM_HEADER
:
6361 case DLT_IEEE802_11_RADIO_AVS
:
6362 case DLT_IEEE802_11_RADIO
:
6364 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6368 * Check that the packet doesn't begin with an
6369 * LE Control marker. (We've already generated
6372 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6377 * Now check the MAC address.
6379 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6384 case DLT_IP_OVER_FC
:
6385 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6387 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6391 bpf_error("ethernet address used in non-ether expression");
6398 struct slist
*s0
, *s1
;
6401 * This is definitely not the best way to do this, but the
6402 * lists will rarely get long.
6409 static struct slist
*
6415 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6420 static struct slist
*
6426 s
= new_stmt(BPF_LD
|BPF_MEM
);
6432 * Modify "index" to use the value stored into its register as an
6433 * offset relative to the beginning of the header for the protocol
6434 * "proto", and allocate a register and put an item "size" bytes long
6435 * (1, 2, or 4) at that offset into that register, making it the register
6439 gen_load(proto
, inst
, size
)
6444 struct slist
*s
, *tmp
;
6446 int regno
= alloc_reg();
6448 free_reg(inst
->regno
);
6452 bpf_error("data size must be 1, 2, or 4");
6468 bpf_error("unsupported index operation");
6472 * The offset is relative to the beginning of the packet
6473 * data, if we have a radio header. (If we don't, this
6476 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6477 linktype
!= DLT_IEEE802_11_RADIO
&&
6478 linktype
!= DLT_PRISM_HEADER
)
6479 bpf_error("radio information not present in capture");
6482 * Load into the X register the offset computed into the
6483 * register specifed by "index".
6485 s
= xfer_to_x(inst
);
6488 * Load the item at that offset.
6490 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6492 sappend(inst
->s
, s
);
6497 * The offset is relative to the beginning of
6498 * the link-layer header.
6500 * XXX - what about ATM LANE? Should the index be
6501 * relative to the beginning of the AAL5 frame, so
6502 * that 0 refers to the beginning of the LE Control
6503 * field, or relative to the beginning of the LAN
6504 * frame, so that 0 refers, for Ethernet LANE, to
6505 * the beginning of the destination address?
6507 s
= gen_llprefixlen();
6510 * If "s" is non-null, it has code to arrange that the
6511 * X register contains the length of the prefix preceding
6512 * the link-layer header. Add to it the offset computed
6513 * into the register specified by "index", and move that
6514 * into the X register. Otherwise, just load into the X
6515 * register the offset computed into the register specifed
6519 sappend(s
, xfer_to_a(inst
));
6520 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6521 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6523 s
= xfer_to_x(inst
);
6526 * Load the item at the sum of the offset we've put in the
6527 * X register and the offset of the start of the link
6528 * layer header (which is 0 if the radio header is
6529 * variable-length; that header length is what we put
6530 * into the X register and then added to the index).
6532 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6535 sappend(inst
->s
, s
);
6551 * The offset is relative to the beginning of
6552 * the network-layer header.
6553 * XXX - are there any cases where we want
6556 s
= gen_off_macpl();
6559 * If "s" is non-null, it has code to arrange that the
6560 * X register contains the offset of the MAC-layer
6561 * payload. Add to it the offset computed into the
6562 * register specified by "index", and move that into
6563 * the X register. Otherwise, just load into the X
6564 * register the offset computed into the register specifed
6568 sappend(s
, xfer_to_a(inst
));
6569 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6570 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6572 s
= xfer_to_x(inst
);
6575 * Load the item at the sum of the offset we've put in the
6576 * X register, the offset of the start of the network
6577 * layer header from the beginning of the MAC-layer
6578 * payload, and the purported offset of the start of the
6579 * MAC-layer payload (which might be 0 if there's a
6580 * variable-length prefix before the link-layer header
6581 * or the link-layer header itself is variable-length;
6582 * the variable-length offset of the start of the
6583 * MAC-layer payload is what we put into the X register
6584 * and then added to the index).
6586 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6587 tmp
->s
.k
= off_macpl
+ off_nl
;
6589 sappend(inst
->s
, s
);
6592 * Do the computation only if the packet contains
6593 * the protocol in question.
6595 b
= gen_proto_abbrev(proto
);
6597 gen_and(inst
->b
, b
);
6610 * The offset is relative to the beginning of
6611 * the transport-layer header.
6613 * Load the X register with the length of the IPv4 header
6614 * (plus the offset of the link-layer header, if it's
6615 * a variable-length header), in bytes.
6617 * XXX - are there any cases where we want
6619 * XXX - we should, if we're built with
6620 * IPv6 support, generate code to load either
6621 * IPv4, IPv6, or both, as appropriate.
6623 s
= gen_loadx_iphdrlen();
6626 * The X register now contains the sum of the length
6627 * of any variable-length header preceding the link-layer
6628 * header, any variable-length link-layer header, and the
6629 * length of the network-layer header.
6631 * Load into the A register the offset relative to
6632 * the beginning of the transport layer header,
6633 * add the X register to that, move that to the
6634 * X register, and load with an offset from the
6635 * X register equal to the offset of the network
6636 * layer header relative to the beginning of
6637 * the MAC-layer payload plus the fixed-length
6638 * portion of the offset of the MAC-layer payload
6639 * from the beginning of the raw packet data.
6641 sappend(s
, xfer_to_a(inst
));
6642 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6643 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6644 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6645 tmp
->s
.k
= off_macpl
+ off_nl
;
6646 sappend(inst
->s
, s
);
6649 * Do the computation only if the packet contains
6650 * the protocol in question - which is true only
6651 * if this is an IP datagram and is the first or
6652 * only fragment of that datagram.
6654 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6656 gen_and(inst
->b
, b
);
6658 gen_and(gen_proto_abbrev(Q_IP
), b
);
6664 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6668 inst
->regno
= regno
;
6669 s
= new_stmt(BPF_ST
);
6671 sappend(inst
->s
, s
);
6677 gen_relation(code
, a0
, a1
, reversed
)
6679 struct arth
*a0
, *a1
;
6682 struct slist
*s0
, *s1
, *s2
;
6683 struct block
*b
, *tmp
;
6687 if (code
== BPF_JEQ
) {
6688 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6689 b
= new_block(JMP(code
));
6693 b
= new_block(BPF_JMP
|code
|BPF_X
);
6699 sappend(a0
->s
, a1
->s
);
6703 free_reg(a0
->regno
);
6704 free_reg(a1
->regno
);
6706 /* 'and' together protocol checks */
6709 gen_and(a0
->b
, tmp
= a1
->b
);
6725 int regno
= alloc_reg();
6726 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6729 s
= new_stmt(BPF_LD
|BPF_LEN
);
6730 s
->next
= new_stmt(BPF_ST
);
6731 s
->next
->s
.k
= regno
;
6746 a
= (struct arth
*)newchunk(sizeof(*a
));
6750 s
= new_stmt(BPF_LD
|BPF_IMM
);
6752 s
->next
= new_stmt(BPF_ST
);
6768 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6771 s
= new_stmt(BPF_ST
);
6779 gen_arth(code
, a0
, a1
)
6781 struct arth
*a0
, *a1
;
6783 struct slist
*s0
, *s1
, *s2
;
6787 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6792 sappend(a0
->s
, a1
->s
);
6794 free_reg(a0
->regno
);
6795 free_reg(a1
->regno
);
6797 s0
= new_stmt(BPF_ST
);
6798 a0
->regno
= s0
->s
.k
= alloc_reg();
6805 * Here we handle simple allocation of the scratch registers.
6806 * If too many registers are alloc'd, the allocator punts.
6808 static int regused
[BPF_MEMWORDS
];
6812 * Initialize the table of used registers and the current register.
6818 memset(regused
, 0, sizeof regused
);
6822 * Return the next free register.
6827 int n
= BPF_MEMWORDS
;
6830 if (regused
[curreg
])
6831 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6833 regused
[curreg
] = 1;
6837 bpf_error("too many registers needed to evaluate expression");
6843 * Return a register to the table so it can
6853 static struct block
*
6860 s
= new_stmt(BPF_LD
|BPF_LEN
);
6861 b
= new_block(JMP(jmp
));
6872 return gen_len(BPF_JGE
, n
);
6876 * Actually, this is less than or equal.
6884 b
= gen_len(BPF_JGT
, n
);
6891 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6892 * the beginning of the link-layer header.
6893 * XXX - that means you can't test values in the radiotap header, but
6894 * as that header is difficult if not impossible to parse generally
6895 * without a loop, that might not be a severe problem. A new keyword
6896 * "radio" could be added for that, although what you'd really want
6897 * would be a way of testing particular radio header values, which
6898 * would generate code appropriate to the radio header in question.
6901 gen_byteop(op
, idx
, val
)
6912 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6915 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6919 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6923 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6927 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6931 b
= new_block(JMP(BPF_JEQ
));
6938 static u_char abroadcast
[] = { 0x0 };
6941 gen_broadcast(proto
)
6944 bpf_u_int32 hostmask
;
6945 struct block
*b0
, *b1
, *b2
;
6946 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6954 case DLT_ARCNET_LINUX
:
6955 return gen_ahostop(abroadcast
, Q_DST
);
6957 return gen_ehostop(ebroadcast
, Q_DST
);
6959 return gen_fhostop(ebroadcast
, Q_DST
);
6961 return gen_thostop(ebroadcast
, Q_DST
);
6962 case DLT_IEEE802_11
:
6963 case DLT_PRISM_HEADER
:
6964 case DLT_IEEE802_11_RADIO_AVS
:
6965 case DLT_IEEE802_11_RADIO
:
6967 return gen_wlanhostop(ebroadcast
, Q_DST
);
6968 case DLT_IP_OVER_FC
:
6969 return gen_ipfchostop(ebroadcast
, Q_DST
);
6973 * Check that the packet doesn't begin with an
6974 * LE Control marker. (We've already generated
6977 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6982 * Now check the MAC address.
6984 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6990 bpf_error("not a broadcast link");
6995 b0
= gen_linktype(ETHERTYPE_IP
);
6996 hostmask
= ~netmask
;
6997 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6998 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6999 (bpf_int32
)(~0 & hostmask
), hostmask
);
7004 bpf_error("only link-layer/IP broadcast filters supported");
7010 * Generate code to test the low-order bit of a MAC address (that's
7011 * the bottom bit of the *first* byte).
7013 static struct block
*
7014 gen_mac_multicast(offset
)
7017 register struct block
*b0
;
7018 register struct slist
*s
;
7020 /* link[offset] & 1 != 0 */
7021 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7022 b0
= new_block(JMP(BPF_JSET
));
7029 gen_multicast(proto
)
7032 register struct block
*b0
, *b1
, *b2
;
7033 register struct slist
*s
;
7041 case DLT_ARCNET_LINUX
:
7042 /* all ARCnet multicasts use the same address */
7043 return gen_ahostop(abroadcast
, Q_DST
);
7045 /* ether[0] & 1 != 0 */
7046 return gen_mac_multicast(0);
7049 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7051 * XXX - was that referring to bit-order issues?
7053 /* fddi[1] & 1 != 0 */
7054 return gen_mac_multicast(1);
7056 /* tr[2] & 1 != 0 */
7057 return gen_mac_multicast(2);
7058 case DLT_IEEE802_11
:
7059 case DLT_PRISM_HEADER
:
7060 case DLT_IEEE802_11_RADIO_AVS
:
7061 case DLT_IEEE802_11_RADIO
:
7066 * For control frames, there is no DA.
7068 * For management frames, DA is at an
7069 * offset of 4 from the beginning of
7072 * For data frames, DA is at an offset
7073 * of 4 from the beginning of the packet
7074 * if To DS is clear and at an offset of
7075 * 16 from the beginning of the packet
7080 * Generate the tests to be done for data frames.
7082 * First, check for To DS set, i.e. "link[1] & 0x01".
7084 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7085 b1
= new_block(JMP(BPF_JSET
));
7086 b1
->s
.k
= 0x01; /* To DS */
7090 * If To DS is set, the DA is at 16.
7092 b0
= gen_mac_multicast(16);
7096 * Now, check for To DS not set, i.e. check
7097 * "!(link[1] & 0x01)".
7099 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7100 b2
= new_block(JMP(BPF_JSET
));
7101 b2
->s
.k
= 0x01; /* To DS */
7106 * If To DS is not set, the DA is at 4.
7108 b1
= gen_mac_multicast(4);
7112 * Now OR together the last two checks. That gives
7113 * the complete set of checks for data frames.
7118 * Now check for a data frame.
7119 * I.e, check "link[0] & 0x08".
7121 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7122 b1
= new_block(JMP(BPF_JSET
));
7127 * AND that with the checks done for data frames.
7132 * If the high-order bit of the type value is 0, this
7133 * is a management frame.
7134 * I.e, check "!(link[0] & 0x08)".
7136 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7137 b2
= new_block(JMP(BPF_JSET
));
7143 * For management frames, the DA is at 4.
7145 b1
= gen_mac_multicast(4);
7149 * OR that with the checks done for data frames.
7150 * That gives the checks done for management and
7156 * If the low-order bit of the type value is 1,
7157 * this is either a control frame or a frame
7158 * with a reserved type, and thus not a
7161 * I.e., check "!(link[0] & 0x04)".
7163 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7164 b1
= new_block(JMP(BPF_JSET
));
7170 * AND that with the checks for data and management
7175 case DLT_IP_OVER_FC
:
7176 b0
= gen_mac_multicast(2);
7181 * Check that the packet doesn't begin with an
7182 * LE Control marker. (We've already generated
7185 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7189 /* ether[off_mac] & 1 != 0 */
7190 b0
= gen_mac_multicast(off_mac
);
7198 /* Link not known to support multicasts */
7202 b0
= gen_linktype(ETHERTYPE_IP
);
7203 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7209 b0
= gen_linktype(ETHERTYPE_IPV6
);
7210 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7215 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7221 * generate command for inbound/outbound. It's here so we can
7222 * make it link-type specific. 'dir' = 0 implies "inbound",
7223 * = 1 implies "outbound".
7229 register struct block
*b0
;
7232 * Only some data link types support inbound/outbound qualifiers.
7236 b0
= gen_relation(BPF_JEQ
,
7237 gen_load(Q_LINK
, gen_loadi(0), 1),
7245 * Match packets sent by this machine.
7247 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7250 * Match packets sent to this machine.
7251 * (No broadcast or multicast packets, or
7252 * packets sent to some other machine and
7253 * received promiscuously.)
7255 * XXX - packets sent to other machines probably
7256 * shouldn't be matched, but what about broadcast
7257 * or multicast packets we received?
7259 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7263 #ifdef HAVE_NET_PFVAR_H
7265 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7266 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7272 /* match outgoing packets */
7273 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7275 /* match incoming packets */
7276 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7280 case DLT_JUNIPER_MFR
:
7281 case DLT_JUNIPER_MLFR
:
7282 case DLT_JUNIPER_MLPPP
:
7283 case DLT_JUNIPER_ATM1
:
7284 case DLT_JUNIPER_ATM2
:
7285 case DLT_JUNIPER_PPPOE
:
7286 case DLT_JUNIPER_PPPOE_ATM
:
7287 case DLT_JUNIPER_GGSN
:
7288 case DLT_JUNIPER_ES
:
7289 case DLT_JUNIPER_MONITOR
:
7290 case DLT_JUNIPER_SERVICES
:
7291 case DLT_JUNIPER_ETHER
:
7292 case DLT_JUNIPER_PPP
:
7293 case DLT_JUNIPER_FRELAY
:
7294 case DLT_JUNIPER_CHDLC
:
7295 case DLT_JUNIPER_VP
:
7296 case DLT_JUNIPER_ST
:
7297 case DLT_JUNIPER_ISM
:
7298 /* juniper flags (including direction) are stored
7299 * the byte after the 3-byte magic number */
7301 /* match outgoing packets */
7302 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7304 /* match incoming packets */
7305 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7310 bpf_error("inbound/outbound not supported on linktype %d",
7318 #ifdef HAVE_NET_PFVAR_H
7319 /* PF firewall log matched interface */
7321 gen_pf_ifname(const char *ifname
)
7326 if (linktype
!= DLT_PFLOG
) {
7327 bpf_error("ifname supported only on PF linktype");
7330 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7331 off
= offsetof(struct pfloghdr
, ifname
);
7332 if (strlen(ifname
) >= len
) {
7333 bpf_error("ifname interface names can only be %d characters",
7337 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7341 /* PF firewall log ruleset name */
7343 gen_pf_ruleset(char *ruleset
)
7347 if (linktype
!= DLT_PFLOG
) {
7348 bpf_error("ruleset supported only on PF linktype");
7352 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7353 bpf_error("ruleset names can only be %ld characters",
7354 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7358 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7359 strlen(ruleset
), (const u_char
*)ruleset
);
7363 /* PF firewall log rule number */
7369 if (linktype
!= DLT_PFLOG
) {
7370 bpf_error("rnr supported only on PF linktype");
7374 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7379 /* PF firewall log sub-rule number */
7381 gen_pf_srnr(int srnr
)
7385 if (linktype
!= DLT_PFLOG
) {
7386 bpf_error("srnr supported only on PF linktype");
7390 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7395 /* PF firewall log reason code */
7397 gen_pf_reason(int reason
)
7401 if (linktype
!= DLT_PFLOG
) {
7402 bpf_error("reason supported only on PF linktype");
7406 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7411 /* PF firewall log action */
7413 gen_pf_action(int action
)
7417 if (linktype
!= DLT_PFLOG
) {
7418 bpf_error("action supported only on PF linktype");
7422 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7426 #else /* !HAVE_NET_PFVAR_H */
7428 gen_pf_ifname(const char *ifname
)
7430 bpf_error("libpcap was compiled without pf support");
7436 gen_pf_ruleset(char *ruleset
)
7438 bpf_error("libpcap was compiled on a machine without pf support");
7446 bpf_error("libpcap was compiled on a machine without pf support");
7452 gen_pf_srnr(int srnr
)
7454 bpf_error("libpcap was compiled on a machine without pf support");
7460 gen_pf_reason(int reason
)
7462 bpf_error("libpcap was compiled on a machine without pf support");
7468 gen_pf_action(int action
)
7470 bpf_error("libpcap was compiled on a machine without pf support");
7474 #endif /* HAVE_NET_PFVAR_H */
7476 /* IEEE 802.11 wireless header */
7478 gen_p80211_type(int type
, int mask
)
7484 case DLT_IEEE802_11
:
7485 case DLT_PRISM_HEADER
:
7486 case DLT_IEEE802_11_RADIO_AVS
:
7487 case DLT_IEEE802_11_RADIO
:
7488 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7493 bpf_error("802.11 link-layer types supported only on 802.11");
7501 gen_p80211_fcdir(int fcdir
)
7507 case DLT_IEEE802_11
:
7508 case DLT_PRISM_HEADER
:
7509 case DLT_IEEE802_11_RADIO_AVS
:
7510 case DLT_IEEE802_11_RADIO
:
7514 bpf_error("frame direction supported only with 802.11 headers");
7518 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7519 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7526 register const u_char
*eaddr
;
7532 case DLT_ARCNET_LINUX
:
7533 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7535 return (gen_ahostop(eaddr
, (int)q
.dir
));
7537 bpf_error("ARCnet address used in non-arc expression");
7543 bpf_error("aid supported only on ARCnet");
7546 bpf_error("ARCnet address used in non-arc expression");
7551 static struct block
*
7552 gen_ahostop(eaddr
, dir
)
7553 register const u_char
*eaddr
;
7556 register struct block
*b0
, *b1
;
7559 /* src comes first, different from Ethernet */
7561 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7564 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7567 b0
= gen_ahostop(eaddr
, Q_SRC
);
7568 b1
= gen_ahostop(eaddr
, Q_DST
);
7574 b0
= gen_ahostop(eaddr
, Q_SRC
);
7575 b1
= gen_ahostop(eaddr
, Q_DST
);
7584 * support IEEE 802.1Q VLAN trunk over ethernet
7590 struct block
*b0
, *b1
;
7592 /* can't check for VLAN-encapsulated packets inside MPLS */
7593 if (label_stack_depth
> 0)
7594 bpf_error("no VLAN match after MPLS");
7597 * Check for a VLAN packet, and then change the offsets to point
7598 * to the type and data fields within the VLAN packet. Just
7599 * increment the offsets, so that we can support a hierarchy, e.g.
7600 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7603 * XXX - this is a bit of a kludge. If we were to split the
7604 * compiler into a parser that parses an expression and
7605 * generates an expression tree, and a code generator that
7606 * takes an expression tree (which could come from our
7607 * parser or from some other parser) and generates BPF code,
7608 * we could perhaps make the offsets parameters of routines
7609 * and, in the handler for an "AND" node, pass to subnodes
7610 * other than the VLAN node the adjusted offsets.
7612 * This would mean that "vlan" would, instead of changing the
7613 * behavior of *all* tests after it, change only the behavior
7614 * of tests ANDed with it. That would change the documented
7615 * semantics of "vlan", which might break some expressions.
7616 * However, it would mean that "(vlan and ip) or ip" would check
7617 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7618 * checking only for VLAN-encapsulated IP, so that could still
7619 * be considered worth doing; it wouldn't break expressions
7620 * that are of the form "vlan and ..." or "vlan N and ...",
7621 * which I suspect are the most common expressions involving
7622 * "vlan". "vlan or ..." doesn't necessarily do what the user
7623 * would really want, now, as all the "or ..." tests would
7624 * be done assuming a VLAN, even though the "or" could be viewed
7625 * as meaning "or, if this isn't a VLAN packet...".
7632 /* check for VLAN */
7633 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7634 (bpf_int32
)ETHERTYPE_8021Q
);
7636 /* If a specific VLAN is requested, check VLAN id */
7637 if (vlan_num
>= 0) {
7638 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7639 (bpf_int32
)vlan_num
, 0x0fff);
7653 bpf_error("no VLAN support for data link type %d",
7668 struct block
*b0
,*b1
;
7671 * Change the offsets to point to the type and data fields within
7672 * the MPLS packet. Just increment the offsets, so that we
7673 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7674 * capture packets with an outer label of 100000 and an inner
7677 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7681 if (label_stack_depth
> 0) {
7682 /* just match the bottom-of-stack bit clear */
7683 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7686 * Indicate that we're checking MPLS-encapsulated headers,
7687 * to make sure higher level code generators don't try to
7688 * match against IP-related protocols such as Q_ARP, Q_RARP
7693 case DLT_C_HDLC
: /* fall through */
7695 b0
= gen_linktype(ETHERTYPE_MPLS
);
7699 b0
= gen_linktype(PPP_MPLS_UCAST
);
7702 /* FIXME add other DLT_s ...
7703 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7704 * leave it for now */
7707 bpf_error("no MPLS support for data link type %d",
7715 /* If a specific MPLS label is requested, check it */
7716 if (label_num
>= 0) {
7717 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7718 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7719 0xfffff000); /* only compare the first 20 bits */
7726 label_stack_depth
++;
7731 * Support PPPOE discovery and session.
7736 /* check for PPPoE discovery */
7737 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7746 * Test against the PPPoE session link-layer type.
7748 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7751 * Change the offsets to point to the type and data fields within
7752 * the PPP packet, and note that this is PPPoE rather than
7755 * XXX - this is a bit of a kludge. If we were to split the
7756 * compiler into a parser that parses an expression and
7757 * generates an expression tree, and a code generator that
7758 * takes an expression tree (which could come from our
7759 * parser or from some other parser) and generates BPF code,
7760 * we could perhaps make the offsets parameters of routines
7761 * and, in the handler for an "AND" node, pass to subnodes
7762 * other than the PPPoE node the adjusted offsets.
7764 * This would mean that "pppoes" would, instead of changing the
7765 * behavior of *all* tests after it, change only the behavior
7766 * of tests ANDed with it. That would change the documented
7767 * semantics of "pppoes", which might break some expressions.
7768 * However, it would mean that "(pppoes and ip) or ip" would check
7769 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7770 * checking only for VLAN-encapsulated IP, so that could still
7771 * be considered worth doing; it wouldn't break expressions
7772 * that are of the form "pppoes and ..." which I suspect are the
7773 * most common expressions involving "pppoes". "pppoes or ..."
7774 * doesn't necessarily do what the user would really want, now,
7775 * as all the "or ..." tests would be done assuming PPPoE, even
7776 * though the "or" could be viewed as meaning "or, if this isn't
7777 * a PPPoE packet...".
7779 orig_linktype
= off_linktype
; /* save original values */
7784 * The "network-layer" protocol is PPPoE, which has a 6-byte
7785 * PPPoE header, followed by a PPP packet.
7787 * There is no HDLC encapsulation for the PPP packet (it's
7788 * encapsulated in PPPoES instead), so the link-layer type
7789 * starts at the first byte of the PPP packet. For PPPoE,
7790 * that offset is relative to the beginning of the total
7791 * link-layer payload, including any 802.2 LLC header, so
7792 * it's 6 bytes past off_nl.
7794 off_linktype
= off_nl
+ 6;
7797 * The network-layer offsets are relative to the beginning
7798 * of the MAC-layer payload; that's past the 6-byte
7799 * PPPoE header and the 2-byte PPP header.
7802 off_nl_nosnap
= 6+2;
7808 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7820 bpf_error("'vpi' supported only on raw ATM");
7821 if (off_vpi
== (u_int
)-1)
7823 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7829 bpf_error("'vci' supported only on raw ATM");
7830 if (off_vci
== (u_int
)-1)
7832 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7837 if (off_proto
== (u_int
)-1)
7838 abort(); /* XXX - this isn't on FreeBSD */
7839 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7844 if (off_payload
== (u_int
)-1)
7846 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7847 0xffffffff, jtype
, reverse
, jvalue
);
7852 bpf_error("'callref' supported only on raw ATM");
7853 if (off_proto
== (u_int
)-1)
7855 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7856 jtype
, reverse
, jvalue
);
7866 gen_atmtype_abbrev(type
)
7869 struct block
*b0
, *b1
;
7874 /* Get all packets in Meta signalling Circuit */
7876 bpf_error("'metac' supported only on raw ATM");
7877 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7878 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7883 /* Get all packets in Broadcast Circuit*/
7885 bpf_error("'bcc' supported only on raw ATM");
7886 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7887 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7892 /* Get all cells in Segment OAM F4 circuit*/
7894 bpf_error("'oam4sc' supported only on raw ATM");
7895 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7896 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7901 /* Get all cells in End-to-End OAM F4 Circuit*/
7903 bpf_error("'oam4ec' supported only on raw ATM");
7904 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7905 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7910 /* Get all packets in connection Signalling Circuit */
7912 bpf_error("'sc' supported only on raw ATM");
7913 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7914 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7919 /* Get all packets in ILMI Circuit */
7921 bpf_error("'ilmic' supported only on raw ATM");
7922 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7923 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7928 /* Get all LANE packets */
7930 bpf_error("'lane' supported only on raw ATM");
7931 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7934 * Arrange that all subsequent tests assume LANE
7935 * rather than LLC-encapsulated packets, and set
7936 * the offsets appropriately for LANE-encapsulated
7939 * "off_mac" is the offset of the Ethernet header,
7940 * which is 2 bytes past the ATM pseudo-header
7941 * (skipping the pseudo-header and 2-byte LE Client
7942 * field). The other offsets are Ethernet offsets
7943 * relative to "off_mac".
7946 off_mac
= off_payload
+ 2; /* MAC header */
7947 off_linktype
= off_mac
+ 12;
7948 off_macpl
= off_mac
+ 14; /* Ethernet */
7949 off_nl
= 0; /* Ethernet II */
7950 off_nl_nosnap
= 3; /* 802.3+802.2 */
7954 /* Get all LLC-encapsulated packets */
7956 bpf_error("'llc' supported only on raw ATM");
7957 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7968 * Filtering for MTP2 messages based on li value
7969 * FISU, length is null
7970 * LSSU, length is 1 or 2
7971 * MSU, length is 3 or more
7974 gen_mtp2type_abbrev(type
)
7977 struct block
*b0
, *b1
;
7982 if ( (linktype
!= DLT_MTP2
) &&
7983 (linktype
!= DLT_ERF
) &&
7984 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7985 bpf_error("'fisu' supported only on MTP2");
7986 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7987 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7991 if ( (linktype
!= DLT_MTP2
) &&
7992 (linktype
!= DLT_ERF
) &&
7993 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7994 bpf_error("'lssu' supported only on MTP2");
7995 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7996 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8001 if ( (linktype
!= DLT_MTP2
) &&
8002 (linktype
!= DLT_ERF
) &&
8003 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8004 bpf_error("'msu' supported only on MTP2");
8005 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8015 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8022 bpf_u_int32 val1
, val2
, val3
;
8024 switch (mtp3field
) {
8027 if (off_sio
== (u_int
)-1)
8028 bpf_error("'sio' supported only on SS7");
8029 /* sio coded on 1 byte so max value 255 */
8031 bpf_error("sio value %u too big; max value = 255",
8033 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8034 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8038 if (off_opc
== (u_int
)-1)
8039 bpf_error("'opc' supported only on SS7");
8040 /* opc coded on 14 bits so max value 16383 */
8042 bpf_error("opc value %u too big; max value = 16383",
8044 /* the following instructions are made to convert jvalue
8045 * to the form used to write opc in an ss7 message*/
8046 val1
= jvalue
& 0x00003c00;
8048 val2
= jvalue
& 0x000003fc;
8050 val3
= jvalue
& 0x00000003;
8052 jvalue
= val1
+ val2
+ val3
;
8053 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8054 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8058 if (off_dpc
== (u_int
)-1)
8059 bpf_error("'dpc' supported only on SS7");
8060 /* dpc coded on 14 bits so max value 16383 */
8062 bpf_error("dpc value %u too big; max value = 16383",
8064 /* the following instructions are made to convert jvalue
8065 * to the forme used to write dpc in an ss7 message*/
8066 val1
= jvalue
& 0x000000ff;
8068 val2
= jvalue
& 0x00003f00;
8070 jvalue
= val1
+ val2
;
8071 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8072 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8076 if (off_sls
== (u_int
)-1)
8077 bpf_error("'sls' supported only on SS7");
8078 /* sls coded on 4 bits so max value 15 */
8080 bpf_error("sls value %u too big; max value = 15",
8082 /* the following instruction is made to convert jvalue
8083 * to the forme used to write sls in an ss7 message*/
8084 jvalue
= jvalue
<< 4;
8085 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8086 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8095 static struct block
*
8096 gen_msg_abbrev(type
)
8102 * Q.2931 signalling protocol messages for handling virtual circuits
8103 * establishment and teardown
8108 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8112 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8116 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8120 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8124 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8127 case A_RELEASE_DONE
:
8128 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8138 gen_atmmulti_abbrev(type
)
8141 struct block
*b0
, *b1
;
8147 bpf_error("'oam' supported only on raw ATM");
8148 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8153 bpf_error("'oamf4' supported only on raw ATM");
8155 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8156 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8158 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8164 * Get Q.2931 signalling messages for switched
8165 * virtual connection
8168 bpf_error("'connectmsg' supported only on raw ATM");
8169 b0
= gen_msg_abbrev(A_SETUP
);
8170 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8172 b0
= gen_msg_abbrev(A_CONNECT
);
8174 b0
= gen_msg_abbrev(A_CONNECTACK
);
8176 b0
= gen_msg_abbrev(A_RELEASE
);
8178 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8180 b0
= gen_atmtype_abbrev(A_SC
);
8186 bpf_error("'metaconnect' supported only on raw ATM");
8187 b0
= gen_msg_abbrev(A_SETUP
);
8188 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8190 b0
= gen_msg_abbrev(A_CONNECT
);
8192 b0
= gen_msg_abbrev(A_RELEASE
);
8194 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8196 b0
= gen_atmtype_abbrev(A_METAC
);