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.12 2005-05-01 08:37:48 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;
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_LINK
, /* relative to the link-layer header */
139 OR_NET
, /* relative to the network-layer header */
140 OR_NET_NOSNAP
, /* relative to the network-layer header, with no SNAP header at the link layer */
141 OR_TRAN_IPV4
, /* relative to the transport-layer header, with IPv4 network layer */
142 OR_TRAN_IPV6
/* relative to the transport-layer header, with IPv6 network layer */
146 * We divy out chunks of memory rather than call malloc each time so
147 * we don't have to worry about leaking memory. It's probably
148 * not a big deal if all this memory was wasted but if this ever
149 * goes into a library that would probably not be a good idea.
151 * XXX - this *is* in a library....
154 #define CHUNK0SIZE 1024
160 static struct chunk chunks
[NCHUNKS
];
161 static int cur_chunk
;
163 static void *newchunk(u_int
);
164 static void freechunks(void);
165 static inline struct block
*new_block(int);
166 static inline struct slist
*new_stmt(int);
167 static struct block
*gen_retblk(int);
168 static inline void syntax(void);
170 static void backpatch(struct block
*, struct block
*);
171 static void merge(struct block
*, struct block
*);
172 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
173 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
174 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
175 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
176 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
177 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
179 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
180 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
181 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
182 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
183 static struct block
*gen_uncond(int);
184 static inline struct block
*gen_true(void);
185 static inline struct block
*gen_false(void);
186 static struct block
*gen_ether_linktype(int);
187 static struct block
*gen_linux_sll_linktype(int);
188 static struct block
*gen_linktype(int);
189 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
190 static struct block
*gen_llc_linktype(int);
191 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
193 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
195 static struct block
*gen_ahostop(const u_char
*, int);
196 static struct block
*gen_ehostop(const u_char
*, int);
197 static struct block
*gen_fhostop(const u_char
*, int);
198 static struct block
*gen_thostop(const u_char
*, int);
199 static struct block
*gen_wlanhostop(const u_char
*, int);
200 static struct block
*gen_ipfchostop(const u_char
*, int);
201 static struct block
*gen_dnhostop(bpf_u_int32
, int);
202 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
204 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
207 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
209 static struct block
*gen_ipfrag(void);
210 static struct block
*gen_portatom(int, bpf_int32
);
211 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
213 static struct block
*gen_portatom6(int, bpf_int32
);
214 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
216 struct block
*gen_portop(int, int, int);
217 static struct block
*gen_port(int, int, int);
218 struct block
*gen_portrangeop(int, int, int, int);
219 static struct block
*gen_portrange(int, int, int, int);
221 struct block
*gen_portop6(int, int, int);
222 static struct block
*gen_port6(int, int, int);
223 struct block
*gen_portrangeop6(int, int, int, int);
224 static struct block
*gen_portrange6(int, int, int, int);
226 static int lookup_proto(const char *, int);
227 static struct block
*gen_protochain(int, int, int);
228 static struct block
*gen_proto(int, int, int);
229 static struct slist
*xfer_to_x(struct arth
*);
230 static struct slist
*xfer_to_a(struct arth
*);
231 static struct block
*gen_mac_multicast(int);
232 static struct block
*gen_len(int, int);
234 static struct block
*gen_msg_abbrev(int type
);
245 /* XXX Round up to nearest long. */
246 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
248 /* XXX Round up to structure boundary. */
252 cp
= &chunks
[cur_chunk
];
253 if (n
> cp
->n_left
) {
254 ++cp
, k
= ++cur_chunk
;
256 bpf_error("out of memory");
257 size
= CHUNK0SIZE
<< k
;
258 cp
->m
= (void *)malloc(size
);
260 bpf_error("out of memory");
261 memset((char *)cp
->m
, 0, size
);
264 bpf_error("out of memory");
267 return (void *)((char *)cp
->m
+ cp
->n_left
);
276 for (i
= 0; i
< NCHUNKS
; ++i
)
277 if (chunks
[i
].m
!= NULL
) {
284 * A strdup whose allocations are freed after code generation is over.
288 register const char *s
;
290 int n
= strlen(s
) + 1;
291 char *cp
= newchunk(n
);
297 static inline struct block
*
303 p
= (struct block
*)newchunk(sizeof(*p
));
310 static inline struct slist
*
316 p
= (struct slist
*)newchunk(sizeof(*p
));
322 static struct block
*
326 struct block
*b
= new_block(BPF_RET
|BPF_K
);
335 bpf_error("syntax error in filter expression");
338 static bpf_u_int32 netmask
;
343 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
344 char *buf
, int optimize
, bpf_u_int32 mask
)
353 if (setjmp(top_ctx
)) {
361 snaplen
= pcap_snapshot(p
);
363 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
364 "snaplen of 0 rejects all packets");
368 lex_init(buf
? buf
: "");
376 root
= gen_retblk(snaplen
);
378 if (optimize
&& !no_optimize
) {
381 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
382 bpf_error("expression rejects all packets");
384 program
->bf_insns
= icode_to_fcode(root
, &len
);
385 program
->bf_len
= len
;
393 * entry point for using the compiler with no pcap open
394 * pass in all the stuff that is needed explicitly instead.
397 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
398 struct bpf_program
*program
,
399 char *buf
, int optimize
, bpf_u_int32 mask
)
404 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
407 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
413 * Clean up a "struct bpf_program" by freeing all the memory allocated
417 pcap_freecode(struct bpf_program
*program
)
420 if (program
->bf_insns
!= NULL
) {
421 free((char *)program
->bf_insns
);
422 program
->bf_insns
= NULL
;
427 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
428 * which of the jt and jf fields has been resolved and which is a pointer
429 * back to another unresolved block (or nil). At least one of the fields
430 * in each block is already resolved.
433 backpatch(list
, target
)
434 struct block
*list
, *target
;
451 * Merge the lists in b0 and b1, using the 'sense' field to indicate
452 * which of jt and jf is the link.
456 struct block
*b0
, *b1
;
458 register struct block
**p
= &b0
;
460 /* Find end of list. */
462 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
464 /* Concatenate the lists. */
472 backpatch(p
, gen_retblk(snaplen
));
473 p
->sense
= !p
->sense
;
474 backpatch(p
, gen_retblk(0));
480 struct block
*b0
, *b1
;
482 backpatch(b0
, b1
->head
);
483 b0
->sense
= !b0
->sense
;
484 b1
->sense
= !b1
->sense
;
486 b1
->sense
= !b1
->sense
;
492 struct block
*b0
, *b1
;
494 b0
->sense
= !b0
->sense
;
495 backpatch(b0
, b1
->head
);
496 b0
->sense
= !b0
->sense
;
505 b
->sense
= !b
->sense
;
508 static struct block
*
509 gen_cmp(offrel
, offset
, size
, v
)
510 enum e_offrel offrel
;
514 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
517 static struct block
*
518 gen_cmp_gt(offrel
, offset
, size
, v
)
519 enum e_offrel offrel
;
523 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
526 static struct block
*
527 gen_cmp_ge(offrel
, offset
, size
, v
)
528 enum e_offrel offrel
;
532 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
535 static struct block
*
536 gen_cmp_lt(offrel
, offset
, size
, v
)
537 enum e_offrel offrel
;
541 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
544 static struct block
*
545 gen_cmp_le(offrel
, offset
, size
, v
)
546 enum e_offrel offrel
;
550 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
553 static struct block
*
554 gen_mcmp(offrel
, offset
, size
, v
, mask
)
555 enum e_offrel offrel
;
560 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
563 static struct block
*
564 gen_bcmp(offrel
, offset
, size
, v
)
565 enum e_offrel offrel
;
566 register u_int offset
, size
;
567 register const u_char
*v
;
569 register struct block
*b
, *tmp
;
573 register const u_char
*p
= &v
[size
- 4];
574 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
575 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
577 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
584 register const u_char
*p
= &v
[size
- 2];
585 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
587 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
594 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
603 * AND the field of size "size" at offset "offset" relative to the header
604 * specified by "offrel" with "mask", and compare it with the value "v"
605 * with the test specified by "jtype"; if "reverse" is true, the test
606 * should test the opposite of "jtype".
608 static struct block
*
609 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
610 enum e_offrel offrel
;
612 bpf_u_int32 offset
, size
, mask
, jtype
;
615 struct slist
*s
, *s2
;
618 s
= gen_load_a(offrel
, offset
, size
);
620 if (mask
!= 0xffffffff) {
621 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
626 b
= new_block(JMP(jtype
));
629 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
635 * Various code constructs need to know the layout of the data link
636 * layer. These variables give the necessary offsets.
640 * This is the offset of the beginning of the MAC-layer header.
641 * It's usually 0, except for ATM LANE.
643 static u_int off_mac
;
646 * "off_linktype" is the offset to information in the link-layer header
647 * giving the packet type.
649 * For Ethernet, it's the offset of the Ethernet type field.
651 * For link-layer types that always use 802.2 headers, it's the
652 * offset of the LLC header.
654 * For PPP, it's the offset of the PPP type field.
656 * For Cisco HDLC, it's the offset of the CHDLC type field.
658 * For BSD loopback, it's the offset of the AF_ value.
660 * For Linux cooked sockets, it's the offset of the type field.
662 * It's set to -1 for no encapsulation, in which case, IP is assumed.
664 static u_int off_linktype
;
667 * TRUE if the link layer includes an ATM pseudo-header.
669 static int is_atm
= 0;
672 * TRUE if "lane" appeared in the filter; it causes us to generate
673 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
675 static int is_lane
= 0;
678 * These are offsets for the ATM pseudo-header.
680 static u_int off_vpi
;
681 static u_int off_vci
;
682 static u_int off_proto
;
685 * This is the offset of the first byte after the ATM pseudo_header,
686 * or -1 if there is no ATM pseudo-header.
688 static u_int off_payload
;
691 * These are offsets to the beginning of the network-layer header.
693 * If the link layer never uses 802.2 LLC:
695 * "off_nl" and "off_nl_nosnap" are the same.
697 * If the link layer always uses 802.2 LLC:
699 * "off_nl" is the offset if there's a SNAP header following
702 * "off_nl_nosnap" is the offset if there's no SNAP header.
704 * If the link layer is Ethernet:
706 * "off_nl" is the offset if the packet is an Ethernet II packet
707 * (we assume no 802.3+802.2+SNAP);
709 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
710 * with an 802.2 header following it.
713 static u_int off_nl_nosnap
;
721 linktype
= pcap_datalink(p
);
723 pcap_fddipad
= p
->fddipad
;
727 * Assume it's not raw ATM with a pseudo-header, for now.
744 off_nl
= 6; /* XXX in reality, variable! */
745 off_nl_nosnap
= 6; /* no 802.2 LLC */
748 case DLT_ARCNET_LINUX
:
750 off_nl
= 8; /* XXX in reality, variable! */
751 off_nl_nosnap
= 8; /* no 802.2 LLC */
756 off_nl
= 14; /* Ethernet II */
757 off_nl_nosnap
= 17; /* 802.3+802.2 */
762 * SLIP doesn't have a link level type. The 16 byte
763 * header is hacked into our SLIP driver.
767 off_nl_nosnap
= 16; /* no 802.2 LLC */
771 /* XXX this may be the same as the DLT_PPP_BSDOS case */
775 off_nl_nosnap
= 24; /* no 802.2 LLC */
782 off_nl_nosnap
= 4; /* no 802.2 LLC */
788 off_nl_nosnap
= 12; /* no 802.2 LLC */
793 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
794 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
797 off_nl_nosnap
= 4; /* no 802.2 LLC */
802 * This does no include the Ethernet header, and
803 * only covers session state.
807 off_nl_nosnap
= 8; /* no 802.2 LLC */
813 off_nl_nosnap
= 24; /* no 802.2 LLC */
818 * FDDI doesn't really have a link-level type field.
819 * We set "off_linktype" to the offset of the LLC header.
821 * To check for Ethernet types, we assume that SSAP = SNAP
822 * is being used and pick out the encapsulated Ethernet type.
823 * XXX - should we generate code to check for SNAP?
827 off_linktype
+= pcap_fddipad
;
829 off_nl
= 21; /* FDDI+802.2+SNAP */
830 off_nl_nosnap
= 16; /* FDDI+802.2 */
832 off_nl
+= pcap_fddipad
;
833 off_nl_nosnap
+= pcap_fddipad
;
839 * Token Ring doesn't really have a link-level type field.
840 * We set "off_linktype" to the offset of the LLC header.
842 * To check for Ethernet types, we assume that SSAP = SNAP
843 * is being used and pick out the encapsulated Ethernet type.
844 * XXX - should we generate code to check for SNAP?
846 * XXX - the header is actually variable-length.
847 * Some various Linux patched versions gave 38
848 * as "off_linktype" and 40 as "off_nl"; however,
849 * if a token ring packet has *no* routing
850 * information, i.e. is not source-routed, the correct
851 * values are 20 and 22, as they are in the vanilla code.
853 * A packet is source-routed iff the uppermost bit
854 * of the first byte of the source address, at an
855 * offset of 8, has the uppermost bit set. If the
856 * packet is source-routed, the total number of bytes
857 * of routing information is 2 plus bits 0x1F00 of
858 * the 16-bit value at an offset of 14 (shifted right
859 * 8 - figure out which byte that is).
862 off_nl
= 22; /* Token Ring+802.2+SNAP */
863 off_nl_nosnap
= 17; /* Token Ring+802.2 */
868 * 802.11 doesn't really have a link-level type field.
869 * We set "off_linktype" to the offset of the LLC header.
871 * To check for Ethernet types, we assume that SSAP = SNAP
872 * is being used and pick out the encapsulated Ethernet type.
873 * XXX - should we generate code to check for SNAP?
875 * XXX - the header is actually variable-length. We
876 * assume a 24-byte link-layer header, as appears in
877 * data frames in networks with no bridges. If the
878 * fromds and tods 802.11 header bits are both set,
879 * it's actually supposed to be 30 bytes.
882 off_nl
= 32; /* 802.11+802.2+SNAP */
883 off_nl_nosnap
= 27; /* 802.11+802.2 */
886 case DLT_PRISM_HEADER
:
888 * Same as 802.11, but with an additional header before
889 * the 802.11 header, containing a bunch of additional
890 * information including radio-level information.
892 * The header is 144 bytes long.
894 * XXX - same variable-length header problem; at least
895 * the Prism header is fixed-length.
897 off_linktype
= 144+24;
898 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
899 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
902 case DLT_IEEE802_11_RADIO_AVS
:
904 * Same as 802.11, but with an additional header before
905 * the 802.11 header, containing a bunch of additional
906 * information including radio-level information.
908 * The header is 64 bytes long, at least in its
909 * current incarnation.
911 * XXX - same variable-length header problem, only
912 * more so; this header is also variable-length,
913 * with the length being the 32-bit big-endian
914 * number at an offset of 4 from the beginning
915 * of the radio header.
917 off_linktype
= 64+24;
918 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
919 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
922 case DLT_IEEE802_11_RADIO
:
924 * Same as 802.11, but with an additional header before
925 * the 802.11 header, containing a bunch of additional
926 * information including radio-level information.
928 * XXX - same variable-length header problem, only
929 * even *more* so; this header is also variable-length,
930 * with the length being the 16-bit number at an offset
931 * of 2 from the beginning of the radio header, and it's
932 * device-dependent (different devices might supply
933 * different amounts of information), so we can't even
934 * assume a fixed length for the current version of the
937 * Therefore, currently, only raw "link[N:M]" filtering is
945 case DLT_ATM_RFC1483
:
946 case DLT_ATM_CLIP
: /* Linux ATM defines this */
948 * assume routed, non-ISO PDUs
949 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
952 off_nl
= 8; /* 802.2+SNAP */
953 off_nl_nosnap
= 3; /* 802.2 */
958 * Full Frontal ATM; you get AALn PDUs with an ATM
962 off_vpi
= SUNATM_VPI_POS
;
963 off_vci
= SUNATM_VCI_POS
;
964 off_proto
= PROTO_POS
;
965 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
966 off_payload
= SUNATM_PKT_BEGIN_POS
;
967 off_linktype
= off_payload
;
968 off_nl
= off_payload
+8; /* 802.2+SNAP */
969 off_nl_nosnap
= off_payload
+3; /* 802.2 */
975 off_nl_nosnap
= 0; /* no 802.2 LLC */
978 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
981 off_nl_nosnap
= 16; /* no 802.2 LLC */
986 * LocalTalk does have a 1-byte type field in the LLAP header,
987 * but really it just indicates whether there is a "short" or
988 * "long" DDP packet following.
992 off_nl_nosnap
= 0; /* no 802.2 LLC */
997 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
998 * link-level type field. We set "off_linktype" to the
999 * offset of the LLC header.
1001 * To check for Ethernet types, we assume that SSAP = SNAP
1002 * is being used and pick out the encapsulated Ethernet type.
1003 * XXX - should we generate code to check for SNAP? RFC
1004 * 2625 says SNAP should be used.
1007 off_nl
= 24; /* IPFC+802.2+SNAP */
1008 off_nl_nosnap
= 19; /* IPFC+802.2 */
1013 * XXX - we should set this to handle SNAP-encapsulated
1014 * frames (NLPID of 0x80).
1018 off_nl_nosnap
= 0; /* no 802.2 LLC */
1021 case DLT_APPLE_IP_OVER_IEEE1394
:
1024 off_nl_nosnap
= 18; /* no 802.2 LLC */
1027 case DLT_LINUX_IRDA
:
1029 * Currently, only raw "link[N:M]" filtering is supported.
1038 * Currently, only raw "link[N:M]" filtering is supported.
1045 case DLT_SYMANTEC_FIREWALL
:
1047 off_nl
= 44; /* Ethernet II */
1048 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1053 /* XXX read this from pf.h? */
1054 off_nl
= PFLOG_HDRLEN
;
1055 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1058 case DLT_JUNIPER_MLFR
:
1059 case DLT_JUNIPER_MLPPP
:
1062 off_nl_nosnap
= -1; /* no 802.2 LLC */
1065 case DLT_JUNIPER_ATM1
:
1066 off_linktype
= 4; /* in reality variable between 4-8 */
1071 case DLT_JUNIPER_ATM2
:
1072 off_linktype
= 8; /* in reality variable between 8-12 */
1085 case DLT_LINUX_LAPD
:
1087 * Currently, only raw "link[N:M]" filtering is supported.
1094 bpf_error("unknown data link type %d", linktype
);
1099 * Load a value relative to the beginning of the specified header.
1101 static struct slist
*
1102 gen_load_a(offrel
, offset
, size
)
1103 enum e_offrel offrel
;
1111 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1116 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1117 s
->s
.k
= off_nl
+ offset
;
1121 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1122 s
->s
.k
= off_nl_nosnap
+ offset
;
1127 * Load the X register with the length of the IPv4 header,
1130 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1134 * Load the item at {length of the link-layer header} +
1135 * {length of the IPv4 header} + {specified offset}.
1137 s
->next
= new_stmt(BPF_LD
|BPF_IND
|size
);
1138 s
->next
->s
.k
= off_nl
+ offset
;
1142 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1143 s
->s
.k
= off_nl
+ 40 + offset
;
1153 static struct block
*
1160 s
= new_stmt(BPF_LD
|BPF_IMM
);
1162 b
= new_block(JMP(BPF_JEQ
));
1168 static inline struct block
*
1171 return gen_uncond(1);
1174 static inline struct block
*
1177 return gen_uncond(0);
1181 * Byte-swap a 32-bit number.
1182 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1183 * big-endian platforms.)
1185 #define SWAPLONG(y) \
1186 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1189 * Generate code to match a particular packet type.
1191 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1192 * value, if <= ETHERMTU. We use that to determine whether to
1193 * match the type/length field or to check the type/length field for
1194 * a value <= ETHERMTU to see whether it's a type field and then do
1195 * the appropriate test.
1197 static struct block
*
1198 gen_ether_linktype(proto
)
1201 struct block
*b0
, *b1
;
1207 case LLCSAP_NETBEUI
:
1209 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1210 * so we check the DSAP and SSAP.
1212 * LLCSAP_IP checks for IP-over-802.2, rather
1213 * than IP-over-Ethernet or IP-over-SNAP.
1215 * XXX - should we check both the DSAP and the
1216 * SSAP, like this, or should we check just the
1217 * DSAP, as we do for other types <= ETHERMTU
1218 * (i.e., other SAP values)?
1220 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1222 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1223 ((proto
<< 8) | proto
));
1231 * Ethernet_II frames, which are Ethernet
1232 * frames with a frame type of ETHERTYPE_IPX;
1234 * Ethernet_802.3 frames, which are 802.3
1235 * frames (i.e., the type/length field is
1236 * a length field, <= ETHERMTU, rather than
1237 * a type field) with the first two bytes
1238 * after the Ethernet/802.3 header being
1241 * Ethernet_802.2 frames, which are 802.3
1242 * frames with an 802.2 LLC header and
1243 * with the IPX LSAP as the DSAP in the LLC
1246 * Ethernet_SNAP frames, which are 802.3
1247 * frames with an LLC header and a SNAP
1248 * header and with an OUI of 0x000000
1249 * (encapsulated Ethernet) and a protocol
1250 * ID of ETHERTYPE_IPX in the SNAP header.
1252 * XXX - should we generate the same code both
1253 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1257 * This generates code to check both for the
1258 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1260 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1261 (bpf_int32
)LLCSAP_IPX
);
1262 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1267 * Now we add code to check for SNAP frames with
1268 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1270 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1274 * Now we generate code to check for 802.3
1275 * frames in general.
1277 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1281 * Now add the check for 802.3 frames before the
1282 * check for Ethernet_802.2 and Ethernet_802.3,
1283 * as those checks should only be done on 802.3
1284 * frames, not on Ethernet frames.
1289 * Now add the check for Ethernet_II frames, and
1290 * do that before checking for the other frame
1293 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1294 (bpf_int32
)ETHERTYPE_IPX
);
1298 case ETHERTYPE_ATALK
:
1299 case ETHERTYPE_AARP
:
1301 * EtherTalk (AppleTalk protocols on Ethernet link
1302 * layer) may use 802.2 encapsulation.
1306 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1307 * we check for an Ethernet type field less than
1308 * 1500, which means it's an 802.3 length field.
1310 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1314 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1315 * SNAP packets with an organization code of
1316 * 0x080007 (Apple, for Appletalk) and a protocol
1317 * type of ETHERTYPE_ATALK (Appletalk).
1319 * 802.2-encapsulated ETHERTYPE_AARP packets are
1320 * SNAP packets with an organization code of
1321 * 0x000000 (encapsulated Ethernet) and a protocol
1322 * type of ETHERTYPE_AARP (Appletalk ARP).
1324 if (proto
== ETHERTYPE_ATALK
)
1325 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1326 else /* proto == ETHERTYPE_AARP */
1327 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1331 * Check for Ethernet encapsulation (Ethertalk
1332 * phase 1?); we just check for the Ethernet
1335 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1341 if (proto
<= ETHERMTU
) {
1343 * This is an LLC SAP value, so the frames
1344 * that match would be 802.2 frames.
1345 * Check that the frame is an 802.2 frame
1346 * (i.e., that the length/type field is
1347 * a length field, <= ETHERMTU) and
1348 * then check the DSAP.
1350 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1352 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1358 * This is an Ethernet type, so compare
1359 * the length/type field with it (if
1360 * the frame is an 802.2 frame, the length
1361 * field will be <= ETHERMTU, and, as
1362 * "proto" is > ETHERMTU, this test
1363 * will fail and the frame won't match,
1364 * which is what we want).
1366 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1373 * Generate code to match a particular packet type.
1375 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1376 * value, if <= ETHERMTU. We use that to determine whether to
1377 * match the type field or to check the type field for the special
1378 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1380 static struct block
*
1381 gen_linux_sll_linktype(proto
)
1384 struct block
*b0
, *b1
;
1390 case LLCSAP_NETBEUI
:
1392 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1393 * so we check the DSAP and SSAP.
1395 * LLCSAP_IP checks for IP-over-802.2, rather
1396 * than IP-over-Ethernet or IP-over-SNAP.
1398 * XXX - should we check both the DSAP and the
1399 * SSAP, like this, or should we check just the
1400 * DSAP, as we do for other types <= ETHERMTU
1401 * (i.e., other SAP values)?
1403 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1404 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1405 ((proto
<< 8) | proto
));
1411 * Ethernet_II frames, which are Ethernet
1412 * frames with a frame type of ETHERTYPE_IPX;
1414 * Ethernet_802.3 frames, which have a frame
1415 * type of LINUX_SLL_P_802_3;
1417 * Ethernet_802.2 frames, which are 802.3
1418 * frames with an 802.2 LLC header (i.e, have
1419 * a frame type of LINUX_SLL_P_802_2) and
1420 * with the IPX LSAP as the DSAP in the LLC
1423 * Ethernet_SNAP frames, which are 802.3
1424 * frames with an LLC header and a SNAP
1425 * header and with an OUI of 0x000000
1426 * (encapsulated Ethernet) and a protocol
1427 * ID of ETHERTYPE_IPX in the SNAP header.
1429 * First, do the checks on LINUX_SLL_P_802_2
1430 * frames; generate the check for either
1431 * Ethernet_802.2 or Ethernet_SNAP frames, and
1432 * then put a check for LINUX_SLL_P_802_2 frames
1435 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1436 (bpf_int32
)LLCSAP_IPX
);
1437 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1440 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1444 * Now check for 802.3 frames and OR that with
1445 * the previous test.
1447 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1451 * Now add the check for Ethernet_II frames, and
1452 * do that before checking for the other frame
1455 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1456 (bpf_int32
)ETHERTYPE_IPX
);
1460 case ETHERTYPE_ATALK
:
1461 case ETHERTYPE_AARP
:
1463 * EtherTalk (AppleTalk protocols on Ethernet link
1464 * layer) may use 802.2 encapsulation.
1468 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1469 * we check for the 802.2 protocol type in the
1470 * "Ethernet type" field.
1472 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1475 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1476 * SNAP packets with an organization code of
1477 * 0x080007 (Apple, for Appletalk) and a protocol
1478 * type of ETHERTYPE_ATALK (Appletalk).
1480 * 802.2-encapsulated ETHERTYPE_AARP packets are
1481 * SNAP packets with an organization code of
1482 * 0x000000 (encapsulated Ethernet) and a protocol
1483 * type of ETHERTYPE_AARP (Appletalk ARP).
1485 if (proto
== ETHERTYPE_ATALK
)
1486 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1488 else /* proto == ETHERTYPE_AARP */
1489 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1494 * Check for Ethernet encapsulation (Ethertalk
1495 * phase 1?); we just check for the Ethernet
1498 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1504 if (proto
<= ETHERMTU
) {
1506 * This is an LLC SAP value, so the frames
1507 * that match would be 802.2 frames.
1508 * Check for the 802.2 protocol type
1509 * in the "Ethernet type" field, and
1510 * then check the DSAP.
1512 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1514 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1520 * This is an Ethernet type, so compare
1521 * the length/type field with it (if
1522 * the frame is an 802.2 frame, the length
1523 * field will be <= ETHERMTU, and, as
1524 * "proto" is > ETHERMTU, this test
1525 * will fail and the frame won't match,
1526 * which is what we want).
1528 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1535 * Generate code to match a particular packet type by matching the
1536 * link-layer type field or fields in the 802.2 LLC header.
1538 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1539 * value, if <= ETHERMTU.
1541 static struct block
*
1545 struct block
*b0
, *b1
, *b2
;
1550 return gen_ether_linktype(proto
);
1558 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1562 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1569 case DLT_IEEE802_11
:
1570 case DLT_PRISM_HEADER
:
1571 case DLT_IEEE802_11_RADIO
:
1574 case DLT_ATM_RFC1483
:
1576 case DLT_IP_OVER_FC
:
1577 return gen_llc_linktype(proto
);
1583 * If "is_lane" is set, check for a LANE-encapsulated
1584 * version of this protocol, otherwise check for an
1585 * LLC-encapsulated version of this protocol.
1587 * We assume LANE means Ethernet, not Token Ring.
1591 * Check that the packet doesn't begin with an
1592 * LE Control marker. (We've already generated
1595 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1600 * Now generate an Ethernet test.
1602 b1
= gen_ether_linktype(proto
);
1607 * Check for LLC encapsulation and then check the
1610 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1611 b1
= gen_llc_linktype(proto
);
1619 return gen_linux_sll_linktype(proto
);
1624 case DLT_SLIP_BSDOS
:
1627 * These types don't provide any type field; packets
1630 * XXX - for IPv4, check for a version number of 4, and,
1631 * for IPv6, check for a version number of 6?
1637 case ETHERTYPE_IPV6
:
1639 return gen_true(); /* always true */
1642 return gen_false(); /* always false */
1649 case DLT_PPP_SERIAL
:
1652 * We use Ethernet protocol types inside libpcap;
1653 * map them to the corresponding PPP protocol types.
1662 case ETHERTYPE_IPV6
:
1671 case ETHERTYPE_ATALK
:
1685 * I'm assuming the "Bridging PDU"s that go
1686 * over PPP are Spanning Tree Protocol
1700 * We use Ethernet protocol types inside libpcap;
1701 * map them to the corresponding PPP protocol types.
1706 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
1707 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
1709 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
1714 case ETHERTYPE_IPV6
:
1724 case ETHERTYPE_ATALK
:
1738 * I'm assuming the "Bridging PDU"s that go
1739 * over PPP are Spanning Tree Protocol
1755 * For DLT_NULL, the link-layer header is a 32-bit
1756 * word containing an AF_ value in *host* byte order,
1757 * and for DLT_ENC, the link-layer header begins
1758 * with a 32-bit work containing an AF_ value in
1761 * In addition, if we're reading a saved capture file,
1762 * the host byte order in the capture may not be the
1763 * same as the host byte order on this machine.
1765 * For DLT_LOOP, the link-layer header is a 32-bit
1766 * word containing an AF_ value in *network* byte order.
1768 * XXX - AF_ values may, unfortunately, be platform-
1769 * dependent; for example, FreeBSD's AF_INET6 is 24
1770 * whilst NetBSD's and OpenBSD's is 26.
1772 * This means that, when reading a capture file, just
1773 * checking for our AF_INET6 value won't work if the
1774 * capture file came from another OS.
1783 case ETHERTYPE_IPV6
:
1790 * Not a type on which we support filtering.
1791 * XXX - support those that have AF_ values
1792 * #defined on this platform, at least?
1797 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1799 * The AF_ value is in host byte order, but
1800 * the BPF interpreter will convert it to
1801 * network byte order.
1803 * If this is a save file, and it's from a
1804 * machine with the opposite byte order to
1805 * ours, we byte-swap the AF_ value.
1807 * Then we run it through "htonl()", and
1808 * generate code to compare against the result.
1810 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1811 bpf_pcap
->sf
.swapped
)
1812 proto
= SWAPLONG(proto
);
1813 proto
= htonl(proto
);
1815 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
1819 * af field is host byte order in contrast to the rest of
1822 if (proto
== ETHERTYPE_IP
)
1823 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
1824 BPF_B
, (bpf_int32
)AF_INET
));
1826 else if (proto
== ETHERTYPE_IPV6
)
1827 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
1828 BPF_B
, (bpf_int32
)AF_INET6
));
1836 case DLT_ARCNET_LINUX
:
1838 * XXX should we check for first fragment if the protocol
1847 case ETHERTYPE_IPV6
:
1848 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1849 (bpf_int32
)ARCTYPE_INET6
));
1853 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1854 (bpf_int32
)ARCTYPE_IP
);
1855 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1856 (bpf_int32
)ARCTYPE_IP_OLD
);
1861 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1862 (bpf_int32
)ARCTYPE_ARP
);
1863 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1864 (bpf_int32
)ARCTYPE_ARP_OLD
);
1868 case ETHERTYPE_REVARP
:
1869 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1870 (bpf_int32
)ARCTYPE_REVARP
));
1872 case ETHERTYPE_ATALK
:
1873 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1874 (bpf_int32
)ARCTYPE_ATALK
));
1881 case ETHERTYPE_ATALK
:
1891 * XXX - assumes a 2-byte Frame Relay header with
1892 * DLCI and flags. What if the address is longer?
1898 * Check for the special NLPID for IP.
1900 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
1903 case ETHERTYPE_IPV6
:
1905 * Check for the special NLPID for IPv6.
1907 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
1912 * Check for several OSI protocols.
1914 * Frame Relay packets typically have an OSI
1915 * NLPID at the beginning; we check for each
1918 * What we check for is the NLPID and a frame
1919 * control field of UI, i.e. 0x03 followed
1922 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1923 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1924 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1935 case DLT_JUNIPER_MLFR
:
1936 case DLT_JUNIPER_MLPPP
:
1937 case DLT_JUNIPER_ATM1
:
1938 case DLT_JUNIPER_ATM2
:
1939 /* just lets verify the magic number for now -
1940 * on ATM we may have up to 6 different encapsulations on the wire
1941 * and need a lot of heuristics to figure out that the payload
1944 * FIXME encapsulation specific BPF_ filters
1946 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1948 case DLT_LINUX_IRDA
:
1949 bpf_error("IrDA link-layer type filtering not implemented");
1952 bpf_error("DOCSIS link-layer type filtering not implemented");
1954 case DLT_LINUX_LAPD
:
1955 bpf_error("LAPD link-layer type filtering not implemented");
1959 * All the types that have no encapsulation should either be
1960 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1961 * all packets are IP packets, or should be handled in some
1962 * special case, if none of them are (if some are and some
1963 * aren't, the lack of encapsulation is a problem, as we'd
1964 * have to find some other way of determining the packet type).
1966 * Therefore, if "off_linktype" is -1, there's an error.
1968 if (off_linktype
== (u_int
)-1)
1972 * Any type not handled above should always have an Ethernet
1973 * type at an offset of "off_linktype". (PPP is partially
1974 * handled above - the protocol type is mapped from the
1975 * Ethernet and LLC types we use internally to the corresponding
1976 * PPP type - but the PPP type is always specified by a value
1977 * at "off_linktype", so we don't have to do the code generation
1980 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1984 * Check for an LLC SNAP packet with a given organization code and
1985 * protocol type; we check the entire contents of the 802.2 LLC and
1986 * snap headers, checking for DSAP and SSAP of SNAP and a control
1987 * field of 0x03 in the LLC header, and for the specified organization
1988 * code and protocol type in the SNAP header.
1990 static struct block
*
1991 gen_snap(orgcode
, ptype
, offset
)
1992 bpf_u_int32 orgcode
;
1996 u_char snapblock
[8];
1998 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1999 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
2000 snapblock
[2] = 0x03; /* control = UI */
2001 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2002 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2003 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2004 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2005 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2006 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2010 * Generate code to match a particular packet type, for link-layer types
2011 * using 802.2 LLC headers.
2013 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2014 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2016 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2017 * value, if <= ETHERMTU. We use that to determine whether to
2018 * match the DSAP or both DSAP and LSAP or to check the OUI and
2019 * protocol ID in a SNAP header.
2021 static struct block
*
2022 gen_llc_linktype(proto
)
2026 * XXX - handle token-ring variable-length header.
2032 case LLCSAP_NETBEUI
:
2034 * XXX - should we check both the DSAP and the
2035 * SSAP, like this, or should we check just the
2036 * DSAP, as we do for other types <= ETHERMTU
2037 * (i.e., other SAP values)?
2039 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2040 ((proto
<< 8) | proto
));
2044 * XXX - are there ever SNAP frames for IPX on
2045 * non-Ethernet 802.x networks?
2047 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2048 (bpf_int32
)LLCSAP_IPX
);
2050 case ETHERTYPE_ATALK
:
2052 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2053 * SNAP packets with an organization code of
2054 * 0x080007 (Apple, for Appletalk) and a protocol
2055 * type of ETHERTYPE_ATALK (Appletalk).
2057 * XXX - check for an organization code of
2058 * encapsulated Ethernet as well?
2060 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2064 * XXX - we don't have to check for IPX 802.3
2065 * here, but should we check for the IPX Ethertype?
2067 if (proto
<= ETHERMTU
) {
2069 * This is an LLC SAP value, so check
2072 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2076 * This is an Ethernet type; we assume that it's
2077 * unlikely that it'll appear in the right place
2078 * at random, and therefore check only the
2079 * location that would hold the Ethernet type
2080 * in a SNAP frame with an organization code of
2081 * 0x000000 (encapsulated Ethernet).
2083 * XXX - if we were to check for the SNAP DSAP and
2084 * LSAP, as per XXX, and were also to check for an
2085 * organization code of 0x000000 (encapsulated
2086 * Ethernet), we'd do
2088 * return gen_snap(0x000000, proto,
2091 * here; for now, we don't, as per the above.
2092 * I don't know whether it's worth the extra CPU
2093 * time to do the right check or not.
2095 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2101 static struct block
*
2102 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2106 u_int src_off
, dst_off
;
2108 struct block
*b0
, *b1
;
2122 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2123 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2129 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2130 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2137 b0
= gen_linktype(proto
);
2138 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2144 static struct block
*
2145 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2146 struct in6_addr
*addr
;
2147 struct in6_addr
*mask
;
2149 u_int src_off
, dst_off
;
2151 struct block
*b0
, *b1
;
2166 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2167 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2173 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2174 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2181 /* this order is important */
2182 a
= (u_int32_t
*)addr
;
2183 m
= (u_int32_t
*)mask
;
2184 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2185 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2187 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2189 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2191 b0
= gen_linktype(proto
);
2197 static struct block
*
2198 gen_ehostop(eaddr
, dir
)
2199 register const u_char
*eaddr
;
2202 register struct block
*b0
, *b1
;
2206 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2209 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2212 b0
= gen_ehostop(eaddr
, Q_SRC
);
2213 b1
= gen_ehostop(eaddr
, Q_DST
);
2219 b0
= gen_ehostop(eaddr
, Q_SRC
);
2220 b1
= gen_ehostop(eaddr
, Q_DST
);
2229 * Like gen_ehostop, but for DLT_FDDI
2231 static struct block
*
2232 gen_fhostop(eaddr
, dir
)
2233 register const u_char
*eaddr
;
2236 struct block
*b0
, *b1
;
2241 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2243 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2248 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2250 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2254 b0
= gen_fhostop(eaddr
, Q_SRC
);
2255 b1
= gen_fhostop(eaddr
, Q_DST
);
2261 b0
= gen_fhostop(eaddr
, Q_SRC
);
2262 b1
= gen_fhostop(eaddr
, Q_DST
);
2271 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2273 static struct block
*
2274 gen_thostop(eaddr
, dir
)
2275 register const u_char
*eaddr
;
2278 register struct block
*b0
, *b1
;
2282 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2285 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2288 b0
= gen_thostop(eaddr
, Q_SRC
);
2289 b1
= gen_thostop(eaddr
, Q_DST
);
2295 b0
= gen_thostop(eaddr
, Q_SRC
);
2296 b1
= gen_thostop(eaddr
, Q_DST
);
2305 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2307 static struct block
*
2308 gen_wlanhostop(eaddr
, dir
)
2309 register const u_char
*eaddr
;
2312 register struct block
*b0
, *b1
, *b2
;
2313 register struct slist
*s
;
2320 * For control frames, there is no SA.
2322 * For management frames, SA is at an
2323 * offset of 10 from the beginning of
2326 * For data frames, SA is at an offset
2327 * of 10 from the beginning of the packet
2328 * if From DS is clear, at an offset of
2329 * 16 from the beginning of the packet
2330 * if From DS is set and To DS is clear,
2331 * and an offset of 24 from the beginning
2332 * of the packet if From DS is set and To DS
2337 * Generate the tests to be done for data frames
2340 * First, check for To DS set, i.e. check "link[1] & 0x01".
2342 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2343 b1
= new_block(JMP(BPF_JSET
));
2344 b1
->s
.k
= 0x01; /* To DS */
2348 * If To DS is set, the SA is at 24.
2350 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2354 * Now, check for To DS not set, i.e. check
2355 * "!(link[1] & 0x01)".
2357 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2358 b2
= new_block(JMP(BPF_JSET
));
2359 b2
->s
.k
= 0x01; /* To DS */
2364 * If To DS is not set, the SA is at 16.
2366 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2370 * Now OR together the last two checks. That gives
2371 * the complete set of checks for data frames with
2377 * Now check for From DS being set, and AND that with
2378 * the ORed-together checks.
2380 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2381 b1
= new_block(JMP(BPF_JSET
));
2382 b1
->s
.k
= 0x02; /* From DS */
2387 * Now check for data frames with From DS not set.
2389 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2390 b2
= new_block(JMP(BPF_JSET
));
2391 b2
->s
.k
= 0x02; /* From DS */
2396 * If From DS isn't set, the SA is at 10.
2398 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2402 * Now OR together the checks for data frames with
2403 * From DS not set and for data frames with From DS
2404 * set; that gives the checks done for data frames.
2409 * Now check for a data frame.
2410 * I.e, check "link[0] & 0x08".
2412 gen_load_a(OR_LINK
, 0, BPF_B
);
2413 b1
= new_block(JMP(BPF_JSET
));
2418 * AND that with the checks done for data frames.
2423 * If the high-order bit of the type value is 0, this
2424 * is a management frame.
2425 * I.e, check "!(link[0] & 0x08)".
2427 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2428 b2
= new_block(JMP(BPF_JSET
));
2434 * For management frames, the SA is at 10.
2436 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2440 * OR that with the checks done for data frames.
2441 * That gives the checks done for management and
2447 * If the low-order bit of the type value is 1,
2448 * this is either a control frame or a frame
2449 * with a reserved type, and thus not a
2452 * I.e., check "!(link[0] & 0x04)".
2454 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2455 b1
= new_block(JMP(BPF_JSET
));
2461 * AND that with the checks for data and management
2471 * For control frames, there is no DA.
2473 * For management frames, DA is at an
2474 * offset of 4 from the beginning of
2477 * For data frames, DA is at an offset
2478 * of 4 from the beginning of the packet
2479 * if To DS is clear and at an offset of
2480 * 16 from the beginning of the packet
2485 * Generate the tests to be done for data frames.
2487 * First, check for To DS set, i.e. "link[1] & 0x01".
2489 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2490 b1
= new_block(JMP(BPF_JSET
));
2491 b1
->s
.k
= 0x01; /* To DS */
2495 * If To DS is set, the DA is at 16.
2497 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2501 * Now, check for To DS not set, i.e. check
2502 * "!(link[1] & 0x01)".
2504 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2505 b2
= new_block(JMP(BPF_JSET
));
2506 b2
->s
.k
= 0x01; /* To DS */
2511 * If To DS is not set, the DA is at 4.
2513 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2517 * Now OR together the last two checks. That gives
2518 * the complete set of checks for data frames.
2523 * Now check for a data frame.
2524 * I.e, check "link[0] & 0x08".
2526 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2527 b1
= new_block(JMP(BPF_JSET
));
2532 * AND that with the checks done for data frames.
2537 * If the high-order bit of the type value is 0, this
2538 * is a management frame.
2539 * I.e, check "!(link[0] & 0x08)".
2541 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2542 b2
= new_block(JMP(BPF_JSET
));
2548 * For management frames, the DA is at 4.
2550 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2554 * OR that with the checks done for data frames.
2555 * That gives the checks done for management and
2561 * If the low-order bit of the type value is 1,
2562 * this is either a control frame or a frame
2563 * with a reserved type, and thus not a
2566 * I.e., check "!(link[0] & 0x04)".
2568 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2569 b1
= new_block(JMP(BPF_JSET
));
2575 * AND that with the checks for data and management
2582 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2583 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2589 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2590 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2599 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2600 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2601 * as the RFC states.)
2603 static struct block
*
2604 gen_ipfchostop(eaddr
, dir
)
2605 register const u_char
*eaddr
;
2608 register struct block
*b0
, *b1
;
2612 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2615 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2618 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2619 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2625 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2626 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2635 * This is quite tricky because there may be pad bytes in front of the
2636 * DECNET header, and then there are two possible data packet formats that
2637 * carry both src and dst addresses, plus 5 packet types in a format that
2638 * carries only the src node, plus 2 types that use a different format and
2639 * also carry just the src node.
2643 * Instead of doing those all right, we just look for data packets with
2644 * 0 or 1 bytes of padding. If you want to look at other packets, that
2645 * will require a lot more hacking.
2647 * To add support for filtering on DECNET "areas" (network numbers)
2648 * one would want to add a "mask" argument to this routine. That would
2649 * make the filter even more inefficient, although one could be clever
2650 * and not generate masking instructions if the mask is 0xFFFF.
2652 static struct block
*
2653 gen_dnhostop(addr
, dir
)
2657 struct block
*b0
, *b1
, *b2
, *tmp
;
2658 u_int offset_lh
; /* offset if long header is received */
2659 u_int offset_sh
; /* offset if short header is received */
2664 offset_sh
= 1; /* follows flags */
2665 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2669 offset_sh
= 3; /* follows flags, dstnode */
2670 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2674 /* Inefficient because we do our Calvinball dance twice */
2675 b0
= gen_dnhostop(addr
, Q_SRC
);
2676 b1
= gen_dnhostop(addr
, Q_DST
);
2682 /* Inefficient because we do our Calvinball dance twice */
2683 b0
= gen_dnhostop(addr
, Q_SRC
);
2684 b1
= gen_dnhostop(addr
, Q_DST
);
2689 bpf_error("ISO host filtering not implemented");
2694 b0
= gen_linktype(ETHERTYPE_DN
);
2695 /* Check for pad = 1, long header case */
2696 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2697 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2698 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
2699 BPF_H
, (bpf_int32
)ntohs(addr
));
2701 /* Check for pad = 0, long header case */
2702 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2703 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2706 /* Check for pad = 1, short header case */
2707 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2708 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2709 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2712 /* Check for pad = 0, short header case */
2713 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2714 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2718 /* Combine with test for linktype */
2723 static struct block
*
2724 gen_host(addr
, mask
, proto
, dir
)
2730 struct block
*b0
, *b1
;
2735 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2736 if (off_linktype
!= (u_int
)-1) {
2737 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2739 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2745 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
2748 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
2751 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
2754 bpf_error("'tcp' modifier applied to host");
2757 bpf_error("'sctp' modifier applied to host");
2760 bpf_error("'udp' modifier applied to host");
2763 bpf_error("'icmp' modifier applied to host");
2766 bpf_error("'igmp' modifier applied to host");
2769 bpf_error("'igrp' modifier applied to host");
2772 bpf_error("'pim' modifier applied to host");
2775 bpf_error("'vrrp' modifier applied to host");
2778 bpf_error("ATALK host filtering not implemented");
2781 bpf_error("AARP host filtering not implemented");
2784 return gen_dnhostop(addr
, dir
);
2787 bpf_error("SCA host filtering not implemented");
2790 bpf_error("LAT host filtering not implemented");
2793 bpf_error("MOPDL host filtering not implemented");
2796 bpf_error("MOPRC host filtering not implemented");
2800 bpf_error("'ip6' modifier applied to ip host");
2803 bpf_error("'icmp6' modifier applied to host");
2807 bpf_error("'ah' modifier applied to host");
2810 bpf_error("'esp' modifier applied to host");
2813 bpf_error("ISO host filtering not implemented");
2816 bpf_error("'esis' modifier applied to host");
2819 bpf_error("'isis' modifier applied to host");
2822 bpf_error("'clnp' modifier applied to host");
2825 bpf_error("'stp' modifier applied to host");
2828 bpf_error("IPX host filtering not implemented");
2831 bpf_error("'netbeui' modifier applied to host");
2840 static struct block
*
2841 gen_host6(addr
, mask
, proto
, dir
)
2842 struct in6_addr
*addr
;
2843 struct in6_addr
*mask
;
2850 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2853 bpf_error("'ip' modifier applied to ip6 host");
2856 bpf_error("'rarp' modifier applied to ip6 host");
2859 bpf_error("'arp' modifier applied to ip6 host");
2862 bpf_error("'sctp' modifier applied to host");
2865 bpf_error("'tcp' modifier applied to host");
2868 bpf_error("'udp' modifier applied to host");
2871 bpf_error("'icmp' modifier applied to host");
2874 bpf_error("'igmp' modifier applied to host");
2877 bpf_error("'igrp' modifier applied to host");
2880 bpf_error("'pim' modifier applied to host");
2883 bpf_error("'vrrp' modifier applied to host");
2886 bpf_error("ATALK host filtering not implemented");
2889 bpf_error("AARP host filtering not implemented");
2892 bpf_error("'decnet' modifier applied to ip6 host");
2895 bpf_error("SCA host filtering not implemented");
2898 bpf_error("LAT host filtering not implemented");
2901 bpf_error("MOPDL host filtering not implemented");
2904 bpf_error("MOPRC host filtering not implemented");
2907 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
2910 bpf_error("'icmp6' modifier applied to host");
2913 bpf_error("'ah' modifier applied to host");
2916 bpf_error("'esp' modifier applied to host");
2919 bpf_error("ISO host filtering not implemented");
2922 bpf_error("'esis' modifier applied to host");
2925 bpf_error("'isis' modifier applied to host");
2928 bpf_error("'clnp' modifier applied to host");
2931 bpf_error("'stp' modifier applied to host");
2934 bpf_error("IPX host filtering not implemented");
2937 bpf_error("'netbeui' modifier applied to host");
2947 static struct block
*
2948 gen_gateway(eaddr
, alist
, proto
, dir
)
2949 const u_char
*eaddr
;
2950 bpf_u_int32
**alist
;
2954 struct block
*b0
, *b1
, *tmp
;
2957 bpf_error("direction applied to 'gateway'");
2964 if (linktype
== DLT_EN10MB
)
2965 b0
= gen_ehostop(eaddr
, Q_OR
);
2966 else if (linktype
== DLT_FDDI
)
2967 b0
= gen_fhostop(eaddr
, Q_OR
);
2968 else if (linktype
== DLT_IEEE802
)
2969 b0
= gen_thostop(eaddr
, Q_OR
);
2970 else if (linktype
== DLT_IEEE802_11
)
2971 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2972 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2974 * Check that the packet doesn't begin with an
2975 * LE Control marker. (We've already generated
2978 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2983 * Now check the MAC address.
2985 b0
= gen_ehostop(eaddr
, Q_OR
);
2987 } else if (linktype
== DLT_IP_OVER_FC
)
2988 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2991 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2993 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2995 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3003 bpf_error("illegal modifier of 'gateway'");
3009 gen_proto_abbrev(proto
)
3018 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3020 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3026 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3028 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3034 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3036 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3042 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3045 #ifndef IPPROTO_IGMP
3046 #define IPPROTO_IGMP 2
3050 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3053 #ifndef IPPROTO_IGRP
3054 #define IPPROTO_IGRP 9
3057 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3061 #define IPPROTO_PIM 103
3065 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3067 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3072 #ifndef IPPROTO_VRRP
3073 #define IPPROTO_VRRP 112
3077 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3081 b1
= gen_linktype(ETHERTYPE_IP
);
3085 b1
= gen_linktype(ETHERTYPE_ARP
);
3089 b1
= gen_linktype(ETHERTYPE_REVARP
);
3093 bpf_error("link layer applied in wrong context");
3096 b1
= gen_linktype(ETHERTYPE_ATALK
);
3100 b1
= gen_linktype(ETHERTYPE_AARP
);
3104 b1
= gen_linktype(ETHERTYPE_DN
);
3108 b1
= gen_linktype(ETHERTYPE_SCA
);
3112 b1
= gen_linktype(ETHERTYPE_LAT
);
3116 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3120 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3125 b1
= gen_linktype(ETHERTYPE_IPV6
);
3128 #ifndef IPPROTO_ICMPV6
3129 #define IPPROTO_ICMPV6 58
3132 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3137 #define IPPROTO_AH 51
3140 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3142 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3148 #define IPPROTO_ESP 50
3151 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3153 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3159 b1
= gen_linktype(LLCSAP_ISONS
);
3163 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3167 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3170 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3171 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3172 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3174 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3176 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3178 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3182 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3183 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3184 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3186 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3188 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3190 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3194 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3195 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3196 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3198 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3203 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3204 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3209 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3210 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3212 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3214 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3219 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3220 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3225 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3226 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3231 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3235 b1
= gen_linktype(LLCSAP_8021D
);
3239 b1
= gen_linktype(LLCSAP_IPX
);
3243 b1
= gen_linktype(LLCSAP_NETBEUI
);
3252 static struct block
*
3259 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3260 b
= new_block(JMP(BPF_JSET
));
3268 static struct block
*
3269 gen_portatom(off
, v
)
3273 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3277 static struct block
*
3278 gen_portatom6(off
, v
)
3282 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3287 gen_portop(port
, proto
, dir
)
3288 int port
, proto
, dir
;
3290 struct block
*b0
, *b1
, *tmp
;
3292 /* ip proto 'proto' */
3293 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3299 b1
= gen_portatom(0, (bpf_int32
)port
);
3303 b1
= gen_portatom(2, (bpf_int32
)port
);
3308 tmp
= gen_portatom(0, (bpf_int32
)port
);
3309 b1
= gen_portatom(2, (bpf_int32
)port
);
3314 tmp
= gen_portatom(0, (bpf_int32
)port
);
3315 b1
= gen_portatom(2, (bpf_int32
)port
);
3327 static struct block
*
3328 gen_port(port
, ip_proto
, dir
)
3333 struct block
*b0
, *b1
, *tmp
;
3338 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3339 * not LLC encapsulation with LLCSAP_IP.
3341 * For IEEE 802 networks - which includes 802.5 token ring
3342 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3343 * says that SNAP encapsulation is used, not LLC encapsulation
3346 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3347 * RFC 2225 say that SNAP encapsulation is used, not LLC
3348 * encapsulation with LLCSAP_IP.
3350 * So we always check for ETHERTYPE_IP.
3352 b0
= gen_linktype(ETHERTYPE_IP
);
3358 b1
= gen_portop(port
, ip_proto
, dir
);
3362 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3363 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3365 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3378 gen_portop6(port
, proto
, dir
)
3379 int port
, proto
, dir
;
3381 struct block
*b0
, *b1
, *tmp
;
3383 /* ip6 proto 'proto' */
3384 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3388 b1
= gen_portatom6(0, (bpf_int32
)port
);
3392 b1
= gen_portatom6(2, (bpf_int32
)port
);
3397 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3398 b1
= gen_portatom6(2, (bpf_int32
)port
);
3403 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3404 b1
= gen_portatom6(2, (bpf_int32
)port
);
3416 static struct block
*
3417 gen_port6(port
, ip_proto
, dir
)
3422 struct block
*b0
, *b1
, *tmp
;
3424 /* link proto ip6 */
3425 b0
= gen_linktype(ETHERTYPE_IPV6
);
3431 b1
= gen_portop6(port
, ip_proto
, dir
);
3435 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3436 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3438 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3450 /* gen_portrange code */
3451 static struct block
*
3452 gen_portrangeatom(off
, v1
, v2
)
3456 struct block
*b1
, *b2
;
3460 * Reverse the order of the ports, so v1 is the lower one.
3469 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3470 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3478 gen_portrangeop(port1
, port2
, proto
, dir
)
3483 struct block
*b0
, *b1
, *tmp
;
3485 /* ip proto 'proto' */
3486 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3492 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3496 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3501 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3502 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3507 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3508 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3520 static struct block
*
3521 gen_portrange(port1
, port2
, ip_proto
, dir
)
3526 struct block
*b0
, *b1
, *tmp
;
3529 b0
= gen_linktype(ETHERTYPE_IP
);
3535 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3539 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3540 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3542 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3554 static struct block
*
3555 gen_portrangeatom6(off
, v1
, v2
)
3559 struct block
*b1
, *b2
;
3563 * Reverse the order of the ports, so v1 is the lower one.
3572 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3573 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3581 gen_portrangeop6(port1
, port2
, proto
, dir
)
3586 struct block
*b0
, *b1
, *tmp
;
3588 /* ip6 proto 'proto' */
3589 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3593 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3597 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3602 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3603 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3608 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3609 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3621 static struct block
*
3622 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3627 struct block
*b0
, *b1
, *tmp
;
3629 /* link proto ip6 */
3630 b0
= gen_linktype(ETHERTYPE_IPV6
);
3636 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3640 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3641 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3643 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3656 lookup_proto(name
, proto
)
3657 register const char *name
;
3667 v
= pcap_nametoproto(name
);
3668 if (v
== PROTO_UNDEF
)
3669 bpf_error("unknown ip proto '%s'", name
);
3673 /* XXX should look up h/w protocol type based on linktype */
3674 v
= pcap_nametoeproto(name
);
3675 if (v
== PROTO_UNDEF
) {
3676 v
= pcap_nametollc(name
);
3677 if (v
== PROTO_UNDEF
)
3678 bpf_error("unknown ether proto '%s'", name
);
3683 if (strcmp(name
, "esis") == 0)
3685 else if (strcmp(name
, "isis") == 0)
3687 else if (strcmp(name
, "clnp") == 0)
3690 bpf_error("unknown osi proto '%s'", name
);
3710 static struct block
*
3711 gen_protochain(v
, proto
, dir
)
3716 #ifdef NO_PROTOCHAIN
3717 return gen_proto(v
, proto
, dir
);
3719 struct block
*b0
, *b
;
3720 struct slist
*s
[100];
3721 int fix2
, fix3
, fix4
, fix5
;
3722 int ahcheck
, again
, end
;
3724 int reg2
= alloc_reg();
3726 memset(s
, 0, sizeof(s
));
3727 fix2
= fix3
= fix4
= fix5
= 0;
3734 b0
= gen_protochain(v
, Q_IP
, dir
);
3735 b
= gen_protochain(v
, Q_IPV6
, dir
);
3739 bpf_error("bad protocol applied for 'protochain'");
3743 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3746 * s[0] is a dummy entry to protect other BPF insn from damage
3747 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3748 * hard to find interdependency made by jump table fixup.
3751 s
[i
] = new_stmt(0); /*dummy*/
3756 b0
= gen_linktype(ETHERTYPE_IP
);
3759 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3760 s
[i
]->s
.k
= off_nl
+ 9;
3762 /* X = ip->ip_hl << 2 */
3763 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3769 b0
= gen_linktype(ETHERTYPE_IPV6
);
3771 /* A = ip6->ip_nxt */
3772 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3773 s
[i
]->s
.k
= off_nl
+ 6;
3775 /* X = sizeof(struct ip6_hdr) */
3776 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3782 bpf_error("unsupported proto to gen_protochain");
3786 /* again: if (A == v) goto end; else fall through; */
3788 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3790 s
[i
]->s
.jt
= NULL
; /*later*/
3791 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3795 #ifndef IPPROTO_NONE
3796 #define IPPROTO_NONE 59
3798 /* if (A == IPPROTO_NONE) goto end */
3799 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3800 s
[i
]->s
.jt
= NULL
; /*later*/
3801 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3802 s
[i
]->s
.k
= IPPROTO_NONE
;
3803 s
[fix5
]->s
.jf
= s
[i
];
3808 if (proto
== Q_IPV6
) {
3809 int v6start
, v6end
, v6advance
, j
;
3812 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3813 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3814 s
[i
]->s
.jt
= NULL
; /*later*/
3815 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3816 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3817 s
[fix2
]->s
.jf
= s
[i
];
3819 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3820 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3821 s
[i
]->s
.jt
= NULL
; /*later*/
3822 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3823 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3825 /* if (A == IPPROTO_ROUTING) goto v6advance */
3826 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3827 s
[i
]->s
.jt
= NULL
; /*later*/
3828 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3829 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3831 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3832 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3833 s
[i
]->s
.jt
= NULL
; /*later*/
3834 s
[i
]->s
.jf
= NULL
; /*later*/
3835 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3846 * X = X + (P[X + 1] + 1) * 8;
3849 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3851 /* A = P[X + packet head] */
3852 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3856 s
[i
] = new_stmt(BPF_ST
);
3860 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3863 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3867 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3869 /* A = P[X + packet head]; */
3870 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3874 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3878 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3882 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3885 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3889 /* goto again; (must use BPF_JA for backward jump) */
3890 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3891 s
[i
]->s
.k
= again
- i
- 1;
3892 s
[i
- 1]->s
.jf
= s
[i
];
3896 for (j
= v6start
; j
<= v6end
; j
++)
3897 s
[j
]->s
.jt
= s
[v6advance
];
3902 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3904 s
[fix2
]->s
.jf
= s
[i
];
3910 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3911 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3912 s
[i
]->s
.jt
= NULL
; /*later*/
3913 s
[i
]->s
.jf
= NULL
; /*later*/
3914 s
[i
]->s
.k
= IPPROTO_AH
;
3916 s
[fix3
]->s
.jf
= s
[ahcheck
];
3923 * X = X + (P[X + 1] + 2) * 4;
3926 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3928 /* A = P[X + packet head]; */
3929 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3933 s
[i
] = new_stmt(BPF_ST
);
3937 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3940 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3944 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3946 /* A = P[X + packet head] */
3947 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3951 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3955 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3959 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3962 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3966 /* goto again; (must use BPF_JA for backward jump) */
3967 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3968 s
[i
]->s
.k
= again
- i
- 1;
3973 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3975 s
[fix2
]->s
.jt
= s
[end
];
3976 s
[fix4
]->s
.jf
= s
[end
];
3977 s
[fix5
]->s
.jt
= s
[end
];
3984 for (i
= 0; i
< max
- 1; i
++)
3985 s
[i
]->next
= s
[i
+ 1];
3986 s
[max
- 1]->next
= NULL
;
3991 b
= new_block(JMP(BPF_JEQ
));
3992 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4003 * Generate code that checks whether the packet is a packet for protocol
4004 * <proto> and whether the type field in that protocol's header has
4005 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4006 * IP packet and checks the protocol number in the IP header against <v>.
4008 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4009 * against Q_IP and Q_IPV6.
4011 static struct block
*
4012 gen_proto(v
, proto
, dir
)
4017 struct block
*b0
, *b1
;
4019 if (dir
!= Q_DEFAULT
)
4020 bpf_error("direction applied to 'proto'");
4025 b0
= gen_proto(v
, Q_IP
, dir
);
4026 b1
= gen_proto(v
, Q_IPV6
, dir
);
4034 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4035 * not LLC encapsulation with LLCSAP_IP.
4037 * For IEEE 802 networks - which includes 802.5 token ring
4038 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4039 * says that SNAP encapsulation is used, not LLC encapsulation
4042 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4043 * RFC 2225 say that SNAP encapsulation is used, not LLC
4044 * encapsulation with LLCSAP_IP.
4046 * So we always check for ETHERTYPE_IP.
4048 b0
= gen_linktype(ETHERTYPE_IP
);
4050 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4052 b1
= gen_protochain(v
, Q_IP
);
4062 * Frame Relay packets typically have an OSI
4063 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4064 * generates code to check for all the OSI
4065 * NLPIDs, so calling it and then adding a check
4066 * for the particular NLPID for which we're
4067 * looking is bogus, as we can just check for
4070 * What we check for is the NLPID and a frame
4071 * control field value of UI, i.e. 0x03 followed
4074 * XXX - assumes a 2-byte Frame Relay header with
4075 * DLCI and flags. What if the address is longer?
4077 * XXX - what about SNAP-encapsulated frames?
4079 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4085 * Cisco uses an Ethertype lookalike - for OSI,
4088 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4089 /* OSI in C-HDLC is stuffed with a fudge byte */
4090 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4095 b0
= gen_linktype(LLCSAP_ISONS
);
4096 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4102 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4104 * 4 is the offset of the PDU type relative to the IS-IS
4107 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4112 bpf_error("arp does not encapsulate another protocol");
4116 bpf_error("rarp does not encapsulate another protocol");
4120 bpf_error("atalk encapsulation is not specifiable");
4124 bpf_error("decnet encapsulation is not specifiable");
4128 bpf_error("sca does not encapsulate another protocol");
4132 bpf_error("lat does not encapsulate another protocol");
4136 bpf_error("moprc does not encapsulate another protocol");
4140 bpf_error("mopdl does not encapsulate another protocol");
4144 return gen_linktype(v
);
4147 bpf_error("'udp proto' is bogus");
4151 bpf_error("'tcp proto' is bogus");
4155 bpf_error("'sctp proto' is bogus");
4159 bpf_error("'icmp proto' is bogus");
4163 bpf_error("'igmp proto' is bogus");
4167 bpf_error("'igrp proto' is bogus");
4171 bpf_error("'pim proto' is bogus");
4175 bpf_error("'vrrp proto' is bogus");
4180 b0
= gen_linktype(ETHERTYPE_IPV6
);
4182 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4184 b1
= gen_protochain(v
, Q_IPV6
);
4190 bpf_error("'icmp6 proto' is bogus");
4194 bpf_error("'ah proto' is bogus");
4197 bpf_error("'ah proto' is bogus");
4200 bpf_error("'stp proto' is bogus");
4203 bpf_error("'ipx proto' is bogus");
4206 bpf_error("'netbeui proto' is bogus");
4217 register const char *name
;
4220 int proto
= q
.proto
;
4224 bpf_u_int32 mask
, addr
;
4226 bpf_u_int32
**alist
;
4229 struct sockaddr_in
*sin
;
4230 struct sockaddr_in6
*sin6
;
4231 struct addrinfo
*res
, *res0
;
4232 struct in6_addr mask128
;
4234 struct block
*b
, *tmp
;
4235 int port
, real_proto
;
4241 addr
= pcap_nametonetaddr(name
);
4243 bpf_error("unknown network '%s'", name
);
4244 /* Left justify network addr and calculate its network mask */
4246 while (addr
&& (addr
& 0xff000000) == 0) {
4250 return gen_host(addr
, mask
, proto
, dir
);
4254 if (proto
== Q_LINK
) {
4258 eaddr
= pcap_ether_hostton(name
);
4261 "unknown ether host '%s'", name
);
4262 b
= gen_ehostop(eaddr
, dir
);
4267 eaddr
= pcap_ether_hostton(name
);
4270 "unknown FDDI host '%s'", name
);
4271 b
= gen_fhostop(eaddr
, dir
);
4276 eaddr
= pcap_ether_hostton(name
);
4279 "unknown token ring host '%s'", name
);
4280 b
= gen_thostop(eaddr
, dir
);
4284 case DLT_IEEE802_11
:
4285 eaddr
= pcap_ether_hostton(name
);
4288 "unknown 802.11 host '%s'", name
);
4289 b
= gen_wlanhostop(eaddr
, dir
);
4293 case DLT_IP_OVER_FC
:
4294 eaddr
= pcap_ether_hostton(name
);
4297 "unknown Fibre Channel host '%s'", name
);
4298 b
= gen_ipfchostop(eaddr
, dir
);
4307 * Check that the packet doesn't begin
4308 * with an LE Control marker. (We've
4309 * already generated a test for LANE.)
4311 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4315 eaddr
= pcap_ether_hostton(name
);
4318 "unknown ether host '%s'", name
);
4319 b
= gen_ehostop(eaddr
, dir
);
4325 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4326 } else if (proto
== Q_DECNET
) {
4327 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4329 * I don't think DECNET hosts can be multihomed, so
4330 * there is no need to build up a list of addresses
4332 return (gen_host(dn_addr
, 0, proto
, dir
));
4335 alist
= pcap_nametoaddr(name
);
4336 if (alist
== NULL
|| *alist
== NULL
)
4337 bpf_error("unknown host '%s'", name
);
4339 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4341 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4343 tmp
= gen_host(**alist
++, 0xffffffff,
4350 memset(&mask128
, 0xff, sizeof(mask128
));
4351 res0
= res
= pcap_nametoaddrinfo(name
);
4353 bpf_error("unknown host '%s'", name
);
4355 tproto
= tproto6
= proto
;
4356 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4360 for (res
= res0
; res
; res
= res
->ai_next
) {
4361 switch (res
->ai_family
) {
4363 if (tproto
== Q_IPV6
)
4366 sin
= (struct sockaddr_in
*)
4368 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4369 0xffffffff, tproto
, dir
);
4372 if (tproto6
== Q_IP
)
4375 sin6
= (struct sockaddr_in6
*)
4377 tmp
= gen_host6(&sin6
->sin6_addr
,
4378 &mask128
, tproto6
, dir
);
4389 bpf_error("unknown host '%s'%s", name
,
4390 (proto
== Q_DEFAULT
)
4392 : " for specified address family");
4399 if (proto
!= Q_DEFAULT
&&
4400 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4401 bpf_error("illegal qualifier of 'port'");
4402 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4403 bpf_error("unknown port '%s'", name
);
4404 if (proto
== Q_UDP
) {
4405 if (real_proto
== IPPROTO_TCP
)
4406 bpf_error("port '%s' is tcp", name
);
4407 else if (real_proto
== IPPROTO_SCTP
)
4408 bpf_error("port '%s' is sctp", name
);
4410 /* override PROTO_UNDEF */
4411 real_proto
= IPPROTO_UDP
;
4413 if (proto
== Q_TCP
) {
4414 if (real_proto
== IPPROTO_UDP
)
4415 bpf_error("port '%s' is udp", name
);
4417 else if (real_proto
== IPPROTO_SCTP
)
4418 bpf_error("port '%s' is sctp", name
);
4420 /* override PROTO_UNDEF */
4421 real_proto
= IPPROTO_TCP
;
4423 if (proto
== Q_SCTP
) {
4424 if (real_proto
== IPPROTO_UDP
)
4425 bpf_error("port '%s' is udp", name
);
4427 else if (real_proto
== IPPROTO_TCP
)
4428 bpf_error("port '%s' is tcp", name
);
4430 /* override PROTO_UNDEF */
4431 real_proto
= IPPROTO_SCTP
;
4434 return gen_port(port
, real_proto
, dir
);
4438 b
= gen_port(port
, real_proto
, dir
);
4439 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4445 if (proto
!= Q_DEFAULT
&&
4446 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4447 bpf_error("illegal qualifier of 'portrange'");
4448 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4449 bpf_error("unknown port in range '%s'", name
);
4450 if (proto
== Q_UDP
) {
4451 if (real_proto
== IPPROTO_TCP
)
4452 bpf_error("port in range '%s' is tcp", name
);
4453 else if (real_proto
== IPPROTO_SCTP
)
4454 bpf_error("port in range '%s' is sctp", name
);
4456 /* override PROTO_UNDEF */
4457 real_proto
= IPPROTO_UDP
;
4459 if (proto
== Q_TCP
) {
4460 if (real_proto
== IPPROTO_UDP
)
4461 bpf_error("port in range '%s' is udp", name
);
4462 else if (real_proto
== IPPROTO_SCTP
)
4463 bpf_error("port in range '%s' is sctp", name
);
4465 /* override PROTO_UNDEF */
4466 real_proto
= IPPROTO_TCP
;
4468 if (proto
== Q_SCTP
) {
4469 if (real_proto
== IPPROTO_UDP
)
4470 bpf_error("port in range '%s' is udp", name
);
4471 else if (real_proto
== IPPROTO_TCP
)
4472 bpf_error("port in range '%s' is tcp", name
);
4474 /* override PROTO_UNDEF */
4475 real_proto
= IPPROTO_SCTP
;
4478 return gen_portrange(port1
, port2
, real_proto
, dir
);
4482 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4483 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4490 eaddr
= pcap_ether_hostton(name
);
4492 bpf_error("unknown ether host: %s", name
);
4494 alist
= pcap_nametoaddr(name
);
4495 if (alist
== NULL
|| *alist
== NULL
)
4496 bpf_error("unknown host '%s'", name
);
4497 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4501 bpf_error("'gateway' not supported in this configuration");
4505 real_proto
= lookup_proto(name
, proto
);
4506 if (real_proto
>= 0)
4507 return gen_proto(real_proto
, proto
, dir
);
4509 bpf_error("unknown protocol: %s", name
);
4512 real_proto
= lookup_proto(name
, proto
);
4513 if (real_proto
>= 0)
4514 return gen_protochain(real_proto
, proto
, dir
);
4516 bpf_error("unknown protocol: %s", name
);
4528 gen_mcode(s1
, s2
, masklen
, q
)
4529 register const char *s1
, *s2
;
4530 register int masklen
;
4533 register int nlen
, mlen
;
4536 nlen
= __pcap_atoin(s1
, &n
);
4537 /* Promote short ipaddr */
4541 mlen
= __pcap_atoin(s2
, &m
);
4542 /* Promote short ipaddr */
4545 bpf_error("non-network bits set in \"%s mask %s\"",
4548 /* Convert mask len to mask */
4550 bpf_error("mask length must be <= 32");
4551 m
= 0xffffffff << (32 - masklen
);
4553 bpf_error("non-network bits set in \"%s/%d\"",
4560 return gen_host(n
, m
, q
.proto
, q
.dir
);
4563 bpf_error("Mask syntax for networks only");
4571 register const char *s
;
4576 int proto
= q
.proto
;
4582 else if (q
.proto
== Q_DECNET
)
4583 vlen
= __pcap_atodn(s
, &v
);
4585 vlen
= __pcap_atoin(s
, &v
);
4592 if (proto
== Q_DECNET
)
4593 return gen_host(v
, 0, proto
, dir
);
4594 else if (proto
== Q_LINK
) {
4595 bpf_error("illegal link layer address");
4598 if (s
== NULL
&& q
.addr
== Q_NET
) {
4599 /* Promote short net number */
4600 while (v
&& (v
& 0xff000000) == 0) {
4605 /* Promote short ipaddr */
4609 return gen_host(v
, mask
, proto
, dir
);
4614 proto
= IPPROTO_UDP
;
4615 else if (proto
== Q_TCP
)
4616 proto
= IPPROTO_TCP
;
4617 else if (proto
== Q_SCTP
)
4618 proto
= IPPROTO_SCTP
;
4619 else if (proto
== Q_DEFAULT
)
4620 proto
= PROTO_UNDEF
;
4622 bpf_error("illegal qualifier of 'port'");
4625 return gen_port((int)v
, proto
, dir
);
4629 b
= gen_port((int)v
, proto
, dir
);
4630 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4637 proto
= IPPROTO_UDP
;
4638 else if (proto
== Q_TCP
)
4639 proto
= IPPROTO_TCP
;
4640 else if (proto
== Q_SCTP
)
4641 proto
= IPPROTO_SCTP
;
4642 else if (proto
== Q_DEFAULT
)
4643 proto
= PROTO_UNDEF
;
4645 bpf_error("illegal qualifier of 'portrange'");
4648 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4652 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4653 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4659 bpf_error("'gateway' requires a name");
4663 return gen_proto((int)v
, proto
, dir
);
4666 return gen_protochain((int)v
, proto
, dir
);
4681 gen_mcode6(s1
, s2
, masklen
, q
)
4682 register const char *s1
, *s2
;
4683 register int masklen
;
4686 struct addrinfo
*res
;
4687 struct in6_addr
*addr
;
4688 struct in6_addr mask
;
4693 bpf_error("no mask %s supported", s2
);
4695 res
= pcap_nametoaddrinfo(s1
);
4697 bpf_error("invalid ip6 address %s", s1
);
4699 bpf_error("%s resolved to multiple address", s1
);
4700 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4702 if (sizeof(mask
) * 8 < masklen
)
4703 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4704 memset(&mask
, 0, sizeof(mask
));
4705 memset(&mask
, 0xff, masklen
/ 8);
4707 mask
.s6_addr
[masklen
/ 8] =
4708 (0xff << (8 - masklen
% 8)) & 0xff;
4711 a
= (u_int32_t
*)addr
;
4712 m
= (u_int32_t
*)&mask
;
4713 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4714 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4715 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4723 bpf_error("Mask syntax for networks only");
4727 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4732 bpf_error("invalid qualifier against IPv6 address");
4740 register const u_char
*eaddr
;
4743 struct block
*b
, *tmp
;
4745 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4746 if (linktype
== DLT_EN10MB
)
4747 return gen_ehostop(eaddr
, (int)q
.dir
);
4748 if (linktype
== DLT_FDDI
)
4749 return gen_fhostop(eaddr
, (int)q
.dir
);
4750 if (linktype
== DLT_IEEE802
)
4751 return gen_thostop(eaddr
, (int)q
.dir
);
4752 if (linktype
== DLT_IEEE802_11
)
4753 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4754 if (linktype
== DLT_SUNATM
&& is_lane
) {
4756 * Check that the packet doesn't begin with an
4757 * LE Control marker. (We've already generated
4760 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
4765 * Now check the MAC address.
4767 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4771 if (linktype
== DLT_IP_OVER_FC
)
4772 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4773 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4775 bpf_error("ethernet address used in non-ether expression");
4781 struct slist
*s0
, *s1
;
4784 * This is definitely not the best way to do this, but the
4785 * lists will rarely get long.
4792 static struct slist
*
4798 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4803 static struct slist
*
4809 s
= new_stmt(BPF_LD
|BPF_MEM
);
4815 gen_load(proto
, index
, size
)
4820 struct slist
*s
, *tmp
;
4822 int regno
= alloc_reg();
4824 free_reg(index
->regno
);
4828 bpf_error("data size must be 1, 2, or 4");
4844 bpf_error("unsupported index operation");
4848 * XXX - what about ATM LANE? Should the index be
4849 * relative to the beginning of the AAL5 frame, so
4850 * that 0 refers to the beginning of the LE Control
4851 * field, or relative to the beginning of the LAN
4852 * frame, so that 0 refers, for Ethernet LANE, to
4853 * the beginning of the destination address?
4855 s
= xfer_to_x(index
);
4856 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4858 sappend(index
->s
, s
);
4873 /* XXX Note that we assume a fixed link header here. */
4874 s
= xfer_to_x(index
);
4875 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4878 sappend(index
->s
, s
);
4880 b
= gen_proto_abbrev(proto
);
4882 gen_and(index
->b
, b
);
4894 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4896 sappend(s
, xfer_to_a(index
));
4897 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4898 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4899 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4901 sappend(index
->s
, s
);
4903 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4905 gen_and(index
->b
, b
);
4907 gen_and(gen_proto_abbrev(Q_IP
), b
);
4913 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4917 index
->regno
= regno
;
4918 s
= new_stmt(BPF_ST
);
4920 sappend(index
->s
, s
);
4926 gen_relation(code
, a0
, a1
, reversed
)
4928 struct arth
*a0
, *a1
;
4931 struct slist
*s0
, *s1
, *s2
;
4932 struct block
*b
, *tmp
;
4936 if (code
== BPF_JEQ
) {
4937 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4938 b
= new_block(JMP(code
));
4942 b
= new_block(BPF_JMP
|code
|BPF_X
);
4948 sappend(a0
->s
, a1
->s
);
4952 free_reg(a0
->regno
);
4953 free_reg(a1
->regno
);
4955 /* 'and' together protocol checks */
4958 gen_and(a0
->b
, tmp
= a1
->b
);
4974 int regno
= alloc_reg();
4975 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4978 s
= new_stmt(BPF_LD
|BPF_LEN
);
4979 s
->next
= new_stmt(BPF_ST
);
4980 s
->next
->s
.k
= regno
;
4995 a
= (struct arth
*)newchunk(sizeof(*a
));
4999 s
= new_stmt(BPF_LD
|BPF_IMM
);
5001 s
->next
= new_stmt(BPF_ST
);
5017 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5020 s
= new_stmt(BPF_ST
);
5028 gen_arth(code
, a0
, a1
)
5030 struct arth
*a0
, *a1
;
5032 struct slist
*s0
, *s1
, *s2
;
5036 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5041 sappend(a0
->s
, a1
->s
);
5043 free_reg(a0
->regno
);
5044 free_reg(a1
->regno
);
5046 s0
= new_stmt(BPF_ST
);
5047 a0
->regno
= s0
->s
.k
= alloc_reg();
5054 * Here we handle simple allocation of the scratch registers.
5055 * If too many registers are alloc'd, the allocator punts.
5057 static int regused
[BPF_MEMWORDS
];
5061 * Return the next free register.
5066 int n
= BPF_MEMWORDS
;
5069 if (regused
[curreg
])
5070 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5072 regused
[curreg
] = 1;
5076 bpf_error("too many registers needed to evaluate expression");
5081 * Return a register to the table so it can
5091 static struct block
*
5098 s
= new_stmt(BPF_LD
|BPF_LEN
);
5099 b
= new_block(JMP(jmp
));
5110 return gen_len(BPF_JGE
, n
);
5114 * Actually, this is less than or equal.
5122 b
= gen_len(BPF_JGT
, n
);
5129 gen_byteop(op
, idx
, val
)
5140 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5143 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5147 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5151 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5155 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5159 b
= new_block(JMP(BPF_JEQ
));
5166 static u_char abroadcast
[] = { 0x0 };
5169 gen_broadcast(proto
)
5172 bpf_u_int32 hostmask
;
5173 struct block
*b0
, *b1
, *b2
;
5174 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5180 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5181 return gen_ahostop(abroadcast
, Q_DST
);
5182 if (linktype
== DLT_EN10MB
)
5183 return gen_ehostop(ebroadcast
, Q_DST
);
5184 if (linktype
== DLT_FDDI
)
5185 return gen_fhostop(ebroadcast
, Q_DST
);
5186 if (linktype
== DLT_IEEE802
)
5187 return gen_thostop(ebroadcast
, Q_DST
);
5188 if (linktype
== DLT_IEEE802_11
)
5189 return gen_wlanhostop(ebroadcast
, Q_DST
);
5190 if (linktype
== DLT_IP_OVER_FC
)
5191 return gen_ipfchostop(ebroadcast
, Q_DST
);
5192 if (linktype
== DLT_SUNATM
&& is_lane
) {
5194 * Check that the packet doesn't begin with an
5195 * LE Control marker. (We've already generated
5198 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5203 * Now check the MAC address.
5205 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5209 bpf_error("not a broadcast link");
5213 b0
= gen_linktype(ETHERTYPE_IP
);
5214 hostmask
= ~netmask
;
5215 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5216 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5217 (bpf_int32
)(~0 & hostmask
), hostmask
);
5222 bpf_error("only link-layer/IP broadcast filters supported");
5227 * Generate code to test the low-order bit of a MAC address (that's
5228 * the bottom bit of the *first* byte).
5230 static struct block
*
5231 gen_mac_multicast(offset
)
5234 register struct block
*b0
;
5235 register struct slist
*s
;
5237 /* link[offset] & 1 != 0 */
5238 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5239 b0
= new_block(JMP(BPF_JSET
));
5246 gen_multicast(proto
)
5249 register struct block
*b0
, *b1
, *b2
;
5250 register struct slist
*s
;
5256 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5257 /* all ARCnet multicasts use the same address */
5258 return gen_ahostop(abroadcast
, Q_DST
);
5260 if (linktype
== DLT_EN10MB
) {
5261 /* ether[0] & 1 != 0 */
5262 return gen_mac_multicast(0);
5265 if (linktype
== DLT_FDDI
) {
5267 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5269 * XXX - was that referring to bit-order issues?
5271 /* fddi[1] & 1 != 0 */
5272 return gen_mac_multicast(1);
5275 if (linktype
== DLT_IEEE802
) {
5276 /* tr[2] & 1 != 0 */
5277 return gen_mac_multicast(2);
5280 if (linktype
== DLT_IEEE802_11
) {
5284 * For control frames, there is no DA.
5286 * For management frames, DA is at an
5287 * offset of 4 from the beginning of
5290 * For data frames, DA is at an offset
5291 * of 4 from the beginning of the packet
5292 * if To DS is clear and at an offset of
5293 * 16 from the beginning of the packet
5298 * Generate the tests to be done for data frames.
5300 * First, check for To DS set, i.e. "link[1] & 0x01".
5302 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5303 b1
= new_block(JMP(BPF_JSET
));
5304 b1
->s
.k
= 0x01; /* To DS */
5308 * If To DS is set, the DA is at 16.
5310 b0
= gen_mac_multicast(16);
5314 * Now, check for To DS not set, i.e. check
5315 * "!(link[1] & 0x01)".
5317 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5318 b2
= new_block(JMP(BPF_JSET
));
5319 b2
->s
.k
= 0x01; /* To DS */
5324 * If To DS is not set, the DA is at 4.
5326 b1
= gen_mac_multicast(4);
5330 * Now OR together the last two checks. That gives
5331 * the complete set of checks for data frames.
5336 * Now check for a data frame.
5337 * I.e, check "link[0] & 0x08".
5339 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5340 b1
= new_block(JMP(BPF_JSET
));
5345 * AND that with the checks done for data frames.
5350 * If the high-order bit of the type value is 0, this
5351 * is a management frame.
5352 * I.e, check "!(link[0] & 0x08)".
5354 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5355 b2
= new_block(JMP(BPF_JSET
));
5361 * For management frames, the DA is at 4.
5363 b1
= gen_mac_multicast(4);
5367 * OR that with the checks done for data frames.
5368 * That gives the checks done for management and
5374 * If the low-order bit of the type value is 1,
5375 * this is either a control frame or a frame
5376 * with a reserved type, and thus not a
5379 * I.e., check "!(link[0] & 0x04)".
5381 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5382 b1
= new_block(JMP(BPF_JSET
));
5388 * AND that with the checks for data and management
5395 if (linktype
== DLT_IP_OVER_FC
) {
5396 b0
= gen_mac_multicast(2);
5400 if (linktype
== DLT_SUNATM
&& is_lane
) {
5402 * Check that the packet doesn't begin with an
5403 * LE Control marker. (We've already generated
5406 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5410 /* ether[off_mac] & 1 != 0 */
5411 b0
= gen_mac_multicast(off_mac
);
5416 /* Link not known to support multicasts */
5420 b0
= gen_linktype(ETHERTYPE_IP
);
5421 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5427 b0
= gen_linktype(ETHERTYPE_IPV6
);
5428 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5433 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5438 * generate command for inbound/outbound. It's here so we can
5439 * make it link-type specific. 'dir' = 0 implies "inbound",
5440 * = 1 implies "outbound".
5446 register struct block
*b0
;
5449 * Only some data link types support inbound/outbound qualifiers.
5453 b0
= gen_relation(BPF_JEQ
,
5454 gen_load(Q_LINK
, gen_loadi(0), 1),
5462 * Match packets sent by this machine.
5464 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
5467 * Match packets sent to this machine.
5468 * (No broadcast or multicast packets, or
5469 * packets sent to some other machine and
5470 * received promiscuously.)
5472 * XXX - packets sent to other machines probably
5473 * shouldn't be matched, but what about broadcast
5474 * or multicast packets we received?
5476 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
5481 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
5482 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5487 /* match outgoing packets */
5488 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
5490 /* match incoming packets */
5491 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
5495 case DLT_JUNIPER_MLFR
:
5496 case DLT_JUNIPER_MLPPP
:
5497 case DLT_JUNIPER_ATM1
:
5498 case DLT_JUNIPER_ATM2
:
5499 /* juniper flags (including direction) are stored
5500 * the byte after the 3-byte magic number */
5502 /* match outgoing packets */
5503 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
5505 /* match incoming packets */
5506 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
5511 bpf_error("inbound/outbound not supported on linktype %d",
5519 /* PF firewall log matched interface */
5521 gen_pf_ifname(const char *ifname
)
5526 if (linktype
== DLT_PFLOG
) {
5527 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5528 off
= offsetof(struct pfloghdr
, ifname
);
5530 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5533 if (strlen(ifname
) >= len
) {
5534 bpf_error("ifname interface names can only be %d characters",
5538 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
5542 /* PF firewall log matched interface */
5544 gen_pf_ruleset(char *ruleset
)
5548 if (linktype
!= DLT_PFLOG
) {
5549 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5552 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5553 bpf_error("ruleset names can only be %ld characters",
5554 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5557 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
5558 strlen(ruleset
), (const u_char
*)ruleset
);
5562 /* PF firewall log rule number */
5568 if (linktype
== DLT_PFLOG
) {
5569 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5572 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5579 /* PF firewall log sub-rule number */
5581 gen_pf_srnr(int srnr
)
5585 if (linktype
!= DLT_PFLOG
) {
5586 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5590 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5595 /* PF firewall log reason code */
5597 gen_pf_reason(int reason
)
5601 if (linktype
== DLT_PFLOG
) {
5602 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
5605 bpf_error("reason not supported on linktype 0x%x", linktype
);
5612 /* PF firewall log action */
5614 gen_pf_action(int action
)
5618 if (linktype
== DLT_PFLOG
) {
5619 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
5622 bpf_error("action not supported on linktype 0x%x", linktype
);
5631 register const u_char
*eaddr
;
5634 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5635 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5636 return gen_ahostop(eaddr
, (int)q
.dir
);
5638 bpf_error("ARCnet address used in non-arc expression");
5642 static struct block
*
5643 gen_ahostop(eaddr
, dir
)
5644 register const u_char
*eaddr
;
5647 register struct block
*b0
, *b1
;
5650 /* src comes first, different from Ethernet */
5652 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
5655 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
5658 b0
= gen_ahostop(eaddr
, Q_SRC
);
5659 b1
= gen_ahostop(eaddr
, Q_DST
);
5665 b0
= gen_ahostop(eaddr
, Q_SRC
);
5666 b1
= gen_ahostop(eaddr
, Q_DST
);
5675 * support IEEE 802.1Q VLAN trunk over ethernet
5684 * Change the offsets to point to the type and data fields within
5685 * the VLAN packet. Just increment the offsets, so that we
5686 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5687 * capture VLAN 200 encapsulated within VLAN 100.
5689 * XXX - this is a bit of a kludge. If we were to split the
5690 * compiler into a parser that parses an expression and
5691 * generates an expression tree, and a code generator that
5692 * takes an expression tree (which could come from our
5693 * parser or from some other parser) and generates BPF code,
5694 * we could perhaps make the offsets parameters of routines
5695 * and, in the handler for an "AND" node, pass to subnodes
5696 * other than the VLAN node the adjusted offsets.
5698 * This would mean that "vlan" would, instead of changing the
5699 * behavior of *all* tests after it, change only the behavior
5700 * of tests ANDed with it. That would change the documented
5701 * semantics of "vlan", which might break some expressions.
5702 * However, it would mean that "(vlan and ip) or ip" would check
5703 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5704 * checking only for VLAN-encapsulated IP, so that could still
5705 * be considered worth doing; it wouldn't break expressions
5706 * that are of the form "vlan and ..." or "vlan N and ...",
5707 * which I suspect are the most common expressions involving
5708 * "vlan". "vlan or ..." doesn't necessarily do what the user
5709 * would really want, now, as all the "or ..." tests would
5710 * be done assuming a VLAN, even though the "or" could be viewed
5711 * as meaning "or, if this isn't a VLAN packet...".
5713 orig_linktype
= off_linktype
; /* save original values */
5725 bpf_error("no VLAN support for data link type %d",
5730 /* check for VLAN */
5731 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5733 /* If a specific VLAN is requested, check VLAN id */
5734 if (vlan_num
>= 0) {
5737 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
5756 * Change the offsets to point to the type and data fields within
5757 * the MPLS packet. Just increment the offsets, so that we
5758 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5759 * capture packets with an outer label of 100000 and an inner
5762 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5764 orig_linktype
= off_linktype
; /* save original values */
5769 case DLT_C_HDLC
: /* fall through */
5775 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
5776 (bpf_int32
)ETHERTYPE_MPLS
);
5784 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
5785 (bpf_int32
)PPP_MPLS_UCAST
);
5788 /* FIXME add other DLT_s ...
5789 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5790 * leave it for now */
5793 bpf_error("no MPLS support for data link type %d",
5800 /* If a specific MPLS label is requested, check it */
5801 if (label_num
>= 0) {
5804 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5805 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
5806 0xfffff000); /* only compare the first 20 bits */
5815 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5827 bpf_error("'vpi' supported only on raw ATM");
5828 if (off_vpi
== (u_int
)-1)
5830 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
5836 bpf_error("'vci' supported only on raw ATM");
5837 if (off_vci
== (u_int
)-1)
5839 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, jtype
,
5844 if (off_proto
== (u_int
)-1)
5845 abort(); /* XXX - this isn't on FreeBSD */
5846 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, jtype
,
5851 if (off_payload
== (u_int
)-1)
5853 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
5854 0xffffffff, jtype
, reverse
, jvalue
);
5859 bpf_error("'callref' supported only on raw ATM");
5860 if (off_proto
== (u_int
)-1)
5862 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
5863 jtype
, reverse
, jvalue
);
5873 gen_atmtype_abbrev(type
)
5876 struct block
*b0
, *b1
;
5881 /* Get all packets in Meta signalling Circuit */
5883 bpf_error("'metac' supported only on raw ATM");
5884 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5885 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5890 /* Get all packets in Broadcast Circuit*/
5892 bpf_error("'bcc' supported only on raw ATM");
5893 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5894 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5899 /* Get all cells in Segment OAM F4 circuit*/
5901 bpf_error("'oam4sc' supported only on raw ATM");
5902 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5903 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5908 /* Get all cells in End-to-End OAM F4 Circuit*/
5910 bpf_error("'oam4ec' supported only on raw ATM");
5911 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5912 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5917 /* Get all packets in connection Signalling Circuit */
5919 bpf_error("'sc' supported only on raw ATM");
5920 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5921 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5926 /* Get all packets in ILMI Circuit */
5928 bpf_error("'ilmic' supported only on raw ATM");
5929 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5930 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5935 /* Get all LANE packets */
5937 bpf_error("'lane' supported only on raw ATM");
5938 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5941 * Arrange that all subsequent tests assume LANE
5942 * rather than LLC-encapsulated packets, and set
5943 * the offsets appropriately for LANE-encapsulated
5946 * "off_mac" is the offset of the Ethernet header,
5947 * which is 2 bytes past the ATM pseudo-header
5948 * (skipping the pseudo-header and 2-byte LE Client
5949 * field). The other offsets are Ethernet offsets
5950 * relative to "off_mac".
5953 off_mac
= off_payload
+ 2; /* MAC header */
5954 off_linktype
= off_mac
+ 12;
5955 off_nl
= off_mac
+ 14; /* Ethernet II */
5956 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5960 /* Get all LLC-encapsulated packets */
5962 bpf_error("'llc' supported only on raw ATM");
5963 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5974 static struct block
*
5975 gen_msg_abbrev(type
)
5981 * Q.2931 signalling protocol messages for handling virtual circuits
5982 * establishment and teardown
5987 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5991 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5995 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5999 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6003 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6006 case A_RELEASE_DONE
:
6007 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6017 gen_atmmulti_abbrev(type
)
6020 struct block
*b0
, *b1
;
6026 bpf_error("'oam' supported only on raw ATM");
6027 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6032 bpf_error("'oamf4' supported only on raw ATM");
6034 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6035 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6037 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6043 * Get Q.2931 signalling messages for switched
6044 * virtual connection
6047 bpf_error("'connectmsg' supported only on raw ATM");
6048 b0
= gen_msg_abbrev(A_SETUP
);
6049 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6051 b0
= gen_msg_abbrev(A_CONNECT
);
6053 b0
= gen_msg_abbrev(A_CONNECTACK
);
6055 b0
= gen_msg_abbrev(A_RELEASE
);
6057 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6059 b0
= gen_atmtype_abbrev(A_SC
);
6065 bpf_error("'metaconnect' supported only on raw ATM");
6066 b0
= gen_msg_abbrev(A_SETUP
);
6067 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6069 b0
= gen_msg_abbrev(A_CONNECT
);
6071 b0
= gen_msg_abbrev(A_RELEASE
);
6073 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6075 b0
= gen_atmtype_abbrev(A_METAC
);