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.209 2004-08-27 07:37:10 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #define offsetof(s, e) ((size_t)&((s *)0)->e)
79 #include <netdb.h> /* for "struct addrinfo" */
82 #include <pcap-namedb.h>
87 #define IPPROTO_SCTP 132
90 #ifdef HAVE_OS_PROTO_H
94 #define JMP(c) ((c)|BPF_JMP|BPF_K)
97 static jmp_buf top_ctx
;
98 static pcap_t
*bpf_pcap
;
100 /* Hack for updating VLAN, MPLS offsets. */
101 static u_int orig_linktype
= -1U, orig_nl
= -1U, orig_nl_nosnap
= -1U;
105 int pcap_fddipad
= PCAP_FDDIPAD
;
112 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(int);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * We divy out chunks of memory rather than call malloc each time so
135 * we don't have to worry about leaking memory. It's probably
136 * not a big deal if all this memory was wasted but it this ever
137 * goes into a library that would probably not be a good idea.
140 #define CHUNK0SIZE 1024
146 static struct chunk chunks
[NCHUNKS
];
147 static int cur_chunk
;
149 static void *newchunk(u_int
);
150 static void freechunks(void);
151 static inline struct block
*new_block(int);
152 static inline struct slist
*new_stmt(int);
153 static struct block
*gen_retblk(int);
154 static inline void syntax(void);
156 static void backpatch(struct block
*, struct block
*);
157 static void merge(struct block
*, struct block
*);
158 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
159 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
160 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
161 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
162 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
163 bpf_u_int32
, bpf_u_int32
, int);
164 static struct block
*gen_uncond(int);
165 static inline struct block
*gen_true(void);
166 static inline struct block
*gen_false(void);
167 static struct block
*gen_ether_linktype(int);
168 static struct block
*gen_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_PPP_WITHDIRECTION
:
754 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
755 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
758 off_nl_nosnap
= 4; /* no 802.2 LLC */
763 * This does no include the Ethernet header, and
764 * only covers session state.
768 off_nl_nosnap
= 8; /* no 802.2 LLC */
774 off_nl_nosnap
= 24; /* no 802.2 LLC */
779 * FDDI doesn't really have a link-level type field.
780 * We set "off_linktype" to the offset of the LLC header.
782 * To check for Ethernet types, we assume that SSAP = SNAP
783 * is being used and pick out the encapsulated Ethernet type.
784 * XXX - should we generate code to check for SNAP?
788 off_linktype
+= pcap_fddipad
;
790 off_nl
= 21; /* FDDI+802.2+SNAP */
791 off_nl_nosnap
= 16; /* FDDI+802.2 */
793 off_nl
+= pcap_fddipad
;
794 off_nl_nosnap
+= pcap_fddipad
;
800 * Token Ring doesn't really have a link-level type field.
801 * We set "off_linktype" to the offset of the LLC header.
803 * To check for Ethernet types, we assume that SSAP = SNAP
804 * is being used and pick out the encapsulated Ethernet type.
805 * XXX - should we generate code to check for SNAP?
807 * XXX - the header is actually variable-length.
808 * Some various Linux patched versions gave 38
809 * as "off_linktype" and 40 as "off_nl"; however,
810 * if a token ring packet has *no* routing
811 * information, i.e. is not source-routed, the correct
812 * values are 20 and 22, as they are in the vanilla code.
814 * A packet is source-routed iff the uppermost bit
815 * of the first byte of the source address, at an
816 * offset of 8, has the uppermost bit set. If the
817 * packet is source-routed, the total number of bytes
818 * of routing information is 2 plus bits 0x1F00 of
819 * the 16-bit value at an offset of 14 (shifted right
820 * 8 - figure out which byte that is).
823 off_nl
= 22; /* Token Ring+802.2+SNAP */
824 off_nl_nosnap
= 17; /* Token Ring+802.2 */
829 * 802.11 doesn't really have a link-level type field.
830 * We set "off_linktype" to the offset of the LLC header.
832 * To check for Ethernet types, we assume that SSAP = SNAP
833 * is being used and pick out the encapsulated Ethernet type.
834 * XXX - should we generate code to check for SNAP?
836 * XXX - the header is actually variable-length. We
837 * assume a 24-byte link-layer header, as appears in
838 * data frames in networks with no bridges. If the
839 * fromds and tods 802.11 header bits are both set,
840 * it's actually supposed to be 30 bytes.
843 off_nl
= 32; /* 802.11+802.2+SNAP */
844 off_nl_nosnap
= 27; /* 802.11+802.2 */
847 case DLT_PRISM_HEADER
:
849 * Same as 802.11, but with an additional header before
850 * the 802.11 header, containing a bunch of additional
851 * information including radio-level information.
853 * The header is 144 bytes long.
855 * XXX - same variable-length header problem; at least
856 * the Prism header is fixed-length.
858 off_linktype
= 144+24;
859 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
860 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
863 case DLT_IEEE802_11_RADIO_AVS
:
865 * Same as 802.11, but with an additional header before
866 * the 802.11 header, containing a bunch of additional
867 * information including radio-level information.
869 * The header is 64 bytes long, at least in its
870 * current incarnation.
872 * XXX - same variable-length header problem, only
873 * more so; this header is also variable-length,
874 * with the length being the 32-bit big-endian
875 * number at an offset of 4 from the beginning
876 * of the radio header.
878 off_linktype
= 64+24;
879 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
880 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
883 case DLT_IEEE802_11_RADIO
:
885 * Same as 802.11, but with an additional header before
886 * the 802.11 header, containing a bunch of additional
887 * information including radio-level information.
889 * XXX - same variable-length header problem, only
890 * even *more* so; this header is also variable-length,
891 * with the length being the 16-bit number at an offset
892 * of 2 from the beginning of the radio header, and it's
893 * device-dependent (different devices might supply
894 * different amounts of information), so we can't even
895 * assume a fixed length for the current version of the
898 * Therefore, currently, only raw "link[N:M]" filtering is
906 case DLT_ATM_RFC1483
:
907 case DLT_ATM_CLIP
: /* Linux ATM defines this */
909 * assume routed, non-ISO PDUs
910 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
913 off_nl
= 8; /* 802.2+SNAP */
914 off_nl_nosnap
= 3; /* 802.2 */
919 * Full Frontal ATM; you get AALn PDUs with an ATM
923 off_vpi
= SUNATM_VPI_POS
;
924 off_vci
= SUNATM_VCI_POS
;
925 off_proto
= PROTO_POS
;
926 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
927 off_payload
= SUNATM_PKT_BEGIN_POS
;
928 off_linktype
= off_payload
;
929 off_nl
= off_payload
+8; /* 802.2+SNAP */
930 off_nl_nosnap
= off_payload
+3; /* 802.2 */
936 off_nl_nosnap
= 0; /* no 802.2 LLC */
939 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
942 off_nl_nosnap
= 16; /* no 802.2 LLC */
947 * LocalTalk does have a 1-byte type field in the LLAP header,
948 * but really it just indicates whether there is a "short" or
949 * "long" DDP packet following.
953 off_nl_nosnap
= 0; /* no 802.2 LLC */
958 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
959 * link-level type field. We set "off_linktype" to the
960 * offset of the LLC header.
962 * To check for Ethernet types, we assume that SSAP = SNAP
963 * is being used and pick out the encapsulated Ethernet type.
964 * XXX - should we generate code to check for SNAP? RFC
965 * 2625 says SNAP should be used.
968 off_nl
= 24; /* IPFC+802.2+SNAP */
969 off_nl_nosnap
= 19; /* IPFC+802.2 */
974 * XXX - we should set this to handle SNAP-encapsulated
975 * frames (NLPID of 0x80).
979 off_nl_nosnap
= 0; /* no 802.2 LLC */
982 case DLT_APPLE_IP_OVER_IEEE1394
:
985 off_nl_nosnap
= 0; /* no 802.2 LLC */
990 * Currently, only raw "link[N:M]" filtering is supported.
999 * Currently, only raw "link[N:M]" filtering is supported.
1006 case DLT_SYMANTEC_FIREWALL
:
1008 off_nl
= 44; /* Ethernet II */
1009 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1014 /* XXX read from header? */
1015 off_nl
= PFLOG_HDRLEN
;
1016 off_nl_nosnap
= PFLOG_HDRLEN
;
1027 bpf_error("unknown data link type %d", linktype
);
1031 static struct block
*
1038 s
= new_stmt(BPF_LD
|BPF_IMM
);
1040 b
= new_block(JMP(BPF_JEQ
));
1046 static inline struct block
*
1049 return gen_uncond(1);
1052 static inline struct block
*
1055 return gen_uncond(0);
1059 * Byte-swap a 32-bit number.
1060 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1061 * big-endian platforms.)
1063 #define SWAPLONG(y) \
1064 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1066 static struct block
*
1067 gen_ether_linktype(proto
)
1070 struct block
*b0
, *b1
;
1076 * OSI protocols always use 802.2 encapsulation.
1077 * XXX - should we check both the DSAP and the
1078 * SSAP, like this, or should we check just the
1081 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1083 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1084 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1089 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1091 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1092 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1096 case LLCSAP_NETBEUI
:
1098 * NetBEUI always uses 802.2 encapsulation.
1099 * XXX - should we check both the DSAP and the
1100 * SSAP, like this, or should we check just the
1103 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1105 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1106 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1114 * Ethernet_II frames, which are Ethernet
1115 * frames with a frame type of ETHERTYPE_IPX;
1117 * Ethernet_802.3 frames, which are 802.3
1118 * frames (i.e., the type/length field is
1119 * a length field, <= ETHERMTU, rather than
1120 * a type field) with the first two bytes
1121 * after the Ethernet/802.3 header being
1124 * Ethernet_802.2 frames, which are 802.3
1125 * frames with an 802.2 LLC header and
1126 * with the IPX LSAP as the DSAP in the LLC
1129 * Ethernet_SNAP frames, which are 802.3
1130 * frames with an LLC header and a SNAP
1131 * header and with an OUI of 0x000000
1132 * (encapsulated Ethernet) and a protocol
1133 * ID of ETHERTYPE_IPX in the SNAP header.
1135 * XXX - should we generate the same code both
1136 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1140 * This generates code to check both for the
1141 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1143 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1144 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1148 * Now we add code to check for SNAP frames with
1149 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1151 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1155 * Now we generate code to check for 802.3
1156 * frames in general.
1158 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1162 * Now add the check for 802.3 frames before the
1163 * check for Ethernet_802.2 and Ethernet_802.3,
1164 * as those checks should only be done on 802.3
1165 * frames, not on Ethernet frames.
1170 * Now add the check for Ethernet_II frames, and
1171 * do that before checking for the other frame
1174 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1178 case ETHERTYPE_ATALK
:
1179 case ETHERTYPE_AARP
:
1181 * EtherTalk (AppleTalk protocols on Ethernet link
1182 * layer) may use 802.2 encapsulation.
1186 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1187 * we check for an Ethernet type field less than
1188 * 1500, which means it's an 802.3 length field.
1190 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1194 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1195 * SNAP packets with an organization code of
1196 * 0x080007 (Apple, for Appletalk) and a protocol
1197 * type of ETHERTYPE_ATALK (Appletalk).
1199 * 802.2-encapsulated ETHERTYPE_AARP packets are
1200 * SNAP packets with an organization code of
1201 * 0x000000 (encapsulated Ethernet) and a protocol
1202 * type of ETHERTYPE_AARP (Appletalk ARP).
1204 if (proto
== ETHERTYPE_ATALK
)
1205 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1206 else /* proto == ETHERTYPE_AARP */
1207 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1211 * Check for Ethernet encapsulation (Ethertalk
1212 * phase 1?); we just check for the Ethernet
1215 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1221 if (proto
<= ETHERMTU
) {
1223 * This is an LLC SAP value, so the frames
1224 * that match would be 802.2 frames.
1225 * Check that the frame is an 802.2 frame
1226 * (i.e., that the length/type field is
1227 * a length field, <= ETHERMTU) and
1228 * then check the DSAP.
1230 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1232 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1237 * This is an Ethernet type, so compare
1238 * the length/type field with it (if
1239 * the frame is an 802.2 frame, the length
1240 * field will be <= ETHERMTU, and, as
1241 * "proto" is > ETHERMTU, this test
1242 * will fail and the frame won't match,
1243 * which is what we want).
1245 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1250 static struct block
*
1254 struct block
*b0
, *b1
, *b2
;
1259 return gen_ether_linktype(proto
);
1267 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1271 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1277 case DLT_IEEE802_11
:
1278 case DLT_PRISM_HEADER
:
1279 case DLT_IEEE802_11_RADIO
:
1282 case DLT_ATM_RFC1483
:
1284 case DLT_IP_OVER_FC
:
1285 return gen_llc(proto
);
1291 * If "is_lane" is set, check for a LANE-encapsulated
1292 * version of this protocol, otherwise check for an
1293 * LLC-encapsulated version of this protocol.
1295 * We assume LANE means Ethernet, not Token Ring.
1299 * Check that the packet doesn't begin with an
1300 * LE Control marker. (We've already generated
1303 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1307 * Now generate an Ethernet test.
1309 b1
= gen_ether_linktype(proto
);
1314 * Check for LLC encapsulation and then check the
1317 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1318 b1
= gen_llc(proto
);
1327 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1328 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1329 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1335 * OSI protocols always use 802.2 encapsulation.
1336 * XXX - should we check both the DSAP and the
1337 * LSAP, like this, or should we check just the
1340 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1341 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1342 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1346 case LLCSAP_NETBEUI
:
1348 * NetBEUI always uses 802.2 encapsulation.
1349 * XXX - should we check both the DSAP and the
1350 * LSAP, like this, or should we check just the
1353 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1354 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1355 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1361 * Ethernet_II frames, which are Ethernet
1362 * frames with a frame type of ETHERTYPE_IPX;
1364 * Ethernet_802.3 frames, which have a frame
1365 * type of LINUX_SLL_P_802_3;
1367 * Ethernet_802.2 frames, which are 802.3
1368 * frames with an 802.2 LLC header (i.e, have
1369 * a frame type of LINUX_SLL_P_802_2) and
1370 * with the IPX LSAP as the DSAP in the LLC
1373 * Ethernet_SNAP frames, which are 802.3
1374 * frames with an LLC header and a SNAP
1375 * header and with an OUI of 0x000000
1376 * (encapsulated Ethernet) and a protocol
1377 * ID of ETHERTYPE_IPX in the SNAP header.
1379 * First, do the checks on LINUX_SLL_P_802_2
1380 * frames; generate the check for either
1381 * Ethernet_802.2 or Ethernet_SNAP frames, and
1382 * then put a check for LINUX_SLL_P_802_2 frames
1385 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1386 (bpf_int32
)LLCSAP_IPX
);
1387 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1390 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1394 * Now check for 802.3 frames and OR that with
1395 * the previous test.
1397 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1401 * Now add the check for Ethernet_II frames, and
1402 * do that before checking for the other frame
1405 b0
= gen_cmp(off_linktype
, BPF_H
,
1406 (bpf_int32
)ETHERTYPE_IPX
);
1410 case ETHERTYPE_ATALK
:
1411 case ETHERTYPE_AARP
:
1413 * EtherTalk (AppleTalk protocols on Ethernet link
1414 * layer) may use 802.2 encapsulation.
1418 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1419 * we check for the 802.2 protocol type in the
1420 * "Ethernet type" field.
1422 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1425 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1426 * SNAP packets with an organization code of
1427 * 0x080007 (Apple, for Appletalk) and a protocol
1428 * type of ETHERTYPE_ATALK (Appletalk).
1430 * 802.2-encapsulated ETHERTYPE_AARP packets are
1431 * SNAP packets with an organization code of
1432 * 0x000000 (encapsulated Ethernet) and a protocol
1433 * type of ETHERTYPE_AARP (Appletalk ARP).
1435 if (proto
== ETHERTYPE_ATALK
)
1436 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1438 else /* proto == ETHERTYPE_AARP */
1439 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1444 * Check for Ethernet encapsulation (Ethertalk
1445 * phase 1?); we just check for the Ethernet
1448 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1454 if (proto
<= ETHERMTU
) {
1456 * This is an LLC SAP value, so the frames
1457 * that match would be 802.2 frames.
1458 * Check for the 802.2 protocol type
1459 * in the "Ethernet type" field, and
1460 * then check the DSAP.
1462 b0
= gen_cmp(off_linktype
, BPF_H
,
1464 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1470 * This is an Ethernet type, so compare
1471 * the length/type field with it (if
1472 * the frame is an 802.2 frame, the length
1473 * field will be <= ETHERMTU, and, as
1474 * "proto" is > ETHERMTU, this test
1475 * will fail and the frame won't match,
1476 * which is what we want).
1478 return gen_cmp(off_linktype
, BPF_H
,
1486 case DLT_SLIP_BSDOS
:
1489 * These types don't provide any type field; packets
1492 * XXX - for IPv4, check for a version number of 4, and,
1493 * for IPv6, check for a version number of 6?
1499 case ETHERTYPE_IPV6
:
1501 return gen_true(); /* always true */
1504 return gen_false(); /* always false */
1510 case DLT_PPP_WITHDIRECTION
:
1511 case DLT_PPP_SERIAL
:
1514 * We use Ethernet protocol types inside libpcap;
1515 * map them to the corresponding PPP protocol types.
1524 case ETHERTYPE_IPV6
:
1533 case ETHERTYPE_ATALK
:
1547 * I'm assuming the "Bridging PDU"s that go
1548 * over PPP are Spanning Tree Protocol
1562 * We use Ethernet protocol types inside libpcap;
1563 * map them to the corresponding PPP protocol types.
1568 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1569 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1571 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1576 case ETHERTYPE_IPV6
:
1586 case ETHERTYPE_ATALK
:
1600 * I'm assuming the "Bridging PDU"s that go
1601 * over PPP are Spanning Tree Protocol
1617 * For DLT_NULL, the link-layer header is a 32-bit
1618 * word containing an AF_ value in *host* byte order,
1619 * and for DLT_ENC, the link-layer header begins
1620 * with a 32-bit work containing an AF_ value in
1623 * In addition, if we're reading a saved capture file,
1624 * the host byte order in the capture may not be the
1625 * same as the host byte order on this machine.
1627 * For DLT_LOOP, the link-layer header is a 32-bit
1628 * word containing an AF_ value in *network* byte order.
1630 * XXX - AF_ values may, unfortunately, be platform-
1631 * dependent; for example, FreeBSD's AF_INET6 is 24
1632 * whilst NetBSD's and OpenBSD's is 26.
1634 * This means that, when reading a capture file, just
1635 * checking for our AF_INET6 value won't work if the
1636 * capture file came from another OS.
1645 case ETHERTYPE_IPV6
:
1652 * Not a type on which we support filtering.
1653 * XXX - support those that have AF_ values
1654 * #defined on this platform, at least?
1659 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1661 * The AF_ value is in host byte order, but
1662 * the BPF interpreter will convert it to
1663 * network byte order.
1665 * If this is a save file, and it's from a
1666 * machine with the opposite byte order to
1667 * ours, we byte-swap the AF_ value.
1669 * Then we run it through "htonl()", and
1670 * generate code to compare against the result.
1672 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1673 bpf_pcap
->sf
.swapped
)
1674 proto
= SWAPLONG(proto
);
1675 proto
= htonl(proto
);
1677 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1681 * af field is host byte order in contrast to the rest of
1684 if (proto
== ETHERTYPE_IP
)
1685 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1686 (bpf_int32
)AF_INET
));
1688 else if (proto
== ETHERTYPE_IPV6
)
1689 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1690 (bpf_int32
)AF_INET6
));
1698 case DLT_ARCNET_LINUX
:
1700 * XXX should we check for first fragment if the protocol
1709 case ETHERTYPE_IPV6
:
1710 return (gen_cmp(off_linktype
, BPF_B
,
1711 (bpf_int32
)ARCTYPE_INET6
));
1715 b0
= gen_cmp(off_linktype
, BPF_B
,
1716 (bpf_int32
)ARCTYPE_IP
);
1717 b1
= gen_cmp(off_linktype
, BPF_B
,
1718 (bpf_int32
)ARCTYPE_IP_OLD
);
1723 b0
= gen_cmp(off_linktype
, BPF_B
,
1724 (bpf_int32
)ARCTYPE_ARP
);
1725 b1
= gen_cmp(off_linktype
, BPF_B
,
1726 (bpf_int32
)ARCTYPE_ARP_OLD
);
1730 case ETHERTYPE_REVARP
:
1731 return (gen_cmp(off_linktype
, BPF_B
,
1732 (bpf_int32
)ARCTYPE_REVARP
));
1734 case ETHERTYPE_ATALK
:
1735 return (gen_cmp(off_linktype
, BPF_B
,
1736 (bpf_int32
)ARCTYPE_ATALK
));
1743 case ETHERTYPE_ATALK
:
1753 * XXX - assumes a 2-byte Frame Relay header with
1754 * DLCI and flags. What if the address is longer?
1760 * Check for the special NLPID for IP.
1762 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1765 case ETHERTYPE_IPV6
:
1767 * Check for the special NLPID for IPv6.
1769 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1774 * Check for several OSI protocols.
1776 * Frame Relay packets typically have an OSI
1777 * NLPID at the beginning; we check for each
1780 * What we check for is the NLPID and a frame
1781 * control field of UI, i.e. 0x03 followed
1784 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1785 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1786 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1797 case DLT_LINUX_IRDA
:
1798 bpf_error("IrDA link-layer type filtering not implemented");
1801 bpf_error("DOCSIS link-layer type filtering not implemented");
1805 * All the types that have no encapsulation should either be
1806 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1807 * all packets are IP packets, or should be handled in some
1808 * special case, if none of them are (if some are and some
1809 * aren't, the lack of encapsulation is a problem, as we'd
1810 * have to find some other way of determining the packet type).
1812 * Therefore, if "off_linktype" is -1, there's an error.
1814 if (off_linktype
== (u_int
)-1)
1818 * Any type not handled above should always have an Ethernet
1819 * type at an offset of "off_linktype". (PPP is partially
1820 * handled above - the protocol type is mapped from the
1821 * Ethernet and LLC types we use internally to the corresponding
1822 * PPP type - but the PPP type is always specified by a value
1823 * at "off_linktype", so we don't have to do the code generation
1826 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1830 * Check for an LLC SNAP packet with a given organization code and
1831 * protocol type; we check the entire contents of the 802.2 LLC and
1832 * snap headers, checking for DSAP and SSAP of SNAP and a control
1833 * field of 0x03 in the LLC header, and for the specified organization
1834 * code and protocol type in the SNAP header.
1836 static struct block
*
1837 gen_snap(orgcode
, ptype
, offset
)
1838 bpf_u_int32 orgcode
;
1842 u_char snapblock
[8];
1844 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1845 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1846 snapblock
[2] = 0x03; /* control = UI */
1847 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1848 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1849 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1850 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1851 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1852 return gen_bcmp(offset
, 8, snapblock
);
1856 * Check for a given protocol value assuming an 802.2 LLC header.
1858 static struct block
*
1863 * XXX - handle token-ring variable-length header.
1868 return gen_cmp(off_linktype
, BPF_H
, (long)
1869 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1872 return gen_cmp(off_linktype
, BPF_H
, (long)
1873 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1875 case LLCSAP_NETBEUI
:
1876 return gen_cmp(off_linktype
, BPF_H
, (long)
1877 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1881 * XXX - are there ever SNAP frames for IPX on
1882 * non-Ethernet 802.x networks?
1884 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1886 case ETHERTYPE_ATALK
:
1888 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1889 * SNAP packets with an organization code of
1890 * 0x080007 (Apple, for Appletalk) and a protocol
1891 * type of ETHERTYPE_ATALK (Appletalk).
1893 * XXX - check for an organization code of
1894 * encapsulated Ethernet as well?
1896 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1900 * XXX - we don't have to check for IPX 802.3
1901 * here, but should we check for the IPX Ethertype?
1903 if (proto
<= ETHERMTU
) {
1905 * This is an LLC SAP value, so check
1908 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1911 * This is an Ethernet type; we assume that it's
1912 * unlikely that it'll appear in the right place
1913 * at random, and therefore check only the
1914 * location that would hold the Ethernet type
1915 * in a SNAP frame with an organization code of
1916 * 0x000000 (encapsulated Ethernet).
1918 * XXX - if we were to check for the SNAP DSAP and
1919 * LSAP, as per XXX, and were also to check for an
1920 * organization code of 0x000000 (encapsulated
1921 * Ethernet), we'd do
1923 * return gen_snap(0x000000, proto,
1926 * here; for now, we don't, as per the above.
1927 * I don't know whether it's worth the extra CPU
1928 * time to do the right check or not.
1930 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1935 static struct block
*
1936 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1940 u_int src_off
, dst_off
;
1942 struct block
*b0
, *b1
;
1956 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1957 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1963 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1964 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1971 b0
= gen_linktype(proto
);
1972 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1978 static struct block
*
1979 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1980 struct in6_addr
*addr
;
1981 struct in6_addr
*mask
;
1983 u_int src_off
, dst_off
;
1985 struct block
*b0
, *b1
;
2000 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2001 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2007 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2008 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2015 /* this order is important */
2016 a
= (u_int32_t
*)addr
;
2017 m
= (u_int32_t
*)mask
;
2018 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2019 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2021 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2023 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2025 b0
= gen_linktype(proto
);
2031 static struct block
*
2032 gen_ehostop(eaddr
, dir
)
2033 register const u_char
*eaddr
;
2036 register struct block
*b0
, *b1
;
2040 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2043 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2046 b0
= gen_ehostop(eaddr
, Q_SRC
);
2047 b1
= gen_ehostop(eaddr
, Q_DST
);
2053 b0
= gen_ehostop(eaddr
, Q_SRC
);
2054 b1
= gen_ehostop(eaddr
, Q_DST
);
2063 * Like gen_ehostop, but for DLT_FDDI
2065 static struct block
*
2066 gen_fhostop(eaddr
, dir
)
2067 register const u_char
*eaddr
;
2070 struct block
*b0
, *b1
;
2075 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2077 return gen_bcmp(6 + 1, 6, eaddr
);
2082 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2084 return gen_bcmp(0 + 1, 6, eaddr
);
2088 b0
= gen_fhostop(eaddr
, Q_SRC
);
2089 b1
= gen_fhostop(eaddr
, Q_DST
);
2095 b0
= gen_fhostop(eaddr
, Q_SRC
);
2096 b1
= gen_fhostop(eaddr
, Q_DST
);
2105 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2107 static struct block
*
2108 gen_thostop(eaddr
, dir
)
2109 register const u_char
*eaddr
;
2112 register struct block
*b0
, *b1
;
2116 return gen_bcmp(8, 6, eaddr
);
2119 return gen_bcmp(2, 6, eaddr
);
2122 b0
= gen_thostop(eaddr
, Q_SRC
);
2123 b1
= gen_thostop(eaddr
, Q_DST
);
2129 b0
= gen_thostop(eaddr
, Q_SRC
);
2130 b1
= gen_thostop(eaddr
, Q_DST
);
2139 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2141 static struct block
*
2142 gen_wlanhostop(eaddr
, dir
)
2143 register const u_char
*eaddr
;
2146 register struct block
*b0
, *b1
, *b2
;
2147 register struct slist
*s
;
2154 * For control frames, there is no SA.
2156 * For management frames, SA is at an
2157 * offset of 10 from the beginning of
2160 * For data frames, SA is at an offset
2161 * of 10 from the beginning of the packet
2162 * if From DS is clear, at an offset of
2163 * 16 from the beginning of the packet
2164 * if From DS is set and To DS is clear,
2165 * and an offset of 24 from the beginning
2166 * of the packet if From DS is set and To DS
2171 * Generate the tests to be done for data frames
2174 * First, check for To DS set, i.e. check "link[1] & 0x01".
2176 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2178 b1
= new_block(JMP(BPF_JSET
));
2179 b1
->s
.k
= 0x01; /* To DS */
2183 * If To DS is set, the SA is at 24.
2185 b0
= gen_bcmp(24, 6, eaddr
);
2189 * Now, check for To DS not set, i.e. check
2190 * "!(link[1] & 0x01)".
2192 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2194 b2
= new_block(JMP(BPF_JSET
));
2195 b2
->s
.k
= 0x01; /* To DS */
2200 * If To DS is not set, the SA is at 16.
2202 b1
= gen_bcmp(16, 6, eaddr
);
2206 * Now OR together the last two checks. That gives
2207 * the complete set of checks for data frames with
2213 * Now check for From DS being set, and AND that with
2214 * the ORed-together checks.
2216 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2218 b1
= new_block(JMP(BPF_JSET
));
2219 b1
->s
.k
= 0x02; /* From DS */
2224 * Now check for data frames with From DS not set.
2226 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2228 b2
= new_block(JMP(BPF_JSET
));
2229 b2
->s
.k
= 0x02; /* From DS */
2234 * If From DS isn't set, the SA is at 10.
2236 b1
= gen_bcmp(10, 6, eaddr
);
2240 * Now OR together the checks for data frames with
2241 * From DS not set and for data frames with From DS
2242 * set; that gives the checks done for data frames.
2247 * Now check for a data frame.
2248 * I.e, check "link[0] & 0x08".
2250 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2252 b1
= new_block(JMP(BPF_JSET
));
2257 * AND that with the checks done for data frames.
2262 * If the high-order bit of the type value is 0, this
2263 * is a management frame.
2264 * I.e, check "!(link[0] & 0x08)".
2266 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2268 b2
= new_block(JMP(BPF_JSET
));
2274 * For management frames, the SA is at 10.
2276 b1
= gen_bcmp(10, 6, eaddr
);
2280 * OR that with the checks done for data frames.
2281 * That gives the checks done for management and
2287 * If the low-order bit of the type value is 1,
2288 * this is either a control frame or a frame
2289 * with a reserved type, and thus not a
2292 * I.e., check "!(link[0] & 0x04)".
2294 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2296 b1
= new_block(JMP(BPF_JSET
));
2302 * AND that with the checks for data and management
2312 * For control frames, there is no DA.
2314 * For management frames, DA is at an
2315 * offset of 4 from the beginning of
2318 * For data frames, DA is at an offset
2319 * of 4 from the beginning of the packet
2320 * if To DS is clear and at an offset of
2321 * 16 from the beginning of the packet
2326 * Generate the tests to be done for data frames.
2328 * First, check for To DS set, i.e. "link[1] & 0x01".
2330 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2332 b1
= new_block(JMP(BPF_JSET
));
2333 b1
->s
.k
= 0x01; /* To DS */
2337 * If To DS is set, the DA is at 16.
2339 b0
= gen_bcmp(16, 6, eaddr
);
2343 * Now, check for To DS not set, i.e. check
2344 * "!(link[1] & 0x01)".
2346 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2348 b2
= new_block(JMP(BPF_JSET
));
2349 b2
->s
.k
= 0x01; /* To DS */
2354 * If To DS is not set, the DA is at 4.
2356 b1
= gen_bcmp(4, 6, eaddr
);
2360 * Now OR together the last two checks. That gives
2361 * the complete set of checks for data frames.
2366 * Now check for a data frame.
2367 * I.e, check "link[0] & 0x08".
2369 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2371 b1
= new_block(JMP(BPF_JSET
));
2376 * AND that with the checks done for data frames.
2381 * If the high-order bit of the type value is 0, this
2382 * is a management frame.
2383 * I.e, check "!(link[0] & 0x08)".
2385 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2387 b2
= new_block(JMP(BPF_JSET
));
2393 * For management frames, the DA is at 4.
2395 b1
= gen_bcmp(4, 6, eaddr
);
2399 * OR that with the checks done for data frames.
2400 * That gives the checks done for management and
2406 * If the low-order bit of the type value is 1,
2407 * this is either a control frame or a frame
2408 * with a reserved type, and thus not a
2411 * I.e., check "!(link[0] & 0x04)".
2413 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2415 b1
= new_block(JMP(BPF_JSET
));
2421 * AND that with the checks for data and management
2428 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2429 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2435 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2436 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2445 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2446 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2447 * as the RFC states.)
2449 static struct block
*
2450 gen_ipfchostop(eaddr
, dir
)
2451 register const u_char
*eaddr
;
2454 register struct block
*b0
, *b1
;
2458 return gen_bcmp(10, 6, eaddr
);
2461 return gen_bcmp(2, 6, eaddr
);
2464 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2465 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2471 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2472 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2481 * This is quite tricky because there may be pad bytes in front of the
2482 * DECNET header, and then there are two possible data packet formats that
2483 * carry both src and dst addresses, plus 5 packet types in a format that
2484 * carries only the src node, plus 2 types that use a different format and
2485 * also carry just the src node.
2489 * Instead of doing those all right, we just look for data packets with
2490 * 0 or 1 bytes of padding. If you want to look at other packets, that
2491 * will require a lot more hacking.
2493 * To add support for filtering on DECNET "areas" (network numbers)
2494 * one would want to add a "mask" argument to this routine. That would
2495 * make the filter even more inefficient, although one could be clever
2496 * and not generate masking instructions if the mask is 0xFFFF.
2498 static struct block
*
2499 gen_dnhostop(addr
, dir
, base_off
)
2504 struct block
*b0
, *b1
, *b2
, *tmp
;
2505 u_int offset_lh
; /* offset if long header is received */
2506 u_int offset_sh
; /* offset if short header is received */
2511 offset_sh
= 1; /* follows flags */
2512 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2516 offset_sh
= 3; /* follows flags, dstnode */
2517 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2521 /* Inefficient because we do our Calvinball dance twice */
2522 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2523 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2529 /* Inefficient because we do our Calvinball dance twice */
2530 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2531 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2536 bpf_error("ISO host filtering not implemented");
2541 b0
= gen_linktype(ETHERTYPE_DN
);
2542 /* Check for pad = 1, long header case */
2543 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2544 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2545 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2546 BPF_H
, (bpf_int32
)ntohs(addr
));
2548 /* Check for pad = 0, long header case */
2549 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2550 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2553 /* Check for pad = 1, short header case */
2554 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2555 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2556 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2557 BPF_H
, (bpf_int32
)ntohs(addr
));
2560 /* Check for pad = 0, short header case */
2561 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2562 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2566 /* Combine with test for linktype */
2571 static struct block
*
2572 gen_host(addr
, mask
, proto
, dir
)
2578 struct block
*b0
, *b1
;
2583 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2584 if (off_linktype
!= (u_int
)-1) {
2585 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2587 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2593 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2594 off_nl
+ 12, off_nl
+ 16);
2597 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2598 off_nl
+ 14, off_nl
+ 24);
2601 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2602 off_nl
+ 14, off_nl
+ 24);
2605 bpf_error("'tcp' modifier applied to host");
2608 bpf_error("'sctp' modifier applied to host");
2611 bpf_error("'udp' modifier applied to host");
2614 bpf_error("'icmp' modifier applied to host");
2617 bpf_error("'igmp' modifier applied to host");
2620 bpf_error("'igrp' modifier applied to host");
2623 bpf_error("'pim' modifier applied to host");
2626 bpf_error("'vrrp' modifier applied to host");
2629 bpf_error("ATALK host filtering not implemented");
2632 bpf_error("AARP host filtering not implemented");
2635 return gen_dnhostop(addr
, dir
, off_nl
);
2638 bpf_error("SCA host filtering not implemented");
2641 bpf_error("LAT host filtering not implemented");
2644 bpf_error("MOPDL host filtering not implemented");
2647 bpf_error("MOPRC host filtering not implemented");
2651 bpf_error("'ip6' modifier applied to ip host");
2654 bpf_error("'icmp6' modifier applied to host");
2658 bpf_error("'ah' modifier applied to host");
2661 bpf_error("'esp' modifier applied to host");
2664 bpf_error("ISO host filtering not implemented");
2667 bpf_error("'esis' modifier applied to host");
2670 bpf_error("'isis' modifier applied to host");
2673 bpf_error("'clnp' modifier applied to host");
2676 bpf_error("'stp' modifier applied to host");
2679 bpf_error("IPX host filtering not implemented");
2682 bpf_error("'netbeui' modifier applied to host");
2691 static struct block
*
2692 gen_host6(addr
, mask
, proto
, dir
)
2693 struct in6_addr
*addr
;
2694 struct in6_addr
*mask
;
2701 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2704 bpf_error("'ip' modifier applied to ip6 host");
2707 bpf_error("'rarp' modifier applied to ip6 host");
2710 bpf_error("'arp' modifier applied to ip6 host");
2713 bpf_error("'sctp' modifier applied to host");
2716 bpf_error("'tcp' modifier applied to host");
2719 bpf_error("'udp' modifier applied to host");
2722 bpf_error("'icmp' modifier applied to host");
2725 bpf_error("'igmp' modifier applied to host");
2728 bpf_error("'igrp' modifier applied to host");
2731 bpf_error("'pim' modifier applied to host");
2734 bpf_error("'vrrp' modifier applied to host");
2737 bpf_error("ATALK host filtering not implemented");
2740 bpf_error("AARP host filtering not implemented");
2743 bpf_error("'decnet' modifier applied to ip6 host");
2746 bpf_error("SCA host filtering not implemented");
2749 bpf_error("LAT host filtering not implemented");
2752 bpf_error("MOPDL host filtering not implemented");
2755 bpf_error("MOPRC host filtering not implemented");
2758 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2759 off_nl
+ 8, off_nl
+ 24);
2762 bpf_error("'icmp6' modifier applied to host");
2765 bpf_error("'ah' modifier applied to host");
2768 bpf_error("'esp' modifier applied to host");
2771 bpf_error("ISO host filtering not implemented");
2774 bpf_error("'esis' modifier applied to host");
2777 bpf_error("'isis' modifier applied to host");
2780 bpf_error("'clnp' modifier applied to host");
2783 bpf_error("'stp' modifier applied to host");
2786 bpf_error("IPX host filtering not implemented");
2789 bpf_error("'netbeui' modifier applied to host");
2799 static struct block
*
2800 gen_gateway(eaddr
, alist
, proto
, dir
)
2801 const u_char
*eaddr
;
2802 bpf_u_int32
**alist
;
2806 struct block
*b0
, *b1
, *tmp
;
2809 bpf_error("direction applied to 'gateway'");
2816 if (linktype
== DLT_EN10MB
)
2817 b0
= gen_ehostop(eaddr
, Q_OR
);
2818 else if (linktype
== DLT_FDDI
)
2819 b0
= gen_fhostop(eaddr
, Q_OR
);
2820 else if (linktype
== DLT_IEEE802
)
2821 b0
= gen_thostop(eaddr
, Q_OR
);
2822 else if (linktype
== DLT_IEEE802_11
)
2823 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2824 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2826 * Check that the packet doesn't begin with an
2827 * LE Control marker. (We've already generated
2830 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2834 * Now check the MAC address.
2836 b0
= gen_ehostop(eaddr
, Q_OR
);
2838 } else if (linktype
== DLT_IP_OVER_FC
)
2839 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2842 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2844 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2846 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2854 bpf_error("illegal modifier of 'gateway'");
2860 gen_proto_abbrev(proto
)
2869 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2871 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2877 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2879 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2885 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2887 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2893 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2896 #ifndef IPPROTO_IGMP
2897 #define IPPROTO_IGMP 2
2901 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2904 #ifndef IPPROTO_IGRP
2905 #define IPPROTO_IGRP 9
2908 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2912 #define IPPROTO_PIM 103
2916 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2918 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2923 #ifndef IPPROTO_VRRP
2924 #define IPPROTO_VRRP 112
2928 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2932 b1
= gen_linktype(ETHERTYPE_IP
);
2936 b1
= gen_linktype(ETHERTYPE_ARP
);
2940 b1
= gen_linktype(ETHERTYPE_REVARP
);
2944 bpf_error("link layer applied in wrong context");
2947 b1
= gen_linktype(ETHERTYPE_ATALK
);
2951 b1
= gen_linktype(ETHERTYPE_AARP
);
2955 b1
= gen_linktype(ETHERTYPE_DN
);
2959 b1
= gen_linktype(ETHERTYPE_SCA
);
2963 b1
= gen_linktype(ETHERTYPE_LAT
);
2967 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2971 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2976 b1
= gen_linktype(ETHERTYPE_IPV6
);
2979 #ifndef IPPROTO_ICMPV6
2980 #define IPPROTO_ICMPV6 58
2983 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2988 #define IPPROTO_AH 51
2991 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2993 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2999 #define IPPROTO_ESP 50
3002 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3004 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3010 b1
= gen_linktype(LLCSAP_ISONS
);
3014 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3018 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3021 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3022 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3023 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3025 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3027 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3029 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3033 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3034 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3035 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3037 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3039 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3041 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3045 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3046 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3047 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3049 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3054 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3055 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3060 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3061 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3063 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3065 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3070 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3071 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3076 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3077 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3082 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3086 b1
= gen_linktype(LLCSAP_8021D
);
3090 b1
= gen_linktype(LLCSAP_IPX
);
3094 b1
= gen_linktype(LLCSAP_NETBEUI
);
3103 static struct block
*
3110 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3111 s
->s
.k
= off_nl
+ 6;
3112 b
= new_block(JMP(BPF_JSET
));
3120 static struct block
*
3121 gen_portatom(off
, v
)
3128 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3131 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3132 s
->next
->s
.k
= off_nl
+ off
;
3134 b
= new_block(JMP(BPF_JEQ
));
3142 static struct block
*
3143 gen_portatom6(off
, v
)
3147 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3152 gen_portop(port
, proto
, dir
)
3153 int port
, proto
, dir
;
3155 struct block
*b0
, *b1
, *tmp
;
3157 /* ip proto 'proto' */
3158 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3164 b1
= gen_portatom(0, (bpf_int32
)port
);
3168 b1
= gen_portatom(2, (bpf_int32
)port
);
3173 tmp
= gen_portatom(0, (bpf_int32
)port
);
3174 b1
= gen_portatom(2, (bpf_int32
)port
);
3179 tmp
= gen_portatom(0, (bpf_int32
)port
);
3180 b1
= gen_portatom(2, (bpf_int32
)port
);
3192 static struct block
*
3193 gen_port(port
, ip_proto
, dir
)
3198 struct block
*b0
, *b1
, *tmp
;
3203 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3204 * not LLC encapsulation with LLCSAP_IP.
3206 * For IEEE 802 networks - which includes 802.5 token ring
3207 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3208 * says that SNAP encapsulation is used, not LLC encapsulation
3211 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3212 * RFC 2225 say that SNAP encapsulation is used, not LLC
3213 * encapsulation with LLCSAP_IP.
3215 * So we always check for ETHERTYPE_IP.
3217 b0
= gen_linktype(ETHERTYPE_IP
);
3223 b1
= gen_portop(port
, ip_proto
, dir
);
3227 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3228 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3230 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3243 gen_portop6(port
, proto
, dir
)
3244 int port
, proto
, dir
;
3246 struct block
*b0
, *b1
, *tmp
;
3248 /* ip proto 'proto' */
3249 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3253 b1
= gen_portatom6(0, (bpf_int32
)port
);
3257 b1
= gen_portatom6(2, (bpf_int32
)port
);
3262 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3263 b1
= gen_portatom6(2, (bpf_int32
)port
);
3268 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3269 b1
= gen_portatom6(2, (bpf_int32
)port
);
3281 static struct block
*
3282 gen_port6(port
, ip_proto
, dir
)
3287 struct block
*b0
, *b1
, *tmp
;
3289 /* ether proto ip */
3290 b0
= gen_linktype(ETHERTYPE_IPV6
);
3296 b1
= gen_portop6(port
, ip_proto
, dir
);
3300 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3301 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3303 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3316 lookup_proto(name
, proto
)
3317 register const char *name
;
3327 v
= pcap_nametoproto(name
);
3328 if (v
== PROTO_UNDEF
)
3329 bpf_error("unknown ip proto '%s'", name
);
3333 /* XXX should look up h/w protocol type based on linktype */
3334 v
= pcap_nametoeproto(name
);
3335 if (v
== PROTO_UNDEF
)
3336 bpf_error("unknown ether proto '%s'", name
);
3340 if (strcmp(name
, "esis") == 0)
3342 else if (strcmp(name
, "isis") == 0)
3344 else if (strcmp(name
, "clnp") == 0)
3347 bpf_error("unknown osi proto '%s'", name
);
3367 static struct block
*
3368 gen_protochain(v
, proto
, dir
)
3373 #ifdef NO_PROTOCHAIN
3374 return gen_proto(v
, proto
, dir
);
3376 struct block
*b0
, *b
;
3377 struct slist
*s
[100];
3378 int fix2
, fix3
, fix4
, fix5
;
3379 int ahcheck
, again
, end
;
3381 int reg2
= alloc_reg();
3383 memset(s
, 0, sizeof(s
));
3384 fix2
= fix3
= fix4
= fix5
= 0;
3391 b0
= gen_protochain(v
, Q_IP
, dir
);
3392 b
= gen_protochain(v
, Q_IPV6
, dir
);
3396 bpf_error("bad protocol applied for 'protochain'");
3400 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3403 * s[0] is a dummy entry to protect other BPF insn from damaged
3404 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3405 * hard to find interdependency made by jump table fixup.
3408 s
[i
] = new_stmt(0); /*dummy*/
3413 b0
= gen_linktype(ETHERTYPE_IP
);
3416 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3417 s
[i
]->s
.k
= off_nl
+ 9;
3419 /* X = ip->ip_hl << 2 */
3420 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3426 b0
= gen_linktype(ETHERTYPE_IPV6
);
3428 /* A = ip6->ip_nxt */
3429 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3430 s
[i
]->s
.k
= off_nl
+ 6;
3432 /* X = sizeof(struct ip6_hdr) */
3433 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3439 bpf_error("unsupported proto to gen_protochain");
3443 /* again: if (A == v) goto end; else fall through; */
3445 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3447 s
[i
]->s
.jt
= NULL
; /*later*/
3448 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3452 #ifndef IPPROTO_NONE
3453 #define IPPROTO_NONE 59
3455 /* if (A == IPPROTO_NONE) goto end */
3456 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3457 s
[i
]->s
.jt
= NULL
; /*later*/
3458 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3459 s
[i
]->s
.k
= IPPROTO_NONE
;
3460 s
[fix5
]->s
.jf
= s
[i
];
3465 if (proto
== Q_IPV6
) {
3466 int v6start
, v6end
, v6advance
, j
;
3469 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3470 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3471 s
[i
]->s
.jt
= NULL
; /*later*/
3472 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3473 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3474 s
[fix2
]->s
.jf
= s
[i
];
3476 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3477 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3478 s
[i
]->s
.jt
= NULL
; /*later*/
3479 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3480 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3482 /* if (A == IPPROTO_ROUTING) goto v6advance */
3483 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3484 s
[i
]->s
.jt
= NULL
; /*later*/
3485 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3486 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3488 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3489 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3490 s
[i
]->s
.jt
= NULL
; /*later*/
3491 s
[i
]->s
.jf
= NULL
; /*later*/
3492 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3503 * X = X + (P[X + 1] + 1) * 8;
3506 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3508 /* A = P[X + packet head] */
3509 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3513 s
[i
] = new_stmt(BPF_ST
);
3517 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3520 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3524 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3526 /* A = P[X + packet head]; */
3527 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3531 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3535 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3539 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3542 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3546 /* goto again; (must use BPF_JA for backward jump) */
3547 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3548 s
[i
]->s
.k
= again
- i
- 1;
3549 s
[i
- 1]->s
.jf
= s
[i
];
3553 for (j
= v6start
; j
<= v6end
; j
++)
3554 s
[j
]->s
.jt
= s
[v6advance
];
3559 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3561 s
[fix2
]->s
.jf
= s
[i
];
3567 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3568 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3569 s
[i
]->s
.jt
= NULL
; /*later*/
3570 s
[i
]->s
.jf
= NULL
; /*later*/
3571 s
[i
]->s
.k
= IPPROTO_AH
;
3573 s
[fix3
]->s
.jf
= s
[ahcheck
];
3580 * X = X + (P[X + 1] + 2) * 4;
3583 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3585 /* A = P[X + packet head]; */
3586 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3590 s
[i
] = new_stmt(BPF_ST
);
3594 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3597 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3601 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3603 /* A = P[X + packet head] */
3604 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3608 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3612 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3616 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3619 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3623 /* goto again; (must use BPF_JA for backward jump) */
3624 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3625 s
[i
]->s
.k
= again
- i
- 1;
3630 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3632 s
[fix2
]->s
.jt
= s
[end
];
3633 s
[fix4
]->s
.jf
= s
[end
];
3634 s
[fix5
]->s
.jt
= s
[end
];
3641 for (i
= 0; i
< max
- 1; i
++)
3642 s
[i
]->next
= s
[i
+ 1];
3643 s
[max
- 1]->next
= NULL
;
3648 b
= new_block(JMP(BPF_JEQ
));
3649 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3659 static struct block
*
3660 gen_proto(v
, proto
, dir
)
3665 struct block
*b0
, *b1
;
3667 if (dir
!= Q_DEFAULT
)
3668 bpf_error("direction applied to 'proto'");
3673 b0
= gen_proto(v
, Q_IP
, dir
);
3674 b1
= gen_proto(v
, Q_IPV6
, dir
);
3682 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3683 * not LLC encapsulation with LLCSAP_IP.
3685 * For IEEE 802 networks - which includes 802.5 token ring
3686 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3687 * says that SNAP encapsulation is used, not LLC encapsulation
3690 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3691 * RFC 2225 say that SNAP encapsulation is used, not LLC
3692 * encapsulation with LLCSAP_IP.
3694 * So we always check for ETHERTYPE_IP.
3696 b0
= gen_linktype(ETHERTYPE_IP
);
3698 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3700 b1
= gen_protochain(v
, Q_IP
);
3710 * Frame Relay packets typically have an OSI
3711 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3712 * generates code to check for all the OSI
3713 * NLPIDs, so calling it and then adding a check
3714 * for the particular NLPID for which we're
3715 * looking is bogus, as we can just check for
3718 * What we check for is the NLPID and a frame
3719 * control field value of UI, i.e. 0x03 followed
3722 * XXX - assumes a 2-byte Frame Relay header with
3723 * DLCI and flags. What if the address is longer?
3725 * XXX - what about SNAP-encapsulated frames?
3727 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3733 * Cisco uses an Ethertype lookalike - for OSI,
3736 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3737 /* OSI in C-HDLC is stuffed with a fudge byte */
3738 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3743 b0
= gen_linktype(LLCSAP_ISONS
);
3744 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3750 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3752 * 4 is the offset of the PDU type relative to the IS-IS
3755 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3760 bpf_error("arp does not encapsulate another protocol");
3764 bpf_error("rarp does not encapsulate another protocol");
3768 bpf_error("atalk encapsulation is not specifiable");
3772 bpf_error("decnet encapsulation is not specifiable");
3776 bpf_error("sca does not encapsulate another protocol");
3780 bpf_error("lat does not encapsulate another protocol");
3784 bpf_error("moprc does not encapsulate another protocol");
3788 bpf_error("mopdl does not encapsulate another protocol");
3792 return gen_linktype(v
);
3795 bpf_error("'udp proto' is bogus");
3799 bpf_error("'tcp proto' is bogus");
3803 bpf_error("'sctp proto' is bogus");
3807 bpf_error("'icmp proto' is bogus");
3811 bpf_error("'igmp proto' is bogus");
3815 bpf_error("'igrp proto' is bogus");
3819 bpf_error("'pim proto' is bogus");
3823 bpf_error("'vrrp proto' is bogus");
3828 b0
= gen_linktype(ETHERTYPE_IPV6
);
3830 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3832 b1
= gen_protochain(v
, Q_IPV6
);
3838 bpf_error("'icmp6 proto' is bogus");
3842 bpf_error("'ah proto' is bogus");
3845 bpf_error("'ah proto' is bogus");
3848 bpf_error("'stp proto' is bogus");
3851 bpf_error("'ipx proto' is bogus");
3854 bpf_error("'netbeui proto' is bogus");
3865 register const char *name
;
3868 int proto
= q
.proto
;
3872 bpf_u_int32 mask
, addr
;
3874 bpf_u_int32
**alist
;
3877 struct sockaddr_in
*sin
;
3878 struct sockaddr_in6
*sin6
;
3879 struct addrinfo
*res
, *res0
;
3880 struct in6_addr mask128
;
3882 struct block
*b
, *tmp
;
3883 int port
, real_proto
;
3888 addr
= pcap_nametonetaddr(name
);
3890 bpf_error("unknown network '%s'", name
);
3891 /* Left justify network addr and calculate its network mask */
3893 while (addr
&& (addr
& 0xff000000) == 0) {
3897 return gen_host(addr
, mask
, proto
, dir
);
3901 if (proto
== Q_LINK
) {
3905 eaddr
= pcap_ether_hostton(name
);
3908 "unknown ether host '%s'", name
);
3909 b
= gen_ehostop(eaddr
, dir
);
3914 eaddr
= pcap_ether_hostton(name
);
3917 "unknown FDDI host '%s'", name
);
3918 b
= gen_fhostop(eaddr
, dir
);
3923 eaddr
= pcap_ether_hostton(name
);
3926 "unknown token ring host '%s'", name
);
3927 b
= gen_thostop(eaddr
, dir
);
3931 case DLT_IEEE802_11
:
3932 eaddr
= pcap_ether_hostton(name
);
3935 "unknown 802.11 host '%s'", name
);
3936 b
= gen_wlanhostop(eaddr
, dir
);
3940 case DLT_IP_OVER_FC
:
3941 eaddr
= pcap_ether_hostton(name
);
3944 "unknown Fibre Channel host '%s'", name
);
3945 b
= gen_ipfchostop(eaddr
, dir
);
3954 * Check that the packet doesn't begin
3955 * with an LE Control marker. (We've
3956 * already generated a test for LANE.)
3958 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3962 eaddr
= pcap_ether_hostton(name
);
3965 "unknown ether host '%s'", name
);
3966 b
= gen_ehostop(eaddr
, dir
);
3972 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3973 } else if (proto
== Q_DECNET
) {
3974 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3976 * I don't think DECNET hosts can be multihomed, so
3977 * there is no need to build up a list of addresses
3979 return (gen_host(dn_addr
, 0, proto
, dir
));
3982 alist
= pcap_nametoaddr(name
);
3983 if (alist
== NULL
|| *alist
== NULL
)
3984 bpf_error("unknown host '%s'", name
);
3986 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
3988 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3990 tmp
= gen_host(**alist
++, 0xffffffff,
3997 memset(&mask128
, 0xff, sizeof(mask128
));
3998 res0
= res
= pcap_nametoaddrinfo(name
);
4000 bpf_error("unknown host '%s'", name
);
4002 tproto
= tproto6
= proto
;
4003 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4007 for (res
= res0
; res
; res
= res
->ai_next
) {
4008 switch (res
->ai_family
) {
4010 if (tproto
== Q_IPV6
)
4013 sin
= (struct sockaddr_in
*)
4015 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4016 0xffffffff, tproto
, dir
);
4019 if (tproto6
== Q_IP
)
4022 sin6
= (struct sockaddr_in6
*)
4024 tmp
= gen_host6(&sin6
->sin6_addr
,
4025 &mask128
, tproto6
, dir
);
4036 bpf_error("unknown host '%s'%s", name
,
4037 (proto
== Q_DEFAULT
)
4039 : " for specified address family");
4046 if (proto
!= Q_DEFAULT
&&
4047 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4048 bpf_error("illegal qualifier of 'port'");
4049 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4050 bpf_error("unknown port '%s'", name
);
4051 if (proto
== Q_UDP
) {
4052 if (real_proto
== IPPROTO_TCP
)
4053 bpf_error("port '%s' is tcp", name
);
4054 else if (real_proto
== IPPROTO_SCTP
)
4055 bpf_error("port '%s' is sctp", name
);
4057 /* override PROTO_UNDEF */
4058 real_proto
= IPPROTO_UDP
;
4060 if (proto
== Q_TCP
) {
4061 if (real_proto
== IPPROTO_UDP
)
4062 bpf_error("port '%s' is udp", name
);
4064 else if (real_proto
== IPPROTO_SCTP
)
4065 bpf_error("port '%s' is sctp", name
);
4067 /* override PROTO_UNDEF */
4068 real_proto
= IPPROTO_TCP
;
4070 if (proto
== Q_SCTP
) {
4071 if (real_proto
== IPPROTO_UDP
)
4072 bpf_error("port '%s' is udp", name
);
4074 else if (real_proto
== IPPROTO_TCP
)
4075 bpf_error("port '%s' is tcp", name
);
4077 /* override PROTO_UNDEF */
4078 real_proto
= IPPROTO_SCTP
;
4081 return gen_port(port
, real_proto
, dir
);
4085 b
= gen_port(port
, real_proto
, dir
);
4086 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4093 eaddr
= pcap_ether_hostton(name
);
4095 bpf_error("unknown ether host: %s", name
);
4097 alist
= pcap_nametoaddr(name
);
4098 if (alist
== NULL
|| *alist
== NULL
)
4099 bpf_error("unknown host '%s'", name
);
4100 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4104 bpf_error("'gateway' not supported in this configuration");
4108 real_proto
= lookup_proto(name
, proto
);
4109 if (real_proto
>= 0)
4110 return gen_proto(real_proto
, proto
, dir
);
4112 bpf_error("unknown protocol: %s", name
);
4115 real_proto
= lookup_proto(name
, proto
);
4116 if (real_proto
>= 0)
4117 return gen_protochain(real_proto
, proto
, dir
);
4119 bpf_error("unknown protocol: %s", name
);
4131 gen_mcode(s1
, s2
, masklen
, q
)
4132 register const char *s1
, *s2
;
4133 register int masklen
;
4136 register int nlen
, mlen
;
4139 nlen
= __pcap_atoin(s1
, &n
);
4140 /* Promote short ipaddr */
4144 mlen
= __pcap_atoin(s2
, &m
);
4145 /* Promote short ipaddr */
4148 bpf_error("non-network bits set in \"%s mask %s\"",
4151 /* Convert mask len to mask */
4153 bpf_error("mask length must be <= 32");
4154 m
= 0xffffffff << (32 - masklen
);
4156 bpf_error("non-network bits set in \"%s/%d\"",
4163 return gen_host(n
, m
, q
.proto
, q
.dir
);
4166 bpf_error("Mask syntax for networks only");
4174 register const char *s
;
4179 int proto
= q
.proto
;
4185 else if (q
.proto
== Q_DECNET
)
4186 vlen
= __pcap_atodn(s
, &v
);
4188 vlen
= __pcap_atoin(s
, &v
);
4195 if (proto
== Q_DECNET
)
4196 return gen_host(v
, 0, proto
, dir
);
4197 else if (proto
== Q_LINK
) {
4198 bpf_error("illegal link layer address");
4201 if (s
== NULL
&& q
.addr
== Q_NET
) {
4202 /* Promote short net number */
4203 while (v
&& (v
& 0xff000000) == 0) {
4208 /* Promote short ipaddr */
4212 return gen_host(v
, mask
, proto
, dir
);
4217 proto
= IPPROTO_UDP
;
4218 else if (proto
== Q_TCP
)
4219 proto
= IPPROTO_TCP
;
4220 else if (proto
== Q_SCTP
)
4221 proto
= IPPROTO_SCTP
;
4222 else if (proto
== Q_DEFAULT
)
4223 proto
= PROTO_UNDEF
;
4225 bpf_error("illegal qualifier of 'port'");
4228 return gen_port((int)v
, proto
, dir
);
4232 b
= gen_port((int)v
, proto
, dir
);
4233 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4239 bpf_error("'gateway' requires a name");
4243 return gen_proto((int)v
, proto
, dir
);
4246 return gen_protochain((int)v
, proto
, dir
);
4261 gen_mcode6(s1
, s2
, masklen
, q
)
4262 register const char *s1
, *s2
;
4263 register int masklen
;
4266 struct addrinfo
*res
;
4267 struct in6_addr
*addr
;
4268 struct in6_addr mask
;
4273 bpf_error("no mask %s supported", s2
);
4275 res
= pcap_nametoaddrinfo(s1
);
4277 bpf_error("invalid ip6 address %s", s1
);
4279 bpf_error("%s resolved to multiple address", s1
);
4280 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4282 if (sizeof(mask
) * 8 < masklen
)
4283 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4284 memset(&mask
, 0, sizeof(mask
));
4285 memset(&mask
, 0xff, masklen
/ 8);
4287 mask
.s6_addr
[masklen
/ 8] =
4288 (0xff << (8 - masklen
% 8)) & 0xff;
4291 a
= (u_int32_t
*)addr
;
4292 m
= (u_int32_t
*)&mask
;
4293 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4294 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4295 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4303 bpf_error("Mask syntax for networks only");
4307 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4312 bpf_error("invalid qualifier against IPv6 address");
4320 register const u_char
*eaddr
;
4323 struct block
*b
, *tmp
;
4325 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4326 if (linktype
== DLT_EN10MB
)
4327 return gen_ehostop(eaddr
, (int)q
.dir
);
4328 if (linktype
== DLT_FDDI
)
4329 return gen_fhostop(eaddr
, (int)q
.dir
);
4330 if (linktype
== DLT_IEEE802
)
4331 return gen_thostop(eaddr
, (int)q
.dir
);
4332 if (linktype
== DLT_IEEE802_11
)
4333 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4334 if (linktype
== DLT_SUNATM
&& is_lane
) {
4336 * Check that the packet doesn't begin with an
4337 * LE Control marker. (We've already generated
4340 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4344 * Now check the MAC address.
4346 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4350 if (linktype
== DLT_IP_OVER_FC
)
4351 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4352 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4354 bpf_error("ethernet address used in non-ether expression");
4360 struct slist
*s0
, *s1
;
4363 * This is definitely not the best way to do this, but the
4364 * lists will rarely get long.
4371 static struct slist
*
4377 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4382 static struct slist
*
4388 s
= new_stmt(BPF_LD
|BPF_MEM
);
4394 gen_load(proto
, index
, size
)
4399 struct slist
*s
, *tmp
;
4401 int regno
= alloc_reg();
4403 free_reg(index
->regno
);
4407 bpf_error("data size must be 1, 2, or 4");
4423 bpf_error("unsupported index operation");
4427 * XXX - what about ATM LANE? Should the index be
4428 * relative to the beginning of the AAL5 frame, so
4429 * that 0 refers to the beginning of the LE Control
4430 * field, or relative to the beginning of the LAN
4431 * frame, so that 0 refers, for Ethernet LANE, to
4432 * the beginning of the destination address?
4434 s
= xfer_to_x(index
);
4435 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4437 sappend(index
->s
, s
);
4452 /* XXX Note that we assume a fixed link header here. */
4453 s
= xfer_to_x(index
);
4454 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4457 sappend(index
->s
, s
);
4459 b
= gen_proto_abbrev(proto
);
4461 gen_and(index
->b
, b
);
4473 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4475 sappend(s
, xfer_to_a(index
));
4476 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4477 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4478 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4480 sappend(index
->s
, s
);
4482 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4484 gen_and(index
->b
, b
);
4486 gen_and(gen_proto_abbrev(Q_IP
), b
);
4492 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4496 index
->regno
= regno
;
4497 s
= new_stmt(BPF_ST
);
4499 sappend(index
->s
, s
);
4505 gen_relation(code
, a0
, a1
, reversed
)
4507 struct arth
*a0
, *a1
;
4510 struct slist
*s0
, *s1
, *s2
;
4511 struct block
*b
, *tmp
;
4515 if (code
== BPF_JEQ
) {
4516 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4517 b
= new_block(JMP(code
));
4521 b
= new_block(BPF_JMP
|code
|BPF_X
);
4527 sappend(a0
->s
, a1
->s
);
4531 free_reg(a0
->regno
);
4532 free_reg(a1
->regno
);
4534 /* 'and' together protocol checks */
4537 gen_and(a0
->b
, tmp
= a1
->b
);
4553 int regno
= alloc_reg();
4554 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4557 s
= new_stmt(BPF_LD
|BPF_LEN
);
4558 s
->next
= new_stmt(BPF_ST
);
4559 s
->next
->s
.k
= regno
;
4574 a
= (struct arth
*)newchunk(sizeof(*a
));
4578 s
= new_stmt(BPF_LD
|BPF_IMM
);
4580 s
->next
= new_stmt(BPF_ST
);
4596 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4599 s
= new_stmt(BPF_ST
);
4607 gen_arth(code
, a0
, a1
)
4609 struct arth
*a0
, *a1
;
4611 struct slist
*s0
, *s1
, *s2
;
4615 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4620 sappend(a0
->s
, a1
->s
);
4622 free_reg(a0
->regno
);
4623 free_reg(a1
->regno
);
4625 s0
= new_stmt(BPF_ST
);
4626 a0
->regno
= s0
->s
.k
= alloc_reg();
4633 * Here we handle simple allocation of the scratch registers.
4634 * If too many registers are alloc'd, the allocator punts.
4636 static int regused
[BPF_MEMWORDS
];
4640 * Return the next free register.
4645 int n
= BPF_MEMWORDS
;
4648 if (regused
[curreg
])
4649 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4651 regused
[curreg
] = 1;
4655 bpf_error("too many registers needed to evaluate expression");
4660 * Return a register to the table so it can
4670 static struct block
*
4677 s
= new_stmt(BPF_LD
|BPF_LEN
);
4678 b
= new_block(JMP(jmp
));
4689 return gen_len(BPF_JGE
, n
);
4693 * Actually, this is less than or equal.
4701 b
= gen_len(BPF_JGT
, n
);
4708 gen_byteop(op
, idx
, val
)
4719 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4722 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4723 b
->s
.code
= JMP(BPF_JGE
);
4728 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4729 b
->s
.code
= JMP(BPF_JGT
);
4733 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4737 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4741 b
= new_block(JMP(BPF_JEQ
));
4748 static u_char abroadcast
[] = { 0x0 };
4751 gen_broadcast(proto
)
4754 bpf_u_int32 hostmask
;
4755 struct block
*b0
, *b1
, *b2
;
4756 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4762 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4763 return gen_ahostop(abroadcast
, Q_DST
);
4764 if (linktype
== DLT_EN10MB
)
4765 return gen_ehostop(ebroadcast
, Q_DST
);
4766 if (linktype
== DLT_FDDI
)
4767 return gen_fhostop(ebroadcast
, Q_DST
);
4768 if (linktype
== DLT_IEEE802
)
4769 return gen_thostop(ebroadcast
, Q_DST
);
4770 if (linktype
== DLT_IEEE802_11
)
4771 return gen_wlanhostop(ebroadcast
, Q_DST
);
4772 if (linktype
== DLT_IP_OVER_FC
)
4773 return gen_ipfchostop(ebroadcast
, Q_DST
);
4774 if (linktype
== DLT_SUNATM
&& is_lane
) {
4776 * Check that the packet doesn't begin with an
4777 * LE Control marker. (We've already generated
4780 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4784 * Now check the MAC address.
4786 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4790 bpf_error("not a broadcast link");
4794 b0
= gen_linktype(ETHERTYPE_IP
);
4795 hostmask
= ~netmask
;
4796 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4797 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4798 (bpf_int32
)(~0 & hostmask
), hostmask
);
4803 bpf_error("only link-layer/IP broadcast filters supported");
4808 * Generate code to test the low-order bit of a MAC address (that's
4809 * the bottom bit of the *first* byte).
4811 static struct block
*
4812 gen_mac_multicast(offset
)
4815 register struct block
*b0
;
4816 register struct slist
*s
;
4818 /* link[offset] & 1 != 0 */
4819 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4821 b0
= new_block(JMP(BPF_JSET
));
4828 gen_multicast(proto
)
4831 register struct block
*b0
, *b1
, *b2
;
4832 register struct slist
*s
;
4838 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4839 /* all ARCnet multicasts use the same address */
4840 return gen_ahostop(abroadcast
, Q_DST
);
4842 if (linktype
== DLT_EN10MB
) {
4843 /* ether[0] & 1 != 0 */
4844 return gen_mac_multicast(0);
4847 if (linktype
== DLT_FDDI
) {
4849 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4851 * XXX - was that referring to bit-order issues?
4853 /* fddi[1] & 1 != 0 */
4854 return gen_mac_multicast(1);
4857 if (linktype
== DLT_IEEE802
) {
4858 /* tr[2] & 1 != 0 */
4859 return gen_mac_multicast(2);
4862 if (linktype
== DLT_IEEE802_11
) {
4866 * For control frames, there is no DA.
4868 * For management frames, DA is at an
4869 * offset of 4 from the beginning of
4872 * For data frames, DA is at an offset
4873 * of 4 from the beginning of the packet
4874 * if To DS is clear and at an offset of
4875 * 16 from the beginning of the packet
4880 * Generate the tests to be done for data frames.
4882 * First, check for To DS set, i.e. "link[1] & 0x01".
4884 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4886 b1
= new_block(JMP(BPF_JSET
));
4887 b1
->s
.k
= 0x01; /* To DS */
4891 * If To DS is set, the DA is at 16.
4893 b0
= gen_mac_multicast(16);
4897 * Now, check for To DS not set, i.e. check
4898 * "!(link[1] & 0x01)".
4900 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4902 b2
= new_block(JMP(BPF_JSET
));
4903 b2
->s
.k
= 0x01; /* To DS */
4908 * If To DS is not set, the DA is at 4.
4910 b1
= gen_mac_multicast(4);
4914 * Now OR together the last two checks. That gives
4915 * the complete set of checks for data frames.
4920 * Now check for a data frame.
4921 * I.e, check "link[0] & 0x08".
4923 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4925 b1
= new_block(JMP(BPF_JSET
));
4930 * AND that with the checks done for data frames.
4935 * If the high-order bit of the type value is 0, this
4936 * is a management frame.
4937 * I.e, check "!(link[0] & 0x08)".
4939 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4941 b2
= new_block(JMP(BPF_JSET
));
4947 * For management frames, the DA is at 4.
4949 b1
= gen_mac_multicast(4);
4953 * OR that with the checks done for data frames.
4954 * That gives the checks done for management and
4960 * If the low-order bit of the type value is 1,
4961 * this is either a control frame or a frame
4962 * with a reserved type, and thus not a
4965 * I.e., check "!(link[0] & 0x04)".
4967 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4969 b1
= new_block(JMP(BPF_JSET
));
4975 * AND that with the checks for data and management
4982 if (linktype
== DLT_IP_OVER_FC
) {
4983 b0
= gen_mac_multicast(2);
4987 if (linktype
== DLT_SUNATM
&& is_lane
) {
4989 * Check that the packet doesn't begin with an
4990 * LE Control marker. (We've already generated
4993 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4996 /* ether[off_mac] & 1 != 0 */
4997 b0
= gen_mac_multicast(off_mac
);
5002 /* Link not known to support multicasts */
5006 b0
= gen_linktype(ETHERTYPE_IP
);
5007 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5008 b1
->s
.code
= JMP(BPF_JGE
);
5014 b0
= gen_linktype(ETHERTYPE_IPV6
);
5015 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5020 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5025 * generate command for inbound/outbound. It's here so we can
5026 * make it link-type specific. 'dir' = 0 implies "inbound",
5027 * = 1 implies "outbound".
5033 register struct block
*b0
;
5036 * Only some data link types support inbound/outbound qualifiers.
5040 b0
= gen_relation(BPF_JEQ
,
5041 gen_load(Q_LINK
, gen_loadi(0), 1),
5049 * Match packets sent by this machine.
5051 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5054 * Match packets sent to this machine.
5055 * (No broadcast or multicast packets, or
5056 * packets sent to some other machine and
5057 * received promiscuously.)
5059 * XXX - packets sent to other machines probably
5060 * shouldn't be matched, but what about broadcast
5061 * or multicast packets we received?
5063 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5068 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5069 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5072 case DLT_PPP_WITHDIRECTION
:
5074 /* match outgoing packets */
5075 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_OUT
);
5077 /* match incoming packets */
5078 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_IN
);
5083 bpf_error("inbound/outbound not supported on linktype %d",
5091 /* PF firewall log matched interface */
5093 gen_pf_ifname(const char *ifname
)
5098 if (linktype
== DLT_PFLOG
) {
5099 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5100 off
= offsetof(struct pfloghdr
, ifname
);
5102 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5105 if (strlen(ifname
) >= len
) {
5106 bpf_error("ifname interface names can only be %d characters",
5110 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5114 /* PF firewall log matched interface */
5116 gen_pf_ruleset(char *ruleset
)
5120 if (linktype
!= DLT_PFLOG
) {
5121 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5124 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5125 bpf_error("ruleset names can only be %ld characters",
5126 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5129 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5130 strlen(ruleset
), (const u_char
*)ruleset
);
5134 /* PF firewall log rule number */
5140 if (linktype
== DLT_PFLOG
) {
5141 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5144 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5151 /* PF firewall log sub-rule number */
5153 gen_pf_srnr(int srnr
)
5157 if (linktype
!= DLT_PFLOG
) {
5158 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5162 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5167 /* PF firewall log reason code */
5169 gen_pf_reason(int reason
)
5173 if (linktype
== DLT_PFLOG
) {
5174 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5177 bpf_error("reason not supported on linktype 0x%x", linktype
);
5184 /* PF firewall log action */
5186 gen_pf_action(int action
)
5190 if (linktype
== DLT_PFLOG
) {
5191 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5194 bpf_error("action not supported on linktype 0x%x", linktype
);
5203 register const u_char
*eaddr
;
5206 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5207 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5208 return gen_ahostop(eaddr
, (int)q
.dir
);
5210 bpf_error("ARCnet address used in non-arc expression");
5214 static struct block
*
5215 gen_ahostop(eaddr
, dir
)
5216 register const u_char
*eaddr
;
5219 register struct block
*b0
, *b1
;
5222 /* src comes first, different from Ethernet */
5224 return gen_bcmp(0, 1, eaddr
);
5227 return gen_bcmp(1, 1, eaddr
);
5230 b0
= gen_ahostop(eaddr
, Q_SRC
);
5231 b1
= gen_ahostop(eaddr
, Q_DST
);
5237 b0
= gen_ahostop(eaddr
, Q_SRC
);
5238 b1
= gen_ahostop(eaddr
, Q_DST
);
5247 * support IEEE 802.1Q VLAN trunk over ethernet
5256 * Change the offsets to point to the type and data fields within
5257 * the VLAN packet. This is somewhat of a kludge.
5259 if (orig_nl
== (u_int
)-1) {
5260 orig_linktype
= off_linktype
; /* save original values */
5262 orig_nl_nosnap
= off_nl_nosnap
;
5273 bpf_error("no VLAN support for data link type %d",
5279 /* check for VLAN */
5280 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5282 /* If a specific VLAN is requested, check VLAN id */
5283 if (vlan_num
>= 0) {
5286 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5304 * Change the offsets to point to the type and data fields within
5305 * the MPLS packet. This is somewhat of a kludge.
5307 if (orig_nl
== (u_int
)-1) {
5308 orig_linktype
= off_linktype
; /* save original values */
5310 orig_nl_nosnap
= off_nl_nosnap
;
5319 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5327 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5335 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5338 /* FIXME add other DLT_s ...
5339 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5340 * leave it for now */
5343 bpf_error("no MPLS support for data link type %d",
5348 bpf_error("'mpls' can't be combined with 'vlan' or another 'mpls'");
5352 /* If a specific MPLS label is requested, check it */
5353 if (label_num
>= 0) {
5356 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5357 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5366 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5378 bpf_error("'vpi' supported only on raw ATM");
5379 if (off_vpi
== (u_int
)-1)
5381 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5382 (u_int
)jvalue
, reverse
);
5387 bpf_error("'vci' supported only on raw ATM");
5388 if (off_vci
== (u_int
)-1)
5390 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5391 (u_int
)jvalue
, reverse
);
5395 if (off_proto
== (u_int
)-1)
5396 abort(); /* XXX - this isn't on FreeBSD */
5397 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5398 (u_int
)jvalue
, reverse
);
5402 if (off_payload
== (u_int
)-1)
5404 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5405 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5410 bpf_error("'callref' supported only on raw ATM");
5411 if (off_proto
== (u_int
)-1)
5413 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5414 (u_int
)jvalue
, reverse
);
5424 gen_atmtype_abbrev(type
)
5427 struct block
*b0
, *b1
;
5432 /* Get all packets in Meta signalling Circuit */
5434 bpf_error("'metac' supported only on raw ATM");
5435 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5436 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5441 /* Get all packets in Broadcast Circuit*/
5443 bpf_error("'bcc' supported only on raw ATM");
5444 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5445 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5450 /* Get all cells in Segment OAM F4 circuit*/
5452 bpf_error("'oam4sc' supported only on raw ATM");
5453 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5454 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5459 /* Get all cells in End-to-End OAM F4 Circuit*/
5461 bpf_error("'oam4ec' supported only on raw ATM");
5462 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5463 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5468 /* Get all packets in connection Signalling Circuit */
5470 bpf_error("'sc' supported only on raw ATM");
5471 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5472 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5477 /* Get all packets in ILMI Circuit */
5479 bpf_error("'ilmic' supported only on raw ATM");
5480 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5481 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5486 /* Get all LANE packets */
5488 bpf_error("'lane' supported only on raw ATM");
5489 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5492 * Arrange that all subsequent tests assume LANE
5493 * rather than LLC-encapsulated packets, and set
5494 * the offsets appropriately for LANE-encapsulated
5497 * "off_mac" is the offset of the Ethernet header,
5498 * which is 2 bytes past the ATM pseudo-header
5499 * (skipping the pseudo-header and 2-byte LE Client
5500 * field). The other offsets are Ethernet offsets
5501 * relative to "off_mac".
5504 off_mac
= off_payload
+ 2; /* MAC header */
5505 off_linktype
= off_mac
+ 12;
5506 off_nl
= off_mac
+ 14; /* Ethernet II */
5507 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5511 /* Get all LLC-encapsulated packets */
5513 bpf_error("'llc' supported only on raw ATM");
5514 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5525 static struct block
*
5526 gen_msg_abbrev(type
)
5532 * Q.2931 signalling protocol messages for handling virtual circuits
5533 * establishment and teardown
5538 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5542 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5546 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5550 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5554 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5557 case A_RELEASE_DONE
:
5558 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5568 gen_atmmulti_abbrev(type
)
5571 struct block
*b0
, *b1
;
5577 bpf_error("'oam' supported only on raw ATM");
5578 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5583 bpf_error("'oamf4' supported only on raw ATM");
5585 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5586 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5588 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5594 * Get Q.2931 signalling messages for switched
5595 * virtual connection
5598 bpf_error("'connectmsg' supported only on raw ATM");
5599 b0
= gen_msg_abbrev(A_SETUP
);
5600 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5602 b0
= gen_msg_abbrev(A_CONNECT
);
5604 b0
= gen_msg_abbrev(A_CONNECTACK
);
5606 b0
= gen_msg_abbrev(A_RELEASE
);
5608 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5610 b0
= gen_atmtype_abbrev(A_SC
);
5616 bpf_error("'metaconnect' supported only on raw ATM");
5617 b0
= gen_msg_abbrev(A_SETUP
);
5618 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5620 b0
= gen_msg_abbrev(A_CONNECT
);
5622 b0
= gen_msg_abbrev(A_RELEASE
);
5624 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5626 b0
= gen_atmtype_abbrev(A_METAC
);