]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
08e89fed6e2bf06fe65e72152eb0b4038096d8fb
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.180 2002-10-18 08:46:13 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_ether_linktype(int);
164 static struct block
*gen_linktype(int);
165 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
166 static struct block
*gen_llc(int);
167 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
169 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
171 static struct block
*gen_ahostop(const u_char
*, int);
172 static struct block
*gen_ehostop(const u_char
*, int);
173 static struct block
*gen_fhostop(const u_char
*, int);
174 static struct block
*gen_thostop(const u_char
*, int);
175 static struct block
*gen_wlanhostop(const u_char
*, int);
176 static struct block
*gen_ipfchostop(const u_char
*, int);
177 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
178 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
180 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
183 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
185 static struct block
*gen_ipfrag(void);
186 static struct block
*gen_portatom(int, bpf_int32
);
188 static struct block
*gen_portatom6(int, bpf_int32
);
190 struct block
*gen_portop(int, int, int);
191 static struct block
*gen_port(int, int, int);
193 struct block
*gen_portop6(int, int, int);
194 static struct block
*gen_port6(int, int, int);
196 static int lookup_proto(const char *, int);
197 static struct block
*gen_protochain(int, int, int);
198 static struct block
*gen_proto(int, int, int);
199 static struct slist
*xfer_to_x(struct arth
*);
200 static struct slist
*xfer_to_a(struct arth
*);
201 static struct block
*gen_mac_multicast(int);
202 static struct block
*gen_len(int, int);
204 static struct block
*gen_msg_abbrev(int type
);
214 /* XXX Round up to nearest long. */
215 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
217 /* XXX Round up to structure boundary. */
221 cp
= &chunks
[cur_chunk
];
222 if (n
> cp
->n_left
) {
223 ++cp
, k
= ++cur_chunk
;
225 bpf_error("out of memory");
226 size
= CHUNK0SIZE
<< k
;
227 cp
->m
= (void *)malloc(size
);
228 memset((char *)cp
->m
, 0, size
);
231 bpf_error("out of memory");
234 return (void *)((char *)cp
->m
+ cp
->n_left
);
243 for (i
= 0; i
< NCHUNKS
; ++i
)
244 if (chunks
[i
].m
!= NULL
) {
251 * A strdup whose allocations are freed after code generation is over.
255 register const char *s
;
257 int n
= strlen(s
) + 1;
258 char *cp
= newchunk(n
);
264 static inline struct block
*
270 p
= (struct block
*)newchunk(sizeof(*p
));
277 static inline struct slist
*
283 p
= (struct slist
*)newchunk(sizeof(*p
));
289 static struct block
*
293 struct block
*b
= new_block(BPF_RET
|BPF_K
);
302 bpf_error("syntax error in filter expression");
305 static bpf_u_int32 netmask
;
310 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
311 char *buf
, int optimize
, bpf_u_int32 mask
)
320 if (setjmp(top_ctx
)) {
328 snaplen
= pcap_snapshot(p
);
330 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
331 "snaplen of 0 rejects all packets");
335 lex_init(buf
? buf
: "");
336 init_linktype(pcap_datalink(p
));
343 root
= gen_retblk(snaplen
);
345 if (optimize
&& !no_optimize
) {
348 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
349 bpf_error("expression rejects all packets");
351 program
->bf_insns
= icode_to_fcode(root
, &len
);
352 program
->bf_len
= len
;
360 * entry point for using the compiler with no pcap open
361 * pass in all the stuff that is needed explicitly instead.
364 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
365 struct bpf_program
*program
,
366 char *buf
, int optimize
, bpf_u_int32 mask
)
371 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
374 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
380 * Clean up a "struct bpf_program" by freeing all the memory allocated
384 pcap_freecode(struct bpf_program
*program
)
387 if (program
->bf_insns
!= NULL
) {
388 free((char *)program
->bf_insns
);
389 program
->bf_insns
= NULL
;
394 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
395 * which of the jt and jf fields has been resolved and which is a pointer
396 * back to another unresolved block (or nil). At least one of the fields
397 * in each block is already resolved.
400 backpatch(list
, target
)
401 struct block
*list
, *target
;
418 * Merge the lists in b0 and b1, using the 'sense' field to indicate
419 * which of jt and jf is the link.
423 struct block
*b0
, *b1
;
425 register struct block
**p
= &b0
;
427 /* Find end of list. */
429 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
431 /* Concatenate the lists. */
439 backpatch(p
, gen_retblk(snaplen
));
440 p
->sense
= !p
->sense
;
441 backpatch(p
, gen_retblk(0));
447 struct block
*b0
, *b1
;
449 backpatch(b0
, b1
->head
);
450 b0
->sense
= !b0
->sense
;
451 b1
->sense
= !b1
->sense
;
453 b1
->sense
= !b1
->sense
;
459 struct block
*b0
, *b1
;
461 b0
->sense
= !b0
->sense
;
462 backpatch(b0
, b1
->head
);
463 b0
->sense
= !b0
->sense
;
472 b
->sense
= !b
->sense
;
475 static struct block
*
476 gen_cmp(offset
, size
, v
)
483 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
486 b
= new_block(JMP(BPF_JEQ
));
493 static struct block
*
494 gen_cmp_gt(offset
, size
, v
)
501 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
504 b
= new_block(JMP(BPF_JGT
));
511 static struct block
*
512 gen_mcmp(offset
, size
, v
, mask
)
517 struct block
*b
= gen_cmp(offset
, size
, v
);
520 if (mask
!= 0xffffffff) {
521 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
528 static struct block
*
529 gen_bcmp(offset
, size
, v
)
530 register u_int offset
, size
;
531 register const u_char
*v
;
533 register struct block
*b
, *tmp
;
537 register const u_char
*p
= &v
[size
- 4];
538 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
539 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
541 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
548 register const u_char
*p
= &v
[size
- 2];
549 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
551 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
558 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
566 static struct block
*
567 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
568 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
574 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
577 if (mask
!= 0xffffffff) {
578 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
582 b
= new_block(JMP(jtype
));
585 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
591 * Various code constructs need to know the layout of the data link
592 * layer. These variables give the necessary offsets.
596 * This is the offset of the beginning of the MAC-layer header.
597 * It's usually 0, except for ATM LANE.
599 static u_int off_mac
;
602 * "off_linktype" is the offset to information in the link-layer header
603 * giving the packet type.
605 * For Ethernet, it's the offset of the Ethernet type field.
607 * For link-layer types that always use 802.2 headers, it's the
608 * offset of the LLC header.
610 * For PPP, it's the offset of the PPP type field.
612 * For Cisco HDLC, it's the offset of the CHDLC type field.
614 * For BSD loopback, it's the offset of the AF_ value.
616 * For Linux cooked sockets, it's the offset of the type field.
618 * It's set to -1 for no encapsulation, in which case, IP is assumed.
620 static u_int off_linktype
;
623 * TRUE if the link layer includes an ATM pseudo-header.
625 static int is_atm
= 0;
628 * TRUE if "lane" appeared in the filter; it causes us to generate
629 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
631 static int is_lane
= 0;
634 * These are offsets for the ATM pseudo-header.
636 static u_int off_vpi
;
637 static u_int off_vci
;
638 static u_int off_proto
;
641 * This is the offset of the first byte after the ATM pseudo_header,
642 * or -1 if there is no ATM pseudo-header.
644 static u_int off_payload
;
647 * These are offsets to the beginning of the network-layer header.
649 * If the link layer never uses 802.2 LLC:
651 * "off_nl" and "off_nl_nosnap" are the same.
653 * If the link layer always uses 802.2 LLC:
655 * "off_nl" is the offset if there's a SNAP header following
658 * "off_nl_nosnap" is the offset if there's no SNAP header.
660 * If the link layer is Ethernet:
662 * "off_nl" is the offset if the packet is an Ethernet II packet
663 * (we assume no 802.3+802.2+SNAP);
665 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
666 * with an 802.2 header following it.
669 static u_int off_nl_nosnap
;
680 * Assume it's not raw ATM with a pseudo-header, for now.
698 off_nl
= 6; /* XXX in reality, variable! */
699 off_nl_nosnap
= 6; /* no 802.2 LLC */
704 off_nl
= 14; /* Ethernet II */
705 off_nl_nosnap
= 17; /* 802.3+802.2 */
710 * SLIP doesn't have a link level type. The 16 byte
711 * header is hacked into our SLIP driver.
715 off_nl_nosnap
= 16; /* no 802.2 LLC */
719 /* XXX this may be the same as the DLT_PPP_BSDOS case */
723 off_nl_nosnap
= 24; /* no 802.2 LLC */
730 off_nl_nosnap
= 4; /* no 802.2 LLC */
734 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
735 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
738 off_nl_nosnap
= 4; /* no 802.2 LLC */
743 * This does no include the Ethernet header, and
744 * only covers session state.
748 off_nl_nosnap
= 8; /* no 802.2 LLC */
754 off_nl_nosnap
= 24; /* no 802.2 LLC */
759 * FDDI doesn't really have a link-level type field.
760 * We set "off_linktype" to the offset of the LLC header.
762 * To check for Ethernet types, we assume that SSAP = SNAP
763 * is being used and pick out the encapsulated Ethernet type.
764 * XXX - should we generate code to check for SNAP?
768 off_linktype
+= pcap_fddipad
;
770 off_nl
= 21; /* FDDI+802.2+SNAP */
771 off_nl_nosnap
= 16; /* FDDI+802.2 */
773 off_nl
+= pcap_fddipad
;
774 off_nl_nosnap
+= pcap_fddipad
;
780 * Token Ring doesn't really have a link-level type field.
781 * We set "off_linktype" to the offset of the LLC header.
783 * To check for Ethernet types, we assume that SSAP = SNAP
784 * is being used and pick out the encapsulated Ethernet type.
785 * XXX - should we generate code to check for SNAP?
787 * XXX - the header is actually variable-length.
788 * Some various Linux patched versions gave 38
789 * as "off_linktype" and 40 as "off_nl"; however,
790 * if a token ring packet has *no* routing
791 * information, i.e. is not source-routed, the correct
792 * values are 20 and 22, as they are in the vanilla code.
794 * A packet is source-routed iff the uppermost bit
795 * of the first byte of the source address, at an
796 * offset of 8, has the uppermost bit set. If the
797 * packet is source-routed, the total number of bytes
798 * of routing information is 2 plus bits 0x1F00 of
799 * the 16-bit value at an offset of 14 (shifted right
800 * 8 - figure out which byte that is).
803 off_nl
= 22; /* Token Ring+802.2+SNAP */
804 off_nl_nosnap
= 17; /* Token Ring+802.2 */
809 * 802.11 doesn't really have a link-level type field.
810 * We set "off_linktype" to the offset of the LLC header.
812 * To check for Ethernet types, we assume that SSAP = SNAP
813 * is being used and pick out the encapsulated Ethernet type.
814 * XXX - should we generate code to check for SNAP?
816 * XXX - the header is actually variable-length. We
817 * assume a 24-byte link-layer header, as appears in
818 * data frames in networks with no bridges.
821 off_nl
= 32; /* 802.11+802.2+SNAP */
822 off_nl_nosnap
= 27; /* 802.11+802.2 */
825 case DLT_PRISM_HEADER
:
827 * Same as 802.11, but with an additional header before
828 * the 802.11 header, containing a bunch of additional
829 * information including radio-level information.
831 * The header is 144 bytes long.
833 * XXX - same variable-length header problem; at least
834 * the Prism header is fixed-length.
836 off_linktype
= 144+24;
837 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
838 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
841 case DLT_ATM_RFC1483
:
842 case DLT_ATM_CLIP
: /* Linux ATM defines this */
844 * assume routed, non-ISO PDUs
845 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
848 off_nl
= 8; /* 802.2+SNAP */
849 off_nl_nosnap
= 3; /* 802.2 */
854 * Full Frontal ATM; you get AALn PDUs with an ATM
858 off_vpi
= SUNATM_VPI_POS
;
859 off_vci
= SUNATM_VCI_POS
;
860 off_proto
= PROTO_POS
;
861 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
862 off_payload
= SUNATM_PKT_BEGIN_POS
;
863 off_linktype
= off_payload
;
864 off_nl
= off_payload
+8; /* 802.2+SNAP */
865 off_nl_nosnap
= off_payload
+3; /* 802.2 */
871 off_nl_nosnap
= 0; /* no 802.2 LLC */
874 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
877 off_nl_nosnap
= 16; /* no 802.2 LLC */
882 * LocalTalk does have a 1-byte type field in the LLAP header,
883 * but really it just indicates whether there is a "short" or
884 * "long" DDP packet following.
888 off_nl_nosnap
= 0; /* no 802.2 LLC */
893 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
894 * link-level type field. We set "off_linktype" to the
895 * offset of the LLC header.
897 * To check for Ethernet types, we assume that SSAP = SNAP
898 * is being used and pick out the encapsulated Ethernet type.
899 * XXX - should we generate code to check for SNAP? RFC
900 * 2625 says SNAP should be used.
903 off_nl
= 24; /* IPFC+802.2+SNAP */
904 off_nl_nosnap
= 19; /* IPFC+802.2 */
909 * XXX - we should set this to handle SNAP-encapsulated
910 * frames (NLPID of 0x80).
914 off_nl_nosnap
= 0; /* no 802.2 LLC */
917 bpf_error("unknown data link type %d", linktype
);
921 static struct block
*
928 s
= new_stmt(BPF_LD
|BPF_IMM
);
930 b
= new_block(JMP(BPF_JEQ
));
936 static inline struct block
*
939 return gen_uncond(1);
942 static inline struct block
*
945 return gen_uncond(0);
949 * Byte-swap a 32-bit number.
950 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
951 * big-endian platforms.)
953 #define SWAPLONG(y) \
954 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
956 static struct block
*
957 gen_ether_linktype(proto
)
960 struct block
*b0
, *b1
;
966 * OSI protocols always use 802.2 encapsulation.
967 * XXX - should we check both the DSAP and the
968 * SSAP, like this, or should we check just the
971 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
973 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
974 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
980 * NetBEUI always uses 802.2 encapsulation.
981 * XXX - should we check both the DSAP and the
982 * SSAP, like this, or should we check just the
985 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
987 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
988 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
996 * Ethernet_II frames, which are Ethernet
997 * frames with a frame type of ETHERTYPE_IPX;
999 * Ethernet_802.3 frames, which are 802.3
1000 * frames (i.e., the type/length field is
1001 * a length field, <= ETHERMTU, rather than
1002 * a type field) with the first two bytes
1003 * after the Ethernet/802.3 header being
1006 * Ethernet_802.2 frames, which are 802.3
1007 * frames with an 802.2 LLC header and
1008 * with the IPX LSAP as the DSAP in the LLC
1011 * Ethernet_SNAP frames, which are 802.3
1012 * frames with an LLC header and a SNAP
1013 * header and with an OUI of 0x000000
1014 * (encapsulated Ethernet) and a protocol
1015 * ID of ETHERTYPE_IPX in the SNAP header.
1017 * XXX - should we generate the same code both
1018 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1022 * This generates code to check both for the
1023 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1025 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1026 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1030 * Now we add code to check for SNAP frames with
1031 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1033 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1037 * Now we generate code to check for 802.3
1038 * frames in general.
1040 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1044 * Now add the check for 802.3 frames before the
1045 * check for Ethernet_802.2 and Ethernet_802.3,
1046 * as those checks should only be done on 802.3
1047 * frames, not on Ethernet frames.
1052 * Now add the check for Ethernet_II frames, and
1053 * do that before checking for the other frame
1056 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1060 case ETHERTYPE_ATALK
:
1061 case ETHERTYPE_AARP
:
1063 * EtherTalk (AppleTalk protocols on Ethernet link
1064 * layer) may use 802.2 encapsulation.
1068 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1069 * we check for an Ethernet type field less than
1070 * 1500, which means it's an 802.3 length field.
1072 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1076 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1077 * SNAP packets with an organization code of
1078 * 0x080007 (Apple, for Appletalk) and a protocol
1079 * type of ETHERTYPE_ATALK (Appletalk).
1081 * 802.2-encapsulated ETHERTYPE_AARP packets are
1082 * SNAP packets with an organization code of
1083 * 0x000000 (encapsulated Ethernet) and a protocol
1084 * type of ETHERTYPE_AARP (Appletalk ARP).
1086 if (proto
== ETHERTYPE_ATALK
)
1087 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1088 else /* proto == ETHERTYPE_AARP */
1089 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1093 * Check for Ethernet encapsulation (Ethertalk
1094 * phase 1?); we just check for the Ethernet
1097 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1103 if (proto
<= ETHERMTU
) {
1105 * This is an LLC SAP value, so the frames
1106 * that match would be 802.2 frames.
1107 * Check that the frame is an 802.2 frame
1108 * (i.e., that the length/type field is
1109 * a length field, <= ETHERMTU) and
1110 * then check the DSAP.
1112 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1114 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1119 * This is an Ethernet type, so compare
1120 * the length/type field with it (if
1121 * the frame is an 802.2 frame, the length
1122 * field will be <= ETHERMTU, and, as
1123 * "proto" is > ETHERMTU, this test
1124 * will fail and the frame won't match,
1125 * which is what we want).
1127 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1132 static struct block
*
1136 struct block
*b0
, *b1
, *b2
;
1141 return gen_ether_linktype(proto
);
1144 case DLT_IEEE802_11
:
1145 case DLT_PRISM_HEADER
:
1148 case DLT_ATM_RFC1483
:
1150 case DLT_IP_OVER_FC
:
1151 return gen_llc(proto
);
1156 * If "is_lane" is set, check for a LANE-encapsulated
1157 * version of this protocol, otherwise check for an
1158 * LLC-encapsulated version of this protocol.
1160 * We assume LANE means Ethernet, not Token Ring.
1164 * Check that the packet doesn't begin with an
1165 * LE Control marker. (We've already generated
1168 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1172 * Now generate an Ethernet test.
1174 b1
= gen_ether_linktype(proto
);
1179 * Check for LLC encapsulation and then check the
1182 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1183 b1
= gen_llc(proto
);
1193 * OSI protocols always use 802.2 encapsulation.
1194 * XXX - should we check both the DSAP and the
1195 * LSAP, like this, or should we check just the
1198 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1199 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1200 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1204 case LLCSAP_NETBEUI
:
1206 * NetBEUI always uses 802.2 encapsulation.
1207 * XXX - should we check both the DSAP and the
1208 * LSAP, like this, or should we check just the
1211 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1212 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1213 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1219 * Ethernet_II frames, which are Ethernet
1220 * frames with a frame type of ETHERTYPE_IPX;
1222 * Ethernet_802.3 frames, which have a frame
1223 * type of LINUX_SLL_P_802_3;
1225 * Ethernet_802.2 frames, which are 802.3
1226 * frames with an 802.2 LLC header (i.e, have
1227 * a frame type of LINUX_SLL_P_802_2) and
1228 * with the IPX LSAP as the DSAP in the LLC
1231 * Ethernet_SNAP frames, which are 802.3
1232 * frames with an LLC header and a SNAP
1233 * header and with an OUI of 0x000000
1234 * (encapsulated Ethernet) and a protocol
1235 * ID of ETHERTYPE_IPX in the SNAP header.
1237 * First, do the checks on LINUX_SLL_P_802_2
1238 * frames; generate the check for either
1239 * Ethernet_802.2 or Ethernet_SNAP frames, and
1240 * then put a check for LINUX_SLL_P_802_2 frames
1243 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1244 (bpf_int32
)LLCSAP_IPX
);
1245 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1248 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1252 * Now check for 802.3 frames and OR that with
1253 * the previous test.
1255 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1259 * Now add the check for Ethernet_II frames, and
1260 * do that before checking for the other frame
1263 b0
= gen_cmp(off_linktype
, BPF_H
,
1264 (bpf_int32
)ETHERTYPE_IPX
);
1268 case ETHERTYPE_ATALK
:
1269 case ETHERTYPE_AARP
:
1271 * EtherTalk (AppleTalk protocols on Ethernet link
1272 * layer) may use 802.2 encapsulation.
1276 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1277 * we check for the 802.2 protocol type in the
1278 * "Ethernet type" field.
1280 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1283 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1284 * SNAP packets with an organization code of
1285 * 0x080007 (Apple, for Appletalk) and a protocol
1286 * type of ETHERTYPE_ATALK (Appletalk).
1288 * 802.2-encapsulated ETHERTYPE_AARP packets are
1289 * SNAP packets with an organization code of
1290 * 0x000000 (encapsulated Ethernet) and a protocol
1291 * type of ETHERTYPE_AARP (Appletalk ARP).
1293 if (proto
== ETHERTYPE_ATALK
)
1294 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1296 else /* proto == ETHERTYPE_AARP */
1297 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1302 * Check for Ethernet encapsulation (Ethertalk
1303 * phase 1?); we just check for the Ethernet
1306 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1312 if (proto
<= ETHERMTU
) {
1314 * This is an LLC SAP value, so the frames
1315 * that match would be 802.2 frames.
1316 * Check for the 802.2 protocol type
1317 * in the "Ethernet type" field, and
1318 * then check the DSAP.
1320 b0
= gen_cmp(off_linktype
, BPF_H
,
1322 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1328 * This is an Ethernet type, so compare
1329 * the length/type field with it (if
1330 * the frame is an 802.2 frame, the length
1331 * field will be <= ETHERMTU, and, as
1332 * "proto" is > ETHERMTU, this test
1333 * will fail and the frame won't match,
1334 * which is what we want).
1336 return gen_cmp(off_linktype
, BPF_H
,
1343 case DLT_SLIP_BSDOS
:
1346 * These types don't provide any type field; packets
1349 * XXX - for IPv4, check for a version number of 4, and,
1350 * for IPv6, check for a version number of 6?
1356 case ETHERTYPE_IPV6
:
1358 return gen_true(); /* always true */
1361 return gen_false(); /* always false */
1366 case DLT_PPP_SERIAL
:
1369 * We use Ethernet protocol types inside libpcap;
1370 * map them to the corresponding PPP protocol types.
1375 proto
= PPP_IP
; /* XXX was 0x21 */
1379 case ETHERTYPE_IPV6
:
1388 case ETHERTYPE_ATALK
:
1402 * I'm assuming the "Bridging PDU"s that go
1403 * over PPP are Spanning Tree Protocol
1417 * We use Ethernet protocol types inside libpcap;
1418 * map them to the corresponding PPP protocol types.
1423 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1424 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1426 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1431 case ETHERTYPE_IPV6
:
1441 case ETHERTYPE_ATALK
:
1455 * I'm assuming the "Bridging PDU"s that go
1456 * over PPP are Spanning Tree Protocol
1471 * For DLT_NULL, the link-layer header is a 32-bit
1472 * word containing an AF_ value in *host* byte order.
1474 * In addition, if we're reading a saved capture file,
1475 * the host byte order in the capture may not be the
1476 * same as the host byte order on this machine.
1478 * For DLT_LOOP, the link-layer header is a 32-bit
1479 * word containing an AF_ value in *network* byte order.
1481 * XXX - AF_ values may, unfortunately, be platform-
1482 * dependent; for example, FreeBSD's AF_INET6 is 24
1483 * whilst NetBSD's and OpenBSD's is 26.
1485 * This means that, when reading a capture file, just
1486 * checking for our AF_INET6 value won't work if the
1487 * capture file came from another OS.
1496 case ETHERTYPE_IPV6
:
1503 * Not a type on which we support filtering.
1504 * XXX - support those that have AF_ values
1505 * #defined on this platform, at least?
1510 if (linktype
== DLT_NULL
) {
1512 * The AF_ value is in host byte order, but
1513 * the BPF interpreter will convert it to
1514 * network byte order.
1516 * If this is a save file, and it's from a
1517 * machine with the opposite byte order to
1518 * ours, we byte-swap the AF_ value.
1520 * Then we run it through "htonl()", and
1521 * generate code to compare against the result.
1523 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1524 bpf_pcap
->sf
.swapped
)
1525 proto
= SWAPLONG(proto
);
1526 proto
= htonl(proto
);
1528 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1532 * XXX should we check for first fragment if the protocol
1539 case ETHERTYPE_IPV6
:
1540 return(gen_cmp(2, BPF_B
,
1541 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1544 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1545 b1
= gen_cmp(2, BPF_B
,
1546 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1550 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1551 b1
= gen_cmp(2, BPF_B
,
1552 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1555 case ETHERTYPE_REVARP
:
1556 return(gen_cmp(2, BPF_B
,
1557 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1558 case ETHERTYPE_ATALK
:
1559 return(gen_cmp(2, BPF_B
,
1560 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1566 case ETHERTYPE_ATALK
:
1575 * XXX - assumes a 2-byte Frame Relay header with
1576 * DLCI and flags. What if the address is longer?
1582 * Check for the special NLPID for IP.
1584 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1587 case ETHERTYPE_IPV6
:
1589 * Check for the special NLPID for IPv6.
1591 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1596 * Check for several OSI protocols.
1598 * Frame Relay packets typically have an OSI
1599 * NLPID at the beginning; we check for each
1602 * What we check for is the NLPID and a frame
1603 * control field of UI, i.e. 0x03 followed
1606 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1607 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1608 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1620 * All the types that have no encapsulation should either be
1621 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1622 * all packets are IP packets, or should be handled in some
1623 * special case, if none of them are (if some are and some
1624 * aren't, the lack of encapsulation is a problem, as we'd
1625 * have to find some other way of determining the packet type).
1627 * Therefore, if "off_linktype" is -1, there's an error.
1629 if (off_linktype
== -1)
1633 * Any type not handled above should always have an Ethernet
1634 * type at an offset of "off_linktype". (PPP is partially
1635 * handled above - the protocol type is mapped from the
1636 * Ethernet and LLC types we use internally to the corresponding
1637 * PPP type - but the PPP type is always specified by a value
1638 * at "off_linktype", so we don't have to do the code generation
1641 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1645 * Check for an LLC SNAP packet with a given organization code and
1646 * protocol type; we check the entire contents of the 802.2 LLC and
1647 * snap headers, checking for DSAP and SSAP of SNAP and a control
1648 * field of 0x03 in the LLC header, and for the specified organization
1649 * code and protocol type in the SNAP header.
1651 static struct block
*
1652 gen_snap(orgcode
, ptype
, offset
)
1653 bpf_u_int32 orgcode
;
1657 u_char snapblock
[8];
1659 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1660 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1661 snapblock
[2] = 0x03; /* control = UI */
1662 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1663 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1664 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1665 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1666 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1667 return gen_bcmp(offset
, 8, snapblock
);
1671 * Check for a given protocol value assuming an 802.2 LLC header.
1673 static struct block
*
1678 * XXX - handle token-ring variable-length header.
1683 return gen_cmp(off_linktype
, BPF_H
, (long)
1684 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1686 case LLCSAP_NETBEUI
:
1687 return gen_cmp(off_linktype
, BPF_H
, (long)
1688 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1692 * XXX - are there ever SNAP frames for IPX on
1693 * non-Ethernet 802.x networks?
1695 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1697 case ETHERTYPE_ATALK
:
1699 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1700 * SNAP packets with an organization code of
1701 * 0x080007 (Apple, for Appletalk) and a protocol
1702 * type of ETHERTYPE_ATALK (Appletalk).
1704 * XXX - check for an organization code of
1705 * encapsulated Ethernet as well?
1707 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1711 * XXX - we don't have to check for IPX 802.3
1712 * here, but should we check for the IPX Ethertype?
1714 if (proto
<= ETHERMTU
) {
1716 * This is an LLC SAP value, so check
1719 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1722 * This is an Ethernet type; we assume that it's
1723 * unlikely that it'll appear in the right place
1724 * at random, and therefore check only the
1725 * location that would hold the Ethernet type
1726 * in a SNAP frame with an organization code of
1727 * 0x000000 (encapsulated Ethernet).
1729 * XXX - if we were to check for the SNAP DSAP and
1730 * LSAP, as per XXX, and were also to check for an
1731 * organization code of 0x000000 (encapsulated
1732 * Ethernet), we'd do
1734 * return gen_snap(0x000000, proto,
1737 * here; for now, we don't, as per the above.
1738 * I don't know whether it's worth the extra CPU
1739 * time to do the right check or not.
1741 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1746 static struct block
*
1747 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1751 u_int src_off
, dst_off
;
1753 struct block
*b0
, *b1
;
1767 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1768 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1774 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1775 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1782 b0
= gen_linktype(proto
);
1783 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1789 static struct block
*
1790 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1791 struct in6_addr
*addr
;
1792 struct in6_addr
*mask
;
1794 u_int src_off
, dst_off
;
1796 struct block
*b0
, *b1
;
1811 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1812 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1818 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1819 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1826 /* this order is important */
1827 a
= (u_int32_t
*)addr
;
1828 m
= (u_int32_t
*)mask
;
1829 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1830 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1832 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1834 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1836 b0
= gen_linktype(proto
);
1842 static struct block
*
1843 gen_ehostop(eaddr
, dir
)
1844 register const u_char
*eaddr
;
1847 register struct block
*b0
, *b1
;
1851 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
1854 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
1857 b0
= gen_ehostop(eaddr
, Q_SRC
);
1858 b1
= gen_ehostop(eaddr
, Q_DST
);
1864 b0
= gen_ehostop(eaddr
, Q_SRC
);
1865 b1
= gen_ehostop(eaddr
, Q_DST
);
1874 * Like gen_ehostop, but for DLT_FDDI
1876 static struct block
*
1877 gen_fhostop(eaddr
, dir
)
1878 register const u_char
*eaddr
;
1881 struct block
*b0
, *b1
;
1886 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1888 return gen_bcmp(6 + 1, 6, eaddr
);
1893 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1895 return gen_bcmp(0 + 1, 6, eaddr
);
1899 b0
= gen_fhostop(eaddr
, Q_SRC
);
1900 b1
= gen_fhostop(eaddr
, Q_DST
);
1906 b0
= gen_fhostop(eaddr
, Q_SRC
);
1907 b1
= gen_fhostop(eaddr
, Q_DST
);
1916 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1918 static struct block
*
1919 gen_thostop(eaddr
, dir
)
1920 register const u_char
*eaddr
;
1923 register struct block
*b0
, *b1
;
1927 return gen_bcmp(8, 6, eaddr
);
1930 return gen_bcmp(2, 6, eaddr
);
1933 b0
= gen_thostop(eaddr
, Q_SRC
);
1934 b1
= gen_thostop(eaddr
, Q_DST
);
1940 b0
= gen_thostop(eaddr
, Q_SRC
);
1941 b1
= gen_thostop(eaddr
, Q_DST
);
1950 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
1952 static struct block
*
1953 gen_wlanhostop(eaddr
, dir
)
1954 register const u_char
*eaddr
;
1957 register struct block
*b0
, *b1
, *b2
;
1958 register struct slist
*s
;
1965 * For control frames, there is no SA.
1967 * For management frames, SA is at an
1968 * offset of 10 from the beginning of
1971 * For data frames, SA is at an offset
1972 * of 10 from the beginning of the packet
1973 * if From DS is clear, at an offset of
1974 * 16 from the beginning of the packet
1975 * if From DS is set and To DS is clear,
1976 * and an offset of 24 from the beginning
1977 * of the packet if From DS is set and To DS
1982 * Generate the tests to be done for data frames
1985 * First, check for To DS set, i.e. check "link[1] & 0x01".
1987 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
1989 b1
= new_block(JMP(BPF_JSET
));
1990 b1
->s
.k
= 0x01; /* To DS */
1994 * If To DS is set, the SA is at 24.
1996 b0
= gen_bcmp(24, 6, eaddr
);
2000 * Now, check for To DS not set, i.e. check
2001 * "!(link[1] & 0x01)".
2003 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2005 b2
= new_block(JMP(BPF_JSET
));
2006 b2
->s
.k
= 0x01; /* To DS */
2011 * If To DS is not set, the SA is at 16.
2013 b1
= gen_bcmp(16, 6, eaddr
);
2017 * Now OR together the last two checks. That gives
2018 * the complete set of checks for data frames with
2024 * Now check for From DS being set, and AND that with
2025 * the ORed-together checks.
2027 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2029 b1
= new_block(JMP(BPF_JSET
));
2030 b1
->s
.k
= 0x02; /* From DS */
2035 * Now check for data frames with From DS not set.
2037 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2039 b2
= new_block(JMP(BPF_JSET
));
2040 b2
->s
.k
= 0x02; /* From DS */
2045 * If From DS isn't set, the SA is at 10.
2047 b1
= gen_bcmp(10, 6, eaddr
);
2051 * Now OR together the checks for data frames with
2052 * From DS not set and for data frames with From DS
2053 * set; that gives the checks done for data frames.
2058 * Now check for a data frame.
2059 * I.e, check "link[0] & 0x08".
2061 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2063 b1
= new_block(JMP(BPF_JSET
));
2068 * AND that with the checks done for data frames.
2073 * If the high-order bit of the type value is 0, this
2074 * is a management frame.
2075 * I.e, check "!(link[0] & 0x08)".
2077 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2079 b2
= new_block(JMP(BPF_JSET
));
2085 * For management frames, the SA is at 10.
2087 b1
= gen_bcmp(10, 6, eaddr
);
2091 * OR that with the checks done for data frames.
2092 * That gives the checks done for management and
2098 * If the low-order bit of the type value is 1,
2099 * this is either a control frame or a frame
2100 * with a reserved type, and thus not a
2103 * I.e., check "!(link[0] & 0x04)".
2105 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2107 b1
= new_block(JMP(BPF_JSET
));
2113 * AND that with the checks for data and management
2123 * For control frames, there is no DA.
2125 * For management frames, DA is at an
2126 * offset of 4 from the beginning of
2129 * For data frames, DA is at an offset
2130 * of 4 from the beginning of the packet
2131 * if To DS is clear and at an offset of
2132 * 16 from the beginning of the packet
2137 * Generate the tests to be done for data frames.
2139 * First, check for To DS set, i.e. "link[1] & 0x01".
2141 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2143 b1
= new_block(JMP(BPF_JSET
));
2144 b1
->s
.k
= 0x01; /* To DS */
2148 * If To DS is set, the DA is at 16.
2150 b0
= gen_bcmp(16, 6, eaddr
);
2154 * Now, check for To DS not set, i.e. check
2155 * "!(link[1] & 0x01)".
2157 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2159 b2
= new_block(JMP(BPF_JSET
));
2160 b2
->s
.k
= 0x01; /* To DS */
2165 * If To DS is not set, the DA is at 4.
2167 b1
= gen_bcmp(4, 6, eaddr
);
2171 * Now OR together the last two checks. That gives
2172 * the complete set of checks for data frames.
2177 * Now check for a data frame.
2178 * I.e, check "link[0] & 0x08".
2180 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2182 b1
= new_block(JMP(BPF_JSET
));
2187 * AND that with the checks done for data frames.
2192 * If the high-order bit of the type value is 0, this
2193 * is a management frame.
2194 * I.e, check "!(link[0] & 0x08)".
2196 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2198 b2
= new_block(JMP(BPF_JSET
));
2204 * For management frames, the DA is at 4.
2206 b1
= gen_bcmp(4, 6, eaddr
);
2210 * OR that with the checks done for data frames.
2211 * That gives the checks done for management and
2217 * If the low-order bit of the type value is 1,
2218 * this is either a control frame or a frame
2219 * with a reserved type, and thus not a
2222 * I.e., check "!(link[0] & 0x04)".
2224 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2226 b1
= new_block(JMP(BPF_JSET
));
2232 * AND that with the checks for data and management
2239 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2240 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2246 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2247 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2256 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2257 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2258 * as the RFC states.)
2260 static struct block
*
2261 gen_ipfchostop(eaddr
, dir
)
2262 register const u_char
*eaddr
;
2265 register struct block
*b0
, *b1
;
2269 return gen_bcmp(10, 6, eaddr
);
2272 return gen_bcmp(2, 6, eaddr
);
2275 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2276 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2282 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2283 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2292 * This is quite tricky because there may be pad bytes in front of the
2293 * DECNET header, and then there are two possible data packet formats that
2294 * carry both src and dst addresses, plus 5 packet types in a format that
2295 * carries only the src node, plus 2 types that use a different format and
2296 * also carry just the src node.
2300 * Instead of doing those all right, we just look for data packets with
2301 * 0 or 1 bytes of padding. If you want to look at other packets, that
2302 * will require a lot more hacking.
2304 * To add support for filtering on DECNET "areas" (network numbers)
2305 * one would want to add a "mask" argument to this routine. That would
2306 * make the filter even more inefficient, although one could be clever
2307 * and not generate masking instructions if the mask is 0xFFFF.
2309 static struct block
*
2310 gen_dnhostop(addr
, dir
, base_off
)
2315 struct block
*b0
, *b1
, *b2
, *tmp
;
2316 u_int offset_lh
; /* offset if long header is received */
2317 u_int offset_sh
; /* offset if short header is received */
2322 offset_sh
= 1; /* follows flags */
2323 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2327 offset_sh
= 3; /* follows flags, dstnode */
2328 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2332 /* Inefficient because we do our Calvinball dance twice */
2333 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2334 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2340 /* Inefficient because we do our Calvinball dance twice */
2341 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2342 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2347 bpf_error("ISO host filtering not implemented");
2352 b0
= gen_linktype(ETHERTYPE_DN
);
2353 /* Check for pad = 1, long header case */
2354 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2355 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2356 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2357 BPF_H
, (bpf_int32
)ntohs(addr
));
2359 /* Check for pad = 0, long header case */
2360 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2361 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2364 /* Check for pad = 1, short header case */
2365 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2366 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2367 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2368 BPF_H
, (bpf_int32
)ntohs(addr
));
2371 /* Check for pad = 0, short header case */
2372 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2373 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2377 /* Combine with test for linktype */
2382 static struct block
*
2383 gen_host(addr
, mask
, proto
, dir
)
2389 struct block
*b0
, *b1
;
2394 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2395 if (off_linktype
!= -1) {
2396 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2398 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2404 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2405 off_nl
+ 12, off_nl
+ 16);
2408 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2409 off_nl
+ 14, off_nl
+ 24);
2412 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2413 off_nl
+ 14, off_nl
+ 24);
2416 bpf_error("'tcp' modifier applied to host");
2419 bpf_error("'sctp' modifier applied to host");
2422 bpf_error("'udp' modifier applied to host");
2425 bpf_error("'icmp' modifier applied to host");
2428 bpf_error("'igmp' modifier applied to host");
2431 bpf_error("'igrp' modifier applied to host");
2434 bpf_error("'pim' modifier applied to host");
2437 bpf_error("'vrrp' modifier applied to host");
2440 bpf_error("ATALK host filtering not implemented");
2443 bpf_error("AARP host filtering not implemented");
2446 return gen_dnhostop(addr
, dir
, off_nl
);
2449 bpf_error("SCA host filtering not implemented");
2452 bpf_error("LAT host filtering not implemented");
2455 bpf_error("MOPDL host filtering not implemented");
2458 bpf_error("MOPRC host filtering not implemented");
2462 bpf_error("'ip6' modifier applied to ip host");
2465 bpf_error("'icmp6' modifier applied to host");
2469 bpf_error("'ah' modifier applied to host");
2472 bpf_error("'esp' modifier applied to host");
2475 bpf_error("ISO host filtering not implemented");
2478 bpf_error("'esis' modifier applied to host");
2481 bpf_error("'isis' modifier applied to host");
2484 bpf_error("'clnp' modifier applied to host");
2487 bpf_error("'stp' modifier applied to host");
2490 bpf_error("IPX host filtering not implemented");
2493 bpf_error("'netbeui' modifier applied to host");
2502 static struct block
*
2503 gen_host6(addr
, mask
, proto
, dir
)
2504 struct in6_addr
*addr
;
2505 struct in6_addr
*mask
;
2512 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2515 bpf_error("'ip' modifier applied to ip6 host");
2518 bpf_error("'rarp' modifier applied to ip6 host");
2521 bpf_error("'arp' modifier applied to ip6 host");
2524 bpf_error("'sctp' modifier applied to host");
2527 bpf_error("'tcp' modifier applied to host");
2530 bpf_error("'udp' modifier applied to host");
2533 bpf_error("'icmp' modifier applied to host");
2536 bpf_error("'igmp' modifier applied to host");
2539 bpf_error("'igrp' modifier applied to host");
2542 bpf_error("'pim' modifier applied to host");
2545 bpf_error("'vrrp' modifier applied to host");
2548 bpf_error("ATALK host filtering not implemented");
2551 bpf_error("AARP host filtering not implemented");
2554 bpf_error("'decnet' modifier applied to ip6 host");
2557 bpf_error("SCA host filtering not implemented");
2560 bpf_error("LAT host filtering not implemented");
2563 bpf_error("MOPDL host filtering not implemented");
2566 bpf_error("MOPRC host filtering not implemented");
2569 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2570 off_nl
+ 8, off_nl
+ 24);
2573 bpf_error("'icmp6' modifier applied to host");
2576 bpf_error("'ah' modifier applied to host");
2579 bpf_error("'esp' modifier applied to host");
2582 bpf_error("ISO host filtering not implemented");
2585 bpf_error("'esis' modifier applied to host");
2588 bpf_error("'isis' modifier applied to host");
2591 bpf_error("'clnp' modifier applied to host");
2594 bpf_error("'stp' modifier applied to host");
2597 bpf_error("IPX host filtering not implemented");
2600 bpf_error("'netbeui' modifier applied to host");
2610 static struct block
*
2611 gen_gateway(eaddr
, alist
, proto
, dir
)
2612 const u_char
*eaddr
;
2613 bpf_u_int32
**alist
;
2617 struct block
*b0
, *b1
, *tmp
;
2620 bpf_error("direction applied to 'gateway'");
2627 if (linktype
== DLT_EN10MB
)
2628 b0
= gen_ehostop(eaddr
, Q_OR
);
2629 else if (linktype
== DLT_FDDI
)
2630 b0
= gen_fhostop(eaddr
, Q_OR
);
2631 else if (linktype
== DLT_IEEE802
)
2632 b0
= gen_thostop(eaddr
, Q_OR
);
2633 else if (linktype
== DLT_IEEE802_11
)
2634 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2635 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2637 * Check that the packet doesn't begin with an
2638 * LE Control marker. (We've already generated
2641 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2645 * Now check the MAC address.
2647 b0
= gen_ehostop(eaddr
, Q_OR
);
2649 } else if (linktype
== DLT_IP_OVER_FC
)
2650 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2653 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2655 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2657 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2665 bpf_error("illegal modifier of 'gateway'");
2671 gen_proto_abbrev(proto
)
2682 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2684 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2690 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2692 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2698 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2700 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2706 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2709 #ifndef IPPROTO_IGMP
2710 #define IPPROTO_IGMP 2
2714 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2717 #ifndef IPPROTO_IGRP
2718 #define IPPROTO_IGRP 9
2721 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2725 #define IPPROTO_PIM 103
2729 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2731 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2736 #ifndef IPPROTO_VRRP
2737 #define IPPROTO_VRRP 112
2741 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2745 b1
= gen_linktype(ETHERTYPE_IP
);
2749 b1
= gen_linktype(ETHERTYPE_ARP
);
2753 b1
= gen_linktype(ETHERTYPE_REVARP
);
2757 bpf_error("link layer applied in wrong context");
2760 b1
= gen_linktype(ETHERTYPE_ATALK
);
2764 b1
= gen_linktype(ETHERTYPE_AARP
);
2768 b1
= gen_linktype(ETHERTYPE_DN
);
2772 b1
= gen_linktype(ETHERTYPE_SCA
);
2776 b1
= gen_linktype(ETHERTYPE_LAT
);
2780 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2784 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2789 b1
= gen_linktype(ETHERTYPE_IPV6
);
2792 #ifndef IPPROTO_ICMPV6
2793 #define IPPROTO_ICMPV6 58
2796 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2801 #define IPPROTO_AH 51
2804 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2806 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2812 #define IPPROTO_ESP 50
2815 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2817 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2823 b1
= gen_linktype(LLCSAP_ISONS
);
2827 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2831 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2835 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2839 b1
= gen_linktype(LLCSAP_8021D
);
2843 b1
= gen_linktype(LLCSAP_IPX
);
2847 b1
= gen_linktype(LLCSAP_NETBEUI
);
2856 static struct block
*
2863 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2864 s
->s
.k
= off_nl
+ 6;
2865 b
= new_block(JMP(BPF_JSET
));
2873 static struct block
*
2874 gen_portatom(off
, v
)
2881 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2884 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2885 s
->next
->s
.k
= off_nl
+ off
;
2887 b
= new_block(JMP(BPF_JEQ
));
2895 static struct block
*
2896 gen_portatom6(off
, v
)
2900 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2905 gen_portop(port
, proto
, dir
)
2906 int port
, proto
, dir
;
2908 struct block
*b0
, *b1
, *tmp
;
2910 /* ip proto 'proto' */
2911 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2917 b1
= gen_portatom(0, (bpf_int32
)port
);
2921 b1
= gen_portatom(2, (bpf_int32
)port
);
2926 tmp
= gen_portatom(0, (bpf_int32
)port
);
2927 b1
= gen_portatom(2, (bpf_int32
)port
);
2932 tmp
= gen_portatom(0, (bpf_int32
)port
);
2933 b1
= gen_portatom(2, (bpf_int32
)port
);
2945 static struct block
*
2946 gen_port(port
, ip_proto
, dir
)
2951 struct block
*b0
, *b1
, *tmp
;
2953 /* ether proto ip */
2954 b0
= gen_linktype(ETHERTYPE_IP
);
2960 b1
= gen_portop(port
, ip_proto
, dir
);
2964 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
2965 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
2967 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
2980 gen_portop6(port
, proto
, dir
)
2981 int port
, proto
, dir
;
2983 struct block
*b0
, *b1
, *tmp
;
2985 /* ip proto 'proto' */
2986 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
2990 b1
= gen_portatom6(0, (bpf_int32
)port
);
2994 b1
= gen_portatom6(2, (bpf_int32
)port
);
2999 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3000 b1
= gen_portatom6(2, (bpf_int32
)port
);
3005 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3006 b1
= gen_portatom6(2, (bpf_int32
)port
);
3018 static struct block
*
3019 gen_port6(port
, ip_proto
, dir
)
3024 struct block
*b0
, *b1
, *tmp
;
3026 /* ether proto ip */
3027 b0
= gen_linktype(ETHERTYPE_IPV6
);
3033 b1
= gen_portop6(port
, ip_proto
, dir
);
3037 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3038 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3040 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3053 lookup_proto(name
, proto
)
3054 register const char *name
;
3064 v
= pcap_nametoproto(name
);
3065 if (v
== PROTO_UNDEF
)
3066 bpf_error("unknown ip proto '%s'", name
);
3070 /* XXX should look up h/w protocol type based on linktype */
3071 v
= pcap_nametoeproto(name
);
3072 if (v
== PROTO_UNDEF
)
3073 bpf_error("unknown ether proto '%s'", name
);
3077 if (strcmp(name
, "esis") == 0)
3079 else if (strcmp(name
, "isis") == 0)
3081 else if (strcmp(name
, "clnp") == 0)
3084 bpf_error("unknown osi proto '%s'", name
);
3104 static struct block
*
3105 gen_protochain(v
, proto
, dir
)
3110 #ifdef NO_PROTOCHAIN
3111 return gen_proto(v
, proto
, dir
);
3113 struct block
*b0
, *b
;
3114 struct slist
*s
[100];
3115 int fix2
, fix3
, fix4
, fix5
;
3116 int ahcheck
, again
, end
;
3118 int reg2
= alloc_reg();
3120 memset(s
, 0, sizeof(s
));
3121 fix2
= fix3
= fix4
= fix5
= 0;
3128 b0
= gen_protochain(v
, Q_IP
, dir
);
3129 b
= gen_protochain(v
, Q_IPV6
, dir
);
3133 bpf_error("bad protocol applied for 'protochain'");
3137 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3140 * s[0] is a dummy entry to protect other BPF insn from damaged
3141 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3142 * hard to find interdependency made by jump table fixup.
3145 s
[i
] = new_stmt(0); /*dummy*/
3150 b0
= gen_linktype(ETHERTYPE_IP
);
3153 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3154 s
[i
]->s
.k
= off_nl
+ 9;
3156 /* X = ip->ip_hl << 2 */
3157 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3163 b0
= gen_linktype(ETHERTYPE_IPV6
);
3165 /* A = ip6->ip_nxt */
3166 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3167 s
[i
]->s
.k
= off_nl
+ 6;
3169 /* X = sizeof(struct ip6_hdr) */
3170 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3176 bpf_error("unsupported proto to gen_protochain");
3180 /* again: if (A == v) goto end; else fall through; */
3182 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3184 s
[i
]->s
.jt
= NULL
; /*later*/
3185 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3189 #ifndef IPPROTO_NONE
3190 #define IPPROTO_NONE 59
3192 /* if (A == IPPROTO_NONE) goto end */
3193 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3194 s
[i
]->s
.jt
= NULL
; /*later*/
3195 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3196 s
[i
]->s
.k
= IPPROTO_NONE
;
3197 s
[fix5
]->s
.jf
= s
[i
];
3202 if (proto
== Q_IPV6
) {
3203 int v6start
, v6end
, v6advance
, j
;
3206 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3207 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3208 s
[i
]->s
.jt
= NULL
; /*later*/
3209 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3210 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3211 s
[fix2
]->s
.jf
= s
[i
];
3213 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3214 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3215 s
[i
]->s
.jt
= NULL
; /*later*/
3216 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3217 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3219 /* if (A == IPPROTO_ROUTING) goto v6advance */
3220 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3221 s
[i
]->s
.jt
= NULL
; /*later*/
3222 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3223 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3225 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3226 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3227 s
[i
]->s
.jt
= NULL
; /*later*/
3228 s
[i
]->s
.jf
= NULL
; /*later*/
3229 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3240 * X = X + (P[X + 1] + 1) * 8;
3243 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3245 /* A = P[X + packet head] */
3246 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3250 s
[i
] = new_stmt(BPF_ST
);
3254 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3257 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3261 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3263 /* A = P[X + packet head]; */
3264 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3268 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3272 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3276 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3279 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3283 /* goto again; (must use BPF_JA for backward jump) */
3284 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3285 s
[i
]->s
.k
= again
- i
- 1;
3286 s
[i
- 1]->s
.jf
= s
[i
];
3290 for (j
= v6start
; j
<= v6end
; j
++)
3291 s
[j
]->s
.jt
= s
[v6advance
];
3296 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3298 s
[fix2
]->s
.jf
= s
[i
];
3304 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3305 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3306 s
[i
]->s
.jt
= NULL
; /*later*/
3307 s
[i
]->s
.jf
= NULL
; /*later*/
3308 s
[i
]->s
.k
= IPPROTO_AH
;
3310 s
[fix3
]->s
.jf
= s
[ahcheck
];
3317 * X = X + (P[X + 1] + 2) * 4;
3320 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3322 /* A = P[X + packet head]; */
3323 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3327 s
[i
] = new_stmt(BPF_ST
);
3331 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3334 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3338 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3340 /* A = P[X + packet head] */
3341 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3345 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3349 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3353 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3356 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3360 /* goto again; (must use BPF_JA for backward jump) */
3361 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3362 s
[i
]->s
.k
= again
- i
- 1;
3367 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3369 s
[fix2
]->s
.jt
= s
[end
];
3370 s
[fix4
]->s
.jf
= s
[end
];
3371 s
[fix5
]->s
.jt
= s
[end
];
3378 for (i
= 0; i
< max
- 1; i
++)
3379 s
[i
]->next
= s
[i
+ 1];
3380 s
[max
- 1]->next
= NULL
;
3385 b
= new_block(JMP(BPF_JEQ
));
3386 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3396 static struct block
*
3397 gen_proto(v
, proto
, dir
)
3402 struct block
*b0
, *b1
;
3404 if (dir
!= Q_DEFAULT
)
3405 bpf_error("direction applied to 'proto'");
3410 b0
= gen_proto(v
, Q_IP
, dir
);
3411 b1
= gen_proto(v
, Q_IPV6
, dir
);
3418 b0
= gen_linktype(ETHERTYPE_IP
);
3420 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3422 b1
= gen_protochain(v
, Q_IP
);
3432 * Frame Relay packets typically have an OSI
3433 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3434 * generates code to check for all the OSI
3435 * NLPIDs, so calling it and then adding a check
3436 * for the particular NLPID for which we're
3437 * looking is bogus, as we can just check for
3440 * What we check for is the NLPID and a frame
3441 * control field value of UI, i.e. 0x03 followed
3444 * XXX - assumes a 2-byte Frame Relay header with
3445 * DLCI and flags. What if the address is longer?
3447 * XXX - what about SNAP-encapsulated frames?
3449 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3453 b0
= gen_linktype(LLCSAP_ISONS
);
3454 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3460 bpf_error("arp does not encapsulate another protocol");
3464 bpf_error("rarp does not encapsulate another protocol");
3468 bpf_error("atalk encapsulation is not specifiable");
3472 bpf_error("decnet encapsulation is not specifiable");
3476 bpf_error("sca does not encapsulate another protocol");
3480 bpf_error("lat does not encapsulate another protocol");
3484 bpf_error("moprc does not encapsulate another protocol");
3488 bpf_error("mopdl does not encapsulate another protocol");
3492 return gen_linktype(v
);
3495 bpf_error("'udp proto' is bogus");
3499 bpf_error("'tcp proto' is bogus");
3503 bpf_error("'sctp proto' is bogus");
3507 bpf_error("'icmp proto' is bogus");
3511 bpf_error("'igmp proto' is bogus");
3515 bpf_error("'igrp proto' is bogus");
3519 bpf_error("'pim proto' is bogus");
3523 bpf_error("'vrrp proto' is bogus");
3528 b0
= gen_linktype(ETHERTYPE_IPV6
);
3530 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3532 b1
= gen_protochain(v
, Q_IPV6
);
3538 bpf_error("'icmp6 proto' is bogus");
3542 bpf_error("'ah proto' is bogus");
3545 bpf_error("'ah proto' is bogus");
3548 bpf_error("'stp proto' is bogus");
3551 bpf_error("'ipx proto' is bogus");
3554 bpf_error("'netbeui proto' is bogus");
3565 register const char *name
;
3568 int proto
= q
.proto
;
3572 bpf_u_int32 mask
, addr
;
3574 bpf_u_int32
**alist
;
3577 struct sockaddr_in
*sin
;
3578 struct sockaddr_in6
*sin6
;
3579 struct addrinfo
*res
, *res0
;
3580 struct in6_addr mask128
;
3582 struct block
*b
, *tmp
;
3583 int port
, real_proto
;
3588 addr
= pcap_nametonetaddr(name
);
3590 bpf_error("unknown network '%s'", name
);
3591 /* Left justify network addr and calculate its network mask */
3593 while (addr
&& (addr
& 0xff000000) == 0) {
3597 return gen_host(addr
, mask
, proto
, dir
);
3601 if (proto
== Q_LINK
) {
3605 eaddr
= pcap_ether_hostton(name
);
3608 "unknown ether host '%s'", name
);
3609 b
= gen_ehostop(eaddr
, dir
);
3614 eaddr
= pcap_ether_hostton(name
);
3617 "unknown FDDI host '%s'", name
);
3618 b
= gen_fhostop(eaddr
, dir
);
3623 eaddr
= pcap_ether_hostton(name
);
3626 "unknown token ring host '%s'", name
);
3627 b
= gen_thostop(eaddr
, dir
);
3631 case DLT_IEEE802_11
:
3632 eaddr
= pcap_ether_hostton(name
);
3635 "unknown 802.11 host '%s'", name
);
3636 b
= gen_wlanhostop(eaddr
, dir
);
3640 case DLT_IP_OVER_FC
:
3641 eaddr
= pcap_ether_hostton(name
);
3644 "unknown Fibre Channel host '%s'", name
);
3645 b
= gen_ipfchostop(eaddr
, dir
);
3654 * Check that the packet doesn't begin
3655 * with an LE Control marker. (We've
3656 * already generated a test for LANE.)
3658 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3662 eaddr
= pcap_ether_hostton(name
);
3665 "unknown ether host '%s'", name
);
3666 b
= gen_ehostop(eaddr
, dir
);
3672 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3673 } else if (proto
== Q_DECNET
) {
3674 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3676 * I don't think DECNET hosts can be multihomed, so
3677 * there is no need to build up a list of addresses
3679 return (gen_host(dn_addr
, 0, proto
, dir
));
3682 alist
= pcap_nametoaddr(name
);
3683 if (alist
== NULL
|| *alist
== NULL
)
3684 bpf_error("unknown host '%s'", name
);
3686 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3688 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3690 tmp
= gen_host(**alist
++, 0xffffffff,
3697 memset(&mask128
, 0xff, sizeof(mask128
));
3698 res0
= res
= pcap_nametoaddrinfo(name
);
3700 bpf_error("unknown host '%s'", name
);
3702 tproto
= tproto6
= proto
;
3703 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3707 for (res
= res0
; res
; res
= res
->ai_next
) {
3708 switch (res
->ai_family
) {
3710 if (tproto
== Q_IPV6
)
3713 sin
= (struct sockaddr_in
*)
3715 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3716 0xffffffff, tproto
, dir
);
3719 if (tproto6
== Q_IP
)
3722 sin6
= (struct sockaddr_in6
*)
3724 tmp
= gen_host6(&sin6
->sin6_addr
,
3725 &mask128
, tproto6
, dir
);
3736 bpf_error("unknown host '%s'%s", name
,
3737 (proto
== Q_DEFAULT
)
3739 : " for specified address family");
3746 if (proto
!= Q_DEFAULT
&&
3747 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3748 bpf_error("illegal qualifier of 'port'");
3749 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3750 bpf_error("unknown port '%s'", name
);
3751 if (proto
== Q_UDP
) {
3752 if (real_proto
== IPPROTO_TCP
)
3753 bpf_error("port '%s' is tcp", name
);
3754 else if (real_proto
== IPPROTO_SCTP
)
3755 bpf_error("port '%s' is sctp", name
);
3757 /* override PROTO_UNDEF */
3758 real_proto
= IPPROTO_UDP
;
3760 if (proto
== Q_TCP
) {
3761 if (real_proto
== IPPROTO_UDP
)
3762 bpf_error("port '%s' is udp", name
);
3764 else if (real_proto
== IPPROTO_SCTP
)
3765 bpf_error("port '%s' is sctp", name
);
3767 /* override PROTO_UNDEF */
3768 real_proto
= IPPROTO_TCP
;
3770 if (proto
== Q_SCTP
) {
3771 if (real_proto
== IPPROTO_UDP
)
3772 bpf_error("port '%s' is udp", name
);
3774 else if (real_proto
== IPPROTO_TCP
)
3775 bpf_error("port '%s' is tcp", name
);
3777 /* override PROTO_UNDEF */
3778 real_proto
= IPPROTO_SCTP
;
3781 return gen_port(port
, real_proto
, dir
);
3785 b
= gen_port(port
, real_proto
, dir
);
3786 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3793 eaddr
= pcap_ether_hostton(name
);
3795 bpf_error("unknown ether host: %s", name
);
3797 alist
= pcap_nametoaddr(name
);
3798 if (alist
== NULL
|| *alist
== NULL
)
3799 bpf_error("unknown host '%s'", name
);
3800 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3804 bpf_error("'gateway' not supported in this configuration");
3808 real_proto
= lookup_proto(name
, proto
);
3809 if (real_proto
>= 0)
3810 return gen_proto(real_proto
, proto
, dir
);
3812 bpf_error("unknown protocol: %s", name
);
3815 real_proto
= lookup_proto(name
, proto
);
3816 if (real_proto
>= 0)
3817 return gen_protochain(real_proto
, proto
, dir
);
3819 bpf_error("unknown protocol: %s", name
);
3831 gen_mcode(s1
, s2
, masklen
, q
)
3832 register const char *s1
, *s2
;
3833 register int masklen
;
3836 register int nlen
, mlen
;
3839 nlen
= __pcap_atoin(s1
, &n
);
3840 /* Promote short ipaddr */
3844 mlen
= __pcap_atoin(s2
, &m
);
3845 /* Promote short ipaddr */
3848 bpf_error("non-network bits set in \"%s mask %s\"",
3851 /* Convert mask len to mask */
3853 bpf_error("mask length must be <= 32");
3854 m
= 0xffffffff << (32 - masklen
);
3856 bpf_error("non-network bits set in \"%s/%d\"",
3863 return gen_host(n
, m
, q
.proto
, q
.dir
);
3866 bpf_error("Mask syntax for networks only");
3873 register const char *s
;
3878 int proto
= q
.proto
;
3884 else if (q
.proto
== Q_DECNET
)
3885 vlen
= __pcap_atodn(s
, &v
);
3887 vlen
= __pcap_atoin(s
, &v
);
3894 if (proto
== Q_DECNET
)
3895 return gen_host(v
, 0, proto
, dir
);
3896 else if (proto
== Q_LINK
) {
3897 bpf_error("illegal link layer address");
3900 if (s
== NULL
&& q
.addr
== Q_NET
) {
3901 /* Promote short net number */
3902 while (v
&& (v
& 0xff000000) == 0) {
3907 /* Promote short ipaddr */
3911 return gen_host(v
, mask
, proto
, dir
);
3916 proto
= IPPROTO_UDP
;
3917 else if (proto
== Q_TCP
)
3918 proto
= IPPROTO_TCP
;
3919 else if (proto
== Q_SCTP
)
3920 proto
= IPPROTO_SCTP
;
3921 else if (proto
== Q_DEFAULT
)
3922 proto
= PROTO_UNDEF
;
3924 bpf_error("illegal qualifier of 'port'");
3927 return gen_port((int)v
, proto
, dir
);
3931 b
= gen_port((int)v
, proto
, dir
);
3932 gen_or(gen_port6((int)v
, proto
, dir
), b
);
3938 bpf_error("'gateway' requires a name");
3942 return gen_proto((int)v
, proto
, dir
);
3945 return gen_protochain((int)v
, proto
, dir
);
3960 gen_mcode6(s1
, s2
, masklen
, q
)
3961 register const char *s1
, *s2
;
3962 register int masklen
;
3965 struct addrinfo
*res
;
3966 struct in6_addr
*addr
;
3967 struct in6_addr mask
;
3972 bpf_error("no mask %s supported", s2
);
3974 res
= pcap_nametoaddrinfo(s1
);
3976 bpf_error("invalid ip6 address %s", s1
);
3978 bpf_error("%s resolved to multiple address", s1
);
3979 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
3981 if (sizeof(mask
) * 8 < masklen
)
3982 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
3983 memset(&mask
, 0, sizeof(mask
));
3984 memset(&mask
, 0xff, masklen
/ 8);
3986 mask
.s6_addr
[masklen
/ 8] =
3987 (0xff << (8 - masklen
% 8)) & 0xff;
3990 a
= (u_int32_t
*)addr
;
3991 m
= (u_int32_t
*)&mask
;
3992 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
3993 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
3994 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4002 bpf_error("Mask syntax for networks only");
4006 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4011 bpf_error("invalid qualifier against IPv6 address");
4019 register const u_char
*eaddr
;
4022 struct block
*b
, *tmp
;
4024 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4025 if (linktype
== DLT_EN10MB
)
4026 return gen_ehostop(eaddr
, (int)q
.dir
);
4027 if (linktype
== DLT_FDDI
)
4028 return gen_fhostop(eaddr
, (int)q
.dir
);
4029 if (linktype
== DLT_IEEE802
)
4030 return gen_thostop(eaddr
, (int)q
.dir
);
4031 if (linktype
== DLT_IEEE802_11
)
4032 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4033 if (linktype
== DLT_SUNATM
&& is_lane
) {
4035 * Check that the packet doesn't begin with an
4036 * LE Control marker. (We've already generated
4039 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4043 * Now check the MAC address.
4045 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4049 if (linktype
== DLT_IP_OVER_FC
)
4050 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4051 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4053 bpf_error("ethernet address used in non-ether expression");
4059 struct slist
*s0
, *s1
;
4062 * This is definitely not the best way to do this, but the
4063 * lists will rarely get long.
4070 static struct slist
*
4076 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4081 static struct slist
*
4087 s
= new_stmt(BPF_LD
|BPF_MEM
);
4093 gen_load(proto
, index
, size
)
4098 struct slist
*s
, *tmp
;
4100 int regno
= alloc_reg();
4102 free_reg(index
->regno
);
4106 bpf_error("data size must be 1, 2, or 4");
4122 bpf_error("unsupported index operation");
4126 * XXX - what about ATM LANE? Should the index be
4127 * relative to the beginning of the AAL5 frame, so
4128 * that 0 refers to the beginning of the LE Control
4129 * field, or relative to the beginning of the LAN
4130 * frame, so that 0 refers, for Ethernet LANE, to
4131 * the beginning of the destination address?
4133 s
= xfer_to_x(index
);
4134 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4136 sappend(index
->s
, s
);
4151 /* XXX Note that we assume a fixed link header here. */
4152 s
= xfer_to_x(index
);
4153 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4156 sappend(index
->s
, s
);
4158 b
= gen_proto_abbrev(proto
);
4160 gen_and(index
->b
, b
);
4172 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4174 sappend(s
, xfer_to_a(index
));
4175 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4176 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4177 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4179 sappend(index
->s
, s
);
4181 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4183 gen_and(index
->b
, b
);
4185 gen_and(gen_proto_abbrev(Q_IP
), b
);
4191 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4195 index
->regno
= regno
;
4196 s
= new_stmt(BPF_ST
);
4198 sappend(index
->s
, s
);
4204 gen_relation(code
, a0
, a1
, reversed
)
4206 struct arth
*a0
, *a1
;
4209 struct slist
*s0
, *s1
, *s2
;
4210 struct block
*b
, *tmp
;
4214 if (code
== BPF_JEQ
) {
4215 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4216 b
= new_block(JMP(code
));
4220 b
= new_block(BPF_JMP
|code
|BPF_X
);
4226 sappend(a0
->s
, a1
->s
);
4230 free_reg(a0
->regno
);
4231 free_reg(a1
->regno
);
4233 /* 'and' together protocol checks */
4236 gen_and(a0
->b
, tmp
= a1
->b
);
4252 int regno
= alloc_reg();
4253 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4256 s
= new_stmt(BPF_LD
|BPF_LEN
);
4257 s
->next
= new_stmt(BPF_ST
);
4258 s
->next
->s
.k
= regno
;
4273 a
= (struct arth
*)newchunk(sizeof(*a
));
4277 s
= new_stmt(BPF_LD
|BPF_IMM
);
4279 s
->next
= new_stmt(BPF_ST
);
4295 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4298 s
= new_stmt(BPF_ST
);
4306 gen_arth(code
, a0
, a1
)
4308 struct arth
*a0
, *a1
;
4310 struct slist
*s0
, *s1
, *s2
;
4314 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4319 sappend(a0
->s
, a1
->s
);
4321 free_reg(a0
->regno
);
4322 free_reg(a1
->regno
);
4324 s0
= new_stmt(BPF_ST
);
4325 a0
->regno
= s0
->s
.k
= alloc_reg();
4332 * Here we handle simple allocation of the scratch registers.
4333 * If too many registers are alloc'd, the allocator punts.
4335 static int regused
[BPF_MEMWORDS
];
4339 * Return the next free register.
4344 int n
= BPF_MEMWORDS
;
4347 if (regused
[curreg
])
4348 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4350 regused
[curreg
] = 1;
4354 bpf_error("too many registers needed to evaluate expression");
4359 * Return a register to the table so it can
4369 static struct block
*
4376 s
= new_stmt(BPF_LD
|BPF_LEN
);
4377 b
= new_block(JMP(jmp
));
4388 return gen_len(BPF_JGE
, n
);
4392 * Actually, this is less than or equal.
4400 b
= gen_len(BPF_JGT
, n
);
4407 gen_byteop(op
, idx
, val
)
4418 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4421 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4422 b
->s
.code
= JMP(BPF_JGE
);
4427 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4428 b
->s
.code
= JMP(BPF_JGT
);
4432 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4436 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4440 b
= new_block(JMP(BPF_JEQ
));
4447 static u_char abroadcast
[] = { 0x0 };
4450 gen_broadcast(proto
)
4453 bpf_u_int32 hostmask
;
4454 struct block
*b0
, *b1
, *b2
;
4455 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4461 if (linktype
== DLT_ARCNET
)
4462 return gen_ahostop(abroadcast
, Q_DST
);
4463 if (linktype
== DLT_EN10MB
)
4464 return gen_ehostop(ebroadcast
, Q_DST
);
4465 if (linktype
== DLT_FDDI
)
4466 return gen_fhostop(ebroadcast
, Q_DST
);
4467 if (linktype
== DLT_IEEE802
)
4468 return gen_thostop(ebroadcast
, Q_DST
);
4469 if (linktype
== DLT_IEEE802_11
)
4470 return gen_wlanhostop(ebroadcast
, Q_DST
);
4471 if (linktype
== DLT_SUNATM
&& is_lane
) {
4473 * Check that the packet doesn't begin with an
4474 * LE Control marker. (We've already generated
4477 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4481 * Now check the MAC address.
4483 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4487 bpf_error("not a broadcast link");
4491 b0
= gen_linktype(ETHERTYPE_IP
);
4492 hostmask
= ~netmask
;
4493 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4494 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4495 (bpf_int32
)(~0 & hostmask
), hostmask
);
4500 bpf_error("only ether/ip broadcast filters supported");
4504 * Generate code to test the low-order bit of a MAC address (that's
4505 * the bottom bit of the *first* byte).
4507 static struct block
*
4508 gen_mac_multicast(offset
)
4511 register struct block
*b0
;
4512 register struct slist
*s
;
4514 /* link[offset] & 1 != 0 */
4515 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4517 b0
= new_block(JMP(BPF_JSET
));
4524 gen_multicast(proto
)
4527 register struct block
*b0
, *b1
, *b2
;
4528 register struct slist
*s
;
4534 if (linktype
== DLT_ARCNET
)
4535 /* all ARCnet multicasts use the same address */
4536 return gen_ahostop(abroadcast
, Q_DST
);
4538 if (linktype
== DLT_EN10MB
) {
4539 /* ether[0] & 1 != 0 */
4540 return gen_mac_multicast(0);
4543 if (linktype
== DLT_FDDI
) {
4545 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4547 * XXX - was that referring to bit-order issues?
4549 /* fddi[1] & 1 != 0 */
4550 return gen_mac_multicast(1);
4553 if (linktype
== DLT_IEEE802
) {
4554 /* tr[2] & 1 != 0 */
4555 return gen_mac_multicast(2);
4558 if (linktype
== DLT_IEEE802_11
) {
4562 * For control frames, there is no DA.
4564 * For management frames, DA is at an
4565 * offset of 4 from the beginning of
4568 * For data frames, DA is at an offset
4569 * of 4 from the beginning of the packet
4570 * if To DS is clear and at an offset of
4571 * 16 from the beginning of the packet
4576 * Generate the tests to be done for data frames.
4578 * First, check for To DS set, i.e. "link[1] & 0x01".
4580 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4582 b1
= new_block(JMP(BPF_JSET
));
4583 b1
->s
.k
= 0x01; /* To DS */
4587 * If To DS is set, the DA is at 16.
4589 b0
= gen_mac_multicast(16);
4593 * Now, check for To DS not set, i.e. check
4594 * "!(link[1] & 0x01)".
4596 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4598 b2
= new_block(JMP(BPF_JSET
));
4599 b2
->s
.k
= 0x01; /* To DS */
4604 * If To DS is not set, the DA is at 4.
4606 b1
= gen_mac_multicast(4);
4610 * Now OR together the last two checks. That gives
4611 * the complete set of checks for data frames.
4616 * Now check for a data frame.
4617 * I.e, check "link[0] & 0x08".
4619 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4621 b1
= new_block(JMP(BPF_JSET
));
4626 * AND that with the checks done for data frames.
4631 * If the high-order bit of the type value is 0, this
4632 * is a management frame.
4633 * I.e, check "!(link[0] & 0x08)".
4635 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4637 b2
= new_block(JMP(BPF_JSET
));
4643 * For management frames, the DA is at 4.
4645 b1
= gen_mac_multicast(4);
4649 * OR that with the checks done for data frames.
4650 * That gives the checks done for management and
4656 * If the low-order bit of the type value is 1,
4657 * this is either a control frame or a frame
4658 * with a reserved type, and thus not a
4661 * I.e., check "!(link[0] & 0x04)".
4663 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4665 b1
= new_block(JMP(BPF_JSET
));
4671 * AND that with the checks for data and management
4678 if (linktype
== DLT_SUNATM
&& is_lane
) {
4680 * Check that the packet doesn't begin with an
4681 * LE Control marker. (We've already generated
4684 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4687 /* ether[off_mac] & 1 != 0 */
4688 b0
= gen_mac_multicast(off_mac
);
4693 /* Link not known to support multicasts */
4697 b0
= gen_linktype(ETHERTYPE_IP
);
4698 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4699 b1
->s
.code
= JMP(BPF_JGE
);
4705 b0
= gen_linktype(ETHERTYPE_IPV6
);
4706 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4711 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4715 * generate command for inbound/outbound. It's here so we can
4716 * make it link-type specific. 'dir' = 0 implies "inbound",
4717 * = 1 implies "outbound".
4723 register struct block
*b0
;
4726 * Only some data link types support inbound/outbound qualifiers.
4731 b0
= gen_relation(BPF_JEQ
,
4732 gen_load(Q_LINK
, gen_loadi(0), 1),
4740 * Match packets sent by this machine.
4742 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
4745 * Match packets sent to this machine.
4746 * (No broadcast or multicast packets, or
4747 * packets sent to some other machine and
4748 * received promiscuously.)
4750 * XXX - packets sent to other machines probably
4751 * shouldn't be matched, but what about broadcast
4752 * or multicast packets we received?
4754 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
4759 bpf_error("inbound/outbound not supported on linktype %d\n",
4769 register const u_char
*eaddr
;
4772 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4773 if (linktype
== DLT_ARCNET
)
4774 return gen_ahostop(eaddr
, (int)q
.dir
);
4776 bpf_error("ARCnet address used in non-arc expression");
4780 static struct block
*
4781 gen_ahostop(eaddr
, dir
)
4782 register const u_char
*eaddr
;
4785 register struct block
*b0
, *b1
;
4788 /* src comes first, different from Ethernet */
4790 return gen_bcmp(0, 1, eaddr
);
4793 return gen_bcmp(1, 1, eaddr
);
4796 b0
= gen_ahostop(eaddr
, Q_SRC
);
4797 b1
= gen_ahostop(eaddr
, Q_DST
);
4803 b0
= gen_ahostop(eaddr
, Q_SRC
);
4804 b1
= gen_ahostop(eaddr
, Q_DST
);
4813 * support IEEE 802.1Q VLAN trunk over ethernet
4822 * Change the offsets to point to the type and data fields within
4823 * the VLAN packet. This is somewhat of a kludge.
4825 if (orig_nl
== (u_int
)-1) {
4826 orig_linktype
= off_linktype
; /* save original values */
4828 orig_nl_nosnap
= off_nl_nosnap
;
4839 bpf_error("no VLAN support for data link type %d",
4845 /* check for VLAN */
4846 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4848 /* If a specific VLAN is requested, check VLAN id */
4849 if (vlan_num
>= 0) {
4852 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
4861 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
4873 bpf_error("'vpi' supported only on raw ATM");
4876 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
4877 (u_int
)jvalue
, reverse
);
4882 bpf_error("'vci' supported only on raw ATM");
4885 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
4886 (u_int
)jvalue
, reverse
);
4890 if (off_proto
== -1)
4891 abort(); /* XXX - this isn't on FreeBSD */
4892 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
4893 (u_int
)jvalue
, reverse
);
4897 if (off_payload
== -1)
4899 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
4900 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
4905 bpf_error("'callref' supported only on raw ATM");
4906 if (off_proto
== -1)
4908 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
4909 (u_int
)jvalue
, reverse
);
4919 gen_atmtype_abbrev(type
)
4922 struct block
*b0
, *b1
;
4927 /* Get all packets in Meta signalling Circuit */
4929 bpf_error("'metac' supported only on raw ATM");
4930 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4931 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
4936 /* Get all packets in Broadcast Circuit*/
4938 bpf_error("'bcc' supported only on raw ATM");
4939 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4940 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
4945 /* Get all cells in Segment OAM F4 circuit*/
4947 bpf_error("'oam4sc' supported only on raw ATM");
4948 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4949 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
4954 /* Get all cells in End-to-End OAM F4 Circuit*/
4956 bpf_error("'oam4ec' supported only on raw ATM");
4957 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4958 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
4963 /* Get all packets in connection Signalling Circuit */
4965 bpf_error("'sc' supported only on raw ATM");
4966 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4967 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
4972 /* Get all packets in ILMI Circuit */
4974 bpf_error("'ilmic' supported only on raw ATM");
4975 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
4976 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
4981 /* Get all LANE packets */
4983 bpf_error("'lane' supported only on raw ATM");
4984 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
4987 * Arrange that all subsequent tests assume LANE
4988 * rather than LLC-encapsulated packets, and set
4989 * the offsets appropriately for LANE-encapsulated
4992 * "off_mac" is the offset of the Ethernet header,
4993 * which is 2 bytes past the ATM pseudo-header
4994 * (skipping the pseudo-header and 2-byte LE Client
4995 * field). The other offsets are Ethernet offsets
4996 * relative to "off_mac".
4999 off_mac
= off_payload
+ 2; /* MAC header */
5000 off_linktype
= off_mac
+ 12;
5001 off_nl
= off_mac
+ 14; /* Ethernet II */
5002 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5006 /* Get all LLC-encapsulated packets */
5008 bpf_error("'llc' supported only on raw ATM");
5009 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5020 static struct block
*
5021 gen_msg_abbrev(type
)
5027 * Q.2931 signalling protocol messages for handling virtual circuits
5028 * establishment and teardown
5033 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5037 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5041 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5045 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5049 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5052 case A_RELEASE_DONE
:
5053 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5063 gen_atmmulti_abbrev(type
)
5066 struct block
*b0
, *b1
;
5072 bpf_error("'oam' supported only on raw ATM");
5073 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5078 bpf_error("'oamf4' supported only on raw ATM");
5080 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5081 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5083 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5089 * Get Q.2931 signalling messages for switched
5090 * virtual connection
5093 bpf_error("'connectmsg' supported only on raw ATM");
5094 b0
= gen_msg_abbrev(A_SETUP
);
5095 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5097 b0
= gen_msg_abbrev(A_CONNECT
);
5099 b0
= gen_msg_abbrev(A_CONNECTACK
);
5101 b0
= gen_msg_abbrev(A_RELEASE
);
5103 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5105 b0
= gen_atmtype_abbrev(A_SC
);
5111 bpf_error("'metaconnect' supported only on raw ATM");
5112 b0
= gen_msg_abbrev(A_SETUP
);
5113 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5115 b0
= gen_msg_abbrev(A_CONNECT
);
5117 b0
= gen_msg_abbrev(A_RELEASE
);
5119 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5121 b0
= gen_atmtype_abbrev(A_METAC
);