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.7 2007-11-05 23:03:35 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
76 #ifdef HAVE_NET_PFVAR_H
77 #include <sys/socket.h>
79 #include <net/pfvar.h>
80 #include <net/if_pflog.h>
83 #define offsetof(s, e) ((size_t)&((s *)0)->e)
87 #include <netdb.h> /* for "struct addrinfo" */
90 #include <pcap/namedb.h>
95 #define IPPROTO_SCTP 132
98 #ifdef HAVE_OS_PROTO_H
102 #define JMP(c) ((c)|BPF_JMP|BPF_K)
105 static jmp_buf top_ctx
;
106 static pcap_t
*bpf_pcap
;
108 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
110 static u_int orig_linktype
= (u_int
)-1, orig_nl
= (u_int
)-1, label_stack_depth
= (u_int
)-1;
112 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
117 static int pcap_fddipad
;
122 bpf_error(const char *fmt
, ...)
127 if (bpf_pcap
!= NULL
)
128 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
135 static void init_linktype(pcap_t
*);
137 static void init_regs(void);
138 static int alloc_reg(void);
139 static void free_reg(int);
141 static struct block
*root
;
144 * Value passed to gen_load_a() to indicate what the offset argument
148 OR_PACKET
, /* relative to the beginning of the packet */
149 OR_LINK
, /* relative to the beginning of the link-layer header */
150 OR_MACPL
, /* relative to the end of the MAC-layer header */
151 OR_NET
, /* relative to the network-layer header */
152 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
153 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
154 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
158 * We divy out chunks of memory rather than call malloc each time so
159 * we don't have to worry about leaking memory. It's probably
160 * not a big deal if all this memory was wasted but if this ever
161 * goes into a library that would probably not be a good idea.
163 * XXX - this *is* in a library....
166 #define CHUNK0SIZE 1024
172 static struct chunk chunks
[NCHUNKS
];
173 static int cur_chunk
;
175 static void *newchunk(u_int
);
176 static void freechunks(void);
177 static inline struct block
*new_block(int);
178 static inline struct slist
*new_stmt(int);
179 static struct block
*gen_retblk(int);
180 static inline void syntax(void);
182 static void backpatch(struct block
*, struct block
*);
183 static void merge(struct block
*, struct block
*);
184 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
185 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
186 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
187 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
188 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
189 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
191 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
192 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
193 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
194 static struct slist
*gen_load_llrel(u_int
, u_int
);
195 static struct slist
*gen_load_macplrel(u_int
, u_int
);
196 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
197 static struct slist
*gen_loadx_iphdrlen(void);
198 static struct block
*gen_uncond(int);
199 static inline struct block
*gen_true(void);
200 static inline struct block
*gen_false(void);
201 static struct block
*gen_ether_linktype(int);
202 static struct block
*gen_linux_sll_linktype(int);
203 static struct slist
*gen_load_radiotap_llprefixlen(void);
204 static struct slist
*gen_load_ppi_llprefixlen(void);
205 static void insert_load_llprefixlen(struct block
*);
206 static struct slist
*gen_llprefixlen(void);
207 static struct slist
*gen_off_macpl(void);
208 static int ethertype_to_ppptype(int);
209 static struct block
*gen_linktype(int);
210 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
211 static struct block
*gen_llc_linktype(int);
212 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
214 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
216 static struct block
*gen_ahostop(const u_char
*, int);
217 static struct block
*gen_ehostop(const u_char
*, int);
218 static struct block
*gen_fhostop(const u_char
*, int);
219 static struct block
*gen_thostop(const u_char
*, int);
220 static struct block
*gen_wlanhostop(const u_char
*, int);
221 static struct block
*gen_ipfchostop(const u_char
*, int);
222 static struct block
*gen_dnhostop(bpf_u_int32
, int);
223 static struct block
*gen_mpls_linktype(int);
224 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
226 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
229 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
231 static struct block
*gen_ipfrag(void);
232 static struct block
*gen_portatom(int, bpf_int32
);
233 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
235 static struct block
*gen_portatom6(int, bpf_int32
);
236 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
238 struct block
*gen_portop(int, int, int);
239 static struct block
*gen_port(int, int, int);
240 struct block
*gen_portrangeop(int, int, int, int);
241 static struct block
*gen_portrange(int, int, int, int);
243 struct block
*gen_portop6(int, int, int);
244 static struct block
*gen_port6(int, int, int);
245 struct block
*gen_portrangeop6(int, int, int, int);
246 static struct block
*gen_portrange6(int, int, int, int);
248 static int lookup_proto(const char *, int);
249 static struct block
*gen_protochain(int, int, int);
250 static struct block
*gen_proto(int, int, int);
251 static struct slist
*xfer_to_x(struct arth
*);
252 static struct slist
*xfer_to_a(struct arth
*);
253 static struct block
*gen_mac_multicast(int);
254 static struct block
*gen_len(int, int);
255 static struct block
*gen_check_802_11_data_frame(void);
257 static struct block
*gen_ppi_dlt_check(void);
258 static struct block
*gen_msg_abbrev(int type
);
269 /* XXX Round up to nearest long. */
270 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
272 /* XXX Round up to structure boundary. */
276 cp
= &chunks
[cur_chunk
];
277 if (n
> cp
->n_left
) {
278 ++cp
, k
= ++cur_chunk
;
280 bpf_error("out of memory");
281 size
= CHUNK0SIZE
<< k
;
282 cp
->m
= (void *)malloc(size
);
284 bpf_error("out of memory");
285 memset((char *)cp
->m
, 0, size
);
288 bpf_error("out of memory");
291 return (void *)((char *)cp
->m
+ cp
->n_left
);
300 for (i
= 0; i
< NCHUNKS
; ++i
)
301 if (chunks
[i
].m
!= NULL
) {
308 * A strdup whose allocations are freed after code generation is over.
312 register const char *s
;
314 int n
= strlen(s
) + 1;
315 char *cp
= newchunk(n
);
321 static inline struct block
*
327 p
= (struct block
*)newchunk(sizeof(*p
));
334 static inline struct slist
*
340 p
= (struct slist
*)newchunk(sizeof(*p
));
346 static struct block
*
350 struct block
*b
= new_block(BPF_RET
|BPF_K
);
359 bpf_error("syntax error in filter expression");
362 static bpf_u_int32 netmask
;
367 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
368 const char *buf
, int optimize
, bpf_u_int32 mask
)
371 const char * volatile xbuf
= buf
;
379 if (setjmp(top_ctx
)) {
387 snaplen
= pcap_snapshot(p
);
389 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
390 "snaplen of 0 rejects all packets");
394 lex_init(xbuf
? xbuf
: "");
402 root
= gen_retblk(snaplen
);
404 if (optimize
&& !no_optimize
) {
407 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
408 bpf_error("expression rejects all packets");
410 program
->bf_insns
= icode_to_fcode(root
, &len
);
411 program
->bf_len
= len
;
419 * entry point for using the compiler with no pcap open
420 * pass in all the stuff that is needed explicitly instead.
423 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
424 struct bpf_program
*program
,
425 const char *buf
, int optimize
, bpf_u_int32 mask
)
430 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
433 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
439 * Clean up a "struct bpf_program" by freeing all the memory allocated
443 pcap_freecode(struct bpf_program
*program
)
446 if (program
->bf_insns
!= NULL
) {
447 free((char *)program
->bf_insns
);
448 program
->bf_insns
= NULL
;
453 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
454 * which of the jt and jf fields has been resolved and which is a pointer
455 * back to another unresolved block (or nil). At least one of the fields
456 * in each block is already resolved.
459 backpatch(list
, target
)
460 struct block
*list
, *target
;
477 * Merge the lists in b0 and b1, using the 'sense' field to indicate
478 * which of jt and jf is the link.
482 struct block
*b0
, *b1
;
484 register struct block
**p
= &b0
;
486 /* Find end of list. */
488 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
490 /* Concatenate the lists. */
498 struct block
*ppi_dlt_check
;
500 ppi_dlt_check
= gen_ppi_dlt_check();
502 if (ppi_dlt_check
!= NULL
)
504 gen_and(ppi_dlt_check
, p
);
507 backpatch(p
, gen_retblk(snaplen
));
508 p
->sense
= !p
->sense
;
509 backpatch(p
, gen_retblk(0));
513 * Insert before the statements of the first (root) block any
514 * statements needed to load the lengths of any variable-length
515 * headers into registers.
517 * XXX - a fancier strategy would be to insert those before the
518 * statements of all blocks that use those lengths and that
519 * have no predecessors that use them, so that we only compute
520 * the lengths if we need them. There might be even better
521 * approaches than that. However, as we're currently only
522 * handling variable-length radiotap headers, and as all
523 * filtering expressions other than raw link[M:N] tests
524 * require the length of that header, doing more for that
525 * header length isn't really worth the effort.
527 insert_load_llprefixlen(root
);
532 struct block
*b0
, *b1
;
534 backpatch(b0
, b1
->head
);
535 b0
->sense
= !b0
->sense
;
536 b1
->sense
= !b1
->sense
;
538 b1
->sense
= !b1
->sense
;
544 struct block
*b0
, *b1
;
546 b0
->sense
= !b0
->sense
;
547 backpatch(b0
, b1
->head
);
548 b0
->sense
= !b0
->sense
;
557 b
->sense
= !b
->sense
;
560 static struct block
*
561 gen_cmp(offrel
, offset
, size
, v
)
562 enum e_offrel offrel
;
566 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
569 static struct block
*
570 gen_cmp_gt(offrel
, offset
, size
, v
)
571 enum e_offrel offrel
;
575 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
578 static struct block
*
579 gen_cmp_ge(offrel
, offset
, size
, v
)
580 enum e_offrel offrel
;
584 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
587 static struct block
*
588 gen_cmp_lt(offrel
, offset
, size
, v
)
589 enum e_offrel offrel
;
593 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
596 static struct block
*
597 gen_cmp_le(offrel
, offset
, size
, v
)
598 enum e_offrel offrel
;
602 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
605 static struct block
*
606 gen_mcmp(offrel
, offset
, size
, v
, mask
)
607 enum e_offrel offrel
;
612 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
615 static struct block
*
616 gen_bcmp(offrel
, offset
, size
, v
)
617 enum e_offrel offrel
;
618 register u_int offset
, size
;
619 register const u_char
*v
;
621 register struct block
*b
, *tmp
;
625 register const u_char
*p
= &v
[size
- 4];
626 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
627 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
629 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
636 register const u_char
*p
= &v
[size
- 2];
637 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
639 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
646 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
655 * AND the field of size "size" at offset "offset" relative to the header
656 * specified by "offrel" with "mask", and compare it with the value "v"
657 * with the test specified by "jtype"; if "reverse" is true, the test
658 * should test the opposite of "jtype".
660 static struct block
*
661 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
662 enum e_offrel offrel
;
664 bpf_u_int32 offset
, size
, mask
, jtype
;
667 struct slist
*s
, *s2
;
670 s
= gen_load_a(offrel
, offset
, size
);
672 if (mask
!= 0xffffffff) {
673 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
678 b
= new_block(JMP(jtype
));
681 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
687 * Various code constructs need to know the layout of the data link
688 * layer. These variables give the necessary offsets from the beginning
689 * of the packet data.
693 * This is the offset of the beginning of the link-layer header from
694 * the beginning of the raw packet data.
696 * It's usually 0, except for 802.11 with a fixed-length radio header.
697 * (For 802.11 with a variable-length radio header, we have to generate
698 * code to compute that offset; off_ll is 0 in that case.)
703 * If there's a variable-length header preceding the link-layer header,
704 * "reg_off_ll" is the register number for a register containing the
705 * length of that header, and therefore the offset of the link-layer
706 * header from the beginning of the raw packet data. Otherwise,
707 * "reg_off_ll" is -1.
709 static int reg_off_ll
;
712 * This is the offset of the beginning of the MAC-layer header from
713 * the beginning of the link-layer header.
714 * It's usually 0, except for ATM LANE, where it's the offset, relative
715 * to the beginning of the raw packet data, of the Ethernet header.
717 static u_int off_mac
;
720 * This is the offset of the beginning of the MAC-layer payload,
721 * from the beginning of the raw packet data.
723 * I.e., it's the sum of the length of the link-layer header (without,
724 * for example, any 802.2 LLC header, so it's the MAC-layer
725 * portion of that header), plus any prefix preceding the
728 static u_int off_macpl
;
731 * This is 1 if the offset of the beginning of the MAC-layer payload
732 * from the beginning of the link-layer header is variable-length.
734 static int off_macpl_is_variable
;
737 * If the link layer has variable_length headers, "reg_off_macpl"
738 * is the register number for a register containing the length of the
739 * link-layer header plus the length of any variable-length header
740 * preceding the link-layer header. Otherwise, "reg_off_macpl"
743 static int reg_off_macpl
;
746 * "off_linktype" is the offset to information in the link-layer header
747 * giving the packet type. This offset is relative to the beginning
748 * of the link-layer header (i.e., it doesn't include off_ll).
750 * For Ethernet, it's the offset of the Ethernet type field.
752 * For link-layer types that always use 802.2 headers, it's the
753 * offset of the LLC header.
755 * For PPP, it's the offset of the PPP type field.
757 * For Cisco HDLC, it's the offset of the CHDLC type field.
759 * For BSD loopback, it's the offset of the AF_ value.
761 * For Linux cooked sockets, it's the offset of the type field.
763 * It's set to -1 for no encapsulation, in which case, IP is assumed.
765 static u_int off_linktype
;
768 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
769 * checks to check the PPP header, assumed to follow a LAN-style link-
770 * layer header and a PPPoE session header.
772 static int is_pppoes
= 0;
775 * TRUE if the link layer includes an ATM pseudo-header.
777 static int is_atm
= 0;
780 * TRUE if "lane" appeared in the filter; it causes us to generate
781 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
783 static int is_lane
= 0;
786 * These are offsets for the ATM pseudo-header.
788 static u_int off_vpi
;
789 static u_int off_vci
;
790 static u_int off_proto
;
793 * These are offsets for the MTP2 fields.
798 * These are offsets for the MTP3 fields.
800 static u_int off_sio
;
801 static u_int off_opc
;
802 static u_int off_dpc
;
803 static u_int off_sls
;
806 * This is the offset of the first byte after the ATM pseudo_header,
807 * or -1 if there is no ATM pseudo-header.
809 static u_int off_payload
;
812 * These are offsets to the beginning of the network-layer header.
813 * They are relative to the beginning of the MAC-layer payload (i.e.,
814 * they don't include off_ll or off_macpl).
816 * If the link layer never uses 802.2 LLC:
818 * "off_nl" and "off_nl_nosnap" are the same.
820 * If the link layer always uses 802.2 LLC:
822 * "off_nl" is the offset if there's a SNAP header following
825 * "off_nl_nosnap" is the offset if there's no SNAP header.
827 * If the link layer is Ethernet:
829 * "off_nl" is the offset if the packet is an Ethernet II packet
830 * (we assume no 802.3+802.2+SNAP);
832 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
833 * with an 802.2 header following it.
836 static u_int off_nl_nosnap
;
844 linktype
= pcap_datalink(p
);
846 pcap_fddipad
= p
->fddipad
;
850 * Assume it's not raw ATM with a pseudo-header, for now.
861 * And that we're not doing PPPoE.
866 * And assume we're not doing SS7.
875 * Also assume it's not 802.11.
879 off_macpl_is_variable
= 0;
883 label_stack_depth
= 0;
893 off_nl
= 0; /* XXX in reality, variable! */
894 off_nl_nosnap
= 0; /* no 802.2 LLC */
897 case DLT_ARCNET_LINUX
:
900 off_nl
= 0; /* XXX in reality, variable! */
901 off_nl_nosnap
= 0; /* no 802.2 LLC */
906 off_macpl
= 14; /* Ethernet header length */
907 off_nl
= 0; /* Ethernet II */
908 off_nl_nosnap
= 3; /* 802.3+802.2 */
913 * SLIP doesn't have a link level type. The 16 byte
914 * header is hacked into our SLIP driver.
919 off_nl_nosnap
= 0; /* no 802.2 LLC */
923 /* XXX this may be the same as the DLT_PPP_BSDOS case */
928 off_nl_nosnap
= 0; /* no 802.2 LLC */
936 off_nl_nosnap
= 0; /* no 802.2 LLC */
943 off_nl_nosnap
= 0; /* no 802.2 LLC */
948 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
949 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
953 off_nl_nosnap
= 0; /* no 802.2 LLC */
958 * This does no include the Ethernet header, and
959 * only covers session state.
964 off_nl_nosnap
= 0; /* no 802.2 LLC */
971 off_nl_nosnap
= 0; /* no 802.2 LLC */
976 * FDDI doesn't really have a link-level type field.
977 * We set "off_linktype" to the offset of the LLC header.
979 * To check for Ethernet types, we assume that SSAP = SNAP
980 * is being used and pick out the encapsulated Ethernet type.
981 * XXX - should we generate code to check for SNAP?
985 off_linktype
+= pcap_fddipad
;
987 off_macpl
= 13; /* FDDI MAC header length */
989 off_macpl
+= pcap_fddipad
;
991 off_nl
= 8; /* 802.2+SNAP */
992 off_nl_nosnap
= 3; /* 802.2 */
997 * Token Ring doesn't really have a link-level type field.
998 * We set "off_linktype" to the offset of the LLC header.
1000 * To check for Ethernet types, we assume that SSAP = SNAP
1001 * is being used and pick out the encapsulated Ethernet type.
1002 * XXX - should we generate code to check for SNAP?
1004 * XXX - the header is actually variable-length.
1005 * Some various Linux patched versions gave 38
1006 * as "off_linktype" and 40 as "off_nl"; however,
1007 * if a token ring packet has *no* routing
1008 * information, i.e. is not source-routed, the correct
1009 * values are 20 and 22, as they are in the vanilla code.
1011 * A packet is source-routed iff the uppermost bit
1012 * of the first byte of the source address, at an
1013 * offset of 8, has the uppermost bit set. If the
1014 * packet is source-routed, the total number of bytes
1015 * of routing information is 2 plus bits 0x1F00 of
1016 * the 16-bit value at an offset of 14 (shifted right
1017 * 8 - figure out which byte that is).
1020 off_macpl
= 14; /* Token Ring MAC header length */
1021 off_nl
= 8; /* 802.2+SNAP */
1022 off_nl_nosnap
= 3; /* 802.2 */
1025 case DLT_IEEE802_11
:
1027 * 802.11 doesn't really have a link-level type field.
1028 * We set "off_linktype" to the offset of the LLC header.
1030 * To check for Ethernet types, we assume that SSAP = SNAP
1031 * is being used and pick out the encapsulated Ethernet type.
1032 * XXX - should we generate code to check for SNAP?
1034 * XXX - the header is actually variable-length. We
1035 * assume a 24-byte link-layer header, as appears in
1036 * data frames in networks with no bridges. If the
1037 * fromds and tods 802.11 header bits are both set,
1038 * it's actually supposed to be 30 bytes.
1041 off_macpl
= 0; /* link-layer header is variable-length */
1042 off_macpl_is_variable
= 1;
1043 off_nl
= 8; /* 802.2+SNAP */
1044 off_nl_nosnap
= 3; /* 802.2 */
1047 case DLT_PRISM_HEADER
:
1049 * Same as 802.11, but with an additional header before
1050 * the 802.11 header, containing a bunch of additional
1051 * information including radio-level information.
1053 * The header is 144 bytes long.
1055 * XXX - same variable-length header problem; at least
1056 * the Prism header is fixed-length.
1060 off_macpl
= 0; /* link-layer header is variable-length */
1061 off_macpl_is_variable
= 1;
1062 off_nl
= 8; /* 802.2+SNAP */
1063 off_nl_nosnap
= 3; /* 802.2 */
1066 case DLT_IEEE802_11_RADIO_AVS
:
1068 * Same as 802.11, but with an additional header before
1069 * the 802.11 header, containing a bunch of additional
1070 * information including radio-level information.
1072 * The header is 64 bytes long, at least in its
1073 * current incarnation.
1075 * XXX - same variable-length header problem, only
1076 * more so; this header is also variable-length,
1077 * with the length being the 32-bit big-endian
1078 * number at an offset of 4 from the beginning
1079 * of the radio header. We should handle that the
1080 * same way we handle the length at the beginning
1081 * of the radiotap header.
1083 * XXX - in Linux, do any drivers that supply an AVS
1084 * header supply a link-layer type other than
1085 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1086 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1087 * any drivers that supply an AVS header but supply
1088 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1089 * have to check the header in the generated code to
1090 * determine whether it's Prism or AVS.
1094 off_macpl
= 0; /* link-layer header is variable-length */
1095 off_macpl_is_variable
= 1;
1096 off_nl
= 8; /* 802.2+SNAP */
1097 off_nl_nosnap
= 3; /* 802.2 */
1101 * At the moment we treat PPI as normal Radiotap encoded
1102 * packets. The difference is in the function that generates
1103 * the code at the beginning to compute the header length.
1104 * Since this code generator of PPI supports bare 802.11
1105 * encapsulation only (i.e. the encapsulated DLT should be
1106 * DLT_IEEE802_11) we generate code to check for this too.
1109 case DLT_IEEE802_11_RADIO
:
1111 * Same as 802.11, but with an additional header before
1112 * the 802.11 header, containing a bunch of additional
1113 * information including radio-level information.
1115 * The radiotap header is variable length, and we
1116 * generate code to compute its length and store it
1117 * in a register. These offsets are relative to the
1118 * beginning of the 802.11 header.
1121 off_macpl
= 0; /* link-layer header is variable-length */
1122 off_macpl_is_variable
= 1;
1123 off_nl
= 8; /* 802.2+SNAP */
1124 off_nl_nosnap
= 3; /* 802.2 */
1127 case DLT_ATM_RFC1483
:
1128 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1130 * assume routed, non-ISO PDUs
1131 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1133 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1134 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1135 * latter would presumably be treated the way PPPoE
1136 * should be, so you can do "pppoe and udp port 2049"
1137 * or "pppoa and tcp port 80" and have it check for
1138 * PPPo{A,E} and a PPP protocol of IP and....
1141 off_macpl
= 0; /* packet begins with LLC header */
1142 off_nl
= 8; /* 802.2+SNAP */
1143 off_nl_nosnap
= 3; /* 802.2 */
1148 * Full Frontal ATM; you get AALn PDUs with an ATM
1152 off_vpi
= SUNATM_VPI_POS
;
1153 off_vci
= SUNATM_VCI_POS
;
1154 off_proto
= PROTO_POS
;
1155 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1156 off_payload
= SUNATM_PKT_BEGIN_POS
;
1157 off_linktype
= off_payload
;
1158 off_macpl
= off_payload
; /* if LLC-encapsulated */
1159 off_nl
= 8; /* 802.2+SNAP */
1160 off_nl_nosnap
= 3; /* 802.2 */
1167 off_nl_nosnap
= 0; /* no 802.2 LLC */
1170 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1174 off_nl_nosnap
= 0; /* no 802.2 LLC */
1179 * LocalTalk does have a 1-byte type field in the LLAP header,
1180 * but really it just indicates whether there is a "short" or
1181 * "long" DDP packet following.
1186 off_nl_nosnap
= 0; /* no 802.2 LLC */
1189 case DLT_IP_OVER_FC
:
1191 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1192 * link-level type field. We set "off_linktype" to the
1193 * offset of the LLC header.
1195 * To check for Ethernet types, we assume that SSAP = SNAP
1196 * is being used and pick out the encapsulated Ethernet type.
1197 * XXX - should we generate code to check for SNAP? RFC
1198 * 2625 says SNAP should be used.
1202 off_nl
= 8; /* 802.2+SNAP */
1203 off_nl_nosnap
= 3; /* 802.2 */
1208 * XXX - we should set this to handle SNAP-encapsulated
1209 * frames (NLPID of 0x80).
1214 off_nl_nosnap
= 0; /* no 802.2 LLC */
1218 * the only BPF-interesting FRF.16 frames are non-control frames;
1219 * Frame Relay has a variable length link-layer
1220 * so lets start with offset 4 for now and increments later on (FIXME);
1226 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1229 case DLT_APPLE_IP_OVER_IEEE1394
:
1233 off_nl_nosnap
= 0; /* no 802.2 LLC */
1236 case DLT_LINUX_IRDA
:
1238 * Currently, only raw "link[N:M]" filtering is supported.
1248 * Currently, only raw "link[N:M]" filtering is supported.
1256 case DLT_SYMANTEC_FIREWALL
:
1259 off_nl
= 0; /* Ethernet II */
1260 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1263 #ifdef HAVE_NET_PFVAR_H
1266 off_macpl
= PFLOG_HDRLEN
;
1268 off_nl_nosnap
= 0; /* no 802.2 LLC */
1272 case DLT_JUNIPER_MFR
:
1273 case DLT_JUNIPER_MLFR
:
1274 case DLT_JUNIPER_MLPPP
:
1275 case DLT_JUNIPER_PPP
:
1276 case DLT_JUNIPER_CHDLC
:
1277 case DLT_JUNIPER_FRELAY
:
1281 off_nl_nosnap
= -1; /* no 802.2 LLC */
1284 case DLT_JUNIPER_ATM1
:
1285 off_linktype
= 4; /* in reality variable between 4-8 */
1286 off_macpl
= 4; /* in reality variable between 4-8 */
1291 case DLT_JUNIPER_ATM2
:
1292 off_linktype
= 8; /* in reality variable between 8-12 */
1293 off_macpl
= 8; /* in reality variable between 8-12 */
1298 /* frames captured on a Juniper PPPoE service PIC
1299 * contain raw ethernet frames */
1300 case DLT_JUNIPER_PPPOE
:
1301 case DLT_JUNIPER_ETHER
:
1304 off_nl
= 18; /* Ethernet II */
1305 off_nl_nosnap
= 21; /* 802.3+802.2 */
1308 case DLT_JUNIPER_PPPOE_ATM
:
1312 off_nl_nosnap
= -1; /* no 802.2 LLC */
1315 case DLT_JUNIPER_GGSN
:
1319 off_nl_nosnap
= -1; /* no 802.2 LLC */
1322 case DLT_JUNIPER_ES
:
1324 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1325 off_nl
= -1; /* not really a network layer but raw IP addresses */
1326 off_nl_nosnap
= -1; /* no 802.2 LLC */
1329 case DLT_JUNIPER_MONITOR
:
1332 off_nl
= 0; /* raw IP/IP6 header */
1333 off_nl_nosnap
= -1; /* no 802.2 LLC */
1336 case DLT_JUNIPER_SERVICES
:
1338 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1339 off_nl
= -1; /* L3 proto location dep. on cookie type */
1340 off_nl_nosnap
= -1; /* no 802.2 LLC */
1343 case DLT_JUNIPER_VP
:
1350 case DLT_JUNIPER_ST
:
1357 case DLT_JUNIPER_ISM
:
1376 case DLT_MTP2_WITH_PHDR
:
1409 case DLT_LINUX_LAPD
:
1411 * Currently, only raw "link[N:M]" filtering is supported.
1421 * Currently, only raw "link[N:M]" filtering is supported.
1429 case DLT_BLUETOOTH_HCI_H4
:
1431 * Currently, only raw "link[N:M]" filtering is supported.
1441 * Currently, only raw "link[N:M]" filtering is supported.
1451 * Currently, only raw "link[N:M]" filtering is supported.
1459 case DLT_IEEE802_15_4_LINUX
:
1461 * Currently, only raw "link[N:M]" filtering is supported.
1469 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1471 * Currently, only raw "link[N:M]" filtering is supported.
1479 case DLT_IEEE802_15_4
:
1481 * Currently, only raw "link[N:M]" filtering is supported.
1491 * Currently, only raw "link[N:M]" filtering is supported.
1501 * Currently, only raw "link[N:M]" filtering is supported.
1511 * Currently, only raw "link[N:M]" filtering is supported.
1519 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1521 * Currently, only raw "link[N:M]" filtering is supported.
1531 * Currently, only raw "link[N:M]" filtering is supported.
1533 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1535 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1536 off_nl_nosnap
= -1; /* no 802.2 LLC */
1537 off_mac
= 1; /* step over the kiss length byte */
1540 bpf_error("unknown data link type %d", linktype
);
1545 * Load a value relative to the beginning of the link-layer header.
1546 * The link-layer header doesn't necessarily begin at the beginning
1547 * of the packet data; there might be a variable-length prefix containing
1548 * radio information.
1550 static struct slist
*
1551 gen_load_llrel(offset
, size
)
1554 struct slist
*s
, *s2
;
1556 s
= gen_llprefixlen();
1559 * If "s" is non-null, it has code to arrange that the X register
1560 * contains the length of the prefix preceding the link-layer
1563 * Otherwise, the length of the prefix preceding the link-layer
1564 * header is "off_ll".
1568 * There's a variable-length prefix preceding the
1569 * link-layer header. "s" points to a list of statements
1570 * that put the length of that prefix into the X register.
1571 * do an indirect load, to use the X register as an offset.
1573 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1578 * There is no variable-length header preceding the
1579 * link-layer header; add in off_ll, which, if there's
1580 * a fixed-length header preceding the link-layer header,
1581 * is the length of that header.
1583 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1584 s
->s
.k
= offset
+ off_ll
;
1590 * Load a value relative to the beginning of the MAC-layer payload.
1592 static struct slist
*
1593 gen_load_macplrel(offset
, size
)
1596 struct slist
*s
, *s2
;
1598 s
= gen_off_macpl();
1601 * If s is non-null, the offset of the MAC-layer payload is
1602 * variable, and s points to a list of instructions that
1603 * arrange that the X register contains that offset.
1605 * Otherwise, the offset of the MAC-layer payload is constant,
1606 * and is in off_macpl.
1610 * The offset of the MAC-layer payload is in the X
1611 * register. Do an indirect load, to use the X register
1614 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1619 * The offset of the MAC-layer payload is constant,
1620 * and is in off_macpl; load the value at that offset
1621 * plus the specified offset.
1623 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1624 s
->s
.k
= off_macpl
+ offset
;
1630 * Load a value relative to the beginning of the specified header.
1632 static struct slist
*
1633 gen_load_a(offrel
, offset
, size
)
1634 enum e_offrel offrel
;
1637 struct slist
*s
, *s2
;
1642 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1647 s
= gen_load_llrel(offset
, size
);
1651 s
= gen_load_macplrel(offset
, size
);
1655 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1659 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1664 * Load the X register with the length of the IPv4 header
1665 * (plus the offset of the link-layer header, if it's
1666 * preceded by a variable-length header such as a radio
1667 * header), in bytes.
1669 s
= gen_loadx_iphdrlen();
1672 * Load the item at {offset of the MAC-layer payload} +
1673 * {offset, relative to the start of the MAC-layer
1674 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1675 * {specified offset}.
1677 * (If the offset of the MAC-layer payload is variable,
1678 * it's included in the value in the X register, and
1681 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1682 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1687 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1698 * Generate code to load into the X register the sum of the length of
1699 * the IPv4 header and any variable-length header preceding the link-layer
1702 static struct slist
*
1703 gen_loadx_iphdrlen()
1705 struct slist
*s
, *s2
;
1707 s
= gen_off_macpl();
1710 * There's a variable-length prefix preceding the
1711 * link-layer header, or the link-layer header is itself
1712 * variable-length. "s" points to a list of statements
1713 * that put the offset of the MAC-layer payload into
1716 * The 4*([k]&0xf) addressing mode can't be used, as we
1717 * don't have a constant offset, so we have to load the
1718 * value in question into the A register and add to it
1719 * the value from the X register.
1721 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1724 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1727 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1732 * The A register now contains the length of the
1733 * IP header. We need to add to it the offset of
1734 * the MAC-layer payload, which is still in the X
1735 * register, and move the result into the X register.
1737 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1738 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1741 * There is no variable-length header preceding the
1742 * link-layer header, and the link-layer header is
1743 * fixed-length; load the length of the IPv4 header,
1744 * which is at an offset of off_nl from the beginning
1745 * of the MAC-layer payload, and thus at an offset
1746 * of off_mac_pl + off_nl from the beginning of the
1749 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1750 s
->s
.k
= off_macpl
+ off_nl
;
1755 static struct block
*
1762 s
= new_stmt(BPF_LD
|BPF_IMM
);
1764 b
= new_block(JMP(BPF_JEQ
));
1770 static inline struct block
*
1773 return gen_uncond(1);
1776 static inline struct block
*
1779 return gen_uncond(0);
1783 * Byte-swap a 32-bit number.
1784 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1785 * big-endian platforms.)
1787 #define SWAPLONG(y) \
1788 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1791 * Generate code to match a particular packet type.
1793 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1794 * value, if <= ETHERMTU. We use that to determine whether to
1795 * match the type/length field or to check the type/length field for
1796 * a value <= ETHERMTU to see whether it's a type field and then do
1797 * the appropriate test.
1799 static struct block
*
1800 gen_ether_linktype(proto
)
1803 struct block
*b0
, *b1
;
1809 case LLCSAP_NETBEUI
:
1811 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1812 * so we check the DSAP and SSAP.
1814 * LLCSAP_IP checks for IP-over-802.2, rather
1815 * than IP-over-Ethernet or IP-over-SNAP.
1817 * XXX - should we check both the DSAP and the
1818 * SSAP, like this, or should we check just the
1819 * DSAP, as we do for other types <= ETHERMTU
1820 * (i.e., other SAP values)?
1822 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1824 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1825 ((proto
<< 8) | proto
));
1833 * Ethernet_II frames, which are Ethernet
1834 * frames with a frame type of ETHERTYPE_IPX;
1836 * Ethernet_802.3 frames, which are 802.3
1837 * frames (i.e., the type/length field is
1838 * a length field, <= ETHERMTU, rather than
1839 * a type field) with the first two bytes
1840 * after the Ethernet/802.3 header being
1843 * Ethernet_802.2 frames, which are 802.3
1844 * frames with an 802.2 LLC header and
1845 * with the IPX LSAP as the DSAP in the LLC
1848 * Ethernet_SNAP frames, which are 802.3
1849 * frames with an LLC header and a SNAP
1850 * header and with an OUI of 0x000000
1851 * (encapsulated Ethernet) and a protocol
1852 * ID of ETHERTYPE_IPX in the SNAP header.
1854 * XXX - should we generate the same code both
1855 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1859 * This generates code to check both for the
1860 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1862 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1863 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1867 * Now we add code to check for SNAP frames with
1868 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1870 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1874 * Now we generate code to check for 802.3
1875 * frames in general.
1877 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1881 * Now add the check for 802.3 frames before the
1882 * check for Ethernet_802.2 and Ethernet_802.3,
1883 * as those checks should only be done on 802.3
1884 * frames, not on Ethernet frames.
1889 * Now add the check for Ethernet_II frames, and
1890 * do that before checking for the other frame
1893 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1894 (bpf_int32
)ETHERTYPE_IPX
);
1898 case ETHERTYPE_ATALK
:
1899 case ETHERTYPE_AARP
:
1901 * EtherTalk (AppleTalk protocols on Ethernet link
1902 * layer) may use 802.2 encapsulation.
1906 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1907 * we check for an Ethernet type field less than
1908 * 1500, which means it's an 802.3 length field.
1910 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1914 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1915 * SNAP packets with an organization code of
1916 * 0x080007 (Apple, for Appletalk) and a protocol
1917 * type of ETHERTYPE_ATALK (Appletalk).
1919 * 802.2-encapsulated ETHERTYPE_AARP packets are
1920 * SNAP packets with an organization code of
1921 * 0x000000 (encapsulated Ethernet) and a protocol
1922 * type of ETHERTYPE_AARP (Appletalk ARP).
1924 if (proto
== ETHERTYPE_ATALK
)
1925 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1926 else /* proto == ETHERTYPE_AARP */
1927 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1931 * Check for Ethernet encapsulation (Ethertalk
1932 * phase 1?); we just check for the Ethernet
1935 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1941 if (proto
<= ETHERMTU
) {
1943 * This is an LLC SAP value, so the frames
1944 * that match would be 802.2 frames.
1945 * Check that the frame is an 802.2 frame
1946 * (i.e., that the length/type field is
1947 * a length field, <= ETHERMTU) and
1948 * then check the DSAP.
1950 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1952 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1958 * This is an Ethernet type, so compare
1959 * the length/type field with it (if
1960 * the frame is an 802.2 frame, the length
1961 * field will be <= ETHERMTU, and, as
1962 * "proto" is > ETHERMTU, this test
1963 * will fail and the frame won't match,
1964 * which is what we want).
1966 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1973 * Generate code to match a particular packet type.
1975 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1976 * value, if <= ETHERMTU. We use that to determine whether to
1977 * match the type field or to check the type field for the special
1978 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1980 static struct block
*
1981 gen_linux_sll_linktype(proto
)
1984 struct block
*b0
, *b1
;
1990 case LLCSAP_NETBEUI
:
1992 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1993 * so we check the DSAP and SSAP.
1995 * LLCSAP_IP checks for IP-over-802.2, rather
1996 * than IP-over-Ethernet or IP-over-SNAP.
1998 * XXX - should we check both the DSAP and the
1999 * SSAP, like this, or should we check just the
2000 * DSAP, as we do for other types <= ETHERMTU
2001 * (i.e., other SAP values)?
2003 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2004 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2005 ((proto
<< 8) | proto
));
2011 * Ethernet_II frames, which are Ethernet
2012 * frames with a frame type of ETHERTYPE_IPX;
2014 * Ethernet_802.3 frames, which have a frame
2015 * type of LINUX_SLL_P_802_3;
2017 * Ethernet_802.2 frames, which are 802.3
2018 * frames with an 802.2 LLC header (i.e, have
2019 * a frame type of LINUX_SLL_P_802_2) and
2020 * with the IPX LSAP as the DSAP in the LLC
2023 * Ethernet_SNAP frames, which are 802.3
2024 * frames with an LLC header and a SNAP
2025 * header and with an OUI of 0x000000
2026 * (encapsulated Ethernet) and a protocol
2027 * ID of ETHERTYPE_IPX in the SNAP header.
2029 * First, do the checks on LINUX_SLL_P_802_2
2030 * frames; generate the check for either
2031 * Ethernet_802.2 or Ethernet_SNAP frames, and
2032 * then put a check for LINUX_SLL_P_802_2 frames
2035 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2036 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2038 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2042 * Now check for 802.3 frames and OR that with
2043 * the previous test.
2045 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2049 * Now add the check for Ethernet_II frames, and
2050 * do that before checking for the other frame
2053 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2054 (bpf_int32
)ETHERTYPE_IPX
);
2058 case ETHERTYPE_ATALK
:
2059 case ETHERTYPE_AARP
:
2061 * EtherTalk (AppleTalk protocols on Ethernet link
2062 * layer) may use 802.2 encapsulation.
2066 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2067 * we check for the 802.2 protocol type in the
2068 * "Ethernet type" field.
2070 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2073 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2074 * SNAP packets with an organization code of
2075 * 0x080007 (Apple, for Appletalk) and a protocol
2076 * type of ETHERTYPE_ATALK (Appletalk).
2078 * 802.2-encapsulated ETHERTYPE_AARP packets are
2079 * SNAP packets with an organization code of
2080 * 0x000000 (encapsulated Ethernet) and a protocol
2081 * type of ETHERTYPE_AARP (Appletalk ARP).
2083 if (proto
== ETHERTYPE_ATALK
)
2084 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2085 else /* proto == ETHERTYPE_AARP */
2086 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2090 * Check for Ethernet encapsulation (Ethertalk
2091 * phase 1?); we just check for the Ethernet
2094 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2100 if (proto
<= ETHERMTU
) {
2102 * This is an LLC SAP value, so the frames
2103 * that match would be 802.2 frames.
2104 * Check for the 802.2 protocol type
2105 * in the "Ethernet type" field, and
2106 * then check the DSAP.
2108 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2110 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2116 * This is an Ethernet type, so compare
2117 * the length/type field with it (if
2118 * the frame is an 802.2 frame, the length
2119 * field will be <= ETHERMTU, and, as
2120 * "proto" is > ETHERMTU, this test
2121 * will fail and the frame won't match,
2122 * which is what we want).
2124 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2130 static struct slist
*
2131 gen_load_radiotap_llprefixlen()
2133 struct slist
*s1
, *s2
;
2136 * Generate code to load the length of the radiotap header into
2137 * the register assigned to hold that length, if one has been
2138 * assigned. (If one hasn't been assigned, no code we've
2139 * generated uses that prefix, so we don't need to generate any
2142 if (reg_off_ll
!= -1) {
2144 * The 2 bytes at offsets of 2 and 3 from the beginning
2145 * of the radiotap header are the length of the radiotap
2146 * header; unfortunately, it's little-endian, so we have
2147 * to load it a byte at a time and construct the value.
2151 * Load the high-order byte, at an offset of 3, shift it
2152 * left a byte, and put the result in the X register.
2154 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2156 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2159 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2163 * Load the next byte, at an offset of 2, and OR the
2164 * value from the X register into it.
2166 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2169 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2173 * Now allocate a register to hold that value and store
2176 s2
= new_stmt(BPF_ST
);
2177 s2
->s
.k
= reg_off_ll
;
2181 * Now move it into the X register.
2183 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2192 * At the moment we treat PPI as normal Radiotap encoded
2193 * packets. The difference is in the function that generates
2194 * the code at the beginning to compute the header length.
2195 * Since this code generator of PPI supports bare 802.11
2196 * encapsulation only (i.e. the encapsulated DLT should be
2197 * DLT_IEEE802_11) we generate code to check for this too;
2198 * that's done in finish_parse().
2200 static struct slist
*
2201 gen_load_ppi_llprefixlen()
2203 struct slist
*s1
, *s2
;
2206 * Generate code to load the length of the radiotap header
2207 * into the register assigned to hold that length, if one has
2210 if (reg_off_ll
!= -1) {
2212 * The 2 bytes at offsets of 2 and 3 from the beginning
2213 * of the radiotap header are the length of the radiotap
2214 * header; unfortunately, it's little-endian, so we have
2215 * to load it a byte at a time and construct the value.
2219 * Load the high-order byte, at an offset of 3, shift it
2220 * left a byte, and put the result in the X register.
2222 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2224 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2227 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2231 * Load the next byte, at an offset of 2, and OR the
2232 * value from the X register into it.
2234 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2237 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2241 * Now allocate a register to hold that value and store
2244 s2
= new_stmt(BPF_ST
);
2245 s2
->s
.k
= reg_off_ll
;
2249 * Now move it into the X register.
2251 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2260 * Load a value relative to the beginning of the link-layer header after the 802.11
2261 * header, i.e. LLC_SNAP.
2262 * The link-layer header doesn't necessarily begin at the beginning
2263 * of the packet data; there might be a variable-length prefix containing
2264 * radio information.
2266 static struct slist
*
2267 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2270 struct slist
*sjset_data_frame_1
;
2271 struct slist
*sjset_data_frame_2
;
2272 struct slist
*sjset_qos
;
2274 if (reg_off_macpl
== -1) {
2276 * No register has been assigned to the offset of
2277 * the MAC-layer payload, which means nobody needs
2278 * it; don't bother computing it - just return
2279 * what we already have.
2285 * This code is not compatible with the optimizer, as
2286 * we are generating jmp instructions within a normal
2287 * slist of instructions
2292 * If "s" is non-null, it has code to arrange that the X register
2293 * contains the length of the prefix preceding the link-layer
2296 * Otherwise, the length of the prefix preceding the link-layer
2297 * header is "off_ll".
2301 * There is no variable-length header preceding the
2302 * link-layer header.
2304 * Load the length of the fixed-length prefix preceding
2305 * the link-layer header (if any) into the X register,
2306 * and store it in the reg_off_macpl register.
2307 * That length is off_ll.
2309 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2314 * The X register contains the offset of the beginning of the
2315 * link-layer header; add 24, which is the minimum length
2316 * of the MAC header for a data frame, to that, and store it
2317 * in reg_off_macpl, and then load the Frame Control field,
2318 * which is at the offset in the X register, with an indexed load.
2320 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2322 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2325 s2
= new_stmt(BPF_ST
);
2326 s2
->s
.k
= reg_off_macpl
;
2329 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2334 * Check the Frame Control field to see if this is a data frame;
2335 * a data frame has the 0x08 bit (b3) in that field set and the
2336 * 0x04 bit (b2) clear.
2338 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2339 sjset_data_frame_1
->s
.k
= 0x08;
2340 sappend(s
, sjset_data_frame_1
);
2343 * If b3 is set, test b2, otherwise go to the first statement of
2344 * the rest of the program.
2346 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2347 sjset_data_frame_2
->s
.k
= 0x04;
2348 sappend(s
, sjset_data_frame_2
);
2349 sjset_data_frame_1
->s
.jf
= snext
;
2352 * If b2 is not set, this is a data frame; test the QoS bit.
2353 * Otherwise, go to the first statement of the rest of the
2356 sjset_data_frame_2
->s
.jt
= snext
;
2357 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2358 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2359 sappend(s
, sjset_qos
);
2362 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2363 * field (the current value of reg_off_macpl is in the
2364 * X register, so store 2 + X in reg_off_macpl).
2365 * Otherwise, go to the first statement of the rest of the
2368 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_IMM
);
2371 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
2373 s2
= new_stmt(BPF_ST
);
2374 s2
->s
.k
= reg_off_macpl
;
2376 sjset_qos
->s
.jf
= snext
;
2382 insert_load_llprefixlen(b
)
2388 * Generate code to load the length of any variable-length
2389 * header preceding the link-layer header into the register
2390 * assigned to that length, if any.
2394 case DLT_IEEE802_11_RADIO
:
2395 s
= gen_load_radiotap_llprefixlen();
2399 s
= gen_load_ppi_llprefixlen();
2408 * Now generate code to load the length of any variable-length
2409 * header preceding the link-layer header into the register
2410 * assigned to that length, if any.
2414 case DLT_IEEE802_11
:
2415 case DLT_IEEE802_11_RADIO
:
2416 case DLT_IEEE802_11_RADIO_AVS
:
2417 case DLT_PRISM_HEADER
:
2419 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2424 * If we have any length loading code, append all the
2425 * existing statements in the block to those statements,
2426 * and make the resulting list the list of statements
2430 sappend(s
, b
->stmts
);
2435 static struct block
*
2436 gen_ppi_dlt_check(void)
2438 struct slist
*s_load_dlt
;
2441 if (linktype
== DLT_PPI
)
2443 /* Create the statements that check for the DLT
2445 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2446 s_load_dlt
->s
.k
= 4;
2448 b
= new_block(JMP(BPF_JEQ
));
2450 b
->stmts
= s_load_dlt
;
2451 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2461 static struct slist
*
2462 gen_radiotap_llprefixlen(void)
2466 if (reg_off_ll
== -1) {
2468 * We haven't yet assigned a register for the length
2469 * of the radiotap header; allocate one.
2471 reg_off_ll
= alloc_reg();
2475 * Load the register containing the radiotap length
2476 * into the X register.
2478 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2479 s
->s
.k
= reg_off_ll
;
2484 * At the moment we treat PPI as normal Radiotap encoded
2485 * packets. The difference is in the function that generates
2486 * the code at the beginning to compute the header length.
2487 * Since this code generator of PPI supports bare 802.11
2488 * encapsulation only (i.e. the encapsulated DLT should be
2489 * DLT_IEEE802_11) we generate code to check for this too.
2491 static struct slist
*
2492 gen_ppi_llprefixlen(void)
2496 if (reg_off_ll
== -1) {
2498 * We haven't yet assigned a register for the length
2499 * of the radiotap header; allocate one.
2501 reg_off_ll
= alloc_reg();
2505 * Load the register containing the radiotap length
2506 * into the X register.
2508 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2509 s
->s
.k
= reg_off_ll
;
2514 * Generate code to compute the link-layer header length, if necessary,
2515 * putting it into the X register, and to return either a pointer to a
2516 * "struct slist" for the list of statements in that code, or NULL if
2517 * no code is necessary.
2519 static struct slist
*
2520 gen_llprefixlen(void)
2525 return gen_ppi_llprefixlen();
2527 case DLT_IEEE802_11_RADIO
:
2528 return gen_radiotap_llprefixlen();
2536 * Generate code to load the register containing the offset of the
2537 * MAC-layer payload into the X register; if no register for that offset
2538 * has been allocated, allocate it first.
2540 static struct slist
*
2545 if (off_macpl_is_variable
) {
2546 if (reg_off_macpl
== -1) {
2548 * We haven't yet assigned a register for the offset
2549 * of the MAC-layer payload; allocate one.
2551 reg_off_macpl
= alloc_reg();
2555 * Load the register containing the offset of the MAC-layer
2556 * payload into the X register.
2558 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2559 s
->s
.k
= reg_off_macpl
;
2563 * That offset isn't variable, so we don't need to
2564 * generate any code.
2571 * Map an Ethernet type to the equivalent PPP type.
2574 ethertype_to_ppptype(proto
)
2584 case ETHERTYPE_IPV6
:
2593 case ETHERTYPE_ATALK
:
2607 * I'm assuming the "Bridging PDU"s that go
2608 * over PPP are Spanning Tree Protocol
2622 * Generate code to match a particular packet type by matching the
2623 * link-layer type field or fields in the 802.2 LLC header.
2625 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2626 * value, if <= ETHERMTU.
2628 static struct block
*
2632 struct block
*b0
, *b1
, *b2
;
2634 /* are we checking MPLS-encapsulated packets? */
2635 if (label_stack_depth
> 0) {
2639 /* FIXME add other L3 proto IDs */
2640 return gen_mpls_linktype(Q_IP
);
2642 case ETHERTYPE_IPV6
:
2644 /* FIXME add other L3 proto IDs */
2645 return gen_mpls_linktype(Q_IPV6
);
2648 bpf_error("unsupported protocol over mpls");
2654 * Are we testing PPPoE packets?
2658 * The PPPoE session header is part of the
2659 * MAC-layer payload, so all references
2660 * should be relative to the beginning of
2665 * We use Ethernet protocol types inside libpcap;
2666 * map them to the corresponding PPP protocol types.
2668 proto
= ethertype_to_ppptype(proto
);
2669 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2675 return gen_ether_linktype(proto
);
2683 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2687 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2695 case DLT_IEEE802_11
:
2696 case DLT_IEEE802_11_RADIO
:
2697 case DLT_IEEE802_11_RADIO_AVS
:
2698 case DLT_PRISM_HEADER
:
2700 * Check that we have a data frame.
2702 b0
= gen_check_802_11_data_frame();
2705 * Now check for the specified link-layer type.
2707 b1
= gen_llc_linktype(proto
);
2715 * XXX - check for asynchronous frames, as per RFC 1103.
2717 return gen_llc_linktype(proto
);
2723 * XXX - check for LLC PDUs, as per IEEE 802.5.
2725 return gen_llc_linktype(proto
);
2729 case DLT_ATM_RFC1483
:
2731 case DLT_IP_OVER_FC
:
2732 return gen_llc_linktype(proto
);
2738 * If "is_lane" is set, check for a LANE-encapsulated
2739 * version of this protocol, otherwise check for an
2740 * LLC-encapsulated version of this protocol.
2742 * We assume LANE means Ethernet, not Token Ring.
2746 * Check that the packet doesn't begin with an
2747 * LE Control marker. (We've already generated
2750 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2755 * Now generate an Ethernet test.
2757 b1
= gen_ether_linktype(proto
);
2762 * Check for LLC encapsulation and then check the
2765 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2766 b1
= gen_llc_linktype(proto
);
2774 return gen_linux_sll_linktype(proto
);
2779 case DLT_SLIP_BSDOS
:
2782 * These types don't provide any type field; packets
2783 * are always IPv4 or IPv6.
2785 * XXX - for IPv4, check for a version number of 4, and,
2786 * for IPv6, check for a version number of 6?
2791 /* Check for a version number of 4. */
2792 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2794 case ETHERTYPE_IPV6
:
2795 /* Check for a version number of 6. */
2796 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2800 return gen_false(); /* always false */
2807 case DLT_PPP_SERIAL
:
2810 * We use Ethernet protocol types inside libpcap;
2811 * map them to the corresponding PPP protocol types.
2813 proto
= ethertype_to_ppptype(proto
);
2814 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2820 * We use Ethernet protocol types inside libpcap;
2821 * map them to the corresponding PPP protocol types.
2827 * Also check for Van Jacobson-compressed IP.
2828 * XXX - do this for other forms of PPP?
2830 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2831 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2833 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2838 proto
= ethertype_to_ppptype(proto
);
2839 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2849 * For DLT_NULL, the link-layer header is a 32-bit
2850 * word containing an AF_ value in *host* byte order,
2851 * and for DLT_ENC, the link-layer header begins
2852 * with a 32-bit work containing an AF_ value in
2855 * In addition, if we're reading a saved capture file,
2856 * the host byte order in the capture may not be the
2857 * same as the host byte order on this machine.
2859 * For DLT_LOOP, the link-layer header is a 32-bit
2860 * word containing an AF_ value in *network* byte order.
2862 * XXX - AF_ values may, unfortunately, be platform-
2863 * dependent; for example, FreeBSD's AF_INET6 is 24
2864 * whilst NetBSD's and OpenBSD's is 26.
2866 * This means that, when reading a capture file, just
2867 * checking for our AF_INET6 value won't work if the
2868 * capture file came from another OS.
2877 case ETHERTYPE_IPV6
:
2884 * Not a type on which we support filtering.
2885 * XXX - support those that have AF_ values
2886 * #defined on this platform, at least?
2891 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2893 * The AF_ value is in host byte order, but
2894 * the BPF interpreter will convert it to
2895 * network byte order.
2897 * If this is a save file, and it's from a
2898 * machine with the opposite byte order to
2899 * ours, we byte-swap the AF_ value.
2901 * Then we run it through "htonl()", and
2902 * generate code to compare against the result.
2904 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2905 bpf_pcap
->sf
.swapped
)
2906 proto
= SWAPLONG(proto
);
2907 proto
= htonl(proto
);
2909 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2911 #ifdef HAVE_NET_PFVAR_H
2914 * af field is host byte order in contrast to the rest of
2917 if (proto
== ETHERTYPE_IP
)
2918 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2919 BPF_B
, (bpf_int32
)AF_INET
));
2921 else if (proto
== ETHERTYPE_IPV6
)
2922 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2923 BPF_B
, (bpf_int32
)AF_INET6
));
2929 #endif /* HAVE_NET_PFVAR_H */
2932 case DLT_ARCNET_LINUX
:
2934 * XXX should we check for first fragment if the protocol
2943 case ETHERTYPE_IPV6
:
2944 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2945 (bpf_int32
)ARCTYPE_INET6
));
2949 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2950 (bpf_int32
)ARCTYPE_IP
);
2951 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2952 (bpf_int32
)ARCTYPE_IP_OLD
);
2957 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2958 (bpf_int32
)ARCTYPE_ARP
);
2959 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2960 (bpf_int32
)ARCTYPE_ARP_OLD
);
2964 case ETHERTYPE_REVARP
:
2965 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2966 (bpf_int32
)ARCTYPE_REVARP
));
2968 case ETHERTYPE_ATALK
:
2969 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2970 (bpf_int32
)ARCTYPE_ATALK
));
2977 case ETHERTYPE_ATALK
:
2987 * XXX - assumes a 2-byte Frame Relay header with
2988 * DLCI and flags. What if the address is longer?
2994 * Check for the special NLPID for IP.
2996 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2999 case ETHERTYPE_IPV6
:
3001 * Check for the special NLPID for IPv6.
3003 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3008 * Check for several OSI protocols.
3010 * Frame Relay packets typically have an OSI
3011 * NLPID at the beginning; we check for each
3014 * What we check for is the NLPID and a frame
3015 * control field of UI, i.e. 0x03 followed
3018 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3019 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3020 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3032 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3034 case DLT_JUNIPER_MFR
:
3035 case DLT_JUNIPER_MLFR
:
3036 case DLT_JUNIPER_MLPPP
:
3037 case DLT_JUNIPER_ATM1
:
3038 case DLT_JUNIPER_ATM2
:
3039 case DLT_JUNIPER_PPPOE
:
3040 case DLT_JUNIPER_PPPOE_ATM
:
3041 case DLT_JUNIPER_GGSN
:
3042 case DLT_JUNIPER_ES
:
3043 case DLT_JUNIPER_MONITOR
:
3044 case DLT_JUNIPER_SERVICES
:
3045 case DLT_JUNIPER_ETHER
:
3046 case DLT_JUNIPER_PPP
:
3047 case DLT_JUNIPER_FRELAY
:
3048 case DLT_JUNIPER_CHDLC
:
3049 case DLT_JUNIPER_VP
:
3050 case DLT_JUNIPER_ST
:
3051 case DLT_JUNIPER_ISM
:
3052 /* just lets verify the magic number for now -
3053 * on ATM we may have up to 6 different encapsulations on the wire
3054 * and need a lot of heuristics to figure out that the payload
3057 * FIXME encapsulation specific BPF_ filters
3059 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3061 case DLT_LINUX_IRDA
:
3062 bpf_error("IrDA link-layer type filtering not implemented");
3065 bpf_error("DOCSIS link-layer type filtering not implemented");
3068 case DLT_MTP2_WITH_PHDR
:
3069 bpf_error("MTP2 link-layer type filtering not implemented");
3072 bpf_error("ERF link-layer type filtering not implemented");
3076 bpf_error("PFSYNC link-layer type filtering not implemented");
3079 case DLT_LINUX_LAPD
:
3080 bpf_error("LAPD link-layer type filtering not implemented");
3084 bpf_error("USB link-layer type filtering not implemented");
3086 case DLT_BLUETOOTH_HCI_H4
:
3087 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3088 bpf_error("Bluetooth link-layer type filtering not implemented");
3091 bpf_error("CAN20B link-layer type filtering not implemented");
3093 case DLT_IEEE802_15_4
:
3094 case DLT_IEEE802_15_4_LINUX
:
3095 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3097 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3098 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3101 bpf_error("SITA link-layer type filtering not implemented");
3104 bpf_error("RAIF1 link-layer type filtering not implemented");
3107 bpf_error("IPMB link-layer type filtering not implemented");
3110 bpf_error("AX.25 link-layer type filtering not implemented");
3114 * All the types that have no encapsulation should either be
3115 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3116 * all packets are IP packets, or should be handled in some
3117 * special case, if none of them are (if some are and some
3118 * aren't, the lack of encapsulation is a problem, as we'd
3119 * have to find some other way of determining the packet type).
3121 * Therefore, if "off_linktype" is -1, there's an error.
3123 if (off_linktype
== (u_int
)-1)
3127 * Any type not handled above should always have an Ethernet
3128 * type at an offset of "off_linktype".
3130 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3134 * Check for an LLC SNAP packet with a given organization code and
3135 * protocol type; we check the entire contents of the 802.2 LLC and
3136 * snap headers, checking for DSAP and SSAP of SNAP and a control
3137 * field of 0x03 in the LLC header, and for the specified organization
3138 * code and protocol type in the SNAP header.
3140 static struct block
*
3141 gen_snap(orgcode
, ptype
)
3142 bpf_u_int32 orgcode
;
3145 u_char snapblock
[8];
3147 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3148 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3149 snapblock
[2] = 0x03; /* control = UI */
3150 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3151 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3152 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3153 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3154 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3155 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3159 * Generate code to match a particular packet type, for link-layer types
3160 * using 802.2 LLC headers.
3162 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3163 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3165 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3166 * value, if <= ETHERMTU. We use that to determine whether to
3167 * match the DSAP or both DSAP and LSAP or to check the OUI and
3168 * protocol ID in a SNAP header.
3170 static struct block
*
3171 gen_llc_linktype(proto
)
3175 * XXX - handle token-ring variable-length header.
3181 case LLCSAP_NETBEUI
:
3183 * XXX - should we check both the DSAP and the
3184 * SSAP, like this, or should we check just the
3185 * DSAP, as we do for other types <= ETHERMTU
3186 * (i.e., other SAP values)?
3188 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3189 ((proto
<< 8) | proto
));
3193 * XXX - are there ever SNAP frames for IPX on
3194 * non-Ethernet 802.x networks?
3196 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3197 (bpf_int32
)LLCSAP_IPX
);
3199 case ETHERTYPE_ATALK
:
3201 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3202 * SNAP packets with an organization code of
3203 * 0x080007 (Apple, for Appletalk) and a protocol
3204 * type of ETHERTYPE_ATALK (Appletalk).
3206 * XXX - check for an organization code of
3207 * encapsulated Ethernet as well?
3209 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3213 * XXX - we don't have to check for IPX 802.3
3214 * here, but should we check for the IPX Ethertype?
3216 if (proto
<= ETHERMTU
) {
3218 * This is an LLC SAP value, so check
3221 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3224 * This is an Ethernet type; we assume that it's
3225 * unlikely that it'll appear in the right place
3226 * at random, and therefore check only the
3227 * location that would hold the Ethernet type
3228 * in a SNAP frame with an organization code of
3229 * 0x000000 (encapsulated Ethernet).
3231 * XXX - if we were to check for the SNAP DSAP and
3232 * LSAP, as per XXX, and were also to check for an
3233 * organization code of 0x000000 (encapsulated
3234 * Ethernet), we'd do
3236 * return gen_snap(0x000000, proto);
3238 * here; for now, we don't, as per the above.
3239 * I don't know whether it's worth the extra CPU
3240 * time to do the right check or not.
3242 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3247 static struct block
*
3248 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3252 u_int src_off
, dst_off
;
3254 struct block
*b0
, *b1
;
3268 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3269 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3275 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3276 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3283 b0
= gen_linktype(proto
);
3284 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3290 static struct block
*
3291 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3292 struct in6_addr
*addr
;
3293 struct in6_addr
*mask
;
3295 u_int src_off
, dst_off
;
3297 struct block
*b0
, *b1
;
3312 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3313 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3319 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3320 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3327 /* this order is important */
3328 a
= (u_int32_t
*)addr
;
3329 m
= (u_int32_t
*)mask
;
3330 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3331 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3333 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3335 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3337 b0
= gen_linktype(proto
);
3343 static struct block
*
3344 gen_ehostop(eaddr
, dir
)
3345 register const u_char
*eaddr
;
3348 register struct block
*b0
, *b1
;
3352 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3355 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3358 b0
= gen_ehostop(eaddr
, Q_SRC
);
3359 b1
= gen_ehostop(eaddr
, Q_DST
);
3365 b0
= gen_ehostop(eaddr
, Q_SRC
);
3366 b1
= gen_ehostop(eaddr
, Q_DST
);
3375 * Like gen_ehostop, but for DLT_FDDI
3377 static struct block
*
3378 gen_fhostop(eaddr
, dir
)
3379 register const u_char
*eaddr
;
3382 struct block
*b0
, *b1
;
3387 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3389 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3394 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3396 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3400 b0
= gen_fhostop(eaddr
, Q_SRC
);
3401 b1
= gen_fhostop(eaddr
, Q_DST
);
3407 b0
= gen_fhostop(eaddr
, Q_SRC
);
3408 b1
= gen_fhostop(eaddr
, Q_DST
);
3417 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3419 static struct block
*
3420 gen_thostop(eaddr
, dir
)
3421 register const u_char
*eaddr
;
3424 register struct block
*b0
, *b1
;
3428 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3431 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3434 b0
= gen_thostop(eaddr
, Q_SRC
);
3435 b1
= gen_thostop(eaddr
, Q_DST
);
3441 b0
= gen_thostop(eaddr
, Q_SRC
);
3442 b1
= gen_thostop(eaddr
, Q_DST
);
3451 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
3453 static struct block
*
3454 gen_wlanhostop(eaddr
, dir
)
3455 register const u_char
*eaddr
;
3458 register struct block
*b0
, *b1
, *b2
;
3459 register struct slist
*s
;
3461 #ifdef ENABLE_WLAN_FILTERING_PATCH
3464 * We need to disable the optimizer because the optimizer is buggy
3465 * and wipes out some LD instructions generated by the below
3466 * code to validate the Frame Control bits
3469 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3476 * For control frames, there is no SA.
3478 * For management frames, SA is at an
3479 * offset of 10 from the beginning of
3482 * For data frames, SA is at an offset
3483 * of 10 from the beginning of the packet
3484 * if From DS is clear, at an offset of
3485 * 16 from the beginning of the packet
3486 * if From DS is set and To DS is clear,
3487 * and an offset of 24 from the beginning
3488 * of the packet if From DS is set and To DS
3493 * Generate the tests to be done for data frames
3496 * First, check for To DS set, i.e. check "link[1] & 0x01".
3498 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3499 b1
= new_block(JMP(BPF_JSET
));
3500 b1
->s
.k
= 0x01; /* To DS */
3504 * If To DS is set, the SA is at 24.
3506 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3510 * Now, check for To DS not set, i.e. check
3511 * "!(link[1] & 0x01)".
3513 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3514 b2
= new_block(JMP(BPF_JSET
));
3515 b2
->s
.k
= 0x01; /* To DS */
3520 * If To DS is not set, the SA is at 16.
3522 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3526 * Now OR together the last two checks. That gives
3527 * the complete set of checks for data frames with
3533 * Now check for From DS being set, and AND that with
3534 * the ORed-together checks.
3536 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3537 b1
= new_block(JMP(BPF_JSET
));
3538 b1
->s
.k
= 0x02; /* From DS */
3543 * Now check for data frames with From DS not set.
3545 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3546 b2
= new_block(JMP(BPF_JSET
));
3547 b2
->s
.k
= 0x02; /* From DS */
3552 * If From DS isn't set, the SA is at 10.
3554 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3558 * Now OR together the checks for data frames with
3559 * From DS not set and for data frames with From DS
3560 * set; that gives the checks done for data frames.
3565 * Now check for a data frame.
3566 * I.e, check "link[0] & 0x08".
3568 gen_load_a(OR_LINK
, 0, BPF_B
);
3569 b1
= new_block(JMP(BPF_JSET
));
3574 * AND that with the checks done for data frames.
3579 * If the high-order bit of the type value is 0, this
3580 * is a management frame.
3581 * I.e, check "!(link[0] & 0x08)".
3583 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3584 b2
= new_block(JMP(BPF_JSET
));
3590 * For management frames, the SA is at 10.
3592 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3596 * OR that with the checks done for data frames.
3597 * That gives the checks done for management and
3603 * If the low-order bit of the type value is 1,
3604 * this is either a control frame or a frame
3605 * with a reserved type, and thus not a
3608 * I.e., check "!(link[0] & 0x04)".
3610 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3611 b1
= new_block(JMP(BPF_JSET
));
3617 * AND that with the checks for data and management
3627 * For control frames, there is no DA.
3629 * For management frames, DA is at an
3630 * offset of 4 from the beginning of
3633 * For data frames, DA is at an offset
3634 * of 4 from the beginning of the packet
3635 * if To DS is clear and at an offset of
3636 * 16 from the beginning of the packet
3641 * Generate the tests to be done for data frames.
3643 * First, check for To DS set, i.e. "link[1] & 0x01".
3645 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3646 b1
= new_block(JMP(BPF_JSET
));
3647 b1
->s
.k
= 0x01; /* To DS */
3651 * If To DS is set, the DA is at 16.
3653 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3657 * Now, check for To DS not set, i.e. check
3658 * "!(link[1] & 0x01)".
3660 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3661 b2
= new_block(JMP(BPF_JSET
));
3662 b2
->s
.k
= 0x01; /* To DS */
3667 * If To DS is not set, the DA is at 4.
3669 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3673 * Now OR together the last two checks. That gives
3674 * the complete set of checks for data frames.
3679 * Now check for a data frame.
3680 * I.e, check "link[0] & 0x08".
3682 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3683 b1
= new_block(JMP(BPF_JSET
));
3688 * AND that with the checks done for data frames.
3693 * If the high-order bit of the type value is 0, this
3694 * is a management frame.
3695 * I.e, check "!(link[0] & 0x08)".
3697 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3698 b2
= new_block(JMP(BPF_JSET
));
3704 * For management frames, the DA is at 4.
3706 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3710 * OR that with the checks done for data frames.
3711 * That gives the checks done for management and
3717 * If the low-order bit of the type value is 1,
3718 * this is either a control frame or a frame
3719 * with a reserved type, and thus not a
3722 * I.e., check "!(link[0] & 0x04)".
3724 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3725 b1
= new_block(JMP(BPF_JSET
));
3731 * AND that with the checks for data and management
3738 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3739 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3745 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3746 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3755 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3756 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3757 * as the RFC states.)
3759 static struct block
*
3760 gen_ipfchostop(eaddr
, dir
)
3761 register const u_char
*eaddr
;
3764 register struct block
*b0
, *b1
;
3768 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3771 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3774 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3775 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3781 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3782 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3791 * This is quite tricky because there may be pad bytes in front of the
3792 * DECNET header, and then there are two possible data packet formats that
3793 * carry both src and dst addresses, plus 5 packet types in a format that
3794 * carries only the src node, plus 2 types that use a different format and
3795 * also carry just the src node.
3799 * Instead of doing those all right, we just look for data packets with
3800 * 0 or 1 bytes of padding. If you want to look at other packets, that
3801 * will require a lot more hacking.
3803 * To add support for filtering on DECNET "areas" (network numbers)
3804 * one would want to add a "mask" argument to this routine. That would
3805 * make the filter even more inefficient, although one could be clever
3806 * and not generate masking instructions if the mask is 0xFFFF.
3808 static struct block
*
3809 gen_dnhostop(addr
, dir
)
3813 struct block
*b0
, *b1
, *b2
, *tmp
;
3814 u_int offset_lh
; /* offset if long header is received */
3815 u_int offset_sh
; /* offset if short header is received */
3820 offset_sh
= 1; /* follows flags */
3821 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3825 offset_sh
= 3; /* follows flags, dstnode */
3826 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3830 /* Inefficient because we do our Calvinball dance twice */
3831 b0
= gen_dnhostop(addr
, Q_SRC
);
3832 b1
= gen_dnhostop(addr
, Q_DST
);
3838 /* Inefficient because we do our Calvinball dance twice */
3839 b0
= gen_dnhostop(addr
, Q_SRC
);
3840 b1
= gen_dnhostop(addr
, Q_DST
);
3845 bpf_error("ISO host filtering not implemented");
3850 b0
= gen_linktype(ETHERTYPE_DN
);
3851 /* Check for pad = 1, long header case */
3852 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3853 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3854 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3855 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3857 /* Check for pad = 0, long header case */
3858 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3859 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3862 /* Check for pad = 1, short header case */
3863 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3864 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3865 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3868 /* Check for pad = 0, short header case */
3869 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3870 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3874 /* Combine with test for linktype */
3880 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3881 * test the bottom-of-stack bit, and then check the version number
3882 * field in the IP header.
3884 static struct block
*
3885 gen_mpls_linktype(proto
)
3888 struct block
*b0
, *b1
;
3893 /* match the bottom-of-stack bit */
3894 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3895 /* match the IPv4 version number */
3896 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3901 /* match the bottom-of-stack bit */
3902 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3903 /* match the IPv4 version number */
3904 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3913 static struct block
*
3914 gen_host(addr
, mask
, proto
, dir
, type
)
3921 struct block
*b0
, *b1
;
3922 const char *typestr
;
3932 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3934 * Only check for non-IPv4 addresses if we're not
3935 * checking MPLS-encapsulated packets.
3937 if (label_stack_depth
== 0) {
3938 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3940 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3946 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3949 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3952 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3955 bpf_error("'tcp' modifier applied to %s", typestr
);
3958 bpf_error("'sctp' modifier applied to %s", typestr
);
3961 bpf_error("'udp' modifier applied to %s", typestr
);
3964 bpf_error("'icmp' modifier applied to %s", typestr
);
3967 bpf_error("'igmp' modifier applied to %s", typestr
);
3970 bpf_error("'igrp' modifier applied to %s", typestr
);
3973 bpf_error("'pim' modifier applied to %s", typestr
);
3976 bpf_error("'vrrp' modifier applied to %s", typestr
);
3979 bpf_error("ATALK host filtering not implemented");
3982 bpf_error("AARP host filtering not implemented");
3985 return gen_dnhostop(addr
, dir
);
3988 bpf_error("SCA host filtering not implemented");
3991 bpf_error("LAT host filtering not implemented");
3994 bpf_error("MOPDL host filtering not implemented");
3997 bpf_error("MOPRC host filtering not implemented");
4001 bpf_error("'ip6' modifier applied to ip host");
4004 bpf_error("'icmp6' modifier applied to %s", typestr
);
4008 bpf_error("'ah' modifier applied to %s", typestr
);
4011 bpf_error("'esp' modifier applied to %s", typestr
);
4014 bpf_error("ISO host filtering not implemented");
4017 bpf_error("'esis' modifier applied to %s", typestr
);
4020 bpf_error("'isis' modifier applied to %s", typestr
);
4023 bpf_error("'clnp' modifier applied to %s", typestr
);
4026 bpf_error("'stp' modifier applied to %s", typestr
);
4029 bpf_error("IPX host filtering not implemented");
4032 bpf_error("'netbeui' modifier applied to %s", typestr
);
4035 bpf_error("'radio' modifier applied to %s", typestr
);
4044 static struct block
*
4045 gen_host6(addr
, mask
, proto
, dir
, type
)
4046 struct in6_addr
*addr
;
4047 struct in6_addr
*mask
;
4052 const char *typestr
;
4062 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4065 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4068 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4071 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4074 bpf_error("'sctp' modifier applied to %s", typestr
);
4077 bpf_error("'tcp' modifier applied to %s", typestr
);
4080 bpf_error("'udp' modifier applied to %s", typestr
);
4083 bpf_error("'icmp' modifier applied to %s", typestr
);
4086 bpf_error("'igmp' modifier applied to %s", typestr
);
4089 bpf_error("'igrp' modifier applied to %s", typestr
);
4092 bpf_error("'pim' modifier applied to %s", typestr
);
4095 bpf_error("'vrrp' modifier applied to %s", typestr
);
4098 bpf_error("ATALK host filtering not implemented");
4101 bpf_error("AARP host filtering not implemented");
4104 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4107 bpf_error("SCA host filtering not implemented");
4110 bpf_error("LAT host filtering not implemented");
4113 bpf_error("MOPDL host filtering not implemented");
4116 bpf_error("MOPRC host filtering not implemented");
4119 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4122 bpf_error("'icmp6' modifier applied to %s", typestr
);
4125 bpf_error("'ah' modifier applied to %s", typestr
);
4128 bpf_error("'esp' modifier applied to %s", typestr
);
4131 bpf_error("ISO host filtering not implemented");
4134 bpf_error("'esis' modifier applied to %s", typestr
);
4137 bpf_error("'isis' modifier applied to %s", typestr
);
4140 bpf_error("'clnp' modifier applied to %s", typestr
);
4143 bpf_error("'stp' modifier applied to %s", typestr
);
4146 bpf_error("IPX host filtering not implemented");
4149 bpf_error("'netbeui' modifier applied to %s", typestr
);
4152 bpf_error("'radio' modifier applied to %s", typestr
);
4162 static struct block
*
4163 gen_gateway(eaddr
, alist
, proto
, dir
)
4164 const u_char
*eaddr
;
4165 bpf_u_int32
**alist
;
4169 struct block
*b0
, *b1
, *tmp
;
4172 bpf_error("direction applied to 'gateway'");
4181 b0
= gen_ehostop(eaddr
, Q_OR
);
4184 b0
= gen_fhostop(eaddr
, Q_OR
);
4187 b0
= gen_thostop(eaddr
, Q_OR
);
4189 case DLT_IEEE802_11
:
4190 case DLT_IEEE802_11_RADIO_AVS
:
4192 case DLT_IEEE802_11_RADIO
:
4193 case DLT_PRISM_HEADER
:
4194 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4199 * Check that the packet doesn't begin with an
4200 * LE Control marker. (We've already generated
4203 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4208 * Now check the MAC address.
4210 b0
= gen_ehostop(eaddr
, Q_OR
);
4214 case DLT_IP_OVER_FC
:
4215 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4219 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4221 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4223 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4232 bpf_error("illegal modifier of 'gateway'");
4238 gen_proto_abbrev(proto
)
4247 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4249 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4255 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4257 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4263 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4265 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4271 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4274 #ifndef IPPROTO_IGMP
4275 #define IPPROTO_IGMP 2
4279 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4282 #ifndef IPPROTO_IGRP
4283 #define IPPROTO_IGRP 9
4286 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4290 #define IPPROTO_PIM 103
4294 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4296 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4301 #ifndef IPPROTO_VRRP
4302 #define IPPROTO_VRRP 112
4306 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4310 b1
= gen_linktype(ETHERTYPE_IP
);
4314 b1
= gen_linktype(ETHERTYPE_ARP
);
4318 b1
= gen_linktype(ETHERTYPE_REVARP
);
4322 bpf_error("link layer applied in wrong context");
4325 b1
= gen_linktype(ETHERTYPE_ATALK
);
4329 b1
= gen_linktype(ETHERTYPE_AARP
);
4333 b1
= gen_linktype(ETHERTYPE_DN
);
4337 b1
= gen_linktype(ETHERTYPE_SCA
);
4341 b1
= gen_linktype(ETHERTYPE_LAT
);
4345 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4349 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4354 b1
= gen_linktype(ETHERTYPE_IPV6
);
4357 #ifndef IPPROTO_ICMPV6
4358 #define IPPROTO_ICMPV6 58
4361 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4366 #define IPPROTO_AH 51
4369 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4371 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4377 #define IPPROTO_ESP 50
4380 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4382 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4388 b1
= gen_linktype(LLCSAP_ISONS
);
4392 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4396 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4399 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4400 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4401 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4403 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4405 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4407 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4411 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4412 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4413 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4415 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4417 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4419 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4423 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4424 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4425 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4427 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4432 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4433 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4438 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4439 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4441 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4443 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4448 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4449 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4454 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4455 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4460 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4464 b1
= gen_linktype(LLCSAP_8021D
);
4468 b1
= gen_linktype(LLCSAP_IPX
);
4472 b1
= gen_linktype(LLCSAP_NETBEUI
);
4476 bpf_error("'radio' is not a valid protocol type");
4484 static struct block
*
4491 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4492 b
= new_block(JMP(BPF_JSET
));
4501 * Generate a comparison to a port value in the transport-layer header
4502 * at the specified offset from the beginning of that header.
4504 * XXX - this handles a variable-length prefix preceding the link-layer
4505 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4506 * variable-length link-layer headers (such as Token Ring or 802.11
4509 static struct block
*
4510 gen_portatom(off
, v
)
4514 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4518 static struct block
*
4519 gen_portatom6(off
, v
)
4523 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4528 gen_portop(port
, proto
, dir
)
4529 int port
, proto
, dir
;
4531 struct block
*b0
, *b1
, *tmp
;
4533 /* ip proto 'proto' */
4534 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4540 b1
= gen_portatom(0, (bpf_int32
)port
);
4544 b1
= gen_portatom(2, (bpf_int32
)port
);
4549 tmp
= gen_portatom(0, (bpf_int32
)port
);
4550 b1
= gen_portatom(2, (bpf_int32
)port
);
4555 tmp
= gen_portatom(0, (bpf_int32
)port
);
4556 b1
= gen_portatom(2, (bpf_int32
)port
);
4568 static struct block
*
4569 gen_port(port
, ip_proto
, dir
)
4574 struct block
*b0
, *b1
, *tmp
;
4579 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4580 * not LLC encapsulation with LLCSAP_IP.
4582 * For IEEE 802 networks - which includes 802.5 token ring
4583 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4584 * says that SNAP encapsulation is used, not LLC encapsulation
4587 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4588 * RFC 2225 say that SNAP encapsulation is used, not LLC
4589 * encapsulation with LLCSAP_IP.
4591 * So we always check for ETHERTYPE_IP.
4593 b0
= gen_linktype(ETHERTYPE_IP
);
4599 b1
= gen_portop(port
, ip_proto
, dir
);
4603 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4604 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4606 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4619 gen_portop6(port
, proto
, dir
)
4620 int port
, proto
, dir
;
4622 struct block
*b0
, *b1
, *tmp
;
4624 /* ip6 proto 'proto' */
4625 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4629 b1
= gen_portatom6(0, (bpf_int32
)port
);
4633 b1
= gen_portatom6(2, (bpf_int32
)port
);
4638 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4639 b1
= gen_portatom6(2, (bpf_int32
)port
);
4644 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4645 b1
= gen_portatom6(2, (bpf_int32
)port
);
4657 static struct block
*
4658 gen_port6(port
, ip_proto
, dir
)
4663 struct block
*b0
, *b1
, *tmp
;
4665 /* link proto ip6 */
4666 b0
= gen_linktype(ETHERTYPE_IPV6
);
4672 b1
= gen_portop6(port
, ip_proto
, dir
);
4676 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4677 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4679 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4691 /* gen_portrange code */
4692 static struct block
*
4693 gen_portrangeatom(off
, v1
, v2
)
4697 struct block
*b1
, *b2
;
4701 * Reverse the order of the ports, so v1 is the lower one.
4710 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4711 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4719 gen_portrangeop(port1
, port2
, proto
, dir
)
4724 struct block
*b0
, *b1
, *tmp
;
4726 /* ip proto 'proto' */
4727 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4733 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4737 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4742 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4743 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4748 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4749 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4761 static struct block
*
4762 gen_portrange(port1
, port2
, ip_proto
, dir
)
4767 struct block
*b0
, *b1
, *tmp
;
4770 b0
= gen_linktype(ETHERTYPE_IP
);
4776 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4780 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4781 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4783 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4795 static struct block
*
4796 gen_portrangeatom6(off
, v1
, v2
)
4800 struct block
*b1
, *b2
;
4804 * Reverse the order of the ports, so v1 is the lower one.
4813 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4814 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4822 gen_portrangeop6(port1
, port2
, proto
, dir
)
4827 struct block
*b0
, *b1
, *tmp
;
4829 /* ip6 proto 'proto' */
4830 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4834 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4838 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4843 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4844 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4849 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4850 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4862 static struct block
*
4863 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4868 struct block
*b0
, *b1
, *tmp
;
4870 /* link proto ip6 */
4871 b0
= gen_linktype(ETHERTYPE_IPV6
);
4877 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4881 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4882 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4884 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4897 lookup_proto(name
, proto
)
4898 register const char *name
;
4908 v
= pcap_nametoproto(name
);
4909 if (v
== PROTO_UNDEF
)
4910 bpf_error("unknown ip proto '%s'", name
);
4914 /* XXX should look up h/w protocol type based on linktype */
4915 v
= pcap_nametoeproto(name
);
4916 if (v
== PROTO_UNDEF
) {
4917 v
= pcap_nametollc(name
);
4918 if (v
== PROTO_UNDEF
)
4919 bpf_error("unknown ether proto '%s'", name
);
4924 if (strcmp(name
, "esis") == 0)
4926 else if (strcmp(name
, "isis") == 0)
4928 else if (strcmp(name
, "clnp") == 0)
4931 bpf_error("unknown osi proto '%s'", name
);
4951 static struct block
*
4952 gen_protochain(v
, proto
, dir
)
4957 #ifdef NO_PROTOCHAIN
4958 return gen_proto(v
, proto
, dir
);
4960 struct block
*b0
, *b
;
4961 struct slist
*s
[100];
4962 int fix2
, fix3
, fix4
, fix5
;
4963 int ahcheck
, again
, end
;
4965 int reg2
= alloc_reg();
4967 memset(s
, 0, sizeof(s
));
4968 fix2
= fix3
= fix4
= fix5
= 0;
4975 b0
= gen_protochain(v
, Q_IP
, dir
);
4976 b
= gen_protochain(v
, Q_IPV6
, dir
);
4980 bpf_error("bad protocol applied for 'protochain'");
4985 * We don't handle variable-length prefixes before the link-layer
4986 * header, or variable-length link-layer headers, here yet.
4987 * We might want to add BPF instructions to do the protochain
4988 * work, to simplify that and, on platforms that have a BPF
4989 * interpreter with the new instructions, let the filtering
4990 * be done in the kernel. (We already require a modified BPF
4991 * engine to do the protochain stuff, to support backward
4992 * branches, and backward branch support is unlikely to appear
4993 * in kernel BPF engines.)
4997 case DLT_IEEE802_11
:
4998 case DLT_IEEE802_11_RADIO
:
4999 case DLT_IEEE802_11_RADIO_AVS
:
5000 case DLT_PRISM_HEADER
:
5002 bpf_error("'protochain' not supported with 802.11");
5005 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5008 * s[0] is a dummy entry to protect other BPF insn from damage
5009 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5010 * hard to find interdependency made by jump table fixup.
5013 s
[i
] = new_stmt(0); /*dummy*/
5018 b0
= gen_linktype(ETHERTYPE_IP
);
5021 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5022 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5024 /* X = ip->ip_hl << 2 */
5025 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5026 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5031 b0
= gen_linktype(ETHERTYPE_IPV6
);
5033 /* A = ip6->ip_nxt */
5034 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5035 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5037 /* X = sizeof(struct ip6_hdr) */
5038 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5044 bpf_error("unsupported proto to gen_protochain");
5048 /* again: if (A == v) goto end; else fall through; */
5050 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5052 s
[i
]->s
.jt
= NULL
; /*later*/
5053 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5057 #ifndef IPPROTO_NONE
5058 #define IPPROTO_NONE 59
5060 /* if (A == IPPROTO_NONE) goto end */
5061 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5062 s
[i
]->s
.jt
= NULL
; /*later*/
5063 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5064 s
[i
]->s
.k
= IPPROTO_NONE
;
5065 s
[fix5
]->s
.jf
= s
[i
];
5070 if (proto
== Q_IPV6
) {
5071 int v6start
, v6end
, v6advance
, j
;
5074 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5075 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5076 s
[i
]->s
.jt
= NULL
; /*later*/
5077 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5078 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5079 s
[fix2
]->s
.jf
= s
[i
];
5081 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5082 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5083 s
[i
]->s
.jt
= NULL
; /*later*/
5084 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5085 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5087 /* if (A == IPPROTO_ROUTING) goto v6advance */
5088 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5089 s
[i
]->s
.jt
= NULL
; /*later*/
5090 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5091 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5093 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5094 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5095 s
[i
]->s
.jt
= NULL
; /*later*/
5096 s
[i
]->s
.jf
= NULL
; /*later*/
5097 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5108 * X = X + (P[X + 1] + 1) * 8;
5111 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5113 /* A = P[X + packet head] */
5114 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5115 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5118 s
[i
] = new_stmt(BPF_ST
);
5122 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5125 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5129 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5131 /* A = P[X + packet head]; */
5132 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5133 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5136 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5140 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5144 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5147 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5151 /* goto again; (must use BPF_JA for backward jump) */
5152 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5153 s
[i
]->s
.k
= again
- i
- 1;
5154 s
[i
- 1]->s
.jf
= s
[i
];
5158 for (j
= v6start
; j
<= v6end
; j
++)
5159 s
[j
]->s
.jt
= s
[v6advance
];
5164 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5166 s
[fix2
]->s
.jf
= s
[i
];
5172 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5173 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5174 s
[i
]->s
.jt
= NULL
; /*later*/
5175 s
[i
]->s
.jf
= NULL
; /*later*/
5176 s
[i
]->s
.k
= IPPROTO_AH
;
5178 s
[fix3
]->s
.jf
= s
[ahcheck
];
5185 * X = X + (P[X + 1] + 2) * 4;
5188 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5190 /* A = P[X + packet head]; */
5191 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5192 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5195 s
[i
] = new_stmt(BPF_ST
);
5199 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5202 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5206 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5208 /* A = P[X + packet head] */
5209 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5210 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5213 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5217 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5221 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5224 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5228 /* goto again; (must use BPF_JA for backward jump) */
5229 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5230 s
[i
]->s
.k
= again
- i
- 1;
5235 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5237 s
[fix2
]->s
.jt
= s
[end
];
5238 s
[fix4
]->s
.jf
= s
[end
];
5239 s
[fix5
]->s
.jt
= s
[end
];
5246 for (i
= 0; i
< max
- 1; i
++)
5247 s
[i
]->next
= s
[i
+ 1];
5248 s
[max
- 1]->next
= NULL
;
5253 b
= new_block(JMP(BPF_JEQ
));
5254 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5264 static struct block
*
5265 gen_check_802_11_data_frame()
5268 struct block
*b0
, *b1
;
5271 * A data frame has the 0x08 bit (b3) in the frame control field set
5272 * and the 0x04 bit (b2) clear.
5274 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5275 b0
= new_block(JMP(BPF_JSET
));
5279 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5280 b1
= new_block(JMP(BPF_JSET
));
5291 * Generate code that checks whether the packet is a packet for protocol
5292 * <proto> and whether the type field in that protocol's header has
5293 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5294 * IP packet and checks the protocol number in the IP header against <v>.
5296 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5297 * against Q_IP and Q_IPV6.
5299 static struct block
*
5300 gen_proto(v
, proto
, dir
)
5305 struct block
*b0
, *b1
;
5307 if (dir
!= Q_DEFAULT
)
5308 bpf_error("direction applied to 'proto'");
5313 b0
= gen_proto(v
, Q_IP
, dir
);
5314 b1
= gen_proto(v
, Q_IPV6
, dir
);
5322 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5323 * not LLC encapsulation with LLCSAP_IP.
5325 * For IEEE 802 networks - which includes 802.5 token ring
5326 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5327 * says that SNAP encapsulation is used, not LLC encapsulation
5330 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5331 * RFC 2225 say that SNAP encapsulation is used, not LLC
5332 * encapsulation with LLCSAP_IP.
5334 * So we always check for ETHERTYPE_IP.
5336 b0
= gen_linktype(ETHERTYPE_IP
);
5338 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5340 b1
= gen_protochain(v
, Q_IP
);
5350 * Frame Relay packets typically have an OSI
5351 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5352 * generates code to check for all the OSI
5353 * NLPIDs, so calling it and then adding a check
5354 * for the particular NLPID for which we're
5355 * looking is bogus, as we can just check for
5358 * What we check for is the NLPID and a frame
5359 * control field value of UI, i.e. 0x03 followed
5362 * XXX - assumes a 2-byte Frame Relay header with
5363 * DLCI and flags. What if the address is longer?
5365 * XXX - what about SNAP-encapsulated frames?
5367 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5373 * Cisco uses an Ethertype lookalike - for OSI,
5376 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5377 /* OSI in C-HDLC is stuffed with a fudge byte */
5378 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5383 b0
= gen_linktype(LLCSAP_ISONS
);
5384 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5390 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5392 * 4 is the offset of the PDU type relative to the IS-IS
5395 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5400 bpf_error("arp does not encapsulate another protocol");
5404 bpf_error("rarp does not encapsulate another protocol");
5408 bpf_error("atalk encapsulation is not specifiable");
5412 bpf_error("decnet encapsulation is not specifiable");
5416 bpf_error("sca does not encapsulate another protocol");
5420 bpf_error("lat does not encapsulate another protocol");
5424 bpf_error("moprc does not encapsulate another protocol");
5428 bpf_error("mopdl does not encapsulate another protocol");
5432 return gen_linktype(v
);
5435 bpf_error("'udp proto' is bogus");
5439 bpf_error("'tcp proto' is bogus");
5443 bpf_error("'sctp proto' is bogus");
5447 bpf_error("'icmp proto' is bogus");
5451 bpf_error("'igmp proto' is bogus");
5455 bpf_error("'igrp proto' is bogus");
5459 bpf_error("'pim proto' is bogus");
5463 bpf_error("'vrrp proto' is bogus");
5468 b0
= gen_linktype(ETHERTYPE_IPV6
);
5470 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5472 b1
= gen_protochain(v
, Q_IPV6
);
5478 bpf_error("'icmp6 proto' is bogus");
5482 bpf_error("'ah proto' is bogus");
5485 bpf_error("'ah proto' is bogus");
5488 bpf_error("'stp proto' is bogus");
5491 bpf_error("'ipx proto' is bogus");
5494 bpf_error("'netbeui proto' is bogus");
5497 bpf_error("'radio proto' is bogus");
5508 register const char *name
;
5511 int proto
= q
.proto
;
5515 bpf_u_int32 mask
, addr
;
5517 bpf_u_int32
**alist
;
5520 struct sockaddr_in
*sin4
;
5521 struct sockaddr_in6
*sin6
;
5522 struct addrinfo
*res
, *res0
;
5523 struct in6_addr mask128
;
5525 struct block
*b
, *tmp
;
5526 int port
, real_proto
;
5532 addr
= pcap_nametonetaddr(name
);
5534 bpf_error("unknown network '%s'", name
);
5535 /* Left justify network addr and calculate its network mask */
5537 while (addr
&& (addr
& 0xff000000) == 0) {
5541 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5545 if (proto
== Q_LINK
) {
5549 eaddr
= pcap_ether_hostton(name
);
5552 "unknown ether host '%s'", name
);
5553 b
= gen_ehostop(eaddr
, dir
);
5558 eaddr
= pcap_ether_hostton(name
);
5561 "unknown FDDI host '%s'", name
);
5562 b
= gen_fhostop(eaddr
, dir
);
5567 eaddr
= pcap_ether_hostton(name
);
5570 "unknown token ring host '%s'", name
);
5571 b
= gen_thostop(eaddr
, dir
);
5575 case DLT_IEEE802_11
:
5576 case DLT_IEEE802_11_RADIO_AVS
:
5577 case DLT_IEEE802_11_RADIO
:
5578 case DLT_PRISM_HEADER
:
5580 eaddr
= pcap_ether_hostton(name
);
5583 "unknown 802.11 host '%s'", name
);
5584 b
= gen_wlanhostop(eaddr
, dir
);
5588 case DLT_IP_OVER_FC
:
5589 eaddr
= pcap_ether_hostton(name
);
5592 "unknown Fibre Channel host '%s'", name
);
5593 b
= gen_ipfchostop(eaddr
, dir
);
5602 * Check that the packet doesn't begin
5603 * with an LE Control marker. (We've
5604 * already generated a test for LANE.)
5606 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5610 eaddr
= pcap_ether_hostton(name
);
5613 "unknown ether host '%s'", name
);
5614 b
= gen_ehostop(eaddr
, dir
);
5620 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5621 } else if (proto
== Q_DECNET
) {
5622 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5624 * I don't think DECNET hosts can be multihomed, so
5625 * there is no need to build up a list of addresses
5627 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5630 alist
= pcap_nametoaddr(name
);
5631 if (alist
== NULL
|| *alist
== NULL
)
5632 bpf_error("unknown host '%s'", name
);
5634 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5636 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5638 tmp
= gen_host(**alist
++, 0xffffffff,
5639 tproto
, dir
, q
.addr
);
5645 memset(&mask128
, 0xff, sizeof(mask128
));
5646 res0
= res
= pcap_nametoaddrinfo(name
);
5648 bpf_error("unknown host '%s'", name
);
5650 tproto
= tproto6
= proto
;
5651 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5655 for (res
= res0
; res
; res
= res
->ai_next
) {
5656 switch (res
->ai_family
) {
5658 if (tproto
== Q_IPV6
)
5661 sin4
= (struct sockaddr_in
*)
5663 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5664 0xffffffff, tproto
, dir
, q
.addr
);
5667 if (tproto6
== Q_IP
)
5670 sin6
= (struct sockaddr_in6
*)
5672 tmp
= gen_host6(&sin6
->sin6_addr
,
5673 &mask128
, tproto6
, dir
, q
.addr
);
5684 bpf_error("unknown host '%s'%s", name
,
5685 (proto
== Q_DEFAULT
)
5687 : " for specified address family");
5694 if (proto
!= Q_DEFAULT
&&
5695 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5696 bpf_error("illegal qualifier of 'port'");
5697 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5698 bpf_error("unknown port '%s'", name
);
5699 if (proto
== Q_UDP
) {
5700 if (real_proto
== IPPROTO_TCP
)
5701 bpf_error("port '%s' is tcp", name
);
5702 else if (real_proto
== IPPROTO_SCTP
)
5703 bpf_error("port '%s' is sctp", name
);
5705 /* override PROTO_UNDEF */
5706 real_proto
= IPPROTO_UDP
;
5708 if (proto
== Q_TCP
) {
5709 if (real_proto
== IPPROTO_UDP
)
5710 bpf_error("port '%s' is udp", name
);
5712 else if (real_proto
== IPPROTO_SCTP
)
5713 bpf_error("port '%s' is sctp", name
);
5715 /* override PROTO_UNDEF */
5716 real_proto
= IPPROTO_TCP
;
5718 if (proto
== Q_SCTP
) {
5719 if (real_proto
== IPPROTO_UDP
)
5720 bpf_error("port '%s' is udp", name
);
5722 else if (real_proto
== IPPROTO_TCP
)
5723 bpf_error("port '%s' is tcp", name
);
5725 /* override PROTO_UNDEF */
5726 real_proto
= IPPROTO_SCTP
;
5729 return gen_port(port
, real_proto
, dir
);
5731 b
= gen_port(port
, real_proto
, dir
);
5732 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5737 if (proto
!= Q_DEFAULT
&&
5738 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5739 bpf_error("illegal qualifier of 'portrange'");
5740 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5741 bpf_error("unknown port in range '%s'", name
);
5742 if (proto
== Q_UDP
) {
5743 if (real_proto
== IPPROTO_TCP
)
5744 bpf_error("port in range '%s' is tcp", name
);
5745 else if (real_proto
== IPPROTO_SCTP
)
5746 bpf_error("port in range '%s' is sctp", name
);
5748 /* override PROTO_UNDEF */
5749 real_proto
= IPPROTO_UDP
;
5751 if (proto
== Q_TCP
) {
5752 if (real_proto
== IPPROTO_UDP
)
5753 bpf_error("port in range '%s' is udp", name
);
5754 else if (real_proto
== IPPROTO_SCTP
)
5755 bpf_error("port in range '%s' is sctp", name
);
5757 /* override PROTO_UNDEF */
5758 real_proto
= IPPROTO_TCP
;
5760 if (proto
== Q_SCTP
) {
5761 if (real_proto
== IPPROTO_UDP
)
5762 bpf_error("port in range '%s' is udp", name
);
5763 else if (real_proto
== IPPROTO_TCP
)
5764 bpf_error("port in range '%s' is tcp", name
);
5766 /* override PROTO_UNDEF */
5767 real_proto
= IPPROTO_SCTP
;
5770 return gen_portrange(port1
, port2
, real_proto
, dir
);
5772 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5773 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5779 eaddr
= pcap_ether_hostton(name
);
5781 bpf_error("unknown ether host: %s", name
);
5783 alist
= pcap_nametoaddr(name
);
5784 if (alist
== NULL
|| *alist
== NULL
)
5785 bpf_error("unknown host '%s'", name
);
5786 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5790 bpf_error("'gateway' not supported in this configuration");
5794 real_proto
= lookup_proto(name
, proto
);
5795 if (real_proto
>= 0)
5796 return gen_proto(real_proto
, proto
, dir
);
5798 bpf_error("unknown protocol: %s", name
);
5801 real_proto
= lookup_proto(name
, proto
);
5802 if (real_proto
>= 0)
5803 return gen_protochain(real_proto
, proto
, dir
);
5805 bpf_error("unknown protocol: %s", name
);
5816 gen_mcode(s1
, s2
, masklen
, q
)
5817 register const char *s1
, *s2
;
5818 register int masklen
;
5821 register int nlen
, mlen
;
5824 nlen
= __pcap_atoin(s1
, &n
);
5825 /* Promote short ipaddr */
5829 mlen
= __pcap_atoin(s2
, &m
);
5830 /* Promote short ipaddr */
5833 bpf_error("non-network bits set in \"%s mask %s\"",
5836 /* Convert mask len to mask */
5838 bpf_error("mask length must be <= 32");
5841 * X << 32 is not guaranteed by C to be 0; it's
5846 m
= 0xffffffff << (32 - masklen
);
5848 bpf_error("non-network bits set in \"%s/%d\"",
5855 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5858 bpf_error("Mask syntax for networks only");
5867 register const char *s
;
5872 int proto
= q
.proto
;
5878 else if (q
.proto
== Q_DECNET
)
5879 vlen
= __pcap_atodn(s
, &v
);
5881 vlen
= __pcap_atoin(s
, &v
);
5888 if (proto
== Q_DECNET
)
5889 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5890 else if (proto
== Q_LINK
) {
5891 bpf_error("illegal link layer address");
5894 if (s
== NULL
&& q
.addr
== Q_NET
) {
5895 /* Promote short net number */
5896 while (v
&& (v
& 0xff000000) == 0) {
5901 /* Promote short ipaddr */
5905 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5910 proto
= IPPROTO_UDP
;
5911 else if (proto
== Q_TCP
)
5912 proto
= IPPROTO_TCP
;
5913 else if (proto
== Q_SCTP
)
5914 proto
= IPPROTO_SCTP
;
5915 else if (proto
== Q_DEFAULT
)
5916 proto
= PROTO_UNDEF
;
5918 bpf_error("illegal qualifier of 'port'");
5921 return gen_port((int)v
, proto
, dir
);
5925 b
= gen_port((int)v
, proto
, dir
);
5926 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5933 proto
= IPPROTO_UDP
;
5934 else if (proto
== Q_TCP
)
5935 proto
= IPPROTO_TCP
;
5936 else if (proto
== Q_SCTP
)
5937 proto
= IPPROTO_SCTP
;
5938 else if (proto
== Q_DEFAULT
)
5939 proto
= PROTO_UNDEF
;
5941 bpf_error("illegal qualifier of 'portrange'");
5944 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5948 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5949 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5955 bpf_error("'gateway' requires a name");
5959 return gen_proto((int)v
, proto
, dir
);
5962 return gen_protochain((int)v
, proto
, dir
);
5977 gen_mcode6(s1
, s2
, masklen
, q
)
5978 register const char *s1
, *s2
;
5979 register int masklen
;
5982 struct addrinfo
*res
;
5983 struct in6_addr
*addr
;
5984 struct in6_addr mask
;
5989 bpf_error("no mask %s supported", s2
);
5991 res
= pcap_nametoaddrinfo(s1
);
5993 bpf_error("invalid ip6 address %s", s1
);
5995 bpf_error("%s resolved to multiple address", s1
);
5996 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5998 if (sizeof(mask
) * 8 < masklen
)
5999 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6000 memset(&mask
, 0, sizeof(mask
));
6001 memset(&mask
, 0xff, masklen
/ 8);
6003 mask
.s6_addr
[masklen
/ 8] =
6004 (0xff << (8 - masklen
% 8)) & 0xff;
6007 a
= (u_int32_t
*)addr
;
6008 m
= (u_int32_t
*)&mask
;
6009 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6010 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6011 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6019 bpf_error("Mask syntax for networks only");
6023 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6028 bpf_error("invalid qualifier against IPv6 address");
6037 register const u_char
*eaddr
;
6040 struct block
*b
, *tmp
;
6042 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6045 return gen_ehostop(eaddr
, (int)q
.dir
);
6047 return gen_fhostop(eaddr
, (int)q
.dir
);
6049 return gen_thostop(eaddr
, (int)q
.dir
);
6050 case DLT_IEEE802_11
:
6051 case DLT_IEEE802_11_RADIO_AVS
:
6052 case DLT_IEEE802_11_RADIO
:
6053 case DLT_PRISM_HEADER
:
6055 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6059 * Check that the packet doesn't begin with an
6060 * LE Control marker. (We've already generated
6063 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6068 * Now check the MAC address.
6070 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6075 case DLT_IP_OVER_FC
:
6076 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6078 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6082 bpf_error("ethernet address used in non-ether expression");
6089 struct slist
*s0
, *s1
;
6092 * This is definitely not the best way to do this, but the
6093 * lists will rarely get long.
6100 static struct slist
*
6106 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6111 static struct slist
*
6117 s
= new_stmt(BPF_LD
|BPF_MEM
);
6123 * Modify "index" to use the value stored into its register as an
6124 * offset relative to the beginning of the header for the protocol
6125 * "proto", and allocate a register and put an item "size" bytes long
6126 * (1, 2, or 4) at that offset into that register, making it the register
6130 gen_load(proto
, inst
, size
)
6135 struct slist
*s
, *tmp
;
6137 int regno
= alloc_reg();
6139 free_reg(inst
->regno
);
6143 bpf_error("data size must be 1, 2, or 4");
6159 bpf_error("unsupported index operation");
6163 * The offset is relative to the beginning of the packet
6164 * data, if we have a radio header. (If we don't, this
6167 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6168 linktype
!= DLT_IEEE802_11_RADIO
&&
6169 linktype
!= DLT_PRISM_HEADER
)
6170 bpf_error("radio information not present in capture");
6173 * Load into the X register the offset computed into the
6174 * register specifed by "index".
6176 s
= xfer_to_x(inst
);
6179 * Load the item at that offset.
6181 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6183 sappend(inst
->s
, s
);
6188 * The offset is relative to the beginning of
6189 * the link-layer header.
6191 * XXX - what about ATM LANE? Should the index be
6192 * relative to the beginning of the AAL5 frame, so
6193 * that 0 refers to the beginning of the LE Control
6194 * field, or relative to the beginning of the LAN
6195 * frame, so that 0 refers, for Ethernet LANE, to
6196 * the beginning of the destination address?
6198 s
= gen_llprefixlen();
6201 * If "s" is non-null, it has code to arrange that the
6202 * X register contains the length of the prefix preceding
6203 * the link-layer header. Add to it the offset computed
6204 * into the register specified by "index", and move that
6205 * into the X register. Otherwise, just load into the X
6206 * register the offset computed into the register specifed
6210 sappend(s
, xfer_to_a(inst
));
6211 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6212 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6214 s
= xfer_to_x(inst
);
6217 * Load the item at the sum of the offset we've put in the
6218 * X register and the offset of the start of the link
6219 * layer header (which is 0 if the radio header is
6220 * variable-length; that header length is what we put
6221 * into the X register and then added to the index).
6223 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6226 sappend(inst
->s
, s
);
6242 * The offset is relative to the beginning of
6243 * the network-layer header.
6244 * XXX - are there any cases where we want
6247 s
= gen_off_macpl();
6250 * If "s" is non-null, it has code to arrange that the
6251 * X register contains the offset of the MAC-layer
6252 * payload. Add to it the offset computed into the
6253 * register specified by "index", and move that into
6254 * the X register. Otherwise, just load into the X
6255 * register the offset computed into the register specifed
6259 sappend(s
, xfer_to_a(inst
));
6260 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6261 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6263 s
= xfer_to_x(inst
);
6266 * Load the item at the sum of the offset we've put in the
6267 * X register, the offset of the start of the network
6268 * layer header from the beginning of the MAC-layer
6269 * payload, and the purported offset of the start of the
6270 * MAC-layer payload (which might be 0 if there's a
6271 * variable-length prefix before the link-layer header
6272 * or the link-layer header itself is variable-length;
6273 * the variable-length offset of the start of the
6274 * MAC-layer payload is what we put into the X register
6275 * and then added to the index).
6277 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6278 tmp
->s
.k
= off_macpl
+ off_nl
;
6280 sappend(inst
->s
, s
);
6283 * Do the computation only if the packet contains
6284 * the protocol in question.
6286 b
= gen_proto_abbrev(proto
);
6288 gen_and(inst
->b
, b
);
6301 * The offset is relative to the beginning of
6302 * the transport-layer header.
6304 * Load the X register with the length of the IPv4 header
6305 * (plus the offset of the link-layer header, if it's
6306 * a variable-length header), in bytes.
6308 * XXX - are there any cases where we want
6310 * XXX - we should, if we're built with
6311 * IPv6 support, generate code to load either
6312 * IPv4, IPv6, or both, as appropriate.
6314 s
= gen_loadx_iphdrlen();
6317 * The X register now contains the sum of the length
6318 * of any variable-length header preceding the link-layer
6319 * header, any variable-length link-layer header, and the
6320 * length of the network-layer header.
6322 * Load into the A register the offset relative to
6323 * the beginning of the transport layer header,
6324 * add the X register to that, move that to the
6325 * X register, and load with an offset from the
6326 * X register equal to the offset of the network
6327 * layer header relative to the beginning of
6328 * the MAC-layer payload plus the fixed-length
6329 * portion of the offset of the MAC-layer payload
6330 * from the beginning of the raw packet data.
6332 sappend(s
, xfer_to_a(inst
));
6333 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6334 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6335 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6336 tmp
->s
.k
= off_macpl
+ off_nl
;
6337 sappend(inst
->s
, s
);
6340 * Do the computation only if the packet contains
6341 * the protocol in question - which is true only
6342 * if this is an IP datagram and is the first or
6343 * only fragment of that datagram.
6345 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6347 gen_and(inst
->b
, b
);
6349 gen_and(gen_proto_abbrev(Q_IP
), b
);
6355 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6359 inst
->regno
= regno
;
6360 s
= new_stmt(BPF_ST
);
6362 sappend(inst
->s
, s
);
6368 gen_relation(code
, a0
, a1
, reversed
)
6370 struct arth
*a0
, *a1
;
6373 struct slist
*s0
, *s1
, *s2
;
6374 struct block
*b
, *tmp
;
6378 if (code
== BPF_JEQ
) {
6379 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6380 b
= new_block(JMP(code
));
6384 b
= new_block(BPF_JMP
|code
|BPF_X
);
6390 sappend(a0
->s
, a1
->s
);
6394 free_reg(a0
->regno
);
6395 free_reg(a1
->regno
);
6397 /* 'and' together protocol checks */
6400 gen_and(a0
->b
, tmp
= a1
->b
);
6416 int regno
= alloc_reg();
6417 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6420 s
= new_stmt(BPF_LD
|BPF_LEN
);
6421 s
->next
= new_stmt(BPF_ST
);
6422 s
->next
->s
.k
= regno
;
6437 a
= (struct arth
*)newchunk(sizeof(*a
));
6441 s
= new_stmt(BPF_LD
|BPF_IMM
);
6443 s
->next
= new_stmt(BPF_ST
);
6459 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6462 s
= new_stmt(BPF_ST
);
6470 gen_arth(code
, a0
, a1
)
6472 struct arth
*a0
, *a1
;
6474 struct slist
*s0
, *s1
, *s2
;
6478 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6483 sappend(a0
->s
, a1
->s
);
6485 free_reg(a0
->regno
);
6486 free_reg(a1
->regno
);
6488 s0
= new_stmt(BPF_ST
);
6489 a0
->regno
= s0
->s
.k
= alloc_reg();
6496 * Here we handle simple allocation of the scratch registers.
6497 * If too many registers are alloc'd, the allocator punts.
6499 static int regused
[BPF_MEMWORDS
];
6503 * Initialize the table of used registers and the current register.
6509 memset(regused
, 0, sizeof regused
);
6513 * Return the next free register.
6518 int n
= BPF_MEMWORDS
;
6521 if (regused
[curreg
])
6522 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6524 regused
[curreg
] = 1;
6528 bpf_error("too many registers needed to evaluate expression");
6534 * Return a register to the table so it can
6544 static struct block
*
6551 s
= new_stmt(BPF_LD
|BPF_LEN
);
6552 b
= new_block(JMP(jmp
));
6563 return gen_len(BPF_JGE
, n
);
6567 * Actually, this is less than or equal.
6575 b
= gen_len(BPF_JGT
, n
);
6582 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6583 * the beginning of the link-layer header.
6584 * XXX - that means you can't test values in the radiotap header, but
6585 * as that header is difficult if not impossible to parse generally
6586 * without a loop, that might not be a severe problem. A new keyword
6587 * "radio" could be added for that, although what you'd really want
6588 * would be a way of testing particular radio header values, which
6589 * would generate code appropriate to the radio header in question.
6592 gen_byteop(op
, idx
, val
)
6603 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6606 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6610 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6614 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6618 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6622 b
= new_block(JMP(BPF_JEQ
));
6629 static u_char abroadcast
[] = { 0x0 };
6632 gen_broadcast(proto
)
6635 bpf_u_int32 hostmask
;
6636 struct block
*b0
, *b1
, *b2
;
6637 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6645 case DLT_ARCNET_LINUX
:
6646 return gen_ahostop(abroadcast
, Q_DST
);
6648 return gen_ehostop(ebroadcast
, Q_DST
);
6650 return gen_fhostop(ebroadcast
, Q_DST
);
6652 return gen_thostop(ebroadcast
, Q_DST
);
6653 case DLT_IEEE802_11
:
6654 case DLT_IEEE802_11_RADIO_AVS
:
6655 case DLT_IEEE802_11_RADIO
:
6657 case DLT_PRISM_HEADER
:
6658 return gen_wlanhostop(ebroadcast
, Q_DST
);
6659 case DLT_IP_OVER_FC
:
6660 return gen_ipfchostop(ebroadcast
, Q_DST
);
6664 * Check that the packet doesn't begin with an
6665 * LE Control marker. (We've already generated
6668 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6673 * Now check the MAC address.
6675 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6681 bpf_error("not a broadcast link");
6686 b0
= gen_linktype(ETHERTYPE_IP
);
6687 hostmask
= ~netmask
;
6688 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6689 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6690 (bpf_int32
)(~0 & hostmask
), hostmask
);
6695 bpf_error("only link-layer/IP broadcast filters supported");
6701 * Generate code to test the low-order bit of a MAC address (that's
6702 * the bottom bit of the *first* byte).
6704 static struct block
*
6705 gen_mac_multicast(offset
)
6708 register struct block
*b0
;
6709 register struct slist
*s
;
6711 /* link[offset] & 1 != 0 */
6712 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6713 b0
= new_block(JMP(BPF_JSET
));
6720 gen_multicast(proto
)
6723 register struct block
*b0
, *b1
, *b2
;
6724 register struct slist
*s
;
6732 case DLT_ARCNET_LINUX
:
6733 /* all ARCnet multicasts use the same address */
6734 return gen_ahostop(abroadcast
, Q_DST
);
6736 /* ether[0] & 1 != 0 */
6737 return gen_mac_multicast(0);
6740 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6742 * XXX - was that referring to bit-order issues?
6744 /* fddi[1] & 1 != 0 */
6745 return gen_mac_multicast(1);
6747 /* tr[2] & 1 != 0 */
6748 return gen_mac_multicast(2);
6749 case DLT_IEEE802_11
:
6750 case DLT_IEEE802_11_RADIO_AVS
:
6752 case DLT_IEEE802_11_RADIO
:
6753 case DLT_PRISM_HEADER
:
6757 * For control frames, there is no DA.
6759 * For management frames, DA is at an
6760 * offset of 4 from the beginning of
6763 * For data frames, DA is at an offset
6764 * of 4 from the beginning of the packet
6765 * if To DS is clear and at an offset of
6766 * 16 from the beginning of the packet
6771 * Generate the tests to be done for data frames.
6773 * First, check for To DS set, i.e. "link[1] & 0x01".
6775 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6776 b1
= new_block(JMP(BPF_JSET
));
6777 b1
->s
.k
= 0x01; /* To DS */
6781 * If To DS is set, the DA is at 16.
6783 b0
= gen_mac_multicast(16);
6787 * Now, check for To DS not set, i.e. check
6788 * "!(link[1] & 0x01)".
6790 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6791 b2
= new_block(JMP(BPF_JSET
));
6792 b2
->s
.k
= 0x01; /* To DS */
6797 * If To DS is not set, the DA is at 4.
6799 b1
= gen_mac_multicast(4);
6803 * Now OR together the last two checks. That gives
6804 * the complete set of checks for data frames.
6809 * Now check for a data frame.
6810 * I.e, check "link[0] & 0x08".
6812 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6813 b1
= new_block(JMP(BPF_JSET
));
6818 * AND that with the checks done for data frames.
6823 * If the high-order bit of the type value is 0, this
6824 * is a management frame.
6825 * I.e, check "!(link[0] & 0x08)".
6827 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6828 b2
= new_block(JMP(BPF_JSET
));
6834 * For management frames, the DA is at 4.
6836 b1
= gen_mac_multicast(4);
6840 * OR that with the checks done for data frames.
6841 * That gives the checks done for management and
6847 * If the low-order bit of the type value is 1,
6848 * this is either a control frame or a frame
6849 * with a reserved type, and thus not a
6852 * I.e., check "!(link[0] & 0x04)".
6854 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6855 b1
= new_block(JMP(BPF_JSET
));
6861 * AND that with the checks for data and management
6866 case DLT_IP_OVER_FC
:
6867 b0
= gen_mac_multicast(2);
6872 * Check that the packet doesn't begin with an
6873 * LE Control marker. (We've already generated
6876 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6880 /* ether[off_mac] & 1 != 0 */
6881 b0
= gen_mac_multicast(off_mac
);
6889 /* Link not known to support multicasts */
6893 b0
= gen_linktype(ETHERTYPE_IP
);
6894 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6900 b0
= gen_linktype(ETHERTYPE_IPV6
);
6901 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6906 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6912 * generate command for inbound/outbound. It's here so we can
6913 * make it link-type specific. 'dir' = 0 implies "inbound",
6914 * = 1 implies "outbound".
6920 register struct block
*b0
;
6923 * Only some data link types support inbound/outbound qualifiers.
6927 b0
= gen_relation(BPF_JEQ
,
6928 gen_load(Q_LINK
, gen_loadi(0), 1),
6936 * Match packets sent by this machine.
6938 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6941 * Match packets sent to this machine.
6942 * (No broadcast or multicast packets, or
6943 * packets sent to some other machine and
6944 * received promiscuously.)
6946 * XXX - packets sent to other machines probably
6947 * shouldn't be matched, but what about broadcast
6948 * or multicast packets we received?
6950 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6954 #ifdef HAVE_NET_PFVAR_H
6956 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6957 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6963 /* match outgoing packets */
6964 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6966 /* match incoming packets */
6967 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6971 case DLT_JUNIPER_MFR
:
6972 case DLT_JUNIPER_MLFR
:
6973 case DLT_JUNIPER_MLPPP
:
6974 case DLT_JUNIPER_ATM1
:
6975 case DLT_JUNIPER_ATM2
:
6976 case DLT_JUNIPER_PPPOE
:
6977 case DLT_JUNIPER_PPPOE_ATM
:
6978 case DLT_JUNIPER_GGSN
:
6979 case DLT_JUNIPER_ES
:
6980 case DLT_JUNIPER_MONITOR
:
6981 case DLT_JUNIPER_SERVICES
:
6982 case DLT_JUNIPER_ETHER
:
6983 case DLT_JUNIPER_PPP
:
6984 case DLT_JUNIPER_FRELAY
:
6985 case DLT_JUNIPER_CHDLC
:
6986 case DLT_JUNIPER_VP
:
6987 case DLT_JUNIPER_ST
:
6988 case DLT_JUNIPER_ISM
:
6989 /* juniper flags (including direction) are stored
6990 * the byte after the 3-byte magic number */
6992 /* match outgoing packets */
6993 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6995 /* match incoming packets */
6996 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7001 bpf_error("inbound/outbound not supported on linktype %d",
7009 #ifdef HAVE_NET_PFVAR_H
7010 /* PF firewall log matched interface */
7012 gen_pf_ifname(const char *ifname
)
7017 if (linktype
!= DLT_PFLOG
) {
7018 bpf_error("ifname supported only on PF linktype");
7021 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7022 off
= offsetof(struct pfloghdr
, ifname
);
7023 if (strlen(ifname
) >= len
) {
7024 bpf_error("ifname interface names can only be %d characters",
7028 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7032 /* PF firewall log ruleset name */
7034 gen_pf_ruleset(char *ruleset
)
7038 if (linktype
!= DLT_PFLOG
) {
7039 bpf_error("ruleset supported only on PF linktype");
7043 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7044 bpf_error("ruleset names can only be %ld characters",
7045 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7049 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7050 strlen(ruleset
), (const u_char
*)ruleset
);
7054 /* PF firewall log rule number */
7060 if (linktype
!= DLT_PFLOG
) {
7061 bpf_error("rnr supported only on PF linktype");
7065 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7070 /* PF firewall log sub-rule number */
7072 gen_pf_srnr(int srnr
)
7076 if (linktype
!= DLT_PFLOG
) {
7077 bpf_error("srnr supported only on PF linktype");
7081 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7086 /* PF firewall log reason code */
7088 gen_pf_reason(int reason
)
7092 if (linktype
!= DLT_PFLOG
) {
7093 bpf_error("reason supported only on PF linktype");
7097 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7102 /* PF firewall log action */
7104 gen_pf_action(int action
)
7108 if (linktype
!= DLT_PFLOG
) {
7109 bpf_error("action supported only on PF linktype");
7113 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7117 #else /* !HAVE_NET_PFVAR_H */
7119 gen_pf_ifname(const char *ifname
)
7121 bpf_error("libpcap was compiled without pf support");
7127 gen_pf_ruleset(char *ruleset
)
7129 bpf_error("libpcap was compiled on a machine without pf support");
7137 bpf_error("libpcap was compiled on a machine without pf support");
7143 gen_pf_srnr(int srnr
)
7145 bpf_error("libpcap was compiled on a machine without pf support");
7151 gen_pf_reason(int reason
)
7153 bpf_error("libpcap was compiled on a machine without pf support");
7159 gen_pf_action(int action
)
7161 bpf_error("libpcap was compiled on a machine without pf support");
7165 #endif /* HAVE_NET_PFVAR_H */
7167 /* IEEE 802.11 wireless header */
7169 gen_p80211_type(int type
, int mask
)
7175 case DLT_IEEE802_11
:
7176 case DLT_PRISM_HEADER
:
7177 case DLT_IEEE802_11_RADIO_AVS
:
7178 case DLT_IEEE802_11_RADIO
:
7179 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7184 bpf_error("802.11 link-layer types supported only on 802.11");
7192 register const u_char
*eaddr
;
7198 case DLT_ARCNET_LINUX
:
7199 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7201 return (gen_ahostop(eaddr
, (int)q
.dir
));
7203 bpf_error("ARCnet address used in non-arc expression");
7209 bpf_error("aid supported only on ARCnet");
7212 bpf_error("ARCnet address used in non-arc expression");
7217 static struct block
*
7218 gen_ahostop(eaddr
, dir
)
7219 register const u_char
*eaddr
;
7222 register struct block
*b0
, *b1
;
7225 /* src comes first, different from Ethernet */
7227 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7230 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7233 b0
= gen_ahostop(eaddr
, Q_SRC
);
7234 b1
= gen_ahostop(eaddr
, Q_DST
);
7240 b0
= gen_ahostop(eaddr
, Q_SRC
);
7241 b1
= gen_ahostop(eaddr
, Q_DST
);
7250 * support IEEE 802.1Q VLAN trunk over ethernet
7256 struct block
*b0
, *b1
;
7258 /* can't check for VLAN-encapsulated packets inside MPLS */
7259 if (label_stack_depth
> 0)
7260 bpf_error("no VLAN match after MPLS");
7263 * Check for a VLAN packet, and then change the offsets to point
7264 * to the type and data fields within the VLAN packet. Just
7265 * increment the offsets, so that we can support a hierarchy, e.g.
7266 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7269 * XXX - this is a bit of a kludge. If we were to split the
7270 * compiler into a parser that parses an expression and
7271 * generates an expression tree, and a code generator that
7272 * takes an expression tree (which could come from our
7273 * parser or from some other parser) and generates BPF code,
7274 * we could perhaps make the offsets parameters of routines
7275 * and, in the handler for an "AND" node, pass to subnodes
7276 * other than the VLAN node the adjusted offsets.
7278 * This would mean that "vlan" would, instead of changing the
7279 * behavior of *all* tests after it, change only the behavior
7280 * of tests ANDed with it. That would change the documented
7281 * semantics of "vlan", which might break some expressions.
7282 * However, it would mean that "(vlan and ip) or ip" would check
7283 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7284 * checking only for VLAN-encapsulated IP, so that could still
7285 * be considered worth doing; it wouldn't break expressions
7286 * that are of the form "vlan and ..." or "vlan N and ...",
7287 * which I suspect are the most common expressions involving
7288 * "vlan". "vlan or ..." doesn't necessarily do what the user
7289 * would really want, now, as all the "or ..." tests would
7290 * be done assuming a VLAN, even though the "or" could be viewed
7291 * as meaning "or, if this isn't a VLAN packet...".
7298 /* check for VLAN */
7299 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7300 (bpf_int32
)ETHERTYPE_8021Q
);
7302 /* If a specific VLAN is requested, check VLAN id */
7303 if (vlan_num
>= 0) {
7304 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7305 (bpf_int32
)vlan_num
, 0x0fff);
7319 bpf_error("no VLAN support for data link type %d",
7334 struct block
*b0
,*b1
;
7337 * Change the offsets to point to the type and data fields within
7338 * the MPLS packet. Just increment the offsets, so that we
7339 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7340 * capture packets with an outer label of 100000 and an inner
7343 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7347 if (label_stack_depth
> 0) {
7348 /* just match the bottom-of-stack bit clear */
7349 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7352 * Indicate that we're checking MPLS-encapsulated headers,
7353 * to make sure higher level code generators don't try to
7354 * match against IP-related protocols such as Q_ARP, Q_RARP
7359 case DLT_C_HDLC
: /* fall through */
7361 b0
= gen_linktype(ETHERTYPE_MPLS
);
7365 b0
= gen_linktype(PPP_MPLS_UCAST
);
7368 /* FIXME add other DLT_s ...
7369 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7370 * leave it for now */
7373 bpf_error("no MPLS support for data link type %d",
7381 /* If a specific MPLS label is requested, check it */
7382 if (label_num
>= 0) {
7383 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7384 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7385 0xfffff000); /* only compare the first 20 bits */
7392 label_stack_depth
++;
7397 * Support PPPOE discovery and session.
7402 /* check for PPPoE discovery */
7403 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7412 * Test against the PPPoE session link-layer type.
7414 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7417 * Change the offsets to point to the type and data fields within
7418 * the PPP packet, and note that this is PPPoE rather than
7421 * XXX - this is a bit of a kludge. If we were to split the
7422 * compiler into a parser that parses an expression and
7423 * generates an expression tree, and a code generator that
7424 * takes an expression tree (which could come from our
7425 * parser or from some other parser) and generates BPF code,
7426 * we could perhaps make the offsets parameters of routines
7427 * and, in the handler for an "AND" node, pass to subnodes
7428 * other than the PPPoE node the adjusted offsets.
7430 * This would mean that "pppoes" would, instead of changing the
7431 * behavior of *all* tests after it, change only the behavior
7432 * of tests ANDed with it. That would change the documented
7433 * semantics of "pppoes", which might break some expressions.
7434 * However, it would mean that "(pppoes and ip) or ip" would check
7435 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7436 * checking only for VLAN-encapsulated IP, so that could still
7437 * be considered worth doing; it wouldn't break expressions
7438 * that are of the form "pppoes and ..." which I suspect are the
7439 * most common expressions involving "pppoes". "pppoes or ..."
7440 * doesn't necessarily do what the user would really want, now,
7441 * as all the "or ..." tests would be done assuming PPPoE, even
7442 * though the "or" could be viewed as meaning "or, if this isn't
7443 * a PPPoE packet...".
7445 orig_linktype
= off_linktype
; /* save original values */
7450 * The "network-layer" protocol is PPPoE, which has a 6-byte
7451 * PPPoE header, followed by a PPP packet.
7453 * There is no HDLC encapsulation for the PPP packet (it's
7454 * encapsulated in PPPoES instead), so the link-layer type
7455 * starts at the first byte of the PPP packet. For PPPoE,
7456 * that offset is relative to the beginning of the total
7457 * link-layer payload, including any 802.2 LLC header, so
7458 * it's 6 bytes past off_nl.
7460 off_linktype
= off_nl
+ 6;
7463 * The network-layer offsets are relative to the beginning
7464 * of the MAC-layer payload; that's past the 6-byte
7465 * PPPoE header and the 2-byte PPP header.
7468 off_nl_nosnap
= 6+2;
7474 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7486 bpf_error("'vpi' supported only on raw ATM");
7487 if (off_vpi
== (u_int
)-1)
7489 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7495 bpf_error("'vci' supported only on raw ATM");
7496 if (off_vci
== (u_int
)-1)
7498 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7503 if (off_proto
== (u_int
)-1)
7504 abort(); /* XXX - this isn't on FreeBSD */
7505 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7510 if (off_payload
== (u_int
)-1)
7512 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7513 0xffffffff, jtype
, reverse
, jvalue
);
7518 bpf_error("'callref' supported only on raw ATM");
7519 if (off_proto
== (u_int
)-1)
7521 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7522 jtype
, reverse
, jvalue
);
7532 gen_atmtype_abbrev(type
)
7535 struct block
*b0
, *b1
;
7540 /* Get all packets in Meta signalling Circuit */
7542 bpf_error("'metac' supported only on raw ATM");
7543 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7544 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7549 /* Get all packets in Broadcast Circuit*/
7551 bpf_error("'bcc' supported only on raw ATM");
7552 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7553 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7558 /* Get all cells in Segment OAM F4 circuit*/
7560 bpf_error("'oam4sc' supported only on raw ATM");
7561 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7562 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7567 /* Get all cells in End-to-End OAM F4 Circuit*/
7569 bpf_error("'oam4ec' supported only on raw ATM");
7570 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7571 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7576 /* Get all packets in connection Signalling Circuit */
7578 bpf_error("'sc' supported only on raw ATM");
7579 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7580 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7585 /* Get all packets in ILMI Circuit */
7587 bpf_error("'ilmic' supported only on raw ATM");
7588 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7589 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7594 /* Get all LANE packets */
7596 bpf_error("'lane' supported only on raw ATM");
7597 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7600 * Arrange that all subsequent tests assume LANE
7601 * rather than LLC-encapsulated packets, and set
7602 * the offsets appropriately for LANE-encapsulated
7605 * "off_mac" is the offset of the Ethernet header,
7606 * which is 2 bytes past the ATM pseudo-header
7607 * (skipping the pseudo-header and 2-byte LE Client
7608 * field). The other offsets are Ethernet offsets
7609 * relative to "off_mac".
7612 off_mac
= off_payload
+ 2; /* MAC header */
7613 off_linktype
= off_mac
+ 12;
7614 off_macpl
= off_mac
+ 14; /* Ethernet */
7615 off_nl
= 0; /* Ethernet II */
7616 off_nl_nosnap
= 3; /* 802.3+802.2 */
7620 /* Get all LLC-encapsulated packets */
7622 bpf_error("'llc' supported only on raw ATM");
7623 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7634 * Filtering for MTP2 messages based on li value
7635 * FISU, length is null
7636 * LSSU, length is 1 or 2
7637 * MSU, length is 3 or more
7640 gen_mtp2type_abbrev(type
)
7643 struct block
*b0
, *b1
;
7648 if ( (linktype
!= DLT_MTP2
) &&
7649 (linktype
!= DLT_ERF
) &&
7650 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7651 bpf_error("'fisu' supported only on MTP2");
7652 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7653 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7657 if ( (linktype
!= DLT_MTP2
) &&
7658 (linktype
!= DLT_ERF
) &&
7659 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7660 bpf_error("'lssu' supported only on MTP2");
7661 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7662 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7667 if ( (linktype
!= DLT_MTP2
) &&
7668 (linktype
!= DLT_ERF
) &&
7669 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7670 bpf_error("'msu' supported only on MTP2");
7671 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7681 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7688 bpf_u_int32 val1
, val2
, val3
;
7690 switch (mtp3field
) {
7693 if (off_sio
== (u_int
)-1)
7694 bpf_error("'sio' supported only on SS7");
7695 /* sio coded on 1 byte so max value 255 */
7697 bpf_error("sio value %u too big; max value = 255",
7699 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7700 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7704 if (off_opc
== (u_int
)-1)
7705 bpf_error("'opc' supported only on SS7");
7706 /* opc coded on 14 bits so max value 16383 */
7708 bpf_error("opc value %u too big; max value = 16383",
7710 /* the following instructions are made to convert jvalue
7711 * to the form used to write opc in an ss7 message*/
7712 val1
= jvalue
& 0x00003c00;
7714 val2
= jvalue
& 0x000003fc;
7716 val3
= jvalue
& 0x00000003;
7718 jvalue
= val1
+ val2
+ val3
;
7719 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7720 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7724 if (off_dpc
== (u_int
)-1)
7725 bpf_error("'dpc' supported only on SS7");
7726 /* dpc coded on 14 bits so max value 16383 */
7728 bpf_error("dpc value %u too big; max value = 16383",
7730 /* the following instructions are made to convert jvalue
7731 * to the forme used to write dpc in an ss7 message*/
7732 val1
= jvalue
& 0x000000ff;
7734 val2
= jvalue
& 0x00003f00;
7736 jvalue
= val1
+ val2
;
7737 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7738 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7742 if (off_sls
== (u_int
)-1)
7743 bpf_error("'sls' supported only on SS7");
7744 /* sls coded on 4 bits so max value 15 */
7746 bpf_error("sls value %u too big; max value = 15",
7748 /* the following instruction is made to convert jvalue
7749 * to the forme used to write sls in an ss7 message*/
7750 jvalue
= jvalue
<< 4;
7751 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7752 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7761 static struct block
*
7762 gen_msg_abbrev(type
)
7768 * Q.2931 signalling protocol messages for handling virtual circuits
7769 * establishment and teardown
7774 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7778 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7782 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7786 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7790 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7793 case A_RELEASE_DONE
:
7794 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7804 gen_atmmulti_abbrev(type
)
7807 struct block
*b0
, *b1
;
7813 bpf_error("'oam' supported only on raw ATM");
7814 b1
= gen_atmmulti_abbrev(A_OAMF4
);
7819 bpf_error("'oamf4' supported only on raw ATM");
7821 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7822 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7824 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7830 * Get Q.2931 signalling messages for switched
7831 * virtual connection
7834 bpf_error("'connectmsg' supported only on raw ATM");
7835 b0
= gen_msg_abbrev(A_SETUP
);
7836 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7838 b0
= gen_msg_abbrev(A_CONNECT
);
7840 b0
= gen_msg_abbrev(A_CONNECTACK
);
7842 b0
= gen_msg_abbrev(A_RELEASE
);
7844 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7846 b0
= gen_atmtype_abbrev(A_SC
);
7852 bpf_error("'metaconnect' supported only on raw ATM");
7853 b0
= gen_msg_abbrev(A_SETUP
);
7854 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7856 b0
= gen_msg_abbrev(A_CONNECT
);
7858 b0
= gen_msg_abbrev(A_RELEASE
);
7860 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7862 b0
= gen_atmtype_abbrev(A_METAC
);