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.29 2005-07-31 19:02:32 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, label_stack_depth
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * Value passed to gen_load_a() to indicate what the offset argument
138 OR_PACKET
, /* relative to the beginning of the packet */
139 OR_LINK
, /* relative to the link-layer header */
140 OR_NET
, /* relative to the network-layer header */
141 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
142 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
143 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
147 * We divy out chunks of memory rather than call malloc each time so
148 * we don't have to worry about leaking memory. It's probably
149 * not a big deal if all this memory was wasted but if this ever
150 * goes into a library that would probably not be a good idea.
152 * XXX - this *is* in a library....
155 #define CHUNK0SIZE 1024
161 static struct chunk chunks
[NCHUNKS
];
162 static int cur_chunk
;
164 static void *newchunk(u_int
);
165 static void freechunks(void);
166 static inline struct block
*new_block(int);
167 static inline struct slist
*new_stmt(int);
168 static struct block
*gen_retblk(int);
169 static inline void syntax(void);
171 static void backpatch(struct block
*, struct block
*);
172 static void merge(struct block
*, struct block
*);
173 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
178 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
180 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
181 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
182 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
183 static struct slist
*gen_load_llrel(u_int
, u_int
);
184 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
185 static struct slist
*gen_loadx_iphdrlen(void);
186 static struct block
*gen_uncond(int);
187 static inline struct block
*gen_true(void);
188 static inline struct block
*gen_false(void);
189 static struct block
*gen_ether_linktype(int);
190 static struct block
*gen_linux_sll_linktype(int);
191 static void insert_radiotap_load_llprefixlen(struct block
*);
192 static void insert_load_llprefixlen(struct block
*);
193 static struct slist
*gen_llprefixlen(void);
194 static struct block
*gen_linktype(int);
195 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
196 static struct block
*gen_llc_linktype(int);
197 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
199 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
201 static struct block
*gen_ahostop(const u_char
*, int);
202 static struct block
*gen_ehostop(const u_char
*, int);
203 static struct block
*gen_fhostop(const u_char
*, int);
204 static struct block
*gen_thostop(const u_char
*, int);
205 static struct block
*gen_wlanhostop(const u_char
*, int);
206 static struct block
*gen_ipfchostop(const u_char
*, int);
207 static struct block
*gen_dnhostop(bpf_u_int32
, int);
208 static struct block
*gen_null(int);
209 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
211 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
214 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
216 static struct block
*gen_ipfrag(void);
217 static struct block
*gen_portatom(int, bpf_int32
);
218 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
220 static struct block
*gen_portatom6(int, bpf_int32
);
221 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
223 struct block
*gen_portop(int, int, int);
224 static struct block
*gen_port(int, int, int);
225 struct block
*gen_portrangeop(int, int, int, int);
226 static struct block
*gen_portrange(int, int, int, int);
228 struct block
*gen_portop6(int, int, int);
229 static struct block
*gen_port6(int, int, int);
230 struct block
*gen_portrangeop6(int, int, int, int);
231 static struct block
*gen_portrange6(int, int, int, int);
233 static int lookup_proto(const char *, int);
234 static struct block
*gen_protochain(int, int, int);
235 static struct block
*gen_proto(int, int, int);
236 static struct slist
*xfer_to_x(struct arth
*);
237 static struct slist
*xfer_to_a(struct arth
*);
238 static struct block
*gen_mac_multicast(int);
239 static struct block
*gen_len(int, int);
241 static struct block
*gen_msg_abbrev(int type
);
252 /* XXX Round up to nearest long. */
253 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
255 /* XXX Round up to structure boundary. */
259 cp
= &chunks
[cur_chunk
];
260 if (n
> cp
->n_left
) {
261 ++cp
, k
= ++cur_chunk
;
263 bpf_error("out of memory");
264 size
= CHUNK0SIZE
<< k
;
265 cp
->m
= (void *)malloc(size
);
267 bpf_error("out of memory");
268 memset((char *)cp
->m
, 0, size
);
271 bpf_error("out of memory");
274 return (void *)((char *)cp
->m
+ cp
->n_left
);
283 for (i
= 0; i
< NCHUNKS
; ++i
)
284 if (chunks
[i
].m
!= NULL
) {
291 * A strdup whose allocations are freed after code generation is over.
295 register const char *s
;
297 int n
= strlen(s
) + 1;
298 char *cp
= newchunk(n
);
304 static inline struct block
*
310 p
= (struct block
*)newchunk(sizeof(*p
));
317 static inline struct slist
*
323 p
= (struct slist
*)newchunk(sizeof(*p
));
329 static struct block
*
333 struct block
*b
= new_block(BPF_RET
|BPF_K
);
342 bpf_error("syntax error in filter expression");
345 static bpf_u_int32 netmask
;
350 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
351 char *buf
, int optimize
, bpf_u_int32 mask
)
360 if (setjmp(top_ctx
)) {
368 snaplen
= pcap_snapshot(p
);
370 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
371 "snaplen of 0 rejects all packets");
375 lex_init(buf
? buf
: "");
383 root
= gen_retblk(snaplen
);
385 if (optimize
&& !no_optimize
) {
388 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
389 bpf_error("expression rejects all packets");
391 program
->bf_insns
= icode_to_fcode(root
, &len
);
392 program
->bf_len
= len
;
400 * entry point for using the compiler with no pcap open
401 * pass in all the stuff that is needed explicitly instead.
404 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
405 struct bpf_program
*program
,
406 char *buf
, int optimize
, bpf_u_int32 mask
)
411 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
414 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
420 * Clean up a "struct bpf_program" by freeing all the memory allocated
424 pcap_freecode(struct bpf_program
*program
)
427 if (program
->bf_insns
!= NULL
) {
428 free((char *)program
->bf_insns
);
429 program
->bf_insns
= NULL
;
434 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
435 * which of the jt and jf fields has been resolved and which is a pointer
436 * back to another unresolved block (or nil). At least one of the fields
437 * in each block is already resolved.
440 backpatch(list
, target
)
441 struct block
*list
, *target
;
458 * Merge the lists in b0 and b1, using the 'sense' field to indicate
459 * which of jt and jf is the link.
463 struct block
*b0
, *b1
;
465 register struct block
**p
= &b0
;
467 /* Find end of list. */
469 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
471 /* Concatenate the lists. */
479 backpatch(p
, gen_retblk(snaplen
));
480 p
->sense
= !p
->sense
;
481 backpatch(p
, gen_retblk(0));
485 * Insert before the statements of the first (root) block any
486 * statements needed to load the lengths of any variable-length
487 * headers into registers.
489 * XXX - a fancier strategy would be to insert those before the
490 * statements of all blocks that use those lengths and that
491 * have no predecessors that use them, so that we only compute
492 * the lengths if we need them. There might be even better
493 * approaches than that. However, as we're currently only
494 * handling variable-length radiotap headers, and as all
495 * filtering expressions other than raw link[M:N] tests
496 * require the length of that header, doing more for that
497 * header length isn't really worth the effort.
499 insert_load_llprefixlen(root
);
504 struct block
*b0
, *b1
;
506 backpatch(b0
, b1
->head
);
507 b0
->sense
= !b0
->sense
;
508 b1
->sense
= !b1
->sense
;
510 b1
->sense
= !b1
->sense
;
516 struct block
*b0
, *b1
;
518 b0
->sense
= !b0
->sense
;
519 backpatch(b0
, b1
->head
);
520 b0
->sense
= !b0
->sense
;
529 b
->sense
= !b
->sense
;
532 static struct block
*
533 gen_cmp(offrel
, offset
, size
, v
)
534 enum e_offrel offrel
;
538 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
541 static struct block
*
542 gen_cmp_gt(offrel
, offset
, size
, v
)
543 enum e_offrel offrel
;
547 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
550 static struct block
*
551 gen_cmp_ge(offrel
, offset
, size
, v
)
552 enum e_offrel offrel
;
556 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
559 static struct block
*
560 gen_cmp_lt(offrel
, offset
, size
, v
)
561 enum e_offrel offrel
;
565 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
568 static struct block
*
569 gen_cmp_le(offrel
, offset
, size
, v
)
570 enum e_offrel offrel
;
574 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
577 static struct block
*
578 gen_mcmp(offrel
, offset
, size
, v
, mask
)
579 enum e_offrel offrel
;
584 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
587 static struct block
*
588 gen_bcmp(offrel
, offset
, size
, v
)
589 enum e_offrel offrel
;
590 register u_int offset
, size
;
591 register const u_char
*v
;
593 register struct block
*b
, *tmp
;
597 register const u_char
*p
= &v
[size
- 4];
598 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
599 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
601 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
608 register const u_char
*p
= &v
[size
- 2];
609 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
611 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
618 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
627 * AND the field of size "size" at offset "offset" relative to the header
628 * specified by "offrel" with "mask", and compare it with the value "v"
629 * with the test specified by "jtype"; if "reverse" is true, the test
630 * should test the opposite of "jtype".
632 static struct block
*
633 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
634 enum e_offrel offrel
;
636 bpf_u_int32 offset
, size
, mask
, jtype
;
639 struct slist
*s
, *s2
;
642 s
= gen_load_a(offrel
, offset
, size
);
644 if (mask
!= 0xffffffff) {
645 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
650 b
= new_block(JMP(jtype
));
653 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
659 * Various code constructs need to know the layout of the data link
660 * layer. These variables give the necessary offsets from the beginning
661 * of the packet data.
663 * If the link layer has variable_length headers, the offsets are offsets
664 * from the end of the link-link-layer header, and "reg_ll_size" is
665 * the register number for a register containing the length of the
666 * link-layer header. Otherwise, "reg_ll_size" is -1.
668 static int reg_ll_size
;
671 * This is the offset of the beginning of the link-layer header.
672 * It's usually 0, except for 802.11 with a fixed-length radio header.
677 * This is the offset of the beginning of the MAC-layer header.
678 * It's usually 0, except for ATM LANE.
680 static u_int off_mac
;
683 * "off_linktype" is the offset to information in the link-layer header
684 * giving the packet type.
686 * For Ethernet, it's the offset of the Ethernet type field.
688 * For link-layer types that always use 802.2 headers, it's the
689 * offset of the LLC header.
691 * For PPP, it's the offset of the PPP type field.
693 * For Cisco HDLC, it's the offset of the CHDLC type field.
695 * For BSD loopback, it's the offset of the AF_ value.
697 * For Linux cooked sockets, it's the offset of the type field.
699 * It's set to -1 for no encapsulation, in which case, IP is assumed.
701 static u_int off_linktype
;
704 * TRUE if the link layer includes an ATM pseudo-header.
706 static int is_atm
= 0;
709 * TRUE if "lane" appeared in the filter; it causes us to generate
710 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
712 static int is_lane
= 0;
715 * These are offsets for the ATM pseudo-header.
717 static u_int off_vpi
;
718 static u_int off_vci
;
719 static u_int off_proto
;
722 * These are offsets for the MTP3 fields.
724 static u_int off_sio
;
725 static u_int off_opc
;
726 static u_int off_dpc
;
727 static u_int off_sls
;
730 * This is the offset of the first byte after the ATM pseudo_header,
731 * or -1 if there is no ATM pseudo-header.
733 static u_int off_payload
;
736 * These are offsets to the beginning of the network-layer header.
738 * If the link layer never uses 802.2 LLC:
740 * "off_nl" and "off_nl_nosnap" are the same.
742 * If the link layer always uses 802.2 LLC:
744 * "off_nl" is the offset if there's a SNAP header following
747 * "off_nl_nosnap" is the offset if there's no SNAP header.
749 * If the link layer is Ethernet:
751 * "off_nl" is the offset if the packet is an Ethernet II packet
752 * (we assume no 802.3+802.2+SNAP);
754 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
755 * with an 802.2 header following it.
758 static u_int off_nl_nosnap
;
766 linktype
= pcap_datalink(p
);
768 pcap_fddipad
= p
->fddipad
;
772 * Assume it's not raw ATM with a pseudo-header, for now.
788 * Also assume it's not 802.11 with a fixed-length radio header.
794 label_stack_depth
= 0;
802 off_nl
= 6; /* XXX in reality, variable! */
803 off_nl_nosnap
= 6; /* no 802.2 LLC */
806 case DLT_ARCNET_LINUX
:
808 off_nl
= 8; /* XXX in reality, variable! */
809 off_nl_nosnap
= 8; /* no 802.2 LLC */
814 off_nl
= 14; /* Ethernet II */
815 off_nl_nosnap
= 17; /* 802.3+802.2 */
820 * SLIP doesn't have a link level type. The 16 byte
821 * header is hacked into our SLIP driver.
825 off_nl_nosnap
= 16; /* no 802.2 LLC */
829 /* XXX this may be the same as the DLT_PPP_BSDOS case */
833 off_nl_nosnap
= 24; /* no 802.2 LLC */
840 off_nl_nosnap
= 4; /* no 802.2 LLC */
846 off_nl_nosnap
= 12; /* no 802.2 LLC */
851 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
852 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
855 off_nl_nosnap
= 4; /* no 802.2 LLC */
860 * This does no include the Ethernet header, and
861 * only covers session state.
865 off_nl_nosnap
= 8; /* no 802.2 LLC */
871 off_nl_nosnap
= 24; /* no 802.2 LLC */
876 * FDDI doesn't really have a link-level type field.
877 * We set "off_linktype" to the offset of the LLC header.
879 * To check for Ethernet types, we assume that SSAP = SNAP
880 * is being used and pick out the encapsulated Ethernet type.
881 * XXX - should we generate code to check for SNAP?
885 off_linktype
+= pcap_fddipad
;
887 off_nl
= 21; /* FDDI+802.2+SNAP */
888 off_nl_nosnap
= 16; /* FDDI+802.2 */
890 off_nl
+= pcap_fddipad
;
891 off_nl_nosnap
+= pcap_fddipad
;
897 * Token Ring doesn't really have a link-level type field.
898 * We set "off_linktype" to the offset of the LLC header.
900 * To check for Ethernet types, we assume that SSAP = SNAP
901 * is being used and pick out the encapsulated Ethernet type.
902 * XXX - should we generate code to check for SNAP?
904 * XXX - the header is actually variable-length.
905 * Some various Linux patched versions gave 38
906 * as "off_linktype" and 40 as "off_nl"; however,
907 * if a token ring packet has *no* routing
908 * information, i.e. is not source-routed, the correct
909 * values are 20 and 22, as they are in the vanilla code.
911 * A packet is source-routed iff the uppermost bit
912 * of the first byte of the source address, at an
913 * offset of 8, has the uppermost bit set. If the
914 * packet is source-routed, the total number of bytes
915 * of routing information is 2 plus bits 0x1F00 of
916 * the 16-bit value at an offset of 14 (shifted right
917 * 8 - figure out which byte that is).
920 off_nl
= 22; /* Token Ring+802.2+SNAP */
921 off_nl_nosnap
= 17; /* Token Ring+802.2 */
926 * 802.11 doesn't really have a link-level type field.
927 * We set "off_linktype" to the offset of the LLC header.
929 * To check for Ethernet types, we assume that SSAP = SNAP
930 * is being used and pick out the encapsulated Ethernet type.
931 * XXX - should we generate code to check for SNAP?
933 * XXX - the header is actually variable-length. We
934 * assume a 24-byte link-layer header, as appears in
935 * data frames in networks with no bridges. If the
936 * fromds and tods 802.11 header bits are both set,
937 * it's actually supposed to be 30 bytes.
940 off_nl
= 32; /* 802.11+802.2+SNAP */
941 off_nl_nosnap
= 27; /* 802.11+802.2 */
944 case DLT_PRISM_HEADER
:
946 * Same as 802.11, but with an additional header before
947 * the 802.11 header, containing a bunch of additional
948 * information including radio-level information.
950 * The header is 144 bytes long.
952 * XXX - same variable-length header problem; at least
953 * the Prism header is fixed-length.
956 off_linktype
= 144+24;
957 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
958 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
961 case DLT_IEEE802_11_RADIO_AVS
:
963 * Same as 802.11, but with an additional header before
964 * the 802.11 header, containing a bunch of additional
965 * information including radio-level information.
967 * The header is 64 bytes long, at least in its
968 * current incarnation.
970 * XXX - same variable-length header problem, only
971 * more so; this header is also variable-length,
972 * with the length being the 32-bit big-endian
973 * number at an offset of 4 from the beginning
974 * of the radio header.
977 off_linktype
= 64+24;
978 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
979 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
982 case DLT_IEEE802_11_RADIO
:
984 * Same as 802.11, but with an additional header before
985 * the 802.11 header, containing a bunch of additional
986 * information including radio-level information.
988 * The radiotap header is variable length, and we
989 * generate code to compute its length and store it
990 * in a register. These offsets are relative to the
991 * beginning of the 802.11 header.
994 off_nl
= 32; /* 802.11+802.2+SNAP */
995 off_nl_nosnap
= 27; /* 802.11+802.2 */
998 case DLT_ATM_RFC1483
:
999 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1001 * assume routed, non-ISO PDUs
1002 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1004 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1005 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1006 * latter would presumably be treated the way PPPoE
1007 * should be, so you can do "pppoe and udp port 2049"
1008 * or "pppoa and tcp port 80" and have it check for
1009 * PPPo{A,E} and a PPP protocol of IP and....
1012 off_nl
= 8; /* 802.2+SNAP */
1013 off_nl_nosnap
= 3; /* 802.2 */
1018 * Full Frontal ATM; you get AALn PDUs with an ATM
1022 off_vpi
= SUNATM_VPI_POS
;
1023 off_vci
= SUNATM_VCI_POS
;
1024 off_proto
= PROTO_POS
;
1025 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
1026 off_payload
= SUNATM_PKT_BEGIN_POS
;
1027 off_linktype
= off_payload
;
1028 off_nl
= off_payload
+8; /* 802.2+SNAP */
1029 off_nl_nosnap
= off_payload
+3; /* 802.2 */
1035 off_nl_nosnap
= 0; /* no 802.2 LLC */
1038 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1041 off_nl_nosnap
= 16; /* no 802.2 LLC */
1046 * LocalTalk does have a 1-byte type field in the LLAP header,
1047 * but really it just indicates whether there is a "short" or
1048 * "long" DDP packet following.
1052 off_nl_nosnap
= 0; /* no 802.2 LLC */
1055 case DLT_IP_OVER_FC
:
1057 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1058 * link-level type field. We set "off_linktype" to the
1059 * offset of the LLC header.
1061 * To check for Ethernet types, we assume that SSAP = SNAP
1062 * is being used and pick out the encapsulated Ethernet type.
1063 * XXX - should we generate code to check for SNAP? RFC
1064 * 2625 says SNAP should be used.
1067 off_nl
= 24; /* IPFC+802.2+SNAP */
1068 off_nl_nosnap
= 19; /* IPFC+802.2 */
1073 * XXX - we should set this to handle SNAP-encapsulated
1074 * frames (NLPID of 0x80).
1078 off_nl_nosnap
= 0; /* no 802.2 LLC */
1081 case DLT_APPLE_IP_OVER_IEEE1394
:
1084 off_nl_nosnap
= 18; /* no 802.2 LLC */
1087 case DLT_LINUX_IRDA
:
1089 * Currently, only raw "link[N:M]" filtering is supported.
1098 * Currently, only raw "link[N:M]" filtering is supported.
1105 case DLT_SYMANTEC_FIREWALL
:
1107 off_nl
= 44; /* Ethernet II */
1108 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1113 /* XXX read this from pf.h? */
1114 off_nl
= PFLOG_HDRLEN
;
1115 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1118 case DLT_JUNIPER_MFR
:
1119 case DLT_JUNIPER_MLFR
:
1120 case DLT_JUNIPER_MLPPP
:
1123 off_nl_nosnap
= -1; /* no 802.2 LLC */
1126 case DLT_JUNIPER_ATM1
:
1127 off_linktype
= 4; /* in reality variable between 4-8 */
1132 case DLT_JUNIPER_ATM2
:
1133 off_linktype
= 8; /* in reality variable between 8-12 */
1138 /* frames captured on a Juniper PPPoE service PIC
1139 * contain raw ethernet frames */
1140 case DLT_JUNIPER_PPPOE
:
1142 off_nl
= 18; /* Ethernet II */
1143 off_nl_nosnap
= 21; /* 802.3+802.2 */
1146 case DLT_JUNIPER_PPPOE_ATM
:
1149 off_nl_nosnap
= -1; /* no 802.2 LLC */
1152 case DLT_JUNIPER_GGSN
:
1155 off_nl_nosnap
= -1; /* no 802.2 LLC */
1158 case DLT_JUNIPER_ES
:
1160 off_nl
= -1; /* not really a network layer but raw IP adresses */
1161 off_nl_nosnap
= -1; /* no 802.2 LLC */
1164 case DLT_JUNIPER_MONITOR
:
1166 off_nl
= 12; /* raw IP/IP6 header */
1167 off_nl_nosnap
= -1; /* no 802.2 LLC */
1170 case DLT_JUNIPER_SERVICES
:
1172 off_nl
= -1; /* L3 proto location dep. on cookie type */
1173 off_nl_nosnap
= -1; /* no 802.2 LLC */
1194 case DLT_LINUX_LAPD
:
1196 * Currently, only raw "link[N:M]" filtering is supported.
1203 bpf_error("unknown data link type %d", linktype
);
1208 * Load a value relative to the beginning of the link-layer header.
1209 * The link-layer header doesn't necessarily begin at the beginning
1210 * of the packet data; there might be a variable-length prefix containing
1211 * radio information.
1213 static struct slist
*
1214 gen_load_llrel(offset
, size
)
1217 struct slist
*s
, *s2
;
1219 s
= gen_llprefixlen();
1222 * If "s" is non-null, it has code to arrange that the X register
1223 * contains the length of the prefix preceding the link-layer
1227 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1231 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1238 * Load a value relative to the beginning of the specified header.
1240 static struct slist
*
1241 gen_load_a(offrel
, offset
, size
)
1242 enum e_offrel offrel
;
1245 struct slist
*s
, *s2
;
1250 s
= gen_load_llrel(offset
, size
);
1254 s
= gen_load_llrel(off_ll
+ offset
, size
);
1258 s
= gen_load_llrel(off_nl
+ offset
, size
);
1262 s
= gen_load_llrel(off_nl_nosnap
+ offset
, size
);
1267 * Load the X register with the length of the IPv4 header,
1270 s
= gen_loadx_iphdrlen();
1273 * Load the item at {length of the link-layer header} +
1274 * {length of the IPv4 header} + {specified offset}.
1276 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1277 s2
->s
.k
= off_nl
+ offset
;
1282 s
= gen_load_llrel(off_nl
+ 40 + offset
, size
);
1293 * Generate code to load into the X register the sum of the length of
1294 * the IPv4 header and any variable-length header preceding the link-layer
1297 static struct slist
*
1298 gen_loadx_iphdrlen()
1300 struct slist
*s
, *s2
;
1302 s
= gen_llprefixlen();
1305 * There's a variable-length prefix preceding the
1306 * link-layer header. "s" points to a list of statements
1307 * that put the length of that prefix into the X register.
1308 * The 4*([k]&0xf) addressing mode can't be used, as we
1309 * don't have a constant offset, so we have to load the
1310 * value in question into the A register and add to it
1311 * the value from the X register.
1313 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1316 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1319 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1324 * The A register now contains the length of the
1325 * IP header. We need to add to it the length
1326 * of the prefix preceding the link-layer
1327 * header, which is still in the X register, and
1328 * move the result into the X register.
1330 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1331 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1334 * There is no variable-length header preceding the
1335 * link-layer header; if there's a fixed-length
1336 * header preceding it, its length is included in
1337 * the off_ variables, so it doesn't need to be added.
1339 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1345 static struct block
*
1352 s
= new_stmt(BPF_LD
|BPF_IMM
);
1354 b
= new_block(JMP(BPF_JEQ
));
1360 static inline struct block
*
1363 return gen_uncond(1);
1366 static inline struct block
*
1369 return gen_uncond(0);
1373 * Byte-swap a 32-bit number.
1374 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1375 * big-endian platforms.)
1377 #define SWAPLONG(y) \
1378 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1381 * Generate code to match a particular packet type.
1383 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1384 * value, if <= ETHERMTU. We use that to determine whether to
1385 * match the type/length field or to check the type/length field for
1386 * a value <= ETHERMTU to see whether it's a type field and then do
1387 * the appropriate test.
1389 static struct block
*
1390 gen_ether_linktype(proto
)
1393 struct block
*b0
, *b1
;
1399 case LLCSAP_NETBEUI
:
1401 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1402 * so we check the DSAP and SSAP.
1404 * LLCSAP_IP checks for IP-over-802.2, rather
1405 * than IP-over-Ethernet or IP-over-SNAP.
1407 * XXX - should we check both the DSAP and the
1408 * SSAP, like this, or should we check just the
1409 * DSAP, as we do for other types <= ETHERMTU
1410 * (i.e., other SAP values)?
1412 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1414 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1415 ((proto
<< 8) | proto
));
1423 * Ethernet_II frames, which are Ethernet
1424 * frames with a frame type of ETHERTYPE_IPX;
1426 * Ethernet_802.3 frames, which are 802.3
1427 * frames (i.e., the type/length field is
1428 * a length field, <= ETHERMTU, rather than
1429 * a type field) with the first two bytes
1430 * after the Ethernet/802.3 header being
1433 * Ethernet_802.2 frames, which are 802.3
1434 * frames with an 802.2 LLC header and
1435 * with the IPX LSAP as the DSAP in the LLC
1438 * Ethernet_SNAP frames, which are 802.3
1439 * frames with an LLC header and a SNAP
1440 * header and with an OUI of 0x000000
1441 * (encapsulated Ethernet) and a protocol
1442 * ID of ETHERTYPE_IPX in the SNAP header.
1444 * XXX - should we generate the same code both
1445 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1449 * This generates code to check both for the
1450 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1452 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1453 (bpf_int32
)LLCSAP_IPX
);
1454 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1459 * Now we add code to check for SNAP frames with
1460 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1462 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1466 * Now we generate code to check for 802.3
1467 * frames in general.
1469 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1473 * Now add the check for 802.3 frames before the
1474 * check for Ethernet_802.2 and Ethernet_802.3,
1475 * as those checks should only be done on 802.3
1476 * frames, not on Ethernet frames.
1481 * Now add the check for Ethernet_II frames, and
1482 * do that before checking for the other frame
1485 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1486 (bpf_int32
)ETHERTYPE_IPX
);
1490 case ETHERTYPE_ATALK
:
1491 case ETHERTYPE_AARP
:
1493 * EtherTalk (AppleTalk protocols on Ethernet link
1494 * layer) may use 802.2 encapsulation.
1498 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1499 * we check for an Ethernet type field less than
1500 * 1500, which means it's an 802.3 length field.
1502 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1506 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1507 * SNAP packets with an organization code of
1508 * 0x080007 (Apple, for Appletalk) and a protocol
1509 * type of ETHERTYPE_ATALK (Appletalk).
1511 * 802.2-encapsulated ETHERTYPE_AARP packets are
1512 * SNAP packets with an organization code of
1513 * 0x000000 (encapsulated Ethernet) and a protocol
1514 * type of ETHERTYPE_AARP (Appletalk ARP).
1516 if (proto
== ETHERTYPE_ATALK
)
1517 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1518 else /* proto == ETHERTYPE_AARP */
1519 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1523 * Check for Ethernet encapsulation (Ethertalk
1524 * phase 1?); we just check for the Ethernet
1527 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1533 if (proto
<= ETHERMTU
) {
1535 * This is an LLC SAP value, so the frames
1536 * that match would be 802.2 frames.
1537 * Check that the frame is an 802.2 frame
1538 * (i.e., that the length/type field is
1539 * a length field, <= ETHERMTU) and
1540 * then check the DSAP.
1542 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1544 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1550 * This is an Ethernet type, so compare
1551 * the length/type field with it (if
1552 * the frame is an 802.2 frame, the length
1553 * field will be <= ETHERMTU, and, as
1554 * "proto" is > ETHERMTU, this test
1555 * will fail and the frame won't match,
1556 * which is what we want).
1558 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1565 * Generate code to match a particular packet type.
1567 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1568 * value, if <= ETHERMTU. We use that to determine whether to
1569 * match the type field or to check the type field for the special
1570 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1572 static struct block
*
1573 gen_linux_sll_linktype(proto
)
1576 struct block
*b0
, *b1
;
1582 case LLCSAP_NETBEUI
:
1584 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1585 * so we check the DSAP and SSAP.
1587 * LLCSAP_IP checks for IP-over-802.2, rather
1588 * than IP-over-Ethernet or IP-over-SNAP.
1590 * XXX - should we check both the DSAP and the
1591 * SSAP, like this, or should we check just the
1592 * DSAP, as we do for other types <= ETHERMTU
1593 * (i.e., other SAP values)?
1595 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1596 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1597 ((proto
<< 8) | proto
));
1603 * Ethernet_II frames, which are Ethernet
1604 * frames with a frame type of ETHERTYPE_IPX;
1606 * Ethernet_802.3 frames, which have a frame
1607 * type of LINUX_SLL_P_802_3;
1609 * Ethernet_802.2 frames, which are 802.3
1610 * frames with an 802.2 LLC header (i.e, have
1611 * a frame type of LINUX_SLL_P_802_2) and
1612 * with the IPX LSAP as the DSAP in the LLC
1615 * Ethernet_SNAP frames, which are 802.3
1616 * frames with an LLC header and a SNAP
1617 * header and with an OUI of 0x000000
1618 * (encapsulated Ethernet) and a protocol
1619 * ID of ETHERTYPE_IPX in the SNAP header.
1621 * First, do the checks on LINUX_SLL_P_802_2
1622 * frames; generate the check for either
1623 * Ethernet_802.2 or Ethernet_SNAP frames, and
1624 * then put a check for LINUX_SLL_P_802_2 frames
1627 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1628 (bpf_int32
)LLCSAP_IPX
);
1629 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1632 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1636 * Now check for 802.3 frames and OR that with
1637 * the previous test.
1639 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1643 * Now add the check for Ethernet_II frames, and
1644 * do that before checking for the other frame
1647 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1648 (bpf_int32
)ETHERTYPE_IPX
);
1652 case ETHERTYPE_ATALK
:
1653 case ETHERTYPE_AARP
:
1655 * EtherTalk (AppleTalk protocols on Ethernet link
1656 * layer) may use 802.2 encapsulation.
1660 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1661 * we check for the 802.2 protocol type in the
1662 * "Ethernet type" field.
1664 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1667 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1668 * SNAP packets with an organization code of
1669 * 0x080007 (Apple, for Appletalk) and a protocol
1670 * type of ETHERTYPE_ATALK (Appletalk).
1672 * 802.2-encapsulated ETHERTYPE_AARP packets are
1673 * SNAP packets with an organization code of
1674 * 0x000000 (encapsulated Ethernet) and a protocol
1675 * type of ETHERTYPE_AARP (Appletalk ARP).
1677 if (proto
== ETHERTYPE_ATALK
)
1678 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1680 else /* proto == ETHERTYPE_AARP */
1681 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1686 * Check for Ethernet encapsulation (Ethertalk
1687 * phase 1?); we just check for the Ethernet
1690 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1696 if (proto
<= ETHERMTU
) {
1698 * This is an LLC SAP value, so the frames
1699 * that match would be 802.2 frames.
1700 * Check for the 802.2 protocol type
1701 * in the "Ethernet type" field, and
1702 * then check the DSAP.
1704 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1706 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1712 * This is an Ethernet type, so compare
1713 * the length/type field with it (if
1714 * the frame is an 802.2 frame, the length
1715 * field will be <= ETHERMTU, and, as
1716 * "proto" is > ETHERMTU, this test
1717 * will fail and the frame won't match,
1718 * which is what we want).
1720 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1727 insert_radiotap_load_llprefixlen(b
)
1730 struct slist
*s1
, *s2
;
1733 * Prepend to the statements in this block code to load the
1734 * length of the radiotap header into the register assigned
1735 * to hold that length, if one has been assigned.
1737 if (reg_ll_size
!= -1) {
1739 * The 2 bytes at offsets of 2 and 3 from the beginning
1740 * of the radiotap header are the length of the radiotap
1741 * header; unfortunately, it's little-endian, so we have
1742 * to load it a byte at a time and construct the value.
1746 * Load the high-order byte, at an offset of 3, shift it
1747 * left a byte, and put the result in the X register.
1749 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1751 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1754 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1758 * Load the next byte, at an offset of 2, and OR the
1759 * value from the X register into it.
1761 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1764 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
1768 * Now allocate a register to hold that value and store
1771 s2
= new_stmt(BPF_ST
);
1772 s2
->s
.k
= reg_ll_size
;
1776 * Now move it into the X register.
1778 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
1782 * Now append all the existing statements in this
1783 * block to these statements.
1785 sappend(s1
, b
->stmts
);
1792 insert_load_llprefixlen(b
)
1797 case DLT_IEEE802_11_RADIO
:
1798 insert_radiotap_load_llprefixlen(b
);
1803 static struct slist
*
1804 gen_radiotap_llprefixlen(void)
1808 if (reg_ll_size
== -1) {
1810 * We haven't yet assigned a register for the length
1811 * of the radiotap header; allocate one.
1813 reg_ll_size
= alloc_reg();
1817 * Load the register containing the radiotap length
1818 * into the X register.
1820 s
= new_stmt(BPF_LDX
|BPF_MEM
);
1821 s
->s
.k
= reg_ll_size
;
1826 * Generate code to compute the link-layer header length, if necessary,
1827 * putting it into the X register, and to return either a pointer to a
1828 * "struct slist" for the list of statements in that code, or NULL if
1829 * no code is necessary.
1831 static struct slist
*
1832 gen_llprefixlen(void)
1836 case DLT_IEEE802_11_RADIO
:
1837 return gen_radiotap_llprefixlen();
1845 * Generate code to match a particular packet type by matching the
1846 * link-layer type field or fields in the 802.2 LLC header.
1848 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1849 * value, if <= ETHERMTU.
1851 static struct block
*
1855 struct block
*b0
, *b1
, *b2
;
1857 /* has the linktype been poisoned by MPLS expression ? */
1858 if (off_linktype
== (u_int
)-1) {
1860 /* FIXME add other L3 proto IDs */
1863 return gen_null(Q_IP
);
1864 /* FIXME add other L3 proto IDs */
1865 case ETHERTYPE_IPV6
:
1867 return gen_null(Q_IPV6
);
1869 bpf_error("unsupported protocol over mpls");
1877 return gen_ether_linktype(proto
);
1885 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1889 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1898 case DLT_IEEE802_11
:
1899 case DLT_IEEE802_11_RADIO_AVS
:
1900 case DLT_IEEE802_11_RADIO
:
1901 case DLT_PRISM_HEADER
:
1902 case DLT_ATM_RFC1483
:
1904 case DLT_IP_OVER_FC
:
1905 return gen_llc_linktype(proto
);
1911 * If "is_lane" is set, check for a LANE-encapsulated
1912 * version of this protocol, otherwise check for an
1913 * LLC-encapsulated version of this protocol.
1915 * We assume LANE means Ethernet, not Token Ring.
1919 * Check that the packet doesn't begin with an
1920 * LE Control marker. (We've already generated
1923 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1928 * Now generate an Ethernet test.
1930 b1
= gen_ether_linktype(proto
);
1935 * Check for LLC encapsulation and then check the
1938 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1939 b1
= gen_llc_linktype(proto
);
1947 return gen_linux_sll_linktype(proto
);
1952 case DLT_SLIP_BSDOS
:
1955 * These types don't provide any type field; packets
1958 * XXX - for IPv4, check for a version number of 4, and,
1959 * for IPv6, check for a version number of 6?
1965 case ETHERTYPE_IPV6
:
1967 return gen_true(); /* always true */
1970 return gen_false(); /* always false */
1977 case DLT_PPP_SERIAL
:
1980 * We use Ethernet protocol types inside libpcap;
1981 * map them to the corresponding PPP protocol types.
1990 case ETHERTYPE_IPV6
:
1999 case ETHERTYPE_ATALK
:
2013 * I'm assuming the "Bridging PDU"s that go
2014 * over PPP are Spanning Tree Protocol
2028 * We use Ethernet protocol types inside libpcap;
2029 * map them to the corresponding PPP protocol types.
2034 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
2035 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
2037 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
2042 case ETHERTYPE_IPV6
:
2052 case ETHERTYPE_ATALK
:
2066 * I'm assuming the "Bridging PDU"s that go
2067 * over PPP are Spanning Tree Protocol
2083 * For DLT_NULL, the link-layer header is a 32-bit
2084 * word containing an AF_ value in *host* byte order,
2085 * and for DLT_ENC, the link-layer header begins
2086 * with a 32-bit work containing an AF_ value in
2089 * In addition, if we're reading a saved capture file,
2090 * the host byte order in the capture may not be the
2091 * same as the host byte order on this machine.
2093 * For DLT_LOOP, the link-layer header is a 32-bit
2094 * word containing an AF_ value in *network* byte order.
2096 * XXX - AF_ values may, unfortunately, be platform-
2097 * dependent; for example, FreeBSD's AF_INET6 is 24
2098 * whilst NetBSD's and OpenBSD's is 26.
2100 * This means that, when reading a capture file, just
2101 * checking for our AF_INET6 value won't work if the
2102 * capture file came from another OS.
2111 case ETHERTYPE_IPV6
:
2118 * Not a type on which we support filtering.
2119 * XXX - support those that have AF_ values
2120 * #defined on this platform, at least?
2125 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
2127 * The AF_ value is in host byte order, but
2128 * the BPF interpreter will convert it to
2129 * network byte order.
2131 * If this is a save file, and it's from a
2132 * machine with the opposite byte order to
2133 * ours, we byte-swap the AF_ value.
2135 * Then we run it through "htonl()", and
2136 * generate code to compare against the result.
2138 if (bpf_pcap
->sf
.rfile
!= NULL
&&
2139 bpf_pcap
->sf
.swapped
)
2140 proto
= SWAPLONG(proto
);
2141 proto
= htonl(proto
);
2143 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
2147 * af field is host byte order in contrast to the rest of
2150 if (proto
== ETHERTYPE_IP
)
2151 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2152 BPF_B
, (bpf_int32
)AF_INET
));
2154 else if (proto
== ETHERTYPE_IPV6
)
2155 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
2156 BPF_B
, (bpf_int32
)AF_INET6
));
2164 case DLT_ARCNET_LINUX
:
2166 * XXX should we check for first fragment if the protocol
2175 case ETHERTYPE_IPV6
:
2176 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2177 (bpf_int32
)ARCTYPE_INET6
));
2181 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2182 (bpf_int32
)ARCTYPE_IP
);
2183 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2184 (bpf_int32
)ARCTYPE_IP_OLD
);
2189 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2190 (bpf_int32
)ARCTYPE_ARP
);
2191 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2192 (bpf_int32
)ARCTYPE_ARP_OLD
);
2196 case ETHERTYPE_REVARP
:
2197 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2198 (bpf_int32
)ARCTYPE_REVARP
));
2200 case ETHERTYPE_ATALK
:
2201 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2202 (bpf_int32
)ARCTYPE_ATALK
));
2209 case ETHERTYPE_ATALK
:
2219 * XXX - assumes a 2-byte Frame Relay header with
2220 * DLCI and flags. What if the address is longer?
2226 * Check for the special NLPID for IP.
2228 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
2231 case ETHERTYPE_IPV6
:
2233 * Check for the special NLPID for IPv6.
2235 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
2240 * Check for several OSI protocols.
2242 * Frame Relay packets typically have an OSI
2243 * NLPID at the beginning; we check for each
2246 * What we check for is the NLPID and a frame
2247 * control field of UI, i.e. 0x03 followed
2250 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
2251 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
2252 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
2263 case DLT_JUNIPER_MFR
:
2264 case DLT_JUNIPER_MLFR
:
2265 case DLT_JUNIPER_MLPPP
:
2266 case DLT_JUNIPER_ATM1
:
2267 case DLT_JUNIPER_ATM2
:
2268 case DLT_JUNIPER_PPPOE
:
2269 case DLT_JUNIPER_PPPOE_ATM
:
2270 case DLT_JUNIPER_GGSN
:
2271 case DLT_JUNIPER_ES
:
2272 case DLT_JUNIPER_MONITOR
:
2273 case DLT_JUNIPER_SERVICES
:
2274 /* just lets verify the magic number for now -
2275 * on ATM we may have up to 6 different encapsulations on the wire
2276 * and need a lot of heuristics to figure out that the payload
2279 * FIXME encapsulation specific BPF_ filters
2281 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
2283 case DLT_LINUX_IRDA
:
2284 bpf_error("IrDA link-layer type filtering not implemented");
2287 bpf_error("DOCSIS link-layer type filtering not implemented");
2289 case DLT_LINUX_LAPD
:
2290 bpf_error("LAPD link-layer type filtering not implemented");
2294 * All the types that have no encapsulation should either be
2295 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
2296 * all packets are IP packets, or should be handled in some
2297 * special case, if none of them are (if some are and some
2298 * aren't, the lack of encapsulation is a problem, as we'd
2299 * have to find some other way of determining the packet type).
2301 * Therefore, if "off_linktype" is -1, there's an error.
2303 if (off_linktype
== (u_int
)-1)
2307 * Any type not handled above should always have an Ethernet
2308 * type at an offset of "off_linktype". (PPP is partially
2309 * handled above - the protocol type is mapped from the
2310 * Ethernet and LLC types we use internally to the corresponding
2311 * PPP type - but the PPP type is always specified by a value
2312 * at "off_linktype", so we don't have to do the code generation
2315 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
2319 * Check for an LLC SNAP packet with a given organization code and
2320 * protocol type; we check the entire contents of the 802.2 LLC and
2321 * snap headers, checking for DSAP and SSAP of SNAP and a control
2322 * field of 0x03 in the LLC header, and for the specified organization
2323 * code and protocol type in the SNAP header.
2325 static struct block
*
2326 gen_snap(orgcode
, ptype
, offset
)
2327 bpf_u_int32 orgcode
;
2331 u_char snapblock
[8];
2333 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
2334 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2335 snapblock
[2] = 0x03; /* control = UI */
2336 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2337 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2338 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2339 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2340 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2341 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2345 * Generate code to match a particular packet type, for link-layer types
2346 * using 802.2 LLC headers.
2348 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2349 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2351 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2352 * value, if <= ETHERMTU. We use that to determine whether to
2353 * match the DSAP or both DSAP and LSAP or to check the OUI and
2354 * protocol ID in a SNAP header.
2356 static struct block
*
2357 gen_llc_linktype(proto
)
2361 * XXX - handle token-ring variable-length header.
2367 case LLCSAP_NETBEUI
:
2369 * XXX - should we check both the DSAP and the
2370 * SSAP, like this, or should we check just the
2371 * DSAP, as we do for other types <= ETHERMTU
2372 * (i.e., other SAP values)?
2374 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2375 ((proto
<< 8) | proto
));
2379 * XXX - are there ever SNAP frames for IPX on
2380 * non-Ethernet 802.x networks?
2382 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2383 (bpf_int32
)LLCSAP_IPX
);
2385 case ETHERTYPE_ATALK
:
2387 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2388 * SNAP packets with an organization code of
2389 * 0x080007 (Apple, for Appletalk) and a protocol
2390 * type of ETHERTYPE_ATALK (Appletalk).
2392 * XXX - check for an organization code of
2393 * encapsulated Ethernet as well?
2395 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2399 * XXX - we don't have to check for IPX 802.3
2400 * here, but should we check for the IPX Ethertype?
2402 if (proto
<= ETHERMTU
) {
2404 * This is an LLC SAP value, so check
2407 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2411 * This is an Ethernet type; we assume that it's
2412 * unlikely that it'll appear in the right place
2413 * at random, and therefore check only the
2414 * location that would hold the Ethernet type
2415 * in a SNAP frame with an organization code of
2416 * 0x000000 (encapsulated Ethernet).
2418 * XXX - if we were to check for the SNAP DSAP and
2419 * LSAP, as per XXX, and were also to check for an
2420 * organization code of 0x000000 (encapsulated
2421 * Ethernet), we'd do
2423 * return gen_snap(0x000000, proto,
2426 * here; for now, we don't, as per the above.
2427 * I don't know whether it's worth the extra CPU
2428 * time to do the right check or not.
2430 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2436 static struct block
*
2437 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2441 u_int src_off
, dst_off
;
2443 struct block
*b0
, *b1
;
2457 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2458 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2464 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2465 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2472 b0
= gen_linktype(proto
);
2473 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2479 static struct block
*
2480 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2481 struct in6_addr
*addr
;
2482 struct in6_addr
*mask
;
2484 u_int src_off
, dst_off
;
2486 struct block
*b0
, *b1
;
2501 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2502 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2508 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2509 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2516 /* this order is important */
2517 a
= (u_int32_t
*)addr
;
2518 m
= (u_int32_t
*)mask
;
2519 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2520 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2522 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2524 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2526 b0
= gen_linktype(proto
);
2532 static struct block
*
2533 gen_ehostop(eaddr
, dir
)
2534 register const u_char
*eaddr
;
2537 register struct block
*b0
, *b1
;
2541 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2544 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2547 b0
= gen_ehostop(eaddr
, Q_SRC
);
2548 b1
= gen_ehostop(eaddr
, Q_DST
);
2554 b0
= gen_ehostop(eaddr
, Q_SRC
);
2555 b1
= gen_ehostop(eaddr
, Q_DST
);
2564 * Like gen_ehostop, but for DLT_FDDI
2566 static struct block
*
2567 gen_fhostop(eaddr
, dir
)
2568 register const u_char
*eaddr
;
2571 struct block
*b0
, *b1
;
2576 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2578 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2583 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2585 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2589 b0
= gen_fhostop(eaddr
, Q_SRC
);
2590 b1
= gen_fhostop(eaddr
, Q_DST
);
2596 b0
= gen_fhostop(eaddr
, Q_SRC
);
2597 b1
= gen_fhostop(eaddr
, Q_DST
);
2606 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2608 static struct block
*
2609 gen_thostop(eaddr
, dir
)
2610 register const u_char
*eaddr
;
2613 register struct block
*b0
, *b1
;
2617 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2620 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2623 b0
= gen_thostop(eaddr
, Q_SRC
);
2624 b1
= gen_thostop(eaddr
, Q_DST
);
2630 b0
= gen_thostop(eaddr
, Q_SRC
);
2631 b1
= gen_thostop(eaddr
, Q_DST
);
2640 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2642 static struct block
*
2643 gen_wlanhostop(eaddr
, dir
)
2644 register const u_char
*eaddr
;
2647 register struct block
*b0
, *b1
, *b2
;
2648 register struct slist
*s
;
2655 * For control frames, there is no SA.
2657 * For management frames, SA is at an
2658 * offset of 10 from the beginning of
2661 * For data frames, SA is at an offset
2662 * of 10 from the beginning of the packet
2663 * if From DS is clear, at an offset of
2664 * 16 from the beginning of the packet
2665 * if From DS is set and To DS is clear,
2666 * and an offset of 24 from the beginning
2667 * of the packet if From DS is set and To DS
2672 * Generate the tests to be done for data frames
2675 * First, check for To DS set, i.e. check "link[1] & 0x01".
2677 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2678 b1
= new_block(JMP(BPF_JSET
));
2679 b1
->s
.k
= 0x01; /* To DS */
2683 * If To DS is set, the SA is at 24.
2685 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2689 * Now, check for To DS not set, i.e. check
2690 * "!(link[1] & 0x01)".
2692 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2693 b2
= new_block(JMP(BPF_JSET
));
2694 b2
->s
.k
= 0x01; /* To DS */
2699 * If To DS is not set, the SA is at 16.
2701 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2705 * Now OR together the last two checks. That gives
2706 * the complete set of checks for data frames with
2712 * Now check for From DS being set, and AND that with
2713 * the ORed-together checks.
2715 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2716 b1
= new_block(JMP(BPF_JSET
));
2717 b1
->s
.k
= 0x02; /* From DS */
2722 * Now check for data frames with From DS not set.
2724 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2725 b2
= new_block(JMP(BPF_JSET
));
2726 b2
->s
.k
= 0x02; /* From DS */
2731 * If From DS isn't set, the SA is at 10.
2733 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2737 * Now OR together the checks for data frames with
2738 * From DS not set and for data frames with From DS
2739 * set; that gives the checks done for data frames.
2744 * Now check for a data frame.
2745 * I.e, check "link[0] & 0x08".
2747 gen_load_a(OR_LINK
, 0, BPF_B
);
2748 b1
= new_block(JMP(BPF_JSET
));
2753 * AND that with the checks done for data frames.
2758 * If the high-order bit of the type value is 0, this
2759 * is a management frame.
2760 * I.e, check "!(link[0] & 0x08)".
2762 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2763 b2
= new_block(JMP(BPF_JSET
));
2769 * For management frames, the SA is at 10.
2771 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2775 * OR that with the checks done for data frames.
2776 * That gives the checks done for management and
2782 * If the low-order bit of the type value is 1,
2783 * this is either a control frame or a frame
2784 * with a reserved type, and thus not a
2787 * I.e., check "!(link[0] & 0x04)".
2789 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2790 b1
= new_block(JMP(BPF_JSET
));
2796 * AND that with the checks for data and management
2806 * For control frames, there is no DA.
2808 * For management frames, DA is at an
2809 * offset of 4 from the beginning of
2812 * For data frames, DA is at an offset
2813 * of 4 from the beginning of the packet
2814 * if To DS is clear and at an offset of
2815 * 16 from the beginning of the packet
2820 * Generate the tests to be done for data frames.
2822 * First, check for To DS set, i.e. "link[1] & 0x01".
2824 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2825 b1
= new_block(JMP(BPF_JSET
));
2826 b1
->s
.k
= 0x01; /* To DS */
2830 * If To DS is set, the DA is at 16.
2832 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2836 * Now, check for To DS not set, i.e. check
2837 * "!(link[1] & 0x01)".
2839 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2840 b2
= new_block(JMP(BPF_JSET
));
2841 b2
->s
.k
= 0x01; /* To DS */
2846 * If To DS is not set, the DA is at 4.
2848 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2852 * Now OR together the last two checks. That gives
2853 * the complete set of checks for data frames.
2858 * Now check for a data frame.
2859 * I.e, check "link[0] & 0x08".
2861 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2862 b1
= new_block(JMP(BPF_JSET
));
2867 * AND that with the checks done for data frames.
2872 * If the high-order bit of the type value is 0, this
2873 * is a management frame.
2874 * I.e, check "!(link[0] & 0x08)".
2876 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2877 b2
= new_block(JMP(BPF_JSET
));
2883 * For management frames, the DA is at 4.
2885 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2889 * OR that with the checks done for data frames.
2890 * That gives the checks done for management and
2896 * If the low-order bit of the type value is 1,
2897 * this is either a control frame or a frame
2898 * with a reserved type, and thus not a
2901 * I.e., check "!(link[0] & 0x04)".
2903 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2904 b1
= new_block(JMP(BPF_JSET
));
2910 * AND that with the checks for data and management
2917 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2918 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2924 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2925 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2934 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2935 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2936 * as the RFC states.)
2938 static struct block
*
2939 gen_ipfchostop(eaddr
, dir
)
2940 register const u_char
*eaddr
;
2943 register struct block
*b0
, *b1
;
2947 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2950 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2953 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2954 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2960 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2961 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2970 * This is quite tricky because there may be pad bytes in front of the
2971 * DECNET header, and then there are two possible data packet formats that
2972 * carry both src and dst addresses, plus 5 packet types in a format that
2973 * carries only the src node, plus 2 types that use a different format and
2974 * also carry just the src node.
2978 * Instead of doing those all right, we just look for data packets with
2979 * 0 or 1 bytes of padding. If you want to look at other packets, that
2980 * will require a lot more hacking.
2982 * To add support for filtering on DECNET "areas" (network numbers)
2983 * one would want to add a "mask" argument to this routine. That would
2984 * make the filter even more inefficient, although one could be clever
2985 * and not generate masking instructions if the mask is 0xFFFF.
2987 static struct block
*
2988 gen_dnhostop(addr
, dir
)
2992 struct block
*b0
, *b1
, *b2
, *tmp
;
2993 u_int offset_lh
; /* offset if long header is received */
2994 u_int offset_sh
; /* offset if short header is received */
2999 offset_sh
= 1; /* follows flags */
3000 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
3004 offset_sh
= 3; /* follows flags, dstnode */
3005 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
3009 /* Inefficient because we do our Calvinball dance twice */
3010 b0
= gen_dnhostop(addr
, Q_SRC
);
3011 b1
= gen_dnhostop(addr
, Q_DST
);
3017 /* Inefficient because we do our Calvinball dance twice */
3018 b0
= gen_dnhostop(addr
, Q_SRC
);
3019 b1
= gen_dnhostop(addr
, Q_DST
);
3024 bpf_error("ISO host filtering not implemented");
3029 b0
= gen_linktype(ETHERTYPE_DN
);
3030 /* Check for pad = 1, long header case */
3031 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3032 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
3033 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
3034 BPF_H
, (bpf_int32
)ntohs(addr
));
3036 /* Check for pad = 0, long header case */
3037 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
3038 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3041 /* Check for pad = 1, short header case */
3042 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
3043 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
3044 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3047 /* Check for pad = 0, short header case */
3048 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
3049 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
3053 /* Combine with test for linktype */
3058 /* generate a null link-layer encapsulation
3060 * which is matching for 0x4 in the first byte of the IPv4 header
3061 * matching for 0x6 in the first byte of the IPv6 header
3063 * we need this for matching to an IP payload in MPLS packets
3065 static struct block
*
3069 struct block
*b0
, *b1
;
3072 /* match the bottom-of-stack bit */
3073 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3074 /* match the IPv4 version number */
3075 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x40, 0xf0);
3079 /* match the bottom-of-stack bit */
3080 b0
= gen_mcmp(OR_NET
, -2, BPF_B
, 0x01, 0x01);
3081 /* match the IPv4 version number */
3082 b1
= gen_mcmp(OR_NET
, 0, BPF_B
, 0x60, 0xf0);
3090 static struct block
*
3091 gen_host(addr
, mask
, proto
, dir
)
3097 struct block
*b0
, *b1
;
3102 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
3103 if (off_linktype
!= (u_int
)-1) {
3104 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
3106 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
3112 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
3115 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
3118 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
3121 bpf_error("'tcp' modifier applied to host");
3124 bpf_error("'sctp' modifier applied to host");
3127 bpf_error("'udp' modifier applied to host");
3130 bpf_error("'icmp' modifier applied to host");
3133 bpf_error("'igmp' modifier applied to host");
3136 bpf_error("'igrp' modifier applied to host");
3139 bpf_error("'pim' modifier applied to host");
3142 bpf_error("'vrrp' modifier applied to host");
3145 bpf_error("ATALK host filtering not implemented");
3148 bpf_error("AARP host filtering not implemented");
3151 return gen_dnhostop(addr
, dir
);
3154 bpf_error("SCA host filtering not implemented");
3157 bpf_error("LAT host filtering not implemented");
3160 bpf_error("MOPDL host filtering not implemented");
3163 bpf_error("MOPRC host filtering not implemented");
3167 bpf_error("'ip6' modifier applied to ip host");
3170 bpf_error("'icmp6' modifier applied to host");
3174 bpf_error("'ah' modifier applied to host");
3177 bpf_error("'esp' modifier applied to host");
3180 bpf_error("ISO host filtering not implemented");
3183 bpf_error("'esis' modifier applied to host");
3186 bpf_error("'isis' modifier applied to host");
3189 bpf_error("'clnp' modifier applied to host");
3192 bpf_error("'stp' modifier applied to host");
3195 bpf_error("IPX host filtering not implemented");
3198 bpf_error("'netbeui' modifier applied to host");
3201 bpf_error("'radio' modifier applied to host");
3210 static struct block
*
3211 gen_host6(addr
, mask
, proto
, dir
)
3212 struct in6_addr
*addr
;
3213 struct in6_addr
*mask
;
3220 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
3223 bpf_error("'ip' modifier applied to ip6 host");
3226 bpf_error("'rarp' modifier applied to ip6 host");
3229 bpf_error("'arp' modifier applied to ip6 host");
3232 bpf_error("'sctp' modifier applied to host");
3235 bpf_error("'tcp' modifier applied to host");
3238 bpf_error("'udp' modifier applied to host");
3241 bpf_error("'icmp' modifier applied to host");
3244 bpf_error("'igmp' modifier applied to host");
3247 bpf_error("'igrp' modifier applied to host");
3250 bpf_error("'pim' modifier applied to host");
3253 bpf_error("'vrrp' modifier applied to host");
3256 bpf_error("ATALK host filtering not implemented");
3259 bpf_error("AARP host filtering not implemented");
3262 bpf_error("'decnet' modifier applied to ip6 host");
3265 bpf_error("SCA host filtering not implemented");
3268 bpf_error("LAT host filtering not implemented");
3271 bpf_error("MOPDL host filtering not implemented");
3274 bpf_error("MOPRC host filtering not implemented");
3277 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
3280 bpf_error("'icmp6' modifier applied to host");
3283 bpf_error("'ah' modifier applied to host");
3286 bpf_error("'esp' modifier applied to host");
3289 bpf_error("ISO host filtering not implemented");
3292 bpf_error("'esis' modifier applied to host");
3295 bpf_error("'isis' modifier applied to host");
3298 bpf_error("'clnp' modifier applied to host");
3301 bpf_error("'stp' modifier applied to host");
3304 bpf_error("IPX host filtering not implemented");
3307 bpf_error("'netbeui' modifier applied to host");
3310 bpf_error("'radio' modifier applied to host");
3320 static struct block
*
3321 gen_gateway(eaddr
, alist
, proto
, dir
)
3322 const u_char
*eaddr
;
3323 bpf_u_int32
**alist
;
3327 struct block
*b0
, *b1
, *tmp
;
3330 bpf_error("direction applied to 'gateway'");
3337 if (linktype
== DLT_EN10MB
)
3338 b0
= gen_ehostop(eaddr
, Q_OR
);
3339 else if (linktype
== DLT_FDDI
)
3340 b0
= gen_fhostop(eaddr
, Q_OR
);
3341 else if (linktype
== DLT_IEEE802
)
3342 b0
= gen_thostop(eaddr
, Q_OR
);
3343 else if (linktype
== DLT_IEEE802_11
||
3344 linktype
== DLT_IEEE802_11_RADIO_AVS
||
3345 linktype
== DLT_IEEE802_11_RADIO
||
3346 linktype
== DLT_PRISM_HEADER
)
3347 b0
= gen_wlanhostop(eaddr
, Q_OR
);
3348 else if (linktype
== DLT_SUNATM
&& is_lane
) {
3350 * Check that the packet doesn't begin with an
3351 * LE Control marker. (We've already generated
3354 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
3359 * Now check the MAC address.
3361 b0
= gen_ehostop(eaddr
, Q_OR
);
3363 } else if (linktype
== DLT_IP_OVER_FC
)
3364 b0
= gen_ipfchostop(eaddr
, Q_OR
);
3367 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
3369 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3371 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3379 bpf_error("illegal modifier of 'gateway'");
3385 gen_proto_abbrev(proto
)
3394 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3396 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3402 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3404 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3410 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3412 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3418 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3421 #ifndef IPPROTO_IGMP
3422 #define IPPROTO_IGMP 2
3426 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3429 #ifndef IPPROTO_IGRP
3430 #define IPPROTO_IGRP 9
3433 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3437 #define IPPROTO_PIM 103
3441 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3443 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3448 #ifndef IPPROTO_VRRP
3449 #define IPPROTO_VRRP 112
3453 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3457 b1
= gen_linktype(ETHERTYPE_IP
);
3461 b1
= gen_linktype(ETHERTYPE_ARP
);
3465 b1
= gen_linktype(ETHERTYPE_REVARP
);
3469 bpf_error("link layer applied in wrong context");
3472 b1
= gen_linktype(ETHERTYPE_ATALK
);
3476 b1
= gen_linktype(ETHERTYPE_AARP
);
3480 b1
= gen_linktype(ETHERTYPE_DN
);
3484 b1
= gen_linktype(ETHERTYPE_SCA
);
3488 b1
= gen_linktype(ETHERTYPE_LAT
);
3492 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3496 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3501 b1
= gen_linktype(ETHERTYPE_IPV6
);
3504 #ifndef IPPROTO_ICMPV6
3505 #define IPPROTO_ICMPV6 58
3508 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3513 #define IPPROTO_AH 51
3516 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3518 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3524 #define IPPROTO_ESP 50
3527 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3529 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3535 b1
= gen_linktype(LLCSAP_ISONS
);
3539 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3543 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3546 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3547 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3548 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3550 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3552 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3554 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3558 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3559 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3560 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3562 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3564 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3566 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3570 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3571 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3572 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3574 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3579 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3580 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3585 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3586 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3588 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3590 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3595 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3596 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3601 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3602 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3607 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3611 b1
= gen_linktype(LLCSAP_8021D
);
3615 b1
= gen_linktype(LLCSAP_IPX
);
3619 b1
= gen_linktype(LLCSAP_NETBEUI
);
3623 bpf_error("'radio' is not a valid protocol type");
3631 static struct block
*
3638 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3639 b
= new_block(JMP(BPF_JSET
));
3648 * Generate a comparison to a port value in the transport-layer header
3649 * at the specified offset from the beginning of that header.
3651 * XXX - this handles a variable-length prefix preceding the link-layer
3652 * header, such as the radiotap or AVS radio prefix, but doesn't handle
3653 * variable-length link-layer headers (such as Token Ring or 802.11
3656 static struct block
*
3657 gen_portatom(off
, v
)
3661 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3665 static struct block
*
3666 gen_portatom6(off
, v
)
3670 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3675 gen_portop(port
, proto
, dir
)
3676 int port
, proto
, dir
;
3678 struct block
*b0
, *b1
, *tmp
;
3680 /* ip proto 'proto' */
3681 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3687 b1
= gen_portatom(0, (bpf_int32
)port
);
3691 b1
= gen_portatom(2, (bpf_int32
)port
);
3696 tmp
= gen_portatom(0, (bpf_int32
)port
);
3697 b1
= gen_portatom(2, (bpf_int32
)port
);
3702 tmp
= gen_portatom(0, (bpf_int32
)port
);
3703 b1
= gen_portatom(2, (bpf_int32
)port
);
3715 static struct block
*
3716 gen_port(port
, ip_proto
, dir
)
3721 struct block
*b0
, *b1
, *tmp
;
3726 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3727 * not LLC encapsulation with LLCSAP_IP.
3729 * For IEEE 802 networks - which includes 802.5 token ring
3730 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3731 * says that SNAP encapsulation is used, not LLC encapsulation
3734 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3735 * RFC 2225 say that SNAP encapsulation is used, not LLC
3736 * encapsulation with LLCSAP_IP.
3738 * So we always check for ETHERTYPE_IP.
3740 b0
= gen_linktype(ETHERTYPE_IP
);
3746 b1
= gen_portop(port
, ip_proto
, dir
);
3750 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3751 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3753 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3766 gen_portop6(port
, proto
, dir
)
3767 int port
, proto
, dir
;
3769 struct block
*b0
, *b1
, *tmp
;
3771 /* ip6 proto 'proto' */
3772 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3776 b1
= gen_portatom6(0, (bpf_int32
)port
);
3780 b1
= gen_portatom6(2, (bpf_int32
)port
);
3785 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3786 b1
= gen_portatom6(2, (bpf_int32
)port
);
3791 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3792 b1
= gen_portatom6(2, (bpf_int32
)port
);
3804 static struct block
*
3805 gen_port6(port
, ip_proto
, dir
)
3810 struct block
*b0
, *b1
, *tmp
;
3812 /* link proto ip6 */
3813 b0
= gen_linktype(ETHERTYPE_IPV6
);
3819 b1
= gen_portop6(port
, ip_proto
, dir
);
3823 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3824 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3826 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3838 /* gen_portrange code */
3839 static struct block
*
3840 gen_portrangeatom(off
, v1
, v2
)
3844 struct block
*b1
, *b2
;
3848 * Reverse the order of the ports, so v1 is the lower one.
3857 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3858 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3866 gen_portrangeop(port1
, port2
, proto
, dir
)
3871 struct block
*b0
, *b1
, *tmp
;
3873 /* ip proto 'proto' */
3874 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3880 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3884 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3889 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3890 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3895 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3896 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3908 static struct block
*
3909 gen_portrange(port1
, port2
, ip_proto
, dir
)
3914 struct block
*b0
, *b1
, *tmp
;
3917 b0
= gen_linktype(ETHERTYPE_IP
);
3923 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3927 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3928 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3930 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3942 static struct block
*
3943 gen_portrangeatom6(off
, v1
, v2
)
3947 struct block
*b1
, *b2
;
3951 * Reverse the order of the ports, so v1 is the lower one.
3960 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3961 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3969 gen_portrangeop6(port1
, port2
, proto
, dir
)
3974 struct block
*b0
, *b1
, *tmp
;
3976 /* ip6 proto 'proto' */
3977 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3981 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3985 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3990 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3991 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3996 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3997 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
4009 static struct block
*
4010 gen_portrange6(port1
, port2
, ip_proto
, dir
)
4015 struct block
*b0
, *b1
, *tmp
;
4017 /* link proto ip6 */
4018 b0
= gen_linktype(ETHERTYPE_IPV6
);
4024 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
4028 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
4029 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
4031 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
4044 lookup_proto(name
, proto
)
4045 register const char *name
;
4055 v
= pcap_nametoproto(name
);
4056 if (v
== PROTO_UNDEF
)
4057 bpf_error("unknown ip proto '%s'", name
);
4061 /* XXX should look up h/w protocol type based on linktype */
4062 v
= pcap_nametoeproto(name
);
4063 if (v
== PROTO_UNDEF
) {
4064 v
= pcap_nametollc(name
);
4065 if (v
== PROTO_UNDEF
)
4066 bpf_error("unknown ether proto '%s'", name
);
4071 if (strcmp(name
, "esis") == 0)
4073 else if (strcmp(name
, "isis") == 0)
4075 else if (strcmp(name
, "clnp") == 0)
4078 bpf_error("unknown osi proto '%s'", name
);
4098 static struct block
*
4099 gen_protochain(v
, proto
, dir
)
4104 #ifdef NO_PROTOCHAIN
4105 return gen_proto(v
, proto
, dir
);
4107 struct block
*b0
, *b
;
4108 struct slist
*s
[100];
4109 int fix2
, fix3
, fix4
, fix5
;
4110 int ahcheck
, again
, end
;
4112 int reg2
= alloc_reg();
4114 memset(s
, 0, sizeof(s
));
4115 fix2
= fix3
= fix4
= fix5
= 0;
4122 b0
= gen_protochain(v
, Q_IP
, dir
);
4123 b
= gen_protochain(v
, Q_IPV6
, dir
);
4127 bpf_error("bad protocol applied for 'protochain'");
4132 * We don't handle variable-length radiotap here headers yet.
4133 * We might want to add BPF instructions to do the protochain
4134 * work, to simplify that and, on platforms that have a BPF
4135 * interpreter with the new instructions, let the filtering
4136 * be done in the kernel. (We already require a modified BPF
4137 * engine to do the protochain stuff, to support backward
4138 * branches, and backward branch support is unlikely to appear
4139 * in kernel BPF engines.)
4141 if (linktype
== DLT_IEEE802_11_RADIO
)
4142 bpf_error("'protochain' not supported with radiotap headers");
4144 no_optimize
= 1; /*this code is not compatible with optimzer yet */
4147 * s[0] is a dummy entry to protect other BPF insn from damage
4148 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
4149 * hard to find interdependency made by jump table fixup.
4152 s
[i
] = new_stmt(0); /*dummy*/
4157 b0
= gen_linktype(ETHERTYPE_IP
);
4160 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4161 s
[i
]->s
.k
= off_nl
+ 9;
4163 /* X = ip->ip_hl << 2 */
4164 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4170 b0
= gen_linktype(ETHERTYPE_IPV6
);
4172 /* A = ip6->ip_nxt */
4173 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
4174 s
[i
]->s
.k
= off_nl
+ 6;
4176 /* X = sizeof(struct ip6_hdr) */
4177 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
4183 bpf_error("unsupported proto to gen_protochain");
4187 /* again: if (A == v) goto end; else fall through; */
4189 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4191 s
[i
]->s
.jt
= NULL
; /*later*/
4192 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4196 #ifndef IPPROTO_NONE
4197 #define IPPROTO_NONE 59
4199 /* if (A == IPPROTO_NONE) goto end */
4200 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4201 s
[i
]->s
.jt
= NULL
; /*later*/
4202 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4203 s
[i
]->s
.k
= IPPROTO_NONE
;
4204 s
[fix5
]->s
.jf
= s
[i
];
4209 if (proto
== Q_IPV6
) {
4210 int v6start
, v6end
, v6advance
, j
;
4213 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
4214 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4215 s
[i
]->s
.jt
= NULL
; /*later*/
4216 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4217 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
4218 s
[fix2
]->s
.jf
= s
[i
];
4220 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
4221 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4222 s
[i
]->s
.jt
= NULL
; /*later*/
4223 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4224 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
4226 /* if (A == IPPROTO_ROUTING) goto v6advance */
4227 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4228 s
[i
]->s
.jt
= NULL
; /*later*/
4229 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
4230 s
[i
]->s
.k
= IPPROTO_ROUTING
;
4232 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
4233 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4234 s
[i
]->s
.jt
= NULL
; /*later*/
4235 s
[i
]->s
.jf
= NULL
; /*later*/
4236 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
4247 * X = X + (P[X + 1] + 1) * 8;
4250 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4252 /* A = P[X + packet head] */
4253 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4257 s
[i
] = new_stmt(BPF_ST
);
4261 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4264 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4268 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4270 /* A = P[X + packet head]; */
4271 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4275 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4279 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4283 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4286 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4290 /* goto again; (must use BPF_JA for backward jump) */
4291 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4292 s
[i
]->s
.k
= again
- i
- 1;
4293 s
[i
- 1]->s
.jf
= s
[i
];
4297 for (j
= v6start
; j
<= v6end
; j
++)
4298 s
[j
]->s
.jt
= s
[v6advance
];
4303 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4305 s
[fix2
]->s
.jf
= s
[i
];
4311 /* if (A == IPPROTO_AH) then fall through; else goto end; */
4312 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
4313 s
[i
]->s
.jt
= NULL
; /*later*/
4314 s
[i
]->s
.jf
= NULL
; /*later*/
4315 s
[i
]->s
.k
= IPPROTO_AH
;
4317 s
[fix3
]->s
.jf
= s
[ahcheck
];
4324 * X = X + (P[X + 1] + 2) * 4;
4327 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4329 /* A = P[X + packet head]; */
4330 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4334 s
[i
] = new_stmt(BPF_ST
);
4338 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
4341 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4345 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4347 /* A = P[X + packet head] */
4348 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
4352 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4356 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
4360 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
4363 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
4367 /* goto again; (must use BPF_JA for backward jump) */
4368 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
4369 s
[i
]->s
.k
= again
- i
- 1;
4374 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
4376 s
[fix2
]->s
.jt
= s
[end
];
4377 s
[fix4
]->s
.jf
= s
[end
];
4378 s
[fix5
]->s
.jt
= s
[end
];
4385 for (i
= 0; i
< max
- 1; i
++)
4386 s
[i
]->next
= s
[i
+ 1];
4387 s
[max
- 1]->next
= NULL
;
4392 b
= new_block(JMP(BPF_JEQ
));
4393 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4404 * Generate code that checks whether the packet is a packet for protocol
4405 * <proto> and whether the type field in that protocol's header has
4406 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4407 * IP packet and checks the protocol number in the IP header against <v>.
4409 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4410 * against Q_IP and Q_IPV6.
4412 static struct block
*
4413 gen_proto(v
, proto
, dir
)
4418 struct block
*b0
, *b1
;
4420 if (dir
!= Q_DEFAULT
)
4421 bpf_error("direction applied to 'proto'");
4426 b0
= gen_proto(v
, Q_IP
, dir
);
4427 b1
= gen_proto(v
, Q_IPV6
, dir
);
4435 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4436 * not LLC encapsulation with LLCSAP_IP.
4438 * For IEEE 802 networks - which includes 802.5 token ring
4439 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4440 * says that SNAP encapsulation is used, not LLC encapsulation
4443 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4444 * RFC 2225 say that SNAP encapsulation is used, not LLC
4445 * encapsulation with LLCSAP_IP.
4447 * So we always check for ETHERTYPE_IP.
4450 b0
= gen_linktype(ETHERTYPE_IP
);
4452 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4454 b1
= gen_protochain(v
, Q_IP
);
4464 * Frame Relay packets typically have an OSI
4465 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4466 * generates code to check for all the OSI
4467 * NLPIDs, so calling it and then adding a check
4468 * for the particular NLPID for which we're
4469 * looking is bogus, as we can just check for
4472 * What we check for is the NLPID and a frame
4473 * control field value of UI, i.e. 0x03 followed
4476 * XXX - assumes a 2-byte Frame Relay header with
4477 * DLCI and flags. What if the address is longer?
4479 * XXX - what about SNAP-encapsulated frames?
4481 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4487 * Cisco uses an Ethertype lookalike - for OSI,
4490 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4491 /* OSI in C-HDLC is stuffed with a fudge byte */
4492 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4497 b0
= gen_linktype(LLCSAP_ISONS
);
4498 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4504 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4506 * 4 is the offset of the PDU type relative to the IS-IS
4509 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4514 bpf_error("arp does not encapsulate another protocol");
4518 bpf_error("rarp does not encapsulate another protocol");
4522 bpf_error("atalk encapsulation is not specifiable");
4526 bpf_error("decnet encapsulation is not specifiable");
4530 bpf_error("sca does not encapsulate another protocol");
4534 bpf_error("lat does not encapsulate another protocol");
4538 bpf_error("moprc does not encapsulate another protocol");
4542 bpf_error("mopdl does not encapsulate another protocol");
4546 return gen_linktype(v
);
4549 bpf_error("'udp proto' is bogus");
4553 bpf_error("'tcp proto' is bogus");
4557 bpf_error("'sctp proto' is bogus");
4561 bpf_error("'icmp proto' is bogus");
4565 bpf_error("'igmp proto' is bogus");
4569 bpf_error("'igrp proto' is bogus");
4573 bpf_error("'pim proto' is bogus");
4577 bpf_error("'vrrp proto' is bogus");
4582 b0
= gen_linktype(ETHERTYPE_IPV6
);
4584 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4586 b1
= gen_protochain(v
, Q_IPV6
);
4592 bpf_error("'icmp6 proto' is bogus");
4596 bpf_error("'ah proto' is bogus");
4599 bpf_error("'ah proto' is bogus");
4602 bpf_error("'stp proto' is bogus");
4605 bpf_error("'ipx proto' is bogus");
4608 bpf_error("'netbeui proto' is bogus");
4611 bpf_error("'radio proto' is bogus");
4622 register const char *name
;
4625 int proto
= q
.proto
;
4629 bpf_u_int32 mask
, addr
;
4631 bpf_u_int32
**alist
;
4634 struct sockaddr_in
*sin
;
4635 struct sockaddr_in6
*sin6
;
4636 struct addrinfo
*res
, *res0
;
4637 struct in6_addr mask128
;
4639 struct block
*b
, *tmp
;
4640 int port
, real_proto
;
4646 addr
= pcap_nametonetaddr(name
);
4648 bpf_error("unknown network '%s'", name
);
4649 /* Left justify network addr and calculate its network mask */
4651 while (addr
&& (addr
& 0xff000000) == 0) {
4655 return gen_host(addr
, mask
, proto
, dir
);
4659 if (proto
== Q_LINK
) {
4663 eaddr
= pcap_ether_hostton(name
);
4666 "unknown ether host '%s'", name
);
4667 b
= gen_ehostop(eaddr
, dir
);
4672 eaddr
= pcap_ether_hostton(name
);
4675 "unknown FDDI host '%s'", name
);
4676 b
= gen_fhostop(eaddr
, dir
);
4681 eaddr
= pcap_ether_hostton(name
);
4684 "unknown token ring host '%s'", name
);
4685 b
= gen_thostop(eaddr
, dir
);
4689 case DLT_IEEE802_11
:
4690 case DLT_IEEE802_11_RADIO_AVS
:
4691 case DLT_IEEE802_11_RADIO
:
4692 case DLT_PRISM_HEADER
:
4693 eaddr
= pcap_ether_hostton(name
);
4696 "unknown 802.11 host '%s'", name
);
4697 b
= gen_wlanhostop(eaddr
, dir
);
4701 case DLT_IP_OVER_FC
:
4702 eaddr
= pcap_ether_hostton(name
);
4705 "unknown Fibre Channel host '%s'", name
);
4706 b
= gen_ipfchostop(eaddr
, dir
);
4715 * Check that the packet doesn't begin
4716 * with an LE Control marker. (We've
4717 * already generated a test for LANE.)
4719 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4723 eaddr
= pcap_ether_hostton(name
);
4726 "unknown ether host '%s'", name
);
4727 b
= gen_ehostop(eaddr
, dir
);
4733 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4734 } else if (proto
== Q_DECNET
) {
4735 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4737 * I don't think DECNET hosts can be multihomed, so
4738 * there is no need to build up a list of addresses
4740 return (gen_host(dn_addr
, 0, proto
, dir
));
4743 alist
= pcap_nametoaddr(name
);
4744 if (alist
== NULL
|| *alist
== NULL
)
4745 bpf_error("unknown host '%s'", name
);
4747 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4749 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4751 tmp
= gen_host(**alist
++, 0xffffffff,
4758 memset(&mask128
, 0xff, sizeof(mask128
));
4759 res0
= res
= pcap_nametoaddrinfo(name
);
4761 bpf_error("unknown host '%s'", name
);
4763 tproto
= tproto6
= proto
;
4764 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4768 for (res
= res0
; res
; res
= res
->ai_next
) {
4769 switch (res
->ai_family
) {
4771 if (tproto
== Q_IPV6
)
4774 sin
= (struct sockaddr_in
*)
4776 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4777 0xffffffff, tproto
, dir
);
4780 if (tproto6
== Q_IP
)
4783 sin6
= (struct sockaddr_in6
*)
4785 tmp
= gen_host6(&sin6
->sin6_addr
,
4786 &mask128
, tproto6
, dir
);
4797 bpf_error("unknown host '%s'%s", name
,
4798 (proto
== Q_DEFAULT
)
4800 : " for specified address family");
4807 if (proto
!= Q_DEFAULT
&&
4808 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4809 bpf_error("illegal qualifier of 'port'");
4810 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4811 bpf_error("unknown port '%s'", name
);
4812 if (proto
== Q_UDP
) {
4813 if (real_proto
== IPPROTO_TCP
)
4814 bpf_error("port '%s' is tcp", name
);
4815 else if (real_proto
== IPPROTO_SCTP
)
4816 bpf_error("port '%s' is sctp", name
);
4818 /* override PROTO_UNDEF */
4819 real_proto
= IPPROTO_UDP
;
4821 if (proto
== Q_TCP
) {
4822 if (real_proto
== IPPROTO_UDP
)
4823 bpf_error("port '%s' is udp", name
);
4825 else if (real_proto
== IPPROTO_SCTP
)
4826 bpf_error("port '%s' is sctp", name
);
4828 /* override PROTO_UNDEF */
4829 real_proto
= IPPROTO_TCP
;
4831 if (proto
== Q_SCTP
) {
4832 if (real_proto
== IPPROTO_UDP
)
4833 bpf_error("port '%s' is udp", name
);
4835 else if (real_proto
== IPPROTO_TCP
)
4836 bpf_error("port '%s' is tcp", name
);
4838 /* override PROTO_UNDEF */
4839 real_proto
= IPPROTO_SCTP
;
4842 return gen_port(port
, real_proto
, dir
);
4846 b
= gen_port(port
, real_proto
, dir
);
4847 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4853 if (proto
!= Q_DEFAULT
&&
4854 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4855 bpf_error("illegal qualifier of 'portrange'");
4856 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4857 bpf_error("unknown port in range '%s'", name
);
4858 if (proto
== Q_UDP
) {
4859 if (real_proto
== IPPROTO_TCP
)
4860 bpf_error("port in range '%s' is tcp", name
);
4861 else if (real_proto
== IPPROTO_SCTP
)
4862 bpf_error("port in range '%s' is sctp", name
);
4864 /* override PROTO_UNDEF */
4865 real_proto
= IPPROTO_UDP
;
4867 if (proto
== Q_TCP
) {
4868 if (real_proto
== IPPROTO_UDP
)
4869 bpf_error("port in range '%s' is udp", name
);
4870 else if (real_proto
== IPPROTO_SCTP
)
4871 bpf_error("port in range '%s' is sctp", name
);
4873 /* override PROTO_UNDEF */
4874 real_proto
= IPPROTO_TCP
;
4876 if (proto
== Q_SCTP
) {
4877 if (real_proto
== IPPROTO_UDP
)
4878 bpf_error("port in range '%s' is udp", name
);
4879 else if (real_proto
== IPPROTO_TCP
)
4880 bpf_error("port in range '%s' is tcp", name
);
4882 /* override PROTO_UNDEF */
4883 real_proto
= IPPROTO_SCTP
;
4886 return gen_portrange(port1
, port2
, real_proto
, dir
);
4890 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4891 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4898 eaddr
= pcap_ether_hostton(name
);
4900 bpf_error("unknown ether host: %s", name
);
4902 alist
= pcap_nametoaddr(name
);
4903 if (alist
== NULL
|| *alist
== NULL
)
4904 bpf_error("unknown host '%s'", name
);
4905 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4909 bpf_error("'gateway' not supported in this configuration");
4913 real_proto
= lookup_proto(name
, proto
);
4914 if (real_proto
>= 0)
4915 return gen_proto(real_proto
, proto
, dir
);
4917 bpf_error("unknown protocol: %s", name
);
4920 real_proto
= lookup_proto(name
, proto
);
4921 if (real_proto
>= 0)
4922 return gen_protochain(real_proto
, proto
, dir
);
4924 bpf_error("unknown protocol: %s", name
);
4936 gen_mcode(s1
, s2
, masklen
, q
)
4937 register const char *s1
, *s2
;
4938 register int masklen
;
4941 register int nlen
, mlen
;
4944 nlen
= __pcap_atoin(s1
, &n
);
4945 /* Promote short ipaddr */
4949 mlen
= __pcap_atoin(s2
, &m
);
4950 /* Promote short ipaddr */
4953 bpf_error("non-network bits set in \"%s mask %s\"",
4956 /* Convert mask len to mask */
4958 bpf_error("mask length must be <= 32");
4959 m
= 0xffffffff << (32 - masklen
);
4961 bpf_error("non-network bits set in \"%s/%d\"",
4968 return gen_host(n
, m
, q
.proto
, q
.dir
);
4971 bpf_error("Mask syntax for networks only");
4979 register const char *s
;
4984 int proto
= q
.proto
;
4990 else if (q
.proto
== Q_DECNET
)
4991 vlen
= __pcap_atodn(s
, &v
);
4993 vlen
= __pcap_atoin(s
, &v
);
5000 if (proto
== Q_DECNET
)
5001 return gen_host(v
, 0, proto
, dir
);
5002 else if (proto
== Q_LINK
) {
5003 bpf_error("illegal link layer address");
5006 if (s
== NULL
&& q
.addr
== Q_NET
) {
5007 /* Promote short net number */
5008 while (v
&& (v
& 0xff000000) == 0) {
5013 /* Promote short ipaddr */
5017 return gen_host(v
, mask
, proto
, dir
);
5022 proto
= IPPROTO_UDP
;
5023 else if (proto
== Q_TCP
)
5024 proto
= IPPROTO_TCP
;
5025 else if (proto
== Q_SCTP
)
5026 proto
= IPPROTO_SCTP
;
5027 else if (proto
== Q_DEFAULT
)
5028 proto
= PROTO_UNDEF
;
5030 bpf_error("illegal qualifier of 'port'");
5033 return gen_port((int)v
, proto
, dir
);
5037 b
= gen_port((int)v
, proto
, dir
);
5038 gen_or(gen_port6((int)v
, proto
, dir
), b
);
5045 proto
= IPPROTO_UDP
;
5046 else if (proto
== Q_TCP
)
5047 proto
= IPPROTO_TCP
;
5048 else if (proto
== Q_SCTP
)
5049 proto
= IPPROTO_SCTP
;
5050 else if (proto
== Q_DEFAULT
)
5051 proto
= PROTO_UNDEF
;
5053 bpf_error("illegal qualifier of 'portrange'");
5056 return gen_portrange((int)v
, (int)v
, proto
, dir
);
5060 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
5061 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
5067 bpf_error("'gateway' requires a name");
5071 return gen_proto((int)v
, proto
, dir
);
5074 return gen_protochain((int)v
, proto
, dir
);
5089 gen_mcode6(s1
, s2
, masklen
, q
)
5090 register const char *s1
, *s2
;
5091 register int masklen
;
5094 struct addrinfo
*res
;
5095 struct in6_addr
*addr
;
5096 struct in6_addr mask
;
5101 bpf_error("no mask %s supported", s2
);
5103 res
= pcap_nametoaddrinfo(s1
);
5105 bpf_error("invalid ip6 address %s", s1
);
5107 bpf_error("%s resolved to multiple address", s1
);
5108 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
5110 if (sizeof(mask
) * 8 < masklen
)
5111 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
5112 memset(&mask
, 0, sizeof(mask
));
5113 memset(&mask
, 0xff, masklen
/ 8);
5115 mask
.s6_addr
[masklen
/ 8] =
5116 (0xff << (8 - masklen
% 8)) & 0xff;
5119 a
= (u_int32_t
*)addr
;
5120 m
= (u_int32_t
*)&mask
;
5121 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
5122 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
5123 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
5131 bpf_error("Mask syntax for networks only");
5135 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
5140 bpf_error("invalid qualifier against IPv6 address");
5148 register const u_char
*eaddr
;
5151 struct block
*b
, *tmp
;
5153 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5154 if (linktype
== DLT_EN10MB
)
5155 return gen_ehostop(eaddr
, (int)q
.dir
);
5156 if (linktype
== DLT_FDDI
)
5157 return gen_fhostop(eaddr
, (int)q
.dir
);
5158 if (linktype
== DLT_IEEE802
)
5159 return gen_thostop(eaddr
, (int)q
.dir
);
5160 if (linktype
== DLT_IEEE802_11
||
5161 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5162 linktype
== DLT_IEEE802_11_RADIO
||
5163 linktype
== DLT_PRISM_HEADER
)
5164 return gen_wlanhostop(eaddr
, (int)q
.dir
);
5165 if (linktype
== DLT_SUNATM
&& is_lane
) {
5167 * Check that the packet doesn't begin with an
5168 * LE Control marker. (We've already generated
5171 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5176 * Now check the MAC address.
5178 b
= gen_ehostop(eaddr
, (int)q
.dir
);
5182 if (linktype
== DLT_IP_OVER_FC
)
5183 return gen_ipfchostop(eaddr
, (int)q
.dir
);
5184 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
5186 bpf_error("ethernet address used in non-ether expression");
5192 struct slist
*s0
, *s1
;
5195 * This is definitely not the best way to do this, but the
5196 * lists will rarely get long.
5203 static struct slist
*
5209 s
= new_stmt(BPF_LDX
|BPF_MEM
);
5214 static struct slist
*
5220 s
= new_stmt(BPF_LD
|BPF_MEM
);
5226 * Modify "index" to use the value stored into its register as an
5227 * offset relative to the beginning of the header for the protocol
5228 * "proto", and allocate a register and put an item "size" bytes long
5229 * (1, 2, or 4) at that offset into that register, making it the register
5233 gen_load(proto
, index
, size
)
5238 struct slist
*s
, *tmp
;
5240 int regno
= alloc_reg();
5242 free_reg(index
->regno
);
5246 bpf_error("data size must be 1, 2, or 4");
5262 bpf_error("unsupported index operation");
5266 * The offset is relative to the beginning of the packet
5267 * data, if we have a radio header. (If we don't, this
5270 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
5271 linktype
!= DLT_IEEE802_11_RADIO
&&
5272 linktype
!= DLT_PRISM_HEADER
)
5273 bpf_error("radio information not present in capture");
5276 * Load into the X register the offset computed into the
5277 * register specifed by "index".
5279 s
= xfer_to_x(index
);
5282 * Load the item at that offset.
5284 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5286 sappend(index
->s
, s
);
5291 * The offset is relative to the beginning of
5292 * the link-layer header.
5294 * XXX - what about ATM LANE? Should the index be
5295 * relative to the beginning of the AAL5 frame, so
5296 * that 0 refers to the beginning of the LE Control
5297 * field, or relative to the beginning of the LAN
5298 * frame, so that 0 refers, for Ethernet LANE, to
5299 * the beginning of the destination address?
5301 s
= gen_llprefixlen();
5304 * If "s" is non-null, it has code to arrange that the
5305 * X register contains the length of the prefix preceding
5306 * the link-layer header. Add to it the offset computed
5307 * into the register specified by "index", and move that
5308 * into the X register. Otherwise, just load into the X
5309 * register the offset computed into the register specifed
5313 sappend(s
, xfer_to_a(index
));
5314 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5315 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5317 s
= xfer_to_x(index
);
5320 * Load the item at the sum of the offset we've put in the
5321 * X register and the offset of the start of the link
5322 * layer header (which is 0 if the radio header is
5323 * variable-length; that header length is what we put
5324 * into the X register and then added to the index).
5326 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5329 sappend(index
->s
, s
);
5345 * The offset is relative to the beginning of
5346 * the network-layer header.
5347 * XXX - are there any cases where we want
5350 s
= gen_llprefixlen();
5353 * If "s" is non-null, it has code to arrange that the
5354 * X register contains the length of the prefix preceding
5355 * the link-layer header. Add to it the offset computed
5356 * into the register specified by "index", and move that
5357 * into the X register. Otherwise, just load into the X
5358 * register the offset computed into the register specifed
5362 sappend(s
, xfer_to_a(index
));
5363 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5364 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5366 s
= xfer_to_x(index
);
5369 * Load the item at the sum of the offset we've put in the
5370 * X register and the offset of the start of the network
5373 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
5376 sappend(index
->s
, s
);
5379 * Do the computation only if the packet contains
5380 * the protocol in question.
5382 b
= gen_proto_abbrev(proto
);
5384 gen_and(index
->b
, b
);
5397 * The offset is relative to the beginning of
5398 * the transport-layer header.
5399 * XXX - are there any cases where we want
5401 * XXX - we should, if we're built with
5402 * IPv6 support, generate code to load either
5403 * IPv4, IPv6, or both, as appropriate.
5405 s
= gen_loadx_iphdrlen();
5408 * The X register now contains the sum of the offset
5409 * of the beginning of the link-layer header and
5410 * the length of the network-layer header. Load
5411 * into the A register the offset relative to
5412 * the beginning of the transport layer header,
5413 * add the X register to that, move that to the
5414 * X register, and load with an offset from the
5415 * X register equal to the offset of the network
5416 * layer header relative to the beginning of
5417 * the link-layer header.
5419 sappend(s
, xfer_to_a(index
));
5420 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
5421 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
5422 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
5424 sappend(index
->s
, s
);
5427 * Do the computation only if the packet contains
5428 * the protocol in question - which is true only
5429 * if this is an IP datagram and is the first or
5430 * only fragment of that datagram.
5432 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
5434 gen_and(index
->b
, b
);
5436 gen_and(gen_proto_abbrev(Q_IP
), b
);
5442 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
5446 index
->regno
= regno
;
5447 s
= new_stmt(BPF_ST
);
5449 sappend(index
->s
, s
);
5455 gen_relation(code
, a0
, a1
, reversed
)
5457 struct arth
*a0
, *a1
;
5460 struct slist
*s0
, *s1
, *s2
;
5461 struct block
*b
, *tmp
;
5465 if (code
== BPF_JEQ
) {
5466 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
5467 b
= new_block(JMP(code
));
5471 b
= new_block(BPF_JMP
|code
|BPF_X
);
5477 sappend(a0
->s
, a1
->s
);
5481 free_reg(a0
->regno
);
5482 free_reg(a1
->regno
);
5484 /* 'and' together protocol checks */
5487 gen_and(a0
->b
, tmp
= a1
->b
);
5503 int regno
= alloc_reg();
5504 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
5507 s
= new_stmt(BPF_LD
|BPF_LEN
);
5508 s
->next
= new_stmt(BPF_ST
);
5509 s
->next
->s
.k
= regno
;
5524 a
= (struct arth
*)newchunk(sizeof(*a
));
5528 s
= new_stmt(BPF_LD
|BPF_IMM
);
5530 s
->next
= new_stmt(BPF_ST
);
5546 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5549 s
= new_stmt(BPF_ST
);
5557 gen_arth(code
, a0
, a1
)
5559 struct arth
*a0
, *a1
;
5561 struct slist
*s0
, *s1
, *s2
;
5565 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5570 sappend(a0
->s
, a1
->s
);
5572 free_reg(a0
->regno
);
5573 free_reg(a1
->regno
);
5575 s0
= new_stmt(BPF_ST
);
5576 a0
->regno
= s0
->s
.k
= alloc_reg();
5583 * Here we handle simple allocation of the scratch registers.
5584 * If too many registers are alloc'd, the allocator punts.
5586 static int regused
[BPF_MEMWORDS
];
5590 * Return the next free register.
5595 int n
= BPF_MEMWORDS
;
5598 if (regused
[curreg
])
5599 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5601 regused
[curreg
] = 1;
5605 bpf_error("too many registers needed to evaluate expression");
5610 * Return a register to the table so it can
5620 static struct block
*
5627 s
= new_stmt(BPF_LD
|BPF_LEN
);
5628 b
= new_block(JMP(jmp
));
5639 return gen_len(BPF_JGE
, n
);
5643 * Actually, this is less than or equal.
5651 b
= gen_len(BPF_JGT
, n
);
5658 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
5659 * the beginning of the link-layer header.
5660 * XXX - that means you can't test values in the radiotap header, but
5661 * as that header is difficult if not impossible to parse generally
5662 * without a loop, that might not be a severe problem. A new keyword
5663 * "radio" could be added for that, although what you'd really want
5664 * would be a way of testing particular radio header values, which
5665 * would generate code appropriate to the radio header in question.
5668 gen_byteop(op
, idx
, val
)
5679 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5682 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5686 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5690 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5694 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5698 b
= new_block(JMP(BPF_JEQ
));
5705 static u_char abroadcast
[] = { 0x0 };
5708 gen_broadcast(proto
)
5711 bpf_u_int32 hostmask
;
5712 struct block
*b0
, *b1
, *b2
;
5713 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5719 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5720 return gen_ahostop(abroadcast
, Q_DST
);
5721 if (linktype
== DLT_EN10MB
)
5722 return gen_ehostop(ebroadcast
, Q_DST
);
5723 if (linktype
== DLT_FDDI
)
5724 return gen_fhostop(ebroadcast
, Q_DST
);
5725 if (linktype
== DLT_IEEE802
)
5726 return gen_thostop(ebroadcast
, Q_DST
);
5727 if (linktype
== DLT_IEEE802_11
||
5728 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5729 linktype
== DLT_IEEE802_11_RADIO
||
5730 linktype
== DLT_PRISM_HEADER
)
5731 return gen_wlanhostop(ebroadcast
, Q_DST
);
5732 if (linktype
== DLT_IP_OVER_FC
)
5733 return gen_ipfchostop(ebroadcast
, Q_DST
);
5734 if (linktype
== DLT_SUNATM
&& is_lane
) {
5736 * Check that the packet doesn't begin with an
5737 * LE Control marker. (We've already generated
5740 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5745 * Now check the MAC address.
5747 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5751 bpf_error("not a broadcast link");
5755 b0
= gen_linktype(ETHERTYPE_IP
);
5756 hostmask
= ~netmask
;
5757 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5758 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5759 (bpf_int32
)(~0 & hostmask
), hostmask
);
5764 bpf_error("only link-layer/IP broadcast filters supported");
5769 * Generate code to test the low-order bit of a MAC address (that's
5770 * the bottom bit of the *first* byte).
5772 static struct block
*
5773 gen_mac_multicast(offset
)
5776 register struct block
*b0
;
5777 register struct slist
*s
;
5779 /* link[offset] & 1 != 0 */
5780 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5781 b0
= new_block(JMP(BPF_JSET
));
5788 gen_multicast(proto
)
5791 register struct block
*b0
, *b1
, *b2
;
5792 register struct slist
*s
;
5798 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5799 /* all ARCnet multicasts use the same address */
5800 return gen_ahostop(abroadcast
, Q_DST
);
5802 if (linktype
== DLT_EN10MB
) {
5803 /* ether[0] & 1 != 0 */
5804 return gen_mac_multicast(0);
5807 if (linktype
== DLT_FDDI
) {
5809 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5811 * XXX - was that referring to bit-order issues?
5813 /* fddi[1] & 1 != 0 */
5814 return gen_mac_multicast(1);
5817 if (linktype
== DLT_IEEE802
) {
5818 /* tr[2] & 1 != 0 */
5819 return gen_mac_multicast(2);
5822 if (linktype
== DLT_IEEE802_11
||
5823 linktype
== DLT_IEEE802_11_RADIO_AVS
||
5824 linktype
== DLT_IEEE802_11_RADIO
||
5825 linktype
== DLT_PRISM_HEADER
) {
5829 * For control frames, there is no DA.
5831 * For management frames, DA is at an
5832 * offset of 4 from the beginning of
5835 * For data frames, DA is at an offset
5836 * of 4 from the beginning of the packet
5837 * if To DS is clear and at an offset of
5838 * 16 from the beginning of the packet
5843 * Generate the tests to be done for data frames.
5845 * First, check for To DS set, i.e. "link[1] & 0x01".
5847 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5848 b1
= new_block(JMP(BPF_JSET
));
5849 b1
->s
.k
= 0x01; /* To DS */
5853 * If To DS is set, the DA is at 16.
5855 b0
= gen_mac_multicast(16);
5859 * Now, check for To DS not set, i.e. check
5860 * "!(link[1] & 0x01)".
5862 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5863 b2
= new_block(JMP(BPF_JSET
));
5864 b2
->s
.k
= 0x01; /* To DS */
5869 * If To DS is not set, the DA is at 4.
5871 b1
= gen_mac_multicast(4);
5875 * Now OR together the last two checks. That gives
5876 * the complete set of checks for data frames.
5881 * Now check for a data frame.
5882 * I.e, check "link[0] & 0x08".
5884 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5885 b1
= new_block(JMP(BPF_JSET
));
5890 * AND that with the checks done for data frames.
5895 * If the high-order bit of the type value is 0, this
5896 * is a management frame.
5897 * I.e, check "!(link[0] & 0x08)".
5899 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5900 b2
= new_block(JMP(BPF_JSET
));
5906 * For management frames, the DA is at 4.
5908 b1
= gen_mac_multicast(4);
5912 * OR that with the checks done for data frames.
5913 * That gives the checks done for management and
5919 * If the low-order bit of the type value is 1,
5920 * this is either a control frame or a frame
5921 * with a reserved type, and thus not a
5924 * I.e., check "!(link[0] & 0x04)".
5926 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5927 b1
= new_block(JMP(BPF_JSET
));
5933 * AND that with the checks for data and management
5940 if (linktype
== DLT_IP_OVER_FC
) {
5941 b0
= gen_mac_multicast(2);
5945 if (linktype
== DLT_SUNATM
&& is_lane
) {
5947 * Check that the packet doesn't begin with an
5948 * LE Control marker. (We've already generated
5951 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5955 /* ether[off_mac] & 1 != 0 */
5956 b0
= gen_mac_multicast(off_mac
);
5961 /* Link not known to support multicasts */
5965 b0
= gen_linktype(ETHERTYPE_IP
);
5966 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5972 b0
= gen_linktype(ETHERTYPE_IPV6
);
5973 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5978 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5983 * generate command for inbound/outbound. It's here so we can
5984 * make it link-type specific. 'dir' = 0 implies "inbound",
5985 * = 1 implies "outbound".
5991 register struct block
*b0
;
5994 * Only some data link types support inbound/outbound qualifiers.
5998 b0
= gen_relation(BPF_JEQ
,
5999 gen_load(Q_LINK
, gen_loadi(0), 1),
6007 * Match packets sent by this machine.
6009 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
6012 * Match packets sent to this machine.
6013 * (No broadcast or multicast packets, or
6014 * packets sent to some other machine and
6015 * received promiscuously.)
6017 * XXX - packets sent to other machines probably
6018 * shouldn't be matched, but what about broadcast
6019 * or multicast packets we received?
6021 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
6026 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
6027 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
6032 /* match outgoing packets */
6033 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
6035 /* match incoming packets */
6036 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
6040 case DLT_JUNIPER_MFR
:
6041 case DLT_JUNIPER_MLFR
:
6042 case DLT_JUNIPER_MLPPP
:
6043 case DLT_JUNIPER_ATM1
:
6044 case DLT_JUNIPER_ATM2
:
6045 case DLT_JUNIPER_PPPOE
:
6046 case DLT_JUNIPER_PPPOE_ATM
:
6047 case DLT_JUNIPER_GGSN
:
6048 case DLT_JUNIPER_ES
:
6049 case DLT_JUNIPER_MONITOR
:
6050 case DLT_JUNIPER_SERVICES
:
6051 /* juniper flags (including direction) are stored
6052 * the byte after the 3-byte magic number */
6054 /* match outgoing packets */
6055 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
6057 /* match incoming packets */
6058 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
6063 bpf_error("inbound/outbound not supported on linktype %d",
6071 /* PF firewall log matched interface */
6073 gen_pf_ifname(const char *ifname
)
6078 if (linktype
== DLT_PFLOG
) {
6079 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
6080 off
= offsetof(struct pfloghdr
, ifname
);
6082 bpf_error("ifname not supported on linktype 0x%x", linktype
);
6085 if (strlen(ifname
) >= len
) {
6086 bpf_error("ifname interface names can only be %d characters",
6090 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
6094 /* PF firewall log ruleset name */
6096 gen_pf_ruleset(char *ruleset
)
6100 if (linktype
!= DLT_PFLOG
) {
6101 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
6104 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
6105 bpf_error("ruleset names can only be %ld characters",
6106 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
6109 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
6110 strlen(ruleset
), (const u_char
*)ruleset
);
6114 /* PF firewall log rule number */
6120 if (linktype
== DLT_PFLOG
) {
6121 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
6124 bpf_error("rnr not supported on linktype 0x%x", linktype
);
6131 /* PF firewall log sub-rule number */
6133 gen_pf_srnr(int srnr
)
6137 if (linktype
!= DLT_PFLOG
) {
6138 bpf_error("srnr not supported on linktype 0x%x", linktype
);
6142 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
6147 /* PF firewall log reason code */
6149 gen_pf_reason(int reason
)
6153 if (linktype
== DLT_PFLOG
) {
6154 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
6157 bpf_error("reason not supported on linktype 0x%x", linktype
);
6164 /* PF firewall log action */
6166 gen_pf_action(int action
)
6170 if (linktype
== DLT_PFLOG
) {
6171 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
6174 bpf_error("action not supported on linktype 0x%x", linktype
);
6183 register const u_char
*eaddr
;
6186 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6187 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
6188 return gen_ahostop(eaddr
, (int)q
.dir
);
6190 bpf_error("ARCnet address used in non-arc expression");
6194 static struct block
*
6195 gen_ahostop(eaddr
, dir
)
6196 register const u_char
*eaddr
;
6199 register struct block
*b0
, *b1
;
6202 /* src comes first, different from Ethernet */
6204 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
6207 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
6210 b0
= gen_ahostop(eaddr
, Q_SRC
);
6211 b1
= gen_ahostop(eaddr
, Q_DST
);
6217 b0
= gen_ahostop(eaddr
, Q_SRC
);
6218 b1
= gen_ahostop(eaddr
, Q_DST
);
6227 * support IEEE 802.1Q VLAN trunk over ethernet
6233 struct block
*b0
,*b1
;
6236 * Change the offsets to point to the type and data fields within
6237 * the VLAN packet. Just increment the offsets, so that we
6238 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
6239 * capture VLAN 200 encapsulated within VLAN 100.
6241 * XXX - this is a bit of a kludge. If we were to split the
6242 * compiler into a parser that parses an expression and
6243 * generates an expression tree, and a code generator that
6244 * takes an expression tree (which could come from our
6245 * parser or from some other parser) and generates BPF code,
6246 * we could perhaps make the offsets parameters of routines
6247 * and, in the handler for an "AND" node, pass to subnodes
6248 * other than the VLAN node the adjusted offsets.
6250 * This would mean that "vlan" would, instead of changing the
6251 * behavior of *all* tests after it, change only the behavior
6252 * of tests ANDed with it. That would change the documented
6253 * semantics of "vlan", which might break some expressions.
6254 * However, it would mean that "(vlan and ip) or ip" would check
6255 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
6256 * checking only for VLAN-encapsulated IP, so that could still
6257 * be considered worth doing; it wouldn't break expressions
6258 * that are of the form "vlan and ..." or "vlan N and ...",
6259 * which I suspect are the most common expressions involving
6260 * "vlan". "vlan or ..." doesn't necessarily do what the user
6261 * would really want, now, as all the "or ..." tests would
6262 * be done assuming a VLAN, even though the "or" could be viewed
6263 * as meaning "or, if this isn't a VLAN packet...".
6265 orig_linktype
= off_linktype
; /* save original values */
6277 bpf_error("no VLAN support for data link type %d",
6282 /* check for VLAN */
6283 if (orig_linktype
!= (u_int
)-1)
6284 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
6286 bpf_error("no VLAN match after MPLS");
6288 /* If a specific VLAN is requested, check VLAN id */
6289 if (vlan_num
>= 0) {
6290 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
6306 struct block
*b0
,*b1
;
6309 * Change the offsets to point to the type and data fields within
6310 * the MPLS packet. Just increment the offsets, so that we
6311 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
6312 * capture packets with an outer label of 100000 and an inner
6315 * XXX - this is a bit of a kludge. See comments in gen_vlan().
6317 orig_linktype
= off_linktype
; /* save original values */
6320 if (label_stack_depth
> 0) {
6321 /* just match the bottom-of-stack bit clear */
6322 b0
= gen_mcmp(OR_LINK
, orig_nl
-2, BPF_B
, 0, 0x01);
6325 /* poison the linktype to make sure higher level
6326 * code generators don't try to match against IP related protocols like
6327 * Q_ARP, Q_RARP etc. */
6331 case DLT_C_HDLC
: /* fall through */
6333 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6334 (bpf_int32
)ETHERTYPE_MPLS
);
6338 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
6339 (bpf_int32
)PPP_MPLS_UCAST
);
6342 /* FIXME add other DLT_s ...
6343 * for Frame-Relay/and ATM this may get messy due to SNAP headers
6344 * leave it for now */
6347 bpf_error("no MPLS support for data link type %d",
6355 /* If a specific MPLS label is requested, check it */
6356 if (label_num
>= 0) {
6357 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
6358 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
6359 0xfffff000); /* only compare the first 20 bits */
6366 label_stack_depth
++;
6371 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
6383 bpf_error("'vpi' supported only on raw ATM");
6384 if (off_vpi
== (u_int
)-1)
6386 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
6392 bpf_error("'vci' supported only on raw ATM");
6393 if (off_vci
== (u_int
)-1)
6395 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
6400 if (off_proto
== (u_int
)-1)
6401 abort(); /* XXX - this isn't on FreeBSD */
6402 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
6407 if (off_payload
== (u_int
)-1)
6409 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
6410 0xffffffff, jtype
, reverse
, jvalue
);
6415 bpf_error("'callref' supported only on raw ATM");
6416 if (off_proto
== (u_int
)-1)
6418 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
6419 jtype
, reverse
, jvalue
);
6429 gen_atmtype_abbrev(type
)
6432 struct block
*b0
, *b1
;
6437 /* Get all packets in Meta signalling Circuit */
6439 bpf_error("'metac' supported only on raw ATM");
6440 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6441 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
6446 /* Get all packets in Broadcast Circuit*/
6448 bpf_error("'bcc' supported only on raw ATM");
6449 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6450 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
6455 /* Get all cells in Segment OAM F4 circuit*/
6457 bpf_error("'oam4sc' supported only on raw ATM");
6458 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6459 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6464 /* Get all cells in End-to-End OAM F4 Circuit*/
6466 bpf_error("'oam4ec' supported only on raw ATM");
6467 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6468 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6473 /* Get all packets in connection Signalling Circuit */
6475 bpf_error("'sc' supported only on raw ATM");
6476 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6477 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
6482 /* Get all packets in ILMI Circuit */
6484 bpf_error("'ilmic' supported only on raw ATM");
6485 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6486 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
6491 /* Get all LANE packets */
6493 bpf_error("'lane' supported only on raw ATM");
6494 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
6497 * Arrange that all subsequent tests assume LANE
6498 * rather than LLC-encapsulated packets, and set
6499 * the offsets appropriately for LANE-encapsulated
6502 * "off_mac" is the offset of the Ethernet header,
6503 * which is 2 bytes past the ATM pseudo-header
6504 * (skipping the pseudo-header and 2-byte LE Client
6505 * field). The other offsets are Ethernet offsets
6506 * relative to "off_mac".
6509 off_mac
= off_payload
+ 2; /* MAC header */
6510 off_linktype
= off_mac
+ 12;
6511 off_nl
= off_mac
+ 14; /* Ethernet II */
6512 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
6516 /* Get all LLC-encapsulated packets */
6518 bpf_error("'llc' supported only on raw ATM");
6519 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
6530 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
6537 bpf_u_int32 val1
, val2
, val3
;
6539 switch (mtp3field
) {
6542 if (off_sio
== (u_int
)-1)
6543 bpf_error("'sio' supported only on SS7");
6544 /* sio coded on 1 byte so max value 255 */
6546 bpf_error("sio value %u too big; max value = 255",
6548 b0
= gen_ncmp(OR_PACKET
, off_sio
, BPF_B
, 0xffffffff,
6549 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6553 if (off_opc
== (u_int
)-1)
6554 bpf_error("'opc' supported only on SS7");
6555 /* opc coded on 14 bits so max value 16383 */
6557 bpf_error("opc value %u too big; max value = 16383",
6559 /* the following instructions are made to convert jvalue
6560 * to the form used to write opc in an ss7 message*/
6561 val1
= jvalue
& 0x00003c00;
6563 val2
= jvalue
& 0x000003fc;
6565 val3
= jvalue
& 0x00000003;
6567 jvalue
= val1
+ val2
+ val3
;
6568 b0
= gen_ncmp(OR_PACKET
, off_opc
, BPF_W
, 0x00c0ff0f,
6569 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6573 if (off_dpc
== (u_int
)-1)
6574 bpf_error("'dpc' supported only on SS7");
6575 /* dpc coded on 14 bits so max value 16383 */
6577 bpf_error("dpc value %u too big; max value = 16383",
6579 /* the following instructions are made to convert jvalue
6580 * to the forme used to write dpc in an ss7 message*/
6581 val1
= jvalue
& 0x000000ff;
6583 val2
= jvalue
& 0x00003f00;
6585 jvalue
= val1
+ val2
;
6586 b0
= gen_ncmp(OR_PACKET
, off_dpc
, BPF_W
, 0xff3f0000,
6587 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
6591 if (off_sls
== (u_int
)-1)
6592 bpf_error("'sls' supported only on SS7");
6593 /* sls coded on 4 bits so max value 15 */
6595 bpf_error("sls value %u too big; max value = 15",
6597 /* the following instruction is made to convert jvalue
6598 * to the forme used to write sls in an ss7 message*/
6599 jvalue
= jvalue
<< 4;
6600 b0
= gen_ncmp(OR_PACKET
, off_sls
, BPF_B
, 0xf0,
6601 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
6610 static struct block
*
6611 gen_msg_abbrev(type
)
6617 * Q.2931 signalling protocol messages for handling virtual circuits
6618 * establishment and teardown
6623 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6627 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6631 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6635 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6639 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6642 case A_RELEASE_DONE
:
6643 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6653 gen_atmmulti_abbrev(type
)
6656 struct block
*b0
, *b1
;
6662 bpf_error("'oam' supported only on raw ATM");
6663 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6668 bpf_error("'oamf4' supported only on raw ATM");
6670 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6671 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6673 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6679 * Get Q.2931 signalling messages for switched
6680 * virtual connection
6683 bpf_error("'connectmsg' supported only on raw ATM");
6684 b0
= gen_msg_abbrev(A_SETUP
);
6685 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6687 b0
= gen_msg_abbrev(A_CONNECT
);
6689 b0
= gen_msg_abbrev(A_CONNECTACK
);
6691 b0
= gen_msg_abbrev(A_RELEASE
);
6693 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6695 b0
= gen_atmtype_abbrev(A_SC
);
6701 bpf_error("'metaconnect' supported only on raw ATM");
6702 b0
= gen_msg_abbrev(A_SETUP
);
6703 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6705 b0
= gen_msg_abbrev(A_CONNECT
);
6707 b0
= gen_msg_abbrev(A_RELEASE
);
6709 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6711 b0
= gen_atmtype_abbrev(A_METAC
);