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.203 2004-03-29 21:04:31 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 offsets. */
101 static u_int orig_linktype
= -1, orig_nl
= -1, orig_nl_nosnap
= -1;
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_linktype(int);
169 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
170 static struct block
*gen_llc(int);
171 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
173 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
175 static struct block
*gen_ahostop(const u_char
*, int);
176 static struct block
*gen_ehostop(const u_char
*, int);
177 static struct block
*gen_fhostop(const u_char
*, int);
178 static struct block
*gen_thostop(const u_char
*, int);
179 static struct block
*gen_wlanhostop(const u_char
*, int);
180 static struct block
*gen_ipfchostop(const u_char
*, int);
181 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
182 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
184 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
187 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
189 static struct block
*gen_ipfrag(void);
190 static struct block
*gen_portatom(int, bpf_int32
);
192 static struct block
*gen_portatom6(int, bpf_int32
);
194 struct block
*gen_portop(int, int, int);
195 static struct block
*gen_port(int, int, int);
197 struct block
*gen_portop6(int, int, int);
198 static struct block
*gen_port6(int, int, int);
200 static int lookup_proto(const char *, int);
201 static struct block
*gen_protochain(int, int, int);
202 static struct block
*gen_proto(int, int, int);
203 static struct slist
*xfer_to_x(struct arth
*);
204 static struct slist
*xfer_to_a(struct arth
*);
205 static struct block
*gen_mac_multicast(int);
206 static struct block
*gen_len(int, int);
208 static struct block
*gen_msg_abbrev(int type
);
219 /* XXX Round up to nearest long. */
220 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
222 /* XXX Round up to structure boundary. */
226 cp
= &chunks
[cur_chunk
];
227 if (n
> cp
->n_left
) {
228 ++cp
, k
= ++cur_chunk
;
230 bpf_error("out of memory");
231 size
= CHUNK0SIZE
<< k
;
232 cp
->m
= (void *)malloc(size
);
234 bpf_error("out of memory");
235 memset((char *)cp
->m
, 0, size
);
238 bpf_error("out of memory");
241 return (void *)((char *)cp
->m
+ cp
->n_left
);
250 for (i
= 0; i
< NCHUNKS
; ++i
)
251 if (chunks
[i
].m
!= NULL
) {
258 * A strdup whose allocations are freed after code generation is over.
262 register const char *s
;
264 int n
= strlen(s
) + 1;
265 char *cp
= newchunk(n
);
271 static inline struct block
*
277 p
= (struct block
*)newchunk(sizeof(*p
));
284 static inline struct slist
*
290 p
= (struct slist
*)newchunk(sizeof(*p
));
296 static struct block
*
300 struct block
*b
= new_block(BPF_RET
|BPF_K
);
309 bpf_error("syntax error in filter expression");
312 static bpf_u_int32 netmask
;
317 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
318 char *buf
, int optimize
, bpf_u_int32 mask
)
327 if (setjmp(top_ctx
)) {
335 snaplen
= pcap_snapshot(p
);
337 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
338 "snaplen of 0 rejects all packets");
342 lex_init(buf
? buf
: "");
343 init_linktype(pcap_datalink(p
));
350 root
= gen_retblk(snaplen
);
352 if (optimize
&& !no_optimize
) {
355 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
356 bpf_error("expression rejects all packets");
358 program
->bf_insns
= icode_to_fcode(root
, &len
);
359 program
->bf_len
= len
;
367 * entry point for using the compiler with no pcap open
368 * pass in all the stuff that is needed explicitly instead.
371 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
372 struct bpf_program
*program
,
373 char *buf
, int optimize
, bpf_u_int32 mask
)
378 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
381 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
387 * Clean up a "struct bpf_program" by freeing all the memory allocated
391 pcap_freecode(struct bpf_program
*program
)
394 if (program
->bf_insns
!= NULL
) {
395 free((char *)program
->bf_insns
);
396 program
->bf_insns
= NULL
;
401 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
402 * which of the jt and jf fields has been resolved and which is a pointer
403 * back to another unresolved block (or nil). At least one of the fields
404 * in each block is already resolved.
407 backpatch(list
, target
)
408 struct block
*list
, *target
;
425 * Merge the lists in b0 and b1, using the 'sense' field to indicate
426 * which of jt and jf is the link.
430 struct block
*b0
, *b1
;
432 register struct block
**p
= &b0
;
434 /* Find end of list. */
436 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
438 /* Concatenate the lists. */
446 backpatch(p
, gen_retblk(snaplen
));
447 p
->sense
= !p
->sense
;
448 backpatch(p
, gen_retblk(0));
454 struct block
*b0
, *b1
;
456 backpatch(b0
, b1
->head
);
457 b0
->sense
= !b0
->sense
;
458 b1
->sense
= !b1
->sense
;
460 b1
->sense
= !b1
->sense
;
466 struct block
*b0
, *b1
;
468 b0
->sense
= !b0
->sense
;
469 backpatch(b0
, b1
->head
);
470 b0
->sense
= !b0
->sense
;
479 b
->sense
= !b
->sense
;
482 static struct block
*
483 gen_cmp(offset
, size
, v
)
490 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
493 b
= new_block(JMP(BPF_JEQ
));
500 static struct block
*
501 gen_cmp_gt(offset
, size
, v
)
508 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
511 b
= new_block(JMP(BPF_JGT
));
518 static struct block
*
519 gen_mcmp(offset
, size
, v
, mask
)
524 struct block
*b
= gen_cmp(offset
, size
, v
);
527 if (mask
!= 0xffffffff) {
528 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
535 static struct block
*
536 gen_bcmp(offset
, size
, v
)
537 register u_int offset
, size
;
538 register const u_char
*v
;
540 register struct block
*b
, *tmp
;
544 register const u_char
*p
= &v
[size
- 4];
545 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
546 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
548 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
555 register const u_char
*p
= &v
[size
- 2];
556 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
558 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
565 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
573 static struct block
*
574 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
575 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
581 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
584 if (mask
!= 0xffffffff) {
585 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
589 b
= new_block(JMP(jtype
));
592 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
598 * Various code constructs need to know the layout of the data link
599 * layer. These variables give the necessary offsets.
603 * This is the offset of the beginning of the MAC-layer header.
604 * It's usually 0, except for ATM LANE.
606 static u_int off_mac
;
609 * "off_linktype" is the offset to information in the link-layer header
610 * giving the packet type.
612 * For Ethernet, it's the offset of the Ethernet type field.
614 * For link-layer types that always use 802.2 headers, it's the
615 * offset of the LLC header.
617 * For PPP, it's the offset of the PPP type field.
619 * For Cisco HDLC, it's the offset of the CHDLC type field.
621 * For BSD loopback, it's the offset of the AF_ value.
623 * For Linux cooked sockets, it's the offset of the type field.
625 * It's set to -1 for no encapsulation, in which case, IP is assumed.
627 static u_int off_linktype
;
630 * TRUE if the link layer includes an ATM pseudo-header.
632 static int is_atm
= 0;
635 * TRUE if "lane" appeared in the filter; it causes us to generate
636 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
638 static int is_lane
= 0;
641 * These are offsets for the ATM pseudo-header.
643 static u_int off_vpi
;
644 static u_int off_vci
;
645 static u_int off_proto
;
648 * This is the offset of the first byte after the ATM pseudo_header,
649 * or -1 if there is no ATM pseudo-header.
651 static u_int off_payload
;
654 * These are offsets to the beginning of the network-layer header.
656 * If the link layer never uses 802.2 LLC:
658 * "off_nl" and "off_nl_nosnap" are the same.
660 * If the link layer always uses 802.2 LLC:
662 * "off_nl" is the offset if there's a SNAP header following
665 * "off_nl_nosnap" is the offset if there's no SNAP header.
667 * If the link layer is Ethernet:
669 * "off_nl" is the offset if the packet is an Ethernet II packet
670 * (we assume no 802.3+802.2+SNAP);
672 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
673 * with an 802.2 header following it.
676 static u_int off_nl_nosnap
;
687 * Assume it's not raw ATM with a pseudo-header, for now.
705 off_nl
= 6; /* XXX in reality, variable! */
706 off_nl_nosnap
= 6; /* no 802.2 LLC */
709 case DLT_ARCNET_LINUX
:
711 off_nl
= 8; /* XXX in reality, variable! */
712 off_nl_nosnap
= 8; /* no 802.2 LLC */
717 off_nl
= 14; /* Ethernet II */
718 off_nl_nosnap
= 17; /* 802.3+802.2 */
723 * SLIP doesn't have a link level type. The 16 byte
724 * header is hacked into our SLIP driver.
728 off_nl_nosnap
= 16; /* no 802.2 LLC */
732 /* XXX this may be the same as the DLT_PPP_BSDOS case */
736 off_nl_nosnap
= 24; /* no 802.2 LLC */
743 off_nl_nosnap
= 4; /* no 802.2 LLC */
749 off_nl_nosnap
= 12; /* no 802.2 LLC */
753 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
754 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
757 off_nl_nosnap
= 4; /* no 802.2 LLC */
762 * This does no include the Ethernet header, and
763 * only covers session state.
767 off_nl_nosnap
= 8; /* no 802.2 LLC */
773 off_nl_nosnap
= 24; /* no 802.2 LLC */
778 * FDDI doesn't really have a link-level type field.
779 * We set "off_linktype" to the offset of the LLC header.
781 * To check for Ethernet types, we assume that SSAP = SNAP
782 * is being used and pick out the encapsulated Ethernet type.
783 * XXX - should we generate code to check for SNAP?
787 off_linktype
+= pcap_fddipad
;
789 off_nl
= 21; /* FDDI+802.2+SNAP */
790 off_nl_nosnap
= 16; /* FDDI+802.2 */
792 off_nl
+= pcap_fddipad
;
793 off_nl_nosnap
+= pcap_fddipad
;
799 * Token Ring doesn't really have a link-level type field.
800 * We set "off_linktype" to the offset of the LLC header.
802 * To check for Ethernet types, we assume that SSAP = SNAP
803 * is being used and pick out the encapsulated Ethernet type.
804 * XXX - should we generate code to check for SNAP?
806 * XXX - the header is actually variable-length.
807 * Some various Linux patched versions gave 38
808 * as "off_linktype" and 40 as "off_nl"; however,
809 * if a token ring packet has *no* routing
810 * information, i.e. is not source-routed, the correct
811 * values are 20 and 22, as they are in the vanilla code.
813 * A packet is source-routed iff the uppermost bit
814 * of the first byte of the source address, at an
815 * offset of 8, has the uppermost bit set. If the
816 * packet is source-routed, the total number of bytes
817 * of routing information is 2 plus bits 0x1F00 of
818 * the 16-bit value at an offset of 14 (shifted right
819 * 8 - figure out which byte that is).
822 off_nl
= 22; /* Token Ring+802.2+SNAP */
823 off_nl_nosnap
= 17; /* Token Ring+802.2 */
828 * 802.11 doesn't really have a link-level type field.
829 * We set "off_linktype" to the offset of the LLC header.
831 * To check for Ethernet types, we assume that SSAP = SNAP
832 * is being used and pick out the encapsulated Ethernet type.
833 * XXX - should we generate code to check for SNAP?
835 * XXX - the header is actually variable-length. We
836 * assume a 24-byte link-layer header, as appears in
837 * data frames in networks with no bridges. If the
838 * fromds and tods 802.11 header bits are both set,
839 * it's actually supposed to be 30 bytes.
842 off_nl
= 32; /* 802.11+802.2+SNAP */
843 off_nl_nosnap
= 27; /* 802.11+802.2 */
846 case DLT_PRISM_HEADER
:
848 * Same as 802.11, but with an additional header before
849 * the 802.11 header, containing a bunch of additional
850 * information including radio-level information.
852 * The header is 144 bytes long.
854 * XXX - same variable-length header problem; at least
855 * the Prism header is fixed-length.
857 off_linktype
= 144+24;
858 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
859 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
862 case DLT_IEEE802_11_RADIO_AVS
:
864 * Same as 802.11, but with an additional header before
865 * the 802.11 header, containing a bunch of additional
866 * information including radio-level information.
868 * The header is 64 bytes long, at least in its
869 * current incarnation.
871 * XXX - same variable-length header problem, only
872 * more so; this header is also variable-length,
873 * with the length being the 32-bit big-endian
874 * number at an offset of 4 from the beginning
875 * of the radio header.
877 off_linktype
= 64+24;
878 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
879 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
882 case DLT_IEEE802_11_RADIO
:
884 * Same as 802.11, but with an additional header before
885 * the 802.11 header, containing a bunch of additional
886 * information including radio-level information.
888 * XXX - same variable-length header problem, only
889 * even *more* so; this header is also variable-length,
890 * with the length being the 16-bit number at an offset
891 * of 2 from the beginning of the radio header, and it's
892 * device-dependent (different devices might supply
893 * different amounts of information), so we can't even
894 * assume a fixed length for the current version of the
897 * Therefore, currently, only raw "link[N:M]" filtering is
905 case DLT_ATM_RFC1483
:
906 case DLT_ATM_CLIP
: /* Linux ATM defines this */
908 * assume routed, non-ISO PDUs
909 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
912 off_nl
= 8; /* 802.2+SNAP */
913 off_nl_nosnap
= 3; /* 802.2 */
918 * Full Frontal ATM; you get AALn PDUs with an ATM
922 off_vpi
= SUNATM_VPI_POS
;
923 off_vci
= SUNATM_VCI_POS
;
924 off_proto
= PROTO_POS
;
925 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
926 off_payload
= SUNATM_PKT_BEGIN_POS
;
927 off_linktype
= off_payload
;
928 off_nl
= off_payload
+8; /* 802.2+SNAP */
929 off_nl_nosnap
= off_payload
+3; /* 802.2 */
935 off_nl_nosnap
= 0; /* no 802.2 LLC */
938 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
941 off_nl_nosnap
= 16; /* no 802.2 LLC */
946 * LocalTalk does have a 1-byte type field in the LLAP header,
947 * but really it just indicates whether there is a "short" or
948 * "long" DDP packet following.
952 off_nl_nosnap
= 0; /* no 802.2 LLC */
957 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
958 * link-level type field. We set "off_linktype" to the
959 * offset of the LLC header.
961 * To check for Ethernet types, we assume that SSAP = SNAP
962 * is being used and pick out the encapsulated Ethernet type.
963 * XXX - should we generate code to check for SNAP? RFC
964 * 2625 says SNAP should be used.
967 off_nl
= 24; /* IPFC+802.2+SNAP */
968 off_nl_nosnap
= 19; /* IPFC+802.2 */
973 * XXX - we should set this to handle SNAP-encapsulated
974 * frames (NLPID of 0x80).
978 off_nl_nosnap
= 0; /* no 802.2 LLC */
981 case DLT_APPLE_IP_OVER_IEEE1394
:
984 off_nl_nosnap
= 0; /* no 802.2 LLC */
989 * Currently, only raw "link[N:M]" filtering is supported.
998 * Currently, only raw "link[N:M]" filtering is supported.
1005 case DLT_SYMANTEC_FIREWALL
:
1007 off_nl
= 44; /* Ethernet II */
1008 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1013 /* XXX read from header? */
1014 off_nl
= PFLOG_HDRLEN
;
1015 off_nl_nosnap
= PFLOG_HDRLEN
;
1026 bpf_error("unknown data link type %d", linktype
);
1030 static struct block
*
1037 s
= new_stmt(BPF_LD
|BPF_IMM
);
1039 b
= new_block(JMP(BPF_JEQ
));
1045 static inline struct block
*
1048 return gen_uncond(1);
1051 static inline struct block
*
1054 return gen_uncond(0);
1058 * Byte-swap a 32-bit number.
1059 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1060 * big-endian platforms.)
1062 #define SWAPLONG(y) \
1063 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1065 static struct block
*
1066 gen_ether_linktype(proto
)
1069 struct block
*b0
, *b1
;
1075 * OSI protocols always use 802.2 encapsulation.
1076 * XXX - should we check both the DSAP and the
1077 * SSAP, like this, or should we check just the
1080 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1082 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1083 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1088 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1090 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1091 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1095 case LLCSAP_NETBEUI
:
1097 * NetBEUI always uses 802.2 encapsulation.
1098 * XXX - should we check both the DSAP and the
1099 * SSAP, like this, or should we check just the
1102 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1104 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1105 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1113 * Ethernet_II frames, which are Ethernet
1114 * frames with a frame type of ETHERTYPE_IPX;
1116 * Ethernet_802.3 frames, which are 802.3
1117 * frames (i.e., the type/length field is
1118 * a length field, <= ETHERMTU, rather than
1119 * a type field) with the first two bytes
1120 * after the Ethernet/802.3 header being
1123 * Ethernet_802.2 frames, which are 802.3
1124 * frames with an 802.2 LLC header and
1125 * with the IPX LSAP as the DSAP in the LLC
1128 * Ethernet_SNAP frames, which are 802.3
1129 * frames with an LLC header and a SNAP
1130 * header and with an OUI of 0x000000
1131 * (encapsulated Ethernet) and a protocol
1132 * ID of ETHERTYPE_IPX in the SNAP header.
1134 * XXX - should we generate the same code both
1135 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1139 * This generates code to check both for the
1140 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1142 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1143 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1147 * Now we add code to check for SNAP frames with
1148 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1150 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1154 * Now we generate code to check for 802.3
1155 * frames in general.
1157 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1161 * Now add the check for 802.3 frames before the
1162 * check for Ethernet_802.2 and Ethernet_802.3,
1163 * as those checks should only be done on 802.3
1164 * frames, not on Ethernet frames.
1169 * Now add the check for Ethernet_II frames, and
1170 * do that before checking for the other frame
1173 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1177 case ETHERTYPE_ATALK
:
1178 case ETHERTYPE_AARP
:
1180 * EtherTalk (AppleTalk protocols on Ethernet link
1181 * layer) may use 802.2 encapsulation.
1185 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1186 * we check for an Ethernet type field less than
1187 * 1500, which means it's an 802.3 length field.
1189 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1193 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1194 * SNAP packets with an organization code of
1195 * 0x080007 (Apple, for Appletalk) and a protocol
1196 * type of ETHERTYPE_ATALK (Appletalk).
1198 * 802.2-encapsulated ETHERTYPE_AARP packets are
1199 * SNAP packets with an organization code of
1200 * 0x000000 (encapsulated Ethernet) and a protocol
1201 * type of ETHERTYPE_AARP (Appletalk ARP).
1203 if (proto
== ETHERTYPE_ATALK
)
1204 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1205 else /* proto == ETHERTYPE_AARP */
1206 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1210 * Check for Ethernet encapsulation (Ethertalk
1211 * phase 1?); we just check for the Ethernet
1214 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1220 if (proto
<= ETHERMTU
) {
1222 * This is an LLC SAP value, so the frames
1223 * that match would be 802.2 frames.
1224 * Check that the frame is an 802.2 frame
1225 * (i.e., that the length/type field is
1226 * a length field, <= ETHERMTU) and
1227 * then check the DSAP.
1229 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1231 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1236 * This is an Ethernet type, so compare
1237 * the length/type field with it (if
1238 * the frame is an 802.2 frame, the length
1239 * field will be <= ETHERMTU, and, as
1240 * "proto" is > ETHERMTU, this test
1241 * will fail and the frame won't match,
1242 * which is what we want).
1244 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1249 static struct block
*
1253 struct block
*b0
, *b1
, *b2
;
1258 return gen_ether_linktype(proto
);
1265 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1269 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1274 case DLT_IEEE802_11
:
1275 case DLT_PRISM_HEADER
:
1276 case DLT_IEEE802_11_RADIO
:
1279 case DLT_ATM_RFC1483
:
1281 case DLT_IP_OVER_FC
:
1282 return gen_llc(proto
);
1287 * If "is_lane" is set, check for a LANE-encapsulated
1288 * version of this protocol, otherwise check for an
1289 * LLC-encapsulated version of this protocol.
1291 * We assume LANE means Ethernet, not Token Ring.
1295 * Check that the packet doesn't begin with an
1296 * LE Control marker. (We've already generated
1299 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1303 * Now generate an Ethernet test.
1305 b1
= gen_ether_linktype(proto
);
1310 * Check for LLC encapsulation and then check the
1313 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1314 b1
= gen_llc(proto
);
1323 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1324 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1325 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1331 * OSI protocols always use 802.2 encapsulation.
1332 * XXX - should we check both the DSAP and the
1333 * LSAP, like this, or should we check just the
1336 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1337 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1338 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1342 case LLCSAP_NETBEUI
:
1344 * NetBEUI always uses 802.2 encapsulation.
1345 * XXX - should we check both the DSAP and the
1346 * LSAP, like this, or should we check just the
1349 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1350 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1351 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1357 * Ethernet_II frames, which are Ethernet
1358 * frames with a frame type of ETHERTYPE_IPX;
1360 * Ethernet_802.3 frames, which have a frame
1361 * type of LINUX_SLL_P_802_3;
1363 * Ethernet_802.2 frames, which are 802.3
1364 * frames with an 802.2 LLC header (i.e, have
1365 * a frame type of LINUX_SLL_P_802_2) and
1366 * with the IPX LSAP as the DSAP in the LLC
1369 * Ethernet_SNAP frames, which are 802.3
1370 * frames with an LLC header and a SNAP
1371 * header and with an OUI of 0x000000
1372 * (encapsulated Ethernet) and a protocol
1373 * ID of ETHERTYPE_IPX in the SNAP header.
1375 * First, do the checks on LINUX_SLL_P_802_2
1376 * frames; generate the check for either
1377 * Ethernet_802.2 or Ethernet_SNAP frames, and
1378 * then put a check for LINUX_SLL_P_802_2 frames
1381 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1382 (bpf_int32
)LLCSAP_IPX
);
1383 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1386 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1390 * Now check for 802.3 frames and OR that with
1391 * the previous test.
1393 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1397 * Now add the check for Ethernet_II frames, and
1398 * do that before checking for the other frame
1401 b0
= gen_cmp(off_linktype
, BPF_H
,
1402 (bpf_int32
)ETHERTYPE_IPX
);
1406 case ETHERTYPE_ATALK
:
1407 case ETHERTYPE_AARP
:
1409 * EtherTalk (AppleTalk protocols on Ethernet link
1410 * layer) may use 802.2 encapsulation.
1414 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1415 * we check for the 802.2 protocol type in the
1416 * "Ethernet type" field.
1418 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1421 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1422 * SNAP packets with an organization code of
1423 * 0x080007 (Apple, for Appletalk) and a protocol
1424 * type of ETHERTYPE_ATALK (Appletalk).
1426 * 802.2-encapsulated ETHERTYPE_AARP packets are
1427 * SNAP packets with an organization code of
1428 * 0x000000 (encapsulated Ethernet) and a protocol
1429 * type of ETHERTYPE_AARP (Appletalk ARP).
1431 if (proto
== ETHERTYPE_ATALK
)
1432 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1434 else /* proto == ETHERTYPE_AARP */
1435 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1440 * Check for Ethernet encapsulation (Ethertalk
1441 * phase 1?); we just check for the Ethernet
1444 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1450 if (proto
<= ETHERMTU
) {
1452 * This is an LLC SAP value, so the frames
1453 * that match would be 802.2 frames.
1454 * Check for the 802.2 protocol type
1455 * in the "Ethernet type" field, and
1456 * then check the DSAP.
1458 b0
= gen_cmp(off_linktype
, BPF_H
,
1460 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1466 * This is an Ethernet type, so compare
1467 * the length/type field with it (if
1468 * the frame is an 802.2 frame, the length
1469 * field will be <= ETHERMTU, and, as
1470 * "proto" is > ETHERMTU, this test
1471 * will fail and the frame won't match,
1472 * which is what we want).
1474 return gen_cmp(off_linktype
, BPF_H
,
1481 case DLT_SLIP_BSDOS
:
1484 * These types don't provide any type field; packets
1487 * XXX - for IPv4, check for a version number of 4, and,
1488 * for IPv6, check for a version number of 6?
1494 case ETHERTYPE_IPV6
:
1496 return gen_true(); /* always true */
1499 return gen_false(); /* always false */
1504 case DLT_PPP_SERIAL
:
1507 * We use Ethernet protocol types inside libpcap;
1508 * map them to the corresponding PPP protocol types.
1517 case ETHERTYPE_IPV6
:
1526 case ETHERTYPE_ATALK
:
1540 * I'm assuming the "Bridging PDU"s that go
1541 * over PPP are Spanning Tree Protocol
1555 * We use Ethernet protocol types inside libpcap;
1556 * map them to the corresponding PPP protocol types.
1561 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1562 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1564 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1569 case ETHERTYPE_IPV6
:
1579 case ETHERTYPE_ATALK
:
1593 * I'm assuming the "Bridging PDU"s that go
1594 * over PPP are Spanning Tree Protocol
1610 * For DLT_NULL, the link-layer header is a 32-bit
1611 * word containing an AF_ value in *host* byte order,
1612 * and for DLT_ENC, the link-layer header begins
1613 * with a 32-bit work containing an AF_ value in
1616 * In addition, if we're reading a saved capture file,
1617 * the host byte order in the capture may not be the
1618 * same as the host byte order on this machine.
1620 * For DLT_LOOP, the link-layer header is a 32-bit
1621 * word containing an AF_ value in *network* byte order.
1623 * XXX - AF_ values may, unfortunately, be platform-
1624 * dependent; for example, FreeBSD's AF_INET6 is 24
1625 * whilst NetBSD's and OpenBSD's is 26.
1627 * This means that, when reading a capture file, just
1628 * checking for our AF_INET6 value won't work if the
1629 * capture file came from another OS.
1638 case ETHERTYPE_IPV6
:
1645 * Not a type on which we support filtering.
1646 * XXX - support those that have AF_ values
1647 * #defined on this platform, at least?
1652 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1654 * The AF_ value is in host byte order, but
1655 * the BPF interpreter will convert it to
1656 * network byte order.
1658 * If this is a save file, and it's from a
1659 * machine with the opposite byte order to
1660 * ours, we byte-swap the AF_ value.
1662 * Then we run it through "htonl()", and
1663 * generate code to compare against the result.
1665 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1666 bpf_pcap
->sf
.swapped
)
1667 proto
= SWAPLONG(proto
);
1668 proto
= htonl(proto
);
1670 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1674 * af field is host byte order in contrast to the rest of
1677 if (proto
== ETHERTYPE_IP
)
1678 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1679 (bpf_int32
)AF_INET
));
1681 else if (proto
== ETHERTYPE_IPV6
)
1682 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1683 (bpf_int32
)AF_INET6
));
1690 case DLT_ARCNET_LINUX
:
1692 * XXX should we check for first fragment if the protocol
1701 case ETHERTYPE_IPV6
:
1702 return (gen_cmp(off_linktype
, BPF_B
,
1703 (bpf_int32
)ARCTYPE_INET6
));
1707 b0
= gen_cmp(off_linktype
, BPF_B
,
1708 (bpf_int32
)ARCTYPE_IP
);
1709 b1
= gen_cmp(off_linktype
, BPF_B
,
1710 (bpf_int32
)ARCTYPE_IP_OLD
);
1715 b0
= gen_cmp(off_linktype
, BPF_B
,
1716 (bpf_int32
)ARCTYPE_ARP
);
1717 b1
= gen_cmp(off_linktype
, BPF_B
,
1718 (bpf_int32
)ARCTYPE_ARP_OLD
);
1722 case ETHERTYPE_REVARP
:
1723 return (gen_cmp(off_linktype
, BPF_B
,
1724 (bpf_int32
)ARCTYPE_REVARP
));
1726 case ETHERTYPE_ATALK
:
1727 return (gen_cmp(off_linktype
, BPF_B
,
1728 (bpf_int32
)ARCTYPE_ATALK
));
1734 case ETHERTYPE_ATALK
:
1743 * XXX - assumes a 2-byte Frame Relay header with
1744 * DLCI and flags. What if the address is longer?
1750 * Check for the special NLPID for IP.
1752 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1755 case ETHERTYPE_IPV6
:
1757 * Check for the special NLPID for IPv6.
1759 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1764 * Check for several OSI protocols.
1766 * Frame Relay packets typically have an OSI
1767 * NLPID at the beginning; we check for each
1770 * What we check for is the NLPID and a frame
1771 * control field of UI, i.e. 0x03 followed
1774 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1775 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1776 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1786 case DLT_LINUX_IRDA
:
1787 bpf_error("IrDA link-layer type filtering not implemented");
1790 bpf_error("DOCSIS link-layer type filtering not implemented");
1794 * All the types that have no encapsulation should either be
1795 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1796 * all packets are IP packets, or should be handled in some
1797 * special case, if none of them are (if some are and some
1798 * aren't, the lack of encapsulation is a problem, as we'd
1799 * have to find some other way of determining the packet type).
1801 * Therefore, if "off_linktype" is -1, there's an error.
1803 if (off_linktype
== (u_int
)-1)
1807 * Any type not handled above should always have an Ethernet
1808 * type at an offset of "off_linktype". (PPP is partially
1809 * handled above - the protocol type is mapped from the
1810 * Ethernet and LLC types we use internally to the corresponding
1811 * PPP type - but the PPP type is always specified by a value
1812 * at "off_linktype", so we don't have to do the code generation
1815 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1819 * Check for an LLC SNAP packet with a given organization code and
1820 * protocol type; we check the entire contents of the 802.2 LLC and
1821 * snap headers, checking for DSAP and SSAP of SNAP and a control
1822 * field of 0x03 in the LLC header, and for the specified organization
1823 * code and protocol type in the SNAP header.
1825 static struct block
*
1826 gen_snap(orgcode
, ptype
, offset
)
1827 bpf_u_int32 orgcode
;
1831 u_char snapblock
[8];
1833 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1834 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1835 snapblock
[2] = 0x03; /* control = UI */
1836 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1837 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1838 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1839 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1840 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1841 return gen_bcmp(offset
, 8, snapblock
);
1845 * Check for a given protocol value assuming an 802.2 LLC header.
1847 static struct block
*
1852 * XXX - handle token-ring variable-length header.
1857 return gen_cmp(off_linktype
, BPF_H
, (long)
1858 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1861 return gen_cmp(off_linktype
, BPF_H
, (long)
1862 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1864 case LLCSAP_NETBEUI
:
1865 return gen_cmp(off_linktype
, BPF_H
, (long)
1866 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1870 * XXX - are there ever SNAP frames for IPX on
1871 * non-Ethernet 802.x networks?
1873 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1875 case ETHERTYPE_ATALK
:
1877 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1878 * SNAP packets with an organization code of
1879 * 0x080007 (Apple, for Appletalk) and a protocol
1880 * type of ETHERTYPE_ATALK (Appletalk).
1882 * XXX - check for an organization code of
1883 * encapsulated Ethernet as well?
1885 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1889 * XXX - we don't have to check for IPX 802.3
1890 * here, but should we check for the IPX Ethertype?
1892 if (proto
<= ETHERMTU
) {
1894 * This is an LLC SAP value, so check
1897 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1900 * This is an Ethernet type; we assume that it's
1901 * unlikely that it'll appear in the right place
1902 * at random, and therefore check only the
1903 * location that would hold the Ethernet type
1904 * in a SNAP frame with an organization code of
1905 * 0x000000 (encapsulated Ethernet).
1907 * XXX - if we were to check for the SNAP DSAP and
1908 * LSAP, as per XXX, and were also to check for an
1909 * organization code of 0x000000 (encapsulated
1910 * Ethernet), we'd do
1912 * return gen_snap(0x000000, proto,
1915 * here; for now, we don't, as per the above.
1916 * I don't know whether it's worth the extra CPU
1917 * time to do the right check or not.
1919 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1924 static struct block
*
1925 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1929 u_int src_off
, dst_off
;
1931 struct block
*b0
, *b1
;
1945 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1946 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1952 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1953 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1960 b0
= gen_linktype(proto
);
1961 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1967 static struct block
*
1968 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1969 struct in6_addr
*addr
;
1970 struct in6_addr
*mask
;
1972 u_int src_off
, dst_off
;
1974 struct block
*b0
, *b1
;
1989 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1990 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1996 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1997 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2004 /* this order is important */
2005 a
= (u_int32_t
*)addr
;
2006 m
= (u_int32_t
*)mask
;
2007 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2008 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2010 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2012 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2014 b0
= gen_linktype(proto
);
2020 static struct block
*
2021 gen_ehostop(eaddr
, dir
)
2022 register const u_char
*eaddr
;
2025 register struct block
*b0
, *b1
;
2029 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2032 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2035 b0
= gen_ehostop(eaddr
, Q_SRC
);
2036 b1
= gen_ehostop(eaddr
, Q_DST
);
2042 b0
= gen_ehostop(eaddr
, Q_SRC
);
2043 b1
= gen_ehostop(eaddr
, Q_DST
);
2052 * Like gen_ehostop, but for DLT_FDDI
2054 static struct block
*
2055 gen_fhostop(eaddr
, dir
)
2056 register const u_char
*eaddr
;
2059 struct block
*b0
, *b1
;
2064 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2066 return gen_bcmp(6 + 1, 6, eaddr
);
2071 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2073 return gen_bcmp(0 + 1, 6, eaddr
);
2077 b0
= gen_fhostop(eaddr
, Q_SRC
);
2078 b1
= gen_fhostop(eaddr
, Q_DST
);
2084 b0
= gen_fhostop(eaddr
, Q_SRC
);
2085 b1
= gen_fhostop(eaddr
, Q_DST
);
2094 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2096 static struct block
*
2097 gen_thostop(eaddr
, dir
)
2098 register const u_char
*eaddr
;
2101 register struct block
*b0
, *b1
;
2105 return gen_bcmp(8, 6, eaddr
);
2108 return gen_bcmp(2, 6, eaddr
);
2111 b0
= gen_thostop(eaddr
, Q_SRC
);
2112 b1
= gen_thostop(eaddr
, Q_DST
);
2118 b0
= gen_thostop(eaddr
, Q_SRC
);
2119 b1
= gen_thostop(eaddr
, Q_DST
);
2128 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2130 static struct block
*
2131 gen_wlanhostop(eaddr
, dir
)
2132 register const u_char
*eaddr
;
2135 register struct block
*b0
, *b1
, *b2
;
2136 register struct slist
*s
;
2143 * For control frames, there is no SA.
2145 * For management frames, SA is at an
2146 * offset of 10 from the beginning of
2149 * For data frames, SA is at an offset
2150 * of 10 from the beginning of the packet
2151 * if From DS is clear, at an offset of
2152 * 16 from the beginning of the packet
2153 * if From DS is set and To DS is clear,
2154 * and an offset of 24 from the beginning
2155 * of the packet if From DS is set and To DS
2160 * Generate the tests to be done for data frames
2163 * First, check for To DS set, i.e. check "link[1] & 0x01".
2165 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2167 b1
= new_block(JMP(BPF_JSET
));
2168 b1
->s
.k
= 0x01; /* To DS */
2172 * If To DS is set, the SA is at 24.
2174 b0
= gen_bcmp(24, 6, eaddr
);
2178 * Now, check for To DS not set, i.e. check
2179 * "!(link[1] & 0x01)".
2181 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2183 b2
= new_block(JMP(BPF_JSET
));
2184 b2
->s
.k
= 0x01; /* To DS */
2189 * If To DS is not set, the SA is at 16.
2191 b1
= gen_bcmp(16, 6, eaddr
);
2195 * Now OR together the last two checks. That gives
2196 * the complete set of checks for data frames with
2202 * Now check for From DS being set, and AND that with
2203 * the ORed-together checks.
2205 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2207 b1
= new_block(JMP(BPF_JSET
));
2208 b1
->s
.k
= 0x02; /* From DS */
2213 * Now check for data frames with From DS not set.
2215 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2217 b2
= new_block(JMP(BPF_JSET
));
2218 b2
->s
.k
= 0x02; /* From DS */
2223 * If From DS isn't set, the SA is at 10.
2225 b1
= gen_bcmp(10, 6, eaddr
);
2229 * Now OR together the checks for data frames with
2230 * From DS not set and for data frames with From DS
2231 * set; that gives the checks done for data frames.
2236 * Now check for a data frame.
2237 * I.e, check "link[0] & 0x08".
2239 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2241 b1
= new_block(JMP(BPF_JSET
));
2246 * AND that with the checks done for data frames.
2251 * If the high-order bit of the type value is 0, this
2252 * is a management frame.
2253 * I.e, check "!(link[0] & 0x08)".
2255 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2257 b2
= new_block(JMP(BPF_JSET
));
2263 * For management frames, the SA is at 10.
2265 b1
= gen_bcmp(10, 6, eaddr
);
2269 * OR that with the checks done for data frames.
2270 * That gives the checks done for management and
2276 * If the low-order bit of the type value is 1,
2277 * this is either a control frame or a frame
2278 * with a reserved type, and thus not a
2281 * I.e., check "!(link[0] & 0x04)".
2283 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2285 b1
= new_block(JMP(BPF_JSET
));
2291 * AND that with the checks for data and management
2301 * For control frames, there is no DA.
2303 * For management frames, DA is at an
2304 * offset of 4 from the beginning of
2307 * For data frames, DA is at an offset
2308 * of 4 from the beginning of the packet
2309 * if To DS is clear and at an offset of
2310 * 16 from the beginning of the packet
2315 * Generate the tests to be done for data frames.
2317 * First, check for To DS set, i.e. "link[1] & 0x01".
2319 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2321 b1
= new_block(JMP(BPF_JSET
));
2322 b1
->s
.k
= 0x01; /* To DS */
2326 * If To DS is set, the DA is at 16.
2328 b0
= gen_bcmp(16, 6, eaddr
);
2332 * Now, check for To DS not set, i.e. check
2333 * "!(link[1] & 0x01)".
2335 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2337 b2
= new_block(JMP(BPF_JSET
));
2338 b2
->s
.k
= 0x01; /* To DS */
2343 * If To DS is not set, the DA is at 4.
2345 b1
= gen_bcmp(4, 6, eaddr
);
2349 * Now OR together the last two checks. That gives
2350 * the complete set of checks for data frames.
2355 * Now check for a data frame.
2356 * I.e, check "link[0] & 0x08".
2358 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2360 b1
= new_block(JMP(BPF_JSET
));
2365 * AND that with the checks done for data frames.
2370 * If the high-order bit of the type value is 0, this
2371 * is a management frame.
2372 * I.e, check "!(link[0] & 0x08)".
2374 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2376 b2
= new_block(JMP(BPF_JSET
));
2382 * For management frames, the DA is at 4.
2384 b1
= gen_bcmp(4, 6, eaddr
);
2388 * OR that with the checks done for data frames.
2389 * That gives the checks done for management and
2395 * If the low-order bit of the type value is 1,
2396 * this is either a control frame or a frame
2397 * with a reserved type, and thus not a
2400 * I.e., check "!(link[0] & 0x04)".
2402 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2404 b1
= new_block(JMP(BPF_JSET
));
2410 * AND that with the checks for data and management
2417 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2418 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2424 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2425 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2434 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2435 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2436 * as the RFC states.)
2438 static struct block
*
2439 gen_ipfchostop(eaddr
, dir
)
2440 register const u_char
*eaddr
;
2443 register struct block
*b0
, *b1
;
2447 return gen_bcmp(10, 6, eaddr
);
2450 return gen_bcmp(2, 6, eaddr
);
2453 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2454 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2460 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2461 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2470 * This is quite tricky because there may be pad bytes in front of the
2471 * DECNET header, and then there are two possible data packet formats that
2472 * carry both src and dst addresses, plus 5 packet types in a format that
2473 * carries only the src node, plus 2 types that use a different format and
2474 * also carry just the src node.
2478 * Instead of doing those all right, we just look for data packets with
2479 * 0 or 1 bytes of padding. If you want to look at other packets, that
2480 * will require a lot more hacking.
2482 * To add support for filtering on DECNET "areas" (network numbers)
2483 * one would want to add a "mask" argument to this routine. That would
2484 * make the filter even more inefficient, although one could be clever
2485 * and not generate masking instructions if the mask is 0xFFFF.
2487 static struct block
*
2488 gen_dnhostop(addr
, dir
, base_off
)
2493 struct block
*b0
, *b1
, *b2
, *tmp
;
2494 u_int offset_lh
; /* offset if long header is received */
2495 u_int offset_sh
; /* offset if short header is received */
2500 offset_sh
= 1; /* follows flags */
2501 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2505 offset_sh
= 3; /* follows flags, dstnode */
2506 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2510 /* Inefficient because we do our Calvinball dance twice */
2511 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2512 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2518 /* Inefficient because we do our Calvinball dance twice */
2519 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2520 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2525 bpf_error("ISO host filtering not implemented");
2530 b0
= gen_linktype(ETHERTYPE_DN
);
2531 /* Check for pad = 1, long header case */
2532 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2533 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2534 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2535 BPF_H
, (bpf_int32
)ntohs(addr
));
2537 /* Check for pad = 0, long header case */
2538 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2539 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2542 /* Check for pad = 1, short header case */
2543 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2544 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2545 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2546 BPF_H
, (bpf_int32
)ntohs(addr
));
2549 /* Check for pad = 0, short header case */
2550 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2551 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2555 /* Combine with test for linktype */
2560 static struct block
*
2561 gen_host(addr
, mask
, proto
, dir
)
2567 struct block
*b0
, *b1
;
2572 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2573 if (off_linktype
!= (u_int
)-1) {
2574 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2576 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2582 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2583 off_nl
+ 12, off_nl
+ 16);
2586 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2587 off_nl
+ 14, off_nl
+ 24);
2590 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2591 off_nl
+ 14, off_nl
+ 24);
2594 bpf_error("'tcp' modifier applied to host");
2597 bpf_error("'sctp' modifier applied to host");
2600 bpf_error("'udp' modifier applied to host");
2603 bpf_error("'icmp' modifier applied to host");
2606 bpf_error("'igmp' modifier applied to host");
2609 bpf_error("'igrp' modifier applied to host");
2612 bpf_error("'pim' modifier applied to host");
2615 bpf_error("'vrrp' modifier applied to host");
2618 bpf_error("ATALK host filtering not implemented");
2621 bpf_error("AARP host filtering not implemented");
2624 return gen_dnhostop(addr
, dir
, off_nl
);
2627 bpf_error("SCA host filtering not implemented");
2630 bpf_error("LAT host filtering not implemented");
2633 bpf_error("MOPDL host filtering not implemented");
2636 bpf_error("MOPRC host filtering not implemented");
2640 bpf_error("'ip6' modifier applied to ip host");
2643 bpf_error("'icmp6' modifier applied to host");
2647 bpf_error("'ah' modifier applied to host");
2650 bpf_error("'esp' modifier applied to host");
2653 bpf_error("ISO host filtering not implemented");
2656 bpf_error("'esis' modifier applied to host");
2659 bpf_error("'isis' modifier applied to host");
2662 bpf_error("'clnp' modifier applied to host");
2665 bpf_error("'stp' modifier applied to host");
2668 bpf_error("IPX host filtering not implemented");
2671 bpf_error("'netbeui' modifier applied to host");
2680 static struct block
*
2681 gen_host6(addr
, mask
, proto
, dir
)
2682 struct in6_addr
*addr
;
2683 struct in6_addr
*mask
;
2690 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2693 bpf_error("'ip' modifier applied to ip6 host");
2696 bpf_error("'rarp' modifier applied to ip6 host");
2699 bpf_error("'arp' modifier applied to ip6 host");
2702 bpf_error("'sctp' modifier applied to host");
2705 bpf_error("'tcp' modifier applied to host");
2708 bpf_error("'udp' modifier applied to host");
2711 bpf_error("'icmp' modifier applied to host");
2714 bpf_error("'igmp' modifier applied to host");
2717 bpf_error("'igrp' modifier applied to host");
2720 bpf_error("'pim' modifier applied to host");
2723 bpf_error("'vrrp' modifier applied to host");
2726 bpf_error("ATALK host filtering not implemented");
2729 bpf_error("AARP host filtering not implemented");
2732 bpf_error("'decnet' modifier applied to ip6 host");
2735 bpf_error("SCA host filtering not implemented");
2738 bpf_error("LAT host filtering not implemented");
2741 bpf_error("MOPDL host filtering not implemented");
2744 bpf_error("MOPRC host filtering not implemented");
2747 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2748 off_nl
+ 8, off_nl
+ 24);
2751 bpf_error("'icmp6' modifier applied to host");
2754 bpf_error("'ah' modifier applied to host");
2757 bpf_error("'esp' modifier applied to host");
2760 bpf_error("ISO host filtering not implemented");
2763 bpf_error("'esis' modifier applied to host");
2766 bpf_error("'isis' modifier applied to host");
2769 bpf_error("'clnp' modifier applied to host");
2772 bpf_error("'stp' modifier applied to host");
2775 bpf_error("IPX host filtering not implemented");
2778 bpf_error("'netbeui' modifier applied to host");
2788 static struct block
*
2789 gen_gateway(eaddr
, alist
, proto
, dir
)
2790 const u_char
*eaddr
;
2791 bpf_u_int32
**alist
;
2795 struct block
*b0
, *b1
, *tmp
;
2798 bpf_error("direction applied to 'gateway'");
2805 if (linktype
== DLT_EN10MB
)
2806 b0
= gen_ehostop(eaddr
, Q_OR
);
2807 else if (linktype
== DLT_FDDI
)
2808 b0
= gen_fhostop(eaddr
, Q_OR
);
2809 else if (linktype
== DLT_IEEE802
)
2810 b0
= gen_thostop(eaddr
, Q_OR
);
2811 else if (linktype
== DLT_IEEE802_11
)
2812 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2813 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2815 * Check that the packet doesn't begin with an
2816 * LE Control marker. (We've already generated
2819 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2823 * Now check the MAC address.
2825 b0
= gen_ehostop(eaddr
, Q_OR
);
2827 } else if (linktype
== DLT_IP_OVER_FC
)
2828 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2831 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2833 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2835 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2843 bpf_error("illegal modifier of 'gateway'");
2849 gen_proto_abbrev(proto
)
2858 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2860 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2866 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2868 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2874 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2876 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2882 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2885 #ifndef IPPROTO_IGMP
2886 #define IPPROTO_IGMP 2
2890 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2893 #ifndef IPPROTO_IGRP
2894 #define IPPROTO_IGRP 9
2897 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2901 #define IPPROTO_PIM 103
2905 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2907 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2912 #ifndef IPPROTO_VRRP
2913 #define IPPROTO_VRRP 112
2917 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2921 b1
= gen_linktype(ETHERTYPE_IP
);
2925 b1
= gen_linktype(ETHERTYPE_ARP
);
2929 b1
= gen_linktype(ETHERTYPE_REVARP
);
2933 bpf_error("link layer applied in wrong context");
2936 b1
= gen_linktype(ETHERTYPE_ATALK
);
2940 b1
= gen_linktype(ETHERTYPE_AARP
);
2944 b1
= gen_linktype(ETHERTYPE_DN
);
2948 b1
= gen_linktype(ETHERTYPE_SCA
);
2952 b1
= gen_linktype(ETHERTYPE_LAT
);
2956 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2960 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2965 b1
= gen_linktype(ETHERTYPE_IPV6
);
2968 #ifndef IPPROTO_ICMPV6
2969 #define IPPROTO_ICMPV6 58
2972 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2977 #define IPPROTO_AH 51
2980 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2982 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2988 #define IPPROTO_ESP 50
2991 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2993 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2999 b1
= gen_linktype(LLCSAP_ISONS
);
3003 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3007 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3010 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3011 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3012 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3014 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3016 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3018 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3022 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3023 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3024 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3026 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3028 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3030 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3034 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3035 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3036 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3038 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3043 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3044 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3049 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3050 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3052 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3054 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3059 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3060 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3065 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3066 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3071 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3075 b1
= gen_linktype(LLCSAP_8021D
);
3079 b1
= gen_linktype(LLCSAP_IPX
);
3083 b1
= gen_linktype(LLCSAP_NETBEUI
);
3092 static struct block
*
3099 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3100 s
->s
.k
= off_nl
+ 6;
3101 b
= new_block(JMP(BPF_JSET
));
3109 static struct block
*
3110 gen_portatom(off
, v
)
3117 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3120 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3121 s
->next
->s
.k
= off_nl
+ off
;
3123 b
= new_block(JMP(BPF_JEQ
));
3131 static struct block
*
3132 gen_portatom6(off
, v
)
3136 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3141 gen_portop(port
, proto
, dir
)
3142 int port
, proto
, dir
;
3144 struct block
*b0
, *b1
, *tmp
;
3146 /* ip proto 'proto' */
3147 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3153 b1
= gen_portatom(0, (bpf_int32
)port
);
3157 b1
= gen_portatom(2, (bpf_int32
)port
);
3162 tmp
= gen_portatom(0, (bpf_int32
)port
);
3163 b1
= gen_portatom(2, (bpf_int32
)port
);
3168 tmp
= gen_portatom(0, (bpf_int32
)port
);
3169 b1
= gen_portatom(2, (bpf_int32
)port
);
3181 static struct block
*
3182 gen_port(port
, ip_proto
, dir
)
3187 struct block
*b0
, *b1
, *tmp
;
3192 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3193 * not LLC encapsulation with LLCSAP_IP.
3195 * For IEEE 802 networks - which includes 802.5 token ring
3196 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3197 * says that SNAP encapsulation is used, not LLC encapsulation
3200 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3201 * RFC 2225 say that SNAP encapsulation is used, not LLC
3202 * encapsulation with LLCSAP_IP.
3204 * So we always check for ETHERTYPE_IP.
3206 b0
= gen_linktype(ETHERTYPE_IP
);
3212 b1
= gen_portop(port
, ip_proto
, dir
);
3216 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3217 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3219 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3232 gen_portop6(port
, proto
, dir
)
3233 int port
, proto
, dir
;
3235 struct block
*b0
, *b1
, *tmp
;
3237 /* ip proto 'proto' */
3238 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3242 b1
= gen_portatom6(0, (bpf_int32
)port
);
3246 b1
= gen_portatom6(2, (bpf_int32
)port
);
3251 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3252 b1
= gen_portatom6(2, (bpf_int32
)port
);
3257 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3258 b1
= gen_portatom6(2, (bpf_int32
)port
);
3270 static struct block
*
3271 gen_port6(port
, ip_proto
, dir
)
3276 struct block
*b0
, *b1
, *tmp
;
3278 /* ether proto ip */
3279 b0
= gen_linktype(ETHERTYPE_IPV6
);
3285 b1
= gen_portop6(port
, ip_proto
, dir
);
3289 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3290 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3292 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3305 lookup_proto(name
, proto
)
3306 register const char *name
;
3316 v
= pcap_nametoproto(name
);
3317 if (v
== PROTO_UNDEF
)
3318 bpf_error("unknown ip proto '%s'", name
);
3322 /* XXX should look up h/w protocol type based on linktype */
3323 v
= pcap_nametoeproto(name
);
3324 if (v
== PROTO_UNDEF
)
3325 bpf_error("unknown ether proto '%s'", name
);
3329 if (strcmp(name
, "esis") == 0)
3331 else if (strcmp(name
, "isis") == 0)
3333 else if (strcmp(name
, "clnp") == 0)
3336 bpf_error("unknown osi proto '%s'", name
);
3356 static struct block
*
3357 gen_protochain(v
, proto
, dir
)
3362 #ifdef NO_PROTOCHAIN
3363 return gen_proto(v
, proto
, dir
);
3365 struct block
*b0
, *b
;
3366 struct slist
*s
[100];
3367 int fix2
, fix3
, fix4
, fix5
;
3368 int ahcheck
, again
, end
;
3370 int reg2
= alloc_reg();
3372 memset(s
, 0, sizeof(s
));
3373 fix2
= fix3
= fix4
= fix5
= 0;
3380 b0
= gen_protochain(v
, Q_IP
, dir
);
3381 b
= gen_protochain(v
, Q_IPV6
, dir
);
3385 bpf_error("bad protocol applied for 'protochain'");
3389 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3392 * s[0] is a dummy entry to protect other BPF insn from damaged
3393 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3394 * hard to find interdependency made by jump table fixup.
3397 s
[i
] = new_stmt(0); /*dummy*/
3402 b0
= gen_linktype(ETHERTYPE_IP
);
3405 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3406 s
[i
]->s
.k
= off_nl
+ 9;
3408 /* X = ip->ip_hl << 2 */
3409 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3415 b0
= gen_linktype(ETHERTYPE_IPV6
);
3417 /* A = ip6->ip_nxt */
3418 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3419 s
[i
]->s
.k
= off_nl
+ 6;
3421 /* X = sizeof(struct ip6_hdr) */
3422 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3428 bpf_error("unsupported proto to gen_protochain");
3432 /* again: if (A == v) goto end; else fall through; */
3434 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3436 s
[i
]->s
.jt
= NULL
; /*later*/
3437 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3441 #ifndef IPPROTO_NONE
3442 #define IPPROTO_NONE 59
3444 /* if (A == IPPROTO_NONE) goto end */
3445 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3446 s
[i
]->s
.jt
= NULL
; /*later*/
3447 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3448 s
[i
]->s
.k
= IPPROTO_NONE
;
3449 s
[fix5
]->s
.jf
= s
[i
];
3454 if (proto
== Q_IPV6
) {
3455 int v6start
, v6end
, v6advance
, j
;
3458 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3459 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3460 s
[i
]->s
.jt
= NULL
; /*later*/
3461 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3462 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3463 s
[fix2
]->s
.jf
= s
[i
];
3465 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3466 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3467 s
[i
]->s
.jt
= NULL
; /*later*/
3468 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3469 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3471 /* if (A == IPPROTO_ROUTING) goto v6advance */
3472 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3473 s
[i
]->s
.jt
= NULL
; /*later*/
3474 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3475 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3477 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3478 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3479 s
[i
]->s
.jt
= NULL
; /*later*/
3480 s
[i
]->s
.jf
= NULL
; /*later*/
3481 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3492 * X = X + (P[X + 1] + 1) * 8;
3495 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3497 /* A = P[X + packet head] */
3498 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3502 s
[i
] = new_stmt(BPF_ST
);
3506 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3509 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3513 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3515 /* A = P[X + packet head]; */
3516 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3520 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3524 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3528 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3531 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3535 /* goto again; (must use BPF_JA for backward jump) */
3536 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3537 s
[i
]->s
.k
= again
- i
- 1;
3538 s
[i
- 1]->s
.jf
= s
[i
];
3542 for (j
= v6start
; j
<= v6end
; j
++)
3543 s
[j
]->s
.jt
= s
[v6advance
];
3548 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3550 s
[fix2
]->s
.jf
= s
[i
];
3556 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3557 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3558 s
[i
]->s
.jt
= NULL
; /*later*/
3559 s
[i
]->s
.jf
= NULL
; /*later*/
3560 s
[i
]->s
.k
= IPPROTO_AH
;
3562 s
[fix3
]->s
.jf
= s
[ahcheck
];
3569 * X = X + (P[X + 1] + 2) * 4;
3572 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3574 /* A = P[X + packet head]; */
3575 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3579 s
[i
] = new_stmt(BPF_ST
);
3583 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3586 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3590 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3592 /* A = P[X + packet head] */
3593 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3597 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3601 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3605 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3608 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3612 /* goto again; (must use BPF_JA for backward jump) */
3613 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3614 s
[i
]->s
.k
= again
- i
- 1;
3619 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3621 s
[fix2
]->s
.jt
= s
[end
];
3622 s
[fix4
]->s
.jf
= s
[end
];
3623 s
[fix5
]->s
.jt
= s
[end
];
3630 for (i
= 0; i
< max
- 1; i
++)
3631 s
[i
]->next
= s
[i
+ 1];
3632 s
[max
- 1]->next
= NULL
;
3637 b
= new_block(JMP(BPF_JEQ
));
3638 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3648 static struct block
*
3649 gen_proto(v
, proto
, dir
)
3654 struct block
*b0
, *b1
;
3656 if (dir
!= Q_DEFAULT
)
3657 bpf_error("direction applied to 'proto'");
3662 b0
= gen_proto(v
, Q_IP
, dir
);
3663 b1
= gen_proto(v
, Q_IPV6
, dir
);
3671 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3672 * not LLC encapsulation with LLCSAP_IP.
3674 * For IEEE 802 networks - which includes 802.5 token ring
3675 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3676 * says that SNAP encapsulation is used, not LLC encapsulation
3679 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3680 * RFC 2225 say that SNAP encapsulation is used, not LLC
3681 * encapsulation with LLCSAP_IP.
3683 * So we always check for ETHERTYPE_IP.
3685 b0
= gen_linktype(ETHERTYPE_IP
);
3687 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3689 b1
= gen_protochain(v
, Q_IP
);
3699 * Frame Relay packets typically have an OSI
3700 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3701 * generates code to check for all the OSI
3702 * NLPIDs, so calling it and then adding a check
3703 * for the particular NLPID for which we're
3704 * looking is bogus, as we can just check for
3707 * What we check for is the NLPID and a frame
3708 * control field value of UI, i.e. 0x03 followed
3711 * XXX - assumes a 2-byte Frame Relay header with
3712 * DLCI and flags. What if the address is longer?
3714 * XXX - what about SNAP-encapsulated frames?
3716 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3721 * Cisco uses an Ethertype lookalike - for OSI,
3724 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3725 /* OSI in C-HDLC is stuffed with a fudge byte */
3726 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3731 b0
= gen_linktype(LLCSAP_ISONS
);
3732 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3738 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3740 * 4 is the offset of the PDU type relative to the IS-IS
3743 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3748 bpf_error("arp does not encapsulate another protocol");
3752 bpf_error("rarp does not encapsulate another protocol");
3756 bpf_error("atalk encapsulation is not specifiable");
3760 bpf_error("decnet encapsulation is not specifiable");
3764 bpf_error("sca does not encapsulate another protocol");
3768 bpf_error("lat does not encapsulate another protocol");
3772 bpf_error("moprc does not encapsulate another protocol");
3776 bpf_error("mopdl does not encapsulate another protocol");
3780 return gen_linktype(v
);
3783 bpf_error("'udp proto' is bogus");
3787 bpf_error("'tcp proto' is bogus");
3791 bpf_error("'sctp proto' is bogus");
3795 bpf_error("'icmp proto' is bogus");
3799 bpf_error("'igmp proto' is bogus");
3803 bpf_error("'igrp proto' is bogus");
3807 bpf_error("'pim proto' is bogus");
3811 bpf_error("'vrrp proto' is bogus");
3816 b0
= gen_linktype(ETHERTYPE_IPV6
);
3818 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3820 b1
= gen_protochain(v
, Q_IPV6
);
3826 bpf_error("'icmp6 proto' is bogus");
3830 bpf_error("'ah proto' is bogus");
3833 bpf_error("'ah proto' is bogus");
3836 bpf_error("'stp proto' is bogus");
3839 bpf_error("'ipx proto' is bogus");
3842 bpf_error("'netbeui proto' is bogus");
3853 register const char *name
;
3856 int proto
= q
.proto
;
3860 bpf_u_int32 mask
, addr
;
3862 bpf_u_int32
**alist
;
3865 struct sockaddr_in
*sin
;
3866 struct sockaddr_in6
*sin6
;
3867 struct addrinfo
*res
, *res0
;
3868 struct in6_addr mask128
;
3870 struct block
*b
, *tmp
;
3871 int port
, real_proto
;
3876 addr
= pcap_nametonetaddr(name
);
3878 bpf_error("unknown network '%s'", name
);
3879 /* Left justify network addr and calculate its network mask */
3881 while (addr
&& (addr
& 0xff000000) == 0) {
3885 return gen_host(addr
, mask
, proto
, dir
);
3889 if (proto
== Q_LINK
) {
3893 eaddr
= pcap_ether_hostton(name
);
3896 "unknown ether host '%s'", name
);
3897 b
= gen_ehostop(eaddr
, dir
);
3902 eaddr
= pcap_ether_hostton(name
);
3905 "unknown FDDI host '%s'", name
);
3906 b
= gen_fhostop(eaddr
, dir
);
3911 eaddr
= pcap_ether_hostton(name
);
3914 "unknown token ring host '%s'", name
);
3915 b
= gen_thostop(eaddr
, dir
);
3919 case DLT_IEEE802_11
:
3920 eaddr
= pcap_ether_hostton(name
);
3923 "unknown 802.11 host '%s'", name
);
3924 b
= gen_wlanhostop(eaddr
, dir
);
3928 case DLT_IP_OVER_FC
:
3929 eaddr
= pcap_ether_hostton(name
);
3932 "unknown Fibre Channel host '%s'", name
);
3933 b
= gen_ipfchostop(eaddr
, dir
);
3942 * Check that the packet doesn't begin
3943 * with an LE Control marker. (We've
3944 * already generated a test for LANE.)
3946 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3950 eaddr
= pcap_ether_hostton(name
);
3953 "unknown ether host '%s'", name
);
3954 b
= gen_ehostop(eaddr
, dir
);
3960 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3961 } else if (proto
== Q_DECNET
) {
3962 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3964 * I don't think DECNET hosts can be multihomed, so
3965 * there is no need to build up a list of addresses
3967 return (gen_host(dn_addr
, 0, proto
, dir
));
3970 alist
= pcap_nametoaddr(name
);
3971 if (alist
== NULL
|| *alist
== NULL
)
3972 bpf_error("unknown host '%s'", name
);
3974 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
3976 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3978 tmp
= gen_host(**alist
++, 0xffffffff,
3985 memset(&mask128
, 0xff, sizeof(mask128
));
3986 res0
= res
= pcap_nametoaddrinfo(name
);
3988 bpf_error("unknown host '%s'", name
);
3990 tproto
= tproto6
= proto
;
3991 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3995 for (res
= res0
; res
; res
= res
->ai_next
) {
3996 switch (res
->ai_family
) {
3998 if (tproto
== Q_IPV6
)
4001 sin
= (struct sockaddr_in
*)
4003 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4004 0xffffffff, tproto
, dir
);
4007 if (tproto6
== Q_IP
)
4010 sin6
= (struct sockaddr_in6
*)
4012 tmp
= gen_host6(&sin6
->sin6_addr
,
4013 &mask128
, tproto6
, dir
);
4024 bpf_error("unknown host '%s'%s", name
,
4025 (proto
== Q_DEFAULT
)
4027 : " for specified address family");
4034 if (proto
!= Q_DEFAULT
&&
4035 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4036 bpf_error("illegal qualifier of 'port'");
4037 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4038 bpf_error("unknown port '%s'", name
);
4039 if (proto
== Q_UDP
) {
4040 if (real_proto
== IPPROTO_TCP
)
4041 bpf_error("port '%s' is tcp", name
);
4042 else if (real_proto
== IPPROTO_SCTP
)
4043 bpf_error("port '%s' is sctp", name
);
4045 /* override PROTO_UNDEF */
4046 real_proto
= IPPROTO_UDP
;
4048 if (proto
== Q_TCP
) {
4049 if (real_proto
== IPPROTO_UDP
)
4050 bpf_error("port '%s' is udp", name
);
4052 else if (real_proto
== IPPROTO_SCTP
)
4053 bpf_error("port '%s' is sctp", name
);
4055 /* override PROTO_UNDEF */
4056 real_proto
= IPPROTO_TCP
;
4058 if (proto
== Q_SCTP
) {
4059 if (real_proto
== IPPROTO_UDP
)
4060 bpf_error("port '%s' is udp", name
);
4062 else if (real_proto
== IPPROTO_TCP
)
4063 bpf_error("port '%s' is tcp", name
);
4065 /* override PROTO_UNDEF */
4066 real_proto
= IPPROTO_SCTP
;
4069 return gen_port(port
, real_proto
, dir
);
4073 b
= gen_port(port
, real_proto
, dir
);
4074 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4081 eaddr
= pcap_ether_hostton(name
);
4083 bpf_error("unknown ether host: %s", name
);
4085 alist
= pcap_nametoaddr(name
);
4086 if (alist
== NULL
|| *alist
== NULL
)
4087 bpf_error("unknown host '%s'", name
);
4088 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4092 bpf_error("'gateway' not supported in this configuration");
4096 real_proto
= lookup_proto(name
, proto
);
4097 if (real_proto
>= 0)
4098 return gen_proto(real_proto
, proto
, dir
);
4100 bpf_error("unknown protocol: %s", name
);
4103 real_proto
= lookup_proto(name
, proto
);
4104 if (real_proto
>= 0)
4105 return gen_protochain(real_proto
, proto
, dir
);
4107 bpf_error("unknown protocol: %s", name
);
4119 gen_mcode(s1
, s2
, masklen
, q
)
4120 register const char *s1
, *s2
;
4121 register int masklen
;
4124 register int nlen
, mlen
;
4127 nlen
= __pcap_atoin(s1
, &n
);
4128 /* Promote short ipaddr */
4132 mlen
= __pcap_atoin(s2
, &m
);
4133 /* Promote short ipaddr */
4136 bpf_error("non-network bits set in \"%s mask %s\"",
4139 /* Convert mask len to mask */
4141 bpf_error("mask length must be <= 32");
4142 m
= 0xffffffff << (32 - masklen
);
4144 bpf_error("non-network bits set in \"%s/%d\"",
4151 return gen_host(n
, m
, q
.proto
, q
.dir
);
4154 bpf_error("Mask syntax for networks only");
4161 register const char *s
;
4166 int proto
= q
.proto
;
4172 else if (q
.proto
== Q_DECNET
)
4173 vlen
= __pcap_atodn(s
, &v
);
4175 vlen
= __pcap_atoin(s
, &v
);
4182 if (proto
== Q_DECNET
)
4183 return gen_host(v
, 0, proto
, dir
);
4184 else if (proto
== Q_LINK
) {
4185 bpf_error("illegal link layer address");
4188 if (s
== NULL
&& q
.addr
== Q_NET
) {
4189 /* Promote short net number */
4190 while (v
&& (v
& 0xff000000) == 0) {
4195 /* Promote short ipaddr */
4199 return gen_host(v
, mask
, proto
, dir
);
4204 proto
= IPPROTO_UDP
;
4205 else if (proto
== Q_TCP
)
4206 proto
= IPPROTO_TCP
;
4207 else if (proto
== Q_SCTP
)
4208 proto
= IPPROTO_SCTP
;
4209 else if (proto
== Q_DEFAULT
)
4210 proto
= PROTO_UNDEF
;
4212 bpf_error("illegal qualifier of 'port'");
4215 return gen_port((int)v
, proto
, dir
);
4219 b
= gen_port((int)v
, proto
, dir
);
4220 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4226 bpf_error("'gateway' requires a name");
4230 return gen_proto((int)v
, proto
, dir
);
4233 return gen_protochain((int)v
, proto
, dir
);
4248 gen_mcode6(s1
, s2
, masklen
, q
)
4249 register const char *s1
, *s2
;
4250 register int masklen
;
4253 struct addrinfo
*res
;
4254 struct in6_addr
*addr
;
4255 struct in6_addr mask
;
4260 bpf_error("no mask %s supported", s2
);
4262 res
= pcap_nametoaddrinfo(s1
);
4264 bpf_error("invalid ip6 address %s", s1
);
4266 bpf_error("%s resolved to multiple address", s1
);
4267 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4269 if (sizeof(mask
) * 8 < masklen
)
4270 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4271 memset(&mask
, 0, sizeof(mask
));
4272 memset(&mask
, 0xff, masklen
/ 8);
4274 mask
.s6_addr
[masklen
/ 8] =
4275 (0xff << (8 - masklen
% 8)) & 0xff;
4278 a
= (u_int32_t
*)addr
;
4279 m
= (u_int32_t
*)&mask
;
4280 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4281 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4282 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4290 bpf_error("Mask syntax for networks only");
4294 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4299 bpf_error("invalid qualifier against IPv6 address");
4307 register const u_char
*eaddr
;
4310 struct block
*b
, *tmp
;
4312 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4313 if (linktype
== DLT_EN10MB
)
4314 return gen_ehostop(eaddr
, (int)q
.dir
);
4315 if (linktype
== DLT_FDDI
)
4316 return gen_fhostop(eaddr
, (int)q
.dir
);
4317 if (linktype
== DLT_IEEE802
)
4318 return gen_thostop(eaddr
, (int)q
.dir
);
4319 if (linktype
== DLT_IEEE802_11
)
4320 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4321 if (linktype
== DLT_SUNATM
&& is_lane
) {
4323 * Check that the packet doesn't begin with an
4324 * LE Control marker. (We've already generated
4327 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4331 * Now check the MAC address.
4333 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4337 if (linktype
== DLT_IP_OVER_FC
)
4338 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4339 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4341 bpf_error("ethernet address used in non-ether expression");
4347 struct slist
*s0
, *s1
;
4350 * This is definitely not the best way to do this, but the
4351 * lists will rarely get long.
4358 static struct slist
*
4364 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4369 static struct slist
*
4375 s
= new_stmt(BPF_LD
|BPF_MEM
);
4381 gen_load(proto
, index
, size
)
4386 struct slist
*s
, *tmp
;
4388 int regno
= alloc_reg();
4390 free_reg(index
->regno
);
4394 bpf_error("data size must be 1, 2, or 4");
4410 bpf_error("unsupported index operation");
4414 * XXX - what about ATM LANE? Should the index be
4415 * relative to the beginning of the AAL5 frame, so
4416 * that 0 refers to the beginning of the LE Control
4417 * field, or relative to the beginning of the LAN
4418 * frame, so that 0 refers, for Ethernet LANE, to
4419 * the beginning of the destination address?
4421 s
= xfer_to_x(index
);
4422 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4424 sappend(index
->s
, s
);
4439 /* XXX Note that we assume a fixed link header here. */
4440 s
= xfer_to_x(index
);
4441 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4444 sappend(index
->s
, s
);
4446 b
= gen_proto_abbrev(proto
);
4448 gen_and(index
->b
, b
);
4460 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4462 sappend(s
, xfer_to_a(index
));
4463 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4464 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4465 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4467 sappend(index
->s
, s
);
4469 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4471 gen_and(index
->b
, b
);
4473 gen_and(gen_proto_abbrev(Q_IP
), b
);
4479 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4483 index
->regno
= regno
;
4484 s
= new_stmt(BPF_ST
);
4486 sappend(index
->s
, s
);
4492 gen_relation(code
, a0
, a1
, reversed
)
4494 struct arth
*a0
, *a1
;
4497 struct slist
*s0
, *s1
, *s2
;
4498 struct block
*b
, *tmp
;
4502 if (code
== BPF_JEQ
) {
4503 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4504 b
= new_block(JMP(code
));
4508 b
= new_block(BPF_JMP
|code
|BPF_X
);
4514 sappend(a0
->s
, a1
->s
);
4518 free_reg(a0
->regno
);
4519 free_reg(a1
->regno
);
4521 /* 'and' together protocol checks */
4524 gen_and(a0
->b
, tmp
= a1
->b
);
4540 int regno
= alloc_reg();
4541 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4544 s
= new_stmt(BPF_LD
|BPF_LEN
);
4545 s
->next
= new_stmt(BPF_ST
);
4546 s
->next
->s
.k
= regno
;
4561 a
= (struct arth
*)newchunk(sizeof(*a
));
4565 s
= new_stmt(BPF_LD
|BPF_IMM
);
4567 s
->next
= new_stmt(BPF_ST
);
4583 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4586 s
= new_stmt(BPF_ST
);
4594 gen_arth(code
, a0
, a1
)
4596 struct arth
*a0
, *a1
;
4598 struct slist
*s0
, *s1
, *s2
;
4602 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4607 sappend(a0
->s
, a1
->s
);
4609 free_reg(a0
->regno
);
4610 free_reg(a1
->regno
);
4612 s0
= new_stmt(BPF_ST
);
4613 a0
->regno
= s0
->s
.k
= alloc_reg();
4620 * Here we handle simple allocation of the scratch registers.
4621 * If too many registers are alloc'd, the allocator punts.
4623 static int regused
[BPF_MEMWORDS
];
4627 * Return the next free register.
4632 int n
= BPF_MEMWORDS
;
4635 if (regused
[curreg
])
4636 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4638 regused
[curreg
] = 1;
4642 bpf_error("too many registers needed to evaluate expression");
4647 * Return a register to the table so it can
4657 static struct block
*
4664 s
= new_stmt(BPF_LD
|BPF_LEN
);
4665 b
= new_block(JMP(jmp
));
4676 return gen_len(BPF_JGE
, n
);
4680 * Actually, this is less than or equal.
4688 b
= gen_len(BPF_JGT
, n
);
4695 gen_byteop(op
, idx
, val
)
4706 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4709 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4710 b
->s
.code
= JMP(BPF_JGE
);
4715 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4716 b
->s
.code
= JMP(BPF_JGT
);
4720 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4724 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4728 b
= new_block(JMP(BPF_JEQ
));
4735 static u_char abroadcast
[] = { 0x0 };
4738 gen_broadcast(proto
)
4741 bpf_u_int32 hostmask
;
4742 struct block
*b0
, *b1
, *b2
;
4743 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4749 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4750 return gen_ahostop(abroadcast
, Q_DST
);
4751 if (linktype
== DLT_EN10MB
)
4752 return gen_ehostop(ebroadcast
, Q_DST
);
4753 if (linktype
== DLT_FDDI
)
4754 return gen_fhostop(ebroadcast
, Q_DST
);
4755 if (linktype
== DLT_IEEE802
)
4756 return gen_thostop(ebroadcast
, Q_DST
);
4757 if (linktype
== DLT_IEEE802_11
)
4758 return gen_wlanhostop(ebroadcast
, Q_DST
);
4759 if (linktype
== DLT_IP_OVER_FC
)
4760 return gen_ipfchostop(ebroadcast
, Q_DST
);
4761 if (linktype
== DLT_SUNATM
&& is_lane
) {
4763 * Check that the packet doesn't begin with an
4764 * LE Control marker. (We've already generated
4767 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4771 * Now check the MAC address.
4773 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4777 bpf_error("not a broadcast link");
4781 b0
= gen_linktype(ETHERTYPE_IP
);
4782 hostmask
= ~netmask
;
4783 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4784 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4785 (bpf_int32
)(~0 & hostmask
), hostmask
);
4790 bpf_error("only link-layer/IP broadcast filters supported");
4794 * Generate code to test the low-order bit of a MAC address (that's
4795 * the bottom bit of the *first* byte).
4797 static struct block
*
4798 gen_mac_multicast(offset
)
4801 register struct block
*b0
;
4802 register struct slist
*s
;
4804 /* link[offset] & 1 != 0 */
4805 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4807 b0
= new_block(JMP(BPF_JSET
));
4814 gen_multicast(proto
)
4817 register struct block
*b0
, *b1
, *b2
;
4818 register struct slist
*s
;
4824 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4825 /* all ARCnet multicasts use the same address */
4826 return gen_ahostop(abroadcast
, Q_DST
);
4828 if (linktype
== DLT_EN10MB
) {
4829 /* ether[0] & 1 != 0 */
4830 return gen_mac_multicast(0);
4833 if (linktype
== DLT_FDDI
) {
4835 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4837 * XXX - was that referring to bit-order issues?
4839 /* fddi[1] & 1 != 0 */
4840 return gen_mac_multicast(1);
4843 if (linktype
== DLT_IEEE802
) {
4844 /* tr[2] & 1 != 0 */
4845 return gen_mac_multicast(2);
4848 if (linktype
== DLT_IEEE802_11
) {
4852 * For control frames, there is no DA.
4854 * For management frames, DA is at an
4855 * offset of 4 from the beginning of
4858 * For data frames, DA is at an offset
4859 * of 4 from the beginning of the packet
4860 * if To DS is clear and at an offset of
4861 * 16 from the beginning of the packet
4866 * Generate the tests to be done for data frames.
4868 * First, check for To DS set, i.e. "link[1] & 0x01".
4870 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4872 b1
= new_block(JMP(BPF_JSET
));
4873 b1
->s
.k
= 0x01; /* To DS */
4877 * If To DS is set, the DA is at 16.
4879 b0
= gen_mac_multicast(16);
4883 * Now, check for To DS not set, i.e. check
4884 * "!(link[1] & 0x01)".
4886 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4888 b2
= new_block(JMP(BPF_JSET
));
4889 b2
->s
.k
= 0x01; /* To DS */
4894 * If To DS is not set, the DA is at 4.
4896 b1
= gen_mac_multicast(4);
4900 * Now OR together the last two checks. That gives
4901 * the complete set of checks for data frames.
4906 * Now check for a data frame.
4907 * I.e, check "link[0] & 0x08".
4909 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4911 b1
= new_block(JMP(BPF_JSET
));
4916 * AND that with the checks done for data frames.
4921 * If the high-order bit of the type value is 0, this
4922 * is a management frame.
4923 * I.e, check "!(link[0] & 0x08)".
4925 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4927 b2
= new_block(JMP(BPF_JSET
));
4933 * For management frames, the DA is at 4.
4935 b1
= gen_mac_multicast(4);
4939 * OR that with the checks done for data frames.
4940 * That gives the checks done for management and
4946 * If the low-order bit of the type value is 1,
4947 * this is either a control frame or a frame
4948 * with a reserved type, and thus not a
4951 * I.e., check "!(link[0] & 0x04)".
4953 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4955 b1
= new_block(JMP(BPF_JSET
));
4961 * AND that with the checks for data and management
4968 if (linktype
== DLT_IP_OVER_FC
) {
4969 b0
= gen_mac_multicast(2);
4973 if (linktype
== DLT_SUNATM
&& is_lane
) {
4975 * Check that the packet doesn't begin with an
4976 * LE Control marker. (We've already generated
4979 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4982 /* ether[off_mac] & 1 != 0 */
4983 b0
= gen_mac_multicast(off_mac
);
4988 /* Link not known to support multicasts */
4992 b0
= gen_linktype(ETHERTYPE_IP
);
4993 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4994 b1
->s
.code
= JMP(BPF_JGE
);
5000 b0
= gen_linktype(ETHERTYPE_IPV6
);
5001 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5006 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5010 * generate command for inbound/outbound. It's here so we can
5011 * make it link-type specific. 'dir' = 0 implies "inbound",
5012 * = 1 implies "outbound".
5018 register struct block
*b0
;
5021 * Only some data link types support inbound/outbound qualifiers.
5025 b0
= gen_relation(BPF_JEQ
,
5026 gen_load(Q_LINK
, gen_loadi(0), 1),
5034 * Match packets sent by this machine.
5036 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5039 * Match packets sent to this machine.
5040 * (No broadcast or multicast packets, or
5041 * packets sent to some other machine and
5042 * received promiscuously.)
5044 * XXX - packets sent to other machines probably
5045 * shouldn't be matched, but what about broadcast
5046 * or multicast packets we received?
5048 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5053 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5054 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5058 bpf_error("inbound/outbound not supported on linktype %d",
5066 /* PF firewall log matched interface */
5068 gen_pf_ifname(const char *ifname
)
5073 if (linktype
== DLT_PFLOG
) {
5074 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5075 off
= offsetof(struct pfloghdr
, ifname
);
5077 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5080 if (strlen(ifname
) >= len
) {
5081 bpf_error("ifname interface names can only be %d characters",
5085 b0
= gen_bcmp(off
, strlen(ifname
), ifname
);
5089 /* PF firewall log matched interface */
5091 gen_pf_ruleset(char *ruleset
)
5095 if (linktype
!= DLT_PFLOG
) {
5096 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5099 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5100 bpf_error("ruleset names can only be %ld characters",
5101 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5104 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5105 strlen(ruleset
), ruleset
);
5109 /* PF firewall log rule number */
5115 if (linktype
== DLT_PFLOG
) {
5116 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5119 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5126 /* PF firewall log sub-rule number */
5128 gen_pf_srnr(int srnr
)
5132 if (linktype
!= DLT_PFLOG
) {
5133 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5137 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5142 /* PF firewall log reason code */
5144 gen_pf_reason(int reason
)
5148 if (linktype
== DLT_PFLOG
) {
5149 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5152 bpf_error("reason not supported on linktype 0x%x", linktype
);
5159 /* PF firewall log action */
5161 gen_pf_action(int action
)
5165 if (linktype
== DLT_PFLOG
) {
5166 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5169 bpf_error("action not supported on linktype 0x%x", linktype
);
5178 register const u_char
*eaddr
;
5181 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5182 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5183 return gen_ahostop(eaddr
, (int)q
.dir
);
5185 bpf_error("ARCnet address used in non-arc expression");
5189 static struct block
*
5190 gen_ahostop(eaddr
, dir
)
5191 register const u_char
*eaddr
;
5194 register struct block
*b0
, *b1
;
5197 /* src comes first, different from Ethernet */
5199 return gen_bcmp(0, 1, eaddr
);
5202 return gen_bcmp(1, 1, eaddr
);
5205 b0
= gen_ahostop(eaddr
, Q_SRC
);
5206 b1
= gen_ahostop(eaddr
, Q_DST
);
5212 b0
= gen_ahostop(eaddr
, Q_SRC
);
5213 b1
= gen_ahostop(eaddr
, Q_DST
);
5222 * support IEEE 802.1Q VLAN trunk over ethernet
5231 * Change the offsets to point to the type and data fields within
5232 * the VLAN packet. This is somewhat of a kludge.
5234 if (orig_nl
== (u_int
)-1) {
5235 orig_linktype
= off_linktype
; /* save original values */
5237 orig_nl_nosnap
= off_nl_nosnap
;
5248 bpf_error("no VLAN support for data link type %d",
5254 /* check for VLAN */
5255 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5257 /* If a specific VLAN is requested, check VLAN id */
5258 if (vlan_num
>= 0) {
5261 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
5270 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5282 bpf_error("'vpi' supported only on raw ATM");
5283 if (off_vpi
== (u_int
)-1)
5285 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5286 (u_int
)jvalue
, reverse
);
5291 bpf_error("'vci' supported only on raw ATM");
5292 if (off_vci
== (u_int
)-1)
5294 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5295 (u_int
)jvalue
, reverse
);
5299 if (off_proto
== (u_int
)-1)
5300 abort(); /* XXX - this isn't on FreeBSD */
5301 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5302 (u_int
)jvalue
, reverse
);
5306 if (off_payload
== (u_int
)-1)
5308 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5309 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5314 bpf_error("'callref' supported only on raw ATM");
5315 if (off_proto
== (u_int
)-1)
5317 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5318 (u_int
)jvalue
, reverse
);
5328 gen_atmtype_abbrev(type
)
5331 struct block
*b0
, *b1
;
5336 /* Get all packets in Meta signalling Circuit */
5338 bpf_error("'metac' supported only on raw ATM");
5339 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5340 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5345 /* Get all packets in Broadcast Circuit*/
5347 bpf_error("'bcc' supported only on raw ATM");
5348 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5349 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5354 /* Get all cells in Segment OAM F4 circuit*/
5356 bpf_error("'oam4sc' supported only on raw ATM");
5357 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5358 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5363 /* Get all cells in End-to-End OAM F4 Circuit*/
5365 bpf_error("'oam4ec' supported only on raw ATM");
5366 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5367 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5372 /* Get all packets in connection Signalling Circuit */
5374 bpf_error("'sc' supported only on raw ATM");
5375 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5376 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5381 /* Get all packets in ILMI Circuit */
5383 bpf_error("'ilmic' supported only on raw ATM");
5384 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5385 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5390 /* Get all LANE packets */
5392 bpf_error("'lane' supported only on raw ATM");
5393 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5396 * Arrange that all subsequent tests assume LANE
5397 * rather than LLC-encapsulated packets, and set
5398 * the offsets appropriately for LANE-encapsulated
5401 * "off_mac" is the offset of the Ethernet header,
5402 * which is 2 bytes past the ATM pseudo-header
5403 * (skipping the pseudo-header and 2-byte LE Client
5404 * field). The other offsets are Ethernet offsets
5405 * relative to "off_mac".
5408 off_mac
= off_payload
+ 2; /* MAC header */
5409 off_linktype
= off_mac
+ 12;
5410 off_nl
= off_mac
+ 14; /* Ethernet II */
5411 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5415 /* Get all LLC-encapsulated packets */
5417 bpf_error("'llc' supported only on raw ATM");
5418 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5429 static struct block
*
5430 gen_msg_abbrev(type
)
5436 * Q.2931 signalling protocol messages for handling virtual circuits
5437 * establishment and teardown
5442 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5446 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5450 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5454 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5458 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5461 case A_RELEASE_DONE
:
5462 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5472 gen_atmmulti_abbrev(type
)
5475 struct block
*b0
, *b1
;
5481 bpf_error("'oam' supported only on raw ATM");
5482 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5487 bpf_error("'oamf4' supported only on raw ATM");
5489 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5490 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5492 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5498 * Get Q.2931 signalling messages for switched
5499 * virtual connection
5502 bpf_error("'connectmsg' supported only on raw ATM");
5503 b0
= gen_msg_abbrev(A_SETUP
);
5504 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5506 b0
= gen_msg_abbrev(A_CONNECT
);
5508 b0
= gen_msg_abbrev(A_CONNECTACK
);
5510 b0
= gen_msg_abbrev(A_RELEASE
);
5512 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5514 b0
= gen_atmtype_abbrev(A_SC
);
5520 bpf_error("'metaconnect' supported only on raw ATM");
5521 b0
= gen_msg_abbrev(A_SETUP
);
5522 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5524 b0
= gen_msg_abbrev(A_CONNECT
);
5526 b0
= gen_msg_abbrev(A_RELEASE
);
5528 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5530 b0
= gen_atmtype_abbrev(A_METAC
);