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.22 2005-06-06 14:14:01 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;
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_host(bpf_u_int32
, bpf_u_int32
, int, int);
210 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
213 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
215 static struct block
*gen_ipfrag(void);
216 static struct block
*gen_portatom(int, bpf_int32
);
217 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
219 static struct block
*gen_portatom6(int, bpf_int32
);
220 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
222 struct block
*gen_portop(int, int, int);
223 static struct block
*gen_port(int, int, int);
224 struct block
*gen_portrangeop(int, int, int, int);
225 static struct block
*gen_portrange(int, int, int, int);
227 struct block
*gen_portop6(int, int, int);
228 static struct block
*gen_port6(int, int, int);
229 struct block
*gen_portrangeop6(int, int, int, int);
230 static struct block
*gen_portrange6(int, int, int, int);
232 static int lookup_proto(const char *, int);
233 static struct block
*gen_protochain(int, int, int);
234 static struct block
*gen_proto(int, int, int);
235 static struct slist
*xfer_to_x(struct arth
*);
236 static struct slist
*xfer_to_a(struct arth
*);
237 static struct block
*gen_mac_multicast(int);
238 static struct block
*gen_len(int, int);
240 static struct block
*gen_msg_abbrev(int type
);
251 /* XXX Round up to nearest long. */
252 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
254 /* XXX Round up to structure boundary. */
258 cp
= &chunks
[cur_chunk
];
259 if (n
> cp
->n_left
) {
260 ++cp
, k
= ++cur_chunk
;
262 bpf_error("out of memory");
263 size
= CHUNK0SIZE
<< k
;
264 cp
->m
= (void *)malloc(size
);
266 bpf_error("out of memory");
267 memset((char *)cp
->m
, 0, size
);
270 bpf_error("out of memory");
273 return (void *)((char *)cp
->m
+ cp
->n_left
);
282 for (i
= 0; i
< NCHUNKS
; ++i
)
283 if (chunks
[i
].m
!= NULL
) {
290 * A strdup whose allocations are freed after code generation is over.
294 register const char *s
;
296 int n
= strlen(s
) + 1;
297 char *cp
= newchunk(n
);
303 static inline struct block
*
309 p
= (struct block
*)newchunk(sizeof(*p
));
316 static inline struct slist
*
322 p
= (struct slist
*)newchunk(sizeof(*p
));
328 static struct block
*
332 struct block
*b
= new_block(BPF_RET
|BPF_K
);
341 bpf_error("syntax error in filter expression");
344 static bpf_u_int32 netmask
;
349 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
350 char *buf
, int optimize
, bpf_u_int32 mask
)
359 if (setjmp(top_ctx
)) {
367 snaplen
= pcap_snapshot(p
);
369 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
370 "snaplen of 0 rejects all packets");
374 lex_init(buf
? buf
: "");
382 root
= gen_retblk(snaplen
);
384 if (optimize
&& !no_optimize
) {
387 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
388 bpf_error("expression rejects all packets");
390 program
->bf_insns
= icode_to_fcode(root
, &len
);
391 program
->bf_len
= len
;
399 * entry point for using the compiler with no pcap open
400 * pass in all the stuff that is needed explicitly instead.
403 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
404 struct bpf_program
*program
,
405 char *buf
, int optimize
, bpf_u_int32 mask
)
410 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
413 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
419 * Clean up a "struct bpf_program" by freeing all the memory allocated
423 pcap_freecode(struct bpf_program
*program
)
426 if (program
->bf_insns
!= NULL
) {
427 free((char *)program
->bf_insns
);
428 program
->bf_insns
= NULL
;
433 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
434 * which of the jt and jf fields has been resolved and which is a pointer
435 * back to another unresolved block (or nil). At least one of the fields
436 * in each block is already resolved.
439 backpatch(list
, target
)
440 struct block
*list
, *target
;
457 * Merge the lists in b0 and b1, using the 'sense' field to indicate
458 * which of jt and jf is the link.
462 struct block
*b0
, *b1
;
464 register struct block
**p
= &b0
;
466 /* Find end of list. */
468 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
470 /* Concatenate the lists. */
478 backpatch(p
, gen_retblk(snaplen
));
479 p
->sense
= !p
->sense
;
480 backpatch(p
, gen_retblk(0));
484 * Insert before the statements of the first (root) block any
485 * statements needed to load the lengths of any variable-length
486 * headers into registers.
488 * XXX - a fancier strategy would be to insert those before the
489 * statements of all blocks that use those lengths and that
490 * have no predecessors that use them, so that we only compute
491 * the lengths if we need them. There might be even better
492 * approaches than that. However, as we're currently only
493 * handling variable-length radiotap headers, and as all
494 * filtering expressions other than raw link[M:N] tests
495 * require the length of that header, doing more for that
496 * header length isn't really worth the effort.
498 insert_load_llprefixlen(root
);
503 struct block
*b0
, *b1
;
505 backpatch(b0
, b1
->head
);
506 b0
->sense
= !b0
->sense
;
507 b1
->sense
= !b1
->sense
;
509 b1
->sense
= !b1
->sense
;
515 struct block
*b0
, *b1
;
517 b0
->sense
= !b0
->sense
;
518 backpatch(b0
, b1
->head
);
519 b0
->sense
= !b0
->sense
;
528 b
->sense
= !b
->sense
;
531 static struct block
*
532 gen_cmp(offrel
, offset
, size
, v
)
533 enum e_offrel offrel
;
537 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
540 static struct block
*
541 gen_cmp_gt(offrel
, offset
, size
, v
)
542 enum e_offrel offrel
;
546 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
549 static struct block
*
550 gen_cmp_ge(offrel
, offset
, size
, v
)
551 enum e_offrel offrel
;
555 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
558 static struct block
*
559 gen_cmp_lt(offrel
, offset
, size
, v
)
560 enum e_offrel offrel
;
564 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
567 static struct block
*
568 gen_cmp_le(offrel
, offset
, size
, v
)
569 enum e_offrel offrel
;
573 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
576 static struct block
*
577 gen_mcmp(offrel
, offset
, size
, v
, mask
)
578 enum e_offrel offrel
;
583 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
586 static struct block
*
587 gen_bcmp(offrel
, offset
, size
, v
)
588 enum e_offrel offrel
;
589 register u_int offset
, size
;
590 register const u_char
*v
;
592 register struct block
*b
, *tmp
;
596 register const u_char
*p
= &v
[size
- 4];
597 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
598 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
600 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
607 register const u_char
*p
= &v
[size
- 2];
608 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
610 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
617 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
626 * AND the field of size "size" at offset "offset" relative to the header
627 * specified by "offrel" with "mask", and compare it with the value "v"
628 * with the test specified by "jtype"; if "reverse" is true, the test
629 * should test the opposite of "jtype".
631 static struct block
*
632 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
633 enum e_offrel offrel
;
635 bpf_u_int32 offset
, size
, mask
, jtype
;
638 struct slist
*s
, *s2
;
641 s
= gen_load_a(offrel
, offset
, size
);
643 if (mask
!= 0xffffffff) {
644 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
649 b
= new_block(JMP(jtype
));
652 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
658 * Various code constructs need to know the layout of the data link
659 * layer. These variables give the necessary offsets from the beginning
660 * of the packet data.
662 * If the link layer has variable_length headers, the offsets are offsets
663 * from the end of the link-link-layer header, and "reg_ll_size" is
664 * the register number for a register containing the length of the
665 * link-layer header. Otherwise, "reg_ll_size" is -1.
667 static int reg_ll_size
;
670 * This is the offset of the beginning of the link-layer header.
671 * It's usually 0, except for 802.11 with a fixed-length radio header.
676 * This is the offset of the beginning of the MAC-layer header.
677 * It's usually 0, except for ATM LANE.
679 static u_int off_mac
;
682 * "off_linktype" is the offset to information in the link-layer header
683 * giving the packet type.
685 * For Ethernet, it's the offset of the Ethernet type field.
687 * For link-layer types that always use 802.2 headers, it's the
688 * offset of the LLC header.
690 * For PPP, it's the offset of the PPP type field.
692 * For Cisco HDLC, it's the offset of the CHDLC type field.
694 * For BSD loopback, it's the offset of the AF_ value.
696 * For Linux cooked sockets, it's the offset of the type field.
698 * It's set to -1 for no encapsulation, in which case, IP is assumed.
700 static u_int off_linktype
;
703 * TRUE if the link layer includes an ATM pseudo-header.
705 static int is_atm
= 0;
708 * TRUE if "lane" appeared in the filter; it causes us to generate
709 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
711 static int is_lane
= 0;
714 * These are offsets for the ATM pseudo-header.
716 static u_int off_vpi
;
717 static u_int off_vci
;
718 static u_int off_proto
;
721 * This is the offset of the first byte after the ATM pseudo_header,
722 * or -1 if there is no ATM pseudo-header.
724 static u_int off_payload
;
727 * These are offsets to the beginning of the network-layer header.
729 * If the link layer never uses 802.2 LLC:
731 * "off_nl" and "off_nl_nosnap" are the same.
733 * If the link layer always uses 802.2 LLC:
735 * "off_nl" is the offset if there's a SNAP header following
738 * "off_nl_nosnap" is the offset if there's no SNAP header.
740 * If the link layer is Ethernet:
742 * "off_nl" is the offset if the packet is an Ethernet II packet
743 * (we assume no 802.3+802.2+SNAP);
745 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
746 * with an 802.2 header following it.
749 static u_int off_nl_nosnap
;
757 linktype
= pcap_datalink(p
);
759 pcap_fddipad
= p
->fddipad
;
763 * Assume it's not raw ATM with a pseudo-header, for now.
774 * Also assume it's not 802.11 with a fixed-length radio header.
787 off_nl
= 6; /* XXX in reality, variable! */
788 off_nl_nosnap
= 6; /* no 802.2 LLC */
791 case DLT_ARCNET_LINUX
:
793 off_nl
= 8; /* XXX in reality, variable! */
794 off_nl_nosnap
= 8; /* no 802.2 LLC */
799 off_nl
= 14; /* Ethernet II */
800 off_nl_nosnap
= 17; /* 802.3+802.2 */
805 * SLIP doesn't have a link level type. The 16 byte
806 * header is hacked into our SLIP driver.
810 off_nl_nosnap
= 16; /* no 802.2 LLC */
814 /* XXX this may be the same as the DLT_PPP_BSDOS case */
818 off_nl_nosnap
= 24; /* no 802.2 LLC */
825 off_nl_nosnap
= 4; /* no 802.2 LLC */
831 off_nl_nosnap
= 12; /* no 802.2 LLC */
836 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
837 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
840 off_nl_nosnap
= 4; /* no 802.2 LLC */
845 * This does no include the Ethernet header, and
846 * only covers session state.
850 off_nl_nosnap
= 8; /* no 802.2 LLC */
856 off_nl_nosnap
= 24; /* no 802.2 LLC */
861 * FDDI doesn't really have a link-level type field.
862 * We set "off_linktype" to the offset of the LLC header.
864 * To check for Ethernet types, we assume that SSAP = SNAP
865 * is being used and pick out the encapsulated Ethernet type.
866 * XXX - should we generate code to check for SNAP?
870 off_linktype
+= pcap_fddipad
;
872 off_nl
= 21; /* FDDI+802.2+SNAP */
873 off_nl_nosnap
= 16; /* FDDI+802.2 */
875 off_nl
+= pcap_fddipad
;
876 off_nl_nosnap
+= pcap_fddipad
;
882 * Token Ring doesn't really have a link-level type field.
883 * We set "off_linktype" to the offset of the LLC header.
885 * To check for Ethernet types, we assume that SSAP = SNAP
886 * is being used and pick out the encapsulated Ethernet type.
887 * XXX - should we generate code to check for SNAP?
889 * XXX - the header is actually variable-length.
890 * Some various Linux patched versions gave 38
891 * as "off_linktype" and 40 as "off_nl"; however,
892 * if a token ring packet has *no* routing
893 * information, i.e. is not source-routed, the correct
894 * values are 20 and 22, as they are in the vanilla code.
896 * A packet is source-routed iff the uppermost bit
897 * of the first byte of the source address, at an
898 * offset of 8, has the uppermost bit set. If the
899 * packet is source-routed, the total number of bytes
900 * of routing information is 2 plus bits 0x1F00 of
901 * the 16-bit value at an offset of 14 (shifted right
902 * 8 - figure out which byte that is).
905 off_nl
= 22; /* Token Ring+802.2+SNAP */
906 off_nl_nosnap
= 17; /* Token Ring+802.2 */
911 * 802.11 doesn't really have a link-level type field.
912 * We set "off_linktype" to the offset of the LLC header.
914 * To check for Ethernet types, we assume that SSAP = SNAP
915 * is being used and pick out the encapsulated Ethernet type.
916 * XXX - should we generate code to check for SNAP?
918 * XXX - the header is actually variable-length. We
919 * assume a 24-byte link-layer header, as appears in
920 * data frames in networks with no bridges. If the
921 * fromds and tods 802.11 header bits are both set,
922 * it's actually supposed to be 30 bytes.
925 off_nl
= 32; /* 802.11+802.2+SNAP */
926 off_nl_nosnap
= 27; /* 802.11+802.2 */
929 case DLT_PRISM_HEADER
:
931 * Same as 802.11, but with an additional header before
932 * the 802.11 header, containing a bunch of additional
933 * information including radio-level information.
935 * The header is 144 bytes long.
937 * XXX - same variable-length header problem; at least
938 * the Prism header is fixed-length.
941 off_linktype
= 144+24;
942 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
943 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
946 case DLT_IEEE802_11_RADIO_AVS
:
948 * Same as 802.11, but with an additional header before
949 * the 802.11 header, containing a bunch of additional
950 * information including radio-level information.
952 * The header is 64 bytes long, at least in its
953 * current incarnation.
955 * XXX - same variable-length header problem, only
956 * more so; this header is also variable-length,
957 * with the length being the 32-bit big-endian
958 * number at an offset of 4 from the beginning
959 * of the radio header.
962 off_linktype
= 64+24;
963 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
964 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
967 case DLT_IEEE802_11_RADIO
:
969 * Same as 802.11, but with an additional header before
970 * the 802.11 header, containing a bunch of additional
971 * information including radio-level information.
973 * The radiotap header is variable length, and we
974 * generate code to compute its length and store it
975 * in a register. These offsets are relative to the
976 * beginning of the 802.11 header.
979 off_nl
= 32; /* 802.11+802.2+SNAP */
980 off_nl_nosnap
= 27; /* 802.11+802.2 */
983 case DLT_ATM_RFC1483
:
984 case DLT_ATM_CLIP
: /* Linux ATM defines this */
986 * assume routed, non-ISO PDUs
987 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
989 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
990 * or PPP with the PPP NLPID (e.g., PPPoA)? The
991 * latter would presumably be treated the way PPPoE
992 * should be, so you can do "pppoe and udp port 2049"
993 * or "pppoa and tcp port 80" and have it check for
994 * PPPo{A,E} and a PPP protocol of IP and....
997 off_nl
= 8; /* 802.2+SNAP */
998 off_nl_nosnap
= 3; /* 802.2 */
1003 * Full Frontal ATM; you get AALn PDUs with an ATM
1007 off_vpi
= SUNATM_VPI_POS
;
1008 off_vci
= SUNATM_VCI_POS
;
1009 off_proto
= PROTO_POS
;
1010 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1011 off_payload
= SUNATM_PKT_BEGIN_POS
;
1012 off_linktype
= off_payload
;
1013 off_nl
= off_payload
+8; /* 802.2+SNAP */
1014 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1020 off_nl_nosnap
= 0; /* no 802.2 LLC */
1023 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1026 off_nl_nosnap
= 16; /* no 802.2 LLC */
1031 * LocalTalk does have a 1-byte type field in the LLAP header,
1032 * but really it just indicates whether there is a "short" or
1033 * "long" DDP packet following.
1037 off_nl_nosnap
= 0; /* no 802.2 LLC */
1040 case DLT_IP_OVER_FC
:
1042 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1043 * link-level type field. We set "off_linktype" to the
1044 * offset of the LLC header.
1046 * To check for Ethernet types, we assume that SSAP = SNAP
1047 * is being used and pick out the encapsulated Ethernet type.
1048 * XXX - should we generate code to check for SNAP? RFC
1049 * 2625 says SNAP should be used.
1052 off_nl
= 24; /* IPFC+802.2+SNAP */
1053 off_nl_nosnap
= 19; /* IPFC+802.2 */
1058 * XXX - we should set this to handle SNAP-encapsulated
1059 * frames (NLPID of 0x80).
1063 off_nl_nosnap
= 0; /* no 802.2 LLC */
1066 case DLT_APPLE_IP_OVER_IEEE1394
:
1069 off_nl_nosnap
= 18; /* no 802.2 LLC */
1072 case DLT_LINUX_IRDA
:
1074 * Currently, only raw "link[N:M]" filtering is supported.
1083 * Currently, only raw "link[N:M]" filtering is supported.
1090 case DLT_SYMANTEC_FIREWALL
:
1092 off_nl
= 44; /* Ethernet II */
1093 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1098 /* XXX read this from pf.h? */
1099 off_nl
= PFLOG_HDRLEN
;
1100 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1103 case DLT_JUNIPER_MLFR
:
1104 case DLT_JUNIPER_MLPPP
:
1107 off_nl_nosnap
= -1; /* no 802.2 LLC */
1110 case DLT_JUNIPER_ATM1
:
1111 off_linktype
= 4; /* in reality variable between 4-8 */
1116 case DLT_JUNIPER_ATM2
:
1117 off_linktype
= 8; /* in reality variable between 8-12 */
1122 /* frames captured on a Juniper PPPoE service PIC
1123 * contain raw ethernet frames */
1124 case DLT_JUNIPER_PPPOE
:
1126 off_nl
= 18; /* Ethernet II */
1127 off_nl_nosnap
= 21; /* 802.3+802.2 */
1130 case DLT_JUNIPER_PPPOE_ATM
:
1133 off_nl_nosnap
= -1; /* no 802.2 LLC */
1136 case DLT_JUNIPER_GGSN
:
1139 off_nl_nosnap
= -1; /* no 802.2 LLC */
1142 case DLT_JUNIPER_ES
:
1144 off_nl
= -1; /* not really a network layer but raw IP adresses */
1145 off_nl_nosnap
= -1; /* no 802.2 LLC */
1148 case DLT_JUNIPER_MONITOR
:
1150 off_nl
= 12; /* raw IP/IP6 header */
1151 off_nl_nosnap
= -1; /* no 802.2 LLC */
1154 case DLT_JUNIPER_SERVICES
:
1156 off_nl
= -1; /* L3 proto location dep. on cookie type */
1157 off_nl_nosnap
= -1; /* no 802.2 LLC */
1168 case DLT_LINUX_LAPD
:
1170 * Currently, only raw "link[N:M]" filtering is supported.
1177 bpf_error("unknown data link type %d", linktype
);
1182 * Load a value relative to the beginning of the link-layer header.
1183 * The link-layer header doesn't necessarily begin at the beginning
1184 * of the packet data; there might be a variable-length prefix containing
1185 * radio information.
1187 static struct slist
*
1188 gen_load_llrel(offset
, size
)
1191 struct slist
*s
, *s2
;
1193 s
= gen_llprefixlen();
1196 * If "s" is non-null, it has code to arrange that the X register
1197 * contains the length of the prefix preceding the link-layer
1201 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1205 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1212 * Load a value relative to the beginning of the specified header.
1214 static struct slist
*
1215 gen_load_a(offrel
, offset
, size
)
1216 enum e_offrel offrel
;
1219 struct slist
*s
, *s2
;
1224 s
= gen_load_llrel(offset
, size
);
1228 s
= gen_load_llrel(off_ll
+ offset
, size
);
1232 s
= gen_load_llrel(off_nl
+ offset
, size
);
1236 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1241 * Load the X register with the length of the IPv4 header,
1244 s
= gen_loadx_iphdrlen();
1247 * Load the item at {length of the link-layer header} +
1248 * {length of the IPv4 header} + {specified offset}.
1250 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1251 s2
->s
.k
= off_nl
+ offset
;
1256 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1267 * Generate code to load into the X register the sum of the length of
1268 * the IPv4 header and any variable-length header preceding the link-layer
1271 static struct slist
*
1272 gen_loadx_iphdrlen()
1274 struct slist
*s
, *s2
;
1276 s
= gen_llprefixlen();
1279 * There's a variable-length prefix preceding the
1280 * link-layer header. "s" points to a list of statements
1281 * that put the length of that prefix into the X register.
1282 * The 4*([k]&0xf) addressing mode can't be used, as we
1283 * don't have a constant offset, so we have to load the
1284 * value in question into the A register and add to it
1285 * the value from the X register.
1287 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1290 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1293 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1298 * The A register now contains the length of the
1299 * IP header. We need to add to it the length
1300 * of the prefix preceding the link-layer
1301 * header, which is still in the X register, and
1302 * move the result into the X register.
1304 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1305 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1308 * There is no variable-length header preceding the
1309 * link-layer header; if there's a fixed-length
1310 * header preceding it, its length is included in
1311 * the off_ variables, so it doesn't need to be added.
1313 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1319 static struct block
*
1326 s
= new_stmt(BPF_LD
|BPF_IMM
);
1328 b
= new_block(JMP(BPF_JEQ
));
1334 static inline struct block
*
1337 return gen_uncond(1);
1340 static inline struct block
*
1343 return gen_uncond(0);
1347 * Byte-swap a 32-bit number.
1348 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1349 * big-endian platforms.)
1351 #define SWAPLONG(y) \
1352 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1355 * Generate code to match a particular packet type.
1357 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1358 * value, if <= ETHERMTU. We use that to determine whether to
1359 * match the type/length field or to check the type/length field for
1360 * a value <= ETHERMTU to see whether it's a type field and then do
1361 * the appropriate test.
1363 static struct block
*
1364 gen_ether_linktype(proto
)
1367 struct block
*b0
, *b1
;
1373 case LLCSAP_NETBEUI
:
1375 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1376 * so we check the DSAP and SSAP.
1378 * LLCSAP_IP checks for IP-over-802.2, rather
1379 * than IP-over-Ethernet or IP-over-SNAP.
1381 * XXX - should we check both the DSAP and the
1382 * SSAP, like this, or should we check just the
1383 * DSAP, as we do for other types <= ETHERMTU
1384 * (i.e., other SAP values)?
1386 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1388 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1389 ((proto
<< 8) | proto
));
1397 * Ethernet_II frames, which are Ethernet
1398 * frames with a frame type of ETHERTYPE_IPX;
1400 * Ethernet_802.3 frames, which are 802.3
1401 * frames (i.e., the type/length field is
1402 * a length field, <= ETHERMTU, rather than
1403 * a type field) with the first two bytes
1404 * after the Ethernet/802.3 header being
1407 * Ethernet_802.2 frames, which are 802.3
1408 * frames with an 802.2 LLC header and
1409 * with the IPX LSAP as the DSAP in the LLC
1412 * Ethernet_SNAP frames, which are 802.3
1413 * frames with an LLC header and a SNAP
1414 * header and with an OUI of 0x000000
1415 * (encapsulated Ethernet) and a protocol
1416 * ID of ETHERTYPE_IPX in the SNAP header.
1418 * XXX - should we generate the same code both
1419 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1423 * This generates code to check both for the
1424 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1426 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1427 (bpf_int32
)LLCSAP_IPX
);
1428 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1433 * Now we add code to check for SNAP frames with
1434 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1436 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1440 * Now we generate code to check for 802.3
1441 * frames in general.
1443 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1447 * Now add the check for 802.3 frames before the
1448 * check for Ethernet_802.2 and Ethernet_802.3,
1449 * as those checks should only be done on 802.3
1450 * frames, not on Ethernet frames.
1455 * Now add the check for Ethernet_II frames, and
1456 * do that before checking for the other frame
1459 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1460 (bpf_int32
)ETHERTYPE_IPX
);
1464 case ETHERTYPE_ATALK
:
1465 case ETHERTYPE_AARP
:
1467 * EtherTalk (AppleTalk protocols on Ethernet link
1468 * layer) may use 802.2 encapsulation.
1472 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1473 * we check for an Ethernet type field less than
1474 * 1500, which means it's an 802.3 length field.
1476 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1480 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1481 * SNAP packets with an organization code of
1482 * 0x080007 (Apple, for Appletalk) and a protocol
1483 * type of ETHERTYPE_ATALK (Appletalk).
1485 * 802.2-encapsulated ETHERTYPE_AARP packets are
1486 * SNAP packets with an organization code of
1487 * 0x000000 (encapsulated Ethernet) and a protocol
1488 * type of ETHERTYPE_AARP (Appletalk ARP).
1490 if (proto
== ETHERTYPE_ATALK
)
1491 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1492 else /* proto == ETHERTYPE_AARP */
1493 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1497 * Check for Ethernet encapsulation (Ethertalk
1498 * phase 1?); we just check for the Ethernet
1501 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1507 if (proto
<= ETHERMTU
) {
1509 * This is an LLC SAP value, so the frames
1510 * that match would be 802.2 frames.
1511 * Check that the frame is an 802.2 frame
1512 * (i.e., that the length/type field is
1513 * a length field, <= ETHERMTU) and
1514 * then check the DSAP.
1516 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1518 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1524 * This is an Ethernet type, so compare
1525 * the length/type field with it (if
1526 * the frame is an 802.2 frame, the length
1527 * field will be <= ETHERMTU, and, as
1528 * "proto" is > ETHERMTU, this test
1529 * will fail and the frame won't match,
1530 * which is what we want).
1532 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1539 * Generate code to match a particular packet type.
1541 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1542 * value, if <= ETHERMTU. We use that to determine whether to
1543 * match the type field or to check the type field for the special
1544 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1546 static struct block
*
1547 gen_linux_sll_linktype(proto
)
1550 struct block
*b0
, *b1
;
1556 case LLCSAP_NETBEUI
:
1558 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1559 * so we check the DSAP and SSAP.
1561 * LLCSAP_IP checks for IP-over-802.2, rather
1562 * than IP-over-Ethernet or IP-over-SNAP.
1564 * XXX - should we check both the DSAP and the
1565 * SSAP, like this, or should we check just the
1566 * DSAP, as we do for other types <= ETHERMTU
1567 * (i.e., other SAP values)?
1569 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1570 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1571 ((proto
<< 8) | proto
));
1577 * Ethernet_II frames, which are Ethernet
1578 * frames with a frame type of ETHERTYPE_IPX;
1580 * Ethernet_802.3 frames, which have a frame
1581 * type of LINUX_SLL_P_802_3;
1583 * Ethernet_802.2 frames, which are 802.3
1584 * frames with an 802.2 LLC header (i.e, have
1585 * a frame type of LINUX_SLL_P_802_2) and
1586 * with the IPX LSAP as the DSAP in the LLC
1589 * Ethernet_SNAP frames, which are 802.3
1590 * frames with an LLC header and a SNAP
1591 * header and with an OUI of 0x000000
1592 * (encapsulated Ethernet) and a protocol
1593 * ID of ETHERTYPE_IPX in the SNAP header.
1595 * First, do the checks on LINUX_SLL_P_802_2
1596 * frames; generate the check for either
1597 * Ethernet_802.2 or Ethernet_SNAP frames, and
1598 * then put a check for LINUX_SLL_P_802_2 frames
1601 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1602 (bpf_int32
)LLCSAP_IPX
);
1603 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1606 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1610 * Now check for 802.3 frames and OR that with
1611 * the previous test.
1613 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1617 * Now add the check for Ethernet_II frames, and
1618 * do that before checking for the other frame
1621 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1622 (bpf_int32
)ETHERTYPE_IPX
);
1626 case ETHERTYPE_ATALK
:
1627 case ETHERTYPE_AARP
:
1629 * EtherTalk (AppleTalk protocols on Ethernet link
1630 * layer) may use 802.2 encapsulation.
1634 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1635 * we check for the 802.2 protocol type in the
1636 * "Ethernet type" field.
1638 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1641 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1642 * SNAP packets with an organization code of
1643 * 0x080007 (Apple, for Appletalk) and a protocol
1644 * type of ETHERTYPE_ATALK (Appletalk).
1646 * 802.2-encapsulated ETHERTYPE_AARP packets are
1647 * SNAP packets with an organization code of
1648 * 0x000000 (encapsulated Ethernet) and a protocol
1649 * type of ETHERTYPE_AARP (Appletalk ARP).
1651 if (proto
== ETHERTYPE_ATALK
)
1652 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1654 else /* proto == ETHERTYPE_AARP */
1655 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1660 * Check for Ethernet encapsulation (Ethertalk
1661 * phase 1?); we just check for the Ethernet
1664 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1670 if (proto
<= ETHERMTU
) {
1672 * This is an LLC SAP value, so the frames
1673 * that match would be 802.2 frames.
1674 * Check for the 802.2 protocol type
1675 * in the "Ethernet type" field, and
1676 * then check the DSAP.
1678 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1680 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1686 * This is an Ethernet type, so compare
1687 * the length/type field with it (if
1688 * the frame is an 802.2 frame, the length
1689 * field will be <= ETHERMTU, and, as
1690 * "proto" is > ETHERMTU, this test
1691 * will fail and the frame won't match,
1692 * which is what we want).
1694 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1701 insert_radiotap_load_llprefixlen(b
)
1704 struct slist
*s1
, *s2
;
1707 * Prepend to the statements in this block code to load the
1708 * length of the radiotap header into the register assigned
1709 * to hold that length, if one has been assigned.
1711 if (reg_ll_size
!= -1) {
1713 * The 2 bytes at offsets of 2 and 3 from the beginning
1714 * of the radiotap header are the length of the radiotap
1715 * header; unfortunately, it's little-endian, so we have
1716 * to load it a byte at a time and construct the value.
1720 * Load the high-order byte, at an offset of 3, shift it
1721 * left a byte, and put the result in the X register.
1723 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1725 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1728 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1732 * Load the next byte, at an offset of 2, and OR the
1733 * value from the X register into it.
1735 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1738 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1742 * Now allocate a register to hold that value and store
1745 s2
= new_stmt(BPF_ST
);
1746 s2
->s
.k
= reg_ll_size
;
1750 * Now move it into the X register.
1752 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1756 * Now append all the existing statements in this
1757 * block to these statements.
1759 sappend(s1
, b
->stmts
);
1766 insert_load_llprefixlen(b
)
1771 case DLT_IEEE802_11_RADIO
:
1772 insert_radiotap_load_llprefixlen(b
);
1777 static struct slist
*
1778 gen_radiotap_llprefixlen(void)
1782 if (reg_ll_size
== -1) {
1784 * We haven't yet assigned a register for the length
1785 * of the radiotap header; allocate one.
1787 reg_ll_size
= alloc_reg();
1791 * Load the register containing the radiotap length
1792 * into the X register.
1794 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1795 s
->s
.k
= reg_ll_size
;
1800 * Generate code to compute the link-layer header length, if necessary,
1801 * putting it into the X register, and to return either a pointer to a
1802 * "struct slist" for the list of statements in that code, or NULL if
1803 * no code is necessary.
1805 static struct slist
*
1806 gen_llprefixlen(void)
1810 case DLT_IEEE802_11_RADIO
:
1811 return gen_radiotap_llprefixlen();
1819 * Generate code to match a particular packet type by matching the
1820 * link-layer type field or fields in the 802.2 LLC header.
1822 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1823 * value, if <= ETHERMTU.
1825 static struct block
*
1829 struct block
*b0
, *b1
, *b2
;
1834 return gen_ether_linktype(proto
);
1842 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1846 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1855 case DLT_IEEE802_11
:
1856 case DLT_IEEE802_11_RADIO_AVS
:
1857 case DLT_IEEE802_11_RADIO
:
1858 case DLT_PRISM_HEADER
:
1859 case DLT_ATM_RFC1483
:
1861 case DLT_IP_OVER_FC
:
1862 return gen_llc_linktype(proto
);
1868 * If "is_lane" is set, check for a LANE-encapsulated
1869 * version of this protocol, otherwise check for an
1870 * LLC-encapsulated version of this protocol.
1872 * We assume LANE means Ethernet, not Token Ring.
1876 * Check that the packet doesn't begin with an
1877 * LE Control marker. (We've already generated
1880 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1885 * Now generate an Ethernet test.
1887 b1
= gen_ether_linktype(proto
);
1892 * Check for LLC encapsulation and then check the
1895 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1896 b1
= gen_llc_linktype(proto
);
1904 return gen_linux_sll_linktype(proto
);
1909 case DLT_SLIP_BSDOS
:
1912 * These types don't provide any type field; packets
1915 * XXX - for IPv4, check for a version number of 4, and,
1916 * for IPv6, check for a version number of 6?
1922 case ETHERTYPE_IPV6
:
1924 return gen_true(); /* always true */
1927 return gen_false(); /* always false */
1934 case DLT_PPP_SERIAL
:
1937 * We use Ethernet protocol types inside libpcap;
1938 * map them to the corresponding PPP protocol types.
1947 case ETHERTYPE_IPV6
:
1956 case ETHERTYPE_ATALK
:
1970 * I'm assuming the "Bridging PDU"s that go
1971 * over PPP are Spanning Tree Protocol
1985 * We use Ethernet protocol types inside libpcap;
1986 * map them to the corresponding PPP protocol types.
1991 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
1992 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
1994 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
1999 case ETHERTYPE_IPV6
:
2009 case ETHERTYPE_ATALK
:
2023 * I'm assuming the "Bridging PDU"s that go
2024 * over PPP are Spanning Tree Protocol
2040 * For DLT_NULL, the link-layer header is a 32-bit
2041 * word containing an AF_ value in *host* byte order,
2042 * and for DLT_ENC, the link-layer header begins
2043 * with a 32-bit work containing an AF_ value in
2046 * In addition, if we're reading a saved capture file,
2047 * the host byte order in the capture may not be the
2048 * same as the host byte order on this machine.
2050 * For DLT_LOOP, the link-layer header is a 32-bit
2051 * word containing an AF_ value in *network* byte order.
2053 * XXX - AF_ values may, unfortunately, be platform-
2054 * dependent; for example, FreeBSD's AF_INET6 is 24
2055 * whilst NetBSD's and OpenBSD's is 26.
2057 * This means that, when reading a capture file, just
2058 * checking for our AF_INET6 value won't work if the
2059 * capture file came from another OS.
2068 case ETHERTYPE_IPV6
:
2075 * Not a type on which we support filtering.
2076 * XXX - support those that have AF_ values
2077 * #defined on this platform, at least?
2082 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2084 * The AF_ value is in host byte order, but
2085 * the BPF interpreter will convert it to
2086 * network byte order.
2088 * If this is a save file, and it's from a
2089 * machine with the opposite byte order to
2090 * ours, we byte-swap the AF_ value.
2092 * Then we run it through "htonl()", and
2093 * generate code to compare against the result.
2095 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2096 bpf_pcap
->sf
.swapped
)
2097 proto
= SWAPLONG(proto
);
2098 proto
= htonl(proto
);
2100 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2104 * af field is host byte order in contrast to the rest of
2107 if (proto
== ETHERTYPE_IP
)
2108 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2109 BPF_B
, (bpf_int32
)AF_INET
));
2111 else if (proto
== ETHERTYPE_IPV6
)
2112 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2113 BPF_B
, (bpf_int32
)AF_INET6
));
2121 case DLT_ARCNET_LINUX
:
2123 * XXX should we check for first fragment if the protocol
2132 case ETHERTYPE_IPV6
:
2133 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2134 (bpf_int32
)ARCTYPE_INET6
));
2138 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2139 (bpf_int32
)ARCTYPE_IP
);
2140 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2141 (bpf_int32
)ARCTYPE_IP_OLD
);
2146 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2147 (bpf_int32
)ARCTYPE_ARP
);
2148 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2149 (bpf_int32
)ARCTYPE_ARP_OLD
);
2153 case ETHERTYPE_REVARP
:
2154 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2155 (bpf_int32
)ARCTYPE_REVARP
));
2157 case ETHERTYPE_ATALK
:
2158 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2159 (bpf_int32
)ARCTYPE_ATALK
));
2166 case ETHERTYPE_ATALK
:
2176 * XXX - assumes a 2-byte Frame Relay header with
2177 * DLCI and flags. What if the address is longer?
2183 * Check for the special NLPID for IP.
2185 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2188 case ETHERTYPE_IPV6
:
2190 * Check for the special NLPID for IPv6.
2192 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2197 * Check for several OSI protocols.
2199 * Frame Relay packets typically have an OSI
2200 * NLPID at the beginning; we check for each
2203 * What we check for is the NLPID and a frame
2204 * control field of UI, i.e. 0x03 followed
2207 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2208 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2209 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2220 case DLT_JUNIPER_MLFR
:
2221 case DLT_JUNIPER_MLPPP
:
2222 case DLT_JUNIPER_ATM1
:
2223 case DLT_JUNIPER_ATM2
:
2224 case DLT_JUNIPER_PPPOE
:
2225 case DLT_JUNIPER_PPPOE_ATM
:
2226 case DLT_JUNIPER_GGSN
:
2227 case DLT_JUNIPER_ES
:
2228 case DLT_JUNIPER_MONITOR
:
2229 case DLT_JUNIPER_SERVICES
:
2230 /* just lets verify the magic number for now -
2231 * on ATM we may have up to 6 different encapsulations on the wire
2232 * and need a lot of heuristics to figure out that the payload
2235 * FIXME encapsulation specific BPF_ filters
2237 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2239 case DLT_LINUX_IRDA
:
2240 bpf_error("IrDA link-layer type filtering not implemented");
2243 bpf_error("DOCSIS link-layer type filtering not implemented");
2245 case DLT_LINUX_LAPD
:
2246 bpf_error("LAPD link-layer type filtering not implemented");
2250 * All the types that have no encapsulation should either be
2251 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2252 * all packets are IP packets, or should be handled in some
2253 * special case, if none of them are (if some are and some
2254 * aren't, the lack of encapsulation is a problem, as we'd
2255 * have to find some other way of determining the packet type).
2257 * Therefore, if "off_linktype" is -1, there's an error.
2259 if (off_linktype
== (u_int
)-1)
2263 * Any type not handled above should always have an Ethernet
2264 * type at an offset of "off_linktype". (PPP is partially
2265 * handled above - the protocol type is mapped from the
2266 * Ethernet and LLC types we use internally to the corresponding
2267 * PPP type - but the PPP type is always specified by a value
2268 * at "off_linktype", so we don't have to do the code generation
2271 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2275 * Check for an LLC SNAP packet with a given organization code and
2276 * protocol type; we check the entire contents of the 802.2 LLC and
2277 * snap headers, checking for DSAP and SSAP of SNAP and a control
2278 * field of 0x03 in the LLC header, and for the specified organization
2279 * code and protocol type in the SNAP header.
2281 static struct block
*
2282 gen_snap(orgcode
, ptype
, offset
)
2283 bpf_u_int32 orgcode
;
2287 u_char snapblock
[8];
2289 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2290 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2291 snapblock
[2] = 0x03; /* control = UI */
2292 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2293 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2294 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2295 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2296 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2297 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2301 * Generate code to match a particular packet type, for link-layer types
2302 * using 802.2 LLC headers.
2304 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2305 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2307 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2308 * value, if <= ETHERMTU. We use that to determine whether to
2309 * match the DSAP or both DSAP and LSAP or to check the OUI and
2310 * protocol ID in a SNAP header.
2312 static struct block
*
2313 gen_llc_linktype(proto
)
2317 * XXX - handle token-ring variable-length header.
2323 case LLCSAP_NETBEUI
:
2325 * XXX - should we check both the DSAP and the
2326 * SSAP, like this, or should we check just the
2327 * DSAP, as we do for other types <= ETHERMTU
2328 * (i.e., other SAP values)?
2330 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2331 ((proto
<< 8) | proto
));
2335 * XXX - are there ever SNAP frames for IPX on
2336 * non-Ethernet 802.x networks?
2338 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2339 (bpf_int32
)LLCSAP_IPX
);
2341 case ETHERTYPE_ATALK
:
2343 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2344 * SNAP packets with an organization code of
2345 * 0x080007 (Apple, for Appletalk) and a protocol
2346 * type of ETHERTYPE_ATALK (Appletalk).
2348 * XXX - check for an organization code of
2349 * encapsulated Ethernet as well?
2351 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2355 * XXX - we don't have to check for IPX 802.3
2356 * here, but should we check for the IPX Ethertype?
2358 if (proto
<= ETHERMTU
) {
2360 * This is an LLC SAP value, so check
2363 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2367 * This is an Ethernet type; we assume that it's
2368 * unlikely that it'll appear in the right place
2369 * at random, and therefore check only the
2370 * location that would hold the Ethernet type
2371 * in a SNAP frame with an organization code of
2372 * 0x000000 (encapsulated Ethernet).
2374 * XXX - if we were to check for the SNAP DSAP and
2375 * LSAP, as per XXX, and were also to check for an
2376 * organization code of 0x000000 (encapsulated
2377 * Ethernet), we'd do
2379 * return gen_snap(0x000000, proto,
2382 * here; for now, we don't, as per the above.
2383 * I don't know whether it's worth the extra CPU
2384 * time to do the right check or not.
2386 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2392 static struct block
*
2393 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2397 u_int src_off
, dst_off
;
2399 struct block
*b0
, *b1
;
2413 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2414 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2420 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2421 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2428 b0
= gen_linktype(proto
);
2429 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2435 static struct block
*
2436 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2437 struct in6_addr
*addr
;
2438 struct in6_addr
*mask
;
2440 u_int src_off
, dst_off
;
2442 struct block
*b0
, *b1
;
2457 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2458 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2464 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2465 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2472 /* this order is important */
2473 a
= (u_int32_t
*)addr
;
2474 m
= (u_int32_t
*)mask
;
2475 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2476 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2478 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2480 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2482 b0
= gen_linktype(proto
);
2488 static struct block
*
2489 gen_ehostop(eaddr
, dir
)
2490 register const u_char
*eaddr
;
2493 register struct block
*b0
, *b1
;
2497 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2500 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2503 b0
= gen_ehostop(eaddr
, Q_SRC
);
2504 b1
= gen_ehostop(eaddr
, Q_DST
);
2510 b0
= gen_ehostop(eaddr
, Q_SRC
);
2511 b1
= gen_ehostop(eaddr
, Q_DST
);
2520 * Like gen_ehostop, but for DLT_FDDI
2522 static struct block
*
2523 gen_fhostop(eaddr
, dir
)
2524 register const u_char
*eaddr
;
2527 struct block
*b0
, *b1
;
2532 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2534 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2539 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2541 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2545 b0
= gen_fhostop(eaddr
, Q_SRC
);
2546 b1
= gen_fhostop(eaddr
, Q_DST
);
2552 b0
= gen_fhostop(eaddr
, Q_SRC
);
2553 b1
= gen_fhostop(eaddr
, Q_DST
);
2562 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2564 static struct block
*
2565 gen_thostop(eaddr
, dir
)
2566 register const u_char
*eaddr
;
2569 register struct block
*b0
, *b1
;
2573 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2576 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2579 b0
= gen_thostop(eaddr
, Q_SRC
);
2580 b1
= gen_thostop(eaddr
, Q_DST
);
2586 b0
= gen_thostop(eaddr
, Q_SRC
);
2587 b1
= gen_thostop(eaddr
, Q_DST
);
2596 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2598 static struct block
*
2599 gen_wlanhostop(eaddr
, dir
)
2600 register const u_char
*eaddr
;
2603 register struct block
*b0
, *b1
, *b2
;
2604 register struct slist
*s
;
2611 * For control frames, there is no SA.
2613 * For management frames, SA is at an
2614 * offset of 10 from the beginning of
2617 * For data frames, SA is at an offset
2618 * of 10 from the beginning of the packet
2619 * if From DS is clear, at an offset of
2620 * 16 from the beginning of the packet
2621 * if From DS is set and To DS is clear,
2622 * and an offset of 24 from the beginning
2623 * of the packet if From DS is set and To DS
2628 * Generate the tests to be done for data frames
2631 * First, check for To DS set, i.e. check "link[1] & 0x01".
2633 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2634 b1
= new_block(JMP(BPF_JSET
));
2635 b1
->s
.k
= 0x01; /* To DS */
2639 * If To DS is set, the SA is at 24.
2641 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2645 * Now, check for To DS not set, i.e. check
2646 * "!(link[1] & 0x01)".
2648 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2649 b2
= new_block(JMP(BPF_JSET
));
2650 b2
->s
.k
= 0x01; /* To DS */
2655 * If To DS is not set, the SA is at 16.
2657 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2661 * Now OR together the last two checks. That gives
2662 * the complete set of checks for data frames with
2668 * Now check for From DS being set, and AND that with
2669 * the ORed-together checks.
2671 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2672 b1
= new_block(JMP(BPF_JSET
));
2673 b1
->s
.k
= 0x02; /* From DS */
2678 * Now check for data frames with From DS not set.
2680 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2681 b2
= new_block(JMP(BPF_JSET
));
2682 b2
->s
.k
= 0x02; /* From DS */
2687 * If From DS isn't set, the SA is at 10.
2689 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2693 * Now OR together the checks for data frames with
2694 * From DS not set and for data frames with From DS
2695 * set; that gives the checks done for data frames.
2700 * Now check for a data frame.
2701 * I.e, check "link[0] & 0x08".
2703 gen_load_a(OR_LINK
, 0, BPF_B
);
2704 b1
= new_block(JMP(BPF_JSET
));
2709 * AND that with the checks done for data frames.
2714 * If the high-order bit of the type value is 0, this
2715 * is a management frame.
2716 * I.e, check "!(link[0] & 0x08)".
2718 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2719 b2
= new_block(JMP(BPF_JSET
));
2725 * For management frames, the SA is at 10.
2727 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2731 * OR that with the checks done for data frames.
2732 * That gives the checks done for management and
2738 * If the low-order bit of the type value is 1,
2739 * this is either a control frame or a frame
2740 * with a reserved type, and thus not a
2743 * I.e., check "!(link[0] & 0x04)".
2745 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2746 b1
= new_block(JMP(BPF_JSET
));
2752 * AND that with the checks for data and management
2762 * For control frames, there is no DA.
2764 * For management frames, DA is at an
2765 * offset of 4 from the beginning of
2768 * For data frames, DA is at an offset
2769 * of 4 from the beginning of the packet
2770 * if To DS is clear and at an offset of
2771 * 16 from the beginning of the packet
2776 * Generate the tests to be done for data frames.
2778 * First, check for To DS set, i.e. "link[1] & 0x01".
2780 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2781 b1
= new_block(JMP(BPF_JSET
));
2782 b1
->s
.k
= 0x01; /* To DS */
2786 * If To DS is set, the DA is at 16.
2788 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2792 * Now, check for To DS not set, i.e. check
2793 * "!(link[1] & 0x01)".
2795 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2796 b2
= new_block(JMP(BPF_JSET
));
2797 b2
->s
.k
= 0x01; /* To DS */
2802 * If To DS is not set, the DA is at 4.
2804 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2808 * Now OR together the last two checks. That gives
2809 * the complete set of checks for data frames.
2814 * Now check for a data frame.
2815 * I.e, check "link[0] & 0x08".
2817 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2818 b1
= new_block(JMP(BPF_JSET
));
2823 * AND that with the checks done for data frames.
2828 * If the high-order bit of the type value is 0, this
2829 * is a management frame.
2830 * I.e, check "!(link[0] & 0x08)".
2832 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2833 b2
= new_block(JMP(BPF_JSET
));
2839 * For management frames, the DA is at 4.
2841 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2845 * OR that with the checks done for data frames.
2846 * That gives the checks done for management and
2852 * If the low-order bit of the type value is 1,
2853 * this is either a control frame or a frame
2854 * with a reserved type, and thus not a
2857 * I.e., check "!(link[0] & 0x04)".
2859 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2860 b1
= new_block(JMP(BPF_JSET
));
2866 * AND that with the checks for data and management
2873 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2874 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2880 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2881 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2890 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2891 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2892 * as the RFC states.)
2894 static struct block
*
2895 gen_ipfchostop(eaddr
, dir
)
2896 register const u_char
*eaddr
;
2899 register struct block
*b0
, *b1
;
2903 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2906 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2909 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2910 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2916 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2917 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2926 * This is quite tricky because there may be pad bytes in front of the
2927 * DECNET header, and then there are two possible data packet formats that
2928 * carry both src and dst addresses, plus 5 packet types in a format that
2929 * carries only the src node, plus 2 types that use a different format and
2930 * also carry just the src node.
2934 * Instead of doing those all right, we just look for data packets with
2935 * 0 or 1 bytes of padding. If you want to look at other packets, that
2936 * will require a lot more hacking.
2938 * To add support for filtering on DECNET "areas" (network numbers)
2939 * one would want to add a "mask" argument to this routine. That would
2940 * make the filter even more inefficient, although one could be clever
2941 * and not generate masking instructions if the mask is 0xFFFF.
2943 static struct block
*
2944 gen_dnhostop(addr
, dir
)
2948 struct block
*b0
, *b1
, *b2
, *tmp
;
2949 u_int offset_lh
; /* offset if long header is received */
2950 u_int offset_sh
; /* offset if short header is received */
2955 offset_sh
= 1; /* follows flags */
2956 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2960 offset_sh
= 3; /* follows flags, dstnode */
2961 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2965 /* Inefficient because we do our Calvinball dance twice */
2966 b0
= gen_dnhostop(addr
, Q_SRC
);
2967 b1
= gen_dnhostop(addr
, Q_DST
);
2973 /* Inefficient because we do our Calvinball dance twice */
2974 b0
= gen_dnhostop(addr
, Q_SRC
);
2975 b1
= gen_dnhostop(addr
, Q_DST
);
2980 bpf_error("ISO host filtering not implemented");
2985 b0
= gen_linktype(ETHERTYPE_DN
);
2986 /* Check for pad = 1, long header case */
2987 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2988 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2989 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
2990 BPF_H
, (bpf_int32
)ntohs(addr
));
2992 /* Check for pad = 0, long header case */
2993 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2994 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2997 /* Check for pad = 1, short header case */
2998 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2999 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3000 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3003 /* Check for pad = 0, short header case */
3004 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3005 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3009 /* Combine with test for linktype */
3014 static struct block
*
3015 gen_host(addr
, mask
, proto
, dir
)
3021 struct block
*b0
, *b1
;
3026 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
3027 if (off_linktype
!= (u_int
)-1) {
3028 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
3030 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
3036 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3039 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3042 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3045 bpf_error("'tcp' modifier applied to host");
3048 bpf_error("'sctp' modifier applied to host");
3051 bpf_error("'udp' modifier applied to host");
3054 bpf_error("'icmp' modifier applied to host");
3057 bpf_error("'igmp' modifier applied to host");
3060 bpf_error("'igrp' modifier applied to host");
3063 bpf_error("'pim' modifier applied to host");
3066 bpf_error("'vrrp' modifier applied to host");
3069 bpf_error("ATALK host filtering not implemented");
3072 bpf_error("AARP host filtering not implemented");
3075 return gen_dnhostop(addr
, dir
);
3078 bpf_error("SCA host filtering not implemented");
3081 bpf_error("LAT host filtering not implemented");
3084 bpf_error("MOPDL host filtering not implemented");
3087 bpf_error("MOPRC host filtering not implemented");
3091 bpf_error("'ip6' modifier applied to ip host");
3094 bpf_error("'icmp6' modifier applied to host");
3098 bpf_error("'ah' modifier applied to host");
3101 bpf_error("'esp' modifier applied to host");
3104 bpf_error("ISO host filtering not implemented");
3107 bpf_error("'esis' modifier applied to host");
3110 bpf_error("'isis' modifier applied to host");
3113 bpf_error("'clnp' modifier applied to host");
3116 bpf_error("'stp' modifier applied to host");
3119 bpf_error("IPX host filtering not implemented");
3122 bpf_error("'netbeui' modifier applied to host");
3125 bpf_error("'radio' modifier applied to host");
3134 static struct block
*
3135 gen_host6(addr
, mask
, proto
, dir
)
3136 struct in6_addr
*addr
;
3137 struct in6_addr
*mask
;
3144 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3147 bpf_error("'ip' modifier applied to ip6 host");
3150 bpf_error("'rarp' modifier applied to ip6 host");
3153 bpf_error("'arp' modifier applied to ip6 host");
3156 bpf_error("'sctp' modifier applied to host");
3159 bpf_error("'tcp' modifier applied to host");
3162 bpf_error("'udp' modifier applied to host");
3165 bpf_error("'icmp' modifier applied to host");
3168 bpf_error("'igmp' modifier applied to host");
3171 bpf_error("'igrp' modifier applied to host");
3174 bpf_error("'pim' modifier applied to host");
3177 bpf_error("'vrrp' modifier applied to host");
3180 bpf_error("ATALK host filtering not implemented");
3183 bpf_error("AARP host filtering not implemented");
3186 bpf_error("'decnet' modifier applied to ip6 host");
3189 bpf_error("SCA host filtering not implemented");
3192 bpf_error("LAT host filtering not implemented");
3195 bpf_error("MOPDL host filtering not implemented");
3198 bpf_error("MOPRC host filtering not implemented");
3201 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3204 bpf_error("'icmp6' modifier applied to host");
3207 bpf_error("'ah' modifier applied to host");
3210 bpf_error("'esp' modifier applied to host");
3213 bpf_error("ISO host filtering not implemented");
3216 bpf_error("'esis' modifier applied to host");
3219 bpf_error("'isis' modifier applied to host");
3222 bpf_error("'clnp' modifier applied to host");
3225 bpf_error("'stp' modifier applied to host");
3228 bpf_error("IPX host filtering not implemented");
3231 bpf_error("'netbeui' modifier applied to host");
3234 bpf_error("'radio' modifier applied to host");
3244 static struct block
*
3245 gen_gateway(eaddr
, alist
, proto
, dir
)
3246 const u_char
*eaddr
;
3247 bpf_u_int32
**alist
;
3251 struct block
*b0
, *b1
, *tmp
;
3254 bpf_error("direction applied to 'gateway'");
3261 if (linktype
== DLT_EN10MB
)
3262 b0
= gen_ehostop(eaddr
, Q_OR
);
3263 else if (linktype
== DLT_FDDI
)
3264 b0
= gen_fhostop(eaddr
, Q_OR
);
3265 else if (linktype
== DLT_IEEE802
)
3266 b0
= gen_thostop(eaddr
, Q_OR
);
3267 else if (linktype
== DLT_IEEE802_11
||
3268 linktype
== DLT_IEEE802_11_RADIO_AVS
||
3269 linktype
== DLT_IEEE802_11_RADIO
||
3270 linktype
== DLT_PRISM_HEADER
)
3271 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3272 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3274 * Check that the packet doesn't begin with an
3275 * LE Control marker. (We've already generated
3278 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3283 * Now check the MAC address.
3285 b0
= gen_ehostop(eaddr
, Q_OR
);
3287 } else if (linktype
== DLT_IP_OVER_FC
)
3288 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3291 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3293 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3295 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3303 bpf_error("illegal modifier of 'gateway'");
3309 gen_proto_abbrev(proto
)
3318 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3320 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3326 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3328 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3334 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3336 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3342 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3345 #ifndef IPPROTO_IGMP
3346 #define IPPROTO_IGMP 2
3350 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3353 #ifndef IPPROTO_IGRP
3354 #define IPPROTO_IGRP 9
3357 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3361 #define IPPROTO_PIM 103
3365 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3367 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3372 #ifndef IPPROTO_VRRP
3373 #define IPPROTO_VRRP 112
3377 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3381 b1
= gen_linktype(ETHERTYPE_IP
);
3385 b1
= gen_linktype(ETHERTYPE_ARP
);
3389 b1
= gen_linktype(ETHERTYPE_REVARP
);
3393 bpf_error("link layer applied in wrong context");
3396 b1
= gen_linktype(ETHERTYPE_ATALK
);
3400 b1
= gen_linktype(ETHERTYPE_AARP
);
3404 b1
= gen_linktype(ETHERTYPE_DN
);
3408 b1
= gen_linktype(ETHERTYPE_SCA
);
3412 b1
= gen_linktype(ETHERTYPE_LAT
);
3416 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3420 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3425 b1
= gen_linktype(ETHERTYPE_IPV6
);
3428 #ifndef IPPROTO_ICMPV6
3429 #define IPPROTO_ICMPV6 58
3432 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3437 #define IPPROTO_AH 51
3440 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3442 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3448 #define IPPROTO_ESP 50
3451 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3453 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3459 b1
= gen_linktype(LLCSAP_ISONS
);
3463 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3467 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3470 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3471 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3472 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3474 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3476 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3478 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3482 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3483 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3484 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3486 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3488 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3490 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3494 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3495 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3496 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3498 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3503 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3504 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3509 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3510 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3512 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3514 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3519 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3520 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3525 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3526 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3531 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3535 b1
= gen_linktype(LLCSAP_8021D
);
3539 b1
= gen_linktype(LLCSAP_IPX
);
3543 b1
= gen_linktype(LLCSAP_NETBEUI
);
3547 bpf_error("'radio' is not a valid protocol type");
3555 static struct block
*
3562 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3563 b
= new_block(JMP(BPF_JSET
));
3572 * Generate a comparison to a port value in the transport-layer header
3573 * at the specified offset from the beginning of that header.
3575 * XXX - this handles a variable-length prefix preceding the link-layer
3576 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3577 * variable-length link-layer headers (such as Token Ring or 802.11
3580 static struct block
*
3581 gen_portatom(off
, v
)
3585 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3589 static struct block
*
3590 gen_portatom6(off
, v
)
3594 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3599 gen_portop(port
, proto
, dir
)
3600 int port
, proto
, dir
;
3602 struct block
*b0
, *b1
, *tmp
;
3604 /* ip proto 'proto' */
3605 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3611 b1
= gen_portatom(0, (bpf_int32
)port
);
3615 b1
= gen_portatom(2, (bpf_int32
)port
);
3620 tmp
= gen_portatom(0, (bpf_int32
)port
);
3621 b1
= gen_portatom(2, (bpf_int32
)port
);
3626 tmp
= gen_portatom(0, (bpf_int32
)port
);
3627 b1
= gen_portatom(2, (bpf_int32
)port
);
3639 static struct block
*
3640 gen_port(port
, ip_proto
, dir
)
3645 struct block
*b0
, *b1
, *tmp
;
3650 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3651 * not LLC encapsulation with LLCSAP_IP.
3653 * For IEEE 802 networks - which includes 802.5 token ring
3654 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3655 * says that SNAP encapsulation is used, not LLC encapsulation
3658 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3659 * RFC 2225 say that SNAP encapsulation is used, not LLC
3660 * encapsulation with LLCSAP_IP.
3662 * So we always check for ETHERTYPE_IP.
3664 b0
= gen_linktype(ETHERTYPE_IP
);
3670 b1
= gen_portop(port
, ip_proto
, dir
);
3674 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3675 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3677 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3690 gen_portop6(port
, proto
, dir
)
3691 int port
, proto
, dir
;
3693 struct block
*b0
, *b1
, *tmp
;
3695 /* ip6 proto 'proto' */
3696 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3700 b1
= gen_portatom6(0, (bpf_int32
)port
);
3704 b1
= gen_portatom6(2, (bpf_int32
)port
);
3709 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3710 b1
= gen_portatom6(2, (bpf_int32
)port
);
3715 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3716 b1
= gen_portatom6(2, (bpf_int32
)port
);
3728 static struct block
*
3729 gen_port6(port
, ip_proto
, dir
)
3734 struct block
*b0
, *b1
, *tmp
;
3736 /* link proto ip6 */
3737 b0
= gen_linktype(ETHERTYPE_IPV6
);
3743 b1
= gen_portop6(port
, ip_proto
, dir
);
3747 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3748 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3750 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3762 /* gen_portrange code */
3763 static struct block
*
3764 gen_portrangeatom(off
, v1
, v2
)
3768 struct block
*b1
, *b2
;
3772 * Reverse the order of the ports, so v1 is the lower one.
3781 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3782 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3790 gen_portrangeop(port1
, port2
, proto
, dir
)
3795 struct block
*b0
, *b1
, *tmp
;
3797 /* ip proto 'proto' */
3798 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3804 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3808 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3813 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3814 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3819 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3820 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3832 static struct block
*
3833 gen_portrange(port1
, port2
, ip_proto
, dir
)
3838 struct block
*b0
, *b1
, *tmp
;
3841 b0
= gen_linktype(ETHERTYPE_IP
);
3847 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3851 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3852 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3854 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3866 static struct block
*
3867 gen_portrangeatom6(off
, v1
, v2
)
3871 struct block
*b1
, *b2
;
3875 * Reverse the order of the ports, so v1 is the lower one.
3884 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3885 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3893 gen_portrangeop6(port1
, port2
, proto
, dir
)
3898 struct block
*b0
, *b1
, *tmp
;
3900 /* ip6 proto 'proto' */
3901 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3905 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3909 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3914 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3915 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3920 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3921 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3933 static struct block
*
3934 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3939 struct block
*b0
, *b1
, *tmp
;
3941 /* link proto ip6 */
3942 b0
= gen_linktype(ETHERTYPE_IPV6
);
3948 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3952 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3953 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3955 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3968 lookup_proto(name
, proto
)
3969 register const char *name
;
3979 v
= pcap_nametoproto(name
);
3980 if (v
== PROTO_UNDEF
)
3981 bpf_error("unknown ip proto '%s'", name
);
3985 /* XXX should look up h/w protocol type based on linktype */
3986 v
= pcap_nametoeproto(name
);
3987 if (v
== PROTO_UNDEF
) {
3988 v
= pcap_nametollc(name
);
3989 if (v
== PROTO_UNDEF
)
3990 bpf_error("unknown ether proto '%s'", name
);
3995 if (strcmp(name
, "esis") == 0)
3997 else if (strcmp(name
, "isis") == 0)
3999 else if (strcmp(name
, "clnp") == 0)
4002 bpf_error("unknown osi proto '%s'", name
);
4022 static struct block
*
4023 gen_protochain(v
, proto
, dir
)
4028 #ifdef NO_PROTOCHAIN
4029 return gen_proto(v
, proto
, dir
);
4031 struct block
*b0
, *b
;
4032 struct slist
*s
[100];
4033 int fix2
, fix3
, fix4
, fix5
;
4034 int ahcheck
, again
, end
;
4036 int reg2
= alloc_reg();
4038 memset(s
, 0, sizeof(s
));
4039 fix2
= fix3
= fix4
= fix5
= 0;
4046 b0
= gen_protochain(v
, Q_IP
, dir
);
4047 b
= gen_protochain(v
, Q_IPV6
, dir
);
4051 bpf_error("bad protocol applied for 'protochain'");
4056 * We don't handle variable-length radiotap here headers yet.
4057 * We might want to add BPF instructions to do the protochain
4058 * work, to simplify that and, on platforms that have a BPF
4059 * interpreter with the new instructions, let the filtering
4060 * be done in the kernel. (We already require a modified BPF
4061 * engine to do the protochain stuff, to support backward
4062 * branches, and backward branch support is unlikely to appear
4063 * in kernel BPF engines.)
4065 if (linktype
== DLT_IEEE802_11_RADIO
)
4066 bpf_error("'protochain' not supported with radiotap headers");
4068 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4071 * s[0] is a dummy entry to protect other BPF insn from damage
4072 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4073 * hard to find interdependency made by jump table fixup.
4076 s
[i
] = new_stmt(0); /*dummy*/
4081 b0
= gen_linktype(ETHERTYPE_IP
);
4084 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4085 s
[i
]->s
.k
= off_nl
+ 9;
4087 /* X = ip->ip_hl << 2 */
4088 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4094 b0
= gen_linktype(ETHERTYPE_IPV6
);
4096 /* A = ip6->ip_nxt */
4097 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4098 s
[i
]->s
.k
= off_nl
+ 6;
4100 /* X = sizeof(struct ip6_hdr) */
4101 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4107 bpf_error("unsupported proto to gen_protochain");
4111 /* again: if (A == v) goto end; else fall through; */
4113 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4115 s
[i
]->s
.jt
= NULL
; /*later*/
4116 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4120 #ifndef IPPROTO_NONE
4121 #define IPPROTO_NONE 59
4123 /* if (A == IPPROTO_NONE) goto end */
4124 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4125 s
[i
]->s
.jt
= NULL
; /*later*/
4126 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4127 s
[i
]->s
.k
= IPPROTO_NONE
;
4128 s
[fix5
]->s
.jf
= s
[i
];
4133 if (proto
== Q_IPV6
) {
4134 int v6start
, v6end
, v6advance
, j
;
4137 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4138 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4139 s
[i
]->s
.jt
= NULL
; /*later*/
4140 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4141 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4142 s
[fix2
]->s
.jf
= s
[i
];
4144 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4145 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4146 s
[i
]->s
.jt
= NULL
; /*later*/
4147 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4148 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4150 /* if (A == IPPROTO_ROUTING) goto v6advance */
4151 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4152 s
[i
]->s
.jt
= NULL
; /*later*/
4153 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4154 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4156 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4157 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4158 s
[i
]->s
.jt
= NULL
; /*later*/
4159 s
[i
]->s
.jf
= NULL
; /*later*/
4160 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4171 * X = X + (P[X + 1] + 1) * 8;
4174 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4176 /* A = P[X + packet head] */
4177 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4181 s
[i
] = new_stmt(BPF_ST
);
4185 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4188 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4192 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4194 /* A = P[X + packet head]; */
4195 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4199 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4203 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4207 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4210 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4214 /* goto again; (must use BPF_JA for backward jump) */
4215 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4216 s
[i
]->s
.k
= again
- i
- 1;
4217 s
[i
- 1]->s
.jf
= s
[i
];
4221 for (j
= v6start
; j
<= v6end
; j
++)
4222 s
[j
]->s
.jt
= s
[v6advance
];
4227 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4229 s
[fix2
]->s
.jf
= s
[i
];
4235 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4236 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4237 s
[i
]->s
.jt
= NULL
; /*later*/
4238 s
[i
]->s
.jf
= NULL
; /*later*/
4239 s
[i
]->s
.k
= IPPROTO_AH
;
4241 s
[fix3
]->s
.jf
= s
[ahcheck
];
4248 * X = X + (P[X + 1] + 2) * 4;
4251 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4253 /* A = P[X + packet head]; */
4254 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4258 s
[i
] = new_stmt(BPF_ST
);
4262 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4265 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4269 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4271 /* A = P[X + packet head] */
4272 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4276 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4280 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4284 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4287 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4291 /* goto again; (must use BPF_JA for backward jump) */
4292 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4293 s
[i
]->s
.k
= again
- i
- 1;
4298 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4300 s
[fix2
]->s
.jt
= s
[end
];
4301 s
[fix4
]->s
.jf
= s
[end
];
4302 s
[fix5
]->s
.jt
= s
[end
];
4309 for (i
= 0; i
< max
- 1; i
++)
4310 s
[i
]->next
= s
[i
+ 1];
4311 s
[max
- 1]->next
= NULL
;
4316 b
= new_block(JMP(BPF_JEQ
));
4317 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4328 * Generate code that checks whether the packet is a packet for protocol
4329 * <proto> and whether the type field in that protocol's header has
4330 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4331 * IP packet and checks the protocol number in the IP header against <v>.
4333 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4334 * against Q_IP and Q_IPV6.
4336 static struct block
*
4337 gen_proto(v
, proto
, dir
)
4342 struct block
*b0
, *b1
;
4344 if (dir
!= Q_DEFAULT
)
4345 bpf_error("direction applied to 'proto'");
4350 b0
= gen_proto(v
, Q_IP
, dir
);
4351 b1
= gen_proto(v
, Q_IPV6
, dir
);
4359 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4360 * not LLC encapsulation with LLCSAP_IP.
4362 * For IEEE 802 networks - which includes 802.5 token ring
4363 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4364 * says that SNAP encapsulation is used, not LLC encapsulation
4367 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4368 * RFC 2225 say that SNAP encapsulation is used, not LLC
4369 * encapsulation with LLCSAP_IP.
4371 * So we always check for ETHERTYPE_IP.
4373 b0
= gen_linktype(ETHERTYPE_IP
);
4375 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4377 b1
= gen_protochain(v
, Q_IP
);
4387 * Frame Relay packets typically have an OSI
4388 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4389 * generates code to check for all the OSI
4390 * NLPIDs, so calling it and then adding a check
4391 * for the particular NLPID for which we're
4392 * looking is bogus, as we can just check for
4395 * What we check for is the NLPID and a frame
4396 * control field value of UI, i.e. 0x03 followed
4399 * XXX - assumes a 2-byte Frame Relay header with
4400 * DLCI and flags. What if the address is longer?
4402 * XXX - what about SNAP-encapsulated frames?
4404 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4410 * Cisco uses an Ethertype lookalike - for OSI,
4413 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4414 /* OSI in C-HDLC is stuffed with a fudge byte */
4415 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4420 b0
= gen_linktype(LLCSAP_ISONS
);
4421 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4427 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4429 * 4 is the offset of the PDU type relative to the IS-IS
4432 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4437 bpf_error("arp does not encapsulate another protocol");
4441 bpf_error("rarp does not encapsulate another protocol");
4445 bpf_error("atalk encapsulation is not specifiable");
4449 bpf_error("decnet encapsulation is not specifiable");
4453 bpf_error("sca does not encapsulate another protocol");
4457 bpf_error("lat does not encapsulate another protocol");
4461 bpf_error("moprc does not encapsulate another protocol");
4465 bpf_error("mopdl does not encapsulate another protocol");
4469 return gen_linktype(v
);
4472 bpf_error("'udp proto' is bogus");
4476 bpf_error("'tcp proto' is bogus");
4480 bpf_error("'sctp proto' is bogus");
4484 bpf_error("'icmp proto' is bogus");
4488 bpf_error("'igmp proto' is bogus");
4492 bpf_error("'igrp proto' is bogus");
4496 bpf_error("'pim proto' is bogus");
4500 bpf_error("'vrrp proto' is bogus");
4505 b0
= gen_linktype(ETHERTYPE_IPV6
);
4507 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4509 b1
= gen_protochain(v
, Q_IPV6
);
4515 bpf_error("'icmp6 proto' is bogus");
4519 bpf_error("'ah proto' is bogus");
4522 bpf_error("'ah proto' is bogus");
4525 bpf_error("'stp proto' is bogus");
4528 bpf_error("'ipx proto' is bogus");
4531 bpf_error("'netbeui proto' is bogus");
4534 bpf_error("'radio proto' is bogus");
4545 register const char *name
;
4548 int proto
= q
.proto
;
4552 bpf_u_int32 mask
, addr
;
4554 bpf_u_int32
**alist
;
4557 struct sockaddr_in
*sin
;
4558 struct sockaddr_in6
*sin6
;
4559 struct addrinfo
*res
, *res0
;
4560 struct in6_addr mask128
;
4562 struct block
*b
, *tmp
;
4563 int port
, real_proto
;
4569 addr
= pcap_nametonetaddr(name
);
4571 bpf_error("unknown network '%s'", name
);
4572 /* Left justify network addr and calculate its network mask */
4574 while (addr
&& (addr
& 0xff000000) == 0) {
4578 return gen_host(addr
, mask
, proto
, dir
);
4582 if (proto
== Q_LINK
) {
4586 eaddr
= pcap_ether_hostton(name
);
4589 "unknown ether host '%s'", name
);
4590 b
= gen_ehostop(eaddr
, dir
);
4595 eaddr
= pcap_ether_hostton(name
);
4598 "unknown FDDI host '%s'", name
);
4599 b
= gen_fhostop(eaddr
, dir
);
4604 eaddr
= pcap_ether_hostton(name
);
4607 "unknown token ring host '%s'", name
);
4608 b
= gen_thostop(eaddr
, dir
);
4612 case DLT_IEEE802_11
:
4613 case DLT_IEEE802_11_RADIO_AVS
:
4614 case DLT_IEEE802_11_RADIO
:
4615 case DLT_PRISM_HEADER
:
4616 eaddr
= pcap_ether_hostton(name
);
4619 "unknown 802.11 host '%s'", name
);
4620 b
= gen_wlanhostop(eaddr
, dir
);
4624 case DLT_IP_OVER_FC
:
4625 eaddr
= pcap_ether_hostton(name
);
4628 "unknown Fibre Channel host '%s'", name
);
4629 b
= gen_ipfchostop(eaddr
, dir
);
4638 * Check that the packet doesn't begin
4639 * with an LE Control marker. (We've
4640 * already generated a test for LANE.)
4642 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4646 eaddr
= pcap_ether_hostton(name
);
4649 "unknown ether host '%s'", name
);
4650 b
= gen_ehostop(eaddr
, dir
);
4656 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4657 } else if (proto
== Q_DECNET
) {
4658 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4660 * I don't think DECNET hosts can be multihomed, so
4661 * there is no need to build up a list of addresses
4663 return (gen_host(dn_addr
, 0, proto
, dir
));
4666 alist
= pcap_nametoaddr(name
);
4667 if (alist
== NULL
|| *alist
== NULL
)
4668 bpf_error("unknown host '%s'", name
);
4670 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4672 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4674 tmp
= gen_host(**alist
++, 0xffffffff,
4681 memset(&mask128
, 0xff, sizeof(mask128
));
4682 res0
= res
= pcap_nametoaddrinfo(name
);
4684 bpf_error("unknown host '%s'", name
);
4686 tproto
= tproto6
= proto
;
4687 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4691 for (res
= res0
; res
; res
= res
->ai_next
) {
4692 switch (res
->ai_family
) {
4694 if (tproto
== Q_IPV6
)
4697 sin
= (struct sockaddr_in
*)
4699 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4700 0xffffffff, tproto
, dir
);
4703 if (tproto6
== Q_IP
)
4706 sin6
= (struct sockaddr_in6
*)
4708 tmp
= gen_host6(&sin6
->sin6_addr
,
4709 &mask128
, tproto6
, dir
);
4720 bpf_error("unknown host '%s'%s", name
,
4721 (proto
== Q_DEFAULT
)
4723 : " for specified address family");
4730 if (proto
!= Q_DEFAULT
&&
4731 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4732 bpf_error("illegal qualifier of 'port'");
4733 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4734 bpf_error("unknown port '%s'", name
);
4735 if (proto
== Q_UDP
) {
4736 if (real_proto
== IPPROTO_TCP
)
4737 bpf_error("port '%s' is tcp", name
);
4738 else if (real_proto
== IPPROTO_SCTP
)
4739 bpf_error("port '%s' is sctp", name
);
4741 /* override PROTO_UNDEF */
4742 real_proto
= IPPROTO_UDP
;
4744 if (proto
== Q_TCP
) {
4745 if (real_proto
== IPPROTO_UDP
)
4746 bpf_error("port '%s' is udp", name
);
4748 else if (real_proto
== IPPROTO_SCTP
)
4749 bpf_error("port '%s' is sctp", name
);
4751 /* override PROTO_UNDEF */
4752 real_proto
= IPPROTO_TCP
;
4754 if (proto
== Q_SCTP
) {
4755 if (real_proto
== IPPROTO_UDP
)
4756 bpf_error("port '%s' is udp", name
);
4758 else if (real_proto
== IPPROTO_TCP
)
4759 bpf_error("port '%s' is tcp", name
);
4761 /* override PROTO_UNDEF */
4762 real_proto
= IPPROTO_SCTP
;
4765 return gen_port(port
, real_proto
, dir
);
4769 b
= gen_port(port
, real_proto
, dir
);
4770 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4776 if (proto
!= Q_DEFAULT
&&
4777 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4778 bpf_error("illegal qualifier of 'portrange'");
4779 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4780 bpf_error("unknown port in range '%s'", name
);
4781 if (proto
== Q_UDP
) {
4782 if (real_proto
== IPPROTO_TCP
)
4783 bpf_error("port in range '%s' is tcp", name
);
4784 else if (real_proto
== IPPROTO_SCTP
)
4785 bpf_error("port in range '%s' is sctp", name
);
4787 /* override PROTO_UNDEF */
4788 real_proto
= IPPROTO_UDP
;
4790 if (proto
== Q_TCP
) {
4791 if (real_proto
== IPPROTO_UDP
)
4792 bpf_error("port in range '%s' is udp", name
);
4793 else if (real_proto
== IPPROTO_SCTP
)
4794 bpf_error("port in range '%s' is sctp", name
);
4796 /* override PROTO_UNDEF */
4797 real_proto
= IPPROTO_TCP
;
4799 if (proto
== Q_SCTP
) {
4800 if (real_proto
== IPPROTO_UDP
)
4801 bpf_error("port in range '%s' is udp", name
);
4802 else if (real_proto
== IPPROTO_TCP
)
4803 bpf_error("port in range '%s' is tcp", name
);
4805 /* override PROTO_UNDEF */
4806 real_proto
= IPPROTO_SCTP
;
4809 return gen_portrange(port1
, port2
, real_proto
, dir
);
4813 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4814 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4821 eaddr
= pcap_ether_hostton(name
);
4823 bpf_error("unknown ether host: %s", name
);
4825 alist
= pcap_nametoaddr(name
);
4826 if (alist
== NULL
|| *alist
== NULL
)
4827 bpf_error("unknown host '%s'", name
);
4828 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4832 bpf_error("'gateway' not supported in this configuration");
4836 real_proto
= lookup_proto(name
, proto
);
4837 if (real_proto
>= 0)
4838 return gen_proto(real_proto
, proto
, dir
);
4840 bpf_error("unknown protocol: %s", name
);
4843 real_proto
= lookup_proto(name
, proto
);
4844 if (real_proto
>= 0)
4845 return gen_protochain(real_proto
, proto
, dir
);
4847 bpf_error("unknown protocol: %s", name
);
4859 gen_mcode(s1
, s2
, masklen
, q
)
4860 register const char *s1
, *s2
;
4861 register int masklen
;
4864 register int nlen
, mlen
;
4867 nlen
= __pcap_atoin(s1
, &n
);
4868 /* Promote short ipaddr */
4872 mlen
= __pcap_atoin(s2
, &m
);
4873 /* Promote short ipaddr */
4876 bpf_error("non-network bits set in \"%s mask %s\"",
4879 /* Convert mask len to mask */
4881 bpf_error("mask length must be <= 32");
4882 m
= 0xffffffff << (32 - masklen
);
4884 bpf_error("non-network bits set in \"%s/%d\"",
4891 return gen_host(n
, m
, q
.proto
, q
.dir
);
4894 bpf_error("Mask syntax for networks only");
4902 register const char *s
;
4907 int proto
= q
.proto
;
4913 else if (q
.proto
== Q_DECNET
)
4914 vlen
= __pcap_atodn(s
, &v
);
4916 vlen
= __pcap_atoin(s
, &v
);
4923 if (proto
== Q_DECNET
)
4924 return gen_host(v
, 0, proto
, dir
);
4925 else if (proto
== Q_LINK
) {
4926 bpf_error("illegal link layer address");
4929 if (s
== NULL
&& q
.addr
== Q_NET
) {
4930 /* Promote short net number */
4931 while (v
&& (v
& 0xff000000) == 0) {
4936 /* Promote short ipaddr */
4940 return gen_host(v
, mask
, proto
, dir
);
4945 proto
= IPPROTO_UDP
;
4946 else if (proto
== Q_TCP
)
4947 proto
= IPPROTO_TCP
;
4948 else if (proto
== Q_SCTP
)
4949 proto
= IPPROTO_SCTP
;
4950 else if (proto
== Q_DEFAULT
)
4951 proto
= PROTO_UNDEF
;
4953 bpf_error("illegal qualifier of 'port'");
4956 return gen_port((int)v
, proto
, dir
);
4960 b
= gen_port((int)v
, proto
, dir
);
4961 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4968 proto
= IPPROTO_UDP
;
4969 else if (proto
== Q_TCP
)
4970 proto
= IPPROTO_TCP
;
4971 else if (proto
== Q_SCTP
)
4972 proto
= IPPROTO_SCTP
;
4973 else if (proto
== Q_DEFAULT
)
4974 proto
= PROTO_UNDEF
;
4976 bpf_error("illegal qualifier of 'portrange'");
4979 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4983 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4984 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4990 bpf_error("'gateway' requires a name");
4994 return gen_proto((int)v
, proto
, dir
);
4997 return gen_protochain((int)v
, proto
, dir
);
5012 gen_mcode6(s1
, s2
, masklen
, q
)
5013 register const char *s1
, *s2
;
5014 register int masklen
;
5017 struct addrinfo
*res
;
5018 struct in6_addr
*addr
;
5019 struct in6_addr mask
;
5024 bpf_error("no mask %s supported", s2
);
5026 res
= pcap_nametoaddrinfo(s1
);
5028 bpf_error("invalid ip6 address %s", s1
);
5030 bpf_error("%s resolved to multiple address", s1
);
5031 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5033 if (sizeof(mask
) * 8 < masklen
)
5034 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5035 memset(&mask
, 0, sizeof(mask
));
5036 memset(&mask
, 0xff, masklen
/ 8);
5038 mask
.s6_addr
[masklen
/ 8] =
5039 (0xff << (8 - masklen
% 8)) & 0xff;
5042 a
= (u_int32_t
*)addr
;
5043 m
= (u_int32_t
*)&mask
;
5044 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5045 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5046 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5054 bpf_error("Mask syntax for networks only");
5058 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
5063 bpf_error("invalid qualifier against IPv6 address");
5071 register const u_char
*eaddr
;
5074 struct block
*b
, *tmp
;
5076 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5077 if (linktype
== DLT_EN10MB
)
5078 return gen_ehostop(eaddr
, (int)q
.dir
);
5079 if (linktype
== DLT_FDDI
)
5080 return gen_fhostop(eaddr
, (int)q
.dir
);
5081 if (linktype
== DLT_IEEE802
)
5082 return gen_thostop(eaddr
, (int)q
.dir
);
5083 if (linktype
== DLT_IEEE802_11
||
5084 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5085 linktype
== DLT_IEEE802_11_RADIO
||
5086 linktype
== DLT_PRISM_HEADER
)
5087 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5088 if (linktype
== DLT_SUNATM
&& is_lane
) {
5090 * Check that the packet doesn't begin with an
5091 * LE Control marker. (We've already generated
5094 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5099 * Now check the MAC address.
5101 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5105 if (linktype
== DLT_IP_OVER_FC
)
5106 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5107 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5109 bpf_error("ethernet address used in non-ether expression");
5115 struct slist
*s0
, *s1
;
5118 * This is definitely not the best way to do this, but the
5119 * lists will rarely get long.
5126 static struct slist
*
5132 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5137 static struct slist
*
5143 s
= new_stmt(BPF_LD
|BPF_MEM
);
5149 * Modify "index" to use the value stored into its register as an
5150 * offset relative to the beginning of the header for the protocol
5151 * "proto", and allocate a register and put an item "size" bytes long
5152 * (1, 2, or 4) at that offset into that register, making it the register
5156 gen_load(proto
, index
, size
)
5161 struct slist
*s
, *tmp
;
5163 int regno
= alloc_reg();
5165 free_reg(index
->regno
);
5169 bpf_error("data size must be 1, 2, or 4");
5185 bpf_error("unsupported index operation");
5189 * The offset is relative to the beginning of the packet
5190 * data, if we have a radio header. (If we don't, this
5193 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5194 linktype
!= DLT_IEEE802_11_RADIO
&&
5195 linktype
!= DLT_PRISM_HEADER
)
5196 bpf_error("radio information not present in capture");
5199 * Load into the X register the offset computed into the
5200 * register specifed by "index".
5202 s
= xfer_to_x(index
);
5205 * Load the item at that offset.
5207 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5209 sappend(index
->s
, s
);
5214 * The offset is relative to the beginning of
5215 * the link-layer header.
5217 * XXX - what about ATM LANE? Should the index be
5218 * relative to the beginning of the AAL5 frame, so
5219 * that 0 refers to the beginning of the LE Control
5220 * field, or relative to the beginning of the LAN
5221 * frame, so that 0 refers, for Ethernet LANE, to
5222 * the beginning of the destination address?
5224 s
= gen_llprefixlen();
5227 * If "s" is non-null, it has code to arrange that the
5228 * X register contains the length of the prefix preceding
5229 * the link-layer header. Add to it the offset computed
5230 * into the register specified by "index", and move that
5231 * into the X register. Otherwise, just load into the X
5232 * register the offset computed into the register specifed
5236 sappend(s
, xfer_to_a(index
));
5237 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5238 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5240 s
= xfer_to_x(index
);
5243 * Load the item at the sum of the offset we've put in the
5244 * X register and the offset of the start of the link
5245 * layer header (which is 0 if the radio header is
5246 * variable-length; that header length is what we put
5247 * into the X register and then added to the index).
5249 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5252 sappend(index
->s
, s
);
5268 * The offset is relative to the beginning of
5269 * the network-layer header.
5270 * XXX - are there any cases where we want
5273 s
= gen_llprefixlen();
5276 * If "s" is non-null, it has code to arrange that the
5277 * X register contains the length of the prefix preceding
5278 * the link-layer header. Add to it the offset computed
5279 * into the register specified by "index", and move that
5280 * into the X register. Otherwise, just load into the X
5281 * register the offset computed into the register specifed
5285 sappend(s
, xfer_to_a(index
));
5286 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5287 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5289 s
= xfer_to_x(index
);
5292 * Load the item at the sum of the offset we've put in the
5293 * X register and the offset of the start of the network
5296 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5299 sappend(index
->s
, s
);
5302 * Do the computation only if the packet contains
5303 * the protocol in question.
5305 b
= gen_proto_abbrev(proto
);
5307 gen_and(index
->b
, b
);
5320 * The offset is relative to the beginning of
5321 * the transport-layer header.
5322 * XXX - are there any cases where we want
5324 * XXX - we should, if we're built with
5325 * IPv6 support, generate code to load either
5326 * IPv4, IPv6, or both, as appropriate.
5328 s
= gen_loadx_iphdrlen();
5331 * The X register now contains the sum of the offset
5332 * of the beginning of the link-layer header and
5333 * the length of the network-layer header. Load
5334 * into the A register the offset relative to
5335 * the beginning of the transport layer header,
5336 * add the X register to that, move that to the
5337 * X register, and load with an offset from the
5338 * X register equal to the offset of the network
5339 * layer header relative to the beginning of
5340 * the link-layer header.
5342 sappend(s
, xfer_to_a(index
));
5343 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5344 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5345 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5347 sappend(index
->s
, s
);
5350 * Do the computation only if the packet contains
5351 * the protocol in question - which is true only
5352 * if this is an IP datagram and is the first or
5353 * only fragment of that datagram.
5355 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5357 gen_and(index
->b
, b
);
5359 gen_and(gen_proto_abbrev(Q_IP
), b
);
5365 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5369 index
->regno
= regno
;
5370 s
= new_stmt(BPF_ST
);
5372 sappend(index
->s
, s
);
5378 gen_relation(code
, a0
, a1
, reversed
)
5380 struct arth
*a0
, *a1
;
5383 struct slist
*s0
, *s1
, *s2
;
5384 struct block
*b
, *tmp
;
5388 if (code
== BPF_JEQ
) {
5389 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5390 b
= new_block(JMP(code
));
5394 b
= new_block(BPF_JMP
|code
|BPF_X
);
5400 sappend(a0
->s
, a1
->s
);
5404 free_reg(a0
->regno
);
5405 free_reg(a1
->regno
);
5407 /* 'and' together protocol checks */
5410 gen_and(a0
->b
, tmp
= a1
->b
);
5426 int regno
= alloc_reg();
5427 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5430 s
= new_stmt(BPF_LD
|BPF_LEN
);
5431 s
->next
= new_stmt(BPF_ST
);
5432 s
->next
->s
.k
= regno
;
5447 a
= (struct arth
*)newchunk(sizeof(*a
));
5451 s
= new_stmt(BPF_LD
|BPF_IMM
);
5453 s
->next
= new_stmt(BPF_ST
);
5469 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5472 s
= new_stmt(BPF_ST
);
5480 gen_arth(code
, a0
, a1
)
5482 struct arth
*a0
, *a1
;
5484 struct slist
*s0
, *s1
, *s2
;
5488 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5493 sappend(a0
->s
, a1
->s
);
5495 free_reg(a0
->regno
);
5496 free_reg(a1
->regno
);
5498 s0
= new_stmt(BPF_ST
);
5499 a0
->regno
= s0
->s
.k
= alloc_reg();
5506 * Here we handle simple allocation of the scratch registers.
5507 * If too many registers are alloc'd, the allocator punts.
5509 static int regused
[BPF_MEMWORDS
];
5513 * Return the next free register.
5518 int n
= BPF_MEMWORDS
;
5521 if (regused
[curreg
])
5522 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5524 regused
[curreg
] = 1;
5528 bpf_error("too many registers needed to evaluate expression");
5533 * Return a register to the table so it can
5543 static struct block
*
5550 s
= new_stmt(BPF_LD
|BPF_LEN
);
5551 b
= new_block(JMP(jmp
));
5562 return gen_len(BPF_JGE
, n
);
5566 * Actually, this is less than or equal.
5574 b
= gen_len(BPF_JGT
, n
);
5581 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5582 * the beginning of the link-layer header.
5583 * XXX - that means you can't test values in the radiotap header, but
5584 * as that header is difficult if not impossible to parse generally
5585 * without a loop, that might not be a severe problem. A new keyword
5586 * "radio" could be added for that, although what you'd really want
5587 * would be a way of testing particular radio header values, which
5588 * would generate code appropriate to the radio header in question.
5591 gen_byteop(op
, idx
, val
)
5602 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5605 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5609 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5613 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5617 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5621 b
= new_block(JMP(BPF_JEQ
));
5628 static u_char abroadcast
[] = { 0x0 };
5631 gen_broadcast(proto
)
5634 bpf_u_int32 hostmask
;
5635 struct block
*b0
, *b1
, *b2
;
5636 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5642 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5643 return gen_ahostop(abroadcast
, Q_DST
);
5644 if (linktype
== DLT_EN10MB
)
5645 return gen_ehostop(ebroadcast
, Q_DST
);
5646 if (linktype
== DLT_FDDI
)
5647 return gen_fhostop(ebroadcast
, Q_DST
);
5648 if (linktype
== DLT_IEEE802
)
5649 return gen_thostop(ebroadcast
, Q_DST
);
5650 if (linktype
== DLT_IEEE802_11
||
5651 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5652 linktype
== DLT_IEEE802_11_RADIO
||
5653 linktype
== DLT_PRISM_HEADER
)
5654 return gen_wlanhostop(ebroadcast
, Q_DST
);
5655 if (linktype
== DLT_IP_OVER_FC
)
5656 return gen_ipfchostop(ebroadcast
, Q_DST
);
5657 if (linktype
== DLT_SUNATM
&& is_lane
) {
5659 * Check that the packet doesn't begin with an
5660 * LE Control marker. (We've already generated
5663 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5668 * Now check the MAC address.
5670 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5674 bpf_error("not a broadcast link");
5678 b0
= gen_linktype(ETHERTYPE_IP
);
5679 hostmask
= ~netmask
;
5680 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5681 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5682 (bpf_int32
)(~0 & hostmask
), hostmask
);
5687 bpf_error("only link-layer/IP broadcast filters supported");
5692 * Generate code to test the low-order bit of a MAC address (that's
5693 * the bottom bit of the *first* byte).
5695 static struct block
*
5696 gen_mac_multicast(offset
)
5699 register struct block
*b0
;
5700 register struct slist
*s
;
5702 /* link[offset] & 1 != 0 */
5703 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5704 b0
= new_block(JMP(BPF_JSET
));
5711 gen_multicast(proto
)
5714 register struct block
*b0
, *b1
, *b2
;
5715 register struct slist
*s
;
5721 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5722 /* all ARCnet multicasts use the same address */
5723 return gen_ahostop(abroadcast
, Q_DST
);
5725 if (linktype
== DLT_EN10MB
) {
5726 /* ether[0] & 1 != 0 */
5727 return gen_mac_multicast(0);
5730 if (linktype
== DLT_FDDI
) {
5732 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5734 * XXX - was that referring to bit-order issues?
5736 /* fddi[1] & 1 != 0 */
5737 return gen_mac_multicast(1);
5740 if (linktype
== DLT_IEEE802
) {
5741 /* tr[2] & 1 != 0 */
5742 return gen_mac_multicast(2);
5745 if (linktype
== DLT_IEEE802_11
||
5746 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5747 linktype
== DLT_IEEE802_11_RADIO
||
5748 linktype
== DLT_PRISM_HEADER
) {
5752 * For control frames, there is no DA.
5754 * For management frames, DA is at an
5755 * offset of 4 from the beginning of
5758 * For data frames, DA is at an offset
5759 * of 4 from the beginning of the packet
5760 * if To DS is clear and at an offset of
5761 * 16 from the beginning of the packet
5766 * Generate the tests to be done for data frames.
5768 * First, check for To DS set, i.e. "link[1] & 0x01".
5770 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5771 b1
= new_block(JMP(BPF_JSET
));
5772 b1
->s
.k
= 0x01; /* To DS */
5776 * If To DS is set, the DA is at 16.
5778 b0
= gen_mac_multicast(16);
5782 * Now, check for To DS not set, i.e. check
5783 * "!(link[1] & 0x01)".
5785 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5786 b2
= new_block(JMP(BPF_JSET
));
5787 b2
->s
.k
= 0x01; /* To DS */
5792 * If To DS is not set, the DA is at 4.
5794 b1
= gen_mac_multicast(4);
5798 * Now OR together the last two checks. That gives
5799 * the complete set of checks for data frames.
5804 * Now check for a data frame.
5805 * I.e, check "link[0] & 0x08".
5807 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5808 b1
= new_block(JMP(BPF_JSET
));
5813 * AND that with the checks done for data frames.
5818 * If the high-order bit of the type value is 0, this
5819 * is a management frame.
5820 * I.e, check "!(link[0] & 0x08)".
5822 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5823 b2
= new_block(JMP(BPF_JSET
));
5829 * For management frames, the DA is at 4.
5831 b1
= gen_mac_multicast(4);
5835 * OR that with the checks done for data frames.
5836 * That gives the checks done for management and
5842 * If the low-order bit of the type value is 1,
5843 * this is either a control frame or a frame
5844 * with a reserved type, and thus not a
5847 * I.e., check "!(link[0] & 0x04)".
5849 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5850 b1
= new_block(JMP(BPF_JSET
));
5856 * AND that with the checks for data and management
5863 if (linktype
== DLT_IP_OVER_FC
) {
5864 b0
= gen_mac_multicast(2);
5868 if (linktype
== DLT_SUNATM
&& is_lane
) {
5870 * Check that the packet doesn't begin with an
5871 * LE Control marker. (We've already generated
5874 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5878 /* ether[off_mac] & 1 != 0 */
5879 b0
= gen_mac_multicast(off_mac
);
5884 /* Link not known to support multicasts */
5888 b0
= gen_linktype(ETHERTYPE_IP
);
5889 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5895 b0
= gen_linktype(ETHERTYPE_IPV6
);
5896 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5901 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5906 * generate command for inbound/outbound. It's here so we can
5907 * make it link-type specific. 'dir' = 0 implies "inbound",
5908 * = 1 implies "outbound".
5914 register struct block
*b0
;
5917 * Only some data link types support inbound/outbound qualifiers.
5921 b0
= gen_relation(BPF_JEQ
,
5922 gen_load(Q_LINK
, gen_loadi(0), 1),
5930 * Match packets sent by this machine.
5932 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
5935 * Match packets sent to this machine.
5936 * (No broadcast or multicast packets, or
5937 * packets sent to some other machine and
5938 * received promiscuously.)
5940 * XXX - packets sent to other machines probably
5941 * shouldn't be matched, but what about broadcast
5942 * or multicast packets we received?
5944 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
5949 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
5950 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5955 /* match outgoing packets */
5956 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
5958 /* match incoming packets */
5959 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
5963 case DLT_JUNIPER_MLFR
:
5964 case DLT_JUNIPER_MLPPP
:
5965 case DLT_JUNIPER_ATM1
:
5966 case DLT_JUNIPER_ATM2
:
5967 case DLT_JUNIPER_PPPOE
:
5968 case DLT_JUNIPER_PPPOE_ATM
:
5969 case DLT_JUNIPER_GGSN
:
5970 case DLT_JUNIPER_ES
:
5971 case DLT_JUNIPER_MONITOR
:
5972 case DLT_JUNIPER_SERVICES
:
5973 /* juniper flags (including direction) are stored
5974 * the byte after the 3-byte magic number */
5976 /* match outgoing packets */
5977 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
5979 /* match incoming packets */
5980 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
5985 bpf_error("inbound/outbound not supported on linktype %d",
5993 /* PF firewall log matched interface */
5995 gen_pf_ifname(const char *ifname
)
6000 if (linktype
== DLT_PFLOG
) {
6001 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6002 off
= offsetof(struct pfloghdr
, ifname
);
6004 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6007 if (strlen(ifname
) >= len
) {
6008 bpf_error("ifname interface names can only be %d characters",
6012 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6016 /* PF firewall log matched interface */
6018 gen_pf_ruleset(char *ruleset
)
6022 if (linktype
!= DLT_PFLOG
) {
6023 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6026 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6027 bpf_error("ruleset names can only be %ld characters",
6028 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6031 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6032 strlen(ruleset
), (const u_char
*)ruleset
);
6036 /* PF firewall log rule number */
6042 if (linktype
== DLT_PFLOG
) {
6043 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6046 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6053 /* PF firewall log sub-rule number */
6055 gen_pf_srnr(int srnr
)
6059 if (linktype
!= DLT_PFLOG
) {
6060 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6064 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6069 /* PF firewall log reason code */
6071 gen_pf_reason(int reason
)
6075 if (linktype
== DLT_PFLOG
) {
6076 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6079 bpf_error("reason not supported on linktype 0x%x", linktype
);
6086 /* PF firewall log action */
6088 gen_pf_action(int action
)
6092 if (linktype
== DLT_PFLOG
) {
6093 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6096 bpf_error("action not supported on linktype 0x%x", linktype
);
6105 register const u_char
*eaddr
;
6108 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6109 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6110 return gen_ahostop(eaddr
, (int)q
.dir
);
6112 bpf_error("ARCnet address used in non-arc expression");
6116 static struct block
*
6117 gen_ahostop(eaddr
, dir
)
6118 register const u_char
*eaddr
;
6121 register struct block
*b0
, *b1
;
6124 /* src comes first, different from Ethernet */
6126 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6129 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6132 b0
= gen_ahostop(eaddr
, Q_SRC
);
6133 b1
= gen_ahostop(eaddr
, Q_DST
);
6139 b0
= gen_ahostop(eaddr
, Q_SRC
);
6140 b1
= gen_ahostop(eaddr
, Q_DST
);
6149 * support IEEE 802.1Q VLAN trunk over ethernet
6158 * Change the offsets to point to the type and data fields within
6159 * the VLAN packet. Just increment the offsets, so that we
6160 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6161 * capture VLAN 200 encapsulated within VLAN 100.
6163 * XXX - this is a bit of a kludge. If we were to split the
6164 * compiler into a parser that parses an expression and
6165 * generates an expression tree, and a code generator that
6166 * takes an expression tree (which could come from our
6167 * parser or from some other parser) and generates BPF code,
6168 * we could perhaps make the offsets parameters of routines
6169 * and, in the handler for an "AND" node, pass to subnodes
6170 * other than the VLAN node the adjusted offsets.
6172 * This would mean that "vlan" would, instead of changing the
6173 * behavior of *all* tests after it, change only the behavior
6174 * of tests ANDed with it. That would change the documented
6175 * semantics of "vlan", which might break some expressions.
6176 * However, it would mean that "(vlan and ip) or ip" would check
6177 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6178 * checking only for VLAN-encapsulated IP, so that could still
6179 * be considered worth doing; it wouldn't break expressions
6180 * that are of the form "vlan and ..." or "vlan N and ...",
6181 * which I suspect are the most common expressions involving
6182 * "vlan". "vlan or ..." doesn't necessarily do what the user
6183 * would really want, now, as all the "or ..." tests would
6184 * be done assuming a VLAN, even though the "or" could be viewed
6185 * as meaning "or, if this isn't a VLAN packet...".
6187 orig_linktype
= off_linktype
; /* save original values */
6199 bpf_error("no VLAN support for data link type %d",
6204 /* check for VLAN */
6205 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6207 /* If a specific VLAN is requested, check VLAN id */
6208 if (vlan_num
>= 0) {
6211 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6230 * Change the offsets to point to the type and data fields within
6231 * the MPLS packet. Just increment the offsets, so that we
6232 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6233 * capture packets with an outer label of 100000 and an inner
6236 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6238 orig_linktype
= off_linktype
; /* save original values */
6243 case DLT_C_HDLC
: /* fall through */
6248 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6249 (bpf_int32
)ETHERTYPE_MPLS
);
6256 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6257 (bpf_int32
)PPP_MPLS_UCAST
);
6260 /* FIXME add other DLT_s ...
6261 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6262 * leave it for now */
6265 bpf_error("no MPLS support for data link type %d",
6272 /* If a specific MPLS label is requested, check it */
6273 if (label_num
>= 0) {
6276 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6277 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6278 0xfffff000); /* only compare the first 20 bits */
6287 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6299 bpf_error("'vpi' supported only on raw ATM");
6300 if (off_vpi
== (u_int
)-1)
6302 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6308 bpf_error("'vci' supported only on raw ATM");
6309 if (off_vci
== (u_int
)-1)
6311 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6316 if (off_proto
== (u_int
)-1)
6317 abort(); /* XXX - this isn't on FreeBSD */
6318 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6323 if (off_payload
== (u_int
)-1)
6325 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6326 0xffffffff, jtype
, reverse
, jvalue
);
6331 bpf_error("'callref' supported only on raw ATM");
6332 if (off_proto
== (u_int
)-1)
6334 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6335 jtype
, reverse
, jvalue
);
6345 gen_atmtype_abbrev(type
)
6348 struct block
*b0
, *b1
;
6353 /* Get all packets in Meta signalling Circuit */
6355 bpf_error("'metac' supported only on raw ATM");
6356 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6357 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6362 /* Get all packets in Broadcast Circuit*/
6364 bpf_error("'bcc' supported only on raw ATM");
6365 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6366 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6371 /* Get all cells in Segment OAM F4 circuit*/
6373 bpf_error("'oam4sc' supported only on raw ATM");
6374 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6375 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6380 /* Get all cells in End-to-End OAM F4 Circuit*/
6382 bpf_error("'oam4ec' supported only on raw ATM");
6383 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6384 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6389 /* Get all packets in connection Signalling Circuit */
6391 bpf_error("'sc' supported only on raw ATM");
6392 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6393 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6398 /* Get all packets in ILMI Circuit */
6400 bpf_error("'ilmic' supported only on raw ATM");
6401 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6402 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6407 /* Get all LANE packets */
6409 bpf_error("'lane' supported only on raw ATM");
6410 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6413 * Arrange that all subsequent tests assume LANE
6414 * rather than LLC-encapsulated packets, and set
6415 * the offsets appropriately for LANE-encapsulated
6418 * "off_mac" is the offset of the Ethernet header,
6419 * which is 2 bytes past the ATM pseudo-header
6420 * (skipping the pseudo-header and 2-byte LE Client
6421 * field). The other offsets are Ethernet offsets
6422 * relative to "off_mac".
6425 off_mac
= off_payload
+ 2; /* MAC header */
6426 off_linktype
= off_mac
+ 12;
6427 off_nl
= off_mac
+ 14; /* Ethernet II */
6428 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6432 /* Get all LLC-encapsulated packets */
6434 bpf_error("'llc' supported only on raw ATM");
6435 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6446 static struct block
*
6447 gen_msg_abbrev(type
)
6453 * Q.2931 signalling protocol messages for handling virtual circuits
6454 * establishment and teardown
6459 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6463 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6467 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6471 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6475 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6478 case A_RELEASE_DONE
:
6479 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6489 gen_atmmulti_abbrev(type
)
6492 struct block
*b0
, *b1
;
6498 bpf_error("'oam' supported only on raw ATM");
6499 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6504 bpf_error("'oamf4' supported only on raw ATM");
6506 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6507 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6509 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6515 * Get Q.2931 signalling messages for switched
6516 * virtual connection
6519 bpf_error("'connectmsg' supported only on raw ATM");
6520 b0
= gen_msg_abbrev(A_SETUP
);
6521 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6523 b0
= gen_msg_abbrev(A_CONNECT
);
6525 b0
= gen_msg_abbrev(A_CONNECTACK
);
6527 b0
= gen_msg_abbrev(A_RELEASE
);
6529 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6531 b0
= gen_atmtype_abbrev(A_SC
);
6537 bpf_error("'metaconnect' supported only on raw ATM");
6538 b0
= gen_msg_abbrev(A_SETUP
);
6539 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6541 b0
= gen_msg_abbrev(A_CONNECT
);
6543 b0
= gen_msg_abbrev(A_RELEASE
);
6545 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6547 b0
= gen_atmtype_abbrev(A_METAC
);