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.19 2008-12-23 20:14:12 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
71 #include "ieee80211.h"
73 #include "sunatmpos.h"
77 #ifdef HAVE_NET_PFVAR_H
78 #include <sys/socket.h>
80 #include <net/pfvar.h>
81 #include <net/if_pflog.h>
84 #define offsetof(s, e) ((size_t)&((s *)0)->e)
88 #include <netdb.h> /* for "struct addrinfo" */
91 #include <pcap/namedb.h>
96 #define IPPROTO_SCTP 132
99 #ifdef HAVE_OS_PROTO_H
100 #include "os-proto.h"
103 #define JMP(c) ((c)|BPF_JMP|BPF_K)
106 static jmp_buf top_ctx
;
107 static pcap_t
*bpf_pcap
;
109 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
111 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
113 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
118 static int pcap_fddipad
;
123 bpf_error(const char *fmt
, ...)
128 if (bpf_pcap
!= NULL
)
129 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
136 static void init_linktype(pcap_t
*);
138 static void init_regs(void);
139 static int alloc_reg(void);
140 static void free_reg(int);
142 static struct block
*root
;
145 * Value passed to gen_load_a() to indicate what the offset argument
149 OR_PACKET
, /* relative to the beginning of the packet */
150 OR_LINK
, /* relative to the beginning of the link-layer header */
151 OR_MACPL
, /* relative to the end of the MAC-layer header */
152 OR_NET
, /* relative to the network-layer header */
153 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
154 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
155 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
159 * We divy out chunks of memory rather than call malloc each time so
160 * we don't have to worry about leaking memory. It's probably
161 * not a big deal if all this memory was wasted but if this ever
162 * goes into a library that would probably not be a good idea.
164 * XXX - this *is* in a library....
167 #define CHUNK0SIZE 1024
173 static struct chunk chunks
[NCHUNKS
];
174 static int cur_chunk
;
176 static void *newchunk(u_int
);
177 static void freechunks(void);
178 static inline struct block
*new_block(int);
179 static inline struct slist
*new_stmt(int);
180 static struct block
*gen_retblk(int);
181 static inline void syntax(void);
183 static void backpatch(struct block
*, struct block
*);
184 static void merge(struct block
*, struct block
*);
185 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
186 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
187 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
188 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
189 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
190 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
192 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
193 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
194 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
195 static struct slist
*gen_load_llrel(u_int
, u_int
);
196 static struct slist
*gen_load_macplrel(u_int
, u_int
);
197 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
198 static struct slist
*gen_loadx_iphdrlen(void);
199 static struct block
*gen_uncond(int);
200 static inline struct block
*gen_true(void);
201 static inline struct block
*gen_false(void);
202 static struct block
*gen_ether_linktype(int);
203 static struct block
*gen_linux_sll_linktype(int);
204 static struct slist
*gen_load_prism_llprefixlen(void);
205 static struct slist
*gen_load_avs_llprefixlen(void);
206 static struct slist
*gen_load_radiotap_llprefixlen(void);
207 static struct slist
*gen_load_ppi_llprefixlen(void);
208 static void insert_compute_vloffsets(struct block
*);
209 static struct slist
*gen_llprefixlen(void);
210 static struct slist
*gen_off_macpl(void);
211 static int ethertype_to_ppptype(int);
212 static struct block
*gen_linktype(int);
213 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
214 static struct block
*gen_llc_linktype(int);
215 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
217 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
219 static struct block
*gen_ahostop(const u_char
*, int);
220 static struct block
*gen_ehostop(const u_char
*, int);
221 static struct block
*gen_fhostop(const u_char
*, int);
222 static struct block
*gen_thostop(const u_char
*, int);
223 static struct block
*gen_wlanhostop(const u_char
*, int);
224 static struct block
*gen_ipfchostop(const u_char
*, int);
225 static struct block
*gen_dnhostop(bpf_u_int32
, int);
226 static struct block
*gen_mpls_linktype(int);
227 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
229 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
232 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
234 static struct block
*gen_ipfrag(void);
235 static struct block
*gen_portatom(int, bpf_int32
);
236 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
238 static struct block
*gen_portatom6(int, bpf_int32
);
239 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
241 struct block
*gen_portop(int, int, int);
242 static struct block
*gen_port(int, int, int);
243 struct block
*gen_portrangeop(int, int, int, int);
244 static struct block
*gen_portrange(int, int, int, int);
246 struct block
*gen_portop6(int, int, int);
247 static struct block
*gen_port6(int, int, int);
248 struct block
*gen_portrangeop6(int, int, int, int);
249 static struct block
*gen_portrange6(int, int, int, int);
251 static int lookup_proto(const char *, int);
252 static struct block
*gen_protochain(int, int, int);
253 static struct block
*gen_proto(int, int, int);
254 static struct slist
*xfer_to_x(struct arth
*);
255 static struct slist
*xfer_to_a(struct arth
*);
256 static struct block
*gen_mac_multicast(int);
257 static struct block
*gen_len(int, int);
258 static struct block
*gen_check_802_11_data_frame(void);
260 static struct block
*gen_ppi_dlt_check(void);
261 static struct block
*gen_msg_abbrev(int type
);
272 /* XXX Round up to nearest long. */
273 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
275 /* XXX Round up to structure boundary. */
279 cp
= &chunks
[cur_chunk
];
280 if (n
> cp
->n_left
) {
281 ++cp
, k
= ++cur_chunk
;
283 bpf_error("out of memory");
284 size
= CHUNK0SIZE
<< k
;
285 cp
->m
= (void *)malloc(size
);
287 bpf_error("out of memory");
288 memset((char *)cp
->m
, 0, size
);
291 bpf_error("out of memory");
294 return (void *)((char *)cp
->m
+ cp
->n_left
);
303 for (i
= 0; i
< NCHUNKS
; ++i
)
304 if (chunks
[i
].m
!= NULL
) {
311 * A strdup whose allocations are freed after code generation is over.
315 register const char *s
;
317 int n
= strlen(s
) + 1;
318 char *cp
= newchunk(n
);
324 static inline struct block
*
330 p
= (struct block
*)newchunk(sizeof(*p
));
337 static inline struct slist
*
343 p
= (struct slist
*)newchunk(sizeof(*p
));
349 static struct block
*
353 struct block
*b
= new_block(BPF_RET
|BPF_K
);
362 bpf_error("syntax error in filter expression");
365 static bpf_u_int32 netmask
;
370 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
371 const char *buf
, int optimize
, bpf_u_int32 mask
)
374 const char * volatile xbuf
= buf
;
382 if (setjmp(top_ctx
)) {
390 snaplen
= pcap_snapshot(p
);
392 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
393 "snaplen of 0 rejects all packets");
397 lex_init(xbuf
? xbuf
: "");
405 root
= gen_retblk(snaplen
);
407 if (optimize
&& !no_optimize
) {
410 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
411 bpf_error("expression rejects all packets");
413 program
->bf_insns
= icode_to_fcode(root
, &len
);
414 program
->bf_len
= len
;
422 * entry point for using the compiler with no pcap open
423 * pass in all the stuff that is needed explicitly instead.
426 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
427 struct bpf_program
*program
,
428 const char *buf
, int optimize
, bpf_u_int32 mask
)
433 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
436 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
442 * Clean up a "struct bpf_program" by freeing all the memory allocated
446 pcap_freecode(struct bpf_program
*program
)
449 if (program
->bf_insns
!= NULL
) {
450 free((char *)program
->bf_insns
);
451 program
->bf_insns
= NULL
;
456 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
457 * which of the jt and jf fields has been resolved and which is a pointer
458 * back to another unresolved block (or nil). At least one of the fields
459 * in each block is already resolved.
462 backpatch(list
, target
)
463 struct block
*list
, *target
;
480 * Merge the lists in b0 and b1, using the 'sense' field to indicate
481 * which of jt and jf is the link.
485 struct block
*b0
, *b1
;
487 register struct block
**p
= &b0
;
489 /* Find end of list. */
491 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
493 /* Concatenate the lists. */
501 struct block
*ppi_dlt_check
;
504 * Insert before the statements of the first (root) block any
505 * statements needed to load the lengths of any variable-length
506 * headers into registers.
508 * XXX - a fancier strategy would be to insert those before the
509 * statements of all blocks that use those lengths and that
510 * have no predecessors that use them, so that we only compute
511 * the lengths if we need them. There might be even better
512 * approaches than that.
514 * However, those strategies would be more complicated, and
515 * as we don't generate code to compute a length if the
516 * program has no tests that use the length, and as most
517 * tests will probably use those lengths, we would just
518 * postpone computing the lengths so that it's not done
519 * for tests that fail early, and it's not clear that's
522 insert_compute_vloffsets(p
->head
);
525 * For DLT_PPI captures, generate a check of the per-packet
526 * DLT value to make sure it's DLT_IEEE802_11.
528 ppi_dlt_check
= gen_ppi_dlt_check();
529 if (ppi_dlt_check
!= NULL
)
530 gen_and(ppi_dlt_check
, p
);
532 backpatch(p
, gen_retblk(snaplen
));
533 p
->sense
= !p
->sense
;
534 backpatch(p
, gen_retblk(0));
540 struct block
*b0
, *b1
;
542 backpatch(b0
, b1
->head
);
543 b0
->sense
= !b0
->sense
;
544 b1
->sense
= !b1
->sense
;
546 b1
->sense
= !b1
->sense
;
552 struct block
*b0
, *b1
;
554 b0
->sense
= !b0
->sense
;
555 backpatch(b0
, b1
->head
);
556 b0
->sense
= !b0
->sense
;
565 b
->sense
= !b
->sense
;
568 static struct block
*
569 gen_cmp(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
577 static struct block
*
578 gen_cmp_gt(offrel
, offset
, size
, v
)
579 enum e_offrel offrel
;
583 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
586 static struct block
*
587 gen_cmp_ge(offrel
, offset
, size
, v
)
588 enum e_offrel offrel
;
592 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
595 static struct block
*
596 gen_cmp_lt(offrel
, offset
, size
, v
)
597 enum e_offrel offrel
;
601 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
604 static struct block
*
605 gen_cmp_le(offrel
, offset
, size
, v
)
606 enum e_offrel offrel
;
610 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
613 static struct block
*
614 gen_mcmp(offrel
, offset
, size
, v
, mask
)
615 enum e_offrel offrel
;
620 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
623 static struct block
*
624 gen_bcmp(offrel
, offset
, size
, v
)
625 enum e_offrel offrel
;
626 register u_int offset
, size
;
627 register const u_char
*v
;
629 register struct block
*b
, *tmp
;
633 register const u_char
*p
= &v
[size
- 4];
634 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
635 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
637 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
644 register const u_char
*p
= &v
[size
- 2];
645 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
647 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
654 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
663 * AND the field of size "size" at offset "offset" relative to the header
664 * specified by "offrel" with "mask", and compare it with the value "v"
665 * with the test specified by "jtype"; if "reverse" is true, the test
666 * should test the opposite of "jtype".
668 static struct block
*
669 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
670 enum e_offrel offrel
;
672 bpf_u_int32 offset
, size
, mask
, jtype
;
675 struct slist
*s
, *s2
;
678 s
= gen_load_a(offrel
, offset
, size
);
680 if (mask
!= 0xffffffff) {
681 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
686 b
= new_block(JMP(jtype
));
689 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
695 * Various code constructs need to know the layout of the data link
696 * layer. These variables give the necessary offsets from the beginning
697 * of the packet data.
701 * This is the offset of the beginning of the link-layer header from
702 * the beginning of the raw packet data.
704 * It's usually 0, except for 802.11 with a fixed-length radio header.
705 * (For 802.11 with a variable-length radio header, we have to generate
706 * code to compute that offset; off_ll is 0 in that case.)
711 * If there's a variable-length header preceding the link-layer header,
712 * "reg_off_ll" is the register number for a register containing the
713 * length of that header, and therefore the offset of the link-layer
714 * header from the beginning of the raw packet data. Otherwise,
715 * "reg_off_ll" is -1.
717 static int reg_off_ll
;
720 * This is the offset of the beginning of the MAC-layer header from
721 * the beginning of the link-layer header.
722 * It's usually 0, except for ATM LANE, where it's the offset, relative
723 * to the beginning of the raw packet data, of the Ethernet header.
725 static u_int off_mac
;
728 * This is the offset of the beginning of the MAC-layer payload,
729 * from the beginning of the raw packet data.
731 * I.e., it's the sum of the length of the link-layer header (without,
732 * for example, any 802.2 LLC header, so it's the MAC-layer
733 * portion of that header), plus any prefix preceding the
736 static u_int off_macpl
;
739 * This is 1 if the offset of the beginning of the MAC-layer payload
740 * from the beginning of the link-layer header is variable-length.
742 static int off_macpl_is_variable
;
745 * If the link layer has variable_length headers, "reg_off_macpl"
746 * is the register number for a register containing the length of the
747 * link-layer header plus the length of any variable-length header
748 * preceding the link-layer header. Otherwise, "reg_off_macpl"
751 static int reg_off_macpl
;
754 * "off_linktype" is the offset to information in the link-layer header
755 * giving the packet type. This offset is relative to the beginning
756 * of the link-layer header (i.e., it doesn't include off_ll).
758 * For Ethernet, it's the offset of the Ethernet type field.
760 * For link-layer types that always use 802.2 headers, it's the
761 * offset of the LLC header.
763 * For PPP, it's the offset of the PPP type field.
765 * For Cisco HDLC, it's the offset of the CHDLC type field.
767 * For BSD loopback, it's the offset of the AF_ value.
769 * For Linux cooked sockets, it's the offset of the type field.
771 * It's set to -1 for no encapsulation, in which case, IP is assumed.
773 static u_int off_linktype
;
776 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
777 * checks to check the PPP header, assumed to follow a LAN-style link-
778 * layer header and a PPPoE session header.
780 static int is_pppoes
= 0;
783 * TRUE if the link layer includes an ATM pseudo-header.
785 static int is_atm
= 0;
788 * TRUE if "lane" appeared in the filter; it causes us to generate
789 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
791 static int is_lane
= 0;
794 * These are offsets for the ATM pseudo-header.
796 static u_int off_vpi
;
797 static u_int off_vci
;
798 static u_int off_proto
;
801 * These are offsets for the MTP2 fields.
806 * These are offsets for the MTP3 fields.
808 static u_int off_sio
;
809 static u_int off_opc
;
810 static u_int off_dpc
;
811 static u_int off_sls
;
814 * This is the offset of the first byte after the ATM pseudo_header,
815 * or -1 if there is no ATM pseudo-header.
817 static u_int off_payload
;
820 * These are offsets to the beginning of the network-layer header.
821 * They are relative to the beginning of the MAC-layer payload (i.e.,
822 * they don't include off_ll or off_macpl).
824 * If the link layer never uses 802.2 LLC:
826 * "off_nl" and "off_nl_nosnap" are the same.
828 * If the link layer always uses 802.2 LLC:
830 * "off_nl" is the offset if there's a SNAP header following
833 * "off_nl_nosnap" is the offset if there's no SNAP header.
835 * If the link layer is Ethernet:
837 * "off_nl" is the offset if the packet is an Ethernet II packet
838 * (we assume no 802.3+802.2+SNAP);
840 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
841 * with an 802.2 header following it.
844 static u_int off_nl_nosnap
;
852 linktype
= pcap_datalink(p
);
854 pcap_fddipad
= p
->fddipad
;
858 * Assume it's not raw ATM with a pseudo-header, for now.
869 * And that we're not doing PPPoE.
874 * And assume we're not doing SS7.
883 * Also assume it's not 802.11.
887 off_macpl_is_variable
= 0;
891 label_stack_depth
= 0;
901 off_nl
= 0; /* XXX in reality, variable! */
902 off_nl_nosnap
= 0; /* no 802.2 LLC */
905 case DLT_ARCNET_LINUX
:
908 off_nl
= 0; /* XXX in reality, variable! */
909 off_nl_nosnap
= 0; /* no 802.2 LLC */
914 off_macpl
= 14; /* Ethernet header length */
915 off_nl
= 0; /* Ethernet II */
916 off_nl_nosnap
= 3; /* 802.3+802.2 */
921 * SLIP doesn't have a link level type. The 16 byte
922 * header is hacked into our SLIP driver.
927 off_nl_nosnap
= 0; /* no 802.2 LLC */
931 /* XXX this may be the same as the DLT_PPP_BSDOS case */
936 off_nl_nosnap
= 0; /* no 802.2 LLC */
944 off_nl_nosnap
= 0; /* no 802.2 LLC */
951 off_nl_nosnap
= 0; /* no 802.2 LLC */
956 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
957 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 * This does no include the Ethernet header, and
967 * only covers session state.
972 off_nl_nosnap
= 0; /* no 802.2 LLC */
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
984 * FDDI doesn't really have a link-level type field.
985 * We set "off_linktype" to the offset of the LLC header.
987 * To check for Ethernet types, we assume that SSAP = SNAP
988 * is being used and pick out the encapsulated Ethernet type.
989 * XXX - should we generate code to check for SNAP?
993 off_linktype
+= pcap_fddipad
;
995 off_macpl
= 13; /* FDDI MAC header length */
997 off_macpl
+= pcap_fddipad
;
999 off_nl
= 8; /* 802.2+SNAP */
1000 off_nl_nosnap
= 3; /* 802.2 */
1005 * Token Ring doesn't really have a link-level type field.
1006 * We set "off_linktype" to the offset of the LLC header.
1008 * To check for Ethernet types, we assume that SSAP = SNAP
1009 * is being used and pick out the encapsulated Ethernet type.
1010 * XXX - should we generate code to check for SNAP?
1012 * XXX - the header is actually variable-length.
1013 * Some various Linux patched versions gave 38
1014 * as "off_linktype" and 40 as "off_nl"; however,
1015 * if a token ring packet has *no* routing
1016 * information, i.e. is not source-routed, the correct
1017 * values are 20 and 22, as they are in the vanilla code.
1019 * A packet is source-routed iff the uppermost bit
1020 * of the first byte of the source address, at an
1021 * offset of 8, has the uppermost bit set. If the
1022 * packet is source-routed, the total number of bytes
1023 * of routing information is 2 plus bits 0x1F00 of
1024 * the 16-bit value at an offset of 14 (shifted right
1025 * 8 - figure out which byte that is).
1028 off_macpl
= 14; /* Token Ring MAC header length */
1029 off_nl
= 8; /* 802.2+SNAP */
1030 off_nl_nosnap
= 3; /* 802.2 */
1033 case DLT_IEEE802_11
:
1034 case DLT_PRISM_HEADER
:
1035 case DLT_IEEE802_11_RADIO_AVS
:
1036 case DLT_IEEE802_11_RADIO
:
1038 * 802.11 doesn't really have a link-level type field.
1039 * We set "off_linktype" to the offset of the LLC header.
1041 * To check for Ethernet types, we assume that SSAP = SNAP
1042 * is being used and pick out the encapsulated Ethernet type.
1043 * XXX - should we generate code to check for SNAP?
1045 * We also handle variable-length radio headers here.
1046 * The Prism header is in theory variable-length, but in
1047 * practice it's always 144 bytes long. However, some
1048 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1049 * sometimes or always supply an AVS header, so we
1050 * have to check whether the radio header is a Prism
1051 * header or an AVS header, so, in practice, it's
1055 off_macpl
= 0; /* link-layer header is variable-length */
1056 off_macpl_is_variable
= 1;
1057 off_nl
= 8; /* 802.2+SNAP */
1058 off_nl_nosnap
= 3; /* 802.2 */
1063 * At the moment we treat PPI the same way that we treat
1064 * normal Radiotap encoded packets. The difference is in
1065 * the function that generates the code at the beginning
1066 * to compute the header length. Since this code generator
1067 * of PPI supports bare 802.11 encapsulation only (i.e.
1068 * the encapsulated DLT should be DLT_IEEE802_11) we
1069 * generate code to check for this too.
1072 off_macpl
= 0; /* link-layer header is variable-length */
1073 off_macpl_is_variable
= 1;
1074 off_nl
= 8; /* 802.2+SNAP */
1075 off_nl_nosnap
= 3; /* 802.2 */
1078 case DLT_ATM_RFC1483
:
1079 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1081 * assume routed, non-ISO PDUs
1082 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1084 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1085 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1086 * latter would presumably be treated the way PPPoE
1087 * should be, so you can do "pppoe and udp port 2049"
1088 * or "pppoa and tcp port 80" and have it check for
1089 * PPPo{A,E} and a PPP protocol of IP and....
1092 off_macpl
= 0; /* packet begins with LLC header */
1093 off_nl
= 8; /* 802.2+SNAP */
1094 off_nl_nosnap
= 3; /* 802.2 */
1099 * Full Frontal ATM; you get AALn PDUs with an ATM
1103 off_vpi
= SUNATM_VPI_POS
;
1104 off_vci
= SUNATM_VCI_POS
;
1105 off_proto
= PROTO_POS
;
1106 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1107 off_payload
= SUNATM_PKT_BEGIN_POS
;
1108 off_linktype
= off_payload
;
1109 off_macpl
= off_payload
; /* if LLC-encapsulated */
1110 off_nl
= 8; /* 802.2+SNAP */
1111 off_nl_nosnap
= 3; /* 802.2 */
1118 off_nl_nosnap
= 0; /* no 802.2 LLC */
1121 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1125 off_nl_nosnap
= 0; /* no 802.2 LLC */
1130 * LocalTalk does have a 1-byte type field in the LLAP header,
1131 * but really it just indicates whether there is a "short" or
1132 * "long" DDP packet following.
1137 off_nl_nosnap
= 0; /* no 802.2 LLC */
1140 case DLT_IP_OVER_FC
:
1142 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1143 * link-level type field. We set "off_linktype" to the
1144 * offset of the LLC header.
1146 * To check for Ethernet types, we assume that SSAP = SNAP
1147 * is being used and pick out the encapsulated Ethernet type.
1148 * XXX - should we generate code to check for SNAP? RFC
1149 * 2625 says SNAP should be used.
1153 off_nl
= 8; /* 802.2+SNAP */
1154 off_nl_nosnap
= 3; /* 802.2 */
1159 * XXX - we should set this to handle SNAP-encapsulated
1160 * frames (NLPID of 0x80).
1165 off_nl_nosnap
= 0; /* no 802.2 LLC */
1169 * the only BPF-interesting FRF.16 frames are non-control frames;
1170 * Frame Relay has a variable length link-layer
1171 * so lets start with offset 4 for now and increments later on (FIXME);
1177 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1180 case DLT_APPLE_IP_OVER_IEEE1394
:
1184 off_nl_nosnap
= 0; /* no 802.2 LLC */
1187 case DLT_LINUX_IRDA
:
1189 * Currently, only raw "link[N:M]" filtering is supported.
1199 * Currently, only raw "link[N:M]" filtering is supported.
1207 case DLT_SYMANTEC_FIREWALL
:
1210 off_nl
= 0; /* Ethernet II */
1211 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1214 #ifdef HAVE_NET_PFVAR_H
1217 off_macpl
= PFLOG_HDRLEN
;
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 case DLT_JUNIPER_MFR
:
1224 case DLT_JUNIPER_MLFR
:
1225 case DLT_JUNIPER_MLPPP
:
1226 case DLT_JUNIPER_PPP
:
1227 case DLT_JUNIPER_CHDLC
:
1228 case DLT_JUNIPER_FRELAY
:
1232 off_nl_nosnap
= -1; /* no 802.2 LLC */
1235 case DLT_JUNIPER_ATM1
:
1236 off_linktype
= 4; /* in reality variable between 4-8 */
1237 off_macpl
= 4; /* in reality variable between 4-8 */
1242 case DLT_JUNIPER_ATM2
:
1243 off_linktype
= 8; /* in reality variable between 8-12 */
1244 off_macpl
= 8; /* in reality variable between 8-12 */
1249 /* frames captured on a Juniper PPPoE service PIC
1250 * contain raw ethernet frames */
1251 case DLT_JUNIPER_PPPOE
:
1252 case DLT_JUNIPER_ETHER
:
1255 off_nl
= 18; /* Ethernet II */
1256 off_nl_nosnap
= 21; /* 802.3+802.2 */
1259 case DLT_JUNIPER_PPPOE_ATM
:
1263 off_nl_nosnap
= -1; /* no 802.2 LLC */
1266 case DLT_JUNIPER_GGSN
:
1270 off_nl_nosnap
= -1; /* no 802.2 LLC */
1273 case DLT_JUNIPER_ES
:
1275 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1276 off_nl
= -1; /* not really a network layer but raw IP addresses */
1277 off_nl_nosnap
= -1; /* no 802.2 LLC */
1280 case DLT_JUNIPER_MONITOR
:
1283 off_nl
= 0; /* raw IP/IP6 header */
1284 off_nl_nosnap
= -1; /* no 802.2 LLC */
1287 case DLT_JUNIPER_SERVICES
:
1289 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1290 off_nl
= -1; /* L3 proto location dep. on cookie type */
1291 off_nl_nosnap
= -1; /* no 802.2 LLC */
1294 case DLT_JUNIPER_VP
:
1301 case DLT_JUNIPER_ST
:
1308 case DLT_JUNIPER_ISM
:
1327 case DLT_MTP2_WITH_PHDR
:
1360 case DLT_LINUX_LAPD
:
1362 * Currently, only raw "link[N:M]" filtering is supported.
1372 * Currently, only raw "link[N:M]" filtering is supported.
1380 case DLT_BLUETOOTH_HCI_H4
:
1382 * Currently, only raw "link[N:M]" filtering is supported.
1392 * Currently, only raw "link[N:M]" filtering is supported.
1402 * Currently, only raw "link[N:M]" filtering is supported.
1410 case DLT_IEEE802_15_4_LINUX
:
1412 * Currently, only raw "link[N:M]" filtering is supported.
1420 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1422 * Currently, only raw "link[N:M]" filtering is supported.
1430 case DLT_IEEE802_15_4
:
1432 * Currently, only raw "link[N:M]" filtering is supported.
1442 * Currently, only raw "link[N:M]" filtering is supported.
1452 * Currently, only raw "link[N:M]" filtering is supported.
1462 * Currently, only raw "link[N:M]" filtering is supported.
1470 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1472 * Currently, only raw "link[N:M]" filtering is supported.
1482 * Currently, only raw "link[N:M]" filtering is supported.
1484 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1486 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1487 off_nl_nosnap
= -1; /* no 802.2 LLC */
1488 off_mac
= 1; /* step over the kiss length byte */
1491 case DLT_IEEE802_15_4_NONASK_PHY
:
1493 * Currently, only raw "link[N:M]" filtering is supported.
1503 * Currently, only raw "link[N:M]" filtering is supported.
1511 case DLT_USB_LINUX_MMAPPED
:
1513 * Currently, only raw "link[N:M]" filtering is supported.
1521 bpf_error("unknown data link type %d", linktype
);
1526 * Load a value relative to the beginning of the link-layer header.
1527 * The link-layer header doesn't necessarily begin at the beginning
1528 * of the packet data; there might be a variable-length prefix containing
1529 * radio information.
1531 static struct slist
*
1532 gen_load_llrel(offset
, size
)
1535 struct slist
*s
, *s2
;
1537 s
= gen_llprefixlen();
1540 * If "s" is non-null, it has code to arrange that the X register
1541 * contains the length of the prefix preceding the link-layer
1544 * Otherwise, the length of the prefix preceding the link-layer
1545 * header is "off_ll".
1549 * There's a variable-length prefix preceding the
1550 * link-layer header. "s" points to a list of statements
1551 * that put the length of that prefix into the X register.
1552 * do an indirect load, to use the X register as an offset.
1554 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1559 * There is no variable-length header preceding the
1560 * link-layer header; add in off_ll, which, if there's
1561 * a fixed-length header preceding the link-layer header,
1562 * is the length of that header.
1564 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1565 s
->s
.k
= offset
+ off_ll
;
1571 * Load a value relative to the beginning of the MAC-layer payload.
1573 static struct slist
*
1574 gen_load_macplrel(offset
, size
)
1577 struct slist
*s
, *s2
;
1579 s
= gen_off_macpl();
1582 * If s is non-null, the offset of the MAC-layer payload is
1583 * variable, and s points to a list of instructions that
1584 * arrange that the X register contains that offset.
1586 * Otherwise, the offset of the MAC-layer payload is constant,
1587 * and is in off_macpl.
1591 * The offset of the MAC-layer payload is in the X
1592 * register. Do an indirect load, to use the X register
1595 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1600 * The offset of the MAC-layer payload is constant,
1601 * and is in off_macpl; load the value at that offset
1602 * plus the specified offset.
1604 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1605 s
->s
.k
= off_macpl
+ offset
;
1611 * Load a value relative to the beginning of the specified header.
1613 static struct slist
*
1614 gen_load_a(offrel
, offset
, size
)
1615 enum e_offrel offrel
;
1618 struct slist
*s
, *s2
;
1623 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1628 s
= gen_load_llrel(offset
, size
);
1632 s
= gen_load_macplrel(offset
, size
);
1636 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1640 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1645 * Load the X register with the length of the IPv4 header
1646 * (plus the offset of the link-layer header, if it's
1647 * preceded by a variable-length header such as a radio
1648 * header), in bytes.
1650 s
= gen_loadx_iphdrlen();
1653 * Load the item at {offset of the MAC-layer payload} +
1654 * {offset, relative to the start of the MAC-layer
1655 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1656 * {specified offset}.
1658 * (If the offset of the MAC-layer payload is variable,
1659 * it's included in the value in the X register, and
1662 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1663 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1668 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1679 * Generate code to load into the X register the sum of the length of
1680 * the IPv4 header and any variable-length header preceding the link-layer
1683 static struct slist
*
1684 gen_loadx_iphdrlen()
1686 struct slist
*s
, *s2
;
1688 s
= gen_off_macpl();
1691 * There's a variable-length prefix preceding the
1692 * link-layer header, or the link-layer header is itself
1693 * variable-length. "s" points to a list of statements
1694 * that put the offset of the MAC-layer payload into
1697 * The 4*([k]&0xf) addressing mode can't be used, as we
1698 * don't have a constant offset, so we have to load the
1699 * value in question into the A register and add to it
1700 * the value from the X register.
1702 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1705 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1708 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1713 * The A register now contains the length of the
1714 * IP header. We need to add to it the offset of
1715 * the MAC-layer payload, which is still in the X
1716 * register, and move the result into the X register.
1718 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1719 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1722 * There is no variable-length header preceding the
1723 * link-layer header, and the link-layer header is
1724 * fixed-length; load the length of the IPv4 header,
1725 * which is at an offset of off_nl from the beginning
1726 * of the MAC-layer payload, and thus at an offset
1727 * of off_mac_pl + off_nl from the beginning of the
1730 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1731 s
->s
.k
= off_macpl
+ off_nl
;
1736 static struct block
*
1743 s
= new_stmt(BPF_LD
|BPF_IMM
);
1745 b
= new_block(JMP(BPF_JEQ
));
1751 static inline struct block
*
1754 return gen_uncond(1);
1757 static inline struct block
*
1760 return gen_uncond(0);
1764 * Byte-swap a 32-bit number.
1765 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1766 * big-endian platforms.)
1768 #define SWAPLONG(y) \
1769 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1772 * Generate code to match a particular packet type.
1774 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1775 * value, if <= ETHERMTU. We use that to determine whether to
1776 * match the type/length field or to check the type/length field for
1777 * a value <= ETHERMTU to see whether it's a type field and then do
1778 * the appropriate test.
1780 static struct block
*
1781 gen_ether_linktype(proto
)
1784 struct block
*b0
, *b1
;
1790 case LLCSAP_NETBEUI
:
1792 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1793 * so we check the DSAP and SSAP.
1795 * LLCSAP_IP checks for IP-over-802.2, rather
1796 * than IP-over-Ethernet or IP-over-SNAP.
1798 * XXX - should we check both the DSAP and the
1799 * SSAP, like this, or should we check just the
1800 * DSAP, as we do for other types <= ETHERMTU
1801 * (i.e., other SAP values)?
1803 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1805 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1806 ((proto
<< 8) | proto
));
1814 * Ethernet_II frames, which are Ethernet
1815 * frames with a frame type of ETHERTYPE_IPX;
1817 * Ethernet_802.3 frames, which are 802.3
1818 * frames (i.e., the type/length field is
1819 * a length field, <= ETHERMTU, rather than
1820 * a type field) with the first two bytes
1821 * after the Ethernet/802.3 header being
1824 * Ethernet_802.2 frames, which are 802.3
1825 * frames with an 802.2 LLC header and
1826 * with the IPX LSAP as the DSAP in the LLC
1829 * Ethernet_SNAP frames, which are 802.3
1830 * frames with an LLC header and a SNAP
1831 * header and with an OUI of 0x000000
1832 * (encapsulated Ethernet) and a protocol
1833 * ID of ETHERTYPE_IPX in the SNAP header.
1835 * XXX - should we generate the same code both
1836 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1840 * This generates code to check both for the
1841 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1843 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1844 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1848 * Now we add code to check for SNAP frames with
1849 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1851 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1855 * Now we generate code to check for 802.3
1856 * frames in general.
1858 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1862 * Now add the check for 802.3 frames before the
1863 * check for Ethernet_802.2 and Ethernet_802.3,
1864 * as those checks should only be done on 802.3
1865 * frames, not on Ethernet frames.
1870 * Now add the check for Ethernet_II frames, and
1871 * do that before checking for the other frame
1874 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1875 (bpf_int32
)ETHERTYPE_IPX
);
1879 case ETHERTYPE_ATALK
:
1880 case ETHERTYPE_AARP
:
1882 * EtherTalk (AppleTalk protocols on Ethernet link
1883 * layer) may use 802.2 encapsulation.
1887 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1888 * we check for an Ethernet type field less than
1889 * 1500, which means it's an 802.3 length field.
1891 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1895 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1896 * SNAP packets with an organization code of
1897 * 0x080007 (Apple, for Appletalk) and a protocol
1898 * type of ETHERTYPE_ATALK (Appletalk).
1900 * 802.2-encapsulated ETHERTYPE_AARP packets are
1901 * SNAP packets with an organization code of
1902 * 0x000000 (encapsulated Ethernet) and a protocol
1903 * type of ETHERTYPE_AARP (Appletalk ARP).
1905 if (proto
== ETHERTYPE_ATALK
)
1906 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1907 else /* proto == ETHERTYPE_AARP */
1908 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1912 * Check for Ethernet encapsulation (Ethertalk
1913 * phase 1?); we just check for the Ethernet
1916 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1922 if (proto
<= ETHERMTU
) {
1924 * This is an LLC SAP value, so the frames
1925 * that match would be 802.2 frames.
1926 * Check that the frame is an 802.2 frame
1927 * (i.e., that the length/type field is
1928 * a length field, <= ETHERMTU) and
1929 * then check the DSAP.
1931 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1933 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1939 * This is an Ethernet type, so compare
1940 * the length/type field with it (if
1941 * the frame is an 802.2 frame, the length
1942 * field will be <= ETHERMTU, and, as
1943 * "proto" is > ETHERMTU, this test
1944 * will fail and the frame won't match,
1945 * which is what we want).
1947 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1954 * Generate code to match a particular packet type.
1956 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1957 * value, if <= ETHERMTU. We use that to determine whether to
1958 * match the type field or to check the type field for the special
1959 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1961 static struct block
*
1962 gen_linux_sll_linktype(proto
)
1965 struct block
*b0
, *b1
;
1971 case LLCSAP_NETBEUI
:
1973 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1974 * so we check the DSAP and SSAP.
1976 * LLCSAP_IP checks for IP-over-802.2, rather
1977 * than IP-over-Ethernet or IP-over-SNAP.
1979 * XXX - should we check both the DSAP and the
1980 * SSAP, like this, or should we check just the
1981 * DSAP, as we do for other types <= ETHERMTU
1982 * (i.e., other SAP values)?
1984 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1985 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1986 ((proto
<< 8) | proto
));
1992 * Ethernet_II frames, which are Ethernet
1993 * frames with a frame type of ETHERTYPE_IPX;
1995 * Ethernet_802.3 frames, which have a frame
1996 * type of LINUX_SLL_P_802_3;
1998 * Ethernet_802.2 frames, which are 802.3
1999 * frames with an 802.2 LLC header (i.e, have
2000 * a frame type of LINUX_SLL_P_802_2) and
2001 * with the IPX LSAP as the DSAP in the LLC
2004 * Ethernet_SNAP frames, which are 802.3
2005 * frames with an LLC header and a SNAP
2006 * header and with an OUI of 0x000000
2007 * (encapsulated Ethernet) and a protocol
2008 * ID of ETHERTYPE_IPX in the SNAP header.
2010 * First, do the checks on LINUX_SLL_P_802_2
2011 * frames; generate the check for either
2012 * Ethernet_802.2 or Ethernet_SNAP frames, and
2013 * then put a check for LINUX_SLL_P_802_2 frames
2016 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2017 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2019 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2023 * Now check for 802.3 frames and OR that with
2024 * the previous test.
2026 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2030 * Now add the check for Ethernet_II frames, and
2031 * do that before checking for the other frame
2034 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2035 (bpf_int32
)ETHERTYPE_IPX
);
2039 case ETHERTYPE_ATALK
:
2040 case ETHERTYPE_AARP
:
2042 * EtherTalk (AppleTalk protocols on Ethernet link
2043 * layer) may use 802.2 encapsulation.
2047 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2048 * we check for the 802.2 protocol type in the
2049 * "Ethernet type" field.
2051 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2054 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2055 * SNAP packets with an organization code of
2056 * 0x080007 (Apple, for Appletalk) and a protocol
2057 * type of ETHERTYPE_ATALK (Appletalk).
2059 * 802.2-encapsulated ETHERTYPE_AARP packets are
2060 * SNAP packets with an organization code of
2061 * 0x000000 (encapsulated Ethernet) and a protocol
2062 * type of ETHERTYPE_AARP (Appletalk ARP).
2064 if (proto
== ETHERTYPE_ATALK
)
2065 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2066 else /* proto == ETHERTYPE_AARP */
2067 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2071 * Check for Ethernet encapsulation (Ethertalk
2072 * phase 1?); we just check for the Ethernet
2075 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2081 if (proto
<= ETHERMTU
) {
2083 * This is an LLC SAP value, so the frames
2084 * that match would be 802.2 frames.
2085 * Check for the 802.2 protocol type
2086 * in the "Ethernet type" field, and
2087 * then check the DSAP.
2089 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2091 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2097 * This is an Ethernet type, so compare
2098 * the length/type field with it (if
2099 * the frame is an 802.2 frame, the length
2100 * field will be <= ETHERMTU, and, as
2101 * "proto" is > ETHERMTU, this test
2102 * will fail and the frame won't match,
2103 * which is what we want).
2105 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2111 static struct slist
*
2112 gen_load_prism_llprefixlen()
2114 struct slist
*s1
, *s2
;
2115 struct slist
*sjeq_avs_cookie
;
2116 struct slist
*sjcommon
;
2119 * This code is not compatible with the optimizer, as
2120 * we are generating jmp instructions within a normal
2121 * slist of instructions
2126 * Generate code to load the length of the radio header into
2127 * the register assigned to hold that length, if one has been
2128 * assigned. (If one hasn't been assigned, no code we've
2129 * generated uses that prefix, so we don't need to generate any
2132 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2133 * or always use the AVS header rather than the Prism header.
2134 * We load a 4-byte big-endian value at the beginning of the
2135 * raw packet data, and see whether, when masked with 0xFFFFF000,
2136 * it's equal to 0x80211000. If so, that indicates that it's
2137 * an AVS header (the masked-out bits are the version number).
2138 * Otherwise, it's a Prism header.
2140 * XXX - the Prism header is also, in theory, variable-length,
2141 * but no known software generates headers that aren't 144
2144 if (reg_off_ll
!= -1) {
2148 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2152 * AND it with 0xFFFFF000.
2154 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2155 s2
->s
.k
= 0xFFFFF000;
2159 * Compare with 0x80211000.
2161 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2162 sjeq_avs_cookie
->s
.k
= 0x80211000;
2163 sappend(s1
, sjeq_avs_cookie
);
2168 * The 4 bytes at an offset of 4 from the beginning of
2169 * the AVS header are the length of the AVS header.
2170 * That field is big-endian.
2172 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2175 sjeq_avs_cookie
->s
.jt
= s2
;
2178 * Now jump to the code to allocate a register
2179 * into which to save the header length and
2180 * store the length there. (The "jump always"
2181 * instruction needs to have the k field set;
2182 * it's added to the PC, so, as we're jumping
2183 * over a single instruction, it should be 1.)
2185 sjcommon
= new_stmt(JMP(BPF_JA
));
2187 sappend(s1
, sjcommon
);
2190 * Now for the code that handles the Prism header.
2191 * Just load the length of the Prism header (144)
2192 * into the A register. Have the test for an AVS
2193 * header branch here if we don't have an AVS header.
2195 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2198 sjeq_avs_cookie
->s
.jf
= s2
;
2201 * Now allocate a register to hold that value and store
2202 * it. The code for the AVS header will jump here after
2203 * loading the length of the AVS header.
2205 s2
= new_stmt(BPF_ST
);
2206 s2
->s
.k
= reg_off_ll
;
2208 sjcommon
->s
.jf
= s2
;
2211 * Now move it into the X register.
2213 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2221 static struct slist
*
2222 gen_load_avs_llprefixlen()
2224 struct slist
*s1
, *s2
;
2227 * Generate code to load the length of the AVS header into
2228 * the register assigned to hold that length, if one has been
2229 * assigned. (If one hasn't been assigned, no code we've
2230 * generated uses that prefix, so we don't need to generate any
2233 if (reg_off_ll
!= -1) {
2235 * The 4 bytes at an offset of 4 from the beginning of
2236 * the AVS header are the length of the AVS header.
2237 * That field is big-endian.
2239 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2243 * Now allocate a register to hold that value and store
2246 s2
= new_stmt(BPF_ST
);
2247 s2
->s
.k
= reg_off_ll
;
2251 * Now move it into the X register.
2253 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2261 static struct slist
*
2262 gen_load_radiotap_llprefixlen()
2264 struct slist
*s1
, *s2
;
2267 * Generate code to load the length of the radiotap header into
2268 * the register assigned to hold that length, if one has been
2269 * assigned. (If one hasn't been assigned, no code we've
2270 * generated uses that prefix, so we don't need to generate any
2273 if (reg_off_ll
!= -1) {
2275 * The 2 bytes at offsets of 2 and 3 from the beginning
2276 * of the radiotap header are the length of the radiotap
2277 * header; unfortunately, it's little-endian, so we have
2278 * to load it a byte at a time and construct the value.
2282 * Load the high-order byte, at an offset of 3, shift it
2283 * left a byte, and put the result in the X register.
2285 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2287 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2290 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2294 * Load the next byte, at an offset of 2, and OR the
2295 * value from the X register into it.
2297 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2300 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2304 * Now allocate a register to hold that value and store
2307 s2
= new_stmt(BPF_ST
);
2308 s2
->s
.k
= reg_off_ll
;
2312 * Now move it into the X register.
2314 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2323 * At the moment we treat PPI as normal Radiotap encoded
2324 * packets. The difference is in the function that generates
2325 * the code at the beginning to compute the header length.
2326 * Since this code generator of PPI supports bare 802.11
2327 * encapsulation only (i.e. the encapsulated DLT should be
2328 * DLT_IEEE802_11) we generate code to check for this too;
2329 * that's done in finish_parse().
2331 static struct slist
*
2332 gen_load_ppi_llprefixlen()
2334 struct slist
*s1
, *s2
;
2337 * Generate code to load the length of the radiotap header
2338 * into the register assigned to hold that length, if one has
2341 if (reg_off_ll
!= -1) {
2343 * The 2 bytes at offsets of 2 and 3 from the beginning
2344 * of the radiotap header are the length of the radiotap
2345 * header; unfortunately, it's little-endian, so we have
2346 * to load it a byte at a time and construct the value.
2350 * Load the high-order byte, at an offset of 3, shift it
2351 * left a byte, and put the result in the X register.
2353 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2355 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2358 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2362 * Load the next byte, at an offset of 2, and OR the
2363 * value from the X register into it.
2365 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2368 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2372 * Now allocate a register to hold that value and store
2375 s2
= new_stmt(BPF_ST
);
2376 s2
->s
.k
= reg_off_ll
;
2380 * Now move it into the X register.
2382 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2391 * Load a value relative to the beginning of the link-layer header after the 802.11
2392 * header, i.e. LLC_SNAP.
2393 * The link-layer header doesn't necessarily begin at the beginning
2394 * of the packet data; there might be a variable-length prefix containing
2395 * radio information.
2397 static struct slist
*
2398 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2401 struct slist
*sjset_data_frame_1
;
2402 struct slist
*sjset_data_frame_2
;
2403 struct slist
*sjset_qos
;
2404 struct slist
*sjset_radiotap_flags
;
2405 struct slist
*sjset_radiotap_tsft
;
2406 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2407 struct slist
*s_roundup
;
2409 if (reg_off_macpl
== -1) {
2411 * No register has been assigned to the offset of
2412 * the MAC-layer payload, which means nobody needs
2413 * it; don't bother computing it - just return
2414 * what we already have.
2420 * This code is not compatible with the optimizer, as
2421 * we are generating jmp instructions within a normal
2422 * slist of instructions
2427 * If "s" is non-null, it has code to arrange that the X register
2428 * contains the length of the prefix preceding the link-layer
2431 * Otherwise, the length of the prefix preceding the link-layer
2432 * header is "off_ll".
2436 * There is no variable-length header preceding the
2437 * link-layer header.
2439 * Load the length of the fixed-length prefix preceding
2440 * the link-layer header (if any) into the X register,
2441 * and store it in the reg_off_macpl register.
2442 * That length is off_ll.
2444 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2449 * The X register contains the offset of the beginning of the
2450 * link-layer header; add 24, which is the minimum length
2451 * of the MAC header for a data frame, to that, and store it
2452 * in reg_off_macpl, and then load the Frame Control field,
2453 * which is at the offset in the X register, with an indexed load.
2455 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2457 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2460 s2
= new_stmt(BPF_ST
);
2461 s2
->s
.k
= reg_off_macpl
;
2464 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2469 * Check the Frame Control field to see if this is a data frame;
2470 * a data frame has the 0x08 bit (b3) in that field set and the
2471 * 0x04 bit (b2) clear.
2473 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2474 sjset_data_frame_1
->s
.k
= 0x08;
2475 sappend(s
, sjset_data_frame_1
);
2478 * If b3 is set, test b2, otherwise go to the first statement of
2479 * the rest of the program.
2481 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2482 sjset_data_frame_2
->s
.k
= 0x04;
2483 sappend(s
, sjset_data_frame_2
);
2484 sjset_data_frame_1
->s
.jf
= snext
;
2487 * If b2 is not set, this is a data frame; test the QoS bit.
2488 * Otherwise, go to the first statement of the rest of the
2491 sjset_data_frame_2
->s
.jt
= snext
;
2492 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2493 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2494 sappend(s
, sjset_qos
);
2497 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2499 * Otherwise, go to the first statement of the rest of the
2502 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2503 s2
->s
.k
= reg_off_macpl
;
2505 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2508 s2
= new_stmt(BPF_ST
);
2509 s2
->s
.k
= reg_off_macpl
;
2513 * If we have a radiotap header, look at it to see whether
2514 * there's Atheros padding between the MAC-layer header
2517 * Note: all of the fields in the radiotap header are
2518 * little-endian, so we byte-swap all of the values
2519 * we test against, as they will be loaded as big-endian
2522 if (linktype
== DLT_IEEE802_11_RADIO
) {
2524 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2525 * in the presence flag?
2527 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2531 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2532 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2533 sappend(s
, sjset_radiotap_flags
);
2536 * If not, skip all of this.
2538 sjset_radiotap_flags
->s
.jf
= snext
;
2541 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2543 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2544 new_stmt(JMP(BPF_JSET
));
2545 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2546 sappend(s
, sjset_radiotap_tsft
);
2549 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2550 * at an offset of 16 from the beginning of the raw packet
2551 * data (8 bytes for the radiotap header and 8 bytes for
2554 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2557 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2561 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2562 sjset_tsft_datapad
->s
.k
= 0x20;
2563 sappend(s
, sjset_tsft_datapad
);
2566 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2567 * at an offset of 8 from the beginning of the raw packet
2568 * data (8 bytes for the radiotap header).
2570 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2573 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2577 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2578 sjset_notsft_datapad
->s
.k
= 0x20;
2579 sappend(s
, sjset_notsft_datapad
);
2582 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2583 * set, round the length of the 802.11 header to
2584 * a multiple of 4. Do that by adding 3 and then
2585 * dividing by and multiplying by 4, which we do by
2588 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2589 s_roundup
->s
.k
= reg_off_macpl
;
2590 sappend(s
, s_roundup
);
2591 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2594 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2597 s2
= new_stmt(BPF_ST
);
2598 s2
->s
.k
= reg_off_macpl
;
2601 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2602 sjset_tsft_datapad
->s
.jf
= snext
;
2603 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2604 sjset_notsft_datapad
->s
.jf
= snext
;
2606 sjset_qos
->s
.jf
= snext
;
2612 insert_compute_vloffsets(b
)
2618 * For link-layer types that have a variable-length header
2619 * preceding the link-layer header, generate code to load
2620 * the offset of the link-layer header into the register
2621 * assigned to that offset, if any.
2625 case DLT_PRISM_HEADER
:
2626 s
= gen_load_prism_llprefixlen();
2629 case DLT_IEEE802_11_RADIO_AVS
:
2630 s
= gen_load_avs_llprefixlen();
2633 case DLT_IEEE802_11_RADIO
:
2634 s
= gen_load_radiotap_llprefixlen();
2638 s
= gen_load_ppi_llprefixlen();
2647 * For link-layer types that have a variable-length link-layer
2648 * header, generate code to load the offset of the MAC-layer
2649 * payload into the register assigned to that offset, if any.
2653 case DLT_IEEE802_11
:
2654 case DLT_PRISM_HEADER
:
2655 case DLT_IEEE802_11_RADIO_AVS
:
2656 case DLT_IEEE802_11_RADIO
:
2658 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2663 * If we have any offset-loading code, append all the
2664 * existing statements in the block to those statements,
2665 * and make the resulting list the list of statements
2669 sappend(s
, b
->stmts
);
2674 static struct block
*
2675 gen_ppi_dlt_check(void)
2677 struct slist
*s_load_dlt
;
2680 if (linktype
== DLT_PPI
)
2682 /* Create the statements that check for the DLT
2684 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2685 s_load_dlt
->s
.k
= 4;
2687 b
= new_block(JMP(BPF_JEQ
));
2689 b
->stmts
= s_load_dlt
;
2690 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2700 static struct slist
*
2701 gen_prism_llprefixlen(void)
2705 if (reg_off_ll
== -1) {
2707 * We haven't yet assigned a register for the length
2708 * of the radio header; allocate one.
2710 reg_off_ll
= alloc_reg();
2714 * Load the register containing the radio length
2715 * into the X register.
2717 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2718 s
->s
.k
= reg_off_ll
;
2722 static struct slist
*
2723 gen_avs_llprefixlen(void)
2727 if (reg_off_ll
== -1) {
2729 * We haven't yet assigned a register for the length
2730 * of the AVS header; allocate one.
2732 reg_off_ll
= alloc_reg();
2736 * Load the register containing the AVS length
2737 * into the X register.
2739 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2740 s
->s
.k
= reg_off_ll
;
2744 static struct slist
*
2745 gen_radiotap_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 radiotap length
2759 * into the X register.
2761 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2762 s
->s
.k
= reg_off_ll
;
2767 * At the moment we treat PPI as normal Radiotap encoded
2768 * packets. The difference is in the function that generates
2769 * the code at the beginning to compute the header length.
2770 * Since this code generator of PPI supports bare 802.11
2771 * encapsulation only (i.e. the encapsulated DLT should be
2772 * DLT_IEEE802_11) we generate code to check for this too.
2774 static struct slist
*
2775 gen_ppi_llprefixlen(void)
2779 if (reg_off_ll
== -1) {
2781 * We haven't yet assigned a register for the length
2782 * of the radiotap header; allocate one.
2784 reg_off_ll
= alloc_reg();
2788 * Load the register containing the PPI length
2789 * into the X register.
2791 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2792 s
->s
.k
= reg_off_ll
;
2797 * Generate code to compute the link-layer header length, if necessary,
2798 * putting it into the X register, and to return either a pointer to a
2799 * "struct slist" for the list of statements in that code, or NULL if
2800 * no code is necessary.
2802 static struct slist
*
2803 gen_llprefixlen(void)
2807 case DLT_PRISM_HEADER
:
2808 return gen_prism_llprefixlen();
2810 case DLT_IEEE802_11_RADIO_AVS
:
2811 return gen_avs_llprefixlen();
2813 case DLT_IEEE802_11_RADIO
:
2814 return gen_radiotap_llprefixlen();
2817 return gen_ppi_llprefixlen();
2825 * Generate code to load the register containing the offset of the
2826 * MAC-layer payload into the X register; if no register for that offset
2827 * has been allocated, allocate it first.
2829 static struct slist
*
2834 if (off_macpl_is_variable
) {
2835 if (reg_off_macpl
== -1) {
2837 * We haven't yet assigned a register for the offset
2838 * of the MAC-layer payload; allocate one.
2840 reg_off_macpl
= alloc_reg();
2844 * Load the register containing the offset of the MAC-layer
2845 * payload into the X register.
2847 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2848 s
->s
.k
= reg_off_macpl
;
2852 * That offset isn't variable, so we don't need to
2853 * generate any code.
2860 * Map an Ethernet type to the equivalent PPP type.
2863 ethertype_to_ppptype(proto
)
2873 case ETHERTYPE_IPV6
:
2882 case ETHERTYPE_ATALK
:
2896 * I'm assuming the "Bridging PDU"s that go
2897 * over PPP are Spanning Tree Protocol
2911 * Generate code to match a particular packet type by matching the
2912 * link-layer type field or fields in the 802.2 LLC header.
2914 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2915 * value, if <= ETHERMTU.
2917 static struct block
*
2921 struct block
*b0
, *b1
, *b2
;
2923 /* are we checking MPLS-encapsulated packets? */
2924 if (label_stack_depth
> 0) {
2928 /* FIXME add other L3 proto IDs */
2929 return gen_mpls_linktype(Q_IP
);
2931 case ETHERTYPE_IPV6
:
2933 /* FIXME add other L3 proto IDs */
2934 return gen_mpls_linktype(Q_IPV6
);
2937 bpf_error("unsupported protocol over mpls");
2943 * Are we testing PPPoE packets?
2947 * The PPPoE session header is part of the
2948 * MAC-layer payload, so all references
2949 * should be relative to the beginning of
2954 * We use Ethernet protocol types inside libpcap;
2955 * map them to the corresponding PPP protocol types.
2957 proto
= ethertype_to_ppptype(proto
);
2958 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2964 return gen_ether_linktype(proto
);
2972 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2976 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2983 case DLT_IEEE802_11
:
2984 case DLT_PRISM_HEADER
:
2985 case DLT_IEEE802_11_RADIO_AVS
:
2986 case DLT_IEEE802_11_RADIO
:
2989 * Check that we have a data frame.
2991 b0
= gen_check_802_11_data_frame();
2994 * Now check for the specified link-layer type.
2996 b1
= gen_llc_linktype(proto
);
3004 * XXX - check for asynchronous frames, as per RFC 1103.
3006 return gen_llc_linktype(proto
);
3012 * XXX - check for LLC PDUs, as per IEEE 802.5.
3014 return gen_llc_linktype(proto
);
3018 case DLT_ATM_RFC1483
:
3020 case DLT_IP_OVER_FC
:
3021 return gen_llc_linktype(proto
);
3027 * If "is_lane" is set, check for a LANE-encapsulated
3028 * version of this protocol, otherwise check for an
3029 * LLC-encapsulated version of this protocol.
3031 * We assume LANE means Ethernet, not Token Ring.
3035 * Check that the packet doesn't begin with an
3036 * LE Control marker. (We've already generated
3039 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3044 * Now generate an Ethernet test.
3046 b1
= gen_ether_linktype(proto
);
3051 * Check for LLC encapsulation and then check the
3054 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3055 b1
= gen_llc_linktype(proto
);
3063 return gen_linux_sll_linktype(proto
);
3068 case DLT_SLIP_BSDOS
:
3071 * These types don't provide any type field; packets
3072 * are always IPv4 or IPv6.
3074 * XXX - for IPv4, check for a version number of 4, and,
3075 * for IPv6, check for a version number of 6?
3080 /* Check for a version number of 4. */
3081 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
3083 case ETHERTYPE_IPV6
:
3084 /* Check for a version number of 6. */
3085 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
3089 return gen_false(); /* always false */
3096 case DLT_PPP_SERIAL
:
3099 * We use Ethernet protocol types inside libpcap;
3100 * map them to the corresponding PPP protocol types.
3102 proto
= ethertype_to_ppptype(proto
);
3103 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3109 * We use Ethernet protocol types inside libpcap;
3110 * map them to the corresponding PPP protocol types.
3116 * Also check for Van Jacobson-compressed IP.
3117 * XXX - do this for other forms of PPP?
3119 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
3120 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
3122 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
3127 proto
= ethertype_to_ppptype(proto
);
3128 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
3138 * For DLT_NULL, the link-layer header is a 32-bit
3139 * word containing an AF_ value in *host* byte order,
3140 * and for DLT_ENC, the link-layer header begins
3141 * with a 32-bit work containing an AF_ value in
3144 * In addition, if we're reading a saved capture file,
3145 * the host byte order in the capture may not be the
3146 * same as the host byte order on this machine.
3148 * For DLT_LOOP, the link-layer header is a 32-bit
3149 * word containing an AF_ value in *network* byte order.
3151 * XXX - AF_ values may, unfortunately, be platform-
3152 * dependent; for example, FreeBSD's AF_INET6 is 24
3153 * whilst NetBSD's and OpenBSD's is 26.
3155 * This means that, when reading a capture file, just
3156 * checking for our AF_INET6 value won't work if the
3157 * capture file came from another OS.
3166 case ETHERTYPE_IPV6
:
3173 * Not a type on which we support filtering.
3174 * XXX - support those that have AF_ values
3175 * #defined on this platform, at least?
3180 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3182 * The AF_ value is in host byte order, but
3183 * the BPF interpreter will convert it to
3184 * network byte order.
3186 * If this is a save file, and it's from a
3187 * machine with the opposite byte order to
3188 * ours, we byte-swap the AF_ value.
3190 * Then we run it through "htonl()", and
3191 * generate code to compare against the result.
3193 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3194 bpf_pcap
->sf
.swapped
)
3195 proto
= SWAPLONG(proto
);
3196 proto
= htonl(proto
);
3198 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3200 #ifdef HAVE_NET_PFVAR_H
3203 * af field is host byte order in contrast to the rest of
3206 if (proto
== ETHERTYPE_IP
)
3207 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3208 BPF_B
, (bpf_int32
)AF_INET
));
3210 else if (proto
== ETHERTYPE_IPV6
)
3211 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3212 BPF_B
, (bpf_int32
)AF_INET6
));
3218 #endif /* HAVE_NET_PFVAR_H */
3221 case DLT_ARCNET_LINUX
:
3223 * XXX should we check for first fragment if the protocol
3232 case ETHERTYPE_IPV6
:
3233 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3234 (bpf_int32
)ARCTYPE_INET6
));
3238 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3239 (bpf_int32
)ARCTYPE_IP
);
3240 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3241 (bpf_int32
)ARCTYPE_IP_OLD
);
3246 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3247 (bpf_int32
)ARCTYPE_ARP
);
3248 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3249 (bpf_int32
)ARCTYPE_ARP_OLD
);
3253 case ETHERTYPE_REVARP
:
3254 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3255 (bpf_int32
)ARCTYPE_REVARP
));
3257 case ETHERTYPE_ATALK
:
3258 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3259 (bpf_int32
)ARCTYPE_ATALK
));
3266 case ETHERTYPE_ATALK
:
3276 * XXX - assumes a 2-byte Frame Relay header with
3277 * DLCI and flags. What if the address is longer?
3283 * Check for the special NLPID for IP.
3285 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3288 case ETHERTYPE_IPV6
:
3290 * Check for the special NLPID for IPv6.
3292 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3297 * Check for several OSI protocols.
3299 * Frame Relay packets typically have an OSI
3300 * NLPID at the beginning; we check for each
3303 * What we check for is the NLPID and a frame
3304 * control field of UI, i.e. 0x03 followed
3307 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3308 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3309 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3321 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3323 case DLT_JUNIPER_MFR
:
3324 case DLT_JUNIPER_MLFR
:
3325 case DLT_JUNIPER_MLPPP
:
3326 case DLT_JUNIPER_ATM1
:
3327 case DLT_JUNIPER_ATM2
:
3328 case DLT_JUNIPER_PPPOE
:
3329 case DLT_JUNIPER_PPPOE_ATM
:
3330 case DLT_JUNIPER_GGSN
:
3331 case DLT_JUNIPER_ES
:
3332 case DLT_JUNIPER_MONITOR
:
3333 case DLT_JUNIPER_SERVICES
:
3334 case DLT_JUNIPER_ETHER
:
3335 case DLT_JUNIPER_PPP
:
3336 case DLT_JUNIPER_FRELAY
:
3337 case DLT_JUNIPER_CHDLC
:
3338 case DLT_JUNIPER_VP
:
3339 case DLT_JUNIPER_ST
:
3340 case DLT_JUNIPER_ISM
:
3341 /* just lets verify the magic number for now -
3342 * on ATM we may have up to 6 different encapsulations on the wire
3343 * and need a lot of heuristics to figure out that the payload
3346 * FIXME encapsulation specific BPF_ filters
3348 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3350 case DLT_LINUX_IRDA
:
3351 bpf_error("IrDA link-layer type filtering not implemented");
3354 bpf_error("DOCSIS link-layer type filtering not implemented");
3357 case DLT_MTP2_WITH_PHDR
:
3358 bpf_error("MTP2 link-layer type filtering not implemented");
3361 bpf_error("ERF link-layer type filtering not implemented");
3365 bpf_error("PFSYNC link-layer type filtering not implemented");
3368 case DLT_LINUX_LAPD
:
3369 bpf_error("LAPD link-layer type filtering not implemented");
3373 case DLT_USB_LINUX_MMAPPED
:
3374 bpf_error("USB link-layer type filtering not implemented");
3376 case DLT_BLUETOOTH_HCI_H4
:
3377 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3378 bpf_error("Bluetooth link-layer type filtering not implemented");
3381 bpf_error("CAN20B link-layer type filtering not implemented");
3383 case DLT_IEEE802_15_4
:
3384 case DLT_IEEE802_15_4_LINUX
:
3385 case DLT_IEEE802_15_4_NONASK_PHY
:
3386 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3388 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3389 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3392 bpf_error("SITA link-layer type filtering not implemented");
3395 bpf_error("RAIF1 link-layer type filtering not implemented");
3398 bpf_error("IPMB link-layer type filtering not implemented");
3401 bpf_error("AX.25 link-layer type filtering not implemented");
3405 * All the types that have no encapsulation should either be
3406 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3407 * all packets are IP packets, or should be handled in some
3408 * special case, if none of them are (if some are and some
3409 * aren't, the lack of encapsulation is a problem, as we'd
3410 * have to find some other way of determining the packet type).
3412 * Therefore, if "off_linktype" is -1, there's an error.
3414 if (off_linktype
== (u_int
)-1)
3418 * Any type not handled above should always have an Ethernet
3419 * type at an offset of "off_linktype".
3421 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3425 * Check for an LLC SNAP packet with a given organization code and
3426 * protocol type; we check the entire contents of the 802.2 LLC and
3427 * snap headers, checking for DSAP and SSAP of SNAP and a control
3428 * field of 0x03 in the LLC header, and for the specified organization
3429 * code and protocol type in the SNAP header.
3431 static struct block
*
3432 gen_snap(orgcode
, ptype
)
3433 bpf_u_int32 orgcode
;
3436 u_char snapblock
[8];
3438 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3439 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3440 snapblock
[2] = 0x03; /* control = UI */
3441 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3442 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3443 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3444 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3445 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3446 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3450 * Generate code to match a particular packet type, for link-layer types
3451 * using 802.2 LLC headers.
3453 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3454 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3456 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3457 * value, if <= ETHERMTU. We use that to determine whether to
3458 * match the DSAP or both DSAP and LSAP or to check the OUI and
3459 * protocol ID in a SNAP header.
3461 static struct block
*
3462 gen_llc_linktype(proto
)
3466 * XXX - handle token-ring variable-length header.
3472 case LLCSAP_NETBEUI
:
3474 * XXX - should we check both the DSAP and the
3475 * SSAP, like this, or should we check just the
3476 * DSAP, as we do for other types <= ETHERMTU
3477 * (i.e., other SAP values)?
3479 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3480 ((proto
<< 8) | proto
));
3484 * XXX - are there ever SNAP frames for IPX on
3485 * non-Ethernet 802.x networks?
3487 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3488 (bpf_int32
)LLCSAP_IPX
);
3490 case ETHERTYPE_ATALK
:
3492 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3493 * SNAP packets with an organization code of
3494 * 0x080007 (Apple, for Appletalk) and a protocol
3495 * type of ETHERTYPE_ATALK (Appletalk).
3497 * XXX - check for an organization code of
3498 * encapsulated Ethernet as well?
3500 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3504 * XXX - we don't have to check for IPX 802.3
3505 * here, but should we check for the IPX Ethertype?
3507 if (proto
<= ETHERMTU
) {
3509 * This is an LLC SAP value, so check
3512 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3515 * This is an Ethernet type; we assume that it's
3516 * unlikely that it'll appear in the right place
3517 * at random, and therefore check only the
3518 * location that would hold the Ethernet type
3519 * in a SNAP frame with an organization code of
3520 * 0x000000 (encapsulated Ethernet).
3522 * XXX - if we were to check for the SNAP DSAP and
3523 * LSAP, as per XXX, and were also to check for an
3524 * organization code of 0x000000 (encapsulated
3525 * Ethernet), we'd do
3527 * return gen_snap(0x000000, proto);
3529 * here; for now, we don't, as per the above.
3530 * I don't know whether it's worth the extra CPU
3531 * time to do the right check or not.
3533 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3538 static struct block
*
3539 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3543 u_int src_off
, dst_off
;
3545 struct block
*b0
, *b1
;
3559 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3560 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3566 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3567 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3574 b0
= gen_linktype(proto
);
3575 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3581 static struct block
*
3582 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3583 struct in6_addr
*addr
;
3584 struct in6_addr
*mask
;
3586 u_int src_off
, dst_off
;
3588 struct block
*b0
, *b1
;
3603 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3604 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3610 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3611 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3618 /* this order is important */
3619 a
= (u_int32_t
*)addr
;
3620 m
= (u_int32_t
*)mask
;
3621 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3622 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3624 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3626 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3628 b0
= gen_linktype(proto
);
3634 static struct block
*
3635 gen_ehostop(eaddr
, dir
)
3636 register const u_char
*eaddr
;
3639 register struct block
*b0
, *b1
;
3643 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3646 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3649 b0
= gen_ehostop(eaddr
, Q_SRC
);
3650 b1
= gen_ehostop(eaddr
, Q_DST
);
3656 b0
= gen_ehostop(eaddr
, Q_SRC
);
3657 b1
= gen_ehostop(eaddr
, Q_DST
);
3666 * Like gen_ehostop, but for DLT_FDDI
3668 static struct block
*
3669 gen_fhostop(eaddr
, dir
)
3670 register const u_char
*eaddr
;
3673 struct block
*b0
, *b1
;
3678 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3680 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3685 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3687 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3691 b0
= gen_fhostop(eaddr
, Q_SRC
);
3692 b1
= gen_fhostop(eaddr
, Q_DST
);
3698 b0
= gen_fhostop(eaddr
, Q_SRC
);
3699 b1
= gen_fhostop(eaddr
, Q_DST
);
3708 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3710 static struct block
*
3711 gen_thostop(eaddr
, dir
)
3712 register const u_char
*eaddr
;
3715 register struct block
*b0
, *b1
;
3719 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3722 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3725 b0
= gen_thostop(eaddr
, Q_SRC
);
3726 b1
= gen_thostop(eaddr
, Q_DST
);
3732 b0
= gen_thostop(eaddr
, Q_SRC
);
3733 b1
= gen_thostop(eaddr
, Q_DST
);
3742 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3743 * various 802.11 + radio headers.
3745 static struct block
*
3746 gen_wlanhostop(eaddr
, dir
)
3747 register const u_char
*eaddr
;
3750 register struct block
*b0
, *b1
, *b2
;
3751 register struct slist
*s
;
3753 #ifdef ENABLE_WLAN_FILTERING_PATCH
3756 * We need to disable the optimizer because the optimizer is buggy
3757 * and wipes out some LD instructions generated by the below
3758 * code to validate the Frame Control bits
3761 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3768 * For control frames, there is no SA.
3770 * For management frames, SA is at an
3771 * offset of 10 from the beginning of
3774 * For data frames, SA is at an offset
3775 * of 10 from the beginning of the packet
3776 * if From DS is clear, at an offset of
3777 * 16 from the beginning of the packet
3778 * if From DS is set and To DS is clear,
3779 * and an offset of 24 from the beginning
3780 * of the packet if From DS is set and To DS
3785 * Generate the tests to be done for data frames
3788 * First, check for To DS set, i.e. check "link[1] & 0x01".
3790 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3791 b1
= new_block(JMP(BPF_JSET
));
3792 b1
->s
.k
= 0x01; /* To DS */
3796 * If To DS is set, the SA is at 24.
3798 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3802 * Now, check for To DS not set, i.e. check
3803 * "!(link[1] & 0x01)".
3805 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3806 b2
= new_block(JMP(BPF_JSET
));
3807 b2
->s
.k
= 0x01; /* To DS */
3812 * If To DS is not set, the SA is at 16.
3814 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3818 * Now OR together the last two checks. That gives
3819 * the complete set of checks for data frames with
3825 * Now check for From DS being set, and AND that with
3826 * the ORed-together checks.
3828 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3829 b1
= new_block(JMP(BPF_JSET
));
3830 b1
->s
.k
= 0x02; /* From DS */
3835 * Now check for data frames with From DS not set.
3837 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3838 b2
= new_block(JMP(BPF_JSET
));
3839 b2
->s
.k
= 0x02; /* From DS */
3844 * If From DS isn't set, the SA is at 10.
3846 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3850 * Now OR together the checks for data frames with
3851 * From DS not set and for data frames with From DS
3852 * set; that gives the checks done for data frames.
3857 * Now check for a data frame.
3858 * I.e, check "link[0] & 0x08".
3860 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3861 b1
= new_block(JMP(BPF_JSET
));
3866 * AND that with the checks done for data frames.
3871 * If the high-order bit of the type value is 0, this
3872 * is a management frame.
3873 * I.e, check "!(link[0] & 0x08)".
3875 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3876 b2
= new_block(JMP(BPF_JSET
));
3882 * For management frames, the SA is at 10.
3884 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3888 * OR that with the checks done for data frames.
3889 * That gives the checks done for management and
3895 * If the low-order bit of the type value is 1,
3896 * this is either a control frame or a frame
3897 * with a reserved type, and thus not a
3900 * I.e., check "!(link[0] & 0x04)".
3902 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3903 b1
= new_block(JMP(BPF_JSET
));
3909 * AND that with the checks for data and management
3919 * For control frames, there is no DA.
3921 * For management frames, DA is at an
3922 * offset of 4 from the beginning of
3925 * For data frames, DA is at an offset
3926 * of 4 from the beginning of the packet
3927 * if To DS is clear and at an offset of
3928 * 16 from the beginning of the packet
3933 * Generate the tests to be done for data frames.
3935 * First, check for To DS set, i.e. "link[1] & 0x01".
3937 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3938 b1
= new_block(JMP(BPF_JSET
));
3939 b1
->s
.k
= 0x01; /* To DS */
3943 * If To DS is set, the DA is at 16.
3945 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3949 * Now, check for To DS not set, i.e. check
3950 * "!(link[1] & 0x01)".
3952 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3953 b2
= new_block(JMP(BPF_JSET
));
3954 b2
->s
.k
= 0x01; /* To DS */
3959 * If To DS is not set, the DA is at 4.
3961 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3965 * Now OR together the last two checks. That gives
3966 * the complete set of checks for data frames.
3971 * Now check for a data frame.
3972 * I.e, check "link[0] & 0x08".
3974 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3975 b1
= new_block(JMP(BPF_JSET
));
3980 * AND that with the checks done for data frames.
3985 * If the high-order bit of the type value is 0, this
3986 * is a management frame.
3987 * I.e, check "!(link[0] & 0x08)".
3989 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3990 b2
= new_block(JMP(BPF_JSET
));
3996 * For management frames, the DA is at 4.
3998 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
4002 * OR that with the checks done for data frames.
4003 * That gives the checks done for management and
4009 * If the low-order bit of the type value is 1,
4010 * this is either a control frame or a frame
4011 * with a reserved type, and thus not a
4014 * I.e., check "!(link[0] & 0x04)".
4016 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
4017 b1
= new_block(JMP(BPF_JSET
));
4023 * AND that with the checks for data and management
4030 * XXX - add RA, TA, and BSSID keywords?
4033 return (gen_bcmp(OR_LINK
, 4, 6, eaddr
));
4037 * Not present in CTS or ACK control frames.
4039 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4040 IEEE80211_FC0_TYPE_MASK
);
4042 b1
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4043 IEEE80211_FC0_SUBTYPE_MASK
);
4045 b2
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4046 IEEE80211_FC0_SUBTYPE_MASK
);
4050 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4056 * Not present in control frames.
4058 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4059 IEEE80211_FC0_TYPE_MASK
);
4061 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
4067 * Present only if the direction mask has both "From DS"
4068 * and "To DS" set. Neither control frames nor management
4069 * frames should have both of those set, so we don't
4070 * check the frame type.
4072 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
,
4073 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4074 b1
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
4079 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4080 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4086 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4087 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4096 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4097 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4098 * as the RFC states.)
4100 static struct block
*
4101 gen_ipfchostop(eaddr
, dir
)
4102 register const u_char
*eaddr
;
4105 register struct block
*b0
, *b1
;
4109 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
4112 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
4115 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4116 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4122 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4123 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4132 * This is quite tricky because there may be pad bytes in front of the
4133 * DECNET header, and then there are two possible data packet formats that
4134 * carry both src and dst addresses, plus 5 packet types in a format that
4135 * carries only the src node, plus 2 types that use a different format and
4136 * also carry just the src node.
4140 * Instead of doing those all right, we just look for data packets with
4141 * 0 or 1 bytes of padding. If you want to look at other packets, that
4142 * will require a lot more hacking.
4144 * To add support for filtering on DECNET "areas" (network numbers)
4145 * one would want to add a "mask" argument to this routine. That would
4146 * make the filter even more inefficient, although one could be clever
4147 * and not generate masking instructions if the mask is 0xFFFF.
4149 static struct block
*
4150 gen_dnhostop(addr
, dir
)
4154 struct block
*b0
, *b1
, *b2
, *tmp
;
4155 u_int offset_lh
; /* offset if long header is received */
4156 u_int offset_sh
; /* offset if short header is received */
4161 offset_sh
= 1; /* follows flags */
4162 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4166 offset_sh
= 3; /* follows flags, dstnode */
4167 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4171 /* Inefficient because we do our Calvinball dance twice */
4172 b0
= gen_dnhostop(addr
, Q_SRC
);
4173 b1
= gen_dnhostop(addr
, Q_DST
);
4179 /* Inefficient because we do our Calvinball dance twice */
4180 b0
= gen_dnhostop(addr
, Q_SRC
);
4181 b1
= gen_dnhostop(addr
, Q_DST
);
4186 bpf_error("ISO host filtering not implemented");
4191 b0
= gen_linktype(ETHERTYPE_DN
);
4192 /* Check for pad = 1, long header case */
4193 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4194 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4195 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
4196 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4198 /* Check for pad = 0, long header case */
4199 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4200 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4203 /* Check for pad = 1, short header case */
4204 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
4205 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4206 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4209 /* Check for pad = 0, short header case */
4210 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4211 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4215 /* Combine with test for linktype */
4221 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4222 * test the bottom-of-stack bit, and then check the version number
4223 * field in the IP header.
4225 static struct block
*
4226 gen_mpls_linktype(proto
)
4229 struct block
*b0
, *b1
;
4234 /* match the bottom-of-stack bit */
4235 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4236 /* match the IPv4 version number */
4237 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4242 /* match the bottom-of-stack bit */
4243 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4244 /* match the IPv4 version number */
4245 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4254 static struct block
*
4255 gen_host(addr
, mask
, proto
, dir
, type
)
4262 struct block
*b0
, *b1
;
4263 const char *typestr
;
4273 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4275 * Only check for non-IPv4 addresses if we're not
4276 * checking MPLS-encapsulated packets.
4278 if (label_stack_depth
== 0) {
4279 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4281 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4287 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4290 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4293 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4296 bpf_error("'tcp' modifier applied to %s", typestr
);
4299 bpf_error("'sctp' modifier applied to %s", typestr
);
4302 bpf_error("'udp' modifier applied to %s", typestr
);
4305 bpf_error("'icmp' modifier applied to %s", typestr
);
4308 bpf_error("'igmp' modifier applied to %s", typestr
);
4311 bpf_error("'igrp' modifier applied to %s", typestr
);
4314 bpf_error("'pim' modifier applied to %s", typestr
);
4317 bpf_error("'vrrp' modifier applied to %s", typestr
);
4320 bpf_error("ATALK host filtering not implemented");
4323 bpf_error("AARP host filtering not implemented");
4326 return gen_dnhostop(addr
, dir
);
4329 bpf_error("SCA host filtering not implemented");
4332 bpf_error("LAT host filtering not implemented");
4335 bpf_error("MOPDL host filtering not implemented");
4338 bpf_error("MOPRC host filtering not implemented");
4342 bpf_error("'ip6' modifier applied to ip host");
4345 bpf_error("'icmp6' modifier applied to %s", typestr
);
4349 bpf_error("'ah' modifier applied to %s", typestr
);
4352 bpf_error("'esp' modifier applied to %s", typestr
);
4355 bpf_error("ISO host filtering not implemented");
4358 bpf_error("'esis' modifier applied to %s", typestr
);
4361 bpf_error("'isis' modifier applied to %s", typestr
);
4364 bpf_error("'clnp' modifier applied to %s", typestr
);
4367 bpf_error("'stp' modifier applied to %s", typestr
);
4370 bpf_error("IPX host filtering not implemented");
4373 bpf_error("'netbeui' modifier applied to %s", typestr
);
4376 bpf_error("'radio' modifier applied to %s", typestr
);
4385 static struct block
*
4386 gen_host6(addr
, mask
, proto
, dir
, type
)
4387 struct in6_addr
*addr
;
4388 struct in6_addr
*mask
;
4393 const char *typestr
;
4403 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4406 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4409 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4412 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4415 bpf_error("'sctp' modifier applied to %s", typestr
);
4418 bpf_error("'tcp' modifier applied to %s", typestr
);
4421 bpf_error("'udp' modifier applied to %s", typestr
);
4424 bpf_error("'icmp' modifier applied to %s", typestr
);
4427 bpf_error("'igmp' modifier applied to %s", typestr
);
4430 bpf_error("'igrp' modifier applied to %s", typestr
);
4433 bpf_error("'pim' modifier applied to %s", typestr
);
4436 bpf_error("'vrrp' modifier applied to %s", typestr
);
4439 bpf_error("ATALK host filtering not implemented");
4442 bpf_error("AARP host filtering not implemented");
4445 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4448 bpf_error("SCA host filtering not implemented");
4451 bpf_error("LAT host filtering not implemented");
4454 bpf_error("MOPDL host filtering not implemented");
4457 bpf_error("MOPRC host filtering not implemented");
4460 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4463 bpf_error("'icmp6' modifier applied to %s", typestr
);
4466 bpf_error("'ah' modifier applied to %s", typestr
);
4469 bpf_error("'esp' modifier applied to %s", typestr
);
4472 bpf_error("ISO host filtering not implemented");
4475 bpf_error("'esis' modifier applied to %s", typestr
);
4478 bpf_error("'isis' modifier applied to %s", typestr
);
4481 bpf_error("'clnp' modifier applied to %s", typestr
);
4484 bpf_error("'stp' modifier applied to %s", typestr
);
4487 bpf_error("IPX host filtering not implemented");
4490 bpf_error("'netbeui' modifier applied to %s", typestr
);
4493 bpf_error("'radio' modifier applied to %s", typestr
);
4503 static struct block
*
4504 gen_gateway(eaddr
, alist
, proto
, dir
)
4505 const u_char
*eaddr
;
4506 bpf_u_int32
**alist
;
4510 struct block
*b0
, *b1
, *tmp
;
4513 bpf_error("direction applied to 'gateway'");
4522 b0
= gen_ehostop(eaddr
, Q_OR
);
4525 b0
= gen_fhostop(eaddr
, Q_OR
);
4528 b0
= gen_thostop(eaddr
, Q_OR
);
4530 case DLT_IEEE802_11
:
4531 case DLT_PRISM_HEADER
:
4532 case DLT_IEEE802_11_RADIO_AVS
:
4533 case DLT_IEEE802_11_RADIO
:
4535 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4540 * Check that the packet doesn't begin with an
4541 * LE Control marker. (We've already generated
4544 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4549 * Now check the MAC address.
4551 b0
= gen_ehostop(eaddr
, Q_OR
);
4555 case DLT_IP_OVER_FC
:
4556 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4560 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4562 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4564 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4573 bpf_error("illegal modifier of 'gateway'");
4579 gen_proto_abbrev(proto
)
4588 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4590 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4596 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4598 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4604 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4606 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4612 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4615 #ifndef IPPROTO_IGMP
4616 #define IPPROTO_IGMP 2
4620 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4623 #ifndef IPPROTO_IGRP
4624 #define IPPROTO_IGRP 9
4627 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4631 #define IPPROTO_PIM 103
4635 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4637 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4642 #ifndef IPPROTO_VRRP
4643 #define IPPROTO_VRRP 112
4647 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4651 b1
= gen_linktype(ETHERTYPE_IP
);
4655 b1
= gen_linktype(ETHERTYPE_ARP
);
4659 b1
= gen_linktype(ETHERTYPE_REVARP
);
4663 bpf_error("link layer applied in wrong context");
4666 b1
= gen_linktype(ETHERTYPE_ATALK
);
4670 b1
= gen_linktype(ETHERTYPE_AARP
);
4674 b1
= gen_linktype(ETHERTYPE_DN
);
4678 b1
= gen_linktype(ETHERTYPE_SCA
);
4682 b1
= gen_linktype(ETHERTYPE_LAT
);
4686 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4690 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4695 b1
= gen_linktype(ETHERTYPE_IPV6
);
4698 #ifndef IPPROTO_ICMPV6
4699 #define IPPROTO_ICMPV6 58
4702 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4707 #define IPPROTO_AH 51
4710 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4712 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4718 #define IPPROTO_ESP 50
4721 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4723 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4729 b1
= gen_linktype(LLCSAP_ISONS
);
4733 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4737 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4740 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4741 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4742 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4744 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4746 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4748 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4752 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4753 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4754 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4756 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4758 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4760 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4764 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4765 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4766 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4768 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4773 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4774 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4779 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4780 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4782 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4784 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4789 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4790 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4795 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4796 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4801 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4805 b1
= gen_linktype(LLCSAP_8021D
);
4809 b1
= gen_linktype(LLCSAP_IPX
);
4813 b1
= gen_linktype(LLCSAP_NETBEUI
);
4817 bpf_error("'radio' is not a valid protocol type");
4825 static struct block
*
4832 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4833 b
= new_block(JMP(BPF_JSET
));
4842 * Generate a comparison to a port value in the transport-layer header
4843 * at the specified offset from the beginning of that header.
4845 * XXX - this handles a variable-length prefix preceding the link-layer
4846 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4847 * variable-length link-layer headers (such as Token Ring or 802.11
4850 static struct block
*
4851 gen_portatom(off
, v
)
4855 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4859 static struct block
*
4860 gen_portatom6(off
, v
)
4864 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4869 gen_portop(port
, proto
, dir
)
4870 int port
, proto
, dir
;
4872 struct block
*b0
, *b1
, *tmp
;
4874 /* ip proto 'proto' */
4875 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4881 b1
= gen_portatom(0, (bpf_int32
)port
);
4885 b1
= gen_portatom(2, (bpf_int32
)port
);
4890 tmp
= gen_portatom(0, (bpf_int32
)port
);
4891 b1
= gen_portatom(2, (bpf_int32
)port
);
4896 tmp
= gen_portatom(0, (bpf_int32
)port
);
4897 b1
= gen_portatom(2, (bpf_int32
)port
);
4909 static struct block
*
4910 gen_port(port
, ip_proto
, dir
)
4915 struct block
*b0
, *b1
, *tmp
;
4920 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4921 * not LLC encapsulation with LLCSAP_IP.
4923 * For IEEE 802 networks - which includes 802.5 token ring
4924 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4925 * says that SNAP encapsulation is used, not LLC encapsulation
4928 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4929 * RFC 2225 say that SNAP encapsulation is used, not LLC
4930 * encapsulation with LLCSAP_IP.
4932 * So we always check for ETHERTYPE_IP.
4934 b0
= gen_linktype(ETHERTYPE_IP
);
4940 b1
= gen_portop(port
, ip_proto
, dir
);
4944 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4945 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4947 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4960 gen_portop6(port
, proto
, dir
)
4961 int port
, proto
, dir
;
4963 struct block
*b0
, *b1
, *tmp
;
4965 /* ip6 proto 'proto' */
4966 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4970 b1
= gen_portatom6(0, (bpf_int32
)port
);
4974 b1
= gen_portatom6(2, (bpf_int32
)port
);
4979 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4980 b1
= gen_portatom6(2, (bpf_int32
)port
);
4985 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4986 b1
= gen_portatom6(2, (bpf_int32
)port
);
4998 static struct block
*
4999 gen_port6(port
, ip_proto
, dir
)
5004 struct block
*b0
, *b1
, *tmp
;
5006 /* link proto ip6 */
5007 b0
= gen_linktype(ETHERTYPE_IPV6
);
5013 b1
= gen_portop6(port
, ip_proto
, dir
);
5017 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5018 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5020 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5032 /* gen_portrange code */
5033 static struct block
*
5034 gen_portrangeatom(off
, v1
, v2
)
5038 struct block
*b1
, *b2
;
5042 * Reverse the order of the ports, so v1 is the lower one.
5051 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5052 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5060 gen_portrangeop(port1
, port2
, proto
, dir
)
5065 struct block
*b0
, *b1
, *tmp
;
5067 /* ip proto 'proto' */
5068 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
5074 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5078 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5083 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5084 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5089 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5090 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5102 static struct block
*
5103 gen_portrange(port1
, port2
, ip_proto
, dir
)
5108 struct block
*b0
, *b1
, *tmp
;
5111 b0
= gen_linktype(ETHERTYPE_IP
);
5117 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5121 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5122 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5124 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5136 static struct block
*
5137 gen_portrangeatom6(off
, v1
, v2
)
5141 struct block
*b1
, *b2
;
5145 * Reverse the order of the ports, so v1 is the lower one.
5154 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5155 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5163 gen_portrangeop6(port1
, port2
, proto
, dir
)
5168 struct block
*b0
, *b1
, *tmp
;
5170 /* ip6 proto 'proto' */
5171 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
5175 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5179 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5184 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5185 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5190 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5191 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5203 static struct block
*
5204 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5209 struct block
*b0
, *b1
, *tmp
;
5211 /* link proto ip6 */
5212 b0
= gen_linktype(ETHERTYPE_IPV6
);
5218 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5222 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5223 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5225 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5238 lookup_proto(name
, proto
)
5239 register const char *name
;
5249 v
= pcap_nametoproto(name
);
5250 if (v
== PROTO_UNDEF
)
5251 bpf_error("unknown ip proto '%s'", name
);
5255 /* XXX should look up h/w protocol type based on linktype */
5256 v
= pcap_nametoeproto(name
);
5257 if (v
== PROTO_UNDEF
) {
5258 v
= pcap_nametollc(name
);
5259 if (v
== PROTO_UNDEF
)
5260 bpf_error("unknown ether proto '%s'", name
);
5265 if (strcmp(name
, "esis") == 0)
5267 else if (strcmp(name
, "isis") == 0)
5269 else if (strcmp(name
, "clnp") == 0)
5272 bpf_error("unknown osi proto '%s'", name
);
5292 static struct block
*
5293 gen_protochain(v
, proto
, dir
)
5298 #ifdef NO_PROTOCHAIN
5299 return gen_proto(v
, proto
, dir
);
5301 struct block
*b0
, *b
;
5302 struct slist
*s
[100];
5303 int fix2
, fix3
, fix4
, fix5
;
5304 int ahcheck
, again
, end
;
5306 int reg2
= alloc_reg();
5308 memset(s
, 0, sizeof(s
));
5309 fix2
= fix3
= fix4
= fix5
= 0;
5316 b0
= gen_protochain(v
, Q_IP
, dir
);
5317 b
= gen_protochain(v
, Q_IPV6
, dir
);
5321 bpf_error("bad protocol applied for 'protochain'");
5326 * We don't handle variable-length prefixes before the link-layer
5327 * header, or variable-length link-layer headers, here yet.
5328 * We might want to add BPF instructions to do the protochain
5329 * work, to simplify that and, on platforms that have a BPF
5330 * interpreter with the new instructions, let the filtering
5331 * be done in the kernel. (We already require a modified BPF
5332 * engine to do the protochain stuff, to support backward
5333 * branches, and backward branch support is unlikely to appear
5334 * in kernel BPF engines.)
5338 case DLT_IEEE802_11
:
5339 case DLT_PRISM_HEADER
:
5340 case DLT_IEEE802_11_RADIO_AVS
:
5341 case DLT_IEEE802_11_RADIO
:
5343 bpf_error("'protochain' not supported with 802.11");
5346 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5349 * s[0] is a dummy entry to protect other BPF insn from damage
5350 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5351 * hard to find interdependency made by jump table fixup.
5354 s
[i
] = new_stmt(0); /*dummy*/
5359 b0
= gen_linktype(ETHERTYPE_IP
);
5362 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5363 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5365 /* X = ip->ip_hl << 2 */
5366 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5367 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5372 b0
= gen_linktype(ETHERTYPE_IPV6
);
5374 /* A = ip6->ip_nxt */
5375 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5376 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5378 /* X = sizeof(struct ip6_hdr) */
5379 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5385 bpf_error("unsupported proto to gen_protochain");
5389 /* again: if (A == v) goto end; else fall through; */
5391 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5393 s
[i
]->s
.jt
= NULL
; /*later*/
5394 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5398 #ifndef IPPROTO_NONE
5399 #define IPPROTO_NONE 59
5401 /* if (A == IPPROTO_NONE) goto end */
5402 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5403 s
[i
]->s
.jt
= NULL
; /*later*/
5404 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5405 s
[i
]->s
.k
= IPPROTO_NONE
;
5406 s
[fix5
]->s
.jf
= s
[i
];
5411 if (proto
== Q_IPV6
) {
5412 int v6start
, v6end
, v6advance
, j
;
5415 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5416 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5417 s
[i
]->s
.jt
= NULL
; /*later*/
5418 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5419 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5420 s
[fix2
]->s
.jf
= s
[i
];
5422 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5423 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5424 s
[i
]->s
.jt
= NULL
; /*later*/
5425 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5426 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5428 /* if (A == IPPROTO_ROUTING) goto v6advance */
5429 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5430 s
[i
]->s
.jt
= NULL
; /*later*/
5431 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5432 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5434 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5435 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5436 s
[i
]->s
.jt
= NULL
; /*later*/
5437 s
[i
]->s
.jf
= NULL
; /*later*/
5438 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5449 * X = X + (P[X + 1] + 1) * 8;
5452 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5454 /* A = P[X + packet head] */
5455 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5456 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5459 s
[i
] = new_stmt(BPF_ST
);
5463 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5466 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5470 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5472 /* A = P[X + packet head]; */
5473 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5474 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5477 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5481 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5485 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5488 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5492 /* goto again; (must use BPF_JA for backward jump) */
5493 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5494 s
[i
]->s
.k
= again
- i
- 1;
5495 s
[i
- 1]->s
.jf
= s
[i
];
5499 for (j
= v6start
; j
<= v6end
; j
++)
5500 s
[j
]->s
.jt
= s
[v6advance
];
5505 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5507 s
[fix2
]->s
.jf
= s
[i
];
5513 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5514 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5515 s
[i
]->s
.jt
= NULL
; /*later*/
5516 s
[i
]->s
.jf
= NULL
; /*later*/
5517 s
[i
]->s
.k
= IPPROTO_AH
;
5519 s
[fix3
]->s
.jf
= s
[ahcheck
];
5526 * X = X + (P[X + 1] + 2) * 4;
5529 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5531 /* A = P[X + packet head]; */
5532 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5533 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5536 s
[i
] = new_stmt(BPF_ST
);
5540 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5543 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5547 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5549 /* A = P[X + packet head] */
5550 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5551 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5554 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5558 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5562 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5565 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5569 /* goto again; (must use BPF_JA for backward jump) */
5570 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5571 s
[i
]->s
.k
= again
- i
- 1;
5576 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5578 s
[fix2
]->s
.jt
= s
[end
];
5579 s
[fix4
]->s
.jf
= s
[end
];
5580 s
[fix5
]->s
.jt
= s
[end
];
5587 for (i
= 0; i
< max
- 1; i
++)
5588 s
[i
]->next
= s
[i
+ 1];
5589 s
[max
- 1]->next
= NULL
;
5594 b
= new_block(JMP(BPF_JEQ
));
5595 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5605 static struct block
*
5606 gen_check_802_11_data_frame()
5609 struct block
*b0
, *b1
;
5612 * A data frame has the 0x08 bit (b3) in the frame control field set
5613 * and the 0x04 bit (b2) clear.
5615 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5616 b0
= new_block(JMP(BPF_JSET
));
5620 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5621 b1
= new_block(JMP(BPF_JSET
));
5632 * Generate code that checks whether the packet is a packet for protocol
5633 * <proto> and whether the type field in that protocol's header has
5634 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5635 * IP packet and checks the protocol number in the IP header against <v>.
5637 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5638 * against Q_IP and Q_IPV6.
5640 static struct block
*
5641 gen_proto(v
, proto
, dir
)
5646 struct block
*b0
, *b1
;
5648 if (dir
!= Q_DEFAULT
)
5649 bpf_error("direction applied to 'proto'");
5654 b0
= gen_proto(v
, Q_IP
, dir
);
5655 b1
= gen_proto(v
, Q_IPV6
, dir
);
5663 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5664 * not LLC encapsulation with LLCSAP_IP.
5666 * For IEEE 802 networks - which includes 802.5 token ring
5667 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5668 * says that SNAP encapsulation is used, not LLC encapsulation
5671 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5672 * RFC 2225 say that SNAP encapsulation is used, not LLC
5673 * encapsulation with LLCSAP_IP.
5675 * So we always check for ETHERTYPE_IP.
5677 b0
= gen_linktype(ETHERTYPE_IP
);
5679 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5681 b1
= gen_protochain(v
, Q_IP
);
5691 * Frame Relay packets typically have an OSI
5692 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5693 * generates code to check for all the OSI
5694 * NLPIDs, so calling it and then adding a check
5695 * for the particular NLPID for which we're
5696 * looking is bogus, as we can just check for
5699 * What we check for is the NLPID and a frame
5700 * control field value of UI, i.e. 0x03 followed
5703 * XXX - assumes a 2-byte Frame Relay header with
5704 * DLCI and flags. What if the address is longer?
5706 * XXX - what about SNAP-encapsulated frames?
5708 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5714 * Cisco uses an Ethertype lookalike - for OSI,
5717 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5718 /* OSI in C-HDLC is stuffed with a fudge byte */
5719 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5724 b0
= gen_linktype(LLCSAP_ISONS
);
5725 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5731 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5733 * 4 is the offset of the PDU type relative to the IS-IS
5736 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5741 bpf_error("arp does not encapsulate another protocol");
5745 bpf_error("rarp does not encapsulate another protocol");
5749 bpf_error("atalk encapsulation is not specifiable");
5753 bpf_error("decnet encapsulation is not specifiable");
5757 bpf_error("sca does not encapsulate another protocol");
5761 bpf_error("lat does not encapsulate another protocol");
5765 bpf_error("moprc does not encapsulate another protocol");
5769 bpf_error("mopdl does not encapsulate another protocol");
5773 return gen_linktype(v
);
5776 bpf_error("'udp proto' is bogus");
5780 bpf_error("'tcp proto' is bogus");
5784 bpf_error("'sctp proto' is bogus");
5788 bpf_error("'icmp proto' is bogus");
5792 bpf_error("'igmp proto' is bogus");
5796 bpf_error("'igrp proto' is bogus");
5800 bpf_error("'pim proto' is bogus");
5804 bpf_error("'vrrp proto' is bogus");
5809 b0
= gen_linktype(ETHERTYPE_IPV6
);
5811 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5813 b1
= gen_protochain(v
, Q_IPV6
);
5819 bpf_error("'icmp6 proto' is bogus");
5823 bpf_error("'ah proto' is bogus");
5826 bpf_error("'ah proto' is bogus");
5829 bpf_error("'stp proto' is bogus");
5832 bpf_error("'ipx proto' is bogus");
5835 bpf_error("'netbeui proto' is bogus");
5838 bpf_error("'radio proto' is bogus");
5849 register const char *name
;
5852 int proto
= q
.proto
;
5856 bpf_u_int32 mask
, addr
;
5858 bpf_u_int32
**alist
;
5861 struct sockaddr_in
*sin4
;
5862 struct sockaddr_in6
*sin6
;
5863 struct addrinfo
*res
, *res0
;
5864 struct in6_addr mask128
;
5866 struct block
*b
, *tmp
;
5867 int port
, real_proto
;
5873 addr
= pcap_nametonetaddr(name
);
5875 bpf_error("unknown network '%s'", name
);
5876 /* Left justify network addr and calculate its network mask */
5878 while (addr
&& (addr
& 0xff000000) == 0) {
5882 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5886 if (proto
== Q_LINK
) {
5890 eaddr
= pcap_ether_hostton(name
);
5893 "unknown ether host '%s'", name
);
5894 b
= gen_ehostop(eaddr
, dir
);
5899 eaddr
= pcap_ether_hostton(name
);
5902 "unknown FDDI host '%s'", name
);
5903 b
= gen_fhostop(eaddr
, dir
);
5908 eaddr
= pcap_ether_hostton(name
);
5911 "unknown token ring host '%s'", name
);
5912 b
= gen_thostop(eaddr
, dir
);
5916 case DLT_IEEE802_11
:
5917 case DLT_PRISM_HEADER
:
5918 case DLT_IEEE802_11_RADIO_AVS
:
5919 case DLT_IEEE802_11_RADIO
:
5921 eaddr
= pcap_ether_hostton(name
);
5924 "unknown 802.11 host '%s'", name
);
5925 b
= gen_wlanhostop(eaddr
, dir
);
5929 case DLT_IP_OVER_FC
:
5930 eaddr
= pcap_ether_hostton(name
);
5933 "unknown Fibre Channel host '%s'", name
);
5934 b
= gen_ipfchostop(eaddr
, dir
);
5943 * Check that the packet doesn't begin
5944 * with an LE Control marker. (We've
5945 * already generated a test for LANE.)
5947 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5951 eaddr
= pcap_ether_hostton(name
);
5954 "unknown ether host '%s'", name
);
5955 b
= gen_ehostop(eaddr
, dir
);
5961 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5962 } else if (proto
== Q_DECNET
) {
5963 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5965 * I don't think DECNET hosts can be multihomed, so
5966 * there is no need to build up a list of addresses
5968 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5971 alist
= pcap_nametoaddr(name
);
5972 if (alist
== NULL
|| *alist
== NULL
)
5973 bpf_error("unknown host '%s'", name
);
5975 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5977 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5979 tmp
= gen_host(**alist
++, 0xffffffff,
5980 tproto
, dir
, q
.addr
);
5986 memset(&mask128
, 0xff, sizeof(mask128
));
5987 res0
= res
= pcap_nametoaddrinfo(name
);
5989 bpf_error("unknown host '%s'", name
);
5991 tproto
= tproto6
= proto
;
5992 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5996 for (res
= res0
; res
; res
= res
->ai_next
) {
5997 switch (res
->ai_family
) {
5999 if (tproto
== Q_IPV6
)
6002 sin4
= (struct sockaddr_in
*)
6004 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6005 0xffffffff, tproto
, dir
, q
.addr
);
6008 if (tproto6
== Q_IP
)
6011 sin6
= (struct sockaddr_in6
*)
6013 tmp
= gen_host6(&sin6
->sin6_addr
,
6014 &mask128
, tproto6
, dir
, q
.addr
);
6025 bpf_error("unknown host '%s'%s", name
,
6026 (proto
== Q_DEFAULT
)
6028 : " for specified address family");
6035 if (proto
!= Q_DEFAULT
&&
6036 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6037 bpf_error("illegal qualifier of 'port'");
6038 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6039 bpf_error("unknown port '%s'", name
);
6040 if (proto
== Q_UDP
) {
6041 if (real_proto
== IPPROTO_TCP
)
6042 bpf_error("port '%s' is tcp", name
);
6043 else if (real_proto
== IPPROTO_SCTP
)
6044 bpf_error("port '%s' is sctp", name
);
6046 /* override PROTO_UNDEF */
6047 real_proto
= IPPROTO_UDP
;
6049 if (proto
== Q_TCP
) {
6050 if (real_proto
== IPPROTO_UDP
)
6051 bpf_error("port '%s' is udp", name
);
6053 else if (real_proto
== IPPROTO_SCTP
)
6054 bpf_error("port '%s' is sctp", name
);
6056 /* override PROTO_UNDEF */
6057 real_proto
= IPPROTO_TCP
;
6059 if (proto
== Q_SCTP
) {
6060 if (real_proto
== IPPROTO_UDP
)
6061 bpf_error("port '%s' is udp", name
);
6063 else if (real_proto
== IPPROTO_TCP
)
6064 bpf_error("port '%s' is tcp", name
);
6066 /* override PROTO_UNDEF */
6067 real_proto
= IPPROTO_SCTP
;
6070 return gen_port(port
, real_proto
, dir
);
6072 b
= gen_port(port
, real_proto
, dir
);
6073 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6078 if (proto
!= Q_DEFAULT
&&
6079 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6080 bpf_error("illegal qualifier of 'portrange'");
6081 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6082 bpf_error("unknown port in range '%s'", name
);
6083 if (proto
== Q_UDP
) {
6084 if (real_proto
== IPPROTO_TCP
)
6085 bpf_error("port in range '%s' is tcp", name
);
6086 else if (real_proto
== IPPROTO_SCTP
)
6087 bpf_error("port in range '%s' is sctp", name
);
6089 /* override PROTO_UNDEF */
6090 real_proto
= IPPROTO_UDP
;
6092 if (proto
== Q_TCP
) {
6093 if (real_proto
== IPPROTO_UDP
)
6094 bpf_error("port in range '%s' is udp", name
);
6095 else if (real_proto
== IPPROTO_SCTP
)
6096 bpf_error("port in range '%s' is sctp", name
);
6098 /* override PROTO_UNDEF */
6099 real_proto
= IPPROTO_TCP
;
6101 if (proto
== Q_SCTP
) {
6102 if (real_proto
== IPPROTO_UDP
)
6103 bpf_error("port in range '%s' is udp", name
);
6104 else if (real_proto
== IPPROTO_TCP
)
6105 bpf_error("port in range '%s' is tcp", name
);
6107 /* override PROTO_UNDEF */
6108 real_proto
= IPPROTO_SCTP
;
6111 return gen_portrange(port1
, port2
, real_proto
, dir
);
6113 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6114 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6120 eaddr
= pcap_ether_hostton(name
);
6122 bpf_error("unknown ether host: %s", name
);
6124 alist
= pcap_nametoaddr(name
);
6125 if (alist
== NULL
|| *alist
== NULL
)
6126 bpf_error("unknown host '%s'", name
);
6127 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6131 bpf_error("'gateway' not supported in this configuration");
6135 real_proto
= lookup_proto(name
, proto
);
6136 if (real_proto
>= 0)
6137 return gen_proto(real_proto
, proto
, dir
);
6139 bpf_error("unknown protocol: %s", name
);
6142 real_proto
= lookup_proto(name
, proto
);
6143 if (real_proto
>= 0)
6144 return gen_protochain(real_proto
, proto
, dir
);
6146 bpf_error("unknown protocol: %s", name
);
6157 gen_mcode(s1
, s2
, masklen
, q
)
6158 register const char *s1
, *s2
;
6159 register int masklen
;
6162 register int nlen
, mlen
;
6165 nlen
= __pcap_atoin(s1
, &n
);
6166 /* Promote short ipaddr */
6170 mlen
= __pcap_atoin(s2
, &m
);
6171 /* Promote short ipaddr */
6174 bpf_error("non-network bits set in \"%s mask %s\"",
6177 /* Convert mask len to mask */
6179 bpf_error("mask length must be <= 32");
6182 * X << 32 is not guaranteed by C to be 0; it's
6187 m
= 0xffffffff << (32 - masklen
);
6189 bpf_error("non-network bits set in \"%s/%d\"",
6196 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6199 bpf_error("Mask syntax for networks only");
6208 register const char *s
;
6213 int proto
= q
.proto
;
6219 else if (q
.proto
== Q_DECNET
)
6220 vlen
= __pcap_atodn(s
, &v
);
6222 vlen
= __pcap_atoin(s
, &v
);
6229 if (proto
== Q_DECNET
)
6230 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6231 else if (proto
== Q_LINK
) {
6232 bpf_error("illegal link layer address");
6235 if (s
== NULL
&& q
.addr
== Q_NET
) {
6236 /* Promote short net number */
6237 while (v
&& (v
& 0xff000000) == 0) {
6242 /* Promote short ipaddr */
6246 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6251 proto
= IPPROTO_UDP
;
6252 else if (proto
== Q_TCP
)
6253 proto
= IPPROTO_TCP
;
6254 else if (proto
== Q_SCTP
)
6255 proto
= IPPROTO_SCTP
;
6256 else if (proto
== Q_DEFAULT
)
6257 proto
= PROTO_UNDEF
;
6259 bpf_error("illegal qualifier of 'port'");
6262 return gen_port((int)v
, proto
, dir
);
6266 b
= gen_port((int)v
, proto
, dir
);
6267 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6274 proto
= IPPROTO_UDP
;
6275 else if (proto
== Q_TCP
)
6276 proto
= IPPROTO_TCP
;
6277 else if (proto
== Q_SCTP
)
6278 proto
= IPPROTO_SCTP
;
6279 else if (proto
== Q_DEFAULT
)
6280 proto
= PROTO_UNDEF
;
6282 bpf_error("illegal qualifier of 'portrange'");
6285 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6289 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6290 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6296 bpf_error("'gateway' requires a name");
6300 return gen_proto((int)v
, proto
, dir
);
6303 return gen_protochain((int)v
, proto
, dir
);
6318 gen_mcode6(s1
, s2
, masklen
, q
)
6319 register const char *s1
, *s2
;
6320 register int masklen
;
6323 struct addrinfo
*res
;
6324 struct in6_addr
*addr
;
6325 struct in6_addr mask
;
6330 bpf_error("no mask %s supported", s2
);
6332 res
= pcap_nametoaddrinfo(s1
);
6334 bpf_error("invalid ip6 address %s", s1
);
6336 bpf_error("%s resolved to multiple address", s1
);
6337 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6339 if (sizeof(mask
) * 8 < masklen
)
6340 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6341 memset(&mask
, 0, sizeof(mask
));
6342 memset(&mask
, 0xff, masklen
/ 8);
6344 mask
.s6_addr
[masklen
/ 8] =
6345 (0xff << (8 - masklen
% 8)) & 0xff;
6348 a
= (u_int32_t
*)addr
;
6349 m
= (u_int32_t
*)&mask
;
6350 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6351 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6352 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6360 bpf_error("Mask syntax for networks only");
6364 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6369 bpf_error("invalid qualifier against IPv6 address");
6378 register const u_char
*eaddr
;
6381 struct block
*b
, *tmp
;
6383 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6386 return gen_ehostop(eaddr
, (int)q
.dir
);
6388 return gen_fhostop(eaddr
, (int)q
.dir
);
6390 return gen_thostop(eaddr
, (int)q
.dir
);
6391 case DLT_IEEE802_11
:
6392 case DLT_PRISM_HEADER
:
6393 case DLT_IEEE802_11_RADIO_AVS
:
6394 case DLT_IEEE802_11_RADIO
:
6396 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6400 * Check that the packet doesn't begin with an
6401 * LE Control marker. (We've already generated
6404 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6409 * Now check the MAC address.
6411 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6416 case DLT_IP_OVER_FC
:
6417 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6419 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6423 bpf_error("ethernet address used in non-ether expression");
6430 struct slist
*s0
, *s1
;
6433 * This is definitely not the best way to do this, but the
6434 * lists will rarely get long.
6441 static struct slist
*
6447 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6452 static struct slist
*
6458 s
= new_stmt(BPF_LD
|BPF_MEM
);
6464 * Modify "index" to use the value stored into its register as an
6465 * offset relative to the beginning of the header for the protocol
6466 * "proto", and allocate a register and put an item "size" bytes long
6467 * (1, 2, or 4) at that offset into that register, making it the register
6471 gen_load(proto
, inst
, size
)
6476 struct slist
*s
, *tmp
;
6478 int regno
= alloc_reg();
6480 free_reg(inst
->regno
);
6484 bpf_error("data size must be 1, 2, or 4");
6500 bpf_error("unsupported index operation");
6504 * The offset is relative to the beginning of the packet
6505 * data, if we have a radio header. (If we don't, this
6508 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6509 linktype
!= DLT_IEEE802_11_RADIO
&&
6510 linktype
!= DLT_PRISM_HEADER
)
6511 bpf_error("radio information not present in capture");
6514 * Load into the X register the offset computed into the
6515 * register specifed by "index".
6517 s
= xfer_to_x(inst
);
6520 * Load the item at that offset.
6522 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6524 sappend(inst
->s
, s
);
6529 * The offset is relative to the beginning of
6530 * the link-layer header.
6532 * XXX - what about ATM LANE? Should the index be
6533 * relative to the beginning of the AAL5 frame, so
6534 * that 0 refers to the beginning of the LE Control
6535 * field, or relative to the beginning of the LAN
6536 * frame, so that 0 refers, for Ethernet LANE, to
6537 * the beginning of the destination address?
6539 s
= gen_llprefixlen();
6542 * If "s" is non-null, it has code to arrange that the
6543 * X register contains the length of the prefix preceding
6544 * the link-layer header. Add to it the offset computed
6545 * into the register specified by "index", and move that
6546 * into the X register. Otherwise, just load into the X
6547 * register the offset computed into the register specifed
6551 sappend(s
, xfer_to_a(inst
));
6552 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6553 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6555 s
= xfer_to_x(inst
);
6558 * Load the item at the sum of the offset we've put in the
6559 * X register and the offset of the start of the link
6560 * layer header (which is 0 if the radio header is
6561 * variable-length; that header length is what we put
6562 * into the X register and then added to the index).
6564 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6567 sappend(inst
->s
, s
);
6583 * The offset is relative to the beginning of
6584 * the network-layer header.
6585 * XXX - are there any cases where we want
6588 s
= gen_off_macpl();
6591 * If "s" is non-null, it has code to arrange that the
6592 * X register contains the offset of the MAC-layer
6593 * payload. Add to it the offset computed into the
6594 * register specified by "index", and move that into
6595 * the X register. Otherwise, just load into the X
6596 * register the offset computed into the register specifed
6600 sappend(s
, xfer_to_a(inst
));
6601 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6602 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6604 s
= xfer_to_x(inst
);
6607 * Load the item at the sum of the offset we've put in the
6608 * X register, the offset of the start of the network
6609 * layer header from the beginning of the MAC-layer
6610 * payload, and the purported offset of the start of the
6611 * MAC-layer payload (which might be 0 if there's a
6612 * variable-length prefix before the link-layer header
6613 * or the link-layer header itself is variable-length;
6614 * the variable-length offset of the start of the
6615 * MAC-layer payload is what we put into the X register
6616 * and then added to the index).
6618 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6619 tmp
->s
.k
= off_macpl
+ off_nl
;
6621 sappend(inst
->s
, s
);
6624 * Do the computation only if the packet contains
6625 * the protocol in question.
6627 b
= gen_proto_abbrev(proto
);
6629 gen_and(inst
->b
, b
);
6642 * The offset is relative to the beginning of
6643 * the transport-layer header.
6645 * Load the X register with the length of the IPv4 header
6646 * (plus the offset of the link-layer header, if it's
6647 * a variable-length header), in bytes.
6649 * XXX - are there any cases where we want
6651 * XXX - we should, if we're built with
6652 * IPv6 support, generate code to load either
6653 * IPv4, IPv6, or both, as appropriate.
6655 s
= gen_loadx_iphdrlen();
6658 * The X register now contains the sum of the length
6659 * of any variable-length header preceding the link-layer
6660 * header, any variable-length link-layer header, and the
6661 * length of the network-layer header.
6663 * Load into the A register the offset relative to
6664 * the beginning of the transport layer header,
6665 * add the X register to that, move that to the
6666 * X register, and load with an offset from the
6667 * X register equal to the offset of the network
6668 * layer header relative to the beginning of
6669 * the MAC-layer payload plus the fixed-length
6670 * portion of the offset of the MAC-layer payload
6671 * from the beginning of the raw packet data.
6673 sappend(s
, xfer_to_a(inst
));
6674 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6675 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6676 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6677 tmp
->s
.k
= off_macpl
+ off_nl
;
6678 sappend(inst
->s
, s
);
6681 * Do the computation only if the packet contains
6682 * the protocol in question - which is true only
6683 * if this is an IP datagram and is the first or
6684 * only fragment of that datagram.
6686 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6688 gen_and(inst
->b
, b
);
6690 gen_and(gen_proto_abbrev(Q_IP
), b
);
6696 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6700 inst
->regno
= regno
;
6701 s
= new_stmt(BPF_ST
);
6703 sappend(inst
->s
, s
);
6709 gen_relation(code
, a0
, a1
, reversed
)
6711 struct arth
*a0
, *a1
;
6714 struct slist
*s0
, *s1
, *s2
;
6715 struct block
*b
, *tmp
;
6719 if (code
== BPF_JEQ
) {
6720 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6721 b
= new_block(JMP(code
));
6725 b
= new_block(BPF_JMP
|code
|BPF_X
);
6731 sappend(a0
->s
, a1
->s
);
6735 free_reg(a0
->regno
);
6736 free_reg(a1
->regno
);
6738 /* 'and' together protocol checks */
6741 gen_and(a0
->b
, tmp
= a1
->b
);
6757 int regno
= alloc_reg();
6758 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6761 s
= new_stmt(BPF_LD
|BPF_LEN
);
6762 s
->next
= new_stmt(BPF_ST
);
6763 s
->next
->s
.k
= regno
;
6778 a
= (struct arth
*)newchunk(sizeof(*a
));
6782 s
= new_stmt(BPF_LD
|BPF_IMM
);
6784 s
->next
= new_stmt(BPF_ST
);
6800 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6803 s
= new_stmt(BPF_ST
);
6811 gen_arth(code
, a0
, a1
)
6813 struct arth
*a0
, *a1
;
6815 struct slist
*s0
, *s1
, *s2
;
6819 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6824 sappend(a0
->s
, a1
->s
);
6826 free_reg(a0
->regno
);
6827 free_reg(a1
->regno
);
6829 s0
= new_stmt(BPF_ST
);
6830 a0
->regno
= s0
->s
.k
= alloc_reg();
6837 * Here we handle simple allocation of the scratch registers.
6838 * If too many registers are alloc'd, the allocator punts.
6840 static int regused
[BPF_MEMWORDS
];
6844 * Initialize the table of used registers and the current register.
6850 memset(regused
, 0, sizeof regused
);
6854 * Return the next free register.
6859 int n
= BPF_MEMWORDS
;
6862 if (regused
[curreg
])
6863 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6865 regused
[curreg
] = 1;
6869 bpf_error("too many registers needed to evaluate expression");
6875 * Return a register to the table so it can
6885 static struct block
*
6892 s
= new_stmt(BPF_LD
|BPF_LEN
);
6893 b
= new_block(JMP(jmp
));
6904 return gen_len(BPF_JGE
, n
);
6908 * Actually, this is less than or equal.
6916 b
= gen_len(BPF_JGT
, n
);
6923 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6924 * the beginning of the link-layer header.
6925 * XXX - that means you can't test values in the radiotap header, but
6926 * as that header is difficult if not impossible to parse generally
6927 * without a loop, that might not be a severe problem. A new keyword
6928 * "radio" could be added for that, although what you'd really want
6929 * would be a way of testing particular radio header values, which
6930 * would generate code appropriate to the radio header in question.
6933 gen_byteop(op
, idx
, val
)
6944 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6947 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6951 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6955 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6959 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6963 b
= new_block(JMP(BPF_JEQ
));
6970 static u_char abroadcast
[] = { 0x0 };
6973 gen_broadcast(proto
)
6976 bpf_u_int32 hostmask
;
6977 struct block
*b0
, *b1
, *b2
;
6978 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6986 case DLT_ARCNET_LINUX
:
6987 return gen_ahostop(abroadcast
, Q_DST
);
6989 return gen_ehostop(ebroadcast
, Q_DST
);
6991 return gen_fhostop(ebroadcast
, Q_DST
);
6993 return gen_thostop(ebroadcast
, Q_DST
);
6994 case DLT_IEEE802_11
:
6995 case DLT_PRISM_HEADER
:
6996 case DLT_IEEE802_11_RADIO_AVS
:
6997 case DLT_IEEE802_11_RADIO
:
6999 return gen_wlanhostop(ebroadcast
, Q_DST
);
7000 case DLT_IP_OVER_FC
:
7001 return gen_ipfchostop(ebroadcast
, Q_DST
);
7005 * Check that the packet doesn't begin with an
7006 * LE Control marker. (We've already generated
7009 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7014 * Now check the MAC address.
7016 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7022 bpf_error("not a broadcast link");
7027 b0
= gen_linktype(ETHERTYPE_IP
);
7028 hostmask
= ~netmask
;
7029 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7030 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
7031 (bpf_int32
)(~0 & hostmask
), hostmask
);
7036 bpf_error("only link-layer/IP broadcast filters supported");
7042 * Generate code to test the low-order bit of a MAC address (that's
7043 * the bottom bit of the *first* byte).
7045 static struct block
*
7046 gen_mac_multicast(offset
)
7049 register struct block
*b0
;
7050 register struct slist
*s
;
7052 /* link[offset] & 1 != 0 */
7053 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
7054 b0
= new_block(JMP(BPF_JSET
));
7061 gen_multicast(proto
)
7064 register struct block
*b0
, *b1
, *b2
;
7065 register struct slist
*s
;
7073 case DLT_ARCNET_LINUX
:
7074 /* all ARCnet multicasts use the same address */
7075 return gen_ahostop(abroadcast
, Q_DST
);
7077 /* ether[0] & 1 != 0 */
7078 return gen_mac_multicast(0);
7081 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7083 * XXX - was that referring to bit-order issues?
7085 /* fddi[1] & 1 != 0 */
7086 return gen_mac_multicast(1);
7088 /* tr[2] & 1 != 0 */
7089 return gen_mac_multicast(2);
7090 case DLT_IEEE802_11
:
7091 case DLT_PRISM_HEADER
:
7092 case DLT_IEEE802_11_RADIO_AVS
:
7093 case DLT_IEEE802_11_RADIO
:
7098 * For control frames, there is no DA.
7100 * For management frames, DA is at an
7101 * offset of 4 from the beginning of
7104 * For data frames, DA is at an offset
7105 * of 4 from the beginning of the packet
7106 * if To DS is clear and at an offset of
7107 * 16 from the beginning of the packet
7112 * Generate the tests to be done for data frames.
7114 * First, check for To DS set, i.e. "link[1] & 0x01".
7116 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7117 b1
= new_block(JMP(BPF_JSET
));
7118 b1
->s
.k
= 0x01; /* To DS */
7122 * If To DS is set, the DA is at 16.
7124 b0
= gen_mac_multicast(16);
7128 * Now, check for To DS not set, i.e. check
7129 * "!(link[1] & 0x01)".
7131 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
7132 b2
= new_block(JMP(BPF_JSET
));
7133 b2
->s
.k
= 0x01; /* To DS */
7138 * If To DS is not set, the DA is at 4.
7140 b1
= gen_mac_multicast(4);
7144 * Now OR together the last two checks. That gives
7145 * the complete set of checks for data frames.
7150 * Now check for a data frame.
7151 * I.e, check "link[0] & 0x08".
7153 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7154 b1
= new_block(JMP(BPF_JSET
));
7159 * AND that with the checks done for data frames.
7164 * If the high-order bit of the type value is 0, this
7165 * is a management frame.
7166 * I.e, check "!(link[0] & 0x08)".
7168 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7169 b2
= new_block(JMP(BPF_JSET
));
7175 * For management frames, the DA is at 4.
7177 b1
= gen_mac_multicast(4);
7181 * OR that with the checks done for data frames.
7182 * That gives the checks done for management and
7188 * If the low-order bit of the type value is 1,
7189 * this is either a control frame or a frame
7190 * with a reserved type, and thus not a
7193 * I.e., check "!(link[0] & 0x04)".
7195 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
7196 b1
= new_block(JMP(BPF_JSET
));
7202 * AND that with the checks for data and management
7207 case DLT_IP_OVER_FC
:
7208 b0
= gen_mac_multicast(2);
7213 * Check that the packet doesn't begin with an
7214 * LE Control marker. (We've already generated
7217 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
7221 /* ether[off_mac] & 1 != 0 */
7222 b0
= gen_mac_multicast(off_mac
);
7230 /* Link not known to support multicasts */
7234 b0
= gen_linktype(ETHERTYPE_IP
);
7235 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7241 b0
= gen_linktype(ETHERTYPE_IPV6
);
7242 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7247 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7253 * generate command for inbound/outbound. It's here so we can
7254 * make it link-type specific. 'dir' = 0 implies "inbound",
7255 * = 1 implies "outbound".
7261 register struct block
*b0
;
7264 * Only some data link types support inbound/outbound qualifiers.
7268 b0
= gen_relation(BPF_JEQ
,
7269 gen_load(Q_LINK
, gen_loadi(0), 1),
7277 * Match packets sent by this machine.
7279 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7282 * Match packets sent to this machine.
7283 * (No broadcast or multicast packets, or
7284 * packets sent to some other machine and
7285 * received promiscuously.)
7287 * XXX - packets sent to other machines probably
7288 * shouldn't be matched, but what about broadcast
7289 * or multicast packets we received?
7291 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7295 #ifdef HAVE_NET_PFVAR_H
7297 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7298 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7304 /* match outgoing packets */
7305 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7307 /* match incoming packets */
7308 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7312 case DLT_JUNIPER_MFR
:
7313 case DLT_JUNIPER_MLFR
:
7314 case DLT_JUNIPER_MLPPP
:
7315 case DLT_JUNIPER_ATM1
:
7316 case DLT_JUNIPER_ATM2
:
7317 case DLT_JUNIPER_PPPOE
:
7318 case DLT_JUNIPER_PPPOE_ATM
:
7319 case DLT_JUNIPER_GGSN
:
7320 case DLT_JUNIPER_ES
:
7321 case DLT_JUNIPER_MONITOR
:
7322 case DLT_JUNIPER_SERVICES
:
7323 case DLT_JUNIPER_ETHER
:
7324 case DLT_JUNIPER_PPP
:
7325 case DLT_JUNIPER_FRELAY
:
7326 case DLT_JUNIPER_CHDLC
:
7327 case DLT_JUNIPER_VP
:
7328 case DLT_JUNIPER_ST
:
7329 case DLT_JUNIPER_ISM
:
7330 /* juniper flags (including direction) are stored
7331 * the byte after the 3-byte magic number */
7333 /* match outgoing packets */
7334 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7336 /* match incoming packets */
7337 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7342 bpf_error("inbound/outbound not supported on linktype %d",
7350 #ifdef HAVE_NET_PFVAR_H
7351 /* PF firewall log matched interface */
7353 gen_pf_ifname(const char *ifname
)
7358 if (linktype
!= DLT_PFLOG
) {
7359 bpf_error("ifname supported only on PF linktype");
7362 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7363 off
= offsetof(struct pfloghdr
, ifname
);
7364 if (strlen(ifname
) >= len
) {
7365 bpf_error("ifname interface names can only be %d characters",
7369 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7373 /* PF firewall log ruleset name */
7375 gen_pf_ruleset(char *ruleset
)
7379 if (linktype
!= DLT_PFLOG
) {
7380 bpf_error("ruleset supported only on PF linktype");
7384 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7385 bpf_error("ruleset names can only be %ld characters",
7386 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7390 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7391 strlen(ruleset
), (const u_char
*)ruleset
);
7395 /* PF firewall log rule number */
7401 if (linktype
!= DLT_PFLOG
) {
7402 bpf_error("rnr supported only on PF linktype");
7406 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7411 /* PF firewall log sub-rule number */
7413 gen_pf_srnr(int srnr
)
7417 if (linktype
!= DLT_PFLOG
) {
7418 bpf_error("srnr supported only on PF linktype");
7422 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7427 /* PF firewall log reason code */
7429 gen_pf_reason(int reason
)
7433 if (linktype
!= DLT_PFLOG
) {
7434 bpf_error("reason supported only on PF linktype");
7438 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7443 /* PF firewall log action */
7445 gen_pf_action(int action
)
7449 if (linktype
!= DLT_PFLOG
) {
7450 bpf_error("action supported only on PF linktype");
7454 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7458 #else /* !HAVE_NET_PFVAR_H */
7460 gen_pf_ifname(const char *ifname
)
7462 bpf_error("libpcap was compiled without pf support");
7468 gen_pf_ruleset(char *ruleset
)
7470 bpf_error("libpcap was compiled on a machine without pf support");
7478 bpf_error("libpcap was compiled on a machine without pf support");
7484 gen_pf_srnr(int srnr
)
7486 bpf_error("libpcap was compiled on a machine without pf support");
7492 gen_pf_reason(int reason
)
7494 bpf_error("libpcap was compiled on a machine without pf support");
7500 gen_pf_action(int action
)
7502 bpf_error("libpcap was compiled on a machine without pf support");
7506 #endif /* HAVE_NET_PFVAR_H */
7508 /* IEEE 802.11 wireless header */
7510 gen_p80211_type(int type
, int mask
)
7516 case DLT_IEEE802_11
:
7517 case DLT_PRISM_HEADER
:
7518 case DLT_IEEE802_11_RADIO_AVS
:
7519 case DLT_IEEE802_11_RADIO
:
7520 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7525 bpf_error("802.11 link-layer types supported only on 802.11");
7533 gen_p80211_fcdir(int fcdir
)
7539 case DLT_IEEE802_11
:
7540 case DLT_PRISM_HEADER
:
7541 case DLT_IEEE802_11_RADIO_AVS
:
7542 case DLT_IEEE802_11_RADIO
:
7546 bpf_error("frame direction supported only with 802.11 headers");
7550 b0
= gen_mcmp(OR_LINK
, 1, BPF_B
, (bpf_int32
)fcdir
,
7551 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7558 register const u_char
*eaddr
;
7564 case DLT_ARCNET_LINUX
:
7565 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7567 return (gen_ahostop(eaddr
, (int)q
.dir
));
7569 bpf_error("ARCnet address used in non-arc expression");
7575 bpf_error("aid supported only on ARCnet");
7578 bpf_error("ARCnet address used in non-arc expression");
7583 static struct block
*
7584 gen_ahostop(eaddr
, dir
)
7585 register const u_char
*eaddr
;
7588 register struct block
*b0
, *b1
;
7591 /* src comes first, different from Ethernet */
7593 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7596 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7599 b0
= gen_ahostop(eaddr
, Q_SRC
);
7600 b1
= gen_ahostop(eaddr
, Q_DST
);
7606 b0
= gen_ahostop(eaddr
, Q_SRC
);
7607 b1
= gen_ahostop(eaddr
, Q_DST
);
7616 * support IEEE 802.1Q VLAN trunk over ethernet
7622 struct block
*b0
, *b1
;
7624 /* can't check for VLAN-encapsulated packets inside MPLS */
7625 if (label_stack_depth
> 0)
7626 bpf_error("no VLAN match after MPLS");
7629 * Check for a VLAN packet, and then change the offsets to point
7630 * to the type and data fields within the VLAN packet. Just
7631 * increment the offsets, so that we can support a hierarchy, e.g.
7632 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7635 * XXX - this is a bit of a kludge. If we were to split the
7636 * compiler into a parser that parses an expression and
7637 * generates an expression tree, and a code generator that
7638 * takes an expression tree (which could come from our
7639 * parser or from some other parser) and generates BPF code,
7640 * we could perhaps make the offsets parameters of routines
7641 * and, in the handler for an "AND" node, pass to subnodes
7642 * other than the VLAN node the adjusted offsets.
7644 * This would mean that "vlan" would, instead of changing the
7645 * behavior of *all* tests after it, change only the behavior
7646 * of tests ANDed with it. That would change the documented
7647 * semantics of "vlan", which might break some expressions.
7648 * However, it would mean that "(vlan and ip) or ip" would check
7649 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7650 * checking only for VLAN-encapsulated IP, so that could still
7651 * be considered worth doing; it wouldn't break expressions
7652 * that are of the form "vlan and ..." or "vlan N and ...",
7653 * which I suspect are the most common expressions involving
7654 * "vlan". "vlan or ..." doesn't necessarily do what the user
7655 * would really want, now, as all the "or ..." tests would
7656 * be done assuming a VLAN, even though the "or" could be viewed
7657 * as meaning "or, if this isn't a VLAN packet...".
7664 /* check for VLAN */
7665 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7666 (bpf_int32
)ETHERTYPE_8021Q
);
7668 /* If a specific VLAN is requested, check VLAN id */
7669 if (vlan_num
>= 0) {
7670 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7671 (bpf_int32
)vlan_num
, 0x0fff);
7685 bpf_error("no VLAN support for data link type %d",
7700 struct block
*b0
,*b1
;
7703 * Change the offsets to point to the type and data fields within
7704 * the MPLS packet. Just increment the offsets, so that we
7705 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7706 * capture packets with an outer label of 100000 and an inner
7709 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7713 if (label_stack_depth
> 0) {
7714 /* just match the bottom-of-stack bit clear */
7715 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7718 * Indicate that we're checking MPLS-encapsulated headers,
7719 * to make sure higher level code generators don't try to
7720 * match against IP-related protocols such as Q_ARP, Q_RARP
7725 case DLT_C_HDLC
: /* fall through */
7727 b0
= gen_linktype(ETHERTYPE_MPLS
);
7731 b0
= gen_linktype(PPP_MPLS_UCAST
);
7734 /* FIXME add other DLT_s ...
7735 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7736 * leave it for now */
7739 bpf_error("no MPLS support for data link type %d",
7747 /* If a specific MPLS label is requested, check it */
7748 if (label_num
>= 0) {
7749 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7750 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7751 0xfffff000); /* only compare the first 20 bits */
7758 label_stack_depth
++;
7763 * Support PPPOE discovery and session.
7768 /* check for PPPoE discovery */
7769 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7778 * Test against the PPPoE session link-layer type.
7780 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7783 * Change the offsets to point to the type and data fields within
7784 * the PPP packet, and note that this is PPPoE rather than
7787 * XXX - this is a bit of a kludge. If we were to split the
7788 * compiler into a parser that parses an expression and
7789 * generates an expression tree, and a code generator that
7790 * takes an expression tree (which could come from our
7791 * parser or from some other parser) and generates BPF code,
7792 * we could perhaps make the offsets parameters of routines
7793 * and, in the handler for an "AND" node, pass to subnodes
7794 * other than the PPPoE node the adjusted offsets.
7796 * This would mean that "pppoes" would, instead of changing the
7797 * behavior of *all* tests after it, change only the behavior
7798 * of tests ANDed with it. That would change the documented
7799 * semantics of "pppoes", which might break some expressions.
7800 * However, it would mean that "(pppoes and ip) or ip" would check
7801 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7802 * checking only for VLAN-encapsulated IP, so that could still
7803 * be considered worth doing; it wouldn't break expressions
7804 * that are of the form "pppoes and ..." which I suspect are the
7805 * most common expressions involving "pppoes". "pppoes or ..."
7806 * doesn't necessarily do what the user would really want, now,
7807 * as all the "or ..." tests would be done assuming PPPoE, even
7808 * though the "or" could be viewed as meaning "or, if this isn't
7809 * a PPPoE packet...".
7811 orig_linktype
= off_linktype
; /* save original values */
7816 * The "network-layer" protocol is PPPoE, which has a 6-byte
7817 * PPPoE header, followed by a PPP packet.
7819 * There is no HDLC encapsulation for the PPP packet (it's
7820 * encapsulated in PPPoES instead), so the link-layer type
7821 * starts at the first byte of the PPP packet. For PPPoE,
7822 * that offset is relative to the beginning of the total
7823 * link-layer payload, including any 802.2 LLC header, so
7824 * it's 6 bytes past off_nl.
7826 off_linktype
= off_nl
+ 6;
7829 * The network-layer offsets are relative to the beginning
7830 * of the MAC-layer payload; that's past the 6-byte
7831 * PPPoE header and the 2-byte PPP header.
7834 off_nl_nosnap
= 6+2;
7840 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7852 bpf_error("'vpi' supported only on raw ATM");
7853 if (off_vpi
== (u_int
)-1)
7855 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7861 bpf_error("'vci' supported only on raw ATM");
7862 if (off_vci
== (u_int
)-1)
7864 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7869 if (off_proto
== (u_int
)-1)
7870 abort(); /* XXX - this isn't on FreeBSD */
7871 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7876 if (off_payload
== (u_int
)-1)
7878 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7879 0xffffffff, jtype
, reverse
, jvalue
);
7884 bpf_error("'callref' supported only on raw ATM");
7885 if (off_proto
== (u_int
)-1)
7887 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7888 jtype
, reverse
, jvalue
);
7898 gen_atmtype_abbrev(type
)
7901 struct block
*b0
, *b1
;
7906 /* Get all packets in Meta signalling Circuit */
7908 bpf_error("'metac' supported only on raw ATM");
7909 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7910 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7915 /* Get all packets in Broadcast Circuit*/
7917 bpf_error("'bcc' supported only on raw ATM");
7918 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7919 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7924 /* Get all cells in Segment OAM F4 circuit*/
7926 bpf_error("'oam4sc' supported only on raw ATM");
7927 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7928 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7933 /* Get all cells in End-to-End OAM F4 Circuit*/
7935 bpf_error("'oam4ec' supported only on raw ATM");
7936 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7937 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7942 /* Get all packets in connection Signalling Circuit */
7944 bpf_error("'sc' supported only on raw ATM");
7945 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7946 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7951 /* Get all packets in ILMI Circuit */
7953 bpf_error("'ilmic' supported only on raw ATM");
7954 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7955 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7960 /* Get all LANE packets */
7962 bpf_error("'lane' supported only on raw ATM");
7963 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7966 * Arrange that all subsequent tests assume LANE
7967 * rather than LLC-encapsulated packets, and set
7968 * the offsets appropriately for LANE-encapsulated
7971 * "off_mac" is the offset of the Ethernet header,
7972 * which is 2 bytes past the ATM pseudo-header
7973 * (skipping the pseudo-header and 2-byte LE Client
7974 * field). The other offsets are Ethernet offsets
7975 * relative to "off_mac".
7978 off_mac
= off_payload
+ 2; /* MAC header */
7979 off_linktype
= off_mac
+ 12;
7980 off_macpl
= off_mac
+ 14; /* Ethernet */
7981 off_nl
= 0; /* Ethernet II */
7982 off_nl_nosnap
= 3; /* 802.3+802.2 */
7986 /* Get all LLC-encapsulated packets */
7988 bpf_error("'llc' supported only on raw ATM");
7989 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8000 * Filtering for MTP2 messages based on li value
8001 * FISU, length is null
8002 * LSSU, length is 1 or 2
8003 * MSU, length is 3 or more
8006 gen_mtp2type_abbrev(type
)
8009 struct block
*b0
, *b1
;
8014 if ( (linktype
!= DLT_MTP2
) &&
8015 (linktype
!= DLT_ERF
) &&
8016 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8017 bpf_error("'fisu' supported only on MTP2");
8018 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8019 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8023 if ( (linktype
!= DLT_MTP2
) &&
8024 (linktype
!= DLT_ERF
) &&
8025 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8026 bpf_error("'lssu' supported only on MTP2");
8027 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8028 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8033 if ( (linktype
!= DLT_MTP2
) &&
8034 (linktype
!= DLT_ERF
) &&
8035 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8036 bpf_error("'msu' supported only on MTP2");
8037 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8047 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8054 bpf_u_int32 val1
, val2
, val3
;
8056 switch (mtp3field
) {
8059 if (off_sio
== (u_int
)-1)
8060 bpf_error("'sio' supported only on SS7");
8061 /* sio coded on 1 byte so max value 255 */
8063 bpf_error("sio value %u too big; max value = 255",
8065 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
8066 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8070 if (off_opc
== (u_int
)-1)
8071 bpf_error("'opc' supported only on SS7");
8072 /* opc coded on 14 bits so max value 16383 */
8074 bpf_error("opc value %u too big; max value = 16383",
8076 /* the following instructions are made to convert jvalue
8077 * to the form used to write opc in an ss7 message*/
8078 val1
= jvalue
& 0x00003c00;
8080 val2
= jvalue
& 0x000003fc;
8082 val3
= jvalue
& 0x00000003;
8084 jvalue
= val1
+ val2
+ val3
;
8085 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
8086 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8090 if (off_dpc
== (u_int
)-1)
8091 bpf_error("'dpc' supported only on SS7");
8092 /* dpc coded on 14 bits so max value 16383 */
8094 bpf_error("dpc value %u too big; max value = 16383",
8096 /* the following instructions are made to convert jvalue
8097 * to the forme used to write dpc in an ss7 message*/
8098 val1
= jvalue
& 0x000000ff;
8100 val2
= jvalue
& 0x00003f00;
8102 jvalue
= val1
+ val2
;
8103 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
8104 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8108 if (off_sls
== (u_int
)-1)
8109 bpf_error("'sls' supported only on SS7");
8110 /* sls coded on 4 bits so max value 15 */
8112 bpf_error("sls value %u too big; max value = 15",
8114 /* the following instruction is made to convert jvalue
8115 * to the forme used to write sls in an ss7 message*/
8116 jvalue
= jvalue
<< 4;
8117 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
8118 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8127 static struct block
*
8128 gen_msg_abbrev(type
)
8134 * Q.2931 signalling protocol messages for handling virtual circuits
8135 * establishment and teardown
8140 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8144 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8148 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8152 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8156 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8159 case A_RELEASE_DONE
:
8160 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8170 gen_atmmulti_abbrev(type
)
8173 struct block
*b0
, *b1
;
8179 bpf_error("'oam' supported only on raw ATM");
8180 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8185 bpf_error("'oamf4' supported only on raw ATM");
8187 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8188 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8190 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8196 * Get Q.2931 signalling messages for switched
8197 * virtual connection
8200 bpf_error("'connectmsg' supported only on raw ATM");
8201 b0
= gen_msg_abbrev(A_SETUP
);
8202 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8204 b0
= gen_msg_abbrev(A_CONNECT
);
8206 b0
= gen_msg_abbrev(A_CONNECTACK
);
8208 b0
= gen_msg_abbrev(A_RELEASE
);
8210 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8212 b0
= gen_atmtype_abbrev(A_SC
);
8218 bpf_error("'metaconnect' supported only on raw ATM");
8219 b0
= gen_msg_abbrev(A_SETUP
);
8220 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8222 b0
= gen_msg_abbrev(A_CONNECT
);
8224 b0
= gen_msg_abbrev(A_RELEASE
);
8226 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8228 b0
= gen_atmtype_abbrev(A_METAC
);