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.213 2004-12-15 00:25:08 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #define offsetof(s, e) ((size_t)&((s *)0)->e)
79 #include <netdb.h> /* for "struct addrinfo" */
82 #include <pcap-namedb.h>
87 #define IPPROTO_SCTP 132
90 #ifdef HAVE_OS_PROTO_H
94 #define JMP(c) ((c)|BPF_JMP|BPF_K)
97 static jmp_buf top_ctx
;
98 static pcap_t
*bpf_pcap
;
100 /* Hack for updating VLAN, MPLS offsets. */
101 static u_int orig_linktype
= -1U, orig_nl
= -1U, orig_nl_nosnap
= -1U;
105 static int pcap_fddipad
;
110 bpf_error(const char *fmt
, ...)
116 if (bpf_pcap
!= NULL
)
117 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
124 static void init_linktype(pcap_t
*);
126 static int alloc_reg(void);
127 static void free_reg(int);
129 static struct block
*root
;
132 * We divy out chunks of memory rather than call malloc each time so
133 * we don't have to worry about leaking memory. It's probably
134 * not a big deal if all this memory was wasted but if this ever
135 * goes into a library that would probably not be a good idea.
137 * XXX - this *is* in a library....
140 #define CHUNK0SIZE 1024
146 static struct chunk chunks
[NCHUNKS
];
147 static int cur_chunk
;
149 static void *newchunk(u_int
);
150 static void freechunks(void);
151 static inline struct block
*new_block(int);
152 static inline struct slist
*new_stmt(int);
153 static struct block
*gen_retblk(int);
154 static inline void syntax(void);
156 static void backpatch(struct block
*, struct block
*);
157 static void merge(struct block
*, struct block
*);
158 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
159 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
160 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
161 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
162 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
163 bpf_u_int32
, bpf_u_int32
, int);
164 static struct block
*gen_uncond(int);
165 static inline struct block
*gen_true(void);
166 static inline struct block
*gen_false(void);
167 static struct block
*gen_ether_linktype(int);
168 static struct block
*gen_linux_sll_linktype(int);
169 static struct block
*gen_linktype(int);
170 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
171 static struct block
*gen_llc(int);
172 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
174 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
176 static struct block
*gen_ahostop(const u_char
*, int);
177 static struct block
*gen_ehostop(const u_char
*, int);
178 static struct block
*gen_fhostop(const u_char
*, int);
179 static struct block
*gen_thostop(const u_char
*, int);
180 static struct block
*gen_wlanhostop(const u_char
*, int);
181 static struct block
*gen_ipfchostop(const u_char
*, int);
182 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
183 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
185 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
188 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
190 static struct block
*gen_ipfrag(void);
191 static struct block
*gen_portatom(int, bpf_int32
);
193 static struct block
*gen_portatom6(int, bpf_int32
);
195 struct block
*gen_portop(int, int, int);
196 static struct block
*gen_port(int, int, int);
198 struct block
*gen_portop6(int, int, int);
199 static struct block
*gen_port6(int, int, int);
201 static int lookup_proto(const char *, int);
202 static struct block
*gen_protochain(int, int, int);
203 static struct block
*gen_proto(int, int, int);
204 static struct slist
*xfer_to_x(struct arth
*);
205 static struct slist
*xfer_to_a(struct arth
*);
206 static struct block
*gen_mac_multicast(int);
207 static struct block
*gen_len(int, int);
209 static struct block
*gen_msg_abbrev(int type
);
220 /* XXX Round up to nearest long. */
221 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
223 /* XXX Round up to structure boundary. */
227 cp
= &chunks
[cur_chunk
];
228 if (n
> cp
->n_left
) {
229 ++cp
, k
= ++cur_chunk
;
231 bpf_error("out of memory");
232 size
= CHUNK0SIZE
<< k
;
233 cp
->m
= (void *)malloc(size
);
235 bpf_error("out of memory");
236 memset((char *)cp
->m
, 0, size
);
239 bpf_error("out of memory");
242 return (void *)((char *)cp
->m
+ cp
->n_left
);
251 for (i
= 0; i
< NCHUNKS
; ++i
)
252 if (chunks
[i
].m
!= NULL
) {
259 * A strdup whose allocations are freed after code generation is over.
263 register const char *s
;
265 int n
= strlen(s
) + 1;
266 char *cp
= newchunk(n
);
272 static inline struct block
*
278 p
= (struct block
*)newchunk(sizeof(*p
));
285 static inline struct slist
*
291 p
= (struct slist
*)newchunk(sizeof(*p
));
297 static struct block
*
301 struct block
*b
= new_block(BPF_RET
|BPF_K
);
310 bpf_error("syntax error in filter expression");
313 static bpf_u_int32 netmask
;
318 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
319 char *buf
, int optimize
, bpf_u_int32 mask
)
328 if (setjmp(top_ctx
)) {
336 snaplen
= pcap_snapshot(p
);
338 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
339 "snaplen of 0 rejects all packets");
343 lex_init(buf
? buf
: "");
351 root
= gen_retblk(snaplen
);
353 if (optimize
&& !no_optimize
) {
356 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
357 bpf_error("expression rejects all packets");
359 program
->bf_insns
= icode_to_fcode(root
, &len
);
360 program
->bf_len
= len
;
368 * entry point for using the compiler with no pcap open
369 * pass in all the stuff that is needed explicitly instead.
372 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
373 struct bpf_program
*program
,
374 char *buf
, int optimize
, bpf_u_int32 mask
)
379 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
382 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
388 * Clean up a "struct bpf_program" by freeing all the memory allocated
392 pcap_freecode(struct bpf_program
*program
)
395 if (program
->bf_insns
!= NULL
) {
396 free((char *)program
->bf_insns
);
397 program
->bf_insns
= NULL
;
402 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
403 * which of the jt and jf fields has been resolved and which is a pointer
404 * back to another unresolved block (or nil). At least one of the fields
405 * in each block is already resolved.
408 backpatch(list
, target
)
409 struct block
*list
, *target
;
426 * Merge the lists in b0 and b1, using the 'sense' field to indicate
427 * which of jt and jf is the link.
431 struct block
*b0
, *b1
;
433 register struct block
**p
= &b0
;
435 /* Find end of list. */
437 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
439 /* Concatenate the lists. */
447 backpatch(p
, gen_retblk(snaplen
));
448 p
->sense
= !p
->sense
;
449 backpatch(p
, gen_retblk(0));
455 struct block
*b0
, *b1
;
457 backpatch(b0
, b1
->head
);
458 b0
->sense
= !b0
->sense
;
459 b1
->sense
= !b1
->sense
;
461 b1
->sense
= !b1
->sense
;
467 struct block
*b0
, *b1
;
469 b0
->sense
= !b0
->sense
;
470 backpatch(b0
, b1
->head
);
471 b0
->sense
= !b0
->sense
;
480 b
->sense
= !b
->sense
;
483 static struct block
*
484 gen_cmp(offset
, size
, v
)
491 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
494 b
= new_block(JMP(BPF_JEQ
));
501 static struct block
*
502 gen_cmp_gt(offset
, size
, v
)
509 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
512 b
= new_block(JMP(BPF_JGT
));
519 static struct block
*
520 gen_mcmp(offset
, size
, v
, mask
)
525 struct block
*b
= gen_cmp(offset
, size
, v
);
528 if (mask
!= 0xffffffff) {
529 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
536 static struct block
*
537 gen_bcmp(offset
, size
, v
)
538 register u_int offset
, size
;
539 register const u_char
*v
;
541 register struct block
*b
, *tmp
;
545 register const u_char
*p
= &v
[size
- 4];
546 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
547 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
549 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
556 register const u_char
*p
= &v
[size
- 2];
557 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
559 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
566 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
574 static struct block
*
575 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
576 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
582 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
585 if (mask
!= 0xffffffff) {
586 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
590 b
= new_block(JMP(jtype
));
593 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
599 * Various code constructs need to know the layout of the data link
600 * layer. These variables give the necessary offsets.
604 * This is the offset of the beginning of the MAC-layer header.
605 * It's usually 0, except for ATM LANE.
607 static u_int off_mac
;
610 * "off_linktype" is the offset to information in the link-layer header
611 * giving the packet type.
613 * For Ethernet, it's the offset of the Ethernet type field.
615 * For link-layer types that always use 802.2 headers, it's the
616 * offset of the LLC header.
618 * For PPP, it's the offset of the PPP type field.
620 * For Cisco HDLC, it's the offset of the CHDLC type field.
622 * For BSD loopback, it's the offset of the AF_ value.
624 * For Linux cooked sockets, it's the offset of the type field.
626 * It's set to -1 for no encapsulation, in which case, IP is assumed.
628 static u_int off_linktype
;
631 * TRUE if the link layer includes an ATM pseudo-header.
633 static int is_atm
= 0;
636 * TRUE if "lane" appeared in the filter; it causes us to generate
637 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
639 static int is_lane
= 0;
642 * These are offsets for the ATM pseudo-header.
644 static u_int off_vpi
;
645 static u_int off_vci
;
646 static u_int off_proto
;
649 * This is the offset of the first byte after the ATM pseudo_header,
650 * or -1 if there is no ATM pseudo-header.
652 static u_int off_payload
;
655 * These are offsets to the beginning of the network-layer header.
657 * If the link layer never uses 802.2 LLC:
659 * "off_nl" and "off_nl_nosnap" are the same.
661 * If the link layer always uses 802.2 LLC:
663 * "off_nl" is the offset if there's a SNAP header following
666 * "off_nl_nosnap" is the offset if there's no SNAP header.
668 * If the link layer is Ethernet:
670 * "off_nl" is the offset if the packet is an Ethernet II packet
671 * (we assume no 802.3+802.2+SNAP);
673 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
674 * with an 802.2 header following it.
677 static u_int off_nl_nosnap
;
685 linktype
= pcap_datalink(p
);
687 pcap_fddipad
= p
->fddipad
;
691 * Assume it's not raw ATM with a pseudo-header, for now.
709 off_nl
= 6; /* XXX in reality, variable! */
710 off_nl_nosnap
= 6; /* no 802.2 LLC */
713 case DLT_ARCNET_LINUX
:
715 off_nl
= 8; /* XXX in reality, variable! */
716 off_nl_nosnap
= 8; /* no 802.2 LLC */
721 off_nl
= 14; /* Ethernet II */
722 off_nl_nosnap
= 17; /* 802.3+802.2 */
727 * SLIP doesn't have a link level type. The 16 byte
728 * header is hacked into our SLIP driver.
732 off_nl_nosnap
= 16; /* no 802.2 LLC */
736 /* XXX this may be the same as the DLT_PPP_BSDOS case */
740 off_nl_nosnap
= 24; /* no 802.2 LLC */
747 off_nl_nosnap
= 4; /* no 802.2 LLC */
753 off_nl_nosnap
= 12; /* no 802.2 LLC */
757 case DLT_PPP_WITHDIRECTION
:
758 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
759 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
762 off_nl_nosnap
= 4; /* no 802.2 LLC */
767 * This does no include the Ethernet header, and
768 * only covers session state.
772 off_nl_nosnap
= 8; /* no 802.2 LLC */
778 off_nl_nosnap
= 24; /* no 802.2 LLC */
783 * FDDI doesn't really have a link-level type field.
784 * We set "off_linktype" to the offset of the LLC header.
786 * To check for Ethernet types, we assume that SSAP = SNAP
787 * is being used and pick out the encapsulated Ethernet type.
788 * XXX - should we generate code to check for SNAP?
792 off_linktype
+= pcap_fddipad
;
794 off_nl
= 21; /* FDDI+802.2+SNAP */
795 off_nl_nosnap
= 16; /* FDDI+802.2 */
797 off_nl
+= pcap_fddipad
;
798 off_nl_nosnap
+= pcap_fddipad
;
804 * Token Ring doesn't really have a link-level type field.
805 * We set "off_linktype" to the offset of the LLC header.
807 * To check for Ethernet types, we assume that SSAP = SNAP
808 * is being used and pick out the encapsulated Ethernet type.
809 * XXX - should we generate code to check for SNAP?
811 * XXX - the header is actually variable-length.
812 * Some various Linux patched versions gave 38
813 * as "off_linktype" and 40 as "off_nl"; however,
814 * if a token ring packet has *no* routing
815 * information, i.e. is not source-routed, the correct
816 * values are 20 and 22, as they are in the vanilla code.
818 * A packet is source-routed iff the uppermost bit
819 * of the first byte of the source address, at an
820 * offset of 8, has the uppermost bit set. If the
821 * packet is source-routed, the total number of bytes
822 * of routing information is 2 plus bits 0x1F00 of
823 * the 16-bit value at an offset of 14 (shifted right
824 * 8 - figure out which byte that is).
827 off_nl
= 22; /* Token Ring+802.2+SNAP */
828 off_nl_nosnap
= 17; /* Token Ring+802.2 */
833 * 802.11 doesn't really have a link-level type field.
834 * We set "off_linktype" to the offset of the LLC header.
836 * To check for Ethernet types, we assume that SSAP = SNAP
837 * is being used and pick out the encapsulated Ethernet type.
838 * XXX - should we generate code to check for SNAP?
840 * XXX - the header is actually variable-length. We
841 * assume a 24-byte link-layer header, as appears in
842 * data frames in networks with no bridges. If the
843 * fromds and tods 802.11 header bits are both set,
844 * it's actually supposed to be 30 bytes.
847 off_nl
= 32; /* 802.11+802.2+SNAP */
848 off_nl_nosnap
= 27; /* 802.11+802.2 */
851 case DLT_PRISM_HEADER
:
853 * Same as 802.11, but with an additional header before
854 * the 802.11 header, containing a bunch of additional
855 * information including radio-level information.
857 * The header is 144 bytes long.
859 * XXX - same variable-length header problem; at least
860 * the Prism header is fixed-length.
862 off_linktype
= 144+24;
863 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
864 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
867 case DLT_IEEE802_11_RADIO_AVS
:
869 * Same as 802.11, but with an additional header before
870 * the 802.11 header, containing a bunch of additional
871 * information including radio-level information.
873 * The header is 64 bytes long, at least in its
874 * current incarnation.
876 * XXX - same variable-length header problem, only
877 * more so; this header is also variable-length,
878 * with the length being the 32-bit big-endian
879 * number at an offset of 4 from the beginning
880 * of the radio header.
882 off_linktype
= 64+24;
883 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
884 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
887 case DLT_IEEE802_11_RADIO
:
889 * Same as 802.11, but with an additional header before
890 * the 802.11 header, containing a bunch of additional
891 * information including radio-level information.
893 * XXX - same variable-length header problem, only
894 * even *more* so; this header is also variable-length,
895 * with the length being the 16-bit number at an offset
896 * of 2 from the beginning of the radio header, and it's
897 * device-dependent (different devices might supply
898 * different amounts of information), so we can't even
899 * assume a fixed length for the current version of the
902 * Therefore, currently, only raw "link[N:M]" filtering is
910 case DLT_ATM_RFC1483
:
911 case DLT_ATM_CLIP
: /* Linux ATM defines this */
913 * assume routed, non-ISO PDUs
914 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
917 off_nl
= 8; /* 802.2+SNAP */
918 off_nl_nosnap
= 3; /* 802.2 */
923 * Full Frontal ATM; you get AALn PDUs with an ATM
927 off_vpi
= SUNATM_VPI_POS
;
928 off_vci
= SUNATM_VCI_POS
;
929 off_proto
= PROTO_POS
;
930 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
931 off_payload
= SUNATM_PKT_BEGIN_POS
;
932 off_linktype
= off_payload
;
933 off_nl
= off_payload
+8; /* 802.2+SNAP */
934 off_nl_nosnap
= off_payload
+3; /* 802.2 */
940 off_nl_nosnap
= 0; /* no 802.2 LLC */
943 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
946 off_nl_nosnap
= 16; /* no 802.2 LLC */
951 * LocalTalk does have a 1-byte type field in the LLAP header,
952 * but really it just indicates whether there is a "short" or
953 * "long" DDP packet following.
957 off_nl_nosnap
= 0; /* no 802.2 LLC */
962 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
963 * link-level type field. We set "off_linktype" to the
964 * offset of the LLC header.
966 * To check for Ethernet types, we assume that SSAP = SNAP
967 * is being used and pick out the encapsulated Ethernet type.
968 * XXX - should we generate code to check for SNAP? RFC
969 * 2625 says SNAP should be used.
972 off_nl
= 24; /* IPFC+802.2+SNAP */
973 off_nl_nosnap
= 19; /* IPFC+802.2 */
978 * XXX - we should set this to handle SNAP-encapsulated
979 * frames (NLPID of 0x80).
983 off_nl_nosnap
= 0; /* no 802.2 LLC */
986 case DLT_APPLE_IP_OVER_IEEE1394
:
989 off_nl_nosnap
= 0; /* no 802.2 LLC */
994 * Currently, only raw "link[N:M]" filtering is supported.
1003 * Currently, only raw "link[N:M]" filtering is supported.
1010 case DLT_SYMANTEC_FIREWALL
:
1012 off_nl
= 44; /* Ethernet II */
1013 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1018 /* XXX read from header? */
1019 off_nl
= PFLOG_HDRLEN
;
1020 off_nl_nosnap
= PFLOG_HDRLEN
;
1023 case DLT_JUNIPER_ATM1
:
1024 off_linktype
= 4; /* in reality variable between 4-8 */
1029 case DLT_JUNIPER_ATM2
:
1030 off_linktype
= 8; /* in reality variable between 8-12 */
1043 bpf_error("unknown data link type %d", linktype
);
1047 static struct block
*
1054 s
= new_stmt(BPF_LD
|BPF_IMM
);
1056 b
= new_block(JMP(BPF_JEQ
));
1062 static inline struct block
*
1065 return gen_uncond(1);
1068 static inline struct block
*
1071 return gen_uncond(0);
1075 * Byte-swap a 32-bit number.
1076 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1077 * big-endian platforms.)
1079 #define SWAPLONG(y) \
1080 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1082 static struct block
*
1083 gen_ether_linktype(proto
)
1086 struct block
*b0
, *b1
;
1092 * OSI protocols always use 802.2 encapsulation.
1093 * XXX - should we check both the DSAP and the
1094 * SSAP, like this, or should we check just the
1097 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1099 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1100 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1105 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1107 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1108 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1112 case LLCSAP_NETBEUI
:
1114 * NetBEUI always uses 802.2 encapsulation.
1115 * XXX - should we check both the DSAP and the
1116 * SSAP, like this, or should we check just the
1119 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1121 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1122 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1130 * Ethernet_II frames, which are Ethernet
1131 * frames with a frame type of ETHERTYPE_IPX;
1133 * Ethernet_802.3 frames, which are 802.3
1134 * frames (i.e., the type/length field is
1135 * a length field, <= ETHERMTU, rather than
1136 * a type field) with the first two bytes
1137 * after the Ethernet/802.3 header being
1140 * Ethernet_802.2 frames, which are 802.3
1141 * frames with an 802.2 LLC header and
1142 * with the IPX LSAP as the DSAP in the LLC
1145 * Ethernet_SNAP frames, which are 802.3
1146 * frames with an LLC header and a SNAP
1147 * header and with an OUI of 0x000000
1148 * (encapsulated Ethernet) and a protocol
1149 * ID of ETHERTYPE_IPX in the SNAP header.
1151 * XXX - should we generate the same code both
1152 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1156 * This generates code to check both for the
1157 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1159 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1160 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1164 * Now we add code to check for SNAP frames with
1165 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1167 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1171 * Now we generate code to check for 802.3
1172 * frames in general.
1174 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1178 * Now add the check for 802.3 frames before the
1179 * check for Ethernet_802.2 and Ethernet_802.3,
1180 * as those checks should only be done on 802.3
1181 * frames, not on Ethernet frames.
1186 * Now add the check for Ethernet_II frames, and
1187 * do that before checking for the other frame
1190 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1194 case ETHERTYPE_ATALK
:
1195 case ETHERTYPE_AARP
:
1197 * EtherTalk (AppleTalk protocols on Ethernet link
1198 * layer) may use 802.2 encapsulation.
1202 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1203 * we check for an Ethernet type field less than
1204 * 1500, which means it's an 802.3 length field.
1206 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1210 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1211 * SNAP packets with an organization code of
1212 * 0x080007 (Apple, for Appletalk) and a protocol
1213 * type of ETHERTYPE_ATALK (Appletalk).
1215 * 802.2-encapsulated ETHERTYPE_AARP packets are
1216 * SNAP packets with an organization code of
1217 * 0x000000 (encapsulated Ethernet) and a protocol
1218 * type of ETHERTYPE_AARP (Appletalk ARP).
1220 if (proto
== ETHERTYPE_ATALK
)
1221 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1222 else /* proto == ETHERTYPE_AARP */
1223 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1227 * Check for Ethernet encapsulation (Ethertalk
1228 * phase 1?); we just check for the Ethernet
1231 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1237 if (proto
<= ETHERMTU
) {
1239 * This is an LLC SAP value, so the frames
1240 * that match would be 802.2 frames.
1241 * Check that the frame is an 802.2 frame
1242 * (i.e., that the length/type field is
1243 * a length field, <= ETHERMTU) and
1244 * then check the DSAP.
1246 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1248 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1253 * This is an Ethernet type, so compare
1254 * the length/type field with it (if
1255 * the frame is an 802.2 frame, the length
1256 * field will be <= ETHERMTU, and, as
1257 * "proto" is > ETHERMTU, this test
1258 * will fail and the frame won't match,
1259 * which is what we want).
1261 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1266 static struct block
*
1267 gen_linux_sll_linktype(proto
)
1270 struct block
*b0
, *b1
;
1275 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1276 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1277 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1283 * OSI protocols always use 802.2 encapsulation.
1284 * XXX - should we check both the DSAP and the
1285 * SSAP, like this, or should we check just the
1288 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1289 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1290 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1294 case LLCSAP_NETBEUI
:
1296 * NetBEUI always uses 802.2 encapsulation.
1297 * XXX - should we check both the DSAP and the
1298 * LSAP, like this, or should we check just the
1301 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1302 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1303 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1309 * Ethernet_II frames, which are Ethernet
1310 * frames with a frame type of ETHERTYPE_IPX;
1312 * Ethernet_802.3 frames, which have a frame
1313 * type of LINUX_SLL_P_802_3;
1315 * Ethernet_802.2 frames, which are 802.3
1316 * frames with an 802.2 LLC header (i.e, have
1317 * a frame type of LINUX_SLL_P_802_2) and
1318 * with the IPX LSAP as the DSAP in the LLC
1321 * Ethernet_SNAP frames, which are 802.3
1322 * frames with an LLC header and a SNAP
1323 * header and with an OUI of 0x000000
1324 * (encapsulated Ethernet) and a protocol
1325 * ID of ETHERTYPE_IPX in the SNAP header.
1327 * First, do the checks on LINUX_SLL_P_802_2
1328 * frames; generate the check for either
1329 * Ethernet_802.2 or Ethernet_SNAP frames, and
1330 * then put a check for LINUX_SLL_P_802_2 frames
1333 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1334 (bpf_int32
)LLCSAP_IPX
);
1335 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1338 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1342 * Now check for 802.3 frames and OR that with
1343 * the previous test.
1345 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1349 * Now add the check for Ethernet_II frames, and
1350 * do that before checking for the other frame
1353 b0
= gen_cmp(off_linktype
, BPF_H
,
1354 (bpf_int32
)ETHERTYPE_IPX
);
1358 case ETHERTYPE_ATALK
:
1359 case ETHERTYPE_AARP
:
1361 * EtherTalk (AppleTalk protocols on Ethernet link
1362 * layer) may use 802.2 encapsulation.
1366 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1367 * we check for the 802.2 protocol type in the
1368 * "Ethernet type" field.
1370 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1373 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1374 * SNAP packets with an organization code of
1375 * 0x080007 (Apple, for Appletalk) and a protocol
1376 * type of ETHERTYPE_ATALK (Appletalk).
1378 * 802.2-encapsulated ETHERTYPE_AARP packets are
1379 * SNAP packets with an organization code of
1380 * 0x000000 (encapsulated Ethernet) and a protocol
1381 * type of ETHERTYPE_AARP (Appletalk ARP).
1383 if (proto
== ETHERTYPE_ATALK
)
1384 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1386 else /* proto == ETHERTYPE_AARP */
1387 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1392 * Check for Ethernet encapsulation (Ethertalk
1393 * phase 1?); we just check for the Ethernet
1396 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1402 if (proto
<= ETHERMTU
) {
1404 * This is an LLC SAP value, so the frames
1405 * that match would be 802.2 frames.
1406 * Check for the 802.2 protocol type
1407 * in the "Ethernet type" field, and
1408 * then check the DSAP.
1410 b0
= gen_cmp(off_linktype
, BPF_H
,
1412 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1418 * This is an Ethernet type, so compare
1419 * the length/type field with it (if
1420 * the frame is an 802.2 frame, the length
1421 * field will be <= ETHERMTU, and, as
1422 * "proto" is > ETHERMTU, this test
1423 * will fail and the frame won't match,
1424 * which is what we want).
1426 return gen_cmp(off_linktype
, BPF_H
,
1432 static struct block
*
1436 struct block
*b0
, *b1
, *b2
;
1441 return gen_ether_linktype(proto
);
1449 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1453 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1459 case DLT_IEEE802_11
:
1460 case DLT_PRISM_HEADER
:
1461 case DLT_IEEE802_11_RADIO
:
1464 case DLT_ATM_RFC1483
:
1466 case DLT_IP_OVER_FC
:
1467 return gen_llc(proto
);
1473 * If "is_lane" is set, check for a LANE-encapsulated
1474 * version of this protocol, otherwise check for an
1475 * LLC-encapsulated version of this protocol.
1477 * We assume LANE means Ethernet, not Token Ring.
1481 * Check that the packet doesn't begin with an
1482 * LE Control marker. (We've already generated
1485 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1489 * Now generate an Ethernet test.
1491 b1
= gen_ether_linktype(proto
);
1496 * Check for LLC encapsulation and then check the
1499 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1500 b1
= gen_llc(proto
);
1506 return gen_linux_sll_linktype(proto
);
1511 case DLT_SLIP_BSDOS
:
1514 * These types don't provide any type field; packets
1517 * XXX - for IPv4, check for a version number of 4, and,
1518 * for IPv6, check for a version number of 6?
1524 case ETHERTYPE_IPV6
:
1526 return gen_true(); /* always true */
1529 return gen_false(); /* always false */
1535 case DLT_PPP_WITHDIRECTION
:
1536 case DLT_PPP_SERIAL
:
1539 * We use Ethernet protocol types inside libpcap;
1540 * map them to the corresponding PPP protocol types.
1549 case ETHERTYPE_IPV6
:
1558 case ETHERTYPE_ATALK
:
1572 * I'm assuming the "Bridging PDU"s that go
1573 * over PPP are Spanning Tree Protocol
1587 * We use Ethernet protocol types inside libpcap;
1588 * map them to the corresponding PPP protocol types.
1593 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1594 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1596 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1601 case ETHERTYPE_IPV6
:
1611 case ETHERTYPE_ATALK
:
1625 * I'm assuming the "Bridging PDU"s that go
1626 * over PPP are Spanning Tree Protocol
1642 * For DLT_NULL, the link-layer header is a 32-bit
1643 * word containing an AF_ value in *host* byte order,
1644 * and for DLT_ENC, the link-layer header begins
1645 * with a 32-bit work containing an AF_ value in
1648 * In addition, if we're reading a saved capture file,
1649 * the host byte order in the capture may not be the
1650 * same as the host byte order on this machine.
1652 * For DLT_LOOP, the link-layer header is a 32-bit
1653 * word containing an AF_ value in *network* byte order.
1655 * XXX - AF_ values may, unfortunately, be platform-
1656 * dependent; for example, FreeBSD's AF_INET6 is 24
1657 * whilst NetBSD's and OpenBSD's is 26.
1659 * This means that, when reading a capture file, just
1660 * checking for our AF_INET6 value won't work if the
1661 * capture file came from another OS.
1670 case ETHERTYPE_IPV6
:
1677 * Not a type on which we support filtering.
1678 * XXX - support those that have AF_ values
1679 * #defined on this platform, at least?
1684 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1686 * The AF_ value is in host byte order, but
1687 * the BPF interpreter will convert it to
1688 * network byte order.
1690 * If this is a save file, and it's from a
1691 * machine with the opposite byte order to
1692 * ours, we byte-swap the AF_ value.
1694 * Then we run it through "htonl()", and
1695 * generate code to compare against the result.
1697 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1698 bpf_pcap
->sf
.swapped
)
1699 proto
= SWAPLONG(proto
);
1700 proto
= htonl(proto
);
1702 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1706 * af field is host byte order in contrast to the rest of
1709 if (proto
== ETHERTYPE_IP
)
1710 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1711 (bpf_int32
)AF_INET
));
1713 else if (proto
== ETHERTYPE_IPV6
)
1714 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1715 (bpf_int32
)AF_INET6
));
1723 case DLT_ARCNET_LINUX
:
1725 * XXX should we check for first fragment if the protocol
1734 case ETHERTYPE_IPV6
:
1735 return (gen_cmp(off_linktype
, BPF_B
,
1736 (bpf_int32
)ARCTYPE_INET6
));
1740 b0
= gen_cmp(off_linktype
, BPF_B
,
1741 (bpf_int32
)ARCTYPE_IP
);
1742 b1
= gen_cmp(off_linktype
, BPF_B
,
1743 (bpf_int32
)ARCTYPE_IP_OLD
);
1748 b0
= gen_cmp(off_linktype
, BPF_B
,
1749 (bpf_int32
)ARCTYPE_ARP
);
1750 b1
= gen_cmp(off_linktype
, BPF_B
,
1751 (bpf_int32
)ARCTYPE_ARP_OLD
);
1755 case ETHERTYPE_REVARP
:
1756 return (gen_cmp(off_linktype
, BPF_B
,
1757 (bpf_int32
)ARCTYPE_REVARP
));
1759 case ETHERTYPE_ATALK
:
1760 return (gen_cmp(off_linktype
, BPF_B
,
1761 (bpf_int32
)ARCTYPE_ATALK
));
1768 case ETHERTYPE_ATALK
:
1778 * XXX - assumes a 2-byte Frame Relay header with
1779 * DLCI and flags. What if the address is longer?
1785 * Check for the special NLPID for IP.
1787 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1790 case ETHERTYPE_IPV6
:
1792 * Check for the special NLPID for IPv6.
1794 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1799 * Check for several OSI protocols.
1801 * Frame Relay packets typically have an OSI
1802 * NLPID at the beginning; we check for each
1805 * What we check for is the NLPID and a frame
1806 * control field of UI, i.e. 0x03 followed
1809 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1810 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1811 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1822 case DLT_JUNIPER_ATM1
:
1823 case DLT_JUNIPER_ATM2
:
1824 /* just lets verify the magic number for now -
1825 * we may have up to 6 different encapsulations on the wire
1826 * and need a lot of heuristics to figure out that the payload
1829 * FIXME encapsulation specific BPF_ filters
1831 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1833 case DLT_LINUX_IRDA
:
1834 bpf_error("IrDA link-layer type filtering not implemented");
1837 bpf_error("DOCSIS link-layer type filtering not implemented");
1841 * All the types that have no encapsulation should either be
1842 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1843 * all packets are IP packets, or should be handled in some
1844 * special case, if none of them are (if some are and some
1845 * aren't, the lack of encapsulation is a problem, as we'd
1846 * have to find some other way of determining the packet type).
1848 * Therefore, if "off_linktype" is -1, there's an error.
1850 if (off_linktype
== (u_int
)-1)
1854 * Any type not handled above should always have an Ethernet
1855 * type at an offset of "off_linktype". (PPP is partially
1856 * handled above - the protocol type is mapped from the
1857 * Ethernet and LLC types we use internally to the corresponding
1858 * PPP type - but the PPP type is always specified by a value
1859 * at "off_linktype", so we don't have to do the code generation
1862 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1866 * Check for an LLC SNAP packet with a given organization code and
1867 * protocol type; we check the entire contents of the 802.2 LLC and
1868 * snap headers, checking for DSAP and SSAP of SNAP and a control
1869 * field of 0x03 in the LLC header, and for the specified organization
1870 * code and protocol type in the SNAP header.
1872 static struct block
*
1873 gen_snap(orgcode
, ptype
, offset
)
1874 bpf_u_int32 orgcode
;
1878 u_char snapblock
[8];
1880 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1881 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1882 snapblock
[2] = 0x03; /* control = UI */
1883 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1884 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1885 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1886 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1887 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1888 return gen_bcmp(offset
, 8, snapblock
);
1892 * Check for a given protocol value assuming an 802.2 LLC header.
1894 static struct block
*
1899 * XXX - handle token-ring variable-length header.
1904 return gen_cmp(off_linktype
, BPF_H
, (long)
1905 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1908 return gen_cmp(off_linktype
, BPF_H
, (long)
1909 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1911 case LLCSAP_NETBEUI
:
1912 return gen_cmp(off_linktype
, BPF_H
, (long)
1913 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1917 * XXX - are there ever SNAP frames for IPX on
1918 * non-Ethernet 802.x networks?
1920 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1922 case ETHERTYPE_ATALK
:
1924 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1925 * SNAP packets with an organization code of
1926 * 0x080007 (Apple, for Appletalk) and a protocol
1927 * type of ETHERTYPE_ATALK (Appletalk).
1929 * XXX - check for an organization code of
1930 * encapsulated Ethernet as well?
1932 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1936 * XXX - we don't have to check for IPX 802.3
1937 * here, but should we check for the IPX Ethertype?
1939 if (proto
<= ETHERMTU
) {
1941 * This is an LLC SAP value, so check
1944 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1947 * This is an Ethernet type; we assume that it's
1948 * unlikely that it'll appear in the right place
1949 * at random, and therefore check only the
1950 * location that would hold the Ethernet type
1951 * in a SNAP frame with an organization code of
1952 * 0x000000 (encapsulated Ethernet).
1954 * XXX - if we were to check for the SNAP DSAP and
1955 * LSAP, as per XXX, and were also to check for an
1956 * organization code of 0x000000 (encapsulated
1957 * Ethernet), we'd do
1959 * return gen_snap(0x000000, proto,
1962 * here; for now, we don't, as per the above.
1963 * I don't know whether it's worth the extra CPU
1964 * time to do the right check or not.
1966 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1971 static struct block
*
1972 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1976 u_int src_off
, dst_off
;
1978 struct block
*b0
, *b1
;
1992 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1993 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1999 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2000 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2007 b0
= gen_linktype(proto
);
2008 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2014 static struct block
*
2015 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2016 struct in6_addr
*addr
;
2017 struct in6_addr
*mask
;
2019 u_int src_off
, dst_off
;
2021 struct block
*b0
, *b1
;
2036 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2037 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2043 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2044 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2051 /* this order is important */
2052 a
= (u_int32_t
*)addr
;
2053 m
= (u_int32_t
*)mask
;
2054 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2055 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2057 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2059 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2061 b0
= gen_linktype(proto
);
2067 static struct block
*
2068 gen_ehostop(eaddr
, dir
)
2069 register const u_char
*eaddr
;
2072 register struct block
*b0
, *b1
;
2076 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2079 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2082 b0
= gen_ehostop(eaddr
, Q_SRC
);
2083 b1
= gen_ehostop(eaddr
, Q_DST
);
2089 b0
= gen_ehostop(eaddr
, Q_SRC
);
2090 b1
= gen_ehostop(eaddr
, Q_DST
);
2099 * Like gen_ehostop, but for DLT_FDDI
2101 static struct block
*
2102 gen_fhostop(eaddr
, dir
)
2103 register const u_char
*eaddr
;
2106 struct block
*b0
, *b1
;
2111 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2113 return gen_bcmp(6 + 1, 6, eaddr
);
2118 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2120 return gen_bcmp(0 + 1, 6, eaddr
);
2124 b0
= gen_fhostop(eaddr
, Q_SRC
);
2125 b1
= gen_fhostop(eaddr
, Q_DST
);
2131 b0
= gen_fhostop(eaddr
, Q_SRC
);
2132 b1
= gen_fhostop(eaddr
, Q_DST
);
2141 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2143 static struct block
*
2144 gen_thostop(eaddr
, dir
)
2145 register const u_char
*eaddr
;
2148 register struct block
*b0
, *b1
;
2152 return gen_bcmp(8, 6, eaddr
);
2155 return gen_bcmp(2, 6, eaddr
);
2158 b0
= gen_thostop(eaddr
, Q_SRC
);
2159 b1
= gen_thostop(eaddr
, Q_DST
);
2165 b0
= gen_thostop(eaddr
, Q_SRC
);
2166 b1
= gen_thostop(eaddr
, Q_DST
);
2175 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2177 static struct block
*
2178 gen_wlanhostop(eaddr
, dir
)
2179 register const u_char
*eaddr
;
2182 register struct block
*b0
, *b1
, *b2
;
2183 register struct slist
*s
;
2190 * For control frames, there is no SA.
2192 * For management frames, SA is at an
2193 * offset of 10 from the beginning of
2196 * For data frames, SA is at an offset
2197 * of 10 from the beginning of the packet
2198 * if From DS is clear, at an offset of
2199 * 16 from the beginning of the packet
2200 * if From DS is set and To DS is clear,
2201 * and an offset of 24 from the beginning
2202 * of the packet if From DS is set and To DS
2207 * Generate the tests to be done for data frames
2210 * First, check for To DS set, i.e. check "link[1] & 0x01".
2212 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2214 b1
= new_block(JMP(BPF_JSET
));
2215 b1
->s
.k
= 0x01; /* To DS */
2219 * If To DS is set, the SA is at 24.
2221 b0
= gen_bcmp(24, 6, eaddr
);
2225 * Now, check for To DS not set, i.e. check
2226 * "!(link[1] & 0x01)".
2228 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2230 b2
= new_block(JMP(BPF_JSET
));
2231 b2
->s
.k
= 0x01; /* To DS */
2236 * If To DS is not set, the SA is at 16.
2238 b1
= gen_bcmp(16, 6, eaddr
);
2242 * Now OR together the last two checks. That gives
2243 * the complete set of checks for data frames with
2249 * Now check for From DS being set, and AND that with
2250 * the ORed-together checks.
2252 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2254 b1
= new_block(JMP(BPF_JSET
));
2255 b1
->s
.k
= 0x02; /* From DS */
2260 * Now check for data frames with From DS not set.
2262 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2264 b2
= new_block(JMP(BPF_JSET
));
2265 b2
->s
.k
= 0x02; /* From DS */
2270 * If From DS isn't set, the SA is at 10.
2272 b1
= gen_bcmp(10, 6, eaddr
);
2276 * Now OR together the checks for data frames with
2277 * From DS not set and for data frames with From DS
2278 * set; that gives the checks done for data frames.
2283 * Now check for a data frame.
2284 * I.e, check "link[0] & 0x08".
2286 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2288 b1
= new_block(JMP(BPF_JSET
));
2293 * AND that with the checks done for data frames.
2298 * If the high-order bit of the type value is 0, this
2299 * is a management frame.
2300 * I.e, check "!(link[0] & 0x08)".
2302 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2304 b2
= new_block(JMP(BPF_JSET
));
2310 * For management frames, the SA is at 10.
2312 b1
= gen_bcmp(10, 6, eaddr
);
2316 * OR that with the checks done for data frames.
2317 * That gives the checks done for management and
2323 * If the low-order bit of the type value is 1,
2324 * this is either a control frame or a frame
2325 * with a reserved type, and thus not a
2328 * I.e., check "!(link[0] & 0x04)".
2330 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2332 b1
= new_block(JMP(BPF_JSET
));
2338 * AND that with the checks for data and management
2348 * For control frames, there is no DA.
2350 * For management frames, DA is at an
2351 * offset of 4 from the beginning of
2354 * For data frames, DA is at an offset
2355 * of 4 from the beginning of the packet
2356 * if To DS is clear and at an offset of
2357 * 16 from the beginning of the packet
2362 * Generate the tests to be done for data frames.
2364 * First, check for To DS set, i.e. "link[1] & 0x01".
2366 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2368 b1
= new_block(JMP(BPF_JSET
));
2369 b1
->s
.k
= 0x01; /* To DS */
2373 * If To DS is set, the DA is at 16.
2375 b0
= gen_bcmp(16, 6, eaddr
);
2379 * Now, check for To DS not set, i.e. check
2380 * "!(link[1] & 0x01)".
2382 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2384 b2
= new_block(JMP(BPF_JSET
));
2385 b2
->s
.k
= 0x01; /* To DS */
2390 * If To DS is not set, the DA is at 4.
2392 b1
= gen_bcmp(4, 6, eaddr
);
2396 * Now OR together the last two checks. That gives
2397 * the complete set of checks for data frames.
2402 * Now check for a data frame.
2403 * I.e, check "link[0] & 0x08".
2405 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2407 b1
= new_block(JMP(BPF_JSET
));
2412 * AND that with the checks done for data frames.
2417 * If the high-order bit of the type value is 0, this
2418 * is a management frame.
2419 * I.e, check "!(link[0] & 0x08)".
2421 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2423 b2
= new_block(JMP(BPF_JSET
));
2429 * For management frames, the DA is at 4.
2431 b1
= gen_bcmp(4, 6, eaddr
);
2435 * OR that with the checks done for data frames.
2436 * That gives the checks done for management and
2442 * If the low-order bit of the type value is 1,
2443 * this is either a control frame or a frame
2444 * with a reserved type, and thus not a
2447 * I.e., check "!(link[0] & 0x04)".
2449 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2451 b1
= new_block(JMP(BPF_JSET
));
2457 * AND that with the checks for data and management
2464 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2465 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2471 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2472 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2481 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2482 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2483 * as the RFC states.)
2485 static struct block
*
2486 gen_ipfchostop(eaddr
, dir
)
2487 register const u_char
*eaddr
;
2490 register struct block
*b0
, *b1
;
2494 return gen_bcmp(10, 6, eaddr
);
2497 return gen_bcmp(2, 6, eaddr
);
2500 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2501 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2507 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2508 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2517 * This is quite tricky because there may be pad bytes in front of the
2518 * DECNET header, and then there are two possible data packet formats that
2519 * carry both src and dst addresses, plus 5 packet types in a format that
2520 * carries only the src node, plus 2 types that use a different format and
2521 * also carry just the src node.
2525 * Instead of doing those all right, we just look for data packets with
2526 * 0 or 1 bytes of padding. If you want to look at other packets, that
2527 * will require a lot more hacking.
2529 * To add support for filtering on DECNET "areas" (network numbers)
2530 * one would want to add a "mask" argument to this routine. That would
2531 * make the filter even more inefficient, although one could be clever
2532 * and not generate masking instructions if the mask is 0xFFFF.
2534 static struct block
*
2535 gen_dnhostop(addr
, dir
, base_off
)
2540 struct block
*b0
, *b1
, *b2
, *tmp
;
2541 u_int offset_lh
; /* offset if long header is received */
2542 u_int offset_sh
; /* offset if short header is received */
2547 offset_sh
= 1; /* follows flags */
2548 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2552 offset_sh
= 3; /* follows flags, dstnode */
2553 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2557 /* Inefficient because we do our Calvinball dance twice */
2558 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2559 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2565 /* Inefficient because we do our Calvinball dance twice */
2566 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2567 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2572 bpf_error("ISO host filtering not implemented");
2577 b0
= gen_linktype(ETHERTYPE_DN
);
2578 /* Check for pad = 1, long header case */
2579 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2580 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2581 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2582 BPF_H
, (bpf_int32
)ntohs(addr
));
2584 /* Check for pad = 0, long header case */
2585 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2586 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2589 /* Check for pad = 1, short header case */
2590 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2591 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2592 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2593 BPF_H
, (bpf_int32
)ntohs(addr
));
2596 /* Check for pad = 0, short header case */
2597 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2598 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2602 /* Combine with test for linktype */
2607 static struct block
*
2608 gen_host(addr
, mask
, proto
, dir
)
2614 struct block
*b0
, *b1
;
2619 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2620 if (off_linktype
!= (u_int
)-1) {
2621 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2623 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2629 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2630 off_nl
+ 12, off_nl
+ 16);
2633 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2634 off_nl
+ 14, off_nl
+ 24);
2637 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2638 off_nl
+ 14, off_nl
+ 24);
2641 bpf_error("'tcp' modifier applied to host");
2644 bpf_error("'sctp' modifier applied to host");
2647 bpf_error("'udp' modifier applied to host");
2650 bpf_error("'icmp' modifier applied to host");
2653 bpf_error("'igmp' modifier applied to host");
2656 bpf_error("'igrp' modifier applied to host");
2659 bpf_error("'pim' modifier applied to host");
2662 bpf_error("'vrrp' modifier applied to host");
2665 bpf_error("ATALK host filtering not implemented");
2668 bpf_error("AARP host filtering not implemented");
2671 return gen_dnhostop(addr
, dir
, off_nl
);
2674 bpf_error("SCA host filtering not implemented");
2677 bpf_error("LAT host filtering not implemented");
2680 bpf_error("MOPDL host filtering not implemented");
2683 bpf_error("MOPRC host filtering not implemented");
2687 bpf_error("'ip6' modifier applied to ip host");
2690 bpf_error("'icmp6' modifier applied to host");
2694 bpf_error("'ah' modifier applied to host");
2697 bpf_error("'esp' modifier applied to host");
2700 bpf_error("ISO host filtering not implemented");
2703 bpf_error("'esis' modifier applied to host");
2706 bpf_error("'isis' modifier applied to host");
2709 bpf_error("'clnp' modifier applied to host");
2712 bpf_error("'stp' modifier applied to host");
2715 bpf_error("IPX host filtering not implemented");
2718 bpf_error("'netbeui' modifier applied to host");
2727 static struct block
*
2728 gen_host6(addr
, mask
, proto
, dir
)
2729 struct in6_addr
*addr
;
2730 struct in6_addr
*mask
;
2737 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2740 bpf_error("'ip' modifier applied to ip6 host");
2743 bpf_error("'rarp' modifier applied to ip6 host");
2746 bpf_error("'arp' modifier applied to ip6 host");
2749 bpf_error("'sctp' modifier applied to host");
2752 bpf_error("'tcp' modifier applied to host");
2755 bpf_error("'udp' modifier applied to host");
2758 bpf_error("'icmp' modifier applied to host");
2761 bpf_error("'igmp' modifier applied to host");
2764 bpf_error("'igrp' modifier applied to host");
2767 bpf_error("'pim' modifier applied to host");
2770 bpf_error("'vrrp' modifier applied to host");
2773 bpf_error("ATALK host filtering not implemented");
2776 bpf_error("AARP host filtering not implemented");
2779 bpf_error("'decnet' modifier applied to ip6 host");
2782 bpf_error("SCA host filtering not implemented");
2785 bpf_error("LAT host filtering not implemented");
2788 bpf_error("MOPDL host filtering not implemented");
2791 bpf_error("MOPRC host filtering not implemented");
2794 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2795 off_nl
+ 8, off_nl
+ 24);
2798 bpf_error("'icmp6' modifier applied to host");
2801 bpf_error("'ah' modifier applied to host");
2804 bpf_error("'esp' modifier applied to host");
2807 bpf_error("ISO host filtering not implemented");
2810 bpf_error("'esis' modifier applied to host");
2813 bpf_error("'isis' modifier applied to host");
2816 bpf_error("'clnp' modifier applied to host");
2819 bpf_error("'stp' modifier applied to host");
2822 bpf_error("IPX host filtering not implemented");
2825 bpf_error("'netbeui' modifier applied to host");
2835 static struct block
*
2836 gen_gateway(eaddr
, alist
, proto
, dir
)
2837 const u_char
*eaddr
;
2838 bpf_u_int32
**alist
;
2842 struct block
*b0
, *b1
, *tmp
;
2845 bpf_error("direction applied to 'gateway'");
2852 if (linktype
== DLT_EN10MB
)
2853 b0
= gen_ehostop(eaddr
, Q_OR
);
2854 else if (linktype
== DLT_FDDI
)
2855 b0
= gen_fhostop(eaddr
, Q_OR
);
2856 else if (linktype
== DLT_IEEE802
)
2857 b0
= gen_thostop(eaddr
, Q_OR
);
2858 else if (linktype
== DLT_IEEE802_11
)
2859 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2860 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2862 * Check that the packet doesn't begin with an
2863 * LE Control marker. (We've already generated
2866 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2870 * Now check the MAC address.
2872 b0
= gen_ehostop(eaddr
, Q_OR
);
2874 } else if (linktype
== DLT_IP_OVER_FC
)
2875 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2878 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2880 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2882 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2890 bpf_error("illegal modifier of 'gateway'");
2896 gen_proto_abbrev(proto
)
2905 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2907 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2913 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2915 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2921 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2923 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2929 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2932 #ifndef IPPROTO_IGMP
2933 #define IPPROTO_IGMP 2
2937 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2940 #ifndef IPPROTO_IGRP
2941 #define IPPROTO_IGRP 9
2944 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2948 #define IPPROTO_PIM 103
2952 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2954 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2959 #ifndef IPPROTO_VRRP
2960 #define IPPROTO_VRRP 112
2964 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2968 b1
= gen_linktype(ETHERTYPE_IP
);
2972 b1
= gen_linktype(ETHERTYPE_ARP
);
2976 b1
= gen_linktype(ETHERTYPE_REVARP
);
2980 bpf_error("link layer applied in wrong context");
2983 b1
= gen_linktype(ETHERTYPE_ATALK
);
2987 b1
= gen_linktype(ETHERTYPE_AARP
);
2991 b1
= gen_linktype(ETHERTYPE_DN
);
2995 b1
= gen_linktype(ETHERTYPE_SCA
);
2999 b1
= gen_linktype(ETHERTYPE_LAT
);
3003 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3007 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3012 b1
= gen_linktype(ETHERTYPE_IPV6
);
3015 #ifndef IPPROTO_ICMPV6
3016 #define IPPROTO_ICMPV6 58
3019 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3024 #define IPPROTO_AH 51
3027 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3029 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3035 #define IPPROTO_ESP 50
3038 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3040 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3046 b1
= gen_linktype(LLCSAP_ISONS
);
3050 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3054 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3057 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3058 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3059 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3061 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3063 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3065 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3069 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3070 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3071 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3073 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3075 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3077 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3081 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3082 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3083 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3085 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3090 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3091 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3096 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3097 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3099 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3101 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3106 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3107 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3112 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3113 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3118 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3122 b1
= gen_linktype(LLCSAP_8021D
);
3126 b1
= gen_linktype(LLCSAP_IPX
);
3130 b1
= gen_linktype(LLCSAP_NETBEUI
);
3139 static struct block
*
3146 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3147 s
->s
.k
= off_nl
+ 6;
3148 b
= new_block(JMP(BPF_JSET
));
3156 static struct block
*
3157 gen_portatom(off
, v
)
3164 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3167 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3168 s
->next
->s
.k
= off_nl
+ off
;
3170 b
= new_block(JMP(BPF_JEQ
));
3178 static struct block
*
3179 gen_portatom6(off
, v
)
3183 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3188 gen_portop(port
, proto
, dir
)
3189 int port
, proto
, dir
;
3191 struct block
*b0
, *b1
, *tmp
;
3193 /* ip proto 'proto' */
3194 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3200 b1
= gen_portatom(0, (bpf_int32
)port
);
3204 b1
= gen_portatom(2, (bpf_int32
)port
);
3209 tmp
= gen_portatom(0, (bpf_int32
)port
);
3210 b1
= gen_portatom(2, (bpf_int32
)port
);
3215 tmp
= gen_portatom(0, (bpf_int32
)port
);
3216 b1
= gen_portatom(2, (bpf_int32
)port
);
3228 static struct block
*
3229 gen_port(port
, ip_proto
, dir
)
3234 struct block
*b0
, *b1
, *tmp
;
3239 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3240 * not LLC encapsulation with LLCSAP_IP.
3242 * For IEEE 802 networks - which includes 802.5 token ring
3243 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3244 * says that SNAP encapsulation is used, not LLC encapsulation
3247 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3248 * RFC 2225 say that SNAP encapsulation is used, not LLC
3249 * encapsulation with LLCSAP_IP.
3251 * So we always check for ETHERTYPE_IP.
3253 b0
= gen_linktype(ETHERTYPE_IP
);
3259 b1
= gen_portop(port
, ip_proto
, dir
);
3263 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3264 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3266 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3279 gen_portop6(port
, proto
, dir
)
3280 int port
, proto
, dir
;
3282 struct block
*b0
, *b1
, *tmp
;
3284 /* ip proto 'proto' */
3285 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3289 b1
= gen_portatom6(0, (bpf_int32
)port
);
3293 b1
= gen_portatom6(2, (bpf_int32
)port
);
3298 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3299 b1
= gen_portatom6(2, (bpf_int32
)port
);
3304 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3305 b1
= gen_portatom6(2, (bpf_int32
)port
);
3317 static struct block
*
3318 gen_port6(port
, ip_proto
, dir
)
3323 struct block
*b0
, *b1
, *tmp
;
3325 /* ether proto ip */
3326 b0
= gen_linktype(ETHERTYPE_IPV6
);
3332 b1
= gen_portop6(port
, ip_proto
, dir
);
3336 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3337 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3339 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3352 lookup_proto(name
, proto
)
3353 register const char *name
;
3363 v
= pcap_nametoproto(name
);
3364 if (v
== PROTO_UNDEF
)
3365 bpf_error("unknown ip proto '%s'", name
);
3369 /* XXX should look up h/w protocol type based on linktype */
3370 v
= pcap_nametoeproto(name
);
3371 if (v
== PROTO_UNDEF
)
3372 bpf_error("unknown ether proto '%s'", name
);
3376 if (strcmp(name
, "esis") == 0)
3378 else if (strcmp(name
, "isis") == 0)
3380 else if (strcmp(name
, "clnp") == 0)
3383 bpf_error("unknown osi proto '%s'", name
);
3403 static struct block
*
3404 gen_protochain(v
, proto
, dir
)
3409 #ifdef NO_PROTOCHAIN
3410 return gen_proto(v
, proto
, dir
);
3412 struct block
*b0
, *b
;
3413 struct slist
*s
[100];
3414 int fix2
, fix3
, fix4
, fix5
;
3415 int ahcheck
, again
, end
;
3417 int reg2
= alloc_reg();
3419 memset(s
, 0, sizeof(s
));
3420 fix2
= fix3
= fix4
= fix5
= 0;
3427 b0
= gen_protochain(v
, Q_IP
, dir
);
3428 b
= gen_protochain(v
, Q_IPV6
, dir
);
3432 bpf_error("bad protocol applied for 'protochain'");
3436 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3439 * s[0] is a dummy entry to protect other BPF insn from damaged
3440 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3441 * hard to find interdependency made by jump table fixup.
3444 s
[i
] = new_stmt(0); /*dummy*/
3449 b0
= gen_linktype(ETHERTYPE_IP
);
3452 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3453 s
[i
]->s
.k
= off_nl
+ 9;
3455 /* X = ip->ip_hl << 2 */
3456 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3462 b0
= gen_linktype(ETHERTYPE_IPV6
);
3464 /* A = ip6->ip_nxt */
3465 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3466 s
[i
]->s
.k
= off_nl
+ 6;
3468 /* X = sizeof(struct ip6_hdr) */
3469 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3475 bpf_error("unsupported proto to gen_protochain");
3479 /* again: if (A == v) goto end; else fall through; */
3481 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3483 s
[i
]->s
.jt
= NULL
; /*later*/
3484 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3488 #ifndef IPPROTO_NONE
3489 #define IPPROTO_NONE 59
3491 /* if (A == IPPROTO_NONE) goto end */
3492 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3493 s
[i
]->s
.jt
= NULL
; /*later*/
3494 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3495 s
[i
]->s
.k
= IPPROTO_NONE
;
3496 s
[fix5
]->s
.jf
= s
[i
];
3501 if (proto
== Q_IPV6
) {
3502 int v6start
, v6end
, v6advance
, j
;
3505 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3506 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3507 s
[i
]->s
.jt
= NULL
; /*later*/
3508 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3509 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3510 s
[fix2
]->s
.jf
= s
[i
];
3512 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3513 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3514 s
[i
]->s
.jt
= NULL
; /*later*/
3515 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3516 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3518 /* if (A == IPPROTO_ROUTING) goto v6advance */
3519 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3520 s
[i
]->s
.jt
= NULL
; /*later*/
3521 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3522 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3524 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3525 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3526 s
[i
]->s
.jt
= NULL
; /*later*/
3527 s
[i
]->s
.jf
= NULL
; /*later*/
3528 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3539 * X = X + (P[X + 1] + 1) * 8;
3542 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3544 /* A = P[X + packet head] */
3545 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3549 s
[i
] = new_stmt(BPF_ST
);
3553 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3556 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3560 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3562 /* A = P[X + packet head]; */
3563 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3567 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3571 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3575 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3578 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3582 /* goto again; (must use BPF_JA for backward jump) */
3583 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3584 s
[i
]->s
.k
= again
- i
- 1;
3585 s
[i
- 1]->s
.jf
= s
[i
];
3589 for (j
= v6start
; j
<= v6end
; j
++)
3590 s
[j
]->s
.jt
= s
[v6advance
];
3595 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3597 s
[fix2
]->s
.jf
= s
[i
];
3603 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3604 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3605 s
[i
]->s
.jt
= NULL
; /*later*/
3606 s
[i
]->s
.jf
= NULL
; /*later*/
3607 s
[i
]->s
.k
= IPPROTO_AH
;
3609 s
[fix3
]->s
.jf
= s
[ahcheck
];
3616 * X = X + (P[X + 1] + 2) * 4;
3619 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3621 /* A = P[X + packet head]; */
3622 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3626 s
[i
] = new_stmt(BPF_ST
);
3630 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3633 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3637 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3639 /* A = P[X + packet head] */
3640 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3644 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3648 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3652 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3655 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3659 /* goto again; (must use BPF_JA for backward jump) */
3660 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3661 s
[i
]->s
.k
= again
- i
- 1;
3666 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3668 s
[fix2
]->s
.jt
= s
[end
];
3669 s
[fix4
]->s
.jf
= s
[end
];
3670 s
[fix5
]->s
.jt
= s
[end
];
3677 for (i
= 0; i
< max
- 1; i
++)
3678 s
[i
]->next
= s
[i
+ 1];
3679 s
[max
- 1]->next
= NULL
;
3684 b
= new_block(JMP(BPF_JEQ
));
3685 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3695 static struct block
*
3696 gen_proto(v
, proto
, dir
)
3701 struct block
*b0
, *b1
;
3703 if (dir
!= Q_DEFAULT
)
3704 bpf_error("direction applied to 'proto'");
3709 b0
= gen_proto(v
, Q_IP
, dir
);
3710 b1
= gen_proto(v
, Q_IPV6
, dir
);
3718 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3719 * not LLC encapsulation with LLCSAP_IP.
3721 * For IEEE 802 networks - which includes 802.5 token ring
3722 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3723 * says that SNAP encapsulation is used, not LLC encapsulation
3726 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3727 * RFC 2225 say that SNAP encapsulation is used, not LLC
3728 * encapsulation with LLCSAP_IP.
3730 * So we always check for ETHERTYPE_IP.
3732 b0
= gen_linktype(ETHERTYPE_IP
);
3734 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3736 b1
= gen_protochain(v
, Q_IP
);
3746 * Frame Relay packets typically have an OSI
3747 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3748 * generates code to check for all the OSI
3749 * NLPIDs, so calling it and then adding a check
3750 * for the particular NLPID for which we're
3751 * looking is bogus, as we can just check for
3754 * What we check for is the NLPID and a frame
3755 * control field value of UI, i.e. 0x03 followed
3758 * XXX - assumes a 2-byte Frame Relay header with
3759 * DLCI and flags. What if the address is longer?
3761 * XXX - what about SNAP-encapsulated frames?
3763 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3769 * Cisco uses an Ethertype lookalike - for OSI,
3772 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3773 /* OSI in C-HDLC is stuffed with a fudge byte */
3774 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3779 b0
= gen_linktype(LLCSAP_ISONS
);
3780 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3786 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3788 * 4 is the offset of the PDU type relative to the IS-IS
3791 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3796 bpf_error("arp does not encapsulate another protocol");
3800 bpf_error("rarp does not encapsulate another protocol");
3804 bpf_error("atalk encapsulation is not specifiable");
3808 bpf_error("decnet encapsulation is not specifiable");
3812 bpf_error("sca does not encapsulate another protocol");
3816 bpf_error("lat does not encapsulate another protocol");
3820 bpf_error("moprc does not encapsulate another protocol");
3824 bpf_error("mopdl does not encapsulate another protocol");
3828 return gen_linktype(v
);
3831 bpf_error("'udp proto' is bogus");
3835 bpf_error("'tcp proto' is bogus");
3839 bpf_error("'sctp proto' is bogus");
3843 bpf_error("'icmp proto' is bogus");
3847 bpf_error("'igmp proto' is bogus");
3851 bpf_error("'igrp proto' is bogus");
3855 bpf_error("'pim proto' is bogus");
3859 bpf_error("'vrrp proto' is bogus");
3864 b0
= gen_linktype(ETHERTYPE_IPV6
);
3866 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3868 b1
= gen_protochain(v
, Q_IPV6
);
3874 bpf_error("'icmp6 proto' is bogus");
3878 bpf_error("'ah proto' is bogus");
3881 bpf_error("'ah proto' is bogus");
3884 bpf_error("'stp proto' is bogus");
3887 bpf_error("'ipx proto' is bogus");
3890 bpf_error("'netbeui proto' is bogus");
3901 register const char *name
;
3904 int proto
= q
.proto
;
3908 bpf_u_int32 mask
, addr
;
3910 bpf_u_int32
**alist
;
3913 struct sockaddr_in
*sin
;
3914 struct sockaddr_in6
*sin6
;
3915 struct addrinfo
*res
, *res0
;
3916 struct in6_addr mask128
;
3918 struct block
*b
, *tmp
;
3919 int port
, real_proto
;
3924 addr
= pcap_nametonetaddr(name
);
3926 bpf_error("unknown network '%s'", name
);
3927 /* Left justify network addr and calculate its network mask */
3929 while (addr
&& (addr
& 0xff000000) == 0) {
3933 return gen_host(addr
, mask
, proto
, dir
);
3937 if (proto
== Q_LINK
) {
3941 eaddr
= pcap_ether_hostton(name
);
3944 "unknown ether host '%s'", name
);
3945 b
= gen_ehostop(eaddr
, dir
);
3950 eaddr
= pcap_ether_hostton(name
);
3953 "unknown FDDI host '%s'", name
);
3954 b
= gen_fhostop(eaddr
, dir
);
3959 eaddr
= pcap_ether_hostton(name
);
3962 "unknown token ring host '%s'", name
);
3963 b
= gen_thostop(eaddr
, dir
);
3967 case DLT_IEEE802_11
:
3968 eaddr
= pcap_ether_hostton(name
);
3971 "unknown 802.11 host '%s'", name
);
3972 b
= gen_wlanhostop(eaddr
, dir
);
3976 case DLT_IP_OVER_FC
:
3977 eaddr
= pcap_ether_hostton(name
);
3980 "unknown Fibre Channel host '%s'", name
);
3981 b
= gen_ipfchostop(eaddr
, dir
);
3990 * Check that the packet doesn't begin
3991 * with an LE Control marker. (We've
3992 * already generated a test for LANE.)
3994 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3998 eaddr
= pcap_ether_hostton(name
);
4001 "unknown ether host '%s'", name
);
4002 b
= gen_ehostop(eaddr
, dir
);
4008 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4009 } else if (proto
== Q_DECNET
) {
4010 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4012 * I don't think DECNET hosts can be multihomed, so
4013 * there is no need to build up a list of addresses
4015 return (gen_host(dn_addr
, 0, proto
, dir
));
4018 alist
= pcap_nametoaddr(name
);
4019 if (alist
== NULL
|| *alist
== NULL
)
4020 bpf_error("unknown host '%s'", name
);
4022 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4024 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4026 tmp
= gen_host(**alist
++, 0xffffffff,
4033 memset(&mask128
, 0xff, sizeof(mask128
));
4034 res0
= res
= pcap_nametoaddrinfo(name
);
4036 bpf_error("unknown host '%s'", name
);
4038 tproto
= tproto6
= proto
;
4039 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4043 for (res
= res0
; res
; res
= res
->ai_next
) {
4044 switch (res
->ai_family
) {
4046 if (tproto
== Q_IPV6
)
4049 sin
= (struct sockaddr_in
*)
4051 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4052 0xffffffff, tproto
, dir
);
4055 if (tproto6
== Q_IP
)
4058 sin6
= (struct sockaddr_in6
*)
4060 tmp
= gen_host6(&sin6
->sin6_addr
,
4061 &mask128
, tproto6
, dir
);
4072 bpf_error("unknown host '%s'%s", name
,
4073 (proto
== Q_DEFAULT
)
4075 : " for specified address family");
4082 if (proto
!= Q_DEFAULT
&&
4083 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4084 bpf_error("illegal qualifier of 'port'");
4085 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4086 bpf_error("unknown port '%s'", name
);
4087 if (proto
== Q_UDP
) {
4088 if (real_proto
== IPPROTO_TCP
)
4089 bpf_error("port '%s' is tcp", name
);
4090 else if (real_proto
== IPPROTO_SCTP
)
4091 bpf_error("port '%s' is sctp", name
);
4093 /* override PROTO_UNDEF */
4094 real_proto
= IPPROTO_UDP
;
4096 if (proto
== Q_TCP
) {
4097 if (real_proto
== IPPROTO_UDP
)
4098 bpf_error("port '%s' is udp", name
);
4100 else if (real_proto
== IPPROTO_SCTP
)
4101 bpf_error("port '%s' is sctp", name
);
4103 /* override PROTO_UNDEF */
4104 real_proto
= IPPROTO_TCP
;
4106 if (proto
== Q_SCTP
) {
4107 if (real_proto
== IPPROTO_UDP
)
4108 bpf_error("port '%s' is udp", name
);
4110 else if (real_proto
== IPPROTO_TCP
)
4111 bpf_error("port '%s' is tcp", name
);
4113 /* override PROTO_UNDEF */
4114 real_proto
= IPPROTO_SCTP
;
4117 return gen_port(port
, real_proto
, dir
);
4121 b
= gen_port(port
, real_proto
, dir
);
4122 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4129 eaddr
= pcap_ether_hostton(name
);
4131 bpf_error("unknown ether host: %s", name
);
4133 alist
= pcap_nametoaddr(name
);
4134 if (alist
== NULL
|| *alist
== NULL
)
4135 bpf_error("unknown host '%s'", name
);
4136 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4140 bpf_error("'gateway' not supported in this configuration");
4144 real_proto
= lookup_proto(name
, proto
);
4145 if (real_proto
>= 0)
4146 return gen_proto(real_proto
, proto
, dir
);
4148 bpf_error("unknown protocol: %s", name
);
4151 real_proto
= lookup_proto(name
, proto
);
4152 if (real_proto
>= 0)
4153 return gen_protochain(real_proto
, proto
, dir
);
4155 bpf_error("unknown protocol: %s", name
);
4167 gen_mcode(s1
, s2
, masklen
, q
)
4168 register const char *s1
, *s2
;
4169 register int masklen
;
4172 register int nlen
, mlen
;
4175 nlen
= __pcap_atoin(s1
, &n
);
4176 /* Promote short ipaddr */
4180 mlen
= __pcap_atoin(s2
, &m
);
4181 /* Promote short ipaddr */
4184 bpf_error("non-network bits set in \"%s mask %s\"",
4187 /* Convert mask len to mask */
4189 bpf_error("mask length must be <= 32");
4190 m
= 0xffffffff << (32 - masklen
);
4192 bpf_error("non-network bits set in \"%s/%d\"",
4199 return gen_host(n
, m
, q
.proto
, q
.dir
);
4202 bpf_error("Mask syntax for networks only");
4210 register const char *s
;
4215 int proto
= q
.proto
;
4221 else if (q
.proto
== Q_DECNET
)
4222 vlen
= __pcap_atodn(s
, &v
);
4224 vlen
= __pcap_atoin(s
, &v
);
4231 if (proto
== Q_DECNET
)
4232 return gen_host(v
, 0, proto
, dir
);
4233 else if (proto
== Q_LINK
) {
4234 bpf_error("illegal link layer address");
4237 if (s
== NULL
&& q
.addr
== Q_NET
) {
4238 /* Promote short net number */
4239 while (v
&& (v
& 0xff000000) == 0) {
4244 /* Promote short ipaddr */
4248 return gen_host(v
, mask
, proto
, dir
);
4253 proto
= IPPROTO_UDP
;
4254 else if (proto
== Q_TCP
)
4255 proto
= IPPROTO_TCP
;
4256 else if (proto
== Q_SCTP
)
4257 proto
= IPPROTO_SCTP
;
4258 else if (proto
== Q_DEFAULT
)
4259 proto
= PROTO_UNDEF
;
4261 bpf_error("illegal qualifier of 'port'");
4264 return gen_port((int)v
, proto
, dir
);
4268 b
= gen_port((int)v
, proto
, dir
);
4269 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4275 bpf_error("'gateway' requires a name");
4279 return gen_proto((int)v
, proto
, dir
);
4282 return gen_protochain((int)v
, proto
, dir
);
4297 gen_mcode6(s1
, s2
, masklen
, q
)
4298 register const char *s1
, *s2
;
4299 register int masklen
;
4302 struct addrinfo
*res
;
4303 struct in6_addr
*addr
;
4304 struct in6_addr mask
;
4309 bpf_error("no mask %s supported", s2
);
4311 res
= pcap_nametoaddrinfo(s1
);
4313 bpf_error("invalid ip6 address %s", s1
);
4315 bpf_error("%s resolved to multiple address", s1
);
4316 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4318 if (sizeof(mask
) * 8 < masklen
)
4319 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4320 memset(&mask
, 0, sizeof(mask
));
4321 memset(&mask
, 0xff, masklen
/ 8);
4323 mask
.s6_addr
[masklen
/ 8] =
4324 (0xff << (8 - masklen
% 8)) & 0xff;
4327 a
= (u_int32_t
*)addr
;
4328 m
= (u_int32_t
*)&mask
;
4329 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4330 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4331 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4339 bpf_error("Mask syntax for networks only");
4343 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4348 bpf_error("invalid qualifier against IPv6 address");
4356 register const u_char
*eaddr
;
4359 struct block
*b
, *tmp
;
4361 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4362 if (linktype
== DLT_EN10MB
)
4363 return gen_ehostop(eaddr
, (int)q
.dir
);
4364 if (linktype
== DLT_FDDI
)
4365 return gen_fhostop(eaddr
, (int)q
.dir
);
4366 if (linktype
== DLT_IEEE802
)
4367 return gen_thostop(eaddr
, (int)q
.dir
);
4368 if (linktype
== DLT_IEEE802_11
)
4369 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4370 if (linktype
== DLT_SUNATM
&& is_lane
) {
4372 * Check that the packet doesn't begin with an
4373 * LE Control marker. (We've already generated
4376 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4380 * Now check the MAC address.
4382 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4386 if (linktype
== DLT_IP_OVER_FC
)
4387 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4388 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4390 bpf_error("ethernet address used in non-ether expression");
4396 struct slist
*s0
, *s1
;
4399 * This is definitely not the best way to do this, but the
4400 * lists will rarely get long.
4407 static struct slist
*
4413 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4418 static struct slist
*
4424 s
= new_stmt(BPF_LD
|BPF_MEM
);
4430 gen_load(proto
, index
, size
)
4435 struct slist
*s
, *tmp
;
4437 int regno
= alloc_reg();
4439 free_reg(index
->regno
);
4443 bpf_error("data size must be 1, 2, or 4");
4459 bpf_error("unsupported index operation");
4463 * XXX - what about ATM LANE? Should the index be
4464 * relative to the beginning of the AAL5 frame, so
4465 * that 0 refers to the beginning of the LE Control
4466 * field, or relative to the beginning of the LAN
4467 * frame, so that 0 refers, for Ethernet LANE, to
4468 * the beginning of the destination address?
4470 s
= xfer_to_x(index
);
4471 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4473 sappend(index
->s
, s
);
4488 /* XXX Note that we assume a fixed link header here. */
4489 s
= xfer_to_x(index
);
4490 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4493 sappend(index
->s
, s
);
4495 b
= gen_proto_abbrev(proto
);
4497 gen_and(index
->b
, b
);
4509 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4511 sappend(s
, xfer_to_a(index
));
4512 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4513 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4514 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4516 sappend(index
->s
, s
);
4518 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4520 gen_and(index
->b
, b
);
4522 gen_and(gen_proto_abbrev(Q_IP
), b
);
4528 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4532 index
->regno
= regno
;
4533 s
= new_stmt(BPF_ST
);
4535 sappend(index
->s
, s
);
4541 gen_relation(code
, a0
, a1
, reversed
)
4543 struct arth
*a0
, *a1
;
4546 struct slist
*s0
, *s1
, *s2
;
4547 struct block
*b
, *tmp
;
4551 if (code
== BPF_JEQ
) {
4552 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4553 b
= new_block(JMP(code
));
4557 b
= new_block(BPF_JMP
|code
|BPF_X
);
4563 sappend(a0
->s
, a1
->s
);
4567 free_reg(a0
->regno
);
4568 free_reg(a1
->regno
);
4570 /* 'and' together protocol checks */
4573 gen_and(a0
->b
, tmp
= a1
->b
);
4589 int regno
= alloc_reg();
4590 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4593 s
= new_stmt(BPF_LD
|BPF_LEN
);
4594 s
->next
= new_stmt(BPF_ST
);
4595 s
->next
->s
.k
= regno
;
4610 a
= (struct arth
*)newchunk(sizeof(*a
));
4614 s
= new_stmt(BPF_LD
|BPF_IMM
);
4616 s
->next
= new_stmt(BPF_ST
);
4632 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4635 s
= new_stmt(BPF_ST
);
4643 gen_arth(code
, a0
, a1
)
4645 struct arth
*a0
, *a1
;
4647 struct slist
*s0
, *s1
, *s2
;
4651 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4656 sappend(a0
->s
, a1
->s
);
4658 free_reg(a0
->regno
);
4659 free_reg(a1
->regno
);
4661 s0
= new_stmt(BPF_ST
);
4662 a0
->regno
= s0
->s
.k
= alloc_reg();
4669 * Here we handle simple allocation of the scratch registers.
4670 * If too many registers are alloc'd, the allocator punts.
4672 static int regused
[BPF_MEMWORDS
];
4676 * Return the next free register.
4681 int n
= BPF_MEMWORDS
;
4684 if (regused
[curreg
])
4685 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4687 regused
[curreg
] = 1;
4691 bpf_error("too many registers needed to evaluate expression");
4696 * Return a register to the table so it can
4706 static struct block
*
4713 s
= new_stmt(BPF_LD
|BPF_LEN
);
4714 b
= new_block(JMP(jmp
));
4725 return gen_len(BPF_JGE
, n
);
4729 * Actually, this is less than or equal.
4737 b
= gen_len(BPF_JGT
, n
);
4744 gen_byteop(op
, idx
, val
)
4755 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4758 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4759 b
->s
.code
= JMP(BPF_JGE
);
4764 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4765 b
->s
.code
= JMP(BPF_JGT
);
4769 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4773 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4777 b
= new_block(JMP(BPF_JEQ
));
4784 static u_char abroadcast
[] = { 0x0 };
4787 gen_broadcast(proto
)
4790 bpf_u_int32 hostmask
;
4791 struct block
*b0
, *b1
, *b2
;
4792 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4798 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4799 return gen_ahostop(abroadcast
, Q_DST
);
4800 if (linktype
== DLT_EN10MB
)
4801 return gen_ehostop(ebroadcast
, Q_DST
);
4802 if (linktype
== DLT_FDDI
)
4803 return gen_fhostop(ebroadcast
, Q_DST
);
4804 if (linktype
== DLT_IEEE802
)
4805 return gen_thostop(ebroadcast
, Q_DST
);
4806 if (linktype
== DLT_IEEE802_11
)
4807 return gen_wlanhostop(ebroadcast
, Q_DST
);
4808 if (linktype
== DLT_IP_OVER_FC
)
4809 return gen_ipfchostop(ebroadcast
, Q_DST
);
4810 if (linktype
== DLT_SUNATM
&& is_lane
) {
4812 * Check that the packet doesn't begin with an
4813 * LE Control marker. (We've already generated
4816 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4820 * Now check the MAC address.
4822 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4826 bpf_error("not a broadcast link");
4830 b0
= gen_linktype(ETHERTYPE_IP
);
4831 hostmask
= ~netmask
;
4832 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4833 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4834 (bpf_int32
)(~0 & hostmask
), hostmask
);
4839 bpf_error("only link-layer/IP broadcast filters supported");
4844 * Generate code to test the low-order bit of a MAC address (that's
4845 * the bottom bit of the *first* byte).
4847 static struct block
*
4848 gen_mac_multicast(offset
)
4851 register struct block
*b0
;
4852 register struct slist
*s
;
4854 /* link[offset] & 1 != 0 */
4855 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4857 b0
= new_block(JMP(BPF_JSET
));
4864 gen_multicast(proto
)
4867 register struct block
*b0
, *b1
, *b2
;
4868 register struct slist
*s
;
4874 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4875 /* all ARCnet multicasts use the same address */
4876 return gen_ahostop(abroadcast
, Q_DST
);
4878 if (linktype
== DLT_EN10MB
) {
4879 /* ether[0] & 1 != 0 */
4880 return gen_mac_multicast(0);
4883 if (linktype
== DLT_FDDI
) {
4885 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4887 * XXX - was that referring to bit-order issues?
4889 /* fddi[1] & 1 != 0 */
4890 return gen_mac_multicast(1);
4893 if (linktype
== DLT_IEEE802
) {
4894 /* tr[2] & 1 != 0 */
4895 return gen_mac_multicast(2);
4898 if (linktype
== DLT_IEEE802_11
) {
4902 * For control frames, there is no DA.
4904 * For management frames, DA is at an
4905 * offset of 4 from the beginning of
4908 * For data frames, DA is at an offset
4909 * of 4 from the beginning of the packet
4910 * if To DS is clear and at an offset of
4911 * 16 from the beginning of the packet
4916 * Generate the tests to be done for data frames.
4918 * First, check for To DS set, i.e. "link[1] & 0x01".
4920 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4922 b1
= new_block(JMP(BPF_JSET
));
4923 b1
->s
.k
= 0x01; /* To DS */
4927 * If To DS is set, the DA is at 16.
4929 b0
= gen_mac_multicast(16);
4933 * Now, check for To DS not set, i.e. check
4934 * "!(link[1] & 0x01)".
4936 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4938 b2
= new_block(JMP(BPF_JSET
));
4939 b2
->s
.k
= 0x01; /* To DS */
4944 * If To DS is not set, the DA is at 4.
4946 b1
= gen_mac_multicast(4);
4950 * Now OR together the last two checks. That gives
4951 * the complete set of checks for data frames.
4956 * Now check for a data frame.
4957 * I.e, check "link[0] & 0x08".
4959 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4961 b1
= new_block(JMP(BPF_JSET
));
4966 * AND that with the checks done for data frames.
4971 * If the high-order bit of the type value is 0, this
4972 * is a management frame.
4973 * I.e, check "!(link[0] & 0x08)".
4975 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4977 b2
= new_block(JMP(BPF_JSET
));
4983 * For management frames, the DA is at 4.
4985 b1
= gen_mac_multicast(4);
4989 * OR that with the checks done for data frames.
4990 * That gives the checks done for management and
4996 * If the low-order bit of the type value is 1,
4997 * this is either a control frame or a frame
4998 * with a reserved type, and thus not a
5001 * I.e., check "!(link[0] & 0x04)".
5003 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5005 b1
= new_block(JMP(BPF_JSET
));
5011 * AND that with the checks for data and management
5018 if (linktype
== DLT_IP_OVER_FC
) {
5019 b0
= gen_mac_multicast(2);
5023 if (linktype
== DLT_SUNATM
&& is_lane
) {
5025 * Check that the packet doesn't begin with an
5026 * LE Control marker. (We've already generated
5029 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5032 /* ether[off_mac] & 1 != 0 */
5033 b0
= gen_mac_multicast(off_mac
);
5038 /* Link not known to support multicasts */
5042 b0
= gen_linktype(ETHERTYPE_IP
);
5043 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5044 b1
->s
.code
= JMP(BPF_JGE
);
5050 b0
= gen_linktype(ETHERTYPE_IPV6
);
5051 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5056 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5061 * generate command for inbound/outbound. It's here so we can
5062 * make it link-type specific. 'dir' = 0 implies "inbound",
5063 * = 1 implies "outbound".
5069 register struct block
*b0
;
5072 * Only some data link types support inbound/outbound qualifiers.
5076 b0
= gen_relation(BPF_JEQ
,
5077 gen_load(Q_LINK
, gen_loadi(0), 1),
5085 * Match packets sent by this machine.
5087 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5090 * Match packets sent to this machine.
5091 * (No broadcast or multicast packets, or
5092 * packets sent to some other machine and
5093 * received promiscuously.)
5095 * XXX - packets sent to other machines probably
5096 * shouldn't be matched, but what about broadcast
5097 * or multicast packets we received?
5099 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5104 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5105 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5108 case DLT_PPP_WITHDIRECTION
:
5110 /* match outgoing packets */
5111 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_OUT
);
5113 /* match incoming packets */
5114 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_IN
);
5118 case DLT_JUNIPER_ATM1
:
5119 case DLT_JUNIPER_ATM2
:
5120 /* juniper flags (including direction) are stored
5121 * the byte after the 3-byte magic number */
5123 /* match outgoing packets */
5124 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5126 /* match incoming packets */
5127 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5132 bpf_error("inbound/outbound not supported on linktype %d",
5140 /* PF firewall log matched interface */
5142 gen_pf_ifname(const char *ifname
)
5147 if (linktype
== DLT_PFLOG
) {
5148 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5149 off
= offsetof(struct pfloghdr
, ifname
);
5151 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5154 if (strlen(ifname
) >= len
) {
5155 bpf_error("ifname interface names can only be %d characters",
5159 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5163 /* PF firewall log matched interface */
5165 gen_pf_ruleset(char *ruleset
)
5169 if (linktype
!= DLT_PFLOG
) {
5170 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5173 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5174 bpf_error("ruleset names can only be %ld characters",
5175 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5178 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5179 strlen(ruleset
), (const u_char
*)ruleset
);
5183 /* PF firewall log rule number */
5189 if (linktype
== DLT_PFLOG
) {
5190 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5193 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5200 /* PF firewall log sub-rule number */
5202 gen_pf_srnr(int srnr
)
5206 if (linktype
!= DLT_PFLOG
) {
5207 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5211 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5216 /* PF firewall log reason code */
5218 gen_pf_reason(int reason
)
5222 if (linktype
== DLT_PFLOG
) {
5223 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5226 bpf_error("reason not supported on linktype 0x%x", linktype
);
5233 /* PF firewall log action */
5235 gen_pf_action(int action
)
5239 if (linktype
== DLT_PFLOG
) {
5240 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5243 bpf_error("action not supported on linktype 0x%x", linktype
);
5252 register const u_char
*eaddr
;
5255 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5256 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5257 return gen_ahostop(eaddr
, (int)q
.dir
);
5259 bpf_error("ARCnet address used in non-arc expression");
5263 static struct block
*
5264 gen_ahostop(eaddr
, dir
)
5265 register const u_char
*eaddr
;
5268 register struct block
*b0
, *b1
;
5271 /* src comes first, different from Ethernet */
5273 return gen_bcmp(0, 1, eaddr
);
5276 return gen_bcmp(1, 1, eaddr
);
5279 b0
= gen_ahostop(eaddr
, Q_SRC
);
5280 b1
= gen_ahostop(eaddr
, Q_DST
);
5286 b0
= gen_ahostop(eaddr
, Q_SRC
);
5287 b1
= gen_ahostop(eaddr
, Q_DST
);
5296 * support IEEE 802.1Q VLAN trunk over ethernet
5305 * Change the offsets to point to the type and data fields within
5306 * the VLAN packet. This is somewhat of a kludge.
5308 if (orig_nl
== (u_int
)-1) {
5309 orig_linktype
= off_linktype
; /* save original values */
5311 orig_nl_nosnap
= off_nl_nosnap
;
5322 bpf_error("no VLAN support for data link type %d",
5328 /* check for VLAN */
5329 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5331 /* If a specific VLAN is requested, check VLAN id */
5332 if (vlan_num
>= 0) {
5335 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5353 * Change the offsets to point to the type and data fields within
5354 * the MPLS packet. This is somewhat of a kludge.
5356 if (orig_nl
== (u_int
)-1) {
5357 orig_linktype
= off_linktype
; /* save original values */
5359 orig_nl_nosnap
= off_nl_nosnap
;
5368 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5376 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5384 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5387 /* FIXME add other DLT_s ...
5388 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5389 * leave it for now */
5392 bpf_error("no MPLS support for data link type %d",
5397 bpf_error("'mpls' can't be combined with 'vlan' or another 'mpls'");
5401 /* If a specific MPLS label is requested, check it */
5402 if (label_num
>= 0) {
5405 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5406 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5415 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5427 bpf_error("'vpi' supported only on raw ATM");
5428 if (off_vpi
== (u_int
)-1)
5430 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5431 (u_int
)jvalue
, reverse
);
5436 bpf_error("'vci' supported only on raw ATM");
5437 if (off_vci
== (u_int
)-1)
5439 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5440 (u_int
)jvalue
, reverse
);
5444 if (off_proto
== (u_int
)-1)
5445 abort(); /* XXX - this isn't on FreeBSD */
5446 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5447 (u_int
)jvalue
, reverse
);
5451 if (off_payload
== (u_int
)-1)
5453 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5454 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5459 bpf_error("'callref' supported only on raw ATM");
5460 if (off_proto
== (u_int
)-1)
5462 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5463 (u_int
)jvalue
, reverse
);
5473 gen_atmtype_abbrev(type
)
5476 struct block
*b0
, *b1
;
5481 /* Get all packets in Meta signalling Circuit */
5483 bpf_error("'metac' supported only on raw ATM");
5484 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5485 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5490 /* Get all packets in Broadcast Circuit*/
5492 bpf_error("'bcc' supported only on raw ATM");
5493 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5494 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5499 /* Get all cells in Segment OAM F4 circuit*/
5501 bpf_error("'oam4sc' supported only on raw ATM");
5502 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5503 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5508 /* Get all cells in End-to-End OAM F4 Circuit*/
5510 bpf_error("'oam4ec' supported only on raw ATM");
5511 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5512 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5517 /* Get all packets in connection Signalling Circuit */
5519 bpf_error("'sc' supported only on raw ATM");
5520 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5521 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5526 /* Get all packets in ILMI Circuit */
5528 bpf_error("'ilmic' supported only on raw ATM");
5529 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5530 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5535 /* Get all LANE packets */
5537 bpf_error("'lane' supported only on raw ATM");
5538 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5541 * Arrange that all subsequent tests assume LANE
5542 * rather than LLC-encapsulated packets, and set
5543 * the offsets appropriately for LANE-encapsulated
5546 * "off_mac" is the offset of the Ethernet header,
5547 * which is 2 bytes past the ATM pseudo-header
5548 * (skipping the pseudo-header and 2-byte LE Client
5549 * field). The other offsets are Ethernet offsets
5550 * relative to "off_mac".
5553 off_mac
= off_payload
+ 2; /* MAC header */
5554 off_linktype
= off_mac
+ 12;
5555 off_nl
= off_mac
+ 14; /* Ethernet II */
5556 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5560 /* Get all LLC-encapsulated packets */
5562 bpf_error("'llc' supported only on raw ATM");
5563 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5574 static struct block
*
5575 gen_msg_abbrev(type
)
5581 * Q.2931 signalling protocol messages for handling virtual circuits
5582 * establishment and teardown
5587 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5591 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5595 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5599 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5603 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5606 case A_RELEASE_DONE
:
5607 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5617 gen_atmmulti_abbrev(type
)
5620 struct block
*b0
, *b1
;
5626 bpf_error("'oam' supported only on raw ATM");
5627 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5632 bpf_error("'oamf4' supported only on raw ATM");
5634 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5635 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5637 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5643 * Get Q.2931 signalling messages for switched
5644 * virtual connection
5647 bpf_error("'connectmsg' supported only on raw ATM");
5648 b0
= gen_msg_abbrev(A_SETUP
);
5649 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5651 b0
= gen_msg_abbrev(A_CONNECT
);
5653 b0
= gen_msg_abbrev(A_CONNECTACK
);
5655 b0
= gen_msg_abbrev(A_RELEASE
);
5657 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5659 b0
= gen_atmtype_abbrev(A_SC
);
5665 bpf_error("'metaconnect' supported only on raw ATM");
5666 b0
= gen_msg_abbrev(A_SETUP
);
5667 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5669 b0
= gen_msg_abbrev(A_CONNECT
);
5671 b0
= gen_msg_abbrev(A_RELEASE
);
5673 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5675 b0
= gen_atmtype_abbrev(A_METAC
);