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.301 2007-11-10 21:53:05 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
;
2278 struct slist
*sjset_radiotap_flags
;
2279 struct slist
*sjset_radiotap_tsft
;
2280 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2281 struct slist
*s_roundup
;
2283 if (reg_off_macpl
== -1) {
2285 * No register has been assigned to the offset of
2286 * the MAC-layer payload, which means nobody needs
2287 * it; don't bother computing it - just return
2288 * what we already have.
2294 * This code is not compatible with the optimizer, as
2295 * we are generating jmp instructions within a normal
2296 * slist of instructions
2301 * If "s" is non-null, it has code to arrange that the X register
2302 * contains the length of the prefix preceding the link-layer
2305 * Otherwise, the length of the prefix preceding the link-layer
2306 * header is "off_ll".
2310 * There is no variable-length header preceding the
2311 * link-layer header.
2313 * Load the length of the fixed-length prefix preceding
2314 * the link-layer header (if any) into the X register,
2315 * and store it in the reg_off_macpl register.
2316 * That length is off_ll.
2318 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2323 * The X register contains the offset of the beginning of the
2324 * link-layer header; add 24, which is the minimum length
2325 * of the MAC header for a data frame, to that, and store it
2326 * in reg_off_macpl, and then load the Frame Control field,
2327 * which is at the offset in the X register, with an indexed load.
2329 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2331 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2334 s2
= new_stmt(BPF_ST
);
2335 s2
->s
.k
= reg_off_macpl
;
2338 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2343 * Check the Frame Control field to see if this is a data frame;
2344 * a data frame has the 0x08 bit (b3) in that field set and the
2345 * 0x04 bit (b2) clear.
2347 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2348 sjset_data_frame_1
->s
.k
= 0x08;
2349 sappend(s
, sjset_data_frame_1
);
2352 * If b3 is set, test b2, otherwise go to the first statement of
2353 * the rest of the program.
2355 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2356 sjset_data_frame_2
->s
.k
= 0x04;
2357 sappend(s
, sjset_data_frame_2
);
2358 sjset_data_frame_1
->s
.jf
= snext
;
2361 * If b2 is not set, this is a data frame; test the QoS bit.
2362 * Otherwise, go to the first statement of the rest of the
2365 sjset_data_frame_2
->s
.jt
= snext
;
2366 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2367 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2368 sappend(s
, sjset_qos
);
2371 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2373 * Otherwise, go to the first statement of the rest of the
2376 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2377 s2
->s
.k
= reg_off_macpl
;
2379 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2382 s2
= new_stmt(BPF_ST
);
2383 s2
->s
.k
= reg_off_macpl
;
2387 * If we have a radiotap header, look at it to see whether
2388 * there's Atheros padding between the MAC-layer header
2391 * Note: all of the fields in the radiotap header are
2392 * little-endian, so we byte-swap all of the values
2393 * we test against, as they will be loaded as big-endian
2396 if (linktype
== DLT_IEEE802_11_RADIO
) {
2398 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2399 * in the presence flag?
2401 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2405 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2406 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2407 sappend(s
, sjset_radiotap_flags
);
2410 * If not, skip all of this.
2412 sjset_radiotap_flags
->s
.jf
= snext
;
2415 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2417 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2418 new_stmt(JMP(BPF_JSET
));
2419 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2420 sappend(s
, sjset_radiotap_tsft
);
2423 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2424 * at an offset of 16 from the beginning of the raw packet
2425 * data (8 bytes for the radiotap header and 8 bytes for
2428 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2431 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2435 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2436 sjset_tsft_datapad
->s
.k
= 0x20;
2437 sappend(s
, sjset_tsft_datapad
);
2440 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2441 * at an offset of 8 from the beginning of the raw packet
2442 * data (8 bytes for the radiotap header).
2444 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2447 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2451 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2452 sjset_notsft_datapad
->s
.k
= 0x20;
2453 sappend(s
, sjset_notsft_datapad
);
2456 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2457 * set, round the length of the 802.11 header to
2458 * a multiple of 4. Do that by adding 3 and then
2459 * dividing by and multiplying by 4, which we do by
2462 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2463 s_roundup
->s
.k
= reg_off_macpl
;
2464 sappend(s
, s_roundup
);
2465 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2468 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2471 s2
= new_stmt(BPF_ST
);
2472 s2
->s
.k
= reg_off_macpl
;
2475 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2476 sjset_tsft_datapad
->s
.jf
= snext
;
2477 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2478 sjset_notsft_datapad
->s
.jf
= snext
;
2480 sjset_qos
->s
.jf
= snext
;
2486 insert_compute_vloffsets(b
)
2492 * For link-layer types that have a variable-length header
2493 * preceding the link-layer header, generate code to load
2494 * the offset of the link-layer header into the register
2495 * assigned to that offset, if any.
2499 case DLT_IEEE802_11_RADIO
:
2500 s
= gen_load_radiotap_llprefixlen();
2504 s
= gen_load_ppi_llprefixlen();
2513 * For link-layer types that have a variable-length link-layer
2514 * header, generate code to load the offset of the MAC-layer
2515 * payload into the register assigned to that offset, if any.
2519 case DLT_IEEE802_11
:
2520 case DLT_IEEE802_11_RADIO
:
2521 case DLT_IEEE802_11_RADIO_AVS
:
2522 case DLT_PRISM_HEADER
:
2524 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2529 * If we have any offset-loading code, append all the
2530 * existing statements in the block to those statements,
2531 * and make the resulting list the list of statements
2535 sappend(s
, b
->stmts
);
2540 static struct block
*
2541 gen_ppi_dlt_check(void)
2543 struct slist
*s_load_dlt
;
2546 if (linktype
== DLT_PPI
)
2548 /* Create the statements that check for the DLT
2550 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2551 s_load_dlt
->s
.k
= 4;
2553 b
= new_block(JMP(BPF_JEQ
));
2555 b
->stmts
= s_load_dlt
;
2556 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2566 static struct slist
*
2567 gen_radiotap_llprefixlen(void)
2571 if (reg_off_ll
== -1) {
2573 * We haven't yet assigned a register for the length
2574 * of the radiotap header; allocate one.
2576 reg_off_ll
= alloc_reg();
2580 * Load the register containing the radiotap length
2581 * into the X register.
2583 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2584 s
->s
.k
= reg_off_ll
;
2589 * At the moment we treat PPI as normal Radiotap encoded
2590 * packets. The difference is in the function that generates
2591 * the code at the beginning to compute the header length.
2592 * Since this code generator of PPI supports bare 802.11
2593 * encapsulation only (i.e. the encapsulated DLT should be
2594 * DLT_IEEE802_11) we generate code to check for this too.
2596 static struct slist
*
2597 gen_ppi_llprefixlen(void)
2601 if (reg_off_ll
== -1) {
2603 * We haven't yet assigned a register for the length
2604 * of the radiotap header; allocate one.
2606 reg_off_ll
= alloc_reg();
2610 * Load the register containing the radiotap length
2611 * into the X register.
2613 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2614 s
->s
.k
= reg_off_ll
;
2619 * Generate code to compute the link-layer header length, if necessary,
2620 * putting it into the X register, and to return either a pointer to a
2621 * "struct slist" for the list of statements in that code, or NULL if
2622 * no code is necessary.
2624 static struct slist
*
2625 gen_llprefixlen(void)
2630 return gen_ppi_llprefixlen();
2632 case DLT_IEEE802_11_RADIO
:
2633 return gen_radiotap_llprefixlen();
2641 * Generate code to load the register containing the offset of the
2642 * MAC-layer payload into the X register; if no register for that offset
2643 * has been allocated, allocate it first.
2645 static struct slist
*
2650 if (off_macpl_is_variable
) {
2651 if (reg_off_macpl
== -1) {
2653 * We haven't yet assigned a register for the offset
2654 * of the MAC-layer payload; allocate one.
2656 reg_off_macpl
= alloc_reg();
2660 * Load the register containing the offset of the MAC-layer
2661 * payload into the X register.
2663 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2664 s
->s
.k
= reg_off_macpl
;
2668 * That offset isn't variable, so we don't need to
2669 * generate any code.
2676 * Map an Ethernet type to the equivalent PPP type.
2679 ethertype_to_ppptype(proto
)
2689 case ETHERTYPE_IPV6
:
2698 case ETHERTYPE_ATALK
:
2712 * I'm assuming the "Bridging PDU"s that go
2713 * over PPP are Spanning Tree Protocol
2727 * Generate code to match a particular packet type by matching the
2728 * link-layer type field or fields in the 802.2 LLC header.
2730 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2731 * value, if <= ETHERMTU.
2733 static struct block
*
2737 struct block
*b0
, *b1
, *b2
;
2739 /* are we checking MPLS-encapsulated packets? */
2740 if (label_stack_depth
> 0) {
2744 /* FIXME add other L3 proto IDs */
2745 return gen_mpls_linktype(Q_IP
);
2747 case ETHERTYPE_IPV6
:
2749 /* FIXME add other L3 proto IDs */
2750 return gen_mpls_linktype(Q_IPV6
);
2753 bpf_error("unsupported protocol over mpls");
2759 * Are we testing PPPoE packets?
2763 * The PPPoE session header is part of the
2764 * MAC-layer payload, so all references
2765 * should be relative to the beginning of
2770 * We use Ethernet protocol types inside libpcap;
2771 * map them to the corresponding PPP protocol types.
2773 proto
= ethertype_to_ppptype(proto
);
2774 return gen_cmp(OR_MACPL
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2780 return gen_ether_linktype(proto
);
2788 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2792 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2800 case DLT_IEEE802_11
:
2801 case DLT_IEEE802_11_RADIO
:
2802 case DLT_IEEE802_11_RADIO_AVS
:
2803 case DLT_PRISM_HEADER
:
2805 * Check that we have a data frame.
2807 b0
= gen_check_802_11_data_frame();
2810 * Now check for the specified link-layer type.
2812 b1
= gen_llc_linktype(proto
);
2820 * XXX - check for asynchronous frames, as per RFC 1103.
2822 return gen_llc_linktype(proto
);
2828 * XXX - check for LLC PDUs, as per IEEE 802.5.
2830 return gen_llc_linktype(proto
);
2834 case DLT_ATM_RFC1483
:
2836 case DLT_IP_OVER_FC
:
2837 return gen_llc_linktype(proto
);
2843 * If "is_lane" is set, check for a LANE-encapsulated
2844 * version of this protocol, otherwise check for an
2845 * LLC-encapsulated version of this protocol.
2847 * We assume LANE means Ethernet, not Token Ring.
2851 * Check that the packet doesn't begin with an
2852 * LE Control marker. (We've already generated
2855 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2860 * Now generate an Ethernet test.
2862 b1
= gen_ether_linktype(proto
);
2867 * Check for LLC encapsulation and then check the
2870 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2871 b1
= gen_llc_linktype(proto
);
2879 return gen_linux_sll_linktype(proto
);
2884 case DLT_SLIP_BSDOS
:
2887 * These types don't provide any type field; packets
2888 * are always IPv4 or IPv6.
2890 * XXX - for IPv4, check for a version number of 4, and,
2891 * for IPv6, check for a version number of 6?
2896 /* Check for a version number of 4. */
2897 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x40, 0xF0);
2899 case ETHERTYPE_IPV6
:
2900 /* Check for a version number of 6. */
2901 return gen_mcmp(OR_LINK
, 0, BPF_B
, 0x60, 0xF0);
2905 return gen_false(); /* always false */
2912 case DLT_PPP_SERIAL
:
2915 * We use Ethernet protocol types inside libpcap;
2916 * map them to the corresponding PPP protocol types.
2918 proto
= ethertype_to_ppptype(proto
);
2919 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2925 * We use Ethernet protocol types inside libpcap;
2926 * map them to the corresponding PPP protocol types.
2932 * Also check for Van Jacobson-compressed IP.
2933 * XXX - do this for other forms of PPP?
2935 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2936 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2938 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2943 proto
= ethertype_to_ppptype(proto
);
2944 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
2954 * For DLT_NULL, the link-layer header is a 32-bit
2955 * word containing an AF_ value in *host* byte order,
2956 * and for DLT_ENC, the link-layer header begins
2957 * with a 32-bit work containing an AF_ value in
2960 * In addition, if we're reading a saved capture file,
2961 * the host byte order in the capture may not be the
2962 * same as the host byte order on this machine.
2964 * For DLT_LOOP, the link-layer header is a 32-bit
2965 * word containing an AF_ value in *network* byte order.
2967 * XXX - AF_ values may, unfortunately, be platform-
2968 * dependent; for example, FreeBSD's AF_INET6 is 24
2969 * whilst NetBSD's and OpenBSD's is 26.
2971 * This means that, when reading a capture file, just
2972 * checking for our AF_INET6 value won't work if the
2973 * capture file came from another OS.
2982 case ETHERTYPE_IPV6
:
2989 * Not a type on which we support filtering.
2990 * XXX - support those that have AF_ values
2991 * #defined on this platform, at least?
2996 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2998 * The AF_ value is in host byte order, but
2999 * the BPF interpreter will convert it to
3000 * network byte order.
3002 * If this is a save file, and it's from a
3003 * machine with the opposite byte order to
3004 * ours, we byte-swap the AF_ value.
3006 * Then we run it through "htonl()", and
3007 * generate code to compare against the result.
3009 if (bpf_pcap
->sf
.rfile
!= NULL
&&
3010 bpf_pcap
->sf
.swapped
)
3011 proto
= SWAPLONG(proto
);
3012 proto
= htonl(proto
);
3014 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
3016 #ifdef HAVE_NET_PFVAR_H
3019 * af field is host byte order in contrast to the rest of
3022 if (proto
== ETHERTYPE_IP
)
3023 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3024 BPF_B
, (bpf_int32
)AF_INET
));
3026 else if (proto
== ETHERTYPE_IPV6
)
3027 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
3028 BPF_B
, (bpf_int32
)AF_INET6
));
3034 #endif /* HAVE_NET_PFVAR_H */
3037 case DLT_ARCNET_LINUX
:
3039 * XXX should we check for first fragment if the protocol
3048 case ETHERTYPE_IPV6
:
3049 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3050 (bpf_int32
)ARCTYPE_INET6
));
3054 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3055 (bpf_int32
)ARCTYPE_IP
);
3056 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3057 (bpf_int32
)ARCTYPE_IP_OLD
);
3062 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3063 (bpf_int32
)ARCTYPE_ARP
);
3064 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3065 (bpf_int32
)ARCTYPE_ARP_OLD
);
3069 case ETHERTYPE_REVARP
:
3070 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3071 (bpf_int32
)ARCTYPE_REVARP
));
3073 case ETHERTYPE_ATALK
:
3074 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
3075 (bpf_int32
)ARCTYPE_ATALK
));
3082 case ETHERTYPE_ATALK
:
3092 * XXX - assumes a 2-byte Frame Relay header with
3093 * DLCI and flags. What if the address is longer?
3099 * Check for the special NLPID for IP.
3101 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
3104 case ETHERTYPE_IPV6
:
3106 * Check for the special NLPID for IPv6.
3108 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
3113 * Check for several OSI protocols.
3115 * Frame Relay packets typically have an OSI
3116 * NLPID at the beginning; we check for each
3119 * What we check for is the NLPID and a frame
3120 * control field of UI, i.e. 0x03 followed
3123 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3124 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3125 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3137 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3139 case DLT_JUNIPER_MFR
:
3140 case DLT_JUNIPER_MLFR
:
3141 case DLT_JUNIPER_MLPPP
:
3142 case DLT_JUNIPER_ATM1
:
3143 case DLT_JUNIPER_ATM2
:
3144 case DLT_JUNIPER_PPPOE
:
3145 case DLT_JUNIPER_PPPOE_ATM
:
3146 case DLT_JUNIPER_GGSN
:
3147 case DLT_JUNIPER_ES
:
3148 case DLT_JUNIPER_MONITOR
:
3149 case DLT_JUNIPER_SERVICES
:
3150 case DLT_JUNIPER_ETHER
:
3151 case DLT_JUNIPER_PPP
:
3152 case DLT_JUNIPER_FRELAY
:
3153 case DLT_JUNIPER_CHDLC
:
3154 case DLT_JUNIPER_VP
:
3155 case DLT_JUNIPER_ST
:
3156 case DLT_JUNIPER_ISM
:
3157 /* just lets verify the magic number for now -
3158 * on ATM we may have up to 6 different encapsulations on the wire
3159 * and need a lot of heuristics to figure out that the payload
3162 * FIXME encapsulation specific BPF_ filters
3164 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3166 case DLT_LINUX_IRDA
:
3167 bpf_error("IrDA link-layer type filtering not implemented");
3170 bpf_error("DOCSIS link-layer type filtering not implemented");
3173 case DLT_MTP2_WITH_PHDR
:
3174 bpf_error("MTP2 link-layer type filtering not implemented");
3177 bpf_error("ERF link-layer type filtering not implemented");
3181 bpf_error("PFSYNC link-layer type filtering not implemented");
3184 case DLT_LINUX_LAPD
:
3185 bpf_error("LAPD link-layer type filtering not implemented");
3189 bpf_error("USB link-layer type filtering not implemented");
3191 case DLT_BLUETOOTH_HCI_H4
:
3192 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3193 bpf_error("Bluetooth link-layer type filtering not implemented");
3196 bpf_error("CAN20B link-layer type filtering not implemented");
3198 case DLT_IEEE802_15_4
:
3199 case DLT_IEEE802_15_4_LINUX
:
3200 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3202 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3203 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3206 bpf_error("SITA link-layer type filtering not implemented");
3209 bpf_error("RAIF1 link-layer type filtering not implemented");
3212 bpf_error("IPMB link-layer type filtering not implemented");
3215 bpf_error("AX.25 link-layer type filtering not implemented");
3219 * All the types that have no encapsulation should either be
3220 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3221 * all packets are IP packets, or should be handled in some
3222 * special case, if none of them are (if some are and some
3223 * aren't, the lack of encapsulation is a problem, as we'd
3224 * have to find some other way of determining the packet type).
3226 * Therefore, if "off_linktype" is -1, there's an error.
3228 if (off_linktype
== (u_int
)-1)
3232 * Any type not handled above should always have an Ethernet
3233 * type at an offset of "off_linktype".
3235 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
3239 * Check for an LLC SNAP packet with a given organization code and
3240 * protocol type; we check the entire contents of the 802.2 LLC and
3241 * snap headers, checking for DSAP and SSAP of SNAP and a control
3242 * field of 0x03 in the LLC header, and for the specified organization
3243 * code and protocol type in the SNAP header.
3245 static struct block
*
3246 gen_snap(orgcode
, ptype
)
3247 bpf_u_int32 orgcode
;
3250 u_char snapblock
[8];
3252 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3253 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3254 snapblock
[2] = 0x03; /* control = UI */
3255 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3256 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3257 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3258 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3259 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3260 return gen_bcmp(OR_MACPL
, 0, 8, snapblock
);
3264 * Generate code to match a particular packet type, for link-layer types
3265 * using 802.2 LLC headers.
3267 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3268 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3270 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3271 * value, if <= ETHERMTU. We use that to determine whether to
3272 * match the DSAP or both DSAP and LSAP or to check the OUI and
3273 * protocol ID in a SNAP header.
3275 static struct block
*
3276 gen_llc_linktype(proto
)
3280 * XXX - handle token-ring variable-length header.
3286 case LLCSAP_NETBEUI
:
3288 * XXX - should we check both the DSAP and the
3289 * SSAP, like this, or should we check just the
3290 * DSAP, as we do for other types <= ETHERMTU
3291 * (i.e., other SAP values)?
3293 return gen_cmp(OR_MACPL
, 0, BPF_H
, (bpf_u_int32
)
3294 ((proto
<< 8) | proto
));
3298 * XXX - are there ever SNAP frames for IPX on
3299 * non-Ethernet 802.x networks?
3301 return gen_cmp(OR_MACPL
, 0, BPF_B
,
3302 (bpf_int32
)LLCSAP_IPX
);
3304 case ETHERTYPE_ATALK
:
3306 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3307 * SNAP packets with an organization code of
3308 * 0x080007 (Apple, for Appletalk) and a protocol
3309 * type of ETHERTYPE_ATALK (Appletalk).
3311 * XXX - check for an organization code of
3312 * encapsulated Ethernet as well?
3314 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3318 * XXX - we don't have to check for IPX 802.3
3319 * here, but should we check for the IPX Ethertype?
3321 if (proto
<= ETHERMTU
) {
3323 * This is an LLC SAP value, so check
3326 return gen_cmp(OR_MACPL
, 0, BPF_B
, (bpf_int32
)proto
);
3329 * This is an Ethernet type; we assume that it's
3330 * unlikely that it'll appear in the right place
3331 * at random, and therefore check only the
3332 * location that would hold the Ethernet type
3333 * in a SNAP frame with an organization code of
3334 * 0x000000 (encapsulated Ethernet).
3336 * XXX - if we were to check for the SNAP DSAP and
3337 * LSAP, as per XXX, and were also to check for an
3338 * organization code of 0x000000 (encapsulated
3339 * Ethernet), we'd do
3341 * return gen_snap(0x000000, proto);
3343 * here; for now, we don't, as per the above.
3344 * I don't know whether it's worth the extra CPU
3345 * time to do the right check or not.
3347 return gen_cmp(OR_MACPL
, 6, BPF_H
, (bpf_int32
)proto
);
3352 static struct block
*
3353 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3357 u_int src_off
, dst_off
;
3359 struct block
*b0
, *b1
;
3373 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3374 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3380 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3381 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3388 b0
= gen_linktype(proto
);
3389 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3395 static struct block
*
3396 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3397 struct in6_addr
*addr
;
3398 struct in6_addr
*mask
;
3400 u_int src_off
, dst_off
;
3402 struct block
*b0
, *b1
;
3417 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3418 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3424 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3425 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3432 /* this order is important */
3433 a
= (u_int32_t
*)addr
;
3434 m
= (u_int32_t
*)mask
;
3435 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3436 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3438 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3440 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3442 b0
= gen_linktype(proto
);
3448 static struct block
*
3449 gen_ehostop(eaddr
, dir
)
3450 register const u_char
*eaddr
;
3453 register struct block
*b0
, *b1
;
3457 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
3460 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
3463 b0
= gen_ehostop(eaddr
, Q_SRC
);
3464 b1
= gen_ehostop(eaddr
, Q_DST
);
3470 b0
= gen_ehostop(eaddr
, Q_SRC
);
3471 b1
= gen_ehostop(eaddr
, Q_DST
);
3480 * Like gen_ehostop, but for DLT_FDDI
3482 static struct block
*
3483 gen_fhostop(eaddr
, dir
)
3484 register const u_char
*eaddr
;
3487 struct block
*b0
, *b1
;
3492 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3494 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
3499 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3501 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
3505 b0
= gen_fhostop(eaddr
, Q_SRC
);
3506 b1
= gen_fhostop(eaddr
, Q_DST
);
3512 b0
= gen_fhostop(eaddr
, Q_SRC
);
3513 b1
= gen_fhostop(eaddr
, Q_DST
);
3522 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3524 static struct block
*
3525 gen_thostop(eaddr
, dir
)
3526 register const u_char
*eaddr
;
3529 register struct block
*b0
, *b1
;
3533 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
3536 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3539 b0
= gen_thostop(eaddr
, Q_SRC
);
3540 b1
= gen_thostop(eaddr
, Q_DST
);
3546 b0
= gen_thostop(eaddr
, Q_SRC
);
3547 b1
= gen_thostop(eaddr
, Q_DST
);
3556 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3557 * various 802.11 + radio headers.
3559 static struct block
*
3560 gen_wlanhostop(eaddr
, dir
)
3561 register const u_char
*eaddr
;
3564 register struct block
*b0
, *b1
, *b2
;
3565 register struct slist
*s
;
3567 #ifdef ENABLE_WLAN_FILTERING_PATCH
3570 * We need to disable the optimizer because the optimizer is buggy
3571 * and wipes out some LD instructions generated by the below
3572 * code to validate the Frame Control bits
3575 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3582 * For control frames, there is no SA.
3584 * For management frames, SA is at an
3585 * offset of 10 from the beginning of
3588 * For data frames, SA is at an offset
3589 * of 10 from the beginning of the packet
3590 * if From DS is clear, at an offset of
3591 * 16 from the beginning of the packet
3592 * if From DS is set and To DS is clear,
3593 * and an offset of 24 from the beginning
3594 * of the packet if From DS is set and To DS
3599 * Generate the tests to be done for data frames
3602 * First, check for To DS set, i.e. check "link[1] & 0x01".
3604 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3605 b1
= new_block(JMP(BPF_JSET
));
3606 b1
->s
.k
= 0x01; /* To DS */
3610 * If To DS is set, the SA is at 24.
3612 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
3616 * Now, check for To DS not set, i.e. check
3617 * "!(link[1] & 0x01)".
3619 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3620 b2
= new_block(JMP(BPF_JSET
));
3621 b2
->s
.k
= 0x01; /* To DS */
3626 * If To DS is not set, the SA is at 16.
3628 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3632 * Now OR together the last two checks. That gives
3633 * the complete set of checks for data frames with
3639 * Now check for From DS being set, and AND that with
3640 * the ORed-together checks.
3642 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3643 b1
= new_block(JMP(BPF_JSET
));
3644 b1
->s
.k
= 0x02; /* From DS */
3649 * Now check for data frames with From DS not set.
3651 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3652 b2
= new_block(JMP(BPF_JSET
));
3653 b2
->s
.k
= 0x02; /* From DS */
3658 * If From DS isn't set, the SA is at 10.
3660 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3664 * Now OR together the checks for data frames with
3665 * From DS not set and for data frames with From DS
3666 * set; that gives the checks done for data frames.
3671 * Now check for a data frame.
3672 * I.e, check "link[0] & 0x08".
3674 gen_load_a(OR_LINK
, 0, BPF_B
);
3675 b1
= new_block(JMP(BPF_JSET
));
3680 * AND that with the checks done for data frames.
3685 * If the high-order bit of the type value is 0, this
3686 * is a management frame.
3687 * I.e, check "!(link[0] & 0x08)".
3689 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3690 b2
= new_block(JMP(BPF_JSET
));
3696 * For management frames, the SA is at 10.
3698 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3702 * OR that with the checks done for data frames.
3703 * That gives the checks done for management and
3709 * If the low-order bit of the type value is 1,
3710 * this is either a control frame or a frame
3711 * with a reserved type, and thus not a
3714 * I.e., check "!(link[0] & 0x04)".
3716 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3717 b1
= new_block(JMP(BPF_JSET
));
3723 * AND that with the checks for data and management
3733 * For control frames, there is no DA.
3735 * For management frames, DA is at an
3736 * offset of 4 from the beginning of
3739 * For data frames, DA is at an offset
3740 * of 4 from the beginning of the packet
3741 * if To DS is clear and at an offset of
3742 * 16 from the beginning of the packet
3747 * Generate the tests to be done for data frames.
3749 * First, check for To DS set, i.e. "link[1] & 0x01".
3751 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3752 b1
= new_block(JMP(BPF_JSET
));
3753 b1
->s
.k
= 0x01; /* To DS */
3757 * If To DS is set, the DA is at 16.
3759 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
3763 * Now, check for To DS not set, i.e. check
3764 * "!(link[1] & 0x01)".
3766 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
3767 b2
= new_block(JMP(BPF_JSET
));
3768 b2
->s
.k
= 0x01; /* To DS */
3773 * If To DS is not set, the DA is at 4.
3775 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3779 * Now OR together the last two checks. That gives
3780 * the complete set of checks for data frames.
3785 * Now check for a data frame.
3786 * I.e, check "link[0] & 0x08".
3788 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3789 b1
= new_block(JMP(BPF_JSET
));
3794 * AND that with the checks done for data frames.
3799 * If the high-order bit of the type value is 0, this
3800 * is a management frame.
3801 * I.e, check "!(link[0] & 0x08)".
3803 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3804 b2
= new_block(JMP(BPF_JSET
));
3810 * For management frames, the DA is at 4.
3812 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
3816 * OR that with the checks done for data frames.
3817 * That gives the checks done for management and
3823 * If the low-order bit of the type value is 1,
3824 * this is either a control frame or a frame
3825 * with a reserved type, and thus not a
3828 * I.e., check "!(link[0] & 0x04)".
3830 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
3831 b1
= new_block(JMP(BPF_JSET
));
3837 * AND that with the checks for data and management
3844 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3845 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3851 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
3852 b1
= gen_wlanhostop(eaddr
, Q_DST
);
3861 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
3862 * (We assume that the addresses are IEEE 48-bit MAC addresses,
3863 * as the RFC states.)
3865 static struct block
*
3866 gen_ipfchostop(eaddr
, dir
)
3867 register const u_char
*eaddr
;
3870 register struct block
*b0
, *b1
;
3874 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
3877 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
3880 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3881 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3887 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
3888 b1
= gen_ipfchostop(eaddr
, Q_DST
);
3897 * This is quite tricky because there may be pad bytes in front of the
3898 * DECNET header, and then there are two possible data packet formats that
3899 * carry both src and dst addresses, plus 5 packet types in a format that
3900 * carries only the src node, plus 2 types that use a different format and
3901 * also carry just the src node.
3905 * Instead of doing those all right, we just look for data packets with
3906 * 0 or 1 bytes of padding. If you want to look at other packets, that
3907 * will require a lot more hacking.
3909 * To add support for filtering on DECNET "areas" (network numbers)
3910 * one would want to add a "mask" argument to this routine. That would
3911 * make the filter even more inefficient, although one could be clever
3912 * and not generate masking instructions if the mask is 0xFFFF.
3914 static struct block
*
3915 gen_dnhostop(addr
, dir
)
3919 struct block
*b0
, *b1
, *b2
, *tmp
;
3920 u_int offset_lh
; /* offset if long header is received */
3921 u_int offset_sh
; /* offset if short header is received */
3926 offset_sh
= 1; /* follows flags */
3927 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3931 offset_sh
= 3; /* follows flags, dstnode */
3932 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3936 /* Inefficient because we do our Calvinball dance twice */
3937 b0
= gen_dnhostop(addr
, Q_SRC
);
3938 b1
= gen_dnhostop(addr
, Q_DST
);
3944 /* Inefficient because we do our Calvinball dance twice */
3945 b0
= gen_dnhostop(addr
, Q_SRC
);
3946 b1
= gen_dnhostop(addr
, Q_DST
);
3951 bpf_error("ISO host filtering not implemented");
3956 b0
= gen_linktype(ETHERTYPE_DN
);
3957 /* Check for pad = 1, long header case */
3958 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3959 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3960 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3961 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3963 /* Check for pad = 0, long header case */
3964 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3965 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3968 /* Check for pad = 1, short header case */
3969 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3970 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3971 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3974 /* Check for pad = 0, short header case */
3975 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3976 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
3980 /* Combine with test for linktype */
3986 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
3987 * test the bottom-of-stack bit, and then check the version number
3988 * field in the IP header.
3990 static struct block
*
3991 gen_mpls_linktype(proto
)
3994 struct block
*b0
, *b1
;
3999 /* match the bottom-of-stack bit */
4000 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4001 /* match the IPv4 version number */
4002 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
4007 /* match the bottom-of-stack bit */
4008 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
4009 /* match the IPv4 version number */
4010 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
4019 static struct block
*
4020 gen_host(addr
, mask
, proto
, dir
, type
)
4027 struct block
*b0
, *b1
;
4028 const char *typestr
;
4038 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4040 * Only check for non-IPv4 addresses if we're not
4041 * checking MPLS-encapsulated packets.
4043 if (label_stack_depth
== 0) {
4044 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4046 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4052 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4055 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4058 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4061 bpf_error("'tcp' modifier applied to %s", typestr
);
4064 bpf_error("'sctp' modifier applied to %s", typestr
);
4067 bpf_error("'udp' modifier applied to %s", typestr
);
4070 bpf_error("'icmp' modifier applied to %s", typestr
);
4073 bpf_error("'igmp' modifier applied to %s", typestr
);
4076 bpf_error("'igrp' modifier applied to %s", typestr
);
4079 bpf_error("'pim' modifier applied to %s", typestr
);
4082 bpf_error("'vrrp' modifier applied to %s", typestr
);
4085 bpf_error("ATALK host filtering not implemented");
4088 bpf_error("AARP host filtering not implemented");
4091 return gen_dnhostop(addr
, dir
);
4094 bpf_error("SCA host filtering not implemented");
4097 bpf_error("LAT host filtering not implemented");
4100 bpf_error("MOPDL host filtering not implemented");
4103 bpf_error("MOPRC host filtering not implemented");
4107 bpf_error("'ip6' modifier applied to ip host");
4110 bpf_error("'icmp6' modifier applied to %s", typestr
);
4114 bpf_error("'ah' modifier applied to %s", typestr
);
4117 bpf_error("'esp' modifier applied to %s", typestr
);
4120 bpf_error("ISO host filtering not implemented");
4123 bpf_error("'esis' modifier applied to %s", typestr
);
4126 bpf_error("'isis' modifier applied to %s", typestr
);
4129 bpf_error("'clnp' modifier applied to %s", typestr
);
4132 bpf_error("'stp' modifier applied to %s", typestr
);
4135 bpf_error("IPX host filtering not implemented");
4138 bpf_error("'netbeui' modifier applied to %s", typestr
);
4141 bpf_error("'radio' modifier applied to %s", typestr
);
4150 static struct block
*
4151 gen_host6(addr
, mask
, proto
, dir
, type
)
4152 struct in6_addr
*addr
;
4153 struct in6_addr
*mask
;
4158 const char *typestr
;
4168 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4171 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4174 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4177 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4180 bpf_error("'sctp' modifier applied to %s", typestr
);
4183 bpf_error("'tcp' modifier applied to %s", typestr
);
4186 bpf_error("'udp' modifier applied to %s", typestr
);
4189 bpf_error("'icmp' modifier applied to %s", typestr
);
4192 bpf_error("'igmp' modifier applied to %s", typestr
);
4195 bpf_error("'igrp' modifier applied to %s", typestr
);
4198 bpf_error("'pim' modifier applied to %s", typestr
);
4201 bpf_error("'vrrp' modifier applied to %s", typestr
);
4204 bpf_error("ATALK host filtering not implemented");
4207 bpf_error("AARP host filtering not implemented");
4210 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4213 bpf_error("SCA host filtering not implemented");
4216 bpf_error("LAT host filtering not implemented");
4219 bpf_error("MOPDL host filtering not implemented");
4222 bpf_error("MOPRC host filtering not implemented");
4225 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4228 bpf_error("'icmp6' modifier applied to %s", typestr
);
4231 bpf_error("'ah' modifier applied to %s", typestr
);
4234 bpf_error("'esp' modifier applied to %s", typestr
);
4237 bpf_error("ISO host filtering not implemented");
4240 bpf_error("'esis' modifier applied to %s", typestr
);
4243 bpf_error("'isis' modifier applied to %s", typestr
);
4246 bpf_error("'clnp' modifier applied to %s", typestr
);
4249 bpf_error("'stp' modifier applied to %s", typestr
);
4252 bpf_error("IPX host filtering not implemented");
4255 bpf_error("'netbeui' modifier applied to %s", typestr
);
4258 bpf_error("'radio' modifier applied to %s", typestr
);
4268 static struct block
*
4269 gen_gateway(eaddr
, alist
, proto
, dir
)
4270 const u_char
*eaddr
;
4271 bpf_u_int32
**alist
;
4275 struct block
*b0
, *b1
, *tmp
;
4278 bpf_error("direction applied to 'gateway'");
4287 b0
= gen_ehostop(eaddr
, Q_OR
);
4290 b0
= gen_fhostop(eaddr
, Q_OR
);
4293 b0
= gen_thostop(eaddr
, Q_OR
);
4295 case DLT_IEEE802_11
:
4296 case DLT_IEEE802_11_RADIO_AVS
:
4298 case DLT_IEEE802_11_RADIO
:
4299 case DLT_PRISM_HEADER
:
4300 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4305 * Check that the packet doesn't begin with an
4306 * LE Control marker. (We've already generated
4309 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4314 * Now check the MAC address.
4316 b0
= gen_ehostop(eaddr
, Q_OR
);
4320 case DLT_IP_OVER_FC
:
4321 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4325 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
4327 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4329 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4338 bpf_error("illegal modifier of 'gateway'");
4344 gen_proto_abbrev(proto
)
4353 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4355 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4361 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4363 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4369 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4371 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4377 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4380 #ifndef IPPROTO_IGMP
4381 #define IPPROTO_IGMP 2
4385 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4388 #ifndef IPPROTO_IGRP
4389 #define IPPROTO_IGRP 9
4392 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4396 #define IPPROTO_PIM 103
4400 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4402 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4407 #ifndef IPPROTO_VRRP
4408 #define IPPROTO_VRRP 112
4412 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4416 b1
= gen_linktype(ETHERTYPE_IP
);
4420 b1
= gen_linktype(ETHERTYPE_ARP
);
4424 b1
= gen_linktype(ETHERTYPE_REVARP
);
4428 bpf_error("link layer applied in wrong context");
4431 b1
= gen_linktype(ETHERTYPE_ATALK
);
4435 b1
= gen_linktype(ETHERTYPE_AARP
);
4439 b1
= gen_linktype(ETHERTYPE_DN
);
4443 b1
= gen_linktype(ETHERTYPE_SCA
);
4447 b1
= gen_linktype(ETHERTYPE_LAT
);
4451 b1
= gen_linktype(ETHERTYPE_MOPDL
);
4455 b1
= gen_linktype(ETHERTYPE_MOPRC
);
4460 b1
= gen_linktype(ETHERTYPE_IPV6
);
4463 #ifndef IPPROTO_ICMPV6
4464 #define IPPROTO_ICMPV6 58
4467 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
4472 #define IPPROTO_AH 51
4475 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
4477 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
4483 #define IPPROTO_ESP 50
4486 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
4488 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
4494 b1
= gen_linktype(LLCSAP_ISONS
);
4498 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
4502 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4505 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
4506 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4507 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4509 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4511 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4513 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4517 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
4518 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4519 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
4521 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4523 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4525 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4529 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
4530 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4531 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
4533 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
4538 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
4539 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
4544 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4545 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4547 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4549 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4554 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
4555 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
4560 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
4561 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
4566 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
4570 b1
= gen_linktype(LLCSAP_8021D
);
4574 b1
= gen_linktype(LLCSAP_IPX
);
4578 b1
= gen_linktype(LLCSAP_NETBEUI
);
4582 bpf_error("'radio' is not a valid protocol type");
4590 static struct block
*
4597 s
= gen_load_a(OR_NET
, 6, BPF_H
);
4598 b
= new_block(JMP(BPF_JSET
));
4607 * Generate a comparison to a port value in the transport-layer header
4608 * at the specified offset from the beginning of that header.
4610 * XXX - this handles a variable-length prefix preceding the link-layer
4611 * header, such as the radiotap or AVS radio prefix, but doesn't handle
4612 * variable-length link-layer headers (such as Token Ring or 802.11
4615 static struct block
*
4616 gen_portatom(off
, v
)
4620 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
4624 static struct block
*
4625 gen_portatom6(off
, v
)
4629 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
4634 gen_portop(port
, proto
, dir
)
4635 int port
, proto
, dir
;
4637 struct block
*b0
, *b1
, *tmp
;
4639 /* ip proto 'proto' */
4640 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4646 b1
= gen_portatom(0, (bpf_int32
)port
);
4650 b1
= gen_portatom(2, (bpf_int32
)port
);
4655 tmp
= gen_portatom(0, (bpf_int32
)port
);
4656 b1
= gen_portatom(2, (bpf_int32
)port
);
4661 tmp
= gen_portatom(0, (bpf_int32
)port
);
4662 b1
= gen_portatom(2, (bpf_int32
)port
);
4674 static struct block
*
4675 gen_port(port
, ip_proto
, dir
)
4680 struct block
*b0
, *b1
, *tmp
;
4685 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4686 * not LLC encapsulation with LLCSAP_IP.
4688 * For IEEE 802 networks - which includes 802.5 token ring
4689 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4690 * says that SNAP encapsulation is used, not LLC encapsulation
4693 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4694 * RFC 2225 say that SNAP encapsulation is used, not LLC
4695 * encapsulation with LLCSAP_IP.
4697 * So we always check for ETHERTYPE_IP.
4699 b0
= gen_linktype(ETHERTYPE_IP
);
4705 b1
= gen_portop(port
, ip_proto
, dir
);
4709 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
4710 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
4712 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
4725 gen_portop6(port
, proto
, dir
)
4726 int port
, proto
, dir
;
4728 struct block
*b0
, *b1
, *tmp
;
4730 /* ip6 proto 'proto' */
4731 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4735 b1
= gen_portatom6(0, (bpf_int32
)port
);
4739 b1
= gen_portatom6(2, (bpf_int32
)port
);
4744 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4745 b1
= gen_portatom6(2, (bpf_int32
)port
);
4750 tmp
= gen_portatom6(0, (bpf_int32
)port
);
4751 b1
= gen_portatom6(2, (bpf_int32
)port
);
4763 static struct block
*
4764 gen_port6(port
, ip_proto
, dir
)
4769 struct block
*b0
, *b1
, *tmp
;
4771 /* link proto ip6 */
4772 b0
= gen_linktype(ETHERTYPE_IPV6
);
4778 b1
= gen_portop6(port
, ip_proto
, dir
);
4782 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
4783 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
4785 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
4797 /* gen_portrange code */
4798 static struct block
*
4799 gen_portrangeatom(off
, v1
, v2
)
4803 struct block
*b1
, *b2
;
4807 * Reverse the order of the ports, so v1 is the lower one.
4816 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
4817 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
4825 gen_portrangeop(port1
, port2
, proto
, dir
)
4830 struct block
*b0
, *b1
, *tmp
;
4832 /* ip proto 'proto' */
4833 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
4839 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4843 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4848 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4849 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4854 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4855 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4867 static struct block
*
4868 gen_portrange(port1
, port2
, ip_proto
, dir
)
4873 struct block
*b0
, *b1
, *tmp
;
4876 b0
= gen_linktype(ETHERTYPE_IP
);
4882 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
4886 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
4887 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
4889 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
4901 static struct block
*
4902 gen_portrangeatom6(off
, v1
, v2
)
4906 struct block
*b1
, *b2
;
4910 * Reverse the order of the ports, so v1 is the lower one.
4919 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
4920 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
4928 gen_portrangeop6(port1
, port2
, proto
, dir
)
4933 struct block
*b0
, *b1
, *tmp
;
4935 /* ip6 proto 'proto' */
4936 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
4940 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4944 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4949 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4950 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4955 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
4956 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4968 static struct block
*
4969 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4974 struct block
*b0
, *b1
, *tmp
;
4976 /* link proto ip6 */
4977 b0
= gen_linktype(ETHERTYPE_IPV6
);
4983 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4987 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4988 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4990 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5003 lookup_proto(name
, proto
)
5004 register const char *name
;
5014 v
= pcap_nametoproto(name
);
5015 if (v
== PROTO_UNDEF
)
5016 bpf_error("unknown ip proto '%s'", name
);
5020 /* XXX should look up h/w protocol type based on linktype */
5021 v
= pcap_nametoeproto(name
);
5022 if (v
== PROTO_UNDEF
) {
5023 v
= pcap_nametollc(name
);
5024 if (v
== PROTO_UNDEF
)
5025 bpf_error("unknown ether proto '%s'", name
);
5030 if (strcmp(name
, "esis") == 0)
5032 else if (strcmp(name
, "isis") == 0)
5034 else if (strcmp(name
, "clnp") == 0)
5037 bpf_error("unknown osi proto '%s'", name
);
5057 static struct block
*
5058 gen_protochain(v
, proto
, dir
)
5063 #ifdef NO_PROTOCHAIN
5064 return gen_proto(v
, proto
, dir
);
5066 struct block
*b0
, *b
;
5067 struct slist
*s
[100];
5068 int fix2
, fix3
, fix4
, fix5
;
5069 int ahcheck
, again
, end
;
5071 int reg2
= alloc_reg();
5073 memset(s
, 0, sizeof(s
));
5074 fix2
= fix3
= fix4
= fix5
= 0;
5081 b0
= gen_protochain(v
, Q_IP
, dir
);
5082 b
= gen_protochain(v
, Q_IPV6
, dir
);
5086 bpf_error("bad protocol applied for 'protochain'");
5091 * We don't handle variable-length prefixes before the link-layer
5092 * header, or variable-length link-layer headers, here yet.
5093 * We might want to add BPF instructions to do the protochain
5094 * work, to simplify that and, on platforms that have a BPF
5095 * interpreter with the new instructions, let the filtering
5096 * be done in the kernel. (We already require a modified BPF
5097 * engine to do the protochain stuff, to support backward
5098 * branches, and backward branch support is unlikely to appear
5099 * in kernel BPF engines.)
5103 case DLT_IEEE802_11
:
5104 case DLT_IEEE802_11_RADIO
:
5105 case DLT_IEEE802_11_RADIO_AVS
:
5106 case DLT_PRISM_HEADER
:
5108 bpf_error("'protochain' not supported with 802.11");
5111 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5114 * s[0] is a dummy entry to protect other BPF insn from damage
5115 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5116 * hard to find interdependency made by jump table fixup.
5119 s
[i
] = new_stmt(0); /*dummy*/
5124 b0
= gen_linktype(ETHERTYPE_IP
);
5127 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5128 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 9;
5130 /* X = ip->ip_hl << 2 */
5131 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5132 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5137 b0
= gen_linktype(ETHERTYPE_IPV6
);
5139 /* A = ip6->ip_nxt */
5140 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5141 s
[i
]->s
.k
= off_macpl
+ off_nl
+ 6;
5143 /* X = sizeof(struct ip6_hdr) */
5144 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5150 bpf_error("unsupported proto to gen_protochain");
5154 /* again: if (A == v) goto end; else fall through; */
5156 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5158 s
[i
]->s
.jt
= NULL
; /*later*/
5159 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5163 #ifndef IPPROTO_NONE
5164 #define IPPROTO_NONE 59
5166 /* if (A == IPPROTO_NONE) goto end */
5167 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5168 s
[i
]->s
.jt
= NULL
; /*later*/
5169 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5170 s
[i
]->s
.k
= IPPROTO_NONE
;
5171 s
[fix5
]->s
.jf
= s
[i
];
5176 if (proto
== Q_IPV6
) {
5177 int v6start
, v6end
, v6advance
, j
;
5180 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5181 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5182 s
[i
]->s
.jt
= NULL
; /*later*/
5183 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5184 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5185 s
[fix2
]->s
.jf
= s
[i
];
5187 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5188 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5189 s
[i
]->s
.jt
= NULL
; /*later*/
5190 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5191 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5193 /* if (A == IPPROTO_ROUTING) goto v6advance */
5194 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5195 s
[i
]->s
.jt
= NULL
; /*later*/
5196 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5197 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5199 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5200 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5201 s
[i
]->s
.jt
= NULL
; /*later*/
5202 s
[i
]->s
.jf
= NULL
; /*later*/
5203 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5214 * X = X + (P[X + 1] + 1) * 8;
5217 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5219 /* A = P[X + packet head] */
5220 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5221 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5224 s
[i
] = new_stmt(BPF_ST
);
5228 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5231 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5235 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5237 /* A = P[X + packet head]; */
5238 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5239 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5242 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5246 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5250 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5253 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5257 /* goto again; (must use BPF_JA for backward jump) */
5258 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5259 s
[i
]->s
.k
= again
- i
- 1;
5260 s
[i
- 1]->s
.jf
= s
[i
];
5264 for (j
= v6start
; j
<= v6end
; j
++)
5265 s
[j
]->s
.jt
= s
[v6advance
];
5270 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5272 s
[fix2
]->s
.jf
= s
[i
];
5278 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5279 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5280 s
[i
]->s
.jt
= NULL
; /*later*/
5281 s
[i
]->s
.jf
= NULL
; /*later*/
5282 s
[i
]->s
.k
= IPPROTO_AH
;
5284 s
[fix3
]->s
.jf
= s
[ahcheck
];
5291 * X = X + (P[X + 1] + 2) * 4;
5294 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5296 /* A = P[X + packet head]; */
5297 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5298 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5301 s
[i
] = new_stmt(BPF_ST
);
5305 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5308 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5312 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5314 /* A = P[X + packet head] */
5315 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5316 s
[i
]->s
.k
= off_macpl
+ off_nl
;
5319 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5323 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5327 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5330 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5334 /* goto again; (must use BPF_JA for backward jump) */
5335 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5336 s
[i
]->s
.k
= again
- i
- 1;
5341 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5343 s
[fix2
]->s
.jt
= s
[end
];
5344 s
[fix4
]->s
.jf
= s
[end
];
5345 s
[fix5
]->s
.jt
= s
[end
];
5352 for (i
= 0; i
< max
- 1; i
++)
5353 s
[i
]->next
= s
[i
+ 1];
5354 s
[max
- 1]->next
= NULL
;
5359 b
= new_block(JMP(BPF_JEQ
));
5360 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5370 static struct block
*
5371 gen_check_802_11_data_frame()
5374 struct block
*b0
, *b1
;
5377 * A data frame has the 0x08 bit (b3) in the frame control field set
5378 * and the 0x04 bit (b2) clear.
5380 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5381 b0
= new_block(JMP(BPF_JSET
));
5385 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5386 b1
= new_block(JMP(BPF_JSET
));
5397 * Generate code that checks whether the packet is a packet for protocol
5398 * <proto> and whether the type field in that protocol's header has
5399 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5400 * IP packet and checks the protocol number in the IP header against <v>.
5402 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5403 * against Q_IP and Q_IPV6.
5405 static struct block
*
5406 gen_proto(v
, proto
, dir
)
5411 struct block
*b0
, *b1
;
5413 if (dir
!= Q_DEFAULT
)
5414 bpf_error("direction applied to 'proto'");
5419 b0
= gen_proto(v
, Q_IP
, dir
);
5420 b1
= gen_proto(v
, Q_IPV6
, dir
);
5428 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5429 * not LLC encapsulation with LLCSAP_IP.
5431 * For IEEE 802 networks - which includes 802.5 token ring
5432 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5433 * says that SNAP encapsulation is used, not LLC encapsulation
5436 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5437 * RFC 2225 say that SNAP encapsulation is used, not LLC
5438 * encapsulation with LLCSAP_IP.
5440 * So we always check for ETHERTYPE_IP.
5442 b0
= gen_linktype(ETHERTYPE_IP
);
5444 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
5446 b1
= gen_protochain(v
, Q_IP
);
5456 * Frame Relay packets typically have an OSI
5457 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5458 * generates code to check for all the OSI
5459 * NLPIDs, so calling it and then adding a check
5460 * for the particular NLPID for which we're
5461 * looking is bogus, as we can just check for
5464 * What we check for is the NLPID and a frame
5465 * control field value of UI, i.e. 0x03 followed
5468 * XXX - assumes a 2-byte Frame Relay header with
5469 * DLCI and flags. What if the address is longer?
5471 * XXX - what about SNAP-encapsulated frames?
5473 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
5479 * Cisco uses an Ethertype lookalike - for OSI,
5482 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
5483 /* OSI in C-HDLC is stuffed with a fudge byte */
5484 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
5489 b0
= gen_linktype(LLCSAP_ISONS
);
5490 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
5496 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5498 * 4 is the offset of the PDU type relative to the IS-IS
5501 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
5506 bpf_error("arp does not encapsulate another protocol");
5510 bpf_error("rarp does not encapsulate another protocol");
5514 bpf_error("atalk encapsulation is not specifiable");
5518 bpf_error("decnet encapsulation is not specifiable");
5522 bpf_error("sca does not encapsulate another protocol");
5526 bpf_error("lat does not encapsulate another protocol");
5530 bpf_error("moprc does not encapsulate another protocol");
5534 bpf_error("mopdl does not encapsulate another protocol");
5538 return gen_linktype(v
);
5541 bpf_error("'udp proto' is bogus");
5545 bpf_error("'tcp proto' is bogus");
5549 bpf_error("'sctp proto' is bogus");
5553 bpf_error("'icmp proto' is bogus");
5557 bpf_error("'igmp proto' is bogus");
5561 bpf_error("'igrp proto' is bogus");
5565 bpf_error("'pim proto' is bogus");
5569 bpf_error("'vrrp proto' is bogus");
5574 b0
= gen_linktype(ETHERTYPE_IPV6
);
5576 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
5578 b1
= gen_protochain(v
, Q_IPV6
);
5584 bpf_error("'icmp6 proto' is bogus");
5588 bpf_error("'ah proto' is bogus");
5591 bpf_error("'ah proto' is bogus");
5594 bpf_error("'stp proto' is bogus");
5597 bpf_error("'ipx proto' is bogus");
5600 bpf_error("'netbeui proto' is bogus");
5603 bpf_error("'radio proto' is bogus");
5614 register const char *name
;
5617 int proto
= q
.proto
;
5621 bpf_u_int32 mask
, addr
;
5623 bpf_u_int32
**alist
;
5626 struct sockaddr_in
*sin4
;
5627 struct sockaddr_in6
*sin6
;
5628 struct addrinfo
*res
, *res0
;
5629 struct in6_addr mask128
;
5631 struct block
*b
, *tmp
;
5632 int port
, real_proto
;
5638 addr
= pcap_nametonetaddr(name
);
5640 bpf_error("unknown network '%s'", name
);
5641 /* Left justify network addr and calculate its network mask */
5643 while (addr
&& (addr
& 0xff000000) == 0) {
5647 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
5651 if (proto
== Q_LINK
) {
5655 eaddr
= pcap_ether_hostton(name
);
5658 "unknown ether host '%s'", name
);
5659 b
= gen_ehostop(eaddr
, dir
);
5664 eaddr
= pcap_ether_hostton(name
);
5667 "unknown FDDI host '%s'", name
);
5668 b
= gen_fhostop(eaddr
, dir
);
5673 eaddr
= pcap_ether_hostton(name
);
5676 "unknown token ring host '%s'", name
);
5677 b
= gen_thostop(eaddr
, dir
);
5681 case DLT_IEEE802_11
:
5682 case DLT_IEEE802_11_RADIO_AVS
:
5683 case DLT_IEEE802_11_RADIO
:
5684 case DLT_PRISM_HEADER
:
5686 eaddr
= pcap_ether_hostton(name
);
5689 "unknown 802.11 host '%s'", name
);
5690 b
= gen_wlanhostop(eaddr
, dir
);
5694 case DLT_IP_OVER_FC
:
5695 eaddr
= pcap_ether_hostton(name
);
5698 "unknown Fibre Channel host '%s'", name
);
5699 b
= gen_ipfchostop(eaddr
, dir
);
5708 * Check that the packet doesn't begin
5709 * with an LE Control marker. (We've
5710 * already generated a test for LANE.)
5712 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
5716 eaddr
= pcap_ether_hostton(name
);
5719 "unknown ether host '%s'", name
);
5720 b
= gen_ehostop(eaddr
, dir
);
5726 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
5727 } else if (proto
== Q_DECNET
) {
5728 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
5730 * I don't think DECNET hosts can be multihomed, so
5731 * there is no need to build up a list of addresses
5733 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
5736 alist
= pcap_nametoaddr(name
);
5737 if (alist
== NULL
|| *alist
== NULL
)
5738 bpf_error("unknown host '%s'", name
);
5740 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
5742 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
5744 tmp
= gen_host(**alist
++, 0xffffffff,
5745 tproto
, dir
, q
.addr
);
5751 memset(&mask128
, 0xff, sizeof(mask128
));
5752 res0
= res
= pcap_nametoaddrinfo(name
);
5754 bpf_error("unknown host '%s'", name
);
5756 tproto
= tproto6
= proto
;
5757 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
5761 for (res
= res0
; res
; res
= res
->ai_next
) {
5762 switch (res
->ai_family
) {
5764 if (tproto
== Q_IPV6
)
5767 sin4
= (struct sockaddr_in
*)
5769 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
5770 0xffffffff, tproto
, dir
, q
.addr
);
5773 if (tproto6
== Q_IP
)
5776 sin6
= (struct sockaddr_in6
*)
5778 tmp
= gen_host6(&sin6
->sin6_addr
,
5779 &mask128
, tproto6
, dir
, q
.addr
);
5790 bpf_error("unknown host '%s'%s", name
,
5791 (proto
== Q_DEFAULT
)
5793 : " for specified address family");
5800 if (proto
!= Q_DEFAULT
&&
5801 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5802 bpf_error("illegal qualifier of 'port'");
5803 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
5804 bpf_error("unknown port '%s'", name
);
5805 if (proto
== Q_UDP
) {
5806 if (real_proto
== IPPROTO_TCP
)
5807 bpf_error("port '%s' is tcp", name
);
5808 else if (real_proto
== IPPROTO_SCTP
)
5809 bpf_error("port '%s' is sctp", name
);
5811 /* override PROTO_UNDEF */
5812 real_proto
= IPPROTO_UDP
;
5814 if (proto
== Q_TCP
) {
5815 if (real_proto
== IPPROTO_UDP
)
5816 bpf_error("port '%s' is udp", name
);
5818 else if (real_proto
== IPPROTO_SCTP
)
5819 bpf_error("port '%s' is sctp", name
);
5821 /* override PROTO_UNDEF */
5822 real_proto
= IPPROTO_TCP
;
5824 if (proto
== Q_SCTP
) {
5825 if (real_proto
== IPPROTO_UDP
)
5826 bpf_error("port '%s' is udp", name
);
5828 else if (real_proto
== IPPROTO_TCP
)
5829 bpf_error("port '%s' is tcp", name
);
5831 /* override PROTO_UNDEF */
5832 real_proto
= IPPROTO_SCTP
;
5835 return gen_port(port
, real_proto
, dir
);
5837 b
= gen_port(port
, real_proto
, dir
);
5838 gen_or(gen_port6(port
, real_proto
, dir
), b
);
5843 if (proto
!= Q_DEFAULT
&&
5844 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
5845 bpf_error("illegal qualifier of 'portrange'");
5846 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
5847 bpf_error("unknown port in range '%s'", name
);
5848 if (proto
== Q_UDP
) {
5849 if (real_proto
== IPPROTO_TCP
)
5850 bpf_error("port in range '%s' is tcp", name
);
5851 else if (real_proto
== IPPROTO_SCTP
)
5852 bpf_error("port in range '%s' is sctp", name
);
5854 /* override PROTO_UNDEF */
5855 real_proto
= IPPROTO_UDP
;
5857 if (proto
== Q_TCP
) {
5858 if (real_proto
== IPPROTO_UDP
)
5859 bpf_error("port in range '%s' is udp", name
);
5860 else if (real_proto
== IPPROTO_SCTP
)
5861 bpf_error("port in range '%s' is sctp", name
);
5863 /* override PROTO_UNDEF */
5864 real_proto
= IPPROTO_TCP
;
5866 if (proto
== Q_SCTP
) {
5867 if (real_proto
== IPPROTO_UDP
)
5868 bpf_error("port in range '%s' is udp", name
);
5869 else if (real_proto
== IPPROTO_TCP
)
5870 bpf_error("port in range '%s' is tcp", name
);
5872 /* override PROTO_UNDEF */
5873 real_proto
= IPPROTO_SCTP
;
5876 return gen_portrange(port1
, port2
, real_proto
, dir
);
5878 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
5879 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
5885 eaddr
= pcap_ether_hostton(name
);
5887 bpf_error("unknown ether host: %s", name
);
5889 alist
= pcap_nametoaddr(name
);
5890 if (alist
== NULL
|| *alist
== NULL
)
5891 bpf_error("unknown host '%s'", name
);
5892 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
5896 bpf_error("'gateway' not supported in this configuration");
5900 real_proto
= lookup_proto(name
, proto
);
5901 if (real_proto
>= 0)
5902 return gen_proto(real_proto
, proto
, dir
);
5904 bpf_error("unknown protocol: %s", name
);
5907 real_proto
= lookup_proto(name
, proto
);
5908 if (real_proto
>= 0)
5909 return gen_protochain(real_proto
, proto
, dir
);
5911 bpf_error("unknown protocol: %s", name
);
5922 gen_mcode(s1
, s2
, masklen
, q
)
5923 register const char *s1
, *s2
;
5924 register int masklen
;
5927 register int nlen
, mlen
;
5930 nlen
= __pcap_atoin(s1
, &n
);
5931 /* Promote short ipaddr */
5935 mlen
= __pcap_atoin(s2
, &m
);
5936 /* Promote short ipaddr */
5939 bpf_error("non-network bits set in \"%s mask %s\"",
5942 /* Convert mask len to mask */
5944 bpf_error("mask length must be <= 32");
5947 * X << 32 is not guaranteed by C to be 0; it's
5952 m
= 0xffffffff << (32 - masklen
);
5954 bpf_error("non-network bits set in \"%s/%d\"",
5961 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
5964 bpf_error("Mask syntax for networks only");
5973 register const char *s
;
5978 int proto
= q
.proto
;
5984 else if (q
.proto
== Q_DECNET
)
5985 vlen
= __pcap_atodn(s
, &v
);
5987 vlen
= __pcap_atoin(s
, &v
);
5994 if (proto
== Q_DECNET
)
5995 return gen_host(v
, 0, proto
, dir
, q
.addr
);
5996 else if (proto
== Q_LINK
) {
5997 bpf_error("illegal link layer address");
6000 if (s
== NULL
&& q
.addr
== Q_NET
) {
6001 /* Promote short net number */
6002 while (v
&& (v
& 0xff000000) == 0) {
6007 /* Promote short ipaddr */
6011 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6016 proto
= IPPROTO_UDP
;
6017 else if (proto
== Q_TCP
)
6018 proto
= IPPROTO_TCP
;
6019 else if (proto
== Q_SCTP
)
6020 proto
= IPPROTO_SCTP
;
6021 else if (proto
== Q_DEFAULT
)
6022 proto
= PROTO_UNDEF
;
6024 bpf_error("illegal qualifier of 'port'");
6027 return gen_port((int)v
, proto
, dir
);
6031 b
= gen_port((int)v
, proto
, dir
);
6032 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6039 proto
= IPPROTO_UDP
;
6040 else if (proto
== Q_TCP
)
6041 proto
= IPPROTO_TCP
;
6042 else if (proto
== Q_SCTP
)
6043 proto
= IPPROTO_SCTP
;
6044 else if (proto
== Q_DEFAULT
)
6045 proto
= PROTO_UNDEF
;
6047 bpf_error("illegal qualifier of 'portrange'");
6050 return gen_portrange((int)v
, (int)v
, proto
, dir
);
6054 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6055 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6061 bpf_error("'gateway' requires a name");
6065 return gen_proto((int)v
, proto
, dir
);
6068 return gen_protochain((int)v
, proto
, dir
);
6083 gen_mcode6(s1
, s2
, masklen
, q
)
6084 register const char *s1
, *s2
;
6085 register int masklen
;
6088 struct addrinfo
*res
;
6089 struct in6_addr
*addr
;
6090 struct in6_addr mask
;
6095 bpf_error("no mask %s supported", s2
);
6097 res
= pcap_nametoaddrinfo(s1
);
6099 bpf_error("invalid ip6 address %s", s1
);
6101 bpf_error("%s resolved to multiple address", s1
);
6102 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6104 if (sizeof(mask
) * 8 < masklen
)
6105 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6106 memset(&mask
, 0, sizeof(mask
));
6107 memset(&mask
, 0xff, masklen
/ 8);
6109 mask
.s6_addr
[masklen
/ 8] =
6110 (0xff << (8 - masklen
% 8)) & 0xff;
6113 a
= (u_int32_t
*)addr
;
6114 m
= (u_int32_t
*)&mask
;
6115 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6116 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6117 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6125 bpf_error("Mask syntax for networks only");
6129 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6134 bpf_error("invalid qualifier against IPv6 address");
6143 register const u_char
*eaddr
;
6146 struct block
*b
, *tmp
;
6148 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6151 return gen_ehostop(eaddr
, (int)q
.dir
);
6153 return gen_fhostop(eaddr
, (int)q
.dir
);
6155 return gen_thostop(eaddr
, (int)q
.dir
);
6156 case DLT_IEEE802_11
:
6157 case DLT_IEEE802_11_RADIO_AVS
:
6158 case DLT_IEEE802_11_RADIO
:
6159 case DLT_PRISM_HEADER
:
6161 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6165 * Check that the packet doesn't begin with an
6166 * LE Control marker. (We've already generated
6169 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
6174 * Now check the MAC address.
6176 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6181 case DLT_IP_OVER_FC
:
6182 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6184 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6188 bpf_error("ethernet address used in non-ether expression");
6195 struct slist
*s0
, *s1
;
6198 * This is definitely not the best way to do this, but the
6199 * lists will rarely get long.
6206 static struct slist
*
6212 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6217 static struct slist
*
6223 s
= new_stmt(BPF_LD
|BPF_MEM
);
6229 * Modify "index" to use the value stored into its register as an
6230 * offset relative to the beginning of the header for the protocol
6231 * "proto", and allocate a register and put an item "size" bytes long
6232 * (1, 2, or 4) at that offset into that register, making it the register
6236 gen_load(proto
, inst
, size
)
6241 struct slist
*s
, *tmp
;
6243 int regno
= alloc_reg();
6245 free_reg(inst
->regno
);
6249 bpf_error("data size must be 1, 2, or 4");
6265 bpf_error("unsupported index operation");
6269 * The offset is relative to the beginning of the packet
6270 * data, if we have a radio header. (If we don't, this
6273 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6274 linktype
!= DLT_IEEE802_11_RADIO
&&
6275 linktype
!= DLT_PRISM_HEADER
)
6276 bpf_error("radio information not present in capture");
6279 * Load into the X register the offset computed into the
6280 * register specifed by "index".
6282 s
= xfer_to_x(inst
);
6285 * Load the item at that offset.
6287 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6289 sappend(inst
->s
, s
);
6294 * The offset is relative to the beginning of
6295 * the link-layer header.
6297 * XXX - what about ATM LANE? Should the index be
6298 * relative to the beginning of the AAL5 frame, so
6299 * that 0 refers to the beginning of the LE Control
6300 * field, or relative to the beginning of the LAN
6301 * frame, so that 0 refers, for Ethernet LANE, to
6302 * the beginning of the destination address?
6304 s
= gen_llprefixlen();
6307 * If "s" is non-null, it has code to arrange that the
6308 * X register contains the length of the prefix preceding
6309 * the link-layer header. Add to it the offset computed
6310 * into the register specified by "index", and move that
6311 * into the X register. Otherwise, just load into the X
6312 * register the offset computed into the register specifed
6316 sappend(s
, xfer_to_a(inst
));
6317 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6318 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6320 s
= xfer_to_x(inst
);
6323 * Load the item at the sum of the offset we've put in the
6324 * X register and the offset of the start of the link
6325 * layer header (which is 0 if the radio header is
6326 * variable-length; that header length is what we put
6327 * into the X register and then added to the index).
6329 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6332 sappend(inst
->s
, s
);
6348 * The offset is relative to the beginning of
6349 * the network-layer header.
6350 * XXX - are there any cases where we want
6353 s
= gen_off_macpl();
6356 * If "s" is non-null, it has code to arrange that the
6357 * X register contains the offset of the MAC-layer
6358 * payload. Add to it the offset computed into the
6359 * register specified by "index", and move that into
6360 * the X register. Otherwise, just load into the X
6361 * register the offset computed into the register specifed
6365 sappend(s
, xfer_to_a(inst
));
6366 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6367 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6369 s
= xfer_to_x(inst
);
6372 * Load the item at the sum of the offset we've put in the
6373 * X register, the offset of the start of the network
6374 * layer header from the beginning of the MAC-layer
6375 * payload, and the purported offset of the start of the
6376 * MAC-layer payload (which might be 0 if there's a
6377 * variable-length prefix before the link-layer header
6378 * or the link-layer header itself is variable-length;
6379 * the variable-length offset of the start of the
6380 * MAC-layer payload is what we put into the X register
6381 * and then added to the index).
6383 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6384 tmp
->s
.k
= off_macpl
+ off_nl
;
6386 sappend(inst
->s
, s
);
6389 * Do the computation only if the packet contains
6390 * the protocol in question.
6392 b
= gen_proto_abbrev(proto
);
6394 gen_and(inst
->b
, b
);
6407 * The offset is relative to the beginning of
6408 * the transport-layer header.
6410 * Load the X register with the length of the IPv4 header
6411 * (plus the offset of the link-layer header, if it's
6412 * a variable-length header), in bytes.
6414 * XXX - are there any cases where we want
6416 * XXX - we should, if we're built with
6417 * IPv6 support, generate code to load either
6418 * IPv4, IPv6, or both, as appropriate.
6420 s
= gen_loadx_iphdrlen();
6423 * The X register now contains the sum of the length
6424 * of any variable-length header preceding the link-layer
6425 * header, any variable-length link-layer header, and the
6426 * length of the network-layer header.
6428 * Load into the A register the offset relative to
6429 * the beginning of the transport layer header,
6430 * add the X register to that, move that to the
6431 * X register, and load with an offset from the
6432 * X register equal to the offset of the network
6433 * layer header relative to the beginning of
6434 * the MAC-layer payload plus the fixed-length
6435 * portion of the offset of the MAC-layer payload
6436 * from the beginning of the raw packet data.
6438 sappend(s
, xfer_to_a(inst
));
6439 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6440 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6441 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6442 tmp
->s
.k
= off_macpl
+ off_nl
;
6443 sappend(inst
->s
, s
);
6446 * Do the computation only if the packet contains
6447 * the protocol in question - which is true only
6448 * if this is an IP datagram and is the first or
6449 * only fragment of that datagram.
6451 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6453 gen_and(inst
->b
, b
);
6455 gen_and(gen_proto_abbrev(Q_IP
), b
);
6461 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6465 inst
->regno
= regno
;
6466 s
= new_stmt(BPF_ST
);
6468 sappend(inst
->s
, s
);
6474 gen_relation(code
, a0
, a1
, reversed
)
6476 struct arth
*a0
, *a1
;
6479 struct slist
*s0
, *s1
, *s2
;
6480 struct block
*b
, *tmp
;
6484 if (code
== BPF_JEQ
) {
6485 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6486 b
= new_block(JMP(code
));
6490 b
= new_block(BPF_JMP
|code
|BPF_X
);
6496 sappend(a0
->s
, a1
->s
);
6500 free_reg(a0
->regno
);
6501 free_reg(a1
->regno
);
6503 /* 'and' together protocol checks */
6506 gen_and(a0
->b
, tmp
= a1
->b
);
6522 int regno
= alloc_reg();
6523 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
6526 s
= new_stmt(BPF_LD
|BPF_LEN
);
6527 s
->next
= new_stmt(BPF_ST
);
6528 s
->next
->s
.k
= regno
;
6543 a
= (struct arth
*)newchunk(sizeof(*a
));
6547 s
= new_stmt(BPF_LD
|BPF_IMM
);
6549 s
->next
= new_stmt(BPF_ST
);
6565 s
= new_stmt(BPF_ALU
|BPF_NEG
);
6568 s
= new_stmt(BPF_ST
);
6576 gen_arth(code
, a0
, a1
)
6578 struct arth
*a0
, *a1
;
6580 struct slist
*s0
, *s1
, *s2
;
6584 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
6589 sappend(a0
->s
, a1
->s
);
6591 free_reg(a0
->regno
);
6592 free_reg(a1
->regno
);
6594 s0
= new_stmt(BPF_ST
);
6595 a0
->regno
= s0
->s
.k
= alloc_reg();
6602 * Here we handle simple allocation of the scratch registers.
6603 * If too many registers are alloc'd, the allocator punts.
6605 static int regused
[BPF_MEMWORDS
];
6609 * Initialize the table of used registers and the current register.
6615 memset(regused
, 0, sizeof regused
);
6619 * Return the next free register.
6624 int n
= BPF_MEMWORDS
;
6627 if (regused
[curreg
])
6628 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
6630 regused
[curreg
] = 1;
6634 bpf_error("too many registers needed to evaluate expression");
6640 * Return a register to the table so it can
6650 static struct block
*
6657 s
= new_stmt(BPF_LD
|BPF_LEN
);
6658 b
= new_block(JMP(jmp
));
6669 return gen_len(BPF_JGE
, n
);
6673 * Actually, this is less than or equal.
6681 b
= gen_len(BPF_JGT
, n
);
6688 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
6689 * the beginning of the link-layer header.
6690 * XXX - that means you can't test values in the radiotap header, but
6691 * as that header is difficult if not impossible to parse generally
6692 * without a loop, that might not be a severe problem. A new keyword
6693 * "radio" could be added for that, although what you'd really want
6694 * would be a way of testing particular radio header values, which
6695 * would generate code appropriate to the radio header in question.
6698 gen_byteop(op
, idx
, val
)
6709 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6712 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6716 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
6720 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
6724 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
6728 b
= new_block(JMP(BPF_JEQ
));
6735 static u_char abroadcast
[] = { 0x0 };
6738 gen_broadcast(proto
)
6741 bpf_u_int32 hostmask
;
6742 struct block
*b0
, *b1
, *b2
;
6743 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
6751 case DLT_ARCNET_LINUX
:
6752 return gen_ahostop(abroadcast
, Q_DST
);
6754 return gen_ehostop(ebroadcast
, Q_DST
);
6756 return gen_fhostop(ebroadcast
, Q_DST
);
6758 return gen_thostop(ebroadcast
, Q_DST
);
6759 case DLT_IEEE802_11
:
6760 case DLT_IEEE802_11_RADIO_AVS
:
6761 case DLT_IEEE802_11_RADIO
:
6763 case DLT_PRISM_HEADER
:
6764 return gen_wlanhostop(ebroadcast
, Q_DST
);
6765 case DLT_IP_OVER_FC
:
6766 return gen_ipfchostop(ebroadcast
, Q_DST
);
6770 * Check that the packet doesn't begin with an
6771 * LE Control marker. (We've already generated
6774 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6779 * Now check the MAC address.
6781 b0
= gen_ehostop(ebroadcast
, Q_DST
);
6787 bpf_error("not a broadcast link");
6792 b0
= gen_linktype(ETHERTYPE_IP
);
6793 hostmask
= ~netmask
;
6794 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
6795 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
6796 (bpf_int32
)(~0 & hostmask
), hostmask
);
6801 bpf_error("only link-layer/IP broadcast filters supported");
6807 * Generate code to test the low-order bit of a MAC address (that's
6808 * the bottom bit of the *first* byte).
6810 static struct block
*
6811 gen_mac_multicast(offset
)
6814 register struct block
*b0
;
6815 register struct slist
*s
;
6817 /* link[offset] & 1 != 0 */
6818 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
6819 b0
= new_block(JMP(BPF_JSET
));
6826 gen_multicast(proto
)
6829 register struct block
*b0
, *b1
, *b2
;
6830 register struct slist
*s
;
6838 case DLT_ARCNET_LINUX
:
6839 /* all ARCnet multicasts use the same address */
6840 return gen_ahostop(abroadcast
, Q_DST
);
6842 /* ether[0] & 1 != 0 */
6843 return gen_mac_multicast(0);
6846 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
6848 * XXX - was that referring to bit-order issues?
6850 /* fddi[1] & 1 != 0 */
6851 return gen_mac_multicast(1);
6853 /* tr[2] & 1 != 0 */
6854 return gen_mac_multicast(2);
6855 case DLT_IEEE802_11
:
6856 case DLT_IEEE802_11_RADIO_AVS
:
6858 case DLT_IEEE802_11_RADIO
:
6859 case DLT_PRISM_HEADER
:
6863 * For control frames, there is no DA.
6865 * For management frames, DA is at an
6866 * offset of 4 from the beginning of
6869 * For data frames, DA is at an offset
6870 * of 4 from the beginning of the packet
6871 * if To DS is clear and at an offset of
6872 * 16 from the beginning of the packet
6877 * Generate the tests to be done for data frames.
6879 * First, check for To DS set, i.e. "link[1] & 0x01".
6881 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6882 b1
= new_block(JMP(BPF_JSET
));
6883 b1
->s
.k
= 0x01; /* To DS */
6887 * If To DS is set, the DA is at 16.
6889 b0
= gen_mac_multicast(16);
6893 * Now, check for To DS not set, i.e. check
6894 * "!(link[1] & 0x01)".
6896 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
6897 b2
= new_block(JMP(BPF_JSET
));
6898 b2
->s
.k
= 0x01; /* To DS */
6903 * If To DS is not set, the DA is at 4.
6905 b1
= gen_mac_multicast(4);
6909 * Now OR together the last two checks. That gives
6910 * the complete set of checks for data frames.
6915 * Now check for a data frame.
6916 * I.e, check "link[0] & 0x08".
6918 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6919 b1
= new_block(JMP(BPF_JSET
));
6924 * AND that with the checks done for data frames.
6929 * If the high-order bit of the type value is 0, this
6930 * is a management frame.
6931 * I.e, check "!(link[0] & 0x08)".
6933 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6934 b2
= new_block(JMP(BPF_JSET
));
6940 * For management frames, the DA is at 4.
6942 b1
= gen_mac_multicast(4);
6946 * OR that with the checks done for data frames.
6947 * That gives the checks done for management and
6953 * If the low-order bit of the type value is 1,
6954 * this is either a control frame or a frame
6955 * with a reserved type, and thus not a
6958 * I.e., check "!(link[0] & 0x04)".
6960 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
6961 b1
= new_block(JMP(BPF_JSET
));
6967 * AND that with the checks for data and management
6972 case DLT_IP_OVER_FC
:
6973 b0
= gen_mac_multicast(2);
6978 * Check that the packet doesn't begin with an
6979 * LE Control marker. (We've already generated
6982 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
6986 /* ether[off_mac] & 1 != 0 */
6987 b0
= gen_mac_multicast(off_mac
);
6995 /* Link not known to support multicasts */
6999 b0
= gen_linktype(ETHERTYPE_IP
);
7000 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
7006 b0
= gen_linktype(ETHERTYPE_IPV6
);
7007 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
7012 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7018 * generate command for inbound/outbound. It's here so we can
7019 * make it link-type specific. 'dir' = 0 implies "inbound",
7020 * = 1 implies "outbound".
7026 register struct block
*b0
;
7029 * Only some data link types support inbound/outbound qualifiers.
7033 b0
= gen_relation(BPF_JEQ
,
7034 gen_load(Q_LINK
, gen_loadi(0), 1),
7042 * Match packets sent by this machine.
7044 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7047 * Match packets sent to this machine.
7048 * (No broadcast or multicast packets, or
7049 * packets sent to some other machine and
7050 * received promiscuously.)
7052 * XXX - packets sent to other machines probably
7053 * shouldn't be matched, but what about broadcast
7054 * or multicast packets we received?
7056 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
7060 #ifdef HAVE_NET_PFVAR_H
7062 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7063 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7069 /* match outgoing packets */
7070 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
7072 /* match incoming packets */
7073 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
7077 case DLT_JUNIPER_MFR
:
7078 case DLT_JUNIPER_MLFR
:
7079 case DLT_JUNIPER_MLPPP
:
7080 case DLT_JUNIPER_ATM1
:
7081 case DLT_JUNIPER_ATM2
:
7082 case DLT_JUNIPER_PPPOE
:
7083 case DLT_JUNIPER_PPPOE_ATM
:
7084 case DLT_JUNIPER_GGSN
:
7085 case DLT_JUNIPER_ES
:
7086 case DLT_JUNIPER_MONITOR
:
7087 case DLT_JUNIPER_SERVICES
:
7088 case DLT_JUNIPER_ETHER
:
7089 case DLT_JUNIPER_PPP
:
7090 case DLT_JUNIPER_FRELAY
:
7091 case DLT_JUNIPER_CHDLC
:
7092 case DLT_JUNIPER_VP
:
7093 case DLT_JUNIPER_ST
:
7094 case DLT_JUNIPER_ISM
:
7095 /* juniper flags (including direction) are stored
7096 * the byte after the 3-byte magic number */
7098 /* match outgoing packets */
7099 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
7101 /* match incoming packets */
7102 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
7107 bpf_error("inbound/outbound not supported on linktype %d",
7115 #ifdef HAVE_NET_PFVAR_H
7116 /* PF firewall log matched interface */
7118 gen_pf_ifname(const char *ifname
)
7123 if (linktype
!= DLT_PFLOG
) {
7124 bpf_error("ifname supported only on PF linktype");
7127 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7128 off
= offsetof(struct pfloghdr
, ifname
);
7129 if (strlen(ifname
) >= len
) {
7130 bpf_error("ifname interface names can only be %d characters",
7134 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
7138 /* PF firewall log ruleset name */
7140 gen_pf_ruleset(char *ruleset
)
7144 if (linktype
!= DLT_PFLOG
) {
7145 bpf_error("ruleset supported only on PF linktype");
7149 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7150 bpf_error("ruleset names can only be %ld characters",
7151 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7155 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
7156 strlen(ruleset
), (const u_char
*)ruleset
);
7160 /* PF firewall log rule number */
7166 if (linktype
!= DLT_PFLOG
) {
7167 bpf_error("rnr supported only on PF linktype");
7171 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7176 /* PF firewall log sub-rule number */
7178 gen_pf_srnr(int srnr
)
7182 if (linktype
!= DLT_PFLOG
) {
7183 bpf_error("srnr supported only on PF linktype");
7187 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7192 /* PF firewall log reason code */
7194 gen_pf_reason(int reason
)
7198 if (linktype
!= DLT_PFLOG
) {
7199 bpf_error("reason supported only on PF linktype");
7203 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7208 /* PF firewall log action */
7210 gen_pf_action(int action
)
7214 if (linktype
!= DLT_PFLOG
) {
7215 bpf_error("action supported only on PF linktype");
7219 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
7223 #else /* !HAVE_NET_PFVAR_H */
7225 gen_pf_ifname(const char *ifname
)
7227 bpf_error("libpcap was compiled without pf support");
7233 gen_pf_ruleset(char *ruleset
)
7235 bpf_error("libpcap was compiled on a machine without pf support");
7243 bpf_error("libpcap was compiled on a machine without pf support");
7249 gen_pf_srnr(int srnr
)
7251 bpf_error("libpcap was compiled on a machine without pf support");
7257 gen_pf_reason(int reason
)
7259 bpf_error("libpcap was compiled on a machine without pf support");
7265 gen_pf_action(int action
)
7267 bpf_error("libpcap was compiled on a machine without pf support");
7271 #endif /* HAVE_NET_PFVAR_H */
7273 /* IEEE 802.11 wireless header */
7275 gen_p80211_type(int type
, int mask
)
7281 case DLT_IEEE802_11
:
7282 case DLT_PRISM_HEADER
:
7283 case DLT_IEEE802_11_RADIO_AVS
:
7284 case DLT_IEEE802_11_RADIO
:
7285 b0
= gen_mcmp(OR_LINK
, 0, BPF_B
, (bpf_int32
)type
,
7290 bpf_error("802.11 link-layer types supported only on 802.11");
7298 register const u_char
*eaddr
;
7304 case DLT_ARCNET_LINUX
:
7305 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7307 return (gen_ahostop(eaddr
, (int)q
.dir
));
7309 bpf_error("ARCnet address used in non-arc expression");
7315 bpf_error("aid supported only on ARCnet");
7318 bpf_error("ARCnet address used in non-arc expression");
7323 static struct block
*
7324 gen_ahostop(eaddr
, dir
)
7325 register const u_char
*eaddr
;
7328 register struct block
*b0
, *b1
;
7331 /* src comes first, different from Ethernet */
7333 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
7336 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
7339 b0
= gen_ahostop(eaddr
, Q_SRC
);
7340 b1
= gen_ahostop(eaddr
, Q_DST
);
7346 b0
= gen_ahostop(eaddr
, Q_SRC
);
7347 b1
= gen_ahostop(eaddr
, Q_DST
);
7356 * support IEEE 802.1Q VLAN trunk over ethernet
7362 struct block
*b0
, *b1
;
7364 /* can't check for VLAN-encapsulated packets inside MPLS */
7365 if (label_stack_depth
> 0)
7366 bpf_error("no VLAN match after MPLS");
7369 * Check for a VLAN packet, and then change the offsets to point
7370 * to the type and data fields within the VLAN packet. Just
7371 * increment the offsets, so that we can support a hierarchy, e.g.
7372 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7375 * XXX - this is a bit of a kludge. If we were to split the
7376 * compiler into a parser that parses an expression and
7377 * generates an expression tree, and a code generator that
7378 * takes an expression tree (which could come from our
7379 * parser or from some other parser) and generates BPF code,
7380 * we could perhaps make the offsets parameters of routines
7381 * and, in the handler for an "AND" node, pass to subnodes
7382 * other than the VLAN node the adjusted offsets.
7384 * This would mean that "vlan" would, instead of changing the
7385 * behavior of *all* tests after it, change only the behavior
7386 * of tests ANDed with it. That would change the documented
7387 * semantics of "vlan", which might break some expressions.
7388 * However, it would mean that "(vlan and ip) or ip" would check
7389 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7390 * checking only for VLAN-encapsulated IP, so that could still
7391 * be considered worth doing; it wouldn't break expressions
7392 * that are of the form "vlan and ..." or "vlan N and ...",
7393 * which I suspect are the most common expressions involving
7394 * "vlan". "vlan or ..." doesn't necessarily do what the user
7395 * would really want, now, as all the "or ..." tests would
7396 * be done assuming a VLAN, even though the "or" could be viewed
7397 * as meaning "or, if this isn't a VLAN packet...".
7404 /* check for VLAN */
7405 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
7406 (bpf_int32
)ETHERTYPE_8021Q
);
7408 /* If a specific VLAN is requested, check VLAN id */
7409 if (vlan_num
>= 0) {
7410 b1
= gen_mcmp(OR_MACPL
, 0, BPF_H
,
7411 (bpf_int32
)vlan_num
, 0x0fff);
7425 bpf_error("no VLAN support for data link type %d",
7440 struct block
*b0
,*b1
;
7443 * Change the offsets to point to the type and data fields within
7444 * the MPLS packet. Just increment the offsets, so that we
7445 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7446 * capture packets with an outer label of 100000 and an inner
7449 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7453 if (label_stack_depth
> 0) {
7454 /* just match the bottom-of-stack bit clear */
7455 b0
= gen_mcmp(OR_MACPL
, orig_nl
-2, BPF_B
, 0, 0x01);
7458 * Indicate that we're checking MPLS-encapsulated headers,
7459 * to make sure higher level code generators don't try to
7460 * match against IP-related protocols such as Q_ARP, Q_RARP
7465 case DLT_C_HDLC
: /* fall through */
7467 b0
= gen_linktype(ETHERTYPE_MPLS
);
7471 b0
= gen_linktype(PPP_MPLS_UCAST
);
7474 /* FIXME add other DLT_s ...
7475 * for Frame-Relay/and ATM this may get messy due to SNAP headers
7476 * leave it for now */
7479 bpf_error("no MPLS support for data link type %d",
7487 /* If a specific MPLS label is requested, check it */
7488 if (label_num
>= 0) {
7489 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
7490 b1
= gen_mcmp(OR_MACPL
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
7491 0xfffff000); /* only compare the first 20 bits */
7498 label_stack_depth
++;
7503 * Support PPPOE discovery and session.
7508 /* check for PPPoE discovery */
7509 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
7518 * Test against the PPPoE session link-layer type.
7520 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
7523 * Change the offsets to point to the type and data fields within
7524 * the PPP packet, and note that this is PPPoE rather than
7527 * XXX - this is a bit of a kludge. If we were to split the
7528 * compiler into a parser that parses an expression and
7529 * generates an expression tree, and a code generator that
7530 * takes an expression tree (which could come from our
7531 * parser or from some other parser) and generates BPF code,
7532 * we could perhaps make the offsets parameters of routines
7533 * and, in the handler for an "AND" node, pass to subnodes
7534 * other than the PPPoE node the adjusted offsets.
7536 * This would mean that "pppoes" would, instead of changing the
7537 * behavior of *all* tests after it, change only the behavior
7538 * of tests ANDed with it. That would change the documented
7539 * semantics of "pppoes", which might break some expressions.
7540 * However, it would mean that "(pppoes and ip) or ip" would check
7541 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7542 * checking only for VLAN-encapsulated IP, so that could still
7543 * be considered worth doing; it wouldn't break expressions
7544 * that are of the form "pppoes and ..." which I suspect are the
7545 * most common expressions involving "pppoes". "pppoes or ..."
7546 * doesn't necessarily do what the user would really want, now,
7547 * as all the "or ..." tests would be done assuming PPPoE, even
7548 * though the "or" could be viewed as meaning "or, if this isn't
7549 * a PPPoE packet...".
7551 orig_linktype
= off_linktype
; /* save original values */
7556 * The "network-layer" protocol is PPPoE, which has a 6-byte
7557 * PPPoE header, followed by a PPP packet.
7559 * There is no HDLC encapsulation for the PPP packet (it's
7560 * encapsulated in PPPoES instead), so the link-layer type
7561 * starts at the first byte of the PPP packet. For PPPoE,
7562 * that offset is relative to the beginning of the total
7563 * link-layer payload, including any 802.2 LLC header, so
7564 * it's 6 bytes past off_nl.
7566 off_linktype
= off_nl
+ 6;
7569 * The network-layer offsets are relative to the beginning
7570 * of the MAC-layer payload; that's past the 6-byte
7571 * PPPoE header and the 2-byte PPP header.
7574 off_nl_nosnap
= 6+2;
7580 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
7592 bpf_error("'vpi' supported only on raw ATM");
7593 if (off_vpi
== (u_int
)-1)
7595 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
7601 bpf_error("'vci' supported only on raw ATM");
7602 if (off_vci
== (u_int
)-1)
7604 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
7609 if (off_proto
== (u_int
)-1)
7610 abort(); /* XXX - this isn't on FreeBSD */
7611 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
7616 if (off_payload
== (u_int
)-1)
7618 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
7619 0xffffffff, jtype
, reverse
, jvalue
);
7624 bpf_error("'callref' supported only on raw ATM");
7625 if (off_proto
== (u_int
)-1)
7627 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
7628 jtype
, reverse
, jvalue
);
7638 gen_atmtype_abbrev(type
)
7641 struct block
*b0
, *b1
;
7646 /* Get all packets in Meta signalling Circuit */
7648 bpf_error("'metac' supported only on raw ATM");
7649 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7650 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
7655 /* Get all packets in Broadcast Circuit*/
7657 bpf_error("'bcc' supported only on raw ATM");
7658 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7659 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
7664 /* Get all cells in Segment OAM F4 circuit*/
7666 bpf_error("'oam4sc' supported only on raw ATM");
7667 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7668 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7673 /* Get all cells in End-to-End OAM F4 Circuit*/
7675 bpf_error("'oam4ec' supported only on raw ATM");
7676 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7677 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7682 /* Get all packets in connection Signalling Circuit */
7684 bpf_error("'sc' supported only on raw ATM");
7685 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7686 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
7691 /* Get all packets in ILMI Circuit */
7693 bpf_error("'ilmic' supported only on raw ATM");
7694 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7695 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
7700 /* Get all LANE packets */
7702 bpf_error("'lane' supported only on raw ATM");
7703 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
7706 * Arrange that all subsequent tests assume LANE
7707 * rather than LLC-encapsulated packets, and set
7708 * the offsets appropriately for LANE-encapsulated
7711 * "off_mac" is the offset of the Ethernet header,
7712 * which is 2 bytes past the ATM pseudo-header
7713 * (skipping the pseudo-header and 2-byte LE Client
7714 * field). The other offsets are Ethernet offsets
7715 * relative to "off_mac".
7718 off_mac
= off_payload
+ 2; /* MAC header */
7719 off_linktype
= off_mac
+ 12;
7720 off_macpl
= off_mac
+ 14; /* Ethernet */
7721 off_nl
= 0; /* Ethernet II */
7722 off_nl_nosnap
= 3; /* 802.3+802.2 */
7726 /* Get all LLC-encapsulated packets */
7728 bpf_error("'llc' supported only on raw ATM");
7729 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
7740 * Filtering for MTP2 messages based on li value
7741 * FISU, length is null
7742 * LSSU, length is 1 or 2
7743 * MSU, length is 3 or more
7746 gen_mtp2type_abbrev(type
)
7749 struct block
*b0
, *b1
;
7754 if ( (linktype
!= DLT_MTP2
) &&
7755 (linktype
!= DLT_ERF
) &&
7756 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7757 bpf_error("'fisu' supported only on MTP2");
7758 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
7759 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
7763 if ( (linktype
!= DLT_MTP2
) &&
7764 (linktype
!= DLT_ERF
) &&
7765 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7766 bpf_error("'lssu' supported only on MTP2");
7767 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
7768 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
7773 if ( (linktype
!= DLT_MTP2
) &&
7774 (linktype
!= DLT_ERF
) &&
7775 (linktype
!= DLT_MTP2_WITH_PHDR
) )
7776 bpf_error("'msu' supported only on MTP2");
7777 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
7787 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
7794 bpf_u_int32 val1
, val2
, val3
;
7796 switch (mtp3field
) {
7799 if (off_sio
== (u_int
)-1)
7800 bpf_error("'sio' supported only on SS7");
7801 /* sio coded on 1 byte so max value 255 */
7803 bpf_error("sio value %u too big; max value = 255",
7805 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
7806 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7810 if (off_opc
== (u_int
)-1)
7811 bpf_error("'opc' supported only on SS7");
7812 /* opc coded on 14 bits so max value 16383 */
7814 bpf_error("opc value %u too big; max value = 16383",
7816 /* the following instructions are made to convert jvalue
7817 * to the form used to write opc in an ss7 message*/
7818 val1
= jvalue
& 0x00003c00;
7820 val2
= jvalue
& 0x000003fc;
7822 val3
= jvalue
& 0x00000003;
7824 jvalue
= val1
+ val2
+ val3
;
7825 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
7826 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7830 if (off_dpc
== (u_int
)-1)
7831 bpf_error("'dpc' supported only on SS7");
7832 /* dpc coded on 14 bits so max value 16383 */
7834 bpf_error("dpc value %u too big; max value = 16383",
7836 /* the following instructions are made to convert jvalue
7837 * to the forme used to write dpc in an ss7 message*/
7838 val1
= jvalue
& 0x000000ff;
7840 val2
= jvalue
& 0x00003f00;
7842 jvalue
= val1
+ val2
;
7843 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
7844 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
7848 if (off_sls
== (u_int
)-1)
7849 bpf_error("'sls' supported only on SS7");
7850 /* sls coded on 4 bits so max value 15 */
7852 bpf_error("sls value %u too big; max value = 15",
7854 /* the following instruction is made to convert jvalue
7855 * to the forme used to write sls in an ss7 message*/
7856 jvalue
= jvalue
<< 4;
7857 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
7858 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
7867 static struct block
*
7868 gen_msg_abbrev(type
)
7874 * Q.2931 signalling protocol messages for handling virtual circuits
7875 * establishment and teardown
7880 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
7884 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
7888 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
7892 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
7896 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
7899 case A_RELEASE_DONE
:
7900 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
7910 gen_atmmulti_abbrev(type
)
7913 struct block
*b0
, *b1
;
7919 bpf_error("'oam' supported only on raw ATM");
7920 b1
= gen_atmmulti_abbrev(A_OAMF4
);
7925 bpf_error("'oamf4' supported only on raw ATM");
7927 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
7928 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
7930 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
7936 * Get Q.2931 signalling messages for switched
7937 * virtual connection
7940 bpf_error("'connectmsg' supported only on raw ATM");
7941 b0
= gen_msg_abbrev(A_SETUP
);
7942 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7944 b0
= gen_msg_abbrev(A_CONNECT
);
7946 b0
= gen_msg_abbrev(A_CONNECTACK
);
7948 b0
= gen_msg_abbrev(A_RELEASE
);
7950 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7952 b0
= gen_atmtype_abbrev(A_SC
);
7958 bpf_error("'metaconnect' supported only on raw ATM");
7959 b0
= gen_msg_abbrev(A_SETUP
);
7960 b1
= gen_msg_abbrev(A_CALLPROCEED
);
7962 b0
= gen_msg_abbrev(A_CONNECT
);
7964 b0
= gen_msg_abbrev(A_RELEASE
);
7966 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
7968 b0
= gen_atmtype_abbrev(A_METAC
);