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.18 2005-05-03 20:33:51 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)
990 off_nl
= 8; /* 802.2+SNAP */
991 off_nl_nosnap
= 3; /* 802.2 */
996 * Full Frontal ATM; you get AALn PDUs with an ATM
1000 off_vpi
= SUNATM_VPI_POS
;
1001 off_vci
= SUNATM_VCI_POS
;
1002 off_proto
= PROTO_POS
;
1003 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1004 off_payload
= SUNATM_PKT_BEGIN_POS
;
1005 off_linktype
= off_payload
;
1006 off_nl
= off_payload
+8; /* 802.2+SNAP */
1007 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1013 off_nl_nosnap
= 0; /* no 802.2 LLC */
1016 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1019 off_nl_nosnap
= 16; /* no 802.2 LLC */
1024 * LocalTalk does have a 1-byte type field in the LLAP header,
1025 * but really it just indicates whether there is a "short" or
1026 * "long" DDP packet following.
1030 off_nl_nosnap
= 0; /* no 802.2 LLC */
1033 case DLT_IP_OVER_FC
:
1035 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1036 * link-level type field. We set "off_linktype" to the
1037 * offset of the LLC header.
1039 * To check for Ethernet types, we assume that SSAP = SNAP
1040 * is being used and pick out the encapsulated Ethernet type.
1041 * XXX - should we generate code to check for SNAP? RFC
1042 * 2625 says SNAP should be used.
1045 off_nl
= 24; /* IPFC+802.2+SNAP */
1046 off_nl_nosnap
= 19; /* IPFC+802.2 */
1051 * XXX - we should set this to handle SNAP-encapsulated
1052 * frames (NLPID of 0x80).
1056 off_nl_nosnap
= 0; /* no 802.2 LLC */
1059 case DLT_APPLE_IP_OVER_IEEE1394
:
1062 off_nl_nosnap
= 18; /* no 802.2 LLC */
1065 case DLT_LINUX_IRDA
:
1067 * Currently, only raw "link[N:M]" filtering is supported.
1076 * Currently, only raw "link[N:M]" filtering is supported.
1083 case DLT_SYMANTEC_FIREWALL
:
1085 off_nl
= 44; /* Ethernet II */
1086 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1091 /* XXX read this from pf.h? */
1092 off_nl
= PFLOG_HDRLEN
;
1093 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1096 case DLT_JUNIPER_MLFR
:
1097 case DLT_JUNIPER_MLPPP
:
1100 off_nl_nosnap
= -1; /* no 802.2 LLC */
1103 case DLT_JUNIPER_ATM1
:
1104 off_linktype
= 4; /* in reality variable between 4-8 */
1109 case DLT_JUNIPER_ATM2
:
1110 off_linktype
= 8; /* in reality variable between 8-12 */
1115 /* frames captured on a Juniper PPPoE service PIC
1116 * contain raw ethernet frames */
1117 case DLT_JUNIPER_PPPOE
:
1119 off_nl
= 18; /* Ethernet II */
1120 off_nl_nosnap
= 21; /* 802.3+802.2 */
1131 case DLT_LINUX_LAPD
:
1133 * Currently, only raw "link[N:M]" filtering is supported.
1140 bpf_error("unknown data link type %d", linktype
);
1145 * Load a value relative to the beginning of the link-layer header.
1146 * The link-layer header doesn't necessarily begin at the beginning
1147 * of the packet data; there might be a variable-length prefix containing
1148 * radio information.
1150 static struct slist
*
1151 gen_load_llrel(offset
, size
)
1154 struct slist
*s
, *s2
;
1156 s
= gen_llprefixlen();
1159 * If "s" is non-null, it has code to arrange that the X register
1160 * contains the length of the prefix preceding the link-layer
1164 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1168 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1175 * Load a value relative to the beginning of the specified header.
1177 static struct slist
*
1178 gen_load_a(offrel
, offset
, size
)
1179 enum e_offrel offrel
;
1182 struct slist
*s
, *s2
;
1187 s
= gen_load_llrel(offset
, size
);
1191 s
= gen_load_llrel(off_ll
+ offset
, size
);
1195 s
= gen_load_llrel(off_nl
+ offset
, size
);
1199 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1204 * Load the X register with the length of the IPv4 header,
1207 s
= gen_loadx_iphdrlen();
1210 * Load the item at {length of the link-layer header} +
1211 * {length of the IPv4 header} + {specified offset}.
1213 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1214 s2
->s
.k
= off_nl
+ offset
;
1219 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1230 * Generate code to load into the X register the sum of the length of
1231 * the IPv4 header and any variable-length header preceding the link-layer
1234 static struct slist
*
1235 gen_loadx_iphdrlen()
1237 struct slist
*s
, *s2
;
1239 s
= gen_llprefixlen();
1242 * There's a variable-length prefix preceding the
1243 * link-layer header. "s" points to a list of statements
1244 * that put the length of that prefix into the X register.
1245 * The 4*([k]&0xf) addressing mode can't be used, as we
1246 * don't have a constant offset, so we have to load the
1247 * value in question into the A register and add to it
1248 * the value from the X register.
1250 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1253 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1256 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1261 * The A register now contains the length of the
1262 * IP header. We need to add to it the length
1263 * of the prefix preceding the link-layer
1264 * header, which is still in the X register, and
1265 * move the result into the X register.
1267 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1268 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1271 * There is no variable-length header preceding the
1272 * link-layer header; if there's a fixed-length
1273 * header preceding it, its length is included in
1274 * the off_ variables, so it doesn't need to be added.
1276 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1282 static struct block
*
1289 s
= new_stmt(BPF_LD
|BPF_IMM
);
1291 b
= new_block(JMP(BPF_JEQ
));
1297 static inline struct block
*
1300 return gen_uncond(1);
1303 static inline struct block
*
1306 return gen_uncond(0);
1310 * Byte-swap a 32-bit number.
1311 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1312 * big-endian platforms.)
1314 #define SWAPLONG(y) \
1315 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1318 * Generate code to match a particular packet type.
1320 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1321 * value, if <= ETHERMTU. We use that to determine whether to
1322 * match the type/length field or to check the type/length field for
1323 * a value <= ETHERMTU to see whether it's a type field and then do
1324 * the appropriate test.
1326 static struct block
*
1327 gen_ether_linktype(proto
)
1330 struct block
*b0
, *b1
;
1336 case LLCSAP_NETBEUI
:
1338 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1339 * so we check the DSAP and SSAP.
1341 * LLCSAP_IP checks for IP-over-802.2, rather
1342 * than IP-over-Ethernet or IP-over-SNAP.
1344 * XXX - should we check both the DSAP and the
1345 * SSAP, like this, or should we check just the
1346 * DSAP, as we do for other types <= ETHERMTU
1347 * (i.e., other SAP values)?
1349 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1351 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1352 ((proto
<< 8) | proto
));
1360 * Ethernet_II frames, which are Ethernet
1361 * frames with a frame type of ETHERTYPE_IPX;
1363 * Ethernet_802.3 frames, which are 802.3
1364 * frames (i.e., the type/length field is
1365 * a length field, <= ETHERMTU, rather than
1366 * a type field) with the first two bytes
1367 * after the Ethernet/802.3 header being
1370 * Ethernet_802.2 frames, which are 802.3
1371 * frames with an 802.2 LLC header and
1372 * with the IPX LSAP as the DSAP in the LLC
1375 * Ethernet_SNAP frames, which are 802.3
1376 * frames with an LLC header and a SNAP
1377 * header and with an OUI of 0x000000
1378 * (encapsulated Ethernet) and a protocol
1379 * ID of ETHERTYPE_IPX in the SNAP header.
1381 * XXX - should we generate the same code both
1382 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1386 * This generates code to check both for the
1387 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1389 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1390 (bpf_int32
)LLCSAP_IPX
);
1391 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1396 * Now we add code to check for SNAP frames with
1397 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1399 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1403 * Now we generate code to check for 802.3
1404 * frames in general.
1406 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1410 * Now add the check for 802.3 frames before the
1411 * check for Ethernet_802.2 and Ethernet_802.3,
1412 * as those checks should only be done on 802.3
1413 * frames, not on Ethernet frames.
1418 * Now add the check for Ethernet_II frames, and
1419 * do that before checking for the other frame
1422 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1423 (bpf_int32
)ETHERTYPE_IPX
);
1427 case ETHERTYPE_ATALK
:
1428 case ETHERTYPE_AARP
:
1430 * EtherTalk (AppleTalk protocols on Ethernet link
1431 * layer) may use 802.2 encapsulation.
1435 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1436 * we check for an Ethernet type field less than
1437 * 1500, which means it's an 802.3 length field.
1439 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1443 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1444 * SNAP packets with an organization code of
1445 * 0x080007 (Apple, for Appletalk) and a protocol
1446 * type of ETHERTYPE_ATALK (Appletalk).
1448 * 802.2-encapsulated ETHERTYPE_AARP packets are
1449 * SNAP packets with an organization code of
1450 * 0x000000 (encapsulated Ethernet) and a protocol
1451 * type of ETHERTYPE_AARP (Appletalk ARP).
1453 if (proto
== ETHERTYPE_ATALK
)
1454 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1455 else /* proto == ETHERTYPE_AARP */
1456 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1460 * Check for Ethernet encapsulation (Ethertalk
1461 * phase 1?); we just check for the Ethernet
1464 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1470 if (proto
<= ETHERMTU
) {
1472 * This is an LLC SAP value, so the frames
1473 * that match would be 802.2 frames.
1474 * Check that the frame is an 802.2 frame
1475 * (i.e., that the length/type field is
1476 * a length field, <= ETHERMTU) and
1477 * then check the DSAP.
1479 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1481 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1487 * This is an Ethernet type, so compare
1488 * the length/type field with it (if
1489 * the frame is an 802.2 frame, the length
1490 * field will be <= ETHERMTU, and, as
1491 * "proto" is > ETHERMTU, this test
1492 * will fail and the frame won't match,
1493 * which is what we want).
1495 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1502 * Generate code to match a particular packet type.
1504 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1505 * value, if <= ETHERMTU. We use that to determine whether to
1506 * match the type field or to check the type field for the special
1507 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1509 static struct block
*
1510 gen_linux_sll_linktype(proto
)
1513 struct block
*b0
, *b1
;
1519 case LLCSAP_NETBEUI
:
1521 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1522 * so we check the DSAP and SSAP.
1524 * LLCSAP_IP checks for IP-over-802.2, rather
1525 * than IP-over-Ethernet or IP-over-SNAP.
1527 * XXX - should we check both the DSAP and the
1528 * SSAP, like this, or should we check just the
1529 * DSAP, as we do for other types <= ETHERMTU
1530 * (i.e., other SAP values)?
1532 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1533 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1534 ((proto
<< 8) | proto
));
1540 * Ethernet_II frames, which are Ethernet
1541 * frames with a frame type of ETHERTYPE_IPX;
1543 * Ethernet_802.3 frames, which have a frame
1544 * type of LINUX_SLL_P_802_3;
1546 * Ethernet_802.2 frames, which are 802.3
1547 * frames with an 802.2 LLC header (i.e, have
1548 * a frame type of LINUX_SLL_P_802_2) and
1549 * with the IPX LSAP as the DSAP in the LLC
1552 * Ethernet_SNAP frames, which are 802.3
1553 * frames with an LLC header and a SNAP
1554 * header and with an OUI of 0x000000
1555 * (encapsulated Ethernet) and a protocol
1556 * ID of ETHERTYPE_IPX in the SNAP header.
1558 * First, do the checks on LINUX_SLL_P_802_2
1559 * frames; generate the check for either
1560 * Ethernet_802.2 or Ethernet_SNAP frames, and
1561 * then put a check for LINUX_SLL_P_802_2 frames
1564 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1565 (bpf_int32
)LLCSAP_IPX
);
1566 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1569 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1573 * Now check for 802.3 frames and OR that with
1574 * the previous test.
1576 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1580 * Now add the check for Ethernet_II frames, and
1581 * do that before checking for the other frame
1584 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1585 (bpf_int32
)ETHERTYPE_IPX
);
1589 case ETHERTYPE_ATALK
:
1590 case ETHERTYPE_AARP
:
1592 * EtherTalk (AppleTalk protocols on Ethernet link
1593 * layer) may use 802.2 encapsulation.
1597 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1598 * we check for the 802.2 protocol type in the
1599 * "Ethernet type" field.
1601 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1604 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1605 * SNAP packets with an organization code of
1606 * 0x080007 (Apple, for Appletalk) and a protocol
1607 * type of ETHERTYPE_ATALK (Appletalk).
1609 * 802.2-encapsulated ETHERTYPE_AARP packets are
1610 * SNAP packets with an organization code of
1611 * 0x000000 (encapsulated Ethernet) and a protocol
1612 * type of ETHERTYPE_AARP (Appletalk ARP).
1614 if (proto
== ETHERTYPE_ATALK
)
1615 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1617 else /* proto == ETHERTYPE_AARP */
1618 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1623 * Check for Ethernet encapsulation (Ethertalk
1624 * phase 1?); we just check for the Ethernet
1627 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1633 if (proto
<= ETHERMTU
) {
1635 * This is an LLC SAP value, so the frames
1636 * that match would be 802.2 frames.
1637 * Check for the 802.2 protocol type
1638 * in the "Ethernet type" field, and
1639 * then check the DSAP.
1641 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1643 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1649 * This is an Ethernet type, so compare
1650 * the length/type field with it (if
1651 * the frame is an 802.2 frame, the length
1652 * field will be <= ETHERMTU, and, as
1653 * "proto" is > ETHERMTU, this test
1654 * will fail and the frame won't match,
1655 * which is what we want).
1657 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1664 insert_radiotap_load_llprefixlen(b
)
1667 struct slist
*s1
, *s2
;
1670 * Prepend to the statements in this block code to load the
1671 * length of the radiotap header into the register assigned
1672 * to hold that length, if one has been assigned.
1674 if (reg_ll_size
!= -1) {
1676 * The 2 bytes at offsets of 2 and 3 from the beginning
1677 * of the radiotap header are the length of the radiotap
1678 * header; unfortunately, it's little-endian, so we have
1679 * to load it a byte at a time and construct the value.
1683 * Load the high-order byte, at an offset of 3, shift it
1684 * left a byte, and put the result in the X register.
1686 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1688 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1691 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1695 * Load the next byte, at an offset of 2, and OR the
1696 * value from the X register into it.
1698 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1701 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1705 * Now allocate a register to hold that value and store
1708 s2
= new_stmt(BPF_ST
);
1709 s2
->s
.k
= reg_ll_size
;
1713 * Now move it into the X register.
1715 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1719 * Now append all the existing statements in this
1720 * block to these statements.
1722 sappend(s1
, b
->stmts
);
1729 insert_load_llprefixlen(b
)
1734 case DLT_IEEE802_11_RADIO
:
1735 insert_radiotap_load_llprefixlen(b
);
1740 static struct slist
*
1741 gen_radiotap_llprefixlen(void)
1745 if (reg_ll_size
== -1) {
1747 * We haven't yet assigned a register for the length
1748 * of the radiotap header; allocate one.
1750 reg_ll_size
= alloc_reg();
1754 * Load the register containing the radiotap length
1755 * into the X register.
1757 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1758 s
->s
.k
= reg_ll_size
;
1763 * Generate code to compute the link-layer header length, if necessary,
1764 * putting it into the X register, and to return either a pointer to a
1765 * "struct slist" for the list of statements in that code, or NULL if
1766 * no code is necessary.
1768 static struct slist
*
1769 gen_llprefixlen(void)
1773 case DLT_IEEE802_11_RADIO
:
1774 return gen_radiotap_llprefixlen();
1782 * Generate code to match a particular packet type by matching the
1783 * link-layer type field or fields in the 802.2 LLC header.
1785 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1786 * value, if <= ETHERMTU.
1788 static struct block
*
1792 struct block
*b0
, *b1
, *b2
;
1797 return gen_ether_linktype(proto
);
1805 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1809 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1818 case DLT_IEEE802_11
:
1819 case DLT_IEEE802_11_RADIO_AVS
:
1820 case DLT_IEEE802_11_RADIO
:
1821 case DLT_PRISM_HEADER
:
1822 case DLT_ATM_RFC1483
:
1824 case DLT_IP_OVER_FC
:
1825 return gen_llc_linktype(proto
);
1831 * If "is_lane" is set, check for a LANE-encapsulated
1832 * version of this protocol, otherwise check for an
1833 * LLC-encapsulated version of this protocol.
1835 * We assume LANE means Ethernet, not Token Ring.
1839 * Check that the packet doesn't begin with an
1840 * LE Control marker. (We've already generated
1843 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1848 * Now generate an Ethernet test.
1850 b1
= gen_ether_linktype(proto
);
1855 * Check for LLC encapsulation and then check the
1858 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1859 b1
= gen_llc_linktype(proto
);
1867 return gen_linux_sll_linktype(proto
);
1872 case DLT_SLIP_BSDOS
:
1875 * These types don't provide any type field; packets
1878 * XXX - for IPv4, check for a version number of 4, and,
1879 * for IPv6, check for a version number of 6?
1885 case ETHERTYPE_IPV6
:
1887 return gen_true(); /* always true */
1890 return gen_false(); /* always false */
1897 case DLT_PPP_SERIAL
:
1900 * We use Ethernet protocol types inside libpcap;
1901 * map them to the corresponding PPP protocol types.
1910 case ETHERTYPE_IPV6
:
1919 case ETHERTYPE_ATALK
:
1933 * I'm assuming the "Bridging PDU"s that go
1934 * over PPP are Spanning Tree Protocol
1948 * We use Ethernet protocol types inside libpcap;
1949 * map them to the corresponding PPP protocol types.
1954 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
1955 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
1957 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
1962 case ETHERTYPE_IPV6
:
1972 case ETHERTYPE_ATALK
:
1986 * I'm assuming the "Bridging PDU"s that go
1987 * over PPP are Spanning Tree Protocol
2003 * For DLT_NULL, the link-layer header is a 32-bit
2004 * word containing an AF_ value in *host* byte order,
2005 * and for DLT_ENC, the link-layer header begins
2006 * with a 32-bit work containing an AF_ value in
2009 * In addition, if we're reading a saved capture file,
2010 * the host byte order in the capture may not be the
2011 * same as the host byte order on this machine.
2013 * For DLT_LOOP, the link-layer header is a 32-bit
2014 * word containing an AF_ value in *network* byte order.
2016 * XXX - AF_ values may, unfortunately, be platform-
2017 * dependent; for example, FreeBSD's AF_INET6 is 24
2018 * whilst NetBSD's and OpenBSD's is 26.
2020 * This means that, when reading a capture file, just
2021 * checking for our AF_INET6 value won't work if the
2022 * capture file came from another OS.
2031 case ETHERTYPE_IPV6
:
2038 * Not a type on which we support filtering.
2039 * XXX - support those that have AF_ values
2040 * #defined on this platform, at least?
2045 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2047 * The AF_ value is in host byte order, but
2048 * the BPF interpreter will convert it to
2049 * network byte order.
2051 * If this is a save file, and it's from a
2052 * machine with the opposite byte order to
2053 * ours, we byte-swap the AF_ value.
2055 * Then we run it through "htonl()", and
2056 * generate code to compare against the result.
2058 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2059 bpf_pcap
->sf
.swapped
)
2060 proto
= SWAPLONG(proto
);
2061 proto
= htonl(proto
);
2063 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2067 * af field is host byte order in contrast to the rest of
2070 if (proto
== ETHERTYPE_IP
)
2071 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2072 BPF_B
, (bpf_int32
)AF_INET
));
2074 else if (proto
== ETHERTYPE_IPV6
)
2075 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2076 BPF_B
, (bpf_int32
)AF_INET6
));
2084 case DLT_ARCNET_LINUX
:
2086 * XXX should we check for first fragment if the protocol
2095 case ETHERTYPE_IPV6
:
2096 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2097 (bpf_int32
)ARCTYPE_INET6
));
2101 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2102 (bpf_int32
)ARCTYPE_IP
);
2103 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2104 (bpf_int32
)ARCTYPE_IP_OLD
);
2109 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2110 (bpf_int32
)ARCTYPE_ARP
);
2111 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2112 (bpf_int32
)ARCTYPE_ARP_OLD
);
2116 case ETHERTYPE_REVARP
:
2117 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2118 (bpf_int32
)ARCTYPE_REVARP
));
2120 case ETHERTYPE_ATALK
:
2121 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2122 (bpf_int32
)ARCTYPE_ATALK
));
2129 case ETHERTYPE_ATALK
:
2139 * XXX - assumes a 2-byte Frame Relay header with
2140 * DLCI and flags. What if the address is longer?
2146 * Check for the special NLPID for IP.
2148 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2151 case ETHERTYPE_IPV6
:
2153 * Check for the special NLPID for IPv6.
2155 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2160 * Check for several OSI protocols.
2162 * Frame Relay packets typically have an OSI
2163 * NLPID at the beginning; we check for each
2166 * What we check for is the NLPID and a frame
2167 * control field of UI, i.e. 0x03 followed
2170 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2171 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2172 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2183 case DLT_JUNIPER_MLFR
:
2184 case DLT_JUNIPER_MLPPP
:
2185 case DLT_JUNIPER_ATM1
:
2186 case DLT_JUNIPER_ATM2
:
2187 case DLT_JUNIPER_PPPOE
:
2188 /* just lets verify the magic number for now -
2189 * on ATM we may have up to 6 different encapsulations on the wire
2190 * and need a lot of heuristics to figure out that the payload
2193 * FIXME encapsulation specific BPF_ filters
2195 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2197 case DLT_LINUX_IRDA
:
2198 bpf_error("IrDA link-layer type filtering not implemented");
2201 bpf_error("DOCSIS link-layer type filtering not implemented");
2203 case DLT_LINUX_LAPD
:
2204 bpf_error("LAPD link-layer type filtering not implemented");
2208 * All the types that have no encapsulation should either be
2209 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2210 * all packets are IP packets, or should be handled in some
2211 * special case, if none of them are (if some are and some
2212 * aren't, the lack of encapsulation is a problem, as we'd
2213 * have to find some other way of determining the packet type).
2215 * Therefore, if "off_linktype" is -1, there's an error.
2217 if (off_linktype
== (u_int
)-1)
2221 * Any type not handled above should always have an Ethernet
2222 * type at an offset of "off_linktype". (PPP is partially
2223 * handled above - the protocol type is mapped from the
2224 * Ethernet and LLC types we use internally to the corresponding
2225 * PPP type - but the PPP type is always specified by a value
2226 * at "off_linktype", so we don't have to do the code generation
2229 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2233 * Check for an LLC SNAP packet with a given organization code and
2234 * protocol type; we check the entire contents of the 802.2 LLC and
2235 * snap headers, checking for DSAP and SSAP of SNAP and a control
2236 * field of 0x03 in the LLC header, and for the specified organization
2237 * code and protocol type in the SNAP header.
2239 static struct block
*
2240 gen_snap(orgcode
, ptype
, offset
)
2241 bpf_u_int32 orgcode
;
2245 u_char snapblock
[8];
2247 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2248 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2249 snapblock
[2] = 0x03; /* control = UI */
2250 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2251 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2252 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2253 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2254 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2255 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2259 * Generate code to match a particular packet type, for link-layer types
2260 * using 802.2 LLC headers.
2262 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2263 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2265 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2266 * value, if <= ETHERMTU. We use that to determine whether to
2267 * match the DSAP or both DSAP and LSAP or to check the OUI and
2268 * protocol ID in a SNAP header.
2270 static struct block
*
2271 gen_llc_linktype(proto
)
2275 * XXX - handle token-ring variable-length header.
2281 case LLCSAP_NETBEUI
:
2283 * XXX - should we check both the DSAP and the
2284 * SSAP, like this, or should we check just the
2285 * DSAP, as we do for other types <= ETHERMTU
2286 * (i.e., other SAP values)?
2288 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2289 ((proto
<< 8) | proto
));
2293 * XXX - are there ever SNAP frames for IPX on
2294 * non-Ethernet 802.x networks?
2296 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2297 (bpf_int32
)LLCSAP_IPX
);
2299 case ETHERTYPE_ATALK
:
2301 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2302 * SNAP packets with an organization code of
2303 * 0x080007 (Apple, for Appletalk) and a protocol
2304 * type of ETHERTYPE_ATALK (Appletalk).
2306 * XXX - check for an organization code of
2307 * encapsulated Ethernet as well?
2309 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2313 * XXX - we don't have to check for IPX 802.3
2314 * here, but should we check for the IPX Ethertype?
2316 if (proto
<= ETHERMTU
) {
2318 * This is an LLC SAP value, so check
2321 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2325 * This is an Ethernet type; we assume that it's
2326 * unlikely that it'll appear in the right place
2327 * at random, and therefore check only the
2328 * location that would hold the Ethernet type
2329 * in a SNAP frame with an organization code of
2330 * 0x000000 (encapsulated Ethernet).
2332 * XXX - if we were to check for the SNAP DSAP and
2333 * LSAP, as per XXX, and were also to check for an
2334 * organization code of 0x000000 (encapsulated
2335 * Ethernet), we'd do
2337 * return gen_snap(0x000000, proto,
2340 * here; for now, we don't, as per the above.
2341 * I don't know whether it's worth the extra CPU
2342 * time to do the right check or not.
2344 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2350 static struct block
*
2351 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2355 u_int src_off
, dst_off
;
2357 struct block
*b0
, *b1
;
2371 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2372 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2378 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2379 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2386 b0
= gen_linktype(proto
);
2387 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2393 static struct block
*
2394 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2395 struct in6_addr
*addr
;
2396 struct in6_addr
*mask
;
2398 u_int src_off
, dst_off
;
2400 struct block
*b0
, *b1
;
2415 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2416 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2422 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2423 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2430 /* this order is important */
2431 a
= (u_int32_t
*)addr
;
2432 m
= (u_int32_t
*)mask
;
2433 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2434 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2436 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2438 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2440 b0
= gen_linktype(proto
);
2446 static struct block
*
2447 gen_ehostop(eaddr
, dir
)
2448 register const u_char
*eaddr
;
2451 register struct block
*b0
, *b1
;
2455 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2458 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2461 b0
= gen_ehostop(eaddr
, Q_SRC
);
2462 b1
= gen_ehostop(eaddr
, Q_DST
);
2468 b0
= gen_ehostop(eaddr
, Q_SRC
);
2469 b1
= gen_ehostop(eaddr
, Q_DST
);
2478 * Like gen_ehostop, but for DLT_FDDI
2480 static struct block
*
2481 gen_fhostop(eaddr
, dir
)
2482 register const u_char
*eaddr
;
2485 struct block
*b0
, *b1
;
2490 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2492 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2497 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2499 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2503 b0
= gen_fhostop(eaddr
, Q_SRC
);
2504 b1
= gen_fhostop(eaddr
, Q_DST
);
2510 b0
= gen_fhostop(eaddr
, Q_SRC
);
2511 b1
= gen_fhostop(eaddr
, Q_DST
);
2520 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2522 static struct block
*
2523 gen_thostop(eaddr
, dir
)
2524 register const u_char
*eaddr
;
2527 register struct block
*b0
, *b1
;
2531 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2534 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2537 b0
= gen_thostop(eaddr
, Q_SRC
);
2538 b1
= gen_thostop(eaddr
, Q_DST
);
2544 b0
= gen_thostop(eaddr
, Q_SRC
);
2545 b1
= gen_thostop(eaddr
, Q_DST
);
2554 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2556 static struct block
*
2557 gen_wlanhostop(eaddr
, dir
)
2558 register const u_char
*eaddr
;
2561 register struct block
*b0
, *b1
, *b2
;
2562 register struct slist
*s
;
2569 * For control frames, there is no SA.
2571 * For management frames, SA is at an
2572 * offset of 10 from the beginning of
2575 * For data frames, SA is at an offset
2576 * of 10 from the beginning of the packet
2577 * if From DS is clear, at an offset of
2578 * 16 from the beginning of the packet
2579 * if From DS is set and To DS is clear,
2580 * and an offset of 24 from the beginning
2581 * of the packet if From DS is set and To DS
2586 * Generate the tests to be done for data frames
2589 * First, check for To DS set, i.e. check "link[1] & 0x01".
2591 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2592 b1
= new_block(JMP(BPF_JSET
));
2593 b1
->s
.k
= 0x01; /* To DS */
2597 * If To DS is set, the SA is at 24.
2599 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2603 * Now, check for To DS not set, i.e. check
2604 * "!(link[1] & 0x01)".
2606 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2607 b2
= new_block(JMP(BPF_JSET
));
2608 b2
->s
.k
= 0x01; /* To DS */
2613 * If To DS is not set, the SA is at 16.
2615 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2619 * Now OR together the last two checks. That gives
2620 * the complete set of checks for data frames with
2626 * Now check for From DS being set, and AND that with
2627 * the ORed-together checks.
2629 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2630 b1
= new_block(JMP(BPF_JSET
));
2631 b1
->s
.k
= 0x02; /* From DS */
2636 * Now check for data frames with From DS not set.
2638 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2639 b2
= new_block(JMP(BPF_JSET
));
2640 b2
->s
.k
= 0x02; /* From DS */
2645 * If From DS isn't set, the SA is at 10.
2647 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2651 * Now OR together the checks for data frames with
2652 * From DS not set and for data frames with From DS
2653 * set; that gives the checks done for data frames.
2658 * Now check for a data frame.
2659 * I.e, check "link[0] & 0x08".
2661 gen_load_a(OR_LINK
, 0, BPF_B
);
2662 b1
= new_block(JMP(BPF_JSET
));
2667 * AND that with the checks done for data frames.
2672 * If the high-order bit of the type value is 0, this
2673 * is a management frame.
2674 * I.e, check "!(link[0] & 0x08)".
2676 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2677 b2
= new_block(JMP(BPF_JSET
));
2683 * For management frames, the SA is at 10.
2685 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2689 * OR that with the checks done for data frames.
2690 * That gives the checks done for management and
2696 * If the low-order bit of the type value is 1,
2697 * this is either a control frame or a frame
2698 * with a reserved type, and thus not a
2701 * I.e., check "!(link[0] & 0x04)".
2703 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2704 b1
= new_block(JMP(BPF_JSET
));
2710 * AND that with the checks for data and management
2720 * For control frames, there is no DA.
2722 * For management frames, DA is at an
2723 * offset of 4 from the beginning of
2726 * For data frames, DA is at an offset
2727 * of 4 from the beginning of the packet
2728 * if To DS is clear and at an offset of
2729 * 16 from the beginning of the packet
2734 * Generate the tests to be done for data frames.
2736 * First, check for To DS set, i.e. "link[1] & 0x01".
2738 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2739 b1
= new_block(JMP(BPF_JSET
));
2740 b1
->s
.k
= 0x01; /* To DS */
2744 * If To DS is set, the DA is at 16.
2746 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2750 * Now, check for To DS not set, i.e. check
2751 * "!(link[1] & 0x01)".
2753 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2754 b2
= new_block(JMP(BPF_JSET
));
2755 b2
->s
.k
= 0x01; /* To DS */
2760 * If To DS is not set, the DA is at 4.
2762 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2766 * Now OR together the last two checks. That gives
2767 * the complete set of checks for data frames.
2772 * Now check for a data frame.
2773 * I.e, check "link[0] & 0x08".
2775 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2776 b1
= new_block(JMP(BPF_JSET
));
2781 * AND that with the checks done for data frames.
2786 * If the high-order bit of the type value is 0, this
2787 * is a management frame.
2788 * I.e, check "!(link[0] & 0x08)".
2790 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2791 b2
= new_block(JMP(BPF_JSET
));
2797 * For management frames, the DA is at 4.
2799 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2803 * OR that with the checks done for data frames.
2804 * That gives the checks done for management and
2810 * If the low-order bit of the type value is 1,
2811 * this is either a control frame or a frame
2812 * with a reserved type, and thus not a
2815 * I.e., check "!(link[0] & 0x04)".
2817 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2818 b1
= new_block(JMP(BPF_JSET
));
2824 * AND that with the checks for data and management
2831 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2832 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2838 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2839 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2848 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2849 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2850 * as the RFC states.)
2852 static struct block
*
2853 gen_ipfchostop(eaddr
, dir
)
2854 register const u_char
*eaddr
;
2857 register struct block
*b0
, *b1
;
2861 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2864 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2867 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2868 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2874 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2875 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2884 * This is quite tricky because there may be pad bytes in front of the
2885 * DECNET header, and then there are two possible data packet formats that
2886 * carry both src and dst addresses, plus 5 packet types in a format that
2887 * carries only the src node, plus 2 types that use a different format and
2888 * also carry just the src node.
2892 * Instead of doing those all right, we just look for data packets with
2893 * 0 or 1 bytes of padding. If you want to look at other packets, that
2894 * will require a lot more hacking.
2896 * To add support for filtering on DECNET "areas" (network numbers)
2897 * one would want to add a "mask" argument to this routine. That would
2898 * make the filter even more inefficient, although one could be clever
2899 * and not generate masking instructions if the mask is 0xFFFF.
2901 static struct block
*
2902 gen_dnhostop(addr
, dir
)
2906 struct block
*b0
, *b1
, *b2
, *tmp
;
2907 u_int offset_lh
; /* offset if long header is received */
2908 u_int offset_sh
; /* offset if short header is received */
2913 offset_sh
= 1; /* follows flags */
2914 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2918 offset_sh
= 3; /* follows flags, dstnode */
2919 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2923 /* Inefficient because we do our Calvinball dance twice */
2924 b0
= gen_dnhostop(addr
, Q_SRC
);
2925 b1
= gen_dnhostop(addr
, Q_DST
);
2931 /* Inefficient because we do our Calvinball dance twice */
2932 b0
= gen_dnhostop(addr
, Q_SRC
);
2933 b1
= gen_dnhostop(addr
, Q_DST
);
2938 bpf_error("ISO host filtering not implemented");
2943 b0
= gen_linktype(ETHERTYPE_DN
);
2944 /* Check for pad = 1, long header case */
2945 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2946 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2947 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
2948 BPF_H
, (bpf_int32
)ntohs(addr
));
2950 /* Check for pad = 0, long header case */
2951 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2952 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2955 /* Check for pad = 1, short header case */
2956 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2957 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2958 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2961 /* Check for pad = 0, short header case */
2962 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2963 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2967 /* Combine with test for linktype */
2972 static struct block
*
2973 gen_host(addr
, mask
, proto
, dir
)
2979 struct block
*b0
, *b1
;
2984 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2985 if (off_linktype
!= (u_int
)-1) {
2986 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2988 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2994 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
2997 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3000 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3003 bpf_error("'tcp' modifier applied to host");
3006 bpf_error("'sctp' modifier applied to host");
3009 bpf_error("'udp' modifier applied to host");
3012 bpf_error("'icmp' modifier applied to host");
3015 bpf_error("'igmp' modifier applied to host");
3018 bpf_error("'igrp' modifier applied to host");
3021 bpf_error("'pim' modifier applied to host");
3024 bpf_error("'vrrp' modifier applied to host");
3027 bpf_error("ATALK host filtering not implemented");
3030 bpf_error("AARP host filtering not implemented");
3033 return gen_dnhostop(addr
, dir
);
3036 bpf_error("SCA host filtering not implemented");
3039 bpf_error("LAT host filtering not implemented");
3042 bpf_error("MOPDL host filtering not implemented");
3045 bpf_error("MOPRC host filtering not implemented");
3049 bpf_error("'ip6' modifier applied to ip host");
3052 bpf_error("'icmp6' modifier applied to host");
3056 bpf_error("'ah' modifier applied to host");
3059 bpf_error("'esp' modifier applied to host");
3062 bpf_error("ISO host filtering not implemented");
3065 bpf_error("'esis' modifier applied to host");
3068 bpf_error("'isis' modifier applied to host");
3071 bpf_error("'clnp' modifier applied to host");
3074 bpf_error("'stp' modifier applied to host");
3077 bpf_error("IPX host filtering not implemented");
3080 bpf_error("'netbeui' modifier applied to host");
3083 bpf_error("'radio' modifier applied to host");
3092 static struct block
*
3093 gen_host6(addr
, mask
, proto
, dir
)
3094 struct in6_addr
*addr
;
3095 struct in6_addr
*mask
;
3102 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3105 bpf_error("'ip' modifier applied to ip6 host");
3108 bpf_error("'rarp' modifier applied to ip6 host");
3111 bpf_error("'arp' modifier applied to ip6 host");
3114 bpf_error("'sctp' modifier applied to host");
3117 bpf_error("'tcp' modifier applied to host");
3120 bpf_error("'udp' modifier applied to host");
3123 bpf_error("'icmp' modifier applied to host");
3126 bpf_error("'igmp' modifier applied to host");
3129 bpf_error("'igrp' modifier applied to host");
3132 bpf_error("'pim' modifier applied to host");
3135 bpf_error("'vrrp' modifier applied to host");
3138 bpf_error("ATALK host filtering not implemented");
3141 bpf_error("AARP host filtering not implemented");
3144 bpf_error("'decnet' modifier applied to ip6 host");
3147 bpf_error("SCA host filtering not implemented");
3150 bpf_error("LAT host filtering not implemented");
3153 bpf_error("MOPDL host filtering not implemented");
3156 bpf_error("MOPRC host filtering not implemented");
3159 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3162 bpf_error("'icmp6' modifier applied to host");
3165 bpf_error("'ah' modifier applied to host");
3168 bpf_error("'esp' modifier applied to host");
3171 bpf_error("ISO host filtering not implemented");
3174 bpf_error("'esis' modifier applied to host");
3177 bpf_error("'isis' modifier applied to host");
3180 bpf_error("'clnp' modifier applied to host");
3183 bpf_error("'stp' modifier applied to host");
3186 bpf_error("IPX host filtering not implemented");
3189 bpf_error("'netbeui' modifier applied to host");
3192 bpf_error("'radio' modifier applied to host");
3202 static struct block
*
3203 gen_gateway(eaddr
, alist
, proto
, dir
)
3204 const u_char
*eaddr
;
3205 bpf_u_int32
**alist
;
3209 struct block
*b0
, *b1
, *tmp
;
3212 bpf_error("direction applied to 'gateway'");
3219 if (linktype
== DLT_EN10MB
)
3220 b0
= gen_ehostop(eaddr
, Q_OR
);
3221 else if (linktype
== DLT_FDDI
)
3222 b0
= gen_fhostop(eaddr
, Q_OR
);
3223 else if (linktype
== DLT_IEEE802
)
3224 b0
= gen_thostop(eaddr
, Q_OR
);
3225 else if (linktype
== DLT_IEEE802_11
||
3226 linktype
== DLT_IEEE802_11_RADIO_AVS
||
3227 linktype
== DLT_IEEE802_11_RADIO
||
3228 linktype
== DLT_PRISM_HEADER
)
3229 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3230 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3232 * Check that the packet doesn't begin with an
3233 * LE Control marker. (We've already generated
3236 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3241 * Now check the MAC address.
3243 b0
= gen_ehostop(eaddr
, Q_OR
);
3245 } else if (linktype
== DLT_IP_OVER_FC
)
3246 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3249 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3251 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3253 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3261 bpf_error("illegal modifier of 'gateway'");
3267 gen_proto_abbrev(proto
)
3276 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3278 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3284 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3286 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3292 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3294 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3300 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3303 #ifndef IPPROTO_IGMP
3304 #define IPPROTO_IGMP 2
3308 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3311 #ifndef IPPROTO_IGRP
3312 #define IPPROTO_IGRP 9
3315 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3319 #define IPPROTO_PIM 103
3323 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3325 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3330 #ifndef IPPROTO_VRRP
3331 #define IPPROTO_VRRP 112
3335 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3339 b1
= gen_linktype(ETHERTYPE_IP
);
3343 b1
= gen_linktype(ETHERTYPE_ARP
);
3347 b1
= gen_linktype(ETHERTYPE_REVARP
);
3351 bpf_error("link layer applied in wrong context");
3354 b1
= gen_linktype(ETHERTYPE_ATALK
);
3358 b1
= gen_linktype(ETHERTYPE_AARP
);
3362 b1
= gen_linktype(ETHERTYPE_DN
);
3366 b1
= gen_linktype(ETHERTYPE_SCA
);
3370 b1
= gen_linktype(ETHERTYPE_LAT
);
3374 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3378 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3383 b1
= gen_linktype(ETHERTYPE_IPV6
);
3386 #ifndef IPPROTO_ICMPV6
3387 #define IPPROTO_ICMPV6 58
3390 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3395 #define IPPROTO_AH 51
3398 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3400 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3406 #define IPPROTO_ESP 50
3409 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3411 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3417 b1
= gen_linktype(LLCSAP_ISONS
);
3421 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3425 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3428 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3429 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3430 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3432 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3434 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3436 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3440 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3441 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3442 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3444 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3446 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3448 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3452 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3453 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3454 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3456 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3461 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3462 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3467 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3468 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3470 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3472 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3477 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3478 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3483 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3484 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3489 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3493 b1
= gen_linktype(LLCSAP_8021D
);
3497 b1
= gen_linktype(LLCSAP_IPX
);
3501 b1
= gen_linktype(LLCSAP_NETBEUI
);
3505 bpf_error("'radio' is not a valid protocol type");
3513 static struct block
*
3520 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3521 b
= new_block(JMP(BPF_JSET
));
3530 * Generate a comparison to a port value in the transport-layer header
3531 * at the specified offset from the beginning of that header.
3533 * XXX - this handles a variable-length prefix preceding the link-layer
3534 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3535 * variable-length link-layer headers (such as Token Ring or 802.11
3538 static struct block
*
3539 gen_portatom(off
, v
)
3543 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3547 static struct block
*
3548 gen_portatom6(off
, v
)
3552 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3557 gen_portop(port
, proto
, dir
)
3558 int port
, proto
, dir
;
3560 struct block
*b0
, *b1
, *tmp
;
3562 /* ip proto 'proto' */
3563 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3569 b1
= gen_portatom(0, (bpf_int32
)port
);
3573 b1
= gen_portatom(2, (bpf_int32
)port
);
3578 tmp
= gen_portatom(0, (bpf_int32
)port
);
3579 b1
= gen_portatom(2, (bpf_int32
)port
);
3584 tmp
= gen_portatom(0, (bpf_int32
)port
);
3585 b1
= gen_portatom(2, (bpf_int32
)port
);
3597 static struct block
*
3598 gen_port(port
, ip_proto
, dir
)
3603 struct block
*b0
, *b1
, *tmp
;
3608 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3609 * not LLC encapsulation with LLCSAP_IP.
3611 * For IEEE 802 networks - which includes 802.5 token ring
3612 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3613 * says that SNAP encapsulation is used, not LLC encapsulation
3616 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3617 * RFC 2225 say that SNAP encapsulation is used, not LLC
3618 * encapsulation with LLCSAP_IP.
3620 * So we always check for ETHERTYPE_IP.
3622 b0
= gen_linktype(ETHERTYPE_IP
);
3628 b1
= gen_portop(port
, ip_proto
, dir
);
3632 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3633 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3635 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3648 gen_portop6(port
, proto
, dir
)
3649 int port
, proto
, dir
;
3651 struct block
*b0
, *b1
, *tmp
;
3653 /* ip6 proto 'proto' */
3654 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3658 b1
= gen_portatom6(0, (bpf_int32
)port
);
3662 b1
= gen_portatom6(2, (bpf_int32
)port
);
3667 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3668 b1
= gen_portatom6(2, (bpf_int32
)port
);
3673 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3674 b1
= gen_portatom6(2, (bpf_int32
)port
);
3686 static struct block
*
3687 gen_port6(port
, ip_proto
, dir
)
3692 struct block
*b0
, *b1
, *tmp
;
3694 /* link proto ip6 */
3695 b0
= gen_linktype(ETHERTYPE_IPV6
);
3701 b1
= gen_portop6(port
, ip_proto
, dir
);
3705 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3706 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3708 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3720 /* gen_portrange code */
3721 static struct block
*
3722 gen_portrangeatom(off
, v1
, v2
)
3726 struct block
*b1
, *b2
;
3730 * Reverse the order of the ports, so v1 is the lower one.
3739 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3740 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3748 gen_portrangeop(port1
, port2
, proto
, dir
)
3753 struct block
*b0
, *b1
, *tmp
;
3755 /* ip proto 'proto' */
3756 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3762 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3766 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3771 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3772 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3777 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3778 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3790 static struct block
*
3791 gen_portrange(port1
, port2
, ip_proto
, dir
)
3796 struct block
*b0
, *b1
, *tmp
;
3799 b0
= gen_linktype(ETHERTYPE_IP
);
3805 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3809 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3810 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3812 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3824 static struct block
*
3825 gen_portrangeatom6(off
, v1
, v2
)
3829 struct block
*b1
, *b2
;
3833 * Reverse the order of the ports, so v1 is the lower one.
3842 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3843 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3851 gen_portrangeop6(port1
, port2
, proto
, dir
)
3856 struct block
*b0
, *b1
, *tmp
;
3858 /* ip6 proto 'proto' */
3859 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3863 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3867 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3872 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3873 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3878 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3879 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3891 static struct block
*
3892 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3897 struct block
*b0
, *b1
, *tmp
;
3899 /* link proto ip6 */
3900 b0
= gen_linktype(ETHERTYPE_IPV6
);
3906 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3910 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3911 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3913 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3926 lookup_proto(name
, proto
)
3927 register const char *name
;
3937 v
= pcap_nametoproto(name
);
3938 if (v
== PROTO_UNDEF
)
3939 bpf_error("unknown ip proto '%s'", name
);
3943 /* XXX should look up h/w protocol type based on linktype */
3944 v
= pcap_nametoeproto(name
);
3945 if (v
== PROTO_UNDEF
) {
3946 v
= pcap_nametollc(name
);
3947 if (v
== PROTO_UNDEF
)
3948 bpf_error("unknown ether proto '%s'", name
);
3953 if (strcmp(name
, "esis") == 0)
3955 else if (strcmp(name
, "isis") == 0)
3957 else if (strcmp(name
, "clnp") == 0)
3960 bpf_error("unknown osi proto '%s'", name
);
3980 static struct block
*
3981 gen_protochain(v
, proto
, dir
)
3986 #ifdef NO_PROTOCHAIN
3987 return gen_proto(v
, proto
, dir
);
3989 struct block
*b0
, *b
;
3990 struct slist
*s
[100];
3991 int fix2
, fix3
, fix4
, fix5
;
3992 int ahcheck
, again
, end
;
3994 int reg2
= alloc_reg();
3996 memset(s
, 0, sizeof(s
));
3997 fix2
= fix3
= fix4
= fix5
= 0;
4004 b0
= gen_protochain(v
, Q_IP
, dir
);
4005 b
= gen_protochain(v
, Q_IPV6
, dir
);
4009 bpf_error("bad protocol applied for 'protochain'");
4014 * We don't handle variable-length radiotap here headers yet.
4015 * We might want to add BPF instructions to do the protochain
4016 * work, to simplify that and, on platforms that have a BPF
4017 * interpreter with the new instructions, let the filtering
4018 * be done in the kernel. (We already require a modified BPF
4019 * engine to do the protochain stuff, to support backward
4020 * branches, and backward branch support is unlikely to appear
4021 * in kernel BPF engines.)
4023 if (linktype
== DLT_IEEE802_11_RADIO
)
4024 bpf_error("'protochain' not supported with radiotap headers");
4026 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4029 * s[0] is a dummy entry to protect other BPF insn from damage
4030 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4031 * hard to find interdependency made by jump table fixup.
4034 s
[i
] = new_stmt(0); /*dummy*/
4039 b0
= gen_linktype(ETHERTYPE_IP
);
4042 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4043 s
[i
]->s
.k
= off_nl
+ 9;
4045 /* X = ip->ip_hl << 2 */
4046 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4052 b0
= gen_linktype(ETHERTYPE_IPV6
);
4054 /* A = ip6->ip_nxt */
4055 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4056 s
[i
]->s
.k
= off_nl
+ 6;
4058 /* X = sizeof(struct ip6_hdr) */
4059 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4065 bpf_error("unsupported proto to gen_protochain");
4069 /* again: if (A == v) goto end; else fall through; */
4071 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4073 s
[i
]->s
.jt
= NULL
; /*later*/
4074 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4078 #ifndef IPPROTO_NONE
4079 #define IPPROTO_NONE 59
4081 /* if (A == IPPROTO_NONE) goto end */
4082 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4083 s
[i
]->s
.jt
= NULL
; /*later*/
4084 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4085 s
[i
]->s
.k
= IPPROTO_NONE
;
4086 s
[fix5
]->s
.jf
= s
[i
];
4091 if (proto
== Q_IPV6
) {
4092 int v6start
, v6end
, v6advance
, j
;
4095 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4096 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4097 s
[i
]->s
.jt
= NULL
; /*later*/
4098 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4099 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4100 s
[fix2
]->s
.jf
= s
[i
];
4102 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4103 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4104 s
[i
]->s
.jt
= NULL
; /*later*/
4105 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4106 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4108 /* if (A == IPPROTO_ROUTING) goto v6advance */
4109 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4110 s
[i
]->s
.jt
= NULL
; /*later*/
4111 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4112 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4114 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4115 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4116 s
[i
]->s
.jt
= NULL
; /*later*/
4117 s
[i
]->s
.jf
= NULL
; /*later*/
4118 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4129 * X = X + (P[X + 1] + 1) * 8;
4132 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4134 /* A = P[X + packet head] */
4135 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4139 s
[i
] = new_stmt(BPF_ST
);
4143 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4146 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4150 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4152 /* A = P[X + packet head]; */
4153 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4157 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4161 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4165 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4168 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4172 /* goto again; (must use BPF_JA for backward jump) */
4173 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4174 s
[i
]->s
.k
= again
- i
- 1;
4175 s
[i
- 1]->s
.jf
= s
[i
];
4179 for (j
= v6start
; j
<= v6end
; j
++)
4180 s
[j
]->s
.jt
= s
[v6advance
];
4185 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4187 s
[fix2
]->s
.jf
= s
[i
];
4193 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4194 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4195 s
[i
]->s
.jt
= NULL
; /*later*/
4196 s
[i
]->s
.jf
= NULL
; /*later*/
4197 s
[i
]->s
.k
= IPPROTO_AH
;
4199 s
[fix3
]->s
.jf
= s
[ahcheck
];
4206 * X = X + (P[X + 1] + 2) * 4;
4209 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4211 /* A = P[X + packet head]; */
4212 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4216 s
[i
] = new_stmt(BPF_ST
);
4220 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4223 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4227 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4229 /* A = P[X + packet head] */
4230 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4234 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4238 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4242 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4245 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4249 /* goto again; (must use BPF_JA for backward jump) */
4250 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4251 s
[i
]->s
.k
= again
- i
- 1;
4256 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4258 s
[fix2
]->s
.jt
= s
[end
];
4259 s
[fix4
]->s
.jf
= s
[end
];
4260 s
[fix5
]->s
.jt
= s
[end
];
4267 for (i
= 0; i
< max
- 1; i
++)
4268 s
[i
]->next
= s
[i
+ 1];
4269 s
[max
- 1]->next
= NULL
;
4274 b
= new_block(JMP(BPF_JEQ
));
4275 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4286 * Generate code that checks whether the packet is a packet for protocol
4287 * <proto> and whether the type field in that protocol's header has
4288 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4289 * IP packet and checks the protocol number in the IP header against <v>.
4291 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4292 * against Q_IP and Q_IPV6.
4294 static struct block
*
4295 gen_proto(v
, proto
, dir
)
4300 struct block
*b0
, *b1
;
4302 if (dir
!= Q_DEFAULT
)
4303 bpf_error("direction applied to 'proto'");
4308 b0
= gen_proto(v
, Q_IP
, dir
);
4309 b1
= gen_proto(v
, Q_IPV6
, dir
);
4317 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4318 * not LLC encapsulation with LLCSAP_IP.
4320 * For IEEE 802 networks - which includes 802.5 token ring
4321 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4322 * says that SNAP encapsulation is used, not LLC encapsulation
4325 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4326 * RFC 2225 say that SNAP encapsulation is used, not LLC
4327 * encapsulation with LLCSAP_IP.
4329 * So we always check for ETHERTYPE_IP.
4331 b0
= gen_linktype(ETHERTYPE_IP
);
4333 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4335 b1
= gen_protochain(v
, Q_IP
);
4345 * Frame Relay packets typically have an OSI
4346 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4347 * generates code to check for all the OSI
4348 * NLPIDs, so calling it and then adding a check
4349 * for the particular NLPID for which we're
4350 * looking is bogus, as we can just check for
4353 * What we check for is the NLPID and a frame
4354 * control field value of UI, i.e. 0x03 followed
4357 * XXX - assumes a 2-byte Frame Relay header with
4358 * DLCI and flags. What if the address is longer?
4360 * XXX - what about SNAP-encapsulated frames?
4362 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4368 * Cisco uses an Ethertype lookalike - for OSI,
4371 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4372 /* OSI in C-HDLC is stuffed with a fudge byte */
4373 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4378 b0
= gen_linktype(LLCSAP_ISONS
);
4379 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4385 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4387 * 4 is the offset of the PDU type relative to the IS-IS
4390 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4395 bpf_error("arp does not encapsulate another protocol");
4399 bpf_error("rarp does not encapsulate another protocol");
4403 bpf_error("atalk encapsulation is not specifiable");
4407 bpf_error("decnet encapsulation is not specifiable");
4411 bpf_error("sca does not encapsulate another protocol");
4415 bpf_error("lat does not encapsulate another protocol");
4419 bpf_error("moprc does not encapsulate another protocol");
4423 bpf_error("mopdl does not encapsulate another protocol");
4427 return gen_linktype(v
);
4430 bpf_error("'udp proto' is bogus");
4434 bpf_error("'tcp proto' is bogus");
4438 bpf_error("'sctp proto' is bogus");
4442 bpf_error("'icmp proto' is bogus");
4446 bpf_error("'igmp proto' is bogus");
4450 bpf_error("'igrp proto' is bogus");
4454 bpf_error("'pim proto' is bogus");
4458 bpf_error("'vrrp proto' is bogus");
4463 b0
= gen_linktype(ETHERTYPE_IPV6
);
4465 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4467 b1
= gen_protochain(v
, Q_IPV6
);
4473 bpf_error("'icmp6 proto' is bogus");
4477 bpf_error("'ah proto' is bogus");
4480 bpf_error("'ah proto' is bogus");
4483 bpf_error("'stp proto' is bogus");
4486 bpf_error("'ipx proto' is bogus");
4489 bpf_error("'netbeui proto' is bogus");
4492 bpf_error("'radio proto' is bogus");
4503 register const char *name
;
4506 int proto
= q
.proto
;
4510 bpf_u_int32 mask
, addr
;
4512 bpf_u_int32
**alist
;
4515 struct sockaddr_in
*sin
;
4516 struct sockaddr_in6
*sin6
;
4517 struct addrinfo
*res
, *res0
;
4518 struct in6_addr mask128
;
4520 struct block
*b
, *tmp
;
4521 int port
, real_proto
;
4527 addr
= pcap_nametonetaddr(name
);
4529 bpf_error("unknown network '%s'", name
);
4530 /* Left justify network addr and calculate its network mask */
4532 while (addr
&& (addr
& 0xff000000) == 0) {
4536 return gen_host(addr
, mask
, proto
, dir
);
4540 if (proto
== Q_LINK
) {
4544 eaddr
= pcap_ether_hostton(name
);
4547 "unknown ether host '%s'", name
);
4548 b
= gen_ehostop(eaddr
, dir
);
4553 eaddr
= pcap_ether_hostton(name
);
4556 "unknown FDDI host '%s'", name
);
4557 b
= gen_fhostop(eaddr
, dir
);
4562 eaddr
= pcap_ether_hostton(name
);
4565 "unknown token ring host '%s'", name
);
4566 b
= gen_thostop(eaddr
, dir
);
4570 case DLT_IEEE802_11
:
4571 case DLT_IEEE802_11_RADIO_AVS
:
4572 case DLT_IEEE802_11_RADIO
:
4573 case DLT_PRISM_HEADER
:
4574 eaddr
= pcap_ether_hostton(name
);
4577 "unknown 802.11 host '%s'", name
);
4578 b
= gen_wlanhostop(eaddr
, dir
);
4582 case DLT_IP_OVER_FC
:
4583 eaddr
= pcap_ether_hostton(name
);
4586 "unknown Fibre Channel host '%s'", name
);
4587 b
= gen_ipfchostop(eaddr
, dir
);
4596 * Check that the packet doesn't begin
4597 * with an LE Control marker. (We've
4598 * already generated a test for LANE.)
4600 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4604 eaddr
= pcap_ether_hostton(name
);
4607 "unknown ether host '%s'", name
);
4608 b
= gen_ehostop(eaddr
, dir
);
4614 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4615 } else if (proto
== Q_DECNET
) {
4616 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4618 * I don't think DECNET hosts can be multihomed, so
4619 * there is no need to build up a list of addresses
4621 return (gen_host(dn_addr
, 0, proto
, dir
));
4624 alist
= pcap_nametoaddr(name
);
4625 if (alist
== NULL
|| *alist
== NULL
)
4626 bpf_error("unknown host '%s'", name
);
4628 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4630 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4632 tmp
= gen_host(**alist
++, 0xffffffff,
4639 memset(&mask128
, 0xff, sizeof(mask128
));
4640 res0
= res
= pcap_nametoaddrinfo(name
);
4642 bpf_error("unknown host '%s'", name
);
4644 tproto
= tproto6
= proto
;
4645 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4649 for (res
= res0
; res
; res
= res
->ai_next
) {
4650 switch (res
->ai_family
) {
4652 if (tproto
== Q_IPV6
)
4655 sin
= (struct sockaddr_in
*)
4657 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4658 0xffffffff, tproto
, dir
);
4661 if (tproto6
== Q_IP
)
4664 sin6
= (struct sockaddr_in6
*)
4666 tmp
= gen_host6(&sin6
->sin6_addr
,
4667 &mask128
, tproto6
, dir
);
4678 bpf_error("unknown host '%s'%s", name
,
4679 (proto
== Q_DEFAULT
)
4681 : " for specified address family");
4688 if (proto
!= Q_DEFAULT
&&
4689 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4690 bpf_error("illegal qualifier of 'port'");
4691 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4692 bpf_error("unknown port '%s'", name
);
4693 if (proto
== Q_UDP
) {
4694 if (real_proto
== IPPROTO_TCP
)
4695 bpf_error("port '%s' is tcp", name
);
4696 else if (real_proto
== IPPROTO_SCTP
)
4697 bpf_error("port '%s' is sctp", name
);
4699 /* override PROTO_UNDEF */
4700 real_proto
= IPPROTO_UDP
;
4702 if (proto
== Q_TCP
) {
4703 if (real_proto
== IPPROTO_UDP
)
4704 bpf_error("port '%s' is udp", name
);
4706 else if (real_proto
== IPPROTO_SCTP
)
4707 bpf_error("port '%s' is sctp", name
);
4709 /* override PROTO_UNDEF */
4710 real_proto
= IPPROTO_TCP
;
4712 if (proto
== Q_SCTP
) {
4713 if (real_proto
== IPPROTO_UDP
)
4714 bpf_error("port '%s' is udp", name
);
4716 else if (real_proto
== IPPROTO_TCP
)
4717 bpf_error("port '%s' is tcp", name
);
4719 /* override PROTO_UNDEF */
4720 real_proto
= IPPROTO_SCTP
;
4723 return gen_port(port
, real_proto
, dir
);
4727 b
= gen_port(port
, real_proto
, dir
);
4728 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4734 if (proto
!= Q_DEFAULT
&&
4735 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4736 bpf_error("illegal qualifier of 'portrange'");
4737 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4738 bpf_error("unknown port in range '%s'", name
);
4739 if (proto
== Q_UDP
) {
4740 if (real_proto
== IPPROTO_TCP
)
4741 bpf_error("port in range '%s' is tcp", name
);
4742 else if (real_proto
== IPPROTO_SCTP
)
4743 bpf_error("port in range '%s' is sctp", name
);
4745 /* override PROTO_UNDEF */
4746 real_proto
= IPPROTO_UDP
;
4748 if (proto
== Q_TCP
) {
4749 if (real_proto
== IPPROTO_UDP
)
4750 bpf_error("port in range '%s' is udp", name
);
4751 else if (real_proto
== IPPROTO_SCTP
)
4752 bpf_error("port in range '%s' is sctp", name
);
4754 /* override PROTO_UNDEF */
4755 real_proto
= IPPROTO_TCP
;
4757 if (proto
== Q_SCTP
) {
4758 if (real_proto
== IPPROTO_UDP
)
4759 bpf_error("port in range '%s' is udp", name
);
4760 else if (real_proto
== IPPROTO_TCP
)
4761 bpf_error("port in range '%s' is tcp", name
);
4763 /* override PROTO_UNDEF */
4764 real_proto
= IPPROTO_SCTP
;
4767 return gen_portrange(port1
, port2
, real_proto
, dir
);
4771 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4772 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4779 eaddr
= pcap_ether_hostton(name
);
4781 bpf_error("unknown ether host: %s", name
);
4783 alist
= pcap_nametoaddr(name
);
4784 if (alist
== NULL
|| *alist
== NULL
)
4785 bpf_error("unknown host '%s'", name
);
4786 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4790 bpf_error("'gateway' not supported in this configuration");
4794 real_proto
= lookup_proto(name
, proto
);
4795 if (real_proto
>= 0)
4796 return gen_proto(real_proto
, proto
, dir
);
4798 bpf_error("unknown protocol: %s", name
);
4801 real_proto
= lookup_proto(name
, proto
);
4802 if (real_proto
>= 0)
4803 return gen_protochain(real_proto
, proto
, dir
);
4805 bpf_error("unknown protocol: %s", name
);
4817 gen_mcode(s1
, s2
, masklen
, q
)
4818 register const char *s1
, *s2
;
4819 register int masklen
;
4822 register int nlen
, mlen
;
4825 nlen
= __pcap_atoin(s1
, &n
);
4826 /* Promote short ipaddr */
4830 mlen
= __pcap_atoin(s2
, &m
);
4831 /* Promote short ipaddr */
4834 bpf_error("non-network bits set in \"%s mask %s\"",
4837 /* Convert mask len to mask */
4839 bpf_error("mask length must be <= 32");
4840 m
= 0xffffffff << (32 - masklen
);
4842 bpf_error("non-network bits set in \"%s/%d\"",
4849 return gen_host(n
, m
, q
.proto
, q
.dir
);
4852 bpf_error("Mask syntax for networks only");
4860 register const char *s
;
4865 int proto
= q
.proto
;
4871 else if (q
.proto
== Q_DECNET
)
4872 vlen
= __pcap_atodn(s
, &v
);
4874 vlen
= __pcap_atoin(s
, &v
);
4881 if (proto
== Q_DECNET
)
4882 return gen_host(v
, 0, proto
, dir
);
4883 else if (proto
== Q_LINK
) {
4884 bpf_error("illegal link layer address");
4887 if (s
== NULL
&& q
.addr
== Q_NET
) {
4888 /* Promote short net number */
4889 while (v
&& (v
& 0xff000000) == 0) {
4894 /* Promote short ipaddr */
4898 return gen_host(v
, mask
, proto
, dir
);
4903 proto
= IPPROTO_UDP
;
4904 else if (proto
== Q_TCP
)
4905 proto
= IPPROTO_TCP
;
4906 else if (proto
== Q_SCTP
)
4907 proto
= IPPROTO_SCTP
;
4908 else if (proto
== Q_DEFAULT
)
4909 proto
= PROTO_UNDEF
;
4911 bpf_error("illegal qualifier of 'port'");
4914 return gen_port((int)v
, proto
, dir
);
4918 b
= gen_port((int)v
, proto
, dir
);
4919 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4926 proto
= IPPROTO_UDP
;
4927 else if (proto
== Q_TCP
)
4928 proto
= IPPROTO_TCP
;
4929 else if (proto
== Q_SCTP
)
4930 proto
= IPPROTO_SCTP
;
4931 else if (proto
== Q_DEFAULT
)
4932 proto
= PROTO_UNDEF
;
4934 bpf_error("illegal qualifier of 'portrange'");
4937 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4941 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4942 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4948 bpf_error("'gateway' requires a name");
4952 return gen_proto((int)v
, proto
, dir
);
4955 return gen_protochain((int)v
, proto
, dir
);
4970 gen_mcode6(s1
, s2
, masklen
, q
)
4971 register const char *s1
, *s2
;
4972 register int masklen
;
4975 struct addrinfo
*res
;
4976 struct in6_addr
*addr
;
4977 struct in6_addr mask
;
4982 bpf_error("no mask %s supported", s2
);
4984 res
= pcap_nametoaddrinfo(s1
);
4986 bpf_error("invalid ip6 address %s", s1
);
4988 bpf_error("%s resolved to multiple address", s1
);
4989 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4991 if (sizeof(mask
) * 8 < masklen
)
4992 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4993 memset(&mask
, 0, sizeof(mask
));
4994 memset(&mask
, 0xff, masklen
/ 8);
4996 mask
.s6_addr
[masklen
/ 8] =
4997 (0xff << (8 - masklen
% 8)) & 0xff;
5000 a
= (u_int32_t
*)addr
;
5001 m
= (u_int32_t
*)&mask
;
5002 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5003 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5004 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5012 bpf_error("Mask syntax for networks only");
5016 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
5021 bpf_error("invalid qualifier against IPv6 address");
5029 register const u_char
*eaddr
;
5032 struct block
*b
, *tmp
;
5034 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5035 if (linktype
== DLT_EN10MB
)
5036 return gen_ehostop(eaddr
, (int)q
.dir
);
5037 if (linktype
== DLT_FDDI
)
5038 return gen_fhostop(eaddr
, (int)q
.dir
);
5039 if (linktype
== DLT_IEEE802
)
5040 return gen_thostop(eaddr
, (int)q
.dir
);
5041 if (linktype
== DLT_IEEE802_11
||
5042 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5043 linktype
== DLT_IEEE802_11_RADIO
||
5044 linktype
== DLT_PRISM_HEADER
)
5045 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5046 if (linktype
== DLT_SUNATM
&& is_lane
) {
5048 * Check that the packet doesn't begin with an
5049 * LE Control marker. (We've already generated
5052 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5057 * Now check the MAC address.
5059 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5063 if (linktype
== DLT_IP_OVER_FC
)
5064 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5065 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5067 bpf_error("ethernet address used in non-ether expression");
5073 struct slist
*s0
, *s1
;
5076 * This is definitely not the best way to do this, but the
5077 * lists will rarely get long.
5084 static struct slist
*
5090 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5095 static struct slist
*
5101 s
= new_stmt(BPF_LD
|BPF_MEM
);
5107 * Modify "index" to use the value stored into its register as an
5108 * offset relative to the beginning of the header for the protocol
5109 * "proto", and allocate a register and put an item "size" bytes long
5110 * (1, 2, or 4) at that offset into that register, making it the register
5114 gen_load(proto
, index
, size
)
5119 struct slist
*s
, *tmp
;
5121 int regno
= alloc_reg();
5123 free_reg(index
->regno
);
5127 bpf_error("data size must be 1, 2, or 4");
5143 bpf_error("unsupported index operation");
5147 * The offset is relative to the beginning of the packet
5148 * data, if we have a radio header. (If we don't, this
5151 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5152 linktype
!= DLT_IEEE802_11_RADIO
&&
5153 linktype
!= DLT_PRISM_HEADER
)
5154 bpf_error("radio information not present in capture");
5157 * Load into the X register the offset computed into the
5158 * register specifed by "index".
5160 s
= xfer_to_x(index
);
5163 * Load the item at that offset.
5165 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5167 sappend(index
->s
, s
);
5172 * The offset is relative to the beginning of
5173 * the link-layer header.
5175 * XXX - what about ATM LANE? Should the index be
5176 * relative to the beginning of the AAL5 frame, so
5177 * that 0 refers to the beginning of the LE Control
5178 * field, or relative to the beginning of the LAN
5179 * frame, so that 0 refers, for Ethernet LANE, to
5180 * the beginning of the destination address?
5182 s
= gen_llprefixlen();
5185 * If "s" is non-null, it has code to arrange that the
5186 * X register contains the length of the prefix preceding
5187 * the link-layer header. Add to it the offset computed
5188 * into the register specified by "index", and move that
5189 * into the X register. Otherwise, just load into the X
5190 * register the offset computed into the register specifed
5194 sappend(s
, xfer_to_a(index
));
5195 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5196 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5198 s
= xfer_to_x(index
);
5201 * Load the item at the sum of the offset we've put in the
5202 * X register and the offset of the start of the link
5203 * layer header (which is 0 if the radio header is
5204 * variable-length; that header length is what we put
5205 * into the X register and then added to the index).
5207 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5210 sappend(index
->s
, s
);
5226 * The offset is relative to the beginning of
5227 * the network-layer header.
5228 * XXX - are there any cases where we want
5231 s
= gen_llprefixlen();
5234 * If "s" is non-null, it has code to arrange that the
5235 * X register contains the length of the prefix preceding
5236 * the link-layer header. Add to it the offset computed
5237 * into the register specified by "index", and move that
5238 * into the X register. Otherwise, just load into the X
5239 * register the offset computed into the register specifed
5243 sappend(s
, xfer_to_a(index
));
5244 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5245 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5247 s
= xfer_to_x(index
);
5250 * Load the item at the sum of the offset we've put in the
5251 * X register and the offset of the start of the network
5254 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5257 sappend(index
->s
, s
);
5260 * Do the computation only if the packet contains
5261 * the protocol in question.
5263 b
= gen_proto_abbrev(proto
);
5265 gen_and(index
->b
, b
);
5278 * The offset is relative to the beginning of
5279 * the transport-layer header.
5280 * XXX - are there any cases where we want
5282 * XXX - we should, if we're built with
5283 * IPv6 support, generate code to load either
5284 * IPv4, IPv6, or both, as appropriate.
5286 s
= gen_loadx_iphdrlen();
5289 * The X register now contains the sum of the offset
5290 * of the beginning of the link-layer header and
5291 * the length of the network-layer header. Load
5292 * into the A register the offset relative to
5293 * the beginning of the transport layer header,
5294 * add the X register to that, move that to the
5295 * X register, and load with an offset from the
5296 * X register equal to the offset of the network
5297 * layer header relative to the beginning of
5298 * the link-layer header.
5300 sappend(s
, xfer_to_a(index
));
5301 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5302 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5303 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5305 sappend(index
->s
, s
);
5308 * Do the computation only if the packet contains
5309 * the protocol in question - which is true only
5310 * if this is an IP datagram and is the first or
5311 * only fragment of that datagram.
5313 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5315 gen_and(index
->b
, b
);
5317 gen_and(gen_proto_abbrev(Q_IP
), b
);
5323 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5327 index
->regno
= regno
;
5328 s
= new_stmt(BPF_ST
);
5330 sappend(index
->s
, s
);
5336 gen_relation(code
, a0
, a1
, reversed
)
5338 struct arth
*a0
, *a1
;
5341 struct slist
*s0
, *s1
, *s2
;
5342 struct block
*b
, *tmp
;
5346 if (code
== BPF_JEQ
) {
5347 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5348 b
= new_block(JMP(code
));
5352 b
= new_block(BPF_JMP
|code
|BPF_X
);
5358 sappend(a0
->s
, a1
->s
);
5362 free_reg(a0
->regno
);
5363 free_reg(a1
->regno
);
5365 /* 'and' together protocol checks */
5368 gen_and(a0
->b
, tmp
= a1
->b
);
5384 int regno
= alloc_reg();
5385 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5388 s
= new_stmt(BPF_LD
|BPF_LEN
);
5389 s
->next
= new_stmt(BPF_ST
);
5390 s
->next
->s
.k
= regno
;
5405 a
= (struct arth
*)newchunk(sizeof(*a
));
5409 s
= new_stmt(BPF_LD
|BPF_IMM
);
5411 s
->next
= new_stmt(BPF_ST
);
5427 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5430 s
= new_stmt(BPF_ST
);
5438 gen_arth(code
, a0
, a1
)
5440 struct arth
*a0
, *a1
;
5442 struct slist
*s0
, *s1
, *s2
;
5446 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5451 sappend(a0
->s
, a1
->s
);
5453 free_reg(a0
->regno
);
5454 free_reg(a1
->regno
);
5456 s0
= new_stmt(BPF_ST
);
5457 a0
->regno
= s0
->s
.k
= alloc_reg();
5464 * Here we handle simple allocation of the scratch registers.
5465 * If too many registers are alloc'd, the allocator punts.
5467 static int regused
[BPF_MEMWORDS
];
5471 * Return the next free register.
5476 int n
= BPF_MEMWORDS
;
5479 if (regused
[curreg
])
5480 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5482 regused
[curreg
] = 1;
5486 bpf_error("too many registers needed to evaluate expression");
5491 * Return a register to the table so it can
5501 static struct block
*
5508 s
= new_stmt(BPF_LD
|BPF_LEN
);
5509 b
= new_block(JMP(jmp
));
5520 return gen_len(BPF_JGE
, n
);
5524 * Actually, this is less than or equal.
5532 b
= gen_len(BPF_JGT
, n
);
5539 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5540 * the beginning of the link-layer header.
5541 * XXX - that means you can't test values in the radiotap header, but
5542 * as that header is difficult if not impossible to parse generally
5543 * without a loop, that might not be a severe problem. A new keyword
5544 * "radio" could be added for that, although what you'd really want
5545 * would be a way of testing particular radio header values, which
5546 * would generate code appropriate to the radio header in question.
5549 gen_byteop(op
, idx
, val
)
5560 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5563 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5567 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5571 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5575 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5579 b
= new_block(JMP(BPF_JEQ
));
5586 static u_char abroadcast
[] = { 0x0 };
5589 gen_broadcast(proto
)
5592 bpf_u_int32 hostmask
;
5593 struct block
*b0
, *b1
, *b2
;
5594 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5600 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5601 return gen_ahostop(abroadcast
, Q_DST
);
5602 if (linktype
== DLT_EN10MB
)
5603 return gen_ehostop(ebroadcast
, Q_DST
);
5604 if (linktype
== DLT_FDDI
)
5605 return gen_fhostop(ebroadcast
, Q_DST
);
5606 if (linktype
== DLT_IEEE802
)
5607 return gen_thostop(ebroadcast
, Q_DST
);
5608 if (linktype
== DLT_IEEE802_11
||
5609 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5610 linktype
== DLT_IEEE802_11_RADIO
||
5611 linktype
== DLT_PRISM_HEADER
)
5612 return gen_wlanhostop(ebroadcast
, Q_DST
);
5613 if (linktype
== DLT_IP_OVER_FC
)
5614 return gen_ipfchostop(ebroadcast
, Q_DST
);
5615 if (linktype
== DLT_SUNATM
&& is_lane
) {
5617 * Check that the packet doesn't begin with an
5618 * LE Control marker. (We've already generated
5621 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5626 * Now check the MAC address.
5628 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5632 bpf_error("not a broadcast link");
5636 b0
= gen_linktype(ETHERTYPE_IP
);
5637 hostmask
= ~netmask
;
5638 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5639 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5640 (bpf_int32
)(~0 & hostmask
), hostmask
);
5645 bpf_error("only link-layer/IP broadcast filters supported");
5650 * Generate code to test the low-order bit of a MAC address (that's
5651 * the bottom bit of the *first* byte).
5653 static struct block
*
5654 gen_mac_multicast(offset
)
5657 register struct block
*b0
;
5658 register struct slist
*s
;
5660 /* link[offset] & 1 != 0 */
5661 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5662 b0
= new_block(JMP(BPF_JSET
));
5669 gen_multicast(proto
)
5672 register struct block
*b0
, *b1
, *b2
;
5673 register struct slist
*s
;
5679 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5680 /* all ARCnet multicasts use the same address */
5681 return gen_ahostop(abroadcast
, Q_DST
);
5683 if (linktype
== DLT_EN10MB
) {
5684 /* ether[0] & 1 != 0 */
5685 return gen_mac_multicast(0);
5688 if (linktype
== DLT_FDDI
) {
5690 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5692 * XXX - was that referring to bit-order issues?
5694 /* fddi[1] & 1 != 0 */
5695 return gen_mac_multicast(1);
5698 if (linktype
== DLT_IEEE802
) {
5699 /* tr[2] & 1 != 0 */
5700 return gen_mac_multicast(2);
5703 if (linktype
== DLT_IEEE802_11
||
5704 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5705 linktype
== DLT_IEEE802_11_RADIO
||
5706 linktype
== DLT_PRISM_HEADER
) {
5710 * For control frames, there is no DA.
5712 * For management frames, DA is at an
5713 * offset of 4 from the beginning of
5716 * For data frames, DA is at an offset
5717 * of 4 from the beginning of the packet
5718 * if To DS is clear and at an offset of
5719 * 16 from the beginning of the packet
5724 * Generate the tests to be done for data frames.
5726 * First, check for To DS set, i.e. "link[1] & 0x01".
5728 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5729 b1
= new_block(JMP(BPF_JSET
));
5730 b1
->s
.k
= 0x01; /* To DS */
5734 * If To DS is set, the DA is at 16.
5736 b0
= gen_mac_multicast(16);
5740 * Now, check for To DS not set, i.e. check
5741 * "!(link[1] & 0x01)".
5743 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5744 b2
= new_block(JMP(BPF_JSET
));
5745 b2
->s
.k
= 0x01; /* To DS */
5750 * If To DS is not set, the DA is at 4.
5752 b1
= gen_mac_multicast(4);
5756 * Now OR together the last two checks. That gives
5757 * the complete set of checks for data frames.
5762 * Now check for a data frame.
5763 * I.e, check "link[0] & 0x08".
5765 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5766 b1
= new_block(JMP(BPF_JSET
));
5771 * AND that with the checks done for data frames.
5776 * If the high-order bit of the type value is 0, this
5777 * is a management frame.
5778 * I.e, check "!(link[0] & 0x08)".
5780 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5781 b2
= new_block(JMP(BPF_JSET
));
5787 * For management frames, the DA is at 4.
5789 b1
= gen_mac_multicast(4);
5793 * OR that with the checks done for data frames.
5794 * That gives the checks done for management and
5800 * If the low-order bit of the type value is 1,
5801 * this is either a control frame or a frame
5802 * with a reserved type, and thus not a
5805 * I.e., check "!(link[0] & 0x04)".
5807 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5808 b1
= new_block(JMP(BPF_JSET
));
5814 * AND that with the checks for data and management
5821 if (linktype
== DLT_IP_OVER_FC
) {
5822 b0
= gen_mac_multicast(2);
5826 if (linktype
== DLT_SUNATM
&& is_lane
) {
5828 * Check that the packet doesn't begin with an
5829 * LE Control marker. (We've already generated
5832 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5836 /* ether[off_mac] & 1 != 0 */
5837 b0
= gen_mac_multicast(off_mac
);
5842 /* Link not known to support multicasts */
5846 b0
= gen_linktype(ETHERTYPE_IP
);
5847 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5853 b0
= gen_linktype(ETHERTYPE_IPV6
);
5854 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5859 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5864 * generate command for inbound/outbound. It's here so we can
5865 * make it link-type specific. 'dir' = 0 implies "inbound",
5866 * = 1 implies "outbound".
5872 register struct block
*b0
;
5875 * Only some data link types support inbound/outbound qualifiers.
5879 b0
= gen_relation(BPF_JEQ
,
5880 gen_load(Q_LINK
, gen_loadi(0), 1),
5888 * Match packets sent by this machine.
5890 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
5893 * Match packets sent to this machine.
5894 * (No broadcast or multicast packets, or
5895 * packets sent to some other machine and
5896 * received promiscuously.)
5898 * XXX - packets sent to other machines probably
5899 * shouldn't be matched, but what about broadcast
5900 * or multicast packets we received?
5902 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
5907 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
5908 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5913 /* match outgoing packets */
5914 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
5916 /* match incoming packets */
5917 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
5921 case DLT_JUNIPER_MLFR
:
5922 case DLT_JUNIPER_MLPPP
:
5923 case DLT_JUNIPER_ATM1
:
5924 case DLT_JUNIPER_ATM2
:
5925 case DLT_JUNIPER_PPPOE
:
5926 /* juniper flags (including direction) are stored
5927 * the byte after the 3-byte magic number */
5929 /* match outgoing packets */
5930 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
5932 /* match incoming packets */
5933 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
5938 bpf_error("inbound/outbound not supported on linktype %d",
5946 /* PF firewall log matched interface */
5948 gen_pf_ifname(const char *ifname
)
5953 if (linktype
== DLT_PFLOG
) {
5954 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5955 off
= offsetof(struct pfloghdr
, ifname
);
5957 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5960 if (strlen(ifname
) >= len
) {
5961 bpf_error("ifname interface names can only be %d characters",
5965 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
5969 /* PF firewall log matched interface */
5971 gen_pf_ruleset(char *ruleset
)
5975 if (linktype
!= DLT_PFLOG
) {
5976 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5979 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5980 bpf_error("ruleset names can only be %ld characters",
5981 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5984 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
5985 strlen(ruleset
), (const u_char
*)ruleset
);
5989 /* PF firewall log rule number */
5995 if (linktype
== DLT_PFLOG
) {
5996 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5999 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6006 /* PF firewall log sub-rule number */
6008 gen_pf_srnr(int srnr
)
6012 if (linktype
!= DLT_PFLOG
) {
6013 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6017 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6022 /* PF firewall log reason code */
6024 gen_pf_reason(int reason
)
6028 if (linktype
== DLT_PFLOG
) {
6029 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6032 bpf_error("reason not supported on linktype 0x%x", linktype
);
6039 /* PF firewall log action */
6041 gen_pf_action(int action
)
6045 if (linktype
== DLT_PFLOG
) {
6046 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6049 bpf_error("action not supported on linktype 0x%x", linktype
);
6058 register const u_char
*eaddr
;
6061 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6062 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6063 return gen_ahostop(eaddr
, (int)q
.dir
);
6065 bpf_error("ARCnet address used in non-arc expression");
6069 static struct block
*
6070 gen_ahostop(eaddr
, dir
)
6071 register const u_char
*eaddr
;
6074 register struct block
*b0
, *b1
;
6077 /* src comes first, different from Ethernet */
6079 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6082 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6085 b0
= gen_ahostop(eaddr
, Q_SRC
);
6086 b1
= gen_ahostop(eaddr
, Q_DST
);
6092 b0
= gen_ahostop(eaddr
, Q_SRC
);
6093 b1
= gen_ahostop(eaddr
, Q_DST
);
6102 * support IEEE 802.1Q VLAN trunk over ethernet
6111 * Change the offsets to point to the type and data fields within
6112 * the VLAN packet. Just increment the offsets, so that we
6113 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6114 * capture VLAN 200 encapsulated within VLAN 100.
6116 * XXX - this is a bit of a kludge. If we were to split the
6117 * compiler into a parser that parses an expression and
6118 * generates an expression tree, and a code generator that
6119 * takes an expression tree (which could come from our
6120 * parser or from some other parser) and generates BPF code,
6121 * we could perhaps make the offsets parameters of routines
6122 * and, in the handler for an "AND" node, pass to subnodes
6123 * other than the VLAN node the adjusted offsets.
6125 * This would mean that "vlan" would, instead of changing the
6126 * behavior of *all* tests after it, change only the behavior
6127 * of tests ANDed with it. That would change the documented
6128 * semantics of "vlan", which might break some expressions.
6129 * However, it would mean that "(vlan and ip) or ip" would check
6130 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6131 * checking only for VLAN-encapsulated IP, so that could still
6132 * be considered worth doing; it wouldn't break expressions
6133 * that are of the form "vlan and ..." or "vlan N and ...",
6134 * which I suspect are the most common expressions involving
6135 * "vlan". "vlan or ..." doesn't necessarily do what the user
6136 * would really want, now, as all the "or ..." tests would
6137 * be done assuming a VLAN, even though the "or" could be viewed
6138 * as meaning "or, if this isn't a VLAN packet...".
6140 orig_linktype
= off_linktype
; /* save original values */
6152 bpf_error("no VLAN support for data link type %d",
6157 /* check for VLAN */
6158 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6160 /* If a specific VLAN is requested, check VLAN id */
6161 if (vlan_num
>= 0) {
6164 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6183 * Change the offsets to point to the type and data fields within
6184 * the MPLS packet. Just increment the offsets, so that we
6185 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6186 * capture packets with an outer label of 100000 and an inner
6189 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6191 orig_linktype
= off_linktype
; /* save original values */
6196 case DLT_C_HDLC
: /* fall through */
6202 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6203 (bpf_int32
)ETHERTYPE_MPLS
);
6211 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6212 (bpf_int32
)PPP_MPLS_UCAST
);
6215 /* FIXME add other DLT_s ...
6216 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6217 * leave it for now */
6220 bpf_error("no MPLS support for data link type %d",
6227 /* If a specific MPLS label is requested, check it */
6228 if (label_num
>= 0) {
6231 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6232 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6233 0xfffff000); /* only compare the first 20 bits */
6242 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6254 bpf_error("'vpi' supported only on raw ATM");
6255 if (off_vpi
== (u_int
)-1)
6257 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6263 bpf_error("'vci' supported only on raw ATM");
6264 if (off_vci
== (u_int
)-1)
6266 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6271 if (off_proto
== (u_int
)-1)
6272 abort(); /* XXX - this isn't on FreeBSD */
6273 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6278 if (off_payload
== (u_int
)-1)
6280 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6281 0xffffffff, jtype
, reverse
, jvalue
);
6286 bpf_error("'callref' supported only on raw ATM");
6287 if (off_proto
== (u_int
)-1)
6289 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6290 jtype
, reverse
, jvalue
);
6300 gen_atmtype_abbrev(type
)
6303 struct block
*b0
, *b1
;
6308 /* Get all packets in Meta signalling Circuit */
6310 bpf_error("'metac' supported only on raw ATM");
6311 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6312 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6317 /* Get all packets in Broadcast Circuit*/
6319 bpf_error("'bcc' supported only on raw ATM");
6320 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6321 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6326 /* Get all cells in Segment OAM F4 circuit*/
6328 bpf_error("'oam4sc' supported only on raw ATM");
6329 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6330 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6335 /* Get all cells in End-to-End OAM F4 Circuit*/
6337 bpf_error("'oam4ec' supported only on raw ATM");
6338 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6339 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6344 /* Get all packets in connection Signalling Circuit */
6346 bpf_error("'sc' supported only on raw ATM");
6347 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6348 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6353 /* Get all packets in ILMI Circuit */
6355 bpf_error("'ilmic' supported only on raw ATM");
6356 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6357 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6362 /* Get all LANE packets */
6364 bpf_error("'lane' supported only on raw ATM");
6365 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6368 * Arrange that all subsequent tests assume LANE
6369 * rather than LLC-encapsulated packets, and set
6370 * the offsets appropriately for LANE-encapsulated
6373 * "off_mac" is the offset of the Ethernet header,
6374 * which is 2 bytes past the ATM pseudo-header
6375 * (skipping the pseudo-header and 2-byte LE Client
6376 * field). The other offsets are Ethernet offsets
6377 * relative to "off_mac".
6380 off_mac
= off_payload
+ 2; /* MAC header */
6381 off_linktype
= off_mac
+ 12;
6382 off_nl
= off_mac
+ 14; /* Ethernet II */
6383 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6387 /* Get all LLC-encapsulated packets */
6389 bpf_error("'llc' supported only on raw ATM");
6390 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6401 static struct block
*
6402 gen_msg_abbrev(type
)
6408 * Q.2931 signalling protocol messages for handling virtual circuits
6409 * establishment and teardown
6414 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6418 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6422 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6426 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6430 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6433 case A_RELEASE_DONE
:
6434 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6444 gen_atmmulti_abbrev(type
)
6447 struct block
*b0
, *b1
;
6453 bpf_error("'oam' supported only on raw ATM");
6454 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6459 bpf_error("'oamf4' supported only on raw ATM");
6461 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6462 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6464 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6470 * Get Q.2931 signalling messages for switched
6471 * virtual connection
6474 bpf_error("'connectmsg' supported only on raw ATM");
6475 b0
= gen_msg_abbrev(A_SETUP
);
6476 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6478 b0
= gen_msg_abbrev(A_CONNECT
);
6480 b0
= gen_msg_abbrev(A_CONNECTACK
);
6482 b0
= gen_msg_abbrev(A_RELEASE
);
6484 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6486 b0
= gen_atmtype_abbrev(A_SC
);
6492 bpf_error("'metaconnect' supported only on raw ATM");
6493 b0
= gen_msg_abbrev(A_SETUP
);
6494 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6496 b0
= gen_msg_abbrev(A_CONNECT
);
6498 b0
= gen_msg_abbrev(A_RELEASE
);
6500 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6502 b0
= gen_atmtype_abbrev(A_METAC
);