]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
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
[] =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.177 2002-08-08 09:09:58 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #include <netdb.h> /* for "struct addrinfo" */
78 #include <pcap-namedb.h>
83 #define IPPROTO_SCTP 132
86 #ifdef HAVE_OS_PROTO_H
90 #define JMP(c) ((c)|BPF_JMP|BPF_K)
93 static jmp_buf top_ctx
;
94 static pcap_t
*bpf_pcap
;
96 /* Hack for updating VLAN offsets. */
97 static u_int orig_linktype
= -1, orig_nl
= -1, orig_nl_nosnap
= -1;
101 int pcap_fddipad
= PCAP_FDDIPAD
;
108 bpf_error(const char *fmt
, ...)
114 if (bpf_pcap
!= NULL
)
115 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
122 static void init_linktype(int);
124 static int alloc_reg(void);
125 static void free_reg(int);
127 static struct block
*root
;
130 * We divy out chunks of memory rather than call malloc each time so
131 * we don't have to worry about leaking memory. It's probably
132 * not a big deal if all this memory was wasted but it this ever
133 * goes into a library that would probably not be a good idea.
136 #define CHUNK0SIZE 1024
142 static struct chunk chunks
[NCHUNKS
];
143 static int cur_chunk
;
145 static void *newchunk(u_int
);
146 static void freechunks(void);
147 static inline struct block
*new_block(int);
148 static inline struct slist
*new_stmt(int);
149 static struct block
*gen_retblk(int);
150 static inline void syntax(void);
152 static void backpatch(struct block
*, struct block
*);
153 static void merge(struct block
*, struct block
*);
154 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
155 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
156 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
157 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
158 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
159 bpf_u_int32
, bpf_u_int32
, int);
160 static struct block
*gen_uncond(int);
161 static inline struct block
*gen_true(void);
162 static inline struct block
*gen_false(void);
163 static struct block
*gen_linktype(int);
164 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
165 static struct block
*gen_llc(int);
166 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
168 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
170 static struct block
*gen_ahostop(const u_char
*, int);
171 static struct block
*gen_ehostop(const u_char
*, int);
172 static struct block
*gen_fhostop(const u_char
*, int);
173 static struct block
*gen_thostop(const u_char
*, int);
174 static struct block
*gen_wlanhostop(const u_char
*, int);
175 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
176 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
178 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
181 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
183 static struct block
*gen_ipfrag(void);
184 static struct block
*gen_portatom(int, bpf_int32
);
186 static struct block
*gen_portatom6(int, bpf_int32
);
188 struct block
*gen_portop(int, int, int);
189 static struct block
*gen_port(int, int, int);
191 struct block
*gen_portop6(int, int, int);
192 static struct block
*gen_port6(int, int, int);
194 static int lookup_proto(const char *, int);
195 static struct block
*gen_protochain(int, int, int);
196 static struct block
*gen_proto(int, int, int);
197 static struct slist
*xfer_to_x(struct arth
*);
198 static struct slist
*xfer_to_a(struct arth
*);
199 static struct block
*gen_mac_multicast(int);
200 static struct block
*gen_len(int, int);
202 static struct block
*gen_msg_abbrev(int type
);
212 /* XXX Round up to nearest long. */
213 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
215 /* XXX Round up to structure boundary. */
219 cp
= &chunks
[cur_chunk
];
220 if (n
> cp
->n_left
) {
221 ++cp
, k
= ++cur_chunk
;
223 bpf_error("out of memory");
224 size
= CHUNK0SIZE
<< k
;
225 cp
->m
= (void *)malloc(size
);
226 memset((char *)cp
->m
, 0, size
);
229 bpf_error("out of memory");
232 return (void *)((char *)cp
->m
+ cp
->n_left
);
241 for (i
= 0; i
< NCHUNKS
; ++i
)
242 if (chunks
[i
].m
!= NULL
) {
249 * A strdup whose allocations are freed after code generation is over.
253 register const char *s
;
255 int n
= strlen(s
) + 1;
256 char *cp
= newchunk(n
);
262 static inline struct block
*
268 p
= (struct block
*)newchunk(sizeof(*p
));
275 static inline struct slist
*
281 p
= (struct slist
*)newchunk(sizeof(*p
));
287 static struct block
*
291 struct block
*b
= new_block(BPF_RET
|BPF_K
);
300 bpf_error("syntax error in filter expression");
303 static bpf_u_int32 netmask
;
308 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
309 char *buf
, int optimize
, bpf_u_int32 mask
)
318 if (setjmp(top_ctx
)) {
326 snaplen
= pcap_snapshot(p
);
328 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
329 "snaplen of 0 rejects all packets");
333 lex_init(buf
? buf
: "");
334 init_linktype(pcap_datalink(p
));
341 root
= gen_retblk(snaplen
);
343 if (optimize
&& !no_optimize
) {
346 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
347 bpf_error("expression rejects all packets");
349 program
->bf_insns
= icode_to_fcode(root
, &len
);
350 program
->bf_len
= len
;
358 * entry point for using the compiler with no pcap open
359 * pass in all the stuff that is needed explicitly instead.
362 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
363 struct bpf_program
*program
,
364 char *buf
, int optimize
, bpf_u_int32 mask
)
369 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
372 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
378 * Clean up a "struct bpf_program" by freeing all the memory allocated
382 pcap_freecode(struct bpf_program
*program
)
385 if (program
->bf_insns
!= NULL
) {
386 free((char *)program
->bf_insns
);
387 program
->bf_insns
= NULL
;
392 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
393 * which of the jt and jf fields has been resolved and which is a pointer
394 * back to another unresolved block (or nil). At least one of the fields
395 * in each block is already resolved.
398 backpatch(list
, target
)
399 struct block
*list
, *target
;
416 * Merge the lists in b0 and b1, using the 'sense' field to indicate
417 * which of jt and jf is the link.
421 struct block
*b0
, *b1
;
423 register struct block
**p
= &b0
;
425 /* Find end of list. */
427 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
429 /* Concatenate the lists. */
437 backpatch(p
, gen_retblk(snaplen
));
438 p
->sense
= !p
->sense
;
439 backpatch(p
, gen_retblk(0));
445 struct block
*b0
, *b1
;
447 backpatch(b0
, b1
->head
);
448 b0
->sense
= !b0
->sense
;
449 b1
->sense
= !b1
->sense
;
451 b1
->sense
= !b1
->sense
;
457 struct block
*b0
, *b1
;
459 b0
->sense
= !b0
->sense
;
460 backpatch(b0
, b1
->head
);
461 b0
->sense
= !b0
->sense
;
470 b
->sense
= !b
->sense
;
473 static struct block
*
474 gen_cmp(offset
, size
, v
)
481 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
484 b
= new_block(JMP(BPF_JEQ
));
491 static struct block
*
492 gen_cmp_gt(offset
, size
, v
)
499 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
502 b
= new_block(JMP(BPF_JGT
));
509 static struct block
*
510 gen_mcmp(offset
, size
, v
, mask
)
515 struct block
*b
= gen_cmp(offset
, size
, v
);
518 if (mask
!= 0xffffffff) {
519 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
526 static struct block
*
527 gen_bcmp(offset
, size
, v
)
528 register u_int offset
, size
;
529 register const u_char
*v
;
531 register struct block
*b
, *tmp
;
535 register const u_char
*p
= &v
[size
- 4];
536 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
537 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
539 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
546 register const u_char
*p
= &v
[size
- 2];
547 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
549 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
556 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
564 static struct block
*
565 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
566 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
572 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
575 if (mask
!= 0xffffffff) {
576 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
580 b
= new_block(JMP(jtype
));
583 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
589 * Various code constructs need to know the layout of the data link
590 * layer. These variables give the necessary offsets.
594 * "off_linktype" is the offset to information in the link-layer header
595 * giving the packet type.
597 * For Ethernet, it's the offset of the Ethernet type field.
599 * For link-layer types that always use 802.2 headers, it's the
600 * offset of the LLC header.
602 * For PPP, it's the offset of the PPP type field.
604 * For Cisco HDLC, it's the offset of the CHDLC type field.
606 * For BSD loopback, it's the offset of the AF_ value.
608 * For Linux cooked sockets, it's the offset of the type field.
610 * It's set to -1 for no encapsulation, in which case, IP is assumed.
612 static u_int off_linktype
;
615 * TRUE if the link layer includes an ATM pseudo-header.
617 static int is_atm
= 0;
620 * These are offsets for the ATM pseudo-header.
622 static u_int off_vpi
;
623 static u_int off_vci
;
624 static u_int off_proto
;
627 * This is the offset to the message type for Q.2931 messages.
629 static u_int off_msg_type
;
632 * These are offsets to the beginning of the network-layer header.
634 * If the link layer never uses 802.2 LLC:
636 * "off_nl" and "off_nl_nosnap" are the same.
638 * If the link layer always uses 802.2 LLC:
640 * "off_nl" is the offset if there's a SNAP header following
643 * "off_nl_nosnap" is the offset if there's no SNAP header.
645 * If the link layer is Ethernet:
647 * "off_nl" is the offset if the packet is an Ethernet II packet
648 * (we assume no 802.3+802.2+SNAP);
650 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
651 * with an 802.2 header following it.
654 static u_int off_nl_nosnap
;
665 * Assume it's not raw ATM with a pseudo-header, for now.
681 off_nl
= 6; /* XXX in reality, variable! */
682 off_nl_nosnap
= 6; /* no 802.2 LLC */
687 off_nl
= 14; /* Ethernet II */
688 off_nl_nosnap
= 17; /* 802.3+802.2 */
693 * SLIP doesn't have a link level type. The 16 byte
694 * header is hacked into our SLIP driver.
698 off_nl_nosnap
= 16; /* no 802.2 LLC */
702 /* XXX this may be the same as the DLT_PPP_BSDOS case */
706 off_nl_nosnap
= 24; /* no 802.2 LLC */
713 off_nl_nosnap
= 4; /* no 802.2 LLC */
717 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
718 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
721 off_nl_nosnap
= 4; /* no 802.2 LLC */
726 * This does no include the Ethernet header, and
727 * only covers session state.
731 off_nl_nosnap
= 8; /* no 802.2 LLC */
737 off_nl_nosnap
= 24; /* no 802.2 LLC */
742 * FDDI doesn't really have a link-level type field.
743 * We set "off_linktype" to the offset of the LLC header.
745 * To check for Ethernet types, we assume that SSAP = SNAP
746 * is being used and pick out the encapsulated Ethernet type.
747 * XXX - should we generate code to check for SNAP?
751 off_linktype
+= pcap_fddipad
;
753 off_nl
= 21; /* FDDI+802.2+SNAP */
754 off_nl_nosnap
= 16; /* FDDI+802.2 */
756 off_nl
+= pcap_fddipad
;
757 off_nl_nosnap
+= pcap_fddipad
;
763 * Token Ring doesn't really have a link-level type field.
764 * We set "off_linktype" to the offset of the LLC header.
766 * To check for Ethernet types, we assume that SSAP = SNAP
767 * is being used and pick out the encapsulated Ethernet type.
768 * XXX - should we generate code to check for SNAP?
770 * XXX - the header is actually variable-length.
771 * Some various Linux patched versions gave 38
772 * as "off_linktype" and 40 as "off_nl"; however,
773 * if a token ring packet has *no* routing
774 * information, i.e. is not source-routed, the correct
775 * values are 20 and 22, as they are in the vanilla code.
777 * A packet is source-routed iff the uppermost bit
778 * of the first byte of the source address, at an
779 * offset of 8, has the uppermost bit set. If the
780 * packet is source-routed, the total number of bytes
781 * of routing information is 2 plus bits 0x1F00 of
782 * the 16-bit value at an offset of 14 (shifted right
783 * 8 - figure out which byte that is).
786 off_nl
= 22; /* Token Ring+802.2+SNAP */
787 off_nl_nosnap
= 17; /* Token Ring+802.2 */
792 * 802.11 doesn't really have a link-level type field.
793 * We set "off_linktype" to the offset of the LLC header.
795 * To check for Ethernet types, we assume that SSAP = SNAP
796 * is being used and pick out the encapsulated Ethernet type.
797 * XXX - should we generate code to check for SNAP?
799 * XXX - the header is actually variable-length. We
800 * assume a 24-byte link-layer header, as appears in
801 * data frames in networks with no bridges.
804 off_nl
= 32; /* 802.11+802.2+SNAP */
805 off_nl_nosnap
= 27; /* 802.11+802.2 */
808 case DLT_PRISM_HEADER
:
810 * Same as 802.11, but with an additional header before
811 * the 802.11 header, containing a bunch of additional
812 * information including radio-level information.
814 * The header is 144 bytes long.
816 * XXX - same variable-length header problem; at least
817 * the Prism header is fixed-length.
819 off_linktype
= 144+24;
820 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
821 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
824 case DLT_ATM_RFC1483
:
825 case DLT_ATM_CLIP
: /* Linux ATM defines this */
827 * assume routed, non-ISO PDUs
828 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
831 off_nl
= 8; /* 802.2+SNAP */
832 off_nl_nosnap
= 3; /* 802.2 */
837 * Full Frontal ATM; you get AALn PDUs with an ATM
841 off_linktype
= SUNATM_PKT_BEGIN_POS
;
842 off_vpi
= SUNATM_VPI_POS
;
843 off_vci
= SUNATM_VCI_POS
;
844 off_proto
= PROTO_POS
;
845 off_msg_type
= SUNATM_PKT_BEGIN_POS
+MSG_TYPE_POS
;
846 off_nl
= SUNATM_PKT_BEGIN_POS
+8; /* 802.2+SNAP */
847 off_nl_nosnap
= SUNATM_PKT_BEGIN_POS
+3; /* 802.2 */
853 off_nl_nosnap
= 0; /* no 802.2 LLC */
856 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
859 off_nl_nosnap
= 16; /* no 802.2 LLC */
864 * LocalTalk does have a 1-byte type field in the LLAP header,
865 * but really it just indicates whether there is a "short" or
866 * "long" DDP packet following.
870 off_nl_nosnap
= 0; /* no 802.2 LLC */
875 * XXX - we should set this to handle SNAP-encapsulated
876 * frames (NLPID of 0x80).
880 off_nl_nosnap
= 0; /* no 802.2 LLC */
883 bpf_error("unknown data link type %d", linktype
);
887 static struct block
*
894 s
= new_stmt(BPF_LD
|BPF_IMM
);
896 b
= new_block(JMP(BPF_JEQ
));
902 static inline struct block
*
905 return gen_uncond(1);
908 static inline struct block
*
911 return gen_uncond(0);
915 * Byte-swap a 32-bit number.
916 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
917 * big-endian platforms.)
919 #define SWAPLONG(y) \
920 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
922 static struct block
*
926 struct block
*b0
, *b1
, *b2
;
935 * OSI protocols always use 802.2 encapsulation.
936 * XXX - should we check both the DSAP and the
937 * SSAP, like this, or should we check just the
940 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
942 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
943 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
949 * NetBEUI always uses 802.2 encapsulation.
950 * XXX - should we check both the DSAP and the
951 * SSAP, like this, or should we check just the
954 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
956 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
957 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
965 * Ethernet_II frames, which are Ethernet
966 * frames with a frame type of ETHERTYPE_IPX;
968 * Ethernet_802.3 frames, which are 802.3
969 * frames (i.e., the type/length field is
970 * a length field, <= ETHERMTU, rather than
971 * a type field) with the first two bytes
972 * after the Ethernet/802.3 header being
975 * Ethernet_802.2 frames, which are 802.3
976 * frames with an 802.2 LLC header and
977 * with the IPX LSAP as the DSAP in the LLC
980 * Ethernet_SNAP frames, which are 802.3
981 * frames with an LLC header and a SNAP
982 * header and with an OUI of 0x000000
983 * (encapsulated Ethernet) and a protocol
984 * ID of ETHERTYPE_IPX in the SNAP header.
986 * XXX - should we generate the same code both
987 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
991 * This generates code to check both for the
992 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
994 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
995 (bpf_int32
)LLCSAP_IPX
);
996 b1
= gen_cmp(off_linktype
+ 2, BPF_H
,
1001 * Now we add code to check for SNAP frames with
1002 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1004 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1008 * Now we generate code to check for 802.3
1009 * frames in general.
1011 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1015 * Now add the check for 802.3 frames before the
1016 * check for Ethernet_802.2 and Ethernet_802.3,
1017 * as those checks should only be done on 802.3
1018 * frames, not on Ethernet frames.
1023 * Now add the check for Ethernet_II frames, and
1024 * do that before checking for the other frame
1027 b0
= gen_cmp(off_linktype
, BPF_H
,
1028 (bpf_int32
)ETHERTYPE_IPX
);
1032 case ETHERTYPE_ATALK
:
1033 case ETHERTYPE_AARP
:
1035 * EtherTalk (AppleTalk protocols on Ethernet link
1036 * layer) may use 802.2 encapsulation.
1040 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1041 * we check for an Ethernet type field less than
1042 * 1500, which means it's an 802.3 length field.
1044 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1048 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1049 * SNAP packets with an organization code of
1050 * 0x080007 (Apple, for Appletalk) and a protocol
1051 * type of ETHERTYPE_ATALK (Appletalk).
1053 * 802.2-encapsulated ETHERTYPE_AARP packets are
1054 * SNAP packets with an organization code of
1055 * 0x000000 (encapsulated Ethernet) and a protocol
1056 * type of ETHERTYPE_AARP (Appletalk ARP).
1058 if (proto
== ETHERTYPE_ATALK
)
1059 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1060 else /* proto == ETHERTYPE_AARP */
1061 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1065 * Check for Ethernet encapsulation (Ethertalk
1066 * phase 1?); we just check for the Ethernet
1069 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1075 if (proto
<= ETHERMTU
) {
1077 * This is an LLC SAP value, so the frames
1078 * that match would be 802.2 frames.
1079 * Check that the frame is an 802.2 frame
1080 * (i.e., that the length/type field is
1081 * a length field, <= ETHERMTU) and
1082 * then check the DSAP.
1084 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1086 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1092 * This is an Ethernet type, so compare
1093 * the length/type field with it (if
1094 * the frame is an 802.2 frame, the length
1095 * field will be <= ETHERMTU, and, as
1096 * "proto" is > ETHERMTU, this test
1097 * will fail and the frame won't match,
1098 * which is what we want).
1100 return gen_cmp(off_linktype
, BPF_H
,
1106 case DLT_IEEE802_11
:
1107 case DLT_PRISM_HEADER
:
1110 case DLT_ATM_RFC1483
:
1112 return gen_llc(proto
);
1117 * Check for LLC encapsulation and then check the protocol.
1118 * XXX - also check for LANE and then check for an Ethernet
1121 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1122 b1
= gen_llc(proto
);
1131 * OSI protocols always use 802.2 encapsulation.
1132 * XXX - should we check both the DSAP and the
1133 * LSAP, like this, or should we check just the
1136 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1137 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1138 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1142 case LLCSAP_NETBEUI
:
1144 * NetBEUI always uses 802.2 encapsulation.
1145 * XXX - should we check both the DSAP and the
1146 * LSAP, like this, or should we check just the
1149 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1150 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1151 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1157 * Ethernet_II frames, which are Ethernet
1158 * frames with a frame type of ETHERTYPE_IPX;
1160 * Ethernet_802.3 frames, which have a frame
1161 * type of LINUX_SLL_P_802_3;
1163 * Ethernet_802.2 frames, which are 802.3
1164 * frames with an 802.2 LLC header (i.e, have
1165 * a frame type of LINUX_SLL_P_802_2) and
1166 * with the IPX LSAP as the DSAP in the LLC
1169 * Ethernet_SNAP frames, which are 802.3
1170 * frames with an LLC header and a SNAP
1171 * header and with an OUI of 0x000000
1172 * (encapsulated Ethernet) and a protocol
1173 * ID of ETHERTYPE_IPX in the SNAP header.
1175 * First, do the checks on LINUX_SLL_P_802_2
1176 * frames; generate the check for either
1177 * Ethernet_802.2 or Ethernet_SNAP frames, and
1178 * then put a check for LINUX_SLL_P_802_2 frames
1181 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1182 (bpf_int32
)LLCSAP_IPX
);
1183 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1186 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1190 * Now check for 802.3 frames and OR that with
1191 * the previous test.
1193 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1197 * Now add the check for Ethernet_II frames, and
1198 * do that before checking for the other frame
1201 b0
= gen_cmp(off_linktype
, BPF_H
,
1202 (bpf_int32
)ETHERTYPE_IPX
);
1206 case ETHERTYPE_ATALK
:
1207 case ETHERTYPE_AARP
:
1209 * EtherTalk (AppleTalk protocols on Ethernet link
1210 * layer) may use 802.2 encapsulation.
1214 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1215 * we check for the 802.2 protocol type in the
1216 * "Ethernet type" field.
1218 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1221 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1222 * SNAP packets with an organization code of
1223 * 0x080007 (Apple, for Appletalk) and a protocol
1224 * type of ETHERTYPE_ATALK (Appletalk).
1226 * 802.2-encapsulated ETHERTYPE_AARP packets are
1227 * SNAP packets with an organization code of
1228 * 0x000000 (encapsulated Ethernet) and a protocol
1229 * type of ETHERTYPE_AARP (Appletalk ARP).
1231 if (proto
== ETHERTYPE_ATALK
)
1232 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1234 else /* proto == ETHERTYPE_AARP */
1235 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1240 * Check for Ethernet encapsulation (Ethertalk
1241 * phase 1?); we just check for the Ethernet
1244 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1250 if (proto
<= ETHERMTU
) {
1252 * This is an LLC SAP value, so the frames
1253 * that match would be 802.2 frames.
1254 * Check for the 802.2 protocol type
1255 * in the "Ethernet type" field, and
1256 * then check the DSAP.
1258 b0
= gen_cmp(off_linktype
, BPF_H
,
1260 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1266 * This is an Ethernet type, so compare
1267 * the length/type field with it (if
1268 * the frame is an 802.2 frame, the length
1269 * field will be <= ETHERMTU, and, as
1270 * "proto" is > ETHERMTU, this test
1271 * will fail and the frame won't match,
1272 * which is what we want).
1274 return gen_cmp(off_linktype
, BPF_H
,
1281 case DLT_SLIP_BSDOS
:
1284 * These types don't provide any type field; packets
1287 * XXX - for IPv4, check for a version number of 4, and,
1288 * for IPv6, check for a version number of 6?
1294 case ETHERTYPE_IPV6
:
1296 return gen_true(); /* always true */
1299 return gen_false(); /* always false */
1304 case DLT_PPP_SERIAL
:
1307 * We use Ethernet protocol types inside libpcap;
1308 * map them to the corresponding PPP protocol types.
1313 proto
= PPP_IP
; /* XXX was 0x21 */
1317 case ETHERTYPE_IPV6
:
1326 case ETHERTYPE_ATALK
:
1340 * I'm assuming the "Bridging PDU"s that go
1341 * over PPP are Spanning Tree Protocol
1355 * We use Ethernet protocol types inside libpcap;
1356 * map them to the corresponding PPP protocol types.
1361 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1362 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1364 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1369 case ETHERTYPE_IPV6
:
1379 case ETHERTYPE_ATALK
:
1393 * I'm assuming the "Bridging PDU"s that go
1394 * over PPP are Spanning Tree Protocol
1409 * For DLT_NULL, the link-layer header is a 32-bit
1410 * word containing an AF_ value in *host* byte order.
1412 * In addition, if we're reading a saved capture file,
1413 * the host byte order in the capture may not be the
1414 * same as the host byte order on this machine.
1416 * For DLT_LOOP, the link-layer header is a 32-bit
1417 * word containing an AF_ value in *network* byte order.
1419 * XXX - AF_ values may, unfortunately, be platform-
1420 * dependent; for example, FreeBSD's AF_INET6 is 24
1421 * whilst NetBSD's and OpenBSD's is 26.
1423 * This means that, when reading a capture file, just
1424 * checking for our AF_INET6 value won't work if the
1425 * capture file came from another OS.
1434 case ETHERTYPE_IPV6
:
1441 * Not a type on which we support filtering.
1442 * XXX - support those that have AF_ values
1443 * #defined on this platform, at least?
1448 if (linktype
== DLT_NULL
) {
1450 * The AF_ value is in host byte order, but
1451 * the BPF interpreter will convert it to
1452 * network byte order.
1454 * If this is a save file, and it's from a
1455 * machine with the opposite byte order to
1456 * ours, we byte-swap the AF_ value.
1458 * Then we run it through "htonl()", and
1459 * generate code to compare against the result.
1461 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1462 bpf_pcap
->sf
.swapped
)
1463 proto
= SWAPLONG(proto
);
1464 proto
= htonl(proto
);
1466 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1470 * XXX should we check for first fragment if the protocol
1477 case ETHERTYPE_IPV6
:
1478 return(gen_cmp(2, BPF_B
,
1479 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1482 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1483 b1
= gen_cmp(2, BPF_B
,
1484 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1488 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1489 b1
= gen_cmp(2, BPF_B
,
1490 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1493 case ETHERTYPE_REVARP
:
1494 return(gen_cmp(2, BPF_B
,
1495 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1496 case ETHERTYPE_ATALK
:
1497 return(gen_cmp(2, BPF_B
,
1498 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1504 case ETHERTYPE_ATALK
:
1513 * XXX - assumes a 2-byte Frame Relay header with
1514 * DLCI and flags. What if the address is longer?
1520 * Check for the special NLPID for IP.
1522 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1525 case ETHERTYPE_IPV6
:
1527 * Check for the special NLPID for IPv6.
1529 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1534 * Check for several OSI protocols.
1536 * Frame Relay packets typically have an OSI
1537 * NLPID at the beginning; we check for each
1540 * What we check for is the NLPID and a frame
1541 * control field of UI, i.e. 0x03 followed
1544 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1545 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1546 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1558 * All the types that have no encapsulation should either be
1559 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1560 * all packets are IP packets, or should be handled in some
1561 * special case, if none of them are (if some are and some
1562 * aren't, the lack of encapsulation is a problem, as we'd
1563 * have to find some other way of determining the packet type).
1565 * Therefore, if "off_linktype" is -1, there's an error.
1567 if (off_linktype
== -1)
1571 * Any type not handled above should always have an Ethernet
1572 * type at an offset of "off_linktype". (PPP is partially
1573 * handled above - the protocol type is mapped from the
1574 * Ethernet and LLC types we use internally to the corresponding
1575 * PPP type - but the PPP type is always specified by a value
1576 * at "off_linktype", so we don't have to do the code generation
1579 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1583 * Check for an LLC SNAP packet with a given organization code and
1584 * protocol type; we check the entire contents of the 802.2 LLC and
1585 * snap headers, checking for DSAP and SSAP of SNAP and a control
1586 * field of 0x03 in the LLC header, and for the specified organization
1587 * code and protocol type in the SNAP header.
1589 static struct block
*
1590 gen_snap(orgcode
, ptype
, offset
)
1591 bpf_u_int32 orgcode
;
1595 u_char snapblock
[8];
1597 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1598 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1599 snapblock
[2] = 0x03; /* control = UI */
1600 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1601 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1602 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1603 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1604 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1605 return gen_bcmp(offset
, 8, snapblock
);
1609 * Check for a given protocol value assuming an 802.2 LLC header.
1611 static struct block
*
1616 * XXX - handle token-ring variable-length header.
1621 return gen_cmp(off_linktype
, BPF_H
, (long)
1622 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1624 case LLCSAP_NETBEUI
:
1625 return gen_cmp(off_linktype
, BPF_H
, (long)
1626 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1630 * XXX - are there ever SNAP frames for IPX on
1631 * non-Ethernet 802.x networks?
1633 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1635 case ETHERTYPE_ATALK
:
1637 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1638 * SNAP packets with an organization code of
1639 * 0x080007 (Apple, for Appletalk) and a protocol
1640 * type of ETHERTYPE_ATALK (Appletalk).
1642 * XXX - check for an organization code of
1643 * encapsulated Ethernet as well?
1645 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1649 * XXX - we don't have to check for IPX 802.3
1650 * here, but should we check for the IPX Ethertype?
1652 if (proto
<= ETHERMTU
) {
1654 * This is an LLC SAP value, so check
1657 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1660 * This is an Ethernet type; we assume that it's
1661 * unlikely that it'll appear in the right place
1662 * at random, and therefore check only the
1663 * location that would hold the Ethernet type
1664 * in a SNAP frame with an organization code of
1665 * 0x000000 (encapsulated Ethernet).
1667 * XXX - if we were to check for the SNAP DSAP and
1668 * LSAP, as per XXX, and were also to check for an
1669 * organization code of 0x000000 (encapsulated
1670 * Ethernet), we'd do
1672 * return gen_snap(0x000000, proto,
1675 * here; for now, we don't, as per the above.
1676 * I don't know whether it's worth the extra CPU
1677 * time to do the right check or not.
1679 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1684 static struct block
*
1685 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1689 u_int src_off
, dst_off
;
1691 struct block
*b0
, *b1
;
1705 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1706 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1712 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1713 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1720 b0
= gen_linktype(proto
);
1721 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1727 static struct block
*
1728 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1729 struct in6_addr
*addr
;
1730 struct in6_addr
*mask
;
1732 u_int src_off
, dst_off
;
1734 struct block
*b0
, *b1
;
1749 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1750 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1756 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1757 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1764 /* this order is important */
1765 a
= (u_int32_t
*)addr
;
1766 m
= (u_int32_t
*)mask
;
1767 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1768 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1770 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1772 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1774 b0
= gen_linktype(proto
);
1780 static struct block
*
1781 gen_ehostop(eaddr
, dir
)
1782 register const u_char
*eaddr
;
1785 register struct block
*b0
, *b1
;
1789 return gen_bcmp(6, 6, eaddr
);
1792 return gen_bcmp(0, 6, eaddr
);
1795 b0
= gen_ehostop(eaddr
, Q_SRC
);
1796 b1
= gen_ehostop(eaddr
, Q_DST
);
1802 b0
= gen_ehostop(eaddr
, Q_SRC
);
1803 b1
= gen_ehostop(eaddr
, Q_DST
);
1812 * Like gen_ehostop, but for DLT_FDDI
1814 static struct block
*
1815 gen_fhostop(eaddr
, dir
)
1816 register const u_char
*eaddr
;
1819 struct block
*b0
, *b1
;
1824 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1826 return gen_bcmp(6 + 1, 6, eaddr
);
1831 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1833 return gen_bcmp(0 + 1, 6, eaddr
);
1837 b0
= gen_fhostop(eaddr
, Q_SRC
);
1838 b1
= gen_fhostop(eaddr
, Q_DST
);
1844 b0
= gen_fhostop(eaddr
, Q_SRC
);
1845 b1
= gen_fhostop(eaddr
, Q_DST
);
1854 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1856 static struct block
*
1857 gen_thostop(eaddr
, dir
)
1858 register const u_char
*eaddr
;
1861 register struct block
*b0
, *b1
;
1865 return gen_bcmp(8, 6, eaddr
);
1868 return gen_bcmp(2, 6, eaddr
);
1871 b0
= gen_thostop(eaddr
, Q_SRC
);
1872 b1
= gen_thostop(eaddr
, Q_DST
);
1878 b0
= gen_thostop(eaddr
, Q_SRC
);
1879 b1
= gen_thostop(eaddr
, Q_DST
);
1888 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
1890 static struct block
*
1891 gen_wlanhostop(eaddr
, dir
)
1892 register const u_char
*eaddr
;
1895 register struct block
*b0
, *b1
, *b2
, *b3
;
1896 register struct slist
*s
;
1903 * For control frames, there is no SA.
1905 * For management frames, SA is at an
1906 * offset of 10 from the beginning of
1909 * For data frames, SA is at an offset
1910 * of 10 from the beginning of the packet
1911 * if From DS is clear, at an offset of
1912 * 16 from the beginning of the packet
1913 * if From DS is set and To DS is clear,
1914 * and an offset of 24 from the beginning
1915 * of the packet if From DS is set and To DS
1920 * If the low-order bit of the type value is 1,
1921 * this is either a control frame or a frame
1922 * with a reserved type, and thus not a
1925 * I.e., first check "!(link[0] & 0x04)".
1927 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1929 b0
= new_block(JMP(BPF_JSET
));
1935 * If the high-order bit of the type value is 0, this
1936 * is a management frame.
1937 * I.e, check "!(link[0] & 0x08)".
1939 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1941 b2
= new_block(JMP(BPF_JSET
));
1947 * For management frames, test the SA at 10.
1949 b1
= gen_bcmp(10, 6, eaddr
);
1954 * If the high-order bit of the type value is 0, this
1955 * is a data frame; check From DS.
1957 * I.e., check "(link[0] & 0x08) && !(link[1] & 0x02)".
1959 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1961 b1
= new_block(JMP(BPF_JSET
));
1965 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1967 b2
= new_block(JMP(BPF_JSET
));
1968 b2
->s
.k
= 0x02; /* From DS */
1974 * For data frames with From DS not set, test the SA at 10;
1975 * then combine that test with the management frame test.
1977 b1
= gen_bcmp(10, 6, eaddr
);
1983 * Now, check for a data frame with From DS set and
1986 * I.e., check "(link[0] & 0x08) && (link[1] & 0x02)
1987 * && !(link[1] & 0x01)".
1989 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1991 b1
= new_block(JMP(BPF_JSET
));
1995 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1997 b2
= new_block(JMP(BPF_JSET
));
1998 b2
->s
.k
= 0x02; /* From DS */
2003 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2005 b2
= new_block(JMP(BPF_JSET
));
2006 b2
->s
.k
= 0x01; /* To DS */
2009 gen_and(b1
, b2
); /* Data && From DS and not To DS */
2012 * Test the SA at 16, and OR the previous SA tests
2015 b1
= gen_bcmp(16, 6, eaddr
);
2021 * And now for a data frame with From DS set and To DS set.
2023 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2025 b1
= new_block(JMP(BPF_JSET
));
2029 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2031 b2
= new_block(JMP(BPF_JSET
));
2032 b2
->s
.k
= 0x02; /* From DS */
2037 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2039 b2
= new_block(JMP(BPF_JSET
));
2040 b2
->s
.k
= 0x01; /* To DS */
2042 gen_and(b1
, b2
); /* Data && From DS and To DS */
2045 * Test the SA at 24, and OR the previous SA tests
2048 b1
= gen_bcmp(24, 6, eaddr
);
2059 * For control frames, there is no DA.
2061 * For management frames, DA is at an
2062 * offset of 4 from the beginning of
2065 * For data frames, DA is at an offset
2066 * of 4 from the beginning of the packet
2067 * if To DS is clear and at an offset of
2068 * 16 from the beginning of the packet
2073 * If the low-order bit of the type value is 1,
2074 * this is either a control frame or a frame
2075 * with a reserved type, and thus not a
2078 * I.e., first check "!(link[0] & 0x04)".
2080 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2082 b0
= new_block(JMP(BPF_JSET
));
2088 * If the high-order bit of the type value is 0, this
2089 * is a management frame.
2090 * I.e, check "!(link[0] & 0x08)".
2092 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2094 b2
= new_block(JMP(BPF_JSET
));
2100 * For management frames, test the DA at 4.
2102 b1
= gen_bcmp(4, 6, eaddr
);
2107 * If the high-order bit of the type value is 0, this
2108 * is a data frame; check To DS.
2110 * I.e., check "(link[0] & 0x08) && !(link[1] & 0x01)".
2112 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2114 b1
= new_block(JMP(BPF_JSET
));
2118 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2120 b2
= new_block(JMP(BPF_JSET
));
2121 b2
->s
.k
= 0x01; /* To DS */
2127 * For data frames with To DS not set, test the DA at 4;
2128 * then combine that test with the management frame test.
2130 b1
= gen_bcmp(4, 6, eaddr
);
2136 * Now, check for a data frame with To DS set.
2138 * I.e., check "(link[0] & 0x08) && (link[1] & 0x01)".
2140 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2142 b1
= new_block(JMP(BPF_JSET
));
2146 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2148 b2
= new_block(JMP(BPF_JSET
));
2149 b2
->s
.k
= 0x01; /* To DS */
2154 * Test the DA at 16, and OR the previous DA tests
2157 b1
= gen_bcmp(16, 6, eaddr
);
2165 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2166 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2172 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2173 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2182 * This is quite tricky because there may be pad bytes in front of the
2183 * DECNET header, and then there are two possible data packet formats that
2184 * carry both src and dst addresses, plus 5 packet types in a format that
2185 * carries only the src node, plus 2 types that use a different format and
2186 * also carry just the src node.
2190 * Instead of doing those all right, we just look for data packets with
2191 * 0 or 1 bytes of padding. If you want to look at other packets, that
2192 * will require a lot more hacking.
2194 * To add support for filtering on DECNET "areas" (network numbers)
2195 * one would want to add a "mask" argument to this routine. That would
2196 * make the filter even more inefficient, although one could be clever
2197 * and not generate masking instructions if the mask is 0xFFFF.
2199 static struct block
*
2200 gen_dnhostop(addr
, dir
, base_off
)
2205 struct block
*b0
, *b1
, *b2
, *tmp
;
2206 u_int offset_lh
; /* offset if long header is received */
2207 u_int offset_sh
; /* offset if short header is received */
2212 offset_sh
= 1; /* follows flags */
2213 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2217 offset_sh
= 3; /* follows flags, dstnode */
2218 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2222 /* Inefficient because we do our Calvinball dance twice */
2223 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2224 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2230 /* Inefficient because we do our Calvinball dance twice */
2231 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2232 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2237 bpf_error("ISO host filtering not implemented");
2242 b0
= gen_linktype(ETHERTYPE_DN
);
2243 /* Check for pad = 1, long header case */
2244 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2245 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2246 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2247 BPF_H
, (bpf_int32
)ntohs(addr
));
2249 /* Check for pad = 0, long header case */
2250 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2251 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2254 /* Check for pad = 1, short header case */
2255 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2256 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2257 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2258 BPF_H
, (bpf_int32
)ntohs(addr
));
2261 /* Check for pad = 0, short header case */
2262 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2263 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2267 /* Combine with test for linktype */
2272 static struct block
*
2273 gen_host(addr
, mask
, proto
, dir
)
2279 struct block
*b0
, *b1
;
2284 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2285 if (off_linktype
!= -1) {
2286 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2288 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2294 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2295 off_nl
+ 12, off_nl
+ 16);
2298 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2299 off_nl
+ 14, off_nl
+ 24);
2302 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2303 off_nl
+ 14, off_nl
+ 24);
2306 bpf_error("'tcp' modifier applied to host");
2309 bpf_error("'sctp' modifier applied to host");
2312 bpf_error("'udp' modifier applied to host");
2315 bpf_error("'icmp' modifier applied to host");
2318 bpf_error("'igmp' modifier applied to host");
2321 bpf_error("'igrp' modifier applied to host");
2324 bpf_error("'pim' modifier applied to host");
2327 bpf_error("'vrrp' modifier applied to host");
2330 bpf_error("ATALK host filtering not implemented");
2333 bpf_error("AARP host filtering not implemented");
2336 return gen_dnhostop(addr
, dir
, off_nl
);
2339 bpf_error("SCA host filtering not implemented");
2342 bpf_error("LAT host filtering not implemented");
2345 bpf_error("MOPDL host filtering not implemented");
2348 bpf_error("MOPRC host filtering not implemented");
2352 bpf_error("'ip6' modifier applied to ip host");
2355 bpf_error("'icmp6' modifier applied to host");
2359 bpf_error("'ah' modifier applied to host");
2362 bpf_error("'esp' modifier applied to host");
2365 bpf_error("ISO host filtering not implemented");
2368 bpf_error("'esis' modifier applied to host");
2371 bpf_error("'isis' modifier applied to host");
2374 bpf_error("'clnp' modifier applied to host");
2377 bpf_error("'stp' modifier applied to host");
2380 bpf_error("IPX host filtering not implemented");
2383 bpf_error("'netbeui' modifier applied to host");
2392 static struct block
*
2393 gen_host6(addr
, mask
, proto
, dir
)
2394 struct in6_addr
*addr
;
2395 struct in6_addr
*mask
;
2402 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2405 bpf_error("'ip' modifier applied to ip6 host");
2408 bpf_error("'rarp' modifier applied to ip6 host");
2411 bpf_error("'arp' modifier applied to ip6 host");
2414 bpf_error("'sctp' modifier applied to host");
2417 bpf_error("'tcp' modifier applied to host");
2420 bpf_error("'udp' modifier applied to host");
2423 bpf_error("'icmp' modifier applied to host");
2426 bpf_error("'igmp' modifier applied to host");
2429 bpf_error("'igrp' modifier applied to host");
2432 bpf_error("'pim' modifier applied to host");
2435 bpf_error("'vrrp' modifier applied to host");
2438 bpf_error("ATALK host filtering not implemented");
2441 bpf_error("AARP host filtering not implemented");
2444 bpf_error("'decnet' modifier applied to ip6 host");
2447 bpf_error("SCA host filtering not implemented");
2450 bpf_error("LAT host filtering not implemented");
2453 bpf_error("MOPDL host filtering not implemented");
2456 bpf_error("MOPRC host filtering not implemented");
2459 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2460 off_nl
+ 8, off_nl
+ 24);
2463 bpf_error("'icmp6' modifier applied to host");
2466 bpf_error("'ah' modifier applied to host");
2469 bpf_error("'esp' modifier applied to host");
2472 bpf_error("ISO host filtering not implemented");
2475 bpf_error("'esis' modifier applied to host");
2478 bpf_error("'isis' modifier applied to host");
2481 bpf_error("'clnp' modifier applied to host");
2484 bpf_error("'stp' modifier applied to host");
2487 bpf_error("IPX host filtering not implemented");
2490 bpf_error("'netbeui' modifier applied to host");
2500 static struct block
*
2501 gen_gateway(eaddr
, alist
, proto
, dir
)
2502 const u_char
*eaddr
;
2503 bpf_u_int32
**alist
;
2507 struct block
*b0
, *b1
, *tmp
;
2510 bpf_error("direction applied to 'gateway'");
2517 if (linktype
== DLT_EN10MB
)
2518 b0
= gen_ehostop(eaddr
, Q_OR
);
2519 else if (linktype
== DLT_FDDI
)
2520 b0
= gen_fhostop(eaddr
, Q_OR
);
2521 else if (linktype
== DLT_IEEE802
)
2522 b0
= gen_thostop(eaddr
, Q_OR
);
2523 else if (linktype
== DLT_IEEE802_11
)
2524 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2527 "'gateway' supported only on ethernet/FDDI/token ring/802.11");
2529 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2531 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2539 bpf_error("illegal modifier of 'gateway'");
2545 gen_proto_abbrev(proto
)
2556 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2558 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2564 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2566 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2572 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2574 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2580 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2583 #ifndef IPPROTO_IGMP
2584 #define IPPROTO_IGMP 2
2588 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2591 #ifndef IPPROTO_IGRP
2592 #define IPPROTO_IGRP 9
2595 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2599 #define IPPROTO_PIM 103
2603 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2605 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2610 #ifndef IPPROTO_VRRP
2611 #define IPPROTO_VRRP 112
2615 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2619 b1
= gen_linktype(ETHERTYPE_IP
);
2623 b1
= gen_linktype(ETHERTYPE_ARP
);
2627 b1
= gen_linktype(ETHERTYPE_REVARP
);
2631 bpf_error("link layer applied in wrong context");
2634 b1
= gen_linktype(ETHERTYPE_ATALK
);
2638 b1
= gen_linktype(ETHERTYPE_AARP
);
2642 b1
= gen_linktype(ETHERTYPE_DN
);
2646 b1
= gen_linktype(ETHERTYPE_SCA
);
2650 b1
= gen_linktype(ETHERTYPE_LAT
);
2654 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2658 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2663 b1
= gen_linktype(ETHERTYPE_IPV6
);
2666 #ifndef IPPROTO_ICMPV6
2667 #define IPPROTO_ICMPV6 58
2670 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2675 #define IPPROTO_AH 51
2678 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2680 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2686 #define IPPROTO_ESP 50
2689 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2691 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2697 b1
= gen_linktype(LLCSAP_ISONS
);
2701 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2705 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2709 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2713 b1
= gen_linktype(LLCSAP_8021D
);
2717 b1
= gen_linktype(LLCSAP_IPX
);
2721 b1
= gen_linktype(LLCSAP_NETBEUI
);
2730 static struct block
*
2737 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2738 s
->s
.k
= off_nl
+ 6;
2739 b
= new_block(JMP(BPF_JSET
));
2747 static struct block
*
2748 gen_portatom(off
, v
)
2755 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2758 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2759 s
->next
->s
.k
= off_nl
+ off
;
2761 b
= new_block(JMP(BPF_JEQ
));
2769 static struct block
*
2770 gen_portatom6(off
, v
)
2774 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2779 gen_portop(port
, proto
, dir
)
2780 int port
, proto
, dir
;
2782 struct block
*b0
, *b1
, *tmp
;
2784 /* ip proto 'proto' */
2785 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2791 b1
= gen_portatom(0, (bpf_int32
)port
);
2795 b1
= gen_portatom(2, (bpf_int32
)port
);
2800 tmp
= gen_portatom(0, (bpf_int32
)port
);
2801 b1
= gen_portatom(2, (bpf_int32
)port
);
2806 tmp
= gen_portatom(0, (bpf_int32
)port
);
2807 b1
= gen_portatom(2, (bpf_int32
)port
);
2819 static struct block
*
2820 gen_port(port
, ip_proto
, dir
)
2825 struct block
*b0
, *b1
, *tmp
;
2827 /* ether proto ip */
2828 b0
= gen_linktype(ETHERTYPE_IP
);
2834 b1
= gen_portop(port
, ip_proto
, dir
);
2838 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2839 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2841 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2854 gen_portop6(port
, proto
, dir
)
2855 int port
, proto
, dir
;
2857 struct block
*b0
, *b1
, *tmp
;
2859 /* ip proto 'proto' */
2860 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2864 b1
= gen_portatom6(0, (bpf_int32
)port
);
2868 b1
= gen_portatom6(2, (bpf_int32
)port
);
2873 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2874 b1
= gen_portatom6(2, (bpf_int32
)port
);
2879 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2880 b1
= gen_portatom6(2, (bpf_int32
)port
);
2892 static struct block
*
2893 gen_port6(port
, ip_proto
, dir
)
2898 struct block
*b0
, *b1
, *tmp
;
2900 /* ether proto ip */
2901 b0
= gen_linktype(ETHERTYPE_IPV6
);
2907 b1
= gen_portop6(port
, ip_proto
, dir
);
2911 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2912 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2914 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2927 lookup_proto(name
, proto
)
2928 register const char *name
;
2938 v
= pcap_nametoproto(name
);
2939 if (v
== PROTO_UNDEF
)
2940 bpf_error("unknown ip proto '%s'", name
);
2944 /* XXX should look up h/w protocol type based on linktype */
2945 v
= pcap_nametoeproto(name
);
2946 if (v
== PROTO_UNDEF
)
2947 bpf_error("unknown ether proto '%s'", name
);
2951 if (strcmp(name
, "esis") == 0)
2953 else if (strcmp(name
, "isis") == 0)
2955 else if (strcmp(name
, "clnp") == 0)
2958 bpf_error("unknown osi proto '%s'", name
);
2978 static struct block
*
2979 gen_protochain(v
, proto
, dir
)
2984 #ifdef NO_PROTOCHAIN
2985 return gen_proto(v
, proto
, dir
);
2987 struct block
*b0
, *b
;
2988 struct slist
*s
[100];
2989 int fix2
, fix3
, fix4
, fix5
;
2990 int ahcheck
, again
, end
;
2992 int reg2
= alloc_reg();
2994 memset(s
, 0, sizeof(s
));
2995 fix2
= fix3
= fix4
= fix5
= 0;
3002 b0
= gen_protochain(v
, Q_IP
, dir
);
3003 b
= gen_protochain(v
, Q_IPV6
, dir
);
3007 bpf_error("bad protocol applied for 'protochain'");
3011 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3014 * s[0] is a dummy entry to protect other BPF insn from damaged
3015 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3016 * hard to find interdependency made by jump table fixup.
3019 s
[i
] = new_stmt(0); /*dummy*/
3024 b0
= gen_linktype(ETHERTYPE_IP
);
3027 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3028 s
[i
]->s
.k
= off_nl
+ 9;
3030 /* X = ip->ip_hl << 2 */
3031 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3037 b0
= gen_linktype(ETHERTYPE_IPV6
);
3039 /* A = ip6->ip_nxt */
3040 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3041 s
[i
]->s
.k
= off_nl
+ 6;
3043 /* X = sizeof(struct ip6_hdr) */
3044 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3050 bpf_error("unsupported proto to gen_protochain");
3054 /* again: if (A == v) goto end; else fall through; */
3056 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3058 s
[i
]->s
.jt
= NULL
; /*later*/
3059 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3063 #ifndef IPPROTO_NONE
3064 #define IPPROTO_NONE 59
3066 /* if (A == IPPROTO_NONE) goto end */
3067 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3068 s
[i
]->s
.jt
= NULL
; /*later*/
3069 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3070 s
[i
]->s
.k
= IPPROTO_NONE
;
3071 s
[fix5
]->s
.jf
= s
[i
];
3076 if (proto
== Q_IPV6
) {
3077 int v6start
, v6end
, v6advance
, j
;
3080 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3081 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3082 s
[i
]->s
.jt
= NULL
; /*later*/
3083 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3084 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3085 s
[fix2
]->s
.jf
= s
[i
];
3087 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3088 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3089 s
[i
]->s
.jt
= NULL
; /*later*/
3090 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3091 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3093 /* if (A == IPPROTO_ROUTING) goto v6advance */
3094 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3095 s
[i
]->s
.jt
= NULL
; /*later*/
3096 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3097 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3099 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3100 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3101 s
[i
]->s
.jt
= NULL
; /*later*/
3102 s
[i
]->s
.jf
= NULL
; /*later*/
3103 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3114 * X = X + (P[X + 1] + 1) * 8;
3117 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3119 /* A = P[X + packet head] */
3120 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3124 s
[i
] = new_stmt(BPF_ST
);
3128 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3131 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3135 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3137 /* A = P[X + packet head]; */
3138 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3142 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3146 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3150 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3153 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3157 /* goto again; (must use BPF_JA for backward jump) */
3158 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3159 s
[i
]->s
.k
= again
- i
- 1;
3160 s
[i
- 1]->s
.jf
= s
[i
];
3164 for (j
= v6start
; j
<= v6end
; j
++)
3165 s
[j
]->s
.jt
= s
[v6advance
];
3170 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3172 s
[fix2
]->s
.jf
= s
[i
];
3178 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3179 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3180 s
[i
]->s
.jt
= NULL
; /*later*/
3181 s
[i
]->s
.jf
= NULL
; /*later*/
3182 s
[i
]->s
.k
= IPPROTO_AH
;
3184 s
[fix3
]->s
.jf
= s
[ahcheck
];
3191 * X = X + (P[X + 1] + 2) * 4;
3194 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3196 /* A = P[X + packet head]; */
3197 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3201 s
[i
] = new_stmt(BPF_ST
);
3205 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3208 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3212 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3214 /* A = P[X + packet head] */
3215 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3219 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3223 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3227 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3230 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3234 /* goto again; (must use BPF_JA for backward jump) */
3235 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3236 s
[i
]->s
.k
= again
- i
- 1;
3241 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3243 s
[fix2
]->s
.jt
= s
[end
];
3244 s
[fix4
]->s
.jf
= s
[end
];
3245 s
[fix5
]->s
.jt
= s
[end
];
3252 for (i
= 0; i
< max
- 1; i
++)
3253 s
[i
]->next
= s
[i
+ 1];
3254 s
[max
- 1]->next
= NULL
;
3259 b
= new_block(JMP(BPF_JEQ
));
3260 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3270 static struct block
*
3271 gen_proto(v
, proto
, dir
)
3276 struct block
*b0
, *b1
;
3278 if (dir
!= Q_DEFAULT
)
3279 bpf_error("direction applied to 'proto'");
3284 b0
= gen_proto(v
, Q_IP
, dir
);
3285 b1
= gen_proto(v
, Q_IPV6
, dir
);
3292 b0
= gen_linktype(ETHERTYPE_IP
);
3294 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3296 b1
= gen_protochain(v
, Q_IP
);
3306 * Frame Relay packets typically have an OSI
3307 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3308 * generates code to check for all the OSI
3309 * NLPIDs, so calling it and then adding a check
3310 * for the particular NLPID for which we're
3311 * looking is bogus, as we can just check for
3314 * What we check for is the NLPID and a frame
3315 * control field value of UI, i.e. 0x03 followed
3318 * XXX - assumes a 2-byte Frame Relay header with
3319 * DLCI and flags. What if the address is longer?
3321 * XXX - what about SNAP-encapsulated frames?
3323 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3327 b0
= gen_linktype(LLCSAP_ISONS
);
3328 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3334 bpf_error("arp does not encapsulate another protocol");
3338 bpf_error("rarp does not encapsulate another protocol");
3342 bpf_error("atalk encapsulation is not specifiable");
3346 bpf_error("decnet encapsulation is not specifiable");
3350 bpf_error("sca does not encapsulate another protocol");
3354 bpf_error("lat does not encapsulate another protocol");
3358 bpf_error("moprc does not encapsulate another protocol");
3362 bpf_error("mopdl does not encapsulate another protocol");
3366 return gen_linktype(v
);
3369 bpf_error("'udp proto' is bogus");
3373 bpf_error("'tcp proto' is bogus");
3377 bpf_error("'sctp proto' is bogus");
3381 bpf_error("'icmp proto' is bogus");
3385 bpf_error("'igmp proto' is bogus");
3389 bpf_error("'igrp proto' is bogus");
3393 bpf_error("'pim proto' is bogus");
3397 bpf_error("'vrrp proto' is bogus");
3402 b0
= gen_linktype(ETHERTYPE_IPV6
);
3404 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3406 b1
= gen_protochain(v
, Q_IPV6
);
3412 bpf_error("'icmp6 proto' is bogus");
3416 bpf_error("'ah proto' is bogus");
3419 bpf_error("'ah proto' is bogus");
3422 bpf_error("'stp proto' is bogus");
3425 bpf_error("'ipx proto' is bogus");
3428 bpf_error("'netbeui proto' is bogus");
3439 register const char *name
;
3442 int proto
= q
.proto
;
3446 bpf_u_int32 mask
, addr
;
3448 bpf_u_int32
**alist
;
3451 struct sockaddr_in
*sin
;
3452 struct sockaddr_in6
*sin6
;
3453 struct addrinfo
*res
, *res0
;
3454 struct in6_addr mask128
;
3456 struct block
*b
, *tmp
;
3457 int port
, real_proto
;
3462 addr
= pcap_nametonetaddr(name
);
3464 bpf_error("unknown network '%s'", name
);
3465 /* Left justify network addr and calculate its network mask */
3467 while (addr
&& (addr
& 0xff000000) == 0) {
3471 return gen_host(addr
, mask
, proto
, dir
);
3475 if (proto
== Q_LINK
) {
3479 eaddr
= pcap_ether_hostton(name
);
3482 "unknown ether host '%s'", name
);
3483 b
= gen_ehostop(eaddr
, dir
);
3488 eaddr
= pcap_ether_hostton(name
);
3491 "unknown FDDI host '%s'", name
);
3492 b
= gen_fhostop(eaddr
, dir
);
3497 eaddr
= pcap_ether_hostton(name
);
3500 "unknown token ring host '%s'", name
);
3501 b
= gen_thostop(eaddr
, dir
);
3505 case DLT_IEEE802_11
:
3506 eaddr
= pcap_ether_hostton(name
);
3509 "unknown 802.11 host '%s'", name
);
3510 b
= gen_wlanhostop(eaddr
, dir
);
3516 "only ethernet/FDDI/token ring/802.11 supports link-level host name");
3519 } else if (proto
== Q_DECNET
) {
3520 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3522 * I don't think DECNET hosts can be multihomed, so
3523 * there is no need to build up a list of addresses
3525 return (gen_host(dn_addr
, 0, proto
, dir
));
3528 alist
= pcap_nametoaddr(name
);
3529 if (alist
== NULL
|| *alist
== NULL
)
3530 bpf_error("unknown host '%s'", name
);
3532 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3534 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3536 tmp
= gen_host(**alist
++, 0xffffffff,
3543 memset(&mask128
, 0xff, sizeof(mask128
));
3544 res0
= res
= pcap_nametoaddrinfo(name
);
3546 bpf_error("unknown host '%s'", name
);
3548 tproto
= tproto6
= proto
;
3549 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3553 for (res
= res0
; res
; res
= res
->ai_next
) {
3554 switch (res
->ai_family
) {
3556 if (tproto
== Q_IPV6
)
3559 sin
= (struct sockaddr_in
*)
3561 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3562 0xffffffff, tproto
, dir
);
3565 if (tproto6
== Q_IP
)
3568 sin6
= (struct sockaddr_in6
*)
3570 tmp
= gen_host6(&sin6
->sin6_addr
,
3571 &mask128
, tproto6
, dir
);
3582 bpf_error("unknown host '%s'%s", name
,
3583 (proto
== Q_DEFAULT
)
3585 : " for specified address family");
3592 if (proto
!= Q_DEFAULT
&&
3593 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3594 bpf_error("illegal qualifier of 'port'");
3595 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3596 bpf_error("unknown port '%s'", name
);
3597 if (proto
== Q_UDP
) {
3598 if (real_proto
== IPPROTO_TCP
)
3599 bpf_error("port '%s' is tcp", name
);
3600 else if (real_proto
== IPPROTO_SCTP
)
3601 bpf_error("port '%s' is sctp", name
);
3603 /* override PROTO_UNDEF */
3604 real_proto
= IPPROTO_UDP
;
3606 if (proto
== Q_TCP
) {
3607 if (real_proto
== IPPROTO_UDP
)
3608 bpf_error("port '%s' is udp", name
);
3610 else if (real_proto
== IPPROTO_SCTP
)
3611 bpf_error("port '%s' is sctp", name
);
3613 /* override PROTO_UNDEF */
3614 real_proto
= IPPROTO_TCP
;
3616 if (proto
== Q_SCTP
) {
3617 if (real_proto
== IPPROTO_UDP
)
3618 bpf_error("port '%s' is udp", name
);
3620 else if (real_proto
== IPPROTO_TCP
)
3621 bpf_error("port '%s' is tcp", name
);
3623 /* override PROTO_UNDEF */
3624 real_proto
= IPPROTO_SCTP
;
3627 return gen_port(port
, real_proto
, dir
);
3631 b
= gen_port(port
, real_proto
, dir
);
3632 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3639 eaddr
= pcap_ether_hostton(name
);
3641 bpf_error("unknown ether host: %s", name
);
3643 alist
= pcap_nametoaddr(name
);
3644 if (alist
== NULL
|| *alist
== NULL
)
3645 bpf_error("unknown host '%s'", name
);
3646 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3650 bpf_error("'gateway' not supported in this configuration");
3654 real_proto
= lookup_proto(name
, proto
);
3655 if (real_proto
>= 0)
3656 return gen_proto(real_proto
, proto
, dir
);
3658 bpf_error("unknown protocol: %s", name
);
3661 real_proto
= lookup_proto(name
, proto
);
3662 if (real_proto
>= 0)
3663 return gen_protochain(real_proto
, proto
, dir
);
3665 bpf_error("unknown protocol: %s", name
);
3677 gen_mcode(s1
, s2
, masklen
, q
)
3678 register const char *s1
, *s2
;
3679 register int masklen
;
3682 register int nlen
, mlen
;
3685 nlen
= __pcap_atoin(s1
, &n
);
3686 /* Promote short ipaddr */
3690 mlen
= __pcap_atoin(s2
, &m
);
3691 /* Promote short ipaddr */
3694 bpf_error("non-network bits set in \"%s mask %s\"",
3697 /* Convert mask len to mask */
3699 bpf_error("mask length must be <= 32");
3700 m
= 0xffffffff << (32 - masklen
);
3702 bpf_error("non-network bits set in \"%s/%d\"",
3709 return gen_host(n
, m
, q
.proto
, q
.dir
);
3712 bpf_error("Mask syntax for networks only");
3719 register const char *s
;
3724 int proto
= q
.proto
;
3730 else if (q
.proto
== Q_DECNET
)
3731 vlen
= __pcap_atodn(s
, &v
);
3733 vlen
= __pcap_atoin(s
, &v
);
3740 if (proto
== Q_DECNET
)
3741 return gen_host(v
, 0, proto
, dir
);
3742 else if (proto
== Q_LINK
) {
3743 bpf_error("illegal link layer address");
3746 if (s
== NULL
&& q
.addr
== Q_NET
) {
3747 /* Promote short net number */
3748 while (v
&& (v
& 0xff000000) == 0) {
3753 /* Promote short ipaddr */
3757 return gen_host(v
, mask
, proto
, dir
);
3762 proto
= IPPROTO_UDP
;
3763 else if (proto
== Q_TCP
)
3764 proto
= IPPROTO_TCP
;
3765 else if (proto
== Q_SCTP
)
3766 proto
= IPPROTO_SCTP
;
3767 else if (proto
== Q_DEFAULT
)
3768 proto
= PROTO_UNDEF
;
3770 bpf_error("illegal qualifier of 'port'");
3773 return gen_port((int)v
, proto
, dir
);
3777 b
= gen_port((int)v
, proto
, dir
);
3778 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3784 bpf_error("'gateway' requires a name");
3788 return gen_proto((int)v
, proto
, dir
);
3791 return gen_protochain((int)v
, proto
, dir
);
3806 gen_mcode6(s1
, s2
, masklen
, q
)
3807 register const char *s1
, *s2
;
3808 register int masklen
;
3811 struct addrinfo
*res
;
3812 struct in6_addr
*addr
;
3813 struct in6_addr mask
;
3818 bpf_error("no mask %s supported", s2
);
3820 res
= pcap_nametoaddrinfo(s1
);
3822 bpf_error("invalid ip6 address %s", s1
);
3824 bpf_error("%s resolved to multiple address", s1
);
3825 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3827 if (sizeof(mask
) * 8 < masklen
)
3828 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3829 memset(&mask
, 0, sizeof(mask
));
3830 memset(&mask
, 0xff, masklen
/ 8);
3832 mask
.s6_addr
[masklen
/ 8] =
3833 (0xff << (8 - masklen
% 8)) & 0xff;
3836 a
= (u_int32_t
*)addr
;
3837 m
= (u_int32_t
*)&mask
;
3838 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3839 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3840 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3848 bpf_error("Mask syntax for networks only");
3852 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3857 bpf_error("invalid qualifier against IPv6 address");
3865 register const u_char
*eaddr
;
3868 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3869 if (linktype
== DLT_EN10MB
)
3870 return gen_ehostop(eaddr
, (int)q
.dir
);
3871 if (linktype
== DLT_FDDI
)
3872 return gen_fhostop(eaddr
, (int)q
.dir
);
3873 if (linktype
== DLT_IEEE802
)
3874 return gen_thostop(eaddr
, (int)q
.dir
);
3875 if (linktype
== DLT_IEEE802_11
)
3876 return gen_wlanhostop(eaddr
, (int)q
.dir
);
3877 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11");
3879 bpf_error("ethernet address used in non-ether expression");
3885 struct slist
*s0
, *s1
;
3888 * This is definitely not the best way to do this, but the
3889 * lists will rarely get long.
3896 static struct slist
*
3902 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3907 static struct slist
*
3913 s
= new_stmt(BPF_LD
|BPF_MEM
);
3919 gen_load(proto
, index
, size
)
3924 struct slist
*s
, *tmp
;
3926 int regno
= alloc_reg();
3928 free_reg(index
->regno
);
3932 bpf_error("data size must be 1, 2, or 4");
3948 bpf_error("unsupported index operation");
3951 s
= xfer_to_x(index
);
3952 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3954 sappend(index
->s
, s
);
3969 /* XXX Note that we assume a fixed link header here. */
3970 s
= xfer_to_x(index
);
3971 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3974 sappend(index
->s
, s
);
3976 b
= gen_proto_abbrev(proto
);
3978 gen_and(index
->b
, b
);
3990 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3992 sappend(s
, xfer_to_a(index
));
3993 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
3994 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
3995 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
3997 sappend(index
->s
, s
);
3999 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4001 gen_and(index
->b
, b
);
4003 gen_and(gen_proto_abbrev(Q_IP
), b
);
4009 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4013 index
->regno
= regno
;
4014 s
= new_stmt(BPF_ST
);
4016 sappend(index
->s
, s
);
4022 gen_relation(code
, a0
, a1
, reversed
)
4024 struct arth
*a0
, *a1
;
4027 struct slist
*s0
, *s1
, *s2
;
4028 struct block
*b
, *tmp
;
4032 if (code
== BPF_JEQ
) {
4033 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4034 b
= new_block(JMP(code
));
4038 b
= new_block(BPF_JMP
|code
|BPF_X
);
4044 sappend(a0
->s
, a1
->s
);
4048 free_reg(a0
->regno
);
4049 free_reg(a1
->regno
);
4051 /* 'and' together protocol checks */
4054 gen_and(a0
->b
, tmp
= a1
->b
);
4070 int regno
= alloc_reg();
4071 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4074 s
= new_stmt(BPF_LD
|BPF_LEN
);
4075 s
->next
= new_stmt(BPF_ST
);
4076 s
->next
->s
.k
= regno
;
4091 a
= (struct arth
*)newchunk(sizeof(*a
));
4095 s
= new_stmt(BPF_LD
|BPF_IMM
);
4097 s
->next
= new_stmt(BPF_ST
);
4113 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4116 s
= new_stmt(BPF_ST
);
4124 gen_arth(code
, a0
, a1
)
4126 struct arth
*a0
, *a1
;
4128 struct slist
*s0
, *s1
, *s2
;
4132 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4137 sappend(a0
->s
, a1
->s
);
4139 free_reg(a0
->regno
);
4140 free_reg(a1
->regno
);
4142 s0
= new_stmt(BPF_ST
);
4143 a0
->regno
= s0
->s
.k
= alloc_reg();
4150 * Here we handle simple allocation of the scratch registers.
4151 * If too many registers are alloc'd, the allocator punts.
4153 static int regused
[BPF_MEMWORDS
];
4157 * Return the next free register.
4162 int n
= BPF_MEMWORDS
;
4165 if (regused
[curreg
])
4166 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4168 regused
[curreg
] = 1;
4172 bpf_error("too many registers needed to evaluate expression");
4177 * Return a register to the table so it can
4187 static struct block
*
4194 s
= new_stmt(BPF_LD
|BPF_LEN
);
4195 b
= new_block(JMP(jmp
));
4206 return gen_len(BPF_JGE
, n
);
4210 * Actually, this is less than or equal.
4218 b
= gen_len(BPF_JGT
, n
);
4225 gen_byteop(op
, idx
, val
)
4236 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4239 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4240 b
->s
.code
= JMP(BPF_JGE
);
4245 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4246 b
->s
.code
= JMP(BPF_JGT
);
4250 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4254 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4258 b
= new_block(JMP(BPF_JEQ
));
4265 static u_char abroadcast
[] = { 0x0 };
4268 gen_broadcast(proto
)
4271 bpf_u_int32 hostmask
;
4272 struct block
*b0
, *b1
, *b2
;
4273 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4279 if (linktype
== DLT_ARCNET
)
4280 return gen_ahostop(abroadcast
, Q_DST
);
4281 if (linktype
== DLT_EN10MB
)
4282 return gen_ehostop(ebroadcast
, Q_DST
);
4283 if (linktype
== DLT_FDDI
)
4284 return gen_fhostop(ebroadcast
, Q_DST
);
4285 if (linktype
== DLT_IEEE802
)
4286 return gen_thostop(ebroadcast
, Q_DST
);
4287 if (linktype
== DLT_IEEE802_11
)
4288 return gen_wlanhostop(ebroadcast
, Q_DST
);
4289 bpf_error("not a broadcast link");
4293 b0
= gen_linktype(ETHERTYPE_IP
);
4294 hostmask
= ~netmask
;
4295 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4296 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4297 (bpf_int32
)(~0 & hostmask
), hostmask
);
4302 bpf_error("only ether/ip broadcast filters supported");
4306 * Generate code to test the low-order bit of a MAC address (that's
4307 * the bottom bit of the *first* byte).
4309 static struct block
*
4310 gen_mac_multicast(offset
)
4313 register struct block
*b0
;
4314 register struct slist
*s
;
4316 /* link[offset] & 1 != 0 */
4317 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4319 b0
= new_block(JMP(BPF_JSET
));
4326 gen_multicast(proto
)
4329 register struct block
*b0
, *b1
, *b2
, *b3
;
4330 register struct slist
*s
;
4336 if (linktype
== DLT_ARCNET
)
4337 /* all ARCnet multicasts use the same address */
4338 return gen_ahostop(abroadcast
, Q_DST
);
4340 if (linktype
== DLT_EN10MB
) {
4341 /* ether[0] & 1 != 0 */
4342 return gen_mac_multicast(0);
4345 if (linktype
== DLT_FDDI
) {
4347 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4349 * XXX - was that referring to bit-order issues?
4351 /* fddi[1] & 1 != 0 */
4352 return gen_mac_multicast(1);
4355 if (linktype
== DLT_IEEE802
) {
4356 /* tr[2] & 1 != 0 */
4357 return gen_mac_multicast(2);
4360 if (linktype
== DLT_IEEE802_11
) {
4364 * For control frames, there is no DA.
4366 * For management frames, DA is at an
4367 * offset of 4 from the beginning of
4370 * For data frames, DA is at an offset
4371 * of 4 from the beginning of the packet
4372 * if To DS is clear and at an offset of
4373 * 16 from the beginning of the packet
4378 * If the low-order bit of the type value is 1,
4379 * this is either a control frame or a frame
4380 * with a reserved type, and thus not a
4383 * I.e., first check "!(link[0] & 0x04)".
4385 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4387 b0
= new_block(JMP(BPF_JSET
));
4393 * If the high-order bit of the type value is 0,
4394 * this is a management frame.
4395 * I.e, check "!(link[0] & 0x08)".
4397 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4399 b2
= new_block(JMP(BPF_JSET
));
4405 * For management frames, test the DA at 4.
4407 b1
= gen_mac_multicast(4);
4412 * If the high-order bit of the type value is 0,
4413 * this is a data frame; check To DS.
4415 * I.e., check "(link[0] & 0x08) && !(link[1] & 0x01)".
4417 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4419 b1
= new_block(JMP(BPF_JSET
));
4423 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4425 b2
= new_block(JMP(BPF_JSET
));
4426 b2
->s
.k
= 0x01; /* To DS */
4432 * For data frames with To DS not set, test the DA
4433 * at 4; then combine that test with the management
4436 b1
= gen_mac_multicast(4);
4442 * Now, check for a data frame with To DS set.
4444 * I.e., check "(link[0] & 0x08) && (link[1] & 0x01)".
4446 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4448 b1
= new_block(JMP(BPF_JSET
));
4452 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4454 b2
= new_block(JMP(BPF_JSET
));
4455 b2
->s
.k
= 0x01; /* To DS */
4460 * Test the DA at 16, and OR the previous DA tests
4463 b1
= gen_mac_multicast(16);
4471 /* Link not known to support multicasts */
4475 b0
= gen_linktype(ETHERTYPE_IP
);
4476 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4477 b1
->s
.code
= JMP(BPF_JGE
);
4483 b0
= gen_linktype(ETHERTYPE_IPV6
);
4484 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4489 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4493 * generate command for inbound/outbound. It's here so we can
4494 * make it link-type specific. 'dir' = 0 implies "inbound",
4495 * = 1 implies "outbound".
4501 register struct block
*b0
;
4504 * Only some data link types support inbound/outbound qualifiers.
4509 b0
= gen_relation(BPF_JEQ
,
4510 gen_load(Q_LINK
, gen_loadi(0), 1),
4518 * Match packets sent by this machine.
4520 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
4523 * Match packets sent to this machine.
4524 * (No broadcast or multicast packets, or
4525 * packets sent to some other machine and
4526 * received promiscuously.)
4528 * XXX - packets sent to other machines probably
4529 * shouldn't be matched, but what about broadcast
4530 * or multicast packets we received?
4532 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
4537 bpf_error("inbound/outbound not supported on linktype %d\n",
4547 register const u_char
*eaddr
;
4550 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4551 if (linktype
== DLT_ARCNET
)
4552 return gen_ahostop(eaddr
, (int)q
.dir
);
4554 bpf_error("ARCnet address used in non-arc expression");
4558 static struct block
*
4559 gen_ahostop(eaddr
, dir
)
4560 register const u_char
*eaddr
;
4563 register struct block
*b0
, *b1
;
4566 /* src comes first, different from Ethernet */
4568 return gen_bcmp(0, 1, eaddr
);
4571 return gen_bcmp(1, 1, eaddr
);
4574 b0
= gen_ahostop(eaddr
, Q_SRC
);
4575 b1
= gen_ahostop(eaddr
, Q_DST
);
4581 b0
= gen_ahostop(eaddr
, Q_SRC
);
4582 b1
= gen_ahostop(eaddr
, Q_DST
);
4591 * support IEEE 802.1Q VLAN trunk over ethernet
4600 * Change the offsets to point to the type and data fields within
4601 * the VLAN packet. This is somewhat of a kludge.
4603 if (orig_nl
== (u_int
)-1) {
4604 orig_linktype
= off_linktype
; /* save original values */
4606 orig_nl_nosnap
= off_nl_nosnap
;
4617 bpf_error("no VLAN support for data link type %d",
4623 /* check for VLAN */
4624 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4626 /* If a specific VLAN is requested, check VLAN id */
4627 if (vlan_num
>= 0) {
4630 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
4639 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
4651 bpf_error("'vpi' supported only on raw ATM");
4654 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
4655 (u_int
)jvalue
, reverse
);
4660 bpf_error("'vci' supported only on raw ATM");
4663 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
4664 (u_int
)jvalue
, reverse
);
4668 if (off_proto
== -1)
4669 abort(); /* XXX - this isn't on FreeBSD */
4670 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
4671 (u_int
)jvalue
, reverse
);
4675 if (off_msg_type
== -1)
4677 b0
= gen_ncmp(BPF_B
, off_msg_type
, 0xffffffff,
4678 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
4683 bpf_error("'callref' supported only on raw ATM");
4684 if (off_proto
== -1)
4686 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
4687 (u_int
)jvalue
, reverse
);
4697 gen_atmtype_abbrev(type
)
4700 struct block
*b0
, *b1
;
4705 /* Get all packets in Meta signalling Circuit */
4707 bpf_error("'metac' supported only on raw ATM");
4708 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4709 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
4714 /* Get all packets in Broadcast Circuit*/
4716 bpf_error("'bcc' supported only on raw ATM");
4717 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4718 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
4723 /* Get all cells in Segment OAM F4 circuit*/
4725 bpf_error("'oam4sc' supported only on raw ATM");
4726 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4727 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4732 /* Get all cells in End-to-End OAM F4 Circuit*/
4734 bpf_error("'oam4ec' supported only on raw ATM");
4735 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4736 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4741 /* Get all packets in connection Signalling Circuit */
4743 bpf_error("'sc' supported only on raw ATM");
4744 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4745 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
4750 /* Get all packets in ILMI Circuit */
4752 bpf_error("'ilmic' supported only on raw ATM");
4753 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4754 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
4759 /* Get all LANE packets */
4761 bpf_error("'lane' supported only on raw ATM");
4762 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
4766 /* Get all LLC-encapsulated packets */
4768 bpf_error("'llc' supported only on raw ATM");
4769 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
4779 static struct block
*
4780 gen_msg_abbrev(type
)
4786 * Q.2931 signalling protocol messages for handling virtual circuits
4787 * establishment and teardown
4792 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
4796 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
4800 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
4804 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
4808 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
4811 case A_RELEASE_DONE
:
4812 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
4822 gen_atmmulti_abbrev(type
)
4825 struct block
*b0
, *b1
;
4831 bpf_error("'oam' supported only on raw ATM");
4832 b1
= gen_atmmulti_abbrev(A_OAMF4
);
4837 bpf_error("'oamf4' supported only on raw ATM");
4839 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4840 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4842 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4848 * Get Q.2931 signalling messages for switched
4849 * virtual connection
4852 bpf_error("'connectmsg' supported only on raw ATM");
4853 b0
= gen_msg_abbrev(A_SETUP
);
4854 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4856 b0
= gen_msg_abbrev(A_CONNECT
);
4858 b0
= gen_msg_abbrev(A_CONNECTACK
);
4860 b0
= gen_msg_abbrev(A_RELEASE
);
4862 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4864 b0
= gen_atmtype_abbrev(A_SC
);
4870 bpf_error("'metaconnect' supported only on raw ATM");
4871 b0
= gen_msg_abbrev(A_SETUP
);
4872 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4874 b0
= gen_msg_abbrev(A_CONNECT
);
4876 b0
= gen_msg_abbrev(A_RELEASE
);
4878 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4880 b0
= gen_atmtype_abbrev(A_METAC
);