]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
bfe680b2959ad1f3908ff108e44342c98a08258f
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.178 2002-08-08 11:07:27 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
;
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 * Generate the tests to be done for data frames
1923 * First, check for To DS set, i.e. check "link[1] & 0x01".
1925 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1927 b1
= new_block(JMP(BPF_JSET
));
1928 b1
->s
.k
= 0x01; /* To DS */
1932 * If To DS is set, the SA is at 24.
1934 b0
= gen_bcmp(24, 6, eaddr
);
1938 * Now, check for To DS not set, i.e. check
1939 * "!(link[1] & 0x01)".
1941 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1943 b2
= new_block(JMP(BPF_JSET
));
1944 b2
->s
.k
= 0x01; /* To DS */
1949 * If To DS is not set, the SA is at 16.
1951 b1
= gen_bcmp(16, 6, eaddr
);
1955 * Now OR together the last two checks. That gives
1956 * the complete set of checks for data frames with
1962 * Now check for From DS being set, and AND that with
1963 * the ORed-together checks.
1965 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1967 b1
= new_block(JMP(BPF_JSET
));
1968 b1
->s
.k
= 0x02; /* From DS */
1973 * Now check for data frames with From DS not set.
1975 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1977 b2
= new_block(JMP(BPF_JSET
));
1978 b2
->s
.k
= 0x02; /* From DS */
1983 * If From DS isn't set, the SA is at 10.
1985 b1
= gen_bcmp(10, 6, eaddr
);
1989 * Now OR together the checks for data frames with
1990 * From DS not set and for data frames with From DS
1991 * set; that gives the checks done for data frames.
1996 * Now check for a data frame.
1997 * I.e, check "link[0] & 0x08".
1999 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2001 b1
= new_block(JMP(BPF_JSET
));
2006 * AND that with the checks done for data frames.
2011 * If the high-order bit of the type value is 0, this
2012 * is a management frame.
2013 * I.e, check "!(link[0] & 0x08)".
2015 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2017 b2
= new_block(JMP(BPF_JSET
));
2023 * For management frames, the SA is at 10.
2025 b1
= gen_bcmp(10, 6, eaddr
);
2029 * OR that with the checks done for data frames.
2030 * That gives the checks done for management and
2036 * If the low-order bit of the type value is 1,
2037 * this is either a control frame or a frame
2038 * with a reserved type, and thus not a
2041 * I.e., check "!(link[0] & 0x04)".
2043 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2045 b1
= new_block(JMP(BPF_JSET
));
2051 * AND that with the checks for data and management
2061 * For control frames, there is no DA.
2063 * For management frames, DA is at an
2064 * offset of 4 from the beginning of
2067 * For data frames, DA is at an offset
2068 * of 4 from the beginning of the packet
2069 * if To DS is clear and at an offset of
2070 * 16 from the beginning of the packet
2075 * Generate the tests to be done for data frames.
2077 * First, check for To DS set, i.e. "link[1] & 0x01".
2079 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2081 b1
= new_block(JMP(BPF_JSET
));
2082 b1
->s
.k
= 0x01; /* To DS */
2086 * If To DS is set, the DA is at 16.
2088 b0
= gen_bcmp(16, 6, eaddr
);
2092 * Now, check for To DS not set, i.e. check
2093 * "!(link[1] & 0x01)".
2095 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2097 b2
= new_block(JMP(BPF_JSET
));
2098 b2
->s
.k
= 0x01; /* To DS */
2103 * If To DS is not set, the DA is at 4.
2105 b1
= gen_bcmp(4, 6, eaddr
);
2109 * Now OR together the last two checks. That gives
2110 * the complete set of checks for data frames.
2115 * Now check for a data frame.
2116 * I.e, check "link[0] & 0x08".
2118 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2120 b1
= new_block(JMP(BPF_JSET
));
2125 * AND that with the checks done for data frames.
2130 * If the high-order bit of the type value is 0, this
2131 * is a management frame.
2132 * I.e, check "!(link[0] & 0x08)".
2134 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2136 b2
= new_block(JMP(BPF_JSET
));
2142 * For management frames, the DA is at 4.
2144 b1
= gen_bcmp(4, 6, eaddr
);
2148 * OR that with the checks done for data frames.
2149 * That gives the checks done for management and
2155 * If the low-order bit of the type value is 1,
2156 * this is either a control frame or a frame
2157 * with a reserved type, and thus not a
2160 * I.e., check "!(link[0] & 0x04)".
2162 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2164 b1
= new_block(JMP(BPF_JSET
));
2170 * AND that with the checks for data and management
2177 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2178 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2184 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2185 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2194 * This is quite tricky because there may be pad bytes in front of the
2195 * DECNET header, and then there are two possible data packet formats that
2196 * carry both src and dst addresses, plus 5 packet types in a format that
2197 * carries only the src node, plus 2 types that use a different format and
2198 * also carry just the src node.
2202 * Instead of doing those all right, we just look for data packets with
2203 * 0 or 1 bytes of padding. If you want to look at other packets, that
2204 * will require a lot more hacking.
2206 * To add support for filtering on DECNET "areas" (network numbers)
2207 * one would want to add a "mask" argument to this routine. That would
2208 * make the filter even more inefficient, although one could be clever
2209 * and not generate masking instructions if the mask is 0xFFFF.
2211 static struct block
*
2212 gen_dnhostop(addr
, dir
, base_off
)
2217 struct block
*b0
, *b1
, *b2
, *tmp
;
2218 u_int offset_lh
; /* offset if long header is received */
2219 u_int offset_sh
; /* offset if short header is received */
2224 offset_sh
= 1; /* follows flags */
2225 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2229 offset_sh
= 3; /* follows flags, dstnode */
2230 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2234 /* Inefficient because we do our Calvinball dance twice */
2235 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2236 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2242 /* Inefficient because we do our Calvinball dance twice */
2243 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2244 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2249 bpf_error("ISO host filtering not implemented");
2254 b0
= gen_linktype(ETHERTYPE_DN
);
2255 /* Check for pad = 1, long header case */
2256 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2257 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2258 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2259 BPF_H
, (bpf_int32
)ntohs(addr
));
2261 /* Check for pad = 0, long header case */
2262 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2263 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2266 /* Check for pad = 1, short header case */
2267 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2268 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2269 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2270 BPF_H
, (bpf_int32
)ntohs(addr
));
2273 /* Check for pad = 0, short header case */
2274 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2275 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2279 /* Combine with test for linktype */
2284 static struct block
*
2285 gen_host(addr
, mask
, proto
, dir
)
2291 struct block
*b0
, *b1
;
2296 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2297 if (off_linktype
!= -1) {
2298 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2300 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2306 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2307 off_nl
+ 12, off_nl
+ 16);
2310 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2311 off_nl
+ 14, off_nl
+ 24);
2314 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2315 off_nl
+ 14, off_nl
+ 24);
2318 bpf_error("'tcp' modifier applied to host");
2321 bpf_error("'sctp' modifier applied to host");
2324 bpf_error("'udp' modifier applied to host");
2327 bpf_error("'icmp' modifier applied to host");
2330 bpf_error("'igmp' modifier applied to host");
2333 bpf_error("'igrp' modifier applied to host");
2336 bpf_error("'pim' modifier applied to host");
2339 bpf_error("'vrrp' modifier applied to host");
2342 bpf_error("ATALK host filtering not implemented");
2345 bpf_error("AARP host filtering not implemented");
2348 return gen_dnhostop(addr
, dir
, off_nl
);
2351 bpf_error("SCA host filtering not implemented");
2354 bpf_error("LAT host filtering not implemented");
2357 bpf_error("MOPDL host filtering not implemented");
2360 bpf_error("MOPRC host filtering not implemented");
2364 bpf_error("'ip6' modifier applied to ip host");
2367 bpf_error("'icmp6' modifier applied to host");
2371 bpf_error("'ah' modifier applied to host");
2374 bpf_error("'esp' modifier applied to host");
2377 bpf_error("ISO host filtering not implemented");
2380 bpf_error("'esis' modifier applied to host");
2383 bpf_error("'isis' modifier applied to host");
2386 bpf_error("'clnp' modifier applied to host");
2389 bpf_error("'stp' modifier applied to host");
2392 bpf_error("IPX host filtering not implemented");
2395 bpf_error("'netbeui' modifier applied to host");
2404 static struct block
*
2405 gen_host6(addr
, mask
, proto
, dir
)
2406 struct in6_addr
*addr
;
2407 struct in6_addr
*mask
;
2414 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2417 bpf_error("'ip' modifier applied to ip6 host");
2420 bpf_error("'rarp' modifier applied to ip6 host");
2423 bpf_error("'arp' modifier applied to ip6 host");
2426 bpf_error("'sctp' modifier applied to host");
2429 bpf_error("'tcp' modifier applied to host");
2432 bpf_error("'udp' modifier applied to host");
2435 bpf_error("'icmp' modifier applied to host");
2438 bpf_error("'igmp' modifier applied to host");
2441 bpf_error("'igrp' modifier applied to host");
2444 bpf_error("'pim' modifier applied to host");
2447 bpf_error("'vrrp' modifier applied to host");
2450 bpf_error("ATALK host filtering not implemented");
2453 bpf_error("AARP host filtering not implemented");
2456 bpf_error("'decnet' modifier applied to ip6 host");
2459 bpf_error("SCA host filtering not implemented");
2462 bpf_error("LAT host filtering not implemented");
2465 bpf_error("MOPDL host filtering not implemented");
2468 bpf_error("MOPRC host filtering not implemented");
2471 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2472 off_nl
+ 8, off_nl
+ 24);
2475 bpf_error("'icmp6' modifier applied to host");
2478 bpf_error("'ah' modifier applied to host");
2481 bpf_error("'esp' modifier applied to host");
2484 bpf_error("ISO host filtering not implemented");
2487 bpf_error("'esis' modifier applied to host");
2490 bpf_error("'isis' modifier applied to host");
2493 bpf_error("'clnp' modifier applied to host");
2496 bpf_error("'stp' modifier applied to host");
2499 bpf_error("IPX host filtering not implemented");
2502 bpf_error("'netbeui' modifier applied to host");
2512 static struct block
*
2513 gen_gateway(eaddr
, alist
, proto
, dir
)
2514 const u_char
*eaddr
;
2515 bpf_u_int32
**alist
;
2519 struct block
*b0
, *b1
, *tmp
;
2522 bpf_error("direction applied to 'gateway'");
2529 if (linktype
== DLT_EN10MB
)
2530 b0
= gen_ehostop(eaddr
, Q_OR
);
2531 else if (linktype
== DLT_FDDI
)
2532 b0
= gen_fhostop(eaddr
, Q_OR
);
2533 else if (linktype
== DLT_IEEE802
)
2534 b0
= gen_thostop(eaddr
, Q_OR
);
2535 else if (linktype
== DLT_IEEE802_11
)
2536 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2539 "'gateway' supported only on ethernet/FDDI/token ring/802.11");
2541 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2543 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2551 bpf_error("illegal modifier of 'gateway'");
2557 gen_proto_abbrev(proto
)
2568 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2570 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2576 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2578 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2584 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2586 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2592 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2595 #ifndef IPPROTO_IGMP
2596 #define IPPROTO_IGMP 2
2600 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2603 #ifndef IPPROTO_IGRP
2604 #define IPPROTO_IGRP 9
2607 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2611 #define IPPROTO_PIM 103
2615 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2617 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2622 #ifndef IPPROTO_VRRP
2623 #define IPPROTO_VRRP 112
2627 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2631 b1
= gen_linktype(ETHERTYPE_IP
);
2635 b1
= gen_linktype(ETHERTYPE_ARP
);
2639 b1
= gen_linktype(ETHERTYPE_REVARP
);
2643 bpf_error("link layer applied in wrong context");
2646 b1
= gen_linktype(ETHERTYPE_ATALK
);
2650 b1
= gen_linktype(ETHERTYPE_AARP
);
2654 b1
= gen_linktype(ETHERTYPE_DN
);
2658 b1
= gen_linktype(ETHERTYPE_SCA
);
2662 b1
= gen_linktype(ETHERTYPE_LAT
);
2666 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2670 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2675 b1
= gen_linktype(ETHERTYPE_IPV6
);
2678 #ifndef IPPROTO_ICMPV6
2679 #define IPPROTO_ICMPV6 58
2682 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2687 #define IPPROTO_AH 51
2690 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2692 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2698 #define IPPROTO_ESP 50
2701 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2703 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2709 b1
= gen_linktype(LLCSAP_ISONS
);
2713 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2717 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2721 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2725 b1
= gen_linktype(LLCSAP_8021D
);
2729 b1
= gen_linktype(LLCSAP_IPX
);
2733 b1
= gen_linktype(LLCSAP_NETBEUI
);
2742 static struct block
*
2749 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2750 s
->s
.k
= off_nl
+ 6;
2751 b
= new_block(JMP(BPF_JSET
));
2759 static struct block
*
2760 gen_portatom(off
, v
)
2767 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2770 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2771 s
->next
->s
.k
= off_nl
+ off
;
2773 b
= new_block(JMP(BPF_JEQ
));
2781 static struct block
*
2782 gen_portatom6(off
, v
)
2786 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2791 gen_portop(port
, proto
, dir
)
2792 int port
, proto
, dir
;
2794 struct block
*b0
, *b1
, *tmp
;
2796 /* ip proto 'proto' */
2797 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2803 b1
= gen_portatom(0, (bpf_int32
)port
);
2807 b1
= gen_portatom(2, (bpf_int32
)port
);
2812 tmp
= gen_portatom(0, (bpf_int32
)port
);
2813 b1
= gen_portatom(2, (bpf_int32
)port
);
2818 tmp
= gen_portatom(0, (bpf_int32
)port
);
2819 b1
= gen_portatom(2, (bpf_int32
)port
);
2831 static struct block
*
2832 gen_port(port
, ip_proto
, dir
)
2837 struct block
*b0
, *b1
, *tmp
;
2839 /* ether proto ip */
2840 b0
= gen_linktype(ETHERTYPE_IP
);
2846 b1
= gen_portop(port
, ip_proto
, dir
);
2850 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2851 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2853 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2866 gen_portop6(port
, proto
, dir
)
2867 int port
, proto
, dir
;
2869 struct block
*b0
, *b1
, *tmp
;
2871 /* ip proto 'proto' */
2872 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2876 b1
= gen_portatom6(0, (bpf_int32
)port
);
2880 b1
= gen_portatom6(2, (bpf_int32
)port
);
2885 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2886 b1
= gen_portatom6(2, (bpf_int32
)port
);
2891 tmp
= gen_portatom6(0, (bpf_int32
)port
);
2892 b1
= gen_portatom6(2, (bpf_int32
)port
);
2904 static struct block
*
2905 gen_port6(port
, ip_proto
, dir
)
2910 struct block
*b0
, *b1
, *tmp
;
2912 /* ether proto ip */
2913 b0
= gen_linktype(ETHERTYPE_IPV6
);
2919 b1
= gen_portop6(port
, ip_proto
, dir
);
2923 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
2924 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
2926 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
2939 lookup_proto(name
, proto
)
2940 register const char *name
;
2950 v
= pcap_nametoproto(name
);
2951 if (v
== PROTO_UNDEF
)
2952 bpf_error("unknown ip proto '%s'", name
);
2956 /* XXX should look up h/w protocol type based on linktype */
2957 v
= pcap_nametoeproto(name
);
2958 if (v
== PROTO_UNDEF
)
2959 bpf_error("unknown ether proto '%s'", name
);
2963 if (strcmp(name
, "esis") == 0)
2965 else if (strcmp(name
, "isis") == 0)
2967 else if (strcmp(name
, "clnp") == 0)
2970 bpf_error("unknown osi proto '%s'", name
);
2990 static struct block
*
2991 gen_protochain(v
, proto
, dir
)
2996 #ifdef NO_PROTOCHAIN
2997 return gen_proto(v
, proto
, dir
);
2999 struct block
*b0
, *b
;
3000 struct slist
*s
[100];
3001 int fix2
, fix3
, fix4
, fix5
;
3002 int ahcheck
, again
, end
;
3004 int reg2
= alloc_reg();
3006 memset(s
, 0, sizeof(s
));
3007 fix2
= fix3
= fix4
= fix5
= 0;
3014 b0
= gen_protochain(v
, Q_IP
, dir
);
3015 b
= gen_protochain(v
, Q_IPV6
, dir
);
3019 bpf_error("bad protocol applied for 'protochain'");
3023 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3026 * s[0] is a dummy entry to protect other BPF insn from damaged
3027 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3028 * hard to find interdependency made by jump table fixup.
3031 s
[i
] = new_stmt(0); /*dummy*/
3036 b0
= gen_linktype(ETHERTYPE_IP
);
3039 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3040 s
[i
]->s
.k
= off_nl
+ 9;
3042 /* X = ip->ip_hl << 2 */
3043 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3049 b0
= gen_linktype(ETHERTYPE_IPV6
);
3051 /* A = ip6->ip_nxt */
3052 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3053 s
[i
]->s
.k
= off_nl
+ 6;
3055 /* X = sizeof(struct ip6_hdr) */
3056 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3062 bpf_error("unsupported proto to gen_protochain");
3066 /* again: if (A == v) goto end; else fall through; */
3068 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3070 s
[i
]->s
.jt
= NULL
; /*later*/
3071 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3075 #ifndef IPPROTO_NONE
3076 #define IPPROTO_NONE 59
3078 /* if (A == IPPROTO_NONE) goto end */
3079 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3080 s
[i
]->s
.jt
= NULL
; /*later*/
3081 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3082 s
[i
]->s
.k
= IPPROTO_NONE
;
3083 s
[fix5
]->s
.jf
= s
[i
];
3088 if (proto
== Q_IPV6
) {
3089 int v6start
, v6end
, v6advance
, j
;
3092 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3093 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3094 s
[i
]->s
.jt
= NULL
; /*later*/
3095 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3096 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3097 s
[fix2
]->s
.jf
= s
[i
];
3099 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
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
; /*update in next stmt*/
3103 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3105 /* if (A == IPPROTO_ROUTING) goto v6advance */
3106 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3107 s
[i
]->s
.jt
= NULL
; /*later*/
3108 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3109 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3111 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3112 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3113 s
[i
]->s
.jt
= NULL
; /*later*/
3114 s
[i
]->s
.jf
= NULL
; /*later*/
3115 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3126 * X = X + (P[X + 1] + 1) * 8;
3129 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3131 /* A = P[X + packet head] */
3132 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3136 s
[i
] = new_stmt(BPF_ST
);
3140 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3143 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3147 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3149 /* A = P[X + packet head]; */
3150 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3154 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3158 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3162 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3165 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3169 /* goto again; (must use BPF_JA for backward jump) */
3170 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3171 s
[i
]->s
.k
= again
- i
- 1;
3172 s
[i
- 1]->s
.jf
= s
[i
];
3176 for (j
= v6start
; j
<= v6end
; j
++)
3177 s
[j
]->s
.jt
= s
[v6advance
];
3182 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3184 s
[fix2
]->s
.jf
= s
[i
];
3190 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3191 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3192 s
[i
]->s
.jt
= NULL
; /*later*/
3193 s
[i
]->s
.jf
= NULL
; /*later*/
3194 s
[i
]->s
.k
= IPPROTO_AH
;
3196 s
[fix3
]->s
.jf
= s
[ahcheck
];
3203 * X = X + (P[X + 1] + 2) * 4;
3206 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3208 /* A = P[X + packet head]; */
3209 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3213 s
[i
] = new_stmt(BPF_ST
);
3217 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3220 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3224 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3226 /* A = P[X + packet head] */
3227 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3231 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3235 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3239 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3242 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3246 /* goto again; (must use BPF_JA for backward jump) */
3247 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3248 s
[i
]->s
.k
= again
- i
- 1;
3253 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3255 s
[fix2
]->s
.jt
= s
[end
];
3256 s
[fix4
]->s
.jf
= s
[end
];
3257 s
[fix5
]->s
.jt
= s
[end
];
3264 for (i
= 0; i
< max
- 1; i
++)
3265 s
[i
]->next
= s
[i
+ 1];
3266 s
[max
- 1]->next
= NULL
;
3271 b
= new_block(JMP(BPF_JEQ
));
3272 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3282 static struct block
*
3283 gen_proto(v
, proto
, dir
)
3288 struct block
*b0
, *b1
;
3290 if (dir
!= Q_DEFAULT
)
3291 bpf_error("direction applied to 'proto'");
3296 b0
= gen_proto(v
, Q_IP
, dir
);
3297 b1
= gen_proto(v
, Q_IPV6
, dir
);
3304 b0
= gen_linktype(ETHERTYPE_IP
);
3306 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3308 b1
= gen_protochain(v
, Q_IP
);
3318 * Frame Relay packets typically have an OSI
3319 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3320 * generates code to check for all the OSI
3321 * NLPIDs, so calling it and then adding a check
3322 * for the particular NLPID for which we're
3323 * looking is bogus, as we can just check for
3326 * What we check for is the NLPID and a frame
3327 * control field value of UI, i.e. 0x03 followed
3330 * XXX - assumes a 2-byte Frame Relay header with
3331 * DLCI and flags. What if the address is longer?
3333 * XXX - what about SNAP-encapsulated frames?
3335 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3339 b0
= gen_linktype(LLCSAP_ISONS
);
3340 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3346 bpf_error("arp does not encapsulate another protocol");
3350 bpf_error("rarp does not encapsulate another protocol");
3354 bpf_error("atalk encapsulation is not specifiable");
3358 bpf_error("decnet encapsulation is not specifiable");
3362 bpf_error("sca does not encapsulate another protocol");
3366 bpf_error("lat does not encapsulate another protocol");
3370 bpf_error("moprc does not encapsulate another protocol");
3374 bpf_error("mopdl does not encapsulate another protocol");
3378 return gen_linktype(v
);
3381 bpf_error("'udp proto' is bogus");
3385 bpf_error("'tcp proto' is bogus");
3389 bpf_error("'sctp proto' is bogus");
3393 bpf_error("'icmp proto' is bogus");
3397 bpf_error("'igmp proto' is bogus");
3401 bpf_error("'igrp proto' is bogus");
3405 bpf_error("'pim proto' is bogus");
3409 bpf_error("'vrrp proto' is bogus");
3414 b0
= gen_linktype(ETHERTYPE_IPV6
);
3416 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3418 b1
= gen_protochain(v
, Q_IPV6
);
3424 bpf_error("'icmp6 proto' is bogus");
3428 bpf_error("'ah proto' is bogus");
3431 bpf_error("'ah proto' is bogus");
3434 bpf_error("'stp proto' is bogus");
3437 bpf_error("'ipx proto' is bogus");
3440 bpf_error("'netbeui proto' is bogus");
3451 register const char *name
;
3454 int proto
= q
.proto
;
3458 bpf_u_int32 mask
, addr
;
3460 bpf_u_int32
**alist
;
3463 struct sockaddr_in
*sin
;
3464 struct sockaddr_in6
*sin6
;
3465 struct addrinfo
*res
, *res0
;
3466 struct in6_addr mask128
;
3468 struct block
*b
, *tmp
;
3469 int port
, real_proto
;
3474 addr
= pcap_nametonetaddr(name
);
3476 bpf_error("unknown network '%s'", name
);
3477 /* Left justify network addr and calculate its network mask */
3479 while (addr
&& (addr
& 0xff000000) == 0) {
3483 return gen_host(addr
, mask
, proto
, dir
);
3487 if (proto
== Q_LINK
) {
3491 eaddr
= pcap_ether_hostton(name
);
3494 "unknown ether host '%s'", name
);
3495 b
= gen_ehostop(eaddr
, dir
);
3500 eaddr
= pcap_ether_hostton(name
);
3503 "unknown FDDI host '%s'", name
);
3504 b
= gen_fhostop(eaddr
, dir
);
3509 eaddr
= pcap_ether_hostton(name
);
3512 "unknown token ring host '%s'", name
);
3513 b
= gen_thostop(eaddr
, dir
);
3517 case DLT_IEEE802_11
:
3518 eaddr
= pcap_ether_hostton(name
);
3521 "unknown 802.11 host '%s'", name
);
3522 b
= gen_wlanhostop(eaddr
, dir
);
3528 "only ethernet/FDDI/token ring/802.11 supports link-level host name");
3531 } else if (proto
== Q_DECNET
) {
3532 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3534 * I don't think DECNET hosts can be multihomed, so
3535 * there is no need to build up a list of addresses
3537 return (gen_host(dn_addr
, 0, proto
, dir
));
3540 alist
= pcap_nametoaddr(name
);
3541 if (alist
== NULL
|| *alist
== NULL
)
3542 bpf_error("unknown host '%s'", name
);
3544 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3546 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3548 tmp
= gen_host(**alist
++, 0xffffffff,
3555 memset(&mask128
, 0xff, sizeof(mask128
));
3556 res0
= res
= pcap_nametoaddrinfo(name
);
3558 bpf_error("unknown host '%s'", name
);
3560 tproto
= tproto6
= proto
;
3561 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3565 for (res
= res0
; res
; res
= res
->ai_next
) {
3566 switch (res
->ai_family
) {
3568 if (tproto
== Q_IPV6
)
3571 sin
= (struct sockaddr_in
*)
3573 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3574 0xffffffff, tproto
, dir
);
3577 if (tproto6
== Q_IP
)
3580 sin6
= (struct sockaddr_in6
*)
3582 tmp
= gen_host6(&sin6
->sin6_addr
,
3583 &mask128
, tproto6
, dir
);
3594 bpf_error("unknown host '%s'%s", name
,
3595 (proto
== Q_DEFAULT
)
3597 : " for specified address family");
3604 if (proto
!= Q_DEFAULT
&&
3605 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3606 bpf_error("illegal qualifier of 'port'");
3607 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3608 bpf_error("unknown port '%s'", name
);
3609 if (proto
== Q_UDP
) {
3610 if (real_proto
== IPPROTO_TCP
)
3611 bpf_error("port '%s' is tcp", name
);
3612 else if (real_proto
== IPPROTO_SCTP
)
3613 bpf_error("port '%s' is sctp", name
);
3615 /* override PROTO_UNDEF */
3616 real_proto
= IPPROTO_UDP
;
3618 if (proto
== Q_TCP
) {
3619 if (real_proto
== IPPROTO_UDP
)
3620 bpf_error("port '%s' is udp", name
);
3622 else if (real_proto
== IPPROTO_SCTP
)
3623 bpf_error("port '%s' is sctp", name
);
3625 /* override PROTO_UNDEF */
3626 real_proto
= IPPROTO_TCP
;
3628 if (proto
== Q_SCTP
) {
3629 if (real_proto
== IPPROTO_UDP
)
3630 bpf_error("port '%s' is udp", name
);
3632 else if (real_proto
== IPPROTO_TCP
)
3633 bpf_error("port '%s' is tcp", name
);
3635 /* override PROTO_UNDEF */
3636 real_proto
= IPPROTO_SCTP
;
3639 return gen_port(port
, real_proto
, dir
);
3643 b
= gen_port(port
, real_proto
, dir
);
3644 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3651 eaddr
= pcap_ether_hostton(name
);
3653 bpf_error("unknown ether host: %s", name
);
3655 alist
= pcap_nametoaddr(name
);
3656 if (alist
== NULL
|| *alist
== NULL
)
3657 bpf_error("unknown host '%s'", name
);
3658 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3662 bpf_error("'gateway' not supported in this configuration");
3666 real_proto
= lookup_proto(name
, proto
);
3667 if (real_proto
>= 0)
3668 return gen_proto(real_proto
, proto
, dir
);
3670 bpf_error("unknown protocol: %s", name
);
3673 real_proto
= lookup_proto(name
, proto
);
3674 if (real_proto
>= 0)
3675 return gen_protochain(real_proto
, proto
, dir
);
3677 bpf_error("unknown protocol: %s", name
);
3689 gen_mcode(s1
, s2
, masklen
, q
)
3690 register const char *s1
, *s2
;
3691 register int masklen
;
3694 register int nlen
, mlen
;
3697 nlen
= __pcap_atoin(s1
, &n
);
3698 /* Promote short ipaddr */
3702 mlen
= __pcap_atoin(s2
, &m
);
3703 /* Promote short ipaddr */
3706 bpf_error("non-network bits set in \"%s mask %s\"",
3709 /* Convert mask len to mask */
3711 bpf_error("mask length must be <= 32");
3712 m
= 0xffffffff << (32 - masklen
);
3714 bpf_error("non-network bits set in \"%s/%d\"",
3721 return gen_host(n
, m
, q
.proto
, q
.dir
);
3724 bpf_error("Mask syntax for networks only");
3731 register const char *s
;
3736 int proto
= q
.proto
;
3742 else if (q
.proto
== Q_DECNET
)
3743 vlen
= __pcap_atodn(s
, &v
);
3745 vlen
= __pcap_atoin(s
, &v
);
3752 if (proto
== Q_DECNET
)
3753 return gen_host(v
, 0, proto
, dir
);
3754 else if (proto
== Q_LINK
) {
3755 bpf_error("illegal link layer address");
3758 if (s
== NULL
&& q
.addr
== Q_NET
) {
3759 /* Promote short net number */
3760 while (v
&& (v
& 0xff000000) == 0) {
3765 /* Promote short ipaddr */
3769 return gen_host(v
, mask
, proto
, dir
);
3774 proto
= IPPROTO_UDP
;
3775 else if (proto
== Q_TCP
)
3776 proto
= IPPROTO_TCP
;
3777 else if (proto
== Q_SCTP
)
3778 proto
= IPPROTO_SCTP
;
3779 else if (proto
== Q_DEFAULT
)
3780 proto
= PROTO_UNDEF
;
3782 bpf_error("illegal qualifier of 'port'");
3785 return gen_port((int)v
, proto
, dir
);
3789 b
= gen_port((int)v
, proto
, dir
);
3790 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3796 bpf_error("'gateway' requires a name");
3800 return gen_proto((int)v
, proto
, dir
);
3803 return gen_protochain((int)v
, proto
, dir
);
3818 gen_mcode6(s1
, s2
, masklen
, q
)
3819 register const char *s1
, *s2
;
3820 register int masklen
;
3823 struct addrinfo
*res
;
3824 struct in6_addr
*addr
;
3825 struct in6_addr mask
;
3830 bpf_error("no mask %s supported", s2
);
3832 res
= pcap_nametoaddrinfo(s1
);
3834 bpf_error("invalid ip6 address %s", s1
);
3836 bpf_error("%s resolved to multiple address", s1
);
3837 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3839 if (sizeof(mask
) * 8 < masklen
)
3840 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3841 memset(&mask
, 0, sizeof(mask
));
3842 memset(&mask
, 0xff, masklen
/ 8);
3844 mask
.s6_addr
[masklen
/ 8] =
3845 (0xff << (8 - masklen
% 8)) & 0xff;
3848 a
= (u_int32_t
*)addr
;
3849 m
= (u_int32_t
*)&mask
;
3850 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3851 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3852 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
3860 bpf_error("Mask syntax for networks only");
3864 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
3869 bpf_error("invalid qualifier against IPv6 address");
3877 register const u_char
*eaddr
;
3880 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
3881 if (linktype
== DLT_EN10MB
)
3882 return gen_ehostop(eaddr
, (int)q
.dir
);
3883 if (linktype
== DLT_FDDI
)
3884 return gen_fhostop(eaddr
, (int)q
.dir
);
3885 if (linktype
== DLT_IEEE802
)
3886 return gen_thostop(eaddr
, (int)q
.dir
);
3887 if (linktype
== DLT_IEEE802_11
)
3888 return gen_wlanhostop(eaddr
, (int)q
.dir
);
3889 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11");
3891 bpf_error("ethernet address used in non-ether expression");
3897 struct slist
*s0
, *s1
;
3900 * This is definitely not the best way to do this, but the
3901 * lists will rarely get long.
3908 static struct slist
*
3914 s
= new_stmt(BPF_LDX
|BPF_MEM
);
3919 static struct slist
*
3925 s
= new_stmt(BPF_LD
|BPF_MEM
);
3931 gen_load(proto
, index
, size
)
3936 struct slist
*s
, *tmp
;
3938 int regno
= alloc_reg();
3940 free_reg(index
->regno
);
3944 bpf_error("data size must be 1, 2, or 4");
3960 bpf_error("unsupported index operation");
3963 s
= xfer_to_x(index
);
3964 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3966 sappend(index
->s
, s
);
3981 /* XXX Note that we assume a fixed link header here. */
3982 s
= xfer_to_x(index
);
3983 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
3986 sappend(index
->s
, s
);
3988 b
= gen_proto_abbrev(proto
);
3990 gen_and(index
->b
, b
);
4002 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4004 sappend(s
, xfer_to_a(index
));
4005 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4006 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4007 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4009 sappend(index
->s
, s
);
4011 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4013 gen_and(index
->b
, b
);
4015 gen_and(gen_proto_abbrev(Q_IP
), b
);
4021 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4025 index
->regno
= regno
;
4026 s
= new_stmt(BPF_ST
);
4028 sappend(index
->s
, s
);
4034 gen_relation(code
, a0
, a1
, reversed
)
4036 struct arth
*a0
, *a1
;
4039 struct slist
*s0
, *s1
, *s2
;
4040 struct block
*b
, *tmp
;
4044 if (code
== BPF_JEQ
) {
4045 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4046 b
= new_block(JMP(code
));
4050 b
= new_block(BPF_JMP
|code
|BPF_X
);
4056 sappend(a0
->s
, a1
->s
);
4060 free_reg(a0
->regno
);
4061 free_reg(a1
->regno
);
4063 /* 'and' together protocol checks */
4066 gen_and(a0
->b
, tmp
= a1
->b
);
4082 int regno
= alloc_reg();
4083 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4086 s
= new_stmt(BPF_LD
|BPF_LEN
);
4087 s
->next
= new_stmt(BPF_ST
);
4088 s
->next
->s
.k
= regno
;
4103 a
= (struct arth
*)newchunk(sizeof(*a
));
4107 s
= new_stmt(BPF_LD
|BPF_IMM
);
4109 s
->next
= new_stmt(BPF_ST
);
4125 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4128 s
= new_stmt(BPF_ST
);
4136 gen_arth(code
, a0
, a1
)
4138 struct arth
*a0
, *a1
;
4140 struct slist
*s0
, *s1
, *s2
;
4144 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4149 sappend(a0
->s
, a1
->s
);
4151 free_reg(a0
->regno
);
4152 free_reg(a1
->regno
);
4154 s0
= new_stmt(BPF_ST
);
4155 a0
->regno
= s0
->s
.k
= alloc_reg();
4162 * Here we handle simple allocation of the scratch registers.
4163 * If too many registers are alloc'd, the allocator punts.
4165 static int regused
[BPF_MEMWORDS
];
4169 * Return the next free register.
4174 int n
= BPF_MEMWORDS
;
4177 if (regused
[curreg
])
4178 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4180 regused
[curreg
] = 1;
4184 bpf_error("too many registers needed to evaluate expression");
4189 * Return a register to the table so it can
4199 static struct block
*
4206 s
= new_stmt(BPF_LD
|BPF_LEN
);
4207 b
= new_block(JMP(jmp
));
4218 return gen_len(BPF_JGE
, n
);
4222 * Actually, this is less than or equal.
4230 b
= gen_len(BPF_JGT
, n
);
4237 gen_byteop(op
, idx
, val
)
4248 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4251 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4252 b
->s
.code
= JMP(BPF_JGE
);
4257 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4258 b
->s
.code
= JMP(BPF_JGT
);
4262 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4266 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4270 b
= new_block(JMP(BPF_JEQ
));
4277 static u_char abroadcast
[] = { 0x0 };
4280 gen_broadcast(proto
)
4283 bpf_u_int32 hostmask
;
4284 struct block
*b0
, *b1
, *b2
;
4285 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4291 if (linktype
== DLT_ARCNET
)
4292 return gen_ahostop(abroadcast
, Q_DST
);
4293 if (linktype
== DLT_EN10MB
)
4294 return gen_ehostop(ebroadcast
, Q_DST
);
4295 if (linktype
== DLT_FDDI
)
4296 return gen_fhostop(ebroadcast
, Q_DST
);
4297 if (linktype
== DLT_IEEE802
)
4298 return gen_thostop(ebroadcast
, Q_DST
);
4299 if (linktype
== DLT_IEEE802_11
)
4300 return gen_wlanhostop(ebroadcast
, Q_DST
);
4301 bpf_error("not a broadcast link");
4305 b0
= gen_linktype(ETHERTYPE_IP
);
4306 hostmask
= ~netmask
;
4307 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4308 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4309 (bpf_int32
)(~0 & hostmask
), hostmask
);
4314 bpf_error("only ether/ip broadcast filters supported");
4318 * Generate code to test the low-order bit of a MAC address (that's
4319 * the bottom bit of the *first* byte).
4321 static struct block
*
4322 gen_mac_multicast(offset
)
4325 register struct block
*b0
;
4326 register struct slist
*s
;
4328 /* link[offset] & 1 != 0 */
4329 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4331 b0
= new_block(JMP(BPF_JSET
));
4338 gen_multicast(proto
)
4341 register struct block
*b0
, *b1
, *b2
;
4342 register struct slist
*s
;
4348 if (linktype
== DLT_ARCNET
)
4349 /* all ARCnet multicasts use the same address */
4350 return gen_ahostop(abroadcast
, Q_DST
);
4352 if (linktype
== DLT_EN10MB
) {
4353 /* ether[0] & 1 != 0 */
4354 return gen_mac_multicast(0);
4357 if (linktype
== DLT_FDDI
) {
4359 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4361 * XXX - was that referring to bit-order issues?
4363 /* fddi[1] & 1 != 0 */
4364 return gen_mac_multicast(1);
4367 if (linktype
== DLT_IEEE802
) {
4368 /* tr[2] & 1 != 0 */
4369 return gen_mac_multicast(2);
4372 if (linktype
== DLT_IEEE802_11
) {
4376 * For control frames, there is no DA.
4378 * For management frames, DA is at an
4379 * offset of 4 from the beginning of
4382 * For data frames, DA is at an offset
4383 * of 4 from the beginning of the packet
4384 * if To DS is clear and at an offset of
4385 * 16 from the beginning of the packet
4390 * Generate the tests to be done for data frames.
4392 * First, check for To DS set, i.e. "link[1] & 0x01".
4394 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4396 b1
= new_block(JMP(BPF_JSET
));
4397 b1
->s
.k
= 0x01; /* To DS */
4401 * If To DS is set, the DA is at 16.
4403 b0
= gen_mac_multicast(16);
4407 * Now, check for To DS not set, i.e. check
4408 * "!(link[1] & 0x01)".
4410 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4412 b2
= new_block(JMP(BPF_JSET
));
4413 b2
->s
.k
= 0x01; /* To DS */
4418 * If To DS is not set, the DA is at 4.
4420 b1
= gen_mac_multicast(4);
4424 * Now OR together the last two checks. That gives
4425 * the complete set of checks for data frames.
4430 * Now check for a data frame.
4431 * I.e, check "link[0] & 0x08".
4433 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4435 b1
= new_block(JMP(BPF_JSET
));
4440 * AND that with the checks done for data frames.
4445 * If the high-order bit of the type value is 0, this
4446 * is a management frame.
4447 * I.e, check "!(link[0] & 0x08)".
4449 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4451 b2
= new_block(JMP(BPF_JSET
));
4457 * For management frames, the DA is at 4.
4459 b1
= gen_mac_multicast(4);
4463 * OR that with the checks done for data frames.
4464 * That gives the checks done for management and
4470 * If the low-order bit of the type value is 1,
4471 * this is either a control frame or a frame
4472 * with a reserved type, and thus not a
4475 * I.e., check "!(link[0] & 0x04)".
4477 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4479 b1
= new_block(JMP(BPF_JSET
));
4485 * AND that with the checks for data and management
4492 /* Link not known to support multicasts */
4496 b0
= gen_linktype(ETHERTYPE_IP
);
4497 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4498 b1
->s
.code
= JMP(BPF_JGE
);
4504 b0
= gen_linktype(ETHERTYPE_IPV6
);
4505 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4510 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4514 * generate command for inbound/outbound. It's here so we can
4515 * make it link-type specific. 'dir' = 0 implies "inbound",
4516 * = 1 implies "outbound".
4522 register struct block
*b0
;
4525 * Only some data link types support inbound/outbound qualifiers.
4530 b0
= gen_relation(BPF_JEQ
,
4531 gen_load(Q_LINK
, gen_loadi(0), 1),
4539 * Match packets sent by this machine.
4541 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
4544 * Match packets sent to this machine.
4545 * (No broadcast or multicast packets, or
4546 * packets sent to some other machine and
4547 * received promiscuously.)
4549 * XXX - packets sent to other machines probably
4550 * shouldn't be matched, but what about broadcast
4551 * or multicast packets we received?
4553 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
4558 bpf_error("inbound/outbound not supported on linktype %d\n",
4568 register const u_char
*eaddr
;
4571 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4572 if (linktype
== DLT_ARCNET
)
4573 return gen_ahostop(eaddr
, (int)q
.dir
);
4575 bpf_error("ARCnet address used in non-arc expression");
4579 static struct block
*
4580 gen_ahostop(eaddr
, dir
)
4581 register const u_char
*eaddr
;
4584 register struct block
*b0
, *b1
;
4587 /* src comes first, different from Ethernet */
4589 return gen_bcmp(0, 1, eaddr
);
4592 return gen_bcmp(1, 1, eaddr
);
4595 b0
= gen_ahostop(eaddr
, Q_SRC
);
4596 b1
= gen_ahostop(eaddr
, Q_DST
);
4602 b0
= gen_ahostop(eaddr
, Q_SRC
);
4603 b1
= gen_ahostop(eaddr
, Q_DST
);
4612 * support IEEE 802.1Q VLAN trunk over ethernet
4621 * Change the offsets to point to the type and data fields within
4622 * the VLAN packet. This is somewhat of a kludge.
4624 if (orig_nl
== (u_int
)-1) {
4625 orig_linktype
= off_linktype
; /* save original values */
4627 orig_nl_nosnap
= off_nl_nosnap
;
4638 bpf_error("no VLAN support for data link type %d",
4644 /* check for VLAN */
4645 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4647 /* If a specific VLAN is requested, check VLAN id */
4648 if (vlan_num
>= 0) {
4651 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
4660 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
4672 bpf_error("'vpi' supported only on raw ATM");
4675 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
4676 (u_int
)jvalue
, reverse
);
4681 bpf_error("'vci' supported only on raw ATM");
4684 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
4685 (u_int
)jvalue
, reverse
);
4689 if (off_proto
== -1)
4690 abort(); /* XXX - this isn't on FreeBSD */
4691 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
4692 (u_int
)jvalue
, reverse
);
4696 if (off_msg_type
== -1)
4698 b0
= gen_ncmp(BPF_B
, off_msg_type
, 0xffffffff,
4699 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
4704 bpf_error("'callref' supported only on raw ATM");
4705 if (off_proto
== -1)
4707 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
4708 (u_int
)jvalue
, reverse
);
4718 gen_atmtype_abbrev(type
)
4721 struct block
*b0
, *b1
;
4726 /* Get all packets in Meta signalling Circuit */
4728 bpf_error("'metac' supported only on raw ATM");
4729 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4730 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
4735 /* Get all packets in Broadcast Circuit*/
4737 bpf_error("'bcc' supported only on raw ATM");
4738 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4739 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
4744 /* Get all cells in Segment OAM F4 circuit*/
4746 bpf_error("'oam4sc' supported only on raw ATM");
4747 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4748 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4753 /* Get all cells in End-to-End OAM F4 Circuit*/
4755 bpf_error("'oam4ec' supported only on raw ATM");
4756 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4757 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4762 /* Get all packets in connection Signalling Circuit */
4764 bpf_error("'sc' supported only on raw ATM");
4765 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4766 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
4771 /* Get all packets in ILMI Circuit */
4773 bpf_error("'ilmic' supported only on raw ATM");
4774 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4775 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
4780 /* Get all LANE packets */
4782 bpf_error("'lane' supported only on raw ATM");
4783 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
4787 /* Get all LLC-encapsulated packets */
4789 bpf_error("'llc' supported only on raw ATM");
4790 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
4800 static struct block
*
4801 gen_msg_abbrev(type
)
4807 * Q.2931 signalling protocol messages for handling virtual circuits
4808 * establishment and teardown
4813 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
4817 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
4821 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
4825 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
4829 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
4832 case A_RELEASE_DONE
:
4833 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
4843 gen_atmmulti_abbrev(type
)
4846 struct block
*b0
, *b1
;
4852 bpf_error("'oam' supported only on raw ATM");
4853 b1
= gen_atmmulti_abbrev(A_OAMF4
);
4858 bpf_error("'oamf4' supported only on raw ATM");
4860 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4861 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4863 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4869 * Get Q.2931 signalling messages for switched
4870 * virtual connection
4873 bpf_error("'connectmsg' supported only on raw ATM");
4874 b0
= gen_msg_abbrev(A_SETUP
);
4875 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4877 b0
= gen_msg_abbrev(A_CONNECT
);
4879 b0
= gen_msg_abbrev(A_CONNECTACK
);
4881 b0
= gen_msg_abbrev(A_RELEASE
);
4883 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4885 b0
= gen_atmtype_abbrev(A_SC
);
4891 bpf_error("'metaconnect' supported only on raw ATM");
4892 b0
= gen_msg_abbrev(A_SETUP
);
4893 b1
= gen_msg_abbrev(A_CALLPROCEED
);
4895 b0
= gen_msg_abbrev(A_CONNECT
);
4897 b0
= gen_msg_abbrev(A_RELEASE
);
4899 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
4901 b0
= gen_atmtype_abbrev(A_METAC
);