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
[] _U_
=
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.236 2005-05-01 00:55:42 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
118 if (bpf_pcap
!= NULL
)
119 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
126 static void init_linktype(pcap_t
*);
128 static int alloc_reg(void);
129 static void free_reg(int);
131 static struct block
*root
;
134 * We divy out chunks of memory rather than call malloc each time so
135 * we don't have to worry about leaking memory. It's probably
136 * not a big deal if all this memory was wasted but if this ever
137 * goes into a library that would probably not be a good idea.
139 * XXX - this *is* in a library....
142 #define CHUNK0SIZE 1024
148 static struct chunk chunks
[NCHUNKS
];
149 static int cur_chunk
;
151 static void *newchunk(u_int
);
152 static void freechunks(void);
153 static inline struct block
*new_block(int);
154 static inline struct slist
*new_stmt(int);
155 static struct block
*gen_retblk(int);
156 static inline void syntax(void);
158 static void backpatch(struct block
*, struct block
*);
159 static void merge(struct block
*, struct block
*);
160 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
161 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
162 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
163 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
164 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
165 bpf_u_int32
, bpf_u_int32
, int);
166 static struct slist
*gen_load_llrel(u_int
, u_int
);
167 static struct slist
*gen_load_nlrel(u_int
, u_int
);
168 static struct slist
*gen_load_ipv4tlrel(u_int
, u_int
);
170 static struct slist
*gen_load_ipv6tlrel(u_int
, u_int
);
172 static struct block
*gen_uncond(int);
173 static inline struct block
*gen_true(void);
174 static inline struct block
*gen_false(void);
175 static struct block
*gen_ether_linktype(int);
176 static struct block
*gen_linux_sll_linktype(int);
177 static struct block
*gen_linktype(int);
178 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
179 static struct block
*gen_llc_linktype(int);
180 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
182 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
184 static struct block
*gen_ahostop(const u_char
*, int);
185 static struct block
*gen_ehostop(const u_char
*, int);
186 static struct block
*gen_fhostop(const u_char
*, int);
187 static struct block
*gen_thostop(const u_char
*, int);
188 static struct block
*gen_wlanhostop(const u_char
*, int);
189 static struct block
*gen_ipfchostop(const u_char
*, int);
190 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
191 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
193 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
196 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
198 static struct block
*gen_ipfrag(void);
199 static struct block
*gen_portatom(int, bpf_int32
);
200 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
202 static struct block
*gen_portatom6(int, bpf_int32
);
203 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
205 struct block
*gen_portop(int, int, int);
206 static struct block
*gen_port(int, int, int);
207 struct block
*gen_portrangeop(int, int, int, int);
208 static struct block
*gen_portrange(int, int, int, int);
210 struct block
*gen_portop6(int, int, int);
211 static struct block
*gen_port6(int, int, int);
212 struct block
*gen_portrangeop6(int, int, int, int);
213 static struct block
*gen_portrange6(int, int, int, int);
215 static int lookup_proto(const char *, int);
216 static struct block
*gen_protochain(int, int, int);
217 static struct block
*gen_proto(int, int, int);
218 static struct slist
*xfer_to_x(struct arth
*);
219 static struct slist
*xfer_to_a(struct arth
*);
220 static struct block
*gen_mac_multicast(int);
221 static struct block
*gen_len(int, int);
223 static struct block
*gen_msg_abbrev(int type
);
234 /* XXX Round up to nearest long. */
235 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
237 /* XXX Round up to structure boundary. */
241 cp
= &chunks
[cur_chunk
];
242 if (n
> cp
->n_left
) {
243 ++cp
, k
= ++cur_chunk
;
245 bpf_error("out of memory");
246 size
= CHUNK0SIZE
<< k
;
247 cp
->m
= (void *)malloc(size
);
249 bpf_error("out of memory");
250 memset((char *)cp
->m
, 0, size
);
253 bpf_error("out of memory");
256 return (void *)((char *)cp
->m
+ cp
->n_left
);
265 for (i
= 0; i
< NCHUNKS
; ++i
)
266 if (chunks
[i
].m
!= NULL
) {
273 * A strdup whose allocations are freed after code generation is over.
277 register const char *s
;
279 int n
= strlen(s
) + 1;
280 char *cp
= newchunk(n
);
286 static inline struct block
*
292 p
= (struct block
*)newchunk(sizeof(*p
));
299 static inline struct slist
*
305 p
= (struct slist
*)newchunk(sizeof(*p
));
311 static struct block
*
315 struct block
*b
= new_block(BPF_RET
|BPF_K
);
324 bpf_error("syntax error in filter expression");
327 static bpf_u_int32 netmask
;
332 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
333 char *buf
, int optimize
, bpf_u_int32 mask
)
342 if (setjmp(top_ctx
)) {
350 snaplen
= pcap_snapshot(p
);
352 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
353 "snaplen of 0 rejects all packets");
357 lex_init(buf
? buf
: "");
365 root
= gen_retblk(snaplen
);
367 if (optimize
&& !no_optimize
) {
370 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
371 bpf_error("expression rejects all packets");
373 program
->bf_insns
= icode_to_fcode(root
, &len
);
374 program
->bf_len
= len
;
382 * entry point for using the compiler with no pcap open
383 * pass in all the stuff that is needed explicitly instead.
386 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
387 struct bpf_program
*program
,
388 char *buf
, int optimize
, bpf_u_int32 mask
)
393 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
396 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
402 * Clean up a "struct bpf_program" by freeing all the memory allocated
406 pcap_freecode(struct bpf_program
*program
)
409 if (program
->bf_insns
!= NULL
) {
410 free((char *)program
->bf_insns
);
411 program
->bf_insns
= NULL
;
416 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
417 * which of the jt and jf fields has been resolved and which is a pointer
418 * back to another unresolved block (or nil). At least one of the fields
419 * in each block is already resolved.
422 backpatch(list
, target
)
423 struct block
*list
, *target
;
440 * Merge the lists in b0 and b1, using the 'sense' field to indicate
441 * which of jt and jf is the link.
445 struct block
*b0
, *b1
;
447 register struct block
**p
= &b0
;
449 /* Find end of list. */
451 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
453 /* Concatenate the lists. */
461 backpatch(p
, gen_retblk(snaplen
));
462 p
->sense
= !p
->sense
;
463 backpatch(p
, gen_retblk(0));
469 struct block
*b0
, *b1
;
471 backpatch(b0
, b1
->head
);
472 b0
->sense
= !b0
->sense
;
473 b1
->sense
= !b1
->sense
;
475 b1
->sense
= !b1
->sense
;
481 struct block
*b0
, *b1
;
483 b0
->sense
= !b0
->sense
;
484 backpatch(b0
, b1
->head
);
485 b0
->sense
= !b0
->sense
;
494 b
->sense
= !b
->sense
;
497 static struct block
*
498 gen_cmp(offset
, size
, v
)
505 s
= gen_load_llrel(offset
, size
);
507 b
= new_block(JMP(BPF_JEQ
));
514 static struct block
*
515 gen_cmp_gt(offset
, size
, v
)
522 s
= gen_load_llrel(offset
, size
);
524 b
= new_block(JMP(BPF_JGT
));
531 static struct block
*
532 gen_mcmp(offset
, size
, v
, mask
)
537 struct block
*b
= gen_cmp(offset
, size
, v
);
540 if (mask
!= 0xffffffff) {
541 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
548 static struct block
*
549 gen_bcmp(offset
, size
, v
)
550 register u_int offset
, size
;
551 register const u_char
*v
;
553 register struct block
*b
, *tmp
;
557 register const u_char
*p
= &v
[size
- 4];
558 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
559 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
561 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
568 register const u_char
*p
= &v
[size
- 2];
569 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
571 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
578 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
586 static struct block
*
587 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
588 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
594 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
597 if (mask
!= 0xffffffff) {
598 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
602 b
= new_block(JMP(jtype
));
605 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
611 * Various code constructs need to know the layout of the data link
612 * layer. These variables give the necessary offsets.
616 * This is the offset of the beginning of the MAC-layer header.
617 * It's usually 0, except for ATM LANE.
619 static u_int off_mac
;
622 * "off_linktype" is the offset to information in the link-layer header
623 * giving the packet type.
625 * For Ethernet, it's the offset of the Ethernet type field.
627 * For link-layer types that always use 802.2 headers, it's the
628 * offset of the LLC header.
630 * For PPP, it's the offset of the PPP type field.
632 * For Cisco HDLC, it's the offset of the CHDLC type field.
634 * For BSD loopback, it's the offset of the AF_ value.
636 * For Linux cooked sockets, it's the offset of the type field.
638 * It's set to -1 for no encapsulation, in which case, IP is assumed.
640 static u_int off_linktype
;
643 * TRUE if the link layer includes an ATM pseudo-header.
645 static int is_atm
= 0;
648 * TRUE if "lane" appeared in the filter; it causes us to generate
649 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
651 static int is_lane
= 0;
654 * These are offsets for the ATM pseudo-header.
656 static u_int off_vpi
;
657 static u_int off_vci
;
658 static u_int off_proto
;
661 * This is the offset of the first byte after the ATM pseudo_header,
662 * or -1 if there is no ATM pseudo-header.
664 static u_int off_payload
;
667 * These are offsets to the beginning of the network-layer header.
669 * If the link layer never uses 802.2 LLC:
671 * "off_nl" and "off_nl_nosnap" are the same.
673 * If the link layer always uses 802.2 LLC:
675 * "off_nl" is the offset if there's a SNAP header following
678 * "off_nl_nosnap" is the offset if there's no SNAP header.
680 * If the link layer is Ethernet:
682 * "off_nl" is the offset if the packet is an Ethernet II packet
683 * (we assume no 802.3+802.2+SNAP);
685 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
686 * with an 802.2 header following it.
689 static u_int off_nl_nosnap
;
697 linktype
= pcap_datalink(p
);
699 pcap_fddipad
= p
->fddipad
;
703 * Assume it's not raw ATM with a pseudo-header, for now.
720 off_nl
= 6; /* XXX in reality, variable! */
721 off_nl_nosnap
= 6; /* no 802.2 LLC */
724 case DLT_ARCNET_LINUX
:
726 off_nl
= 8; /* XXX in reality, variable! */
727 off_nl_nosnap
= 8; /* no 802.2 LLC */
732 off_nl
= 14; /* Ethernet II */
733 off_nl_nosnap
= 17; /* 802.3+802.2 */
738 * SLIP doesn't have a link level type. The 16 byte
739 * header is hacked into our SLIP driver.
743 off_nl_nosnap
= 16; /* no 802.2 LLC */
747 /* XXX this may be the same as the DLT_PPP_BSDOS case */
751 off_nl_nosnap
= 24; /* no 802.2 LLC */
758 off_nl_nosnap
= 4; /* no 802.2 LLC */
764 off_nl_nosnap
= 12; /* no 802.2 LLC */
769 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
770 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
773 off_nl_nosnap
= 4; /* no 802.2 LLC */
778 * This does no include the Ethernet header, and
779 * only covers session state.
783 off_nl_nosnap
= 8; /* no 802.2 LLC */
789 off_nl_nosnap
= 24; /* no 802.2 LLC */
794 * FDDI doesn't really have a link-level type field.
795 * We set "off_linktype" to the offset of the LLC header.
797 * To check for Ethernet types, we assume that SSAP = SNAP
798 * is being used and pick out the encapsulated Ethernet type.
799 * XXX - should we generate code to check for SNAP?
803 off_linktype
+= pcap_fddipad
;
805 off_nl
= 21; /* FDDI+802.2+SNAP */
806 off_nl_nosnap
= 16; /* FDDI+802.2 */
808 off_nl
+= pcap_fddipad
;
809 off_nl_nosnap
+= pcap_fddipad
;
815 * Token Ring 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.
823 * Some various Linux patched versions gave 38
824 * as "off_linktype" and 40 as "off_nl"; however,
825 * if a token ring packet has *no* routing
826 * information, i.e. is not source-routed, the correct
827 * values are 20 and 22, as they are in the vanilla code.
829 * A packet is source-routed iff the uppermost bit
830 * of the first byte of the source address, at an
831 * offset of 8, has the uppermost bit set. If the
832 * packet is source-routed, the total number of bytes
833 * of routing information is 2 plus bits 0x1F00 of
834 * the 16-bit value at an offset of 14 (shifted right
835 * 8 - figure out which byte that is).
838 off_nl
= 22; /* Token Ring+802.2+SNAP */
839 off_nl_nosnap
= 17; /* Token Ring+802.2 */
844 * 802.11 doesn't really have a link-level type field.
845 * We set "off_linktype" to the offset of the LLC header.
847 * To check for Ethernet types, we assume that SSAP = SNAP
848 * is being used and pick out the encapsulated Ethernet type.
849 * XXX - should we generate code to check for SNAP?
851 * XXX - the header is actually variable-length. We
852 * assume a 24-byte link-layer header, as appears in
853 * data frames in networks with no bridges. If the
854 * fromds and tods 802.11 header bits are both set,
855 * it's actually supposed to be 30 bytes.
858 off_nl
= 32; /* 802.11+802.2+SNAP */
859 off_nl_nosnap
= 27; /* 802.11+802.2 */
862 case DLT_PRISM_HEADER
:
864 * Same as 802.11, but with an additional header before
865 * the 802.11 header, containing a bunch of additional
866 * information including radio-level information.
868 * The header is 144 bytes long.
870 * XXX - same variable-length header problem; at least
871 * the Prism header is fixed-length.
873 off_linktype
= 144+24;
874 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
875 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
878 case DLT_IEEE802_11_RADIO_AVS
:
880 * Same as 802.11, but with an additional header before
881 * the 802.11 header, containing a bunch of additional
882 * information including radio-level information.
884 * The header is 64 bytes long, at least in its
885 * current incarnation.
887 * XXX - same variable-length header problem, only
888 * more so; this header is also variable-length,
889 * with the length being the 32-bit big-endian
890 * number at an offset of 4 from the beginning
891 * of the radio header.
893 off_linktype
= 64+24;
894 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
895 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
898 case DLT_IEEE802_11_RADIO
:
900 * Same as 802.11, but with an additional header before
901 * the 802.11 header, containing a bunch of additional
902 * information including radio-level information.
904 * XXX - same variable-length header problem, only
905 * even *more* so; this header is also variable-length,
906 * with the length being the 16-bit number at an offset
907 * of 2 from the beginning of the radio header, and it's
908 * device-dependent (different devices might supply
909 * different amounts of information), so we can't even
910 * assume a fixed length for the current version of the
913 * Therefore, currently, only raw "link[N:M]" filtering is
921 case DLT_ATM_RFC1483
:
922 case DLT_ATM_CLIP
: /* Linux ATM defines this */
924 * assume routed, non-ISO PDUs
925 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
928 off_nl
= 8; /* 802.2+SNAP */
929 off_nl_nosnap
= 3; /* 802.2 */
934 * Full Frontal ATM; you get AALn PDUs with an ATM
938 off_vpi
= SUNATM_VPI_POS
;
939 off_vci
= SUNATM_VCI_POS
;
940 off_proto
= PROTO_POS
;
941 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
942 off_payload
= SUNATM_PKT_BEGIN_POS
;
943 off_linktype
= off_payload
;
944 off_nl
= off_payload
+8; /* 802.2+SNAP */
945 off_nl_nosnap
= off_payload
+3; /* 802.2 */
951 off_nl_nosnap
= 0; /* no 802.2 LLC */
954 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
957 off_nl_nosnap
= 16; /* no 802.2 LLC */
962 * LocalTalk does have a 1-byte type field in the LLAP header,
963 * but really it just indicates whether there is a "short" or
964 * "long" DDP packet following.
968 off_nl_nosnap
= 0; /* no 802.2 LLC */
973 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
974 * link-level type field. We set "off_linktype" to the
975 * offset of the LLC header.
977 * To check for Ethernet types, we assume that SSAP = SNAP
978 * is being used and pick out the encapsulated Ethernet type.
979 * XXX - should we generate code to check for SNAP? RFC
980 * 2625 says SNAP should be used.
983 off_nl
= 24; /* IPFC+802.2+SNAP */
984 off_nl_nosnap
= 19; /* IPFC+802.2 */
989 * XXX - we should set this to handle SNAP-encapsulated
990 * frames (NLPID of 0x80).
994 off_nl_nosnap
= 0; /* no 802.2 LLC */
997 case DLT_APPLE_IP_OVER_IEEE1394
:
1000 off_nl_nosnap
= 18; /* no 802.2 LLC */
1003 case DLT_LINUX_IRDA
:
1005 * Currently, only raw "link[N:M]" filtering is supported.
1014 * Currently, only raw "link[N:M]" filtering is supported.
1021 case DLT_SYMANTEC_FIREWALL
:
1023 off_nl
= 44; /* Ethernet II */
1024 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1029 /* XXX read this from pf.h? */
1030 off_nl
= PFLOG_HDRLEN
;
1031 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1034 case DLT_JUNIPER_MLFR
:
1035 case DLT_JUNIPER_MLPPP
:
1038 off_nl_nosnap
= -1; /* no 802.2 LLC */
1041 case DLT_JUNIPER_ATM1
:
1042 off_linktype
= 4; /* in reality variable between 4-8 */
1047 case DLT_JUNIPER_ATM2
:
1048 off_linktype
= 8; /* in reality variable between 8-12 */
1061 case DLT_LINUX_LAPD
:
1063 * Currently, only raw "link[N:M]" filtering is supported.
1070 bpf_error("unknown data link type %d", linktype
);
1075 * Load a value relative to the beginning of the link-layer header.
1077 static struct slist
*
1078 gen_load_llrel(offset
, size
)
1083 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1089 * Load a value relative to the beginning of the network-layer header.
1091 static struct slist
*
1092 gen_load_nlrel(offset
, size
)
1095 return gen_load_llrel(off_nl
+ offset
, size
);
1099 * Load a value relative to the beginning of the transport-layer header,
1100 * where the network-layer header is an IPv4 header. (This doesn't handle
1103 static struct slist
*
1104 gen_load_ipv4tlrel(offset
, size
)
1110 * Load the X register with the length of the IPv4 header,
1113 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1117 * Load the item at {length of the link-layer header} + {length
1118 * of the IPv4 header} + {specified offset}.
1120 s
->next
= new_stmt(BPF_LD
|BPF_IND
|size
);
1121 s
->next
->s
.k
= off_nl
+ offset
;
1128 * Load a value relative to the beginning of the transport-layer header,
1129 * where the network-layer header is an IPv6 header. (This doesn't handle
1130 * extension headers.)
1132 static struct slist
*
1133 gen_load_ipv6tlrel(offset
, size
)
1138 s
= gen_load_nlrel(40 + offset
, size
);
1143 static struct block
*
1150 s
= new_stmt(BPF_LD
|BPF_IMM
);
1152 b
= new_block(JMP(BPF_JEQ
));
1158 static inline struct block
*
1161 return gen_uncond(1);
1164 static inline struct block
*
1167 return gen_uncond(0);
1171 * Byte-swap a 32-bit number.
1172 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1173 * big-endian platforms.)
1175 #define SWAPLONG(y) \
1176 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1179 * Generate code to match a particular packet type.
1181 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1182 * value, if <= ETHERMTU. We use that to determine whether to
1183 * match the type/length field or to check the type/length field for
1184 * a value <= ETHERMTU to see whether it's a type field and then do
1185 * the appropriate test.
1187 static struct block
*
1188 gen_ether_linktype(proto
)
1191 struct block
*b0
, *b1
;
1197 case LLCSAP_NETBEUI
:
1199 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1200 * so we check the DSAP and SSAP.
1202 * LLCSAP_IP checks for IP-over-802.2, rather
1203 * than IP-over-Ethernet or IP-over-SNAP.
1205 * XXX - should we check both the DSAP and the
1206 * SSAP, like this, or should we check just the
1207 * DSAP, as we do for other types <= ETHERMTU
1208 * (i.e., other SAP values)?
1210 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1212 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1213 ((proto
<< 8) | proto
));
1221 * Ethernet_II frames, which are Ethernet
1222 * frames with a frame type of ETHERTYPE_IPX;
1224 * Ethernet_802.3 frames, which are 802.3
1225 * frames (i.e., the type/length field is
1226 * a length field, <= ETHERMTU, rather than
1227 * a type field) with the first two bytes
1228 * after the Ethernet/802.3 header being
1231 * Ethernet_802.2 frames, which are 802.3
1232 * frames with an 802.2 LLC header and
1233 * with the IPX LSAP as the DSAP in the LLC
1236 * Ethernet_SNAP frames, which are 802.3
1237 * frames with an LLC header and a SNAP
1238 * header and with an OUI of 0x000000
1239 * (encapsulated Ethernet) and a protocol
1240 * ID of ETHERTYPE_IPX in the SNAP header.
1242 * XXX - should we generate the same code both
1243 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1247 * This generates code to check both for the
1248 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1250 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1251 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1255 * Now we add code to check for SNAP frames with
1256 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1258 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1262 * Now we generate code to check for 802.3
1263 * frames in general.
1265 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1269 * Now add the check for 802.3 frames before the
1270 * check for Ethernet_802.2 and Ethernet_802.3,
1271 * as those checks should only be done on 802.3
1272 * frames, not on Ethernet frames.
1277 * Now add the check for Ethernet_II frames, and
1278 * do that before checking for the other frame
1281 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1285 case ETHERTYPE_ATALK
:
1286 case ETHERTYPE_AARP
:
1288 * EtherTalk (AppleTalk protocols on Ethernet link
1289 * layer) may use 802.2 encapsulation.
1293 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1294 * we check for an Ethernet type field less than
1295 * 1500, which means it's an 802.3 length field.
1297 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1301 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1302 * SNAP packets with an organization code of
1303 * 0x080007 (Apple, for Appletalk) and a protocol
1304 * type of ETHERTYPE_ATALK (Appletalk).
1306 * 802.2-encapsulated ETHERTYPE_AARP packets are
1307 * SNAP packets with an organization code of
1308 * 0x000000 (encapsulated Ethernet) and a protocol
1309 * type of ETHERTYPE_AARP (Appletalk ARP).
1311 if (proto
== ETHERTYPE_ATALK
)
1312 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1313 else /* proto == ETHERTYPE_AARP */
1314 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1318 * Check for Ethernet encapsulation (Ethertalk
1319 * phase 1?); we just check for the Ethernet
1322 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1328 if (proto
<= ETHERMTU
) {
1330 * This is an LLC SAP value, so the frames
1331 * that match would be 802.2 frames.
1332 * Check that the frame is an 802.2 frame
1333 * (i.e., that the length/type field is
1334 * a length field, <= ETHERMTU) and
1335 * then check the DSAP.
1337 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1339 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1344 * This is an Ethernet type, so compare
1345 * the length/type field with it (if
1346 * the frame is an 802.2 frame, the length
1347 * field will be <= ETHERMTU, and, as
1348 * "proto" is > ETHERMTU, this test
1349 * will fail and the frame won't match,
1350 * which is what we want).
1352 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1358 * Generate code to match a particular packet type.
1360 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1361 * value, if <= ETHERMTU. We use that to determine whether to
1362 * match the type field or to check the type field for the special
1363 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1365 static struct block
*
1366 gen_linux_sll_linktype(proto
)
1369 struct block
*b0
, *b1
;
1375 case LLCSAP_NETBEUI
:
1377 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1378 * so we check the DSAP and SSAP.
1380 * LLCSAP_IP checks for IP-over-802.2, rather
1381 * than IP-over-Ethernet or IP-over-SNAP.
1383 * XXX - should we check both the DSAP and the
1384 * SSAP, like this, or should we check just the
1385 * DSAP, as we do for other types <= ETHERMTU
1386 * (i.e., other SAP values)?
1388 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1389 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1390 ((proto
<< 8) | proto
));
1396 * Ethernet_II frames, which are Ethernet
1397 * frames with a frame type of ETHERTYPE_IPX;
1399 * Ethernet_802.3 frames, which have a frame
1400 * type of LINUX_SLL_P_802_3;
1402 * Ethernet_802.2 frames, which are 802.3
1403 * frames with an 802.2 LLC header (i.e, have
1404 * a frame type of LINUX_SLL_P_802_2) and
1405 * with the IPX LSAP as the DSAP in the LLC
1408 * Ethernet_SNAP frames, which are 802.3
1409 * frames with an LLC header and a SNAP
1410 * header and with an OUI of 0x000000
1411 * (encapsulated Ethernet) and a protocol
1412 * ID of ETHERTYPE_IPX in the SNAP header.
1414 * First, do the checks on LINUX_SLL_P_802_2
1415 * frames; generate the check for either
1416 * Ethernet_802.2 or Ethernet_SNAP frames, and
1417 * then put a check for LINUX_SLL_P_802_2 frames
1420 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1421 (bpf_int32
)LLCSAP_IPX
);
1422 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1425 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1429 * Now check for 802.3 frames and OR that with
1430 * the previous test.
1432 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1436 * Now add the check for Ethernet_II frames, and
1437 * do that before checking for the other frame
1440 b0
= gen_cmp(off_linktype
, BPF_H
,
1441 (bpf_int32
)ETHERTYPE_IPX
);
1445 case ETHERTYPE_ATALK
:
1446 case ETHERTYPE_AARP
:
1448 * EtherTalk (AppleTalk protocols on Ethernet link
1449 * layer) may use 802.2 encapsulation.
1453 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1454 * we check for the 802.2 protocol type in the
1455 * "Ethernet type" field.
1457 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1460 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1461 * SNAP packets with an organization code of
1462 * 0x080007 (Apple, for Appletalk) and a protocol
1463 * type of ETHERTYPE_ATALK (Appletalk).
1465 * 802.2-encapsulated ETHERTYPE_AARP packets are
1466 * SNAP packets with an organization code of
1467 * 0x000000 (encapsulated Ethernet) and a protocol
1468 * type of ETHERTYPE_AARP (Appletalk ARP).
1470 if (proto
== ETHERTYPE_ATALK
)
1471 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1473 else /* proto == ETHERTYPE_AARP */
1474 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1479 * Check for Ethernet encapsulation (Ethertalk
1480 * phase 1?); we just check for the Ethernet
1483 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1489 if (proto
<= ETHERMTU
) {
1491 * This is an LLC SAP value, so the frames
1492 * that match would be 802.2 frames.
1493 * Check for the 802.2 protocol type
1494 * in the "Ethernet type" field, and
1495 * then check the DSAP.
1497 b0
= gen_cmp(off_linktype
, BPF_H
,
1499 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1505 * This is an Ethernet type, so compare
1506 * the length/type field with it (if
1507 * the frame is an 802.2 frame, the length
1508 * field will be <= ETHERMTU, and, as
1509 * "proto" is > ETHERMTU, this test
1510 * will fail and the frame won't match,
1511 * which is what we want).
1513 return gen_cmp(off_linktype
, BPF_H
,
1520 * Generate code to match a particular packet type by matching the
1521 * link-layer type field or fields in the 802.2 LLC header.
1523 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1524 * value, if <= ETHERMTU.
1526 static struct block
*
1530 struct block
*b0
, *b1
, *b2
;
1535 return gen_ether_linktype(proto
);
1543 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1547 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1553 case DLT_IEEE802_11
:
1554 case DLT_PRISM_HEADER
:
1555 case DLT_IEEE802_11_RADIO
:
1558 case DLT_ATM_RFC1483
:
1560 case DLT_IP_OVER_FC
:
1561 return gen_llc_linktype(proto
);
1567 * If "is_lane" is set, check for a LANE-encapsulated
1568 * version of this protocol, otherwise check for an
1569 * LLC-encapsulated version of this protocol.
1571 * We assume LANE means Ethernet, not Token Ring.
1575 * Check that the packet doesn't begin with an
1576 * LE Control marker. (We've already generated
1579 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1583 * Now generate an Ethernet test.
1585 b1
= gen_ether_linktype(proto
);
1590 * Check for LLC encapsulation and then check the
1593 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1594 b1
= gen_llc_linktype(proto
);
1602 return gen_linux_sll_linktype(proto
);
1607 case DLT_SLIP_BSDOS
:
1610 * These types don't provide any type field; packets
1613 * XXX - for IPv4, check for a version number of 4, and,
1614 * for IPv6, check for a version number of 6?
1620 case ETHERTYPE_IPV6
:
1622 return gen_true(); /* always true */
1625 return gen_false(); /* always false */
1632 case DLT_PPP_SERIAL
:
1635 * We use Ethernet protocol types inside libpcap;
1636 * map them to the corresponding PPP protocol types.
1645 case ETHERTYPE_IPV6
:
1654 case ETHERTYPE_ATALK
:
1668 * I'm assuming the "Bridging PDU"s that go
1669 * over PPP are Spanning Tree Protocol
1683 * We use Ethernet protocol types inside libpcap;
1684 * map them to the corresponding PPP protocol types.
1689 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1690 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1692 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1697 case ETHERTYPE_IPV6
:
1707 case ETHERTYPE_ATALK
:
1721 * I'm assuming the "Bridging PDU"s that go
1722 * over PPP are Spanning Tree Protocol
1738 * For DLT_NULL, the link-layer header is a 32-bit
1739 * word containing an AF_ value in *host* byte order,
1740 * and for DLT_ENC, the link-layer header begins
1741 * with a 32-bit work containing an AF_ value in
1744 * In addition, if we're reading a saved capture file,
1745 * the host byte order in the capture may not be the
1746 * same as the host byte order on this machine.
1748 * For DLT_LOOP, the link-layer header is a 32-bit
1749 * word containing an AF_ value in *network* byte order.
1751 * XXX - AF_ values may, unfortunately, be platform-
1752 * dependent; for example, FreeBSD's AF_INET6 is 24
1753 * whilst NetBSD's and OpenBSD's is 26.
1755 * This means that, when reading a capture file, just
1756 * checking for our AF_INET6 value won't work if the
1757 * capture file came from another OS.
1766 case ETHERTYPE_IPV6
:
1773 * Not a type on which we support filtering.
1774 * XXX - support those that have AF_ values
1775 * #defined on this platform, at least?
1780 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1782 * The AF_ value is in host byte order, but
1783 * the BPF interpreter will convert it to
1784 * network byte order.
1786 * If this is a save file, and it's from a
1787 * machine with the opposite byte order to
1788 * ours, we byte-swap the AF_ value.
1790 * Then we run it through "htonl()", and
1791 * generate code to compare against the result.
1793 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1794 bpf_pcap
->sf
.swapped
)
1795 proto
= SWAPLONG(proto
);
1796 proto
= htonl(proto
);
1798 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1802 * af field is host byte order in contrast to the rest of
1805 if (proto
== ETHERTYPE_IP
)
1806 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1807 (bpf_int32
)AF_INET
));
1809 else if (proto
== ETHERTYPE_IPV6
)
1810 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1811 (bpf_int32
)AF_INET6
));
1819 case DLT_ARCNET_LINUX
:
1821 * XXX should we check for first fragment if the protocol
1830 case ETHERTYPE_IPV6
:
1831 return (gen_cmp(off_linktype
, BPF_B
,
1832 (bpf_int32
)ARCTYPE_INET6
));
1836 b0
= gen_cmp(off_linktype
, BPF_B
,
1837 (bpf_int32
)ARCTYPE_IP
);
1838 b1
= gen_cmp(off_linktype
, BPF_B
,
1839 (bpf_int32
)ARCTYPE_IP_OLD
);
1844 b0
= gen_cmp(off_linktype
, BPF_B
,
1845 (bpf_int32
)ARCTYPE_ARP
);
1846 b1
= gen_cmp(off_linktype
, BPF_B
,
1847 (bpf_int32
)ARCTYPE_ARP_OLD
);
1851 case ETHERTYPE_REVARP
:
1852 return (gen_cmp(off_linktype
, BPF_B
,
1853 (bpf_int32
)ARCTYPE_REVARP
));
1855 case ETHERTYPE_ATALK
:
1856 return (gen_cmp(off_linktype
, BPF_B
,
1857 (bpf_int32
)ARCTYPE_ATALK
));
1864 case ETHERTYPE_ATALK
:
1874 * XXX - assumes a 2-byte Frame Relay header with
1875 * DLCI and flags. What if the address is longer?
1881 * Check for the special NLPID for IP.
1883 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1886 case ETHERTYPE_IPV6
:
1888 * Check for the special NLPID for IPv6.
1890 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1895 * Check for several OSI protocols.
1897 * Frame Relay packets typically have an OSI
1898 * NLPID at the beginning; we check for each
1901 * What we check for is the NLPID and a frame
1902 * control field of UI, i.e. 0x03 followed
1905 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1906 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1907 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1918 case DLT_JUNIPER_MLFR
:
1919 case DLT_JUNIPER_MLPPP
:
1920 case DLT_JUNIPER_ATM1
:
1921 case DLT_JUNIPER_ATM2
:
1922 /* just lets verify the magic number for now -
1923 * on ATM we may have up to 6 different encapsulations on the wire
1924 * and need a lot of heuristics to figure out that the payload
1927 * FIXME encapsulation specific BPF_ filters
1929 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1931 case DLT_LINUX_IRDA
:
1932 bpf_error("IrDA link-layer type filtering not implemented");
1935 bpf_error("DOCSIS link-layer type filtering not implemented");
1937 case DLT_LINUX_LAPD
:
1938 bpf_error("LAPD link-layer type filtering not implemented");
1942 * All the types that have no encapsulation should either be
1943 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1944 * all packets are IP packets, or should be handled in some
1945 * special case, if none of them are (if some are and some
1946 * aren't, the lack of encapsulation is a problem, as we'd
1947 * have to find some other way of determining the packet type).
1949 * Therefore, if "off_linktype" is -1, there's an error.
1951 if (off_linktype
== (u_int
)-1)
1955 * Any type not handled above should always have an Ethernet
1956 * type at an offset of "off_linktype". (PPP is partially
1957 * handled above - the protocol type is mapped from the
1958 * Ethernet and LLC types we use internally to the corresponding
1959 * PPP type - but the PPP type is always specified by a value
1960 * at "off_linktype", so we don't have to do the code generation
1963 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1967 * Check for an LLC SNAP packet with a given organization code and
1968 * protocol type; we check the entire contents of the 802.2 LLC and
1969 * snap headers, checking for DSAP and SSAP of SNAP and a control
1970 * field of 0x03 in the LLC header, and for the specified organization
1971 * code and protocol type in the SNAP header.
1973 static struct block
*
1974 gen_snap(orgcode
, ptype
, offset
)
1975 bpf_u_int32 orgcode
;
1979 u_char snapblock
[8];
1981 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1982 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1983 snapblock
[2] = 0x03; /* control = UI */
1984 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1985 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1986 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1987 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1988 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1989 return gen_bcmp(offset
, 8, snapblock
);
1993 * Generate code to match a particular packet type, for link-layer types
1994 * using 802.2 LLC headers.
1996 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1997 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1999 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2000 * value, if <= ETHERMTU. We use that to determine whether to
2001 * match the DSAP or both DSAP and LSAP or to check the OUI and
2002 * protocol ID in a SNAP header.
2004 static struct block
*
2005 gen_llc_linktype(proto
)
2009 * XXX - handle token-ring variable-length header.
2015 case LLCSAP_NETBEUI
:
2017 * XXX - should we check both the DSAP and the
2018 * SSAP, like this, or should we check just the
2019 * DSAP, as we do for other types <= ETHERMTU
2020 * (i.e., other SAP values)?
2022 return gen_cmp(off_linktype
, BPF_H
, (long)
2023 ((proto
<< 8) | proto
));
2027 * XXX - are there ever SNAP frames for IPX on
2028 * non-Ethernet 802.x networks?
2030 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2032 case ETHERTYPE_ATALK
:
2034 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2035 * SNAP packets with an organization code of
2036 * 0x080007 (Apple, for Appletalk) and a protocol
2037 * type of ETHERTYPE_ATALK (Appletalk).
2039 * XXX - check for an organization code of
2040 * encapsulated Ethernet as well?
2042 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2046 * XXX - we don't have to check for IPX 802.3
2047 * here, but should we check for the IPX Ethertype?
2049 if (proto
<= ETHERMTU
) {
2051 * This is an LLC SAP value, so check
2054 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
2057 * This is an Ethernet type; we assume that it's
2058 * unlikely that it'll appear in the right place
2059 * at random, and therefore check only the
2060 * location that would hold the Ethernet type
2061 * in a SNAP frame with an organization code of
2062 * 0x000000 (encapsulated Ethernet).
2064 * XXX - if we were to check for the SNAP DSAP and
2065 * LSAP, as per XXX, and were also to check for an
2066 * organization code of 0x000000 (encapsulated
2067 * Ethernet), we'd do
2069 * return gen_snap(0x000000, proto,
2072 * here; for now, we don't, as per the above.
2073 * I don't know whether it's worth the extra CPU
2074 * time to do the right check or not.
2076 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2081 static struct block
*
2082 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2086 u_int src_off
, dst_off
;
2088 struct block
*b0
, *b1
;
2102 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2103 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2109 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2110 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2117 b0
= gen_linktype(proto
);
2118 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2124 static struct block
*
2125 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2126 struct in6_addr
*addr
;
2127 struct in6_addr
*mask
;
2129 u_int src_off
, dst_off
;
2131 struct block
*b0
, *b1
;
2146 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2147 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2153 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2154 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2161 /* this order is important */
2162 a
= (u_int32_t
*)addr
;
2163 m
= (u_int32_t
*)mask
;
2164 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2165 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2167 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2169 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2171 b0
= gen_linktype(proto
);
2177 static struct block
*
2178 gen_ehostop(eaddr
, dir
)
2179 register const u_char
*eaddr
;
2182 register struct block
*b0
, *b1
;
2186 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2189 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2192 b0
= gen_ehostop(eaddr
, Q_SRC
);
2193 b1
= gen_ehostop(eaddr
, Q_DST
);
2199 b0
= gen_ehostop(eaddr
, Q_SRC
);
2200 b1
= gen_ehostop(eaddr
, Q_DST
);
2209 * Like gen_ehostop, but for DLT_FDDI
2211 static struct block
*
2212 gen_fhostop(eaddr
, dir
)
2213 register const u_char
*eaddr
;
2216 struct block
*b0
, *b1
;
2221 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2223 return gen_bcmp(6 + 1, 6, eaddr
);
2228 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2230 return gen_bcmp(0 + 1, 6, eaddr
);
2234 b0
= gen_fhostop(eaddr
, Q_SRC
);
2235 b1
= gen_fhostop(eaddr
, Q_DST
);
2241 b0
= gen_fhostop(eaddr
, Q_SRC
);
2242 b1
= gen_fhostop(eaddr
, Q_DST
);
2251 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2253 static struct block
*
2254 gen_thostop(eaddr
, dir
)
2255 register const u_char
*eaddr
;
2258 register struct block
*b0
, *b1
;
2262 return gen_bcmp(8, 6, eaddr
);
2265 return gen_bcmp(2, 6, eaddr
);
2268 b0
= gen_thostop(eaddr
, Q_SRC
);
2269 b1
= gen_thostop(eaddr
, Q_DST
);
2275 b0
= gen_thostop(eaddr
, Q_SRC
);
2276 b1
= gen_thostop(eaddr
, Q_DST
);
2285 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2287 static struct block
*
2288 gen_wlanhostop(eaddr
, dir
)
2289 register const u_char
*eaddr
;
2292 register struct block
*b0
, *b1
, *b2
;
2293 register struct slist
*s
;
2300 * For control frames, there is no SA.
2302 * For management frames, SA is at an
2303 * offset of 10 from the beginning of
2306 * For data frames, SA is at an offset
2307 * of 10 from the beginning of the packet
2308 * if From DS is clear, at an offset of
2309 * 16 from the beginning of the packet
2310 * if From DS is set and To DS is clear,
2311 * and an offset of 24 from the beginning
2312 * of the packet if From DS is set and To DS
2317 * Generate the tests to be done for data frames
2320 * First, check for To DS set, i.e. check "link[1] & 0x01".
2322 s
= gen_load_llrel(1, BPF_B
);
2323 b1
= new_block(JMP(BPF_JSET
));
2324 b1
->s
.k
= 0x01; /* To DS */
2328 * If To DS is set, the SA is at 24.
2330 b0
= gen_bcmp(24, 6, eaddr
);
2334 * Now, check for To DS not set, i.e. check
2335 * "!(link[1] & 0x01)".
2337 s
= gen_load_llrel(1, BPF_B
);
2338 b2
= new_block(JMP(BPF_JSET
));
2339 b2
->s
.k
= 0x01; /* To DS */
2344 * If To DS is not set, the SA is at 16.
2346 b1
= gen_bcmp(16, 6, eaddr
);
2350 * Now OR together the last two checks. That gives
2351 * the complete set of checks for data frames with
2357 * Now check for From DS being set, and AND that with
2358 * the ORed-together checks.
2360 s
= gen_load_llrel(1, BPF_B
);
2361 b1
= new_block(JMP(BPF_JSET
));
2362 b1
->s
.k
= 0x02; /* From DS */
2367 * Now check for data frames with From DS not set.
2369 s
= gen_load_llrel(1, BPF_B
);
2370 b2
= new_block(JMP(BPF_JSET
));
2371 b2
->s
.k
= 0x02; /* From DS */
2376 * If From DS isn't set, the SA is at 10.
2378 b1
= gen_bcmp(10, 6, eaddr
);
2382 * Now OR together the checks for data frames with
2383 * From DS not set and for data frames with From DS
2384 * set; that gives the checks done for data frames.
2389 * Now check for a data frame.
2390 * I.e, check "link[0] & 0x08".
2392 gen_load_llrel(0, BPF_B
);
2393 b1
= new_block(JMP(BPF_JSET
));
2398 * AND that with the checks done for data frames.
2403 * If the high-order bit of the type value is 0, this
2404 * is a management frame.
2405 * I.e, check "!(link[0] & 0x08)".
2407 s
= gen_load_llrel(0, BPF_B
);
2408 b2
= new_block(JMP(BPF_JSET
));
2414 * For management frames, the SA is at 10.
2416 b1
= gen_bcmp(10, 6, eaddr
);
2420 * OR that with the checks done for data frames.
2421 * That gives the checks done for management and
2427 * If the low-order bit of the type value is 1,
2428 * this is either a control frame or a frame
2429 * with a reserved type, and thus not a
2432 * I.e., check "!(link[0] & 0x04)".
2434 s
= gen_load_llrel(0, BPF_B
);
2435 b1
= new_block(JMP(BPF_JSET
));
2441 * AND that with the checks for data and management
2451 * For control frames, there is no DA.
2453 * For management frames, DA is at an
2454 * offset of 4 from the beginning of
2457 * For data frames, DA is at an offset
2458 * of 4 from the beginning of the packet
2459 * if To DS is clear and at an offset of
2460 * 16 from the beginning of the packet
2465 * Generate the tests to be done for data frames.
2467 * First, check for To DS set, i.e. "link[1] & 0x01".
2469 s
= gen_load_llrel(1, BPF_B
);
2470 b1
= new_block(JMP(BPF_JSET
));
2471 b1
->s
.k
= 0x01; /* To DS */
2475 * If To DS is set, the DA is at 16.
2477 b0
= gen_bcmp(16, 6, eaddr
);
2481 * Now, check for To DS not set, i.e. check
2482 * "!(link[1] & 0x01)".
2484 s
= gen_load_llrel(1, BPF_B
);
2485 b2
= new_block(JMP(BPF_JSET
));
2486 b2
->s
.k
= 0x01; /* To DS */
2491 * If To DS is not set, the DA is at 4.
2493 b1
= gen_bcmp(4, 6, eaddr
);
2497 * Now OR together the last two checks. That gives
2498 * the complete set of checks for data frames.
2503 * Now check for a data frame.
2504 * I.e, check "link[0] & 0x08".
2506 s
= gen_load_llrel(0, BPF_B
);
2507 b1
= new_block(JMP(BPF_JSET
));
2512 * AND that with the checks done for data frames.
2517 * If the high-order bit of the type value is 0, this
2518 * is a management frame.
2519 * I.e, check "!(link[0] & 0x08)".
2521 s
= gen_load_llrel(0, BPF_B
);
2522 b2
= new_block(JMP(BPF_JSET
));
2528 * For management frames, the DA is at 4.
2530 b1
= gen_bcmp(4, 6, eaddr
);
2534 * OR that with the checks done for data frames.
2535 * That gives the checks done for management and
2541 * If the low-order bit of the type value is 1,
2542 * this is either a control frame or a frame
2543 * with a reserved type, and thus not a
2546 * I.e., check "!(link[0] & 0x04)".
2548 s
= gen_load_llrel(0, BPF_B
);
2549 b1
= new_block(JMP(BPF_JSET
));
2555 * AND that with the checks for data and management
2562 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2563 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2569 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2570 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2579 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2580 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2581 * as the RFC states.)
2583 static struct block
*
2584 gen_ipfchostop(eaddr
, dir
)
2585 register const u_char
*eaddr
;
2588 register struct block
*b0
, *b1
;
2592 return gen_bcmp(10, 6, eaddr
);
2595 return gen_bcmp(2, 6, eaddr
);
2598 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2599 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2605 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2606 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2615 * This is quite tricky because there may be pad bytes in front of the
2616 * DECNET header, and then there are two possible data packet formats that
2617 * carry both src and dst addresses, plus 5 packet types in a format that
2618 * carries only the src node, plus 2 types that use a different format and
2619 * also carry just the src node.
2623 * Instead of doing those all right, we just look for data packets with
2624 * 0 or 1 bytes of padding. If you want to look at other packets, that
2625 * will require a lot more hacking.
2627 * To add support for filtering on DECNET "areas" (network numbers)
2628 * one would want to add a "mask" argument to this routine. That would
2629 * make the filter even more inefficient, although one could be clever
2630 * and not generate masking instructions if the mask is 0xFFFF.
2632 static struct block
*
2633 gen_dnhostop(addr
, dir
, base_off
)
2638 struct block
*b0
, *b1
, *b2
, *tmp
;
2639 u_int offset_lh
; /* offset if long header is received */
2640 u_int offset_sh
; /* offset if short header is received */
2645 offset_sh
= 1; /* follows flags */
2646 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2650 offset_sh
= 3; /* follows flags, dstnode */
2651 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2655 /* Inefficient because we do our Calvinball dance twice */
2656 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2657 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2663 /* Inefficient because we do our Calvinball dance twice */
2664 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2665 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2670 bpf_error("ISO host filtering not implemented");
2675 b0
= gen_linktype(ETHERTYPE_DN
);
2676 /* Check for pad = 1, long header case */
2677 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2678 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2679 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2680 BPF_H
, (bpf_int32
)ntohs(addr
));
2682 /* Check for pad = 0, long header case */
2683 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2684 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2687 /* Check for pad = 1, short header case */
2688 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2689 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2690 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2691 BPF_H
, (bpf_int32
)ntohs(addr
));
2694 /* Check for pad = 0, short header case */
2695 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2696 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2700 /* Combine with test for linktype */
2705 static struct block
*
2706 gen_host(addr
, mask
, proto
, dir
)
2712 struct block
*b0
, *b1
;
2717 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2718 if (off_linktype
!= (u_int
)-1) {
2719 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2721 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2727 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2728 off_nl
+ 12, off_nl
+ 16);
2731 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2732 off_nl
+ 14, off_nl
+ 24);
2735 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2736 off_nl
+ 14, off_nl
+ 24);
2739 bpf_error("'tcp' modifier applied to host");
2742 bpf_error("'sctp' modifier applied to host");
2745 bpf_error("'udp' modifier applied to host");
2748 bpf_error("'icmp' modifier applied to host");
2751 bpf_error("'igmp' modifier applied to host");
2754 bpf_error("'igrp' modifier applied to host");
2757 bpf_error("'pim' modifier applied to host");
2760 bpf_error("'vrrp' modifier applied to host");
2763 bpf_error("ATALK host filtering not implemented");
2766 bpf_error("AARP host filtering not implemented");
2769 return gen_dnhostop(addr
, dir
, off_nl
);
2772 bpf_error("SCA host filtering not implemented");
2775 bpf_error("LAT host filtering not implemented");
2778 bpf_error("MOPDL host filtering not implemented");
2781 bpf_error("MOPRC host filtering not implemented");
2785 bpf_error("'ip6' modifier applied to ip host");
2788 bpf_error("'icmp6' modifier applied to host");
2792 bpf_error("'ah' modifier applied to host");
2795 bpf_error("'esp' modifier applied to host");
2798 bpf_error("ISO host filtering not implemented");
2801 bpf_error("'esis' modifier applied to host");
2804 bpf_error("'isis' modifier applied to host");
2807 bpf_error("'clnp' modifier applied to host");
2810 bpf_error("'stp' modifier applied to host");
2813 bpf_error("IPX host filtering not implemented");
2816 bpf_error("'netbeui' modifier applied to host");
2825 static struct block
*
2826 gen_host6(addr
, mask
, proto
, dir
)
2827 struct in6_addr
*addr
;
2828 struct in6_addr
*mask
;
2835 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2838 bpf_error("'ip' modifier applied to ip6 host");
2841 bpf_error("'rarp' modifier applied to ip6 host");
2844 bpf_error("'arp' modifier applied to ip6 host");
2847 bpf_error("'sctp' modifier applied to host");
2850 bpf_error("'tcp' modifier applied to host");
2853 bpf_error("'udp' modifier applied to host");
2856 bpf_error("'icmp' modifier applied to host");
2859 bpf_error("'igmp' modifier applied to host");
2862 bpf_error("'igrp' modifier applied to host");
2865 bpf_error("'pim' modifier applied to host");
2868 bpf_error("'vrrp' modifier applied to host");
2871 bpf_error("ATALK host filtering not implemented");
2874 bpf_error("AARP host filtering not implemented");
2877 bpf_error("'decnet' modifier applied to ip6 host");
2880 bpf_error("SCA host filtering not implemented");
2883 bpf_error("LAT host filtering not implemented");
2886 bpf_error("MOPDL host filtering not implemented");
2889 bpf_error("MOPRC host filtering not implemented");
2892 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2893 off_nl
+ 8, off_nl
+ 24);
2896 bpf_error("'icmp6' modifier applied to host");
2899 bpf_error("'ah' modifier applied to host");
2902 bpf_error("'esp' modifier applied to host");
2905 bpf_error("ISO host filtering not implemented");
2908 bpf_error("'esis' modifier applied to host");
2911 bpf_error("'isis' modifier applied to host");
2914 bpf_error("'clnp' modifier applied to host");
2917 bpf_error("'stp' modifier applied to host");
2920 bpf_error("IPX host filtering not implemented");
2923 bpf_error("'netbeui' modifier applied to host");
2933 static struct block
*
2934 gen_gateway(eaddr
, alist
, proto
, dir
)
2935 const u_char
*eaddr
;
2936 bpf_u_int32
**alist
;
2940 struct block
*b0
, *b1
, *tmp
;
2943 bpf_error("direction applied to 'gateway'");
2950 if (linktype
== DLT_EN10MB
)
2951 b0
= gen_ehostop(eaddr
, Q_OR
);
2952 else if (linktype
== DLT_FDDI
)
2953 b0
= gen_fhostop(eaddr
, Q_OR
);
2954 else if (linktype
== DLT_IEEE802
)
2955 b0
= gen_thostop(eaddr
, Q_OR
);
2956 else if (linktype
== DLT_IEEE802_11
)
2957 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2958 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2960 * Check that the packet doesn't begin with an
2961 * LE Control marker. (We've already generated
2964 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2968 * Now check the MAC address.
2970 b0
= gen_ehostop(eaddr
, Q_OR
);
2972 } else if (linktype
== DLT_IP_OVER_FC
)
2973 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2976 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2978 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2980 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2988 bpf_error("illegal modifier of 'gateway'");
2994 gen_proto_abbrev(proto
)
3003 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
3005 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
3011 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
3013 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
3019 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3021 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3027 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3030 #ifndef IPPROTO_IGMP
3031 #define IPPROTO_IGMP 2
3035 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3038 #ifndef IPPROTO_IGRP
3039 #define IPPROTO_IGRP 9
3042 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3046 #define IPPROTO_PIM 103
3050 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3052 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3057 #ifndef IPPROTO_VRRP
3058 #define IPPROTO_VRRP 112
3062 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3066 b1
= gen_linktype(ETHERTYPE_IP
);
3070 b1
= gen_linktype(ETHERTYPE_ARP
);
3074 b1
= gen_linktype(ETHERTYPE_REVARP
);
3078 bpf_error("link layer applied in wrong context");
3081 b1
= gen_linktype(ETHERTYPE_ATALK
);
3085 b1
= gen_linktype(ETHERTYPE_AARP
);
3089 b1
= gen_linktype(ETHERTYPE_DN
);
3093 b1
= gen_linktype(ETHERTYPE_SCA
);
3097 b1
= gen_linktype(ETHERTYPE_LAT
);
3101 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3105 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3110 b1
= gen_linktype(ETHERTYPE_IPV6
);
3113 #ifndef IPPROTO_ICMPV6
3114 #define IPPROTO_ICMPV6 58
3117 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3122 #define IPPROTO_AH 51
3125 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3127 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3133 #define IPPROTO_ESP 50
3136 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3138 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3144 b1
= gen_linktype(LLCSAP_ISONS
);
3148 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3152 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3155 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3156 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3157 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3159 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3161 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3163 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3167 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3168 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3169 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3171 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3173 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3175 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3179 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3180 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3181 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3183 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3188 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3189 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3194 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3195 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3197 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3199 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3204 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3205 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3210 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3211 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3216 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3220 b1
= gen_linktype(LLCSAP_8021D
);
3224 b1
= gen_linktype(LLCSAP_IPX
);
3228 b1
= gen_linktype(LLCSAP_NETBEUI
);
3237 static struct block
*
3244 s
= gen_load_nlrel(6, BPF_H
);
3245 b
= new_block(JMP(BPF_JSET
));
3253 static struct block
*
3254 gen_portatom(off
, v
)
3261 s
= gen_load_ipv4tlrel(off
, BPF_H
);
3263 b
= new_block(JMP(BPF_JEQ
));
3271 static struct block
*
3272 gen_portatom6(off
, v
)
3276 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3281 gen_portop(port
, proto
, dir
)
3282 int port
, proto
, dir
;
3284 struct block
*b0
, *b1
, *tmp
;
3286 /* ip proto 'proto' */
3287 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3293 b1
= gen_portatom(0, (bpf_int32
)port
);
3297 b1
= gen_portatom(2, (bpf_int32
)port
);
3302 tmp
= gen_portatom(0, (bpf_int32
)port
);
3303 b1
= gen_portatom(2, (bpf_int32
)port
);
3308 tmp
= gen_portatom(0, (bpf_int32
)port
);
3309 b1
= gen_portatom(2, (bpf_int32
)port
);
3321 static struct block
*
3322 gen_port(port
, ip_proto
, dir
)
3327 struct block
*b0
, *b1
, *tmp
;
3332 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3333 * not LLC encapsulation with LLCSAP_IP.
3335 * For IEEE 802 networks - which includes 802.5 token ring
3336 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3337 * says that SNAP encapsulation is used, not LLC encapsulation
3340 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3341 * RFC 2225 say that SNAP encapsulation is used, not LLC
3342 * encapsulation with LLCSAP_IP.
3344 * So we always check for ETHERTYPE_IP.
3346 b0
= gen_linktype(ETHERTYPE_IP
);
3352 b1
= gen_portop(port
, ip_proto
, dir
);
3356 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3357 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3359 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3372 gen_portop6(port
, proto
, dir
)
3373 int port
, proto
, dir
;
3375 struct block
*b0
, *b1
, *tmp
;
3377 /* ip6 proto 'proto' */
3378 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3382 b1
= gen_portatom6(0, (bpf_int32
)port
);
3386 b1
= gen_portatom6(2, (bpf_int32
)port
);
3391 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3392 b1
= gen_portatom6(2, (bpf_int32
)port
);
3397 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3398 b1
= gen_portatom6(2, (bpf_int32
)port
);
3410 static struct block
*
3411 gen_port6(port
, ip_proto
, dir
)
3416 struct block
*b0
, *b1
, *tmp
;
3418 /* link proto ip6 */
3419 b0
= gen_linktype(ETHERTYPE_IPV6
);
3425 b1
= gen_portop6(port
, ip_proto
, dir
);
3429 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3430 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3432 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3444 /* gen_portrange code */
3445 static struct block
*
3446 gen_portrangeatom(off
, v1
, v2
)
3450 struct slist
*s1
, *s2
;
3451 struct block
*b1
, *b2
;
3455 * Reverse the order of the ports, so v1 is the lower one.
3464 s1
= gen_load_ipv4tlrel(off
, BPF_H
);
3466 b1
= new_block(JMP(BPF_JGE
));
3470 s2
= gen_load_ipv4tlrel(off
, BPF_H
);
3472 b2
= new_block(JMP(BPF_JGT
));
3483 gen_portrangeop(port1
, port2
, proto
, dir
)
3488 struct block
*b0
, *b1
, *tmp
;
3490 /* ip proto 'proto' */
3491 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3497 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3501 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3506 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3507 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3512 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3513 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3525 static struct block
*
3526 gen_portrange(port1
, port2
, ip_proto
, dir
)
3531 struct block
*b0
, *b1
, *tmp
;
3534 b0
= gen_linktype(ETHERTYPE_IP
);
3540 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3544 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3545 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3547 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3559 static struct block
*
3560 gen_portrangeatom6(off
, v1
, v2
)
3564 struct slist
*s1
, *s2
;
3565 struct block
*b1
, *b2
;
3569 * Reverse the order of the ports, so v1 is the lower one.
3578 s1
= gen_load_ipv6tlrel(off
, BPF_H
);
3580 b1
= new_block(JMP(BPF_JGE
));
3584 s2
= gen_load_ipv6tlrel(off
, BPF_H
);
3586 b2
= new_block(JMP(BPF_JGT
));
3597 gen_portrangeop6(port1
, port2
, proto
, dir
)
3602 struct block
*b0
, *b1
, *tmp
;
3604 /* ip6 proto 'proto' */
3605 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3609 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3613 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3618 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3619 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3624 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3625 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3637 static struct block
*
3638 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3643 struct block
*b0
, *b1
, *tmp
;
3645 /* link proto ip6 */
3646 b0
= gen_linktype(ETHERTYPE_IPV6
);
3652 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3656 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3657 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3659 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3672 lookup_proto(name
, proto
)
3673 register const char *name
;
3683 v
= pcap_nametoproto(name
);
3684 if (v
== PROTO_UNDEF
)
3685 bpf_error("unknown ip proto '%s'", name
);
3689 /* XXX should look up h/w protocol type based on linktype */
3690 v
= pcap_nametoeproto(name
);
3691 if (v
== PROTO_UNDEF
) {
3692 v
= pcap_nametollc(name
);
3693 if (v
== PROTO_UNDEF
)
3694 bpf_error("unknown ether proto '%s'", name
);
3699 if (strcmp(name
, "esis") == 0)
3701 else if (strcmp(name
, "isis") == 0)
3703 else if (strcmp(name
, "clnp") == 0)
3706 bpf_error("unknown osi proto '%s'", name
);
3726 static struct block
*
3727 gen_protochain(v
, proto
, dir
)
3732 #ifdef NO_PROTOCHAIN
3733 return gen_proto(v
, proto
, dir
);
3735 struct block
*b0
, *b
;
3736 struct slist
*s
[100];
3737 int fix2
, fix3
, fix4
, fix5
;
3738 int ahcheck
, again
, end
;
3740 int reg2
= alloc_reg();
3742 memset(s
, 0, sizeof(s
));
3743 fix2
= fix3
= fix4
= fix5
= 0;
3750 b0
= gen_protochain(v
, Q_IP
, dir
);
3751 b
= gen_protochain(v
, Q_IPV6
, dir
);
3755 bpf_error("bad protocol applied for 'protochain'");
3759 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3762 * s[0] is a dummy entry to protect other BPF insn from damage
3763 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3764 * hard to find interdependency made by jump table fixup.
3767 s
[i
] = new_stmt(0); /*dummy*/
3772 b0
= gen_linktype(ETHERTYPE_IP
);
3775 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3776 s
[i
]->s
.k
= off_nl
+ 9;
3778 /* X = ip->ip_hl << 2 */
3779 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3785 b0
= gen_linktype(ETHERTYPE_IPV6
);
3787 /* A = ip6->ip_nxt */
3788 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3789 s
[i
]->s
.k
= off_nl
+ 6;
3791 /* X = sizeof(struct ip6_hdr) */
3792 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3798 bpf_error("unsupported proto to gen_protochain");
3802 /* again: if (A == v) goto end; else fall through; */
3804 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3806 s
[i
]->s
.jt
= NULL
; /*later*/
3807 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3811 #ifndef IPPROTO_NONE
3812 #define IPPROTO_NONE 59
3814 /* if (A == IPPROTO_NONE) goto end */
3815 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3816 s
[i
]->s
.jt
= NULL
; /*later*/
3817 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3818 s
[i
]->s
.k
= IPPROTO_NONE
;
3819 s
[fix5
]->s
.jf
= s
[i
];
3824 if (proto
== Q_IPV6
) {
3825 int v6start
, v6end
, v6advance
, j
;
3828 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3829 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3830 s
[i
]->s
.jt
= NULL
; /*later*/
3831 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3832 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3833 s
[fix2
]->s
.jf
= s
[i
];
3835 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3836 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3837 s
[i
]->s
.jt
= NULL
; /*later*/
3838 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3839 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3841 /* if (A == IPPROTO_ROUTING) goto v6advance */
3842 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3843 s
[i
]->s
.jt
= NULL
; /*later*/
3844 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3845 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3847 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3848 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3849 s
[i
]->s
.jt
= NULL
; /*later*/
3850 s
[i
]->s
.jf
= NULL
; /*later*/
3851 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3862 * X = X + (P[X + 1] + 1) * 8;
3865 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3867 /* A = P[X + packet head] */
3868 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3872 s
[i
] = new_stmt(BPF_ST
);
3876 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3879 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3883 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3885 /* A = P[X + packet head]; */
3886 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3890 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3894 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3898 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3901 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3905 /* goto again; (must use BPF_JA for backward jump) */
3906 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3907 s
[i
]->s
.k
= again
- i
- 1;
3908 s
[i
- 1]->s
.jf
= s
[i
];
3912 for (j
= v6start
; j
<= v6end
; j
++)
3913 s
[j
]->s
.jt
= s
[v6advance
];
3918 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3920 s
[fix2
]->s
.jf
= s
[i
];
3926 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3927 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3928 s
[i
]->s
.jt
= NULL
; /*later*/
3929 s
[i
]->s
.jf
= NULL
; /*later*/
3930 s
[i
]->s
.k
= IPPROTO_AH
;
3932 s
[fix3
]->s
.jf
= s
[ahcheck
];
3939 * X = X + (P[X + 1] + 2) * 4;
3942 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3944 /* A = P[X + packet head]; */
3945 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3949 s
[i
] = new_stmt(BPF_ST
);
3953 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3956 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3960 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3962 /* A = P[X + packet head] */
3963 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3967 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3971 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3975 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3978 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3982 /* goto again; (must use BPF_JA for backward jump) */
3983 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3984 s
[i
]->s
.k
= again
- i
- 1;
3989 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3991 s
[fix2
]->s
.jt
= s
[end
];
3992 s
[fix4
]->s
.jf
= s
[end
];
3993 s
[fix5
]->s
.jt
= s
[end
];
4000 for (i
= 0; i
< max
- 1; i
++)
4001 s
[i
]->next
= s
[i
+ 1];
4002 s
[max
- 1]->next
= NULL
;
4007 b
= new_block(JMP(BPF_JEQ
));
4008 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
4019 * Generate code that checks whether the packet is a packet for protocol
4020 * <proto> and whether the type field in that protocol's header has
4021 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4022 * IP packet and checks the protocol number in the IP header against <v>.
4024 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4025 * against Q_IP and Q_IPV6.
4027 static struct block
*
4028 gen_proto(v
, proto
, dir
)
4033 struct block
*b0
, *b1
;
4035 if (dir
!= Q_DEFAULT
)
4036 bpf_error("direction applied to 'proto'");
4041 b0
= gen_proto(v
, Q_IP
, dir
);
4042 b1
= gen_proto(v
, Q_IPV6
, dir
);
4050 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4051 * not LLC encapsulation with LLCSAP_IP.
4053 * For IEEE 802 networks - which includes 802.5 token ring
4054 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4055 * says that SNAP encapsulation is used, not LLC encapsulation
4058 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4059 * RFC 2225 say that SNAP encapsulation is used, not LLC
4060 * encapsulation with LLCSAP_IP.
4062 * So we always check for ETHERTYPE_IP.
4064 b0
= gen_linktype(ETHERTYPE_IP
);
4066 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
4068 b1
= gen_protochain(v
, Q_IP
);
4078 * Frame Relay packets typically have an OSI
4079 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4080 * generates code to check for all the OSI
4081 * NLPIDs, so calling it and then adding a check
4082 * for the particular NLPID for which we're
4083 * looking is bogus, as we can just check for
4086 * What we check for is the NLPID and a frame
4087 * control field value of UI, i.e. 0x03 followed
4090 * XXX - assumes a 2-byte Frame Relay header with
4091 * DLCI and flags. What if the address is longer?
4093 * XXX - what about SNAP-encapsulated frames?
4095 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
4101 * Cisco uses an Ethertype lookalike - for OSI,
4104 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4105 /* OSI in C-HDLC is stuffed with a fudge byte */
4106 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
4111 b0
= gen_linktype(LLCSAP_ISONS
);
4112 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
4118 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4120 * 4 is the offset of the PDU type relative to the IS-IS
4123 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
4128 bpf_error("arp does not encapsulate another protocol");
4132 bpf_error("rarp does not encapsulate another protocol");
4136 bpf_error("atalk encapsulation is not specifiable");
4140 bpf_error("decnet encapsulation is not specifiable");
4144 bpf_error("sca does not encapsulate another protocol");
4148 bpf_error("lat does not encapsulate another protocol");
4152 bpf_error("moprc does not encapsulate another protocol");
4156 bpf_error("mopdl does not encapsulate another protocol");
4160 return gen_linktype(v
);
4163 bpf_error("'udp proto' is bogus");
4167 bpf_error("'tcp proto' is bogus");
4171 bpf_error("'sctp proto' is bogus");
4175 bpf_error("'icmp proto' is bogus");
4179 bpf_error("'igmp proto' is bogus");
4183 bpf_error("'igrp proto' is bogus");
4187 bpf_error("'pim proto' is bogus");
4191 bpf_error("'vrrp proto' is bogus");
4196 b0
= gen_linktype(ETHERTYPE_IPV6
);
4198 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
4200 b1
= gen_protochain(v
, Q_IPV6
);
4206 bpf_error("'icmp6 proto' is bogus");
4210 bpf_error("'ah proto' is bogus");
4213 bpf_error("'ah proto' is bogus");
4216 bpf_error("'stp proto' is bogus");
4219 bpf_error("'ipx proto' is bogus");
4222 bpf_error("'netbeui proto' is bogus");
4233 register const char *name
;
4236 int proto
= q
.proto
;
4240 bpf_u_int32 mask
, addr
;
4242 bpf_u_int32
**alist
;
4245 struct sockaddr_in
*sin
;
4246 struct sockaddr_in6
*sin6
;
4247 struct addrinfo
*res
, *res0
;
4248 struct in6_addr mask128
;
4250 struct block
*b
, *tmp
;
4251 int port
, real_proto
;
4257 addr
= pcap_nametonetaddr(name
);
4259 bpf_error("unknown network '%s'", name
);
4260 /* Left justify network addr and calculate its network mask */
4262 while (addr
&& (addr
& 0xff000000) == 0) {
4266 return gen_host(addr
, mask
, proto
, dir
);
4270 if (proto
== Q_LINK
) {
4274 eaddr
= pcap_ether_hostton(name
);
4277 "unknown ether host '%s'", name
);
4278 b
= gen_ehostop(eaddr
, dir
);
4283 eaddr
= pcap_ether_hostton(name
);
4286 "unknown FDDI host '%s'", name
);
4287 b
= gen_fhostop(eaddr
, dir
);
4292 eaddr
= pcap_ether_hostton(name
);
4295 "unknown token ring host '%s'", name
);
4296 b
= gen_thostop(eaddr
, dir
);
4300 case DLT_IEEE802_11
:
4301 eaddr
= pcap_ether_hostton(name
);
4304 "unknown 802.11 host '%s'", name
);
4305 b
= gen_wlanhostop(eaddr
, dir
);
4309 case DLT_IP_OVER_FC
:
4310 eaddr
= pcap_ether_hostton(name
);
4313 "unknown Fibre Channel host '%s'", name
);
4314 b
= gen_ipfchostop(eaddr
, dir
);
4323 * Check that the packet doesn't begin
4324 * with an LE Control marker. (We've
4325 * already generated a test for LANE.)
4327 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4331 eaddr
= pcap_ether_hostton(name
);
4334 "unknown ether host '%s'", name
);
4335 b
= gen_ehostop(eaddr
, dir
);
4341 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4342 } else if (proto
== Q_DECNET
) {
4343 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4345 * I don't think DECNET hosts can be multihomed, so
4346 * there is no need to build up a list of addresses
4348 return (gen_host(dn_addr
, 0, proto
, dir
));
4351 alist
= pcap_nametoaddr(name
);
4352 if (alist
== NULL
|| *alist
== NULL
)
4353 bpf_error("unknown host '%s'", name
);
4355 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4357 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4359 tmp
= gen_host(**alist
++, 0xffffffff,
4366 memset(&mask128
, 0xff, sizeof(mask128
));
4367 res0
= res
= pcap_nametoaddrinfo(name
);
4369 bpf_error("unknown host '%s'", name
);
4371 tproto
= tproto6
= proto
;
4372 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4376 for (res
= res0
; res
; res
= res
->ai_next
) {
4377 switch (res
->ai_family
) {
4379 if (tproto
== Q_IPV6
)
4382 sin
= (struct sockaddr_in
*)
4384 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4385 0xffffffff, tproto
, dir
);
4388 if (tproto6
== Q_IP
)
4391 sin6
= (struct sockaddr_in6
*)
4393 tmp
= gen_host6(&sin6
->sin6_addr
,
4394 &mask128
, tproto6
, dir
);
4405 bpf_error("unknown host '%s'%s", name
,
4406 (proto
== Q_DEFAULT
)
4408 : " for specified address family");
4415 if (proto
!= Q_DEFAULT
&&
4416 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4417 bpf_error("illegal qualifier of 'port'");
4418 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4419 bpf_error("unknown port '%s'", name
);
4420 if (proto
== Q_UDP
) {
4421 if (real_proto
== IPPROTO_TCP
)
4422 bpf_error("port '%s' is tcp", name
);
4423 else if (real_proto
== IPPROTO_SCTP
)
4424 bpf_error("port '%s' is sctp", name
);
4426 /* override PROTO_UNDEF */
4427 real_proto
= IPPROTO_UDP
;
4429 if (proto
== Q_TCP
) {
4430 if (real_proto
== IPPROTO_UDP
)
4431 bpf_error("port '%s' is udp", name
);
4433 else if (real_proto
== IPPROTO_SCTP
)
4434 bpf_error("port '%s' is sctp", name
);
4436 /* override PROTO_UNDEF */
4437 real_proto
= IPPROTO_TCP
;
4439 if (proto
== Q_SCTP
) {
4440 if (real_proto
== IPPROTO_UDP
)
4441 bpf_error("port '%s' is udp", name
);
4443 else if (real_proto
== IPPROTO_TCP
)
4444 bpf_error("port '%s' is tcp", name
);
4446 /* override PROTO_UNDEF */
4447 real_proto
= IPPROTO_SCTP
;
4450 return gen_port(port
, real_proto
, dir
);
4454 b
= gen_port(port
, real_proto
, dir
);
4455 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4461 if (proto
!= Q_DEFAULT
&&
4462 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4463 bpf_error("illegal qualifier of 'portrange'");
4464 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4465 bpf_error("unknown port in range '%s'", name
);
4466 if (proto
== Q_UDP
) {
4467 if (real_proto
== IPPROTO_TCP
)
4468 bpf_error("port in range '%s' is tcp", name
);
4469 else if (real_proto
== IPPROTO_SCTP
)
4470 bpf_error("port in range '%s' is sctp", name
);
4472 /* override PROTO_UNDEF */
4473 real_proto
= IPPROTO_UDP
;
4475 if (proto
== Q_TCP
) {
4476 if (real_proto
== IPPROTO_UDP
)
4477 bpf_error("port in range '%s' is udp", name
);
4478 else if (real_proto
== IPPROTO_SCTP
)
4479 bpf_error("port in range '%s' is sctp", name
);
4481 /* override PROTO_UNDEF */
4482 real_proto
= IPPROTO_TCP
;
4484 if (proto
== Q_SCTP
) {
4485 if (real_proto
== IPPROTO_UDP
)
4486 bpf_error("port in range '%s' is udp", name
);
4487 else if (real_proto
== IPPROTO_TCP
)
4488 bpf_error("port in range '%s' is tcp", name
);
4490 /* override PROTO_UNDEF */
4491 real_proto
= IPPROTO_SCTP
;
4494 return gen_portrange(port1
, port2
, real_proto
, dir
);
4498 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4499 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4506 eaddr
= pcap_ether_hostton(name
);
4508 bpf_error("unknown ether host: %s", name
);
4510 alist
= pcap_nametoaddr(name
);
4511 if (alist
== NULL
|| *alist
== NULL
)
4512 bpf_error("unknown host '%s'", name
);
4513 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4517 bpf_error("'gateway' not supported in this configuration");
4521 real_proto
= lookup_proto(name
, proto
);
4522 if (real_proto
>= 0)
4523 return gen_proto(real_proto
, proto
, dir
);
4525 bpf_error("unknown protocol: %s", name
);
4528 real_proto
= lookup_proto(name
, proto
);
4529 if (real_proto
>= 0)
4530 return gen_protochain(real_proto
, proto
, dir
);
4532 bpf_error("unknown protocol: %s", name
);
4544 gen_mcode(s1
, s2
, masklen
, q
)
4545 register const char *s1
, *s2
;
4546 register int masklen
;
4549 register int nlen
, mlen
;
4552 nlen
= __pcap_atoin(s1
, &n
);
4553 /* Promote short ipaddr */
4557 mlen
= __pcap_atoin(s2
, &m
);
4558 /* Promote short ipaddr */
4561 bpf_error("non-network bits set in \"%s mask %s\"",
4564 /* Convert mask len to mask */
4566 bpf_error("mask length must be <= 32");
4567 m
= 0xffffffff << (32 - masklen
);
4569 bpf_error("non-network bits set in \"%s/%d\"",
4576 return gen_host(n
, m
, q
.proto
, q
.dir
);
4579 bpf_error("Mask syntax for networks only");
4587 register const char *s
;
4592 int proto
= q
.proto
;
4598 else if (q
.proto
== Q_DECNET
)
4599 vlen
= __pcap_atodn(s
, &v
);
4601 vlen
= __pcap_atoin(s
, &v
);
4608 if (proto
== Q_DECNET
)
4609 return gen_host(v
, 0, proto
, dir
);
4610 else if (proto
== Q_LINK
) {
4611 bpf_error("illegal link layer address");
4614 if (s
== NULL
&& q
.addr
== Q_NET
) {
4615 /* Promote short net number */
4616 while (v
&& (v
& 0xff000000) == 0) {
4621 /* Promote short ipaddr */
4625 return gen_host(v
, mask
, proto
, dir
);
4630 proto
= IPPROTO_UDP
;
4631 else if (proto
== Q_TCP
)
4632 proto
= IPPROTO_TCP
;
4633 else if (proto
== Q_SCTP
)
4634 proto
= IPPROTO_SCTP
;
4635 else if (proto
== Q_DEFAULT
)
4636 proto
= PROTO_UNDEF
;
4638 bpf_error("illegal qualifier of 'port'");
4641 return gen_port((int)v
, proto
, dir
);
4645 b
= gen_port((int)v
, proto
, dir
);
4646 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4653 proto
= IPPROTO_UDP
;
4654 else if (proto
== Q_TCP
)
4655 proto
= IPPROTO_TCP
;
4656 else if (proto
== Q_SCTP
)
4657 proto
= IPPROTO_SCTP
;
4658 else if (proto
== Q_DEFAULT
)
4659 proto
= PROTO_UNDEF
;
4661 bpf_error("illegal qualifier of 'portrange'");
4664 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4668 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4669 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4675 bpf_error("'gateway' requires a name");
4679 return gen_proto((int)v
, proto
, dir
);
4682 return gen_protochain((int)v
, proto
, dir
);
4697 gen_mcode6(s1
, s2
, masklen
, q
)
4698 register const char *s1
, *s2
;
4699 register int masklen
;
4702 struct addrinfo
*res
;
4703 struct in6_addr
*addr
;
4704 struct in6_addr mask
;
4709 bpf_error("no mask %s supported", s2
);
4711 res
= pcap_nametoaddrinfo(s1
);
4713 bpf_error("invalid ip6 address %s", s1
);
4715 bpf_error("%s resolved to multiple address", s1
);
4716 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4718 if (sizeof(mask
) * 8 < masklen
)
4719 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4720 memset(&mask
, 0, sizeof(mask
));
4721 memset(&mask
, 0xff, masklen
/ 8);
4723 mask
.s6_addr
[masklen
/ 8] =
4724 (0xff << (8 - masklen
% 8)) & 0xff;
4727 a
= (u_int32_t
*)addr
;
4728 m
= (u_int32_t
*)&mask
;
4729 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4730 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4731 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4739 bpf_error("Mask syntax for networks only");
4743 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4748 bpf_error("invalid qualifier against IPv6 address");
4756 register const u_char
*eaddr
;
4759 struct block
*b
, *tmp
;
4761 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4762 if (linktype
== DLT_EN10MB
)
4763 return gen_ehostop(eaddr
, (int)q
.dir
);
4764 if (linktype
== DLT_FDDI
)
4765 return gen_fhostop(eaddr
, (int)q
.dir
);
4766 if (linktype
== DLT_IEEE802
)
4767 return gen_thostop(eaddr
, (int)q
.dir
);
4768 if (linktype
== DLT_IEEE802_11
)
4769 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4770 if (linktype
== DLT_SUNATM
&& is_lane
) {
4772 * Check that the packet doesn't begin with an
4773 * LE Control marker. (We've already generated
4776 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4780 * Now check the MAC address.
4782 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4786 if (linktype
== DLT_IP_OVER_FC
)
4787 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4788 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4790 bpf_error("ethernet address used in non-ether expression");
4796 struct slist
*s0
, *s1
;
4799 * This is definitely not the best way to do this, but the
4800 * lists will rarely get long.
4807 static struct slist
*
4813 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4818 static struct slist
*
4824 s
= new_stmt(BPF_LD
|BPF_MEM
);
4830 gen_load(proto
, index
, size
)
4835 struct slist
*s
, *tmp
;
4837 int regno
= alloc_reg();
4839 free_reg(index
->regno
);
4843 bpf_error("data size must be 1, 2, or 4");
4859 bpf_error("unsupported index operation");
4863 * XXX - what about ATM LANE? Should the index be
4864 * relative to the beginning of the AAL5 frame, so
4865 * that 0 refers to the beginning of the LE Control
4866 * field, or relative to the beginning of the LAN
4867 * frame, so that 0 refers, for Ethernet LANE, to
4868 * the beginning of the destination address?
4870 s
= xfer_to_x(index
);
4871 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4873 sappend(index
->s
, s
);
4888 /* XXX Note that we assume a fixed link header here. */
4889 s
= xfer_to_x(index
);
4890 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4893 sappend(index
->s
, s
);
4895 b
= gen_proto_abbrev(proto
);
4897 gen_and(index
->b
, b
);
4909 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4911 sappend(s
, xfer_to_a(index
));
4912 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4913 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4914 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4916 sappend(index
->s
, s
);
4918 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4920 gen_and(index
->b
, b
);
4922 gen_and(gen_proto_abbrev(Q_IP
), b
);
4928 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4932 index
->regno
= regno
;
4933 s
= new_stmt(BPF_ST
);
4935 sappend(index
->s
, s
);
4941 gen_relation(code
, a0
, a1
, reversed
)
4943 struct arth
*a0
, *a1
;
4946 struct slist
*s0
, *s1
, *s2
;
4947 struct block
*b
, *tmp
;
4951 if (code
== BPF_JEQ
) {
4952 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4953 b
= new_block(JMP(code
));
4957 b
= new_block(BPF_JMP
|code
|BPF_X
);
4963 sappend(a0
->s
, a1
->s
);
4967 free_reg(a0
->regno
);
4968 free_reg(a1
->regno
);
4970 /* 'and' together protocol checks */
4973 gen_and(a0
->b
, tmp
= a1
->b
);
4989 int regno
= alloc_reg();
4990 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4993 s
= new_stmt(BPF_LD
|BPF_LEN
);
4994 s
->next
= new_stmt(BPF_ST
);
4995 s
->next
->s
.k
= regno
;
5010 a
= (struct arth
*)newchunk(sizeof(*a
));
5014 s
= new_stmt(BPF_LD
|BPF_IMM
);
5016 s
->next
= new_stmt(BPF_ST
);
5032 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5035 s
= new_stmt(BPF_ST
);
5043 gen_arth(code
, a0
, a1
)
5045 struct arth
*a0
, *a1
;
5047 struct slist
*s0
, *s1
, *s2
;
5051 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5056 sappend(a0
->s
, a1
->s
);
5058 free_reg(a0
->regno
);
5059 free_reg(a1
->regno
);
5061 s0
= new_stmt(BPF_ST
);
5062 a0
->regno
= s0
->s
.k
= alloc_reg();
5069 * Here we handle simple allocation of the scratch registers.
5070 * If too many registers are alloc'd, the allocator punts.
5072 static int regused
[BPF_MEMWORDS
];
5076 * Return the next free register.
5081 int n
= BPF_MEMWORDS
;
5084 if (regused
[curreg
])
5085 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5087 regused
[curreg
] = 1;
5091 bpf_error("too many registers needed to evaluate expression");
5096 * Return a register to the table so it can
5106 static struct block
*
5113 s
= new_stmt(BPF_LD
|BPF_LEN
);
5114 b
= new_block(JMP(jmp
));
5125 return gen_len(BPF_JGE
, n
);
5129 * Actually, this is less than or equal.
5137 b
= gen_len(BPF_JGT
, n
);
5144 gen_byteop(op
, idx
, val
)
5155 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5158 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5159 b
->s
.code
= JMP(BPF_JGE
);
5164 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5165 b
->s
.code
= JMP(BPF_JGT
);
5169 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5173 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5177 b
= new_block(JMP(BPF_JEQ
));
5184 static u_char abroadcast
[] = { 0x0 };
5187 gen_broadcast(proto
)
5190 bpf_u_int32 hostmask
;
5191 struct block
*b0
, *b1
, *b2
;
5192 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5198 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5199 return gen_ahostop(abroadcast
, Q_DST
);
5200 if (linktype
== DLT_EN10MB
)
5201 return gen_ehostop(ebroadcast
, Q_DST
);
5202 if (linktype
== DLT_FDDI
)
5203 return gen_fhostop(ebroadcast
, Q_DST
);
5204 if (linktype
== DLT_IEEE802
)
5205 return gen_thostop(ebroadcast
, Q_DST
);
5206 if (linktype
== DLT_IEEE802_11
)
5207 return gen_wlanhostop(ebroadcast
, Q_DST
);
5208 if (linktype
== DLT_IP_OVER_FC
)
5209 return gen_ipfchostop(ebroadcast
, Q_DST
);
5210 if (linktype
== DLT_SUNATM
&& is_lane
) {
5212 * Check that the packet doesn't begin with an
5213 * LE Control marker. (We've already generated
5216 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5220 * Now check the MAC address.
5222 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5226 bpf_error("not a broadcast link");
5230 b0
= gen_linktype(ETHERTYPE_IP
);
5231 hostmask
= ~netmask
;
5232 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
5233 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
5234 (bpf_int32
)(~0 & hostmask
), hostmask
);
5239 bpf_error("only link-layer/IP broadcast filters supported");
5244 * Generate code to test the low-order bit of a MAC address (that's
5245 * the bottom bit of the *first* byte).
5247 static struct block
*
5248 gen_mac_multicast(offset
)
5251 register struct block
*b0
;
5252 register struct slist
*s
;
5254 /* link[offset] & 1 != 0 */
5255 s
= gen_load_llrel(offset
, BPF_B
);
5256 b0
= new_block(JMP(BPF_JSET
));
5263 gen_multicast(proto
)
5266 register struct block
*b0
, *b1
, *b2
;
5267 register struct slist
*s
;
5273 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5274 /* all ARCnet multicasts use the same address */
5275 return gen_ahostop(abroadcast
, Q_DST
);
5277 if (linktype
== DLT_EN10MB
) {
5278 /* ether[0] & 1 != 0 */
5279 return gen_mac_multicast(0);
5282 if (linktype
== DLT_FDDI
) {
5284 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5286 * XXX - was that referring to bit-order issues?
5288 /* fddi[1] & 1 != 0 */
5289 return gen_mac_multicast(1);
5292 if (linktype
== DLT_IEEE802
) {
5293 /* tr[2] & 1 != 0 */
5294 return gen_mac_multicast(2);
5297 if (linktype
== DLT_IEEE802_11
) {
5301 * For control frames, there is no DA.
5303 * For management frames, DA is at an
5304 * offset of 4 from the beginning of
5307 * For data frames, DA is at an offset
5308 * of 4 from the beginning of the packet
5309 * if To DS is clear and at an offset of
5310 * 16 from the beginning of the packet
5315 * Generate the tests to be done for data frames.
5317 * First, check for To DS set, i.e. "link[1] & 0x01".
5319 s
= gen_load_llrel(1, BPF_B
);
5320 b1
= new_block(JMP(BPF_JSET
));
5321 b1
->s
.k
= 0x01; /* To DS */
5325 * If To DS is set, the DA is at 16.
5327 b0
= gen_mac_multicast(16);
5331 * Now, check for To DS not set, i.e. check
5332 * "!(link[1] & 0x01)".
5334 s
= gen_load_llrel(1, BPF_B
);
5335 b2
= new_block(JMP(BPF_JSET
));
5336 b2
->s
.k
= 0x01; /* To DS */
5341 * If To DS is not set, the DA is at 4.
5343 b1
= gen_mac_multicast(4);
5347 * Now OR together the last two checks. That gives
5348 * the complete set of checks for data frames.
5353 * Now check for a data frame.
5354 * I.e, check "link[0] & 0x08".
5356 s
= gen_load_llrel(0, BPF_B
);
5357 b1
= new_block(JMP(BPF_JSET
));
5362 * AND that with the checks done for data frames.
5367 * If the high-order bit of the type value is 0, this
5368 * is a management frame.
5369 * I.e, check "!(link[0] & 0x08)".
5371 s
= gen_load_llrel(0, BPF_B
);
5372 b2
= new_block(JMP(BPF_JSET
));
5378 * For management frames, the DA is at 4.
5380 b1
= gen_mac_multicast(4);
5384 * OR that with the checks done for data frames.
5385 * That gives the checks done for management and
5391 * If the low-order bit of the type value is 1,
5392 * this is either a control frame or a frame
5393 * with a reserved type, and thus not a
5396 * I.e., check "!(link[0] & 0x04)".
5398 s
= gen_load_llrel(0, BPF_B
);
5399 b1
= new_block(JMP(BPF_JSET
));
5405 * AND that with the checks for data and management
5412 if (linktype
== DLT_IP_OVER_FC
) {
5413 b0
= gen_mac_multicast(2);
5417 if (linktype
== DLT_SUNATM
&& is_lane
) {
5419 * Check that the packet doesn't begin with an
5420 * LE Control marker. (We've already generated
5423 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5426 /* ether[off_mac] & 1 != 0 */
5427 b0
= gen_mac_multicast(off_mac
);
5432 /* Link not known to support multicasts */
5436 b0
= gen_linktype(ETHERTYPE_IP
);
5437 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5438 b1
->s
.code
= JMP(BPF_JGE
);
5444 b0
= gen_linktype(ETHERTYPE_IPV6
);
5445 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5450 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5455 * generate command for inbound/outbound. It's here so we can
5456 * make it link-type specific. 'dir' = 0 implies "inbound",
5457 * = 1 implies "outbound".
5463 register struct block
*b0
;
5466 * Only some data link types support inbound/outbound qualifiers.
5470 b0
= gen_relation(BPF_JEQ
,
5471 gen_load(Q_LINK
, gen_loadi(0), 1),
5479 * Match packets sent by this machine.
5481 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5484 * Match packets sent to this machine.
5485 * (No broadcast or multicast packets, or
5486 * packets sent to some other machine and
5487 * received promiscuously.)
5489 * XXX - packets sent to other machines probably
5490 * shouldn't be matched, but what about broadcast
5491 * or multicast packets we received?
5493 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5498 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5499 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5504 /* match outgoing packets */
5505 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5507 /* match incoming packets */
5508 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5512 case DLT_JUNIPER_MLFR
:
5513 case DLT_JUNIPER_MLPPP
:
5514 case DLT_JUNIPER_ATM1
:
5515 case DLT_JUNIPER_ATM2
:
5516 /* juniper flags (including direction) are stored
5517 * the byte after the 3-byte magic number */
5519 /* match outgoing packets */
5520 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5522 /* match incoming packets */
5523 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5528 bpf_error("inbound/outbound not supported on linktype %d",
5536 /* PF firewall log matched interface */
5538 gen_pf_ifname(const char *ifname
)
5543 if (linktype
== DLT_PFLOG
) {
5544 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5545 off
= offsetof(struct pfloghdr
, ifname
);
5547 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5550 if (strlen(ifname
) >= len
) {
5551 bpf_error("ifname interface names can only be %d characters",
5555 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5559 /* PF firewall log matched interface */
5561 gen_pf_ruleset(char *ruleset
)
5565 if (linktype
!= DLT_PFLOG
) {
5566 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5569 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5570 bpf_error("ruleset names can only be %ld characters",
5571 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5574 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5575 strlen(ruleset
), (const u_char
*)ruleset
);
5579 /* PF firewall log rule number */
5585 if (linktype
== DLT_PFLOG
) {
5586 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5589 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5596 /* PF firewall log sub-rule number */
5598 gen_pf_srnr(int srnr
)
5602 if (linktype
!= DLT_PFLOG
) {
5603 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5607 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5612 /* PF firewall log reason code */
5614 gen_pf_reason(int reason
)
5618 if (linktype
== DLT_PFLOG
) {
5619 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5622 bpf_error("reason not supported on linktype 0x%x", linktype
);
5629 /* PF firewall log action */
5631 gen_pf_action(int action
)
5635 if (linktype
== DLT_PFLOG
) {
5636 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5639 bpf_error("action not supported on linktype 0x%x", linktype
);
5648 register const u_char
*eaddr
;
5651 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5652 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5653 return gen_ahostop(eaddr
, (int)q
.dir
);
5655 bpf_error("ARCnet address used in non-arc expression");
5659 static struct block
*
5660 gen_ahostop(eaddr
, dir
)
5661 register const u_char
*eaddr
;
5664 register struct block
*b0
, *b1
;
5667 /* src comes first, different from Ethernet */
5669 return gen_bcmp(0, 1, eaddr
);
5672 return gen_bcmp(1, 1, eaddr
);
5675 b0
= gen_ahostop(eaddr
, Q_SRC
);
5676 b1
= gen_ahostop(eaddr
, Q_DST
);
5682 b0
= gen_ahostop(eaddr
, Q_SRC
);
5683 b1
= gen_ahostop(eaddr
, Q_DST
);
5692 * support IEEE 802.1Q VLAN trunk over ethernet
5701 * Change the offsets to point to the type and data fields within
5702 * the VLAN packet. Just increment the offsets, so that we
5703 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5704 * capture VLAN 200 encapsulated within VLAN 100.
5706 * XXX - this is a bit of a kludge. If we were to split the
5707 * compiler into a parser that parses an expression and
5708 * generates an expression tree, and a code generator that
5709 * takes an expression tree (which could come from our
5710 * parser or from some other parser) and generates BPF code,
5711 * we could perhaps make the offsets parameters of routines
5712 * and, in the handler for an "AND" node, pass to subnodes
5713 * other than the VLAN node the adjusted offsets.
5715 * This would mean that "vlan" would, instead of changing the
5716 * behavior of *all* tests after it, change only the behavior
5717 * of tests ANDed with it. That would change the documented
5718 * semantics of "vlan", which might break some expressions.
5719 * However, it would mean that "(vlan and ip) or ip" would check
5720 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5721 * checking only for VLAN-encapsulated IP, so that could still
5722 * be considered worth doing; it wouldn't break expressions
5723 * that are of the form "vlan and ..." or "vlan N and ...",
5724 * which I suspect are the most common expressions involving
5725 * "vlan". "vlan or ..." doesn't necessarily do what the user
5726 * would really want, now, as all the "or ..." tests would
5727 * be done assuming a VLAN, even though the "or" could be viewed
5728 * as meaning "or, if this isn't a VLAN packet...".
5730 orig_linktype
= off_linktype
; /* save original values */
5742 bpf_error("no VLAN support for data link type %d",
5747 /* check for VLAN */
5748 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5750 /* If a specific VLAN is requested, check VLAN id */
5751 if (vlan_num
>= 0) {
5754 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5772 * Change the offsets to point to the type and data fields within
5773 * the MPLS packet. Just increment the offsets, so that we
5774 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5775 * capture packets with an outer label of 100000 and an inner
5778 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5780 orig_linktype
= off_linktype
; /* save original values */
5785 case DLT_C_HDLC
: /* fall through */
5791 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5799 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5802 /* FIXME add other DLT_s ...
5803 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5804 * leave it for now */
5807 bpf_error("no MPLS support for data link type %d",
5814 /* If a specific MPLS label is requested, check it */
5815 if (label_num
>= 0) {
5818 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5819 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5828 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5840 bpf_error("'vpi' supported only on raw ATM");
5841 if (off_vpi
== (u_int
)-1)
5843 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5844 (u_int
)jvalue
, reverse
);
5849 bpf_error("'vci' supported only on raw ATM");
5850 if (off_vci
== (u_int
)-1)
5852 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5853 (u_int
)jvalue
, reverse
);
5857 if (off_proto
== (u_int
)-1)
5858 abort(); /* XXX - this isn't on FreeBSD */
5859 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5860 (u_int
)jvalue
, reverse
);
5864 if (off_payload
== (u_int
)-1)
5866 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5867 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5872 bpf_error("'callref' supported only on raw ATM");
5873 if (off_proto
== (u_int
)-1)
5875 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5876 (u_int
)jvalue
, reverse
);
5886 gen_atmtype_abbrev(type
)
5889 struct block
*b0
, *b1
;
5894 /* Get all packets in Meta signalling Circuit */
5896 bpf_error("'metac' supported only on raw ATM");
5897 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5898 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5903 /* Get all packets in Broadcast Circuit*/
5905 bpf_error("'bcc' supported only on raw ATM");
5906 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5907 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5912 /* Get all cells in Segment OAM F4 circuit*/
5914 bpf_error("'oam4sc' supported only on raw ATM");
5915 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5916 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5921 /* Get all cells in End-to-End OAM F4 Circuit*/
5923 bpf_error("'oam4ec' supported only on raw ATM");
5924 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5925 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5930 /* Get all packets in connection Signalling Circuit */
5932 bpf_error("'sc' supported only on raw ATM");
5933 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5934 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5939 /* Get all packets in ILMI Circuit */
5941 bpf_error("'ilmic' supported only on raw ATM");
5942 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5943 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5948 /* Get all LANE packets */
5950 bpf_error("'lane' supported only on raw ATM");
5951 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5954 * Arrange that all subsequent tests assume LANE
5955 * rather than LLC-encapsulated packets, and set
5956 * the offsets appropriately for LANE-encapsulated
5959 * "off_mac" is the offset of the Ethernet header,
5960 * which is 2 bytes past the ATM pseudo-header
5961 * (skipping the pseudo-header and 2-byte LE Client
5962 * field). The other offsets are Ethernet offsets
5963 * relative to "off_mac".
5966 off_mac
= off_payload
+ 2; /* MAC header */
5967 off_linktype
= off_mac
+ 12;
5968 off_nl
= off_mac
+ 14; /* Ethernet II */
5969 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5973 /* Get all LLC-encapsulated packets */
5975 bpf_error("'llc' supported only on raw ATM");
5976 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5987 static struct block
*
5988 gen_msg_abbrev(type
)
5994 * Q.2931 signalling protocol messages for handling virtual circuits
5995 * establishment and teardown
6000 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
6004 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
6008 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
6012 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
6016 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
6019 case A_RELEASE_DONE
:
6020 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6030 gen_atmmulti_abbrev(type
)
6033 struct block
*b0
, *b1
;
6039 bpf_error("'oam' supported only on raw ATM");
6040 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6045 bpf_error("'oamf4' supported only on raw ATM");
6047 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6048 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6050 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6056 * Get Q.2931 signalling messages for switched
6057 * virtual connection
6060 bpf_error("'connectmsg' supported only on raw ATM");
6061 b0
= gen_msg_abbrev(A_SETUP
);
6062 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6064 b0
= gen_msg_abbrev(A_CONNECT
);
6066 b0
= gen_msg_abbrev(A_CONNECTACK
);
6068 b0
= gen_msg_abbrev(A_RELEASE
);
6070 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6072 b0
= gen_atmtype_abbrev(A_SC
);
6078 bpf_error("'metaconnect' supported only on raw ATM");
6079 b0
= gen_msg_abbrev(A_SETUP
);
6080 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6082 b0
= gen_msg_abbrev(A_CONNECT
);
6084 b0
= gen_msg_abbrev(A_RELEASE
);
6086 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6088 b0
= gen_atmtype_abbrev(A_METAC
);