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.9 2007-11-07 19:33:00 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_compute_vloffsets(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
;
501 * Insert before the statements of the first (root) block any
502 * statements needed to load the lengths of any variable-length
503 * headers into registers.
505 * XXX - a fancier strategy would be to insert those before the
506 * statements of all blocks that use those lengths and that
507 * have no predecessors that use them, so that we only compute
508 * the lengths if we need them. There might be even better
509 * approaches than that.
511 * However, those strategies would be more complicated, and
512 * as we don't generate code to compute a length if the
513 * program has no tests that use the length, and as most
514 * tests will probably use those lengths, we would just
515 * postpone computing the lengths so that it's not done
516 * for tests that fail early, and it's not clear that's
519 insert_compute_vloffsets(p
->head
);
522 * For DLT_PPI captures, generate a check of the per-packet
523 * DLT value to make sure it's DLT_IEEE802_11.
525 ppi_dlt_check
= gen_ppi_dlt_check();
526 if (ppi_dlt_check
!= NULL
)
527 gen_and(ppi_dlt_check
, p
);
529 backpatch(p
, gen_retblk(snaplen
));
530 p
->sense
= !p
->sense
;
531 backpatch(p
, gen_retblk(0));
537 struct block
*b0
, *b1
;
539 backpatch(b0
, b1
->head
);
540 b0
->sense
= !b0
->sense
;
541 b1
->sense
= !b1
->sense
;
543 b1
->sense
= !b1
->sense
;
549 struct block
*b0
, *b1
;
551 b0
->sense
= !b0
->sense
;
552 backpatch(b0
, b1
->head
);
553 b0
->sense
= !b0
->sense
;
562 b
->sense
= !b
->sense
;
565 static struct block
*
566 gen_cmp(offrel
, offset
, size
, v
)
567 enum e_offrel offrel
;
571 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
574 static struct block
*
575 gen_cmp_gt(offrel
, offset
, size
, v
)
576 enum e_offrel offrel
;
580 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
583 static struct block
*
584 gen_cmp_ge(offrel
, offset
, size
, v
)
585 enum e_offrel offrel
;
589 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
592 static struct block
*
593 gen_cmp_lt(offrel
, offset
, size
, v
)
594 enum e_offrel offrel
;
598 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
601 static struct block
*
602 gen_cmp_le(offrel
, offset
, size
, v
)
603 enum e_offrel offrel
;
607 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
610 static struct block
*
611 gen_mcmp(offrel
, offset
, size
, v
, mask
)
612 enum e_offrel offrel
;
617 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
620 static struct block
*
621 gen_bcmp(offrel
, offset
, size
, v
)
622 enum e_offrel offrel
;
623 register u_int offset
, size
;
624 register const u_char
*v
;
626 register struct block
*b
, *tmp
;
630 register const u_char
*p
= &v
[size
- 4];
631 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
632 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
634 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
641 register const u_char
*p
= &v
[size
- 2];
642 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
644 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
651 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
660 * AND the field of size "size" at offset "offset" relative to the header
661 * specified by "offrel" with "mask", and compare it with the value "v"
662 * with the test specified by "jtype"; if "reverse" is true, the test
663 * should test the opposite of "jtype".
665 static struct block
*
666 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
667 enum e_offrel offrel
;
669 bpf_u_int32 offset
, size
, mask
, jtype
;
672 struct slist
*s
, *s2
;
675 s
= gen_load_a(offrel
, offset
, size
);
677 if (mask
!= 0xffffffff) {
678 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
683 b
= new_block(JMP(jtype
));
686 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
692 * Various code constructs need to know the layout of the data link
693 * layer. These variables give the necessary offsets from the beginning
694 * of the packet data.
698 * This is the offset of the beginning of the link-layer header from
699 * the beginning of the raw packet data.
701 * It's usually 0, except for 802.11 with a fixed-length radio header.
702 * (For 802.11 with a variable-length radio header, we have to generate
703 * code to compute that offset; off_ll is 0 in that case.)
708 * If there's a variable-length header preceding the link-layer header,
709 * "reg_off_ll" is the register number for a register containing the
710 * length of that header, and therefore the offset of the link-layer
711 * header from the beginning of the raw packet data. Otherwise,
712 * "reg_off_ll" is -1.
714 static int reg_off_ll
;
717 * This is the offset of the beginning of the MAC-layer header from
718 * the beginning of the link-layer header.
719 * It's usually 0, except for ATM LANE, where it's the offset, relative
720 * to the beginning of the raw packet data, of the Ethernet header.
722 static u_int off_mac
;
725 * This is the offset of the beginning of the MAC-layer payload,
726 * from the beginning of the raw packet data.
728 * I.e., it's the sum of the length of the link-layer header (without,
729 * for example, any 802.2 LLC header, so it's the MAC-layer
730 * portion of that header), plus any prefix preceding the
733 static u_int off_macpl
;
736 * This is 1 if the offset of the beginning of the MAC-layer payload
737 * from the beginning of the link-layer header is variable-length.
739 static int off_macpl_is_variable
;
742 * If the link layer has variable_length headers, "reg_off_macpl"
743 * is the register number for a register containing the length of the
744 * link-layer header plus the length of any variable-length header
745 * preceding the link-layer header. Otherwise, "reg_off_macpl"
748 static int reg_off_macpl
;
751 * "off_linktype" is the offset to information in the link-layer header
752 * giving the packet type. This offset is relative to the beginning
753 * of the link-layer header (i.e., it doesn't include off_ll).
755 * For Ethernet, it's the offset of the Ethernet type field.
757 * For link-layer types that always use 802.2 headers, it's the
758 * offset of the LLC header.
760 * For PPP, it's the offset of the PPP type field.
762 * For Cisco HDLC, it's the offset of the CHDLC type field.
764 * For BSD loopback, it's the offset of the AF_ value.
766 * For Linux cooked sockets, it's the offset of the type field.
768 * It's set to -1 for no encapsulation, in which case, IP is assumed.
770 static u_int off_linktype
;
773 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
774 * checks to check the PPP header, assumed to follow a LAN-style link-
775 * layer header and a PPPoE session header.
777 static int is_pppoes
= 0;
780 * TRUE if the link layer includes an ATM pseudo-header.
782 static int is_atm
= 0;
785 * TRUE if "lane" appeared in the filter; it causes us to generate
786 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
788 static int is_lane
= 0;
791 * These are offsets for the ATM pseudo-header.
793 static u_int off_vpi
;
794 static u_int off_vci
;
795 static u_int off_proto
;
798 * These are offsets for the MTP2 fields.
803 * These are offsets for the MTP3 fields.
805 static u_int off_sio
;
806 static u_int off_opc
;
807 static u_int off_dpc
;
808 static u_int off_sls
;
811 * This is the offset of the first byte after the ATM pseudo_header,
812 * or -1 if there is no ATM pseudo-header.
814 static u_int off_payload
;
817 * These are offsets to the beginning of the network-layer header.
818 * They are relative to the beginning of the MAC-layer payload (i.e.,
819 * they don't include off_ll or off_macpl).
821 * If the link layer never uses 802.2 LLC:
823 * "off_nl" and "off_nl_nosnap" are the same.
825 * If the link layer always uses 802.2 LLC:
827 * "off_nl" is the offset if there's a SNAP header following
830 * "off_nl_nosnap" is the offset if there's no SNAP header.
832 * If the link layer is Ethernet:
834 * "off_nl" is the offset if the packet is an Ethernet II packet
835 * (we assume no 802.3+802.2+SNAP);
837 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
838 * with an 802.2 header following it.
841 static u_int off_nl_nosnap
;
849 linktype
= pcap_datalink(p
);
851 pcap_fddipad
= p
->fddipad
;
855 * Assume it's not raw ATM with a pseudo-header, for now.
866 * And that we're not doing PPPoE.
871 * And assume we're not doing SS7.
880 * Also assume it's not 802.11.
884 off_macpl_is_variable
= 0;
888 label_stack_depth
= 0;
898 off_nl
= 0; /* XXX in reality, variable! */
899 off_nl_nosnap
= 0; /* no 802.2 LLC */
902 case DLT_ARCNET_LINUX
:
905 off_nl
= 0; /* XXX in reality, variable! */
906 off_nl_nosnap
= 0; /* no 802.2 LLC */
911 off_macpl
= 14; /* Ethernet header length */
912 off_nl
= 0; /* Ethernet II */
913 off_nl_nosnap
= 3; /* 802.3+802.2 */
918 * SLIP doesn't have a link level type. The 16 byte
919 * header is hacked into our SLIP driver.
924 off_nl_nosnap
= 0; /* no 802.2 LLC */
928 /* XXX this may be the same as the DLT_PPP_BSDOS case */
933 off_nl_nosnap
= 0; /* no 802.2 LLC */
941 off_nl_nosnap
= 0; /* no 802.2 LLC */
948 off_nl_nosnap
= 0; /* no 802.2 LLC */
953 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
954 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
958 off_nl_nosnap
= 0; /* no 802.2 LLC */
963 * This does no include the Ethernet header, and
964 * only covers session state.
969 off_nl_nosnap
= 0; /* no 802.2 LLC */
976 off_nl_nosnap
= 0; /* no 802.2 LLC */
981 * FDDI doesn't really have a link-level type field.
982 * We set "off_linktype" to the offset of the LLC header.
984 * To check for Ethernet types, we assume that SSAP = SNAP
985 * is being used and pick out the encapsulated Ethernet type.
986 * XXX - should we generate code to check for SNAP?
990 off_linktype
+= pcap_fddipad
;
992 off_macpl
= 13; /* FDDI MAC header length */
994 off_macpl
+= pcap_fddipad
;
996 off_nl
= 8; /* 802.2+SNAP */
997 off_nl_nosnap
= 3; /* 802.2 */
1002 * Token Ring doesn't really have a link-level type field.
1003 * We set "off_linktype" to the offset of the LLC header.
1005 * To check for Ethernet types, we assume that SSAP = SNAP
1006 * is being used and pick out the encapsulated Ethernet type.
1007 * XXX - should we generate code to check for SNAP?
1009 * XXX - the header is actually variable-length.
1010 * Some various Linux patched versions gave 38
1011 * as "off_linktype" and 40 as "off_nl"; however,
1012 * if a token ring packet has *no* routing
1013 * information, i.e. is not source-routed, the correct
1014 * values are 20 and 22, as they are in the vanilla code.
1016 * A packet is source-routed iff the uppermost bit
1017 * of the first byte of the source address, at an
1018 * offset of 8, has the uppermost bit set. If the
1019 * packet is source-routed, the total number of bytes
1020 * of routing information is 2 plus bits 0x1F00 of
1021 * the 16-bit value at an offset of 14 (shifted right
1022 * 8 - figure out which byte that is).
1025 off_macpl
= 14; /* Token Ring MAC header length */
1026 off_nl
= 8; /* 802.2+SNAP */
1027 off_nl_nosnap
= 3; /* 802.2 */
1030 case DLT_IEEE802_11
:
1032 * 802.11 doesn't really have a link-level type field.
1033 * We set "off_linktype" to the offset of the LLC header.
1035 * To check for Ethernet types, we assume that SSAP = SNAP
1036 * is being used and pick out the encapsulated Ethernet type.
1037 * XXX - should we generate code to check for SNAP?
1039 * XXX - the header is actually variable-length. We
1040 * assume a 24-byte link-layer header, as appears in
1041 * data frames in networks with no bridges. If the
1042 * fromds and tods 802.11 header bits are both set,
1043 * it's actually supposed to be 30 bytes.
1046 off_macpl
= 0; /* link-layer header is variable-length */
1047 off_macpl_is_variable
= 1;
1048 off_nl
= 8; /* 802.2+SNAP */
1049 off_nl_nosnap
= 3; /* 802.2 */
1052 case DLT_PRISM_HEADER
:
1054 * Same as 802.11, but with an additional header before
1055 * the 802.11 header, containing a bunch of additional
1056 * information including radio-level information.
1058 * The header is 144 bytes long.
1060 * XXX - same variable-length header problem; at least
1061 * the Prism header is fixed-length.
1065 off_macpl
= 0; /* link-layer header is variable-length */
1066 off_macpl_is_variable
= 1;
1067 off_nl
= 8; /* 802.2+SNAP */
1068 off_nl_nosnap
= 3; /* 802.2 */
1071 case DLT_IEEE802_11_RADIO_AVS
:
1073 * Same as 802.11, but with an additional header before
1074 * the 802.11 header, containing a bunch of additional
1075 * information including radio-level information.
1077 * The header is 64 bytes long, at least in its
1078 * current incarnation.
1080 * XXX - same variable-length header problem, only
1081 * more so; this header is also variable-length,
1082 * with the length being the 32-bit big-endian
1083 * number at an offset of 4 from the beginning
1084 * of the radio header. We should handle that the
1085 * same way we handle the length at the beginning
1086 * of the radiotap header.
1088 * XXX - in Linux, do any drivers that supply an AVS
1089 * header supply a link-layer type other than
1090 * ARPHRD_IEEE80211_PRISM? If so, we should map that
1091 * to DLT_IEEE802_11_RADIO_AVS; if not, or if there are
1092 * any drivers that supply an AVS header but supply
1093 * an ARPHRD value of ARPHRD_IEEE80211_PRISM, we'll
1094 * have to check the header in the generated code to
1095 * determine whether it's Prism or AVS.
1099 off_macpl
= 0; /* link-layer header is variable-length */
1100 off_macpl_is_variable
= 1;
1101 off_nl
= 8; /* 802.2+SNAP */
1102 off_nl_nosnap
= 3; /* 802.2 */
1106 * At the moment we treat PPI as normal Radiotap encoded
1107 * packets. The difference is in the function that generates
1108 * the code at the beginning to compute the header length.
1109 * Since this code generator of PPI supports bare 802.11
1110 * encapsulation only (i.e. the encapsulated DLT should be
1111 * DLT_IEEE802_11) we generate code to check for this too.
1114 case DLT_IEEE802_11_RADIO
:
1116 * Same as 802.11, but with an additional header before
1117 * the 802.11 header, containing a bunch of additional
1118 * information including radio-level information.
1120 * The radiotap header is variable length, and we
1121 * generate code to compute its length and store it
1122 * in a register. These offsets are relative to the
1123 * beginning of the 802.11 header.
1126 off_macpl
= 0; /* link-layer header is variable-length */
1127 off_macpl_is_variable
= 1;
1128 off_nl
= 8; /* 802.2+SNAP */
1129 off_nl_nosnap
= 3; /* 802.2 */
1132 case DLT_ATM_RFC1483
:
1133 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1135 * assume routed, non-ISO PDUs
1136 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1138 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1139 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1140 * latter would presumably be treated the way PPPoE
1141 * should be, so you can do "pppoe and udp port 2049"
1142 * or "pppoa and tcp port 80" and have it check for
1143 * PPPo{A,E} and a PPP protocol of IP and....
1146 off_macpl
= 0; /* packet begins with LLC header */
1147 off_nl
= 8; /* 802.2+SNAP */
1148 off_nl_nosnap
= 3; /* 802.2 */
1153 * Full Frontal ATM; you get AALn PDUs with an ATM
1157 off_vpi
= SUNATM_VPI_POS
;
1158 off_vci
= SUNATM_VCI_POS
;
1159 off_proto
= PROTO_POS
;
1160 off_mac
= -1; /* assume LLC-encapsulated, so no MAC-layer header */
1161 off_payload
= SUNATM_PKT_BEGIN_POS
;
1162 off_linktype
= off_payload
;
1163 off_macpl
= off_payload
; /* if LLC-encapsulated */
1164 off_nl
= 8; /* 802.2+SNAP */
1165 off_nl_nosnap
= 3; /* 802.2 */
1172 off_nl_nosnap
= 0; /* no 802.2 LLC */
1175 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1179 off_nl_nosnap
= 0; /* no 802.2 LLC */
1184 * LocalTalk does have a 1-byte type field in the LLAP header,
1185 * but really it just indicates whether there is a "short" or
1186 * "long" DDP packet following.
1191 off_nl_nosnap
= 0; /* no 802.2 LLC */
1194 case DLT_IP_OVER_FC
:
1196 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1197 * link-level type field. We set "off_linktype" to the
1198 * offset of the LLC header.
1200 * To check for Ethernet types, we assume that SSAP = SNAP
1201 * is being used and pick out the encapsulated Ethernet type.
1202 * XXX - should we generate code to check for SNAP? RFC
1203 * 2625 says SNAP should be used.
1207 off_nl
= 8; /* 802.2+SNAP */
1208 off_nl_nosnap
= 3; /* 802.2 */
1213 * XXX - we should set this to handle SNAP-encapsulated
1214 * frames (NLPID of 0x80).
1219 off_nl_nosnap
= 0; /* no 802.2 LLC */
1223 * the only BPF-interesting FRF.16 frames are non-control frames;
1224 * Frame Relay has a variable length link-layer
1225 * so lets start with offset 4 for now and increments later on (FIXME);
1231 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1234 case DLT_APPLE_IP_OVER_IEEE1394
:
1238 off_nl_nosnap
= 0; /* no 802.2 LLC */
1241 case DLT_LINUX_IRDA
:
1243 * Currently, only raw "link[N:M]" filtering is supported.
1253 * Currently, only raw "link[N:M]" filtering is supported.
1261 case DLT_SYMANTEC_FIREWALL
:
1264 off_nl
= 0; /* Ethernet II */
1265 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1268 #ifdef HAVE_NET_PFVAR_H
1271 off_macpl
= PFLOG_HDRLEN
;
1273 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 case DLT_JUNIPER_MFR
:
1278 case DLT_JUNIPER_MLFR
:
1279 case DLT_JUNIPER_MLPPP
:
1280 case DLT_JUNIPER_PPP
:
1281 case DLT_JUNIPER_CHDLC
:
1282 case DLT_JUNIPER_FRELAY
:
1286 off_nl_nosnap
= -1; /* no 802.2 LLC */
1289 case DLT_JUNIPER_ATM1
:
1290 off_linktype
= 4; /* in reality variable between 4-8 */
1291 off_macpl
= 4; /* in reality variable between 4-8 */
1296 case DLT_JUNIPER_ATM2
:
1297 off_linktype
= 8; /* in reality variable between 8-12 */
1298 off_macpl
= 8; /* in reality variable between 8-12 */
1303 /* frames captured on a Juniper PPPoE service PIC
1304 * contain raw ethernet frames */
1305 case DLT_JUNIPER_PPPOE
:
1306 case DLT_JUNIPER_ETHER
:
1309 off_nl
= 18; /* Ethernet II */
1310 off_nl_nosnap
= 21; /* 802.3+802.2 */
1313 case DLT_JUNIPER_PPPOE_ATM
:
1317 off_nl_nosnap
= -1; /* no 802.2 LLC */
1320 case DLT_JUNIPER_GGSN
:
1324 off_nl_nosnap
= -1; /* no 802.2 LLC */
1327 case DLT_JUNIPER_ES
:
1329 off_macpl
= -1; /* not really a network layer but raw IP addresses */
1330 off_nl
= -1; /* not really a network layer but raw IP addresses */
1331 off_nl_nosnap
= -1; /* no 802.2 LLC */
1334 case DLT_JUNIPER_MONITOR
:
1337 off_nl
= 0; /* raw IP/IP6 header */
1338 off_nl_nosnap
= -1; /* no 802.2 LLC */
1341 case DLT_JUNIPER_SERVICES
:
1343 off_macpl
= -1; /* L3 proto location dep. on cookie type */
1344 off_nl
= -1; /* L3 proto location dep. on cookie type */
1345 off_nl_nosnap
= -1; /* no 802.2 LLC */
1348 case DLT_JUNIPER_VP
:
1355 case DLT_JUNIPER_ST
:
1362 case DLT_JUNIPER_ISM
:
1381 case DLT_MTP2_WITH_PHDR
:
1414 case DLT_LINUX_LAPD
:
1416 * Currently, only raw "link[N:M]" filtering is supported.
1426 * Currently, only raw "link[N:M]" filtering is supported.
1434 case DLT_BLUETOOTH_HCI_H4
:
1436 * Currently, only raw "link[N:M]" filtering is supported.
1446 * Currently, only raw "link[N:M]" filtering is supported.
1456 * Currently, only raw "link[N:M]" filtering is supported.
1464 case DLT_IEEE802_15_4_LINUX
:
1466 * Currently, only raw "link[N:M]" filtering is supported.
1474 case DLT_IEEE802_16_MAC_CPS_RADIO
:
1476 * Currently, only raw "link[N:M]" filtering is supported.
1484 case DLT_IEEE802_15_4
:
1486 * Currently, only raw "link[N:M]" filtering is supported.
1496 * Currently, only raw "link[N:M]" filtering is supported.
1506 * Currently, only raw "link[N:M]" filtering is supported.
1516 * Currently, only raw "link[N:M]" filtering is supported.
1524 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
1526 * Currently, only raw "link[N:M]" filtering is supported.
1536 * Currently, only raw "link[N:M]" filtering is supported.
1538 off_linktype
= -1; /* variable, min 15, max 71 steps of 7 */
1540 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1541 off_nl_nosnap
= -1; /* no 802.2 LLC */
1542 off_mac
= 1; /* step over the kiss length byte */
1545 bpf_error("unknown data link type %d", linktype
);
1550 * Load a value relative to the beginning of the link-layer header.
1551 * The link-layer header doesn't necessarily begin at the beginning
1552 * of the packet data; there might be a variable-length prefix containing
1553 * radio information.
1555 static struct slist
*
1556 gen_load_llrel(offset
, size
)
1559 struct slist
*s
, *s2
;
1561 s
= gen_llprefixlen();
1564 * If "s" is non-null, it has code to arrange that the X register
1565 * contains the length of the prefix preceding the link-layer
1568 * Otherwise, the length of the prefix preceding the link-layer
1569 * header is "off_ll".
1573 * There's a variable-length prefix preceding the
1574 * link-layer header. "s" points to a list of statements
1575 * that put the length of that prefix into the X register.
1576 * do an indirect load, to use the X register as an offset.
1578 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1583 * There is no variable-length header preceding the
1584 * link-layer header; add in off_ll, which, if there's
1585 * a fixed-length header preceding the link-layer header,
1586 * is the length of that header.
1588 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1589 s
->s
.k
= offset
+ off_ll
;
1595 * Load a value relative to the beginning of the MAC-layer payload.
1597 static struct slist
*
1598 gen_load_macplrel(offset
, size
)
1601 struct slist
*s
, *s2
;
1603 s
= gen_off_macpl();
1606 * If s is non-null, the offset of the MAC-layer payload is
1607 * variable, and s points to a list of instructions that
1608 * arrange that the X register contains that offset.
1610 * Otherwise, the offset of the MAC-layer payload is constant,
1611 * and is in off_macpl.
1615 * The offset of the MAC-layer payload is in the X
1616 * register. Do an indirect load, to use the X register
1619 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1624 * The offset of the MAC-layer payload is constant,
1625 * and is in off_macpl; load the value at that offset
1626 * plus the specified offset.
1628 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1629 s
->s
.k
= off_macpl
+ offset
;
1635 * Load a value relative to the beginning of the specified header.
1637 static struct slist
*
1638 gen_load_a(offrel
, offset
, size
)
1639 enum e_offrel offrel
;
1642 struct slist
*s
, *s2
;
1647 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1652 s
= gen_load_llrel(offset
, size
);
1656 s
= gen_load_macplrel(offset
, size
);
1660 s
= gen_load_macplrel(off_nl
+ offset
, size
);
1664 s
= gen_load_macplrel(off_nl_nosnap
+ offset
, size
);
1669 * Load the X register with the length of the IPv4 header
1670 * (plus the offset of the link-layer header, if it's
1671 * preceded by a variable-length header such as a radio
1672 * header), in bytes.
1674 s
= gen_loadx_iphdrlen();
1677 * Load the item at {offset of the MAC-layer payload} +
1678 * {offset, relative to the start of the MAC-layer
1679 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1680 * {specified offset}.
1682 * (If the offset of the MAC-layer payload is variable,
1683 * it's included in the value in the X register, and
1686 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1687 s2
->s
.k
= off_macpl
+ off_nl
+ offset
;
1692 s
= gen_load_macplrel(off_nl
+ 40 + offset
, size
);
1703 * Generate code to load into the X register the sum of the length of
1704 * the IPv4 header and any variable-length header preceding the link-layer
1707 static struct slist
*
1708 gen_loadx_iphdrlen()
1710 struct slist
*s
, *s2
;
1712 s
= gen_off_macpl();
1715 * There's a variable-length prefix preceding the
1716 * link-layer header, or the link-layer header is itself
1717 * variable-length. "s" points to a list of statements
1718 * that put the offset of the MAC-layer payload into
1721 * The 4*([k]&0xf) addressing mode can't be used, as we
1722 * don't have a constant offset, so we have to load the
1723 * value in question into the A register and add to it
1724 * the value from the X register.
1726 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1729 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1732 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1737 * The A register now contains the length of the
1738 * IP header. We need to add to it the offset of
1739 * the MAC-layer payload, which is still in the X
1740 * register, and move the result into the X register.
1742 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1743 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1746 * There is no variable-length header preceding the
1747 * link-layer header, and the link-layer header is
1748 * fixed-length; load the length of the IPv4 header,
1749 * which is at an offset of off_nl from the beginning
1750 * of the MAC-layer payload, and thus at an offset
1751 * of off_mac_pl + off_nl from the beginning of the
1754 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1755 s
->s
.k
= off_macpl
+ off_nl
;
1760 static struct block
*
1767 s
= new_stmt(BPF_LD
|BPF_IMM
);
1769 b
= new_block(JMP(BPF_JEQ
));
1775 static inline struct block
*
1778 return gen_uncond(1);
1781 static inline struct block
*
1784 return gen_uncond(0);
1788 * Byte-swap a 32-bit number.
1789 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1790 * big-endian platforms.)
1792 #define SWAPLONG(y) \
1793 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1796 * Generate code to match a particular packet type.
1798 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1799 * value, if <= ETHERMTU. We use that to determine whether to
1800 * match the type/length field or to check the type/length field for
1801 * a value <= ETHERMTU to see whether it's a type field and then do
1802 * the appropriate test.
1804 static struct block
*
1805 gen_ether_linktype(proto
)
1808 struct block
*b0
, *b1
;
1814 case LLCSAP_NETBEUI
:
1816 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1817 * so we check the DSAP and SSAP.
1819 * LLCSAP_IP checks for IP-over-802.2, rather
1820 * than IP-over-Ethernet or IP-over-SNAP.
1822 * XXX - should we check both the DSAP and the
1823 * SSAP, like this, or should we check just the
1824 * DSAP, as we do for other types <= ETHERMTU
1825 * (i.e., other SAP values)?
1827 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1829 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
1830 ((proto
<< 8) | proto
));
1838 * Ethernet_II frames, which are Ethernet
1839 * frames with a frame type of ETHERTYPE_IPX;
1841 * Ethernet_802.3 frames, which are 802.3
1842 * frames (i.e., the type/length field is
1843 * a length field, <= ETHERMTU, rather than
1844 * a type field) with the first two bytes
1845 * after the Ethernet/802.3 header being
1848 * Ethernet_802.2 frames, which are 802.3
1849 * frames with an 802.2 LLC header and
1850 * with the IPX LSAP as the DSAP in the LLC
1853 * Ethernet_SNAP frames, which are 802.3
1854 * frames with an LLC header and a SNAP
1855 * header and with an OUI of 0x000000
1856 * (encapsulated Ethernet) and a protocol
1857 * ID of ETHERTYPE_IPX in the SNAP header.
1859 * XXX - should we generate the same code both
1860 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1864 * This generates code to check both for the
1865 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1867 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1868 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1872 * Now we add code to check for SNAP frames with
1873 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1875 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1879 * Now we generate code to check for 802.3
1880 * frames in general.
1882 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1886 * Now add the check for 802.3 frames before the
1887 * check for Ethernet_802.2 and Ethernet_802.3,
1888 * as those checks should only be done on 802.3
1889 * frames, not on Ethernet frames.
1894 * Now add the check for Ethernet_II frames, and
1895 * do that before checking for the other frame
1898 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1899 (bpf_int32
)ETHERTYPE_IPX
);
1903 case ETHERTYPE_ATALK
:
1904 case ETHERTYPE_AARP
:
1906 * EtherTalk (AppleTalk protocols on Ethernet link
1907 * layer) may use 802.2 encapsulation.
1911 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1912 * we check for an Ethernet type field less than
1913 * 1500, which means it's an 802.3 length field.
1915 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1919 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1920 * SNAP packets with an organization code of
1921 * 0x080007 (Apple, for Appletalk) and a protocol
1922 * type of ETHERTYPE_ATALK (Appletalk).
1924 * 802.2-encapsulated ETHERTYPE_AARP packets are
1925 * SNAP packets with an organization code of
1926 * 0x000000 (encapsulated Ethernet) and a protocol
1927 * type of ETHERTYPE_AARP (Appletalk ARP).
1929 if (proto
== ETHERTYPE_ATALK
)
1930 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1931 else /* proto == ETHERTYPE_AARP */
1932 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1936 * Check for Ethernet encapsulation (Ethertalk
1937 * phase 1?); we just check for the Ethernet
1940 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1946 if (proto
<= ETHERMTU
) {
1948 * This is an LLC SAP value, so the frames
1949 * that match would be 802.2 frames.
1950 * Check that the frame is an 802.2 frame
1951 * (i.e., that the length/type field is
1952 * a length field, <= ETHERMTU) and
1953 * then check the DSAP.
1955 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1957 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1963 * This is an Ethernet type, so compare
1964 * the length/type field with it (if
1965 * the frame is an 802.2 frame, the length
1966 * field will be <= ETHERMTU, and, as
1967 * "proto" is > ETHERMTU, this test
1968 * will fail and the frame won't match,
1969 * which is what we want).
1971 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1978 * Generate code to match a particular packet type.
1980 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1981 * value, if <= ETHERMTU. We use that to determine whether to
1982 * match the type field or to check the type field for the special
1983 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1985 static struct block
*
1986 gen_linux_sll_linktype(proto
)
1989 struct block
*b0
, *b1
;
1995 case LLCSAP_NETBEUI
:
1997 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1998 * so we check the DSAP and SSAP.
2000 * LLCSAP_IP checks for IP-over-802.2, rather
2001 * than IP-over-Ethernet or IP-over-SNAP.
2003 * XXX - should we check both the DSAP and the
2004 * SSAP, like this, or should we check just the
2005 * DSAP, as we do for other types <= ETHERMTU
2006 * (i.e., other SAP values)?
2008 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2009 b1
= gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_int32
)
2010 ((proto
<< 8) | proto
));
2016 * Ethernet_II frames, which are Ethernet
2017 * frames with a frame type of ETHERTYPE_IPX;
2019 * Ethernet_802.3 frames, which have a frame
2020 * type of LINUX_SLL_P_802_3;
2022 * Ethernet_802.2 frames, which are 802.3
2023 * frames with an 802.2 LLC header (i.e, have
2024 * a frame type of LINUX_SLL_P_802_2) and
2025 * with the IPX LSAP as the DSAP in the LLC
2028 * Ethernet_SNAP frames, which are 802.3
2029 * frames with an LLC header and a SNAP
2030 * header and with an OUI of 0x000000
2031 * (encapsulated Ethernet) and a protocol
2032 * ID of ETHERTYPE_IPX in the SNAP header.
2034 * First, do the checks on LINUX_SLL_P_802_2
2035 * frames; generate the check for either
2036 * Ethernet_802.2 or Ethernet_SNAP frames, and
2037 * then put a check for LINUX_SLL_P_802_2 frames
2040 b0
= gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2041 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2043 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2047 * Now check for 802.3 frames and OR that with
2048 * the previous test.
2050 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
2054 * Now add the check for Ethernet_II frames, and
2055 * do that before checking for the other frame
2058 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2059 (bpf_int32
)ETHERTYPE_IPX
);
2063 case ETHERTYPE_ATALK
:
2064 case ETHERTYPE_AARP
:
2066 * EtherTalk (AppleTalk protocols on Ethernet link
2067 * layer) may use 802.2 encapsulation.
2071 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2072 * we check for the 802.2 protocol type in the
2073 * "Ethernet type" field.
2075 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
2078 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2079 * SNAP packets with an organization code of
2080 * 0x080007 (Apple, for Appletalk) and a protocol
2081 * type of ETHERTYPE_ATALK (Appletalk).
2083 * 802.2-encapsulated ETHERTYPE_AARP packets are
2084 * SNAP packets with an organization code of
2085 * 0x000000 (encapsulated Ethernet) and a protocol
2086 * type of ETHERTYPE_AARP (Appletalk ARP).
2088 if (proto
== ETHERTYPE_ATALK
)
2089 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2090 else /* proto == ETHERTYPE_AARP */
2091 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2095 * Check for Ethernet encapsulation (Ethertalk
2096 * phase 1?); we just check for the Ethernet
2099 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2105 if (proto
<= ETHERMTU
) {
2107 * This is an LLC SAP value, so the frames
2108 * that match would be 802.2 frames.
2109 * Check for the 802.2 protocol type
2110 * in the "Ethernet type" field, and
2111 * then check the DSAP.
2113 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2115 b1
= gen_cmp(OR_LINK
, off_macpl
, BPF_B
,
2121 * This is an Ethernet type, so compare
2122 * the length/type field with it (if
2123 * the frame is an 802.2 frame, the length
2124 * field will be <= ETHERMTU, and, as
2125 * "proto" is > ETHERMTU, this test
2126 * will fail and the frame won't match,
2127 * which is what we want).
2129 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2135 static struct slist
*
2136 gen_load_radiotap_llprefixlen()
2138 struct slist
*s1
, *s2
;
2141 * Generate code to load the length of the radiotap header into
2142 * the register assigned to hold that length, if one has been
2143 * assigned. (If one hasn't been assigned, no code we've
2144 * generated uses that prefix, so we don't need to generate any
2147 if (reg_off_ll
!= -1) {
2149 * The 2 bytes at offsets of 2 and 3 from the beginning
2150 * of the radiotap header are the length of the radiotap
2151 * header; unfortunately, it's little-endian, so we have
2152 * to load it a byte at a time and construct the value.
2156 * Load the high-order byte, at an offset of 3, shift it
2157 * left a byte, and put the result in the X register.
2159 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2161 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2164 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2168 * Load the next byte, at an offset of 2, and OR the
2169 * value from the X register into it.
2171 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2174 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2178 * Now allocate a register to hold that value and store
2181 s2
= new_stmt(BPF_ST
);
2182 s2
->s
.k
= reg_off_ll
;
2186 * Now move it into the X register.
2188 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2197 * At the moment we treat PPI as normal Radiotap encoded
2198 * packets. The difference is in the function that generates
2199 * the code at the beginning to compute the header length.
2200 * Since this code generator of PPI supports bare 802.11
2201 * encapsulation only (i.e. the encapsulated DLT should be
2202 * DLT_IEEE802_11) we generate code to check for this too;
2203 * that's done in finish_parse().
2205 static struct slist
*
2206 gen_load_ppi_llprefixlen()
2208 struct slist
*s1
, *s2
;
2211 * Generate code to load the length of the radiotap header
2212 * into the register assigned to hold that length, if one has
2215 if (reg_off_ll
!= -1) {
2217 * The 2 bytes at offsets of 2 and 3 from the beginning
2218 * of the radiotap header are the length of the radiotap
2219 * header; unfortunately, it's little-endian, so we have
2220 * to load it a byte at a time and construct the value.
2224 * Load the high-order byte, at an offset of 3, shift it
2225 * left a byte, and put the result in the X register.
2227 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2229 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2232 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2236 * Load the next byte, at an offset of 2, and OR the
2237 * value from the X register into it.
2239 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2242 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2246 * Now allocate a register to hold that value and store
2249 s2
= new_stmt(BPF_ST
);
2250 s2
->s
.k
= reg_off_ll
;
2254 * Now move it into the X register.
2256 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2265 * Load a value relative to the beginning of the link-layer header after the 802.11
2266 * header, i.e. LLC_SNAP.
2267 * The link-layer header doesn't necessarily begin at the beginning
2268 * of the packet data; there might be a variable-length prefix containing
2269 * radio information.
2271 static struct slist
*
2272 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2275 struct slist
*sjset_data_frame_1
;
2276 struct slist
*sjset_data_frame_2
;
2277 struct slist
*sjset_qos
;
2279 if (reg_off_macpl
== -1) {
2281 * No register has been assigned to the offset of
2282 * the MAC-layer payload, which means nobody needs
2283 * it; don't bother computing it - just return
2284 * what we already have.
2290 * This code is not compatible with the optimizer, as
2291 * we are generating jmp instructions within a normal
2292 * slist of instructions
2297 * If "s" is non-null, it has code to arrange that the X register
2298 * contains the length of the prefix preceding the link-layer
2301 * Otherwise, the length of the prefix preceding the link-layer
2302 * header is "off_ll".
2306 * There is no variable-length header preceding the
2307 * link-layer header.
2309 * Load the length of the fixed-length prefix preceding
2310 * the link-layer header (if any) into the X register,
2311 * and store it in the reg_off_macpl register.
2312 * That length is off_ll.
2314 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2319 * The X register contains the offset of the beginning of the
2320 * link-layer header; add 24, which is the minimum length
2321 * of the MAC header for a data frame, to that, and store it
2322 * in reg_off_macpl, and then load the Frame Control field,
2323 * which is at the offset in the X register, with an indexed load.
2325 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2327 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2330 s2
= new_stmt(BPF_ST
);
2331 s2
->s
.k
= reg_off_macpl
;
2334 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2339 * Check the Frame Control field to see if this is a data frame;
2340 * a data frame has the 0x08 bit (b3) in that field set and the
2341 * 0x04 bit (b2) clear.
2343 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2344 sjset_data_frame_1
->s
.k
= 0x08;
2345 sappend(s
, sjset_data_frame_1
);
2348 * If b3 is set, test b2, otherwise go to the first statement of
2349 * the rest of the program.
2351 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2352 sjset_data_frame_2
->s
.k
= 0x04;
2353 sappend(s
, sjset_data_frame_2
);
2354 sjset_data_frame_1
->s
.jf
= snext
;
2357 * If b2 is not set, this is a data frame; test the QoS bit.
2358 * Otherwise, go to the first statement of the rest of the
2361 sjset_data_frame_2
->s
.jt
= snext
;
2362 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2363 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2364 sappend(s
, sjset_qos
);
2367 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2369 * Otherwise, go to the first statement of the rest of the
2372 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2373 s2
->s
.k
= reg_off_macpl
;
2375 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2378 s2
= new_stmt(BPF_ST
);
2379 s2
->s
.k
= reg_off_macpl
;
2381 sjset_qos
->s
.jf
= snext
;
2387 insert_compute_vloffsets(b
)
2393 * For link-layer types that have a variable-length header
2394 * preceding the link-layer header, generate code to load
2395 * the offset of the link-layer header into the register
2396 * assigned to that offset, if any.
2400 case DLT_IEEE802_11_RADIO
:
2401 s
= gen_load_radiotap_llprefixlen();
2405 s
= gen_load_ppi_llprefixlen();
2414 * For link-layer types that have a variable-length link-layer
2415 * header, generate code to load the offset of the MAC-layer
2416 * payload into the register assigned to that offset, if any.
2420 case DLT_IEEE802_11
:
2421 case DLT_IEEE802_11_RADIO
:
2422 case DLT_IEEE802_11_RADIO_AVS
:
2423 case DLT_PRISM_HEADER
:
2425 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2430 * If we have any offset-loading code, append all the
2431 * existing statements in the block to those statements,
2432 * and make the resulting list the list of statements
2436 sappend(s
, b
->stmts
);
2441 static struct block
*
2442 gen_ppi_dlt_check(void)
2444 struct slist
*s_load_dlt
;
2447 if (linktype
== DLT_PPI
)
2449 /* Create the statements that check for the DLT
2451 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2452 s_load_dlt
->s
.k
= 4;
2454 b
= new_block(JMP(BPF_JEQ
));
2456 b
->stmts
= s_load_dlt
;
2457 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2467 static struct slist
*
2468 gen_radiotap_llprefixlen(void)
2472 if (reg_off_ll
== -1) {
2474 * We haven't yet assigned a register for the length
2475 * of the radiotap header; allocate one.
2477 reg_off_ll
= alloc_reg();
2481 * Load the register containing the radiotap length
2482 * into the X register.
2484 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2485 s
->s
.k
= reg_off_ll
;
2490 * At the moment we treat PPI as normal Radiotap encoded
2491 * packets. The difference is in the function that generates
2492 * the code at the beginning to compute the header length.
2493 * Since this code generator of PPI supports bare 802.11
2494 * encapsulation only (i.e. the encapsulated DLT should be
2495 * DLT_IEEE802_11) we generate code to check for this too.
2497 static struct slist
*
2498 gen_ppi_llprefixlen(void)
2502 if (reg_off_ll
== -1) {
2504 * We haven't yet assigned a register for the length
2505 * of the radiotap header; allocate one.
2507 reg_off_ll
= alloc_reg();
2511 * Load the register containing the radiotap length
2512 * into the X register.
2514 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2515 s
->s
.k
= reg_off_ll
;
2520 * Generate code to compute the link-layer header length, if necessary,
2521 * putting it into the X register, and to return either a pointer to a
2522 * "struct slist" for the list of statements in that code, or NULL if
2523 * no code is necessary.
2525 static struct slist
*
2526 gen_llprefixlen(void)
2531 return gen_ppi_llprefixlen();
2533 case DLT_IEEE802_11_RADIO
:
2534 return gen_radiotap_llprefixlen();
2542 * Generate code to load the register containing the offset of the
2543 * MAC-layer payload into the X register; if no register for that offset
2544 * has been allocated, allocate it first.
2546 static struct slist
*
2551 if (off_macpl_is_variable
) {
2552 if (reg_off_macpl
== -1) {
2554 * We haven't yet assigned a register for the offset
2555 * of the MAC-layer payload; allocate one.
2557 reg_off_macpl
= alloc_reg();
2561 * Load the register containing the offset of the MAC-layer
2562 * payload into the X register.
2564 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2565 s
->s
.k
= reg_off_macpl
;
2569 * That offset isn't variable, so we don't need to
2570 * generate any code.
2577 * Map an Ethernet type to the equivalent PPP type.
2580 ethertype_to_ppptype(proto
)
2590 case ETHERTYPE_IPV6
:
2599 case ETHERTYPE_ATALK
:
2613 * I'm assuming the "Bridging PDU"s that go
2614 * over PPP are Spanning Tree Protocol
2628 * Generate code to match a particular packet type by matching the
2629 * link-layer type field or fields in the 802.2 LLC header.
2631 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2632 * value, if <= ETHERMTU.
2634 static struct block
*
2638 struct block
*b0
, *b1
, *b2
;
2640 /* are we checking MPLS-encapsulated packets? */
2641 if (label_stack_depth
> 0) {
2645 /* FIXME add other L3 proto IDs */
2646 return gen_mpls_linktype(Q_IP
);
2648 case ETHERTYPE_IPV6
:
2650 /* FIXME add other L3 proto IDs */
2651 return gen_mpls_linktype(Q_IPV6
);
2654 bpf_error("unsupported protocol over mpls");
2660 * Are we testing PPPoE packets?
2664 * The PPPoE session header is part of the
2665 * MAC-layer payload, so all references
2666 * should be relative to the beginning of
2671 * We use Ethernet protocol types inside libpcap;
2672 * map them to the corresponding PPP protocol types.
2674 proto
= ethertype_to_ppptype(proto
);
2675 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2681 return gen_ether_linktype(proto
);
2689 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2693 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2701 case DLT_IEEE802_11
:
2702 case DLT_IEEE802_11_RADIO
:
2703 case DLT_IEEE802_11_RADIO_AVS
:
2704 case DLT_PRISM_HEADER
:
2706 * Check that we have a data frame.
2708 b0
= gen_check_802_11_data_frame();
2711 * Now check for the specified link-layer type.
2713 b1
= gen_llc_linktype(proto
);
2721 * XXX - check for asynchronous frames, as per RFC 1103.
2723 return gen_llc_linktype(proto
);
2729 * XXX - check for LLC PDUs, as per IEEE 802.5.
2731 return gen_llc_linktype(proto
);
2735 case DLT_ATM_RFC1483
:
2737 case DLT_IP_OVER_FC
:
2738 return gen_llc_linktype(proto
);
2744 * If "is_lane" is set, check for a LANE-encapsulated
2745 * version of this protocol, otherwise check for an
2746 * LLC-encapsulated version of this protocol.
2748 * We assume LANE means Ethernet, not Token Ring.
2752 * Check that the packet doesn't begin with an
2753 * LE Control marker. (We've already generated
2756 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2761 * Now generate an Ethernet test.
2763 b1
= gen_ether_linktype(proto
);
2768 * Check for LLC encapsulation and then check the
2771 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2772 b1
= gen_llc_linktype(proto
);
2780 return gen_linux_sll_linktype(proto
);
2785 case DLT_SLIP_BSDOS
:
2788 * These types don't provide any type field; packets
2789 * are always IPv4 or IPv6.
2791 * XXX - for IPv4, check for a version number of 4, and,
2792 * for IPv6, check for a version number of 6?
2797 /* Check for a version number of 4. */
2798 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2800 case ETHERTYPE_IPV6
:
2801 /* Check for a version number of 6. */
2802 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2806 return gen_false(); /* always false */
2813 case DLT_PPP_SERIAL
:
2816 * We use Ethernet protocol types inside libpcap;
2817 * map them to the corresponding PPP protocol types.
2819 proto
= ethertype_to_ppptype(proto
);
2820 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2826 * We use Ethernet protocol types inside libpcap;
2827 * map them to the corresponding PPP protocol types.
2833 * Also check for Van Jacobson-compressed IP.
2834 * XXX - do this for other forms of PPP?
2836 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2837 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2839 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2844 proto
= ethertype_to_ppptype(proto
);
2845 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2855 * For DLT_NULL, the link-layer header is a 32-bit
2856 * word containing an AF_ value in *host* byte order,
2857 * and for DLT_ENC, the link-layer header begins
2858 * with a 32-bit work containing an AF_ value in
2861 * In addition, if we're reading a saved capture file,
2862 * the host byte order in the capture may not be the
2863 * same as the host byte order on this machine.
2865 * For DLT_LOOP, the link-layer header is a 32-bit
2866 * word containing an AF_ value in *network* byte order.
2868 * XXX - AF_ values may, unfortunately, be platform-
2869 * dependent; for example, FreeBSD's AF_INET6 is 24
2870 * whilst NetBSD's and OpenBSD's is 26.
2872 * This means that, when reading a capture file, just
2873 * checking for our AF_INET6 value won't work if the
2874 * capture file came from another OS.
2883 case ETHERTYPE_IPV6
:
2890 * Not a type on which we support filtering.
2891 * XXX - support those that have AF_ values
2892 * #defined on this platform, at least?
2897 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2899 * The AF_ value is in host byte order, but
2900 * the BPF interpreter will convert it to
2901 * network byte order.
2903 * If this is a save file, and it's from a
2904 * machine with the opposite byte order to
2905 * ours, we byte-swap the AF_ value.
2907 * Then we run it through "htonl()", and
2908 * generate code to compare against the result.
2910 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2911 bpf_pcap
->sf
.swapped
)
2912 proto
= SWAPLONG(proto
);
2913 proto
= htonl(proto
);
2915 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2917 #ifdef HAVE_NET_PFVAR_H
2920 * af field is host byte order in contrast to the rest of
2923 if (proto
== ETHERTYPE_IP
)
2924 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2925 BPF_B
, (bpf_int32
)AF_INET
));
2927 else if (proto
== ETHERTYPE_IPV6
)
2928 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2929 BPF_B
, (bpf_int32
)AF_INET6
));
2935 #endif /* HAVE_NET_PFVAR_H */
2938 case DLT_ARCNET_LINUX
:
2940 * XXX should we check for first fragment if the protocol
2949 case ETHERTYPE_IPV6
:
2950 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2951 (bpf_int32
)ARCTYPE_INET6
));
2955 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2956 (bpf_int32
)ARCTYPE_IP
);
2957 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2958 (bpf_int32
)ARCTYPE_IP_OLD
);
2963 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2964 (bpf_int32
)ARCTYPE_ARP
);
2965 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2966 (bpf_int32
)ARCTYPE_ARP_OLD
);
2970 case ETHERTYPE_REVARP
:
2971 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2972 (bpf_int32
)ARCTYPE_REVARP
));
2974 case ETHERTYPE_ATALK
:
2975 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2976 (bpf_int32
)ARCTYPE_ATALK
));
2983 case ETHERTYPE_ATALK
:
2993 * XXX - assumes a 2-byte Frame Relay header with
2994 * DLCI and flags. What if the address is longer?
3000 * Check for the special NLPID for IP.
3002 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3005 case ETHERTYPE_IPV6
:
3007 * Check for the special NLPID for IPv6.
3009 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3014 * Check for several OSI protocols.
3016 * Frame Relay packets typically have an OSI
3017 * NLPID at the beginning; we check for each
3020 * What we check for is the NLPID and a frame
3021 * control field of UI, i.e. 0x03 followed
3024 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3025 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3026 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3038 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3040 case DLT_JUNIPER_MFR
:
3041 case DLT_JUNIPER_MLFR
:
3042 case DLT_JUNIPER_MLPPP
:
3043 case DLT_JUNIPER_ATM1
:
3044 case DLT_JUNIPER_ATM2
:
3045 case DLT_JUNIPER_PPPOE
:
3046 case DLT_JUNIPER_PPPOE_ATM
:
3047 case DLT_JUNIPER_GGSN
:
3048 case DLT_JUNIPER_ES
:
3049 case DLT_JUNIPER_MONITOR
:
3050 case DLT_JUNIPER_SERVICES
:
3051 case DLT_JUNIPER_ETHER
:
3052 case DLT_JUNIPER_PPP
:
3053 case DLT_JUNIPER_FRELAY
:
3054 case DLT_JUNIPER_CHDLC
:
3055 case DLT_JUNIPER_VP
:
3056 case DLT_JUNIPER_ST
:
3057 case DLT_JUNIPER_ISM
:
3058 /* just lets verify the magic number for now -
3059 * on ATM we may have up to 6 different encapsulations on the wire
3060 * and need a lot of heuristics to figure out that the payload
3063 * FIXME encapsulation specific BPF_ filters
3065 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3067 case DLT_LINUX_IRDA
:
3068 bpf_error("IrDA link-layer type filtering not implemented");
3071 bpf_error("DOCSIS link-layer type filtering not implemented");
3074 case DLT_MTP2_WITH_PHDR
:
3075 bpf_error("MTP2 link-layer type filtering not implemented");
3078 bpf_error("ERF link-layer type filtering not implemented");
3082 bpf_error("PFSYNC link-layer type filtering not implemented");
3085 case DLT_LINUX_LAPD
:
3086 bpf_error("LAPD link-layer type filtering not implemented");
3090 bpf_error("USB link-layer type filtering not implemented");
3092 case DLT_BLUETOOTH_HCI_H4
:
3093 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3094 bpf_error("Bluetooth link-layer type filtering not implemented");
3097 bpf_error("CAN20B link-layer type filtering not implemented");
3099 case DLT_IEEE802_15_4
:
3100 case DLT_IEEE802_15_4_LINUX
:
3101 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3103 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3104 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3107 bpf_error("SITA link-layer type filtering not implemented");
3110 bpf_error("RAIF1 link-layer type filtering not implemented");
3113 bpf_error("IPMB link-layer type filtering not implemented");
3116 bpf_error("AX.25 link-layer type filtering not implemented");
3120 * All the types that have no encapsulation should either be
3121 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3122 * all packets are IP packets, or should be handled in some
3123 * special case, if none of them are (if some are and some
3124 * aren't, the lack of encapsulation is a problem, as we'd
3125 * have to find some other way of determining the packet type).
3127 * Therefore, if "off_linktype" is -1, there's an error.
3129 if (off_linktype
== (u_int
)-1)
3133 * Any type not handled above should always have an Ethernet
3134 * type at an offset of "off_linktype".
3136 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3140 * Check for an LLC SNAP packet with a given organization code and
3141 * protocol type; we check the entire contents of the 802.2 LLC and
3142 * snap headers, checking for DSAP and SSAP of SNAP and a control
3143 * field of 0x03 in the LLC header, and for the specified organization
3144 * code and protocol type in the SNAP header.
3146 static struct block
*
3147 gen_snap(orgcode
, ptype
)
3148 bpf_u_int32 orgcode
;
3151 u_char snapblock
[8];
3153 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3154 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3155 snapblock
[2] = 0x03; /* control = UI */
3156 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3157 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3158 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3159 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3160 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3161 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3165 * Generate code to match a particular packet type, for link-layer types
3166 * using 802.2 LLC headers.
3168 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3169 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3171 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3172 * value, if <= ETHERMTU. We use that to determine whether to
3173 * match the DSAP or both DSAP and LSAP or to check the OUI and
3174 * protocol ID in a SNAP header.
3176 static struct block
*
3177 gen_llc_linktype(proto
)
3181 * XXX - handle token-ring variable-length header.
3187 case LLCSAP_NETBEUI
:
3189 * XXX - should we check both the DSAP and the
3190 * SSAP, like this, or should we check just the
3191 * DSAP, as we do for other types <= ETHERMTU
3192 * (i.e., other SAP values)?
3194 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3195 ((proto
<< 8) | proto
));
3199 * XXX - are there ever SNAP frames for IPX on
3200 * non-Ethernet 802.x networks?
3202 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3203 (bpf_int32
)LLCSAP_IPX
);
3205 case ETHERTYPE_ATALK
:
3207 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3208 * SNAP packets with an organization code of
3209 * 0x080007 (Apple, for Appletalk) and a protocol
3210 * type of ETHERTYPE_ATALK (Appletalk).
3212 * XXX - check for an organization code of
3213 * encapsulated Ethernet as well?
3215 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3219 * XXX - we don't have to check for IPX 802.3
3220 * here, but should we check for the IPX Ethertype?
3222 if (proto
<= ETHERMTU
) {
3224 * This is an LLC SAP value, so check
3227 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3230 * This is an Ethernet type; we assume that it's
3231 * unlikely that it'll appear in the right place
3232 * at random, and therefore check only the
3233 * location that would hold the Ethernet type
3234 * in a SNAP frame with an organization code of
3235 * 0x000000 (encapsulated Ethernet).
3237 * XXX - if we were to check for the SNAP DSAP and
3238 * LSAP, as per XXX, and were also to check for an
3239 * organization code of 0x000000 (encapsulated
3240 * Ethernet), we'd do
3242 * return gen_snap(0x000000, proto);
3244 * here; for now, we don't, as per the above.
3245 * I don't know whether it's worth the extra CPU
3246 * time to do the right check or not.
3248 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3253 static struct block
*
3254 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3258 u_int src_off
, dst_off
;
3260 struct block
*b0
, *b1
;
3274 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3275 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3281 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3282 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3289 b0
= gen_linktype(proto
);
3290 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3296 static struct block
*
3297 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3298 struct in6_addr
*addr
;
3299 struct in6_addr
*mask
;
3301 u_int src_off
, dst_off
;
3303 struct block
*b0
, *b1
;
3318 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3319 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3325 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3326 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3333 /* this order is important */
3334 a
= (u_int32_t
*)addr
;
3335 m
= (u_int32_t
*)mask
;
3336 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3337 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3339 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3341 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3343 b0
= gen_linktype(proto
);
3349 static struct block
*
3350 gen_ehostop(eaddr
, dir
)
3351 register const u_char
*eaddr
;
3354 register struct block
*b0
, *b1
;
3358 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3361 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3364 b0
= gen_ehostop(eaddr
, Q_SRC
);
3365 b1
= gen_ehostop(eaddr
, Q_DST
);
3371 b0
= gen_ehostop(eaddr
, Q_SRC
);
3372 b1
= gen_ehostop(eaddr
, Q_DST
);
3381 * Like gen_ehostop, but for DLT_FDDI
3383 static struct block
*
3384 gen_fhostop(eaddr
, dir
)
3385 register const u_char
*eaddr
;
3388 struct block
*b0
, *b1
;
3393 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3395 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3400 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3402 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3406 b0
= gen_fhostop(eaddr
, Q_SRC
);
3407 b1
= gen_fhostop(eaddr
, Q_DST
);
3413 b0
= gen_fhostop(eaddr
, Q_SRC
);
3414 b1
= gen_fhostop(eaddr
, Q_DST
);
3423 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3425 static struct block
*
3426 gen_thostop(eaddr
, dir
)
3427 register const u_char
*eaddr
;
3430 register struct block
*b0
, *b1
;
3434 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3437 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3440 b0
= gen_thostop(eaddr
, Q_SRC
);
3441 b1
= gen_thostop(eaddr
, Q_DST
);
3447 b0
= gen_thostop(eaddr
, Q_SRC
);
3448 b1
= gen_thostop(eaddr
, Q_DST
);
3457 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
3459 static struct block
*
3460 gen_wlanhostop(eaddr
, dir
)
3461 register const u_char
*eaddr
;
3464 register struct block
*b0
, *b1
, *b2
;
3465 register struct slist
*s
;
3467 #ifdef ENABLE_WLAN_FILTERING_PATCH
3470 * We need to disable the optimizer because the optimizer is buggy
3471 * and wipes out some LD instructions generated by the below
3472 * code to validate the Frame Control bits
3475 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3482 * For control frames, there is no SA.
3484 * For management frames, SA is at an
3485 * offset of 10 from the beginning of
3488 * For data frames, SA is at an offset
3489 * of 10 from the beginning of the packet
3490 * if From DS is clear, at an offset of
3491 * 16 from the beginning of the packet
3492 * if From DS is set and To DS is clear,
3493 * and an offset of 24 from the beginning
3494 * of the packet if From DS is set and To DS
3499 * Generate the tests to be done for data frames
3502 * First, check for To DS set, i.e. check "link[1] & 0x01".
3504 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3505 b1
= new_block(JMP(BPF_JSET
));
3506 b1
->s
.k
= 0x01; /* To DS */
3510 * If To DS is set, the SA is at 24.
3512 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3516 * Now, check for To DS not set, i.e. check
3517 * "!(link[1] & 0x01)".
3519 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3520 b2
= new_block(JMP(BPF_JSET
));
3521 b2
->s
.k
= 0x01; /* To DS */
3526 * If To DS is not set, the SA is at 16.
3528 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3532 * Now OR together the last two checks. That gives
3533 * the complete set of checks for data frames with
3539 * Now check for From DS being set, and AND that with
3540 * the ORed-together checks.
3542 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3543 b1
= new_block(JMP(BPF_JSET
));
3544 b1
->s
.k
= 0x02; /* From DS */
3549 * Now check for data frames with From DS not set.
3551 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3552 b2
= new_block(JMP(BPF_JSET
));
3553 b2
->s
.k
= 0x02; /* From DS */
3558 * If From DS isn't set, the SA is at 10.
3560 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3564 * Now OR together the checks for data frames with
3565 * From DS not set and for data frames with From DS
3566 * set; that gives the checks done for data frames.
3571 * Now check for a data frame.
3572 * I.e, check "link[0] & 0x08".
3574 gen_load_a(OR_LINK
, 0, BPF_B
);
3575 b1
= new_block(JMP(BPF_JSET
));
3580 * AND that with the checks done for data frames.
3585 * If the high-order bit of the type value is 0, this
3586 * is a management frame.
3587 * I.e, check "!(link[0] & 0x08)".
3589 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3590 b2
= new_block(JMP(BPF_JSET
));
3596 * For management frames, the SA is at 10.
3598 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3602 * OR that with the checks done for data frames.
3603 * That gives the checks done for management and
3609 * If the low-order bit of the type value is 1,
3610 * this is either a control frame or a frame
3611 * with a reserved type, and thus not a
3614 * I.e., check "!(link[0] & 0x04)".
3616 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3617 b1
= new_block(JMP(BPF_JSET
));
3623 * AND that with the checks for data and management
3633 * For control frames, there is no DA.
3635 * For management frames, DA is at an
3636 * offset of 4 from the beginning of
3639 * For data frames, DA is at an offset
3640 * of 4 from the beginning of the packet
3641 * if To DS is clear and at an offset of
3642 * 16 from the beginning of the packet
3647 * Generate the tests to be done for data frames.
3649 * First, check for To DS set, i.e. "link[1] & 0x01".
3651 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3652 b1
= new_block(JMP(BPF_JSET
));
3653 b1
->s
.k
= 0x01; /* To DS */
3657 * If To DS is set, the DA is at 16.
3659 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3663 * Now, check for To DS not set, i.e. check
3664 * "!(link[1] & 0x01)".
3666 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3667 b2
= new_block(JMP(BPF_JSET
));
3668 b2
->s
.k
= 0x01; /* To DS */
3673 * If To DS is not set, the DA is at 4.
3675 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3679 * Now OR together the last two checks. That gives
3680 * the complete set of checks for data frames.
3685 * Now check for a data frame.
3686 * I.e, check "link[0] & 0x08".
3688 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3689 b1
= new_block(JMP(BPF_JSET
));
3694 * AND that with the checks done for data frames.
3699 * If the high-order bit of the type value is 0, this
3700 * is a management frame.
3701 * I.e, check "!(link[0] & 0x08)".
3703 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3704 b2
= new_block(JMP(BPF_JSET
));
3710 * For management frames, the DA is at 4.
3712 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3716 * OR that with the checks done for data frames.
3717 * That gives the checks done for management and
3723 * If the low-order bit of the type value is 1,
3724 * this is either a control frame or a frame
3725 * with a reserved type, and thus not a
3728 * I.e., check "!(link[0] & 0x04)".
3730 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3731 b1
= new_block(JMP(BPF_JSET
));
3737 * AND that with the checks for data and management
3744 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3745 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3751 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3752 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3761 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3762 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3763 * as the RFC states.)
3765 static struct block
*
3766 gen_ipfchostop(eaddr
, dir
)
3767 register const u_char
*eaddr
;
3770 register struct block
*b0
, *b1
;
3774 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3777 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3780 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3781 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3787 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3788 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3797 * This is quite tricky because there may be pad bytes in front of the
3798 * DECNET header, and then there are two possible data packet formats that
3799 * carry both src and dst addresses, plus 5 packet types in a format that
3800 * carries only the src node, plus 2 types that use a different format and
3801 * also carry just the src node.
3805 * Instead of doing those all right, we just look for data packets with
3806 * 0 or 1 bytes of padding. If you want to look at other packets, that
3807 * will require a lot more hacking.
3809 * To add support for filtering on DECNET "areas" (network numbers)
3810 * one would want to add a "mask" argument to this routine. That would
3811 * make the filter even more inefficient, although one could be clever
3812 * and not generate masking instructions if the mask is 0xFFFF.
3814 static struct block
*
3815 gen_dnhostop(addr
, dir
)
3819 struct block
*b0
, *b1
, *b2
, *tmp
;
3820 u_int offset_lh
; /* offset if long header is received */
3821 u_int offset_sh
; /* offset if short header is received */
3826 offset_sh
= 1; /* follows flags */
3827 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3831 offset_sh
= 3; /* follows flags, dstnode */
3832 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3836 /* Inefficient because we do our Calvinball dance twice */
3837 b0
= gen_dnhostop(addr
, Q_SRC
);
3838 b1
= gen_dnhostop(addr
, Q_DST
);
3844 /* Inefficient because we do our Calvinball dance twice */
3845 b0
= gen_dnhostop(addr
, Q_SRC
);
3846 b1
= gen_dnhostop(addr
, Q_DST
);
3851 bpf_error("ISO host filtering not implemented");
3856 b0
= gen_linktype(ETHERTYPE_DN
);
3857 /* Check for pad = 1, long header case */
3858 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3859 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3860 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3861 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3863 /* Check for pad = 0, long header case */
3864 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3865 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3868 /* Check for pad = 1, short header case */
3869 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3870 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3871 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3874 /* Check for pad = 0, short header case */
3875 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3876 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3880 /* Combine with test for linktype */
3886 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3887 * test the bottom-of-stack bit, and then check the version number
3888 * field in the IP header.
3890 static struct block
*
3891 gen_mpls_linktype(proto
)
3894 struct block
*b0
, *b1
;
3899 /* match the bottom-of-stack bit */
3900 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3901 /* match the IPv4 version number */
3902 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3907 /* match the bottom-of-stack bit */
3908 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3909 /* match the IPv4 version number */
3910 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3919 static struct block
*
3920 gen_host(addr
, mask
, proto
, dir
, type
)
3927 struct block
*b0
, *b1
;
3928 const char *typestr
;
3938 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
3940 * Only check for non-IPv4 addresses if we're not
3941 * checking MPLS-encapsulated packets.
3943 if (label_stack_depth
== 0) {
3944 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
3946 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
3952 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3955 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3958 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3961 bpf_error("'tcp' modifier applied to %s", typestr
);
3964 bpf_error("'sctp' modifier applied to %s", typestr
);
3967 bpf_error("'udp' modifier applied to %s", typestr
);
3970 bpf_error("'icmp' modifier applied to %s", typestr
);
3973 bpf_error("'igmp' modifier applied to %s", typestr
);
3976 bpf_error("'igrp' modifier applied to %s", typestr
);
3979 bpf_error("'pim' modifier applied to %s", typestr
);
3982 bpf_error("'vrrp' modifier applied to %s", typestr
);
3985 bpf_error("ATALK host filtering not implemented");
3988 bpf_error("AARP host filtering not implemented");
3991 return gen_dnhostop(addr
, dir
);
3994 bpf_error("SCA host filtering not implemented");
3997 bpf_error("LAT host filtering not implemented");
4000 bpf_error("MOPDL host filtering not implemented");
4003 bpf_error("MOPRC host filtering not implemented");
4007 bpf_error("'ip6' modifier applied to ip host");
4010 bpf_error("'icmp6' modifier applied to %s", typestr
);
4014 bpf_error("'ah' modifier applied to %s", typestr
);
4017 bpf_error("'esp' modifier applied to %s", typestr
);
4020 bpf_error("ISO host filtering not implemented");
4023 bpf_error("'esis' modifier applied to %s", typestr
);
4026 bpf_error("'isis' modifier applied to %s", typestr
);
4029 bpf_error("'clnp' modifier applied to %s", typestr
);
4032 bpf_error("'stp' modifier applied to %s", typestr
);
4035 bpf_error("IPX host filtering not implemented");
4038 bpf_error("'netbeui' modifier applied to %s", typestr
);
4041 bpf_error("'radio' modifier applied to %s", typestr
);
4050 static struct block
*
4051 gen_host6(addr
, mask
, proto
, dir
, type
)
4052 struct in6_addr
*addr
;
4053 struct in6_addr
*mask
;
4058 const char *typestr
;
4068 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4071 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4074 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4077 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4080 bpf_error("'sctp' modifier applied to %s", typestr
);
4083 bpf_error("'tcp' modifier applied to %s", typestr
);
4086 bpf_error("'udp' modifier applied to %s", typestr
);
4089 bpf_error("'icmp' modifier applied to %s", typestr
);
4092 bpf_error("'igmp' modifier applied to %s", typestr
);
4095 bpf_error("'igrp' modifier applied to %s", typestr
);
4098 bpf_error("'pim' modifier applied to %s", typestr
);
4101 bpf_error("'vrrp' modifier applied to %s", typestr
);
4104 bpf_error("ATALK host filtering not implemented");
4107 bpf_error("AARP host filtering not implemented");
4110 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4113 bpf_error("SCA host filtering not implemented");
4116 bpf_error("LAT host filtering not implemented");
4119 bpf_error("MOPDL host filtering not implemented");
4122 bpf_error("MOPRC host filtering not implemented");
4125 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4128 bpf_error("'icmp6' modifier applied to %s", typestr
);
4131 bpf_error("'ah' modifier applied to %s", typestr
);
4134 bpf_error("'esp' modifier applied to %s", typestr
);
4137 bpf_error("ISO host filtering not implemented");
4140 bpf_error("'esis' modifier applied to %s", typestr
);
4143 bpf_error("'isis' modifier applied to %s", typestr
);
4146 bpf_error("'clnp' modifier applied to %s", typestr
);
4149 bpf_error("'stp' modifier applied to %s", typestr
);
4152 bpf_error("IPX host filtering not implemented");
4155 bpf_error("'netbeui' modifier applied to %s", typestr
);
4158 bpf_error("'radio' modifier applied to %s", typestr
);
4168 static struct block
*
4169 gen_gateway(eaddr
, alist
, proto
, dir
)
4170 const u_char
*eaddr
;
4171 bpf_u_int32
**alist
;
4175 struct block
*b0
, *b1
, *tmp
;
4178 bpf_error("direction applied to 'gateway'");
4187 b0
= gen_ehostop(eaddr
, Q_OR
);
4190 b0
= gen_fhostop(eaddr
, Q_OR
);
4193 b0
= gen_thostop(eaddr
, Q_OR
);
4195 case DLT_IEEE802_11
:
4196 case DLT_IEEE802_11_RADIO_AVS
:
4198 case DLT_IEEE802_11_RADIO
:
4199 case DLT_PRISM_HEADER
:
4200 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4205 * Check that the packet doesn't begin with an
4206 * LE Control marker. (We've already generated
4209 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4214 * Now check the MAC address.
4216 b0
= gen_ehostop(eaddr
, Q_OR
);
4220 case DLT_IP_OVER_FC
:
4221 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4225 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4227 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4229 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4238 bpf_error("illegal modifier of 'gateway'");
4244 gen_proto_abbrev(proto
)
4253 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4255 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4261 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4263 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4269 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4271 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4277 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4280 #ifndef IPPROTO_IGMP
4281 #define IPPROTO_IGMP 2
4285 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4288 #ifndef IPPROTO_IGRP
4289 #define IPPROTO_IGRP 9
4292 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4296 #define IPPROTO_PIM 103
4300 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4302 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4307 #ifndef IPPROTO_VRRP
4308 #define IPPROTO_VRRP 112
4312 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4316 b1
= gen_linktype(ETHERTYPE_IP
);
4320 b1
= gen_linktype(ETHERTYPE_ARP
);
4324 b1
= gen_linktype(ETHERTYPE_REVARP
);
4328 bpf_error("link layer applied in wrong context");
4331 b1
= gen_linktype(ETHERTYPE_ATALK
);
4335 b1
= gen_linktype(ETHERTYPE_AARP
);
4339 b1
= gen_linktype(ETHERTYPE_DN
);
4343 b1
= gen_linktype(ETHERTYPE_SCA
);
4347 b1
= gen_linktype(ETHERTYPE_LAT
);
4351 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4355 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4360 b1
= gen_linktype(ETHERTYPE_IPV6
);
4363 #ifndef IPPROTO_ICMPV6
4364 #define IPPROTO_ICMPV6 58
4367 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4372 #define IPPROTO_AH 51
4375 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4377 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4383 #define IPPROTO_ESP 50
4386 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4388 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4394 b1
= gen_linktype(LLCSAP_ISONS
);
4398 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4402 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4405 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4406 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4407 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4409 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4411 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4413 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4417 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4418 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4419 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4421 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4423 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4425 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4429 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4430 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4431 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4433 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4438 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4439 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4444 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4445 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4447 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4449 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4454 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4455 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4460 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4461 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4466 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4470 b1
= gen_linktype(LLCSAP_8021D
);
4474 b1
= gen_linktype(LLCSAP_IPX
);
4478 b1
= gen_linktype(LLCSAP_NETBEUI
);
4482 bpf_error("'radio' is not a valid protocol type");
4490 static struct block
*
4497 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4498 b
= new_block(JMP(BPF_JSET
));
4507 * Generate a comparison to a port value in the transport-layer header
4508 * at the specified offset from the beginning of that header.
4510 * XXX - this handles a variable-length prefix preceding the link-layer
4511 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4512 * variable-length link-layer headers (such as Token Ring or 802.11
4515 static struct block
*
4516 gen_portatom(off
, v
)
4520 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4524 static struct block
*
4525 gen_portatom6(off
, v
)
4529 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4534 gen_portop(port
, proto
, dir
)
4535 int port
, proto
, dir
;
4537 struct block
*b0
, *b1
, *tmp
;
4539 /* ip proto 'proto' */
4540 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4546 b1
= 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
);
4561 tmp
= gen_portatom(0, (bpf_int32
)port
);
4562 b1
= gen_portatom(2, (bpf_int32
)port
);
4574 static struct block
*
4575 gen_port(port
, ip_proto
, dir
)
4580 struct block
*b0
, *b1
, *tmp
;
4585 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4586 * not LLC encapsulation with LLCSAP_IP.
4588 * For IEEE 802 networks - which includes 802.5 token ring
4589 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4590 * says that SNAP encapsulation is used, not LLC encapsulation
4593 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4594 * RFC 2225 say that SNAP encapsulation is used, not LLC
4595 * encapsulation with LLCSAP_IP.
4597 * So we always check for ETHERTYPE_IP.
4599 b0
= gen_linktype(ETHERTYPE_IP
);
4605 b1
= gen_portop(port
, ip_proto
, dir
);
4609 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4610 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4612 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4625 gen_portop6(port
, proto
, dir
)
4626 int port
, proto
, dir
;
4628 struct block
*b0
, *b1
, *tmp
;
4630 /* ip6 proto 'proto' */
4631 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4635 b1
= 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
);
4650 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4651 b1
= gen_portatom6(2, (bpf_int32
)port
);
4663 static struct block
*
4664 gen_port6(port
, ip_proto
, dir
)
4669 struct block
*b0
, *b1
, *tmp
;
4671 /* link proto ip6 */
4672 b0
= gen_linktype(ETHERTYPE_IPV6
);
4678 b1
= gen_portop6(port
, ip_proto
, dir
);
4682 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4683 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4685 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4697 /* gen_portrange code */
4698 static struct block
*
4699 gen_portrangeatom(off
, v1
, v2
)
4703 struct block
*b1
, *b2
;
4707 * Reverse the order of the ports, so v1 is the lower one.
4716 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4717 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4725 gen_portrangeop(port1
, port2
, proto
, dir
)
4730 struct block
*b0
, *b1
, *tmp
;
4732 /* ip proto 'proto' */
4733 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4739 b1
= 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
);
4754 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4755 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4767 static struct block
*
4768 gen_portrange(port1
, port2
, ip_proto
, dir
)
4773 struct block
*b0
, *b1
, *tmp
;
4776 b0
= gen_linktype(ETHERTYPE_IP
);
4782 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4786 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4787 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4789 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4801 static struct block
*
4802 gen_portrangeatom6(off
, v1
, v2
)
4806 struct block
*b1
, *b2
;
4810 * Reverse the order of the ports, so v1 is the lower one.
4819 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4820 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4828 gen_portrangeop6(port1
, port2
, proto
, dir
)
4833 struct block
*b0
, *b1
, *tmp
;
4835 /* ip6 proto 'proto' */
4836 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4840 b1
= 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
);
4855 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4856 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4868 static struct block
*
4869 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4874 struct block
*b0
, *b1
, *tmp
;
4876 /* link proto ip6 */
4877 b0
= gen_linktype(ETHERTYPE_IPV6
);
4883 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4887 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4888 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4890 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4903 lookup_proto(name
, proto
)
4904 register const char *name
;
4914 v
= pcap_nametoproto(name
);
4915 if (v
== PROTO_UNDEF
)
4916 bpf_error("unknown ip proto '%s'", name
);
4920 /* XXX should look up h/w protocol type based on linktype */
4921 v
= pcap_nametoeproto(name
);
4922 if (v
== PROTO_UNDEF
) {
4923 v
= pcap_nametollc(name
);
4924 if (v
== PROTO_UNDEF
)
4925 bpf_error("unknown ether proto '%s'", name
);
4930 if (strcmp(name
, "esis") == 0)
4932 else if (strcmp(name
, "isis") == 0)
4934 else if (strcmp(name
, "clnp") == 0)
4937 bpf_error("unknown osi proto '%s'", name
);
4957 static struct block
*
4958 gen_protochain(v
, proto
, dir
)
4963 #ifdef NO_PROTOCHAIN
4964 return gen_proto(v
, proto
, dir
);
4966 struct block
*b0
, *b
;
4967 struct slist
*s
[100];
4968 int fix2
, fix3
, fix4
, fix5
;
4969 int ahcheck
, again
, end
;
4971 int reg2
= alloc_reg();
4973 memset(s
, 0, sizeof(s
));
4974 fix2
= fix3
= fix4
= fix5
= 0;
4981 b0
= gen_protochain(v
, Q_IP
, dir
);
4982 b
= gen_protochain(v
, Q_IPV6
, dir
);
4986 bpf_error("bad protocol applied for 'protochain'");
4991 * We don't handle variable-length prefixes before the link-layer
4992 * header, or variable-length link-layer headers, here yet.
4993 * We might want to add BPF instructions to do the protochain
4994 * work, to simplify that and, on platforms that have a BPF
4995 * interpreter with the new instructions, let the filtering
4996 * be done in the kernel. (We already require a modified BPF
4997 * engine to do the protochain stuff, to support backward
4998 * branches, and backward branch support is unlikely to appear
4999 * in kernel BPF engines.)
5003 case DLT_IEEE802_11
:
5004 case DLT_IEEE802_11_RADIO
:
5005 case DLT_IEEE802_11_RADIO_AVS
:
5006 case DLT_PRISM_HEADER
:
5008 bpf_error("'protochain' not supported with 802.11");
5011 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5014 * s[0] is a dummy entry to protect other BPF insn from damage
5015 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5016 * hard to find interdependency made by jump table fixup.
5019 s
[i
] = new_stmt(0); /*dummy*/
5024 b0
= gen_linktype(ETHERTYPE_IP
);
5027 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5028 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5030 /* X = ip->ip_hl << 2 */
5031 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5032 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5037 b0
= gen_linktype(ETHERTYPE_IPV6
);
5039 /* A = ip6->ip_nxt */
5040 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5041 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5043 /* X = sizeof(struct ip6_hdr) */
5044 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5050 bpf_error("unsupported proto to gen_protochain");
5054 /* again: if (A == v) goto end; else fall through; */
5056 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5058 s
[i
]->s
.jt
= NULL
; /*later*/
5059 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5063 #ifndef IPPROTO_NONE
5064 #define IPPROTO_NONE 59
5066 /* if (A == IPPROTO_NONE) goto end */
5067 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5068 s
[i
]->s
.jt
= NULL
; /*later*/
5069 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5070 s
[i
]->s
.k
= IPPROTO_NONE
;
5071 s
[fix5
]->s
.jf
= s
[i
];
5076 if (proto
== Q_IPV6
) {
5077 int v6start
, v6end
, v6advance
, j
;
5080 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5081 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5082 s
[i
]->s
.jt
= NULL
; /*later*/
5083 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5084 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5085 s
[fix2
]->s
.jf
= s
[i
];
5087 /* if (A == IPPROTO_DSTOPTS) 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_DSTOPTS
;
5093 /* if (A == IPPROTO_ROUTING) goto v6advance */
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
; /*update in next stmt*/
5097 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5099 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5100 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5101 s
[i
]->s
.jt
= NULL
; /*later*/
5102 s
[i
]->s
.jf
= NULL
; /*later*/
5103 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5114 * X = X + (P[X + 1] + 1) * 8;
5117 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5119 /* A = P[X + packet head] */
5120 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5121 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5124 s
[i
] = new_stmt(BPF_ST
);
5128 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5131 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5135 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5137 /* A = P[X + packet head]; */
5138 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5139 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5142 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5146 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5150 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5153 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5157 /* goto again; (must use BPF_JA for backward jump) */
5158 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5159 s
[i
]->s
.k
= again
- i
- 1;
5160 s
[i
- 1]->s
.jf
= s
[i
];
5164 for (j
= v6start
; j
<= v6end
; j
++)
5165 s
[j
]->s
.jt
= s
[v6advance
];
5170 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5172 s
[fix2
]->s
.jf
= s
[i
];
5178 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5179 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5180 s
[i
]->s
.jt
= NULL
; /*later*/
5181 s
[i
]->s
.jf
= NULL
; /*later*/
5182 s
[i
]->s
.k
= IPPROTO_AH
;
5184 s
[fix3
]->s
.jf
= s
[ahcheck
];
5191 * X = X + (P[X + 1] + 2) * 4;
5194 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5196 /* A = P[X + packet head]; */
5197 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5198 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5201 s
[i
] = new_stmt(BPF_ST
);
5205 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5208 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5212 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5214 /* A = P[X + packet head] */
5215 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5216 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5219 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5223 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5227 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5230 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5234 /* goto again; (must use BPF_JA for backward jump) */
5235 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5236 s
[i
]->s
.k
= again
- i
- 1;
5241 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5243 s
[fix2
]->s
.jt
= s
[end
];
5244 s
[fix4
]->s
.jf
= s
[end
];
5245 s
[fix5
]->s
.jt
= s
[end
];
5252 for (i
= 0; i
< max
- 1; i
++)
5253 s
[i
]->next
= s
[i
+ 1];
5254 s
[max
- 1]->next
= NULL
;
5259 b
= new_block(JMP(BPF_JEQ
));
5260 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5270 static struct block
*
5271 gen_check_802_11_data_frame()
5274 struct block
*b0
, *b1
;
5277 * A data frame has the 0x08 bit (b3) in the frame control field set
5278 * and the 0x04 bit (b2) clear.
5280 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5281 b0
= new_block(JMP(BPF_JSET
));
5285 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5286 b1
= new_block(JMP(BPF_JSET
));
5297 * Generate code that checks whether the packet is a packet for protocol
5298 * <proto> and whether the type field in that protocol's header has
5299 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5300 * IP packet and checks the protocol number in the IP header against <v>.
5302 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5303 * against Q_IP and Q_IPV6.
5305 static struct block
*
5306 gen_proto(v
, proto
, dir
)
5311 struct block
*b0
, *b1
;
5313 if (dir
!= Q_DEFAULT
)
5314 bpf_error("direction applied to 'proto'");
5319 b0
= gen_proto(v
, Q_IP
, dir
);
5320 b1
= gen_proto(v
, Q_IPV6
, dir
);
5328 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5329 * not LLC encapsulation with LLCSAP_IP.
5331 * For IEEE 802 networks - which includes 802.5 token ring
5332 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5333 * says that SNAP encapsulation is used, not LLC encapsulation
5336 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5337 * RFC 2225 say that SNAP encapsulation is used, not LLC
5338 * encapsulation with LLCSAP_IP.
5340 * So we always check for ETHERTYPE_IP.
5342 b0
= gen_linktype(ETHERTYPE_IP
);
5344 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5346 b1
= gen_protochain(v
, Q_IP
);
5356 * Frame Relay packets typically have an OSI
5357 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5358 * generates code to check for all the OSI
5359 * NLPIDs, so calling it and then adding a check
5360 * for the particular NLPID for which we're
5361 * looking is bogus, as we can just check for
5364 * What we check for is the NLPID and a frame
5365 * control field value of UI, i.e. 0x03 followed
5368 * XXX - assumes a 2-byte Frame Relay header with
5369 * DLCI and flags. What if the address is longer?
5371 * XXX - what about SNAP-encapsulated frames?
5373 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5379 * Cisco uses an Ethertype lookalike - for OSI,
5382 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5383 /* OSI in C-HDLC is stuffed with a fudge byte */
5384 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5389 b0
= gen_linktype(LLCSAP_ISONS
);
5390 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5396 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5398 * 4 is the offset of the PDU type relative to the IS-IS
5401 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5406 bpf_error("arp does not encapsulate another protocol");
5410 bpf_error("rarp does not encapsulate another protocol");
5414 bpf_error("atalk encapsulation is not specifiable");
5418 bpf_error("decnet encapsulation is not specifiable");
5422 bpf_error("sca does not encapsulate another protocol");
5426 bpf_error("lat does not encapsulate another protocol");
5430 bpf_error("moprc does not encapsulate another protocol");
5434 bpf_error("mopdl does not encapsulate another protocol");
5438 return gen_linktype(v
);
5441 bpf_error("'udp proto' is bogus");
5445 bpf_error("'tcp proto' is bogus");
5449 bpf_error("'sctp proto' is bogus");
5453 bpf_error("'icmp proto' is bogus");
5457 bpf_error("'igmp proto' is bogus");
5461 bpf_error("'igrp proto' is bogus");
5465 bpf_error("'pim proto' is bogus");
5469 bpf_error("'vrrp proto' is bogus");
5474 b0
= gen_linktype(ETHERTYPE_IPV6
);
5476 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5478 b1
= gen_protochain(v
, Q_IPV6
);
5484 bpf_error("'icmp6 proto' is bogus");
5488 bpf_error("'ah proto' is bogus");
5491 bpf_error("'ah proto' is bogus");
5494 bpf_error("'stp proto' is bogus");
5497 bpf_error("'ipx proto' is bogus");
5500 bpf_error("'netbeui proto' is bogus");
5503 bpf_error("'radio proto' is bogus");
5514 register const char *name
;
5517 int proto
= q
.proto
;
5521 bpf_u_int32 mask
, addr
;
5523 bpf_u_int32
**alist
;
5526 struct sockaddr_in
*sin4
;
5527 struct sockaddr_in6
*sin6
;
5528 struct addrinfo
*res
, *res0
;
5529 struct in6_addr mask128
;
5531 struct block
*b
, *tmp
;
5532 int port
, real_proto
;
5538 addr
= pcap_nametonetaddr(name
);
5540 bpf_error("unknown network '%s'", name
);
5541 /* Left justify network addr and calculate its network mask */
5543 while (addr
&& (addr
& 0xff000000) == 0) {
5547 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5551 if (proto
== Q_LINK
) {
5555 eaddr
= pcap_ether_hostton(name
);
5558 "unknown ether host '%s'", name
);
5559 b
= gen_ehostop(eaddr
, dir
);
5564 eaddr
= pcap_ether_hostton(name
);
5567 "unknown FDDI host '%s'", name
);
5568 b
= gen_fhostop(eaddr
, dir
);
5573 eaddr
= pcap_ether_hostton(name
);
5576 "unknown token ring host '%s'", name
);
5577 b
= gen_thostop(eaddr
, dir
);
5581 case DLT_IEEE802_11
:
5582 case DLT_IEEE802_11_RADIO_AVS
:
5583 case DLT_IEEE802_11_RADIO
:
5584 case DLT_PRISM_HEADER
:
5586 eaddr
= pcap_ether_hostton(name
);
5589 "unknown 802.11 host '%s'", name
);
5590 b
= gen_wlanhostop(eaddr
, dir
);
5594 case DLT_IP_OVER_FC
:
5595 eaddr
= pcap_ether_hostton(name
);
5598 "unknown Fibre Channel host '%s'", name
);
5599 b
= gen_ipfchostop(eaddr
, dir
);
5608 * Check that the packet doesn't begin
5609 * with an LE Control marker. (We've
5610 * already generated a test for LANE.)
5612 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5616 eaddr
= pcap_ether_hostton(name
);
5619 "unknown ether host '%s'", name
);
5620 b
= gen_ehostop(eaddr
, dir
);
5626 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5627 } else if (proto
== Q_DECNET
) {
5628 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5630 * I don't think DECNET hosts can be multihomed, so
5631 * there is no need to build up a list of addresses
5633 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5636 alist
= pcap_nametoaddr(name
);
5637 if (alist
== NULL
|| *alist
== NULL
)
5638 bpf_error("unknown host '%s'", name
);
5640 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5642 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5644 tmp
= gen_host(**alist
++, 0xffffffff,
5645 tproto
, dir
, q
.addr
);
5651 memset(&mask128
, 0xff, sizeof(mask128
));
5652 res0
= res
= pcap_nametoaddrinfo(name
);
5654 bpf_error("unknown host '%s'", name
);
5656 tproto
= tproto6
= proto
;
5657 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5661 for (res
= res0
; res
; res
= res
->ai_next
) {
5662 switch (res
->ai_family
) {
5664 if (tproto
== Q_IPV6
)
5667 sin4
= (struct sockaddr_in
*)
5669 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5670 0xffffffff, tproto
, dir
, q
.addr
);
5673 if (tproto6
== Q_IP
)
5676 sin6
= (struct sockaddr_in6
*)
5678 tmp
= gen_host6(&sin6
->sin6_addr
,
5679 &mask128
, tproto6
, dir
, q
.addr
);
5690 bpf_error("unknown host '%s'%s", name
,
5691 (proto
== Q_DEFAULT
)
5693 : " for specified address family");
5700 if (proto
!= Q_DEFAULT
&&
5701 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5702 bpf_error("illegal qualifier of 'port'");
5703 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5704 bpf_error("unknown port '%s'", name
);
5705 if (proto
== Q_UDP
) {
5706 if (real_proto
== IPPROTO_TCP
)
5707 bpf_error("port '%s' is tcp", name
);
5708 else if (real_proto
== IPPROTO_SCTP
)
5709 bpf_error("port '%s' is sctp", name
);
5711 /* override PROTO_UNDEF */
5712 real_proto
= IPPROTO_UDP
;
5714 if (proto
== Q_TCP
) {
5715 if (real_proto
== IPPROTO_UDP
)
5716 bpf_error("port '%s' is udp", name
);
5718 else if (real_proto
== IPPROTO_SCTP
)
5719 bpf_error("port '%s' is sctp", name
);
5721 /* override PROTO_UNDEF */
5722 real_proto
= IPPROTO_TCP
;
5724 if (proto
== Q_SCTP
) {
5725 if (real_proto
== IPPROTO_UDP
)
5726 bpf_error("port '%s' is udp", name
);
5728 else if (real_proto
== IPPROTO_TCP
)
5729 bpf_error("port '%s' is tcp", name
);
5731 /* override PROTO_UNDEF */
5732 real_proto
= IPPROTO_SCTP
;
5735 return gen_port(port
, real_proto
, dir
);
5737 b
= gen_port(port
, real_proto
, dir
);
5738 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5743 if (proto
!= Q_DEFAULT
&&
5744 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5745 bpf_error("illegal qualifier of 'portrange'");
5746 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5747 bpf_error("unknown port in range '%s'", name
);
5748 if (proto
== Q_UDP
) {
5749 if (real_proto
== IPPROTO_TCP
)
5750 bpf_error("port in range '%s' is tcp", name
);
5751 else if (real_proto
== IPPROTO_SCTP
)
5752 bpf_error("port in range '%s' is sctp", name
);
5754 /* override PROTO_UNDEF */
5755 real_proto
= IPPROTO_UDP
;
5757 if (proto
== Q_TCP
) {
5758 if (real_proto
== IPPROTO_UDP
)
5759 bpf_error("port in range '%s' is udp", name
);
5760 else if (real_proto
== IPPROTO_SCTP
)
5761 bpf_error("port in range '%s' is sctp", name
);
5763 /* override PROTO_UNDEF */
5764 real_proto
= IPPROTO_TCP
;
5766 if (proto
== Q_SCTP
) {
5767 if (real_proto
== IPPROTO_UDP
)
5768 bpf_error("port in range '%s' is udp", name
);
5769 else if (real_proto
== IPPROTO_TCP
)
5770 bpf_error("port in range '%s' is tcp", name
);
5772 /* override PROTO_UNDEF */
5773 real_proto
= IPPROTO_SCTP
;
5776 return gen_portrange(port1
, port2
, real_proto
, dir
);
5778 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5779 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5785 eaddr
= pcap_ether_hostton(name
);
5787 bpf_error("unknown ether host: %s", name
);
5789 alist
= pcap_nametoaddr(name
);
5790 if (alist
== NULL
|| *alist
== NULL
)
5791 bpf_error("unknown host '%s'", name
);
5792 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5796 bpf_error("'gateway' not supported in this configuration");
5800 real_proto
= lookup_proto(name
, proto
);
5801 if (real_proto
>= 0)
5802 return gen_proto(real_proto
, proto
, dir
);
5804 bpf_error("unknown protocol: %s", name
);
5807 real_proto
= lookup_proto(name
, proto
);
5808 if (real_proto
>= 0)
5809 return gen_protochain(real_proto
, proto
, dir
);
5811 bpf_error("unknown protocol: %s", name
);
5822 gen_mcode(s1
, s2
, masklen
, q
)
5823 register const char *s1
, *s2
;
5824 register int masklen
;
5827 register int nlen
, mlen
;
5830 nlen
= __pcap_atoin(s1
, &n
);
5831 /* Promote short ipaddr */
5835 mlen
= __pcap_atoin(s2
, &m
);
5836 /* Promote short ipaddr */
5839 bpf_error("non-network bits set in \"%s mask %s\"",
5842 /* Convert mask len to mask */
5844 bpf_error("mask length must be <= 32");
5847 * X << 32 is not guaranteed by C to be 0; it's
5852 m
= 0xffffffff << (32 - masklen
);
5854 bpf_error("non-network bits set in \"%s/%d\"",
5861 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5864 bpf_error("Mask syntax for networks only");
5873 register const char *s
;
5878 int proto
= q
.proto
;
5884 else if (q
.proto
== Q_DECNET
)
5885 vlen
= __pcap_atodn(s
, &v
);
5887 vlen
= __pcap_atoin(s
, &v
);
5894 if (proto
== Q_DECNET
)
5895 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5896 else if (proto
== Q_LINK
) {
5897 bpf_error("illegal link layer address");
5900 if (s
== NULL
&& q
.addr
== Q_NET
) {
5901 /* Promote short net number */
5902 while (v
&& (v
& 0xff000000) == 0) {
5907 /* Promote short ipaddr */
5911 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
5916 proto
= IPPROTO_UDP
;
5917 else if (proto
== Q_TCP
)
5918 proto
= IPPROTO_TCP
;
5919 else if (proto
== Q_SCTP
)
5920 proto
= IPPROTO_SCTP
;
5921 else if (proto
== Q_DEFAULT
)
5922 proto
= PROTO_UNDEF
;
5924 bpf_error("illegal qualifier of 'port'");
5927 return gen_port((int)v
, proto
, dir
);
5931 b
= gen_port((int)v
, proto
, dir
);
5932 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5939 proto
= IPPROTO_UDP
;
5940 else if (proto
== Q_TCP
)
5941 proto
= IPPROTO_TCP
;
5942 else if (proto
== Q_SCTP
)
5943 proto
= IPPROTO_SCTP
;
5944 else if (proto
== Q_DEFAULT
)
5945 proto
= PROTO_UNDEF
;
5947 bpf_error("illegal qualifier of 'portrange'");
5950 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5954 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5955 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5961 bpf_error("'gateway' requires a name");
5965 return gen_proto((int)v
, proto
, dir
);
5968 return gen_protochain((int)v
, proto
, dir
);
5983 gen_mcode6(s1
, s2
, masklen
, q
)
5984 register const char *s1
, *s2
;
5985 register int masklen
;
5988 struct addrinfo
*res
;
5989 struct in6_addr
*addr
;
5990 struct in6_addr mask
;
5995 bpf_error("no mask %s supported", s2
);
5997 res
= pcap_nametoaddrinfo(s1
);
5999 bpf_error("invalid ip6 address %s", s1
);
6001 bpf_error("%s resolved to multiple address", s1
);
6002 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6004 if (sizeof(mask
) * 8 < masklen
)
6005 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6006 memset(&mask
, 0, sizeof(mask
));
6007 memset(&mask
, 0xff, masklen
/ 8);
6009 mask
.s6_addr
[masklen
/ 8] =
6010 (0xff << (8 - masklen
% 8)) & 0xff;
6013 a
= (u_int32_t
*)addr
;
6014 m
= (u_int32_t
*)&mask
;
6015 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6016 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6017 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6025 bpf_error("Mask syntax for networks only");
6029 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6034 bpf_error("invalid qualifier against IPv6 address");
6043 register const u_char
*eaddr
;
6046 struct block
*b
, *tmp
;
6048 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6051 return gen_ehostop(eaddr
, (int)q
.dir
);
6053 return gen_fhostop(eaddr
, (int)q
.dir
);
6055 return gen_thostop(eaddr
, (int)q
.dir
);
6056 case DLT_IEEE802_11
:
6057 case DLT_IEEE802_11_RADIO_AVS
:
6058 case DLT_IEEE802_11_RADIO
:
6059 case DLT_PRISM_HEADER
:
6061 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6065 * Check that the packet doesn't begin with an
6066 * LE Control marker. (We've already generated
6069 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6074 * Now check the MAC address.
6076 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6081 case DLT_IP_OVER_FC
:
6082 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6084 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6088 bpf_error("ethernet address used in non-ether expression");
6095 struct slist
*s0
, *s1
;
6098 * This is definitely not the best way to do this, but the
6099 * lists will rarely get long.
6106 static struct slist
*
6112 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6117 static struct slist
*
6123 s
= new_stmt(BPF_LD
|BPF_MEM
);
6129 * Modify "index" to use the value stored into its register as an
6130 * offset relative to the beginning of the header for the protocol
6131 * "proto", and allocate a register and put an item "size" bytes long
6132 * (1, 2, or 4) at that offset into that register, making it the register
6136 gen_load(proto
, inst
, size
)
6141 struct slist
*s
, *tmp
;
6143 int regno
= alloc_reg();
6145 free_reg(inst
->regno
);
6149 bpf_error("data size must be 1, 2, or 4");
6165 bpf_error("unsupported index operation");
6169 * The offset is relative to the beginning of the packet
6170 * data, if we have a radio header. (If we don't, this
6173 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6174 linktype
!= DLT_IEEE802_11_RADIO
&&
6175 linktype
!= DLT_PRISM_HEADER
)
6176 bpf_error("radio information not present in capture");
6179 * Load into the X register the offset computed into the
6180 * register specifed by "index".
6182 s
= xfer_to_x(inst
);
6185 * Load the item at that offset.
6187 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6189 sappend(inst
->s
, s
);
6194 * The offset is relative to the beginning of
6195 * the link-layer header.
6197 * XXX - what about ATM LANE? Should the index be
6198 * relative to the beginning of the AAL5 frame, so
6199 * that 0 refers to the beginning of the LE Control
6200 * field, or relative to the beginning of the LAN
6201 * frame, so that 0 refers, for Ethernet LANE, to
6202 * the beginning of the destination address?
6204 s
= gen_llprefixlen();
6207 * If "s" is non-null, it has code to arrange that the
6208 * X register contains the length of the prefix preceding
6209 * the link-layer header. Add to it the offset computed
6210 * into the register specified by "index", and move that
6211 * into the X register. Otherwise, just load into the X
6212 * register the offset computed into the register specifed
6216 sappend(s
, xfer_to_a(inst
));
6217 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6218 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6220 s
= xfer_to_x(inst
);
6223 * Load the item at the sum of the offset we've put in the
6224 * X register and the offset of the start of the link
6225 * layer header (which is 0 if the radio header is
6226 * variable-length; that header length is what we put
6227 * into the X register and then added to the index).
6229 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6232 sappend(inst
->s
, s
);
6248 * The offset is relative to the beginning of
6249 * the network-layer header.
6250 * XXX - are there any cases where we want
6253 s
= gen_off_macpl();
6256 * If "s" is non-null, it has code to arrange that the
6257 * X register contains the offset of the MAC-layer
6258 * payload. Add to it the offset computed into the
6259 * register specified by "index", and move that into
6260 * the X register. Otherwise, just load into the X
6261 * register the offset computed into the register specifed
6265 sappend(s
, xfer_to_a(inst
));
6266 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6267 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6269 s
= xfer_to_x(inst
);
6272 * Load the item at the sum of the offset we've put in the
6273 * X register, the offset of the start of the network
6274 * layer header from the beginning of the MAC-layer
6275 * payload, and the purported offset of the start of the
6276 * MAC-layer payload (which might be 0 if there's a
6277 * variable-length prefix before the link-layer header
6278 * or the link-layer header itself is variable-length;
6279 * the variable-length offset of the start of the
6280 * MAC-layer payload is what we put into the X register
6281 * and then added to the index).
6283 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6284 tmp
->s
.k
= off_macpl
+ off_nl
;
6286 sappend(inst
->s
, s
);
6289 * Do the computation only if the packet contains
6290 * the protocol in question.
6292 b
= gen_proto_abbrev(proto
);
6294 gen_and(inst
->b
, b
);
6307 * The offset is relative to the beginning of
6308 * the transport-layer header.
6310 * Load the X register with the length of the IPv4 header
6311 * (plus the offset of the link-layer header, if it's
6312 * a variable-length header), in bytes.
6314 * XXX - are there any cases where we want
6316 * XXX - we should, if we're built with
6317 * IPv6 support, generate code to load either
6318 * IPv4, IPv6, or both, as appropriate.
6320 s
= gen_loadx_iphdrlen();
6323 * The X register now contains the sum of the length
6324 * of any variable-length header preceding the link-layer
6325 * header, any variable-length link-layer header, and the
6326 * length of the network-layer header.
6328 * Load into the A register the offset relative to
6329 * the beginning of the transport layer header,
6330 * add the X register to that, move that to the
6331 * X register, and load with an offset from the
6332 * X register equal to the offset of the network
6333 * layer header relative to the beginning of
6334 * the MAC-layer payload plus the fixed-length
6335 * portion of the offset of the MAC-layer payload
6336 * from the beginning of the raw packet data.
6338 sappend(s
, xfer_to_a(inst
));
6339 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6340 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6341 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6342 tmp
->s
.k
= off_macpl
+ off_nl
;
6343 sappend(inst
->s
, s
);
6346 * Do the computation only if the packet contains
6347 * the protocol in question - which is true only
6348 * if this is an IP datagram and is the first or
6349 * only fragment of that datagram.
6351 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6353 gen_and(inst
->b
, b
);
6355 gen_and(gen_proto_abbrev(Q_IP
), b
);
6361 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6365 inst
->regno
= regno
;
6366 s
= new_stmt(BPF_ST
);
6368 sappend(inst
->s
, s
);
6374 gen_relation(code
, a0
, a1
, reversed
)
6376 struct arth
*a0
, *a1
;
6379 struct slist
*s0
, *s1
, *s2
;
6380 struct block
*b
, *tmp
;
6384 if (code
== BPF_JEQ
) {
6385 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6386 b
= new_block(JMP(code
));
6390 b
= new_block(BPF_JMP
|code
|BPF_X
);
6396 sappend(a0
->s
, a1
->s
);
6400 free_reg(a0
->regno
);
6401 free_reg(a1
->regno
);
6403 /* 'and' together protocol checks */
6406 gen_and(a0
->b
, tmp
= a1
->b
);
6422 int regno
= alloc_reg();
6423 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6426 s
= new_stmt(BPF_LD
|BPF_LEN
);
6427 s
->next
= new_stmt(BPF_ST
);
6428 s
->next
->s
.k
= regno
;
6443 a
= (struct arth
*)newchunk(sizeof(*a
));
6447 s
= new_stmt(BPF_LD
|BPF_IMM
);
6449 s
->next
= new_stmt(BPF_ST
);
6465 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6468 s
= new_stmt(BPF_ST
);
6476 gen_arth(code
, a0
, a1
)
6478 struct arth
*a0
, *a1
;
6480 struct slist
*s0
, *s1
, *s2
;
6484 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6489 sappend(a0
->s
, a1
->s
);
6491 free_reg(a0
->regno
);
6492 free_reg(a1
->regno
);
6494 s0
= new_stmt(BPF_ST
);
6495 a0
->regno
= s0
->s
.k
= alloc_reg();
6502 * Here we handle simple allocation of the scratch registers.
6503 * If too many registers are alloc'd, the allocator punts.
6505 static int regused
[BPF_MEMWORDS
];
6509 * Initialize the table of used registers and the current register.
6515 memset(regused
, 0, sizeof regused
);
6519 * Return the next free register.
6524 int n
= BPF_MEMWORDS
;
6527 if (regused
[curreg
])
6528 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6530 regused
[curreg
] = 1;
6534 bpf_error("too many registers needed to evaluate expression");
6540 * Return a register to the table so it can
6550 static struct block
*
6557 s
= new_stmt(BPF_LD
|BPF_LEN
);
6558 b
= new_block(JMP(jmp
));
6569 return gen_len(BPF_JGE
, n
);
6573 * Actually, this is less than or equal.
6581 b
= gen_len(BPF_JGT
, n
);
6588 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6589 * the beginning of the link-layer header.
6590 * XXX - that means you can't test values in the radiotap header, but
6591 * as that header is difficult if not impossible to parse generally
6592 * without a loop, that might not be a severe problem. A new keyword
6593 * "radio" could be added for that, although what you'd really want
6594 * would be a way of testing particular radio header values, which
6595 * would generate code appropriate to the radio header in question.
6598 gen_byteop(op
, idx
, val
)
6609 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6612 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6616 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6620 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6624 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6628 b
= new_block(JMP(BPF_JEQ
));
6635 static u_char abroadcast
[] = { 0x0 };
6638 gen_broadcast(proto
)
6641 bpf_u_int32 hostmask
;
6642 struct block
*b0
, *b1
, *b2
;
6643 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6651 case DLT_ARCNET_LINUX
:
6652 return gen_ahostop(abroadcast
, Q_DST
);
6654 return gen_ehostop(ebroadcast
, Q_DST
);
6656 return gen_fhostop(ebroadcast
, Q_DST
);
6658 return gen_thostop(ebroadcast
, Q_DST
);
6659 case DLT_IEEE802_11
:
6660 case DLT_IEEE802_11_RADIO_AVS
:
6661 case DLT_IEEE802_11_RADIO
:
6663 case DLT_PRISM_HEADER
:
6664 return gen_wlanhostop(ebroadcast
, Q_DST
);
6665 case DLT_IP_OVER_FC
:
6666 return gen_ipfchostop(ebroadcast
, Q_DST
);
6670 * Check that the packet doesn't begin with an
6671 * LE Control marker. (We've already generated
6674 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6679 * Now check the MAC address.
6681 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6687 bpf_error("not a broadcast link");
6692 b0
= gen_linktype(ETHERTYPE_IP
);
6693 hostmask
= ~netmask
;
6694 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6695 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6696 (bpf_int32
)(~0 & hostmask
), hostmask
);
6701 bpf_error("only link-layer/IP broadcast filters supported");
6707 * Generate code to test the low-order bit of a MAC address (that's
6708 * the bottom bit of the *first* byte).
6710 static struct block
*
6711 gen_mac_multicast(offset
)
6714 register struct block
*b0
;
6715 register struct slist
*s
;
6717 /* link[offset] & 1 != 0 */
6718 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6719 b0
= new_block(JMP(BPF_JSET
));
6726 gen_multicast(proto
)
6729 register struct block
*b0
, *b1
, *b2
;
6730 register struct slist
*s
;
6738 case DLT_ARCNET_LINUX
:
6739 /* all ARCnet multicasts use the same address */
6740 return gen_ahostop(abroadcast
, Q_DST
);
6742 /* ether[0] & 1 != 0 */
6743 return gen_mac_multicast(0);
6746 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6748 * XXX - was that referring to bit-order issues?
6750 /* fddi[1] & 1 != 0 */
6751 return gen_mac_multicast(1);
6753 /* tr[2] & 1 != 0 */
6754 return gen_mac_multicast(2);
6755 case DLT_IEEE802_11
:
6756 case DLT_IEEE802_11_RADIO_AVS
:
6758 case DLT_IEEE802_11_RADIO
:
6759 case DLT_PRISM_HEADER
:
6763 * For control frames, there is no DA.
6765 * For management frames, DA is at an
6766 * offset of 4 from the beginning of
6769 * For data frames, DA is at an offset
6770 * of 4 from the beginning of the packet
6771 * if To DS is clear and at an offset of
6772 * 16 from the beginning of the packet
6777 * Generate the tests to be done for data frames.
6779 * First, check for To DS set, i.e. "link[1] & 0x01".
6781 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6782 b1
= new_block(JMP(BPF_JSET
));
6783 b1
->s
.k
= 0x01; /* To DS */
6787 * If To DS is set, the DA is at 16.
6789 b0
= gen_mac_multicast(16);
6793 * Now, check for To DS not set, i.e. check
6794 * "!(link[1] & 0x01)".
6796 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6797 b2
= new_block(JMP(BPF_JSET
));
6798 b2
->s
.k
= 0x01; /* To DS */
6803 * If To DS is not set, the DA is at 4.
6805 b1
= gen_mac_multicast(4);
6809 * Now OR together the last two checks. That gives
6810 * the complete set of checks for data frames.
6815 * Now check for a data frame.
6816 * I.e, check "link[0] & 0x08".
6818 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6819 b1
= new_block(JMP(BPF_JSET
));
6824 * AND that with the checks done for data frames.
6829 * If the high-order bit of the type value is 0, this
6830 * is a management frame.
6831 * I.e, check "!(link[0] & 0x08)".
6833 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6834 b2
= new_block(JMP(BPF_JSET
));
6840 * For management frames, the DA is at 4.
6842 b1
= gen_mac_multicast(4);
6846 * OR that with the checks done for data frames.
6847 * That gives the checks done for management and
6853 * If the low-order bit of the type value is 1,
6854 * this is either a control frame or a frame
6855 * with a reserved type, and thus not a
6858 * I.e., check "!(link[0] & 0x04)".
6860 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6861 b1
= new_block(JMP(BPF_JSET
));
6867 * AND that with the checks for data and management
6872 case DLT_IP_OVER_FC
:
6873 b0
= gen_mac_multicast(2);
6878 * Check that the packet doesn't begin with an
6879 * LE Control marker. (We've already generated
6882 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6886 /* ether[off_mac] & 1 != 0 */
6887 b0
= gen_mac_multicast(off_mac
);
6895 /* Link not known to support multicasts */
6899 b0
= gen_linktype(ETHERTYPE_IP
);
6900 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
6906 b0
= gen_linktype(ETHERTYPE_IPV6
);
6907 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
6912 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
6918 * generate command for inbound/outbound. It's here so we can
6919 * make it link-type specific. 'dir' = 0 implies "inbound",
6920 * = 1 implies "outbound".
6926 register struct block
*b0
;
6929 * Only some data link types support inbound/outbound qualifiers.
6933 b0
= gen_relation(BPF_JEQ
,
6934 gen_load(Q_LINK
, gen_loadi(0), 1),
6942 * Match packets sent by this machine.
6944 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6947 * Match packets sent to this machine.
6948 * (No broadcast or multicast packets, or
6949 * packets sent to some other machine and
6950 * received promiscuously.)
6952 * XXX - packets sent to other machines probably
6953 * shouldn't be matched, but what about broadcast
6954 * or multicast packets we received?
6956 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6960 #ifdef HAVE_NET_PFVAR_H
6962 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6963 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6969 /* match outgoing packets */
6970 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6972 /* match incoming packets */
6973 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6977 case DLT_JUNIPER_MFR
:
6978 case DLT_JUNIPER_MLFR
:
6979 case DLT_JUNIPER_MLPPP
:
6980 case DLT_JUNIPER_ATM1
:
6981 case DLT_JUNIPER_ATM2
:
6982 case DLT_JUNIPER_PPPOE
:
6983 case DLT_JUNIPER_PPPOE_ATM
:
6984 case DLT_JUNIPER_GGSN
:
6985 case DLT_JUNIPER_ES
:
6986 case DLT_JUNIPER_MONITOR
:
6987 case DLT_JUNIPER_SERVICES
:
6988 case DLT_JUNIPER_ETHER
:
6989 case DLT_JUNIPER_PPP
:
6990 case DLT_JUNIPER_FRELAY
:
6991 case DLT_JUNIPER_CHDLC
:
6992 case DLT_JUNIPER_VP
:
6993 case DLT_JUNIPER_ST
:
6994 case DLT_JUNIPER_ISM
:
6995 /* juniper flags (including direction) are stored
6996 * the byte after the 3-byte magic number */
6998 /* match outgoing packets */
6999 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7001 /* match incoming packets */
7002 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7007 bpf_error("inbound/outbound not supported on linktype %d",
7015 #ifdef HAVE_NET_PFVAR_H
7016 /* PF firewall log matched interface */
7018 gen_pf_ifname(const char *ifname
)
7023 if (linktype
!= DLT_PFLOG
) {
7024 bpf_error("ifname supported only on PF linktype");
7027 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7028 off
= offsetof(struct pfloghdr
, ifname
);
7029 if (strlen(ifname
) >= len
) {
7030 bpf_error("ifname interface names can only be %d characters",
7034 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7038 /* PF firewall log ruleset name */
7040 gen_pf_ruleset(char *ruleset
)
7044 if (linktype
!= DLT_PFLOG
) {
7045 bpf_error("ruleset supported only on PF linktype");
7049 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7050 bpf_error("ruleset names can only be %ld characters",
7051 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7055 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7056 strlen(ruleset
), (const u_char
*)ruleset
);
7060 /* PF firewall log rule number */
7066 if (linktype
!= DLT_PFLOG
) {
7067 bpf_error("rnr supported only on PF linktype");
7071 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7076 /* PF firewall log sub-rule number */
7078 gen_pf_srnr(int srnr
)
7082 if (linktype
!= DLT_PFLOG
) {
7083 bpf_error("srnr supported only on PF linktype");
7087 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7092 /* PF firewall log reason code */
7094 gen_pf_reason(int reason
)
7098 if (linktype
!= DLT_PFLOG
) {
7099 bpf_error("reason supported only on PF linktype");
7103 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7108 /* PF firewall log action */
7110 gen_pf_action(int action
)
7114 if (linktype
!= DLT_PFLOG
) {
7115 bpf_error("action supported only on PF linktype");
7119 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7123 #else /* !HAVE_NET_PFVAR_H */
7125 gen_pf_ifname(const char *ifname
)
7127 bpf_error("libpcap was compiled without pf support");
7133 gen_pf_ruleset(char *ruleset
)
7135 bpf_error("libpcap was compiled on a machine without pf support");
7143 bpf_error("libpcap was compiled on a machine without pf support");
7149 gen_pf_srnr(int srnr
)
7151 bpf_error("libpcap was compiled on a machine without pf support");
7157 gen_pf_reason(int reason
)
7159 bpf_error("libpcap was compiled on a machine without pf support");
7165 gen_pf_action(int action
)
7167 bpf_error("libpcap was compiled on a machine without pf support");
7171 #endif /* HAVE_NET_PFVAR_H */
7173 /* IEEE 802.11 wireless header */
7175 gen_p80211_type(int type
, int mask
)
7181 case DLT_IEEE802_11
:
7182 case DLT_PRISM_HEADER
:
7183 case DLT_IEEE802_11_RADIO_AVS
:
7184 case DLT_IEEE802_11_RADIO
:
7185 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7190 bpf_error("802.11 link-layer types supported only on 802.11");
7198 register const u_char
*eaddr
;
7204 case DLT_ARCNET_LINUX
:
7205 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7207 return (gen_ahostop(eaddr
, (int)q
.dir
));
7209 bpf_error("ARCnet address used in non-arc expression");
7215 bpf_error("aid supported only on ARCnet");
7218 bpf_error("ARCnet address used in non-arc expression");
7223 static struct block
*
7224 gen_ahostop(eaddr
, dir
)
7225 register const u_char
*eaddr
;
7228 register struct block
*b0
, *b1
;
7231 /* src comes first, different from Ethernet */
7233 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7236 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7239 b0
= gen_ahostop(eaddr
, Q_SRC
);
7240 b1
= gen_ahostop(eaddr
, Q_DST
);
7246 b0
= gen_ahostop(eaddr
, Q_SRC
);
7247 b1
= gen_ahostop(eaddr
, Q_DST
);
7256 * support IEEE 802.1Q VLAN trunk over ethernet
7262 struct block
*b0
, *b1
;
7264 /* can't check for VLAN-encapsulated packets inside MPLS */
7265 if (label_stack_depth
> 0)
7266 bpf_error("no VLAN match after MPLS");
7269 * Check for a VLAN packet, and then change the offsets to point
7270 * to the type and data fields within the VLAN packet. Just
7271 * increment the offsets, so that we can support a hierarchy, e.g.
7272 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7275 * XXX - this is a bit of a kludge. If we were to split the
7276 * compiler into a parser that parses an expression and
7277 * generates an expression tree, and a code generator that
7278 * takes an expression tree (which could come from our
7279 * parser or from some other parser) and generates BPF code,
7280 * we could perhaps make the offsets parameters of routines
7281 * and, in the handler for an "AND" node, pass to subnodes
7282 * other than the VLAN node the adjusted offsets.
7284 * This would mean that "vlan" would, instead of changing the
7285 * behavior of *all* tests after it, change only the behavior
7286 * of tests ANDed with it. That would change the documented
7287 * semantics of "vlan", which might break some expressions.
7288 * However, it would mean that "(vlan and ip) or ip" would check
7289 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7290 * checking only for VLAN-encapsulated IP, so that could still
7291 * be considered worth doing; it wouldn't break expressions
7292 * that are of the form "vlan and ..." or "vlan N and ...",
7293 * which I suspect are the most common expressions involving
7294 * "vlan". "vlan or ..." doesn't necessarily do what the user
7295 * would really want, now, as all the "or ..." tests would
7296 * be done assuming a VLAN, even though the "or" could be viewed
7297 * as meaning "or, if this isn't a VLAN packet...".
7304 /* check for VLAN */
7305 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7306 (bpf_int32
)ETHERTYPE_8021Q
);
7308 /* If a specific VLAN is requested, check VLAN id */
7309 if (vlan_num
>= 0) {
7310 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7311 (bpf_int32
)vlan_num
, 0x0fff);
7325 bpf_error("no VLAN support for data link type %d",
7340 struct block
*b0
,*b1
;
7343 * Change the offsets to point to the type and data fields within
7344 * the MPLS packet. Just increment the offsets, so that we
7345 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7346 * capture packets with an outer label of 100000 and an inner
7349 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7353 if (label_stack_depth
> 0) {
7354 /* just match the bottom-of-stack bit clear */
7355 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7358 * Indicate that we're checking MPLS-encapsulated headers,
7359 * to make sure higher level code generators don't try to
7360 * match against IP-related protocols such as Q_ARP, Q_RARP
7365 case DLT_C_HDLC
: /* fall through */
7367 b0
= gen_linktype(ETHERTYPE_MPLS
);
7371 b0
= gen_linktype(PPP_MPLS_UCAST
);
7374 /* FIXME add other DLT_s ...
7375 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7376 * leave it for now */
7379 bpf_error("no MPLS support for data link type %d",
7387 /* If a specific MPLS label is requested, check it */
7388 if (label_num
>= 0) {
7389 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7390 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7391 0xfffff000); /* only compare the first 20 bits */
7398 label_stack_depth
++;
7403 * Support PPPOE discovery and session.
7408 /* check for PPPoE discovery */
7409 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7418 * Test against the PPPoE session link-layer type.
7420 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7423 * Change the offsets to point to the type and data fields within
7424 * the PPP packet, and note that this is PPPoE rather than
7427 * XXX - this is a bit of a kludge. If we were to split the
7428 * compiler into a parser that parses an expression and
7429 * generates an expression tree, and a code generator that
7430 * takes an expression tree (which could come from our
7431 * parser or from some other parser) and generates BPF code,
7432 * we could perhaps make the offsets parameters of routines
7433 * and, in the handler for an "AND" node, pass to subnodes
7434 * other than the PPPoE node the adjusted offsets.
7436 * This would mean that "pppoes" would, instead of changing the
7437 * behavior of *all* tests after it, change only the behavior
7438 * of tests ANDed with it. That would change the documented
7439 * semantics of "pppoes", which might break some expressions.
7440 * However, it would mean that "(pppoes and ip) or ip" would check
7441 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7442 * checking only for VLAN-encapsulated IP, so that could still
7443 * be considered worth doing; it wouldn't break expressions
7444 * that are of the form "pppoes and ..." which I suspect are the
7445 * most common expressions involving "pppoes". "pppoes or ..."
7446 * doesn't necessarily do what the user would really want, now,
7447 * as all the "or ..." tests would be done assuming PPPoE, even
7448 * though the "or" could be viewed as meaning "or, if this isn't
7449 * a PPPoE packet...".
7451 orig_linktype
= off_linktype
; /* save original values */
7456 * The "network-layer" protocol is PPPoE, which has a 6-byte
7457 * PPPoE header, followed by a PPP packet.
7459 * There is no HDLC encapsulation for the PPP packet (it's
7460 * encapsulated in PPPoES instead), so the link-layer type
7461 * starts at the first byte of the PPP packet. For PPPoE,
7462 * that offset is relative to the beginning of the total
7463 * link-layer payload, including any 802.2 LLC header, so
7464 * it's 6 bytes past off_nl.
7466 off_linktype
= off_nl
+ 6;
7469 * The network-layer offsets are relative to the beginning
7470 * of the MAC-layer payload; that's past the 6-byte
7471 * PPPoE header and the 2-byte PPP header.
7474 off_nl_nosnap
= 6+2;
7480 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7492 bpf_error("'vpi' supported only on raw ATM");
7493 if (off_vpi
== (u_int
)-1)
7495 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7501 bpf_error("'vci' supported only on raw ATM");
7502 if (off_vci
== (u_int
)-1)
7504 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7509 if (off_proto
== (u_int
)-1)
7510 abort(); /* XXX - this isn't on FreeBSD */
7511 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7516 if (off_payload
== (u_int
)-1)
7518 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7519 0xffffffff, jtype
, reverse
, jvalue
);
7524 bpf_error("'callref' supported only on raw ATM");
7525 if (off_proto
== (u_int
)-1)
7527 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7528 jtype
, reverse
, jvalue
);
7538 gen_atmtype_abbrev(type
)
7541 struct block
*b0
, *b1
;
7546 /* Get all packets in Meta signalling Circuit */
7548 bpf_error("'metac' supported only on raw ATM");
7549 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7550 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7555 /* Get all packets in Broadcast Circuit*/
7557 bpf_error("'bcc' supported only on raw ATM");
7558 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7559 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7564 /* Get all cells in Segment OAM F4 circuit*/
7566 bpf_error("'oam4sc' supported only on raw ATM");
7567 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7568 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7573 /* Get all cells in End-to-End OAM F4 Circuit*/
7575 bpf_error("'oam4ec' supported only on raw ATM");
7576 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7577 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7582 /* Get all packets in connection Signalling Circuit */
7584 bpf_error("'sc' supported only on raw ATM");
7585 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7586 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7591 /* Get all packets in ILMI Circuit */
7593 bpf_error("'ilmic' supported only on raw ATM");
7594 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7595 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7600 /* Get all LANE packets */
7602 bpf_error("'lane' supported only on raw ATM");
7603 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7606 * Arrange that all subsequent tests assume LANE
7607 * rather than LLC-encapsulated packets, and set
7608 * the offsets appropriately for LANE-encapsulated
7611 * "off_mac" is the offset of the Ethernet header,
7612 * which is 2 bytes past the ATM pseudo-header
7613 * (skipping the pseudo-header and 2-byte LE Client
7614 * field). The other offsets are Ethernet offsets
7615 * relative to "off_mac".
7618 off_mac
= off_payload
+ 2; /* MAC header */
7619 off_linktype
= off_mac
+ 12;
7620 off_macpl
= off_mac
+ 14; /* Ethernet */
7621 off_nl
= 0; /* Ethernet II */
7622 off_nl_nosnap
= 3; /* 802.3+802.2 */
7626 /* Get all LLC-encapsulated packets */
7628 bpf_error("'llc' supported only on raw ATM");
7629 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7640 * Filtering for MTP2 messages based on li value
7641 * FISU, length is null
7642 * LSSU, length is 1 or 2
7643 * MSU, length is 3 or more
7646 gen_mtp2type_abbrev(type
)
7649 struct block
*b0
, *b1
;
7654 if ( (linktype
!= DLT_MTP2
) &&
7655 (linktype
!= DLT_ERF
) &&
7656 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7657 bpf_error("'fisu' supported only on MTP2");
7658 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7659 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7663 if ( (linktype
!= DLT_MTP2
) &&
7664 (linktype
!= DLT_ERF
) &&
7665 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7666 bpf_error("'lssu' supported only on MTP2");
7667 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7668 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7673 if ( (linktype
!= DLT_MTP2
) &&
7674 (linktype
!= DLT_ERF
) &&
7675 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7676 bpf_error("'msu' supported only on MTP2");
7677 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7687 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7694 bpf_u_int32 val1
, val2
, val3
;
7696 switch (mtp3field
) {
7699 if (off_sio
== (u_int
)-1)
7700 bpf_error("'sio' supported only on SS7");
7701 /* sio coded on 1 byte so max value 255 */
7703 bpf_error("sio value %u too big; max value = 255",
7705 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7706 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7710 if (off_opc
== (u_int
)-1)
7711 bpf_error("'opc' supported only on SS7");
7712 /* opc coded on 14 bits so max value 16383 */
7714 bpf_error("opc value %u too big; max value = 16383",
7716 /* the following instructions are made to convert jvalue
7717 * to the form used to write opc in an ss7 message*/
7718 val1
= jvalue
& 0x00003c00;
7720 val2
= jvalue
& 0x000003fc;
7722 val3
= jvalue
& 0x00000003;
7724 jvalue
= val1
+ val2
+ val3
;
7725 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7726 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7730 if (off_dpc
== (u_int
)-1)
7731 bpf_error("'dpc' supported only on SS7");
7732 /* dpc coded on 14 bits so max value 16383 */
7734 bpf_error("dpc value %u too big; max value = 16383",
7736 /* the following instructions are made to convert jvalue
7737 * to the forme used to write dpc in an ss7 message*/
7738 val1
= jvalue
& 0x000000ff;
7740 val2
= jvalue
& 0x00003f00;
7742 jvalue
= val1
+ val2
;
7743 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7744 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7748 if (off_sls
== (u_int
)-1)
7749 bpf_error("'sls' supported only on SS7");
7750 /* sls coded on 4 bits so max value 15 */
7752 bpf_error("sls value %u too big; max value = 15",
7754 /* the following instruction is made to convert jvalue
7755 * to the forme used to write sls in an ss7 message*/
7756 jvalue
= jvalue
<< 4;
7757 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7758 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7767 static struct block
*
7768 gen_msg_abbrev(type
)
7774 * Q.2931 signalling protocol messages for handling virtual circuits
7775 * establishment and teardown
7780 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7784 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7788 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7792 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7796 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7799 case A_RELEASE_DONE
:
7800 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7810 gen_atmmulti_abbrev(type
)
7813 struct block
*b0
, *b1
;
7819 bpf_error("'oam' supported only on raw ATM");
7820 b1
= gen_atmmulti_abbrev(A_OAMF4
);
7825 bpf_error("'oamf4' supported only on raw ATM");
7827 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7828 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7830 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7836 * Get Q.2931 signalling messages for switched
7837 * virtual connection
7840 bpf_error("'connectmsg' supported only on raw ATM");
7841 b0
= gen_msg_abbrev(A_SETUP
);
7842 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7844 b0
= gen_msg_abbrev(A_CONNECT
);
7846 b0
= gen_msg_abbrev(A_CONNECTACK
);
7848 b0
= gen_msg_abbrev(A_RELEASE
);
7850 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7852 b0
= gen_atmtype_abbrev(A_SC
);
7858 bpf_error("'metaconnect' supported only on raw ATM");
7859 b0
= gen_msg_abbrev(A_SETUP
);
7860 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7862 b0
= gen_msg_abbrev(A_CONNECT
);
7864 b0
= gen_msg_abbrev(A_RELEASE
);
7866 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7868 b0
= gen_atmtype_abbrev(A_METAC
);