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.211 2004-11-06 22:45:17 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 int pcap_fddipad
= PCAP_FDDIPAD
;
112 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(int);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * We divy out chunks of memory rather than call malloc each time so
135 * we don't have to worry about leaking memory. It's probably
136 * not a big deal if all this memory was wasted but it this ever
137 * goes into a library that would probably not be a good idea.
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
: "");
344 init_linktype(pcap_datalink(p
));
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
;
688 * Assume it's not raw ATM with a pseudo-header, for now.
706 off_nl
= 6; /* XXX in reality, variable! */
707 off_nl_nosnap
= 6; /* no 802.2 LLC */
710 case DLT_ARCNET_LINUX
:
712 off_nl
= 8; /* XXX in reality, variable! */
713 off_nl_nosnap
= 8; /* no 802.2 LLC */
718 off_nl
= 14; /* Ethernet II */
719 off_nl_nosnap
= 17; /* 802.3+802.2 */
724 * SLIP doesn't have a link level type. The 16 byte
725 * header is hacked into our SLIP driver.
729 off_nl_nosnap
= 16; /* no 802.2 LLC */
733 /* XXX this may be the same as the DLT_PPP_BSDOS case */
737 off_nl_nosnap
= 24; /* no 802.2 LLC */
744 off_nl_nosnap
= 4; /* no 802.2 LLC */
750 off_nl_nosnap
= 12; /* no 802.2 LLC */
754 case DLT_PPP_WITHDIRECTION
:
755 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
756 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
759 off_nl_nosnap
= 4; /* no 802.2 LLC */
764 * This does no include the Ethernet header, and
765 * only covers session state.
769 off_nl_nosnap
= 8; /* no 802.2 LLC */
775 off_nl_nosnap
= 24; /* no 802.2 LLC */
780 * FDDI doesn't really have a link-level type field.
781 * We set "off_linktype" to the offset of the LLC header.
783 * To check for Ethernet types, we assume that SSAP = SNAP
784 * is being used and pick out the encapsulated Ethernet type.
785 * XXX - should we generate code to check for SNAP?
789 off_linktype
+= pcap_fddipad
;
791 off_nl
= 21; /* FDDI+802.2+SNAP */
792 off_nl_nosnap
= 16; /* FDDI+802.2 */
794 off_nl
+= pcap_fddipad
;
795 off_nl_nosnap
+= pcap_fddipad
;
801 * Token Ring doesn't really have a link-level type field.
802 * We set "off_linktype" to the offset of the LLC header.
804 * To check for Ethernet types, we assume that SSAP = SNAP
805 * is being used and pick out the encapsulated Ethernet type.
806 * XXX - should we generate code to check for SNAP?
808 * XXX - the header is actually variable-length.
809 * Some various Linux patched versions gave 38
810 * as "off_linktype" and 40 as "off_nl"; however,
811 * if a token ring packet has *no* routing
812 * information, i.e. is not source-routed, the correct
813 * values are 20 and 22, as they are in the vanilla code.
815 * A packet is source-routed iff the uppermost bit
816 * of the first byte of the source address, at an
817 * offset of 8, has the uppermost bit set. If the
818 * packet is source-routed, the total number of bytes
819 * of routing information is 2 plus bits 0x1F00 of
820 * the 16-bit value at an offset of 14 (shifted right
821 * 8 - figure out which byte that is).
824 off_nl
= 22; /* Token Ring+802.2+SNAP */
825 off_nl_nosnap
= 17; /* Token Ring+802.2 */
830 * 802.11 doesn't really have a link-level type field.
831 * We set "off_linktype" to the offset of the LLC header.
833 * To check for Ethernet types, we assume that SSAP = SNAP
834 * is being used and pick out the encapsulated Ethernet type.
835 * XXX - should we generate code to check for SNAP?
837 * XXX - the header is actually variable-length. We
838 * assume a 24-byte link-layer header, as appears in
839 * data frames in networks with no bridges. If the
840 * fromds and tods 802.11 header bits are both set,
841 * it's actually supposed to be 30 bytes.
844 off_nl
= 32; /* 802.11+802.2+SNAP */
845 off_nl_nosnap
= 27; /* 802.11+802.2 */
848 case DLT_PRISM_HEADER
:
850 * Same as 802.11, but with an additional header before
851 * the 802.11 header, containing a bunch of additional
852 * information including radio-level information.
854 * The header is 144 bytes long.
856 * XXX - same variable-length header problem; at least
857 * the Prism header is fixed-length.
859 off_linktype
= 144+24;
860 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
861 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
864 case DLT_IEEE802_11_RADIO_AVS
:
866 * Same as 802.11, but with an additional header before
867 * the 802.11 header, containing a bunch of additional
868 * information including radio-level information.
870 * The header is 64 bytes long, at least in its
871 * current incarnation.
873 * XXX - same variable-length header problem, only
874 * more so; this header is also variable-length,
875 * with the length being the 32-bit big-endian
876 * number at an offset of 4 from the beginning
877 * of the radio header.
879 off_linktype
= 64+24;
880 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
881 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
884 case DLT_IEEE802_11_RADIO
:
886 * Same as 802.11, but with an additional header before
887 * the 802.11 header, containing a bunch of additional
888 * information including radio-level information.
890 * XXX - same variable-length header problem, only
891 * even *more* so; this header is also variable-length,
892 * with the length being the 16-bit number at an offset
893 * of 2 from the beginning of the radio header, and it's
894 * device-dependent (different devices might supply
895 * different amounts of information), so we can't even
896 * assume a fixed length for the current version of the
899 * Therefore, currently, only raw "link[N:M]" filtering is
907 case DLT_ATM_RFC1483
:
908 case DLT_ATM_CLIP
: /* Linux ATM defines this */
910 * assume routed, non-ISO PDUs
911 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
914 off_nl
= 8; /* 802.2+SNAP */
915 off_nl_nosnap
= 3; /* 802.2 */
920 * Full Frontal ATM; you get AALn PDUs with an ATM
924 off_vpi
= SUNATM_VPI_POS
;
925 off_vci
= SUNATM_VCI_POS
;
926 off_proto
= PROTO_POS
;
927 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
928 off_payload
= SUNATM_PKT_BEGIN_POS
;
929 off_linktype
= off_payload
;
930 off_nl
= off_payload
+8; /* 802.2+SNAP */
931 off_nl_nosnap
= off_payload
+3; /* 802.2 */
937 off_nl_nosnap
= 0; /* no 802.2 LLC */
940 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
943 off_nl_nosnap
= 16; /* no 802.2 LLC */
948 * LocalTalk does have a 1-byte type field in the LLAP header,
949 * but really it just indicates whether there is a "short" or
950 * "long" DDP packet following.
954 off_nl_nosnap
= 0; /* no 802.2 LLC */
959 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
960 * link-level type field. We set "off_linktype" to the
961 * offset of the LLC header.
963 * To check for Ethernet types, we assume that SSAP = SNAP
964 * is being used and pick out the encapsulated Ethernet type.
965 * XXX - should we generate code to check for SNAP? RFC
966 * 2625 says SNAP should be used.
969 off_nl
= 24; /* IPFC+802.2+SNAP */
970 off_nl_nosnap
= 19; /* IPFC+802.2 */
975 * XXX - we should set this to handle SNAP-encapsulated
976 * frames (NLPID of 0x80).
980 off_nl_nosnap
= 0; /* no 802.2 LLC */
983 case DLT_APPLE_IP_OVER_IEEE1394
:
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
991 * Currently, only raw "link[N:M]" filtering is supported.
1000 * Currently, only raw "link[N:M]" filtering is supported.
1007 case DLT_SYMANTEC_FIREWALL
:
1009 off_nl
= 44; /* Ethernet II */
1010 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1015 /* XXX read from header? */
1016 off_nl
= PFLOG_HDRLEN
;
1017 off_nl_nosnap
= PFLOG_HDRLEN
;
1020 case DLT_JUNIPER_ATM1
:
1021 off_linktype
= 4; /* in reality variable between 4-8 */
1026 case DLT_JUNIPER_ATM2
:
1027 off_linktype
= 8; /* in reality variable between 8-12 */
1040 bpf_error("unknown data link type %d", linktype
);
1044 static struct block
*
1051 s
= new_stmt(BPF_LD
|BPF_IMM
);
1053 b
= new_block(JMP(BPF_JEQ
));
1059 static inline struct block
*
1062 return gen_uncond(1);
1065 static inline struct block
*
1068 return gen_uncond(0);
1072 * Byte-swap a 32-bit number.
1073 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1074 * big-endian platforms.)
1076 #define SWAPLONG(y) \
1077 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1079 static struct block
*
1080 gen_ether_linktype(proto
)
1083 struct block
*b0
, *b1
;
1089 * OSI protocols always use 802.2 encapsulation.
1090 * XXX - should we check both the DSAP and the
1091 * SSAP, like this, or should we check just the
1094 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1096 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1097 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1102 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1104 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1105 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1109 case LLCSAP_NETBEUI
:
1111 * NetBEUI always uses 802.2 encapsulation.
1112 * XXX - should we check both the DSAP and the
1113 * SSAP, like this, or should we check just the
1116 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1118 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1119 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1127 * Ethernet_II frames, which are Ethernet
1128 * frames with a frame type of ETHERTYPE_IPX;
1130 * Ethernet_802.3 frames, which are 802.3
1131 * frames (i.e., the type/length field is
1132 * a length field, <= ETHERMTU, rather than
1133 * a type field) with the first two bytes
1134 * after the Ethernet/802.3 header being
1137 * Ethernet_802.2 frames, which are 802.3
1138 * frames with an 802.2 LLC header and
1139 * with the IPX LSAP as the DSAP in the LLC
1142 * Ethernet_SNAP frames, which are 802.3
1143 * frames with an LLC header and a SNAP
1144 * header and with an OUI of 0x000000
1145 * (encapsulated Ethernet) and a protocol
1146 * ID of ETHERTYPE_IPX in the SNAP header.
1148 * XXX - should we generate the same code both
1149 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1153 * This generates code to check both for the
1154 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1156 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1157 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1161 * Now we add code to check for SNAP frames with
1162 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1164 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1168 * Now we generate code to check for 802.3
1169 * frames in general.
1171 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1175 * Now add the check for 802.3 frames before the
1176 * check for Ethernet_802.2 and Ethernet_802.3,
1177 * as those checks should only be done on 802.3
1178 * frames, not on Ethernet frames.
1183 * Now add the check for Ethernet_II frames, and
1184 * do that before checking for the other frame
1187 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1191 case ETHERTYPE_ATALK
:
1192 case ETHERTYPE_AARP
:
1194 * EtherTalk (AppleTalk protocols on Ethernet link
1195 * layer) may use 802.2 encapsulation.
1199 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1200 * we check for an Ethernet type field less than
1201 * 1500, which means it's an 802.3 length field.
1203 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1207 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1208 * SNAP packets with an organization code of
1209 * 0x080007 (Apple, for Appletalk) and a protocol
1210 * type of ETHERTYPE_ATALK (Appletalk).
1212 * 802.2-encapsulated ETHERTYPE_AARP packets are
1213 * SNAP packets with an organization code of
1214 * 0x000000 (encapsulated Ethernet) and a protocol
1215 * type of ETHERTYPE_AARP (Appletalk ARP).
1217 if (proto
== ETHERTYPE_ATALK
)
1218 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1219 else /* proto == ETHERTYPE_AARP */
1220 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1224 * Check for Ethernet encapsulation (Ethertalk
1225 * phase 1?); we just check for the Ethernet
1228 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1234 if (proto
<= ETHERMTU
) {
1236 * This is an LLC SAP value, so the frames
1237 * that match would be 802.2 frames.
1238 * Check that the frame is an 802.2 frame
1239 * (i.e., that the length/type field is
1240 * a length field, <= ETHERMTU) and
1241 * then check the DSAP.
1243 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1245 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1250 * This is an Ethernet type, so compare
1251 * the length/type field with it (if
1252 * the frame is an 802.2 frame, the length
1253 * field will be <= ETHERMTU, and, as
1254 * "proto" is > ETHERMTU, this test
1255 * will fail and the frame won't match,
1256 * which is what we want).
1258 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1263 static struct block
*
1264 gen_linux_sll_linktype(proto
)
1267 struct block
*b0
, *b1
;
1272 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1273 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1274 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1280 * OSI protocols always use 802.2 encapsulation.
1281 * XXX - should we check both the DSAP and the
1282 * LSAP, like this, or should we check just the
1285 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1286 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1287 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1291 case LLCSAP_NETBEUI
:
1293 * NetBEUI always uses 802.2 encapsulation.
1294 * XXX - should we check both the DSAP and the
1295 * LSAP, like this, or should we check just the
1298 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1299 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1300 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1306 * Ethernet_II frames, which are Ethernet
1307 * frames with a frame type of ETHERTYPE_IPX;
1309 * Ethernet_802.3 frames, which have a frame
1310 * type of LINUX_SLL_P_802_3;
1312 * Ethernet_802.2 frames, which are 802.3
1313 * frames with an 802.2 LLC header (i.e, have
1314 * a frame type of LINUX_SLL_P_802_2) and
1315 * with the IPX LSAP as the DSAP in the LLC
1318 * Ethernet_SNAP frames, which are 802.3
1319 * frames with an LLC header and a SNAP
1320 * header and with an OUI of 0x000000
1321 * (encapsulated Ethernet) and a protocol
1322 * ID of ETHERTYPE_IPX in the SNAP header.
1324 * First, do the checks on LINUX_SLL_P_802_2
1325 * frames; generate the check for either
1326 * Ethernet_802.2 or Ethernet_SNAP frames, and
1327 * then put a check for LINUX_SLL_P_802_2 frames
1330 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1331 (bpf_int32
)LLCSAP_IPX
);
1332 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1335 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1339 * Now check for 802.3 frames and OR that with
1340 * the previous test.
1342 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1346 * Now add the check for Ethernet_II frames, and
1347 * do that before checking for the other frame
1350 b0
= gen_cmp(off_linktype
, BPF_H
,
1351 (bpf_int32
)ETHERTYPE_IPX
);
1355 case ETHERTYPE_ATALK
:
1356 case ETHERTYPE_AARP
:
1358 * EtherTalk (AppleTalk protocols on Ethernet link
1359 * layer) may use 802.2 encapsulation.
1363 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1364 * we check for the 802.2 protocol type in the
1365 * "Ethernet type" field.
1367 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1370 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1371 * SNAP packets with an organization code of
1372 * 0x080007 (Apple, for Appletalk) and a protocol
1373 * type of ETHERTYPE_ATALK (Appletalk).
1375 * 802.2-encapsulated ETHERTYPE_AARP packets are
1376 * SNAP packets with an organization code of
1377 * 0x000000 (encapsulated Ethernet) and a protocol
1378 * type of ETHERTYPE_AARP (Appletalk ARP).
1380 if (proto
== ETHERTYPE_ATALK
)
1381 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1383 else /* proto == ETHERTYPE_AARP */
1384 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1389 * Check for Ethernet encapsulation (Ethertalk
1390 * phase 1?); we just check for the Ethernet
1393 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1399 if (proto
<= ETHERMTU
) {
1401 * This is an LLC SAP value, so the frames
1402 * that match would be 802.2 frames.
1403 * Check for the 802.2 protocol type
1404 * in the "Ethernet type" field, and
1405 * then check the DSAP.
1407 b0
= gen_cmp(off_linktype
, BPF_H
,
1409 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1415 * This is an Ethernet type, so compare
1416 * the length/type field with it (if
1417 * the frame is an 802.2 frame, the length
1418 * field will be <= ETHERMTU, and, as
1419 * "proto" is > ETHERMTU, this test
1420 * will fail and the frame won't match,
1421 * which is what we want).
1423 return gen_cmp(off_linktype
, BPF_H
,
1429 static struct block
*
1433 struct block
*b0
, *b1
, *b2
;
1438 return gen_ether_linktype(proto
);
1446 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1450 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1456 case DLT_IEEE802_11
:
1457 case DLT_PRISM_HEADER
:
1458 case DLT_IEEE802_11_RADIO
:
1461 case DLT_ATM_RFC1483
:
1463 case DLT_IP_OVER_FC
:
1464 return gen_llc(proto
);
1470 * If "is_lane" is set, check for a LANE-encapsulated
1471 * version of this protocol, otherwise check for an
1472 * LLC-encapsulated version of this protocol.
1474 * We assume LANE means Ethernet, not Token Ring.
1478 * Check that the packet doesn't begin with an
1479 * LE Control marker. (We've already generated
1482 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1486 * Now generate an Ethernet test.
1488 b1
= gen_ether_linktype(proto
);
1493 * Check for LLC encapsulation and then check the
1496 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1497 b1
= gen_llc(proto
);
1503 return gen_linux_sll_linktype(proto
);
1508 case DLT_SLIP_BSDOS
:
1511 * These types don't provide any type field; packets
1514 * XXX - for IPv4, check for a version number of 4, and,
1515 * for IPv6, check for a version number of 6?
1521 case ETHERTYPE_IPV6
:
1523 return gen_true(); /* always true */
1526 return gen_false(); /* always false */
1532 case DLT_PPP_WITHDIRECTION
:
1533 case DLT_PPP_SERIAL
:
1536 * We use Ethernet protocol types inside libpcap;
1537 * map them to the corresponding PPP protocol types.
1546 case ETHERTYPE_IPV6
:
1555 case ETHERTYPE_ATALK
:
1569 * I'm assuming the "Bridging PDU"s that go
1570 * over PPP are Spanning Tree Protocol
1584 * We use Ethernet protocol types inside libpcap;
1585 * map them to the corresponding PPP protocol types.
1590 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1591 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1593 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1598 case ETHERTYPE_IPV6
:
1608 case ETHERTYPE_ATALK
:
1622 * I'm assuming the "Bridging PDU"s that go
1623 * over PPP are Spanning Tree Protocol
1639 * For DLT_NULL, the link-layer header is a 32-bit
1640 * word containing an AF_ value in *host* byte order,
1641 * and for DLT_ENC, the link-layer header begins
1642 * with a 32-bit work containing an AF_ value in
1645 * In addition, if we're reading a saved capture file,
1646 * the host byte order in the capture may not be the
1647 * same as the host byte order on this machine.
1649 * For DLT_LOOP, the link-layer header is a 32-bit
1650 * word containing an AF_ value in *network* byte order.
1652 * XXX - AF_ values may, unfortunately, be platform-
1653 * dependent; for example, FreeBSD's AF_INET6 is 24
1654 * whilst NetBSD's and OpenBSD's is 26.
1656 * This means that, when reading a capture file, just
1657 * checking for our AF_INET6 value won't work if the
1658 * capture file came from another OS.
1667 case ETHERTYPE_IPV6
:
1674 * Not a type on which we support filtering.
1675 * XXX - support those that have AF_ values
1676 * #defined on this platform, at least?
1681 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1683 * The AF_ value is in host byte order, but
1684 * the BPF interpreter will convert it to
1685 * network byte order.
1687 * If this is a save file, and it's from a
1688 * machine with the opposite byte order to
1689 * ours, we byte-swap the AF_ value.
1691 * Then we run it through "htonl()", and
1692 * generate code to compare against the result.
1694 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1695 bpf_pcap
->sf
.swapped
)
1696 proto
= SWAPLONG(proto
);
1697 proto
= htonl(proto
);
1699 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1703 * af field is host byte order in contrast to the rest of
1706 if (proto
== ETHERTYPE_IP
)
1707 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1708 (bpf_int32
)AF_INET
));
1710 else if (proto
== ETHERTYPE_IPV6
)
1711 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1712 (bpf_int32
)AF_INET6
));
1720 case DLT_ARCNET_LINUX
:
1722 * XXX should we check for first fragment if the protocol
1731 case ETHERTYPE_IPV6
:
1732 return (gen_cmp(off_linktype
, BPF_B
,
1733 (bpf_int32
)ARCTYPE_INET6
));
1737 b0
= gen_cmp(off_linktype
, BPF_B
,
1738 (bpf_int32
)ARCTYPE_IP
);
1739 b1
= gen_cmp(off_linktype
, BPF_B
,
1740 (bpf_int32
)ARCTYPE_IP_OLD
);
1745 b0
= gen_cmp(off_linktype
, BPF_B
,
1746 (bpf_int32
)ARCTYPE_ARP
);
1747 b1
= gen_cmp(off_linktype
, BPF_B
,
1748 (bpf_int32
)ARCTYPE_ARP_OLD
);
1752 case ETHERTYPE_REVARP
:
1753 return (gen_cmp(off_linktype
, BPF_B
,
1754 (bpf_int32
)ARCTYPE_REVARP
));
1756 case ETHERTYPE_ATALK
:
1757 return (gen_cmp(off_linktype
, BPF_B
,
1758 (bpf_int32
)ARCTYPE_ATALK
));
1765 case ETHERTYPE_ATALK
:
1775 * XXX - assumes a 2-byte Frame Relay header with
1776 * DLCI and flags. What if the address is longer?
1782 * Check for the special NLPID for IP.
1784 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1787 case ETHERTYPE_IPV6
:
1789 * Check for the special NLPID for IPv6.
1791 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1796 * Check for several OSI protocols.
1798 * Frame Relay packets typically have an OSI
1799 * NLPID at the beginning; we check for each
1802 * What we check for is the NLPID and a frame
1803 * control field of UI, i.e. 0x03 followed
1806 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1807 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1808 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1819 case DLT_JUNIPER_ATM1
:
1820 case DLT_JUNIPER_ATM2
:
1821 /* just lets verify the magic number for now -
1822 * we may have upto 6 different encapsulations on the wire
1823 * and need a lot of heuristics to figure out that the payload
1826 * FIXME encapsulation specific BPF_ filters
1828 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1830 case DLT_LINUX_IRDA
:
1831 bpf_error("IrDA link-layer type filtering not implemented");
1834 bpf_error("DOCSIS link-layer type filtering not implemented");
1838 * All the types that have no encapsulation should either be
1839 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1840 * all packets are IP packets, or should be handled in some
1841 * special case, if none of them are (if some are and some
1842 * aren't, the lack of encapsulation is a problem, as we'd
1843 * have to find some other way of determining the packet type).
1845 * Therefore, if "off_linktype" is -1, there's an error.
1847 if (off_linktype
== (u_int
)-1)
1851 * Any type not handled above should always have an Ethernet
1852 * type at an offset of "off_linktype". (PPP is partially
1853 * handled above - the protocol type is mapped from the
1854 * Ethernet and LLC types we use internally to the corresponding
1855 * PPP type - but the PPP type is always specified by a value
1856 * at "off_linktype", so we don't have to do the code generation
1859 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1863 * Check for an LLC SNAP packet with a given organization code and
1864 * protocol type; we check the entire contents of the 802.2 LLC and
1865 * snap headers, checking for DSAP and SSAP of SNAP and a control
1866 * field of 0x03 in the LLC header, and for the specified organization
1867 * code and protocol type in the SNAP header.
1869 static struct block
*
1870 gen_snap(orgcode
, ptype
, offset
)
1871 bpf_u_int32 orgcode
;
1875 u_char snapblock
[8];
1877 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1878 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1879 snapblock
[2] = 0x03; /* control = UI */
1880 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1881 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1882 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1883 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1884 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1885 return gen_bcmp(offset
, 8, snapblock
);
1889 * Check for a given protocol value assuming an 802.2 LLC header.
1891 static struct block
*
1896 * XXX - handle token-ring variable-length header.
1901 return gen_cmp(off_linktype
, BPF_H
, (long)
1902 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1905 return gen_cmp(off_linktype
, BPF_H
, (long)
1906 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1908 case LLCSAP_NETBEUI
:
1909 return gen_cmp(off_linktype
, BPF_H
, (long)
1910 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1914 * XXX - are there ever SNAP frames for IPX on
1915 * non-Ethernet 802.x networks?
1917 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1919 case ETHERTYPE_ATALK
:
1921 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1922 * SNAP packets with an organization code of
1923 * 0x080007 (Apple, for Appletalk) and a protocol
1924 * type of ETHERTYPE_ATALK (Appletalk).
1926 * XXX - check for an organization code of
1927 * encapsulated Ethernet as well?
1929 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1933 * XXX - we don't have to check for IPX 802.3
1934 * here, but should we check for the IPX Ethertype?
1936 if (proto
<= ETHERMTU
) {
1938 * This is an LLC SAP value, so check
1941 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1944 * This is an Ethernet type; we assume that it's
1945 * unlikely that it'll appear in the right place
1946 * at random, and therefore check only the
1947 * location that would hold the Ethernet type
1948 * in a SNAP frame with an organization code of
1949 * 0x000000 (encapsulated Ethernet).
1951 * XXX - if we were to check for the SNAP DSAP and
1952 * LSAP, as per XXX, and were also to check for an
1953 * organization code of 0x000000 (encapsulated
1954 * Ethernet), we'd do
1956 * return gen_snap(0x000000, proto,
1959 * here; for now, we don't, as per the above.
1960 * I don't know whether it's worth the extra CPU
1961 * time to do the right check or not.
1963 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1968 static struct block
*
1969 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1973 u_int src_off
, dst_off
;
1975 struct block
*b0
, *b1
;
1989 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1990 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1996 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1997 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2004 b0
= gen_linktype(proto
);
2005 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2011 static struct block
*
2012 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2013 struct in6_addr
*addr
;
2014 struct in6_addr
*mask
;
2016 u_int src_off
, dst_off
;
2018 struct block
*b0
, *b1
;
2033 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2034 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2040 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2041 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2048 /* this order is important */
2049 a
= (u_int32_t
*)addr
;
2050 m
= (u_int32_t
*)mask
;
2051 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2052 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2054 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2056 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2058 b0
= gen_linktype(proto
);
2064 static struct block
*
2065 gen_ehostop(eaddr
, dir
)
2066 register const u_char
*eaddr
;
2069 register struct block
*b0
, *b1
;
2073 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2076 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2079 b0
= gen_ehostop(eaddr
, Q_SRC
);
2080 b1
= gen_ehostop(eaddr
, Q_DST
);
2086 b0
= gen_ehostop(eaddr
, Q_SRC
);
2087 b1
= gen_ehostop(eaddr
, Q_DST
);
2096 * Like gen_ehostop, but for DLT_FDDI
2098 static struct block
*
2099 gen_fhostop(eaddr
, dir
)
2100 register const u_char
*eaddr
;
2103 struct block
*b0
, *b1
;
2108 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2110 return gen_bcmp(6 + 1, 6, eaddr
);
2115 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2117 return gen_bcmp(0 + 1, 6, eaddr
);
2121 b0
= gen_fhostop(eaddr
, Q_SRC
);
2122 b1
= gen_fhostop(eaddr
, Q_DST
);
2128 b0
= gen_fhostop(eaddr
, Q_SRC
);
2129 b1
= gen_fhostop(eaddr
, Q_DST
);
2138 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2140 static struct block
*
2141 gen_thostop(eaddr
, dir
)
2142 register const u_char
*eaddr
;
2145 register struct block
*b0
, *b1
;
2149 return gen_bcmp(8, 6, eaddr
);
2152 return gen_bcmp(2, 6, eaddr
);
2155 b0
= gen_thostop(eaddr
, Q_SRC
);
2156 b1
= gen_thostop(eaddr
, Q_DST
);
2162 b0
= gen_thostop(eaddr
, Q_SRC
);
2163 b1
= gen_thostop(eaddr
, Q_DST
);
2172 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2174 static struct block
*
2175 gen_wlanhostop(eaddr
, dir
)
2176 register const u_char
*eaddr
;
2179 register struct block
*b0
, *b1
, *b2
;
2180 register struct slist
*s
;
2187 * For control frames, there is no SA.
2189 * For management frames, SA is at an
2190 * offset of 10 from the beginning of
2193 * For data frames, SA is at an offset
2194 * of 10 from the beginning of the packet
2195 * if From DS is clear, at an offset of
2196 * 16 from the beginning of the packet
2197 * if From DS is set and To DS is clear,
2198 * and an offset of 24 from the beginning
2199 * of the packet if From DS is set and To DS
2204 * Generate the tests to be done for data frames
2207 * First, check for To DS set, i.e. check "link[1] & 0x01".
2209 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2211 b1
= new_block(JMP(BPF_JSET
));
2212 b1
->s
.k
= 0x01; /* To DS */
2216 * If To DS is set, the SA is at 24.
2218 b0
= gen_bcmp(24, 6, eaddr
);
2222 * Now, check for To DS not set, i.e. check
2223 * "!(link[1] & 0x01)".
2225 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2227 b2
= new_block(JMP(BPF_JSET
));
2228 b2
->s
.k
= 0x01; /* To DS */
2233 * If To DS is not set, the SA is at 16.
2235 b1
= gen_bcmp(16, 6, eaddr
);
2239 * Now OR together the last two checks. That gives
2240 * the complete set of checks for data frames with
2246 * Now check for From DS being set, and AND that with
2247 * the ORed-together checks.
2249 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2251 b1
= new_block(JMP(BPF_JSET
));
2252 b1
->s
.k
= 0x02; /* From DS */
2257 * Now check for data frames with From DS not set.
2259 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2261 b2
= new_block(JMP(BPF_JSET
));
2262 b2
->s
.k
= 0x02; /* From DS */
2267 * If From DS isn't set, the SA is at 10.
2269 b1
= gen_bcmp(10, 6, eaddr
);
2273 * Now OR together the checks for data frames with
2274 * From DS not set and for data frames with From DS
2275 * set; that gives the checks done for data frames.
2280 * Now check for a data frame.
2281 * I.e, check "link[0] & 0x08".
2283 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2285 b1
= new_block(JMP(BPF_JSET
));
2290 * AND that with the checks done for data frames.
2295 * If the high-order bit of the type value is 0, this
2296 * is a management frame.
2297 * I.e, check "!(link[0] & 0x08)".
2299 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2301 b2
= new_block(JMP(BPF_JSET
));
2307 * For management frames, the SA is at 10.
2309 b1
= gen_bcmp(10, 6, eaddr
);
2313 * OR that with the checks done for data frames.
2314 * That gives the checks done for management and
2320 * If the low-order bit of the type value is 1,
2321 * this is either a control frame or a frame
2322 * with a reserved type, and thus not a
2325 * I.e., check "!(link[0] & 0x04)".
2327 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2329 b1
= new_block(JMP(BPF_JSET
));
2335 * AND that with the checks for data and management
2345 * For control frames, there is no DA.
2347 * For management frames, DA is at an
2348 * offset of 4 from the beginning of
2351 * For data frames, DA is at an offset
2352 * of 4 from the beginning of the packet
2353 * if To DS is clear and at an offset of
2354 * 16 from the beginning of the packet
2359 * Generate the tests to be done for data frames.
2361 * First, check for To DS set, i.e. "link[1] & 0x01".
2363 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2365 b1
= new_block(JMP(BPF_JSET
));
2366 b1
->s
.k
= 0x01; /* To DS */
2370 * If To DS is set, the DA is at 16.
2372 b0
= gen_bcmp(16, 6, eaddr
);
2376 * Now, check for To DS not set, i.e. check
2377 * "!(link[1] & 0x01)".
2379 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2381 b2
= new_block(JMP(BPF_JSET
));
2382 b2
->s
.k
= 0x01; /* To DS */
2387 * If To DS is not set, the DA is at 4.
2389 b1
= gen_bcmp(4, 6, eaddr
);
2393 * Now OR together the last two checks. That gives
2394 * the complete set of checks for data frames.
2399 * Now check for a data frame.
2400 * I.e, check "link[0] & 0x08".
2402 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2404 b1
= new_block(JMP(BPF_JSET
));
2409 * AND that with the checks done for data frames.
2414 * If the high-order bit of the type value is 0, this
2415 * is a management frame.
2416 * I.e, check "!(link[0] & 0x08)".
2418 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2420 b2
= new_block(JMP(BPF_JSET
));
2426 * For management frames, the DA is at 4.
2428 b1
= gen_bcmp(4, 6, eaddr
);
2432 * OR that with the checks done for data frames.
2433 * That gives the checks done for management and
2439 * If the low-order bit of the type value is 1,
2440 * this is either a control frame or a frame
2441 * with a reserved type, and thus not a
2444 * I.e., check "!(link[0] & 0x04)".
2446 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2448 b1
= new_block(JMP(BPF_JSET
));
2454 * AND that with the checks for data and management
2461 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2462 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2468 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2469 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2478 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2479 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2480 * as the RFC states.)
2482 static struct block
*
2483 gen_ipfchostop(eaddr
, dir
)
2484 register const u_char
*eaddr
;
2487 register struct block
*b0
, *b1
;
2491 return gen_bcmp(10, 6, eaddr
);
2494 return gen_bcmp(2, 6, eaddr
);
2497 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2498 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2504 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2505 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2514 * This is quite tricky because there may be pad bytes in front of the
2515 * DECNET header, and then there are two possible data packet formats that
2516 * carry both src and dst addresses, plus 5 packet types in a format that
2517 * carries only the src node, plus 2 types that use a different format and
2518 * also carry just the src node.
2522 * Instead of doing those all right, we just look for data packets with
2523 * 0 or 1 bytes of padding. If you want to look at other packets, that
2524 * will require a lot more hacking.
2526 * To add support for filtering on DECNET "areas" (network numbers)
2527 * one would want to add a "mask" argument to this routine. That would
2528 * make the filter even more inefficient, although one could be clever
2529 * and not generate masking instructions if the mask is 0xFFFF.
2531 static struct block
*
2532 gen_dnhostop(addr
, dir
, base_off
)
2537 struct block
*b0
, *b1
, *b2
, *tmp
;
2538 u_int offset_lh
; /* offset if long header is received */
2539 u_int offset_sh
; /* offset if short header is received */
2544 offset_sh
= 1; /* follows flags */
2545 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2549 offset_sh
= 3; /* follows flags, dstnode */
2550 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2554 /* Inefficient because we do our Calvinball dance twice */
2555 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2556 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2562 /* Inefficient because we do our Calvinball dance twice */
2563 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2564 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2569 bpf_error("ISO host filtering not implemented");
2574 b0
= gen_linktype(ETHERTYPE_DN
);
2575 /* Check for pad = 1, long header case */
2576 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2577 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2578 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2579 BPF_H
, (bpf_int32
)ntohs(addr
));
2581 /* Check for pad = 0, long header case */
2582 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2583 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2586 /* Check for pad = 1, short header case */
2587 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2588 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2589 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2590 BPF_H
, (bpf_int32
)ntohs(addr
));
2593 /* Check for pad = 0, short header case */
2594 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2595 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2599 /* Combine with test for linktype */
2604 static struct block
*
2605 gen_host(addr
, mask
, proto
, dir
)
2611 struct block
*b0
, *b1
;
2616 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2617 if (off_linktype
!= (u_int
)-1) {
2618 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2620 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2626 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2627 off_nl
+ 12, off_nl
+ 16);
2630 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2631 off_nl
+ 14, off_nl
+ 24);
2634 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2635 off_nl
+ 14, off_nl
+ 24);
2638 bpf_error("'tcp' modifier applied to host");
2641 bpf_error("'sctp' modifier applied to host");
2644 bpf_error("'udp' modifier applied to host");
2647 bpf_error("'icmp' modifier applied to host");
2650 bpf_error("'igmp' modifier applied to host");
2653 bpf_error("'igrp' modifier applied to host");
2656 bpf_error("'pim' modifier applied to host");
2659 bpf_error("'vrrp' modifier applied to host");
2662 bpf_error("ATALK host filtering not implemented");
2665 bpf_error("AARP host filtering not implemented");
2668 return gen_dnhostop(addr
, dir
, off_nl
);
2671 bpf_error("SCA host filtering not implemented");
2674 bpf_error("LAT host filtering not implemented");
2677 bpf_error("MOPDL host filtering not implemented");
2680 bpf_error("MOPRC host filtering not implemented");
2684 bpf_error("'ip6' modifier applied to ip host");
2687 bpf_error("'icmp6' modifier applied to host");
2691 bpf_error("'ah' modifier applied to host");
2694 bpf_error("'esp' modifier applied to host");
2697 bpf_error("ISO host filtering not implemented");
2700 bpf_error("'esis' modifier applied to host");
2703 bpf_error("'isis' modifier applied to host");
2706 bpf_error("'clnp' modifier applied to host");
2709 bpf_error("'stp' modifier applied to host");
2712 bpf_error("IPX host filtering not implemented");
2715 bpf_error("'netbeui' modifier applied to host");
2724 static struct block
*
2725 gen_host6(addr
, mask
, proto
, dir
)
2726 struct in6_addr
*addr
;
2727 struct in6_addr
*mask
;
2734 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2737 bpf_error("'ip' modifier applied to ip6 host");
2740 bpf_error("'rarp' modifier applied to ip6 host");
2743 bpf_error("'arp' modifier applied to ip6 host");
2746 bpf_error("'sctp' modifier applied to host");
2749 bpf_error("'tcp' modifier applied to host");
2752 bpf_error("'udp' modifier applied to host");
2755 bpf_error("'icmp' modifier applied to host");
2758 bpf_error("'igmp' modifier applied to host");
2761 bpf_error("'igrp' modifier applied to host");
2764 bpf_error("'pim' modifier applied to host");
2767 bpf_error("'vrrp' modifier applied to host");
2770 bpf_error("ATALK host filtering not implemented");
2773 bpf_error("AARP host filtering not implemented");
2776 bpf_error("'decnet' modifier applied to ip6 host");
2779 bpf_error("SCA host filtering not implemented");
2782 bpf_error("LAT host filtering not implemented");
2785 bpf_error("MOPDL host filtering not implemented");
2788 bpf_error("MOPRC host filtering not implemented");
2791 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2792 off_nl
+ 8, off_nl
+ 24);
2795 bpf_error("'icmp6' modifier applied to host");
2798 bpf_error("'ah' modifier applied to host");
2801 bpf_error("'esp' modifier applied to host");
2804 bpf_error("ISO host filtering not implemented");
2807 bpf_error("'esis' modifier applied to host");
2810 bpf_error("'isis' modifier applied to host");
2813 bpf_error("'clnp' modifier applied to host");
2816 bpf_error("'stp' modifier applied to host");
2819 bpf_error("IPX host filtering not implemented");
2822 bpf_error("'netbeui' modifier applied to host");
2832 static struct block
*
2833 gen_gateway(eaddr
, alist
, proto
, dir
)
2834 const u_char
*eaddr
;
2835 bpf_u_int32
**alist
;
2839 struct block
*b0
, *b1
, *tmp
;
2842 bpf_error("direction applied to 'gateway'");
2849 if (linktype
== DLT_EN10MB
)
2850 b0
= gen_ehostop(eaddr
, Q_OR
);
2851 else if (linktype
== DLT_FDDI
)
2852 b0
= gen_fhostop(eaddr
, Q_OR
);
2853 else if (linktype
== DLT_IEEE802
)
2854 b0
= gen_thostop(eaddr
, Q_OR
);
2855 else if (linktype
== DLT_IEEE802_11
)
2856 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2857 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2859 * Check that the packet doesn't begin with an
2860 * LE Control marker. (We've already generated
2863 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2867 * Now check the MAC address.
2869 b0
= gen_ehostop(eaddr
, Q_OR
);
2871 } else if (linktype
== DLT_IP_OVER_FC
)
2872 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2875 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2877 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2879 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2887 bpf_error("illegal modifier of 'gateway'");
2893 gen_proto_abbrev(proto
)
2902 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2904 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2910 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2912 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2918 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2920 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2926 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2929 #ifndef IPPROTO_IGMP
2930 #define IPPROTO_IGMP 2
2934 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2937 #ifndef IPPROTO_IGRP
2938 #define IPPROTO_IGRP 9
2941 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2945 #define IPPROTO_PIM 103
2949 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2951 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2956 #ifndef IPPROTO_VRRP
2957 #define IPPROTO_VRRP 112
2961 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2965 b1
= gen_linktype(ETHERTYPE_IP
);
2969 b1
= gen_linktype(ETHERTYPE_ARP
);
2973 b1
= gen_linktype(ETHERTYPE_REVARP
);
2977 bpf_error("link layer applied in wrong context");
2980 b1
= gen_linktype(ETHERTYPE_ATALK
);
2984 b1
= gen_linktype(ETHERTYPE_AARP
);
2988 b1
= gen_linktype(ETHERTYPE_DN
);
2992 b1
= gen_linktype(ETHERTYPE_SCA
);
2996 b1
= gen_linktype(ETHERTYPE_LAT
);
3000 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3004 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3009 b1
= gen_linktype(ETHERTYPE_IPV6
);
3012 #ifndef IPPROTO_ICMPV6
3013 #define IPPROTO_ICMPV6 58
3016 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3021 #define IPPROTO_AH 51
3024 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3026 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3032 #define IPPROTO_ESP 50
3035 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3037 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3043 b1
= gen_linktype(LLCSAP_ISONS
);
3047 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3051 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3054 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3055 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3056 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3058 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3060 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3062 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3066 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3067 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3068 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3070 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3072 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3074 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3078 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3079 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3080 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3082 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3087 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3088 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3093 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3094 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3096 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3098 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3103 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3104 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3109 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3110 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3115 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3119 b1
= gen_linktype(LLCSAP_8021D
);
3123 b1
= gen_linktype(LLCSAP_IPX
);
3127 b1
= gen_linktype(LLCSAP_NETBEUI
);
3136 static struct block
*
3143 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3144 s
->s
.k
= off_nl
+ 6;
3145 b
= new_block(JMP(BPF_JSET
));
3153 static struct block
*
3154 gen_portatom(off
, v
)
3161 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3164 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3165 s
->next
->s
.k
= off_nl
+ off
;
3167 b
= new_block(JMP(BPF_JEQ
));
3175 static struct block
*
3176 gen_portatom6(off
, v
)
3180 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3185 gen_portop(port
, proto
, dir
)
3186 int port
, proto
, dir
;
3188 struct block
*b0
, *b1
, *tmp
;
3190 /* ip proto 'proto' */
3191 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3197 b1
= gen_portatom(0, (bpf_int32
)port
);
3201 b1
= gen_portatom(2, (bpf_int32
)port
);
3206 tmp
= gen_portatom(0, (bpf_int32
)port
);
3207 b1
= gen_portatom(2, (bpf_int32
)port
);
3212 tmp
= gen_portatom(0, (bpf_int32
)port
);
3213 b1
= gen_portatom(2, (bpf_int32
)port
);
3225 static struct block
*
3226 gen_port(port
, ip_proto
, dir
)
3231 struct block
*b0
, *b1
, *tmp
;
3236 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3237 * not LLC encapsulation with LLCSAP_IP.
3239 * For IEEE 802 networks - which includes 802.5 token ring
3240 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3241 * says that SNAP encapsulation is used, not LLC encapsulation
3244 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3245 * RFC 2225 say that SNAP encapsulation is used, not LLC
3246 * encapsulation with LLCSAP_IP.
3248 * So we always check for ETHERTYPE_IP.
3250 b0
= gen_linktype(ETHERTYPE_IP
);
3256 b1
= gen_portop(port
, ip_proto
, dir
);
3260 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3261 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3263 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3276 gen_portop6(port
, proto
, dir
)
3277 int port
, proto
, dir
;
3279 struct block
*b0
, *b1
, *tmp
;
3281 /* ip proto 'proto' */
3282 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3286 b1
= gen_portatom6(0, (bpf_int32
)port
);
3290 b1
= gen_portatom6(2, (bpf_int32
)port
);
3295 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3296 b1
= gen_portatom6(2, (bpf_int32
)port
);
3301 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3302 b1
= gen_portatom6(2, (bpf_int32
)port
);
3314 static struct block
*
3315 gen_port6(port
, ip_proto
, dir
)
3320 struct block
*b0
, *b1
, *tmp
;
3322 /* ether proto ip */
3323 b0
= gen_linktype(ETHERTYPE_IPV6
);
3329 b1
= gen_portop6(port
, ip_proto
, dir
);
3333 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3334 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3336 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3349 lookup_proto(name
, proto
)
3350 register const char *name
;
3360 v
= pcap_nametoproto(name
);
3361 if (v
== PROTO_UNDEF
)
3362 bpf_error("unknown ip proto '%s'", name
);
3366 /* XXX should look up h/w protocol type based on linktype */
3367 v
= pcap_nametoeproto(name
);
3368 if (v
== PROTO_UNDEF
)
3369 bpf_error("unknown ether proto '%s'", name
);
3373 if (strcmp(name
, "esis") == 0)
3375 else if (strcmp(name
, "isis") == 0)
3377 else if (strcmp(name
, "clnp") == 0)
3380 bpf_error("unknown osi proto '%s'", name
);
3400 static struct block
*
3401 gen_protochain(v
, proto
, dir
)
3406 #ifdef NO_PROTOCHAIN
3407 return gen_proto(v
, proto
, dir
);
3409 struct block
*b0
, *b
;
3410 struct slist
*s
[100];
3411 int fix2
, fix3
, fix4
, fix5
;
3412 int ahcheck
, again
, end
;
3414 int reg2
= alloc_reg();
3416 memset(s
, 0, sizeof(s
));
3417 fix2
= fix3
= fix4
= fix5
= 0;
3424 b0
= gen_protochain(v
, Q_IP
, dir
);
3425 b
= gen_protochain(v
, Q_IPV6
, dir
);
3429 bpf_error("bad protocol applied for 'protochain'");
3433 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3436 * s[0] is a dummy entry to protect other BPF insn from damaged
3437 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3438 * hard to find interdependency made by jump table fixup.
3441 s
[i
] = new_stmt(0); /*dummy*/
3446 b0
= gen_linktype(ETHERTYPE_IP
);
3449 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3450 s
[i
]->s
.k
= off_nl
+ 9;
3452 /* X = ip->ip_hl << 2 */
3453 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3459 b0
= gen_linktype(ETHERTYPE_IPV6
);
3461 /* A = ip6->ip_nxt */
3462 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3463 s
[i
]->s
.k
= off_nl
+ 6;
3465 /* X = sizeof(struct ip6_hdr) */
3466 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3472 bpf_error("unsupported proto to gen_protochain");
3476 /* again: if (A == v) goto end; else fall through; */
3478 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3480 s
[i
]->s
.jt
= NULL
; /*later*/
3481 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3485 #ifndef IPPROTO_NONE
3486 #define IPPROTO_NONE 59
3488 /* if (A == IPPROTO_NONE) goto end */
3489 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3490 s
[i
]->s
.jt
= NULL
; /*later*/
3491 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3492 s
[i
]->s
.k
= IPPROTO_NONE
;
3493 s
[fix5
]->s
.jf
= s
[i
];
3498 if (proto
== Q_IPV6
) {
3499 int v6start
, v6end
, v6advance
, j
;
3502 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3503 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3504 s
[i
]->s
.jt
= NULL
; /*later*/
3505 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3506 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3507 s
[fix2
]->s
.jf
= s
[i
];
3509 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3510 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3511 s
[i
]->s
.jt
= NULL
; /*later*/
3512 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3513 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3515 /* if (A == IPPROTO_ROUTING) goto v6advance */
3516 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3517 s
[i
]->s
.jt
= NULL
; /*later*/
3518 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3519 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3521 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3522 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3523 s
[i
]->s
.jt
= NULL
; /*later*/
3524 s
[i
]->s
.jf
= NULL
; /*later*/
3525 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3536 * X = X + (P[X + 1] + 1) * 8;
3539 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3541 /* A = P[X + packet head] */
3542 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3546 s
[i
] = new_stmt(BPF_ST
);
3550 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3553 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3557 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3559 /* A = P[X + packet head]; */
3560 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3564 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3568 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3572 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3575 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3579 /* goto again; (must use BPF_JA for backward jump) */
3580 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3581 s
[i
]->s
.k
= again
- i
- 1;
3582 s
[i
- 1]->s
.jf
= s
[i
];
3586 for (j
= v6start
; j
<= v6end
; j
++)
3587 s
[j
]->s
.jt
= s
[v6advance
];
3592 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3594 s
[fix2
]->s
.jf
= s
[i
];
3600 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3601 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3602 s
[i
]->s
.jt
= NULL
; /*later*/
3603 s
[i
]->s
.jf
= NULL
; /*later*/
3604 s
[i
]->s
.k
= IPPROTO_AH
;
3606 s
[fix3
]->s
.jf
= s
[ahcheck
];
3613 * X = X + (P[X + 1] + 2) * 4;
3616 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3618 /* A = P[X + packet head]; */
3619 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3623 s
[i
] = new_stmt(BPF_ST
);
3627 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3630 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3634 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3636 /* A = P[X + packet head] */
3637 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3641 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3645 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3649 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3652 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3656 /* goto again; (must use BPF_JA for backward jump) */
3657 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3658 s
[i
]->s
.k
= again
- i
- 1;
3663 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3665 s
[fix2
]->s
.jt
= s
[end
];
3666 s
[fix4
]->s
.jf
= s
[end
];
3667 s
[fix5
]->s
.jt
= s
[end
];
3674 for (i
= 0; i
< max
- 1; i
++)
3675 s
[i
]->next
= s
[i
+ 1];
3676 s
[max
- 1]->next
= NULL
;
3681 b
= new_block(JMP(BPF_JEQ
));
3682 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3692 static struct block
*
3693 gen_proto(v
, proto
, dir
)
3698 struct block
*b0
, *b1
;
3700 if (dir
!= Q_DEFAULT
)
3701 bpf_error("direction applied to 'proto'");
3706 b0
= gen_proto(v
, Q_IP
, dir
);
3707 b1
= gen_proto(v
, Q_IPV6
, dir
);
3715 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3716 * not LLC encapsulation with LLCSAP_IP.
3718 * For IEEE 802 networks - which includes 802.5 token ring
3719 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3720 * says that SNAP encapsulation is used, not LLC encapsulation
3723 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3724 * RFC 2225 say that SNAP encapsulation is used, not LLC
3725 * encapsulation with LLCSAP_IP.
3727 * So we always check for ETHERTYPE_IP.
3729 b0
= gen_linktype(ETHERTYPE_IP
);
3731 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3733 b1
= gen_protochain(v
, Q_IP
);
3743 * Frame Relay packets typically have an OSI
3744 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3745 * generates code to check for all the OSI
3746 * NLPIDs, so calling it and then adding a check
3747 * for the particular NLPID for which we're
3748 * looking is bogus, as we can just check for
3751 * What we check for is the NLPID and a frame
3752 * control field value of UI, i.e. 0x03 followed
3755 * XXX - assumes a 2-byte Frame Relay header with
3756 * DLCI and flags. What if the address is longer?
3758 * XXX - what about SNAP-encapsulated frames?
3760 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3766 * Cisco uses an Ethertype lookalike - for OSI,
3769 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3770 /* OSI in C-HDLC is stuffed with a fudge byte */
3771 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3776 b0
= gen_linktype(LLCSAP_ISONS
);
3777 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3783 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3785 * 4 is the offset of the PDU type relative to the IS-IS
3788 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3793 bpf_error("arp does not encapsulate another protocol");
3797 bpf_error("rarp does not encapsulate another protocol");
3801 bpf_error("atalk encapsulation is not specifiable");
3805 bpf_error("decnet encapsulation is not specifiable");
3809 bpf_error("sca does not encapsulate another protocol");
3813 bpf_error("lat does not encapsulate another protocol");
3817 bpf_error("moprc does not encapsulate another protocol");
3821 bpf_error("mopdl does not encapsulate another protocol");
3825 return gen_linktype(v
);
3828 bpf_error("'udp proto' is bogus");
3832 bpf_error("'tcp proto' is bogus");
3836 bpf_error("'sctp proto' is bogus");
3840 bpf_error("'icmp proto' is bogus");
3844 bpf_error("'igmp proto' is bogus");
3848 bpf_error("'igrp proto' is bogus");
3852 bpf_error("'pim proto' is bogus");
3856 bpf_error("'vrrp proto' is bogus");
3861 b0
= gen_linktype(ETHERTYPE_IPV6
);
3863 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3865 b1
= gen_protochain(v
, Q_IPV6
);
3871 bpf_error("'icmp6 proto' is bogus");
3875 bpf_error("'ah proto' is bogus");
3878 bpf_error("'ah proto' is bogus");
3881 bpf_error("'stp proto' is bogus");
3884 bpf_error("'ipx proto' is bogus");
3887 bpf_error("'netbeui proto' is bogus");
3898 register const char *name
;
3901 int proto
= q
.proto
;
3905 bpf_u_int32 mask
, addr
;
3907 bpf_u_int32
**alist
;
3910 struct sockaddr_in
*sin
;
3911 struct sockaddr_in6
*sin6
;
3912 struct addrinfo
*res
, *res0
;
3913 struct in6_addr mask128
;
3915 struct block
*b
, *tmp
;
3916 int port
, real_proto
;
3921 addr
= pcap_nametonetaddr(name
);
3923 bpf_error("unknown network '%s'", name
);
3924 /* Left justify network addr and calculate its network mask */
3926 while (addr
&& (addr
& 0xff000000) == 0) {
3930 return gen_host(addr
, mask
, proto
, dir
);
3934 if (proto
== Q_LINK
) {
3938 eaddr
= pcap_ether_hostton(name
);
3941 "unknown ether host '%s'", name
);
3942 b
= gen_ehostop(eaddr
, dir
);
3947 eaddr
= pcap_ether_hostton(name
);
3950 "unknown FDDI host '%s'", name
);
3951 b
= gen_fhostop(eaddr
, dir
);
3956 eaddr
= pcap_ether_hostton(name
);
3959 "unknown token ring host '%s'", name
);
3960 b
= gen_thostop(eaddr
, dir
);
3964 case DLT_IEEE802_11
:
3965 eaddr
= pcap_ether_hostton(name
);
3968 "unknown 802.11 host '%s'", name
);
3969 b
= gen_wlanhostop(eaddr
, dir
);
3973 case DLT_IP_OVER_FC
:
3974 eaddr
= pcap_ether_hostton(name
);
3977 "unknown Fibre Channel host '%s'", name
);
3978 b
= gen_ipfchostop(eaddr
, dir
);
3987 * Check that the packet doesn't begin
3988 * with an LE Control marker. (We've
3989 * already generated a test for LANE.)
3991 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3995 eaddr
= pcap_ether_hostton(name
);
3998 "unknown ether host '%s'", name
);
3999 b
= gen_ehostop(eaddr
, dir
);
4005 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4006 } else if (proto
== Q_DECNET
) {
4007 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4009 * I don't think DECNET hosts can be multihomed, so
4010 * there is no need to build up a list of addresses
4012 return (gen_host(dn_addr
, 0, proto
, dir
));
4015 alist
= pcap_nametoaddr(name
);
4016 if (alist
== NULL
|| *alist
== NULL
)
4017 bpf_error("unknown host '%s'", name
);
4019 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4021 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4023 tmp
= gen_host(**alist
++, 0xffffffff,
4030 memset(&mask128
, 0xff, sizeof(mask128
));
4031 res0
= res
= pcap_nametoaddrinfo(name
);
4033 bpf_error("unknown host '%s'", name
);
4035 tproto
= tproto6
= proto
;
4036 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4040 for (res
= res0
; res
; res
= res
->ai_next
) {
4041 switch (res
->ai_family
) {
4043 if (tproto
== Q_IPV6
)
4046 sin
= (struct sockaddr_in
*)
4048 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4049 0xffffffff, tproto
, dir
);
4052 if (tproto6
== Q_IP
)
4055 sin6
= (struct sockaddr_in6
*)
4057 tmp
= gen_host6(&sin6
->sin6_addr
,
4058 &mask128
, tproto6
, dir
);
4069 bpf_error("unknown host '%s'%s", name
,
4070 (proto
== Q_DEFAULT
)
4072 : " for specified address family");
4079 if (proto
!= Q_DEFAULT
&&
4080 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4081 bpf_error("illegal qualifier of 'port'");
4082 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4083 bpf_error("unknown port '%s'", name
);
4084 if (proto
== Q_UDP
) {
4085 if (real_proto
== IPPROTO_TCP
)
4086 bpf_error("port '%s' is tcp", name
);
4087 else if (real_proto
== IPPROTO_SCTP
)
4088 bpf_error("port '%s' is sctp", name
);
4090 /* override PROTO_UNDEF */
4091 real_proto
= IPPROTO_UDP
;
4093 if (proto
== Q_TCP
) {
4094 if (real_proto
== IPPROTO_UDP
)
4095 bpf_error("port '%s' is udp", name
);
4097 else if (real_proto
== IPPROTO_SCTP
)
4098 bpf_error("port '%s' is sctp", name
);
4100 /* override PROTO_UNDEF */
4101 real_proto
= IPPROTO_TCP
;
4103 if (proto
== Q_SCTP
) {
4104 if (real_proto
== IPPROTO_UDP
)
4105 bpf_error("port '%s' is udp", name
);
4107 else if (real_proto
== IPPROTO_TCP
)
4108 bpf_error("port '%s' is tcp", name
);
4110 /* override PROTO_UNDEF */
4111 real_proto
= IPPROTO_SCTP
;
4114 return gen_port(port
, real_proto
, dir
);
4118 b
= gen_port(port
, real_proto
, dir
);
4119 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4126 eaddr
= pcap_ether_hostton(name
);
4128 bpf_error("unknown ether host: %s", name
);
4130 alist
= pcap_nametoaddr(name
);
4131 if (alist
== NULL
|| *alist
== NULL
)
4132 bpf_error("unknown host '%s'", name
);
4133 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4137 bpf_error("'gateway' not supported in this configuration");
4141 real_proto
= lookup_proto(name
, proto
);
4142 if (real_proto
>= 0)
4143 return gen_proto(real_proto
, proto
, dir
);
4145 bpf_error("unknown protocol: %s", name
);
4148 real_proto
= lookup_proto(name
, proto
);
4149 if (real_proto
>= 0)
4150 return gen_protochain(real_proto
, proto
, dir
);
4152 bpf_error("unknown protocol: %s", name
);
4164 gen_mcode(s1
, s2
, masklen
, q
)
4165 register const char *s1
, *s2
;
4166 register int masklen
;
4169 register int nlen
, mlen
;
4172 nlen
= __pcap_atoin(s1
, &n
);
4173 /* Promote short ipaddr */
4177 mlen
= __pcap_atoin(s2
, &m
);
4178 /* Promote short ipaddr */
4181 bpf_error("non-network bits set in \"%s mask %s\"",
4184 /* Convert mask len to mask */
4186 bpf_error("mask length must be <= 32");
4187 m
= 0xffffffff << (32 - masklen
);
4189 bpf_error("non-network bits set in \"%s/%d\"",
4196 return gen_host(n
, m
, q
.proto
, q
.dir
);
4199 bpf_error("Mask syntax for networks only");
4207 register const char *s
;
4212 int proto
= q
.proto
;
4218 else if (q
.proto
== Q_DECNET
)
4219 vlen
= __pcap_atodn(s
, &v
);
4221 vlen
= __pcap_atoin(s
, &v
);
4228 if (proto
== Q_DECNET
)
4229 return gen_host(v
, 0, proto
, dir
);
4230 else if (proto
== Q_LINK
) {
4231 bpf_error("illegal link layer address");
4234 if (s
== NULL
&& q
.addr
== Q_NET
) {
4235 /* Promote short net number */
4236 while (v
&& (v
& 0xff000000) == 0) {
4241 /* Promote short ipaddr */
4245 return gen_host(v
, mask
, proto
, dir
);
4250 proto
= IPPROTO_UDP
;
4251 else if (proto
== Q_TCP
)
4252 proto
= IPPROTO_TCP
;
4253 else if (proto
== Q_SCTP
)
4254 proto
= IPPROTO_SCTP
;
4255 else if (proto
== Q_DEFAULT
)
4256 proto
= PROTO_UNDEF
;
4258 bpf_error("illegal qualifier of 'port'");
4261 return gen_port((int)v
, proto
, dir
);
4265 b
= gen_port((int)v
, proto
, dir
);
4266 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4272 bpf_error("'gateway' requires a name");
4276 return gen_proto((int)v
, proto
, dir
);
4279 return gen_protochain((int)v
, proto
, dir
);
4294 gen_mcode6(s1
, s2
, masklen
, q
)
4295 register const char *s1
, *s2
;
4296 register int masklen
;
4299 struct addrinfo
*res
;
4300 struct in6_addr
*addr
;
4301 struct in6_addr mask
;
4306 bpf_error("no mask %s supported", s2
);
4308 res
= pcap_nametoaddrinfo(s1
);
4310 bpf_error("invalid ip6 address %s", s1
);
4312 bpf_error("%s resolved to multiple address", s1
);
4313 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4315 if (sizeof(mask
) * 8 < masklen
)
4316 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4317 memset(&mask
, 0, sizeof(mask
));
4318 memset(&mask
, 0xff, masklen
/ 8);
4320 mask
.s6_addr
[masklen
/ 8] =
4321 (0xff << (8 - masklen
% 8)) & 0xff;
4324 a
= (u_int32_t
*)addr
;
4325 m
= (u_int32_t
*)&mask
;
4326 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4327 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4328 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4336 bpf_error("Mask syntax for networks only");
4340 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4345 bpf_error("invalid qualifier against IPv6 address");
4353 register const u_char
*eaddr
;
4356 struct block
*b
, *tmp
;
4358 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4359 if (linktype
== DLT_EN10MB
)
4360 return gen_ehostop(eaddr
, (int)q
.dir
);
4361 if (linktype
== DLT_FDDI
)
4362 return gen_fhostop(eaddr
, (int)q
.dir
);
4363 if (linktype
== DLT_IEEE802
)
4364 return gen_thostop(eaddr
, (int)q
.dir
);
4365 if (linktype
== DLT_IEEE802_11
)
4366 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4367 if (linktype
== DLT_SUNATM
&& is_lane
) {
4369 * Check that the packet doesn't begin with an
4370 * LE Control marker. (We've already generated
4373 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4377 * Now check the MAC address.
4379 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4383 if (linktype
== DLT_IP_OVER_FC
)
4384 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4385 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4387 bpf_error("ethernet address used in non-ether expression");
4393 struct slist
*s0
, *s1
;
4396 * This is definitely not the best way to do this, but the
4397 * lists will rarely get long.
4404 static struct slist
*
4410 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4415 static struct slist
*
4421 s
= new_stmt(BPF_LD
|BPF_MEM
);
4427 gen_load(proto
, index
, size
)
4432 struct slist
*s
, *tmp
;
4434 int regno
= alloc_reg();
4436 free_reg(index
->regno
);
4440 bpf_error("data size must be 1, 2, or 4");
4456 bpf_error("unsupported index operation");
4460 * XXX - what about ATM LANE? Should the index be
4461 * relative to the beginning of the AAL5 frame, so
4462 * that 0 refers to the beginning of the LE Control
4463 * field, or relative to the beginning of the LAN
4464 * frame, so that 0 refers, for Ethernet LANE, to
4465 * the beginning of the destination address?
4467 s
= xfer_to_x(index
);
4468 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4470 sappend(index
->s
, s
);
4485 /* XXX Note that we assume a fixed link header here. */
4486 s
= xfer_to_x(index
);
4487 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4490 sappend(index
->s
, s
);
4492 b
= gen_proto_abbrev(proto
);
4494 gen_and(index
->b
, b
);
4506 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4508 sappend(s
, xfer_to_a(index
));
4509 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4510 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4511 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4513 sappend(index
->s
, s
);
4515 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4517 gen_and(index
->b
, b
);
4519 gen_and(gen_proto_abbrev(Q_IP
), b
);
4525 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4529 index
->regno
= regno
;
4530 s
= new_stmt(BPF_ST
);
4532 sappend(index
->s
, s
);
4538 gen_relation(code
, a0
, a1
, reversed
)
4540 struct arth
*a0
, *a1
;
4543 struct slist
*s0
, *s1
, *s2
;
4544 struct block
*b
, *tmp
;
4548 if (code
== BPF_JEQ
) {
4549 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4550 b
= new_block(JMP(code
));
4554 b
= new_block(BPF_JMP
|code
|BPF_X
);
4560 sappend(a0
->s
, a1
->s
);
4564 free_reg(a0
->regno
);
4565 free_reg(a1
->regno
);
4567 /* 'and' together protocol checks */
4570 gen_and(a0
->b
, tmp
= a1
->b
);
4586 int regno
= alloc_reg();
4587 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4590 s
= new_stmt(BPF_LD
|BPF_LEN
);
4591 s
->next
= new_stmt(BPF_ST
);
4592 s
->next
->s
.k
= regno
;
4607 a
= (struct arth
*)newchunk(sizeof(*a
));
4611 s
= new_stmt(BPF_LD
|BPF_IMM
);
4613 s
->next
= new_stmt(BPF_ST
);
4629 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4632 s
= new_stmt(BPF_ST
);
4640 gen_arth(code
, a0
, a1
)
4642 struct arth
*a0
, *a1
;
4644 struct slist
*s0
, *s1
, *s2
;
4648 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4653 sappend(a0
->s
, a1
->s
);
4655 free_reg(a0
->regno
);
4656 free_reg(a1
->regno
);
4658 s0
= new_stmt(BPF_ST
);
4659 a0
->regno
= s0
->s
.k
= alloc_reg();
4666 * Here we handle simple allocation of the scratch registers.
4667 * If too many registers are alloc'd, the allocator punts.
4669 static int regused
[BPF_MEMWORDS
];
4673 * Return the next free register.
4678 int n
= BPF_MEMWORDS
;
4681 if (regused
[curreg
])
4682 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4684 regused
[curreg
] = 1;
4688 bpf_error("too many registers needed to evaluate expression");
4693 * Return a register to the table so it can
4703 static struct block
*
4710 s
= new_stmt(BPF_LD
|BPF_LEN
);
4711 b
= new_block(JMP(jmp
));
4722 return gen_len(BPF_JGE
, n
);
4726 * Actually, this is less than or equal.
4734 b
= gen_len(BPF_JGT
, n
);
4741 gen_byteop(op
, idx
, val
)
4752 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4755 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4756 b
->s
.code
= JMP(BPF_JGE
);
4761 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4762 b
->s
.code
= JMP(BPF_JGT
);
4766 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4770 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4774 b
= new_block(JMP(BPF_JEQ
));
4781 static u_char abroadcast
[] = { 0x0 };
4784 gen_broadcast(proto
)
4787 bpf_u_int32 hostmask
;
4788 struct block
*b0
, *b1
, *b2
;
4789 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4795 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4796 return gen_ahostop(abroadcast
, Q_DST
);
4797 if (linktype
== DLT_EN10MB
)
4798 return gen_ehostop(ebroadcast
, Q_DST
);
4799 if (linktype
== DLT_FDDI
)
4800 return gen_fhostop(ebroadcast
, Q_DST
);
4801 if (linktype
== DLT_IEEE802
)
4802 return gen_thostop(ebroadcast
, Q_DST
);
4803 if (linktype
== DLT_IEEE802_11
)
4804 return gen_wlanhostop(ebroadcast
, Q_DST
);
4805 if (linktype
== DLT_IP_OVER_FC
)
4806 return gen_ipfchostop(ebroadcast
, Q_DST
);
4807 if (linktype
== DLT_SUNATM
&& is_lane
) {
4809 * Check that the packet doesn't begin with an
4810 * LE Control marker. (We've already generated
4813 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4817 * Now check the MAC address.
4819 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4823 bpf_error("not a broadcast link");
4827 b0
= gen_linktype(ETHERTYPE_IP
);
4828 hostmask
= ~netmask
;
4829 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4830 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4831 (bpf_int32
)(~0 & hostmask
), hostmask
);
4836 bpf_error("only link-layer/IP broadcast filters supported");
4841 * Generate code to test the low-order bit of a MAC address (that's
4842 * the bottom bit of the *first* byte).
4844 static struct block
*
4845 gen_mac_multicast(offset
)
4848 register struct block
*b0
;
4849 register struct slist
*s
;
4851 /* link[offset] & 1 != 0 */
4852 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4854 b0
= new_block(JMP(BPF_JSET
));
4861 gen_multicast(proto
)
4864 register struct block
*b0
, *b1
, *b2
;
4865 register struct slist
*s
;
4871 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4872 /* all ARCnet multicasts use the same address */
4873 return gen_ahostop(abroadcast
, Q_DST
);
4875 if (linktype
== DLT_EN10MB
) {
4876 /* ether[0] & 1 != 0 */
4877 return gen_mac_multicast(0);
4880 if (linktype
== DLT_FDDI
) {
4882 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4884 * XXX - was that referring to bit-order issues?
4886 /* fddi[1] & 1 != 0 */
4887 return gen_mac_multicast(1);
4890 if (linktype
== DLT_IEEE802
) {
4891 /* tr[2] & 1 != 0 */
4892 return gen_mac_multicast(2);
4895 if (linktype
== DLT_IEEE802_11
) {
4899 * For control frames, there is no DA.
4901 * For management frames, DA is at an
4902 * offset of 4 from the beginning of
4905 * For data frames, DA is at an offset
4906 * of 4 from the beginning of the packet
4907 * if To DS is clear and at an offset of
4908 * 16 from the beginning of the packet
4913 * Generate the tests to be done for data frames.
4915 * First, check for To DS set, i.e. "link[1] & 0x01".
4917 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4919 b1
= new_block(JMP(BPF_JSET
));
4920 b1
->s
.k
= 0x01; /* To DS */
4924 * If To DS is set, the DA is at 16.
4926 b0
= gen_mac_multicast(16);
4930 * Now, check for To DS not set, i.e. check
4931 * "!(link[1] & 0x01)".
4933 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4935 b2
= new_block(JMP(BPF_JSET
));
4936 b2
->s
.k
= 0x01; /* To DS */
4941 * If To DS is not set, the DA is at 4.
4943 b1
= gen_mac_multicast(4);
4947 * Now OR together the last two checks. That gives
4948 * the complete set of checks for data frames.
4953 * Now check for a data frame.
4954 * I.e, check "link[0] & 0x08".
4956 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4958 b1
= new_block(JMP(BPF_JSET
));
4963 * AND that with the checks done for data frames.
4968 * If the high-order bit of the type value is 0, this
4969 * is a management frame.
4970 * I.e, check "!(link[0] & 0x08)".
4972 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4974 b2
= new_block(JMP(BPF_JSET
));
4980 * For management frames, the DA is at 4.
4982 b1
= gen_mac_multicast(4);
4986 * OR that with the checks done for data frames.
4987 * That gives the checks done for management and
4993 * If the low-order bit of the type value is 1,
4994 * this is either a control frame or a frame
4995 * with a reserved type, and thus not a
4998 * I.e., check "!(link[0] & 0x04)".
5000 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5002 b1
= new_block(JMP(BPF_JSET
));
5008 * AND that with the checks for data and management
5015 if (linktype
== DLT_IP_OVER_FC
) {
5016 b0
= gen_mac_multicast(2);
5020 if (linktype
== DLT_SUNATM
&& is_lane
) {
5022 * Check that the packet doesn't begin with an
5023 * LE Control marker. (We've already generated
5026 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5029 /* ether[off_mac] & 1 != 0 */
5030 b0
= gen_mac_multicast(off_mac
);
5035 /* Link not known to support multicasts */
5039 b0
= gen_linktype(ETHERTYPE_IP
);
5040 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5041 b1
->s
.code
= JMP(BPF_JGE
);
5047 b0
= gen_linktype(ETHERTYPE_IPV6
);
5048 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5053 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5058 * generate command for inbound/outbound. It's here so we can
5059 * make it link-type specific. 'dir' = 0 implies "inbound",
5060 * = 1 implies "outbound".
5066 register struct block
*b0
;
5069 * Only some data link types support inbound/outbound qualifiers.
5073 b0
= gen_relation(BPF_JEQ
,
5074 gen_load(Q_LINK
, gen_loadi(0), 1),
5082 * Match packets sent by this machine.
5084 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5087 * Match packets sent to this machine.
5088 * (No broadcast or multicast packets, or
5089 * packets sent to some other machine and
5090 * received promiscuously.)
5092 * XXX - packets sent to other machines probably
5093 * shouldn't be matched, but what about broadcast
5094 * or multicast packets we received?
5096 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5101 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5102 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5105 case DLT_PPP_WITHDIRECTION
:
5107 /* match outgoing packets */
5108 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_OUT
);
5110 /* match incoming packets */
5111 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_IN
);
5115 case DLT_JUNIPER_ATM1
:
5116 case DLT_JUNIPER_ATM2
:
5117 /* juniper flags (including direction) are stored
5118 * the byte after the 3-byte magic number */
5120 /* match outgoing packets */
5121 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5123 /* match incoming packets */
5124 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5129 bpf_error("inbound/outbound not supported on linktype %d",
5137 /* PF firewall log matched interface */
5139 gen_pf_ifname(const char *ifname
)
5144 if (linktype
== DLT_PFLOG
) {
5145 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5146 off
= offsetof(struct pfloghdr
, ifname
);
5148 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5151 if (strlen(ifname
) >= len
) {
5152 bpf_error("ifname interface names can only be %d characters",
5156 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5160 /* PF firewall log matched interface */
5162 gen_pf_ruleset(char *ruleset
)
5166 if (linktype
!= DLT_PFLOG
) {
5167 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5170 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5171 bpf_error("ruleset names can only be %ld characters",
5172 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5175 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5176 strlen(ruleset
), (const u_char
*)ruleset
);
5180 /* PF firewall log rule number */
5186 if (linktype
== DLT_PFLOG
) {
5187 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5190 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5197 /* PF firewall log sub-rule number */
5199 gen_pf_srnr(int srnr
)
5203 if (linktype
!= DLT_PFLOG
) {
5204 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5208 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5213 /* PF firewall log reason code */
5215 gen_pf_reason(int reason
)
5219 if (linktype
== DLT_PFLOG
) {
5220 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5223 bpf_error("reason not supported on linktype 0x%x", linktype
);
5230 /* PF firewall log action */
5232 gen_pf_action(int action
)
5236 if (linktype
== DLT_PFLOG
) {
5237 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5240 bpf_error("action not supported on linktype 0x%x", linktype
);
5249 register const u_char
*eaddr
;
5252 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5253 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5254 return gen_ahostop(eaddr
, (int)q
.dir
);
5256 bpf_error("ARCnet address used in non-arc expression");
5260 static struct block
*
5261 gen_ahostop(eaddr
, dir
)
5262 register const u_char
*eaddr
;
5265 register struct block
*b0
, *b1
;
5268 /* src comes first, different from Ethernet */
5270 return gen_bcmp(0, 1, eaddr
);
5273 return gen_bcmp(1, 1, eaddr
);
5276 b0
= gen_ahostop(eaddr
, Q_SRC
);
5277 b1
= gen_ahostop(eaddr
, Q_DST
);
5283 b0
= gen_ahostop(eaddr
, Q_SRC
);
5284 b1
= gen_ahostop(eaddr
, Q_DST
);
5293 * support IEEE 802.1Q VLAN trunk over ethernet
5302 * Change the offsets to point to the type and data fields within
5303 * the VLAN packet. This is somewhat of a kludge.
5305 if (orig_nl
== (u_int
)-1) {
5306 orig_linktype
= off_linktype
; /* save original values */
5308 orig_nl_nosnap
= off_nl_nosnap
;
5319 bpf_error("no VLAN support for data link type %d",
5325 /* check for VLAN */
5326 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5328 /* If a specific VLAN is requested, check VLAN id */
5329 if (vlan_num
>= 0) {
5332 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5350 * Change the offsets to point to the type and data fields within
5351 * the MPLS packet. This is somewhat of a kludge.
5353 if (orig_nl
== (u_int
)-1) {
5354 orig_linktype
= off_linktype
; /* save original values */
5356 orig_nl_nosnap
= off_nl_nosnap
;
5365 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5373 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5381 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5384 /* FIXME add other DLT_s ...
5385 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5386 * leave it for now */
5389 bpf_error("no MPLS support for data link type %d",
5394 bpf_error("'mpls' can't be combined with 'vlan' or another 'mpls'");
5398 /* If a specific MPLS label is requested, check it */
5399 if (label_num
>= 0) {
5402 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5403 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5412 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5424 bpf_error("'vpi' supported only on raw ATM");
5425 if (off_vpi
== (u_int
)-1)
5427 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5428 (u_int
)jvalue
, reverse
);
5433 bpf_error("'vci' supported only on raw ATM");
5434 if (off_vci
== (u_int
)-1)
5436 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5437 (u_int
)jvalue
, reverse
);
5441 if (off_proto
== (u_int
)-1)
5442 abort(); /* XXX - this isn't on FreeBSD */
5443 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5444 (u_int
)jvalue
, reverse
);
5448 if (off_payload
== (u_int
)-1)
5450 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5451 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5456 bpf_error("'callref' supported only on raw ATM");
5457 if (off_proto
== (u_int
)-1)
5459 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5460 (u_int
)jvalue
, reverse
);
5470 gen_atmtype_abbrev(type
)
5473 struct block
*b0
, *b1
;
5478 /* Get all packets in Meta signalling Circuit */
5480 bpf_error("'metac' supported only on raw ATM");
5481 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5482 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5487 /* Get all packets in Broadcast Circuit*/
5489 bpf_error("'bcc' supported only on raw ATM");
5490 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5491 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5496 /* Get all cells in Segment OAM F4 circuit*/
5498 bpf_error("'oam4sc' supported only on raw ATM");
5499 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5500 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5505 /* Get all cells in End-to-End OAM F4 Circuit*/
5507 bpf_error("'oam4ec' supported only on raw ATM");
5508 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5509 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5514 /* Get all packets in connection Signalling Circuit */
5516 bpf_error("'sc' supported only on raw ATM");
5517 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5518 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5523 /* Get all packets in ILMI Circuit */
5525 bpf_error("'ilmic' supported only on raw ATM");
5526 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5527 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5532 /* Get all LANE packets */
5534 bpf_error("'lane' supported only on raw ATM");
5535 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5538 * Arrange that all subsequent tests assume LANE
5539 * rather than LLC-encapsulated packets, and set
5540 * the offsets appropriately for LANE-encapsulated
5543 * "off_mac" is the offset of the Ethernet header,
5544 * which is 2 bytes past the ATM pseudo-header
5545 * (skipping the pseudo-header and 2-byte LE Client
5546 * field). The other offsets are Ethernet offsets
5547 * relative to "off_mac".
5550 off_mac
= off_payload
+ 2; /* MAC header */
5551 off_linktype
= off_mac
+ 12;
5552 off_nl
= off_mac
+ 14; /* Ethernet II */
5553 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5557 /* Get all LLC-encapsulated packets */
5559 bpf_error("'llc' supported only on raw ATM");
5560 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5571 static struct block
*
5572 gen_msg_abbrev(type
)
5578 * Q.2931 signalling protocol messages for handling virtual circuits
5579 * establishment and teardown
5584 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5588 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5592 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5596 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5600 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5603 case A_RELEASE_DONE
:
5604 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5614 gen_atmmulti_abbrev(type
)
5617 struct block
*b0
, *b1
;
5623 bpf_error("'oam' supported only on raw ATM");
5624 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5629 bpf_error("'oamf4' supported only on raw ATM");
5631 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5632 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5634 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5640 * Get Q.2931 signalling messages for switched
5641 * virtual connection
5644 bpf_error("'connectmsg' supported only on raw ATM");
5645 b0
= gen_msg_abbrev(A_SETUP
);
5646 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5648 b0
= gen_msg_abbrev(A_CONNECT
);
5650 b0
= gen_msg_abbrev(A_CONNECTACK
);
5652 b0
= gen_msg_abbrev(A_RELEASE
);
5654 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5656 b0
= gen_atmtype_abbrev(A_SC
);
5662 bpf_error("'metaconnect' supported only on raw ATM");
5663 b0
= gen_msg_abbrev(A_SETUP
);
5664 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5666 b0
= gen_msg_abbrev(A_CONNECT
);
5668 b0
= gen_msg_abbrev(A_RELEASE
);
5670 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5672 b0
= gen_atmtype_abbrev(A_METAC
);