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.221.2.26 2005-07-11 13:58:52 hannes 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"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_PACKET
, /* relative to the beginning of the packet */
139 OR_LINK
, /* relative to the link-layer header */
140 OR_NET
, /* relative to the network-layer header */
141 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
155 #define CHUNK0SIZE 1024
161 static struct chunk chunks
[NCHUNKS
];
162 static int cur_chunk
;
164 static void *newchunk(u_int
);
165 static void freechunks(void);
166 static inline struct block
*new_block(int);
167 static inline struct slist
*new_stmt(int);
168 static struct block
*gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block
*, struct block
*);
172 static void merge(struct block
*, struct block
*);
173 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
178 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
180 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
181 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
182 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
183 static struct slist
*gen_load_llrel(u_int
, u_int
);
184 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
185 static struct slist
*gen_loadx_iphdrlen(void);
186 static struct block
*gen_uncond(int);
187 static inline struct block
*gen_true(void);
188 static inline struct block
*gen_false(void);
189 static struct block
*gen_ether_linktype(int);
190 static struct block
*gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block
*);
192 static void insert_load_llprefixlen(struct block
*);
193 static struct slist
*gen_llprefixlen(void);
194 static struct block
*gen_linktype(int);
195 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
196 static struct block
*gen_llc_linktype(int);
197 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
199 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
201 static struct block
*gen_ahostop(const u_char
*, int);
202 static struct block
*gen_ehostop(const u_char
*, int);
203 static struct block
*gen_fhostop(const u_char
*, int);
204 static struct block
*gen_thostop(const u_char
*, int);
205 static struct block
*gen_wlanhostop(const u_char
*, int);
206 static struct block
*gen_ipfchostop(const u_char
*, int);
207 static struct block
*gen_dnhostop(bpf_u_int32
, int);
208 static struct block
*gen_null(int);
209 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
211 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
214 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
216 static struct block
*gen_ipfrag(void);
217 static struct block
*gen_portatom(int, bpf_int32
);
218 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
220 static struct block
*gen_portatom6(int, bpf_int32
);
221 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
223 struct block
*gen_portop(int, int, int);
224 static struct block
*gen_port(int, int, int);
225 struct block
*gen_portrangeop(int, int, int, int);
226 static struct block
*gen_portrange(int, int, int, int);
228 struct block
*gen_portop6(int, int, int);
229 static struct block
*gen_port6(int, int, int);
230 struct block
*gen_portrangeop6(int, int, int, int);
231 static struct block
*gen_portrange6(int, int, int, int);
233 static int lookup_proto(const char *, int);
234 static struct block
*gen_protochain(int, int, int);
235 static struct block
*gen_proto(int, int, int);
236 static struct slist
*xfer_to_x(struct arth
*);
237 static struct slist
*xfer_to_a(struct arth
*);
238 static struct block
*gen_mac_multicast(int);
239 static struct block
*gen_len(int, int);
241 static struct block
*gen_msg_abbrev(int type
);
252 /* XXX Round up to nearest long. */
253 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
255 /* XXX Round up to structure boundary. */
259 cp
= &chunks
[cur_chunk
];
260 if (n
> cp
->n_left
) {
261 ++cp
, k
= ++cur_chunk
;
263 bpf_error("out of memory");
264 size
= CHUNK0SIZE
<< k
;
265 cp
->m
= (void *)malloc(size
);
267 bpf_error("out of memory");
268 memset((char *)cp
->m
, 0, size
);
271 bpf_error("out of memory");
274 return (void *)((char *)cp
->m
+ cp
->n_left
);
283 for (i
= 0; i
< NCHUNKS
; ++i
)
284 if (chunks
[i
].m
!= NULL
) {
291 * A strdup whose allocations are freed after code generation is over.
295 register const char *s
;
297 int n
= strlen(s
) + 1;
298 char *cp
= newchunk(n
);
304 static inline struct block
*
310 p
= (struct block
*)newchunk(sizeof(*p
));
317 static inline struct slist
*
323 p
= (struct slist
*)newchunk(sizeof(*p
));
329 static struct block
*
333 struct block
*b
= new_block(BPF_RET
|BPF_K
);
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask
;
350 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
351 char *buf
, int optimize
, bpf_u_int32 mask
)
360 if (setjmp(top_ctx
)) {
368 snaplen
= pcap_snapshot(p
);
370 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
371 "snaplen of 0 rejects all packets");
375 lex_init(buf
? buf
: "");
383 root
= gen_retblk(snaplen
);
385 if (optimize
&& !no_optimize
) {
388 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
389 bpf_error("expression rejects all packets");
391 program
->bf_insns
= icode_to_fcode(root
, &len
);
392 program
->bf_len
= len
;
400 * entry point for using the compiler with no pcap open
401 * pass in all the stuff that is needed explicitly instead.
404 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
405 struct bpf_program
*program
,
406 char *buf
, int optimize
, bpf_u_int32 mask
)
411 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
414 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
420 * Clean up a "struct bpf_program" by freeing all the memory allocated
424 pcap_freecode(struct bpf_program
*program
)
427 if (program
->bf_insns
!= NULL
) {
428 free((char *)program
->bf_insns
);
429 program
->bf_insns
= NULL
;
434 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
435 * which of the jt and jf fields has been resolved and which is a pointer
436 * back to another unresolved block (or nil). At least one of the fields
437 * in each block is already resolved.
440 backpatch(list
, target
)
441 struct block
*list
, *target
;
458 * Merge the lists in b0 and b1, using the 'sense' field to indicate
459 * which of jt and jf is the link.
463 struct block
*b0
, *b1
;
465 register struct block
**p
= &b0
;
467 /* Find end of list. */
469 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
471 /* Concatenate the lists. */
479 backpatch(p
, gen_retblk(snaplen
));
480 p
->sense
= !p
->sense
;
481 backpatch(p
, gen_retblk(0));
485 * Insert before the statements of the first (root) block any
486 * statements needed to load the lengths of any variable-length
487 * headers into registers.
489 * XXX - a fancier strategy would be to insert those before the
490 * statements of all blocks that use those lengths and that
491 * have no predecessors that use them, so that we only compute
492 * the lengths if we need them. There might be even better
493 * approaches than that. However, as we're currently only
494 * handling variable-length radiotap headers, and as all
495 * filtering expressions other than raw link[M:N] tests
496 * require the length of that header, doing more for that
497 * header length isn't really worth the effort.
499 insert_load_llprefixlen(root
);
504 struct block
*b0
, *b1
;
506 backpatch(b0
, b1
->head
);
507 b0
->sense
= !b0
->sense
;
508 b1
->sense
= !b1
->sense
;
510 b1
->sense
= !b1
->sense
;
516 struct block
*b0
, *b1
;
518 b0
->sense
= !b0
->sense
;
519 backpatch(b0
, b1
->head
);
520 b0
->sense
= !b0
->sense
;
529 b
->sense
= !b
->sense
;
532 static struct block
*
533 gen_cmp(offrel
, offset
, size
, v
)
534 enum e_offrel offrel
;
538 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
541 static struct block
*
542 gen_cmp_gt(offrel
, offset
, size
, v
)
543 enum e_offrel offrel
;
547 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
550 static struct block
*
551 gen_cmp_ge(offrel
, offset
, size
, v
)
552 enum e_offrel offrel
;
556 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
559 static struct block
*
560 gen_cmp_lt(offrel
, offset
, size
, v
)
561 enum e_offrel offrel
;
565 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
568 static struct block
*
569 gen_cmp_le(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
577 static struct block
*
578 gen_mcmp(offrel
, offset
, size
, v
, mask
)
579 enum e_offrel offrel
;
584 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
587 static struct block
*
588 gen_bcmp(offrel
, offset
, size
, v
)
589 enum e_offrel offrel
;
590 register u_int offset
, size
;
591 register const u_char
*v
;
593 register struct block
*b
, *tmp
;
597 register const u_char
*p
= &v
[size
- 4];
598 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
599 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
601 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
608 register const u_char
*p
= &v
[size
- 2];
609 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
611 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
618 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
627 * AND the field of size "size" at offset "offset" relative to the header
628 * specified by "offrel" with "mask", and compare it with the value "v"
629 * with the test specified by "jtype"; if "reverse" is true, the test
630 * should test the opposite of "jtype".
632 static struct block
*
633 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
634 enum e_offrel offrel
;
636 bpf_u_int32 offset
, size
, mask
, jtype
;
639 struct slist
*s
, *s2
;
642 s
= gen_load_a(offrel
, offset
, size
);
644 if (mask
!= 0xffffffff) {
645 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
650 b
= new_block(JMP(jtype
));
653 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
659 * Various code constructs need to know the layout of the data link
660 * layer. These variables give the necessary offsets from the beginning
661 * of the packet data.
663 * If the link layer has variable_length headers, the offsets are offsets
664 * from the end of the link-link-layer header, and "reg_ll_size" is
665 * the register number for a register containing the length of the
666 * link-layer header. Otherwise, "reg_ll_size" is -1.
668 static int reg_ll_size
;
671 * This is the offset of the beginning of the link-layer header.
672 * It's usually 0, except for 802.11 with a fixed-length radio header.
677 * This is the offset of the beginning of the MAC-layer header.
678 * It's usually 0, except for ATM LANE.
680 static u_int off_mac
;
683 * "off_linktype" is the offset to information in the link-layer header
684 * giving the packet type.
686 * For Ethernet, it's the offset of the Ethernet type field.
688 * For link-layer types that always use 802.2 headers, it's the
689 * offset of the LLC header.
691 * For PPP, it's the offset of the PPP type field.
693 * For Cisco HDLC, it's the offset of the CHDLC type field.
695 * For BSD loopback, it's the offset of the AF_ value.
697 * For Linux cooked sockets, it's the offset of the type field.
699 * It's set to -1 for no encapsulation, in which case, IP is assumed.
701 static u_int off_linktype
;
704 * TRUE if the link layer includes an ATM pseudo-header.
706 static int is_atm
= 0;
709 * TRUE if "lane" appeared in the filter; it causes us to generate
710 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
712 static int is_lane
= 0;
715 * These are offsets for the ATM pseudo-header.
717 static u_int off_vpi
;
718 static u_int off_vci
;
719 static u_int off_proto
;
722 * These are offsets for the MTP3 fields.
724 static u_int off_sio
;
725 static u_int off_opc
;
726 static u_int off_dpc
;
727 static u_int off_sls
;
730 * This is the offset of the first byte after the ATM pseudo_header,
731 * or -1 if there is no ATM pseudo-header.
733 static u_int off_payload
;
736 * These are offsets to the beginning of the network-layer header.
738 * If the link layer never uses 802.2 LLC:
740 * "off_nl" and "off_nl_nosnap" are the same.
742 * If the link layer always uses 802.2 LLC:
744 * "off_nl" is the offset if there's a SNAP header following
747 * "off_nl_nosnap" is the offset if there's no SNAP header.
749 * If the link layer is Ethernet:
751 * "off_nl" is the offset if the packet is an Ethernet II packet
752 * (we assume no 802.3+802.2+SNAP);
754 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
755 * with an 802.2 header following it.
758 static u_int off_nl_nosnap
;
766 linktype
= pcap_datalink(p
);
768 pcap_fddipad
= p
->fddipad
;
772 * Assume it's not raw ATM with a pseudo-header, for now.
788 * Also assume it's not 802.11 with a fixed-length radio header.
794 label_stack_depth
= 0;
802 off_nl
= 6; /* XXX in reality, variable! */
803 off_nl_nosnap
= 6; /* no 802.2 LLC */
806 case DLT_ARCNET_LINUX
:
808 off_nl
= 8; /* XXX in reality, variable! */
809 off_nl_nosnap
= 8; /* no 802.2 LLC */
814 off_nl
= 14; /* Ethernet II */
815 off_nl_nosnap
= 17; /* 802.3+802.2 */
820 * SLIP doesn't have a link level type. The 16 byte
821 * header is hacked into our SLIP driver.
825 off_nl_nosnap
= 16; /* no 802.2 LLC */
829 /* XXX this may be the same as the DLT_PPP_BSDOS case */
833 off_nl_nosnap
= 24; /* no 802.2 LLC */
840 off_nl_nosnap
= 4; /* no 802.2 LLC */
846 off_nl_nosnap
= 12; /* no 802.2 LLC */
851 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
852 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
855 off_nl_nosnap
= 4; /* no 802.2 LLC */
860 * This does no include the Ethernet header, and
861 * only covers session state.
865 off_nl_nosnap
= 8; /* no 802.2 LLC */
871 off_nl_nosnap
= 24; /* no 802.2 LLC */
876 * FDDI doesn't really have a link-level type field.
877 * We set "off_linktype" to the offset of the LLC header.
879 * To check for Ethernet types, we assume that SSAP = SNAP
880 * is being used and pick out the encapsulated Ethernet type.
881 * XXX - should we generate code to check for SNAP?
885 off_linktype
+= pcap_fddipad
;
887 off_nl
= 21; /* FDDI+802.2+SNAP */
888 off_nl_nosnap
= 16; /* FDDI+802.2 */
890 off_nl
+= pcap_fddipad
;
891 off_nl_nosnap
+= pcap_fddipad
;
897 * Token Ring doesn't really have a link-level type field.
898 * We set "off_linktype" to the offset of the LLC header.
900 * To check for Ethernet types, we assume that SSAP = SNAP
901 * is being used and pick out the encapsulated Ethernet type.
902 * XXX - should we generate code to check for SNAP?
904 * XXX - the header is actually variable-length.
905 * Some various Linux patched versions gave 38
906 * as "off_linktype" and 40 as "off_nl"; however,
907 * if a token ring packet has *no* routing
908 * information, i.e. is not source-routed, the correct
909 * values are 20 and 22, as they are in the vanilla code.
911 * A packet is source-routed iff the uppermost bit
912 * of the first byte of the source address, at an
913 * offset of 8, has the uppermost bit set. If the
914 * packet is source-routed, the total number of bytes
915 * of routing information is 2 plus bits 0x1F00 of
916 * the 16-bit value at an offset of 14 (shifted right
917 * 8 - figure out which byte that is).
920 off_nl
= 22; /* Token Ring+802.2+SNAP */
921 off_nl_nosnap
= 17; /* Token Ring+802.2 */
926 * 802.11 doesn't really have a link-level type field.
927 * We set "off_linktype" to the offset of the LLC header.
929 * To check for Ethernet types, we assume that SSAP = SNAP
930 * is being used and pick out the encapsulated Ethernet type.
931 * XXX - should we generate code to check for SNAP?
933 * XXX - the header is actually variable-length. We
934 * assume a 24-byte link-layer header, as appears in
935 * data frames in networks with no bridges. If the
936 * fromds and tods 802.11 header bits are both set,
937 * it's actually supposed to be 30 bytes.
940 off_nl
= 32; /* 802.11+802.2+SNAP */
941 off_nl_nosnap
= 27; /* 802.11+802.2 */
944 case DLT_PRISM_HEADER
:
946 * Same as 802.11, but with an additional header before
947 * the 802.11 header, containing a bunch of additional
948 * information including radio-level information.
950 * The header is 144 bytes long.
952 * XXX - same variable-length header problem; at least
953 * the Prism header is fixed-length.
956 off_linktype
= 144+24;
957 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
958 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
961 case DLT_IEEE802_11_RADIO_AVS
:
963 * Same as 802.11, but with an additional header before
964 * the 802.11 header, containing a bunch of additional
965 * information including radio-level information.
967 * The header is 64 bytes long, at least in its
968 * current incarnation.
970 * XXX - same variable-length header problem, only
971 * more so; this header is also variable-length,
972 * with the length being the 32-bit big-endian
973 * number at an offset of 4 from the beginning
974 * of the radio header.
977 off_linktype
= 64+24;
978 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
979 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
982 case DLT_IEEE802_11_RADIO
:
984 * Same as 802.11, but with an additional header before
985 * the 802.11 header, containing a bunch of additional
986 * information including radio-level information.
988 * The radiotap header is variable length, and we
989 * generate code to compute its length and store it
990 * in a register. These offsets are relative to the
991 * beginning of the 802.11 header.
994 off_nl
= 32; /* 802.11+802.2+SNAP */
995 off_nl_nosnap
= 27; /* 802.11+802.2 */
998 case DLT_ATM_RFC1483
:
999 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1001 * assume routed, non-ISO PDUs
1002 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1004 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1005 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1006 * latter would presumably be treated the way PPPoE
1007 * should be, so you can do "pppoe and udp port 2049"
1008 * or "pppoa and tcp port 80" and have it check for
1009 * PPPo{A,E} and a PPP protocol of IP and....
1012 off_nl
= 8; /* 802.2+SNAP */
1013 off_nl_nosnap
= 3; /* 802.2 */
1018 * Full Frontal ATM; you get AALn PDUs with an ATM
1022 off_vpi
= SUNATM_VPI_POS
;
1023 off_vci
= SUNATM_VCI_POS
;
1024 off_proto
= PROTO_POS
;
1025 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1026 off_payload
= SUNATM_PKT_BEGIN_POS
;
1027 off_linktype
= off_payload
;
1028 off_nl
= off_payload
+8; /* 802.2+SNAP */
1029 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1035 off_nl_nosnap
= 0; /* no 802.2 LLC */
1038 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1041 off_nl_nosnap
= 16; /* no 802.2 LLC */
1046 * LocalTalk does have a 1-byte type field in the LLAP header,
1047 * but really it just indicates whether there is a "short" or
1048 * "long" DDP packet following.
1052 off_nl_nosnap
= 0; /* no 802.2 LLC */
1055 case DLT_IP_OVER_FC
:
1057 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1058 * link-level type field. We set "off_linktype" to the
1059 * offset of the LLC header.
1061 * To check for Ethernet types, we assume that SSAP = SNAP
1062 * is being used and pick out the encapsulated Ethernet type.
1063 * XXX - should we generate code to check for SNAP? RFC
1064 * 2625 says SNAP should be used.
1067 off_nl
= 24; /* IPFC+802.2+SNAP */
1068 off_nl_nosnap
= 19; /* IPFC+802.2 */
1073 * XXX - we should set this to handle SNAP-encapsulated
1074 * frames (NLPID of 0x80).
1078 off_nl_nosnap
= 0; /* no 802.2 LLC */
1081 case DLT_APPLE_IP_OVER_IEEE1394
:
1084 off_nl_nosnap
= 18; /* no 802.2 LLC */
1087 case DLT_LINUX_IRDA
:
1089 * Currently, only raw "link[N:M]" filtering is supported.
1098 * Currently, only raw "link[N:M]" filtering is supported.
1105 case DLT_SYMANTEC_FIREWALL
:
1107 off_nl
= 44; /* Ethernet II */
1108 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1113 /* XXX read this from pf.h? */
1114 off_nl
= PFLOG_HDRLEN
;
1115 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1118 case DLT_JUNIPER_MLFR
:
1119 case DLT_JUNIPER_MLPPP
:
1122 off_nl_nosnap
= -1; /* no 802.2 LLC */
1125 case DLT_JUNIPER_ATM1
:
1126 off_linktype
= 4; /* in reality variable between 4-8 */
1131 case DLT_JUNIPER_ATM2
:
1132 off_linktype
= 8; /* in reality variable between 8-12 */
1137 /* frames captured on a Juniper PPPoE service PIC
1138 * contain raw ethernet frames */
1139 case DLT_JUNIPER_PPPOE
:
1141 off_nl
= 18; /* Ethernet II */
1142 off_nl_nosnap
= 21; /* 802.3+802.2 */
1145 case DLT_JUNIPER_PPPOE_ATM
:
1148 off_nl_nosnap
= -1; /* no 802.2 LLC */
1151 case DLT_JUNIPER_GGSN
:
1154 off_nl_nosnap
= -1; /* no 802.2 LLC */
1157 case DLT_JUNIPER_ES
:
1159 off_nl
= -1; /* not really a network layer but raw IP adresses */
1160 off_nl_nosnap
= -1; /* no 802.2 LLC */
1163 case DLT_JUNIPER_MONITOR
:
1165 off_nl
= 12; /* raw IP/IP6 header */
1166 off_nl_nosnap
= -1; /* no 802.2 LLC */
1169 case DLT_JUNIPER_SERVICES
:
1171 off_nl
= -1; /* L3 proto location dep. on cookie type */
1172 off_nl_nosnap
= -1; /* no 802.2 LLC */
1193 case DLT_LINUX_LAPD
:
1195 * Currently, only raw "link[N:M]" filtering is supported.
1202 bpf_error("unknown data link type %d", linktype
);
1207 * Load a value relative to the beginning of the link-layer header.
1208 * The link-layer header doesn't necessarily begin at the beginning
1209 * of the packet data; there might be a variable-length prefix containing
1210 * radio information.
1212 static struct slist
*
1213 gen_load_llrel(offset
, size
)
1216 struct slist
*s
, *s2
;
1218 s
= gen_llprefixlen();
1221 * If "s" is non-null, it has code to arrange that the X register
1222 * contains the length of the prefix preceding the link-layer
1226 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1230 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1237 * Load a value relative to the beginning of the specified header.
1239 static struct slist
*
1240 gen_load_a(offrel
, offset
, size
)
1241 enum e_offrel offrel
;
1244 struct slist
*s
, *s2
;
1249 s
= gen_load_llrel(offset
, size
);
1253 s
= gen_load_llrel(off_ll
+ offset
, size
);
1257 s
= gen_load_llrel(off_nl
+ offset
, size
);
1261 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1266 * Load the X register with the length of the IPv4 header,
1269 s
= gen_loadx_iphdrlen();
1272 * Load the item at {length of the link-layer header} +
1273 * {length of the IPv4 header} + {specified offset}.
1275 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1276 s2
->s
.k
= off_nl
+ offset
;
1281 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1292 * Generate code to load into the X register the sum of the length of
1293 * the IPv4 header and any variable-length header preceding the link-layer
1296 static struct slist
*
1297 gen_loadx_iphdrlen()
1299 struct slist
*s
, *s2
;
1301 s
= gen_llprefixlen();
1304 * There's a variable-length prefix preceding the
1305 * link-layer header. "s" points to a list of statements
1306 * that put the length of that prefix into the X register.
1307 * The 4*([k]&0xf) addressing mode can't be used, as we
1308 * don't have a constant offset, so we have to load the
1309 * value in question into the A register and add to it
1310 * the value from the X register.
1312 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1315 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1318 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1323 * The A register now contains the length of the
1324 * IP header. We need to add to it the length
1325 * of the prefix preceding the link-layer
1326 * header, which is still in the X register, and
1327 * move the result into the X register.
1329 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1330 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1333 * There is no variable-length header preceding the
1334 * link-layer header; if there's a fixed-length
1335 * header preceding it, its length is included in
1336 * the off_ variables, so it doesn't need to be added.
1338 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1344 static struct block
*
1351 s
= new_stmt(BPF_LD
|BPF_IMM
);
1353 b
= new_block(JMP(BPF_JEQ
));
1359 static inline struct block
*
1362 return gen_uncond(1);
1365 static inline struct block
*
1368 return gen_uncond(0);
1372 * Byte-swap a 32-bit number.
1373 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1374 * big-endian platforms.)
1376 #define SWAPLONG(y) \
1377 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1380 * Generate code to match a particular packet type.
1382 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1383 * value, if <= ETHERMTU. We use that to determine whether to
1384 * match the type/length field or to check the type/length field for
1385 * a value <= ETHERMTU to see whether it's a type field and then do
1386 * the appropriate test.
1388 static struct block
*
1389 gen_ether_linktype(proto
)
1392 struct block
*b0
, *b1
;
1398 case LLCSAP_NETBEUI
:
1400 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1401 * so we check the DSAP and SSAP.
1403 * LLCSAP_IP checks for IP-over-802.2, rather
1404 * than IP-over-Ethernet or IP-over-SNAP.
1406 * XXX - should we check both the DSAP and the
1407 * SSAP, like this, or should we check just the
1408 * DSAP, as we do for other types <= ETHERMTU
1409 * (i.e., other SAP values)?
1411 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1413 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1414 ((proto
<< 8) | proto
));
1422 * Ethernet_II frames, which are Ethernet
1423 * frames with a frame type of ETHERTYPE_IPX;
1425 * Ethernet_802.3 frames, which are 802.3
1426 * frames (i.e., the type/length field is
1427 * a length field, <= ETHERMTU, rather than
1428 * a type field) with the first two bytes
1429 * after the Ethernet/802.3 header being
1432 * Ethernet_802.2 frames, which are 802.3
1433 * frames with an 802.2 LLC header and
1434 * with the IPX LSAP as the DSAP in the LLC
1437 * Ethernet_SNAP frames, which are 802.3
1438 * frames with an LLC header and a SNAP
1439 * header and with an OUI of 0x000000
1440 * (encapsulated Ethernet) and a protocol
1441 * ID of ETHERTYPE_IPX in the SNAP header.
1443 * XXX - should we generate the same code both
1444 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1448 * This generates code to check both for the
1449 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1451 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1452 (bpf_int32
)LLCSAP_IPX
);
1453 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1458 * Now we add code to check for SNAP frames with
1459 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1461 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1465 * Now we generate code to check for 802.3
1466 * frames in general.
1468 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1472 * Now add the check for 802.3 frames before the
1473 * check for Ethernet_802.2 and Ethernet_802.3,
1474 * as those checks should only be done on 802.3
1475 * frames, not on Ethernet frames.
1480 * Now add the check for Ethernet_II frames, and
1481 * do that before checking for the other frame
1484 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1485 (bpf_int32
)ETHERTYPE_IPX
);
1489 case ETHERTYPE_ATALK
:
1490 case ETHERTYPE_AARP
:
1492 * EtherTalk (AppleTalk protocols on Ethernet link
1493 * layer) may use 802.2 encapsulation.
1497 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1498 * we check for an Ethernet type field less than
1499 * 1500, which means it's an 802.3 length field.
1501 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1505 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1506 * SNAP packets with an organization code of
1507 * 0x080007 (Apple, for Appletalk) and a protocol
1508 * type of ETHERTYPE_ATALK (Appletalk).
1510 * 802.2-encapsulated ETHERTYPE_AARP packets are
1511 * SNAP packets with an organization code of
1512 * 0x000000 (encapsulated Ethernet) and a protocol
1513 * type of ETHERTYPE_AARP (Appletalk ARP).
1515 if (proto
== ETHERTYPE_ATALK
)
1516 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1517 else /* proto == ETHERTYPE_AARP */
1518 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1522 * Check for Ethernet encapsulation (Ethertalk
1523 * phase 1?); we just check for the Ethernet
1526 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1532 if (proto
<= ETHERMTU
) {
1534 * This is an LLC SAP value, so the frames
1535 * that match would be 802.2 frames.
1536 * Check that the frame is an 802.2 frame
1537 * (i.e., that the length/type field is
1538 * a length field, <= ETHERMTU) and
1539 * then check the DSAP.
1541 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1543 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1549 * This is an Ethernet type, so compare
1550 * the length/type field with it (if
1551 * the frame is an 802.2 frame, the length
1552 * field will be <= ETHERMTU, and, as
1553 * "proto" is > ETHERMTU, this test
1554 * will fail and the frame won't match,
1555 * which is what we want).
1557 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1564 * Generate code to match a particular packet type.
1566 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1567 * value, if <= ETHERMTU. We use that to determine whether to
1568 * match the type field or to check the type field for the special
1569 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1571 static struct block
*
1572 gen_linux_sll_linktype(proto
)
1575 struct block
*b0
, *b1
;
1581 case LLCSAP_NETBEUI
:
1583 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1584 * so we check the DSAP and SSAP.
1586 * LLCSAP_IP checks for IP-over-802.2, rather
1587 * than IP-over-Ethernet or IP-over-SNAP.
1589 * XXX - should we check both the DSAP and the
1590 * SSAP, like this, or should we check just the
1591 * DSAP, as we do for other types <= ETHERMTU
1592 * (i.e., other SAP values)?
1594 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1595 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1596 ((proto
<< 8) | proto
));
1602 * Ethernet_II frames, which are Ethernet
1603 * frames with a frame type of ETHERTYPE_IPX;
1605 * Ethernet_802.3 frames, which have a frame
1606 * type of LINUX_SLL_P_802_3;
1608 * Ethernet_802.2 frames, which are 802.3
1609 * frames with an 802.2 LLC header (i.e, have
1610 * a frame type of LINUX_SLL_P_802_2) and
1611 * with the IPX LSAP as the DSAP in the LLC
1614 * Ethernet_SNAP frames, which are 802.3
1615 * frames with an LLC header and a SNAP
1616 * header and with an OUI of 0x000000
1617 * (encapsulated Ethernet) and a protocol
1618 * ID of ETHERTYPE_IPX in the SNAP header.
1620 * First, do the checks on LINUX_SLL_P_802_2
1621 * frames; generate the check for either
1622 * Ethernet_802.2 or Ethernet_SNAP frames, and
1623 * then put a check for LINUX_SLL_P_802_2 frames
1626 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1627 (bpf_int32
)LLCSAP_IPX
);
1628 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1631 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1635 * Now check for 802.3 frames and OR that with
1636 * the previous test.
1638 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1642 * Now add the check for Ethernet_II frames, and
1643 * do that before checking for the other frame
1646 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1647 (bpf_int32
)ETHERTYPE_IPX
);
1651 case ETHERTYPE_ATALK
:
1652 case ETHERTYPE_AARP
:
1654 * EtherTalk (AppleTalk protocols on Ethernet link
1655 * layer) may use 802.2 encapsulation.
1659 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1660 * we check for the 802.2 protocol type in the
1661 * "Ethernet type" field.
1663 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1666 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1667 * SNAP packets with an organization code of
1668 * 0x080007 (Apple, for Appletalk) and a protocol
1669 * type of ETHERTYPE_ATALK (Appletalk).
1671 * 802.2-encapsulated ETHERTYPE_AARP packets are
1672 * SNAP packets with an organization code of
1673 * 0x000000 (encapsulated Ethernet) and a protocol
1674 * type of ETHERTYPE_AARP (Appletalk ARP).
1676 if (proto
== ETHERTYPE_ATALK
)
1677 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1679 else /* proto == ETHERTYPE_AARP */
1680 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1685 * Check for Ethernet encapsulation (Ethertalk
1686 * phase 1?); we just check for the Ethernet
1689 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1695 if (proto
<= ETHERMTU
) {
1697 * This is an LLC SAP value, so the frames
1698 * that match would be 802.2 frames.
1699 * Check for the 802.2 protocol type
1700 * in the "Ethernet type" field, and
1701 * then check the DSAP.
1703 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1705 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1711 * This is an Ethernet type, so compare
1712 * the length/type field with it (if
1713 * the frame is an 802.2 frame, the length
1714 * field will be <= ETHERMTU, and, as
1715 * "proto" is > ETHERMTU, this test
1716 * will fail and the frame won't match,
1717 * which is what we want).
1719 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1726 insert_radiotap_load_llprefixlen(b
)
1729 struct slist
*s1
, *s2
;
1732 * Prepend to the statements in this block code to load the
1733 * length of the radiotap header into the register assigned
1734 * to hold that length, if one has been assigned.
1736 if (reg_ll_size
!= -1) {
1738 * The 2 bytes at offsets of 2 and 3 from the beginning
1739 * of the radiotap header are the length of the radiotap
1740 * header; unfortunately, it's little-endian, so we have
1741 * to load it a byte at a time and construct the value.
1745 * Load the high-order byte, at an offset of 3, shift it
1746 * left a byte, and put the result in the X register.
1748 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1750 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1753 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1757 * Load the next byte, at an offset of 2, and OR the
1758 * value from the X register into it.
1760 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1763 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1767 * Now allocate a register to hold that value and store
1770 s2
= new_stmt(BPF_ST
);
1771 s2
->s
.k
= reg_ll_size
;
1775 * Now move it into the X register.
1777 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1781 * Now append all the existing statements in this
1782 * block to these statements.
1784 sappend(s1
, b
->stmts
);
1791 insert_load_llprefixlen(b
)
1796 case DLT_IEEE802_11_RADIO
:
1797 insert_radiotap_load_llprefixlen(b
);
1802 static struct slist
*
1803 gen_radiotap_llprefixlen(void)
1807 if (reg_ll_size
== -1) {
1809 * We haven't yet assigned a register for the length
1810 * of the radiotap header; allocate one.
1812 reg_ll_size
= alloc_reg();
1816 * Load the register containing the radiotap length
1817 * into the X register.
1819 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1820 s
->s
.k
= reg_ll_size
;
1825 * Generate code to compute the link-layer header length, if necessary,
1826 * putting it into the X register, and to return either a pointer to a
1827 * "struct slist" for the list of statements in that code, or NULL if
1828 * no code is necessary.
1830 static struct slist
*
1831 gen_llprefixlen(void)
1835 case DLT_IEEE802_11_RADIO
:
1836 return gen_radiotap_llprefixlen();
1844 * Generate code to match a particular packet type by matching the
1845 * link-layer type field or fields in the 802.2 LLC header.
1847 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1848 * value, if <= ETHERMTU.
1850 static struct block
*
1854 struct block
*b0
, *b1
, *b2
;
1859 return gen_ether_linktype(proto
);
1867 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1871 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1880 case DLT_IEEE802_11
:
1881 case DLT_IEEE802_11_RADIO_AVS
:
1882 case DLT_IEEE802_11_RADIO
:
1883 case DLT_PRISM_HEADER
:
1884 case DLT_ATM_RFC1483
:
1886 case DLT_IP_OVER_FC
:
1887 return gen_llc_linktype(proto
);
1893 * If "is_lane" is set, check for a LANE-encapsulated
1894 * version of this protocol, otherwise check for an
1895 * LLC-encapsulated version of this protocol.
1897 * We assume LANE means Ethernet, not Token Ring.
1901 * Check that the packet doesn't begin with an
1902 * LE Control marker. (We've already generated
1905 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1910 * Now generate an Ethernet test.
1912 b1
= gen_ether_linktype(proto
);
1917 * Check for LLC encapsulation and then check the
1920 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1921 b1
= gen_llc_linktype(proto
);
1929 return gen_linux_sll_linktype(proto
);
1934 case DLT_SLIP_BSDOS
:
1937 * These types don't provide any type field; packets
1940 * XXX - for IPv4, check for a version number of 4, and,
1941 * for IPv6, check for a version number of 6?
1947 case ETHERTYPE_IPV6
:
1949 return gen_true(); /* always true */
1952 return gen_false(); /* always false */
1959 case DLT_PPP_SERIAL
:
1962 * We use Ethernet protocol types inside libpcap;
1963 * map them to the corresponding PPP protocol types.
1972 case ETHERTYPE_IPV6
:
1981 case ETHERTYPE_ATALK
:
1995 * I'm assuming the "Bridging PDU"s that go
1996 * over PPP are Spanning Tree Protocol
2010 * We use Ethernet protocol types inside libpcap;
2011 * map them to the corresponding PPP protocol types.
2016 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2017 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2019 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2024 case ETHERTYPE_IPV6
:
2034 case ETHERTYPE_ATALK
:
2048 * I'm assuming the "Bridging PDU"s that go
2049 * over PPP are Spanning Tree Protocol
2065 * For DLT_NULL, the link-layer header is a 32-bit
2066 * word containing an AF_ value in *host* byte order,
2067 * and for DLT_ENC, the link-layer header begins
2068 * with a 32-bit work containing an AF_ value in
2071 * In addition, if we're reading a saved capture file,
2072 * the host byte order in the capture may not be the
2073 * same as the host byte order on this machine.
2075 * For DLT_LOOP, the link-layer header is a 32-bit
2076 * word containing an AF_ value in *network* byte order.
2078 * XXX - AF_ values may, unfortunately, be platform-
2079 * dependent; for example, FreeBSD's AF_INET6 is 24
2080 * whilst NetBSD's and OpenBSD's is 26.
2082 * This means that, when reading a capture file, just
2083 * checking for our AF_INET6 value won't work if the
2084 * capture file came from another OS.
2093 case ETHERTYPE_IPV6
:
2100 * Not a type on which we support filtering.
2101 * XXX - support those that have AF_ values
2102 * #defined on this platform, at least?
2107 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2109 * The AF_ value is in host byte order, but
2110 * the BPF interpreter will convert it to
2111 * network byte order.
2113 * If this is a save file, and it's from a
2114 * machine with the opposite byte order to
2115 * ours, we byte-swap the AF_ value.
2117 * Then we run it through "htonl()", and
2118 * generate code to compare against the result.
2120 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2121 bpf_pcap
->sf
.swapped
)
2122 proto
= SWAPLONG(proto
);
2123 proto
= htonl(proto
);
2125 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2129 * af field is host byte order in contrast to the rest of
2132 if (proto
== ETHERTYPE_IP
)
2133 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2134 BPF_B
, (bpf_int32
)AF_INET
));
2136 else if (proto
== ETHERTYPE_IPV6
)
2137 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2138 BPF_B
, (bpf_int32
)AF_INET6
));
2146 case DLT_ARCNET_LINUX
:
2148 * XXX should we check for first fragment if the protocol
2157 case ETHERTYPE_IPV6
:
2158 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2159 (bpf_int32
)ARCTYPE_INET6
));
2163 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2164 (bpf_int32
)ARCTYPE_IP
);
2165 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2166 (bpf_int32
)ARCTYPE_IP_OLD
);
2171 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2172 (bpf_int32
)ARCTYPE_ARP
);
2173 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2174 (bpf_int32
)ARCTYPE_ARP_OLD
);
2178 case ETHERTYPE_REVARP
:
2179 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2180 (bpf_int32
)ARCTYPE_REVARP
));
2182 case ETHERTYPE_ATALK
:
2183 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2184 (bpf_int32
)ARCTYPE_ATALK
));
2191 case ETHERTYPE_ATALK
:
2201 * XXX - assumes a 2-byte Frame Relay header with
2202 * DLCI and flags. What if the address is longer?
2208 * Check for the special NLPID for IP.
2210 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2213 case ETHERTYPE_IPV6
:
2215 * Check for the special NLPID for IPv6.
2217 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2222 * Check for several OSI protocols.
2224 * Frame Relay packets typically have an OSI
2225 * NLPID at the beginning; we check for each
2228 * What we check for is the NLPID and a frame
2229 * control field of UI, i.e. 0x03 followed
2232 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2233 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2234 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2245 case DLT_JUNIPER_MLFR
:
2246 case DLT_JUNIPER_MLPPP
:
2247 case DLT_JUNIPER_ATM1
:
2248 case DLT_JUNIPER_ATM2
:
2249 case DLT_JUNIPER_PPPOE
:
2250 case DLT_JUNIPER_PPPOE_ATM
:
2251 case DLT_JUNIPER_GGSN
:
2252 case DLT_JUNIPER_ES
:
2253 case DLT_JUNIPER_MONITOR
:
2254 case DLT_JUNIPER_SERVICES
:
2255 /* just lets verify the magic number for now -
2256 * on ATM we may have up to 6 different encapsulations on the wire
2257 * and need a lot of heuristics to figure out that the payload
2260 * FIXME encapsulation specific BPF_ filters
2262 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2264 case DLT_LINUX_IRDA
:
2265 bpf_error("IrDA link-layer type filtering not implemented");
2268 bpf_error("DOCSIS link-layer type filtering not implemented");
2270 case DLT_LINUX_LAPD
:
2271 bpf_error("LAPD link-layer type filtering not implemented");
2275 * All the types that have no encapsulation should either be
2276 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2277 * all packets are IP packets, or should be handled in some
2278 * special case, if none of them are (if some are and some
2279 * aren't, the lack of encapsulation is a problem, as we'd
2280 * have to find some other way of determining the packet type).
2282 * Therefore, if "off_linktype" is -1, there's an error.
2284 if (off_linktype
== (u_int
)-1)
2288 * Any type not handled above should always have an Ethernet
2289 * type at an offset of "off_linktype". (PPP is partially
2290 * handled above - the protocol type is mapped from the
2291 * Ethernet and LLC types we use internally to the corresponding
2292 * PPP type - but the PPP type is always specified by a value
2293 * at "off_linktype", so we don't have to do the code generation
2296 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2300 * Check for an LLC SNAP packet with a given organization code and
2301 * protocol type; we check the entire contents of the 802.2 LLC and
2302 * snap headers, checking for DSAP and SSAP of SNAP and a control
2303 * field of 0x03 in the LLC header, and for the specified organization
2304 * code and protocol type in the SNAP header.
2306 static struct block
*
2307 gen_snap(orgcode
, ptype
, offset
)
2308 bpf_u_int32 orgcode
;
2312 u_char snapblock
[8];
2314 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2315 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2316 snapblock
[2] = 0x03; /* control = UI */
2317 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2318 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2319 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2320 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2321 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2322 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2326 * Generate code to match a particular packet type, for link-layer types
2327 * using 802.2 LLC headers.
2329 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2330 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2332 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2333 * value, if <= ETHERMTU. We use that to determine whether to
2334 * match the DSAP or both DSAP and LSAP or to check the OUI and
2335 * protocol ID in a SNAP header.
2337 static struct block
*
2338 gen_llc_linktype(proto
)
2342 * XXX - handle token-ring variable-length header.
2348 case LLCSAP_NETBEUI
:
2350 * XXX - should we check both the DSAP and the
2351 * SSAP, like this, or should we check just the
2352 * DSAP, as we do for other types <= ETHERMTU
2353 * (i.e., other SAP values)?
2355 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2356 ((proto
<< 8) | proto
));
2360 * XXX - are there ever SNAP frames for IPX on
2361 * non-Ethernet 802.x networks?
2363 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2364 (bpf_int32
)LLCSAP_IPX
);
2366 case ETHERTYPE_ATALK
:
2368 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2369 * SNAP packets with an organization code of
2370 * 0x080007 (Apple, for Appletalk) and a protocol
2371 * type of ETHERTYPE_ATALK (Appletalk).
2373 * XXX - check for an organization code of
2374 * encapsulated Ethernet as well?
2376 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2380 * XXX - we don't have to check for IPX 802.3
2381 * here, but should we check for the IPX Ethertype?
2383 if (proto
<= ETHERMTU
) {
2385 * This is an LLC SAP value, so check
2388 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2392 * This is an Ethernet type; we assume that it's
2393 * unlikely that it'll appear in the right place
2394 * at random, and therefore check only the
2395 * location that would hold the Ethernet type
2396 * in a SNAP frame with an organization code of
2397 * 0x000000 (encapsulated Ethernet).
2399 * XXX - if we were to check for the SNAP DSAP and
2400 * LSAP, as per XXX, and were also to check for an
2401 * organization code of 0x000000 (encapsulated
2402 * Ethernet), we'd do
2404 * return gen_snap(0x000000, proto,
2407 * here; for now, we don't, as per the above.
2408 * I don't know whether it's worth the extra CPU
2409 * time to do the right check or not.
2411 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2417 static struct block
*
2418 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2422 u_int src_off
, dst_off
;
2424 struct block
*b0
, *b1
;
2438 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2439 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2445 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2446 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2453 if (off_linktype
!= (u_int
)-1)
2454 b0
= gen_linktype(proto
);
2456 b0
= gen_null(Q_IP
);
2457 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2463 static struct block
*
2464 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2465 struct in6_addr
*addr
;
2466 struct in6_addr
*mask
;
2468 u_int src_off
, dst_off
;
2470 struct block
*b0
, *b1
;
2485 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2486 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2492 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2493 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2500 /* this order is important */
2501 a
= (u_int32_t
*)addr
;
2502 m
= (u_int32_t
*)mask
;
2503 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2504 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2506 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2508 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2510 b0
= gen_linktype(proto
);
2516 static struct block
*
2517 gen_ehostop(eaddr
, dir
)
2518 register const u_char
*eaddr
;
2521 register struct block
*b0
, *b1
;
2525 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2528 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2531 b0
= gen_ehostop(eaddr
, Q_SRC
);
2532 b1
= gen_ehostop(eaddr
, Q_DST
);
2538 b0
= gen_ehostop(eaddr
, Q_SRC
);
2539 b1
= gen_ehostop(eaddr
, Q_DST
);
2548 * Like gen_ehostop, but for DLT_FDDI
2550 static struct block
*
2551 gen_fhostop(eaddr
, dir
)
2552 register const u_char
*eaddr
;
2555 struct block
*b0
, *b1
;
2560 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2562 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2567 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2569 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2573 b0
= gen_fhostop(eaddr
, Q_SRC
);
2574 b1
= gen_fhostop(eaddr
, Q_DST
);
2580 b0
= gen_fhostop(eaddr
, Q_SRC
);
2581 b1
= gen_fhostop(eaddr
, Q_DST
);
2590 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2592 static struct block
*
2593 gen_thostop(eaddr
, dir
)
2594 register const u_char
*eaddr
;
2597 register struct block
*b0
, *b1
;
2601 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2604 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2607 b0
= gen_thostop(eaddr
, Q_SRC
);
2608 b1
= gen_thostop(eaddr
, Q_DST
);
2614 b0
= gen_thostop(eaddr
, Q_SRC
);
2615 b1
= gen_thostop(eaddr
, Q_DST
);
2624 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2626 static struct block
*
2627 gen_wlanhostop(eaddr
, dir
)
2628 register const u_char
*eaddr
;
2631 register struct block
*b0
, *b1
, *b2
;
2632 register struct slist
*s
;
2639 * For control frames, there is no SA.
2641 * For management frames, SA is at an
2642 * offset of 10 from the beginning of
2645 * For data frames, SA is at an offset
2646 * of 10 from the beginning of the packet
2647 * if From DS is clear, at an offset of
2648 * 16 from the beginning of the packet
2649 * if From DS is set and To DS is clear,
2650 * and an offset of 24 from the beginning
2651 * of the packet if From DS is set and To DS
2656 * Generate the tests to be done for data frames
2659 * First, check for To DS set, i.e. check "link[1] & 0x01".
2661 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2662 b1
= new_block(JMP(BPF_JSET
));
2663 b1
->s
.k
= 0x01; /* To DS */
2667 * If To DS is set, the SA is at 24.
2669 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2673 * Now, check for To DS not set, i.e. check
2674 * "!(link[1] & 0x01)".
2676 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2677 b2
= new_block(JMP(BPF_JSET
));
2678 b2
->s
.k
= 0x01; /* To DS */
2683 * If To DS is not set, the SA is at 16.
2685 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2689 * Now OR together the last two checks. That gives
2690 * the complete set of checks for data frames with
2696 * Now check for From DS being set, and AND that with
2697 * the ORed-together checks.
2699 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2700 b1
= new_block(JMP(BPF_JSET
));
2701 b1
->s
.k
= 0x02; /* From DS */
2706 * Now check for data frames with From DS not set.
2708 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2709 b2
= new_block(JMP(BPF_JSET
));
2710 b2
->s
.k
= 0x02; /* From DS */
2715 * If From DS isn't set, the SA is at 10.
2717 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2721 * Now OR together the checks for data frames with
2722 * From DS not set and for data frames with From DS
2723 * set; that gives the checks done for data frames.
2728 * Now check for a data frame.
2729 * I.e, check "link[0] & 0x08".
2731 gen_load_a(OR_LINK
, 0, BPF_B
);
2732 b1
= new_block(JMP(BPF_JSET
));
2737 * AND that with the checks done for data frames.
2742 * If the high-order bit of the type value is 0, this
2743 * is a management frame.
2744 * I.e, check "!(link[0] & 0x08)".
2746 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2747 b2
= new_block(JMP(BPF_JSET
));
2753 * For management frames, the SA is at 10.
2755 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2759 * OR that with the checks done for data frames.
2760 * That gives the checks done for management and
2766 * If the low-order bit of the type value is 1,
2767 * this is either a control frame or a frame
2768 * with a reserved type, and thus not a
2771 * I.e., check "!(link[0] & 0x04)".
2773 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2774 b1
= new_block(JMP(BPF_JSET
));
2780 * AND that with the checks for data and management
2790 * For control frames, there is no DA.
2792 * For management frames, DA is at an
2793 * offset of 4 from the beginning of
2796 * For data frames, DA is at an offset
2797 * of 4 from the beginning of the packet
2798 * if To DS is clear and at an offset of
2799 * 16 from the beginning of the packet
2804 * Generate the tests to be done for data frames.
2806 * First, check for To DS set, i.e. "link[1] & 0x01".
2808 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2809 b1
= new_block(JMP(BPF_JSET
));
2810 b1
->s
.k
= 0x01; /* To DS */
2814 * If To DS is set, the DA is at 16.
2816 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2820 * Now, check for To DS not set, i.e. check
2821 * "!(link[1] & 0x01)".
2823 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2824 b2
= new_block(JMP(BPF_JSET
));
2825 b2
->s
.k
= 0x01; /* To DS */
2830 * If To DS is not set, the DA is at 4.
2832 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2836 * Now OR together the last two checks. That gives
2837 * the complete set of checks for data frames.
2842 * Now check for a data frame.
2843 * I.e, check "link[0] & 0x08".
2845 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2846 b1
= new_block(JMP(BPF_JSET
));
2851 * AND that with the checks done for data frames.
2856 * If the high-order bit of the type value is 0, this
2857 * is a management frame.
2858 * I.e, check "!(link[0] & 0x08)".
2860 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2861 b2
= new_block(JMP(BPF_JSET
));
2867 * For management frames, the DA is at 4.
2869 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2873 * OR that with the checks done for data frames.
2874 * That gives the checks done for management and
2880 * If the low-order bit of the type value is 1,
2881 * this is either a control frame or a frame
2882 * with a reserved type, and thus not a
2885 * I.e., check "!(link[0] & 0x04)".
2887 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2888 b1
= new_block(JMP(BPF_JSET
));
2894 * AND that with the checks for data and management
2901 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2902 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2908 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2909 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2918 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2919 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2920 * as the RFC states.)
2922 static struct block
*
2923 gen_ipfchostop(eaddr
, dir
)
2924 register const u_char
*eaddr
;
2927 register struct block
*b0
, *b1
;
2931 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2934 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2937 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2938 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2944 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2945 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2954 * This is quite tricky because there may be pad bytes in front of the
2955 * DECNET header, and then there are two possible data packet formats that
2956 * carry both src and dst addresses, plus 5 packet types in a format that
2957 * carries only the src node, plus 2 types that use a different format and
2958 * also carry just the src node.
2962 * Instead of doing those all right, we just look for data packets with
2963 * 0 or 1 bytes of padding. If you want to look at other packets, that
2964 * will require a lot more hacking.
2966 * To add support for filtering on DECNET "areas" (network numbers)
2967 * one would want to add a "mask" argument to this routine. That would
2968 * make the filter even more inefficient, although one could be clever
2969 * and not generate masking instructions if the mask is 0xFFFF.
2971 static struct block
*
2972 gen_dnhostop(addr
, dir
)
2976 struct block
*b0
, *b1
, *b2
, *tmp
;
2977 u_int offset_lh
; /* offset if long header is received */
2978 u_int offset_sh
; /* offset if short header is received */
2983 offset_sh
= 1; /* follows flags */
2984 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2988 offset_sh
= 3; /* follows flags, dstnode */
2989 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2993 /* Inefficient because we do our Calvinball dance twice */
2994 b0
= gen_dnhostop(addr
, Q_SRC
);
2995 b1
= gen_dnhostop(addr
, Q_DST
);
3001 /* Inefficient because we do our Calvinball dance twice */
3002 b0
= gen_dnhostop(addr
, Q_SRC
);
3003 b1
= gen_dnhostop(addr
, Q_DST
);
3008 bpf_error("ISO host filtering not implemented");
3013 b0
= gen_linktype(ETHERTYPE_DN
);
3014 /* Check for pad = 1, long header case */
3015 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3016 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3017 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3018 BPF_H
, (bpf_int32
)ntohs(addr
));
3020 /* Check for pad = 0, long header case */
3021 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3022 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3025 /* Check for pad = 1, short header case */
3026 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3027 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3028 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3031 /* Check for pad = 0, short header case */
3032 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3033 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3037 /* Combine with test for linktype */
3042 /* generate a null link-layer encapsulation
3044 * which is matching for 0x4 in the first byte of the IPv4 header
3045 * matching for 0x6 in the first byte of the IPv6 header
3047 * we need this for matching to an IP payload in MPLS packets
3049 static struct block
*
3053 struct block
*b0
, *b1
;
3056 /* match the bottom-of-stack bit */
3057 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3058 /* match the IPv4 version number */
3059 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3063 /* match the bottom-of-stack bit */
3064 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3065 /* match the IPv4 version number */
3066 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3074 static struct block
*
3075 gen_host(addr
, mask
, proto
, dir
)
3081 struct block
*b0
, *b1
;
3086 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
3087 if (off_linktype
!= (u_int
)-1) {
3088 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
3090 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
3096 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3099 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3102 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3105 bpf_error("'tcp' modifier applied to host");
3108 bpf_error("'sctp' modifier applied to host");
3111 bpf_error("'udp' modifier applied to host");
3114 bpf_error("'icmp' modifier applied to host");
3117 bpf_error("'igmp' modifier applied to host");
3120 bpf_error("'igrp' modifier applied to host");
3123 bpf_error("'pim' modifier applied to host");
3126 bpf_error("'vrrp' modifier applied to host");
3129 bpf_error("ATALK host filtering not implemented");
3132 bpf_error("AARP host filtering not implemented");
3135 return gen_dnhostop(addr
, dir
);
3138 bpf_error("SCA host filtering not implemented");
3141 bpf_error("LAT host filtering not implemented");
3144 bpf_error("MOPDL host filtering not implemented");
3147 bpf_error("MOPRC host filtering not implemented");
3151 bpf_error("'ip6' modifier applied to ip host");
3154 bpf_error("'icmp6' modifier applied to host");
3158 bpf_error("'ah' modifier applied to host");
3161 bpf_error("'esp' modifier applied to host");
3164 bpf_error("ISO host filtering not implemented");
3167 bpf_error("'esis' modifier applied to host");
3170 bpf_error("'isis' modifier applied to host");
3173 bpf_error("'clnp' modifier applied to host");
3176 bpf_error("'stp' modifier applied to host");
3179 bpf_error("IPX host filtering not implemented");
3182 bpf_error("'netbeui' modifier applied to host");
3185 bpf_error("'radio' modifier applied to host");
3194 static struct block
*
3195 gen_host6(addr
, mask
, proto
, dir
)
3196 struct in6_addr
*addr
;
3197 struct in6_addr
*mask
;
3204 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3207 bpf_error("'ip' modifier applied to ip6 host");
3210 bpf_error("'rarp' modifier applied to ip6 host");
3213 bpf_error("'arp' modifier applied to ip6 host");
3216 bpf_error("'sctp' modifier applied to host");
3219 bpf_error("'tcp' modifier applied to host");
3222 bpf_error("'udp' modifier applied to host");
3225 bpf_error("'icmp' modifier applied to host");
3228 bpf_error("'igmp' modifier applied to host");
3231 bpf_error("'igrp' modifier applied to host");
3234 bpf_error("'pim' modifier applied to host");
3237 bpf_error("'vrrp' modifier applied to host");
3240 bpf_error("ATALK host filtering not implemented");
3243 bpf_error("AARP host filtering not implemented");
3246 bpf_error("'decnet' modifier applied to ip6 host");
3249 bpf_error("SCA host filtering not implemented");
3252 bpf_error("LAT host filtering not implemented");
3255 bpf_error("MOPDL host filtering not implemented");
3258 bpf_error("MOPRC host filtering not implemented");
3261 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3264 bpf_error("'icmp6' modifier applied to host");
3267 bpf_error("'ah' modifier applied to host");
3270 bpf_error("'esp' modifier applied to host");
3273 bpf_error("ISO host filtering not implemented");
3276 bpf_error("'esis' modifier applied to host");
3279 bpf_error("'isis' modifier applied to host");
3282 bpf_error("'clnp' modifier applied to host");
3285 bpf_error("'stp' modifier applied to host");
3288 bpf_error("IPX host filtering not implemented");
3291 bpf_error("'netbeui' modifier applied to host");
3294 bpf_error("'radio' modifier applied to host");
3304 static struct block
*
3305 gen_gateway(eaddr
, alist
, proto
, dir
)
3306 const u_char
*eaddr
;
3307 bpf_u_int32
**alist
;
3311 struct block
*b0
, *b1
, *tmp
;
3314 bpf_error("direction applied to 'gateway'");
3321 if (linktype
== DLT_EN10MB
)
3322 b0
= gen_ehostop(eaddr
, Q_OR
);
3323 else if (linktype
== DLT_FDDI
)
3324 b0
= gen_fhostop(eaddr
, Q_OR
);
3325 else if (linktype
== DLT_IEEE802
)
3326 b0
= gen_thostop(eaddr
, Q_OR
);
3327 else if (linktype
== DLT_IEEE802_11
||
3328 linktype
== DLT_IEEE802_11_RADIO_AVS
||
3329 linktype
== DLT_IEEE802_11_RADIO
||
3330 linktype
== DLT_PRISM_HEADER
)
3331 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3332 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3334 * Check that the packet doesn't begin with an
3335 * LE Control marker. (We've already generated
3338 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3343 * Now check the MAC address.
3345 b0
= gen_ehostop(eaddr
, Q_OR
);
3347 } else if (linktype
== DLT_IP_OVER_FC
)
3348 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3351 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3353 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3355 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3363 bpf_error("illegal modifier of 'gateway'");
3369 gen_proto_abbrev(proto
)
3378 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3380 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3386 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3388 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3394 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3396 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3402 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3405 #ifndef IPPROTO_IGMP
3406 #define IPPROTO_IGMP 2
3410 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3413 #ifndef IPPROTO_IGRP
3414 #define IPPROTO_IGRP 9
3417 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3421 #define IPPROTO_PIM 103
3425 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3427 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3432 #ifndef IPPROTO_VRRP
3433 #define IPPROTO_VRRP 112
3437 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3441 if (off_linktype
!= (u_int
)-1)
3442 b1
= gen_linktype(ETHERTYPE_IP
);
3444 b1
= gen_null(Q_IP
);
3448 b1
= gen_linktype(ETHERTYPE_ARP
);
3452 b1
= gen_linktype(ETHERTYPE_REVARP
);
3456 bpf_error("link layer applied in wrong context");
3459 b1
= gen_linktype(ETHERTYPE_ATALK
);
3463 b1
= gen_linktype(ETHERTYPE_AARP
);
3467 b1
= gen_linktype(ETHERTYPE_DN
);
3471 b1
= gen_linktype(ETHERTYPE_SCA
);
3475 b1
= gen_linktype(ETHERTYPE_LAT
);
3479 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3483 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3488 b1
= gen_linktype(ETHERTYPE_IPV6
);
3491 #ifndef IPPROTO_ICMPV6
3492 #define IPPROTO_ICMPV6 58
3495 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3500 #define IPPROTO_AH 51
3503 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3505 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3511 #define IPPROTO_ESP 50
3514 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3516 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3522 b1
= gen_linktype(LLCSAP_ISONS
);
3526 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3530 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3533 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3534 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3535 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3537 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3539 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3541 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3545 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3546 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3547 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3549 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3551 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3553 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3557 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3558 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3559 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3561 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3566 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3567 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3572 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3573 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3575 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3577 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3582 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3583 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3588 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3589 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3594 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3598 b1
= gen_linktype(LLCSAP_8021D
);
3602 b1
= gen_linktype(LLCSAP_IPX
);
3606 b1
= gen_linktype(LLCSAP_NETBEUI
);
3610 bpf_error("'radio' is not a valid protocol type");
3618 static struct block
*
3625 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3626 b
= new_block(JMP(BPF_JSET
));
3635 * Generate a comparison to a port value in the transport-layer header
3636 * at the specified offset from the beginning of that header.
3638 * XXX - this handles a variable-length prefix preceding the link-layer
3639 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3640 * variable-length link-layer headers (such as Token Ring or 802.11
3643 static struct block
*
3644 gen_portatom(off
, v
)
3648 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3652 static struct block
*
3653 gen_portatom6(off
, v
)
3657 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3662 gen_portop(port
, proto
, dir
)
3663 int port
, proto
, dir
;
3665 struct block
*b0
, *b1
, *tmp
;
3667 /* ip proto 'proto' */
3668 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3674 b1
= gen_portatom(0, (bpf_int32
)port
);
3678 b1
= gen_portatom(2, (bpf_int32
)port
);
3683 tmp
= gen_portatom(0, (bpf_int32
)port
);
3684 b1
= gen_portatom(2, (bpf_int32
)port
);
3689 tmp
= gen_portatom(0, (bpf_int32
)port
);
3690 b1
= gen_portatom(2, (bpf_int32
)port
);
3702 static struct block
*
3703 gen_port(port
, ip_proto
, dir
)
3708 struct block
*b0
, *b1
, *tmp
;
3713 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3714 * not LLC encapsulation with LLCSAP_IP.
3716 * For IEEE 802 networks - which includes 802.5 token ring
3717 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3718 * says that SNAP encapsulation is used, not LLC encapsulation
3721 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3722 * RFC 2225 say that SNAP encapsulation is used, not LLC
3723 * encapsulation with LLCSAP_IP.
3725 * So we always check for ETHERTYPE_IP.
3727 b0
= gen_linktype(ETHERTYPE_IP
);
3733 b1
= gen_portop(port
, ip_proto
, dir
);
3737 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3738 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3740 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3753 gen_portop6(port
, proto
, dir
)
3754 int port
, proto
, dir
;
3756 struct block
*b0
, *b1
, *tmp
;
3758 /* ip6 proto 'proto' */
3759 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3763 b1
= gen_portatom6(0, (bpf_int32
)port
);
3767 b1
= gen_portatom6(2, (bpf_int32
)port
);
3772 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3773 b1
= gen_portatom6(2, (bpf_int32
)port
);
3778 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3779 b1
= gen_portatom6(2, (bpf_int32
)port
);
3791 static struct block
*
3792 gen_port6(port
, ip_proto
, dir
)
3797 struct block
*b0
, *b1
, *tmp
;
3799 /* link proto ip6 */
3800 b0
= gen_linktype(ETHERTYPE_IPV6
);
3806 b1
= gen_portop6(port
, ip_proto
, dir
);
3810 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3811 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3813 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3825 /* gen_portrange code */
3826 static struct block
*
3827 gen_portrangeatom(off
, v1
, v2
)
3831 struct block
*b1
, *b2
;
3835 * Reverse the order of the ports, so v1 is the lower one.
3844 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3845 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3853 gen_portrangeop(port1
, port2
, proto
, dir
)
3858 struct block
*b0
, *b1
, *tmp
;
3860 /* ip proto 'proto' */
3861 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3867 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3871 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3876 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3877 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3882 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3883 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3895 static struct block
*
3896 gen_portrange(port1
, port2
, ip_proto
, dir
)
3901 struct block
*b0
, *b1
, *tmp
;
3904 b0
= gen_linktype(ETHERTYPE_IP
);
3910 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3914 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3915 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3917 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3929 static struct block
*
3930 gen_portrangeatom6(off
, v1
, v2
)
3934 struct block
*b1
, *b2
;
3938 * Reverse the order of the ports, so v1 is the lower one.
3947 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3948 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3956 gen_portrangeop6(port1
, port2
, proto
, dir
)
3961 struct block
*b0
, *b1
, *tmp
;
3963 /* ip6 proto 'proto' */
3964 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3968 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3972 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3977 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3978 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3983 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3984 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3996 static struct block
*
3997 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4002 struct block
*b0
, *b1
, *tmp
;
4004 /* link proto ip6 */
4005 b0
= gen_linktype(ETHERTYPE_IPV6
);
4011 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4015 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4016 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4018 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4031 lookup_proto(name
, proto
)
4032 register const char *name
;
4042 v
= pcap_nametoproto(name
);
4043 if (v
== PROTO_UNDEF
)
4044 bpf_error("unknown ip proto '%s'", name
);
4048 /* XXX should look up h/w protocol type based on linktype */
4049 v
= pcap_nametoeproto(name
);
4050 if (v
== PROTO_UNDEF
) {
4051 v
= pcap_nametollc(name
);
4052 if (v
== PROTO_UNDEF
)
4053 bpf_error("unknown ether proto '%s'", name
);
4058 if (strcmp(name
, "esis") == 0)
4060 else if (strcmp(name
, "isis") == 0)
4062 else if (strcmp(name
, "clnp") == 0)
4065 bpf_error("unknown osi proto '%s'", name
);
4085 static struct block
*
4086 gen_protochain(v
, proto
, dir
)
4091 #ifdef NO_PROTOCHAIN
4092 return gen_proto(v
, proto
, dir
);
4094 struct block
*b0
, *b
;
4095 struct slist
*s
[100];
4096 int fix2
, fix3
, fix4
, fix5
;
4097 int ahcheck
, again
, end
;
4099 int reg2
= alloc_reg();
4101 memset(s
, 0, sizeof(s
));
4102 fix2
= fix3
= fix4
= fix5
= 0;
4109 b0
= gen_protochain(v
, Q_IP
, dir
);
4110 b
= gen_protochain(v
, Q_IPV6
, dir
);
4114 bpf_error("bad protocol applied for 'protochain'");
4119 * We don't handle variable-length radiotap here headers yet.
4120 * We might want to add BPF instructions to do the protochain
4121 * work, to simplify that and, on platforms that have a BPF
4122 * interpreter with the new instructions, let the filtering
4123 * be done in the kernel. (We already require a modified BPF
4124 * engine to do the protochain stuff, to support backward
4125 * branches, and backward branch support is unlikely to appear
4126 * in kernel BPF engines.)
4128 if (linktype
== DLT_IEEE802_11_RADIO
)
4129 bpf_error("'protochain' not supported with radiotap headers");
4131 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4134 * s[0] is a dummy entry to protect other BPF insn from damage
4135 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4136 * hard to find interdependency made by jump table fixup.
4139 s
[i
] = new_stmt(0); /*dummy*/
4144 b0
= gen_linktype(ETHERTYPE_IP
);
4147 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4148 s
[i
]->s
.k
= off_nl
+ 9;
4150 /* X = ip->ip_hl << 2 */
4151 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4157 b0
= gen_linktype(ETHERTYPE_IPV6
);
4159 /* A = ip6->ip_nxt */
4160 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4161 s
[i
]->s
.k
= off_nl
+ 6;
4163 /* X = sizeof(struct ip6_hdr) */
4164 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4170 bpf_error("unsupported proto to gen_protochain");
4174 /* again: if (A == v) goto end; else fall through; */
4176 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4178 s
[i
]->s
.jt
= NULL
; /*later*/
4179 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4183 #ifndef IPPROTO_NONE
4184 #define IPPROTO_NONE 59
4186 /* if (A == IPPROTO_NONE) goto end */
4187 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4188 s
[i
]->s
.jt
= NULL
; /*later*/
4189 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4190 s
[i
]->s
.k
= IPPROTO_NONE
;
4191 s
[fix5
]->s
.jf
= s
[i
];
4196 if (proto
== Q_IPV6
) {
4197 int v6start
, v6end
, v6advance
, j
;
4200 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4201 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4202 s
[i
]->s
.jt
= NULL
; /*later*/
4203 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4204 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4205 s
[fix2
]->s
.jf
= s
[i
];
4207 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4208 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4209 s
[i
]->s
.jt
= NULL
; /*later*/
4210 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4211 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4213 /* if (A == IPPROTO_ROUTING) goto v6advance */
4214 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4215 s
[i
]->s
.jt
= NULL
; /*later*/
4216 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4217 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4219 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4220 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4221 s
[i
]->s
.jt
= NULL
; /*later*/
4222 s
[i
]->s
.jf
= NULL
; /*later*/
4223 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4234 * X = X + (P[X + 1] + 1) * 8;
4237 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4239 /* A = P[X + packet head] */
4240 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4244 s
[i
] = new_stmt(BPF_ST
);
4248 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4251 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4255 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4257 /* A = P[X + packet head]; */
4258 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4262 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4266 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4270 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4273 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4277 /* goto again; (must use BPF_JA for backward jump) */
4278 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4279 s
[i
]->s
.k
= again
- i
- 1;
4280 s
[i
- 1]->s
.jf
= s
[i
];
4284 for (j
= v6start
; j
<= v6end
; j
++)
4285 s
[j
]->s
.jt
= s
[v6advance
];
4290 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4292 s
[fix2
]->s
.jf
= s
[i
];
4298 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4299 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4300 s
[i
]->s
.jt
= NULL
; /*later*/
4301 s
[i
]->s
.jf
= NULL
; /*later*/
4302 s
[i
]->s
.k
= IPPROTO_AH
;
4304 s
[fix3
]->s
.jf
= s
[ahcheck
];
4311 * X = X + (P[X + 1] + 2) * 4;
4314 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4316 /* A = P[X + packet head]; */
4317 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4321 s
[i
] = new_stmt(BPF_ST
);
4325 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4328 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4332 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4334 /* A = P[X + packet head] */
4335 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4339 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4343 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4347 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4350 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4354 /* goto again; (must use BPF_JA for backward jump) */
4355 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4356 s
[i
]->s
.k
= again
- i
- 1;
4361 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4363 s
[fix2
]->s
.jt
= s
[end
];
4364 s
[fix4
]->s
.jf
= s
[end
];
4365 s
[fix5
]->s
.jt
= s
[end
];
4372 for (i
= 0; i
< max
- 1; i
++)
4373 s
[i
]->next
= s
[i
+ 1];
4374 s
[max
- 1]->next
= NULL
;
4379 b
= new_block(JMP(BPF_JEQ
));
4380 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4391 * Generate code that checks whether the packet is a packet for protocol
4392 * <proto> and whether the type field in that protocol's header has
4393 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4394 * IP packet and checks the protocol number in the IP header against <v>.
4396 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4397 * against Q_IP and Q_IPV6.
4399 static struct block
*
4400 gen_proto(v
, proto
, dir
)
4405 struct block
*b0
, *b1
;
4407 if (dir
!= Q_DEFAULT
)
4408 bpf_error("direction applied to 'proto'");
4413 b0
= gen_proto(v
, Q_IP
, dir
);
4414 b1
= gen_proto(v
, Q_IPV6
, dir
);
4422 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4423 * not LLC encapsulation with LLCSAP_IP.
4425 * For IEEE 802 networks - which includes 802.5 token ring
4426 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4427 * says that SNAP encapsulation is used, not LLC encapsulation
4430 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4431 * RFC 2225 say that SNAP encapsulation is used, not LLC
4432 * encapsulation with LLCSAP_IP.
4434 * So we always check for ETHERTYPE_IP.
4437 if (off_linktype
!= (u_int
)-1)
4438 b0
= gen_linktype(ETHERTYPE_IP
);
4440 b0
= gen_null(Q_IP
);
4442 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4444 b1
= gen_protochain(v
, Q_IP
);
4454 * Frame Relay packets typically have an OSI
4455 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4456 * generates code to check for all the OSI
4457 * NLPIDs, so calling it and then adding a check
4458 * for the particular NLPID for which we're
4459 * looking is bogus, as we can just check for
4462 * What we check for is the NLPID and a frame
4463 * control field value of UI, i.e. 0x03 followed
4466 * XXX - assumes a 2-byte Frame Relay header with
4467 * DLCI and flags. What if the address is longer?
4469 * XXX - what about SNAP-encapsulated frames?
4471 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4477 * Cisco uses an Ethertype lookalike - for OSI,
4480 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4481 /* OSI in C-HDLC is stuffed with a fudge byte */
4482 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4487 b0
= gen_linktype(LLCSAP_ISONS
);
4488 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4494 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4496 * 4 is the offset of the PDU type relative to the IS-IS
4499 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4504 bpf_error("arp does not encapsulate another protocol");
4508 bpf_error("rarp does not encapsulate another protocol");
4512 bpf_error("atalk encapsulation is not specifiable");
4516 bpf_error("decnet encapsulation is not specifiable");
4520 bpf_error("sca does not encapsulate another protocol");
4524 bpf_error("lat does not encapsulate another protocol");
4528 bpf_error("moprc does not encapsulate another protocol");
4532 bpf_error("mopdl does not encapsulate another protocol");
4536 return gen_linktype(v
);
4539 bpf_error("'udp proto' is bogus");
4543 bpf_error("'tcp proto' is bogus");
4547 bpf_error("'sctp proto' is bogus");
4551 bpf_error("'icmp proto' is bogus");
4555 bpf_error("'igmp proto' is bogus");
4559 bpf_error("'igrp proto' is bogus");
4563 bpf_error("'pim proto' is bogus");
4567 bpf_error("'vrrp proto' is bogus");
4572 b0
= gen_linktype(ETHERTYPE_IPV6
);
4574 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4576 b1
= gen_protochain(v
, Q_IPV6
);
4582 bpf_error("'icmp6 proto' is bogus");
4586 bpf_error("'ah proto' is bogus");
4589 bpf_error("'ah proto' is bogus");
4592 bpf_error("'stp proto' is bogus");
4595 bpf_error("'ipx proto' is bogus");
4598 bpf_error("'netbeui proto' is bogus");
4601 bpf_error("'radio proto' is bogus");
4612 register const char *name
;
4615 int proto
= q
.proto
;
4619 bpf_u_int32 mask
, addr
;
4621 bpf_u_int32
**alist
;
4624 struct sockaddr_in
*sin
;
4625 struct sockaddr_in6
*sin6
;
4626 struct addrinfo
*res
, *res0
;
4627 struct in6_addr mask128
;
4629 struct block
*b
, *tmp
;
4630 int port
, real_proto
;
4636 addr
= pcap_nametonetaddr(name
);
4638 bpf_error("unknown network '%s'", name
);
4639 /* Left justify network addr and calculate its network mask */
4641 while (addr
&& (addr
& 0xff000000) == 0) {
4645 return gen_host(addr
, mask
, proto
, dir
);
4649 if (proto
== Q_LINK
) {
4653 eaddr
= pcap_ether_hostton(name
);
4656 "unknown ether host '%s'", name
);
4657 b
= gen_ehostop(eaddr
, dir
);
4662 eaddr
= pcap_ether_hostton(name
);
4665 "unknown FDDI host '%s'", name
);
4666 b
= gen_fhostop(eaddr
, dir
);
4671 eaddr
= pcap_ether_hostton(name
);
4674 "unknown token ring host '%s'", name
);
4675 b
= gen_thostop(eaddr
, dir
);
4679 case DLT_IEEE802_11
:
4680 case DLT_IEEE802_11_RADIO_AVS
:
4681 case DLT_IEEE802_11_RADIO
:
4682 case DLT_PRISM_HEADER
:
4683 eaddr
= pcap_ether_hostton(name
);
4686 "unknown 802.11 host '%s'", name
);
4687 b
= gen_wlanhostop(eaddr
, dir
);
4691 case DLT_IP_OVER_FC
:
4692 eaddr
= pcap_ether_hostton(name
);
4695 "unknown Fibre Channel host '%s'", name
);
4696 b
= gen_ipfchostop(eaddr
, dir
);
4705 * Check that the packet doesn't begin
4706 * with an LE Control marker. (We've
4707 * already generated a test for LANE.)
4709 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4713 eaddr
= pcap_ether_hostton(name
);
4716 "unknown ether host '%s'", name
);
4717 b
= gen_ehostop(eaddr
, dir
);
4723 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4724 } else if (proto
== Q_DECNET
) {
4725 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4727 * I don't think DECNET hosts can be multihomed, so
4728 * there is no need to build up a list of addresses
4730 return (gen_host(dn_addr
, 0, proto
, dir
));
4733 alist
= pcap_nametoaddr(name
);
4734 if (alist
== NULL
|| *alist
== NULL
)
4735 bpf_error("unknown host '%s'", name
);
4737 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4739 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4741 tmp
= gen_host(**alist
++, 0xffffffff,
4748 memset(&mask128
, 0xff, sizeof(mask128
));
4749 res0
= res
= pcap_nametoaddrinfo(name
);
4751 bpf_error("unknown host '%s'", name
);
4753 tproto
= tproto6
= proto
;
4754 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4758 for (res
= res0
; res
; res
= res
->ai_next
) {
4759 switch (res
->ai_family
) {
4761 if (tproto
== Q_IPV6
)
4764 sin
= (struct sockaddr_in
*)
4766 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4767 0xffffffff, tproto
, dir
);
4770 if (tproto6
== Q_IP
)
4773 sin6
= (struct sockaddr_in6
*)
4775 tmp
= gen_host6(&sin6
->sin6_addr
,
4776 &mask128
, tproto6
, dir
);
4787 bpf_error("unknown host '%s'%s", name
,
4788 (proto
== Q_DEFAULT
)
4790 : " for specified address family");
4797 if (proto
!= Q_DEFAULT
&&
4798 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4799 bpf_error("illegal qualifier of 'port'");
4800 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4801 bpf_error("unknown port '%s'", name
);
4802 if (proto
== Q_UDP
) {
4803 if (real_proto
== IPPROTO_TCP
)
4804 bpf_error("port '%s' is tcp", name
);
4805 else if (real_proto
== IPPROTO_SCTP
)
4806 bpf_error("port '%s' is sctp", name
);
4808 /* override PROTO_UNDEF */
4809 real_proto
= IPPROTO_UDP
;
4811 if (proto
== Q_TCP
) {
4812 if (real_proto
== IPPROTO_UDP
)
4813 bpf_error("port '%s' is udp", name
);
4815 else if (real_proto
== IPPROTO_SCTP
)
4816 bpf_error("port '%s' is sctp", name
);
4818 /* override PROTO_UNDEF */
4819 real_proto
= IPPROTO_TCP
;
4821 if (proto
== Q_SCTP
) {
4822 if (real_proto
== IPPROTO_UDP
)
4823 bpf_error("port '%s' is udp", name
);
4825 else if (real_proto
== IPPROTO_TCP
)
4826 bpf_error("port '%s' is tcp", name
);
4828 /* override PROTO_UNDEF */
4829 real_proto
= IPPROTO_SCTP
;
4832 return gen_port(port
, real_proto
, dir
);
4836 b
= gen_port(port
, real_proto
, dir
);
4837 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4843 if (proto
!= Q_DEFAULT
&&
4844 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4845 bpf_error("illegal qualifier of 'portrange'");
4846 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4847 bpf_error("unknown port in range '%s'", name
);
4848 if (proto
== Q_UDP
) {
4849 if (real_proto
== IPPROTO_TCP
)
4850 bpf_error("port in range '%s' is tcp", name
);
4851 else if (real_proto
== IPPROTO_SCTP
)
4852 bpf_error("port in range '%s' is sctp", name
);
4854 /* override PROTO_UNDEF */
4855 real_proto
= IPPROTO_UDP
;
4857 if (proto
== Q_TCP
) {
4858 if (real_proto
== IPPROTO_UDP
)
4859 bpf_error("port in range '%s' is udp", name
);
4860 else if (real_proto
== IPPROTO_SCTP
)
4861 bpf_error("port in range '%s' is sctp", name
);
4863 /* override PROTO_UNDEF */
4864 real_proto
= IPPROTO_TCP
;
4866 if (proto
== Q_SCTP
) {
4867 if (real_proto
== IPPROTO_UDP
)
4868 bpf_error("port in range '%s' is udp", name
);
4869 else if (real_proto
== IPPROTO_TCP
)
4870 bpf_error("port in range '%s' is tcp", name
);
4872 /* override PROTO_UNDEF */
4873 real_proto
= IPPROTO_SCTP
;
4876 return gen_portrange(port1
, port2
, real_proto
, dir
);
4880 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4881 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4888 eaddr
= pcap_ether_hostton(name
);
4890 bpf_error("unknown ether host: %s", name
);
4892 alist
= pcap_nametoaddr(name
);
4893 if (alist
== NULL
|| *alist
== NULL
)
4894 bpf_error("unknown host '%s'", name
);
4895 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4899 bpf_error("'gateway' not supported in this configuration");
4903 real_proto
= lookup_proto(name
, proto
);
4904 if (real_proto
>= 0)
4905 return gen_proto(real_proto
, proto
, dir
);
4907 bpf_error("unknown protocol: %s", name
);
4910 real_proto
= lookup_proto(name
, proto
);
4911 if (real_proto
>= 0)
4912 return gen_protochain(real_proto
, proto
, dir
);
4914 bpf_error("unknown protocol: %s", name
);
4926 gen_mcode(s1
, s2
, masklen
, q
)
4927 register const char *s1
, *s2
;
4928 register int masklen
;
4931 register int nlen
, mlen
;
4934 nlen
= __pcap_atoin(s1
, &n
);
4935 /* Promote short ipaddr */
4939 mlen
= __pcap_atoin(s2
, &m
);
4940 /* Promote short ipaddr */
4943 bpf_error("non-network bits set in \"%s mask %s\"",
4946 /* Convert mask len to mask */
4948 bpf_error("mask length must be <= 32");
4949 m
= 0xffffffff << (32 - masklen
);
4951 bpf_error("non-network bits set in \"%s/%d\"",
4958 return gen_host(n
, m
, q
.proto
, q
.dir
);
4961 bpf_error("Mask syntax for networks only");
4969 register const char *s
;
4974 int proto
= q
.proto
;
4980 else if (q
.proto
== Q_DECNET
)
4981 vlen
= __pcap_atodn(s
, &v
);
4983 vlen
= __pcap_atoin(s
, &v
);
4990 if (proto
== Q_DECNET
)
4991 return gen_host(v
, 0, proto
, dir
);
4992 else if (proto
== Q_LINK
) {
4993 bpf_error("illegal link layer address");
4996 if (s
== NULL
&& q
.addr
== Q_NET
) {
4997 /* Promote short net number */
4998 while (v
&& (v
& 0xff000000) == 0) {
5003 /* Promote short ipaddr */
5007 return gen_host(v
, mask
, proto
, dir
);
5012 proto
= IPPROTO_UDP
;
5013 else if (proto
== Q_TCP
)
5014 proto
= IPPROTO_TCP
;
5015 else if (proto
== Q_SCTP
)
5016 proto
= IPPROTO_SCTP
;
5017 else if (proto
== Q_DEFAULT
)
5018 proto
= PROTO_UNDEF
;
5020 bpf_error("illegal qualifier of 'port'");
5023 return gen_port((int)v
, proto
, dir
);
5027 b
= gen_port((int)v
, proto
, dir
);
5028 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5035 proto
= IPPROTO_UDP
;
5036 else if (proto
== Q_TCP
)
5037 proto
= IPPROTO_TCP
;
5038 else if (proto
== Q_SCTP
)
5039 proto
= IPPROTO_SCTP
;
5040 else if (proto
== Q_DEFAULT
)
5041 proto
= PROTO_UNDEF
;
5043 bpf_error("illegal qualifier of 'portrange'");
5046 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5050 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5051 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5057 bpf_error("'gateway' requires a name");
5061 return gen_proto((int)v
, proto
, dir
);
5064 return gen_protochain((int)v
, proto
, dir
);
5079 gen_mcode6(s1
, s2
, masklen
, q
)
5080 register const char *s1
, *s2
;
5081 register int masklen
;
5084 struct addrinfo
*res
;
5085 struct in6_addr
*addr
;
5086 struct in6_addr mask
;
5091 bpf_error("no mask %s supported", s2
);
5093 res
= pcap_nametoaddrinfo(s1
);
5095 bpf_error("invalid ip6 address %s", s1
);
5097 bpf_error("%s resolved to multiple address", s1
);
5098 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5100 if (sizeof(mask
) * 8 < masklen
)
5101 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5102 memset(&mask
, 0, sizeof(mask
));
5103 memset(&mask
, 0xff, masklen
/ 8);
5105 mask
.s6_addr
[masklen
/ 8] =
5106 (0xff << (8 - masklen
% 8)) & 0xff;
5109 a
= (u_int32_t
*)addr
;
5110 m
= (u_int32_t
*)&mask
;
5111 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5112 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5113 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5121 bpf_error("Mask syntax for networks only");
5125 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
5130 bpf_error("invalid qualifier against IPv6 address");
5138 register const u_char
*eaddr
;
5141 struct block
*b
, *tmp
;
5143 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5144 if (linktype
== DLT_EN10MB
)
5145 return gen_ehostop(eaddr
, (int)q
.dir
);
5146 if (linktype
== DLT_FDDI
)
5147 return gen_fhostop(eaddr
, (int)q
.dir
);
5148 if (linktype
== DLT_IEEE802
)
5149 return gen_thostop(eaddr
, (int)q
.dir
);
5150 if (linktype
== DLT_IEEE802_11
||
5151 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5152 linktype
== DLT_IEEE802_11_RADIO
||
5153 linktype
== DLT_PRISM_HEADER
)
5154 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5155 if (linktype
== DLT_SUNATM
&& is_lane
) {
5157 * Check that the packet doesn't begin with an
5158 * LE Control marker. (We've already generated
5161 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5166 * Now check the MAC address.
5168 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5172 if (linktype
== DLT_IP_OVER_FC
)
5173 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5174 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5176 bpf_error("ethernet address used in non-ether expression");
5182 struct slist
*s0
, *s1
;
5185 * This is definitely not the best way to do this, but the
5186 * lists will rarely get long.
5193 static struct slist
*
5199 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5204 static struct slist
*
5210 s
= new_stmt(BPF_LD
|BPF_MEM
);
5216 * Modify "index" to use the value stored into its register as an
5217 * offset relative to the beginning of the header for the protocol
5218 * "proto", and allocate a register and put an item "size" bytes long
5219 * (1, 2, or 4) at that offset into that register, making it the register
5223 gen_load(proto
, index
, size
)
5228 struct slist
*s
, *tmp
;
5230 int regno
= alloc_reg();
5232 free_reg(index
->regno
);
5236 bpf_error("data size must be 1, 2, or 4");
5252 bpf_error("unsupported index operation");
5256 * The offset is relative to the beginning of the packet
5257 * data, if we have a radio header. (If we don't, this
5260 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5261 linktype
!= DLT_IEEE802_11_RADIO
&&
5262 linktype
!= DLT_PRISM_HEADER
)
5263 bpf_error("radio information not present in capture");
5266 * Load into the X register the offset computed into the
5267 * register specifed by "index".
5269 s
= xfer_to_x(index
);
5272 * Load the item at that offset.
5274 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5276 sappend(index
->s
, s
);
5281 * The offset is relative to the beginning of
5282 * the link-layer header.
5284 * XXX - what about ATM LANE? Should the index be
5285 * relative to the beginning of the AAL5 frame, so
5286 * that 0 refers to the beginning of the LE Control
5287 * field, or relative to the beginning of the LAN
5288 * frame, so that 0 refers, for Ethernet LANE, to
5289 * the beginning of the destination address?
5291 s
= gen_llprefixlen();
5294 * If "s" is non-null, it has code to arrange that the
5295 * X register contains the length of the prefix preceding
5296 * the link-layer header. Add to it the offset computed
5297 * into the register specified by "index", and move that
5298 * into the X register. Otherwise, just load into the X
5299 * register the offset computed into the register specifed
5303 sappend(s
, xfer_to_a(index
));
5304 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5305 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5307 s
= xfer_to_x(index
);
5310 * Load the item at the sum of the offset we've put in the
5311 * X register and the offset of the start of the link
5312 * layer header (which is 0 if the radio header is
5313 * variable-length; that header length is what we put
5314 * into the X register and then added to the index).
5316 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5319 sappend(index
->s
, s
);
5335 * The offset is relative to the beginning of
5336 * the network-layer header.
5337 * XXX - are there any cases where we want
5340 s
= gen_llprefixlen();
5343 * If "s" is non-null, it has code to arrange that the
5344 * X register contains the length of the prefix preceding
5345 * the link-layer header. Add to it the offset computed
5346 * into the register specified by "index", and move that
5347 * into the X register. Otherwise, just load into the X
5348 * register the offset computed into the register specifed
5352 sappend(s
, xfer_to_a(index
));
5353 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5354 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5356 s
= xfer_to_x(index
);
5359 * Load the item at the sum of the offset we've put in the
5360 * X register and the offset of the start of the network
5363 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5366 sappend(index
->s
, s
);
5369 * Do the computation only if the packet contains
5370 * the protocol in question.
5372 b
= gen_proto_abbrev(proto
);
5374 gen_and(index
->b
, b
);
5387 * The offset is relative to the beginning of
5388 * the transport-layer header.
5389 * XXX - are there any cases where we want
5391 * XXX - we should, if we're built with
5392 * IPv6 support, generate code to load either
5393 * IPv4, IPv6, or both, as appropriate.
5395 s
= gen_loadx_iphdrlen();
5398 * The X register now contains the sum of the offset
5399 * of the beginning of the link-layer header and
5400 * the length of the network-layer header. Load
5401 * into the A register the offset relative to
5402 * the beginning of the transport layer header,
5403 * add the X register to that, move that to the
5404 * X register, and load with an offset from the
5405 * X register equal to the offset of the network
5406 * layer header relative to the beginning of
5407 * the link-layer header.
5409 sappend(s
, xfer_to_a(index
));
5410 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5411 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5412 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5414 sappend(index
->s
, s
);
5417 * Do the computation only if the packet contains
5418 * the protocol in question - which is true only
5419 * if this is an IP datagram and is the first or
5420 * only fragment of that datagram.
5422 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5424 gen_and(index
->b
, b
);
5426 gen_and(gen_proto_abbrev(Q_IP
), b
);
5432 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5436 index
->regno
= regno
;
5437 s
= new_stmt(BPF_ST
);
5439 sappend(index
->s
, s
);
5445 gen_relation(code
, a0
, a1
, reversed
)
5447 struct arth
*a0
, *a1
;
5450 struct slist
*s0
, *s1
, *s2
;
5451 struct block
*b
, *tmp
;
5455 if (code
== BPF_JEQ
) {
5456 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5457 b
= new_block(JMP(code
));
5461 b
= new_block(BPF_JMP
|code
|BPF_X
);
5467 sappend(a0
->s
, a1
->s
);
5471 free_reg(a0
->regno
);
5472 free_reg(a1
->regno
);
5474 /* 'and' together protocol checks */
5477 gen_and(a0
->b
, tmp
= a1
->b
);
5493 int regno
= alloc_reg();
5494 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5497 s
= new_stmt(BPF_LD
|BPF_LEN
);
5498 s
->next
= new_stmt(BPF_ST
);
5499 s
->next
->s
.k
= regno
;
5514 a
= (struct arth
*)newchunk(sizeof(*a
));
5518 s
= new_stmt(BPF_LD
|BPF_IMM
);
5520 s
->next
= new_stmt(BPF_ST
);
5536 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5539 s
= new_stmt(BPF_ST
);
5547 gen_arth(code
, a0
, a1
)
5549 struct arth
*a0
, *a1
;
5551 struct slist
*s0
, *s1
, *s2
;
5555 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5560 sappend(a0
->s
, a1
->s
);
5562 free_reg(a0
->regno
);
5563 free_reg(a1
->regno
);
5565 s0
= new_stmt(BPF_ST
);
5566 a0
->regno
= s0
->s
.k
= alloc_reg();
5573 * Here we handle simple allocation of the scratch registers.
5574 * If too many registers are alloc'd, the allocator punts.
5576 static int regused
[BPF_MEMWORDS
];
5580 * Return the next free register.
5585 int n
= BPF_MEMWORDS
;
5588 if (regused
[curreg
])
5589 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5591 regused
[curreg
] = 1;
5595 bpf_error("too many registers needed to evaluate expression");
5600 * Return a register to the table so it can
5610 static struct block
*
5617 s
= new_stmt(BPF_LD
|BPF_LEN
);
5618 b
= new_block(JMP(jmp
));
5629 return gen_len(BPF_JGE
, n
);
5633 * Actually, this is less than or equal.
5641 b
= gen_len(BPF_JGT
, n
);
5648 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5649 * the beginning of the link-layer header.
5650 * XXX - that means you can't test values in the radiotap header, but
5651 * as that header is difficult if not impossible to parse generally
5652 * without a loop, that might not be a severe problem. A new keyword
5653 * "radio" could be added for that, although what you'd really want
5654 * would be a way of testing particular radio header values, which
5655 * would generate code appropriate to the radio header in question.
5658 gen_byteop(op
, idx
, val
)
5669 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5672 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5676 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5680 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5684 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5688 b
= new_block(JMP(BPF_JEQ
));
5695 static u_char abroadcast
[] = { 0x0 };
5698 gen_broadcast(proto
)
5701 bpf_u_int32 hostmask
;
5702 struct block
*b0
, *b1
, *b2
;
5703 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5709 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5710 return gen_ahostop(abroadcast
, Q_DST
);
5711 if (linktype
== DLT_EN10MB
)
5712 return gen_ehostop(ebroadcast
, Q_DST
);
5713 if (linktype
== DLT_FDDI
)
5714 return gen_fhostop(ebroadcast
, Q_DST
);
5715 if (linktype
== DLT_IEEE802
)
5716 return gen_thostop(ebroadcast
, Q_DST
);
5717 if (linktype
== DLT_IEEE802_11
||
5718 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5719 linktype
== DLT_IEEE802_11_RADIO
||
5720 linktype
== DLT_PRISM_HEADER
)
5721 return gen_wlanhostop(ebroadcast
, Q_DST
);
5722 if (linktype
== DLT_IP_OVER_FC
)
5723 return gen_ipfchostop(ebroadcast
, Q_DST
);
5724 if (linktype
== DLT_SUNATM
&& is_lane
) {
5726 * Check that the packet doesn't begin with an
5727 * LE Control marker. (We've already generated
5730 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5735 * Now check the MAC address.
5737 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5741 bpf_error("not a broadcast link");
5745 b0
= gen_linktype(ETHERTYPE_IP
);
5746 hostmask
= ~netmask
;
5747 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5748 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5749 (bpf_int32
)(~0 & hostmask
), hostmask
);
5754 bpf_error("only link-layer/IP broadcast filters supported");
5759 * Generate code to test the low-order bit of a MAC address (that's
5760 * the bottom bit of the *first* byte).
5762 static struct block
*
5763 gen_mac_multicast(offset
)
5766 register struct block
*b0
;
5767 register struct slist
*s
;
5769 /* link[offset] & 1 != 0 */
5770 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5771 b0
= new_block(JMP(BPF_JSET
));
5778 gen_multicast(proto
)
5781 register struct block
*b0
, *b1
, *b2
;
5782 register struct slist
*s
;
5788 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5789 /* all ARCnet multicasts use the same address */
5790 return gen_ahostop(abroadcast
, Q_DST
);
5792 if (linktype
== DLT_EN10MB
) {
5793 /* ether[0] & 1 != 0 */
5794 return gen_mac_multicast(0);
5797 if (linktype
== DLT_FDDI
) {
5799 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5801 * XXX - was that referring to bit-order issues?
5803 /* fddi[1] & 1 != 0 */
5804 return gen_mac_multicast(1);
5807 if (linktype
== DLT_IEEE802
) {
5808 /* tr[2] & 1 != 0 */
5809 return gen_mac_multicast(2);
5812 if (linktype
== DLT_IEEE802_11
||
5813 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5814 linktype
== DLT_IEEE802_11_RADIO
||
5815 linktype
== DLT_PRISM_HEADER
) {
5819 * For control frames, there is no DA.
5821 * For management frames, DA is at an
5822 * offset of 4 from the beginning of
5825 * For data frames, DA is at an offset
5826 * of 4 from the beginning of the packet
5827 * if To DS is clear and at an offset of
5828 * 16 from the beginning of the packet
5833 * Generate the tests to be done for data frames.
5835 * First, check for To DS set, i.e. "link[1] & 0x01".
5837 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5838 b1
= new_block(JMP(BPF_JSET
));
5839 b1
->s
.k
= 0x01; /* To DS */
5843 * If To DS is set, the DA is at 16.
5845 b0
= gen_mac_multicast(16);
5849 * Now, check for To DS not set, i.e. check
5850 * "!(link[1] & 0x01)".
5852 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5853 b2
= new_block(JMP(BPF_JSET
));
5854 b2
->s
.k
= 0x01; /* To DS */
5859 * If To DS is not set, the DA is at 4.
5861 b1
= gen_mac_multicast(4);
5865 * Now OR together the last two checks. That gives
5866 * the complete set of checks for data frames.
5871 * Now check for a data frame.
5872 * I.e, check "link[0] & 0x08".
5874 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5875 b1
= new_block(JMP(BPF_JSET
));
5880 * AND that with the checks done for data frames.
5885 * If the high-order bit of the type value is 0, this
5886 * is a management frame.
5887 * I.e, check "!(link[0] & 0x08)".
5889 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5890 b2
= new_block(JMP(BPF_JSET
));
5896 * For management frames, the DA is at 4.
5898 b1
= gen_mac_multicast(4);
5902 * OR that with the checks done for data frames.
5903 * That gives the checks done for management and
5909 * If the low-order bit of the type value is 1,
5910 * this is either a control frame or a frame
5911 * with a reserved type, and thus not a
5914 * I.e., check "!(link[0] & 0x04)".
5916 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5917 b1
= new_block(JMP(BPF_JSET
));
5923 * AND that with the checks for data and management
5930 if (linktype
== DLT_IP_OVER_FC
) {
5931 b0
= gen_mac_multicast(2);
5935 if (linktype
== DLT_SUNATM
&& is_lane
) {
5937 * Check that the packet doesn't begin with an
5938 * LE Control marker. (We've already generated
5941 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5945 /* ether[off_mac] & 1 != 0 */
5946 b0
= gen_mac_multicast(off_mac
);
5951 /* Link not known to support multicasts */
5955 b0
= gen_linktype(ETHERTYPE_IP
);
5956 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5962 b0
= gen_linktype(ETHERTYPE_IPV6
);
5963 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5968 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5973 * generate command for inbound/outbound. It's here so we can
5974 * make it link-type specific. 'dir' = 0 implies "inbound",
5975 * = 1 implies "outbound".
5981 register struct block
*b0
;
5984 * Only some data link types support inbound/outbound qualifiers.
5988 b0
= gen_relation(BPF_JEQ
,
5989 gen_load(Q_LINK
, gen_loadi(0), 1),
5997 * Match packets sent by this machine.
5999 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6002 * Match packets sent to this machine.
6003 * (No broadcast or multicast packets, or
6004 * packets sent to some other machine and
6005 * received promiscuously.)
6007 * XXX - packets sent to other machines probably
6008 * shouldn't be matched, but what about broadcast
6009 * or multicast packets we received?
6011 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6016 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6017 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6022 /* match outgoing packets */
6023 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6025 /* match incoming packets */
6026 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6030 case DLT_JUNIPER_MLFR
:
6031 case DLT_JUNIPER_MLPPP
:
6032 case DLT_JUNIPER_ATM1
:
6033 case DLT_JUNIPER_ATM2
:
6034 case DLT_JUNIPER_PPPOE
:
6035 case DLT_JUNIPER_PPPOE_ATM
:
6036 case DLT_JUNIPER_GGSN
:
6037 case DLT_JUNIPER_ES
:
6038 case DLT_JUNIPER_MONITOR
:
6039 case DLT_JUNIPER_SERVICES
:
6040 /* juniper flags (including direction) are stored
6041 * the byte after the 3-byte magic number */
6043 /* match outgoing packets */
6044 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6046 /* match incoming packets */
6047 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6052 bpf_error("inbound/outbound not supported on linktype %d",
6060 /* PF firewall log matched interface */
6062 gen_pf_ifname(const char *ifname
)
6067 if (linktype
== DLT_PFLOG
) {
6068 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6069 off
= offsetof(struct pfloghdr
, ifname
);
6071 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6074 if (strlen(ifname
) >= len
) {
6075 bpf_error("ifname interface names can only be %d characters",
6079 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6083 /* PF firewall log matched interface */
6085 gen_pf_ruleset(char *ruleset
)
6089 if (linktype
!= DLT_PFLOG
) {
6090 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6093 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6094 bpf_error("ruleset names can only be %ld characters",
6095 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6098 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6099 strlen(ruleset
), (const u_char
*)ruleset
);
6103 /* PF firewall log rule number */
6109 if (linktype
== DLT_PFLOG
) {
6110 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6113 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6120 /* PF firewall log sub-rule number */
6122 gen_pf_srnr(int srnr
)
6126 if (linktype
!= DLT_PFLOG
) {
6127 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6131 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6136 /* PF firewall log reason code */
6138 gen_pf_reason(int reason
)
6142 if (linktype
== DLT_PFLOG
) {
6143 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6146 bpf_error("reason not supported on linktype 0x%x", linktype
);
6153 /* PF firewall log action */
6155 gen_pf_action(int action
)
6159 if (linktype
== DLT_PFLOG
) {
6160 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6163 bpf_error("action not supported on linktype 0x%x", linktype
);
6172 register const u_char
*eaddr
;
6175 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6176 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6177 return gen_ahostop(eaddr
, (int)q
.dir
);
6179 bpf_error("ARCnet address used in non-arc expression");
6183 static struct block
*
6184 gen_ahostop(eaddr
, dir
)
6185 register const u_char
*eaddr
;
6188 register struct block
*b0
, *b1
;
6191 /* src comes first, different from Ethernet */
6193 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6196 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6199 b0
= gen_ahostop(eaddr
, Q_SRC
);
6200 b1
= gen_ahostop(eaddr
, Q_DST
);
6206 b0
= gen_ahostop(eaddr
, Q_SRC
);
6207 b1
= gen_ahostop(eaddr
, Q_DST
);
6216 * support IEEE 802.1Q VLAN trunk over ethernet
6222 struct block
*b0
,*b1
;
6225 * Change the offsets to point to the type and data fields within
6226 * the VLAN packet. Just increment the offsets, so that we
6227 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6228 * capture VLAN 200 encapsulated within VLAN 100.
6230 * XXX - this is a bit of a kludge. If we were to split the
6231 * compiler into a parser that parses an expression and
6232 * generates an expression tree, and a code generator that
6233 * takes an expression tree (which could come from our
6234 * parser or from some other parser) and generates BPF code,
6235 * we could perhaps make the offsets parameters of routines
6236 * and, in the handler for an "AND" node, pass to subnodes
6237 * other than the VLAN node the adjusted offsets.
6239 * This would mean that "vlan" would, instead of changing the
6240 * behavior of *all* tests after it, change only the behavior
6241 * of tests ANDed with it. That would change the documented
6242 * semantics of "vlan", which might break some expressions.
6243 * However, it would mean that "(vlan and ip) or ip" would check
6244 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6245 * checking only for VLAN-encapsulated IP, so that could still
6246 * be considered worth doing; it wouldn't break expressions
6247 * that are of the form "vlan and ..." or "vlan N and ...",
6248 * which I suspect are the most common expressions involving
6249 * "vlan". "vlan or ..." doesn't necessarily do what the user
6250 * would really want, now, as all the "or ..." tests would
6251 * be done assuming a VLAN, even though the "or" could be viewed
6252 * as meaning "or, if this isn't a VLAN packet...".
6254 orig_linktype
= off_linktype
; /* save original values */
6266 bpf_error("no VLAN support for data link type %d",
6271 /* check for VLAN */
6272 if (orig_linktype
!= (u_int
)-1)
6273 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6275 bpf_error("no VLAN match after MPLS");
6277 /* If a specific VLAN is requested, check VLAN id */
6278 if (vlan_num
>= 0) {
6279 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6295 struct block
*b0
,*b1
;
6298 * Change the offsets to point to the type and data fields within
6299 * the MPLS packet. Just increment the offsets, so that we
6300 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6301 * capture packets with an outer label of 100000 and an inner
6304 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6306 orig_linktype
= off_linktype
; /* save original values */
6309 if (label_stack_depth
> 0) {
6310 /* just match the bottom-of-stack bit clear */
6311 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6314 /* poison the linktype to make sure higher level
6315 * code generators don't try to match against IP related protocols like
6316 * Q_ARP, Q_RARP etc. */
6320 case DLT_C_HDLC
: /* fall through */
6322 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6323 (bpf_int32
)ETHERTYPE_MPLS
);
6327 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6328 (bpf_int32
)PPP_MPLS_UCAST
);
6331 /* FIXME add other DLT_s ...
6332 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6333 * leave it for now */
6336 bpf_error("no MPLS support for data link type %d",
6344 /* If a specific MPLS label is requested, check it */
6345 if (label_num
>= 0) {
6346 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6347 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6348 0xfffff000); /* only compare the first 20 bits */
6355 label_stack_depth
++;
6360 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6372 bpf_error("'vpi' supported only on raw ATM");
6373 if (off_vpi
== (u_int
)-1)
6375 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6381 bpf_error("'vci' supported only on raw ATM");
6382 if (off_vci
== (u_int
)-1)
6384 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6389 if (off_proto
== (u_int
)-1)
6390 abort(); /* XXX - this isn't on FreeBSD */
6391 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6396 if (off_payload
== (u_int
)-1)
6398 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6399 0xffffffff, jtype
, reverse
, jvalue
);
6404 bpf_error("'callref' supported only on raw ATM");
6405 if (off_proto
== (u_int
)-1)
6407 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6408 jtype
, reverse
, jvalue
);
6418 gen_atmtype_abbrev(type
)
6421 struct block
*b0
, *b1
;
6426 /* Get all packets in Meta signalling Circuit */
6428 bpf_error("'metac' supported only on raw ATM");
6429 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6430 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6435 /* Get all packets in Broadcast Circuit*/
6437 bpf_error("'bcc' supported only on raw ATM");
6438 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6439 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6444 /* Get all cells in Segment OAM F4 circuit*/
6446 bpf_error("'oam4sc' supported only on raw ATM");
6447 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6448 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6453 /* Get all cells in End-to-End OAM F4 Circuit*/
6455 bpf_error("'oam4ec' supported only on raw ATM");
6456 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6457 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6462 /* Get all packets in connection Signalling Circuit */
6464 bpf_error("'sc' supported only on raw ATM");
6465 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6466 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6471 /* Get all packets in ILMI Circuit */
6473 bpf_error("'ilmic' supported only on raw ATM");
6474 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6475 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6480 /* Get all LANE packets */
6482 bpf_error("'lane' supported only on raw ATM");
6483 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6486 * Arrange that all subsequent tests assume LANE
6487 * rather than LLC-encapsulated packets, and set
6488 * the offsets appropriately for LANE-encapsulated
6491 * "off_mac" is the offset of the Ethernet header,
6492 * which is 2 bytes past the ATM pseudo-header
6493 * (skipping the pseudo-header and 2-byte LE Client
6494 * field). The other offsets are Ethernet offsets
6495 * relative to "off_mac".
6498 off_mac
= off_payload
+ 2; /* MAC header */
6499 off_linktype
= off_mac
+ 12;
6500 off_nl
= off_mac
+ 14; /* Ethernet II */
6501 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6505 /* Get all LLC-encapsulated packets */
6507 bpf_error("'llc' supported only on raw ATM");
6508 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6519 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6526 bpf_u_int32 val1
, val2
, val3
;
6528 switch (mtp3field
) {
6531 if (off_sio
== (u_int
)-1)
6532 bpf_error("'sio' supported only on SS7");
6533 /* sio coded on 1 byte so max value 255 */
6535 bpf_error("sio value %u too big; max value = 255",
6537 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6538 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6542 if (off_opc
== (u_int
)-1)
6543 bpf_error("'opc' supported only on SS7");
6544 /* opc coded on 14 bits so max value 16383 */
6546 bpf_error("opc value %u too big; max value = 16383",
6548 /* the following instructions are made to convert jvalue
6549 * to the form used to write opc in an ss7 message*/
6550 val1
= jvalue
& 0x00003c00;
6552 val2
= jvalue
& 0x000003fc;
6554 val3
= jvalue
& 0x00000003;
6556 jvalue
= val1
+ val2
+ val3
;
6557 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6558 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6562 if (off_dpc
== (u_int
)-1)
6563 bpf_error("'dpc' supported only on SS7");
6564 /* dpc coded on 14 bits so max value 16383 */
6566 bpf_error("dpc value %u too big; max value = 16383",
6568 /* the following instructions are made to convert jvalue
6569 * to the forme used to write dpc in an ss7 message*/
6570 val1
= jvalue
& 0x000000ff;
6572 val2
= jvalue
& 0x00003f00;
6574 jvalue
= val1
+ val2
;
6575 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6576 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6580 if (off_sls
== (u_int
)-1)
6581 bpf_error("'sls' supported only on SS7");
6582 /* sls coded on 4 bits so max value 15 */
6584 bpf_error("sls value %u too big; max value = 15",
6586 /* the following instruction is made to convert jvalue
6587 * to the forme used to write sls in an ss7 message*/
6588 jvalue
= jvalue
<< 4;
6589 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6590 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6599 static struct block
*
6600 gen_msg_abbrev(type
)
6606 * Q.2931 signalling protocol messages for handling virtual circuits
6607 * establishment and teardown
6612 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6616 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6620 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6624 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6628 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6631 case A_RELEASE_DONE
:
6632 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6642 gen_atmmulti_abbrev(type
)
6645 struct block
*b0
, *b1
;
6651 bpf_error("'oam' supported only on raw ATM");
6652 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6657 bpf_error("'oamf4' supported only on raw ATM");
6659 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6660 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6662 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6668 * Get Q.2931 signalling messages for switched
6669 * virtual connection
6672 bpf_error("'connectmsg' supported only on raw ATM");
6673 b0
= gen_msg_abbrev(A_SETUP
);
6674 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6676 b0
= gen_msg_abbrev(A_CONNECT
);
6678 b0
= gen_msg_abbrev(A_CONNECTACK
);
6680 b0
= gen_msg_abbrev(A_RELEASE
);
6682 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6684 b0
= gen_atmtype_abbrev(A_SC
);
6690 bpf_error("'metaconnect' supported only on raw ATM");
6691 b0
= gen_msg_abbrev(A_SETUP
);
6692 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6694 b0
= gen_msg_abbrev(A_CONNECT
);
6696 b0
= gen_msg_abbrev(A_RELEASE
);
6698 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6700 b0
= gen_atmtype_abbrev(A_METAC
);