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.11 2005-05-01 04:14:15 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_u_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
;
611 bpf_u_int32 offset
, size
, mask
, jtype
, v
;
617 s
= gen_load_a(offrel
, offset
, size
);
619 if (mask
!= 0xffffffff) {
620 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
624 b
= new_block(JMP(jtype
));
627 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
633 * Various code constructs need to know the layout of the data link
634 * layer. These variables give the necessary offsets.
638 * This is the offset of the beginning of the MAC-layer header.
639 * It's usually 0, except for ATM LANE.
641 static u_int off_mac
;
644 * "off_linktype" is the offset to information in the link-layer header
645 * giving the packet type.
647 * For Ethernet, it's the offset of the Ethernet type field.
649 * For link-layer types that always use 802.2 headers, it's the
650 * offset of the LLC header.
652 * For PPP, it's the offset of the PPP type field.
654 * For Cisco HDLC, it's the offset of the CHDLC type field.
656 * For BSD loopback, it's the offset of the AF_ value.
658 * For Linux cooked sockets, it's the offset of the type field.
660 * It's set to -1 for no encapsulation, in which case, IP is assumed.
662 static u_int off_linktype
;
665 * TRUE if the link layer includes an ATM pseudo-header.
667 static int is_atm
= 0;
670 * TRUE if "lane" appeared in the filter; it causes us to generate
671 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
673 static int is_lane
= 0;
676 * These are offsets for the ATM pseudo-header.
678 static u_int off_vpi
;
679 static u_int off_vci
;
680 static u_int off_proto
;
683 * This is the offset of the first byte after the ATM pseudo_header,
684 * or -1 if there is no ATM pseudo-header.
686 static u_int off_payload
;
689 * These are offsets to the beginning of the network-layer header.
691 * If the link layer never uses 802.2 LLC:
693 * "off_nl" and "off_nl_nosnap" are the same.
695 * If the link layer always uses 802.2 LLC:
697 * "off_nl" is the offset if there's a SNAP header following
700 * "off_nl_nosnap" is the offset if there's no SNAP header.
702 * If the link layer is Ethernet:
704 * "off_nl" is the offset if the packet is an Ethernet II packet
705 * (we assume no 802.3+802.2+SNAP);
707 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
708 * with an 802.2 header following it.
711 static u_int off_nl_nosnap
;
719 linktype
= pcap_datalink(p
);
721 pcap_fddipad
= p
->fddipad
;
725 * Assume it's not raw ATM with a pseudo-header, for now.
742 off_nl
= 6; /* XXX in reality, variable! */
743 off_nl_nosnap
= 6; /* no 802.2 LLC */
746 case DLT_ARCNET_LINUX
:
748 off_nl
= 8; /* XXX in reality, variable! */
749 off_nl_nosnap
= 8; /* no 802.2 LLC */
754 off_nl
= 14; /* Ethernet II */
755 off_nl_nosnap
= 17; /* 802.3+802.2 */
760 * SLIP doesn't have a link level type. The 16 byte
761 * header is hacked into our SLIP driver.
765 off_nl_nosnap
= 16; /* no 802.2 LLC */
769 /* XXX this may be the same as the DLT_PPP_BSDOS case */
773 off_nl_nosnap
= 24; /* no 802.2 LLC */
780 off_nl_nosnap
= 4; /* no 802.2 LLC */
786 off_nl_nosnap
= 12; /* no 802.2 LLC */
791 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
792 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
795 off_nl_nosnap
= 4; /* no 802.2 LLC */
800 * This does no include the Ethernet header, and
801 * only covers session state.
805 off_nl_nosnap
= 8; /* no 802.2 LLC */
811 off_nl_nosnap
= 24; /* no 802.2 LLC */
816 * FDDI doesn't really have a link-level type field.
817 * We set "off_linktype" to the offset of the LLC header.
819 * To check for Ethernet types, we assume that SSAP = SNAP
820 * is being used and pick out the encapsulated Ethernet type.
821 * XXX - should we generate code to check for SNAP?
825 off_linktype
+= pcap_fddipad
;
827 off_nl
= 21; /* FDDI+802.2+SNAP */
828 off_nl_nosnap
= 16; /* FDDI+802.2 */
830 off_nl
+= pcap_fddipad
;
831 off_nl_nosnap
+= pcap_fddipad
;
837 * Token Ring doesn't really have a link-level type field.
838 * We set "off_linktype" to the offset of the LLC header.
840 * To check for Ethernet types, we assume that SSAP = SNAP
841 * is being used and pick out the encapsulated Ethernet type.
842 * XXX - should we generate code to check for SNAP?
844 * XXX - the header is actually variable-length.
845 * Some various Linux patched versions gave 38
846 * as "off_linktype" and 40 as "off_nl"; however,
847 * if a token ring packet has *no* routing
848 * information, i.e. is not source-routed, the correct
849 * values are 20 and 22, as they are in the vanilla code.
851 * A packet is source-routed iff the uppermost bit
852 * of the first byte of the source address, at an
853 * offset of 8, has the uppermost bit set. If the
854 * packet is source-routed, the total number of bytes
855 * of routing information is 2 plus bits 0x1F00 of
856 * the 16-bit value at an offset of 14 (shifted right
857 * 8 - figure out which byte that is).
860 off_nl
= 22; /* Token Ring+802.2+SNAP */
861 off_nl_nosnap
= 17; /* Token Ring+802.2 */
866 * 802.11 doesn't really have a link-level type field.
867 * We set "off_linktype" to the offset of the LLC header.
869 * To check for Ethernet types, we assume that SSAP = SNAP
870 * is being used and pick out the encapsulated Ethernet type.
871 * XXX - should we generate code to check for SNAP?
873 * XXX - the header is actually variable-length. We
874 * assume a 24-byte link-layer header, as appears in
875 * data frames in networks with no bridges. If the
876 * fromds and tods 802.11 header bits are both set,
877 * it's actually supposed to be 30 bytes.
880 off_nl
= 32; /* 802.11+802.2+SNAP */
881 off_nl_nosnap
= 27; /* 802.11+802.2 */
884 case DLT_PRISM_HEADER
:
886 * Same as 802.11, but with an additional header before
887 * the 802.11 header, containing a bunch of additional
888 * information including radio-level information.
890 * The header is 144 bytes long.
892 * XXX - same variable-length header problem; at least
893 * the Prism header is fixed-length.
895 off_linktype
= 144+24;
896 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
897 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
900 case DLT_IEEE802_11_RADIO_AVS
:
902 * Same as 802.11, but with an additional header before
903 * the 802.11 header, containing a bunch of additional
904 * information including radio-level information.
906 * The header is 64 bytes long, at least in its
907 * current incarnation.
909 * XXX - same variable-length header problem, only
910 * more so; this header is also variable-length,
911 * with the length being the 32-bit big-endian
912 * number at an offset of 4 from the beginning
913 * of the radio header.
915 off_linktype
= 64+24;
916 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
917 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
920 case DLT_IEEE802_11_RADIO
:
922 * Same as 802.11, but with an additional header before
923 * the 802.11 header, containing a bunch of additional
924 * information including radio-level information.
926 * XXX - same variable-length header problem, only
927 * even *more* so; this header is also variable-length,
928 * with the length being the 16-bit number at an offset
929 * of 2 from the beginning of the radio header, and it's
930 * device-dependent (different devices might supply
931 * different amounts of information), so we can't even
932 * assume a fixed length for the current version of the
935 * Therefore, currently, only raw "link[N:M]" filtering is
943 case DLT_ATM_RFC1483
:
944 case DLT_ATM_CLIP
: /* Linux ATM defines this */
946 * assume routed, non-ISO PDUs
947 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
950 off_nl
= 8; /* 802.2+SNAP */
951 off_nl_nosnap
= 3; /* 802.2 */
956 * Full Frontal ATM; you get AALn PDUs with an ATM
960 off_vpi
= SUNATM_VPI_POS
;
961 off_vci
= SUNATM_VCI_POS
;
962 off_proto
= PROTO_POS
;
963 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
964 off_payload
= SUNATM_PKT_BEGIN_POS
;
965 off_linktype
= off_payload
;
966 off_nl
= off_payload
+8; /* 802.2+SNAP */
967 off_nl_nosnap
= off_payload
+3; /* 802.2 */
973 off_nl_nosnap
= 0; /* no 802.2 LLC */
976 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
979 off_nl_nosnap
= 16; /* no 802.2 LLC */
984 * LocalTalk does have a 1-byte type field in the LLAP header,
985 * but really it just indicates whether there is a "short" or
986 * "long" DDP packet following.
990 off_nl_nosnap
= 0; /* no 802.2 LLC */
995 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
996 * link-level type field. We set "off_linktype" to the
997 * offset of the LLC header.
999 * To check for Ethernet types, we assume that SSAP = SNAP
1000 * is being used and pick out the encapsulated Ethernet type.
1001 * XXX - should we generate code to check for SNAP? RFC
1002 * 2625 says SNAP should be used.
1005 off_nl
= 24; /* IPFC+802.2+SNAP */
1006 off_nl_nosnap
= 19; /* IPFC+802.2 */
1011 * XXX - we should set this to handle SNAP-encapsulated
1012 * frames (NLPID of 0x80).
1016 off_nl_nosnap
= 0; /* no 802.2 LLC */
1019 case DLT_APPLE_IP_OVER_IEEE1394
:
1022 off_nl_nosnap
= 18; /* no 802.2 LLC */
1025 case DLT_LINUX_IRDA
:
1027 * Currently, only raw "link[N:M]" filtering is supported.
1036 * Currently, only raw "link[N:M]" filtering is supported.
1043 case DLT_SYMANTEC_FIREWALL
:
1045 off_nl
= 44; /* Ethernet II */
1046 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1051 /* XXX read this from pf.h? */
1052 off_nl
= PFLOG_HDRLEN
;
1053 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1056 case DLT_JUNIPER_MLFR
:
1057 case DLT_JUNIPER_MLPPP
:
1060 off_nl_nosnap
= -1; /* no 802.2 LLC */
1063 case DLT_JUNIPER_ATM1
:
1064 off_linktype
= 4; /* in reality variable between 4-8 */
1069 case DLT_JUNIPER_ATM2
:
1070 off_linktype
= 8; /* in reality variable between 8-12 */
1083 case DLT_LINUX_LAPD
:
1085 * Currently, only raw "link[N:M]" filtering is supported.
1092 bpf_error("unknown data link type %d", linktype
);
1097 * Load a value relative to the beginning of the specified header.
1099 static struct slist
*
1100 gen_load_a(offrel
, offset
, size
)
1101 enum e_offrel offrel
;
1109 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1114 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1115 s
->s
.k
= off_nl
+ offset
;
1119 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1120 s
->s
.k
= off_nl_nosnap
+ offset
;
1125 * Load the X register with the length of the IPv4 header,
1128 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1132 * Load the item at {length of the link-layer header} +
1133 * {length of the IPv4 header} + {specified offset}.
1135 s
->next
= new_stmt(BPF_LD
|BPF_IND
|size
);
1136 s
->next
->s
.k
= off_nl
+ offset
;
1140 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1141 s
->s
.k
= off_nl
+ 40 + offset
;
1151 static struct block
*
1158 s
= new_stmt(BPF_LD
|BPF_IMM
);
1160 b
= new_block(JMP(BPF_JEQ
));
1166 static inline struct block
*
1169 return gen_uncond(1);
1172 static inline struct block
*
1175 return gen_uncond(0);
1179 * Byte-swap a 32-bit number.
1180 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1181 * big-endian platforms.)
1183 #define SWAPLONG(y) \
1184 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1187 * Generate code to match a particular packet type.
1189 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1190 * value, if <= ETHERMTU. We use that to determine whether to
1191 * match the type/length field or to check the type/length field for
1192 * a value <= ETHERMTU to see whether it's a type field and then do
1193 * the appropriate test.
1195 static struct block
*
1196 gen_ether_linktype(proto
)
1199 struct block
*b0
, *b1
;
1205 case LLCSAP_NETBEUI
:
1207 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1208 * so we check the DSAP and SSAP.
1210 * LLCSAP_IP checks for IP-over-802.2, rather
1211 * than IP-over-Ethernet or IP-over-SNAP.
1213 * XXX - should we check both the DSAP and the
1214 * SSAP, like this, or should we check just the
1215 * DSAP, as we do for other types <= ETHERMTU
1216 * (i.e., other SAP values)?
1218 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1220 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1221 ((proto
<< 8) | proto
));
1229 * Ethernet_II frames, which are Ethernet
1230 * frames with a frame type of ETHERTYPE_IPX;
1232 * Ethernet_802.3 frames, which are 802.3
1233 * frames (i.e., the type/length field is
1234 * a length field, <= ETHERMTU, rather than
1235 * a type field) with the first two bytes
1236 * after the Ethernet/802.3 header being
1239 * Ethernet_802.2 frames, which are 802.3
1240 * frames with an 802.2 LLC header and
1241 * with the IPX LSAP as the DSAP in the LLC
1244 * Ethernet_SNAP frames, which are 802.3
1245 * frames with an LLC header and a SNAP
1246 * header and with an OUI of 0x000000
1247 * (encapsulated Ethernet) and a protocol
1248 * ID of ETHERTYPE_IPX in the SNAP header.
1250 * XXX - should we generate the same code both
1251 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1255 * This generates code to check both for the
1256 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1258 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1259 (bpf_int32
)LLCSAP_IPX
);
1260 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
,
1265 * Now we add code to check for SNAP frames with
1266 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1268 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1272 * Now we generate code to check for 802.3
1273 * frames in general.
1275 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1279 * Now add the check for 802.3 frames before the
1280 * check for Ethernet_802.2 and Ethernet_802.3,
1281 * as those checks should only be done on 802.3
1282 * frames, not on Ethernet frames.
1287 * Now add the check for Ethernet_II frames, and
1288 * do that before checking for the other frame
1291 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1292 (bpf_int32
)ETHERTYPE_IPX
);
1296 case ETHERTYPE_ATALK
:
1297 case ETHERTYPE_AARP
:
1299 * EtherTalk (AppleTalk protocols on Ethernet link
1300 * layer) may use 802.2 encapsulation.
1304 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1305 * we check for an Ethernet type field less than
1306 * 1500, which means it's an 802.3 length field.
1308 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1312 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1313 * SNAP packets with an organization code of
1314 * 0x080007 (Apple, for Appletalk) and a protocol
1315 * type of ETHERTYPE_ATALK (Appletalk).
1317 * 802.2-encapsulated ETHERTYPE_AARP packets are
1318 * SNAP packets with an organization code of
1319 * 0x000000 (encapsulated Ethernet) and a protocol
1320 * type of ETHERTYPE_AARP (Appletalk ARP).
1322 if (proto
== ETHERTYPE_ATALK
)
1323 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1324 else /* proto == ETHERTYPE_AARP */
1325 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1329 * Check for Ethernet encapsulation (Ethertalk
1330 * phase 1?); we just check for the Ethernet
1333 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1339 if (proto
<= ETHERMTU
) {
1341 * This is an LLC SAP value, so the frames
1342 * that match would be 802.2 frames.
1343 * Check that the frame is an 802.2 frame
1344 * (i.e., that the length/type field is
1345 * a length field, <= ETHERMTU) and
1346 * then check the DSAP.
1348 b0
= gen_cmp_gt(OR_LINK
, off_linktype
, BPF_H
, ETHERMTU
);
1350 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1356 * This is an Ethernet type, so compare
1357 * the length/type field with it (if
1358 * the frame is an 802.2 frame, the length
1359 * field will be <= ETHERMTU, and, as
1360 * "proto" is > ETHERMTU, this test
1361 * will fail and the frame won't match,
1362 * which is what we want).
1364 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1371 * Generate code to match a particular packet type.
1373 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1374 * value, if <= ETHERMTU. We use that to determine whether to
1375 * match the type field or to check the type field for the special
1376 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1378 static struct block
*
1379 gen_linux_sll_linktype(proto
)
1382 struct block
*b0
, *b1
;
1388 case LLCSAP_NETBEUI
:
1390 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1391 * so we check the DSAP and SSAP.
1393 * LLCSAP_IP checks for IP-over-802.2, rather
1394 * than IP-over-Ethernet or IP-over-SNAP.
1396 * XXX - should we check both the DSAP and the
1397 * SSAP, like this, or should we check just the
1398 * DSAP, as we do for other types <= ETHERMTU
1399 * (i.e., other SAP values)?
1401 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1402 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_H
, (bpf_int32
)
1403 ((proto
<< 8) | proto
));
1409 * Ethernet_II frames, which are Ethernet
1410 * frames with a frame type of ETHERTYPE_IPX;
1412 * Ethernet_802.3 frames, which have a frame
1413 * type of LINUX_SLL_P_802_3;
1415 * Ethernet_802.2 frames, which are 802.3
1416 * frames with an 802.2 LLC header (i.e, have
1417 * a frame type of LINUX_SLL_P_802_2) and
1418 * with the IPX LSAP as the DSAP in the LLC
1421 * Ethernet_SNAP frames, which are 802.3
1422 * frames with an LLC header and a SNAP
1423 * header and with an OUI of 0x000000
1424 * (encapsulated Ethernet) and a protocol
1425 * ID of ETHERTYPE_IPX in the SNAP header.
1427 * First, do the checks on LINUX_SLL_P_802_2
1428 * frames; generate the check for either
1429 * Ethernet_802.2 or Ethernet_SNAP frames, and
1430 * then put a check for LINUX_SLL_P_802_2 frames
1433 b0
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1434 (bpf_int32
)LLCSAP_IPX
);
1435 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1438 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1442 * Now check for 802.3 frames and OR that with
1443 * the previous test.
1445 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1449 * Now add the check for Ethernet_II frames, and
1450 * do that before checking for the other frame
1453 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1454 (bpf_int32
)ETHERTYPE_IPX
);
1458 case ETHERTYPE_ATALK
:
1459 case ETHERTYPE_AARP
:
1461 * EtherTalk (AppleTalk protocols on Ethernet link
1462 * layer) may use 802.2 encapsulation.
1466 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1467 * we check for the 802.2 protocol type in the
1468 * "Ethernet type" field.
1470 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1473 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1474 * SNAP packets with an organization code of
1475 * 0x080007 (Apple, for Appletalk) and a protocol
1476 * type of ETHERTYPE_ATALK (Appletalk).
1478 * 802.2-encapsulated ETHERTYPE_AARP packets are
1479 * SNAP packets with an organization code of
1480 * 0x000000 (encapsulated Ethernet) and a protocol
1481 * type of ETHERTYPE_AARP (Appletalk ARP).
1483 if (proto
== ETHERTYPE_ATALK
)
1484 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1486 else /* proto == ETHERTYPE_AARP */
1487 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1492 * Check for Ethernet encapsulation (Ethertalk
1493 * phase 1?); we just check for the Ethernet
1496 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1502 if (proto
<= ETHERMTU
) {
1504 * This is an LLC SAP value, so the frames
1505 * that match would be 802.2 frames.
1506 * Check for the 802.2 protocol type
1507 * in the "Ethernet type" field, and
1508 * then check the DSAP.
1510 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1512 b1
= gen_cmp(OR_LINK
, off_linktype
+ 2, BPF_B
,
1518 * This is an Ethernet type, so compare
1519 * the length/type field with it (if
1520 * the frame is an 802.2 frame, the length
1521 * field will be <= ETHERMTU, and, as
1522 * "proto" is > ETHERMTU, this test
1523 * will fail and the frame won't match,
1524 * which is what we want).
1526 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1533 * Generate code to match a particular packet type by matching the
1534 * link-layer type field or fields in the 802.2 LLC header.
1536 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1537 * value, if <= ETHERMTU.
1539 static struct block
*
1543 struct block
*b0
, *b1
, *b2
;
1548 return gen_ether_linktype(proto
);
1556 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1560 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
,
1567 case DLT_IEEE802_11
:
1568 case DLT_PRISM_HEADER
:
1569 case DLT_IEEE802_11_RADIO
:
1572 case DLT_ATM_RFC1483
:
1574 case DLT_IP_OVER_FC
:
1575 return gen_llc_linktype(proto
);
1581 * If "is_lane" is set, check for a LANE-encapsulated
1582 * version of this protocol, otherwise check for an
1583 * LLC-encapsulated version of this protocol.
1585 * We assume LANE means Ethernet, not Token Ring.
1589 * Check that the packet doesn't begin with an
1590 * LE Control marker. (We've already generated
1593 b0
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
1598 * Now generate an Ethernet test.
1600 b1
= gen_ether_linktype(proto
);
1605 * Check for LLC encapsulation and then check the
1608 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1609 b1
= gen_llc_linktype(proto
);
1617 return gen_linux_sll_linktype(proto
);
1622 case DLT_SLIP_BSDOS
:
1625 * These types don't provide any type field; packets
1628 * XXX - for IPv4, check for a version number of 4, and,
1629 * for IPv6, check for a version number of 6?
1635 case ETHERTYPE_IPV6
:
1637 return gen_true(); /* always true */
1640 return gen_false(); /* always false */
1647 case DLT_PPP_SERIAL
:
1650 * We use Ethernet protocol types inside libpcap;
1651 * map them to the corresponding PPP protocol types.
1660 case ETHERTYPE_IPV6
:
1669 case ETHERTYPE_ATALK
:
1683 * I'm assuming the "Bridging PDU"s that go
1684 * over PPP are Spanning Tree Protocol
1698 * We use Ethernet protocol types inside libpcap;
1699 * map them to the corresponding PPP protocol types.
1704 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_IP
);
1705 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJC
);
1707 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_H
, PPP_VJNC
);
1712 case ETHERTYPE_IPV6
:
1722 case ETHERTYPE_ATALK
:
1736 * I'm assuming the "Bridging PDU"s that go
1737 * over PPP are Spanning Tree Protocol
1753 * For DLT_NULL, the link-layer header is a 32-bit
1754 * word containing an AF_ value in *host* byte order,
1755 * and for DLT_ENC, the link-layer header begins
1756 * with a 32-bit work containing an AF_ value in
1759 * In addition, if we're reading a saved capture file,
1760 * the host byte order in the capture may not be the
1761 * same as the host byte order on this machine.
1763 * For DLT_LOOP, the link-layer header is a 32-bit
1764 * word containing an AF_ value in *network* byte order.
1766 * XXX - AF_ values may, unfortunately, be platform-
1767 * dependent; for example, FreeBSD's AF_INET6 is 24
1768 * whilst NetBSD's and OpenBSD's is 26.
1770 * This means that, when reading a capture file, just
1771 * checking for our AF_INET6 value won't work if the
1772 * capture file came from another OS.
1781 case ETHERTYPE_IPV6
:
1788 * Not a type on which we support filtering.
1789 * XXX - support those that have AF_ values
1790 * #defined on this platform, at least?
1795 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1797 * The AF_ value is in host byte order, but
1798 * the BPF interpreter will convert it to
1799 * network byte order.
1801 * If this is a save file, and it's from a
1802 * machine with the opposite byte order to
1803 * ours, we byte-swap the AF_ value.
1805 * Then we run it through "htonl()", and
1806 * generate code to compare against the result.
1808 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1809 bpf_pcap
->sf
.swapped
)
1810 proto
= SWAPLONG(proto
);
1811 proto
= htonl(proto
);
1813 return (gen_cmp(OR_LINK
, 0, BPF_W
, (bpf_int32
)proto
));
1817 * af field is host byte order in contrast to the rest of
1820 if (proto
== ETHERTYPE_IP
)
1821 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
1822 BPF_B
, (bpf_int32
)AF_INET
));
1824 else if (proto
== ETHERTYPE_IPV6
)
1825 return (gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, af
),
1826 BPF_B
, (bpf_int32
)AF_INET6
));
1834 case DLT_ARCNET_LINUX
:
1836 * XXX should we check for first fragment if the protocol
1845 case ETHERTYPE_IPV6
:
1846 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1847 (bpf_int32
)ARCTYPE_INET6
));
1851 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1852 (bpf_int32
)ARCTYPE_IP
);
1853 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1854 (bpf_int32
)ARCTYPE_IP_OLD
);
1859 b0
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1860 (bpf_int32
)ARCTYPE_ARP
);
1861 b1
= gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1862 (bpf_int32
)ARCTYPE_ARP_OLD
);
1866 case ETHERTYPE_REVARP
:
1867 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1868 (bpf_int32
)ARCTYPE_REVARP
));
1870 case ETHERTYPE_ATALK
:
1871 return (gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
1872 (bpf_int32
)ARCTYPE_ATALK
));
1879 case ETHERTYPE_ATALK
:
1889 * XXX - assumes a 2-byte Frame Relay header with
1890 * DLCI and flags. What if the address is longer?
1896 * Check for the special NLPID for IP.
1898 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0xcc);
1901 case ETHERTYPE_IPV6
:
1903 * Check for the special NLPID for IPv6.
1905 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | 0x8e);
1910 * Check for several OSI protocols.
1912 * Frame Relay packets typically have an OSI
1913 * NLPID at the beginning; we check for each
1916 * What we check for is the NLPID and a frame
1917 * control field of UI, i.e. 0x03 followed
1920 b0
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1921 b1
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1922 b2
= gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1933 case DLT_JUNIPER_MLFR
:
1934 case DLT_JUNIPER_MLPPP
:
1935 case DLT_JUNIPER_ATM1
:
1936 case DLT_JUNIPER_ATM2
:
1937 /* just lets verify the magic number for now -
1938 * on ATM we may have up to 6 different encapsulations on the wire
1939 * and need a lot of heuristics to figure out that the payload
1942 * FIXME encapsulation specific BPF_ filters
1944 return gen_mcmp(OR_LINK
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1946 case DLT_LINUX_IRDA
:
1947 bpf_error("IrDA link-layer type filtering not implemented");
1950 bpf_error("DOCSIS link-layer type filtering not implemented");
1952 case DLT_LINUX_LAPD
:
1953 bpf_error("LAPD link-layer type filtering not implemented");
1957 * All the types that have no encapsulation should either be
1958 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1959 * all packets are IP packets, or should be handled in some
1960 * special case, if none of them are (if some are and some
1961 * aren't, the lack of encapsulation is a problem, as we'd
1962 * have to find some other way of determining the packet type).
1964 * Therefore, if "off_linktype" is -1, there's an error.
1966 if (off_linktype
== (u_int
)-1)
1970 * Any type not handled above should always have an Ethernet
1971 * type at an offset of "off_linktype". (PPP is partially
1972 * handled above - the protocol type is mapped from the
1973 * Ethernet and LLC types we use internally to the corresponding
1974 * PPP type - but the PPP type is always specified by a value
1975 * at "off_linktype", so we don't have to do the code generation
1978 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_int32
)proto
);
1982 * Check for an LLC SNAP packet with a given organization code and
1983 * protocol type; we check the entire contents of the 802.2 LLC and
1984 * snap headers, checking for DSAP and SSAP of SNAP and a control
1985 * field of 0x03 in the LLC header, and for the specified organization
1986 * code and protocol type in the SNAP header.
1988 static struct block
*
1989 gen_snap(orgcode
, ptype
, offset
)
1990 bpf_u_int32 orgcode
;
1994 u_char snapblock
[8];
1996 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1997 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1998 snapblock
[2] = 0x03; /* control = UI */
1999 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
2000 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
2001 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
2002 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
2003 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
2004 return gen_bcmp(OR_LINK
, offset
, 8, snapblock
);
2008 * Generate code to match a particular packet type, for link-layer types
2009 * using 802.2 LLC headers.
2011 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
2012 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
2014 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2015 * value, if <= ETHERMTU. We use that to determine whether to
2016 * match the DSAP or both DSAP and LSAP or to check the OUI and
2017 * protocol ID in a SNAP header.
2019 static struct block
*
2020 gen_llc_linktype(proto
)
2024 * XXX - handle token-ring variable-length header.
2030 case LLCSAP_NETBEUI
:
2032 * XXX - should we check both the DSAP and the
2033 * SSAP, like this, or should we check just the
2034 * DSAP, as we do for other types <= ETHERMTU
2035 * (i.e., other SAP values)?
2037 return gen_cmp(OR_LINK
, off_linktype
, BPF_H
, (bpf_u_int32
)
2038 ((proto
<< 8) | proto
));
2042 * XXX - are there ever SNAP frames for IPX on
2043 * non-Ethernet 802.x networks?
2045 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2046 (bpf_int32
)LLCSAP_IPX
);
2048 case ETHERTYPE_ATALK
:
2050 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2051 * SNAP packets with an organization code of
2052 * 0x080007 (Apple, for Appletalk) and a protocol
2053 * type of ETHERTYPE_ATALK (Appletalk).
2055 * XXX - check for an organization code of
2056 * encapsulated Ethernet as well?
2058 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2062 * XXX - we don't have to check for IPX 802.3
2063 * here, but should we check for the IPX Ethertype?
2065 if (proto
<= ETHERMTU
) {
2067 * This is an LLC SAP value, so check
2070 return gen_cmp(OR_LINK
, off_linktype
, BPF_B
,
2074 * This is an Ethernet type; we assume that it's
2075 * unlikely that it'll appear in the right place
2076 * at random, and therefore check only the
2077 * location that would hold the Ethernet type
2078 * in a SNAP frame with an organization code of
2079 * 0x000000 (encapsulated Ethernet).
2081 * XXX - if we were to check for the SNAP DSAP and
2082 * LSAP, as per XXX, and were also to check for an
2083 * organization code of 0x000000 (encapsulated
2084 * Ethernet), we'd do
2086 * return gen_snap(0x000000, proto,
2089 * here; for now, we don't, as per the above.
2090 * I don't know whether it's worth the extra CPU
2091 * time to do the right check or not.
2093 return gen_cmp(OR_LINK
, off_linktype
+6, BPF_H
,
2099 static struct block
*
2100 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2104 u_int src_off
, dst_off
;
2106 struct block
*b0
, *b1
;
2120 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2121 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2127 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2128 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2135 b0
= gen_linktype(proto
);
2136 b1
= gen_mcmp(OR_NET
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2142 static struct block
*
2143 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2144 struct in6_addr
*addr
;
2145 struct in6_addr
*mask
;
2147 u_int src_off
, dst_off
;
2149 struct block
*b0
, *b1
;
2164 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2165 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2171 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2172 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2179 /* this order is important */
2180 a
= (u_int32_t
*)addr
;
2181 m
= (u_int32_t
*)mask
;
2182 b1
= gen_mcmp(OR_NET
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2183 b0
= gen_mcmp(OR_NET
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2185 b0
= gen_mcmp(OR_NET
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2187 b0
= gen_mcmp(OR_NET
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2189 b0
= gen_linktype(proto
);
2195 static struct block
*
2196 gen_ehostop(eaddr
, dir
)
2197 register const u_char
*eaddr
;
2200 register struct block
*b0
, *b1
;
2204 return gen_bcmp(OR_LINK
, off_mac
+ 6, 6, eaddr
);
2207 return gen_bcmp(OR_LINK
, off_mac
+ 0, 6, eaddr
);
2210 b0
= gen_ehostop(eaddr
, Q_SRC
);
2211 b1
= gen_ehostop(eaddr
, Q_DST
);
2217 b0
= gen_ehostop(eaddr
, Q_SRC
);
2218 b1
= gen_ehostop(eaddr
, Q_DST
);
2227 * Like gen_ehostop, but for DLT_FDDI
2229 static struct block
*
2230 gen_fhostop(eaddr
, dir
)
2231 register const u_char
*eaddr
;
2234 struct block
*b0
, *b1
;
2239 return gen_bcmp(OR_LINK
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
2241 return gen_bcmp(OR_LINK
, 6 + 1, 6, eaddr
);
2246 return gen_bcmp(OR_LINK
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
2248 return gen_bcmp(OR_LINK
, 0 + 1, 6, eaddr
);
2252 b0
= gen_fhostop(eaddr
, Q_SRC
);
2253 b1
= gen_fhostop(eaddr
, Q_DST
);
2259 b0
= gen_fhostop(eaddr
, Q_SRC
);
2260 b1
= gen_fhostop(eaddr
, Q_DST
);
2269 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2271 static struct block
*
2272 gen_thostop(eaddr
, dir
)
2273 register const u_char
*eaddr
;
2276 register struct block
*b0
, *b1
;
2280 return gen_bcmp(OR_LINK
, 8, 6, eaddr
);
2283 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2286 b0
= gen_thostop(eaddr
, Q_SRC
);
2287 b1
= gen_thostop(eaddr
, Q_DST
);
2293 b0
= gen_thostop(eaddr
, Q_SRC
);
2294 b1
= gen_thostop(eaddr
, Q_DST
);
2303 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2305 static struct block
*
2306 gen_wlanhostop(eaddr
, dir
)
2307 register const u_char
*eaddr
;
2310 register struct block
*b0
, *b1
, *b2
;
2311 register struct slist
*s
;
2318 * For control frames, there is no SA.
2320 * For management frames, SA is at an
2321 * offset of 10 from the beginning of
2324 * For data frames, SA is at an offset
2325 * of 10 from the beginning of the packet
2326 * if From DS is clear, at an offset of
2327 * 16 from the beginning of the packet
2328 * if From DS is set and To DS is clear,
2329 * and an offset of 24 from the beginning
2330 * of the packet if From DS is set and To DS
2335 * Generate the tests to be done for data frames
2338 * First, check for To DS set, i.e. check "link[1] & 0x01".
2340 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2341 b1
= new_block(JMP(BPF_JSET
));
2342 b1
->s
.k
= 0x01; /* To DS */
2346 * If To DS is set, the SA is at 24.
2348 b0
= gen_bcmp(OR_LINK
, 24, 6, eaddr
);
2352 * Now, check for To DS not set, i.e. check
2353 * "!(link[1] & 0x01)".
2355 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2356 b2
= new_block(JMP(BPF_JSET
));
2357 b2
->s
.k
= 0x01; /* To DS */
2362 * If To DS is not set, the SA is at 16.
2364 b1
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2368 * Now OR together the last two checks. That gives
2369 * the complete set of checks for data frames with
2375 * Now check for From DS being set, and AND that with
2376 * the ORed-together checks.
2378 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2379 b1
= new_block(JMP(BPF_JSET
));
2380 b1
->s
.k
= 0x02; /* From DS */
2385 * Now check for data frames with From DS not set.
2387 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2388 b2
= new_block(JMP(BPF_JSET
));
2389 b2
->s
.k
= 0x02; /* From DS */
2394 * If From DS isn't set, the SA is at 10.
2396 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2400 * Now OR together the checks for data frames with
2401 * From DS not set and for data frames with From DS
2402 * set; that gives the checks done for data frames.
2407 * Now check for a data frame.
2408 * I.e, check "link[0] & 0x08".
2410 gen_load_a(OR_LINK
, 0, BPF_B
);
2411 b1
= new_block(JMP(BPF_JSET
));
2416 * AND that with the checks done for data frames.
2421 * If the high-order bit of the type value is 0, this
2422 * is a management frame.
2423 * I.e, check "!(link[0] & 0x08)".
2425 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2426 b2
= new_block(JMP(BPF_JSET
));
2432 * For management frames, the SA is at 10.
2434 b1
= gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2438 * OR that with the checks done for data frames.
2439 * That gives the checks done for management and
2445 * If the low-order bit of the type value is 1,
2446 * this is either a control frame or a frame
2447 * with a reserved type, and thus not a
2450 * I.e., check "!(link[0] & 0x04)".
2452 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2453 b1
= new_block(JMP(BPF_JSET
));
2459 * AND that with the checks for data and management
2469 * For control frames, there is no DA.
2471 * For management frames, DA is at an
2472 * offset of 4 from the beginning of
2475 * For data frames, DA is at an offset
2476 * of 4 from the beginning of the packet
2477 * if To DS is clear and at an offset of
2478 * 16 from the beginning of the packet
2483 * Generate the tests to be done for data frames.
2485 * First, check for To DS set, i.e. "link[1] & 0x01".
2487 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2488 b1
= new_block(JMP(BPF_JSET
));
2489 b1
->s
.k
= 0x01; /* To DS */
2493 * If To DS is set, the DA is at 16.
2495 b0
= gen_bcmp(OR_LINK
, 16, 6, eaddr
);
2499 * Now, check for To DS not set, i.e. check
2500 * "!(link[1] & 0x01)".
2502 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
2503 b2
= new_block(JMP(BPF_JSET
));
2504 b2
->s
.k
= 0x01; /* To DS */
2509 * If To DS is not set, the DA is at 4.
2511 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2515 * Now OR together the last two checks. That gives
2516 * the complete set of checks for data frames.
2521 * Now check for a data frame.
2522 * I.e, check "link[0] & 0x08".
2524 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2525 b1
= new_block(JMP(BPF_JSET
));
2530 * AND that with the checks done for data frames.
2535 * If the high-order bit of the type value is 0, this
2536 * is a management frame.
2537 * I.e, check "!(link[0] & 0x08)".
2539 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2540 b2
= new_block(JMP(BPF_JSET
));
2546 * For management frames, the DA is at 4.
2548 b1
= gen_bcmp(OR_LINK
, 4, 6, eaddr
);
2552 * OR that with the checks done for data frames.
2553 * That gives the checks done for management and
2559 * If the low-order bit of the type value is 1,
2560 * this is either a control frame or a frame
2561 * with a reserved type, and thus not a
2564 * I.e., check "!(link[0] & 0x04)".
2566 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
2567 b1
= new_block(JMP(BPF_JSET
));
2573 * AND that with the checks for data and management
2580 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2581 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2587 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2588 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2597 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2598 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2599 * as the RFC states.)
2601 static struct block
*
2602 gen_ipfchostop(eaddr
, dir
)
2603 register const u_char
*eaddr
;
2606 register struct block
*b0
, *b1
;
2610 return gen_bcmp(OR_LINK
, 10, 6, eaddr
);
2613 return gen_bcmp(OR_LINK
, 2, 6, eaddr
);
2616 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2617 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2623 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2624 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2633 * This is quite tricky because there may be pad bytes in front of the
2634 * DECNET header, and then there are two possible data packet formats that
2635 * carry both src and dst addresses, plus 5 packet types in a format that
2636 * carries only the src node, plus 2 types that use a different format and
2637 * also carry just the src node.
2641 * Instead of doing those all right, we just look for data packets with
2642 * 0 or 1 bytes of padding. If you want to look at other packets, that
2643 * will require a lot more hacking.
2645 * To add support for filtering on DECNET "areas" (network numbers)
2646 * one would want to add a "mask" argument to this routine. That would
2647 * make the filter even more inefficient, although one could be clever
2648 * and not generate masking instructions if the mask is 0xFFFF.
2650 static struct block
*
2651 gen_dnhostop(addr
, dir
)
2655 struct block
*b0
, *b1
, *b2
, *tmp
;
2656 u_int offset_lh
; /* offset if long header is received */
2657 u_int offset_sh
; /* offset if short header is received */
2662 offset_sh
= 1; /* follows flags */
2663 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2667 offset_sh
= 3; /* follows flags, dstnode */
2668 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2672 /* Inefficient because we do our Calvinball dance twice */
2673 b0
= gen_dnhostop(addr
, Q_SRC
);
2674 b1
= gen_dnhostop(addr
, Q_DST
);
2680 /* Inefficient because we do our Calvinball dance twice */
2681 b0
= gen_dnhostop(addr
, Q_SRC
);
2682 b1
= gen_dnhostop(addr
, Q_DST
);
2687 bpf_error("ISO host filtering not implemented");
2692 b0
= gen_linktype(ETHERTYPE_DN
);
2693 /* Check for pad = 1, long header case */
2694 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2695 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2696 b1
= gen_cmp(OR_NET
, 2 + 1 + offset_lh
,
2697 BPF_H
, (bpf_int32
)ntohs(addr
));
2699 /* Check for pad = 0, long header case */
2700 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2701 b2
= gen_cmp(OR_NET
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2704 /* Check for pad = 1, short header case */
2705 tmp
= gen_mcmp(OR_NET
, 2, BPF_H
,
2706 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2707 b2
= gen_cmp(OR_NET
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2710 /* Check for pad = 0, short header case */
2711 tmp
= gen_mcmp(OR_NET
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2712 b2
= gen_cmp(OR_NET
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2716 /* Combine with test for linktype */
2721 static struct block
*
2722 gen_host(addr
, mask
, proto
, dir
)
2728 struct block
*b0
, *b1
;
2733 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2734 if (off_linktype
!= (u_int
)-1) {
2735 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2737 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2743 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
2746 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
2749 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
2752 bpf_error("'tcp' modifier applied to host");
2755 bpf_error("'sctp' modifier applied to host");
2758 bpf_error("'udp' modifier applied to host");
2761 bpf_error("'icmp' modifier applied to host");
2764 bpf_error("'igmp' modifier applied to host");
2767 bpf_error("'igrp' modifier applied to host");
2770 bpf_error("'pim' modifier applied to host");
2773 bpf_error("'vrrp' modifier applied to host");
2776 bpf_error("ATALK host filtering not implemented");
2779 bpf_error("AARP host filtering not implemented");
2782 return gen_dnhostop(addr
, dir
);
2785 bpf_error("SCA host filtering not implemented");
2788 bpf_error("LAT host filtering not implemented");
2791 bpf_error("MOPDL host filtering not implemented");
2794 bpf_error("MOPRC host filtering not implemented");
2798 bpf_error("'ip6' modifier applied to ip host");
2801 bpf_error("'icmp6' modifier applied to host");
2805 bpf_error("'ah' modifier applied to host");
2808 bpf_error("'esp' modifier applied to host");
2811 bpf_error("ISO host filtering not implemented");
2814 bpf_error("'esis' modifier applied to host");
2817 bpf_error("'isis' modifier applied to host");
2820 bpf_error("'clnp' modifier applied to host");
2823 bpf_error("'stp' modifier applied to host");
2826 bpf_error("IPX host filtering not implemented");
2829 bpf_error("'netbeui' modifier applied to host");
2838 static struct block
*
2839 gen_host6(addr
, mask
, proto
, dir
)
2840 struct in6_addr
*addr
;
2841 struct in6_addr
*mask
;
2848 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2851 bpf_error("'ip' modifier applied to ip6 host");
2854 bpf_error("'rarp' modifier applied to ip6 host");
2857 bpf_error("'arp' modifier applied to ip6 host");
2860 bpf_error("'sctp' modifier applied to host");
2863 bpf_error("'tcp' modifier applied to host");
2866 bpf_error("'udp' modifier applied to host");
2869 bpf_error("'icmp' modifier applied to host");
2872 bpf_error("'igmp' modifier applied to host");
2875 bpf_error("'igrp' modifier applied to host");
2878 bpf_error("'pim' modifier applied to host");
2881 bpf_error("'vrrp' modifier applied to host");
2884 bpf_error("ATALK host filtering not implemented");
2887 bpf_error("AARP host filtering not implemented");
2890 bpf_error("'decnet' modifier applied to ip6 host");
2893 bpf_error("SCA host filtering not implemented");
2896 bpf_error("LAT host filtering not implemented");
2899 bpf_error("MOPDL host filtering not implemented");
2902 bpf_error("MOPRC host filtering not implemented");
2905 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
2908 bpf_error("'icmp6' modifier applied to host");
2911 bpf_error("'ah' modifier applied to host");
2914 bpf_error("'esp' modifier applied to host");
2917 bpf_error("ISO host filtering not implemented");
2920 bpf_error("'esis' modifier applied to host");
2923 bpf_error("'isis' modifier applied to host");
2926 bpf_error("'clnp' modifier applied to host");
2929 bpf_error("'stp' modifier applied to host");
2932 bpf_error("IPX host filtering not implemented");
2935 bpf_error("'netbeui' modifier applied to host");
2945 static struct block
*
2946 gen_gateway(eaddr
, alist
, proto
, dir
)
2947 const u_char
*eaddr
;
2948 bpf_u_int32
**alist
;
2952 struct block
*b0
, *b1
, *tmp
;
2955 bpf_error("direction applied to 'gateway'");
2962 if (linktype
== DLT_EN10MB
)
2963 b0
= gen_ehostop(eaddr
, Q_OR
);
2964 else if (linktype
== DLT_FDDI
)
2965 b0
= gen_fhostop(eaddr
, Q_OR
);
2966 else if (linktype
== DLT_IEEE802
)
2967 b0
= gen_thostop(eaddr
, Q_OR
);
2968 else if (linktype
== DLT_IEEE802_11
)
2969 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2970 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2972 * Check that the packet doesn't begin with an
2973 * LE Control marker. (We've already generated
2976 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
2981 * Now check the MAC address.
2983 b0
= gen_ehostop(eaddr
, Q_OR
);
2985 } else if (linktype
== DLT_IP_OVER_FC
)
2986 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2989 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2991 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2993 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
3001 bpf_error("illegal modifier of 'gateway'");
3007 gen_proto_abbrev(proto
)
3016 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3018 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3024 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3026 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3032 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3034 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3040 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3043 #ifndef IPPROTO_IGMP
3044 #define IPPROTO_IGMP 2
3048 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3051 #ifndef IPPROTO_IGRP
3052 #define IPPROTO_IGRP 9
3055 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3059 #define IPPROTO_PIM 103
3063 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3065 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3070 #ifndef IPPROTO_VRRP
3071 #define IPPROTO_VRRP 112
3075 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3079 b1
= gen_linktype(ETHERTYPE_IP
);
3083 b1
= gen_linktype(ETHERTYPE_ARP
);
3087 b1
= gen_linktype(ETHERTYPE_REVARP
);
3091 bpf_error("link layer applied in wrong context");
3094 b1
= gen_linktype(ETHERTYPE_ATALK
);
3098 b1
= gen_linktype(ETHERTYPE_AARP
);
3102 b1
= gen_linktype(ETHERTYPE_DN
);
3106 b1
= gen_linktype(ETHERTYPE_SCA
);
3110 b1
= gen_linktype(ETHERTYPE_LAT
);
3114 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3118 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3123 b1
= gen_linktype(ETHERTYPE_IPV6
);
3126 #ifndef IPPROTO_ICMPV6
3127 #define IPPROTO_ICMPV6 58
3130 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3135 #define IPPROTO_AH 51
3138 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3140 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3146 #define IPPROTO_ESP 50
3149 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3151 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3157 b1
= gen_linktype(LLCSAP_ISONS
);
3161 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3165 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3168 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3169 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3170 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3172 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3174 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3176 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3180 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3181 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3182 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3184 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3186 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3188 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3192 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3193 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3194 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3196 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3201 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3202 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3207 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3208 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3210 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3212 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3217 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3218 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3223 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3224 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3229 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3233 b1
= gen_linktype(LLCSAP_8021D
);
3237 b1
= gen_linktype(LLCSAP_IPX
);
3241 b1
= gen_linktype(LLCSAP_NETBEUI
);
3250 static struct block
*
3257 s
= gen_load_a(OR_NET
, 6, BPF_H
);
3258 b
= new_block(JMP(BPF_JSET
));
3266 static struct block
*
3267 gen_portatom(off
, v
)
3271 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
3275 static struct block
*
3276 gen_portatom6(off
, v
)
3280 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
3285 gen_portop(port
, proto
, dir
)
3286 int port
, proto
, dir
;
3288 struct block
*b0
, *b1
, *tmp
;
3290 /* ip proto 'proto' */
3291 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3297 b1
= gen_portatom(0, (bpf_int32
)port
);
3301 b1
= gen_portatom(2, (bpf_int32
)port
);
3306 tmp
= gen_portatom(0, (bpf_int32
)port
);
3307 b1
= gen_portatom(2, (bpf_int32
)port
);
3312 tmp
= gen_portatom(0, (bpf_int32
)port
);
3313 b1
= gen_portatom(2, (bpf_int32
)port
);
3325 static struct block
*
3326 gen_port(port
, ip_proto
, dir
)
3331 struct block
*b0
, *b1
, *tmp
;
3336 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3337 * not LLC encapsulation with LLCSAP_IP.
3339 * For IEEE 802 networks - which includes 802.5 token ring
3340 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3341 * says that SNAP encapsulation is used, not LLC encapsulation
3344 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3345 * RFC 2225 say that SNAP encapsulation is used, not LLC
3346 * encapsulation with LLCSAP_IP.
3348 * So we always check for ETHERTYPE_IP.
3350 b0
= gen_linktype(ETHERTYPE_IP
);
3356 b1
= gen_portop(port
, ip_proto
, dir
);
3360 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3361 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3363 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3376 gen_portop6(port
, proto
, dir
)
3377 int port
, proto
, dir
;
3379 struct block
*b0
, *b1
, *tmp
;
3381 /* ip6 proto 'proto' */
3382 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3386 b1
= gen_portatom6(0, (bpf_int32
)port
);
3390 b1
= gen_portatom6(2, (bpf_int32
)port
);
3395 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3396 b1
= gen_portatom6(2, (bpf_int32
)port
);
3401 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3402 b1
= gen_portatom6(2, (bpf_int32
)port
);
3414 static struct block
*
3415 gen_port6(port
, ip_proto
, dir
)
3420 struct block
*b0
, *b1
, *tmp
;
3422 /* link proto ip6 */
3423 b0
= gen_linktype(ETHERTYPE_IPV6
);
3429 b1
= gen_portop6(port
, ip_proto
, dir
);
3433 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3434 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3436 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3448 /* gen_portrange code */
3449 static struct block
*
3450 gen_portrangeatom(off
, v1
, v2
)
3454 struct block
*b1
, *b2
;
3458 * Reverse the order of the ports, so v1 is the lower one.
3467 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
3468 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
3476 gen_portrangeop(port1
, port2
, proto
, dir
)
3481 struct block
*b0
, *b1
, *tmp
;
3483 /* ip proto 'proto' */
3484 tmp
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)proto
);
3490 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3494 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3499 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3500 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3505 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3506 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3518 static struct block
*
3519 gen_portrange(port1
, port2
, ip_proto
, dir
)
3524 struct block
*b0
, *b1
, *tmp
;
3527 b0
= gen_linktype(ETHERTYPE_IP
);
3533 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3537 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3538 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3540 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3552 static struct block
*
3553 gen_portrangeatom6(off
, v1
, v2
)
3557 struct block
*b1
, *b2
;
3561 * Reverse the order of the ports, so v1 is the lower one.
3570 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
3571 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
3579 gen_portrangeop6(port1
, port2
, proto
, dir
)
3584 struct block
*b0
, *b1
, *tmp
;
3586 /* ip6 proto 'proto' */
3587 b0
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)proto
);
3591 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3595 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3600 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3601 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3606 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3607 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3619 static struct block
*
3620 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3625 struct block
*b0
, *b1
, *tmp
;
3627 /* link proto ip6 */
3628 b0
= gen_linktype(ETHERTYPE_IPV6
);
3634 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3638 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3639 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3641 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3654 lookup_proto(name
, proto
)
3655 register const char *name
;
3665 v
= pcap_nametoproto(name
);
3666 if (v
== PROTO_UNDEF
)
3667 bpf_error("unknown ip proto '%s'", name
);
3671 /* XXX should look up h/w protocol type based on linktype */
3672 v
= pcap_nametoeproto(name
);
3673 if (v
== PROTO_UNDEF
) {
3674 v
= pcap_nametollc(name
);
3675 if (v
== PROTO_UNDEF
)
3676 bpf_error("unknown ether proto '%s'", name
);
3681 if (strcmp(name
, "esis") == 0)
3683 else if (strcmp(name
, "isis") == 0)
3685 else if (strcmp(name
, "clnp") == 0)
3688 bpf_error("unknown osi proto '%s'", name
);
3708 static struct block
*
3709 gen_protochain(v
, proto
, dir
)
3714 #ifdef NO_PROTOCHAIN
3715 return gen_proto(v
, proto
, dir
);
3717 struct block
*b0
, *b
;
3718 struct slist
*s
[100];
3719 int fix2
, fix3
, fix4
, fix5
;
3720 int ahcheck
, again
, end
;
3722 int reg2
= alloc_reg();
3724 memset(s
, 0, sizeof(s
));
3725 fix2
= fix3
= fix4
= fix5
= 0;
3732 b0
= gen_protochain(v
, Q_IP
, dir
);
3733 b
= gen_protochain(v
, Q_IPV6
, dir
);
3737 bpf_error("bad protocol applied for 'protochain'");
3741 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3744 * s[0] is a dummy entry to protect other BPF insn from damage
3745 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3746 * hard to find interdependency made by jump table fixup.
3749 s
[i
] = new_stmt(0); /*dummy*/
3754 b0
= gen_linktype(ETHERTYPE_IP
);
3757 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3758 s
[i
]->s
.k
= off_nl
+ 9;
3760 /* X = ip->ip_hl << 2 */
3761 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3767 b0
= gen_linktype(ETHERTYPE_IPV6
);
3769 /* A = ip6->ip_nxt */
3770 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3771 s
[i
]->s
.k
= off_nl
+ 6;
3773 /* X = sizeof(struct ip6_hdr) */
3774 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3780 bpf_error("unsupported proto to gen_protochain");
3784 /* again: if (A == v) goto end; else fall through; */
3786 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3788 s
[i
]->s
.jt
= NULL
; /*later*/
3789 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3793 #ifndef IPPROTO_NONE
3794 #define IPPROTO_NONE 59
3796 /* if (A == IPPROTO_NONE) goto end */
3797 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3798 s
[i
]->s
.jt
= NULL
; /*later*/
3799 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3800 s
[i
]->s
.k
= IPPROTO_NONE
;
3801 s
[fix5
]->s
.jf
= s
[i
];
3806 if (proto
== Q_IPV6
) {
3807 int v6start
, v6end
, v6advance
, j
;
3810 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3811 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3812 s
[i
]->s
.jt
= NULL
; /*later*/
3813 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3814 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3815 s
[fix2
]->s
.jf
= s
[i
];
3817 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3818 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3819 s
[i
]->s
.jt
= NULL
; /*later*/
3820 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3821 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3823 /* if (A == IPPROTO_ROUTING) goto v6advance */
3824 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3825 s
[i
]->s
.jt
= NULL
; /*later*/
3826 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3827 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3829 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3830 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3831 s
[i
]->s
.jt
= NULL
; /*later*/
3832 s
[i
]->s
.jf
= NULL
; /*later*/
3833 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3844 * X = X + (P[X + 1] + 1) * 8;
3847 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3849 /* A = P[X + packet head] */
3850 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3854 s
[i
] = new_stmt(BPF_ST
);
3858 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3861 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3865 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3867 /* A = P[X + packet head]; */
3868 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3872 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3876 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3880 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3883 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3887 /* goto again; (must use BPF_JA for backward jump) */
3888 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3889 s
[i
]->s
.k
= again
- i
- 1;
3890 s
[i
- 1]->s
.jf
= s
[i
];
3894 for (j
= v6start
; j
<= v6end
; j
++)
3895 s
[j
]->s
.jt
= s
[v6advance
];
3900 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3902 s
[fix2
]->s
.jf
= s
[i
];
3908 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3909 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3910 s
[i
]->s
.jt
= NULL
; /*later*/
3911 s
[i
]->s
.jf
= NULL
; /*later*/
3912 s
[i
]->s
.k
= IPPROTO_AH
;
3914 s
[fix3
]->s
.jf
= s
[ahcheck
];
3921 * X = X + (P[X + 1] + 2) * 4;
3924 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3926 /* A = P[X + packet head]; */
3927 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3931 s
[i
] = new_stmt(BPF_ST
);
3935 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3938 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3942 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3944 /* A = P[X + packet head] */
3945 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3949 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3953 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3957 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3960 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3964 /* goto again; (must use BPF_JA for backward jump) */
3965 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3966 s
[i
]->s
.k
= again
- i
- 1;
3971 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3973 s
[fix2
]->s
.jt
= s
[end
];
3974 s
[fix4
]->s
.jf
= s
[end
];
3975 s
[fix5
]->s
.jt
= s
[end
];
3982 for (i
= 0; i
< max
- 1; i
++)
3983 s
[i
]->next
= s
[i
+ 1];
3984 s
[max
- 1]->next
= NULL
;
3989 b
= new_block(JMP(BPF_JEQ
));
3990 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4001 * Generate code that checks whether the packet is a packet for protocol
4002 * <proto> and whether the type field in that protocol's header has
4003 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4004 * IP packet and checks the protocol number in the IP header against <v>.
4006 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4007 * against Q_IP and Q_IPV6.
4009 static struct block
*
4010 gen_proto(v
, proto
, dir
)
4015 struct block
*b0
, *b1
;
4017 if (dir
!= Q_DEFAULT
)
4018 bpf_error("direction applied to 'proto'");
4023 b0
= gen_proto(v
, Q_IP
, dir
);
4024 b1
= gen_proto(v
, Q_IPV6
, dir
);
4032 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4033 * not LLC encapsulation with LLCSAP_IP.
4035 * For IEEE 802 networks - which includes 802.5 token ring
4036 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4037 * says that SNAP encapsulation is used, not LLC encapsulation
4040 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4041 * RFC 2225 say that SNAP encapsulation is used, not LLC
4042 * encapsulation with LLCSAP_IP.
4044 * So we always check for ETHERTYPE_IP.
4046 b0
= gen_linktype(ETHERTYPE_IP
);
4048 b1
= gen_cmp(OR_NET
, 9, BPF_B
, (bpf_int32
)v
);
4050 b1
= gen_protochain(v
, Q_IP
);
4060 * Frame Relay packets typically have an OSI
4061 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4062 * generates code to check for all the OSI
4063 * NLPIDs, so calling it and then adding a check
4064 * for the particular NLPID for which we're
4065 * looking is bogus, as we can just check for
4068 * What we check for is the NLPID and a frame
4069 * control field value of UI, i.e. 0x03 followed
4072 * XXX - assumes a 2-byte Frame Relay header with
4073 * DLCI and flags. What if the address is longer?
4075 * XXX - what about SNAP-encapsulated frames?
4077 return gen_cmp(OR_LINK
, 2, BPF_H
, (0x03<<8) | v
);
4083 * Cisco uses an Ethertype lookalike - for OSI,
4086 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4087 /* OSI in C-HDLC is stuffed with a fudge byte */
4088 b1
= gen_cmp(OR_NET_NOSNAP
, 1, BPF_B
, (long)v
);
4093 b0
= gen_linktype(LLCSAP_ISONS
);
4094 b1
= gen_cmp(OR_NET_NOSNAP
, 0, BPF_B
, (long)v
);
4100 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4102 * 4 is the offset of the PDU type relative to the IS-IS
4105 b1
= gen_cmp(OR_NET_NOSNAP
, 4, BPF_B
, (long)v
);
4110 bpf_error("arp does not encapsulate another protocol");
4114 bpf_error("rarp does not encapsulate another protocol");
4118 bpf_error("atalk encapsulation is not specifiable");
4122 bpf_error("decnet encapsulation is not specifiable");
4126 bpf_error("sca does not encapsulate another protocol");
4130 bpf_error("lat does not encapsulate another protocol");
4134 bpf_error("moprc does not encapsulate another protocol");
4138 bpf_error("mopdl does not encapsulate another protocol");
4142 return gen_linktype(v
);
4145 bpf_error("'udp proto' is bogus");
4149 bpf_error("'tcp proto' is bogus");
4153 bpf_error("'sctp proto' is bogus");
4157 bpf_error("'icmp proto' is bogus");
4161 bpf_error("'igmp proto' is bogus");
4165 bpf_error("'igrp proto' is bogus");
4169 bpf_error("'pim proto' is bogus");
4173 bpf_error("'vrrp proto' is bogus");
4178 b0
= gen_linktype(ETHERTYPE_IPV6
);
4180 b1
= gen_cmp(OR_NET
, 6, BPF_B
, (bpf_int32
)v
);
4182 b1
= gen_protochain(v
, Q_IPV6
);
4188 bpf_error("'icmp6 proto' is bogus");
4192 bpf_error("'ah proto' is bogus");
4195 bpf_error("'ah proto' is bogus");
4198 bpf_error("'stp proto' is bogus");
4201 bpf_error("'ipx proto' is bogus");
4204 bpf_error("'netbeui proto' is bogus");
4215 register const char *name
;
4218 int proto
= q
.proto
;
4222 bpf_u_int32 mask
, addr
;
4224 bpf_u_int32
**alist
;
4227 struct sockaddr_in
*sin
;
4228 struct sockaddr_in6
*sin6
;
4229 struct addrinfo
*res
, *res0
;
4230 struct in6_addr mask128
;
4232 struct block
*b
, *tmp
;
4233 int port
, real_proto
;
4239 addr
= pcap_nametonetaddr(name
);
4241 bpf_error("unknown network '%s'", name
);
4242 /* Left justify network addr and calculate its network mask */
4244 while (addr
&& (addr
& 0xff000000) == 0) {
4248 return gen_host(addr
, mask
, proto
, dir
);
4252 if (proto
== Q_LINK
) {
4256 eaddr
= pcap_ether_hostton(name
);
4259 "unknown ether host '%s'", name
);
4260 b
= gen_ehostop(eaddr
, dir
);
4265 eaddr
= pcap_ether_hostton(name
);
4268 "unknown FDDI host '%s'", name
);
4269 b
= gen_fhostop(eaddr
, dir
);
4274 eaddr
= pcap_ether_hostton(name
);
4277 "unknown token ring host '%s'", name
);
4278 b
= gen_thostop(eaddr
, dir
);
4282 case DLT_IEEE802_11
:
4283 eaddr
= pcap_ether_hostton(name
);
4286 "unknown 802.11 host '%s'", name
);
4287 b
= gen_wlanhostop(eaddr
, dir
);
4291 case DLT_IP_OVER_FC
:
4292 eaddr
= pcap_ether_hostton(name
);
4295 "unknown Fibre Channel host '%s'", name
);
4296 b
= gen_ipfchostop(eaddr
, dir
);
4305 * Check that the packet doesn't begin
4306 * with an LE Control marker. (We've
4307 * already generated a test for LANE.)
4309 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
,
4313 eaddr
= pcap_ether_hostton(name
);
4316 "unknown ether host '%s'", name
);
4317 b
= gen_ehostop(eaddr
, dir
);
4323 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4324 } else if (proto
== Q_DECNET
) {
4325 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4327 * I don't think DECNET hosts can be multihomed, so
4328 * there is no need to build up a list of addresses
4330 return (gen_host(dn_addr
, 0, proto
, dir
));
4333 alist
= pcap_nametoaddr(name
);
4334 if (alist
== NULL
|| *alist
== NULL
)
4335 bpf_error("unknown host '%s'", name
);
4337 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4339 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4341 tmp
= gen_host(**alist
++, 0xffffffff,
4348 memset(&mask128
, 0xff, sizeof(mask128
));
4349 res0
= res
= pcap_nametoaddrinfo(name
);
4351 bpf_error("unknown host '%s'", name
);
4353 tproto
= tproto6
= proto
;
4354 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4358 for (res
= res0
; res
; res
= res
->ai_next
) {
4359 switch (res
->ai_family
) {
4361 if (tproto
== Q_IPV6
)
4364 sin
= (struct sockaddr_in
*)
4366 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4367 0xffffffff, tproto
, dir
);
4370 if (tproto6
== Q_IP
)
4373 sin6
= (struct sockaddr_in6
*)
4375 tmp
= gen_host6(&sin6
->sin6_addr
,
4376 &mask128
, tproto6
, dir
);
4387 bpf_error("unknown host '%s'%s", name
,
4388 (proto
== Q_DEFAULT
)
4390 : " for specified address family");
4397 if (proto
!= Q_DEFAULT
&&
4398 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4399 bpf_error("illegal qualifier of 'port'");
4400 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4401 bpf_error("unknown port '%s'", name
);
4402 if (proto
== Q_UDP
) {
4403 if (real_proto
== IPPROTO_TCP
)
4404 bpf_error("port '%s' is tcp", name
);
4405 else if (real_proto
== IPPROTO_SCTP
)
4406 bpf_error("port '%s' is sctp", name
);
4408 /* override PROTO_UNDEF */
4409 real_proto
= IPPROTO_UDP
;
4411 if (proto
== Q_TCP
) {
4412 if (real_proto
== IPPROTO_UDP
)
4413 bpf_error("port '%s' is udp", name
);
4415 else if (real_proto
== IPPROTO_SCTP
)
4416 bpf_error("port '%s' is sctp", name
);
4418 /* override PROTO_UNDEF */
4419 real_proto
= IPPROTO_TCP
;
4421 if (proto
== Q_SCTP
) {
4422 if (real_proto
== IPPROTO_UDP
)
4423 bpf_error("port '%s' is udp", name
);
4425 else if (real_proto
== IPPROTO_TCP
)
4426 bpf_error("port '%s' is tcp", name
);
4428 /* override PROTO_UNDEF */
4429 real_proto
= IPPROTO_SCTP
;
4432 return gen_port(port
, real_proto
, dir
);
4436 b
= gen_port(port
, real_proto
, dir
);
4437 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4443 if (proto
!= Q_DEFAULT
&&
4444 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4445 bpf_error("illegal qualifier of 'portrange'");
4446 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4447 bpf_error("unknown port in range '%s'", name
);
4448 if (proto
== Q_UDP
) {
4449 if (real_proto
== IPPROTO_TCP
)
4450 bpf_error("port in range '%s' is tcp", name
);
4451 else if (real_proto
== IPPROTO_SCTP
)
4452 bpf_error("port in range '%s' is sctp", name
);
4454 /* override PROTO_UNDEF */
4455 real_proto
= IPPROTO_UDP
;
4457 if (proto
== Q_TCP
) {
4458 if (real_proto
== IPPROTO_UDP
)
4459 bpf_error("port in range '%s' is udp", name
);
4460 else if (real_proto
== IPPROTO_SCTP
)
4461 bpf_error("port in range '%s' is sctp", name
);
4463 /* override PROTO_UNDEF */
4464 real_proto
= IPPROTO_TCP
;
4466 if (proto
== Q_SCTP
) {
4467 if (real_proto
== IPPROTO_UDP
)
4468 bpf_error("port in range '%s' is udp", name
);
4469 else if (real_proto
== IPPROTO_TCP
)
4470 bpf_error("port in range '%s' is tcp", name
);
4472 /* override PROTO_UNDEF */
4473 real_proto
= IPPROTO_SCTP
;
4476 return gen_portrange(port1
, port2
, real_proto
, dir
);
4480 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4481 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4488 eaddr
= pcap_ether_hostton(name
);
4490 bpf_error("unknown ether host: %s", name
);
4492 alist
= pcap_nametoaddr(name
);
4493 if (alist
== NULL
|| *alist
== NULL
)
4494 bpf_error("unknown host '%s'", name
);
4495 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4499 bpf_error("'gateway' not supported in this configuration");
4503 real_proto
= lookup_proto(name
, proto
);
4504 if (real_proto
>= 0)
4505 return gen_proto(real_proto
, proto
, dir
);
4507 bpf_error("unknown protocol: %s", name
);
4510 real_proto
= lookup_proto(name
, proto
);
4511 if (real_proto
>= 0)
4512 return gen_protochain(real_proto
, proto
, dir
);
4514 bpf_error("unknown protocol: %s", name
);
4526 gen_mcode(s1
, s2
, masklen
, q
)
4527 register const char *s1
, *s2
;
4528 register int masklen
;
4531 register int nlen
, mlen
;
4534 nlen
= __pcap_atoin(s1
, &n
);
4535 /* Promote short ipaddr */
4539 mlen
= __pcap_atoin(s2
, &m
);
4540 /* Promote short ipaddr */
4543 bpf_error("non-network bits set in \"%s mask %s\"",
4546 /* Convert mask len to mask */
4548 bpf_error("mask length must be <= 32");
4549 m
= 0xffffffff << (32 - masklen
);
4551 bpf_error("non-network bits set in \"%s/%d\"",
4558 return gen_host(n
, m
, q
.proto
, q
.dir
);
4561 bpf_error("Mask syntax for networks only");
4569 register const char *s
;
4574 int proto
= q
.proto
;
4580 else if (q
.proto
== Q_DECNET
)
4581 vlen
= __pcap_atodn(s
, &v
);
4583 vlen
= __pcap_atoin(s
, &v
);
4590 if (proto
== Q_DECNET
)
4591 return gen_host(v
, 0, proto
, dir
);
4592 else if (proto
== Q_LINK
) {
4593 bpf_error("illegal link layer address");
4596 if (s
== NULL
&& q
.addr
== Q_NET
) {
4597 /* Promote short net number */
4598 while (v
&& (v
& 0xff000000) == 0) {
4603 /* Promote short ipaddr */
4607 return gen_host(v
, mask
, proto
, dir
);
4612 proto
= IPPROTO_UDP
;
4613 else if (proto
== Q_TCP
)
4614 proto
= IPPROTO_TCP
;
4615 else if (proto
== Q_SCTP
)
4616 proto
= IPPROTO_SCTP
;
4617 else if (proto
== Q_DEFAULT
)
4618 proto
= PROTO_UNDEF
;
4620 bpf_error("illegal qualifier of 'port'");
4623 return gen_port((int)v
, proto
, dir
);
4627 b
= gen_port((int)v
, proto
, dir
);
4628 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4635 proto
= IPPROTO_UDP
;
4636 else if (proto
== Q_TCP
)
4637 proto
= IPPROTO_TCP
;
4638 else if (proto
== Q_SCTP
)
4639 proto
= IPPROTO_SCTP
;
4640 else if (proto
== Q_DEFAULT
)
4641 proto
= PROTO_UNDEF
;
4643 bpf_error("illegal qualifier of 'portrange'");
4646 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4650 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4651 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4657 bpf_error("'gateway' requires a name");
4661 return gen_proto((int)v
, proto
, dir
);
4664 return gen_protochain((int)v
, proto
, dir
);
4679 gen_mcode6(s1
, s2
, masklen
, q
)
4680 register const char *s1
, *s2
;
4681 register int masklen
;
4684 struct addrinfo
*res
;
4685 struct in6_addr
*addr
;
4686 struct in6_addr mask
;
4691 bpf_error("no mask %s supported", s2
);
4693 res
= pcap_nametoaddrinfo(s1
);
4695 bpf_error("invalid ip6 address %s", s1
);
4697 bpf_error("%s resolved to multiple address", s1
);
4698 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4700 if (sizeof(mask
) * 8 < masklen
)
4701 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4702 memset(&mask
, 0, sizeof(mask
));
4703 memset(&mask
, 0xff, masklen
/ 8);
4705 mask
.s6_addr
[masklen
/ 8] =
4706 (0xff << (8 - masklen
% 8)) & 0xff;
4709 a
= (u_int32_t
*)addr
;
4710 m
= (u_int32_t
*)&mask
;
4711 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4712 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4713 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4721 bpf_error("Mask syntax for networks only");
4725 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4730 bpf_error("invalid qualifier against IPv6 address");
4738 register const u_char
*eaddr
;
4741 struct block
*b
, *tmp
;
4743 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4744 if (linktype
== DLT_EN10MB
)
4745 return gen_ehostop(eaddr
, (int)q
.dir
);
4746 if (linktype
== DLT_FDDI
)
4747 return gen_fhostop(eaddr
, (int)q
.dir
);
4748 if (linktype
== DLT_IEEE802
)
4749 return gen_thostop(eaddr
, (int)q
.dir
);
4750 if (linktype
== DLT_IEEE802_11
)
4751 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4752 if (linktype
== DLT_SUNATM
&& is_lane
) {
4754 * Check that the packet doesn't begin with an
4755 * LE Control marker. (We've already generated
4758 tmp
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
4763 * Now check the MAC address.
4765 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4769 if (linktype
== DLT_IP_OVER_FC
)
4770 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4771 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4773 bpf_error("ethernet address used in non-ether expression");
4779 struct slist
*s0
, *s1
;
4782 * This is definitely not the best way to do this, but the
4783 * lists will rarely get long.
4790 static struct slist
*
4796 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4801 static struct slist
*
4807 s
= new_stmt(BPF_LD
|BPF_MEM
);
4813 gen_load(proto
, index
, size
)
4818 struct slist
*s
, *tmp
;
4820 int regno
= alloc_reg();
4822 free_reg(index
->regno
);
4826 bpf_error("data size must be 1, 2, or 4");
4842 bpf_error("unsupported index operation");
4846 * XXX - what about ATM LANE? Should the index be
4847 * relative to the beginning of the AAL5 frame, so
4848 * that 0 refers to the beginning of the LE Control
4849 * field, or relative to the beginning of the LAN
4850 * frame, so that 0 refers, for Ethernet LANE, to
4851 * the beginning of the destination address?
4853 s
= xfer_to_x(index
);
4854 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4856 sappend(index
->s
, s
);
4871 /* XXX Note that we assume a fixed link header here. */
4872 s
= xfer_to_x(index
);
4873 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4876 sappend(index
->s
, s
);
4878 b
= gen_proto_abbrev(proto
);
4880 gen_and(index
->b
, b
);
4892 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4894 sappend(s
, xfer_to_a(index
));
4895 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4896 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4897 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4899 sappend(index
->s
, s
);
4901 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4903 gen_and(index
->b
, b
);
4905 gen_and(gen_proto_abbrev(Q_IP
), b
);
4911 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4915 index
->regno
= regno
;
4916 s
= new_stmt(BPF_ST
);
4918 sappend(index
->s
, s
);
4924 gen_relation(code
, a0
, a1
, reversed
)
4926 struct arth
*a0
, *a1
;
4929 struct slist
*s0
, *s1
, *s2
;
4930 struct block
*b
, *tmp
;
4934 if (code
== BPF_JEQ
) {
4935 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4936 b
= new_block(JMP(code
));
4940 b
= new_block(BPF_JMP
|code
|BPF_X
);
4946 sappend(a0
->s
, a1
->s
);
4950 free_reg(a0
->regno
);
4951 free_reg(a1
->regno
);
4953 /* 'and' together protocol checks */
4956 gen_and(a0
->b
, tmp
= a1
->b
);
4972 int regno
= alloc_reg();
4973 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4976 s
= new_stmt(BPF_LD
|BPF_LEN
);
4977 s
->next
= new_stmt(BPF_ST
);
4978 s
->next
->s
.k
= regno
;
4993 a
= (struct arth
*)newchunk(sizeof(*a
));
4997 s
= new_stmt(BPF_LD
|BPF_IMM
);
4999 s
->next
= new_stmt(BPF_ST
);
5015 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5018 s
= new_stmt(BPF_ST
);
5026 gen_arth(code
, a0
, a1
)
5028 struct arth
*a0
, *a1
;
5030 struct slist
*s0
, *s1
, *s2
;
5034 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5039 sappend(a0
->s
, a1
->s
);
5041 free_reg(a0
->regno
);
5042 free_reg(a1
->regno
);
5044 s0
= new_stmt(BPF_ST
);
5045 a0
->regno
= s0
->s
.k
= alloc_reg();
5052 * Here we handle simple allocation of the scratch registers.
5053 * If too many registers are alloc'd, the allocator punts.
5055 static int regused
[BPF_MEMWORDS
];
5059 * Return the next free register.
5064 int n
= BPF_MEMWORDS
;
5067 if (regused
[curreg
])
5068 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5070 regused
[curreg
] = 1;
5074 bpf_error("too many registers needed to evaluate expression");
5079 * Return a register to the table so it can
5089 static struct block
*
5096 s
= new_stmt(BPF_LD
|BPF_LEN
);
5097 b
= new_block(JMP(jmp
));
5108 return gen_len(BPF_JGE
, n
);
5112 * Actually, this is less than or equal.
5120 b
= gen_len(BPF_JGT
, n
);
5127 gen_byteop(op
, idx
, val
)
5138 return gen_cmp(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5141 b
= gen_cmp_lt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5145 b
= gen_cmp_gt(OR_LINK
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5149 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5153 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5157 b
= new_block(JMP(BPF_JEQ
));
5164 static u_char abroadcast
[] = { 0x0 };
5167 gen_broadcast(proto
)
5170 bpf_u_int32 hostmask
;
5171 struct block
*b0
, *b1
, *b2
;
5172 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5178 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5179 return gen_ahostop(abroadcast
, Q_DST
);
5180 if (linktype
== DLT_EN10MB
)
5181 return gen_ehostop(ebroadcast
, Q_DST
);
5182 if (linktype
== DLT_FDDI
)
5183 return gen_fhostop(ebroadcast
, Q_DST
);
5184 if (linktype
== DLT_IEEE802
)
5185 return gen_thostop(ebroadcast
, Q_DST
);
5186 if (linktype
== DLT_IEEE802_11
)
5187 return gen_wlanhostop(ebroadcast
, Q_DST
);
5188 if (linktype
== DLT_IP_OVER_FC
)
5189 return gen_ipfchostop(ebroadcast
, Q_DST
);
5190 if (linktype
== DLT_SUNATM
&& is_lane
) {
5192 * Check that the packet doesn't begin with an
5193 * LE Control marker. (We've already generated
5196 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5201 * Now check the MAC address.
5203 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5207 bpf_error("not a broadcast link");
5211 b0
= gen_linktype(ETHERTYPE_IP
);
5212 hostmask
= ~netmask
;
5213 b1
= gen_mcmp(OR_NET
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
5214 b2
= gen_mcmp(OR_NET
, 16, BPF_W
,
5215 (bpf_int32
)(~0 & hostmask
), hostmask
);
5220 bpf_error("only link-layer/IP broadcast filters supported");
5225 * Generate code to test the low-order bit of a MAC address (that's
5226 * the bottom bit of the *first* byte).
5228 static struct block
*
5229 gen_mac_multicast(offset
)
5232 register struct block
*b0
;
5233 register struct slist
*s
;
5235 /* link[offset] & 1 != 0 */
5236 s
= gen_load_a(OR_LINK
, offset
, BPF_B
);
5237 b0
= new_block(JMP(BPF_JSET
));
5244 gen_multicast(proto
)
5247 register struct block
*b0
, *b1
, *b2
;
5248 register struct slist
*s
;
5254 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5255 /* all ARCnet multicasts use the same address */
5256 return gen_ahostop(abroadcast
, Q_DST
);
5258 if (linktype
== DLT_EN10MB
) {
5259 /* ether[0] & 1 != 0 */
5260 return gen_mac_multicast(0);
5263 if (linktype
== DLT_FDDI
) {
5265 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5267 * XXX - was that referring to bit-order issues?
5269 /* fddi[1] & 1 != 0 */
5270 return gen_mac_multicast(1);
5273 if (linktype
== DLT_IEEE802
) {
5274 /* tr[2] & 1 != 0 */
5275 return gen_mac_multicast(2);
5278 if (linktype
== DLT_IEEE802_11
) {
5282 * For control frames, there is no DA.
5284 * For management frames, DA is at an
5285 * offset of 4 from the beginning of
5288 * For data frames, DA is at an offset
5289 * of 4 from the beginning of the packet
5290 * if To DS is clear and at an offset of
5291 * 16 from the beginning of the packet
5296 * Generate the tests to be done for data frames.
5298 * First, check for To DS set, i.e. "link[1] & 0x01".
5300 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5301 b1
= new_block(JMP(BPF_JSET
));
5302 b1
->s
.k
= 0x01; /* To DS */
5306 * If To DS is set, the DA is at 16.
5308 b0
= gen_mac_multicast(16);
5312 * Now, check for To DS not set, i.e. check
5313 * "!(link[1] & 0x01)".
5315 s
= gen_load_a(OR_LINK
, 1, BPF_B
);
5316 b2
= new_block(JMP(BPF_JSET
));
5317 b2
->s
.k
= 0x01; /* To DS */
5322 * If To DS is not set, the DA is at 4.
5324 b1
= gen_mac_multicast(4);
5328 * Now OR together the last two checks. That gives
5329 * the complete set of checks for data frames.
5334 * Now check for a data frame.
5335 * I.e, check "link[0] & 0x08".
5337 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5338 b1
= new_block(JMP(BPF_JSET
));
5343 * AND that with the checks done for data frames.
5348 * If the high-order bit of the type value is 0, this
5349 * is a management frame.
5350 * I.e, check "!(link[0] & 0x08)".
5352 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5353 b2
= new_block(JMP(BPF_JSET
));
5359 * For management frames, the DA is at 4.
5361 b1
= gen_mac_multicast(4);
5365 * OR that with the checks done for data frames.
5366 * That gives the checks done for management and
5372 * If the low-order bit of the type value is 1,
5373 * this is either a control frame or a frame
5374 * with a reserved type, and thus not a
5377 * I.e., check "!(link[0] & 0x04)".
5379 s
= gen_load_a(OR_LINK
, 0, BPF_B
);
5380 b1
= new_block(JMP(BPF_JSET
));
5386 * AND that with the checks for data and management
5393 if (linktype
== DLT_IP_OVER_FC
) {
5394 b0
= gen_mac_multicast(2);
5398 if (linktype
== DLT_SUNATM
&& is_lane
) {
5400 * Check that the packet doesn't begin with an
5401 * LE Control marker. (We've already generated
5404 b1
= gen_cmp(OR_LINK
, SUNATM_PKT_BEGIN_POS
, BPF_H
,
5408 /* ether[off_mac] & 1 != 0 */
5409 b0
= gen_mac_multicast(off_mac
);
5414 /* Link not known to support multicasts */
5418 b0
= gen_linktype(ETHERTYPE_IP
);
5419 b1
= gen_cmp_ge(OR_NET
, 16, BPF_B
, (bpf_int32
)224);
5425 b0
= gen_linktype(ETHERTYPE_IPV6
);
5426 b1
= gen_cmp(OR_NET
, 24, BPF_B
, (bpf_int32
)255);
5431 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5436 * generate command for inbound/outbound. It's here so we can
5437 * make it link-type specific. 'dir' = 0 implies "inbound",
5438 * = 1 implies "outbound".
5444 register struct block
*b0
;
5447 * Only some data link types support inbound/outbound qualifiers.
5451 b0
= gen_relation(BPF_JEQ
,
5452 gen_load(Q_LINK
, gen_loadi(0), 1),
5460 * Match packets sent by this machine.
5462 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
5465 * Match packets sent to this machine.
5466 * (No broadcast or multicast packets, or
5467 * packets sent to some other machine and
5468 * received promiscuously.)
5470 * XXX - packets sent to other machines probably
5471 * shouldn't be matched, but what about broadcast
5472 * or multicast packets we received?
5474 b0
= gen_cmp(OR_LINK
, 0, BPF_H
, LINUX_SLL_HOST
);
5479 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, dir
), BPF_B
,
5480 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5485 /* match outgoing packets */
5486 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_OUT
);
5488 /* match incoming packets */
5489 b0
= gen_cmp(OR_LINK
, 0, BPF_B
, PPP_PPPD_IN
);
5493 case DLT_JUNIPER_MLFR
:
5494 case DLT_JUNIPER_MLPPP
:
5495 case DLT_JUNIPER_ATM1
:
5496 case DLT_JUNIPER_ATM2
:
5497 /* juniper flags (including direction) are stored
5498 * the byte after the 3-byte magic number */
5500 /* match outgoing packets */
5501 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 0, 0x01);
5503 /* match incoming packets */
5504 b0
= gen_mcmp(OR_LINK
, 3, BPF_B
, 1, 0x01);
5509 bpf_error("inbound/outbound not supported on linktype %d",
5517 /* PF firewall log matched interface */
5519 gen_pf_ifname(const char *ifname
)
5524 if (linktype
== DLT_PFLOG
) {
5525 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5526 off
= offsetof(struct pfloghdr
, ifname
);
5528 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5531 if (strlen(ifname
) >= len
) {
5532 bpf_error("ifname interface names can only be %d characters",
5536 b0
= gen_bcmp(OR_LINK
, off
, strlen(ifname
), (const u_char
*)ifname
);
5540 /* PF firewall log matched interface */
5542 gen_pf_ruleset(char *ruleset
)
5546 if (linktype
!= DLT_PFLOG
) {
5547 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5550 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5551 bpf_error("ruleset names can only be %ld characters",
5552 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5555 b0
= gen_bcmp(OR_LINK
, offsetof(struct pfloghdr
, ruleset
),
5556 strlen(ruleset
), (const u_char
*)ruleset
);
5560 /* PF firewall log rule number */
5566 if (linktype
== DLT_PFLOG
) {
5567 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5570 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5577 /* PF firewall log sub-rule number */
5579 gen_pf_srnr(int srnr
)
5583 if (linktype
!= DLT_PFLOG
) {
5584 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5588 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5593 /* PF firewall log reason code */
5595 gen_pf_reason(int reason
)
5599 if (linktype
== DLT_PFLOG
) {
5600 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, reason
), BPF_B
,
5603 bpf_error("reason not supported on linktype 0x%x", linktype
);
5610 /* PF firewall log action */
5612 gen_pf_action(int action
)
5616 if (linktype
== DLT_PFLOG
) {
5617 b0
= gen_cmp(OR_LINK
, offsetof(struct pfloghdr
, action
), BPF_B
,
5620 bpf_error("action not supported on linktype 0x%x", linktype
);
5629 register const u_char
*eaddr
;
5632 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5633 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5634 return gen_ahostop(eaddr
, (int)q
.dir
);
5636 bpf_error("ARCnet address used in non-arc expression");
5640 static struct block
*
5641 gen_ahostop(eaddr
, dir
)
5642 register const u_char
*eaddr
;
5645 register struct block
*b0
, *b1
;
5648 /* src comes first, different from Ethernet */
5650 return gen_bcmp(OR_LINK
, 0, 1, eaddr
);
5653 return gen_bcmp(OR_LINK
, 1, 1, eaddr
);
5656 b0
= gen_ahostop(eaddr
, Q_SRC
);
5657 b1
= gen_ahostop(eaddr
, Q_DST
);
5663 b0
= gen_ahostop(eaddr
, Q_SRC
);
5664 b1
= gen_ahostop(eaddr
, Q_DST
);
5673 * support IEEE 802.1Q VLAN trunk over ethernet
5682 * Change the offsets to point to the type and data fields within
5683 * the VLAN packet. Just increment the offsets, so that we
5684 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5685 * capture VLAN 200 encapsulated within VLAN 100.
5687 * XXX - this is a bit of a kludge. If we were to split the
5688 * compiler into a parser that parses an expression and
5689 * generates an expression tree, and a code generator that
5690 * takes an expression tree (which could come from our
5691 * parser or from some other parser) and generates BPF code,
5692 * we could perhaps make the offsets parameters of routines
5693 * and, in the handler for an "AND" node, pass to subnodes
5694 * other than the VLAN node the adjusted offsets.
5696 * This would mean that "vlan" would, instead of changing the
5697 * behavior of *all* tests after it, change only the behavior
5698 * of tests ANDed with it. That would change the documented
5699 * semantics of "vlan", which might break some expressions.
5700 * However, it would mean that "(vlan and ip) or ip" would check
5701 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5702 * checking only for VLAN-encapsulated IP, so that could still
5703 * be considered worth doing; it wouldn't break expressions
5704 * that are of the form "vlan and ..." or "vlan N and ...",
5705 * which I suspect are the most common expressions involving
5706 * "vlan". "vlan or ..." doesn't necessarily do what the user
5707 * would really want, now, as all the "or ..." tests would
5708 * be done assuming a VLAN, even though the "or" could be viewed
5709 * as meaning "or, if this isn't a VLAN packet...".
5711 orig_linktype
= off_linktype
; /* save original values */
5723 bpf_error("no VLAN support for data link type %d",
5728 /* check for VLAN */
5729 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5731 /* If a specific VLAN is requested, check VLAN id */
5732 if (vlan_num
>= 0) {
5735 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_H
, (bpf_int32
)vlan_num
,
5754 * Change the offsets to point to the type and data fields within
5755 * the MPLS packet. Just increment the offsets, so that we
5756 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5757 * capture packets with an outer label of 100000 and an inner
5760 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5762 orig_linktype
= off_linktype
; /* save original values */
5767 case DLT_C_HDLC
: /* fall through */
5773 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
5774 (bpf_int32
)ETHERTYPE_MPLS
);
5782 b0
= gen_cmp(OR_LINK
, orig_linktype
, BPF_H
,
5783 (bpf_int32
)PPP_MPLS_UCAST
);
5786 /* FIXME add other DLT_s ...
5787 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5788 * leave it for now */
5791 bpf_error("no MPLS support for data link type %d",
5798 /* If a specific MPLS label is requested, check it */
5799 if (label_num
>= 0) {
5802 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5803 b1
= gen_mcmp(OR_LINK
, orig_nl
, BPF_W
, (bpf_int32
)label_num
,
5804 0xfffff000); /* only compare the first 20 bits */
5813 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5825 bpf_error("'vpi' supported only on raw ATM");
5826 if (off_vpi
== (u_int
)-1)
5828 b0
= gen_ncmp(OR_LINK
, off_vpi
, BPF_B
, 0xffffffff, (u_int
)jtype
,
5829 (u_int
)jvalue
, reverse
);
5834 bpf_error("'vci' supported only on raw ATM");
5835 if (off_vci
== (u_int
)-1)
5837 b0
= gen_ncmp(OR_LINK
, off_vci
, BPF_H
, 0xffffffff, (u_int
)jtype
,
5838 reverse
, (u_int
)jvalue
);
5842 if (off_proto
== (u_int
)-1)
5843 abort(); /* XXX - this isn't on FreeBSD */
5844 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0x0f, (u_int
)jtype
,
5845 reverse
, (u_int
)jvalue
);
5849 if (off_payload
== (u_int
)-1)
5851 b0
= gen_ncmp(OR_LINK
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
5852 0xffffffff, (u_int
)jtype
, reverse
, (u_int
)jvalue
);
5857 bpf_error("'callref' supported only on raw ATM");
5858 if (off_proto
== (u_int
)-1)
5860 b0
= gen_ncmp(OR_LINK
, off_proto
, BPF_B
, 0xffffffff,
5861 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
5871 gen_atmtype_abbrev(type
)
5874 struct block
*b0
, *b1
;
5879 /* Get all packets in Meta signalling Circuit */
5881 bpf_error("'metac' supported only on raw ATM");
5882 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5883 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5888 /* Get all packets in Broadcast Circuit*/
5890 bpf_error("'bcc' supported only on raw ATM");
5891 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5892 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5897 /* Get all cells in Segment OAM F4 circuit*/
5899 bpf_error("'oam4sc' supported only on raw ATM");
5900 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5901 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5906 /* Get all cells in End-to-End OAM F4 Circuit*/
5908 bpf_error("'oam4ec' supported only on raw ATM");
5909 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5910 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5915 /* Get all packets in connection Signalling Circuit */
5917 bpf_error("'sc' supported only on raw ATM");
5918 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5919 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5924 /* Get all packets in ILMI Circuit */
5926 bpf_error("'ilmic' supported only on raw ATM");
5927 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5928 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5933 /* Get all LANE packets */
5935 bpf_error("'lane' supported only on raw ATM");
5936 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5939 * Arrange that all subsequent tests assume LANE
5940 * rather than LLC-encapsulated packets, and set
5941 * the offsets appropriately for LANE-encapsulated
5944 * "off_mac" is the offset of the Ethernet header,
5945 * which is 2 bytes past the ATM pseudo-header
5946 * (skipping the pseudo-header and 2-byte LE Client
5947 * field). The other offsets are Ethernet offsets
5948 * relative to "off_mac".
5951 off_mac
= off_payload
+ 2; /* MAC header */
5952 off_linktype
= off_mac
+ 12;
5953 off_nl
= off_mac
+ 14; /* Ethernet II */
5954 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5958 /* Get all LLC-encapsulated packets */
5960 bpf_error("'llc' supported only on raw ATM");
5961 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5972 static struct block
*
5973 gen_msg_abbrev(type
)
5979 * Q.2931 signalling protocol messages for handling virtual circuits
5980 * establishment and teardown
5985 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5989 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5993 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5997 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6001 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6004 case A_RELEASE_DONE
:
6005 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6015 gen_atmmulti_abbrev(type
)
6018 struct block
*b0
, *b1
;
6024 bpf_error("'oam' supported only on raw ATM");
6025 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6030 bpf_error("'oamf4' supported only on raw ATM");
6032 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6033 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6035 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6041 * Get Q.2931 signalling messages for switched
6042 * virtual connection
6045 bpf_error("'connectmsg' supported only on raw ATM");
6046 b0
= gen_msg_abbrev(A_SETUP
);
6047 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6049 b0
= gen_msg_abbrev(A_CONNECT
);
6051 b0
= gen_msg_abbrev(A_CONNECTACK
);
6053 b0
= gen_msg_abbrev(A_RELEASE
);
6055 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6057 b0
= gen_atmtype_abbrev(A_SC
);
6063 bpf_error("'metaconnect' supported only on raw ATM");
6064 b0
= gen_msg_abbrev(A_SETUP
);
6065 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6067 b0
= gen_msg_abbrev(A_CONNECT
);
6069 b0
= gen_msg_abbrev(A_RELEASE
);
6071 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6073 b0
= gen_atmtype_abbrev(A_METAC
);