]>
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.185 2003-02-05 01:53:29 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 */
702 case DLT_ARCNET_LINUX
:
704 off_nl
= 8; /* XXX in reality, variable! */
705 off_nl_nosnap
= 8; /* no 802.2 LLC */
710 off_nl
= 14; /* Ethernet II */
711 off_nl_nosnap
= 17; /* 802.3+802.2 */
716 * SLIP doesn't have a link level type. The 16 byte
717 * header is hacked into our SLIP driver.
721 off_nl_nosnap
= 16; /* no 802.2 LLC */
725 /* XXX this may be the same as the DLT_PPP_BSDOS case */
729 off_nl_nosnap
= 24; /* no 802.2 LLC */
736 off_nl_nosnap
= 4; /* no 802.2 LLC */
740 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
741 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
744 off_nl_nosnap
= 4; /* no 802.2 LLC */
749 * This does no include the Ethernet header, and
750 * only covers session state.
754 off_nl_nosnap
= 8; /* no 802.2 LLC */
760 off_nl_nosnap
= 24; /* no 802.2 LLC */
765 * FDDI doesn't really have a link-level type field.
766 * We set "off_linktype" to the offset of the LLC header.
768 * To check for Ethernet types, we assume that SSAP = SNAP
769 * is being used and pick out the encapsulated Ethernet type.
770 * XXX - should we generate code to check for SNAP?
774 off_linktype
+= pcap_fddipad
;
776 off_nl
= 21; /* FDDI+802.2+SNAP */
777 off_nl_nosnap
= 16; /* FDDI+802.2 */
779 off_nl
+= pcap_fddipad
;
780 off_nl_nosnap
+= pcap_fddipad
;
786 * Token Ring doesn't really have a link-level type field.
787 * We set "off_linktype" to the offset of the LLC header.
789 * To check for Ethernet types, we assume that SSAP = SNAP
790 * is being used and pick out the encapsulated Ethernet type.
791 * XXX - should we generate code to check for SNAP?
793 * XXX - the header is actually variable-length.
794 * Some various Linux patched versions gave 38
795 * as "off_linktype" and 40 as "off_nl"; however,
796 * if a token ring packet has *no* routing
797 * information, i.e. is not source-routed, the correct
798 * values are 20 and 22, as they are in the vanilla code.
800 * A packet is source-routed iff the uppermost bit
801 * of the first byte of the source address, at an
802 * offset of 8, has the uppermost bit set. If the
803 * packet is source-routed, the total number of bytes
804 * of routing information is 2 plus bits 0x1F00 of
805 * the 16-bit value at an offset of 14 (shifted right
806 * 8 - figure out which byte that is).
809 off_nl
= 22; /* Token Ring+802.2+SNAP */
810 off_nl_nosnap
= 17; /* Token Ring+802.2 */
815 * 802.11 doesn't really have a link-level type field.
816 * We set "off_linktype" to the offset of the LLC header.
818 * To check for Ethernet types, we assume that SSAP = SNAP
819 * is being used and pick out the encapsulated Ethernet type.
820 * XXX - should we generate code to check for SNAP?
822 * XXX - the header is actually variable-length. We
823 * assume a 24-byte link-layer header, as appears in
824 * data frames in networks with no bridges.
827 off_nl
= 32; /* 802.11+802.2+SNAP */
828 off_nl_nosnap
= 27; /* 802.11+802.2 */
831 case DLT_PRISM_HEADER
:
833 * Same as 802.11, but with an additional header before
834 * the 802.11 header, containing a bunch of additional
835 * information including radio-level information.
837 * The header is 144 bytes long.
839 * XXX - same variable-length header problem; at least
840 * the Prism header is fixed-length.
842 off_linktype
= 144+24;
843 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
844 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
847 case DLT_IEEE802_11_RADIO
:
849 * Same as 802.11, but with an additional header before
850 * the 802.11 header, containing a bunch of additional
851 * information including radio-level information.
853 * The header is 64 bytes long.
855 * XXX - same variable-length header problem, only
856 * more so; this header is also variable-length,
857 * with the length being the 32-bit big-endian
858 * number at an offset of 4 from the beginning
859 * of the radio header.
861 off_linktype
= 64+24;
862 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
863 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
866 case DLT_ATM_RFC1483
:
867 case DLT_ATM_CLIP
: /* Linux ATM defines this */
869 * assume routed, non-ISO PDUs
870 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
873 off_nl
= 8; /* 802.2+SNAP */
874 off_nl_nosnap
= 3; /* 802.2 */
879 * Full Frontal ATM; you get AALn PDUs with an ATM
883 off_vpi
= SUNATM_VPI_POS
;
884 off_vci
= SUNATM_VCI_POS
;
885 off_proto
= PROTO_POS
;
886 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
887 off_payload
= SUNATM_PKT_BEGIN_POS
;
888 off_linktype
= off_payload
;
889 off_nl
= off_payload
+8; /* 802.2+SNAP */
890 off_nl_nosnap
= off_payload
+3; /* 802.2 */
896 off_nl_nosnap
= 0; /* no 802.2 LLC */
899 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
902 off_nl_nosnap
= 16; /* no 802.2 LLC */
907 * LocalTalk does have a 1-byte type field in the LLAP header,
908 * but really it just indicates whether there is a "short" or
909 * "long" DDP packet following.
913 off_nl_nosnap
= 0; /* no 802.2 LLC */
918 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
919 * link-level type field. We set "off_linktype" to the
920 * offset of the LLC header.
922 * To check for Ethernet types, we assume that SSAP = SNAP
923 * is being used and pick out the encapsulated Ethernet type.
924 * XXX - should we generate code to check for SNAP? RFC
925 * 2625 says SNAP should be used.
928 off_nl
= 24; /* IPFC+802.2+SNAP */
929 off_nl_nosnap
= 19; /* IPFC+802.2 */
934 * XXX - we should set this to handle SNAP-encapsulated
935 * frames (NLPID of 0x80).
939 off_nl_nosnap
= 0; /* no 802.2 LLC */
942 bpf_error("unknown data link type %d", linktype
);
946 static struct block
*
953 s
= new_stmt(BPF_LD
|BPF_IMM
);
955 b
= new_block(JMP(BPF_JEQ
));
961 static inline struct block
*
964 return gen_uncond(1);
967 static inline struct block
*
970 return gen_uncond(0);
974 * Byte-swap a 32-bit number.
975 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
976 * big-endian platforms.)
978 #define SWAPLONG(y) \
979 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
981 static struct block
*
982 gen_ether_linktype(proto
)
985 struct block
*b0
, *b1
;
991 * OSI protocols always use 802.2 encapsulation.
992 * XXX - should we check both the DSAP and the
993 * SSAP, like this, or should we check just the
996 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
998 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
999 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1004 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1006 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1007 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1011 case LLCSAP_NETBEUI
:
1013 * NetBEUI always uses 802.2 encapsulation.
1014 * XXX - should we check both the DSAP and the
1015 * SSAP, like this, or should we check just the
1018 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1020 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1021 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1029 * Ethernet_II frames, which are Ethernet
1030 * frames with a frame type of ETHERTYPE_IPX;
1032 * Ethernet_802.3 frames, which are 802.3
1033 * frames (i.e., the type/length field is
1034 * a length field, <= ETHERMTU, rather than
1035 * a type field) with the first two bytes
1036 * after the Ethernet/802.3 header being
1039 * Ethernet_802.2 frames, which are 802.3
1040 * frames with an 802.2 LLC header and
1041 * with the IPX LSAP as the DSAP in the LLC
1044 * Ethernet_SNAP frames, which are 802.3
1045 * frames with an LLC header and a SNAP
1046 * header and with an OUI of 0x000000
1047 * (encapsulated Ethernet) and a protocol
1048 * ID of ETHERTYPE_IPX in the SNAP header.
1050 * XXX - should we generate the same code both
1051 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1055 * This generates code to check both for the
1056 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1058 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1059 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1063 * Now we add code to check for SNAP frames with
1064 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1066 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1070 * Now we generate code to check for 802.3
1071 * frames in general.
1073 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1077 * Now add the check for 802.3 frames before the
1078 * check for Ethernet_802.2 and Ethernet_802.3,
1079 * as those checks should only be done on 802.3
1080 * frames, not on Ethernet frames.
1085 * Now add the check for Ethernet_II frames, and
1086 * do that before checking for the other frame
1089 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1093 case ETHERTYPE_ATALK
:
1094 case ETHERTYPE_AARP
:
1096 * EtherTalk (AppleTalk protocols on Ethernet link
1097 * layer) may use 802.2 encapsulation.
1101 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1102 * we check for an Ethernet type field less than
1103 * 1500, which means it's an 802.3 length field.
1105 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1109 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1110 * SNAP packets with an organization code of
1111 * 0x080007 (Apple, for Appletalk) and a protocol
1112 * type of ETHERTYPE_ATALK (Appletalk).
1114 * 802.2-encapsulated ETHERTYPE_AARP packets are
1115 * SNAP packets with an organization code of
1116 * 0x000000 (encapsulated Ethernet) and a protocol
1117 * type of ETHERTYPE_AARP (Appletalk ARP).
1119 if (proto
== ETHERTYPE_ATALK
)
1120 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1121 else /* proto == ETHERTYPE_AARP */
1122 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1126 * Check for Ethernet encapsulation (Ethertalk
1127 * phase 1?); we just check for the Ethernet
1130 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1136 if (proto
<= ETHERMTU
) {
1138 * This is an LLC SAP value, so the frames
1139 * that match would be 802.2 frames.
1140 * Check that the frame is an 802.2 frame
1141 * (i.e., that the length/type field is
1142 * a length field, <= ETHERMTU) and
1143 * then check the DSAP.
1145 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1147 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1152 * This is an Ethernet type, so compare
1153 * the length/type field with it (if
1154 * the frame is an 802.2 frame, the length
1155 * field will be <= ETHERMTU, and, as
1156 * "proto" is > ETHERMTU, this test
1157 * will fail and the frame won't match,
1158 * which is what we want).
1160 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1165 static struct block
*
1169 struct block
*b0
, *b1
, *b2
;
1174 return gen_ether_linktype(proto
);
1181 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1185 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1190 case DLT_IEEE802_11
:
1191 case DLT_PRISM_HEADER
:
1192 case DLT_IEEE802_11_RADIO
:
1195 case DLT_ATM_RFC1483
:
1197 case DLT_IP_OVER_FC
:
1198 return gen_llc(proto
);
1203 * If "is_lane" is set, check for a LANE-encapsulated
1204 * version of this protocol, otherwise check for an
1205 * LLC-encapsulated version of this protocol.
1207 * We assume LANE means Ethernet, not Token Ring.
1211 * Check that the packet doesn't begin with an
1212 * LE Control marker. (We've already generated
1215 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1219 * Now generate an Ethernet test.
1221 b1
= gen_ether_linktype(proto
);
1226 * Check for LLC encapsulation and then check the
1229 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1230 b1
= gen_llc(proto
);
1239 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1240 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1241 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1247 * OSI protocols always use 802.2 encapsulation.
1248 * XXX - should we check both the DSAP and the
1249 * LSAP, like this, or should we check just the
1252 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1253 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1254 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1258 case LLCSAP_NETBEUI
:
1260 * NetBEUI always uses 802.2 encapsulation.
1261 * XXX - should we check both the DSAP and the
1262 * LSAP, like this, or should we check just the
1265 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1266 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1267 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1273 * Ethernet_II frames, which are Ethernet
1274 * frames with a frame type of ETHERTYPE_IPX;
1276 * Ethernet_802.3 frames, which have a frame
1277 * type of LINUX_SLL_P_802_3;
1279 * Ethernet_802.2 frames, which are 802.3
1280 * frames with an 802.2 LLC header (i.e, have
1281 * a frame type of LINUX_SLL_P_802_2) and
1282 * with the IPX LSAP as the DSAP in the LLC
1285 * Ethernet_SNAP frames, which are 802.3
1286 * frames with an LLC header and a SNAP
1287 * header and with an OUI of 0x000000
1288 * (encapsulated Ethernet) and a protocol
1289 * ID of ETHERTYPE_IPX in the SNAP header.
1291 * First, do the checks on LINUX_SLL_P_802_2
1292 * frames; generate the check for either
1293 * Ethernet_802.2 or Ethernet_SNAP frames, and
1294 * then put a check for LINUX_SLL_P_802_2 frames
1297 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1298 (bpf_int32
)LLCSAP_IPX
);
1299 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1302 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1306 * Now check for 802.3 frames and OR that with
1307 * the previous test.
1309 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1313 * Now add the check for Ethernet_II frames, and
1314 * do that before checking for the other frame
1317 b0
= gen_cmp(off_linktype
, BPF_H
,
1318 (bpf_int32
)ETHERTYPE_IPX
);
1322 case ETHERTYPE_ATALK
:
1323 case ETHERTYPE_AARP
:
1325 * EtherTalk (AppleTalk protocols on Ethernet link
1326 * layer) may use 802.2 encapsulation.
1330 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1331 * we check for the 802.2 protocol type in the
1332 * "Ethernet type" field.
1334 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1337 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1338 * SNAP packets with an organization code of
1339 * 0x080007 (Apple, for Appletalk) and a protocol
1340 * type of ETHERTYPE_ATALK (Appletalk).
1342 * 802.2-encapsulated ETHERTYPE_AARP packets are
1343 * SNAP packets with an organization code of
1344 * 0x000000 (encapsulated Ethernet) and a protocol
1345 * type of ETHERTYPE_AARP (Appletalk ARP).
1347 if (proto
== ETHERTYPE_ATALK
)
1348 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1350 else /* proto == ETHERTYPE_AARP */
1351 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1356 * Check for Ethernet encapsulation (Ethertalk
1357 * phase 1?); we just check for the Ethernet
1360 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1366 if (proto
<= ETHERMTU
) {
1368 * This is an LLC SAP value, so the frames
1369 * that match would be 802.2 frames.
1370 * Check for the 802.2 protocol type
1371 * in the "Ethernet type" field, and
1372 * then check the DSAP.
1374 b0
= gen_cmp(off_linktype
, BPF_H
,
1376 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1382 * This is an Ethernet type, so compare
1383 * the length/type field with it (if
1384 * the frame is an 802.2 frame, the length
1385 * field will be <= ETHERMTU, and, as
1386 * "proto" is > ETHERMTU, this test
1387 * will fail and the frame won't match,
1388 * which is what we want).
1390 return gen_cmp(off_linktype
, BPF_H
,
1397 case DLT_SLIP_BSDOS
:
1400 * These types don't provide any type field; packets
1403 * XXX - for IPv4, check for a version number of 4, and,
1404 * for IPv6, check for a version number of 6?
1410 case ETHERTYPE_IPV6
:
1412 return gen_true(); /* always true */
1415 return gen_false(); /* always false */
1420 case DLT_PPP_SERIAL
:
1423 * We use Ethernet protocol types inside libpcap;
1424 * map them to the corresponding PPP protocol types.
1433 case ETHERTYPE_IPV6
:
1442 case ETHERTYPE_ATALK
:
1456 * I'm assuming the "Bridging PDU"s that go
1457 * over PPP are Spanning Tree Protocol
1471 * We use Ethernet protocol types inside libpcap;
1472 * map them to the corresponding PPP protocol types.
1477 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1478 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1480 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1485 case ETHERTYPE_IPV6
:
1495 case ETHERTYPE_ATALK
:
1509 * I'm assuming the "Bridging PDU"s that go
1510 * over PPP are Spanning Tree Protocol
1525 * For DLT_NULL, the link-layer header is a 32-bit
1526 * word containing an AF_ value in *host* byte order.
1528 * In addition, if we're reading a saved capture file,
1529 * the host byte order in the capture may not be the
1530 * same as the host byte order on this machine.
1532 * For DLT_LOOP, the link-layer header is a 32-bit
1533 * word containing an AF_ value in *network* byte order.
1535 * XXX - AF_ values may, unfortunately, be platform-
1536 * dependent; for example, FreeBSD's AF_INET6 is 24
1537 * whilst NetBSD's and OpenBSD's is 26.
1539 * This means that, when reading a capture file, just
1540 * checking for our AF_INET6 value won't work if the
1541 * capture file came from another OS.
1550 case ETHERTYPE_IPV6
:
1557 * Not a type on which we support filtering.
1558 * XXX - support those that have AF_ values
1559 * #defined on this platform, at least?
1564 if (linktype
== DLT_NULL
) {
1566 * The AF_ value is in host byte order, but
1567 * the BPF interpreter will convert it to
1568 * network byte order.
1570 * If this is a save file, and it's from a
1571 * machine with the opposite byte order to
1572 * ours, we byte-swap the AF_ value.
1574 * Then we run it through "htonl()", and
1575 * generate code to compare against the result.
1577 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1578 bpf_pcap
->sf
.swapped
)
1579 proto
= SWAPLONG(proto
);
1580 proto
= htonl(proto
);
1582 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1585 case DLT_ARCNET_LINUX
:
1587 * XXX should we check for first fragment if the protocol
1596 case ETHERTYPE_IPV6
:
1597 return (gen_cmp(off_linktype
, BPF_B
,
1598 (bpf_int32
)ARCTYPE_INET6
));
1602 b0
= gen_cmp(off_linktype
, BPF_B
,
1603 (bpf_int32
)ARCTYPE_IP
);
1604 b1
= gen_cmp(off_linktype
, BPF_B
,
1605 (bpf_int32
)ARCTYPE_IP_OLD
);
1610 b0
= gen_cmp(off_linktype
, BPF_B
,
1611 (bpf_int32
)ARCTYPE_ARP
);
1612 b1
= gen_cmp(off_linktype
, BPF_B
,
1613 (bpf_int32
)ARCTYPE_ARP_OLD
);
1617 case ETHERTYPE_REVARP
:
1618 return (gen_cmp(off_linktype
, BPF_B
,
1619 (bpf_int32
)ARCTYPE_REVARP
));
1621 case ETHERTYPE_ATALK
:
1622 return (gen_cmp(off_linktype
, BPF_B
,
1623 (bpf_int32
)ARCTYPE_ATALK
));
1629 case ETHERTYPE_ATALK
:
1638 * XXX - assumes a 2-byte Frame Relay header with
1639 * DLCI and flags. What if the address is longer?
1645 * Check for the special NLPID for IP.
1647 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1650 case ETHERTYPE_IPV6
:
1652 * Check for the special NLPID for IPv6.
1654 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1659 * Check for several OSI protocols.
1661 * Frame Relay packets typically have an OSI
1662 * NLPID at the beginning; we check for each
1665 * What we check for is the NLPID and a frame
1666 * control field of UI, i.e. 0x03 followed
1669 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1670 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1671 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1683 * All the types that have no encapsulation should either be
1684 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1685 * all packets are IP packets, or should be handled in some
1686 * special case, if none of them are (if some are and some
1687 * aren't, the lack of encapsulation is a problem, as we'd
1688 * have to find some other way of determining the packet type).
1690 * Therefore, if "off_linktype" is -1, there's an error.
1692 if (off_linktype
== -1)
1696 * Any type not handled above should always have an Ethernet
1697 * type at an offset of "off_linktype". (PPP is partially
1698 * handled above - the protocol type is mapped from the
1699 * Ethernet and LLC types we use internally to the corresponding
1700 * PPP type - but the PPP type is always specified by a value
1701 * at "off_linktype", so we don't have to do the code generation
1704 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1708 * Check for an LLC SNAP packet with a given organization code and
1709 * protocol type; we check the entire contents of the 802.2 LLC and
1710 * snap headers, checking for DSAP and SSAP of SNAP and a control
1711 * field of 0x03 in the LLC header, and for the specified organization
1712 * code and protocol type in the SNAP header.
1714 static struct block
*
1715 gen_snap(orgcode
, ptype
, offset
)
1716 bpf_u_int32 orgcode
;
1720 u_char snapblock
[8];
1722 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1723 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1724 snapblock
[2] = 0x03; /* control = UI */
1725 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1726 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1727 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1728 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1729 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1730 return gen_bcmp(offset
, 8, snapblock
);
1734 * Check for a given protocol value assuming an 802.2 LLC header.
1736 static struct block
*
1741 * XXX - handle token-ring variable-length header.
1746 return gen_cmp(off_linktype
, BPF_H
, (long)
1747 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1750 return gen_cmp(off_linktype
, BPF_H
, (long)
1751 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1753 case LLCSAP_NETBEUI
:
1754 return gen_cmp(off_linktype
, BPF_H
, (long)
1755 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1759 * XXX - are there ever SNAP frames for IPX on
1760 * non-Ethernet 802.x networks?
1762 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1764 case ETHERTYPE_ATALK
:
1766 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1767 * SNAP packets with an organization code of
1768 * 0x080007 (Apple, for Appletalk) and a protocol
1769 * type of ETHERTYPE_ATALK (Appletalk).
1771 * XXX - check for an organization code of
1772 * encapsulated Ethernet as well?
1774 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1778 * XXX - we don't have to check for IPX 802.3
1779 * here, but should we check for the IPX Ethertype?
1781 if (proto
<= ETHERMTU
) {
1783 * This is an LLC SAP value, so check
1786 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1789 * This is an Ethernet type; we assume that it's
1790 * unlikely that it'll appear in the right place
1791 * at random, and therefore check only the
1792 * location that would hold the Ethernet type
1793 * in a SNAP frame with an organization code of
1794 * 0x000000 (encapsulated Ethernet).
1796 * XXX - if we were to check for the SNAP DSAP and
1797 * LSAP, as per XXX, and were also to check for an
1798 * organization code of 0x000000 (encapsulated
1799 * Ethernet), we'd do
1801 * return gen_snap(0x000000, proto,
1804 * here; for now, we don't, as per the above.
1805 * I don't know whether it's worth the extra CPU
1806 * time to do the right check or not.
1808 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1813 static struct block
*
1814 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1818 u_int src_off
, dst_off
;
1820 struct block
*b0
, *b1
;
1834 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1835 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1841 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1842 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1849 b0
= gen_linktype(proto
);
1850 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
1856 static struct block
*
1857 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
1858 struct in6_addr
*addr
;
1859 struct in6_addr
*mask
;
1861 u_int src_off
, dst_off
;
1863 struct block
*b0
, *b1
;
1878 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1879 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1885 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
1886 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
1893 /* this order is important */
1894 a
= (u_int32_t
*)addr
;
1895 m
= (u_int32_t
*)mask
;
1896 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
1897 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
1899 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
1901 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
1903 b0
= gen_linktype(proto
);
1909 static struct block
*
1910 gen_ehostop(eaddr
, dir
)
1911 register const u_char
*eaddr
;
1914 register struct block
*b0
, *b1
;
1918 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
1921 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
1924 b0
= gen_ehostop(eaddr
, Q_SRC
);
1925 b1
= gen_ehostop(eaddr
, Q_DST
);
1931 b0
= gen_ehostop(eaddr
, Q_SRC
);
1932 b1
= gen_ehostop(eaddr
, Q_DST
);
1941 * Like gen_ehostop, but for DLT_FDDI
1943 static struct block
*
1944 gen_fhostop(eaddr
, dir
)
1945 register const u_char
*eaddr
;
1948 struct block
*b0
, *b1
;
1953 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
1955 return gen_bcmp(6 + 1, 6, eaddr
);
1960 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
1962 return gen_bcmp(0 + 1, 6, eaddr
);
1966 b0
= gen_fhostop(eaddr
, Q_SRC
);
1967 b1
= gen_fhostop(eaddr
, Q_DST
);
1973 b0
= gen_fhostop(eaddr
, Q_SRC
);
1974 b1
= gen_fhostop(eaddr
, Q_DST
);
1983 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
1985 static struct block
*
1986 gen_thostop(eaddr
, dir
)
1987 register const u_char
*eaddr
;
1990 register struct block
*b0
, *b1
;
1994 return gen_bcmp(8, 6, eaddr
);
1997 return gen_bcmp(2, 6, eaddr
);
2000 b0
= gen_thostop(eaddr
, Q_SRC
);
2001 b1
= gen_thostop(eaddr
, Q_DST
);
2007 b0
= gen_thostop(eaddr
, Q_SRC
);
2008 b1
= gen_thostop(eaddr
, Q_DST
);
2017 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2019 static struct block
*
2020 gen_wlanhostop(eaddr
, dir
)
2021 register const u_char
*eaddr
;
2024 register struct block
*b0
, *b1
, *b2
;
2025 register struct slist
*s
;
2032 * For control frames, there is no SA.
2034 * For management frames, SA is at an
2035 * offset of 10 from the beginning of
2038 * For data frames, SA is at an offset
2039 * of 10 from the beginning of the packet
2040 * if From DS is clear, at an offset of
2041 * 16 from the beginning of the packet
2042 * if From DS is set and To DS is clear,
2043 * and an offset of 24 from the beginning
2044 * of the packet if From DS is set and To DS
2049 * Generate the tests to be done for data frames
2052 * First, check for To DS set, i.e. check "link[1] & 0x01".
2054 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2056 b1
= new_block(JMP(BPF_JSET
));
2057 b1
->s
.k
= 0x01; /* To DS */
2061 * If To DS is set, the SA is at 24.
2063 b0
= gen_bcmp(24, 6, eaddr
);
2067 * Now, check for To DS not set, i.e. check
2068 * "!(link[1] & 0x01)".
2070 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2072 b2
= new_block(JMP(BPF_JSET
));
2073 b2
->s
.k
= 0x01; /* To DS */
2078 * If To DS is not set, the SA is at 16.
2080 b1
= gen_bcmp(16, 6, eaddr
);
2084 * Now OR together the last two checks. That gives
2085 * the complete set of checks for data frames with
2091 * Now check for From DS being set, and AND that with
2092 * the ORed-together checks.
2094 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2096 b1
= new_block(JMP(BPF_JSET
));
2097 b1
->s
.k
= 0x02; /* From DS */
2102 * Now check for data frames with From DS not set.
2104 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2106 b2
= new_block(JMP(BPF_JSET
));
2107 b2
->s
.k
= 0x02; /* From DS */
2112 * If From DS isn't set, the SA is at 10.
2114 b1
= gen_bcmp(10, 6, eaddr
);
2118 * Now OR together the checks for data frames with
2119 * From DS not set and for data frames with From DS
2120 * set; that gives the checks done for data frames.
2125 * Now check for a data frame.
2126 * I.e, check "link[0] & 0x08".
2128 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2130 b1
= new_block(JMP(BPF_JSET
));
2135 * AND that with the checks done for data frames.
2140 * If the high-order bit of the type value is 0, this
2141 * is a management frame.
2142 * I.e, check "!(link[0] & 0x08)".
2144 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2146 b2
= new_block(JMP(BPF_JSET
));
2152 * For management frames, the SA is at 10.
2154 b1
= gen_bcmp(10, 6, eaddr
);
2158 * OR that with the checks done for data frames.
2159 * That gives the checks done for management and
2165 * If the low-order bit of the type value is 1,
2166 * this is either a control frame or a frame
2167 * with a reserved type, and thus not a
2170 * I.e., check "!(link[0] & 0x04)".
2172 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2174 b1
= new_block(JMP(BPF_JSET
));
2180 * AND that with the checks for data and management
2190 * For control frames, there is no DA.
2192 * For management frames, DA is at an
2193 * offset of 4 from the beginning of
2196 * For data frames, DA is at an offset
2197 * of 4 from the beginning of the packet
2198 * if To DS is clear and at an offset of
2199 * 16 from the beginning of the packet
2204 * Generate the tests to be done for data frames.
2206 * First, check for To DS set, i.e. "link[1] & 0x01".
2208 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2210 b1
= new_block(JMP(BPF_JSET
));
2211 b1
->s
.k
= 0x01; /* To DS */
2215 * If To DS is set, the DA is at 16.
2217 b0
= gen_bcmp(16, 6, eaddr
);
2221 * Now, check for To DS not set, i.e. check
2222 * "!(link[1] & 0x01)".
2224 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2226 b2
= new_block(JMP(BPF_JSET
));
2227 b2
->s
.k
= 0x01; /* To DS */
2232 * If To DS is not set, the DA is at 4.
2234 b1
= gen_bcmp(4, 6, eaddr
);
2238 * Now OR together the last two checks. That gives
2239 * the complete set of checks for data frames.
2244 * Now check for a data frame.
2245 * I.e, check "link[0] & 0x08".
2247 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2249 b1
= new_block(JMP(BPF_JSET
));
2254 * AND that with the checks done for data frames.
2259 * If the high-order bit of the type value is 0, this
2260 * is a management frame.
2261 * I.e, check "!(link[0] & 0x08)".
2263 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2265 b2
= new_block(JMP(BPF_JSET
));
2271 * For management frames, the DA is at 4.
2273 b1
= gen_bcmp(4, 6, eaddr
);
2277 * OR that with the checks done for data frames.
2278 * That gives the checks done for management and
2284 * If the low-order bit of the type value is 1,
2285 * this is either a control frame or a frame
2286 * with a reserved type, and thus not a
2289 * I.e., check "!(link[0] & 0x04)".
2291 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2293 b1
= new_block(JMP(BPF_JSET
));
2299 * AND that with the checks for data and management
2306 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2307 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2313 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2314 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2323 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2324 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2325 * as the RFC states.)
2327 static struct block
*
2328 gen_ipfchostop(eaddr
, dir
)
2329 register const u_char
*eaddr
;
2332 register struct block
*b0
, *b1
;
2336 return gen_bcmp(10, 6, eaddr
);
2339 return gen_bcmp(2, 6, eaddr
);
2342 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2343 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2349 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2350 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2359 * This is quite tricky because there may be pad bytes in front of the
2360 * DECNET header, and then there are two possible data packet formats that
2361 * carry both src and dst addresses, plus 5 packet types in a format that
2362 * carries only the src node, plus 2 types that use a different format and
2363 * also carry just the src node.
2367 * Instead of doing those all right, we just look for data packets with
2368 * 0 or 1 bytes of padding. If you want to look at other packets, that
2369 * will require a lot more hacking.
2371 * To add support for filtering on DECNET "areas" (network numbers)
2372 * one would want to add a "mask" argument to this routine. That would
2373 * make the filter even more inefficient, although one could be clever
2374 * and not generate masking instructions if the mask is 0xFFFF.
2376 static struct block
*
2377 gen_dnhostop(addr
, dir
, base_off
)
2382 struct block
*b0
, *b1
, *b2
, *tmp
;
2383 u_int offset_lh
; /* offset if long header is received */
2384 u_int offset_sh
; /* offset if short header is received */
2389 offset_sh
= 1; /* follows flags */
2390 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2394 offset_sh
= 3; /* follows flags, dstnode */
2395 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2399 /* Inefficient because we do our Calvinball dance twice */
2400 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2401 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2407 /* Inefficient because we do our Calvinball dance twice */
2408 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2409 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2414 bpf_error("ISO host filtering not implemented");
2419 b0
= gen_linktype(ETHERTYPE_DN
);
2420 /* Check for pad = 1, long header case */
2421 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2422 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2423 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2424 BPF_H
, (bpf_int32
)ntohs(addr
));
2426 /* Check for pad = 0, long header case */
2427 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2428 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2431 /* Check for pad = 1, short header case */
2432 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2433 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2434 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2435 BPF_H
, (bpf_int32
)ntohs(addr
));
2438 /* Check for pad = 0, short header case */
2439 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2440 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2444 /* Combine with test for linktype */
2449 static struct block
*
2450 gen_host(addr
, mask
, proto
, dir
)
2456 struct block
*b0
, *b1
;
2461 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2462 if (off_linktype
!= -1) {
2463 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2465 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2471 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2472 off_nl
+ 12, off_nl
+ 16);
2475 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2476 off_nl
+ 14, off_nl
+ 24);
2479 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2480 off_nl
+ 14, off_nl
+ 24);
2483 bpf_error("'tcp' modifier applied to host");
2486 bpf_error("'sctp' modifier applied to host");
2489 bpf_error("'udp' modifier applied to host");
2492 bpf_error("'icmp' modifier applied to host");
2495 bpf_error("'igmp' modifier applied to host");
2498 bpf_error("'igrp' modifier applied to host");
2501 bpf_error("'pim' modifier applied to host");
2504 bpf_error("'vrrp' modifier applied to host");
2507 bpf_error("ATALK host filtering not implemented");
2510 bpf_error("AARP host filtering not implemented");
2513 return gen_dnhostop(addr
, dir
, off_nl
);
2516 bpf_error("SCA host filtering not implemented");
2519 bpf_error("LAT host filtering not implemented");
2522 bpf_error("MOPDL host filtering not implemented");
2525 bpf_error("MOPRC host filtering not implemented");
2529 bpf_error("'ip6' modifier applied to ip host");
2532 bpf_error("'icmp6' modifier applied to host");
2536 bpf_error("'ah' modifier applied to host");
2539 bpf_error("'esp' modifier applied to host");
2542 bpf_error("ISO host filtering not implemented");
2545 bpf_error("'esis' modifier applied to host");
2548 bpf_error("'isis' modifier applied to host");
2551 bpf_error("'clnp' modifier applied to host");
2554 bpf_error("'stp' modifier applied to host");
2557 bpf_error("IPX host filtering not implemented");
2560 bpf_error("'netbeui' modifier applied to host");
2569 static struct block
*
2570 gen_host6(addr
, mask
, proto
, dir
)
2571 struct in6_addr
*addr
;
2572 struct in6_addr
*mask
;
2579 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2582 bpf_error("'ip' modifier applied to ip6 host");
2585 bpf_error("'rarp' modifier applied to ip6 host");
2588 bpf_error("'arp' modifier applied to ip6 host");
2591 bpf_error("'sctp' modifier applied to host");
2594 bpf_error("'tcp' modifier applied to host");
2597 bpf_error("'udp' modifier applied to host");
2600 bpf_error("'icmp' modifier applied to host");
2603 bpf_error("'igmp' modifier applied to host");
2606 bpf_error("'igrp' modifier applied to host");
2609 bpf_error("'pim' modifier applied to host");
2612 bpf_error("'vrrp' modifier applied to host");
2615 bpf_error("ATALK host filtering not implemented");
2618 bpf_error("AARP host filtering not implemented");
2621 bpf_error("'decnet' modifier applied to ip6 host");
2624 bpf_error("SCA host filtering not implemented");
2627 bpf_error("LAT host filtering not implemented");
2630 bpf_error("MOPDL host filtering not implemented");
2633 bpf_error("MOPRC host filtering not implemented");
2636 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2637 off_nl
+ 8, off_nl
+ 24);
2640 bpf_error("'icmp6' modifier applied to host");
2643 bpf_error("'ah' modifier applied to host");
2646 bpf_error("'esp' modifier applied to host");
2649 bpf_error("ISO host filtering not implemented");
2652 bpf_error("'esis' modifier applied to host");
2655 bpf_error("'isis' modifier applied to host");
2658 bpf_error("'clnp' modifier applied to host");
2661 bpf_error("'stp' modifier applied to host");
2664 bpf_error("IPX host filtering not implemented");
2667 bpf_error("'netbeui' modifier applied to host");
2677 static struct block
*
2678 gen_gateway(eaddr
, alist
, proto
, dir
)
2679 const u_char
*eaddr
;
2680 bpf_u_int32
**alist
;
2684 struct block
*b0
, *b1
, *tmp
;
2687 bpf_error("direction applied to 'gateway'");
2694 if (linktype
== DLT_EN10MB
)
2695 b0
= gen_ehostop(eaddr
, Q_OR
);
2696 else if (linktype
== DLT_FDDI
)
2697 b0
= gen_fhostop(eaddr
, Q_OR
);
2698 else if (linktype
== DLT_IEEE802
)
2699 b0
= gen_thostop(eaddr
, Q_OR
);
2700 else if (linktype
== DLT_IEEE802_11
)
2701 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2702 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2704 * Check that the packet doesn't begin with an
2705 * LE Control marker. (We've already generated
2708 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2712 * Now check the MAC address.
2714 b0
= gen_ehostop(eaddr
, Q_OR
);
2716 } else if (linktype
== DLT_IP_OVER_FC
)
2717 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2720 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2722 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2724 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2732 bpf_error("illegal modifier of 'gateway'");
2738 gen_proto_abbrev(proto
)
2747 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2749 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2755 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2757 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2763 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2765 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2771 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2774 #ifndef IPPROTO_IGMP
2775 #define IPPROTO_IGMP 2
2779 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2782 #ifndef IPPROTO_IGRP
2783 #define IPPROTO_IGRP 9
2786 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2790 #define IPPROTO_PIM 103
2794 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2796 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2801 #ifndef IPPROTO_VRRP
2802 #define IPPROTO_VRRP 112
2806 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2810 b1
= gen_linktype(ETHERTYPE_IP
);
2814 b1
= gen_linktype(ETHERTYPE_ARP
);
2818 b1
= gen_linktype(ETHERTYPE_REVARP
);
2822 bpf_error("link layer applied in wrong context");
2825 b1
= gen_linktype(ETHERTYPE_ATALK
);
2829 b1
= gen_linktype(ETHERTYPE_AARP
);
2833 b1
= gen_linktype(ETHERTYPE_DN
);
2837 b1
= gen_linktype(ETHERTYPE_SCA
);
2841 b1
= gen_linktype(ETHERTYPE_LAT
);
2845 b1
= gen_linktype(ETHERTYPE_MOPDL
);
2849 b1
= gen_linktype(ETHERTYPE_MOPRC
);
2854 b1
= gen_linktype(ETHERTYPE_IPV6
);
2857 #ifndef IPPROTO_ICMPV6
2858 #define IPPROTO_ICMPV6 58
2861 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
2866 #define IPPROTO_AH 51
2869 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
2871 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
2877 #define IPPROTO_ESP 50
2880 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
2882 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
2888 b1
= gen_linktype(LLCSAP_ISONS
);
2892 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
2896 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
2899 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
2900 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2901 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
2903 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
2905 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
2907 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2911 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
2912 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2913 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
2915 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
2917 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
2919 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2923 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
2924 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2925 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
2927 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
2932 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
2933 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
2938 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
2939 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
2941 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2943 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2948 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2949 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2954 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
2955 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
2960 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
2964 b1
= gen_linktype(LLCSAP_8021D
);
2968 b1
= gen_linktype(LLCSAP_IPX
);
2972 b1
= gen_linktype(LLCSAP_NETBEUI
);
2981 static struct block
*
2988 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
2989 s
->s
.k
= off_nl
+ 6;
2990 b
= new_block(JMP(BPF_JSET
));
2998 static struct block
*
2999 gen_portatom(off
, v
)
3006 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3009 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3010 s
->next
->s
.k
= off_nl
+ off
;
3012 b
= new_block(JMP(BPF_JEQ
));
3020 static struct block
*
3021 gen_portatom6(off
, v
)
3025 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3030 gen_portop(port
, proto
, dir
)
3031 int port
, proto
, dir
;
3033 struct block
*b0
, *b1
, *tmp
;
3035 /* ip proto 'proto' */
3036 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3042 b1
= gen_portatom(0, (bpf_int32
)port
);
3046 b1
= gen_portatom(2, (bpf_int32
)port
);
3051 tmp
= gen_portatom(0, (bpf_int32
)port
);
3052 b1
= gen_portatom(2, (bpf_int32
)port
);
3057 tmp
= gen_portatom(0, (bpf_int32
)port
);
3058 b1
= gen_portatom(2, (bpf_int32
)port
);
3070 static struct block
*
3071 gen_port(port
, ip_proto
, dir
)
3076 struct block
*b0
, *b1
, *tmp
;
3081 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3082 * not LLC encapsulation with LLCSAP_IP.
3084 * For IEEE 802 networks - which includes 802.5 token ring
3085 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3086 * says that SNAP encapsulation is used, not LLC encapsulation
3089 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3090 * RFC 2225 say that SNAP encapsulation is used, not LLC
3091 * encapsulation with LLCSAP_IP.
3093 * So we always check for ETHERTYPE_IP.
3095 b0
= gen_linktype(ETHERTYPE_IP
);
3101 b1
= gen_portop(port
, ip_proto
, dir
);
3105 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3106 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3108 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3121 gen_portop6(port
, proto
, dir
)
3122 int port
, proto
, dir
;
3124 struct block
*b0
, *b1
, *tmp
;
3126 /* ip proto 'proto' */
3127 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3131 b1
= gen_portatom6(0, (bpf_int32
)port
);
3135 b1
= gen_portatom6(2, (bpf_int32
)port
);
3140 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3141 b1
= gen_portatom6(2, (bpf_int32
)port
);
3146 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3147 b1
= gen_portatom6(2, (bpf_int32
)port
);
3159 static struct block
*
3160 gen_port6(port
, ip_proto
, dir
)
3165 struct block
*b0
, *b1
, *tmp
;
3167 /* ether proto ip */
3168 b0
= gen_linktype(ETHERTYPE_IPV6
);
3174 b1
= gen_portop6(port
, ip_proto
, dir
);
3178 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3179 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3181 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3194 lookup_proto(name
, proto
)
3195 register const char *name
;
3205 v
= pcap_nametoproto(name
);
3206 if (v
== PROTO_UNDEF
)
3207 bpf_error("unknown ip proto '%s'", name
);
3211 /* XXX should look up h/w protocol type based on linktype */
3212 v
= pcap_nametoeproto(name
);
3213 if (v
== PROTO_UNDEF
)
3214 bpf_error("unknown ether proto '%s'", name
);
3218 if (strcmp(name
, "esis") == 0)
3220 else if (strcmp(name
, "isis") == 0)
3222 else if (strcmp(name
, "clnp") == 0)
3225 bpf_error("unknown osi proto '%s'", name
);
3245 static struct block
*
3246 gen_protochain(v
, proto
, dir
)
3251 #ifdef NO_PROTOCHAIN
3252 return gen_proto(v
, proto
, dir
);
3254 struct block
*b0
, *b
;
3255 struct slist
*s
[100];
3256 int fix2
, fix3
, fix4
, fix5
;
3257 int ahcheck
, again
, end
;
3259 int reg2
= alloc_reg();
3261 memset(s
, 0, sizeof(s
));
3262 fix2
= fix3
= fix4
= fix5
= 0;
3269 b0
= gen_protochain(v
, Q_IP
, dir
);
3270 b
= gen_protochain(v
, Q_IPV6
, dir
);
3274 bpf_error("bad protocol applied for 'protochain'");
3278 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3281 * s[0] is a dummy entry to protect other BPF insn from damaged
3282 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3283 * hard to find interdependency made by jump table fixup.
3286 s
[i
] = new_stmt(0); /*dummy*/
3291 b0
= gen_linktype(ETHERTYPE_IP
);
3294 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3295 s
[i
]->s
.k
= off_nl
+ 9;
3297 /* X = ip->ip_hl << 2 */
3298 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3304 b0
= gen_linktype(ETHERTYPE_IPV6
);
3306 /* A = ip6->ip_nxt */
3307 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3308 s
[i
]->s
.k
= off_nl
+ 6;
3310 /* X = sizeof(struct ip6_hdr) */
3311 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3317 bpf_error("unsupported proto to gen_protochain");
3321 /* again: if (A == v) goto end; else fall through; */
3323 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3325 s
[i
]->s
.jt
= NULL
; /*later*/
3326 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3330 #ifndef IPPROTO_NONE
3331 #define IPPROTO_NONE 59
3333 /* if (A == IPPROTO_NONE) goto end */
3334 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3335 s
[i
]->s
.jt
= NULL
; /*later*/
3336 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3337 s
[i
]->s
.k
= IPPROTO_NONE
;
3338 s
[fix5
]->s
.jf
= s
[i
];
3343 if (proto
== Q_IPV6
) {
3344 int v6start
, v6end
, v6advance
, j
;
3347 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3348 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3349 s
[i
]->s
.jt
= NULL
; /*later*/
3350 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3351 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3352 s
[fix2
]->s
.jf
= s
[i
];
3354 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3355 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3356 s
[i
]->s
.jt
= NULL
; /*later*/
3357 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3358 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3360 /* if (A == IPPROTO_ROUTING) goto v6advance */
3361 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3362 s
[i
]->s
.jt
= NULL
; /*later*/
3363 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3364 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3366 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3367 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3368 s
[i
]->s
.jt
= NULL
; /*later*/
3369 s
[i
]->s
.jf
= NULL
; /*later*/
3370 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3381 * X = X + (P[X + 1] + 1) * 8;
3384 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3386 /* A = P[X + packet head] */
3387 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3391 s
[i
] = new_stmt(BPF_ST
);
3395 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3398 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3402 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3404 /* A = P[X + packet head]; */
3405 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3409 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3413 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3417 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3420 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3424 /* goto again; (must use BPF_JA for backward jump) */
3425 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3426 s
[i
]->s
.k
= again
- i
- 1;
3427 s
[i
- 1]->s
.jf
= s
[i
];
3431 for (j
= v6start
; j
<= v6end
; j
++)
3432 s
[j
]->s
.jt
= s
[v6advance
];
3437 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3439 s
[fix2
]->s
.jf
= s
[i
];
3445 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3446 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3447 s
[i
]->s
.jt
= NULL
; /*later*/
3448 s
[i
]->s
.jf
= NULL
; /*later*/
3449 s
[i
]->s
.k
= IPPROTO_AH
;
3451 s
[fix3
]->s
.jf
= s
[ahcheck
];
3458 * X = X + (P[X + 1] + 2) * 4;
3461 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3463 /* A = P[X + packet head]; */
3464 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3468 s
[i
] = new_stmt(BPF_ST
);
3472 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3475 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3479 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3481 /* A = P[X + packet head] */
3482 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3486 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3490 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3494 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3497 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3501 /* goto again; (must use BPF_JA for backward jump) */
3502 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3503 s
[i
]->s
.k
= again
- i
- 1;
3508 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3510 s
[fix2
]->s
.jt
= s
[end
];
3511 s
[fix4
]->s
.jf
= s
[end
];
3512 s
[fix5
]->s
.jt
= s
[end
];
3519 for (i
= 0; i
< max
- 1; i
++)
3520 s
[i
]->next
= s
[i
+ 1];
3521 s
[max
- 1]->next
= NULL
;
3526 b
= new_block(JMP(BPF_JEQ
));
3527 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3537 static struct block
*
3538 gen_proto(v
, proto
, dir
)
3543 struct block
*b0
, *b1
;
3545 if (dir
!= Q_DEFAULT
)
3546 bpf_error("direction applied to 'proto'");
3551 b0
= gen_proto(v
, Q_IP
, dir
);
3552 b1
= gen_proto(v
, Q_IPV6
, dir
);
3560 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3561 * not LLC encapsulation with LLCSAP_IP.
3563 * For IEEE 802 networks - which includes 802.5 token ring
3564 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3565 * says that SNAP encapsulation is used, not LLC encapsulation
3568 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3569 * RFC 2225 say that SNAP encapsulation is used, not LLC
3570 * encapsulation with LLCSAP_IP.
3572 * So we always check for ETHERTYPE_IP.
3574 b0
= gen_linktype(ETHERTYPE_IP
);
3576 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3578 b1
= gen_protochain(v
, Q_IP
);
3588 * Frame Relay packets typically have an OSI
3589 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3590 * generates code to check for all the OSI
3591 * NLPIDs, so calling it and then adding a check
3592 * for the particular NLPID for which we're
3593 * looking is bogus, as we can just check for
3596 * What we check for is the NLPID and a frame
3597 * control field value of UI, i.e. 0x03 followed
3600 * XXX - assumes a 2-byte Frame Relay header with
3601 * DLCI and flags. What if the address is longer?
3603 * XXX - what about SNAP-encapsulated frames?
3605 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3610 * Cisco uses an Ethertype lookalike - for OSI,
3613 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3614 /* OSI in C-HDLC is stuffed with a fudge byte */
3615 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3620 b0
= gen_linktype(LLCSAP_ISONS
);
3621 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3627 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3629 * 4 is the offset of the PDU type relative to the IS-IS
3632 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3637 bpf_error("arp does not encapsulate another protocol");
3641 bpf_error("rarp does not encapsulate another protocol");
3645 bpf_error("atalk encapsulation is not specifiable");
3649 bpf_error("decnet encapsulation is not specifiable");
3653 bpf_error("sca does not encapsulate another protocol");
3657 bpf_error("lat does not encapsulate another protocol");
3661 bpf_error("moprc does not encapsulate another protocol");
3665 bpf_error("mopdl does not encapsulate another protocol");
3669 return gen_linktype(v
);
3672 bpf_error("'udp proto' is bogus");
3676 bpf_error("'tcp proto' is bogus");
3680 bpf_error("'sctp proto' is bogus");
3684 bpf_error("'icmp proto' is bogus");
3688 bpf_error("'igmp proto' is bogus");
3692 bpf_error("'igrp proto' is bogus");
3696 bpf_error("'pim proto' is bogus");
3700 bpf_error("'vrrp proto' is bogus");
3705 b0
= gen_linktype(ETHERTYPE_IPV6
);
3707 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3709 b1
= gen_protochain(v
, Q_IPV6
);
3715 bpf_error("'icmp6 proto' is bogus");
3719 bpf_error("'ah proto' is bogus");
3722 bpf_error("'ah proto' is bogus");
3725 bpf_error("'stp proto' is bogus");
3728 bpf_error("'ipx proto' is bogus");
3731 bpf_error("'netbeui proto' is bogus");
3742 register const char *name
;
3745 int proto
= q
.proto
;
3749 bpf_u_int32 mask
, addr
;
3751 bpf_u_int32
**alist
;
3754 struct sockaddr_in
*sin
;
3755 struct sockaddr_in6
*sin6
;
3756 struct addrinfo
*res
, *res0
;
3757 struct in6_addr mask128
;
3759 struct block
*b
, *tmp
;
3760 int port
, real_proto
;
3765 addr
= pcap_nametonetaddr(name
);
3767 bpf_error("unknown network '%s'", name
);
3768 /* Left justify network addr and calculate its network mask */
3770 while (addr
&& (addr
& 0xff000000) == 0) {
3774 return gen_host(addr
, mask
, proto
, dir
);
3778 if (proto
== Q_LINK
) {
3782 eaddr
= pcap_ether_hostton(name
);
3785 "unknown ether host '%s'", name
);
3786 b
= gen_ehostop(eaddr
, dir
);
3791 eaddr
= pcap_ether_hostton(name
);
3794 "unknown FDDI host '%s'", name
);
3795 b
= gen_fhostop(eaddr
, dir
);
3800 eaddr
= pcap_ether_hostton(name
);
3803 "unknown token ring host '%s'", name
);
3804 b
= gen_thostop(eaddr
, dir
);
3808 case DLT_IEEE802_11
:
3809 eaddr
= pcap_ether_hostton(name
);
3812 "unknown 802.11 host '%s'", name
);
3813 b
= gen_wlanhostop(eaddr
, dir
);
3817 case DLT_IP_OVER_FC
:
3818 eaddr
= pcap_ether_hostton(name
);
3821 "unknown Fibre Channel host '%s'", name
);
3822 b
= gen_ipfchostop(eaddr
, dir
);
3831 * Check that the packet doesn't begin
3832 * with an LE Control marker. (We've
3833 * already generated a test for LANE.)
3835 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
3839 eaddr
= pcap_ether_hostton(name
);
3842 "unknown ether host '%s'", name
);
3843 b
= gen_ehostop(eaddr
, dir
);
3849 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3850 } else if (proto
== Q_DECNET
) {
3851 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
3853 * I don't think DECNET hosts can be multihomed, so
3854 * there is no need to build up a list of addresses
3856 return (gen_host(dn_addr
, 0, proto
, dir
));
3859 alist
= pcap_nametoaddr(name
);
3860 if (alist
== NULL
|| *alist
== NULL
)
3861 bpf_error("unknown host '%s'", name
);
3863 if (off_linktype
== -1 && tproto
== Q_DEFAULT
)
3865 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
3867 tmp
= gen_host(**alist
++, 0xffffffff,
3874 memset(&mask128
, 0xff, sizeof(mask128
));
3875 res0
= res
= pcap_nametoaddrinfo(name
);
3877 bpf_error("unknown host '%s'", name
);
3879 tproto
= tproto6
= proto
;
3880 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
3884 for (res
= res0
; res
; res
= res
->ai_next
) {
3885 switch (res
->ai_family
) {
3887 if (tproto
== Q_IPV6
)
3890 sin
= (struct sockaddr_in
*)
3892 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
3893 0xffffffff, tproto
, dir
);
3896 if (tproto6
== Q_IP
)
3899 sin6
= (struct sockaddr_in6
*)
3901 tmp
= gen_host6(&sin6
->sin6_addr
,
3902 &mask128
, tproto6
, dir
);
3913 bpf_error("unknown host '%s'%s", name
,
3914 (proto
== Q_DEFAULT
)
3916 : " for specified address family");
3923 if (proto
!= Q_DEFAULT
&&
3924 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
3925 bpf_error("illegal qualifier of 'port'");
3926 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
3927 bpf_error("unknown port '%s'", name
);
3928 if (proto
== Q_UDP
) {
3929 if (real_proto
== IPPROTO_TCP
)
3930 bpf_error("port '%s' is tcp", name
);
3931 else if (real_proto
== IPPROTO_SCTP
)
3932 bpf_error("port '%s' is sctp", name
);
3934 /* override PROTO_UNDEF */
3935 real_proto
= IPPROTO_UDP
;
3937 if (proto
== Q_TCP
) {
3938 if (real_proto
== IPPROTO_UDP
)
3939 bpf_error("port '%s' is udp", name
);
3941 else if (real_proto
== IPPROTO_SCTP
)
3942 bpf_error("port '%s' is sctp", name
);
3944 /* override PROTO_UNDEF */
3945 real_proto
= IPPROTO_TCP
;
3947 if (proto
== Q_SCTP
) {
3948 if (real_proto
== IPPROTO_UDP
)
3949 bpf_error("port '%s' is udp", name
);
3951 else if (real_proto
== IPPROTO_TCP
)
3952 bpf_error("port '%s' is tcp", name
);
3954 /* override PROTO_UNDEF */
3955 real_proto
= IPPROTO_SCTP
;
3958 return gen_port(port
, real_proto
, dir
);
3962 b
= gen_port(port
, real_proto
, dir
);
3963 gen_or(gen_port6(port
, real_proto
, dir
), b
);
3970 eaddr
= pcap_ether_hostton(name
);
3972 bpf_error("unknown ether host: %s", name
);
3974 alist
= pcap_nametoaddr(name
);
3975 if (alist
== NULL
|| *alist
== NULL
)
3976 bpf_error("unknown host '%s'", name
);
3977 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
3981 bpf_error("'gateway' not supported in this configuration");
3985 real_proto
= lookup_proto(name
, proto
);
3986 if (real_proto
>= 0)
3987 return gen_proto(real_proto
, proto
, dir
);
3989 bpf_error("unknown protocol: %s", name
);
3992 real_proto
= lookup_proto(name
, proto
);
3993 if (real_proto
>= 0)
3994 return gen_protochain(real_proto
, proto
, dir
);
3996 bpf_error("unknown protocol: %s", name
);
4008 gen_mcode(s1
, s2
, masklen
, q
)
4009 register const char *s1
, *s2
;
4010 register int masklen
;
4013 register int nlen
, mlen
;
4016 nlen
= __pcap_atoin(s1
, &n
);
4017 /* Promote short ipaddr */
4021 mlen
= __pcap_atoin(s2
, &m
);
4022 /* Promote short ipaddr */
4025 bpf_error("non-network bits set in \"%s mask %s\"",
4028 /* Convert mask len to mask */
4030 bpf_error("mask length must be <= 32");
4031 m
= 0xffffffff << (32 - masklen
);
4033 bpf_error("non-network bits set in \"%s/%d\"",
4040 return gen_host(n
, m
, q
.proto
, q
.dir
);
4043 bpf_error("Mask syntax for networks only");
4050 register const char *s
;
4055 int proto
= q
.proto
;
4061 else if (q
.proto
== Q_DECNET
)
4062 vlen
= __pcap_atodn(s
, &v
);
4064 vlen
= __pcap_atoin(s
, &v
);
4071 if (proto
== Q_DECNET
)
4072 return gen_host(v
, 0, proto
, dir
);
4073 else if (proto
== Q_LINK
) {
4074 bpf_error("illegal link layer address");
4077 if (s
== NULL
&& q
.addr
== Q_NET
) {
4078 /* Promote short net number */
4079 while (v
&& (v
& 0xff000000) == 0) {
4084 /* Promote short ipaddr */
4088 return gen_host(v
, mask
, proto
, dir
);
4093 proto
= IPPROTO_UDP
;
4094 else if (proto
== Q_TCP
)
4095 proto
= IPPROTO_TCP
;
4096 else if (proto
== Q_SCTP
)
4097 proto
= IPPROTO_SCTP
;
4098 else if (proto
== Q_DEFAULT
)
4099 proto
= PROTO_UNDEF
;
4101 bpf_error("illegal qualifier of 'port'");
4104 return gen_port((int)v
, proto
, dir
);
4108 b
= gen_port((int)v
, proto
, dir
);
4109 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4115 bpf_error("'gateway' requires a name");
4119 return gen_proto((int)v
, proto
, dir
);
4122 return gen_protochain((int)v
, proto
, dir
);
4137 gen_mcode6(s1
, s2
, masklen
, q
)
4138 register const char *s1
, *s2
;
4139 register int masklen
;
4142 struct addrinfo
*res
;
4143 struct in6_addr
*addr
;
4144 struct in6_addr mask
;
4149 bpf_error("no mask %s supported", s2
);
4151 res
= pcap_nametoaddrinfo(s1
);
4153 bpf_error("invalid ip6 address %s", s1
);
4155 bpf_error("%s resolved to multiple address", s1
);
4156 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4158 if (sizeof(mask
) * 8 < masklen
)
4159 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4160 memset(&mask
, 0, sizeof(mask
));
4161 memset(&mask
, 0xff, masklen
/ 8);
4163 mask
.s6_addr
[masklen
/ 8] =
4164 (0xff << (8 - masklen
% 8)) & 0xff;
4167 a
= (u_int32_t
*)addr
;
4168 m
= (u_int32_t
*)&mask
;
4169 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4170 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4171 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4179 bpf_error("Mask syntax for networks only");
4183 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4188 bpf_error("invalid qualifier against IPv6 address");
4196 register const u_char
*eaddr
;
4199 struct block
*b
, *tmp
;
4201 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4202 if (linktype
== DLT_EN10MB
)
4203 return gen_ehostop(eaddr
, (int)q
.dir
);
4204 if (linktype
== DLT_FDDI
)
4205 return gen_fhostop(eaddr
, (int)q
.dir
);
4206 if (linktype
== DLT_IEEE802
)
4207 return gen_thostop(eaddr
, (int)q
.dir
);
4208 if (linktype
== DLT_IEEE802_11
)
4209 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4210 if (linktype
== DLT_SUNATM
&& is_lane
) {
4212 * Check that the packet doesn't begin with an
4213 * LE Control marker. (We've already generated
4216 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4220 * Now check the MAC address.
4222 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4226 if (linktype
== DLT_IP_OVER_FC
)
4227 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4228 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4230 bpf_error("ethernet address used in non-ether expression");
4236 struct slist
*s0
, *s1
;
4239 * This is definitely not the best way to do this, but the
4240 * lists will rarely get long.
4247 static struct slist
*
4253 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4258 static struct slist
*
4264 s
= new_stmt(BPF_LD
|BPF_MEM
);
4270 gen_load(proto
, index
, size
)
4275 struct slist
*s
, *tmp
;
4277 int regno
= alloc_reg();
4279 free_reg(index
->regno
);
4283 bpf_error("data size must be 1, 2, or 4");
4299 bpf_error("unsupported index operation");
4303 * XXX - what about ATM LANE? Should the index be
4304 * relative to the beginning of the AAL5 frame, so
4305 * that 0 refers to the beginning of the LE Control
4306 * field, or relative to the beginning of the LAN
4307 * frame, so that 0 refers, for Ethernet LANE, to
4308 * the beginning of the destination address?
4310 s
= xfer_to_x(index
);
4311 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4313 sappend(index
->s
, s
);
4328 /* XXX Note that we assume a fixed link header here. */
4329 s
= xfer_to_x(index
);
4330 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4333 sappend(index
->s
, s
);
4335 b
= gen_proto_abbrev(proto
);
4337 gen_and(index
->b
, b
);
4349 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4351 sappend(s
, xfer_to_a(index
));
4352 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4353 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4354 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4356 sappend(index
->s
, s
);
4358 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4360 gen_and(index
->b
, b
);
4362 gen_and(gen_proto_abbrev(Q_IP
), b
);
4368 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4372 index
->regno
= regno
;
4373 s
= new_stmt(BPF_ST
);
4375 sappend(index
->s
, s
);
4381 gen_relation(code
, a0
, a1
, reversed
)
4383 struct arth
*a0
, *a1
;
4386 struct slist
*s0
, *s1
, *s2
;
4387 struct block
*b
, *tmp
;
4391 if (code
== BPF_JEQ
) {
4392 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4393 b
= new_block(JMP(code
));
4397 b
= new_block(BPF_JMP
|code
|BPF_X
);
4403 sappend(a0
->s
, a1
->s
);
4407 free_reg(a0
->regno
);
4408 free_reg(a1
->regno
);
4410 /* 'and' together protocol checks */
4413 gen_and(a0
->b
, tmp
= a1
->b
);
4429 int regno
= alloc_reg();
4430 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4433 s
= new_stmt(BPF_LD
|BPF_LEN
);
4434 s
->next
= new_stmt(BPF_ST
);
4435 s
->next
->s
.k
= regno
;
4450 a
= (struct arth
*)newchunk(sizeof(*a
));
4454 s
= new_stmt(BPF_LD
|BPF_IMM
);
4456 s
->next
= new_stmt(BPF_ST
);
4472 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4475 s
= new_stmt(BPF_ST
);
4483 gen_arth(code
, a0
, a1
)
4485 struct arth
*a0
, *a1
;
4487 struct slist
*s0
, *s1
, *s2
;
4491 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4496 sappend(a0
->s
, a1
->s
);
4498 free_reg(a0
->regno
);
4499 free_reg(a1
->regno
);
4501 s0
= new_stmt(BPF_ST
);
4502 a0
->regno
= s0
->s
.k
= alloc_reg();
4509 * Here we handle simple allocation of the scratch registers.
4510 * If too many registers are alloc'd, the allocator punts.
4512 static int regused
[BPF_MEMWORDS
];
4516 * Return the next free register.
4521 int n
= BPF_MEMWORDS
;
4524 if (regused
[curreg
])
4525 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4527 regused
[curreg
] = 1;
4531 bpf_error("too many registers needed to evaluate expression");
4536 * Return a register to the table so it can
4546 static struct block
*
4553 s
= new_stmt(BPF_LD
|BPF_LEN
);
4554 b
= new_block(JMP(jmp
));
4565 return gen_len(BPF_JGE
, n
);
4569 * Actually, this is less than or equal.
4577 b
= gen_len(BPF_JGT
, n
);
4584 gen_byteop(op
, idx
, val
)
4595 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4598 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4599 b
->s
.code
= JMP(BPF_JGE
);
4604 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4605 b
->s
.code
= JMP(BPF_JGT
);
4609 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4613 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4617 b
= new_block(JMP(BPF_JEQ
));
4624 static u_char abroadcast
[] = { 0x0 };
4627 gen_broadcast(proto
)
4630 bpf_u_int32 hostmask
;
4631 struct block
*b0
, *b1
, *b2
;
4632 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4638 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4639 return gen_ahostop(abroadcast
, Q_DST
);
4640 if (linktype
== DLT_EN10MB
)
4641 return gen_ehostop(ebroadcast
, Q_DST
);
4642 if (linktype
== DLT_FDDI
)
4643 return gen_fhostop(ebroadcast
, Q_DST
);
4644 if (linktype
== DLT_IEEE802
)
4645 return gen_thostop(ebroadcast
, Q_DST
);
4646 if (linktype
== DLT_IEEE802_11
)
4647 return gen_wlanhostop(ebroadcast
, Q_DST
);
4648 if (linktype
== DLT_SUNATM
&& is_lane
) {
4650 * Check that the packet doesn't begin with an
4651 * LE Control marker. (We've already generated
4654 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4658 * Now check the MAC address.
4660 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4664 bpf_error("not a broadcast link");
4668 b0
= gen_linktype(ETHERTYPE_IP
);
4669 hostmask
= ~netmask
;
4670 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4671 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4672 (bpf_int32
)(~0 & hostmask
), hostmask
);
4677 bpf_error("only ether/ip broadcast filters supported");
4681 * Generate code to test the low-order bit of a MAC address (that's
4682 * the bottom bit of the *first* byte).
4684 static struct block
*
4685 gen_mac_multicast(offset
)
4688 register struct block
*b0
;
4689 register struct slist
*s
;
4691 /* link[offset] & 1 != 0 */
4692 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4694 b0
= new_block(JMP(BPF_JSET
));
4701 gen_multicast(proto
)
4704 register struct block
*b0
, *b1
, *b2
;
4705 register struct slist
*s
;
4711 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4712 /* all ARCnet multicasts use the same address */
4713 return gen_ahostop(abroadcast
, Q_DST
);
4715 if (linktype
== DLT_EN10MB
) {
4716 /* ether[0] & 1 != 0 */
4717 return gen_mac_multicast(0);
4720 if (linktype
== DLT_FDDI
) {
4722 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4724 * XXX - was that referring to bit-order issues?
4726 /* fddi[1] & 1 != 0 */
4727 return gen_mac_multicast(1);
4730 if (linktype
== DLT_IEEE802
) {
4731 /* tr[2] & 1 != 0 */
4732 return gen_mac_multicast(2);
4735 if (linktype
== DLT_IEEE802_11
) {
4739 * For control frames, there is no DA.
4741 * For management frames, DA is at an
4742 * offset of 4 from the beginning of
4745 * For data frames, DA is at an offset
4746 * of 4 from the beginning of the packet
4747 * if To DS is clear and at an offset of
4748 * 16 from the beginning of the packet
4753 * Generate the tests to be done for data frames.
4755 * First, check for To DS set, i.e. "link[1] & 0x01".
4757 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4759 b1
= new_block(JMP(BPF_JSET
));
4760 b1
->s
.k
= 0x01; /* To DS */
4764 * If To DS is set, the DA is at 16.
4766 b0
= gen_mac_multicast(16);
4770 * Now, check for To DS not set, i.e. check
4771 * "!(link[1] & 0x01)".
4773 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4775 b2
= new_block(JMP(BPF_JSET
));
4776 b2
->s
.k
= 0x01; /* To DS */
4781 * If To DS is not set, the DA is at 4.
4783 b1
= gen_mac_multicast(4);
4787 * Now OR together the last two checks. That gives
4788 * the complete set of checks for data frames.
4793 * Now check for a data frame.
4794 * I.e, check "link[0] & 0x08".
4796 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4798 b1
= new_block(JMP(BPF_JSET
));
4803 * AND that with the checks done for data frames.
4808 * If the high-order bit of the type value is 0, this
4809 * is a management frame.
4810 * I.e, check "!(link[0] & 0x08)".
4812 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4814 b2
= new_block(JMP(BPF_JSET
));
4820 * For management frames, the DA is at 4.
4822 b1
= gen_mac_multicast(4);
4826 * OR that with the checks done for data frames.
4827 * That gives the checks done for management and
4833 * If the low-order bit of the type value is 1,
4834 * this is either a control frame or a frame
4835 * with a reserved type, and thus not a
4838 * I.e., check "!(link[0] & 0x04)".
4840 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4842 b1
= new_block(JMP(BPF_JSET
));
4848 * AND that with the checks for data and management
4855 if (linktype
== DLT_SUNATM
&& is_lane
) {
4857 * Check that the packet doesn't begin with an
4858 * LE Control marker. (We've already generated
4861 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4864 /* ether[off_mac] & 1 != 0 */
4865 b0
= gen_mac_multicast(off_mac
);
4870 /* Link not known to support multicasts */
4874 b0
= gen_linktype(ETHERTYPE_IP
);
4875 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
4876 b1
->s
.code
= JMP(BPF_JGE
);
4882 b0
= gen_linktype(ETHERTYPE_IPV6
);
4883 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
4888 bpf_error("only IP multicast filters supported on ethernet/FDDI");
4892 * generate command for inbound/outbound. It's here so we can
4893 * make it link-type specific. 'dir' = 0 implies "inbound",
4894 * = 1 implies "outbound".
4900 register struct block
*b0
;
4903 * Only some data link types support inbound/outbound qualifiers.
4908 b0
= gen_relation(BPF_JEQ
,
4909 gen_load(Q_LINK
, gen_loadi(0), 1),
4917 * Match packets sent by this machine.
4919 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
4922 * Match packets sent to this machine.
4923 * (No broadcast or multicast packets, or
4924 * packets sent to some other machine and
4925 * received promiscuously.)
4927 * XXX - packets sent to other machines probably
4928 * shouldn't be matched, but what about broadcast
4929 * or multicast packets we received?
4931 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
4936 bpf_error("inbound/outbound not supported on linktype %d\n",
4946 register const u_char
*eaddr
;
4949 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4950 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4951 return gen_ahostop(eaddr
, (int)q
.dir
);
4953 bpf_error("ARCnet address used in non-arc expression");
4957 static struct block
*
4958 gen_ahostop(eaddr
, dir
)
4959 register const u_char
*eaddr
;
4962 register struct block
*b0
, *b1
;
4965 /* src comes first, different from Ethernet */
4967 return gen_bcmp(0, 1, eaddr
);
4970 return gen_bcmp(1, 1, eaddr
);
4973 b0
= gen_ahostop(eaddr
, Q_SRC
);
4974 b1
= gen_ahostop(eaddr
, Q_DST
);
4980 b0
= gen_ahostop(eaddr
, Q_SRC
);
4981 b1
= gen_ahostop(eaddr
, Q_DST
);
4990 * support IEEE 802.1Q VLAN trunk over ethernet
4999 * Change the offsets to point to the type and data fields within
5000 * the VLAN packet. This is somewhat of a kludge.
5002 if (orig_nl
== (u_int
)-1) {
5003 orig_linktype
= off_linktype
; /* save original values */
5005 orig_nl_nosnap
= off_nl_nosnap
;
5016 bpf_error("no VLAN support for data link type %d",
5022 /* check for VLAN */
5023 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5025 /* If a specific VLAN is requested, check VLAN id */
5026 if (vlan_num
>= 0) {
5029 b1
= gen_cmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
);
5038 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5050 bpf_error("'vpi' supported only on raw ATM");
5053 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5054 (u_int
)jvalue
, reverse
);
5059 bpf_error("'vci' supported only on raw ATM");
5062 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5063 (u_int
)jvalue
, reverse
);
5067 if (off_proto
== -1)
5068 abort(); /* XXX - this isn't on FreeBSD */
5069 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5070 (u_int
)jvalue
, reverse
);
5074 if (off_payload
== -1)
5076 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5077 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5082 bpf_error("'callref' supported only on raw ATM");
5083 if (off_proto
== -1)
5085 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5086 (u_int
)jvalue
, reverse
);
5096 gen_atmtype_abbrev(type
)
5099 struct block
*b0
, *b1
;
5104 /* Get all packets in Meta signalling Circuit */
5106 bpf_error("'metac' supported only on raw ATM");
5107 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5108 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5113 /* Get all packets in Broadcast Circuit*/
5115 bpf_error("'bcc' supported only on raw ATM");
5116 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5117 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5122 /* Get all cells in Segment OAM F4 circuit*/
5124 bpf_error("'oam4sc' supported only on raw ATM");
5125 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5126 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5131 /* Get all cells in End-to-End OAM F4 Circuit*/
5133 bpf_error("'oam4ec' supported only on raw ATM");
5134 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5135 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5140 /* Get all packets in connection Signalling Circuit */
5142 bpf_error("'sc' supported only on raw ATM");
5143 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5144 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5149 /* Get all packets in ILMI Circuit */
5151 bpf_error("'ilmic' supported only on raw ATM");
5152 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5153 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5158 /* Get all LANE packets */
5160 bpf_error("'lane' supported only on raw ATM");
5161 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5164 * Arrange that all subsequent tests assume LANE
5165 * rather than LLC-encapsulated packets, and set
5166 * the offsets appropriately for LANE-encapsulated
5169 * "off_mac" is the offset of the Ethernet header,
5170 * which is 2 bytes past the ATM pseudo-header
5171 * (skipping the pseudo-header and 2-byte LE Client
5172 * field). The other offsets are Ethernet offsets
5173 * relative to "off_mac".
5176 off_mac
= off_payload
+ 2; /* MAC header */
5177 off_linktype
= off_mac
+ 12;
5178 off_nl
= off_mac
+ 14; /* Ethernet II */
5179 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5183 /* Get all LLC-encapsulated packets */
5185 bpf_error("'llc' supported only on raw ATM");
5186 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5197 static struct block
*
5198 gen_msg_abbrev(type
)
5204 * Q.2931 signalling protocol messages for handling virtual circuits
5205 * establishment and teardown
5210 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5214 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5218 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5222 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5226 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5229 case A_RELEASE_DONE
:
5230 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5240 gen_atmmulti_abbrev(type
)
5243 struct block
*b0
, *b1
;
5249 bpf_error("'oam' supported only on raw ATM");
5250 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5255 bpf_error("'oamf4' supported only on raw ATM");
5257 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5258 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5260 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5266 * Get Q.2931 signalling messages for switched
5267 * virtual connection
5270 bpf_error("'connectmsg' supported only on raw ATM");
5271 b0
= gen_msg_abbrev(A_SETUP
);
5272 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5274 b0
= gen_msg_abbrev(A_CONNECT
);
5276 b0
= gen_msg_abbrev(A_CONNECTACK
);
5278 b0
= gen_msg_abbrev(A_RELEASE
);
5280 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5282 b0
= gen_atmtype_abbrev(A_SC
);
5288 bpf_error("'metaconnect' supported only on raw ATM");
5289 b0
= gen_msg_abbrev(A_SETUP
);
5290 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5292 b0
= gen_msg_abbrev(A_CONNECT
);
5294 b0
= gen_msg_abbrev(A_RELEASE
);
5296 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5298 b0
= gen_atmtype_abbrev(A_METAC
);