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.210 2004-10-19 15:55:28 hannes Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #define offsetof(s, e) ((size_t)&((s *)0)->e)
79 #include <netdb.h> /* for "struct addrinfo" */
82 #include <pcap-namedb.h>
87 #define IPPROTO_SCTP 132
90 #ifdef HAVE_OS_PROTO_H
94 #define JMP(c) ((c)|BPF_JMP|BPF_K)
97 static jmp_buf top_ctx
;
98 static pcap_t
*bpf_pcap
;
100 /* Hack for updating VLAN, MPLS offsets. */
101 static u_int orig_linktype
= -1U, orig_nl
= -1U, orig_nl_nosnap
= -1U;
105 int pcap_fddipad
= PCAP_FDDIPAD
;
112 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(int);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * We divy out chunks of memory rather than call malloc each time so
135 * we don't have to worry about leaking memory. It's probably
136 * not a big deal if all this memory was wasted but it this ever
137 * goes into a library that would probably not be a good idea.
140 #define CHUNK0SIZE 1024
146 static struct chunk chunks
[NCHUNKS
];
147 static int cur_chunk
;
149 static void *newchunk(u_int
);
150 static void freechunks(void);
151 static inline struct block
*new_block(int);
152 static inline struct slist
*new_stmt(int);
153 static struct block
*gen_retblk(int);
154 static inline void syntax(void);
156 static void backpatch(struct block
*, struct block
*);
157 static void merge(struct block
*, struct block
*);
158 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
159 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
160 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
161 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
162 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
163 bpf_u_int32
, bpf_u_int32
, int);
164 static struct block
*gen_uncond(int);
165 static inline struct block
*gen_true(void);
166 static inline struct block
*gen_false(void);
167 static struct block
*gen_ether_linktype(int);
168 static struct block
*gen_linktype(int);
169 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
170 static struct block
*gen_llc(int);
171 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
173 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
175 static struct block
*gen_ahostop(const u_char
*, int);
176 static struct block
*gen_ehostop(const u_char
*, int);
177 static struct block
*gen_fhostop(const u_char
*, int);
178 static struct block
*gen_thostop(const u_char
*, int);
179 static struct block
*gen_wlanhostop(const u_char
*, int);
180 static struct block
*gen_ipfchostop(const u_char
*, int);
181 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
182 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
184 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
187 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
189 static struct block
*gen_ipfrag(void);
190 static struct block
*gen_portatom(int, bpf_int32
);
192 static struct block
*gen_portatom6(int, bpf_int32
);
194 struct block
*gen_portop(int, int, int);
195 static struct block
*gen_port(int, int, int);
197 struct block
*gen_portop6(int, int, int);
198 static struct block
*gen_port6(int, int, int);
200 static int lookup_proto(const char *, int);
201 static struct block
*gen_protochain(int, int, int);
202 static struct block
*gen_proto(int, int, int);
203 static struct slist
*xfer_to_x(struct arth
*);
204 static struct slist
*xfer_to_a(struct arth
*);
205 static struct block
*gen_mac_multicast(int);
206 static struct block
*gen_len(int, int);
208 static struct block
*gen_msg_abbrev(int type
);
219 /* XXX Round up to nearest long. */
220 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
222 /* XXX Round up to structure boundary. */
226 cp
= &chunks
[cur_chunk
];
227 if (n
> cp
->n_left
) {
228 ++cp
, k
= ++cur_chunk
;
230 bpf_error("out of memory");
231 size
= CHUNK0SIZE
<< k
;
232 cp
->m
= (void *)malloc(size
);
234 bpf_error("out of memory");
235 memset((char *)cp
->m
, 0, size
);
238 bpf_error("out of memory");
241 return (void *)((char *)cp
->m
+ cp
->n_left
);
250 for (i
= 0; i
< NCHUNKS
; ++i
)
251 if (chunks
[i
].m
!= NULL
) {
258 * A strdup whose allocations are freed after code generation is over.
262 register const char *s
;
264 int n
= strlen(s
) + 1;
265 char *cp
= newchunk(n
);
271 static inline struct block
*
277 p
= (struct block
*)newchunk(sizeof(*p
));
284 static inline struct slist
*
290 p
= (struct slist
*)newchunk(sizeof(*p
));
296 static struct block
*
300 struct block
*b
= new_block(BPF_RET
|BPF_K
);
309 bpf_error("syntax error in filter expression");
312 static bpf_u_int32 netmask
;
317 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
318 char *buf
, int optimize
, bpf_u_int32 mask
)
327 if (setjmp(top_ctx
)) {
335 snaplen
= pcap_snapshot(p
);
337 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
338 "snaplen of 0 rejects all packets");
342 lex_init(buf
? buf
: "");
343 init_linktype(pcap_datalink(p
));
350 root
= gen_retblk(snaplen
);
352 if (optimize
&& !no_optimize
) {
355 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
356 bpf_error("expression rejects all packets");
358 program
->bf_insns
= icode_to_fcode(root
, &len
);
359 program
->bf_len
= len
;
367 * entry point for using the compiler with no pcap open
368 * pass in all the stuff that is needed explicitly instead.
371 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
372 struct bpf_program
*program
,
373 char *buf
, int optimize
, bpf_u_int32 mask
)
378 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
381 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
387 * Clean up a "struct bpf_program" by freeing all the memory allocated
391 pcap_freecode(struct bpf_program
*program
)
394 if (program
->bf_insns
!= NULL
) {
395 free((char *)program
->bf_insns
);
396 program
->bf_insns
= NULL
;
401 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
402 * which of the jt and jf fields has been resolved and which is a pointer
403 * back to another unresolved block (or nil). At least one of the fields
404 * in each block is already resolved.
407 backpatch(list
, target
)
408 struct block
*list
, *target
;
425 * Merge the lists in b0 and b1, using the 'sense' field to indicate
426 * which of jt and jf is the link.
430 struct block
*b0
, *b1
;
432 register struct block
**p
= &b0
;
434 /* Find end of list. */
436 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
438 /* Concatenate the lists. */
446 backpatch(p
, gen_retblk(snaplen
));
447 p
->sense
= !p
->sense
;
448 backpatch(p
, gen_retblk(0));
454 struct block
*b0
, *b1
;
456 backpatch(b0
, b1
->head
);
457 b0
->sense
= !b0
->sense
;
458 b1
->sense
= !b1
->sense
;
460 b1
->sense
= !b1
->sense
;
466 struct block
*b0
, *b1
;
468 b0
->sense
= !b0
->sense
;
469 backpatch(b0
, b1
->head
);
470 b0
->sense
= !b0
->sense
;
479 b
->sense
= !b
->sense
;
482 static struct block
*
483 gen_cmp(offset
, size
, v
)
490 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
493 b
= new_block(JMP(BPF_JEQ
));
500 static struct block
*
501 gen_cmp_gt(offset
, size
, v
)
508 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
511 b
= new_block(JMP(BPF_JGT
));
518 static struct block
*
519 gen_mcmp(offset
, size
, v
, mask
)
524 struct block
*b
= gen_cmp(offset
, size
, v
);
527 if (mask
!= 0xffffffff) {
528 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
535 static struct block
*
536 gen_bcmp(offset
, size
, v
)
537 register u_int offset
, size
;
538 register const u_char
*v
;
540 register struct block
*b
, *tmp
;
544 register const u_char
*p
= &v
[size
- 4];
545 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
546 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
548 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
555 register const u_char
*p
= &v
[size
- 2];
556 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
558 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
565 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
573 static struct block
*
574 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
575 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
581 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
584 if (mask
!= 0xffffffff) {
585 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
589 b
= new_block(JMP(jtype
));
592 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
598 * Various code constructs need to know the layout of the data link
599 * layer. These variables give the necessary offsets.
603 * This is the offset of the beginning of the MAC-layer header.
604 * It's usually 0, except for ATM LANE.
606 static u_int off_mac
;
609 * "off_linktype" is the offset to information in the link-layer header
610 * giving the packet type.
612 * For Ethernet, it's the offset of the Ethernet type field.
614 * For link-layer types that always use 802.2 headers, it's the
615 * offset of the LLC header.
617 * For PPP, it's the offset of the PPP type field.
619 * For Cisco HDLC, it's the offset of the CHDLC type field.
621 * For BSD loopback, it's the offset of the AF_ value.
623 * For Linux cooked sockets, it's the offset of the type field.
625 * It's set to -1 for no encapsulation, in which case, IP is assumed.
627 static u_int off_linktype
;
630 * TRUE if the link layer includes an ATM pseudo-header.
632 static int is_atm
= 0;
635 * TRUE if "lane" appeared in the filter; it causes us to generate
636 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
638 static int is_lane
= 0;
641 * These are offsets for the ATM pseudo-header.
643 static u_int off_vpi
;
644 static u_int off_vci
;
645 static u_int off_proto
;
648 * This is the offset of the first byte after the ATM pseudo_header,
649 * or -1 if there is no ATM pseudo-header.
651 static u_int off_payload
;
654 * These are offsets to the beginning of the network-layer header.
656 * If the link layer never uses 802.2 LLC:
658 * "off_nl" and "off_nl_nosnap" are the same.
660 * If the link layer always uses 802.2 LLC:
662 * "off_nl" is the offset if there's a SNAP header following
665 * "off_nl_nosnap" is the offset if there's no SNAP header.
667 * If the link layer is Ethernet:
669 * "off_nl" is the offset if the packet is an Ethernet II packet
670 * (we assume no 802.3+802.2+SNAP);
672 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
673 * with an 802.2 header following it.
676 static u_int off_nl_nosnap
;
687 * Assume it's not raw ATM with a pseudo-header, for now.
705 off_nl
= 6; /* XXX in reality, variable! */
706 off_nl_nosnap
= 6; /* no 802.2 LLC */
709 case DLT_ARCNET_LINUX
:
711 off_nl
= 8; /* XXX in reality, variable! */
712 off_nl_nosnap
= 8; /* no 802.2 LLC */
717 off_nl
= 14; /* Ethernet II */
718 off_nl_nosnap
= 17; /* 802.3+802.2 */
723 * SLIP doesn't have a link level type. The 16 byte
724 * header is hacked into our SLIP driver.
728 off_nl_nosnap
= 16; /* no 802.2 LLC */
732 /* XXX this may be the same as the DLT_PPP_BSDOS case */
736 off_nl_nosnap
= 24; /* no 802.2 LLC */
743 off_nl_nosnap
= 4; /* no 802.2 LLC */
749 off_nl_nosnap
= 12; /* no 802.2 LLC */
753 case DLT_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
;
1019 case DLT_JUNIPER_ATM1
:
1020 off_linktype
= 4; /* in reality variable between 4-8 */
1025 case DLT_JUNIPER_ATM2
:
1026 off_linktype
= 8; /* in reality variable between 8-12 */
1039 bpf_error("unknown data link type %d", linktype
);
1043 static struct block
*
1050 s
= new_stmt(BPF_LD
|BPF_IMM
);
1052 b
= new_block(JMP(BPF_JEQ
));
1058 static inline struct block
*
1061 return gen_uncond(1);
1064 static inline struct block
*
1067 return gen_uncond(0);
1071 * Byte-swap a 32-bit number.
1072 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1073 * big-endian platforms.)
1075 #define SWAPLONG(y) \
1076 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1078 static struct block
*
1079 gen_ether_linktype(proto
)
1082 struct block
*b0
, *b1
;
1088 * OSI protocols always use 802.2 encapsulation.
1089 * XXX - should we check both the DSAP and the
1090 * SSAP, like this, or should we check just the
1093 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1095 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1096 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1101 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1103 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1104 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1108 case LLCSAP_NETBEUI
:
1110 * NetBEUI always uses 802.2 encapsulation.
1111 * XXX - should we check both the DSAP and the
1112 * SSAP, like this, or should we check just the
1115 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1117 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1118 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1126 * Ethernet_II frames, which are Ethernet
1127 * frames with a frame type of ETHERTYPE_IPX;
1129 * Ethernet_802.3 frames, which are 802.3
1130 * frames (i.e., the type/length field is
1131 * a length field, <= ETHERMTU, rather than
1132 * a type field) with the first two bytes
1133 * after the Ethernet/802.3 header being
1136 * Ethernet_802.2 frames, which are 802.3
1137 * frames with an 802.2 LLC header and
1138 * with the IPX LSAP as the DSAP in the LLC
1141 * Ethernet_SNAP frames, which are 802.3
1142 * frames with an LLC header and a SNAP
1143 * header and with an OUI of 0x000000
1144 * (encapsulated Ethernet) and a protocol
1145 * ID of ETHERTYPE_IPX in the SNAP header.
1147 * XXX - should we generate the same code both
1148 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1152 * This generates code to check both for the
1153 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1155 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1156 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1160 * Now we add code to check for SNAP frames with
1161 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1163 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1167 * Now we generate code to check for 802.3
1168 * frames in general.
1170 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1174 * Now add the check for 802.3 frames before the
1175 * check for Ethernet_802.2 and Ethernet_802.3,
1176 * as those checks should only be done on 802.3
1177 * frames, not on Ethernet frames.
1182 * Now add the check for Ethernet_II frames, and
1183 * do that before checking for the other frame
1186 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1190 case ETHERTYPE_ATALK
:
1191 case ETHERTYPE_AARP
:
1193 * EtherTalk (AppleTalk protocols on Ethernet link
1194 * layer) may use 802.2 encapsulation.
1198 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1199 * we check for an Ethernet type field less than
1200 * 1500, which means it's an 802.3 length field.
1202 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1206 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1207 * SNAP packets with an organization code of
1208 * 0x080007 (Apple, for Appletalk) and a protocol
1209 * type of ETHERTYPE_ATALK (Appletalk).
1211 * 802.2-encapsulated ETHERTYPE_AARP packets are
1212 * SNAP packets with an organization code of
1213 * 0x000000 (encapsulated Ethernet) and a protocol
1214 * type of ETHERTYPE_AARP (Appletalk ARP).
1216 if (proto
== ETHERTYPE_ATALK
)
1217 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1218 else /* proto == ETHERTYPE_AARP */
1219 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1223 * Check for Ethernet encapsulation (Ethertalk
1224 * phase 1?); we just check for the Ethernet
1227 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1233 if (proto
<= ETHERMTU
) {
1235 * This is an LLC SAP value, so the frames
1236 * that match would be 802.2 frames.
1237 * Check that the frame is an 802.2 frame
1238 * (i.e., that the length/type field is
1239 * a length field, <= ETHERMTU) and
1240 * then check the DSAP.
1242 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1244 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1249 * This is an Ethernet type, so compare
1250 * the length/type field with it (if
1251 * the frame is an 802.2 frame, the length
1252 * field will be <= ETHERMTU, and, as
1253 * "proto" is > ETHERMTU, this test
1254 * will fail and the frame won't match,
1255 * which is what we want).
1257 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1262 static struct block
*
1266 struct block
*b0
, *b1
, *b2
;
1271 return gen_ether_linktype(proto
);
1279 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1283 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1289 case DLT_IEEE802_11
:
1290 case DLT_PRISM_HEADER
:
1291 case DLT_IEEE802_11_RADIO
:
1294 case DLT_ATM_RFC1483
:
1296 case DLT_IP_OVER_FC
:
1297 return gen_llc(proto
);
1303 * If "is_lane" is set, check for a LANE-encapsulated
1304 * version of this protocol, otherwise check for an
1305 * LLC-encapsulated version of this protocol.
1307 * We assume LANE means Ethernet, not Token Ring.
1311 * Check that the packet doesn't begin with an
1312 * LE Control marker. (We've already generated
1315 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1319 * Now generate an Ethernet test.
1321 b1
= gen_ether_linktype(proto
);
1326 * Check for LLC encapsulation and then check the
1329 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1330 b1
= gen_llc(proto
);
1339 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1340 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1341 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1347 * OSI protocols always use 802.2 encapsulation.
1348 * XXX - should we check both the DSAP and the
1349 * LSAP, like this, or should we check just the
1352 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1353 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1354 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1358 case LLCSAP_NETBEUI
:
1360 * NetBEUI always uses 802.2 encapsulation.
1361 * XXX - should we check both the DSAP and the
1362 * LSAP, like this, or should we check just the
1365 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1366 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1367 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1373 * Ethernet_II frames, which are Ethernet
1374 * frames with a frame type of ETHERTYPE_IPX;
1376 * Ethernet_802.3 frames, which have a frame
1377 * type of LINUX_SLL_P_802_3;
1379 * Ethernet_802.2 frames, which are 802.3
1380 * frames with an 802.2 LLC header (i.e, have
1381 * a frame type of LINUX_SLL_P_802_2) and
1382 * with the IPX LSAP as the DSAP in the LLC
1385 * Ethernet_SNAP frames, which are 802.3
1386 * frames with an LLC header and a SNAP
1387 * header and with an OUI of 0x000000
1388 * (encapsulated Ethernet) and a protocol
1389 * ID of ETHERTYPE_IPX in the SNAP header.
1391 * First, do the checks on LINUX_SLL_P_802_2
1392 * frames; generate the check for either
1393 * Ethernet_802.2 or Ethernet_SNAP frames, and
1394 * then put a check for LINUX_SLL_P_802_2 frames
1397 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1398 (bpf_int32
)LLCSAP_IPX
);
1399 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1402 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1406 * Now check for 802.3 frames and OR that with
1407 * the previous test.
1409 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1413 * Now add the check for Ethernet_II frames, and
1414 * do that before checking for the other frame
1417 b0
= gen_cmp(off_linktype
, BPF_H
,
1418 (bpf_int32
)ETHERTYPE_IPX
);
1422 case ETHERTYPE_ATALK
:
1423 case ETHERTYPE_AARP
:
1425 * EtherTalk (AppleTalk protocols on Ethernet link
1426 * layer) may use 802.2 encapsulation.
1430 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1431 * we check for the 802.2 protocol type in the
1432 * "Ethernet type" field.
1434 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1437 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1438 * SNAP packets with an organization code of
1439 * 0x080007 (Apple, for Appletalk) and a protocol
1440 * type of ETHERTYPE_ATALK (Appletalk).
1442 * 802.2-encapsulated ETHERTYPE_AARP packets are
1443 * SNAP packets with an organization code of
1444 * 0x000000 (encapsulated Ethernet) and a protocol
1445 * type of ETHERTYPE_AARP (Appletalk ARP).
1447 if (proto
== ETHERTYPE_ATALK
)
1448 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1450 else /* proto == ETHERTYPE_AARP */
1451 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1456 * Check for Ethernet encapsulation (Ethertalk
1457 * phase 1?); we just check for the Ethernet
1460 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1466 if (proto
<= ETHERMTU
) {
1468 * This is an LLC SAP value, so the frames
1469 * that match would be 802.2 frames.
1470 * Check for the 802.2 protocol type
1471 * in the "Ethernet type" field, and
1472 * then check the DSAP.
1474 b0
= gen_cmp(off_linktype
, BPF_H
,
1476 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1482 * This is an Ethernet type, so compare
1483 * the length/type field with it (if
1484 * the frame is an 802.2 frame, the length
1485 * field will be <= ETHERMTU, and, as
1486 * "proto" is > ETHERMTU, this test
1487 * will fail and the frame won't match,
1488 * which is what we want).
1490 return gen_cmp(off_linktype
, BPF_H
,
1498 case DLT_SLIP_BSDOS
:
1501 * These types don't provide any type field; packets
1504 * XXX - for IPv4, check for a version number of 4, and,
1505 * for IPv6, check for a version number of 6?
1511 case ETHERTYPE_IPV6
:
1513 return gen_true(); /* always true */
1516 return gen_false(); /* always false */
1522 case DLT_PPP_WITHDIRECTION
:
1523 case DLT_PPP_SERIAL
:
1526 * We use Ethernet protocol types inside libpcap;
1527 * map them to the corresponding PPP protocol types.
1536 case ETHERTYPE_IPV6
:
1545 case ETHERTYPE_ATALK
:
1559 * I'm assuming the "Bridging PDU"s that go
1560 * over PPP are Spanning Tree Protocol
1574 * We use Ethernet protocol types inside libpcap;
1575 * map them to the corresponding PPP protocol types.
1580 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1581 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1583 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1588 case ETHERTYPE_IPV6
:
1598 case ETHERTYPE_ATALK
:
1612 * I'm assuming the "Bridging PDU"s that go
1613 * over PPP are Spanning Tree Protocol
1629 * For DLT_NULL, the link-layer header is a 32-bit
1630 * word containing an AF_ value in *host* byte order,
1631 * and for DLT_ENC, the link-layer header begins
1632 * with a 32-bit work containing an AF_ value in
1635 * In addition, if we're reading a saved capture file,
1636 * the host byte order in the capture may not be the
1637 * same as the host byte order on this machine.
1639 * For DLT_LOOP, the link-layer header is a 32-bit
1640 * word containing an AF_ value in *network* byte order.
1642 * XXX - AF_ values may, unfortunately, be platform-
1643 * dependent; for example, FreeBSD's AF_INET6 is 24
1644 * whilst NetBSD's and OpenBSD's is 26.
1646 * This means that, when reading a capture file, just
1647 * checking for our AF_INET6 value won't work if the
1648 * capture file came from another OS.
1657 case ETHERTYPE_IPV6
:
1664 * Not a type on which we support filtering.
1665 * XXX - support those that have AF_ values
1666 * #defined on this platform, at least?
1671 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1673 * The AF_ value is in host byte order, but
1674 * the BPF interpreter will convert it to
1675 * network byte order.
1677 * If this is a save file, and it's from a
1678 * machine with the opposite byte order to
1679 * ours, we byte-swap the AF_ value.
1681 * Then we run it through "htonl()", and
1682 * generate code to compare against the result.
1684 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1685 bpf_pcap
->sf
.swapped
)
1686 proto
= SWAPLONG(proto
);
1687 proto
= htonl(proto
);
1689 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1693 * af field is host byte order in contrast to the rest of
1696 if (proto
== ETHERTYPE_IP
)
1697 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1698 (bpf_int32
)AF_INET
));
1700 else if (proto
== ETHERTYPE_IPV6
)
1701 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1702 (bpf_int32
)AF_INET6
));
1710 case DLT_ARCNET_LINUX
:
1712 * XXX should we check for first fragment if the protocol
1721 case ETHERTYPE_IPV6
:
1722 return (gen_cmp(off_linktype
, BPF_B
,
1723 (bpf_int32
)ARCTYPE_INET6
));
1727 b0
= gen_cmp(off_linktype
, BPF_B
,
1728 (bpf_int32
)ARCTYPE_IP
);
1729 b1
= gen_cmp(off_linktype
, BPF_B
,
1730 (bpf_int32
)ARCTYPE_IP_OLD
);
1735 b0
= gen_cmp(off_linktype
, BPF_B
,
1736 (bpf_int32
)ARCTYPE_ARP
);
1737 b1
= gen_cmp(off_linktype
, BPF_B
,
1738 (bpf_int32
)ARCTYPE_ARP_OLD
);
1742 case ETHERTYPE_REVARP
:
1743 return (gen_cmp(off_linktype
, BPF_B
,
1744 (bpf_int32
)ARCTYPE_REVARP
));
1746 case ETHERTYPE_ATALK
:
1747 return (gen_cmp(off_linktype
, BPF_B
,
1748 (bpf_int32
)ARCTYPE_ATALK
));
1755 case ETHERTYPE_ATALK
:
1765 * XXX - assumes a 2-byte Frame Relay header with
1766 * DLCI and flags. What if the address is longer?
1772 * Check for the special NLPID for IP.
1774 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1777 case ETHERTYPE_IPV6
:
1779 * Check for the special NLPID for IPv6.
1781 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1786 * Check for several OSI protocols.
1788 * Frame Relay packets typically have an OSI
1789 * NLPID at the beginning; we check for each
1792 * What we check for is the NLPID and a frame
1793 * control field of UI, i.e. 0x03 followed
1796 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1797 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1798 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1809 case DLT_JUNIPER_ATM1
:
1810 case DLT_JUNIPER_ATM2
:
1811 /* just lets verify the magic number for now -
1812 * we may have upto 6 different encapsulations on the wire
1813 * and need a lot of heuristics to figure out that the payload
1816 * FIXME encapsulation specific BPF_ filters
1818 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1820 case DLT_LINUX_IRDA
:
1821 bpf_error("IrDA link-layer type filtering not implemented");
1824 bpf_error("DOCSIS link-layer type filtering not implemented");
1828 * All the types that have no encapsulation should either be
1829 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1830 * all packets are IP packets, or should be handled in some
1831 * special case, if none of them are (if some are and some
1832 * aren't, the lack of encapsulation is a problem, as we'd
1833 * have to find some other way of determining the packet type).
1835 * Therefore, if "off_linktype" is -1, there's an error.
1837 if (off_linktype
== (u_int
)-1)
1841 * Any type not handled above should always have an Ethernet
1842 * type at an offset of "off_linktype". (PPP is partially
1843 * handled above - the protocol type is mapped from the
1844 * Ethernet and LLC types we use internally to the corresponding
1845 * PPP type - but the PPP type is always specified by a value
1846 * at "off_linktype", so we don't have to do the code generation
1849 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1853 * Check for an LLC SNAP packet with a given organization code and
1854 * protocol type; we check the entire contents of the 802.2 LLC and
1855 * snap headers, checking for DSAP and SSAP of SNAP and a control
1856 * field of 0x03 in the LLC header, and for the specified organization
1857 * code and protocol type in the SNAP header.
1859 static struct block
*
1860 gen_snap(orgcode
, ptype
, offset
)
1861 bpf_u_int32 orgcode
;
1865 u_char snapblock
[8];
1867 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1868 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1869 snapblock
[2] = 0x03; /* control = UI */
1870 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1871 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1872 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1873 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1874 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1875 return gen_bcmp(offset
, 8, snapblock
);
1879 * Check for a given protocol value assuming an 802.2 LLC header.
1881 static struct block
*
1886 * XXX - handle token-ring variable-length header.
1891 return gen_cmp(off_linktype
, BPF_H
, (long)
1892 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1895 return gen_cmp(off_linktype
, BPF_H
, (long)
1896 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1898 case LLCSAP_NETBEUI
:
1899 return gen_cmp(off_linktype
, BPF_H
, (long)
1900 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1904 * XXX - are there ever SNAP frames for IPX on
1905 * non-Ethernet 802.x networks?
1907 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1909 case ETHERTYPE_ATALK
:
1911 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1912 * SNAP packets with an organization code of
1913 * 0x080007 (Apple, for Appletalk) and a protocol
1914 * type of ETHERTYPE_ATALK (Appletalk).
1916 * XXX - check for an organization code of
1917 * encapsulated Ethernet as well?
1919 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1923 * XXX - we don't have to check for IPX 802.3
1924 * here, but should we check for the IPX Ethertype?
1926 if (proto
<= ETHERMTU
) {
1928 * This is an LLC SAP value, so check
1931 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1934 * This is an Ethernet type; we assume that it's
1935 * unlikely that it'll appear in the right place
1936 * at random, and therefore check only the
1937 * location that would hold the Ethernet type
1938 * in a SNAP frame with an organization code of
1939 * 0x000000 (encapsulated Ethernet).
1941 * XXX - if we were to check for the SNAP DSAP and
1942 * LSAP, as per XXX, and were also to check for an
1943 * organization code of 0x000000 (encapsulated
1944 * Ethernet), we'd do
1946 * return gen_snap(0x000000, proto,
1949 * here; for now, we don't, as per the above.
1950 * I don't know whether it's worth the extra CPU
1951 * time to do the right check or not.
1953 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1958 static struct block
*
1959 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1963 u_int src_off
, dst_off
;
1965 struct block
*b0
, *b1
;
1979 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1980 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1986 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1987 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1994 b0
= gen_linktype(proto
);
1995 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2001 static struct block
*
2002 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2003 struct in6_addr
*addr
;
2004 struct in6_addr
*mask
;
2006 u_int src_off
, dst_off
;
2008 struct block
*b0
, *b1
;
2023 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2024 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2030 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2031 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2038 /* this order is important */
2039 a
= (u_int32_t
*)addr
;
2040 m
= (u_int32_t
*)mask
;
2041 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2042 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2044 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2046 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2048 b0
= gen_linktype(proto
);
2054 static struct block
*
2055 gen_ehostop(eaddr
, dir
)
2056 register const u_char
*eaddr
;
2059 register struct block
*b0
, *b1
;
2063 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2066 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2069 b0
= gen_ehostop(eaddr
, Q_SRC
);
2070 b1
= gen_ehostop(eaddr
, Q_DST
);
2076 b0
= gen_ehostop(eaddr
, Q_SRC
);
2077 b1
= gen_ehostop(eaddr
, Q_DST
);
2086 * Like gen_ehostop, but for DLT_FDDI
2088 static struct block
*
2089 gen_fhostop(eaddr
, dir
)
2090 register const u_char
*eaddr
;
2093 struct block
*b0
, *b1
;
2098 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2100 return gen_bcmp(6 + 1, 6, eaddr
);
2105 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2107 return gen_bcmp(0 + 1, 6, eaddr
);
2111 b0
= gen_fhostop(eaddr
, Q_SRC
);
2112 b1
= gen_fhostop(eaddr
, Q_DST
);
2118 b0
= gen_fhostop(eaddr
, Q_SRC
);
2119 b1
= gen_fhostop(eaddr
, Q_DST
);
2128 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2130 static struct block
*
2131 gen_thostop(eaddr
, dir
)
2132 register const u_char
*eaddr
;
2135 register struct block
*b0
, *b1
;
2139 return gen_bcmp(8, 6, eaddr
);
2142 return gen_bcmp(2, 6, eaddr
);
2145 b0
= gen_thostop(eaddr
, Q_SRC
);
2146 b1
= gen_thostop(eaddr
, Q_DST
);
2152 b0
= gen_thostop(eaddr
, Q_SRC
);
2153 b1
= gen_thostop(eaddr
, Q_DST
);
2162 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2164 static struct block
*
2165 gen_wlanhostop(eaddr
, dir
)
2166 register const u_char
*eaddr
;
2169 register struct block
*b0
, *b1
, *b2
;
2170 register struct slist
*s
;
2177 * For control frames, there is no SA.
2179 * For management frames, SA is at an
2180 * offset of 10 from the beginning of
2183 * For data frames, SA is at an offset
2184 * of 10 from the beginning of the packet
2185 * if From DS is clear, at an offset of
2186 * 16 from the beginning of the packet
2187 * if From DS is set and To DS is clear,
2188 * and an offset of 24 from the beginning
2189 * of the packet if From DS is set and To DS
2194 * Generate the tests to be done for data frames
2197 * First, check for To DS set, i.e. check "link[1] & 0x01".
2199 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2201 b1
= new_block(JMP(BPF_JSET
));
2202 b1
->s
.k
= 0x01; /* To DS */
2206 * If To DS is set, the SA is at 24.
2208 b0
= gen_bcmp(24, 6, eaddr
);
2212 * Now, check for To DS not set, i.e. check
2213 * "!(link[1] & 0x01)".
2215 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2217 b2
= new_block(JMP(BPF_JSET
));
2218 b2
->s
.k
= 0x01; /* To DS */
2223 * If To DS is not set, the SA is at 16.
2225 b1
= gen_bcmp(16, 6, eaddr
);
2229 * Now OR together the last two checks. That gives
2230 * the complete set of checks for data frames with
2236 * Now check for From DS being set, and AND that with
2237 * the ORed-together checks.
2239 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2241 b1
= new_block(JMP(BPF_JSET
));
2242 b1
->s
.k
= 0x02; /* From DS */
2247 * Now check for data frames with From DS not set.
2249 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2251 b2
= new_block(JMP(BPF_JSET
));
2252 b2
->s
.k
= 0x02; /* From DS */
2257 * If From DS isn't set, the SA is at 10.
2259 b1
= gen_bcmp(10, 6, eaddr
);
2263 * Now OR together the checks for data frames with
2264 * From DS not set and for data frames with From DS
2265 * set; that gives the checks done for data frames.
2270 * Now check for a data frame.
2271 * I.e, check "link[0] & 0x08".
2273 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2275 b1
= new_block(JMP(BPF_JSET
));
2280 * AND that with the checks done for data frames.
2285 * If the high-order bit of the type value is 0, this
2286 * is a management frame.
2287 * I.e, check "!(link[0] & 0x08)".
2289 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2291 b2
= new_block(JMP(BPF_JSET
));
2297 * For management frames, the SA is at 10.
2299 b1
= gen_bcmp(10, 6, eaddr
);
2303 * OR that with the checks done for data frames.
2304 * That gives the checks done for management and
2310 * If the low-order bit of the type value is 1,
2311 * this is either a control frame or a frame
2312 * with a reserved type, and thus not a
2315 * I.e., check "!(link[0] & 0x04)".
2317 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2319 b1
= new_block(JMP(BPF_JSET
));
2325 * AND that with the checks for data and management
2335 * For control frames, there is no DA.
2337 * For management frames, DA is at an
2338 * offset of 4 from the beginning of
2341 * For data frames, DA is at an offset
2342 * of 4 from the beginning of the packet
2343 * if To DS is clear and at an offset of
2344 * 16 from the beginning of the packet
2349 * Generate the tests to be done for data frames.
2351 * First, check for To DS set, i.e. "link[1] & 0x01".
2353 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2355 b1
= new_block(JMP(BPF_JSET
));
2356 b1
->s
.k
= 0x01; /* To DS */
2360 * If To DS is set, the DA is at 16.
2362 b0
= gen_bcmp(16, 6, eaddr
);
2366 * Now, check for To DS not set, i.e. check
2367 * "!(link[1] & 0x01)".
2369 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2371 b2
= new_block(JMP(BPF_JSET
));
2372 b2
->s
.k
= 0x01; /* To DS */
2377 * If To DS is not set, the DA is at 4.
2379 b1
= gen_bcmp(4, 6, eaddr
);
2383 * Now OR together the last two checks. That gives
2384 * the complete set of checks for data frames.
2389 * Now check for a data frame.
2390 * I.e, check "link[0] & 0x08".
2392 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2394 b1
= new_block(JMP(BPF_JSET
));
2399 * AND that with the checks done for data frames.
2404 * If the high-order bit of the type value is 0, this
2405 * is a management frame.
2406 * I.e, check "!(link[0] & 0x08)".
2408 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2410 b2
= new_block(JMP(BPF_JSET
));
2416 * For management frames, the DA is at 4.
2418 b1
= gen_bcmp(4, 6, eaddr
);
2422 * OR that with the checks done for data frames.
2423 * That gives the checks done for management and
2429 * If the low-order bit of the type value is 1,
2430 * this is either a control frame or a frame
2431 * with a reserved type, and thus not a
2434 * I.e., check "!(link[0] & 0x04)".
2436 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2438 b1
= new_block(JMP(BPF_JSET
));
2444 * AND that with the checks for data and management
2451 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2452 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2458 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2459 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2468 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2469 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2470 * as the RFC states.)
2472 static struct block
*
2473 gen_ipfchostop(eaddr
, dir
)
2474 register const u_char
*eaddr
;
2477 register struct block
*b0
, *b1
;
2481 return gen_bcmp(10, 6, eaddr
);
2484 return gen_bcmp(2, 6, eaddr
);
2487 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2488 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2494 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2495 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2504 * This is quite tricky because there may be pad bytes in front of the
2505 * DECNET header, and then there are two possible data packet formats that
2506 * carry both src and dst addresses, plus 5 packet types in a format that
2507 * carries only the src node, plus 2 types that use a different format and
2508 * also carry just the src node.
2512 * Instead of doing those all right, we just look for data packets with
2513 * 0 or 1 bytes of padding. If you want to look at other packets, that
2514 * will require a lot more hacking.
2516 * To add support for filtering on DECNET "areas" (network numbers)
2517 * one would want to add a "mask" argument to this routine. That would
2518 * make the filter even more inefficient, although one could be clever
2519 * and not generate masking instructions if the mask is 0xFFFF.
2521 static struct block
*
2522 gen_dnhostop(addr
, dir
, base_off
)
2527 struct block
*b0
, *b1
, *b2
, *tmp
;
2528 u_int offset_lh
; /* offset if long header is received */
2529 u_int offset_sh
; /* offset if short header is received */
2534 offset_sh
= 1; /* follows flags */
2535 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2539 offset_sh
= 3; /* follows flags, dstnode */
2540 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2544 /* Inefficient because we do our Calvinball dance twice */
2545 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2546 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2552 /* Inefficient because we do our Calvinball dance twice */
2553 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2554 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2559 bpf_error("ISO host filtering not implemented");
2564 b0
= gen_linktype(ETHERTYPE_DN
);
2565 /* Check for pad = 1, long header case */
2566 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2567 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2568 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2569 BPF_H
, (bpf_int32
)ntohs(addr
));
2571 /* Check for pad = 0, long header case */
2572 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2573 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2576 /* Check for pad = 1, short header case */
2577 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2578 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2579 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2580 BPF_H
, (bpf_int32
)ntohs(addr
));
2583 /* Check for pad = 0, short header case */
2584 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2585 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2589 /* Combine with test for linktype */
2594 static struct block
*
2595 gen_host(addr
, mask
, proto
, dir
)
2601 struct block
*b0
, *b1
;
2606 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2607 if (off_linktype
!= (u_int
)-1) {
2608 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2610 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2616 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2617 off_nl
+ 12, off_nl
+ 16);
2620 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2621 off_nl
+ 14, off_nl
+ 24);
2624 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2625 off_nl
+ 14, off_nl
+ 24);
2628 bpf_error("'tcp' modifier applied to host");
2631 bpf_error("'sctp' modifier applied to host");
2634 bpf_error("'udp' modifier applied to host");
2637 bpf_error("'icmp' modifier applied to host");
2640 bpf_error("'igmp' modifier applied to host");
2643 bpf_error("'igrp' modifier applied to host");
2646 bpf_error("'pim' modifier applied to host");
2649 bpf_error("'vrrp' modifier applied to host");
2652 bpf_error("ATALK host filtering not implemented");
2655 bpf_error("AARP host filtering not implemented");
2658 return gen_dnhostop(addr
, dir
, off_nl
);
2661 bpf_error("SCA host filtering not implemented");
2664 bpf_error("LAT host filtering not implemented");
2667 bpf_error("MOPDL host filtering not implemented");
2670 bpf_error("MOPRC host filtering not implemented");
2674 bpf_error("'ip6' modifier applied to ip host");
2677 bpf_error("'icmp6' modifier applied to host");
2681 bpf_error("'ah' modifier applied to host");
2684 bpf_error("'esp' modifier applied to host");
2687 bpf_error("ISO host filtering not implemented");
2690 bpf_error("'esis' modifier applied to host");
2693 bpf_error("'isis' modifier applied to host");
2696 bpf_error("'clnp' modifier applied to host");
2699 bpf_error("'stp' modifier applied to host");
2702 bpf_error("IPX host filtering not implemented");
2705 bpf_error("'netbeui' modifier applied to host");
2714 static struct block
*
2715 gen_host6(addr
, mask
, proto
, dir
)
2716 struct in6_addr
*addr
;
2717 struct in6_addr
*mask
;
2724 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2727 bpf_error("'ip' modifier applied to ip6 host");
2730 bpf_error("'rarp' modifier applied to ip6 host");
2733 bpf_error("'arp' modifier applied to ip6 host");
2736 bpf_error("'sctp' modifier applied to host");
2739 bpf_error("'tcp' modifier applied to host");
2742 bpf_error("'udp' modifier applied to host");
2745 bpf_error("'icmp' modifier applied to host");
2748 bpf_error("'igmp' modifier applied to host");
2751 bpf_error("'igrp' modifier applied to host");
2754 bpf_error("'pim' modifier applied to host");
2757 bpf_error("'vrrp' modifier applied to host");
2760 bpf_error("ATALK host filtering not implemented");
2763 bpf_error("AARP host filtering not implemented");
2766 bpf_error("'decnet' modifier applied to ip6 host");
2769 bpf_error("SCA host filtering not implemented");
2772 bpf_error("LAT host filtering not implemented");
2775 bpf_error("MOPDL host filtering not implemented");
2778 bpf_error("MOPRC host filtering not implemented");
2781 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2782 off_nl
+ 8, off_nl
+ 24);
2785 bpf_error("'icmp6' modifier applied to host");
2788 bpf_error("'ah' modifier applied to host");
2791 bpf_error("'esp' modifier applied to host");
2794 bpf_error("ISO host filtering not implemented");
2797 bpf_error("'esis' modifier applied to host");
2800 bpf_error("'isis' modifier applied to host");
2803 bpf_error("'clnp' modifier applied to host");
2806 bpf_error("'stp' modifier applied to host");
2809 bpf_error("IPX host filtering not implemented");
2812 bpf_error("'netbeui' modifier applied to host");
2822 static struct block
*
2823 gen_gateway(eaddr
, alist
, proto
, dir
)
2824 const u_char
*eaddr
;
2825 bpf_u_int32
**alist
;
2829 struct block
*b0
, *b1
, *tmp
;
2832 bpf_error("direction applied to 'gateway'");
2839 if (linktype
== DLT_EN10MB
)
2840 b0
= gen_ehostop(eaddr
, Q_OR
);
2841 else if (linktype
== DLT_FDDI
)
2842 b0
= gen_fhostop(eaddr
, Q_OR
);
2843 else if (linktype
== DLT_IEEE802
)
2844 b0
= gen_thostop(eaddr
, Q_OR
);
2845 else if (linktype
== DLT_IEEE802_11
)
2846 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2847 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2849 * Check that the packet doesn't begin with an
2850 * LE Control marker. (We've already generated
2853 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2857 * Now check the MAC address.
2859 b0
= gen_ehostop(eaddr
, Q_OR
);
2861 } else if (linktype
== DLT_IP_OVER_FC
)
2862 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2865 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2867 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2869 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2877 bpf_error("illegal modifier of 'gateway'");
2883 gen_proto_abbrev(proto
)
2892 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2894 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2900 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2902 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2908 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2910 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2916 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2919 #ifndef IPPROTO_IGMP
2920 #define IPPROTO_IGMP 2
2924 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2927 #ifndef IPPROTO_IGRP
2928 #define IPPROTO_IGRP 9
2931 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2935 #define IPPROTO_PIM 103
2939 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2941 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2946 #ifndef IPPROTO_VRRP
2947 #define IPPROTO_VRRP 112
2951 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2955 b1
= gen_linktype(ETHERTYPE_IP
);
2959 b1
= gen_linktype(ETHERTYPE_ARP
);
2963 b1
= gen_linktype(ETHERTYPE_REVARP
);
2967 bpf_error("link layer applied in wrong context");
2970 b1
= gen_linktype(ETHERTYPE_ATALK
);
2974 b1
= gen_linktype(ETHERTYPE_AARP
);
2978 b1
= gen_linktype(ETHERTYPE_DN
);
2982 b1
= gen_linktype(ETHERTYPE_SCA
);
2986 b1
= gen_linktype(ETHERTYPE_LAT
);
2990 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2994 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2999 b1
= gen_linktype(ETHERTYPE_IPV6
);
3002 #ifndef IPPROTO_ICMPV6
3003 #define IPPROTO_ICMPV6 58
3006 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3011 #define IPPROTO_AH 51
3014 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3016 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3022 #define IPPROTO_ESP 50
3025 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3027 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3033 b1
= gen_linktype(LLCSAP_ISONS
);
3037 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3041 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3044 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3045 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3046 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3048 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3050 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3052 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3056 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3057 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3058 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3060 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3062 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3064 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3068 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3069 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3070 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3072 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3077 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3078 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3083 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3084 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3086 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3088 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3093 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3094 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3099 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3100 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3105 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3109 b1
= gen_linktype(LLCSAP_8021D
);
3113 b1
= gen_linktype(LLCSAP_IPX
);
3117 b1
= gen_linktype(LLCSAP_NETBEUI
);
3126 static struct block
*
3133 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3134 s
->s
.k
= off_nl
+ 6;
3135 b
= new_block(JMP(BPF_JSET
));
3143 static struct block
*
3144 gen_portatom(off
, v
)
3151 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3154 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3155 s
->next
->s
.k
= off_nl
+ off
;
3157 b
= new_block(JMP(BPF_JEQ
));
3165 static struct block
*
3166 gen_portatom6(off
, v
)
3170 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3175 gen_portop(port
, proto
, dir
)
3176 int port
, proto
, dir
;
3178 struct block
*b0
, *b1
, *tmp
;
3180 /* ip proto 'proto' */
3181 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3187 b1
= gen_portatom(0, (bpf_int32
)port
);
3191 b1
= gen_portatom(2, (bpf_int32
)port
);
3196 tmp
= gen_portatom(0, (bpf_int32
)port
);
3197 b1
= gen_portatom(2, (bpf_int32
)port
);
3202 tmp
= gen_portatom(0, (bpf_int32
)port
);
3203 b1
= gen_portatom(2, (bpf_int32
)port
);
3215 static struct block
*
3216 gen_port(port
, ip_proto
, dir
)
3221 struct block
*b0
, *b1
, *tmp
;
3226 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3227 * not LLC encapsulation with LLCSAP_IP.
3229 * For IEEE 802 networks - which includes 802.5 token ring
3230 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3231 * says that SNAP encapsulation is used, not LLC encapsulation
3234 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3235 * RFC 2225 say that SNAP encapsulation is used, not LLC
3236 * encapsulation with LLCSAP_IP.
3238 * So we always check for ETHERTYPE_IP.
3240 b0
= gen_linktype(ETHERTYPE_IP
);
3246 b1
= gen_portop(port
, ip_proto
, dir
);
3250 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3251 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3253 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3266 gen_portop6(port
, proto
, dir
)
3267 int port
, proto
, dir
;
3269 struct block
*b0
, *b1
, *tmp
;
3271 /* ip proto 'proto' */
3272 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3276 b1
= gen_portatom6(0, (bpf_int32
)port
);
3280 b1
= gen_portatom6(2, (bpf_int32
)port
);
3285 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3286 b1
= gen_portatom6(2, (bpf_int32
)port
);
3291 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3292 b1
= gen_portatom6(2, (bpf_int32
)port
);
3304 static struct block
*
3305 gen_port6(port
, ip_proto
, dir
)
3310 struct block
*b0
, *b1
, *tmp
;
3312 /* ether proto ip */
3313 b0
= gen_linktype(ETHERTYPE_IPV6
);
3319 b1
= gen_portop6(port
, ip_proto
, dir
);
3323 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3324 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3326 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3339 lookup_proto(name
, proto
)
3340 register const char *name
;
3350 v
= pcap_nametoproto(name
);
3351 if (v
== PROTO_UNDEF
)
3352 bpf_error("unknown ip proto '%s'", name
);
3356 /* XXX should look up h/w protocol type based on linktype */
3357 v
= pcap_nametoeproto(name
);
3358 if (v
== PROTO_UNDEF
)
3359 bpf_error("unknown ether proto '%s'", name
);
3363 if (strcmp(name
, "esis") == 0)
3365 else if (strcmp(name
, "isis") == 0)
3367 else if (strcmp(name
, "clnp") == 0)
3370 bpf_error("unknown osi proto '%s'", name
);
3390 static struct block
*
3391 gen_protochain(v
, proto
, dir
)
3396 #ifdef NO_PROTOCHAIN
3397 return gen_proto(v
, proto
, dir
);
3399 struct block
*b0
, *b
;
3400 struct slist
*s
[100];
3401 int fix2
, fix3
, fix4
, fix5
;
3402 int ahcheck
, again
, end
;
3404 int reg2
= alloc_reg();
3406 memset(s
, 0, sizeof(s
));
3407 fix2
= fix3
= fix4
= fix5
= 0;
3414 b0
= gen_protochain(v
, Q_IP
, dir
);
3415 b
= gen_protochain(v
, Q_IPV6
, dir
);
3419 bpf_error("bad protocol applied for 'protochain'");
3423 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3426 * s[0] is a dummy entry to protect other BPF insn from damaged
3427 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3428 * hard to find interdependency made by jump table fixup.
3431 s
[i
] = new_stmt(0); /*dummy*/
3436 b0
= gen_linktype(ETHERTYPE_IP
);
3439 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3440 s
[i
]->s
.k
= off_nl
+ 9;
3442 /* X = ip->ip_hl << 2 */
3443 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3449 b0
= gen_linktype(ETHERTYPE_IPV6
);
3451 /* A = ip6->ip_nxt */
3452 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3453 s
[i
]->s
.k
= off_nl
+ 6;
3455 /* X = sizeof(struct ip6_hdr) */
3456 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3462 bpf_error("unsupported proto to gen_protochain");
3466 /* again: if (A == v) goto end; else fall through; */
3468 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3470 s
[i
]->s
.jt
= NULL
; /*later*/
3471 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3475 #ifndef IPPROTO_NONE
3476 #define IPPROTO_NONE 59
3478 /* if (A == IPPROTO_NONE) goto end */
3479 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3480 s
[i
]->s
.jt
= NULL
; /*later*/
3481 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3482 s
[i
]->s
.k
= IPPROTO_NONE
;
3483 s
[fix5
]->s
.jf
= s
[i
];
3488 if (proto
== Q_IPV6
) {
3489 int v6start
, v6end
, v6advance
, j
;
3492 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3493 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3494 s
[i
]->s
.jt
= NULL
; /*later*/
3495 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3496 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3497 s
[fix2
]->s
.jf
= s
[i
];
3499 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3500 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3501 s
[i
]->s
.jt
= NULL
; /*later*/
3502 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3503 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3505 /* if (A == IPPROTO_ROUTING) goto v6advance */
3506 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3507 s
[i
]->s
.jt
= NULL
; /*later*/
3508 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3509 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3511 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3512 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3513 s
[i
]->s
.jt
= NULL
; /*later*/
3514 s
[i
]->s
.jf
= NULL
; /*later*/
3515 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3526 * X = X + (P[X + 1] + 1) * 8;
3529 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3531 /* A = P[X + packet head] */
3532 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3536 s
[i
] = new_stmt(BPF_ST
);
3540 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3543 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3547 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3549 /* A = P[X + packet head]; */
3550 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3554 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3558 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3562 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3565 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3569 /* goto again; (must use BPF_JA for backward jump) */
3570 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3571 s
[i
]->s
.k
= again
- i
- 1;
3572 s
[i
- 1]->s
.jf
= s
[i
];
3576 for (j
= v6start
; j
<= v6end
; j
++)
3577 s
[j
]->s
.jt
= s
[v6advance
];
3582 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3584 s
[fix2
]->s
.jf
= s
[i
];
3590 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3591 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3592 s
[i
]->s
.jt
= NULL
; /*later*/
3593 s
[i
]->s
.jf
= NULL
; /*later*/
3594 s
[i
]->s
.k
= IPPROTO_AH
;
3596 s
[fix3
]->s
.jf
= s
[ahcheck
];
3603 * X = X + (P[X + 1] + 2) * 4;
3606 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3608 /* A = P[X + packet head]; */
3609 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3613 s
[i
] = new_stmt(BPF_ST
);
3617 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3620 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3624 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3626 /* A = P[X + packet head] */
3627 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3631 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3635 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3639 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3642 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3646 /* goto again; (must use BPF_JA for backward jump) */
3647 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3648 s
[i
]->s
.k
= again
- i
- 1;
3653 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3655 s
[fix2
]->s
.jt
= s
[end
];
3656 s
[fix4
]->s
.jf
= s
[end
];
3657 s
[fix5
]->s
.jt
= s
[end
];
3664 for (i
= 0; i
< max
- 1; i
++)
3665 s
[i
]->next
= s
[i
+ 1];
3666 s
[max
- 1]->next
= NULL
;
3671 b
= new_block(JMP(BPF_JEQ
));
3672 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3682 static struct block
*
3683 gen_proto(v
, proto
, dir
)
3688 struct block
*b0
, *b1
;
3690 if (dir
!= Q_DEFAULT
)
3691 bpf_error("direction applied to 'proto'");
3696 b0
= gen_proto(v
, Q_IP
, dir
);
3697 b1
= gen_proto(v
, Q_IPV6
, dir
);
3705 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3706 * not LLC encapsulation with LLCSAP_IP.
3708 * For IEEE 802 networks - which includes 802.5 token ring
3709 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3710 * says that SNAP encapsulation is used, not LLC encapsulation
3713 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3714 * RFC 2225 say that SNAP encapsulation is used, not LLC
3715 * encapsulation with LLCSAP_IP.
3717 * So we always check for ETHERTYPE_IP.
3719 b0
= gen_linktype(ETHERTYPE_IP
);
3721 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3723 b1
= gen_protochain(v
, Q_IP
);
3733 * Frame Relay packets typically have an OSI
3734 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3735 * generates code to check for all the OSI
3736 * NLPIDs, so calling it and then adding a check
3737 * for the particular NLPID for which we're
3738 * looking is bogus, as we can just check for
3741 * What we check for is the NLPID and a frame
3742 * control field value of UI, i.e. 0x03 followed
3745 * XXX - assumes a 2-byte Frame Relay header with
3746 * DLCI and flags. What if the address is longer?
3748 * XXX - what about SNAP-encapsulated frames?
3750 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3756 * Cisco uses an Ethertype lookalike - for OSI,
3759 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3760 /* OSI in C-HDLC is stuffed with a fudge byte */
3761 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3766 b0
= gen_linktype(LLCSAP_ISONS
);
3767 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3773 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3775 * 4 is the offset of the PDU type relative to the IS-IS
3778 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3783 bpf_error("arp does not encapsulate another protocol");
3787 bpf_error("rarp does not encapsulate another protocol");
3791 bpf_error("atalk encapsulation is not specifiable");
3795 bpf_error("decnet encapsulation is not specifiable");
3799 bpf_error("sca does not encapsulate another protocol");
3803 bpf_error("lat does not encapsulate another protocol");
3807 bpf_error("moprc does not encapsulate another protocol");
3811 bpf_error("mopdl does not encapsulate another protocol");
3815 return gen_linktype(v
);
3818 bpf_error("'udp proto' is bogus");
3822 bpf_error("'tcp proto' is bogus");
3826 bpf_error("'sctp proto' is bogus");
3830 bpf_error("'icmp proto' is bogus");
3834 bpf_error("'igmp proto' is bogus");
3838 bpf_error("'igrp proto' is bogus");
3842 bpf_error("'pim proto' is bogus");
3846 bpf_error("'vrrp proto' is bogus");
3851 b0
= gen_linktype(ETHERTYPE_IPV6
);
3853 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3855 b1
= gen_protochain(v
, Q_IPV6
);
3861 bpf_error("'icmp6 proto' is bogus");
3865 bpf_error("'ah proto' is bogus");
3868 bpf_error("'ah proto' is bogus");
3871 bpf_error("'stp proto' is bogus");
3874 bpf_error("'ipx proto' is bogus");
3877 bpf_error("'netbeui proto' is bogus");
3888 register const char *name
;
3891 int proto
= q
.proto
;
3895 bpf_u_int32 mask
, addr
;
3897 bpf_u_int32
**alist
;
3900 struct sockaddr_in
*sin
;
3901 struct sockaddr_in6
*sin6
;
3902 struct addrinfo
*res
, *res0
;
3903 struct in6_addr mask128
;
3905 struct block
*b
, *tmp
;
3906 int port
, real_proto
;
3911 addr
= pcap_nametonetaddr(name
);
3913 bpf_error("unknown network '%s'", name
);
3914 /* Left justify network addr and calculate its network mask */
3916 while (addr
&& (addr
& 0xff000000) == 0) {
3920 return gen_host(addr
, mask
, proto
, dir
);
3924 if (proto
== Q_LINK
) {
3928 eaddr
= pcap_ether_hostton(name
);
3931 "unknown ether host '%s'", name
);
3932 b
= gen_ehostop(eaddr
, dir
);
3937 eaddr
= pcap_ether_hostton(name
);
3940 "unknown FDDI host '%s'", name
);
3941 b
= gen_fhostop(eaddr
, dir
);
3946 eaddr
= pcap_ether_hostton(name
);
3949 "unknown token ring host '%s'", name
);
3950 b
= gen_thostop(eaddr
, dir
);
3954 case DLT_IEEE802_11
:
3955 eaddr
= pcap_ether_hostton(name
);
3958 "unknown 802.11 host '%s'", name
);
3959 b
= gen_wlanhostop(eaddr
, dir
);
3963 case DLT_IP_OVER_FC
:
3964 eaddr
= pcap_ether_hostton(name
);
3967 "unknown Fibre Channel host '%s'", name
);
3968 b
= gen_ipfchostop(eaddr
, dir
);
3977 * Check that the packet doesn't begin
3978 * with an LE Control marker. (We've
3979 * already generated a test for LANE.)
3981 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3985 eaddr
= pcap_ether_hostton(name
);
3988 "unknown ether host '%s'", name
);
3989 b
= gen_ehostop(eaddr
, dir
);
3995 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3996 } else if (proto
== Q_DECNET
) {
3997 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3999 * I don't think DECNET hosts can be multihomed, so
4000 * there is no need to build up a list of addresses
4002 return (gen_host(dn_addr
, 0, proto
, dir
));
4005 alist
= pcap_nametoaddr(name
);
4006 if (alist
== NULL
|| *alist
== NULL
)
4007 bpf_error("unknown host '%s'", name
);
4009 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4011 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4013 tmp
= gen_host(**alist
++, 0xffffffff,
4020 memset(&mask128
, 0xff, sizeof(mask128
));
4021 res0
= res
= pcap_nametoaddrinfo(name
);
4023 bpf_error("unknown host '%s'", name
);
4025 tproto
= tproto6
= proto
;
4026 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4030 for (res
= res0
; res
; res
= res
->ai_next
) {
4031 switch (res
->ai_family
) {
4033 if (tproto
== Q_IPV6
)
4036 sin
= (struct sockaddr_in
*)
4038 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4039 0xffffffff, tproto
, dir
);
4042 if (tproto6
== Q_IP
)
4045 sin6
= (struct sockaddr_in6
*)
4047 tmp
= gen_host6(&sin6
->sin6_addr
,
4048 &mask128
, tproto6
, dir
);
4059 bpf_error("unknown host '%s'%s", name
,
4060 (proto
== Q_DEFAULT
)
4062 : " for specified address family");
4069 if (proto
!= Q_DEFAULT
&&
4070 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4071 bpf_error("illegal qualifier of 'port'");
4072 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4073 bpf_error("unknown port '%s'", name
);
4074 if (proto
== Q_UDP
) {
4075 if (real_proto
== IPPROTO_TCP
)
4076 bpf_error("port '%s' is tcp", name
);
4077 else if (real_proto
== IPPROTO_SCTP
)
4078 bpf_error("port '%s' is sctp", name
);
4080 /* override PROTO_UNDEF */
4081 real_proto
= IPPROTO_UDP
;
4083 if (proto
== Q_TCP
) {
4084 if (real_proto
== IPPROTO_UDP
)
4085 bpf_error("port '%s' is udp", name
);
4087 else if (real_proto
== IPPROTO_SCTP
)
4088 bpf_error("port '%s' is sctp", name
);
4090 /* override PROTO_UNDEF */
4091 real_proto
= IPPROTO_TCP
;
4093 if (proto
== Q_SCTP
) {
4094 if (real_proto
== IPPROTO_UDP
)
4095 bpf_error("port '%s' is udp", name
);
4097 else if (real_proto
== IPPROTO_TCP
)
4098 bpf_error("port '%s' is tcp", name
);
4100 /* override PROTO_UNDEF */
4101 real_proto
= IPPROTO_SCTP
;
4104 return gen_port(port
, real_proto
, dir
);
4108 b
= gen_port(port
, real_proto
, dir
);
4109 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4116 eaddr
= pcap_ether_hostton(name
);
4118 bpf_error("unknown ether host: %s", name
);
4120 alist
= pcap_nametoaddr(name
);
4121 if (alist
== NULL
|| *alist
== NULL
)
4122 bpf_error("unknown host '%s'", name
);
4123 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4127 bpf_error("'gateway' not supported in this configuration");
4131 real_proto
= lookup_proto(name
, proto
);
4132 if (real_proto
>= 0)
4133 return gen_proto(real_proto
, proto
, dir
);
4135 bpf_error("unknown protocol: %s", name
);
4138 real_proto
= lookup_proto(name
, proto
);
4139 if (real_proto
>= 0)
4140 return gen_protochain(real_proto
, proto
, dir
);
4142 bpf_error("unknown protocol: %s", name
);
4154 gen_mcode(s1
, s2
, masklen
, q
)
4155 register const char *s1
, *s2
;
4156 register int masklen
;
4159 register int nlen
, mlen
;
4162 nlen
= __pcap_atoin(s1
, &n
);
4163 /* Promote short ipaddr */
4167 mlen
= __pcap_atoin(s2
, &m
);
4168 /* Promote short ipaddr */
4171 bpf_error("non-network bits set in \"%s mask %s\"",
4174 /* Convert mask len to mask */
4176 bpf_error("mask length must be <= 32");
4177 m
= 0xffffffff << (32 - masklen
);
4179 bpf_error("non-network bits set in \"%s/%d\"",
4186 return gen_host(n
, m
, q
.proto
, q
.dir
);
4189 bpf_error("Mask syntax for networks only");
4197 register const char *s
;
4202 int proto
= q
.proto
;
4208 else if (q
.proto
== Q_DECNET
)
4209 vlen
= __pcap_atodn(s
, &v
);
4211 vlen
= __pcap_atoin(s
, &v
);
4218 if (proto
== Q_DECNET
)
4219 return gen_host(v
, 0, proto
, dir
);
4220 else if (proto
== Q_LINK
) {
4221 bpf_error("illegal link layer address");
4224 if (s
== NULL
&& q
.addr
== Q_NET
) {
4225 /* Promote short net number */
4226 while (v
&& (v
& 0xff000000) == 0) {
4231 /* Promote short ipaddr */
4235 return gen_host(v
, mask
, proto
, dir
);
4240 proto
= IPPROTO_UDP
;
4241 else if (proto
== Q_TCP
)
4242 proto
= IPPROTO_TCP
;
4243 else if (proto
== Q_SCTP
)
4244 proto
= IPPROTO_SCTP
;
4245 else if (proto
== Q_DEFAULT
)
4246 proto
= PROTO_UNDEF
;
4248 bpf_error("illegal qualifier of 'port'");
4251 return gen_port((int)v
, proto
, dir
);
4255 b
= gen_port((int)v
, proto
, dir
);
4256 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4262 bpf_error("'gateway' requires a name");
4266 return gen_proto((int)v
, proto
, dir
);
4269 return gen_protochain((int)v
, proto
, dir
);
4284 gen_mcode6(s1
, s2
, masklen
, q
)
4285 register const char *s1
, *s2
;
4286 register int masklen
;
4289 struct addrinfo
*res
;
4290 struct in6_addr
*addr
;
4291 struct in6_addr mask
;
4296 bpf_error("no mask %s supported", s2
);
4298 res
= pcap_nametoaddrinfo(s1
);
4300 bpf_error("invalid ip6 address %s", s1
);
4302 bpf_error("%s resolved to multiple address", s1
);
4303 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4305 if (sizeof(mask
) * 8 < masklen
)
4306 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4307 memset(&mask
, 0, sizeof(mask
));
4308 memset(&mask
, 0xff, masklen
/ 8);
4310 mask
.s6_addr
[masklen
/ 8] =
4311 (0xff << (8 - masklen
% 8)) & 0xff;
4314 a
= (u_int32_t
*)addr
;
4315 m
= (u_int32_t
*)&mask
;
4316 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4317 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4318 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4326 bpf_error("Mask syntax for networks only");
4330 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4335 bpf_error("invalid qualifier against IPv6 address");
4343 register const u_char
*eaddr
;
4346 struct block
*b
, *tmp
;
4348 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4349 if (linktype
== DLT_EN10MB
)
4350 return gen_ehostop(eaddr
, (int)q
.dir
);
4351 if (linktype
== DLT_FDDI
)
4352 return gen_fhostop(eaddr
, (int)q
.dir
);
4353 if (linktype
== DLT_IEEE802
)
4354 return gen_thostop(eaddr
, (int)q
.dir
);
4355 if (linktype
== DLT_IEEE802_11
)
4356 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4357 if (linktype
== DLT_SUNATM
&& is_lane
) {
4359 * Check that the packet doesn't begin with an
4360 * LE Control marker. (We've already generated
4363 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4367 * Now check the MAC address.
4369 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4373 if (linktype
== DLT_IP_OVER_FC
)
4374 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4375 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4377 bpf_error("ethernet address used in non-ether expression");
4383 struct slist
*s0
, *s1
;
4386 * This is definitely not the best way to do this, but the
4387 * lists will rarely get long.
4394 static struct slist
*
4400 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4405 static struct slist
*
4411 s
= new_stmt(BPF_LD
|BPF_MEM
);
4417 gen_load(proto
, index
, size
)
4422 struct slist
*s
, *tmp
;
4424 int regno
= alloc_reg();
4426 free_reg(index
->regno
);
4430 bpf_error("data size must be 1, 2, or 4");
4446 bpf_error("unsupported index operation");
4450 * XXX - what about ATM LANE? Should the index be
4451 * relative to the beginning of the AAL5 frame, so
4452 * that 0 refers to the beginning of the LE Control
4453 * field, or relative to the beginning of the LAN
4454 * frame, so that 0 refers, for Ethernet LANE, to
4455 * the beginning of the destination address?
4457 s
= xfer_to_x(index
);
4458 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4460 sappend(index
->s
, s
);
4475 /* XXX Note that we assume a fixed link header here. */
4476 s
= xfer_to_x(index
);
4477 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4480 sappend(index
->s
, s
);
4482 b
= gen_proto_abbrev(proto
);
4484 gen_and(index
->b
, b
);
4496 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4498 sappend(s
, xfer_to_a(index
));
4499 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4500 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4501 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4503 sappend(index
->s
, s
);
4505 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4507 gen_and(index
->b
, b
);
4509 gen_and(gen_proto_abbrev(Q_IP
), b
);
4515 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4519 index
->regno
= regno
;
4520 s
= new_stmt(BPF_ST
);
4522 sappend(index
->s
, s
);
4528 gen_relation(code
, a0
, a1
, reversed
)
4530 struct arth
*a0
, *a1
;
4533 struct slist
*s0
, *s1
, *s2
;
4534 struct block
*b
, *tmp
;
4538 if (code
== BPF_JEQ
) {
4539 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4540 b
= new_block(JMP(code
));
4544 b
= new_block(BPF_JMP
|code
|BPF_X
);
4550 sappend(a0
->s
, a1
->s
);
4554 free_reg(a0
->regno
);
4555 free_reg(a1
->regno
);
4557 /* 'and' together protocol checks */
4560 gen_and(a0
->b
, tmp
= a1
->b
);
4576 int regno
= alloc_reg();
4577 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4580 s
= new_stmt(BPF_LD
|BPF_LEN
);
4581 s
->next
= new_stmt(BPF_ST
);
4582 s
->next
->s
.k
= regno
;
4597 a
= (struct arth
*)newchunk(sizeof(*a
));
4601 s
= new_stmt(BPF_LD
|BPF_IMM
);
4603 s
->next
= new_stmt(BPF_ST
);
4619 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4622 s
= new_stmt(BPF_ST
);
4630 gen_arth(code
, a0
, a1
)
4632 struct arth
*a0
, *a1
;
4634 struct slist
*s0
, *s1
, *s2
;
4638 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4643 sappend(a0
->s
, a1
->s
);
4645 free_reg(a0
->regno
);
4646 free_reg(a1
->regno
);
4648 s0
= new_stmt(BPF_ST
);
4649 a0
->regno
= s0
->s
.k
= alloc_reg();
4656 * Here we handle simple allocation of the scratch registers.
4657 * If too many registers are alloc'd, the allocator punts.
4659 static int regused
[BPF_MEMWORDS
];
4663 * Return the next free register.
4668 int n
= BPF_MEMWORDS
;
4671 if (regused
[curreg
])
4672 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4674 regused
[curreg
] = 1;
4678 bpf_error("too many registers needed to evaluate expression");
4683 * Return a register to the table so it can
4693 static struct block
*
4700 s
= new_stmt(BPF_LD
|BPF_LEN
);
4701 b
= new_block(JMP(jmp
));
4712 return gen_len(BPF_JGE
, n
);
4716 * Actually, this is less than or equal.
4724 b
= gen_len(BPF_JGT
, n
);
4731 gen_byteop(op
, idx
, val
)
4742 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4745 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4746 b
->s
.code
= JMP(BPF_JGE
);
4751 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4752 b
->s
.code
= JMP(BPF_JGT
);
4756 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4760 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4764 b
= new_block(JMP(BPF_JEQ
));
4771 static u_char abroadcast
[] = { 0x0 };
4774 gen_broadcast(proto
)
4777 bpf_u_int32 hostmask
;
4778 struct block
*b0
, *b1
, *b2
;
4779 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4785 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4786 return gen_ahostop(abroadcast
, Q_DST
);
4787 if (linktype
== DLT_EN10MB
)
4788 return gen_ehostop(ebroadcast
, Q_DST
);
4789 if (linktype
== DLT_FDDI
)
4790 return gen_fhostop(ebroadcast
, Q_DST
);
4791 if (linktype
== DLT_IEEE802
)
4792 return gen_thostop(ebroadcast
, Q_DST
);
4793 if (linktype
== DLT_IEEE802_11
)
4794 return gen_wlanhostop(ebroadcast
, Q_DST
);
4795 if (linktype
== DLT_IP_OVER_FC
)
4796 return gen_ipfchostop(ebroadcast
, Q_DST
);
4797 if (linktype
== DLT_SUNATM
&& is_lane
) {
4799 * Check that the packet doesn't begin with an
4800 * LE Control marker. (We've already generated
4803 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4807 * Now check the MAC address.
4809 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4813 bpf_error("not a broadcast link");
4817 b0
= gen_linktype(ETHERTYPE_IP
);
4818 hostmask
= ~netmask
;
4819 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4820 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4821 (bpf_int32
)(~0 & hostmask
), hostmask
);
4826 bpf_error("only link-layer/IP broadcast filters supported");
4831 * Generate code to test the low-order bit of a MAC address (that's
4832 * the bottom bit of the *first* byte).
4834 static struct block
*
4835 gen_mac_multicast(offset
)
4838 register struct block
*b0
;
4839 register struct slist
*s
;
4841 /* link[offset] & 1 != 0 */
4842 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4844 b0
= new_block(JMP(BPF_JSET
));
4851 gen_multicast(proto
)
4854 register struct block
*b0
, *b1
, *b2
;
4855 register struct slist
*s
;
4861 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4862 /* all ARCnet multicasts use the same address */
4863 return gen_ahostop(abroadcast
, Q_DST
);
4865 if (linktype
== DLT_EN10MB
) {
4866 /* ether[0] & 1 != 0 */
4867 return gen_mac_multicast(0);
4870 if (linktype
== DLT_FDDI
) {
4872 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4874 * XXX - was that referring to bit-order issues?
4876 /* fddi[1] & 1 != 0 */
4877 return gen_mac_multicast(1);
4880 if (linktype
== DLT_IEEE802
) {
4881 /* tr[2] & 1 != 0 */
4882 return gen_mac_multicast(2);
4885 if (linktype
== DLT_IEEE802_11
) {
4889 * For control frames, there is no DA.
4891 * For management frames, DA is at an
4892 * offset of 4 from the beginning of
4895 * For data frames, DA is at an offset
4896 * of 4 from the beginning of the packet
4897 * if To DS is clear and at an offset of
4898 * 16 from the beginning of the packet
4903 * Generate the tests to be done for data frames.
4905 * First, check for To DS set, i.e. "link[1] & 0x01".
4907 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4909 b1
= new_block(JMP(BPF_JSET
));
4910 b1
->s
.k
= 0x01; /* To DS */
4914 * If To DS is set, the DA is at 16.
4916 b0
= gen_mac_multicast(16);
4920 * Now, check for To DS not set, i.e. check
4921 * "!(link[1] & 0x01)".
4923 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4925 b2
= new_block(JMP(BPF_JSET
));
4926 b2
->s
.k
= 0x01; /* To DS */
4931 * If To DS is not set, the DA is at 4.
4933 b1
= gen_mac_multicast(4);
4937 * Now OR together the last two checks. That gives
4938 * the complete set of checks for data frames.
4943 * Now check for a data frame.
4944 * I.e, check "link[0] & 0x08".
4946 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4948 b1
= new_block(JMP(BPF_JSET
));
4953 * AND that with the checks done for data frames.
4958 * If the high-order bit of the type value is 0, this
4959 * is a management frame.
4960 * I.e, check "!(link[0] & 0x08)".
4962 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4964 b2
= new_block(JMP(BPF_JSET
));
4970 * For management frames, the DA is at 4.
4972 b1
= gen_mac_multicast(4);
4976 * OR that with the checks done for data frames.
4977 * That gives the checks done for management and
4983 * If the low-order bit of the type value is 1,
4984 * this is either a control frame or a frame
4985 * with a reserved type, and thus not a
4988 * I.e., check "!(link[0] & 0x04)".
4990 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4992 b1
= new_block(JMP(BPF_JSET
));
4998 * AND that with the checks for data and management
5005 if (linktype
== DLT_IP_OVER_FC
) {
5006 b0
= gen_mac_multicast(2);
5010 if (linktype
== DLT_SUNATM
&& is_lane
) {
5012 * Check that the packet doesn't begin with an
5013 * LE Control marker. (We've already generated
5016 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5019 /* ether[off_mac] & 1 != 0 */
5020 b0
= gen_mac_multicast(off_mac
);
5025 /* Link not known to support multicasts */
5029 b0
= gen_linktype(ETHERTYPE_IP
);
5030 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5031 b1
->s
.code
= JMP(BPF_JGE
);
5037 b0
= gen_linktype(ETHERTYPE_IPV6
);
5038 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5043 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5048 * generate command for inbound/outbound. It's here so we can
5049 * make it link-type specific. 'dir' = 0 implies "inbound",
5050 * = 1 implies "outbound".
5056 register struct block
*b0
;
5059 * Only some data link types support inbound/outbound qualifiers.
5063 b0
= gen_relation(BPF_JEQ
,
5064 gen_load(Q_LINK
, gen_loadi(0), 1),
5072 * Match packets sent by this machine.
5074 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5077 * Match packets sent to this machine.
5078 * (No broadcast or multicast packets, or
5079 * packets sent to some other machine and
5080 * received promiscuously.)
5082 * XXX - packets sent to other machines probably
5083 * shouldn't be matched, but what about broadcast
5084 * or multicast packets we received?
5086 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5091 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5092 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5095 case DLT_PPP_WITHDIRECTION
:
5097 /* match outgoing packets */
5098 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_OUT
);
5100 /* match incoming packets */
5101 b0
= gen_cmp(0, BPF_B
, PPP_WITHDIRECTION_IN
);
5105 case DLT_JUNIPER_ATM1
:
5106 case DLT_JUNIPER_ATM2
:
5107 /* juniper flags (including direction) are stored
5108 * the byte after the 3-byte magic number */
5110 /* match outgoing packets */
5111 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5113 /* match incoming packets */
5114 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5119 bpf_error("inbound/outbound not supported on linktype %d",
5127 /* PF firewall log matched interface */
5129 gen_pf_ifname(const char *ifname
)
5134 if (linktype
== DLT_PFLOG
) {
5135 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5136 off
= offsetof(struct pfloghdr
, ifname
);
5138 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5141 if (strlen(ifname
) >= len
) {
5142 bpf_error("ifname interface names can only be %d characters",
5146 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5150 /* PF firewall log matched interface */
5152 gen_pf_ruleset(char *ruleset
)
5156 if (linktype
!= DLT_PFLOG
) {
5157 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5160 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5161 bpf_error("ruleset names can only be %ld characters",
5162 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5165 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5166 strlen(ruleset
), (const u_char
*)ruleset
);
5170 /* PF firewall log rule number */
5176 if (linktype
== DLT_PFLOG
) {
5177 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5180 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5187 /* PF firewall log sub-rule number */
5189 gen_pf_srnr(int srnr
)
5193 if (linktype
!= DLT_PFLOG
) {
5194 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5198 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5203 /* PF firewall log reason code */
5205 gen_pf_reason(int reason
)
5209 if (linktype
== DLT_PFLOG
) {
5210 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5213 bpf_error("reason not supported on linktype 0x%x", linktype
);
5220 /* PF firewall log action */
5222 gen_pf_action(int action
)
5226 if (linktype
== DLT_PFLOG
) {
5227 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5230 bpf_error("action not supported on linktype 0x%x", linktype
);
5239 register const u_char
*eaddr
;
5242 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5243 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5244 return gen_ahostop(eaddr
, (int)q
.dir
);
5246 bpf_error("ARCnet address used in non-arc expression");
5250 static struct block
*
5251 gen_ahostop(eaddr
, dir
)
5252 register const u_char
*eaddr
;
5255 register struct block
*b0
, *b1
;
5258 /* src comes first, different from Ethernet */
5260 return gen_bcmp(0, 1, eaddr
);
5263 return gen_bcmp(1, 1, eaddr
);
5266 b0
= gen_ahostop(eaddr
, Q_SRC
);
5267 b1
= gen_ahostop(eaddr
, Q_DST
);
5273 b0
= gen_ahostop(eaddr
, Q_SRC
);
5274 b1
= gen_ahostop(eaddr
, Q_DST
);
5283 * support IEEE 802.1Q VLAN trunk over ethernet
5292 * Change the offsets to point to the type and data fields within
5293 * the VLAN packet. This is somewhat of a kludge.
5295 if (orig_nl
== (u_int
)-1) {
5296 orig_linktype
= off_linktype
; /* save original values */
5298 orig_nl_nosnap
= off_nl_nosnap
;
5309 bpf_error("no VLAN support for data link type %d",
5315 /* check for VLAN */
5316 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5318 /* If a specific VLAN is requested, check VLAN id */
5319 if (vlan_num
>= 0) {
5322 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5340 * Change the offsets to point to the type and data fields within
5341 * the MPLS packet. This is somewhat of a kludge.
5343 if (orig_nl
== (u_int
)-1) {
5344 orig_linktype
= off_linktype
; /* save original values */
5346 orig_nl_nosnap
= off_nl_nosnap
;
5355 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5363 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5371 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5374 /* FIXME add other DLT_s ...
5375 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5376 * leave it for now */
5379 bpf_error("no MPLS support for data link type %d",
5384 bpf_error("'mpls' can't be combined with 'vlan' or another 'mpls'");
5388 /* If a specific MPLS label is requested, check it */
5389 if (label_num
>= 0) {
5392 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5393 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5402 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5414 bpf_error("'vpi' supported only on raw ATM");
5415 if (off_vpi
== (u_int
)-1)
5417 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5418 (u_int
)jvalue
, reverse
);
5423 bpf_error("'vci' supported only on raw ATM");
5424 if (off_vci
== (u_int
)-1)
5426 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5427 (u_int
)jvalue
, reverse
);
5431 if (off_proto
== (u_int
)-1)
5432 abort(); /* XXX - this isn't on FreeBSD */
5433 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5434 (u_int
)jvalue
, reverse
);
5438 if (off_payload
== (u_int
)-1)
5440 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5441 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5446 bpf_error("'callref' supported only on raw ATM");
5447 if (off_proto
== (u_int
)-1)
5449 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5450 (u_int
)jvalue
, reverse
);
5460 gen_atmtype_abbrev(type
)
5463 struct block
*b0
, *b1
;
5468 /* Get all packets in Meta signalling Circuit */
5470 bpf_error("'metac' supported only on raw ATM");
5471 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5472 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5477 /* Get all packets in Broadcast Circuit*/
5479 bpf_error("'bcc' supported only on raw ATM");
5480 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5481 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5486 /* Get all cells in Segment OAM F4 circuit*/
5488 bpf_error("'oam4sc' supported only on raw ATM");
5489 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5490 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5495 /* Get all cells in End-to-End OAM F4 Circuit*/
5497 bpf_error("'oam4ec' supported only on raw ATM");
5498 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5499 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5504 /* Get all packets in connection Signalling Circuit */
5506 bpf_error("'sc' supported only on raw ATM");
5507 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5508 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5513 /* Get all packets in ILMI Circuit */
5515 bpf_error("'ilmic' supported only on raw ATM");
5516 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5517 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5522 /* Get all LANE packets */
5524 bpf_error("'lane' supported only on raw ATM");
5525 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5528 * Arrange that all subsequent tests assume LANE
5529 * rather than LLC-encapsulated packets, and set
5530 * the offsets appropriately for LANE-encapsulated
5533 * "off_mac" is the offset of the Ethernet header,
5534 * which is 2 bytes past the ATM pseudo-header
5535 * (skipping the pseudo-header and 2-byte LE Client
5536 * field). The other offsets are Ethernet offsets
5537 * relative to "off_mac".
5540 off_mac
= off_payload
+ 2; /* MAC header */
5541 off_linktype
= off_mac
+ 12;
5542 off_nl
= off_mac
+ 14; /* Ethernet II */
5543 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5547 /* Get all LLC-encapsulated packets */
5549 bpf_error("'llc' supported only on raw ATM");
5550 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5561 static struct block
*
5562 gen_msg_abbrev(type
)
5568 * Q.2931 signalling protocol messages for handling virtual circuits
5569 * establishment and teardown
5574 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5578 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5582 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5586 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5590 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5593 case A_RELEASE_DONE
:
5594 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5604 gen_atmmulti_abbrev(type
)
5607 struct block
*b0
, *b1
;
5613 bpf_error("'oam' supported only on raw ATM");
5614 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5619 bpf_error("'oamf4' supported only on raw ATM");
5621 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5622 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5624 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5630 * Get Q.2931 signalling messages for switched
5631 * virtual connection
5634 bpf_error("'connectmsg' supported only on raw ATM");
5635 b0
= gen_msg_abbrev(A_SETUP
);
5636 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5638 b0
= gen_msg_abbrev(A_CONNECT
);
5640 b0
= gen_msg_abbrev(A_CONNECTACK
);
5642 b0
= gen_msg_abbrev(A_RELEASE
);
5644 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5646 b0
= gen_atmtype_abbrev(A_SC
);
5652 bpf_error("'metaconnect' supported only on raw ATM");
5653 b0
= gen_msg_abbrev(A_SETUP
);
5654 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5656 b0
= gen_msg_abbrev(A_CONNECT
);
5658 b0
= gen_msg_abbrev(A_RELEASE
);
5660 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5662 b0
= gen_atmtype_abbrev(A_METAC
);