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.205 2004-06-16 08:20:29 hannes Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #define offsetof(s, e) ((size_t)&((s *)0)->e)
79 #include <netdb.h> /* for "struct addrinfo" */
82 #include <pcap-namedb.h>
87 #define IPPROTO_SCTP 132
90 #ifdef HAVE_OS_PROTO_H
94 #define JMP(c) ((c)|BPF_JMP|BPF_K)
97 static jmp_buf top_ctx
;
98 static pcap_t
*bpf_pcap
;
100 /* Hack for updating VLAN, MPLS offsets. */
101 static u_int orig_linktype
= -1U, orig_nl
= -1U, orig_nl_nosnap
= -1U;
105 int pcap_fddipad
= PCAP_FDDIPAD
;
112 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(int);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * We divy out chunks of memory rather than call malloc each time so
135 * we don't have to worry about leaking memory. It's probably
136 * not a big deal if all this memory was wasted but it this ever
137 * goes into a library that would probably not be a good idea.
140 #define CHUNK0SIZE 1024
146 static struct chunk chunks
[NCHUNKS
];
147 static int cur_chunk
;
149 static void *newchunk(u_int
);
150 static void freechunks(void);
151 static inline struct block
*new_block(int);
152 static inline struct slist
*new_stmt(int);
153 static struct block
*gen_retblk(int);
154 static inline void syntax(void);
156 static void backpatch(struct block
*, struct block
*);
157 static void merge(struct block
*, struct block
*);
158 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
159 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
160 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
161 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
162 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
163 bpf_u_int32
, bpf_u_int32
, int);
164 static struct block
*gen_uncond(int);
165 static inline struct block
*gen_true(void);
166 static inline struct block
*gen_false(void);
167 static struct block
*gen_ether_linktype(int);
168 static struct block
*gen_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
);
1266 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1270 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1276 case DLT_IEEE802_11
:
1277 case DLT_PRISM_HEADER
:
1278 case DLT_IEEE802_11_RADIO
:
1281 case DLT_ATM_RFC1483
:
1283 case DLT_IP_OVER_FC
:
1284 return gen_llc(proto
);
1290 * If "is_lane" is set, check for a LANE-encapsulated
1291 * version of this protocol, otherwise check for an
1292 * LLC-encapsulated version of this protocol.
1294 * We assume LANE means Ethernet, not Token Ring.
1298 * Check that the packet doesn't begin with an
1299 * LE Control marker. (We've already generated
1302 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1306 * Now generate an Ethernet test.
1308 b1
= gen_ether_linktype(proto
);
1313 * Check for LLC encapsulation and then check the
1316 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1317 b1
= gen_llc(proto
);
1326 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1327 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1328 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1334 * OSI protocols always use 802.2 encapsulation.
1335 * XXX - should we check both the DSAP and the
1336 * LSAP, like this, or should we check just the
1339 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1340 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1341 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1345 case LLCSAP_NETBEUI
:
1347 * NetBEUI always uses 802.2 encapsulation.
1348 * XXX - should we check both the DSAP and the
1349 * LSAP, like this, or should we check just the
1352 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1353 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1354 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1360 * Ethernet_II frames, which are Ethernet
1361 * frames with a frame type of ETHERTYPE_IPX;
1363 * Ethernet_802.3 frames, which have a frame
1364 * type of LINUX_SLL_P_802_3;
1366 * Ethernet_802.2 frames, which are 802.3
1367 * frames with an 802.2 LLC header (i.e, have
1368 * a frame type of LINUX_SLL_P_802_2) and
1369 * with the IPX LSAP as the DSAP in the LLC
1372 * Ethernet_SNAP frames, which are 802.3
1373 * frames with an LLC header and a SNAP
1374 * header and with an OUI of 0x000000
1375 * (encapsulated Ethernet) and a protocol
1376 * ID of ETHERTYPE_IPX in the SNAP header.
1378 * First, do the checks on LINUX_SLL_P_802_2
1379 * frames; generate the check for either
1380 * Ethernet_802.2 or Ethernet_SNAP frames, and
1381 * then put a check for LINUX_SLL_P_802_2 frames
1384 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1385 (bpf_int32
)LLCSAP_IPX
);
1386 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1389 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1393 * Now check for 802.3 frames and OR that with
1394 * the previous test.
1396 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1400 * Now add the check for Ethernet_II frames, and
1401 * do that before checking for the other frame
1404 b0
= gen_cmp(off_linktype
, BPF_H
,
1405 (bpf_int32
)ETHERTYPE_IPX
);
1409 case ETHERTYPE_ATALK
:
1410 case ETHERTYPE_AARP
:
1412 * EtherTalk (AppleTalk protocols on Ethernet link
1413 * layer) may use 802.2 encapsulation.
1417 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1418 * we check for the 802.2 protocol type in the
1419 * "Ethernet type" field.
1421 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1424 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1425 * SNAP packets with an organization code of
1426 * 0x080007 (Apple, for Appletalk) and a protocol
1427 * type of ETHERTYPE_ATALK (Appletalk).
1429 * 802.2-encapsulated ETHERTYPE_AARP packets are
1430 * SNAP packets with an organization code of
1431 * 0x000000 (encapsulated Ethernet) and a protocol
1432 * type of ETHERTYPE_AARP (Appletalk ARP).
1434 if (proto
== ETHERTYPE_ATALK
)
1435 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1437 else /* proto == ETHERTYPE_AARP */
1438 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1443 * Check for Ethernet encapsulation (Ethertalk
1444 * phase 1?); we just check for the Ethernet
1447 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1453 if (proto
<= ETHERMTU
) {
1455 * This is an LLC SAP value, so the frames
1456 * that match would be 802.2 frames.
1457 * Check for the 802.2 protocol type
1458 * in the "Ethernet type" field, and
1459 * then check the DSAP.
1461 b0
= gen_cmp(off_linktype
, BPF_H
,
1463 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1469 * This is an Ethernet type, so compare
1470 * the length/type field with it (if
1471 * the frame is an 802.2 frame, the length
1472 * field will be <= ETHERMTU, and, as
1473 * "proto" is > ETHERMTU, this test
1474 * will fail and the frame won't match,
1475 * which is what we want).
1477 return gen_cmp(off_linktype
, BPF_H
,
1485 case DLT_SLIP_BSDOS
:
1488 * These types don't provide any type field; packets
1491 * XXX - for IPv4, check for a version number of 4, and,
1492 * for IPv6, check for a version number of 6?
1498 case ETHERTYPE_IPV6
:
1500 return gen_true(); /* always true */
1503 return gen_false(); /* always false */
1509 case DLT_PPP_SERIAL
:
1512 * We use Ethernet protocol types inside libpcap;
1513 * map them to the corresponding PPP protocol types.
1522 case ETHERTYPE_IPV6
:
1531 case ETHERTYPE_ATALK
:
1545 * I'm assuming the "Bridging PDU"s that go
1546 * over PPP are Spanning Tree Protocol
1560 * We use Ethernet protocol types inside libpcap;
1561 * map them to the corresponding PPP protocol types.
1566 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1567 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1569 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1574 case ETHERTYPE_IPV6
:
1584 case ETHERTYPE_ATALK
:
1598 * I'm assuming the "Bridging PDU"s that go
1599 * over PPP are Spanning Tree Protocol
1615 * For DLT_NULL, the link-layer header is a 32-bit
1616 * word containing an AF_ value in *host* byte order,
1617 * and for DLT_ENC, the link-layer header begins
1618 * with a 32-bit work containing an AF_ value in
1621 * In addition, if we're reading a saved capture file,
1622 * the host byte order in the capture may not be the
1623 * same as the host byte order on this machine.
1625 * For DLT_LOOP, the link-layer header is a 32-bit
1626 * word containing an AF_ value in *network* byte order.
1628 * XXX - AF_ values may, unfortunately, be platform-
1629 * dependent; for example, FreeBSD's AF_INET6 is 24
1630 * whilst NetBSD's and OpenBSD's is 26.
1632 * This means that, when reading a capture file, just
1633 * checking for our AF_INET6 value won't work if the
1634 * capture file came from another OS.
1643 case ETHERTYPE_IPV6
:
1650 * Not a type on which we support filtering.
1651 * XXX - support those that have AF_ values
1652 * #defined on this platform, at least?
1657 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1659 * The AF_ value is in host byte order, but
1660 * the BPF interpreter will convert it to
1661 * network byte order.
1663 * If this is a save file, and it's from a
1664 * machine with the opposite byte order to
1665 * ours, we byte-swap the AF_ value.
1667 * Then we run it through "htonl()", and
1668 * generate code to compare against the result.
1670 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1671 bpf_pcap
->sf
.swapped
)
1672 proto
= SWAPLONG(proto
);
1673 proto
= htonl(proto
);
1675 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1679 * af field is host byte order in contrast to the rest of
1682 if (proto
== ETHERTYPE_IP
)
1683 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1684 (bpf_int32
)AF_INET
));
1686 else if (proto
== ETHERTYPE_IPV6
)
1687 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1688 (bpf_int32
)AF_INET6
));
1696 case DLT_ARCNET_LINUX
:
1698 * XXX should we check for first fragment if the protocol
1707 case ETHERTYPE_IPV6
:
1708 return (gen_cmp(off_linktype
, BPF_B
,
1709 (bpf_int32
)ARCTYPE_INET6
));
1713 b0
= gen_cmp(off_linktype
, BPF_B
,
1714 (bpf_int32
)ARCTYPE_IP
);
1715 b1
= gen_cmp(off_linktype
, BPF_B
,
1716 (bpf_int32
)ARCTYPE_IP_OLD
);
1721 b0
= gen_cmp(off_linktype
, BPF_B
,
1722 (bpf_int32
)ARCTYPE_ARP
);
1723 b1
= gen_cmp(off_linktype
, BPF_B
,
1724 (bpf_int32
)ARCTYPE_ARP_OLD
);
1728 case ETHERTYPE_REVARP
:
1729 return (gen_cmp(off_linktype
, BPF_B
,
1730 (bpf_int32
)ARCTYPE_REVARP
));
1732 case ETHERTYPE_ATALK
:
1733 return (gen_cmp(off_linktype
, BPF_B
,
1734 (bpf_int32
)ARCTYPE_ATALK
));
1741 case ETHERTYPE_ATALK
:
1751 * XXX - assumes a 2-byte Frame Relay header with
1752 * DLCI and flags. What if the address is longer?
1758 * Check for the special NLPID for IP.
1760 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1763 case ETHERTYPE_IPV6
:
1765 * Check for the special NLPID for IPv6.
1767 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1772 * Check for several OSI protocols.
1774 * Frame Relay packets typically have an OSI
1775 * NLPID at the beginning; we check for each
1778 * What we check for is the NLPID and a frame
1779 * control field of UI, i.e. 0x03 followed
1782 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1783 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1784 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1795 case DLT_LINUX_IRDA
:
1796 bpf_error("IrDA link-layer type filtering not implemented");
1799 bpf_error("DOCSIS link-layer type filtering not implemented");
1803 * All the types that have no encapsulation should either be
1804 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1805 * all packets are IP packets, or should be handled in some
1806 * special case, if none of them are (if some are and some
1807 * aren't, the lack of encapsulation is a problem, as we'd
1808 * have to find some other way of determining the packet type).
1810 * Therefore, if "off_linktype" is -1, there's an error.
1812 if (off_linktype
== (u_int
)-1)
1816 * Any type not handled above should always have an Ethernet
1817 * type at an offset of "off_linktype". (PPP is partially
1818 * handled above - the protocol type is mapped from the
1819 * Ethernet and LLC types we use internally to the corresponding
1820 * PPP type - but the PPP type is always specified by a value
1821 * at "off_linktype", so we don't have to do the code generation
1824 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1828 * Check for an LLC SNAP packet with a given organization code and
1829 * protocol type; we check the entire contents of the 802.2 LLC and
1830 * snap headers, checking for DSAP and SSAP of SNAP and a control
1831 * field of 0x03 in the LLC header, and for the specified organization
1832 * code and protocol type in the SNAP header.
1834 static struct block
*
1835 gen_snap(orgcode
, ptype
, offset
)
1836 bpf_u_int32 orgcode
;
1840 u_char snapblock
[8];
1842 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1843 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1844 snapblock
[2] = 0x03; /* control = UI */
1845 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1846 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1847 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1848 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1849 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1850 return gen_bcmp(offset
, 8, snapblock
);
1854 * Check for a given protocol value assuming an 802.2 LLC header.
1856 static struct block
*
1861 * XXX - handle token-ring variable-length header.
1866 return gen_cmp(off_linktype
, BPF_H
, (long)
1867 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1870 return gen_cmp(off_linktype
, BPF_H
, (long)
1871 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1873 case LLCSAP_NETBEUI
:
1874 return gen_cmp(off_linktype
, BPF_H
, (long)
1875 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1879 * XXX - are there ever SNAP frames for IPX on
1880 * non-Ethernet 802.x networks?
1882 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1884 case ETHERTYPE_ATALK
:
1886 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1887 * SNAP packets with an organization code of
1888 * 0x080007 (Apple, for Appletalk) and a protocol
1889 * type of ETHERTYPE_ATALK (Appletalk).
1891 * XXX - check for an organization code of
1892 * encapsulated Ethernet as well?
1894 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1898 * XXX - we don't have to check for IPX 802.3
1899 * here, but should we check for the IPX Ethertype?
1901 if (proto
<= ETHERMTU
) {
1903 * This is an LLC SAP value, so check
1906 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1909 * This is an Ethernet type; we assume that it's
1910 * unlikely that it'll appear in the right place
1911 * at random, and therefore check only the
1912 * location that would hold the Ethernet type
1913 * in a SNAP frame with an organization code of
1914 * 0x000000 (encapsulated Ethernet).
1916 * XXX - if we were to check for the SNAP DSAP and
1917 * LSAP, as per XXX, and were also to check for an
1918 * organization code of 0x000000 (encapsulated
1919 * Ethernet), we'd do
1921 * return gen_snap(0x000000, proto,
1924 * here; for now, we don't, as per the above.
1925 * I don't know whether it's worth the extra CPU
1926 * time to do the right check or not.
1928 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1933 static struct block
*
1934 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1938 u_int src_off
, dst_off
;
1940 struct block
*b0
, *b1
;
1954 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1955 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1961 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1962 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1969 b0
= gen_linktype(proto
);
1970 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1976 static struct block
*
1977 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1978 struct in6_addr
*addr
;
1979 struct in6_addr
*mask
;
1981 u_int src_off
, dst_off
;
1983 struct block
*b0
, *b1
;
1998 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1999 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2005 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2006 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2013 /* this order is important */
2014 a
= (u_int32_t
*)addr
;
2015 m
= (u_int32_t
*)mask
;
2016 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2017 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2019 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2021 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2023 b0
= gen_linktype(proto
);
2029 static struct block
*
2030 gen_ehostop(eaddr
, dir
)
2031 register const u_char
*eaddr
;
2034 register struct block
*b0
, *b1
;
2038 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2041 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2044 b0
= gen_ehostop(eaddr
, Q_SRC
);
2045 b1
= gen_ehostop(eaddr
, Q_DST
);
2051 b0
= gen_ehostop(eaddr
, Q_SRC
);
2052 b1
= gen_ehostop(eaddr
, Q_DST
);
2061 * Like gen_ehostop, but for DLT_FDDI
2063 static struct block
*
2064 gen_fhostop(eaddr
, dir
)
2065 register const u_char
*eaddr
;
2068 struct block
*b0
, *b1
;
2073 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2075 return gen_bcmp(6 + 1, 6, eaddr
);
2080 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2082 return gen_bcmp(0 + 1, 6, eaddr
);
2086 b0
= gen_fhostop(eaddr
, Q_SRC
);
2087 b1
= gen_fhostop(eaddr
, Q_DST
);
2093 b0
= gen_fhostop(eaddr
, Q_SRC
);
2094 b1
= gen_fhostop(eaddr
, Q_DST
);
2103 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2105 static struct block
*
2106 gen_thostop(eaddr
, dir
)
2107 register const u_char
*eaddr
;
2110 register struct block
*b0
, *b1
;
2114 return gen_bcmp(8, 6, eaddr
);
2117 return gen_bcmp(2, 6, eaddr
);
2120 b0
= gen_thostop(eaddr
, Q_SRC
);
2121 b1
= gen_thostop(eaddr
, Q_DST
);
2127 b0
= gen_thostop(eaddr
, Q_SRC
);
2128 b1
= gen_thostop(eaddr
, Q_DST
);
2137 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2139 static struct block
*
2140 gen_wlanhostop(eaddr
, dir
)
2141 register const u_char
*eaddr
;
2144 register struct block
*b0
, *b1
, *b2
;
2145 register struct slist
*s
;
2152 * For control frames, there is no SA.
2154 * For management frames, SA is at an
2155 * offset of 10 from the beginning of
2158 * For data frames, SA is at an offset
2159 * of 10 from the beginning of the packet
2160 * if From DS is clear, at an offset of
2161 * 16 from the beginning of the packet
2162 * if From DS is set and To DS is clear,
2163 * and an offset of 24 from the beginning
2164 * of the packet if From DS is set and To DS
2169 * Generate the tests to be done for data frames
2172 * First, check for To DS set, i.e. check "link[1] & 0x01".
2174 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2176 b1
= new_block(JMP(BPF_JSET
));
2177 b1
->s
.k
= 0x01; /* To DS */
2181 * If To DS is set, the SA is at 24.
2183 b0
= gen_bcmp(24, 6, eaddr
);
2187 * Now, check for To DS not set, i.e. check
2188 * "!(link[1] & 0x01)".
2190 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2192 b2
= new_block(JMP(BPF_JSET
));
2193 b2
->s
.k
= 0x01; /* To DS */
2198 * If To DS is not set, the SA is at 16.
2200 b1
= gen_bcmp(16, 6, eaddr
);
2204 * Now OR together the last two checks. That gives
2205 * the complete set of checks for data frames with
2211 * Now check for From DS being set, and AND that with
2212 * the ORed-together checks.
2214 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2216 b1
= new_block(JMP(BPF_JSET
));
2217 b1
->s
.k
= 0x02; /* From DS */
2222 * Now check for data frames with From DS not set.
2224 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2226 b2
= new_block(JMP(BPF_JSET
));
2227 b2
->s
.k
= 0x02; /* From DS */
2232 * If From DS isn't set, the SA is at 10.
2234 b1
= gen_bcmp(10, 6, eaddr
);
2238 * Now OR together the checks for data frames with
2239 * From DS not set and for data frames with From DS
2240 * set; that gives the checks done for data frames.
2245 * Now check for a data frame.
2246 * I.e, check "link[0] & 0x08".
2248 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2250 b1
= new_block(JMP(BPF_JSET
));
2255 * AND that with the checks done for data frames.
2260 * If the high-order bit of the type value is 0, this
2261 * is a management frame.
2262 * I.e, check "!(link[0] & 0x08)".
2264 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2266 b2
= new_block(JMP(BPF_JSET
));
2272 * For management frames, the SA is at 10.
2274 b1
= gen_bcmp(10, 6, eaddr
);
2278 * OR that with the checks done for data frames.
2279 * That gives the checks done for management and
2285 * If the low-order bit of the type value is 1,
2286 * this is either a control frame or a frame
2287 * with a reserved type, and thus not a
2290 * I.e., check "!(link[0] & 0x04)".
2292 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2294 b1
= new_block(JMP(BPF_JSET
));
2300 * AND that with the checks for data and management
2310 * For control frames, there is no DA.
2312 * For management frames, DA is at an
2313 * offset of 4 from the beginning of
2316 * For data frames, DA is at an offset
2317 * of 4 from the beginning of the packet
2318 * if To DS is clear and at an offset of
2319 * 16 from the beginning of the packet
2324 * Generate the tests to be done for data frames.
2326 * First, check for To DS set, i.e. "link[1] & 0x01".
2328 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2330 b1
= new_block(JMP(BPF_JSET
));
2331 b1
->s
.k
= 0x01; /* To DS */
2335 * If To DS is set, the DA is at 16.
2337 b0
= gen_bcmp(16, 6, eaddr
);
2341 * Now, check for To DS not set, i.e. check
2342 * "!(link[1] & 0x01)".
2344 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2346 b2
= new_block(JMP(BPF_JSET
));
2347 b2
->s
.k
= 0x01; /* To DS */
2352 * If To DS is not set, the DA is at 4.
2354 b1
= gen_bcmp(4, 6, eaddr
);
2358 * Now OR together the last two checks. That gives
2359 * the complete set of checks for data frames.
2364 * Now check for a data frame.
2365 * I.e, check "link[0] & 0x08".
2367 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2369 b1
= new_block(JMP(BPF_JSET
));
2374 * AND that with the checks done for data frames.
2379 * If the high-order bit of the type value is 0, this
2380 * is a management frame.
2381 * I.e, check "!(link[0] & 0x08)".
2383 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2385 b2
= new_block(JMP(BPF_JSET
));
2391 * For management frames, the DA is at 4.
2393 b1
= gen_bcmp(4, 6, eaddr
);
2397 * OR that with the checks done for data frames.
2398 * That gives the checks done for management and
2404 * If the low-order bit of the type value is 1,
2405 * this is either a control frame or a frame
2406 * with a reserved type, and thus not a
2409 * I.e., check "!(link[0] & 0x04)".
2411 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2413 b1
= new_block(JMP(BPF_JSET
));
2419 * AND that with the checks for data and management
2426 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2427 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2433 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2434 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2443 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2444 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2445 * as the RFC states.)
2447 static struct block
*
2448 gen_ipfchostop(eaddr
, dir
)
2449 register const u_char
*eaddr
;
2452 register struct block
*b0
, *b1
;
2456 return gen_bcmp(10, 6, eaddr
);
2459 return gen_bcmp(2, 6, eaddr
);
2462 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2463 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2469 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2470 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2479 * This is quite tricky because there may be pad bytes in front of the
2480 * DECNET header, and then there are two possible data packet formats that
2481 * carry both src and dst addresses, plus 5 packet types in a format that
2482 * carries only the src node, plus 2 types that use a different format and
2483 * also carry just the src node.
2487 * Instead of doing those all right, we just look for data packets with
2488 * 0 or 1 bytes of padding. If you want to look at other packets, that
2489 * will require a lot more hacking.
2491 * To add support for filtering on DECNET "areas" (network numbers)
2492 * one would want to add a "mask" argument to this routine. That would
2493 * make the filter even more inefficient, although one could be clever
2494 * and not generate masking instructions if the mask is 0xFFFF.
2496 static struct block
*
2497 gen_dnhostop(addr
, dir
, base_off
)
2502 struct block
*b0
, *b1
, *b2
, *tmp
;
2503 u_int offset_lh
; /* offset if long header is received */
2504 u_int offset_sh
; /* offset if short header is received */
2509 offset_sh
= 1; /* follows flags */
2510 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2514 offset_sh
= 3; /* follows flags, dstnode */
2515 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2519 /* Inefficient because we do our Calvinball dance twice */
2520 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2521 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2527 /* Inefficient because we do our Calvinball dance twice */
2528 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2529 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2534 bpf_error("ISO host filtering not implemented");
2539 b0
= gen_linktype(ETHERTYPE_DN
);
2540 /* Check for pad = 1, long header case */
2541 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2542 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2543 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2544 BPF_H
, (bpf_int32
)ntohs(addr
));
2546 /* Check for pad = 0, long header case */
2547 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2548 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2551 /* Check for pad = 1, short header case */
2552 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2553 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2554 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2555 BPF_H
, (bpf_int32
)ntohs(addr
));
2558 /* Check for pad = 0, short header case */
2559 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2560 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2564 /* Combine with test for linktype */
2569 static struct block
*
2570 gen_host(addr
, mask
, proto
, dir
)
2576 struct block
*b0
, *b1
;
2581 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2582 if (off_linktype
!= (u_int
)-1) {
2583 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2585 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2591 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2592 off_nl
+ 12, off_nl
+ 16);
2595 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2596 off_nl
+ 14, off_nl
+ 24);
2599 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2600 off_nl
+ 14, off_nl
+ 24);
2603 bpf_error("'tcp' modifier applied to host");
2606 bpf_error("'sctp' modifier applied to host");
2609 bpf_error("'udp' modifier applied to host");
2612 bpf_error("'icmp' modifier applied to host");
2615 bpf_error("'igmp' modifier applied to host");
2618 bpf_error("'igrp' modifier applied to host");
2621 bpf_error("'pim' modifier applied to host");
2624 bpf_error("'vrrp' modifier applied to host");
2627 bpf_error("ATALK host filtering not implemented");
2630 bpf_error("AARP host filtering not implemented");
2633 return gen_dnhostop(addr
, dir
, off_nl
);
2636 bpf_error("SCA host filtering not implemented");
2639 bpf_error("LAT host filtering not implemented");
2642 bpf_error("MOPDL host filtering not implemented");
2645 bpf_error("MOPRC host filtering not implemented");
2649 bpf_error("'ip6' modifier applied to ip host");
2652 bpf_error("'icmp6' modifier applied to host");
2656 bpf_error("'ah' modifier applied to host");
2659 bpf_error("'esp' modifier applied to host");
2662 bpf_error("ISO host filtering not implemented");
2665 bpf_error("'esis' modifier applied to host");
2668 bpf_error("'isis' modifier applied to host");
2671 bpf_error("'clnp' modifier applied to host");
2674 bpf_error("'stp' modifier applied to host");
2677 bpf_error("IPX host filtering not implemented");
2680 bpf_error("'netbeui' modifier applied to host");
2689 static struct block
*
2690 gen_host6(addr
, mask
, proto
, dir
)
2691 struct in6_addr
*addr
;
2692 struct in6_addr
*mask
;
2699 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2702 bpf_error("'ip' modifier applied to ip6 host");
2705 bpf_error("'rarp' modifier applied to ip6 host");
2708 bpf_error("'arp' modifier applied to ip6 host");
2711 bpf_error("'sctp' modifier applied to host");
2714 bpf_error("'tcp' modifier applied to host");
2717 bpf_error("'udp' modifier applied to host");
2720 bpf_error("'icmp' modifier applied to host");
2723 bpf_error("'igmp' modifier applied to host");
2726 bpf_error("'igrp' modifier applied to host");
2729 bpf_error("'pim' modifier applied to host");
2732 bpf_error("'vrrp' modifier applied to host");
2735 bpf_error("ATALK host filtering not implemented");
2738 bpf_error("AARP host filtering not implemented");
2741 bpf_error("'decnet' modifier applied to ip6 host");
2744 bpf_error("SCA host filtering not implemented");
2747 bpf_error("LAT host filtering not implemented");
2750 bpf_error("MOPDL host filtering not implemented");
2753 bpf_error("MOPRC host filtering not implemented");
2756 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2757 off_nl
+ 8, off_nl
+ 24);
2760 bpf_error("'icmp6' modifier applied to host");
2763 bpf_error("'ah' modifier applied to host");
2766 bpf_error("'esp' modifier applied to host");
2769 bpf_error("ISO host filtering not implemented");
2772 bpf_error("'esis' modifier applied to host");
2775 bpf_error("'isis' modifier applied to host");
2778 bpf_error("'clnp' modifier applied to host");
2781 bpf_error("'stp' modifier applied to host");
2784 bpf_error("IPX host filtering not implemented");
2787 bpf_error("'netbeui' modifier applied to host");
2797 static struct block
*
2798 gen_gateway(eaddr
, alist
, proto
, dir
)
2799 const u_char
*eaddr
;
2800 bpf_u_int32
**alist
;
2804 struct block
*b0
, *b1
, *tmp
;
2807 bpf_error("direction applied to 'gateway'");
2814 if (linktype
== DLT_EN10MB
)
2815 b0
= gen_ehostop(eaddr
, Q_OR
);
2816 else if (linktype
== DLT_FDDI
)
2817 b0
= gen_fhostop(eaddr
, Q_OR
);
2818 else if (linktype
== DLT_IEEE802
)
2819 b0
= gen_thostop(eaddr
, Q_OR
);
2820 else if (linktype
== DLT_IEEE802_11
)
2821 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2822 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2824 * Check that the packet doesn't begin with an
2825 * LE Control marker. (We've already generated
2828 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2832 * Now check the MAC address.
2834 b0
= gen_ehostop(eaddr
, Q_OR
);
2836 } else if (linktype
== DLT_IP_OVER_FC
)
2837 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2840 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2842 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2844 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2852 bpf_error("illegal modifier of 'gateway'");
2858 gen_proto_abbrev(proto
)
2867 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2869 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2875 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2877 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2883 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2885 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2891 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2894 #ifndef IPPROTO_IGMP
2895 #define IPPROTO_IGMP 2
2899 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2902 #ifndef IPPROTO_IGRP
2903 #define IPPROTO_IGRP 9
2906 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2910 #define IPPROTO_PIM 103
2914 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2916 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2921 #ifndef IPPROTO_VRRP
2922 #define IPPROTO_VRRP 112
2926 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2930 b1
= gen_linktype(ETHERTYPE_IP
);
2934 b1
= gen_linktype(ETHERTYPE_ARP
);
2938 b1
= gen_linktype(ETHERTYPE_REVARP
);
2942 bpf_error("link layer applied in wrong context");
2945 b1
= gen_linktype(ETHERTYPE_ATALK
);
2949 b1
= gen_linktype(ETHERTYPE_AARP
);
2953 b1
= gen_linktype(ETHERTYPE_DN
);
2957 b1
= gen_linktype(ETHERTYPE_SCA
);
2961 b1
= gen_linktype(ETHERTYPE_LAT
);
2965 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2969 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2974 b1
= gen_linktype(ETHERTYPE_IPV6
);
2977 #ifndef IPPROTO_ICMPV6
2978 #define IPPROTO_ICMPV6 58
2981 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2986 #define IPPROTO_AH 51
2989 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2991 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2997 #define IPPROTO_ESP 50
3000 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3002 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3008 b1
= gen_linktype(LLCSAP_ISONS
);
3012 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3016 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3019 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3020 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3021 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3023 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3025 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3027 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3031 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3032 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3033 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3035 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3037 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3039 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3043 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3044 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3045 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3047 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3052 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3053 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3058 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3059 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3061 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3063 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3068 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3069 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3074 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3075 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3080 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3084 b1
= gen_linktype(LLCSAP_8021D
);
3088 b1
= gen_linktype(LLCSAP_IPX
);
3092 b1
= gen_linktype(LLCSAP_NETBEUI
);
3101 static struct block
*
3108 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3109 s
->s
.k
= off_nl
+ 6;
3110 b
= new_block(JMP(BPF_JSET
));
3118 static struct block
*
3119 gen_portatom(off
, v
)
3126 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3129 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3130 s
->next
->s
.k
= off_nl
+ off
;
3132 b
= new_block(JMP(BPF_JEQ
));
3140 static struct block
*
3141 gen_portatom6(off
, v
)
3145 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3150 gen_portop(port
, proto
, dir
)
3151 int port
, proto
, dir
;
3153 struct block
*b0
, *b1
, *tmp
;
3155 /* ip proto 'proto' */
3156 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3162 b1
= gen_portatom(0, (bpf_int32
)port
);
3166 b1
= gen_portatom(2, (bpf_int32
)port
);
3171 tmp
= gen_portatom(0, (bpf_int32
)port
);
3172 b1
= gen_portatom(2, (bpf_int32
)port
);
3177 tmp
= gen_portatom(0, (bpf_int32
)port
);
3178 b1
= gen_portatom(2, (bpf_int32
)port
);
3190 static struct block
*
3191 gen_port(port
, ip_proto
, dir
)
3196 struct block
*b0
, *b1
, *tmp
;
3201 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3202 * not LLC encapsulation with LLCSAP_IP.
3204 * For IEEE 802 networks - which includes 802.5 token ring
3205 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3206 * says that SNAP encapsulation is used, not LLC encapsulation
3209 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3210 * RFC 2225 say that SNAP encapsulation is used, not LLC
3211 * encapsulation with LLCSAP_IP.
3213 * So we always check for ETHERTYPE_IP.
3215 b0
= gen_linktype(ETHERTYPE_IP
);
3221 b1
= gen_portop(port
, ip_proto
, dir
);
3225 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3226 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3228 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3241 gen_portop6(port
, proto
, dir
)
3242 int port
, proto
, dir
;
3244 struct block
*b0
, *b1
, *tmp
;
3246 /* ip proto 'proto' */
3247 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3251 b1
= gen_portatom6(0, (bpf_int32
)port
);
3255 b1
= gen_portatom6(2, (bpf_int32
)port
);
3260 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3261 b1
= gen_portatom6(2, (bpf_int32
)port
);
3266 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3267 b1
= gen_portatom6(2, (bpf_int32
)port
);
3279 static struct block
*
3280 gen_port6(port
, ip_proto
, dir
)
3285 struct block
*b0
, *b1
, *tmp
;
3287 /* ether proto ip */
3288 b0
= gen_linktype(ETHERTYPE_IPV6
);
3294 b1
= gen_portop6(port
, ip_proto
, dir
);
3298 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3299 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3301 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3314 lookup_proto(name
, proto
)
3315 register const char *name
;
3325 v
= pcap_nametoproto(name
);
3326 if (v
== PROTO_UNDEF
)
3327 bpf_error("unknown ip proto '%s'", name
);
3331 /* XXX should look up h/w protocol type based on linktype */
3332 v
= pcap_nametoeproto(name
);
3333 if (v
== PROTO_UNDEF
)
3334 bpf_error("unknown ether proto '%s'", name
);
3338 if (strcmp(name
, "esis") == 0)
3340 else if (strcmp(name
, "isis") == 0)
3342 else if (strcmp(name
, "clnp") == 0)
3345 bpf_error("unknown osi proto '%s'", name
);
3365 static struct block
*
3366 gen_protochain(v
, proto
, dir
)
3371 #ifdef NO_PROTOCHAIN
3372 return gen_proto(v
, proto
, dir
);
3374 struct block
*b0
, *b
;
3375 struct slist
*s
[100];
3376 int fix2
, fix3
, fix4
, fix5
;
3377 int ahcheck
, again
, end
;
3379 int reg2
= alloc_reg();
3381 memset(s
, 0, sizeof(s
));
3382 fix2
= fix3
= fix4
= fix5
= 0;
3389 b0
= gen_protochain(v
, Q_IP
, dir
);
3390 b
= gen_protochain(v
, Q_IPV6
, dir
);
3394 bpf_error("bad protocol applied for 'protochain'");
3398 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3401 * s[0] is a dummy entry to protect other BPF insn from damaged
3402 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3403 * hard to find interdependency made by jump table fixup.
3406 s
[i
] = new_stmt(0); /*dummy*/
3411 b0
= gen_linktype(ETHERTYPE_IP
);
3414 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3415 s
[i
]->s
.k
= off_nl
+ 9;
3417 /* X = ip->ip_hl << 2 */
3418 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3424 b0
= gen_linktype(ETHERTYPE_IPV6
);
3426 /* A = ip6->ip_nxt */
3427 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3428 s
[i
]->s
.k
= off_nl
+ 6;
3430 /* X = sizeof(struct ip6_hdr) */
3431 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3437 bpf_error("unsupported proto to gen_protochain");
3441 /* again: if (A == v) goto end; else fall through; */
3443 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3445 s
[i
]->s
.jt
= NULL
; /*later*/
3446 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3450 #ifndef IPPROTO_NONE
3451 #define IPPROTO_NONE 59
3453 /* if (A == IPPROTO_NONE) goto end */
3454 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3455 s
[i
]->s
.jt
= NULL
; /*later*/
3456 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3457 s
[i
]->s
.k
= IPPROTO_NONE
;
3458 s
[fix5
]->s
.jf
= s
[i
];
3463 if (proto
== Q_IPV6
) {
3464 int v6start
, v6end
, v6advance
, j
;
3467 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3468 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3469 s
[i
]->s
.jt
= NULL
; /*later*/
3470 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3471 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3472 s
[fix2
]->s
.jf
= s
[i
];
3474 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3475 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3476 s
[i
]->s
.jt
= NULL
; /*later*/
3477 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3478 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3480 /* if (A == IPPROTO_ROUTING) goto v6advance */
3481 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3482 s
[i
]->s
.jt
= NULL
; /*later*/
3483 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3484 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3486 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3487 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3488 s
[i
]->s
.jt
= NULL
; /*later*/
3489 s
[i
]->s
.jf
= NULL
; /*later*/
3490 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3501 * X = X + (P[X + 1] + 1) * 8;
3504 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3506 /* A = P[X + packet head] */
3507 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3511 s
[i
] = new_stmt(BPF_ST
);
3515 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3518 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3522 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3524 /* A = P[X + packet head]; */
3525 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3529 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3533 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3537 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3540 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3544 /* goto again; (must use BPF_JA for backward jump) */
3545 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3546 s
[i
]->s
.k
= again
- i
- 1;
3547 s
[i
- 1]->s
.jf
= s
[i
];
3551 for (j
= v6start
; j
<= v6end
; j
++)
3552 s
[j
]->s
.jt
= s
[v6advance
];
3557 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3559 s
[fix2
]->s
.jf
= s
[i
];
3565 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3566 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3567 s
[i
]->s
.jt
= NULL
; /*later*/
3568 s
[i
]->s
.jf
= NULL
; /*later*/
3569 s
[i
]->s
.k
= IPPROTO_AH
;
3571 s
[fix3
]->s
.jf
= s
[ahcheck
];
3578 * X = X + (P[X + 1] + 2) * 4;
3581 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3583 /* A = P[X + packet head]; */
3584 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3588 s
[i
] = new_stmt(BPF_ST
);
3592 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3595 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3599 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3601 /* A = P[X + packet head] */
3602 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3606 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3610 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3614 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3617 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3621 /* goto again; (must use BPF_JA for backward jump) */
3622 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3623 s
[i
]->s
.k
= again
- i
- 1;
3628 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3630 s
[fix2
]->s
.jt
= s
[end
];
3631 s
[fix4
]->s
.jf
= s
[end
];
3632 s
[fix5
]->s
.jt
= s
[end
];
3639 for (i
= 0; i
< max
- 1; i
++)
3640 s
[i
]->next
= s
[i
+ 1];
3641 s
[max
- 1]->next
= NULL
;
3646 b
= new_block(JMP(BPF_JEQ
));
3647 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3657 static struct block
*
3658 gen_proto(v
, proto
, dir
)
3663 struct block
*b0
, *b1
;
3665 if (dir
!= Q_DEFAULT
)
3666 bpf_error("direction applied to 'proto'");
3671 b0
= gen_proto(v
, Q_IP
, dir
);
3672 b1
= gen_proto(v
, Q_IPV6
, dir
);
3680 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3681 * not LLC encapsulation with LLCSAP_IP.
3683 * For IEEE 802 networks - which includes 802.5 token ring
3684 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3685 * says that SNAP encapsulation is used, not LLC encapsulation
3688 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3689 * RFC 2225 say that SNAP encapsulation is used, not LLC
3690 * encapsulation with LLCSAP_IP.
3692 * So we always check for ETHERTYPE_IP.
3694 b0
= gen_linktype(ETHERTYPE_IP
);
3696 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3698 b1
= gen_protochain(v
, Q_IP
);
3708 * Frame Relay packets typically have an OSI
3709 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3710 * generates code to check for all the OSI
3711 * NLPIDs, so calling it and then adding a check
3712 * for the particular NLPID for which we're
3713 * looking is bogus, as we can just check for
3716 * What we check for is the NLPID and a frame
3717 * control field value of UI, i.e. 0x03 followed
3720 * XXX - assumes a 2-byte Frame Relay header with
3721 * DLCI and flags. What if the address is longer?
3723 * XXX - what about SNAP-encapsulated frames?
3725 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3731 * Cisco uses an Ethertype lookalike - for OSI,
3734 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3735 /* OSI in C-HDLC is stuffed with a fudge byte */
3736 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3741 b0
= gen_linktype(LLCSAP_ISONS
);
3742 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3748 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3750 * 4 is the offset of the PDU type relative to the IS-IS
3753 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3758 bpf_error("arp does not encapsulate another protocol");
3762 bpf_error("rarp does not encapsulate another protocol");
3766 bpf_error("atalk encapsulation is not specifiable");
3770 bpf_error("decnet encapsulation is not specifiable");
3774 bpf_error("sca does not encapsulate another protocol");
3778 bpf_error("lat does not encapsulate another protocol");
3782 bpf_error("moprc does not encapsulate another protocol");
3786 bpf_error("mopdl does not encapsulate another protocol");
3790 return gen_linktype(v
);
3793 bpf_error("'udp proto' is bogus");
3797 bpf_error("'tcp proto' is bogus");
3801 bpf_error("'sctp proto' is bogus");
3805 bpf_error("'icmp proto' is bogus");
3809 bpf_error("'igmp proto' is bogus");
3813 bpf_error("'igrp proto' is bogus");
3817 bpf_error("'pim proto' is bogus");
3821 bpf_error("'vrrp proto' is bogus");
3826 b0
= gen_linktype(ETHERTYPE_IPV6
);
3828 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3830 b1
= gen_protochain(v
, Q_IPV6
);
3836 bpf_error("'icmp6 proto' is bogus");
3840 bpf_error("'ah proto' is bogus");
3843 bpf_error("'ah proto' is bogus");
3846 bpf_error("'stp proto' is bogus");
3849 bpf_error("'ipx proto' is bogus");
3852 bpf_error("'netbeui proto' is bogus");
3863 register const char *name
;
3866 int proto
= q
.proto
;
3870 bpf_u_int32 mask
, addr
;
3872 bpf_u_int32
**alist
;
3875 struct sockaddr_in
*sin
;
3876 struct sockaddr_in6
*sin6
;
3877 struct addrinfo
*res
, *res0
;
3878 struct in6_addr mask128
;
3880 struct block
*b
, *tmp
;
3881 int port
, real_proto
;
3886 addr
= pcap_nametonetaddr(name
);
3888 bpf_error("unknown network '%s'", name
);
3889 /* Left justify network addr and calculate its network mask */
3891 while (addr
&& (addr
& 0xff000000) == 0) {
3895 return gen_host(addr
, mask
, proto
, dir
);
3899 if (proto
== Q_LINK
) {
3903 eaddr
= pcap_ether_hostton(name
);
3906 "unknown ether host '%s'", name
);
3907 b
= gen_ehostop(eaddr
, dir
);
3912 eaddr
= pcap_ether_hostton(name
);
3915 "unknown FDDI host '%s'", name
);
3916 b
= gen_fhostop(eaddr
, dir
);
3921 eaddr
= pcap_ether_hostton(name
);
3924 "unknown token ring host '%s'", name
);
3925 b
= gen_thostop(eaddr
, dir
);
3929 case DLT_IEEE802_11
:
3930 eaddr
= pcap_ether_hostton(name
);
3933 "unknown 802.11 host '%s'", name
);
3934 b
= gen_wlanhostop(eaddr
, dir
);
3938 case DLT_IP_OVER_FC
:
3939 eaddr
= pcap_ether_hostton(name
);
3942 "unknown Fibre Channel host '%s'", name
);
3943 b
= gen_ipfchostop(eaddr
, dir
);
3952 * Check that the packet doesn't begin
3953 * with an LE Control marker. (We've
3954 * already generated a test for LANE.)
3956 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3960 eaddr
= pcap_ether_hostton(name
);
3963 "unknown ether host '%s'", name
);
3964 b
= gen_ehostop(eaddr
, dir
);
3970 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3971 } else if (proto
== Q_DECNET
) {
3972 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3974 * I don't think DECNET hosts can be multihomed, so
3975 * there is no need to build up a list of addresses
3977 return (gen_host(dn_addr
, 0, proto
, dir
));
3980 alist
= pcap_nametoaddr(name
);
3981 if (alist
== NULL
|| *alist
== NULL
)
3982 bpf_error("unknown host '%s'", name
);
3984 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
3986 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3988 tmp
= gen_host(**alist
++, 0xffffffff,
3995 memset(&mask128
, 0xff, sizeof(mask128
));
3996 res0
= res
= pcap_nametoaddrinfo(name
);
3998 bpf_error("unknown host '%s'", name
);
4000 tproto
= tproto6
= proto
;
4001 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4005 for (res
= res0
; res
; res
= res
->ai_next
) {
4006 switch (res
->ai_family
) {
4008 if (tproto
== Q_IPV6
)
4011 sin
= (struct sockaddr_in
*)
4013 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4014 0xffffffff, tproto
, dir
);
4017 if (tproto6
== Q_IP
)
4020 sin6
= (struct sockaddr_in6
*)
4022 tmp
= gen_host6(&sin6
->sin6_addr
,
4023 &mask128
, tproto6
, dir
);
4034 bpf_error("unknown host '%s'%s", name
,
4035 (proto
== Q_DEFAULT
)
4037 : " for specified address family");
4044 if (proto
!= Q_DEFAULT
&&
4045 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4046 bpf_error("illegal qualifier of 'port'");
4047 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4048 bpf_error("unknown port '%s'", name
);
4049 if (proto
== Q_UDP
) {
4050 if (real_proto
== IPPROTO_TCP
)
4051 bpf_error("port '%s' is tcp", name
);
4052 else if (real_proto
== IPPROTO_SCTP
)
4053 bpf_error("port '%s' is sctp", name
);
4055 /* override PROTO_UNDEF */
4056 real_proto
= IPPROTO_UDP
;
4058 if (proto
== Q_TCP
) {
4059 if (real_proto
== IPPROTO_UDP
)
4060 bpf_error("port '%s' is udp", name
);
4062 else if (real_proto
== IPPROTO_SCTP
)
4063 bpf_error("port '%s' is sctp", name
);
4065 /* override PROTO_UNDEF */
4066 real_proto
= IPPROTO_TCP
;
4068 if (proto
== Q_SCTP
) {
4069 if (real_proto
== IPPROTO_UDP
)
4070 bpf_error("port '%s' is udp", name
);
4072 else if (real_proto
== IPPROTO_TCP
)
4073 bpf_error("port '%s' is tcp", name
);
4075 /* override PROTO_UNDEF */
4076 real_proto
= IPPROTO_SCTP
;
4079 return gen_port(port
, real_proto
, dir
);
4083 b
= gen_port(port
, real_proto
, dir
);
4084 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4091 eaddr
= pcap_ether_hostton(name
);
4093 bpf_error("unknown ether host: %s", name
);
4095 alist
= pcap_nametoaddr(name
);
4096 if (alist
== NULL
|| *alist
== NULL
)
4097 bpf_error("unknown host '%s'", name
);
4098 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4102 bpf_error("'gateway' not supported in this configuration");
4106 real_proto
= lookup_proto(name
, proto
);
4107 if (real_proto
>= 0)
4108 return gen_proto(real_proto
, proto
, dir
);
4110 bpf_error("unknown protocol: %s", name
);
4113 real_proto
= lookup_proto(name
, proto
);
4114 if (real_proto
>= 0)
4115 return gen_protochain(real_proto
, proto
, dir
);
4117 bpf_error("unknown protocol: %s", name
);
4129 gen_mcode(s1
, s2
, masklen
, q
)
4130 register const char *s1
, *s2
;
4131 register int masklen
;
4134 register int nlen
, mlen
;
4137 nlen
= __pcap_atoin(s1
, &n
);
4138 /* Promote short ipaddr */
4142 mlen
= __pcap_atoin(s2
, &m
);
4143 /* Promote short ipaddr */
4146 bpf_error("non-network bits set in \"%s mask %s\"",
4149 /* Convert mask len to mask */
4151 bpf_error("mask length must be <= 32");
4152 m
= 0xffffffff << (32 - masklen
);
4154 bpf_error("non-network bits set in \"%s/%d\"",
4161 return gen_host(n
, m
, q
.proto
, q
.dir
);
4164 bpf_error("Mask syntax for networks only");
4172 register const char *s
;
4177 int proto
= q
.proto
;
4183 else if (q
.proto
== Q_DECNET
)
4184 vlen
= __pcap_atodn(s
, &v
);
4186 vlen
= __pcap_atoin(s
, &v
);
4193 if (proto
== Q_DECNET
)
4194 return gen_host(v
, 0, proto
, dir
);
4195 else if (proto
== Q_LINK
) {
4196 bpf_error("illegal link layer address");
4199 if (s
== NULL
&& q
.addr
== Q_NET
) {
4200 /* Promote short net number */
4201 while (v
&& (v
& 0xff000000) == 0) {
4206 /* Promote short ipaddr */
4210 return gen_host(v
, mask
, proto
, dir
);
4215 proto
= IPPROTO_UDP
;
4216 else if (proto
== Q_TCP
)
4217 proto
= IPPROTO_TCP
;
4218 else if (proto
== Q_SCTP
)
4219 proto
= IPPROTO_SCTP
;
4220 else if (proto
== Q_DEFAULT
)
4221 proto
= PROTO_UNDEF
;
4223 bpf_error("illegal qualifier of 'port'");
4226 return gen_port((int)v
, proto
, dir
);
4230 b
= gen_port((int)v
, proto
, dir
);
4231 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4237 bpf_error("'gateway' requires a name");
4241 return gen_proto((int)v
, proto
, dir
);
4244 return gen_protochain((int)v
, proto
, dir
);
4259 gen_mcode6(s1
, s2
, masklen
, q
)
4260 register const char *s1
, *s2
;
4261 register int masklen
;
4264 struct addrinfo
*res
;
4265 struct in6_addr
*addr
;
4266 struct in6_addr mask
;
4271 bpf_error("no mask %s supported", s2
);
4273 res
= pcap_nametoaddrinfo(s1
);
4275 bpf_error("invalid ip6 address %s", s1
);
4277 bpf_error("%s resolved to multiple address", s1
);
4278 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4280 if (sizeof(mask
) * 8 < masklen
)
4281 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4282 memset(&mask
, 0, sizeof(mask
));
4283 memset(&mask
, 0xff, masklen
/ 8);
4285 mask
.s6_addr
[masklen
/ 8] =
4286 (0xff << (8 - masklen
% 8)) & 0xff;
4289 a
= (u_int32_t
*)addr
;
4290 m
= (u_int32_t
*)&mask
;
4291 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4292 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4293 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4301 bpf_error("Mask syntax for networks only");
4305 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4310 bpf_error("invalid qualifier against IPv6 address");
4318 register const u_char
*eaddr
;
4321 struct block
*b
, *tmp
;
4323 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4324 if (linktype
== DLT_EN10MB
)
4325 return gen_ehostop(eaddr
, (int)q
.dir
);
4326 if (linktype
== DLT_FDDI
)
4327 return gen_fhostop(eaddr
, (int)q
.dir
);
4328 if (linktype
== DLT_IEEE802
)
4329 return gen_thostop(eaddr
, (int)q
.dir
);
4330 if (linktype
== DLT_IEEE802_11
)
4331 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4332 if (linktype
== DLT_SUNATM
&& is_lane
) {
4334 * Check that the packet doesn't begin with an
4335 * LE Control marker. (We've already generated
4338 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4342 * Now check the MAC address.
4344 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4348 if (linktype
== DLT_IP_OVER_FC
)
4349 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4350 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4352 bpf_error("ethernet address used in non-ether expression");
4358 struct slist
*s0
, *s1
;
4361 * This is definitely not the best way to do this, but the
4362 * lists will rarely get long.
4369 static struct slist
*
4375 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4380 static struct slist
*
4386 s
= new_stmt(BPF_LD
|BPF_MEM
);
4392 gen_load(proto
, index
, size
)
4397 struct slist
*s
, *tmp
;
4399 int regno
= alloc_reg();
4401 free_reg(index
->regno
);
4405 bpf_error("data size must be 1, 2, or 4");
4421 bpf_error("unsupported index operation");
4425 * XXX - what about ATM LANE? Should the index be
4426 * relative to the beginning of the AAL5 frame, so
4427 * that 0 refers to the beginning of the LE Control
4428 * field, or relative to the beginning of the LAN
4429 * frame, so that 0 refers, for Ethernet LANE, to
4430 * the beginning of the destination address?
4432 s
= xfer_to_x(index
);
4433 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4435 sappend(index
->s
, s
);
4450 /* XXX Note that we assume a fixed link header here. */
4451 s
= xfer_to_x(index
);
4452 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4455 sappend(index
->s
, s
);
4457 b
= gen_proto_abbrev(proto
);
4459 gen_and(index
->b
, b
);
4471 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4473 sappend(s
, xfer_to_a(index
));
4474 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4475 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4476 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4478 sappend(index
->s
, s
);
4480 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4482 gen_and(index
->b
, b
);
4484 gen_and(gen_proto_abbrev(Q_IP
), b
);
4490 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4494 index
->regno
= regno
;
4495 s
= new_stmt(BPF_ST
);
4497 sappend(index
->s
, s
);
4503 gen_relation(code
, a0
, a1
, reversed
)
4505 struct arth
*a0
, *a1
;
4508 struct slist
*s0
, *s1
, *s2
;
4509 struct block
*b
, *tmp
;
4513 if (code
== BPF_JEQ
) {
4514 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4515 b
= new_block(JMP(code
));
4519 b
= new_block(BPF_JMP
|code
|BPF_X
);
4525 sappend(a0
->s
, a1
->s
);
4529 free_reg(a0
->regno
);
4530 free_reg(a1
->regno
);
4532 /* 'and' together protocol checks */
4535 gen_and(a0
->b
, tmp
= a1
->b
);
4551 int regno
= alloc_reg();
4552 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4555 s
= new_stmt(BPF_LD
|BPF_LEN
);
4556 s
->next
= new_stmt(BPF_ST
);
4557 s
->next
->s
.k
= regno
;
4572 a
= (struct arth
*)newchunk(sizeof(*a
));
4576 s
= new_stmt(BPF_LD
|BPF_IMM
);
4578 s
->next
= new_stmt(BPF_ST
);
4594 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4597 s
= new_stmt(BPF_ST
);
4605 gen_arth(code
, a0
, a1
)
4607 struct arth
*a0
, *a1
;
4609 struct slist
*s0
, *s1
, *s2
;
4613 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4618 sappend(a0
->s
, a1
->s
);
4620 free_reg(a0
->regno
);
4621 free_reg(a1
->regno
);
4623 s0
= new_stmt(BPF_ST
);
4624 a0
->regno
= s0
->s
.k
= alloc_reg();
4631 * Here we handle simple allocation of the scratch registers.
4632 * If too many registers are alloc'd, the allocator punts.
4634 static int regused
[BPF_MEMWORDS
];
4638 * Return the next free register.
4643 int n
= BPF_MEMWORDS
;
4646 if (regused
[curreg
])
4647 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4649 regused
[curreg
] = 1;
4653 bpf_error("too many registers needed to evaluate expression");
4658 * Return a register to the table so it can
4668 static struct block
*
4675 s
= new_stmt(BPF_LD
|BPF_LEN
);
4676 b
= new_block(JMP(jmp
));
4687 return gen_len(BPF_JGE
, n
);
4691 * Actually, this is less than or equal.
4699 b
= gen_len(BPF_JGT
, n
);
4706 gen_byteop(op
, idx
, val
)
4717 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4720 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4721 b
->s
.code
= JMP(BPF_JGE
);
4726 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4727 b
->s
.code
= JMP(BPF_JGT
);
4731 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4735 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4739 b
= new_block(JMP(BPF_JEQ
));
4746 static u_char abroadcast
[] = { 0x0 };
4749 gen_broadcast(proto
)
4752 bpf_u_int32 hostmask
;
4753 struct block
*b0
, *b1
, *b2
;
4754 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4760 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4761 return gen_ahostop(abroadcast
, Q_DST
);
4762 if (linktype
== DLT_EN10MB
)
4763 return gen_ehostop(ebroadcast
, Q_DST
);
4764 if (linktype
== DLT_FDDI
)
4765 return gen_fhostop(ebroadcast
, Q_DST
);
4766 if (linktype
== DLT_IEEE802
)
4767 return gen_thostop(ebroadcast
, Q_DST
);
4768 if (linktype
== DLT_IEEE802_11
)
4769 return gen_wlanhostop(ebroadcast
, Q_DST
);
4770 if (linktype
== DLT_IP_OVER_FC
)
4771 return gen_ipfchostop(ebroadcast
, Q_DST
);
4772 if (linktype
== DLT_SUNATM
&& is_lane
) {
4774 * Check that the packet doesn't begin with an
4775 * LE Control marker. (We've already generated
4778 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4782 * Now check the MAC address.
4784 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4788 bpf_error("not a broadcast link");
4792 b0
= gen_linktype(ETHERTYPE_IP
);
4793 hostmask
= ~netmask
;
4794 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4795 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4796 (bpf_int32
)(~0 & hostmask
), hostmask
);
4801 bpf_error("only link-layer/IP broadcast filters supported");
4806 * Generate code to test the low-order bit of a MAC address (that's
4807 * the bottom bit of the *first* byte).
4809 static struct block
*
4810 gen_mac_multicast(offset
)
4813 register struct block
*b0
;
4814 register struct slist
*s
;
4816 /* link[offset] & 1 != 0 */
4817 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4819 b0
= new_block(JMP(BPF_JSET
));
4826 gen_multicast(proto
)
4829 register struct block
*b0
, *b1
, *b2
;
4830 register struct slist
*s
;
4836 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4837 /* all ARCnet multicasts use the same address */
4838 return gen_ahostop(abroadcast
, Q_DST
);
4840 if (linktype
== DLT_EN10MB
) {
4841 /* ether[0] & 1 != 0 */
4842 return gen_mac_multicast(0);
4845 if (linktype
== DLT_FDDI
) {
4847 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4849 * XXX - was that referring to bit-order issues?
4851 /* fddi[1] & 1 != 0 */
4852 return gen_mac_multicast(1);
4855 if (linktype
== DLT_IEEE802
) {
4856 /* tr[2] & 1 != 0 */
4857 return gen_mac_multicast(2);
4860 if (linktype
== DLT_IEEE802_11
) {
4864 * For control frames, there is no DA.
4866 * For management frames, DA is at an
4867 * offset of 4 from the beginning of
4870 * For data frames, DA is at an offset
4871 * of 4 from the beginning of the packet
4872 * if To DS is clear and at an offset of
4873 * 16 from the beginning of the packet
4878 * Generate the tests to be done for data frames.
4880 * First, check for To DS set, i.e. "link[1] & 0x01".
4882 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4884 b1
= new_block(JMP(BPF_JSET
));
4885 b1
->s
.k
= 0x01; /* To DS */
4889 * If To DS is set, the DA is at 16.
4891 b0
= gen_mac_multicast(16);
4895 * Now, check for To DS not set, i.e. check
4896 * "!(link[1] & 0x01)".
4898 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4900 b2
= new_block(JMP(BPF_JSET
));
4901 b2
->s
.k
= 0x01; /* To DS */
4906 * If To DS is not set, the DA is at 4.
4908 b1
= gen_mac_multicast(4);
4912 * Now OR together the last two checks. That gives
4913 * the complete set of checks for data frames.
4918 * Now check for a data frame.
4919 * I.e, check "link[0] & 0x08".
4921 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4923 b1
= new_block(JMP(BPF_JSET
));
4928 * AND that with the checks done for data frames.
4933 * If the high-order bit of the type value is 0, this
4934 * is a management frame.
4935 * I.e, check "!(link[0] & 0x08)".
4937 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4939 b2
= new_block(JMP(BPF_JSET
));
4945 * For management frames, the DA is at 4.
4947 b1
= gen_mac_multicast(4);
4951 * OR that with the checks done for data frames.
4952 * That gives the checks done for management and
4958 * If the low-order bit of the type value is 1,
4959 * this is either a control frame or a frame
4960 * with a reserved type, and thus not a
4963 * I.e., check "!(link[0] & 0x04)".
4965 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4967 b1
= new_block(JMP(BPF_JSET
));
4973 * AND that with the checks for data and management
4980 if (linktype
== DLT_IP_OVER_FC
) {
4981 b0
= gen_mac_multicast(2);
4985 if (linktype
== DLT_SUNATM
&& is_lane
) {
4987 * Check that the packet doesn't begin with an
4988 * LE Control marker. (We've already generated
4991 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4994 /* ether[off_mac] & 1 != 0 */
4995 b0
= gen_mac_multicast(off_mac
);
5000 /* Link not known to support multicasts */
5004 b0
= gen_linktype(ETHERTYPE_IP
);
5005 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5006 b1
->s
.code
= JMP(BPF_JGE
);
5012 b0
= gen_linktype(ETHERTYPE_IPV6
);
5013 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5018 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5023 * generate command for inbound/outbound. It's here so we can
5024 * make it link-type specific. 'dir' = 0 implies "inbound",
5025 * = 1 implies "outbound".
5031 register struct block
*b0
;
5034 * Only some data link types support inbound/outbound qualifiers.
5038 b0
= gen_relation(BPF_JEQ
,
5039 gen_load(Q_LINK
, gen_loadi(0), 1),
5047 * Match packets sent by this machine.
5049 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5052 * Match packets sent to this machine.
5053 * (No broadcast or multicast packets, or
5054 * packets sent to some other machine and
5055 * received promiscuously.)
5057 * XXX - packets sent to other machines probably
5058 * shouldn't be matched, but what about broadcast
5059 * or multicast packets we received?
5061 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5066 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5067 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5071 bpf_error("inbound/outbound not supported on linktype %d",
5079 /* PF firewall log matched interface */
5081 gen_pf_ifname(const char *ifname
)
5086 if (linktype
== DLT_PFLOG
) {
5087 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5088 off
= offsetof(struct pfloghdr
, ifname
);
5090 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5093 if (strlen(ifname
) >= len
) {
5094 bpf_error("ifname interface names can only be %d characters",
5098 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5102 /* PF firewall log matched interface */
5104 gen_pf_ruleset(char *ruleset
)
5108 if (linktype
!= DLT_PFLOG
) {
5109 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5112 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5113 bpf_error("ruleset names can only be %ld characters",
5114 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5117 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5118 strlen(ruleset
), (const u_char
*)ruleset
);
5122 /* PF firewall log rule number */
5128 if (linktype
== DLT_PFLOG
) {
5129 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5132 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5139 /* PF firewall log sub-rule number */
5141 gen_pf_srnr(int srnr
)
5145 if (linktype
!= DLT_PFLOG
) {
5146 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5150 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5155 /* PF firewall log reason code */
5157 gen_pf_reason(int reason
)
5161 if (linktype
== DLT_PFLOG
) {
5162 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5165 bpf_error("reason not supported on linktype 0x%x", linktype
);
5172 /* PF firewall log action */
5174 gen_pf_action(int action
)
5178 if (linktype
== DLT_PFLOG
) {
5179 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5182 bpf_error("action not supported on linktype 0x%x", linktype
);
5191 register const u_char
*eaddr
;
5194 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5195 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5196 return gen_ahostop(eaddr
, (int)q
.dir
);
5198 bpf_error("ARCnet address used in non-arc expression");
5202 static struct block
*
5203 gen_ahostop(eaddr
, dir
)
5204 register const u_char
*eaddr
;
5207 register struct block
*b0
, *b1
;
5210 /* src comes first, different from Ethernet */
5212 return gen_bcmp(0, 1, eaddr
);
5215 return gen_bcmp(1, 1, eaddr
);
5218 b0
= gen_ahostop(eaddr
, Q_SRC
);
5219 b1
= gen_ahostop(eaddr
, Q_DST
);
5225 b0
= gen_ahostop(eaddr
, Q_SRC
);
5226 b1
= gen_ahostop(eaddr
, Q_DST
);
5235 * support IEEE 802.1Q VLAN trunk over ethernet
5244 * Change the offsets to point to the type and data fields within
5245 * the VLAN packet. This is somewhat of a kludge.
5247 if (orig_nl
== (u_int
)-1) {
5248 orig_linktype
= off_linktype
; /* save original values */
5250 orig_nl_nosnap
= off_nl_nosnap
;
5261 bpf_error("no VLAN support for data link type %d",
5267 /* check for VLAN */
5268 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5270 /* If a specific VLAN is requested, check VLAN id */
5271 if (vlan_num
>= 0) {
5274 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
5292 * Change the offsets to point to the type and data fields within
5293 * the MPLS packet. This is somewhat of a kludge.
5295 if (orig_nl
== (u_int
)-1) {
5296 orig_linktype
= off_linktype
; /* save original values */
5298 orig_nl_nosnap
= off_nl_nosnap
;
5307 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5315 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5323 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5326 /* FIXME add other DLT_s ...
5327 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5328 * leave it for now */
5331 bpf_error("no MPLS support for data link type %d",
5337 /* If a specific MPLS label is requested, check it */
5338 if (label_num
>= 0) {
5341 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5342 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5351 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5363 bpf_error("'vpi' supported only on raw ATM");
5364 if (off_vpi
== (u_int
)-1)
5366 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5367 (u_int
)jvalue
, reverse
);
5372 bpf_error("'vci' supported only on raw ATM");
5373 if (off_vci
== (u_int
)-1)
5375 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5376 (u_int
)jvalue
, reverse
);
5380 if (off_proto
== (u_int
)-1)
5381 abort(); /* XXX - this isn't on FreeBSD */
5382 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5383 (u_int
)jvalue
, reverse
);
5387 if (off_payload
== (u_int
)-1)
5389 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5390 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5395 bpf_error("'callref' supported only on raw ATM");
5396 if (off_proto
== (u_int
)-1)
5398 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5399 (u_int
)jvalue
, reverse
);
5409 gen_atmtype_abbrev(type
)
5412 struct block
*b0
, *b1
;
5417 /* Get all packets in Meta signalling Circuit */
5419 bpf_error("'metac' supported only on raw ATM");
5420 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5421 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5426 /* Get all packets in Broadcast Circuit*/
5428 bpf_error("'bcc' supported only on raw ATM");
5429 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5430 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5435 /* Get all cells in Segment OAM F4 circuit*/
5437 bpf_error("'oam4sc' supported only on raw ATM");
5438 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5439 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5444 /* Get all cells in End-to-End OAM F4 Circuit*/
5446 bpf_error("'oam4ec' supported only on raw ATM");
5447 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5448 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5453 /* Get all packets in connection Signalling Circuit */
5455 bpf_error("'sc' supported only on raw ATM");
5456 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5457 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5462 /* Get all packets in ILMI Circuit */
5464 bpf_error("'ilmic' supported only on raw ATM");
5465 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5466 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5471 /* Get all LANE packets */
5473 bpf_error("'lane' supported only on raw ATM");
5474 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5477 * Arrange that all subsequent tests assume LANE
5478 * rather than LLC-encapsulated packets, and set
5479 * the offsets appropriately for LANE-encapsulated
5482 * "off_mac" is the offset of the Ethernet header,
5483 * which is 2 bytes past the ATM pseudo-header
5484 * (skipping the pseudo-header and 2-byte LE Client
5485 * field). The other offsets are Ethernet offsets
5486 * relative to "off_mac".
5489 off_mac
= off_payload
+ 2; /* MAC header */
5490 off_linktype
= off_mac
+ 12;
5491 off_nl
= off_mac
+ 14; /* Ethernet II */
5492 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5496 /* Get all LLC-encapsulated packets */
5498 bpf_error("'llc' supported only on raw ATM");
5499 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5510 static struct block
*
5511 gen_msg_abbrev(type
)
5517 * Q.2931 signalling protocol messages for handling virtual circuits
5518 * establishment and teardown
5523 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5527 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5531 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5535 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5539 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5542 case A_RELEASE_DONE
:
5543 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5553 gen_atmmulti_abbrev(type
)
5556 struct block
*b0
, *b1
;
5562 bpf_error("'oam' supported only on raw ATM");
5563 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5568 bpf_error("'oamf4' supported only on raw ATM");
5570 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5571 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5573 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5579 * Get Q.2931 signalling messages for switched
5580 * virtual connection
5583 bpf_error("'connectmsg' supported only on raw ATM");
5584 b0
= gen_msg_abbrev(A_SETUP
);
5585 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5587 b0
= gen_msg_abbrev(A_CONNECT
);
5589 b0
= gen_msg_abbrev(A_CONNECTACK
);
5591 b0
= gen_msg_abbrev(A_RELEASE
);
5593 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5595 b0
= gen_atmtype_abbrev(A_SC
);
5601 bpf_error("'metaconnect' supported only on raw ATM");
5602 b0
= gen_msg_abbrev(A_SETUP
);
5603 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5605 b0
= gen_msg_abbrev(A_CONNECT
);
5607 b0
= gen_msg_abbrev(A_RELEASE
);
5609 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5611 b0
= gen_atmtype_abbrev(A_METAC
);