]>
The Tcpdump Group git mirrors - libpcap/blob - gencode.c
1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.182 2002-12-06 00:01:33 hannes Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
40 * XXX - why was this included even on UNIX?
49 #include <sys/param.h>
52 #include <netinet/in.h>
64 #include "ethertype.h"
69 #include "sunatmpos.h"
75 #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
:
843 off_nl
= 4; /* FIXME SNAP */
846 case DLT_ATM_CLIP
: /* Linux ATM defines this */
848 * assume routed, non-ISO PDUs
849 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
852 off_nl
= 8; /* 802.2+SNAP */
853 off_nl_nosnap
= 3; /* 802.2 */
858 * Full Frontal ATM; you get AALn PDUs with an ATM
862 off_vpi
= SUNATM_VPI_POS
;
863 off_vci
= SUNATM_VCI_POS
;
864 off_proto
= PROTO_POS
;
865 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
866 off_payload
= SUNATM_PKT_BEGIN_POS
;
867 off_linktype
= off_payload
;
868 off_nl
= off_payload
+8; /* 802.2+SNAP */
869 off_nl_nosnap
= off_payload
+3; /* 802.2 */
875 off_nl_nosnap
= 0; /* no 802.2 LLC */
878 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
881 off_nl_nosnap
= 16; /* no 802.2 LLC */
886 * LocalTalk does have a 1-byte type field in the LLAP header,
887 * but really it just indicates whether there is a "short" or
888 * "long" DDP packet following.
892 off_nl_nosnap
= 0; /* no 802.2 LLC */
897 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
898 * link-level type field. We set "off_linktype" to the
899 * offset of the LLC header.
901 * To check for Ethernet types, we assume that SSAP = SNAP
902 * is being used and pick out the encapsulated Ethernet type.
903 * XXX - should we generate code to check for SNAP? RFC
904 * 2625 says SNAP should be used.
907 off_nl
= 24; /* IPFC+802.2+SNAP */
908 off_nl_nosnap
= 19; /* IPFC+802.2 */
913 * XXX - we should set this to handle SNAP-encapsulated
914 * frames (NLPID of 0x80).
918 off_nl_nosnap
= 0; /* no 802.2 LLC */
921 bpf_error("unknown data link type %d", linktype
);
925 static struct block
*
932 s
= new_stmt(BPF_LD
|BPF_IMM
);
934 b
= new_block(JMP(BPF_JEQ
));
940 static inline struct block
*
943 return gen_uncond(1);
946 static inline struct block
*
949 return gen_uncond(0);
953 * Byte-swap a 32-bit number.
954 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
955 * big-endian platforms.)
957 #define SWAPLONG(y) \
958 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
960 static struct block
*
961 gen_ether_linktype(proto
)
964 struct block
*b0
, *b1
;
970 * OSI protocols always use 802.2 encapsulation.
972 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
974 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
975 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
980 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
982 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
983 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
989 * NetBEUI always uses 802.2 encapsulation.
991 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
993 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
994 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1002 * Ethernet_II frames, which are Ethernet
1003 * frames with a frame type of ETHERTYPE_IPX;
1005 * Ethernet_802.3 frames, which are 802.3
1006 * frames (i.e., the type/length field is
1007 * a length field, <= ETHERMTU, rather than
1008 * a type field) with the first two bytes
1009 * after the Ethernet/802.3 header being
1012 * Ethernet_802.2 frames, which are 802.3
1013 * frames with an 802.2 LLC header and
1014 * with the IPX LSAP as the DSAP in the LLC
1017 * Ethernet_SNAP frames, which are 802.3
1018 * frames with an LLC header and a SNAP
1019 * header and with an OUI of 0x000000
1020 * (encapsulated Ethernet) and a protocol
1021 * ID of ETHERTYPE_IPX in the SNAP header.
1023 * XXX - should we generate the same code both
1024 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1028 * This generates code to check both for the
1029 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1031 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1032 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1036 * Now we add code to check for SNAP frames with
1037 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1039 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1043 * Now we generate code to check for 802.3
1044 * frames in general.
1046 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1050 * Now add the check for 802.3 frames before the
1051 * check for Ethernet_802.2 and Ethernet_802.3,
1052 * as those checks should only be done on 802.3
1053 * frames, not on Ethernet frames.
1058 * Now add the check for Ethernet_II frames, and
1059 * do that before checking for the other frame
1062 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1066 case ETHERTYPE_ATALK
:
1067 case ETHERTYPE_AARP
:
1069 * EtherTalk (AppleTalk protocols on Ethernet link
1070 * layer) may use 802.2 encapsulation.
1074 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1075 * we check for an Ethernet type field less than
1076 * 1500, which means it's an 802.3 length field.
1078 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1082 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1083 * SNAP packets with an organization code of
1084 * 0x080007 (Apple, for Appletalk) and a protocol
1085 * type of ETHERTYPE_ATALK (Appletalk).
1087 * 802.2-encapsulated ETHERTYPE_AARP packets are
1088 * SNAP packets with an organization code of
1089 * 0x000000 (encapsulated Ethernet) and a protocol
1090 * type of ETHERTYPE_AARP (Appletalk ARP).
1092 if (proto
== ETHERTYPE_ATALK
)
1093 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1094 else /* proto == ETHERTYPE_AARP */
1095 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1099 * Check for Ethernet encapsulation (Ethertalk
1100 * phase 1?); we just check for the Ethernet
1103 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1109 if (proto
<= ETHERMTU
) {
1111 * This is an LLC SAP value, so the frames
1112 * that match would be 802.2 frames.
1113 * Check that the frame is an 802.2 frame
1114 * (i.e., that the length/type field is
1115 * a length field, <= ETHERMTU) and
1116 * then check the DSAP.
1118 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1120 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1125 * This is an Ethernet type, so compare
1126 * the length/type field with it (if
1127 * the frame is an 802.2 frame, the length
1128 * field will be <= ETHERMTU, and, as
1129 * "proto" is > ETHERMTU, this test
1130 * will fail and the frame won't match,
1131 * which is what we want).
1133 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1138 static struct block
*
1142 struct block
*b0
, *b1
, *b2
;
1147 return gen_ether_linktype(proto
);
1153 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1156 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1159 case DLT_IEEE802_11
:
1160 case DLT_PRISM_HEADER
:
1163 case DLT_ATM_RFC1483
:
1165 case DLT_IP_OVER_FC
:
1166 return gen_llc(proto
);
1171 * If "is_lane" is set, check for a LANE-encapsulated
1172 * version of this protocol, otherwise check for an
1173 * LLC-encapsulated version of this protocol.
1175 * We assume LANE means Ethernet, not Token Ring.
1179 * Check that the packet doesn't begin with an
1180 * LE Control marker. (We've already generated
1183 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1187 * Now generate an Ethernet test.
1189 b1
= gen_ether_linktype(proto
);
1194 * Check for LLC encapsulation and then check the
1197 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1198 b1
= gen_llc(proto
);
1207 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1208 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1209 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1215 * OSI protocols always use 802.2 encapsulation.
1217 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1218 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1219 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1223 case LLCSAP_NETBEUI
:
1225 * NetBEUI always uses 802.2 encapsulation.
1226 * XXX - should we check both the DSAP and the
1227 * LSAP, like this, or should we check just the
1230 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1231 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1232 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1238 * Ethernet_II frames, which are Ethernet
1239 * frames with a frame type of ETHERTYPE_IPX;
1241 * Ethernet_802.3 frames, which have a frame
1242 * type of LINUX_SLL_P_802_3;
1244 * Ethernet_802.2 frames, which are 802.3
1245 * frames with an 802.2 LLC header (i.e, have
1246 * a frame type of LINUX_SLL_P_802_2) and
1247 * with the IPX LSAP as the DSAP in the LLC
1250 * Ethernet_SNAP frames, which are 802.3
1251 * frames with an LLC header and a SNAP
1252 * header and with an OUI of 0x000000
1253 * (encapsulated Ethernet) and a protocol
1254 * ID of ETHERTYPE_IPX in the SNAP header.
1256 * First, do the checks on LINUX_SLL_P_802_2
1257 * frames; generate the check for either
1258 * Ethernet_802.2 or Ethernet_SNAP frames, and
1259 * then put a check for LINUX_SLL_P_802_2 frames
1262 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1263 (bpf_int32
)LLCSAP_IPX
);
1264 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1267 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1271 * Now check for 802.3 frames and OR that with
1272 * the previous test.
1274 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1278 * Now add the check for Ethernet_II frames, and
1279 * do that before checking for the other frame
1282 b0
= gen_cmp(off_linktype
, BPF_H
,
1283 (bpf_int32
)ETHERTYPE_IPX
);
1287 case ETHERTYPE_ATALK
:
1288 case ETHERTYPE_AARP
:
1290 * EtherTalk (AppleTalk protocols on Ethernet link
1291 * layer) may use 802.2 encapsulation.
1295 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1296 * we check for the 802.2 protocol type in the
1297 * "Ethernet type" field.
1299 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1302 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1303 * SNAP packets with an organization code of
1304 * 0x080007 (Apple, for Appletalk) and a protocol
1305 * type of ETHERTYPE_ATALK (Appletalk).
1307 * 802.2-encapsulated ETHERTYPE_AARP packets are
1308 * SNAP packets with an organization code of
1309 * 0x000000 (encapsulated Ethernet) and a protocol
1310 * type of ETHERTYPE_AARP (Appletalk ARP).
1312 if (proto
== ETHERTYPE_ATALK
)
1313 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1315 else /* proto == ETHERTYPE_AARP */
1316 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1321 * Check for Ethernet encapsulation (Ethertalk
1322 * phase 1?); we just check for the Ethernet
1325 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1331 if (proto
<= ETHERMTU
) {
1333 * This is an LLC SAP value, so the frames
1334 * that match would be 802.2 frames.
1335 * Check for the 802.2 protocol type
1336 * in the "Ethernet type" field, and
1337 * then check the DSAP.
1339 b0
= gen_cmp(off_linktype
, BPF_H
,
1341 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1347 * This is an Ethernet type, so compare
1348 * the length/type field with it (if
1349 * the frame is an 802.2 frame, the length
1350 * field will be <= ETHERMTU, and, as
1351 * "proto" is > ETHERMTU, this test
1352 * will fail and the frame won't match,
1353 * which is what we want).
1355 return gen_cmp(off_linktype
, BPF_H
,
1362 case DLT_SLIP_BSDOS
:
1365 * These types don't provide any type field; packets
1368 * XXX - for IPv4, check for a version number of 4, and,
1369 * for IPv6, check for a version number of 6?
1375 case ETHERTYPE_IPV6
:
1377 return gen_true(); /* always true */
1380 return gen_false(); /* always false */
1385 case DLT_PPP_SERIAL
:
1388 * We use Ethernet protocol types inside libpcap;
1389 * map them to the corresponding PPP protocol types.
1398 case ETHERTYPE_IPV6
:
1407 case ETHERTYPE_ATALK
:
1421 * I'm assuming the "Bridging PDU"s that go
1422 * over PPP are Spanning Tree Protocol
1436 * We use Ethernet protocol types inside libpcap;
1437 * map them to the corresponding PPP protocol types.
1442 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1443 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1445 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1450 case ETHERTYPE_IPV6
:
1460 case ETHERTYPE_ATALK
:
1474 * I'm assuming the "Bridging PDU"s that go
1475 * over PPP are Spanning Tree Protocol
1490 * For DLT_NULL, the link-layer header is a 32-bit
1491 * word containing an AF_ value in *host* byte order.
1493 * In addition, if we're reading a saved capture file,
1494 * the host byte order in the capture may not be the
1495 * same as the host byte order on this machine.
1497 * For DLT_LOOP, the link-layer header is a 32-bit
1498 * word containing an AF_ value in *network* byte order.
1500 * XXX - AF_ values may, unfortunately, be platform-
1501 * dependent; for example, FreeBSD's AF_INET6 is 24
1502 * whilst NetBSD's and OpenBSD's is 26.
1504 * This means that, when reading a capture file, just
1505 * checking for our AF_INET6 value won't work if the
1506 * capture file came from another OS.
1515 case ETHERTYPE_IPV6
:
1522 * Not a type on which we support filtering.
1523 * XXX - support those that have AF_ values
1524 * #defined on this platform, at least?
1529 if (linktype
== DLT_NULL
) {
1531 * The AF_ value is in host byte order, but
1532 * the BPF interpreter will convert it to
1533 * network byte order.
1535 * If this is a save file, and it's from a
1536 * machine with the opposite byte order to
1537 * ours, we byte-swap the AF_ value.
1539 * Then we run it through "htonl()", and
1540 * generate code to compare against the result.
1542 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1543 bpf_pcap
->sf
.swapped
)
1544 proto
= SWAPLONG(proto
);
1545 proto
= htonl(proto
);
1547 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1551 * XXX should we check for first fragment if the protocol
1558 case ETHERTYPE_IPV6
:
1559 return(gen_cmp(2, BPF_B
,
1560 (bpf_int32
)htonl(ARCTYPE_INET6
)));
1563 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_IP
));
1564 b1
= gen_cmp(2, BPF_B
,
1565 (bpf_int32
)htonl(ARCTYPE_IP_OLD
));
1569 b0
= gen_cmp(2, BPF_B
, (bpf_int32
)htonl(ARCTYPE_ARP
));
1570 b1
= gen_cmp(2, BPF_B
,
1571 (bpf_int32
)htonl(ARCTYPE_ARP_OLD
));
1574 case ETHERTYPE_REVARP
:
1575 return(gen_cmp(2, BPF_B
,
1576 (bpf_int32
)htonl(ARCTYPE_REVARP
)));
1577 case ETHERTYPE_ATALK
:
1578 return(gen_cmp(2, BPF_B
,
1579 (bpf_int32
)htonl(ARCTYPE_ATALK
)));
1585 case ETHERTYPE_ATALK
:
1594 * XXX - assumes a 2-byte Frame Relay header with
1595 * DLCI and flags. What if the address is longer?
1601 * Check for the special NLPID for IP.
1603 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1606 case ETHERTYPE_IPV6
:
1608 * Check for the special NLPID for IPv6.
1610 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1615 * Check for several OSI protocols.
1617 * Frame Relay packets typically have an OSI
1618 * NLPID at the beginning; we check for each
1621 * What we check for is the NLPID and a frame
1622 * control field of UI, i.e. 0x03 followed
1625 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1626 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1627 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1639 * All the types that have no encapsulation should either be
1640 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1641 * all packets are IP packets, or should be handled in some
1642 * special case, if none of them are (if some are and some
1643 * aren't, the lack of encapsulation is a problem, as we'd
1644 * have to find some other way of determining the packet type).
1646 * Therefore, if "off_linktype" is -1, there's an error.
1648 if (off_linktype
== -1)
1652 * Any type not handled above should always have an Ethernet
1653 * type at an offset of "off_linktype". (PPP is partially
1654 * handled above - the protocol type is mapped from the
1655 * Ethernet and LLC types we use internally to the corresponding
1656 * PPP type - but the PPP type is always specified by a value
1657 * at "off_linktype", so we don't have to do the code generation
1660 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1664 * Check for an LLC SNAP packet with a given organization code and
1665 * protocol type; we check the entire contents of the 802.2 LLC and
1666 * snap headers, checking for DSAP and SSAP of SNAP and a control
1667 * field of 0x03 in the LLC header, and for the specified organization
1668 * code and protocol type in the SNAP header.
1670 static struct block
*
1671 gen_snap(orgcode
, ptype
, offset
)
1672 bpf_u_int32 orgcode
;
1676 u_char snapblock
[8];
1678 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1679 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1680 snapblock
[2] = 0x03; /* control = UI */
1681 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1682 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1683 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1684 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1685 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1686 return gen_bcmp(offset
, 8, snapblock
);
1690 * Check for a given protocol value assuming an 802.2 LLC header.
1692 static struct block
*
1697 * XXX - handle token-ring variable-length header.
1702 return gen_cmp(off_linktype
, BPF_H
, (long)
1703 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1706 return gen_cmp(off_linktype
, BPF_H
, (long)
1707 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1709 case LLCSAP_NETBEUI
:
1710 return gen_cmp(off_linktype
, BPF_H
, (long)
1711 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1715 * XXX - are there ever SNAP frames for IPX on
1716 * non-Ethernet 802.x networks?
1718 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1720 case ETHERTYPE_ATALK
:
1722 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1723 * SNAP packets with an organization code of
1724 * 0x080007 (Apple, for Appletalk) and a protocol
1725 * type of ETHERTYPE_ATALK (Appletalk).
1727 * XXX - check for an organization code of
1728 * encapsulated Ethernet as well?
1730 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1734 * XXX - we don't have to check for IPX 802.3
1735 * here, but should we check for the IPX Ethertype?
1737 if (proto
<= ETHERMTU
) {
1739 * This is an LLC SAP value, so check
1742 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1745 * This is an Ethernet type; we assume that it's
1746 * unlikely that it'll appear in the right place
1747 * at random, and therefore check only the
1748 * location that would hold the Ethernet type
1749 * in a SNAP frame with an organization code of
1750 * 0x000000 (encapsulated Ethernet).
1752 * XXX - if we were to check for the SNAP DSAP and
1753 * LSAP, as per XXX, and were also to check for an
1754 * organization code of 0x000000 (encapsulated
1755 * Ethernet), we'd do
1757 * return gen_snap(0x000000, proto,
1760 * here; for now, we don't, as per the above.
1761 * I don't know whether it's worth the extra CPU
1762 * time to do the right check or not.
1764 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1769 static struct block
*
1770 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1774 u_int src_off
, dst_off
;
1776 struct block
*b0
, *b1
;
1790 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1791 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1797 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1798 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1805 b0
= gen_linktype(proto
);
1806 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1812 static struct block
*
1813 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1814 struct in6_addr
*addr
;
1815 struct in6_addr
*mask
;
1817 u_int src_off
, dst_off
;
1819 struct block
*b0
, *b1
;
1834 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1835 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1841 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1842 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1849 /* this order is important */
1850 a
= (u_int32_t
*)addr
;
1851 m
= (u_int32_t
*)mask
;
1852 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1853 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1855 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1857 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1859 b0
= gen_linktype(proto
);
1865 static struct block
*
1866 gen_ehostop(eaddr
, dir
)
1867 register const u_char
*eaddr
;
1870 register struct block
*b0
, *b1
;
1874 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
1877 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
1880 b0
= gen_ehostop(eaddr
, Q_SRC
);
1881 b1
= gen_ehostop(eaddr
, Q_DST
);
1887 b0
= gen_ehostop(eaddr
, Q_SRC
);
1888 b1
= gen_ehostop(eaddr
, Q_DST
);
1897 * Like gen_ehostop, but for DLT_FDDI
1899 static struct block
*
1900 gen_fhostop(eaddr
, dir
)
1901 register const u_char
*eaddr
;
1904 struct block
*b0
, *b1
;
1909 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1911 return gen_bcmp(6 + 1, 6, eaddr
);
1916 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1918 return gen_bcmp(0 + 1, 6, eaddr
);
1922 b0
= gen_fhostop(eaddr
, Q_SRC
);
1923 b1
= gen_fhostop(eaddr
, Q_DST
);
1929 b0
= gen_fhostop(eaddr
, Q_SRC
);
1930 b1
= gen_fhostop(eaddr
, Q_DST
);
1939 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1941 static struct block
*
1942 gen_thostop(eaddr
, dir
)
1943 register const u_char
*eaddr
;
1946 register struct block
*b0
, *b1
;
1950 return gen_bcmp(8, 6, eaddr
);
1953 return gen_bcmp(2, 6, eaddr
);
1956 b0
= gen_thostop(eaddr
, Q_SRC
);
1957 b1
= gen_thostop(eaddr
, Q_DST
);
1963 b0
= gen_thostop(eaddr
, Q_SRC
);
1964 b1
= gen_thostop(eaddr
, Q_DST
);
1973 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
1975 static struct block
*
1976 gen_wlanhostop(eaddr
, dir
)
1977 register const u_char
*eaddr
;
1980 register struct block
*b0
, *b1
, *b2
;
1981 register struct slist
*s
;
1988 * For control frames, there is no SA.
1990 * For management frames, SA is at an
1991 * offset of 10 from the beginning of
1994 * For data frames, SA is at an offset
1995 * of 10 from the beginning of the packet
1996 * if From DS is clear, at an offset of
1997 * 16 from the beginning of the packet
1998 * if From DS is set and To DS is clear,
1999 * and an offset of 24 from the beginning
2000 * of the packet if From DS is set and To DS
2005 * Generate the tests to be done for data frames
2008 * First, check for To DS set, i.e. check "link[1] & 0x01".
2010 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2012 b1
= new_block(JMP(BPF_JSET
));
2013 b1
->s
.k
= 0x01; /* To DS */
2017 * If To DS is set, the SA is at 24.
2019 b0
= gen_bcmp(24, 6, eaddr
);
2023 * Now, check for To DS not set, i.e. check
2024 * "!(link[1] & 0x01)".
2026 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2028 b2
= new_block(JMP(BPF_JSET
));
2029 b2
->s
.k
= 0x01; /* To DS */
2034 * If To DS is not set, the SA is at 16.
2036 b1
= gen_bcmp(16, 6, eaddr
);
2040 * Now OR together the last two checks. That gives
2041 * the complete set of checks for data frames with
2047 * Now check for From DS being set, and AND that with
2048 * the ORed-together checks.
2050 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2052 b1
= new_block(JMP(BPF_JSET
));
2053 b1
->s
.k
= 0x02; /* From DS */
2058 * Now check for data frames with From DS not set.
2060 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2062 b2
= new_block(JMP(BPF_JSET
));
2063 b2
->s
.k
= 0x02; /* From DS */
2068 * If From DS isn't set, the SA is at 10.
2070 b1
= gen_bcmp(10, 6, eaddr
);
2074 * Now OR together the checks for data frames with
2075 * From DS not set and for data frames with From DS
2076 * set; that gives the checks done for data frames.
2081 * Now check for a data frame.
2082 * I.e, check "link[0] & 0x08".
2084 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2086 b1
= new_block(JMP(BPF_JSET
));
2091 * AND that with the checks done for data frames.
2096 * If the high-order bit of the type value is 0, this
2097 * is a management frame.
2098 * I.e, check "!(link[0] & 0x08)".
2100 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2102 b2
= new_block(JMP(BPF_JSET
));
2108 * For management frames, the SA is at 10.
2110 b1
= gen_bcmp(10, 6, eaddr
);
2114 * OR that with the checks done for data frames.
2115 * That gives the checks done for management and
2121 * If the low-order bit of the type value is 1,
2122 * this is either a control frame or a frame
2123 * with a reserved type, and thus not a
2126 * I.e., check "!(link[0] & 0x04)".
2128 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2130 b1
= new_block(JMP(BPF_JSET
));
2136 * AND that with the checks for data and management
2146 * For control frames, there is no DA.
2148 * For management frames, DA is at an
2149 * offset of 4 from the beginning of
2152 * For data frames, DA is at an offset
2153 * of 4 from the beginning of the packet
2154 * if To DS is clear and at an offset of
2155 * 16 from the beginning of the packet
2160 * Generate the tests to be done for data frames.
2162 * First, check for To DS set, i.e. "link[1] & 0x01".
2164 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2166 b1
= new_block(JMP(BPF_JSET
));
2167 b1
->s
.k
= 0x01; /* To DS */
2171 * If To DS is set, the DA is at 16.
2173 b0
= gen_bcmp(16, 6, eaddr
);
2177 * Now, check for To DS not set, i.e. check
2178 * "!(link[1] & 0x01)".
2180 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2182 b2
= new_block(JMP(BPF_JSET
));
2183 b2
->s
.k
= 0x01; /* To DS */
2188 * If To DS is not set, the DA is at 4.
2190 b1
= gen_bcmp(4, 6, eaddr
);
2194 * Now OR together the last two checks. That gives
2195 * the complete set of checks for data frames.
2200 * Now check for a data frame.
2201 * I.e, check "link[0] & 0x08".
2203 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2205 b1
= new_block(JMP(BPF_JSET
));
2210 * AND that with the checks done for data frames.
2215 * If the high-order bit of the type value is 0, this
2216 * is a management frame.
2217 * I.e, check "!(link[0] & 0x08)".
2219 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2221 b2
= new_block(JMP(BPF_JSET
));
2227 * For management frames, the DA is at 4.
2229 b1
= gen_bcmp(4, 6, eaddr
);
2233 * OR that with the checks done for data frames.
2234 * That gives the checks done for management and
2240 * If the low-order bit of the type value is 1,
2241 * this is either a control frame or a frame
2242 * with a reserved type, and thus not a
2245 * I.e., check "!(link[0] & 0x04)".
2247 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2249 b1
= new_block(JMP(BPF_JSET
));
2255 * AND that with the checks for data and management
2262 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2263 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2269 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2270 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2279 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2280 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2281 * as the RFC states.)
2283 static struct block
*
2284 gen_ipfchostop(eaddr
, dir
)
2285 register const u_char
*eaddr
;
2288 register struct block
*b0
, *b1
;
2292 return gen_bcmp(10, 6, eaddr
);
2295 return gen_bcmp(2, 6, eaddr
);
2298 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2299 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2305 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2306 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2315 * This is quite tricky because there may be pad bytes in front of the
2316 * DECNET header, and then there are two possible data packet formats that
2317 * carry both src and dst addresses, plus 5 packet types in a format that
2318 * carries only the src node, plus 2 types that use a different format and
2319 * also carry just the src node.
2323 * Instead of doing those all right, we just look for data packets with
2324 * 0 or 1 bytes of padding. If you want to look at other packets, that
2325 * will require a lot more hacking.
2327 * To add support for filtering on DECNET "areas" (network numbers)
2328 * one would want to add a "mask" argument to this routine. That would
2329 * make the filter even more inefficient, although one could be clever
2330 * and not generate masking instructions if the mask is 0xFFFF.
2332 static struct block
*
2333 gen_dnhostop(addr
, dir
, base_off
)
2338 struct block
*b0
, *b1
, *b2
, *tmp
;
2339 u_int offset_lh
; /* offset if long header is received */
2340 u_int offset_sh
; /* offset if short header is received */
2345 offset_sh
= 1; /* follows flags */
2346 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2350 offset_sh
= 3; /* follows flags, dstnode */
2351 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2355 /* Inefficient because we do our Calvinball dance twice */
2356 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2357 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2363 /* Inefficient because we do our Calvinball dance twice */
2364 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2365 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2370 bpf_error("ISO host filtering not implemented");
2375 b0
= gen_linktype(ETHERTYPE_DN
);
2376 /* Check for pad = 1, long header case */
2377 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2378 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2379 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2380 BPF_H
, (bpf_int32
)ntohs(addr
));
2382 /* Check for pad = 0, long header case */
2383 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2384 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2387 /* Check for pad = 1, short header case */
2388 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2389 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2390 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2391 BPF_H
, (bpf_int32
)ntohs(addr
));
2394 /* Check for pad = 0, short header case */
2395 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2396 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2400 /* Combine with test for linktype */
2405 static struct block
*
2406 gen_host(addr
, mask
, proto
, dir
)
2412 struct block
*b0
, *b1
;
2417 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2418 if (off_linktype
!= -1) {
2419 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2421 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2427 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2428 off_nl
+ 12, off_nl
+ 16);
2431 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2432 off_nl
+ 14, off_nl
+ 24);
2435 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2436 off_nl
+ 14, off_nl
+ 24);
2439 bpf_error("'tcp' modifier applied to host");
2442 bpf_error("'sctp' modifier applied to host");
2445 bpf_error("'udp' modifier applied to host");
2448 bpf_error("'icmp' modifier applied to host");
2451 bpf_error("'igmp' modifier applied to host");
2454 bpf_error("'igrp' modifier applied to host");
2457 bpf_error("'pim' modifier applied to host");
2460 bpf_error("'vrrp' modifier applied to host");
2463 bpf_error("ATALK host filtering not implemented");
2466 bpf_error("AARP host filtering not implemented");
2469 return gen_dnhostop(addr
, dir
, off_nl
);
2472 bpf_error("SCA host filtering not implemented");
2475 bpf_error("LAT host filtering not implemented");
2478 bpf_error("MOPDL host filtering not implemented");
2481 bpf_error("MOPRC host filtering not implemented");
2485 bpf_error("'ip6' modifier applied to ip host");
2488 bpf_error("'icmp6' modifier applied to host");
2492 bpf_error("'ah' modifier applied to host");
2495 bpf_error("'esp' modifier applied to host");
2498 bpf_error("ISO host filtering not implemented");
2501 bpf_error("'esis' modifier applied to host");
2504 bpf_error("'isis' modifier applied to host");
2507 bpf_error("'clnp' modifier applied to host");
2510 bpf_error("'stp' modifier applied to host");
2513 bpf_error("IPX host filtering not implemented");
2516 bpf_error("'netbeui' modifier applied to host");
2525 static struct block
*
2526 gen_host6(addr
, mask
, proto
, dir
)
2527 struct in6_addr
*addr
;
2528 struct in6_addr
*mask
;
2535 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2538 bpf_error("'ip' modifier applied to ip6 host");
2541 bpf_error("'rarp' modifier applied to ip6 host");
2544 bpf_error("'arp' modifier applied to ip6 host");
2547 bpf_error("'sctp' modifier applied to host");
2550 bpf_error("'tcp' modifier applied to host");
2553 bpf_error("'udp' modifier applied to host");
2556 bpf_error("'icmp' modifier applied to host");
2559 bpf_error("'igmp' modifier applied to host");
2562 bpf_error("'igrp' modifier applied to host");
2565 bpf_error("'pim' modifier applied to host");
2568 bpf_error("'vrrp' modifier applied to host");
2571 bpf_error("ATALK host filtering not implemented");
2574 bpf_error("AARP host filtering not implemented");
2577 bpf_error("'decnet' modifier applied to ip6 host");
2580 bpf_error("SCA host filtering not implemented");
2583 bpf_error("LAT host filtering not implemented");
2586 bpf_error("MOPDL host filtering not implemented");
2589 bpf_error("MOPRC host filtering not implemented");
2592 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2593 off_nl
+ 8, off_nl
+ 24);
2596 bpf_error("'icmp6' modifier applied to host");
2599 bpf_error("'ah' modifier applied to host");
2602 bpf_error("'esp' modifier applied to host");
2605 bpf_error("ISO host filtering not implemented");
2608 bpf_error("'esis' modifier applied to host");
2611 bpf_error("'isis' modifier applied to host");
2614 bpf_error("'clnp' modifier applied to host");
2617 bpf_error("'stp' modifier applied to host");
2620 bpf_error("IPX host filtering not implemented");
2623 bpf_error("'netbeui' modifier applied to host");
2633 static struct block
*
2634 gen_gateway(eaddr
, alist
, proto
, dir
)
2635 const u_char
*eaddr
;
2636 bpf_u_int32
**alist
;
2640 struct block
*b0
, *b1
, *tmp
;
2643 bpf_error("direction applied to 'gateway'");
2650 if (linktype
== DLT_EN10MB
)
2651 b0
= gen_ehostop(eaddr
, Q_OR
);
2652 else if (linktype
== DLT_FDDI
)
2653 b0
= gen_fhostop(eaddr
, Q_OR
);
2654 else if (linktype
== DLT_IEEE802
)
2655 b0
= gen_thostop(eaddr
, Q_OR
);
2656 else if (linktype
== DLT_IEEE802_11
)
2657 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2658 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2660 * Check that the packet doesn't begin with an
2661 * LE Control marker. (We've already generated
2664 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2668 * Now check the MAC address.
2670 b0
= gen_ehostop(eaddr
, Q_OR
);
2672 } else if (linktype
== DLT_IP_OVER_FC
)
2673 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2676 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2678 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2680 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2688 bpf_error("illegal modifier of 'gateway'");
2694 gen_proto_abbrev(proto
)
2703 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2705 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2711 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2713 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2719 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2721 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2727 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2730 #ifndef IPPROTO_IGMP
2731 #define IPPROTO_IGMP 2
2735 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2738 #ifndef IPPROTO_IGRP
2739 #define IPPROTO_IGRP 9
2742 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2746 #define IPPROTO_PIM 103
2750 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2752 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2757 #ifndef IPPROTO_VRRP
2758 #define IPPROTO_VRRP 112
2762 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2766 b1
= gen_linktype(ETHERTYPE_IP
);
2770 b1
= gen_linktype(ETHERTYPE_ARP
);
2774 b1
= gen_linktype(ETHERTYPE_REVARP
);
2778 bpf_error("link layer applied in wrong context");
2781 b1
= gen_linktype(ETHERTYPE_ATALK
);
2785 b1
= gen_linktype(ETHERTYPE_AARP
);
2789 b1
= gen_linktype(ETHERTYPE_DN
);
2793 b1
= gen_linktype(ETHERTYPE_SCA
);
2797 b1
= gen_linktype(ETHERTYPE_LAT
);
2801 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2805 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2810 b1
= gen_linktype(ETHERTYPE_IPV6
);
2813 #ifndef IPPROTO_ICMPV6
2814 #define IPPROTO_ICMPV6 58
2817 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2822 #define IPPROTO_AH 51
2825 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2827 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2833 #define IPPROTO_ESP 50
2836 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2838 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2844 b1
= gen_linktype(LLCSAP_ISONS
);
2848 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2852 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2855 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
2856 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2857 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
2859 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
2861 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
2863 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2867 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
2868 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2869 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
2871 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
2873 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
2875 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2879 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
2880 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2881 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2883 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
2888 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
2889 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
2894 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
2895 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
2897 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2899 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2904 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2905 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2910 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2911 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2916 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2920 b1
= gen_linktype(LLCSAP_8021D
);
2924 b1
= gen_linktype(LLCSAP_IPX
);
2928 b1
= gen_linktype(LLCSAP_NETBEUI
);
2937 static struct block
*
2944 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2945 s
->s
.k
= off_nl
+ 6;
2946 b
= new_block(JMP(BPF_JSET
));
2954 static struct block
*
2955 gen_portatom(off
, v
)
2962 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
2965 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
2966 s
->next
->s
.k
= off_nl
+ off
;
2968 b
= new_block(JMP(BPF_JEQ
));
2976 static struct block
*
2977 gen_portatom6(off
, v
)
2981 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
2986 gen_portop(port
, proto
, dir
)
2987 int port
, proto
, dir
;
2989 struct block
*b0
, *b1
, *tmp
;
2991 /* ip proto 'proto' */
2992 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
2998 b1
= gen_portatom(0, (bpf_int32
)port
);
3002 b1
= gen_portatom(2, (bpf_int32
)port
);
3007 tmp
= gen_portatom(0, (bpf_int32
)port
);
3008 b1
= gen_portatom(2, (bpf_int32
)port
);
3013 tmp
= gen_portatom(0, (bpf_int32
)port
);
3014 b1
= gen_portatom(2, (bpf_int32
)port
);
3026 static struct block
*
3027 gen_port(port
, ip_proto
, dir
)
3032 struct block
*b0
, *b1
, *tmp
;
3035 case DLT_IEEE802_11
:
3036 case DLT_PRISM_HEADER
:
3039 case DLT_ATM_RFC1483
:
3041 b0
= gen_linktype(LLCSAP_IP
);
3044 b0
= gen_linktype(ETHERTYPE_IP
);
3052 b1
= gen_portop(port
, ip_proto
, dir
);
3056 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3057 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3059 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3072 gen_portop6(port
, proto
, dir
)
3073 int port
, proto
, dir
;
3075 struct block
*b0
, *b1
, *tmp
;
3077 /* ip proto 'proto' */
3078 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3082 b1
= gen_portatom6(0, (bpf_int32
)port
);
3086 b1
= gen_portatom6(2, (bpf_int32
)port
);
3091 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3092 b1
= gen_portatom6(2, (bpf_int32
)port
);
3097 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3098 b1
= gen_portatom6(2, (bpf_int32
)port
);
3110 static struct block
*
3111 gen_port6(port
, ip_proto
, dir
)
3116 struct block
*b0
, *b1
, *tmp
;
3118 /* ether proto ip */
3119 b0
= gen_linktype(ETHERTYPE_IPV6
);
3125 b1
= gen_portop6(port
, ip_proto
, dir
);
3129 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3130 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3132 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3145 lookup_proto(name
, proto
)
3146 register const char *name
;
3156 v
= pcap_nametoproto(name
);
3157 if (v
== PROTO_UNDEF
)
3158 bpf_error("unknown ip proto '%s'", name
);
3162 /* XXX should look up h/w protocol type based on linktype */
3163 v
= pcap_nametoeproto(name
);
3164 if (v
== PROTO_UNDEF
)
3165 bpf_error("unknown ether proto '%s'", name
);
3169 if (strcmp(name
, "esis") == 0)
3171 else if (strcmp(name
, "isis") == 0)
3173 else if (strcmp(name
, "clnp") == 0)
3176 bpf_error("unknown osi proto '%s'", name
);
3196 static struct block
*
3197 gen_protochain(v
, proto
, dir
)
3202 #ifdef NO_PROTOCHAIN
3203 return gen_proto(v
, proto
, dir
);
3205 struct block
*b0
, *b
;
3206 struct slist
*s
[100];
3207 int fix2
, fix3
, fix4
, fix5
;
3208 int ahcheck
, again
, end
;
3210 int reg2
= alloc_reg();
3212 memset(s
, 0, sizeof(s
));
3213 fix2
= fix3
= fix4
= fix5
= 0;
3220 b0
= gen_protochain(v
, Q_IP
, dir
);
3221 b
= gen_protochain(v
, Q_IPV6
, dir
);
3225 bpf_error("bad protocol applied for 'protochain'");
3229 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3232 * s[0] is a dummy entry to protect other BPF insn from damaged
3233 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3234 * hard to find interdependency made by jump table fixup.
3237 s
[i
] = new_stmt(0); /*dummy*/
3242 b0
= gen_linktype(ETHERTYPE_IP
);
3245 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3246 s
[i
]->s
.k
= off_nl
+ 9;
3248 /* X = ip->ip_hl << 2 */
3249 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3255 b0
= gen_linktype(ETHERTYPE_IPV6
);
3257 /* A = ip6->ip_nxt */
3258 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3259 s
[i
]->s
.k
= off_nl
+ 6;
3261 /* X = sizeof(struct ip6_hdr) */
3262 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3268 bpf_error("unsupported proto to gen_protochain");
3272 /* again: if (A == v) goto end; else fall through; */
3274 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3276 s
[i
]->s
.jt
= NULL
; /*later*/
3277 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3281 #ifndef IPPROTO_NONE
3282 #define IPPROTO_NONE 59
3284 /* if (A == IPPROTO_NONE) goto end */
3285 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3286 s
[i
]->s
.jt
= NULL
; /*later*/
3287 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3288 s
[i
]->s
.k
= IPPROTO_NONE
;
3289 s
[fix5
]->s
.jf
= s
[i
];
3294 if (proto
== Q_IPV6
) {
3295 int v6start
, v6end
, v6advance
, j
;
3298 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3299 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3300 s
[i
]->s
.jt
= NULL
; /*later*/
3301 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3302 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3303 s
[fix2
]->s
.jf
= s
[i
];
3305 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3306 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3307 s
[i
]->s
.jt
= NULL
; /*later*/
3308 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3309 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3311 /* if (A == IPPROTO_ROUTING) goto v6advance */
3312 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3313 s
[i
]->s
.jt
= NULL
; /*later*/
3314 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3315 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3317 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3318 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3319 s
[i
]->s
.jt
= NULL
; /*later*/
3320 s
[i
]->s
.jf
= NULL
; /*later*/
3321 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3332 * X = X + (P[X + 1] + 1) * 8;
3335 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3337 /* A = P[X + packet head] */
3338 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3342 s
[i
] = new_stmt(BPF_ST
);
3346 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3349 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3353 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3355 /* A = P[X + packet head]; */
3356 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3360 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3364 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3368 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3371 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3375 /* goto again; (must use BPF_JA for backward jump) */
3376 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3377 s
[i
]->s
.k
= again
- i
- 1;
3378 s
[i
- 1]->s
.jf
= s
[i
];
3382 for (j
= v6start
; j
<= v6end
; j
++)
3383 s
[j
]->s
.jt
= s
[v6advance
];
3388 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3390 s
[fix2
]->s
.jf
= s
[i
];
3396 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3397 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3398 s
[i
]->s
.jt
= NULL
; /*later*/
3399 s
[i
]->s
.jf
= NULL
; /*later*/
3400 s
[i
]->s
.k
= IPPROTO_AH
;
3402 s
[fix3
]->s
.jf
= s
[ahcheck
];
3409 * X = X + (P[X + 1] + 2) * 4;
3412 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3414 /* A = P[X + packet head]; */
3415 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3419 s
[i
] = new_stmt(BPF_ST
);
3423 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3426 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3430 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3432 /* A = P[X + packet head] */
3433 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3437 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3441 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3445 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3448 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3452 /* goto again; (must use BPF_JA for backward jump) */
3453 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3454 s
[i
]->s
.k
= again
- i
- 1;
3459 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3461 s
[fix2
]->s
.jt
= s
[end
];
3462 s
[fix4
]->s
.jf
= s
[end
];
3463 s
[fix5
]->s
.jt
= s
[end
];
3470 for (i
= 0; i
< max
- 1; i
++)
3471 s
[i
]->next
= s
[i
+ 1];
3472 s
[max
- 1]->next
= NULL
;
3477 b
= new_block(JMP(BPF_JEQ
));
3478 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3488 static struct block
*
3489 gen_proto(v
, proto
, dir
)
3494 struct block
*b0
, *b1
;
3496 if (dir
!= Q_DEFAULT
)
3497 bpf_error("direction applied to 'proto'");
3502 b0
= gen_proto(v
, Q_IP
, dir
);
3503 b1
= gen_proto(v
, Q_IPV6
, dir
);
3511 case DLT_IEEE802_11
:
3512 case DLT_PRISM_HEADER
:
3515 case DLT_ATM_RFC1483
:
3517 b0
= gen_linktype(LLCSAP_IP
);
3520 b0
= gen_linktype(ETHERTYPE_IP
);
3524 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3526 b1
= gen_protochain(v
, Q_IP
);
3536 * Frame Relay packets typically have an OSI
3537 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3538 * generates code to check for all the OSI
3539 * NLPIDs, so calling it and then adding a check
3540 * for the particular NLPID for which we're
3541 * looking is bogus, as we can just check for
3544 * What we check for is the NLPID and a frame
3545 * control field value of UI, i.e. 0x03 followed
3548 * XXX - assumes a 2-byte Frame Relay header with
3549 * DLCI and flags. What if the address is longer?
3551 * XXX - what about SNAP-encapsulated frames?
3553 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3557 /* Cisco uses an Ethertype lookalike - for OSI its 0xfefe */
3558 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3559 /* OSI in C-HDLC is stuffed with a fudge byte */
3560 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3564 b0
= gen_linktype(LLCSAP_ISONS
);
3565 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3571 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3572 /* 4 is the offset of the PDU type relative to the IS-IS header */
3573 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3578 bpf_error("arp does not encapsulate another protocol");
3582 bpf_error("rarp does not encapsulate another protocol");
3586 bpf_error("atalk encapsulation is not specifiable");
3590 bpf_error("decnet encapsulation is not specifiable");
3594 bpf_error("sca does not encapsulate another protocol");
3598 bpf_error("lat does not encapsulate another protocol");
3602 bpf_error("moprc does not encapsulate another protocol");
3606 bpf_error("mopdl does not encapsulate another protocol");
3610 return gen_linktype(v
);
3613 bpf_error("'udp proto' is bogus");
3617 bpf_error("'tcp proto' is bogus");
3621 bpf_error("'sctp proto' is bogus");
3625 bpf_error("'icmp proto' is bogus");
3629 bpf_error("'igmp proto' is bogus");
3633 bpf_error("'igrp proto' is bogus");
3637 bpf_error("'pim proto' is bogus");
3641 bpf_error("'vrrp proto' is bogus");
3646 b0
= gen_linktype(ETHERTYPE_IPV6
);
3648 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3650 b1
= gen_protochain(v
, Q_IPV6
);
3656 bpf_error("'icmp6 proto' is bogus");
3660 bpf_error("'ah proto' is bogus");
3663 bpf_error("'ah proto' is bogus");
3666 bpf_error("'stp proto' is bogus");
3669 bpf_error("'ipx proto' is bogus");
3672 bpf_error("'netbeui proto' is bogus");
3683 register const char *name
;
3686 int proto
= q
.proto
;
3690 bpf_u_int32 mask
, addr
;
3692 bpf_u_int32
**alist
;
3695 struct sockaddr_in
*sin
;
3696 struct sockaddr_in6
*sin6
;
3697 struct addrinfo
*res
, *res0
;
3698 struct in6_addr mask128
;
3700 struct block
*b
, *tmp
;
3701 int port
, real_proto
;
3706 addr
= pcap_nametonetaddr(name
);
3708 bpf_error("unknown network '%s'", name
);
3709 /* Left justify network addr and calculate its network mask */
3711 while (addr
&& (addr
& 0xff000000) == 0) {
3715 return gen_host(addr
, mask
, proto
, dir
);
3719 if (proto
== Q_LINK
) {
3723 eaddr
= pcap_ether_hostton(name
);
3726 "unknown ether host '%s'", name
);
3727 b
= gen_ehostop(eaddr
, dir
);
3732 eaddr
= pcap_ether_hostton(name
);
3735 "unknown FDDI host '%s'", name
);
3736 b
= gen_fhostop(eaddr
, dir
);
3741 eaddr
= pcap_ether_hostton(name
);
3744 "unknown token ring host '%s'", name
);
3745 b
= gen_thostop(eaddr
, dir
);
3749 case DLT_IEEE802_11
:
3750 eaddr
= pcap_ether_hostton(name
);
3753 "unknown 802.11 host '%s'", name
);
3754 b
= gen_wlanhostop(eaddr
, dir
);
3758 case DLT_IP_OVER_FC
:
3759 eaddr
= pcap_ether_hostton(name
);
3762 "unknown Fibre Channel host '%s'", name
);
3763 b
= gen_ipfchostop(eaddr
, dir
);
3772 * Check that the packet doesn't begin
3773 * with an LE Control marker. (We've
3774 * already generated a test for LANE.)
3776 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3780 eaddr
= pcap_ether_hostton(name
);
3783 "unknown ether host '%s'", name
);
3784 b
= gen_ehostop(eaddr
, dir
);
3790 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3791 } else if (proto
== Q_DECNET
) {
3792 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3794 * I don't think DECNET hosts can be multihomed, so
3795 * there is no need to build up a list of addresses
3797 return (gen_host(dn_addr
, 0, proto
, dir
));
3800 alist
= pcap_nametoaddr(name
);
3801 if (alist
== NULL
|| *alist
== NULL
)
3802 bpf_error("unknown host '%s'", name
);
3804 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3806 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3808 tmp
= gen_host(**alist
++, 0xffffffff,
3815 memset(&mask128
, 0xff, sizeof(mask128
));
3816 res0
= res
= pcap_nametoaddrinfo(name
);
3818 bpf_error("unknown host '%s'", name
);
3820 tproto
= tproto6
= proto
;
3821 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3825 for (res
= res0
; res
; res
= res
->ai_next
) {
3826 switch (res
->ai_family
) {
3828 if (tproto
== Q_IPV6
)
3831 sin
= (struct sockaddr_in
*)
3833 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3834 0xffffffff, tproto
, dir
);
3837 if (tproto6
== Q_IP
)
3840 sin6
= (struct sockaddr_in6
*)
3842 tmp
= gen_host6(&sin6
->sin6_addr
,
3843 &mask128
, tproto6
, dir
);
3854 bpf_error("unknown host '%s'%s", name
,
3855 (proto
== Q_DEFAULT
)
3857 : " for specified address family");
3864 if (proto
!= Q_DEFAULT
&&
3865 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3866 bpf_error("illegal qualifier of 'port'");
3867 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3868 bpf_error("unknown port '%s'", name
);
3869 if (proto
== Q_UDP
) {
3870 if (real_proto
== IPPROTO_TCP
)
3871 bpf_error("port '%s' is tcp", name
);
3872 else if (real_proto
== IPPROTO_SCTP
)
3873 bpf_error("port '%s' is sctp", name
);
3875 /* override PROTO_UNDEF */
3876 real_proto
= IPPROTO_UDP
;
3878 if (proto
== Q_TCP
) {
3879 if (real_proto
== IPPROTO_UDP
)
3880 bpf_error("port '%s' is udp", name
);
3882 else if (real_proto
== IPPROTO_SCTP
)
3883 bpf_error("port '%s' is sctp", name
);
3885 /* override PROTO_UNDEF */
3886 real_proto
= IPPROTO_TCP
;
3888 if (proto
== Q_SCTP
) {
3889 if (real_proto
== IPPROTO_UDP
)
3890 bpf_error("port '%s' is udp", name
);
3892 else if (real_proto
== IPPROTO_TCP
)
3893 bpf_error("port '%s' is tcp", name
);
3895 /* override PROTO_UNDEF */
3896 real_proto
= IPPROTO_SCTP
;
3899 return gen_port(port
, real_proto
, dir
);
3903 b
= gen_port(port
, real_proto
, dir
);
3904 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3911 eaddr
= pcap_ether_hostton(name
);
3913 bpf_error("unknown ether host: %s", name
);
3915 alist
= pcap_nametoaddr(name
);
3916 if (alist
== NULL
|| *alist
== NULL
)
3917 bpf_error("unknown host '%s'", name
);
3918 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3922 bpf_error("'gateway' not supported in this configuration");
3926 real_proto
= lookup_proto(name
, proto
);
3927 if (real_proto
>= 0)
3928 return gen_proto(real_proto
, proto
, dir
);
3930 bpf_error("unknown protocol: %s", name
);
3933 real_proto
= lookup_proto(name
, proto
);
3934 if (real_proto
>= 0)
3935 return gen_protochain(real_proto
, proto
, dir
);
3937 bpf_error("unknown protocol: %s", name
);
3949 gen_mcode(s1
, s2
, masklen
, q
)
3950 register const char *s1
, *s2
;
3951 register int masklen
;
3954 register int nlen
, mlen
;
3957 nlen
= __pcap_atoin(s1
, &n
);
3958 /* Promote short ipaddr */
3962 mlen
= __pcap_atoin(s2
, &m
);
3963 /* Promote short ipaddr */
3966 bpf_error("non-network bits set in \"%s mask %s\"",
3969 /* Convert mask len to mask */
3971 bpf_error("mask length must be <= 32");
3972 m
= 0xffffffff << (32 - masklen
);
3974 bpf_error("non-network bits set in \"%s/%d\"",
3981 return gen_host(n
, m
, q
.proto
, q
.dir
);
3984 bpf_error("Mask syntax for networks only");
3991 register const char *s
;
3996 int proto
= q
.proto
;
4002 else if (q
.proto
== Q_DECNET
)
4003 vlen
= __pcap_atodn(s
, &v
);
4005 vlen
= __pcap_atoin(s
, &v
);
4012 if (proto
== Q_DECNET
)
4013 return gen_host(v
, 0, proto
, dir
);
4014 else if (proto
== Q_LINK
) {
4015 bpf_error("illegal link layer address");
4018 if (s
== NULL
&& q
.addr
== Q_NET
) {
4019 /* Promote short net number */
4020 while (v
&& (v
& 0xff000000) == 0) {
4025 /* Promote short ipaddr */
4029 return gen_host(v
, mask
, proto
, dir
);
4034 proto
= IPPROTO_UDP
;
4035 else if (proto
== Q_TCP
)
4036 proto
= IPPROTO_TCP
;
4037 else if (proto
== Q_SCTP
)
4038 proto
= IPPROTO_SCTP
;
4039 else if (proto
== Q_DEFAULT
)
4040 proto
= PROTO_UNDEF
;
4042 bpf_error("illegal qualifier of 'port'");
4045 return gen_port((int)v
, proto
, dir
);
4049 b
= gen_port((int)v
, proto
, dir
);
4050 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4056 bpf_error("'gateway' requires a name");
4060 return gen_proto((int)v
, proto
, dir
);
4063 return gen_protochain((int)v
, proto
, dir
);
4078 gen_mcode6(s1
, s2
, masklen
, q
)
4079 register const char *s1
, *s2
;
4080 register int masklen
;
4083 struct addrinfo
*res
;
4084 struct in6_addr
*addr
;
4085 struct in6_addr mask
;
4090 bpf_error("no mask %s supported", s2
);
4092 res
= pcap_nametoaddrinfo(s1
);
4094 bpf_error("invalid ip6 address %s", s1
);
4096 bpf_error("%s resolved to multiple address", s1
);
4097 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4099 if (sizeof(mask
) * 8 < masklen
)
4100 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4101 memset(&mask
, 0, sizeof(mask
));
4102 memset(&mask
, 0xff, masklen
/ 8);
4104 mask
.s6_addr
[masklen
/ 8] =
4105 (0xff << (8 - masklen
% 8)) & 0xff;
4108 a
= (u_int32_t
*)addr
;
4109 m
= (u_int32_t
*)&mask
;
4110 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4111 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4112 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4120 bpf_error("Mask syntax for networks only");
4124 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4129 bpf_error("invalid qualifier against IPv6 address");
4137 register const u_char
*eaddr
;
4140 struct block
*b
, *tmp
;
4142 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4143 if (linktype
== DLT_EN10MB
)
4144 return gen_ehostop(eaddr
, (int)q
.dir
);
4145 if (linktype
== DLT_FDDI
)
4146 return gen_fhostop(eaddr
, (int)q
.dir
);
4147 if (linktype
== DLT_IEEE802
)
4148 return gen_thostop(eaddr
, (int)q
.dir
);
4149 if (linktype
== DLT_IEEE802_11
)
4150 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4151 if (linktype
== DLT_SUNATM
&& is_lane
) {
4153 * Check that the packet doesn't begin with an
4154 * LE Control marker. (We've already generated
4157 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4161 * Now check the MAC address.
4163 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4167 if (linktype
== DLT_IP_OVER_FC
)
4168 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4169 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4171 bpf_error("ethernet address used in non-ether expression");
4177 struct slist
*s0
, *s1
;
4180 * This is definitely not the best way to do this, but the
4181 * lists will rarely get long.
4188 static struct slist
*
4194 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4199 static struct slist
*
4205 s
= new_stmt(BPF_LD
|BPF_MEM
);
4211 gen_load(proto
, index
, size
)
4216 struct slist
*s
, *tmp
;
4218 int regno
= alloc_reg();
4220 free_reg(index
->regno
);
4224 bpf_error("data size must be 1, 2, or 4");
4240 bpf_error("unsupported index operation");
4244 * XXX - what about ATM LANE? Should the index be
4245 * relative to the beginning of the AAL5 frame, so
4246 * that 0 refers to the beginning of the LE Control
4247 * field, or relative to the beginning of the LAN
4248 * frame, so that 0 refers, for Ethernet LANE, to
4249 * the beginning of the destination address?
4251 s
= xfer_to_x(index
);
4252 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4254 sappend(index
->s
, s
);
4269 /* XXX Note that we assume a fixed link header here. */
4270 s
= xfer_to_x(index
);
4271 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4274 sappend(index
->s
, s
);
4276 b
= gen_proto_abbrev(proto
);
4278 gen_and(index
->b
, b
);
4290 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4292 sappend(s
, xfer_to_a(index
));
4293 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4294 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4295 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4297 sappend(index
->s
, s
);
4299 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4301 gen_and(index
->b
, b
);
4303 gen_and(gen_proto_abbrev(Q_IP
), b
);
4309 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4313 index
->regno
= regno
;
4314 s
= new_stmt(BPF_ST
);
4316 sappend(index
->s
, s
);
4322 gen_relation(code
, a0
, a1
, reversed
)
4324 struct arth
*a0
, *a1
;
4327 struct slist
*s0
, *s1
, *s2
;
4328 struct block
*b
, *tmp
;
4332 if (code
== BPF_JEQ
) {
4333 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4334 b
= new_block(JMP(code
));
4338 b
= new_block(BPF_JMP
|code
|BPF_X
);
4344 sappend(a0
->s
, a1
->s
);
4348 free_reg(a0
->regno
);
4349 free_reg(a1
->regno
);
4351 /* 'and' together protocol checks */
4354 gen_and(a0
->b
, tmp
= a1
->b
);
4370 int regno
= alloc_reg();
4371 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4374 s
= new_stmt(BPF_LD
|BPF_LEN
);
4375 s
->next
= new_stmt(BPF_ST
);
4376 s
->next
->s
.k
= regno
;
4391 a
= (struct arth
*)newchunk(sizeof(*a
));
4395 s
= new_stmt(BPF_LD
|BPF_IMM
);
4397 s
->next
= new_stmt(BPF_ST
);
4413 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4416 s
= new_stmt(BPF_ST
);
4424 gen_arth(code
, a0
, a1
)
4426 struct arth
*a0
, *a1
;
4428 struct slist
*s0
, *s1
, *s2
;
4432 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4437 sappend(a0
->s
, a1
->s
);
4439 free_reg(a0
->regno
);
4440 free_reg(a1
->regno
);
4442 s0
= new_stmt(BPF_ST
);
4443 a0
->regno
= s0
->s
.k
= alloc_reg();
4450 * Here we handle simple allocation of the scratch registers.
4451 * If too many registers are alloc'd, the allocator punts.
4453 static int regused
[BPF_MEMWORDS
];
4457 * Return the next free register.
4462 int n
= BPF_MEMWORDS
;
4465 if (regused
[curreg
])
4466 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4468 regused
[curreg
] = 1;
4472 bpf_error("too many registers needed to evaluate expression");
4477 * Return a register to the table so it can
4487 static struct block
*
4494 s
= new_stmt(BPF_LD
|BPF_LEN
);
4495 b
= new_block(JMP(jmp
));
4506 return gen_len(BPF_JGE
, n
);
4510 * Actually, this is less than or equal.
4518 b
= gen_len(BPF_JGT
, n
);
4525 gen_byteop(op
, idx
, val
)
4536 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4539 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4540 b
->s
.code
= JMP(BPF_JGE
);
4545 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4546 b
->s
.code
= JMP(BPF_JGT
);
4550 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4554 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4558 b
= new_block(JMP(BPF_JEQ
));
4565 static u_char abroadcast
[] = { 0x0 };
4568 gen_broadcast(proto
)
4571 bpf_u_int32 hostmask
;
4572 struct block
*b0
, *b1
, *b2
;
4573 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4579 if (linktype
== DLT_ARCNET
)
4580 return gen_ahostop(abroadcast
, Q_DST
);
4581 if (linktype
== DLT_EN10MB
)
4582 return gen_ehostop(ebroadcast
, Q_DST
);
4583 if (linktype
== DLT_FDDI
)
4584 return gen_fhostop(ebroadcast
, Q_DST
);
4585 if (linktype
== DLT_IEEE802
)
4586 return gen_thostop(ebroadcast
, Q_DST
);
4587 if (linktype
== DLT_IEEE802_11
)
4588 return gen_wlanhostop(ebroadcast
, Q_DST
);
4589 if (linktype
== DLT_SUNATM
&& is_lane
) {
4591 * Check that the packet doesn't begin with an
4592 * LE Control marker. (We've already generated
4595 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4599 * Now check the MAC address.
4601 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4605 bpf_error("not a broadcast link");
4609 b0
= gen_linktype(ETHERTYPE_IP
);
4610 hostmask
= ~netmask
;
4611 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4612 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4613 (bpf_int32
)(~0 & hostmask
), hostmask
);
4618 bpf_error("only ether/ip broadcast filters supported");
4622 * Generate code to test the low-order bit of a MAC address (that's
4623 * the bottom bit of the *first* byte).
4625 static struct block
*
4626 gen_mac_multicast(offset
)
4629 register struct block
*b0
;
4630 register struct slist
*s
;
4632 /* link[offset] & 1 != 0 */
4633 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4635 b0
= new_block(JMP(BPF_JSET
));
4642 gen_multicast(proto
)
4645 register struct block
*b0
, *b1
, *b2
;
4646 register struct slist
*s
;
4652 if (linktype
== DLT_ARCNET
)
4653 /* all ARCnet multicasts use the same address */
4654 return gen_ahostop(abroadcast
, Q_DST
);
4656 if (linktype
== DLT_EN10MB
) {
4657 /* ether[0] & 1 != 0 */
4658 return gen_mac_multicast(0);
4661 if (linktype
== DLT_FDDI
) {
4663 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4665 * XXX - was that referring to bit-order issues?
4667 /* fddi[1] & 1 != 0 */
4668 return gen_mac_multicast(1);
4671 if (linktype
== DLT_IEEE802
) {
4672 /* tr[2] & 1 != 0 */
4673 return gen_mac_multicast(2);
4676 if (linktype
== DLT_IEEE802_11
) {
4680 * For control frames, there is no DA.
4682 * For management frames, DA is at an
4683 * offset of 4 from the beginning of
4686 * For data frames, DA is at an offset
4687 * of 4 from the beginning of the packet
4688 * if To DS is clear and at an offset of
4689 * 16 from the beginning of the packet
4694 * Generate the tests to be done for data frames.
4696 * First, check for To DS set, i.e. "link[1] & 0x01".
4698 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4700 b1
= new_block(JMP(BPF_JSET
));
4701 b1
->s
.k
= 0x01; /* To DS */
4705 * If To DS is set, the DA is at 16.
4707 b0
= gen_mac_multicast(16);
4711 * Now, check for To DS not set, i.e. check
4712 * "!(link[1] & 0x01)".
4714 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4716 b2
= new_block(JMP(BPF_JSET
));
4717 b2
->s
.k
= 0x01; /* To DS */
4722 * If To DS is not set, the DA is at 4.
4724 b1
= gen_mac_multicast(4);
4728 * Now OR together the last two checks. That gives
4729 * the complete set of checks for data frames.
4734 * Now check for a data frame.
4735 * I.e, check "link[0] & 0x08".
4737 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4739 b1
= new_block(JMP(BPF_JSET
));
4744 * AND that with the checks done for data frames.
4749 * If the high-order bit of the type value is 0, this
4750 * is a management frame.
4751 * I.e, check "!(link[0] & 0x08)".
4753 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4755 b2
= new_block(JMP(BPF_JSET
));
4761 * For management frames, the DA is at 4.
4763 b1
= gen_mac_multicast(4);
4767 * OR that with the checks done for data frames.
4768 * That gives the checks done for management and
4774 * If the low-order bit of the type value is 1,
4775 * this is either a control frame or a frame
4776 * with a reserved type, and thus not a
4779 * I.e., check "!(link[0] & 0x04)".
4781 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4783 b1
= new_block(JMP(BPF_JSET
));
4789 * AND that with the checks for data and management
4796 if (linktype
== DLT_SUNATM
&& is_lane
) {
4798 * Check that the packet doesn't begin with an
4799 * LE Control marker. (We've already generated
4802 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4805 /* ether[off_mac] & 1 != 0 */
4806 b0
= gen_mac_multicast(off_mac
);
4811 /* Link not known to support multicasts */
4815 b0
= gen_linktype(ETHERTYPE_IP
);
4816 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4817 b1
->s
.code
= JMP(BPF_JGE
);
4823 b0
= gen_linktype(ETHERTYPE_IPV6
);
4824 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4829 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4833 * generate command for inbound/outbound. It's here so we can
4834 * make it link-type specific. 'dir' = 0 implies "inbound",
4835 * = 1 implies "outbound".
4841 register struct block
*b0
;
4844 * Only some data link types support inbound/outbound qualifiers.
4849 b0
= gen_relation(BPF_JEQ
,
4850 gen_load(Q_LINK
, gen_loadi(0), 1),
4858 * Match packets sent by this machine.
4860 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
4863 * Match packets sent to this machine.
4864 * (No broadcast or multicast packets, or
4865 * packets sent to some other machine and
4866 * received promiscuously.)
4868 * XXX - packets sent to other machines probably
4869 * shouldn't be matched, but what about broadcast
4870 * or multicast packets we received?
4872 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
4877 bpf_error("inbound/outbound not supported on linktype %d\n",
4887 register const u_char
*eaddr
;
4890 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4891 if (linktype
== DLT_ARCNET
)
4892 return gen_ahostop(eaddr
, (int)q
.dir
);
4894 bpf_error("ARCnet address used in non-arc expression");
4898 static struct block
*
4899 gen_ahostop(eaddr
, dir
)
4900 register const u_char
*eaddr
;
4903 register struct block
*b0
, *b1
;
4906 /* src comes first, different from Ethernet */
4908 return gen_bcmp(0, 1, eaddr
);
4911 return gen_bcmp(1, 1, eaddr
);
4914 b0
= gen_ahostop(eaddr
, Q_SRC
);
4915 b1
= gen_ahostop(eaddr
, Q_DST
);
4921 b0
= gen_ahostop(eaddr
, Q_SRC
);
4922 b1
= gen_ahostop(eaddr
, Q_DST
);
4931 * support IEEE 802.1Q VLAN trunk over ethernet
4940 * Change the offsets to point to the type and data fields within
4941 * the VLAN packet. This is somewhat of a kludge.
4943 if (orig_nl
== (u_int
)-1) {
4944 orig_linktype
= off_linktype
; /* save original values */
4946 orig_nl_nosnap
= off_nl_nosnap
;
4957 bpf_error("no VLAN support for data link type %d",
4963 /* check for VLAN */
4964 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
4966 /* If a specific VLAN is requested, check VLAN id */
4967 if (vlan_num
>= 0) {
4970 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
4979 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
4991 bpf_error("'vpi' supported only on raw ATM");
4994 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
4995 (u_int
)jvalue
, reverse
);
5000 bpf_error("'vci' supported only on raw ATM");
5003 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5004 (u_int
)jvalue
, reverse
);
5008 if (off_proto
== -1)
5009 abort(); /* XXX - this isn't on FreeBSD */
5010 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5011 (u_int
)jvalue
, reverse
);
5015 if (off_payload
== -1)
5017 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5018 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5023 bpf_error("'callref' supported only on raw ATM");
5024 if (off_proto
== -1)
5026 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5027 (u_int
)jvalue
, reverse
);
5037 gen_atmtype_abbrev(type
)
5040 struct block
*b0
, *b1
;
5045 /* Get all packets in Meta signalling Circuit */
5047 bpf_error("'metac' supported only on raw ATM");
5048 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5049 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5054 /* Get all packets in Broadcast Circuit*/
5056 bpf_error("'bcc' supported only on raw ATM");
5057 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5058 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5063 /* Get all cells in Segment OAM F4 circuit*/
5065 bpf_error("'oam4sc' supported only on raw ATM");
5066 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5067 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5072 /* Get all cells in End-to-End OAM F4 Circuit*/
5074 bpf_error("'oam4ec' supported only on raw ATM");
5075 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5076 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5081 /* Get all packets in connection Signalling Circuit */
5083 bpf_error("'sc' supported only on raw ATM");
5084 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5085 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5090 /* Get all packets in ILMI Circuit */
5092 bpf_error("'ilmic' supported only on raw ATM");
5093 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5094 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5099 /* Get all LANE packets */
5101 bpf_error("'lane' supported only on raw ATM");
5102 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5105 * Arrange that all subsequent tests assume LANE
5106 * rather than LLC-encapsulated packets, and set
5107 * the offsets appropriately for LANE-encapsulated
5110 * "off_mac" is the offset of the Ethernet header,
5111 * which is 2 bytes past the ATM pseudo-header
5112 * (skipping the pseudo-header and 2-byte LE Client
5113 * field). The other offsets are Ethernet offsets
5114 * relative to "off_mac".
5117 off_mac
= off_payload
+ 2; /* MAC header */
5118 off_linktype
= off_mac
+ 12;
5119 off_nl
= off_mac
+ 14; /* Ethernet II */
5120 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5124 /* Get all LLC-encapsulated packets */
5126 bpf_error("'llc' supported only on raw ATM");
5127 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5138 static struct block
*
5139 gen_msg_abbrev(type
)
5145 * Q.2931 signalling protocol messages for handling virtual circuits
5146 * establishment and teardown
5151 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5155 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5159 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5163 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5167 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5170 case A_RELEASE_DONE
:
5171 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5181 gen_atmmulti_abbrev(type
)
5184 struct block
*b0
, *b1
;
5190 bpf_error("'oam' supported only on raw ATM");
5191 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5196 bpf_error("'oamf4' supported only on raw ATM");
5198 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5199 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5201 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5207 * Get Q.2931 signalling messages for switched
5208 * virtual connection
5211 bpf_error("'connectmsg' supported only on raw ATM");
5212 b0
= gen_msg_abbrev(A_SETUP
);
5213 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5215 b0
= gen_msg_abbrev(A_CONNECT
);
5217 b0
= gen_msg_abbrev(A_CONNECTACK
);
5219 b0
= gen_msg_abbrev(A_RELEASE
);
5221 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5223 b0
= gen_atmtype_abbrev(A_SC
);
5229 bpf_error("'metaconnect' supported only on raw ATM");
5230 b0
= gen_msg_abbrev(A_SETUP
);
5231 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5233 b0
= gen_msg_abbrev(A_CONNECT
);
5235 b0
= gen_msg_abbrev(A_RELEASE
);
5237 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5239 b0
= gen_atmtype_abbrev(A_METAC
);