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.221.2.9 2005-05-01 00:38:34 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
);
169 static struct block
*gen_uncond(int);
170 static inline struct block
*gen_true(void);
171 static inline struct block
*gen_false(void);
172 static struct block
*gen_ether_linktype(int);
173 static struct block
*gen_linux_sll_linktype(int);
174 static struct block
*gen_linktype(int);
175 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
176 static struct block
*gen_llc_linktype(int);
177 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
179 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
181 static struct block
*gen_ahostop(const u_char
*, int);
182 static struct block
*gen_ehostop(const u_char
*, int);
183 static struct block
*gen_fhostop(const u_char
*, int);
184 static struct block
*gen_thostop(const u_char
*, int);
185 static struct block
*gen_wlanhostop(const u_char
*, int);
186 static struct block
*gen_ipfchostop(const u_char
*, int);
187 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
188 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
190 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
193 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
195 static struct block
*gen_ipfrag(void);
196 static struct block
*gen_portatom(int, bpf_int32
);
197 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
199 static struct block
*gen_portatom6(int, bpf_int32
);
200 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
202 struct block
*gen_portop(int, int, int);
203 static struct block
*gen_port(int, int, int);
204 struct block
*gen_portrangeop(int, int, int, int);
205 static struct block
*gen_portrange(int, int, int, int);
207 struct block
*gen_portop6(int, int, int);
208 static struct block
*gen_port6(int, int, int);
209 struct block
*gen_portrangeop6(int, int, int, int);
210 static struct block
*gen_portrange6(int, int, int, int);
212 static int lookup_proto(const char *, int);
213 static struct block
*gen_protochain(int, int, int);
214 static struct block
*gen_proto(int, int, int);
215 static struct slist
*xfer_to_x(struct arth
*);
216 static struct slist
*xfer_to_a(struct arth
*);
217 static struct block
*gen_mac_multicast(int);
218 static struct block
*gen_len(int, int);
220 static struct block
*gen_msg_abbrev(int type
);
231 /* XXX Round up to nearest long. */
232 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
234 /* XXX Round up to structure boundary. */
238 cp
= &chunks
[cur_chunk
];
239 if (n
> cp
->n_left
) {
240 ++cp
, k
= ++cur_chunk
;
242 bpf_error("out of memory");
243 size
= CHUNK0SIZE
<< k
;
244 cp
->m
= (void *)malloc(size
);
246 bpf_error("out of memory");
247 memset((char *)cp
->m
, 0, size
);
250 bpf_error("out of memory");
253 return (void *)((char *)cp
->m
+ cp
->n_left
);
262 for (i
= 0; i
< NCHUNKS
; ++i
)
263 if (chunks
[i
].m
!= NULL
) {
270 * A strdup whose allocations are freed after code generation is over.
274 register const char *s
;
276 int n
= strlen(s
) + 1;
277 char *cp
= newchunk(n
);
283 static inline struct block
*
289 p
= (struct block
*)newchunk(sizeof(*p
));
296 static inline struct slist
*
302 p
= (struct slist
*)newchunk(sizeof(*p
));
308 static struct block
*
312 struct block
*b
= new_block(BPF_RET
|BPF_K
);
321 bpf_error("syntax error in filter expression");
324 static bpf_u_int32 netmask
;
329 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
330 char *buf
, int optimize
, bpf_u_int32 mask
)
339 if (setjmp(top_ctx
)) {
347 snaplen
= pcap_snapshot(p
);
349 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
350 "snaplen of 0 rejects all packets");
354 lex_init(buf
? buf
: "");
362 root
= gen_retblk(snaplen
);
364 if (optimize
&& !no_optimize
) {
367 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
368 bpf_error("expression rejects all packets");
370 program
->bf_insns
= icode_to_fcode(root
, &len
);
371 program
->bf_len
= len
;
379 * entry point for using the compiler with no pcap open
380 * pass in all the stuff that is needed explicitly instead.
383 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
384 struct bpf_program
*program
,
385 char *buf
, int optimize
, bpf_u_int32 mask
)
390 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
393 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
399 * Clean up a "struct bpf_program" by freeing all the memory allocated
403 pcap_freecode(struct bpf_program
*program
)
406 if (program
->bf_insns
!= NULL
) {
407 free((char *)program
->bf_insns
);
408 program
->bf_insns
= NULL
;
413 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
414 * which of the jt and jf fields has been resolved and which is a pointer
415 * back to another unresolved block (or nil). At least one of the fields
416 * in each block is already resolved.
419 backpatch(list
, target
)
420 struct block
*list
, *target
;
437 * Merge the lists in b0 and b1, using the 'sense' field to indicate
438 * which of jt and jf is the link.
442 struct block
*b0
, *b1
;
444 register struct block
**p
= &b0
;
446 /* Find end of list. */
448 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
450 /* Concatenate the lists. */
458 backpatch(p
, gen_retblk(snaplen
));
459 p
->sense
= !p
->sense
;
460 backpatch(p
, gen_retblk(0));
466 struct block
*b0
, *b1
;
468 backpatch(b0
, b1
->head
);
469 b0
->sense
= !b0
->sense
;
470 b1
->sense
= !b1
->sense
;
472 b1
->sense
= !b1
->sense
;
478 struct block
*b0
, *b1
;
480 b0
->sense
= !b0
->sense
;
481 backpatch(b0
, b1
->head
);
482 b0
->sense
= !b0
->sense
;
491 b
->sense
= !b
->sense
;
494 static struct block
*
495 gen_cmp(offset
, size
, v
)
502 s
= gen_load_llrel(offset
, size
);
504 b
= new_block(JMP(BPF_JEQ
));
511 static struct block
*
512 gen_cmp_gt(offset
, size
, v
)
519 s
= gen_load_llrel(offset
, size
);
521 b
= new_block(JMP(BPF_JGT
));
528 static struct block
*
529 gen_mcmp(offset
, size
, v
, mask
)
534 struct block
*b
= gen_cmp(offset
, size
, v
);
537 if (mask
!= 0xffffffff) {
538 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
545 static struct block
*
546 gen_bcmp(offset
, size
, v
)
547 register u_int offset
, size
;
548 register const u_char
*v
;
550 register struct block
*b
, *tmp
;
554 register const u_char
*p
= &v
[size
- 4];
555 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
556 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
558 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
565 register const u_char
*p
= &v
[size
- 2];
566 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
568 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
575 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
583 static struct block
*
584 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
585 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
591 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
594 if (mask
!= 0xffffffff) {
595 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
599 b
= new_block(JMP(jtype
));
602 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
608 * Various code constructs need to know the layout of the data link
609 * layer. These variables give the necessary offsets.
613 * This is the offset of the beginning of the MAC-layer header.
614 * It's usually 0, except for ATM LANE.
616 static u_int off_mac
;
619 * "off_linktype" is the offset to information in the link-layer header
620 * giving the packet type.
622 * For Ethernet, it's the offset of the Ethernet type field.
624 * For link-layer types that always use 802.2 headers, it's the
625 * offset of the LLC header.
627 * For PPP, it's the offset of the PPP type field.
629 * For Cisco HDLC, it's the offset of the CHDLC type field.
631 * For BSD loopback, it's the offset of the AF_ value.
633 * For Linux cooked sockets, it's the offset of the type field.
635 * It's set to -1 for no encapsulation, in which case, IP is assumed.
637 static u_int off_linktype
;
640 * TRUE if the link layer includes an ATM pseudo-header.
642 static int is_atm
= 0;
645 * TRUE if "lane" appeared in the filter; it causes us to generate
646 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
648 static int is_lane
= 0;
651 * These are offsets for the ATM pseudo-header.
653 static u_int off_vpi
;
654 static u_int off_vci
;
655 static u_int off_proto
;
658 * This is the offset of the first byte after the ATM pseudo_header,
659 * or -1 if there is no ATM pseudo-header.
661 static u_int off_payload
;
664 * These are offsets to the beginning of the network-layer header.
666 * If the link layer never uses 802.2 LLC:
668 * "off_nl" and "off_nl_nosnap" are the same.
670 * If the link layer always uses 802.2 LLC:
672 * "off_nl" is the offset if there's a SNAP header following
675 * "off_nl_nosnap" is the offset if there's no SNAP header.
677 * If the link layer is Ethernet:
679 * "off_nl" is the offset if the packet is an Ethernet II packet
680 * (we assume no 802.3+802.2+SNAP);
682 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
683 * with an 802.2 header following it.
686 static u_int off_nl_nosnap
;
694 linktype
= pcap_datalink(p
);
696 pcap_fddipad
= p
->fddipad
;
700 * Assume it's not raw ATM with a pseudo-header, for now.
717 off_nl
= 6; /* XXX in reality, variable! */
718 off_nl_nosnap
= 6; /* no 802.2 LLC */
721 case DLT_ARCNET_LINUX
:
723 off_nl
= 8; /* XXX in reality, variable! */
724 off_nl_nosnap
= 8; /* no 802.2 LLC */
729 off_nl
= 14; /* Ethernet II */
730 off_nl_nosnap
= 17; /* 802.3+802.2 */
735 * SLIP doesn't have a link level type. The 16 byte
736 * header is hacked into our SLIP driver.
740 off_nl_nosnap
= 16; /* no 802.2 LLC */
744 /* XXX this may be the same as the DLT_PPP_BSDOS case */
748 off_nl_nosnap
= 24; /* no 802.2 LLC */
755 off_nl_nosnap
= 4; /* no 802.2 LLC */
761 off_nl_nosnap
= 12; /* no 802.2 LLC */
766 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
767 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
770 off_nl_nosnap
= 4; /* no 802.2 LLC */
775 * This does no include the Ethernet header, and
776 * only covers session state.
780 off_nl_nosnap
= 8; /* no 802.2 LLC */
786 off_nl_nosnap
= 24; /* no 802.2 LLC */
791 * FDDI doesn't really have a link-level type field.
792 * We set "off_linktype" to the offset of the LLC header.
794 * To check for Ethernet types, we assume that SSAP = SNAP
795 * is being used and pick out the encapsulated Ethernet type.
796 * XXX - should we generate code to check for SNAP?
800 off_linktype
+= pcap_fddipad
;
802 off_nl
= 21; /* FDDI+802.2+SNAP */
803 off_nl_nosnap
= 16; /* FDDI+802.2 */
805 off_nl
+= pcap_fddipad
;
806 off_nl_nosnap
+= pcap_fddipad
;
812 * Token Ring doesn't really have a link-level type field.
813 * We set "off_linktype" to the offset of the LLC header.
815 * To check for Ethernet types, we assume that SSAP = SNAP
816 * is being used and pick out the encapsulated Ethernet type.
817 * XXX - should we generate code to check for SNAP?
819 * XXX - the header is actually variable-length.
820 * Some various Linux patched versions gave 38
821 * as "off_linktype" and 40 as "off_nl"; however,
822 * if a token ring packet has *no* routing
823 * information, i.e. is not source-routed, the correct
824 * values are 20 and 22, as they are in the vanilla code.
826 * A packet is source-routed iff the uppermost bit
827 * of the first byte of the source address, at an
828 * offset of 8, has the uppermost bit set. If the
829 * packet is source-routed, the total number of bytes
830 * of routing information is 2 plus bits 0x1F00 of
831 * the 16-bit value at an offset of 14 (shifted right
832 * 8 - figure out which byte that is).
835 off_nl
= 22; /* Token Ring+802.2+SNAP */
836 off_nl_nosnap
= 17; /* Token Ring+802.2 */
841 * 802.11 doesn't really have a link-level type field.
842 * We set "off_linktype" to the offset of the LLC header.
844 * To check for Ethernet types, we assume that SSAP = SNAP
845 * is being used and pick out the encapsulated Ethernet type.
846 * XXX - should we generate code to check for SNAP?
848 * XXX - the header is actually variable-length. We
849 * assume a 24-byte link-layer header, as appears in
850 * data frames in networks with no bridges. If the
851 * fromds and tods 802.11 header bits are both set,
852 * it's actually supposed to be 30 bytes.
855 off_nl
= 32; /* 802.11+802.2+SNAP */
856 off_nl_nosnap
= 27; /* 802.11+802.2 */
859 case DLT_PRISM_HEADER
:
861 * Same as 802.11, but with an additional header before
862 * the 802.11 header, containing a bunch of additional
863 * information including radio-level information.
865 * The header is 144 bytes long.
867 * XXX - same variable-length header problem; at least
868 * the Prism header is fixed-length.
870 off_linktype
= 144+24;
871 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
872 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
875 case DLT_IEEE802_11_RADIO_AVS
:
877 * Same as 802.11, but with an additional header before
878 * the 802.11 header, containing a bunch of additional
879 * information including radio-level information.
881 * The header is 64 bytes long, at least in its
882 * current incarnation.
884 * XXX - same variable-length header problem, only
885 * more so; this header is also variable-length,
886 * with the length being the 32-bit big-endian
887 * number at an offset of 4 from the beginning
888 * of the radio header.
890 off_linktype
= 64+24;
891 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
892 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
895 case DLT_IEEE802_11_RADIO
:
897 * Same as 802.11, but with an additional header before
898 * the 802.11 header, containing a bunch of additional
899 * information including radio-level information.
901 * XXX - same variable-length header problem, only
902 * even *more* so; this header is also variable-length,
903 * with the length being the 16-bit number at an offset
904 * of 2 from the beginning of the radio header, and it's
905 * device-dependent (different devices might supply
906 * different amounts of information), so we can't even
907 * assume a fixed length for the current version of the
910 * Therefore, currently, only raw "link[N:M]" filtering is
918 case DLT_ATM_RFC1483
:
919 case DLT_ATM_CLIP
: /* Linux ATM defines this */
921 * assume routed, non-ISO PDUs
922 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
925 off_nl
= 8; /* 802.2+SNAP */
926 off_nl_nosnap
= 3; /* 802.2 */
931 * Full Frontal ATM; you get AALn PDUs with an ATM
935 off_vpi
= SUNATM_VPI_POS
;
936 off_vci
= SUNATM_VCI_POS
;
937 off_proto
= PROTO_POS
;
938 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
939 off_payload
= SUNATM_PKT_BEGIN_POS
;
940 off_linktype
= off_payload
;
941 off_nl
= off_payload
+8; /* 802.2+SNAP */
942 off_nl_nosnap
= off_payload
+3; /* 802.2 */
948 off_nl_nosnap
= 0; /* no 802.2 LLC */
951 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
954 off_nl_nosnap
= 16; /* no 802.2 LLC */
959 * LocalTalk does have a 1-byte type field in the LLAP header,
960 * but really it just indicates whether there is a "short" or
961 * "long" DDP packet following.
965 off_nl_nosnap
= 0; /* no 802.2 LLC */
970 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
971 * link-level type field. We set "off_linktype" to the
972 * offset of the LLC header.
974 * To check for Ethernet types, we assume that SSAP = SNAP
975 * is being used and pick out the encapsulated Ethernet type.
976 * XXX - should we generate code to check for SNAP? RFC
977 * 2625 says SNAP should be used.
980 off_nl
= 24; /* IPFC+802.2+SNAP */
981 off_nl_nosnap
= 19; /* IPFC+802.2 */
986 * XXX - we should set this to handle SNAP-encapsulated
987 * frames (NLPID of 0x80).
991 off_nl_nosnap
= 0; /* no 802.2 LLC */
994 case DLT_APPLE_IP_OVER_IEEE1394
:
997 off_nl_nosnap
= 18; /* no 802.2 LLC */
1000 case DLT_LINUX_IRDA
:
1002 * Currently, only raw "link[N:M]" filtering is supported.
1011 * Currently, only raw "link[N:M]" filtering is supported.
1018 case DLT_SYMANTEC_FIREWALL
:
1020 off_nl
= 44; /* Ethernet II */
1021 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1026 /* XXX read this from pf.h? */
1027 off_nl
= PFLOG_HDRLEN
;
1028 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1031 case DLT_JUNIPER_MLFR
:
1032 case DLT_JUNIPER_MLPPP
:
1035 off_nl_nosnap
= -1; /* no 802.2 LLC */
1038 case DLT_JUNIPER_ATM1
:
1039 off_linktype
= 4; /* in reality variable between 4-8 */
1044 case DLT_JUNIPER_ATM2
:
1045 off_linktype
= 8; /* in reality variable between 8-12 */
1058 case DLT_LINUX_LAPD
:
1060 * Currently, only raw "link[N:M]" filtering is supported.
1067 bpf_error("unknown data link type %d", linktype
);
1072 * Load a value relative to the beginning of the link-layer header.
1074 static struct slist
*
1075 gen_load_llrel(offset
, size
)
1080 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1086 * Load a value relative to the beginning of the network-layer header.
1088 static struct slist
*
1089 gen_load_nlrel(offset
, size
)
1092 return gen_load_llrel(off_nl
+ offset
, size
);
1096 * Load a value relative to the beginning of the transport-layer header,
1097 * where the network-layer header is an IPv4 header. (This doesn't handle
1100 static struct slist
*
1101 gen_load_ipv4tlrel(offset
, size
)
1107 * Load the X register with the length of the IPv4 header,
1110 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1114 * Load the item at {length of the link-layer header} + {length
1115 * of the IPv4 header} + {specified offset}.
1117 s
->next
= new_stmt(BPF_LD
|BPF_IND
|size
);
1118 s
->next
->s
.k
= off_nl
+ offset
;
1123 static struct block
*
1130 s
= new_stmt(BPF_LD
|BPF_IMM
);
1132 b
= new_block(JMP(BPF_JEQ
));
1138 static inline struct block
*
1141 return gen_uncond(1);
1144 static inline struct block
*
1147 return gen_uncond(0);
1151 * Byte-swap a 32-bit number.
1152 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1153 * big-endian platforms.)
1155 #define SWAPLONG(y) \
1156 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1159 * Generate code to match a particular packet type.
1161 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1162 * value, if <= ETHERMTU. We use that to determine whether to
1163 * match the type/length field or to check the type/length field for
1164 * a value <= ETHERMTU to see whether it's a type field and then do
1165 * the appropriate test.
1167 static struct block
*
1168 gen_ether_linktype(proto
)
1171 struct block
*b0
, *b1
;
1177 case LLCSAP_NETBEUI
:
1179 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1180 * so we check the DSAP and SSAP.
1182 * LLCSAP_IP checks for IP-over-802.2, rather
1183 * than IP-over-Ethernet or IP-over-SNAP.
1185 * XXX - should we check both the DSAP and the
1186 * SSAP, like this, or should we check just the
1187 * DSAP, as we do for other types <= ETHERMTU
1188 * (i.e., other SAP values)?
1190 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1192 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1193 ((proto
<< 8) | proto
));
1201 * Ethernet_II frames, which are Ethernet
1202 * frames with a frame type of ETHERTYPE_IPX;
1204 * Ethernet_802.3 frames, which are 802.3
1205 * frames (i.e., the type/length field is
1206 * a length field, <= ETHERMTU, rather than
1207 * a type field) with the first two bytes
1208 * after the Ethernet/802.3 header being
1211 * Ethernet_802.2 frames, which are 802.3
1212 * frames with an 802.2 LLC header and
1213 * with the IPX LSAP as the DSAP in the LLC
1216 * Ethernet_SNAP frames, which are 802.3
1217 * frames with an LLC header and a SNAP
1218 * header and with an OUI of 0x000000
1219 * (encapsulated Ethernet) and a protocol
1220 * ID of ETHERTYPE_IPX in the SNAP header.
1222 * XXX - should we generate the same code both
1223 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1227 * This generates code to check both for the
1228 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1230 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1231 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1235 * Now we add code to check for SNAP frames with
1236 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1238 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1242 * Now we generate code to check for 802.3
1243 * frames in general.
1245 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1249 * Now add the check for 802.3 frames before the
1250 * check for Ethernet_802.2 and Ethernet_802.3,
1251 * as those checks should only be done on 802.3
1252 * frames, not on Ethernet frames.
1257 * Now add the check for Ethernet_II frames, and
1258 * do that before checking for the other frame
1261 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1265 case ETHERTYPE_ATALK
:
1266 case ETHERTYPE_AARP
:
1268 * EtherTalk (AppleTalk protocols on Ethernet link
1269 * layer) may use 802.2 encapsulation.
1273 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1274 * we check for an Ethernet type field less than
1275 * 1500, which means it's an 802.3 length field.
1277 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1281 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1282 * SNAP packets with an organization code of
1283 * 0x080007 (Apple, for Appletalk) and a protocol
1284 * type of ETHERTYPE_ATALK (Appletalk).
1286 * 802.2-encapsulated ETHERTYPE_AARP packets are
1287 * SNAP packets with an organization code of
1288 * 0x000000 (encapsulated Ethernet) and a protocol
1289 * type of ETHERTYPE_AARP (Appletalk ARP).
1291 if (proto
== ETHERTYPE_ATALK
)
1292 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1293 else /* proto == ETHERTYPE_AARP */
1294 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1298 * Check for Ethernet encapsulation (Ethertalk
1299 * phase 1?); we just check for the Ethernet
1302 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1308 if (proto
<= ETHERMTU
) {
1310 * This is an LLC SAP value, so the frames
1311 * that match would be 802.2 frames.
1312 * Check that the frame is an 802.2 frame
1313 * (i.e., that the length/type field is
1314 * a length field, <= ETHERMTU) and
1315 * then check the DSAP.
1317 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1319 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1324 * This is an Ethernet type, so compare
1325 * the length/type field with it (if
1326 * the frame is an 802.2 frame, the length
1327 * field will be <= ETHERMTU, and, as
1328 * "proto" is > ETHERMTU, this test
1329 * will fail and the frame won't match,
1330 * which is what we want).
1332 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1338 * Generate code to match a particular packet type.
1340 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1341 * value, if <= ETHERMTU. We use that to determine whether to
1342 * match the type field or to check the type field for the special
1343 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1345 static struct block
*
1346 gen_linux_sll_linktype(proto
)
1349 struct block
*b0
, *b1
;
1355 case LLCSAP_NETBEUI
:
1357 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1358 * so we check the DSAP and SSAP.
1360 * LLCSAP_IP checks for IP-over-802.2, rather
1361 * than IP-over-Ethernet or IP-over-SNAP.
1363 * XXX - should we check both the DSAP and the
1364 * SSAP, like this, or should we check just the
1365 * DSAP, as we do for other types <= ETHERMTU
1366 * (i.e., other SAP values)?
1368 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1369 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1370 ((proto
<< 8) | proto
));
1376 * Ethernet_II frames, which are Ethernet
1377 * frames with a frame type of ETHERTYPE_IPX;
1379 * Ethernet_802.3 frames, which have a frame
1380 * type of LINUX_SLL_P_802_3;
1382 * Ethernet_802.2 frames, which are 802.3
1383 * frames with an 802.2 LLC header (i.e, have
1384 * a frame type of LINUX_SLL_P_802_2) and
1385 * with the IPX LSAP as the DSAP in the LLC
1388 * Ethernet_SNAP frames, which are 802.3
1389 * frames with an LLC header and a SNAP
1390 * header and with an OUI of 0x000000
1391 * (encapsulated Ethernet) and a protocol
1392 * ID of ETHERTYPE_IPX in the SNAP header.
1394 * First, do the checks on LINUX_SLL_P_802_2
1395 * frames; generate the check for either
1396 * Ethernet_802.2 or Ethernet_SNAP frames, and
1397 * then put a check for LINUX_SLL_P_802_2 frames
1400 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1401 (bpf_int32
)LLCSAP_IPX
);
1402 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1405 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1409 * Now check for 802.3 frames and OR that with
1410 * the previous test.
1412 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1416 * Now add the check for Ethernet_II frames, and
1417 * do that before checking for the other frame
1420 b0
= gen_cmp(off_linktype
, BPF_H
,
1421 (bpf_int32
)ETHERTYPE_IPX
);
1425 case ETHERTYPE_ATALK
:
1426 case ETHERTYPE_AARP
:
1428 * EtherTalk (AppleTalk protocols on Ethernet link
1429 * layer) may use 802.2 encapsulation.
1433 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1434 * we check for the 802.2 protocol type in the
1435 * "Ethernet type" field.
1437 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1440 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1441 * SNAP packets with an organization code of
1442 * 0x080007 (Apple, for Appletalk) and a protocol
1443 * type of ETHERTYPE_ATALK (Appletalk).
1445 * 802.2-encapsulated ETHERTYPE_AARP packets are
1446 * SNAP packets with an organization code of
1447 * 0x000000 (encapsulated Ethernet) and a protocol
1448 * type of ETHERTYPE_AARP (Appletalk ARP).
1450 if (proto
== ETHERTYPE_ATALK
)
1451 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1453 else /* proto == ETHERTYPE_AARP */
1454 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1459 * Check for Ethernet encapsulation (Ethertalk
1460 * phase 1?); we just check for the Ethernet
1463 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1469 if (proto
<= ETHERMTU
) {
1471 * This is an LLC SAP value, so the frames
1472 * that match would be 802.2 frames.
1473 * Check for the 802.2 protocol type
1474 * in the "Ethernet type" field, and
1475 * then check the DSAP.
1477 b0
= gen_cmp(off_linktype
, BPF_H
,
1479 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1485 * This is an Ethernet type, so compare
1486 * the length/type field with it (if
1487 * the frame is an 802.2 frame, the length
1488 * field will be <= ETHERMTU, and, as
1489 * "proto" is > ETHERMTU, this test
1490 * will fail and the frame won't match,
1491 * which is what we want).
1493 return gen_cmp(off_linktype
, BPF_H
,
1500 * Generate code to match a particular packet type by matching the
1501 * link-layer type field or fields in the 802.2 LLC header.
1503 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1504 * value, if <= ETHERMTU.
1506 static struct block
*
1510 struct block
*b0
, *b1
, *b2
;
1515 return gen_ether_linktype(proto
);
1523 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1527 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1533 case DLT_IEEE802_11
:
1534 case DLT_PRISM_HEADER
:
1535 case DLT_IEEE802_11_RADIO
:
1538 case DLT_ATM_RFC1483
:
1540 case DLT_IP_OVER_FC
:
1541 return gen_llc_linktype(proto
);
1547 * If "is_lane" is set, check for a LANE-encapsulated
1548 * version of this protocol, otherwise check for an
1549 * LLC-encapsulated version of this protocol.
1551 * We assume LANE means Ethernet, not Token Ring.
1555 * Check that the packet doesn't begin with an
1556 * LE Control marker. (We've already generated
1559 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1563 * Now generate an Ethernet test.
1565 b1
= gen_ether_linktype(proto
);
1570 * Check for LLC encapsulation and then check the
1573 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1574 b1
= gen_llc_linktype(proto
);
1582 return gen_linux_sll_linktype(proto
);
1587 case DLT_SLIP_BSDOS
:
1590 * These types don't provide any type field; packets
1593 * XXX - for IPv4, check for a version number of 4, and,
1594 * for IPv6, check for a version number of 6?
1600 case ETHERTYPE_IPV6
:
1602 return gen_true(); /* always true */
1605 return gen_false(); /* always false */
1612 case DLT_PPP_SERIAL
:
1615 * We use Ethernet protocol types inside libpcap;
1616 * map them to the corresponding PPP protocol types.
1625 case ETHERTYPE_IPV6
:
1634 case ETHERTYPE_ATALK
:
1648 * I'm assuming the "Bridging PDU"s that go
1649 * over PPP are Spanning Tree Protocol
1663 * We use Ethernet protocol types inside libpcap;
1664 * map them to the corresponding PPP protocol types.
1669 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1670 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1672 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1677 case ETHERTYPE_IPV6
:
1687 case ETHERTYPE_ATALK
:
1701 * I'm assuming the "Bridging PDU"s that go
1702 * over PPP are Spanning Tree Protocol
1718 * For DLT_NULL, the link-layer header is a 32-bit
1719 * word containing an AF_ value in *host* byte order,
1720 * and for DLT_ENC, the link-layer header begins
1721 * with a 32-bit work containing an AF_ value in
1724 * In addition, if we're reading a saved capture file,
1725 * the host byte order in the capture may not be the
1726 * same as the host byte order on this machine.
1728 * For DLT_LOOP, the link-layer header is a 32-bit
1729 * word containing an AF_ value in *network* byte order.
1731 * XXX - AF_ values may, unfortunately, be platform-
1732 * dependent; for example, FreeBSD's AF_INET6 is 24
1733 * whilst NetBSD's and OpenBSD's is 26.
1735 * This means that, when reading a capture file, just
1736 * checking for our AF_INET6 value won't work if the
1737 * capture file came from another OS.
1746 case ETHERTYPE_IPV6
:
1753 * Not a type on which we support filtering.
1754 * XXX - support those that have AF_ values
1755 * #defined on this platform, at least?
1760 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1762 * The AF_ value is in host byte order, but
1763 * the BPF interpreter will convert it to
1764 * network byte order.
1766 * If this is a save file, and it's from a
1767 * machine with the opposite byte order to
1768 * ours, we byte-swap the AF_ value.
1770 * Then we run it through "htonl()", and
1771 * generate code to compare against the result.
1773 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1774 bpf_pcap
->sf
.swapped
)
1775 proto
= SWAPLONG(proto
);
1776 proto
= htonl(proto
);
1778 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1782 * af field is host byte order in contrast to the rest of
1785 if (proto
== ETHERTYPE_IP
)
1786 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1787 (bpf_int32
)AF_INET
));
1789 else if (proto
== ETHERTYPE_IPV6
)
1790 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1791 (bpf_int32
)AF_INET6
));
1799 case DLT_ARCNET_LINUX
:
1801 * XXX should we check for first fragment if the protocol
1810 case ETHERTYPE_IPV6
:
1811 return (gen_cmp(off_linktype
, BPF_B
,
1812 (bpf_int32
)ARCTYPE_INET6
));
1816 b0
= gen_cmp(off_linktype
, BPF_B
,
1817 (bpf_int32
)ARCTYPE_IP
);
1818 b1
= gen_cmp(off_linktype
, BPF_B
,
1819 (bpf_int32
)ARCTYPE_IP_OLD
);
1824 b0
= gen_cmp(off_linktype
, BPF_B
,
1825 (bpf_int32
)ARCTYPE_ARP
);
1826 b1
= gen_cmp(off_linktype
, BPF_B
,
1827 (bpf_int32
)ARCTYPE_ARP_OLD
);
1831 case ETHERTYPE_REVARP
:
1832 return (gen_cmp(off_linktype
, BPF_B
,
1833 (bpf_int32
)ARCTYPE_REVARP
));
1835 case ETHERTYPE_ATALK
:
1836 return (gen_cmp(off_linktype
, BPF_B
,
1837 (bpf_int32
)ARCTYPE_ATALK
));
1844 case ETHERTYPE_ATALK
:
1854 * XXX - assumes a 2-byte Frame Relay header with
1855 * DLCI and flags. What if the address is longer?
1861 * Check for the special NLPID for IP.
1863 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1866 case ETHERTYPE_IPV6
:
1868 * Check for the special NLPID for IPv6.
1870 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1875 * Check for several OSI protocols.
1877 * Frame Relay packets typically have an OSI
1878 * NLPID at the beginning; we check for each
1881 * What we check for is the NLPID and a frame
1882 * control field of UI, i.e. 0x03 followed
1885 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1886 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1887 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1898 case DLT_JUNIPER_MLFR
:
1899 case DLT_JUNIPER_MLPPP
:
1900 case DLT_JUNIPER_ATM1
:
1901 case DLT_JUNIPER_ATM2
:
1902 /* just lets verify the magic number for now -
1903 * on ATM we may have up to 6 different encapsulations on the wire
1904 * and need a lot of heuristics to figure out that the payload
1907 * FIXME encapsulation specific BPF_ filters
1909 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1911 case DLT_LINUX_IRDA
:
1912 bpf_error("IrDA link-layer type filtering not implemented");
1915 bpf_error("DOCSIS link-layer type filtering not implemented");
1917 case DLT_LINUX_LAPD
:
1918 bpf_error("LAPD link-layer type filtering not implemented");
1922 * All the types that have no encapsulation should either be
1923 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1924 * all packets are IP packets, or should be handled in some
1925 * special case, if none of them are (if some are and some
1926 * aren't, the lack of encapsulation is a problem, as we'd
1927 * have to find some other way of determining the packet type).
1929 * Therefore, if "off_linktype" is -1, there's an error.
1931 if (off_linktype
== (u_int
)-1)
1935 * Any type not handled above should always have an Ethernet
1936 * type at an offset of "off_linktype". (PPP is partially
1937 * handled above - the protocol type is mapped from the
1938 * Ethernet and LLC types we use internally to the corresponding
1939 * PPP type - but the PPP type is always specified by a value
1940 * at "off_linktype", so we don't have to do the code generation
1943 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1947 * Check for an LLC SNAP packet with a given organization code and
1948 * protocol type; we check the entire contents of the 802.2 LLC and
1949 * snap headers, checking for DSAP and SSAP of SNAP and a control
1950 * field of 0x03 in the LLC header, and for the specified organization
1951 * code and protocol type in the SNAP header.
1953 static struct block
*
1954 gen_snap(orgcode
, ptype
, offset
)
1955 bpf_u_int32 orgcode
;
1959 u_char snapblock
[8];
1961 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1962 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1963 snapblock
[2] = 0x03; /* control = UI */
1964 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1965 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1966 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1967 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1968 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1969 return gen_bcmp(offset
, 8, snapblock
);
1973 * Generate code to match a particular packet type, for link-layer types
1974 * using 802.2 LLC headers.
1976 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1977 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1979 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1980 * value, if <= ETHERMTU. We use that to determine whether to
1981 * match the DSAP or both DSAP and LSAP or to check the OUI and
1982 * protocol ID in a SNAP header.
1984 static struct block
*
1985 gen_llc_linktype(proto
)
1989 * XXX - handle token-ring variable-length header.
1995 case LLCSAP_NETBEUI
:
1997 * XXX - should we check both the DSAP and the
1998 * SSAP, like this, or should we check just the
1999 * DSAP, as we do for other types <= ETHERMTU
2000 * (i.e., other SAP values)?
2002 return gen_cmp(off_linktype
, BPF_H
, (long)
2003 ((proto
<< 8) | proto
));
2007 * XXX - are there ever SNAP frames for IPX on
2008 * non-Ethernet 802.x networks?
2010 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2012 case ETHERTYPE_ATALK
:
2014 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2015 * SNAP packets with an organization code of
2016 * 0x080007 (Apple, for Appletalk) and a protocol
2017 * type of ETHERTYPE_ATALK (Appletalk).
2019 * XXX - check for an organization code of
2020 * encapsulated Ethernet as well?
2022 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
2026 * XXX - we don't have to check for IPX 802.3
2027 * here, but should we check for the IPX Ethertype?
2029 if (proto
<= ETHERMTU
) {
2031 * This is an LLC SAP value, so check
2034 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
2037 * This is an Ethernet type; we assume that it's
2038 * unlikely that it'll appear in the right place
2039 * at random, and therefore check only the
2040 * location that would hold the Ethernet type
2041 * in a SNAP frame with an organization code of
2042 * 0x000000 (encapsulated Ethernet).
2044 * XXX - if we were to check for the SNAP DSAP and
2045 * LSAP, as per XXX, and were also to check for an
2046 * organization code of 0x000000 (encapsulated
2047 * Ethernet), we'd do
2049 * return gen_snap(0x000000, proto,
2052 * here; for now, we don't, as per the above.
2053 * I don't know whether it's worth the extra CPU
2054 * time to do the right check or not.
2056 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2061 static struct block
*
2062 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2066 u_int src_off
, dst_off
;
2068 struct block
*b0
, *b1
;
2082 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2083 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2089 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2090 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2097 b0
= gen_linktype(proto
);
2098 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2104 static struct block
*
2105 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2106 struct in6_addr
*addr
;
2107 struct in6_addr
*mask
;
2109 u_int src_off
, dst_off
;
2111 struct block
*b0
, *b1
;
2126 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2127 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2133 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2134 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2141 /* this order is important */
2142 a
= (u_int32_t
*)addr
;
2143 m
= (u_int32_t
*)mask
;
2144 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2145 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2147 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2149 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2151 b0
= gen_linktype(proto
);
2157 static struct block
*
2158 gen_ehostop(eaddr
, dir
)
2159 register const u_char
*eaddr
;
2162 register struct block
*b0
, *b1
;
2166 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2169 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2172 b0
= gen_ehostop(eaddr
, Q_SRC
);
2173 b1
= gen_ehostop(eaddr
, Q_DST
);
2179 b0
= gen_ehostop(eaddr
, Q_SRC
);
2180 b1
= gen_ehostop(eaddr
, Q_DST
);
2189 * Like gen_ehostop, but for DLT_FDDI
2191 static struct block
*
2192 gen_fhostop(eaddr
, dir
)
2193 register const u_char
*eaddr
;
2196 struct block
*b0
, *b1
;
2201 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2203 return gen_bcmp(6 + 1, 6, eaddr
);
2208 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2210 return gen_bcmp(0 + 1, 6, eaddr
);
2214 b0
= gen_fhostop(eaddr
, Q_SRC
);
2215 b1
= gen_fhostop(eaddr
, Q_DST
);
2221 b0
= gen_fhostop(eaddr
, Q_SRC
);
2222 b1
= gen_fhostop(eaddr
, Q_DST
);
2231 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2233 static struct block
*
2234 gen_thostop(eaddr
, dir
)
2235 register const u_char
*eaddr
;
2238 register struct block
*b0
, *b1
;
2242 return gen_bcmp(8, 6, eaddr
);
2245 return gen_bcmp(2, 6, eaddr
);
2248 b0
= gen_thostop(eaddr
, Q_SRC
);
2249 b1
= gen_thostop(eaddr
, Q_DST
);
2255 b0
= gen_thostop(eaddr
, Q_SRC
);
2256 b1
= gen_thostop(eaddr
, Q_DST
);
2265 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2267 static struct block
*
2268 gen_wlanhostop(eaddr
, dir
)
2269 register const u_char
*eaddr
;
2272 register struct block
*b0
, *b1
, *b2
;
2273 register struct slist
*s
;
2280 * For control frames, there is no SA.
2282 * For management frames, SA is at an
2283 * offset of 10 from the beginning of
2286 * For data frames, SA is at an offset
2287 * of 10 from the beginning of the packet
2288 * if From DS is clear, at an offset of
2289 * 16 from the beginning of the packet
2290 * if From DS is set and To DS is clear,
2291 * and an offset of 24 from the beginning
2292 * of the packet if From DS is set and To DS
2297 * Generate the tests to be done for data frames
2300 * First, check for To DS set, i.e. check "link[1] & 0x01".
2302 s
= gen_load_llrel(1, BPF_B
);
2303 b1
= new_block(JMP(BPF_JSET
));
2304 b1
->s
.k
= 0x01; /* To DS */
2308 * If To DS is set, the SA is at 24.
2310 b0
= gen_bcmp(24, 6, eaddr
);
2314 * Now, check for To DS not set, i.e. check
2315 * "!(link[1] & 0x01)".
2317 s
= gen_load_llrel(1, BPF_B
);
2318 b2
= new_block(JMP(BPF_JSET
));
2319 b2
->s
.k
= 0x01; /* To DS */
2324 * If To DS is not set, the SA is at 16.
2326 b1
= gen_bcmp(16, 6, eaddr
);
2330 * Now OR together the last two checks. That gives
2331 * the complete set of checks for data frames with
2337 * Now check for From DS being set, and AND that with
2338 * the ORed-together checks.
2340 s
= gen_load_llrel(1, BPF_B
);
2341 b1
= new_block(JMP(BPF_JSET
));
2342 b1
->s
.k
= 0x02; /* From DS */
2347 * Now check for data frames with From DS not set.
2349 s
= gen_load_llrel(1, BPF_B
);
2350 b2
= new_block(JMP(BPF_JSET
));
2351 b2
->s
.k
= 0x02; /* From DS */
2356 * If From DS isn't set, the SA is at 10.
2358 b1
= gen_bcmp(10, 6, eaddr
);
2362 * Now OR together the checks for data frames with
2363 * From DS not set and for data frames with From DS
2364 * set; that gives the checks done for data frames.
2369 * Now check for a data frame.
2370 * I.e, check "link[0] & 0x08".
2372 gen_load_llrel(0, BPF_B
);
2373 b1
= new_block(JMP(BPF_JSET
));
2378 * AND that with the checks done for data frames.
2383 * If the high-order bit of the type value is 0, this
2384 * is a management frame.
2385 * I.e, check "!(link[0] & 0x08)".
2387 s
= gen_load_llrel(0, BPF_B
);
2388 b2
= new_block(JMP(BPF_JSET
));
2394 * For management frames, the SA is at 10.
2396 b1
= gen_bcmp(10, 6, eaddr
);
2400 * OR that with the checks done for data frames.
2401 * That gives the checks done for management and
2407 * If the low-order bit of the type value is 1,
2408 * this is either a control frame or a frame
2409 * with a reserved type, and thus not a
2412 * I.e., check "!(link[0] & 0x04)".
2414 s
= gen_load_llrel(0, BPF_B
);
2415 b1
= new_block(JMP(BPF_JSET
));
2421 * AND that with the checks for data and management
2431 * For control frames, there is no DA.
2433 * For management frames, DA is at an
2434 * offset of 4 from the beginning of
2437 * For data frames, DA is at an offset
2438 * of 4 from the beginning of the packet
2439 * if To DS is clear and at an offset of
2440 * 16 from the beginning of the packet
2445 * Generate the tests to be done for data frames.
2447 * First, check for To DS set, i.e. "link[1] & 0x01".
2449 s
= gen_load_llrel(1, BPF_B
);
2450 b1
= new_block(JMP(BPF_JSET
));
2451 b1
->s
.k
= 0x01; /* To DS */
2455 * If To DS is set, the DA is at 16.
2457 b0
= gen_bcmp(16, 6, eaddr
);
2461 * Now, check for To DS not set, i.e. check
2462 * "!(link[1] & 0x01)".
2464 s
= gen_load_llrel(1, BPF_B
);
2465 b2
= new_block(JMP(BPF_JSET
));
2466 b2
->s
.k
= 0x01; /* To DS */
2471 * If To DS is not set, the DA is at 4.
2473 b1
= gen_bcmp(4, 6, eaddr
);
2477 * Now OR together the last two checks. That gives
2478 * the complete set of checks for data frames.
2483 * Now check for a data frame.
2484 * I.e, check "link[0] & 0x08".
2486 s
= gen_load_llrel(0, BPF_B
);
2487 b1
= new_block(JMP(BPF_JSET
));
2492 * AND that with the checks done for data frames.
2497 * If the high-order bit of the type value is 0, this
2498 * is a management frame.
2499 * I.e, check "!(link[0] & 0x08)".
2501 s
= gen_load_llrel(0, BPF_B
);
2502 b2
= new_block(JMP(BPF_JSET
));
2508 * For management frames, the DA is at 4.
2510 b1
= gen_bcmp(4, 6, eaddr
);
2514 * OR that with the checks done for data frames.
2515 * That gives the checks done for management and
2521 * If the low-order bit of the type value is 1,
2522 * this is either a control frame or a frame
2523 * with a reserved type, and thus not a
2526 * I.e., check "!(link[0] & 0x04)".
2528 s
= gen_load_llrel(0, BPF_B
);
2529 b1
= new_block(JMP(BPF_JSET
));
2535 * AND that with the checks for data and management
2542 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2543 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2549 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2550 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2559 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2560 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2561 * as the RFC states.)
2563 static struct block
*
2564 gen_ipfchostop(eaddr
, dir
)
2565 register const u_char
*eaddr
;
2568 register struct block
*b0
, *b1
;
2572 return gen_bcmp(10, 6, eaddr
);
2575 return gen_bcmp(2, 6, eaddr
);
2578 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2579 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2585 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2586 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2595 * This is quite tricky because there may be pad bytes in front of the
2596 * DECNET header, and then there are two possible data packet formats that
2597 * carry both src and dst addresses, plus 5 packet types in a format that
2598 * carries only the src node, plus 2 types that use a different format and
2599 * also carry just the src node.
2603 * Instead of doing those all right, we just look for data packets with
2604 * 0 or 1 bytes of padding. If you want to look at other packets, that
2605 * will require a lot more hacking.
2607 * To add support for filtering on DECNET "areas" (network numbers)
2608 * one would want to add a "mask" argument to this routine. That would
2609 * make the filter even more inefficient, although one could be clever
2610 * and not generate masking instructions if the mask is 0xFFFF.
2612 static struct block
*
2613 gen_dnhostop(addr
, dir
, base_off
)
2618 struct block
*b0
, *b1
, *b2
, *tmp
;
2619 u_int offset_lh
; /* offset if long header is received */
2620 u_int offset_sh
; /* offset if short header is received */
2625 offset_sh
= 1; /* follows flags */
2626 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2630 offset_sh
= 3; /* follows flags, dstnode */
2631 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2635 /* Inefficient because we do our Calvinball dance twice */
2636 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2637 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2643 /* Inefficient because we do our Calvinball dance twice */
2644 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2645 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2650 bpf_error("ISO host filtering not implemented");
2655 b0
= gen_linktype(ETHERTYPE_DN
);
2656 /* Check for pad = 1, long header case */
2657 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2658 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2659 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2660 BPF_H
, (bpf_int32
)ntohs(addr
));
2662 /* Check for pad = 0, long header case */
2663 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2664 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2667 /* Check for pad = 1, short header case */
2668 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2669 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2670 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2671 BPF_H
, (bpf_int32
)ntohs(addr
));
2674 /* Check for pad = 0, short header case */
2675 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2676 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2680 /* Combine with test for linktype */
2685 static struct block
*
2686 gen_host(addr
, mask
, proto
, dir
)
2692 struct block
*b0
, *b1
;
2697 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2698 if (off_linktype
!= (u_int
)-1) {
2699 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2701 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2707 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2708 off_nl
+ 12, off_nl
+ 16);
2711 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2712 off_nl
+ 14, off_nl
+ 24);
2715 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2716 off_nl
+ 14, off_nl
+ 24);
2719 bpf_error("'tcp' modifier applied to host");
2722 bpf_error("'sctp' modifier applied to host");
2725 bpf_error("'udp' modifier applied to host");
2728 bpf_error("'icmp' modifier applied to host");
2731 bpf_error("'igmp' modifier applied to host");
2734 bpf_error("'igrp' modifier applied to host");
2737 bpf_error("'pim' modifier applied to host");
2740 bpf_error("'vrrp' modifier applied to host");
2743 bpf_error("ATALK host filtering not implemented");
2746 bpf_error("AARP host filtering not implemented");
2749 return gen_dnhostop(addr
, dir
, off_nl
);
2752 bpf_error("SCA host filtering not implemented");
2755 bpf_error("LAT host filtering not implemented");
2758 bpf_error("MOPDL host filtering not implemented");
2761 bpf_error("MOPRC host filtering not implemented");
2765 bpf_error("'ip6' modifier applied to ip host");
2768 bpf_error("'icmp6' modifier applied to host");
2772 bpf_error("'ah' modifier applied to host");
2775 bpf_error("'esp' modifier applied to host");
2778 bpf_error("ISO host filtering not implemented");
2781 bpf_error("'esis' modifier applied to host");
2784 bpf_error("'isis' modifier applied to host");
2787 bpf_error("'clnp' modifier applied to host");
2790 bpf_error("'stp' modifier applied to host");
2793 bpf_error("IPX host filtering not implemented");
2796 bpf_error("'netbeui' modifier applied to host");
2805 static struct block
*
2806 gen_host6(addr
, mask
, proto
, dir
)
2807 struct in6_addr
*addr
;
2808 struct in6_addr
*mask
;
2815 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2818 bpf_error("'ip' modifier applied to ip6 host");
2821 bpf_error("'rarp' modifier applied to ip6 host");
2824 bpf_error("'arp' modifier applied to ip6 host");
2827 bpf_error("'sctp' modifier applied to host");
2830 bpf_error("'tcp' modifier applied to host");
2833 bpf_error("'udp' modifier applied to host");
2836 bpf_error("'icmp' modifier applied to host");
2839 bpf_error("'igmp' modifier applied to host");
2842 bpf_error("'igrp' modifier applied to host");
2845 bpf_error("'pim' modifier applied to host");
2848 bpf_error("'vrrp' modifier applied to host");
2851 bpf_error("ATALK host filtering not implemented");
2854 bpf_error("AARP host filtering not implemented");
2857 bpf_error("'decnet' modifier applied to ip6 host");
2860 bpf_error("SCA host filtering not implemented");
2863 bpf_error("LAT host filtering not implemented");
2866 bpf_error("MOPDL host filtering not implemented");
2869 bpf_error("MOPRC host filtering not implemented");
2872 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2873 off_nl
+ 8, off_nl
+ 24);
2876 bpf_error("'icmp6' modifier applied to host");
2879 bpf_error("'ah' modifier applied to host");
2882 bpf_error("'esp' modifier applied to host");
2885 bpf_error("ISO host filtering not implemented");
2888 bpf_error("'esis' modifier applied to host");
2891 bpf_error("'isis' modifier applied to host");
2894 bpf_error("'clnp' modifier applied to host");
2897 bpf_error("'stp' modifier applied to host");
2900 bpf_error("IPX host filtering not implemented");
2903 bpf_error("'netbeui' modifier applied to host");
2913 static struct block
*
2914 gen_gateway(eaddr
, alist
, proto
, dir
)
2915 const u_char
*eaddr
;
2916 bpf_u_int32
**alist
;
2920 struct block
*b0
, *b1
, *tmp
;
2923 bpf_error("direction applied to 'gateway'");
2930 if (linktype
== DLT_EN10MB
)
2931 b0
= gen_ehostop(eaddr
, Q_OR
);
2932 else if (linktype
== DLT_FDDI
)
2933 b0
= gen_fhostop(eaddr
, Q_OR
);
2934 else if (linktype
== DLT_IEEE802
)
2935 b0
= gen_thostop(eaddr
, Q_OR
);
2936 else if (linktype
== DLT_IEEE802_11
)
2937 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2938 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2940 * Check that the packet doesn't begin with an
2941 * LE Control marker. (We've already generated
2944 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2948 * Now check the MAC address.
2950 b0
= gen_ehostop(eaddr
, Q_OR
);
2952 } else if (linktype
== DLT_IP_OVER_FC
)
2953 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2956 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2958 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2960 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2968 bpf_error("illegal modifier of 'gateway'");
2974 gen_proto_abbrev(proto
)
2983 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2985 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2991 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2993 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2999 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
3001 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
3007 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
3010 #ifndef IPPROTO_IGMP
3011 #define IPPROTO_IGMP 2
3015 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
3018 #ifndef IPPROTO_IGRP
3019 #define IPPROTO_IGRP 9
3022 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
3026 #define IPPROTO_PIM 103
3030 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
3032 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
3037 #ifndef IPPROTO_VRRP
3038 #define IPPROTO_VRRP 112
3042 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3046 b1
= gen_linktype(ETHERTYPE_IP
);
3050 b1
= gen_linktype(ETHERTYPE_ARP
);
3054 b1
= gen_linktype(ETHERTYPE_REVARP
);
3058 bpf_error("link layer applied in wrong context");
3061 b1
= gen_linktype(ETHERTYPE_ATALK
);
3065 b1
= gen_linktype(ETHERTYPE_AARP
);
3069 b1
= gen_linktype(ETHERTYPE_DN
);
3073 b1
= gen_linktype(ETHERTYPE_SCA
);
3077 b1
= gen_linktype(ETHERTYPE_LAT
);
3081 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3085 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3090 b1
= gen_linktype(ETHERTYPE_IPV6
);
3093 #ifndef IPPROTO_ICMPV6
3094 #define IPPROTO_ICMPV6 58
3097 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3102 #define IPPROTO_AH 51
3105 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3107 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3113 #define IPPROTO_ESP 50
3116 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3118 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3124 b1
= gen_linktype(LLCSAP_ISONS
);
3128 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3132 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3135 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3136 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3137 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3139 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3141 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3143 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3147 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3148 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3149 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3151 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3153 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3155 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3159 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3160 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3161 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3163 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3168 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3169 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3174 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3175 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3177 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3179 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3184 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3185 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3190 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3191 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3196 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3200 b1
= gen_linktype(LLCSAP_8021D
);
3204 b1
= gen_linktype(LLCSAP_IPX
);
3208 b1
= gen_linktype(LLCSAP_NETBEUI
);
3217 static struct block
*
3224 s
= gen_load_nlrel(6, BPF_H
);
3225 b
= new_block(JMP(BPF_JSET
));
3233 static struct block
*
3234 gen_portatom(off
, v
)
3241 s
= gen_load_ipv4tlrel(off
, BPF_H
);
3243 b
= new_block(JMP(BPF_JEQ
));
3251 static struct block
*
3252 gen_portatom6(off
, v
)
3256 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3261 gen_portop(port
, proto
, dir
)
3262 int port
, proto
, dir
;
3264 struct block
*b0
, *b1
, *tmp
;
3266 /* ip proto 'proto' */
3267 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3273 b1
= gen_portatom(0, (bpf_int32
)port
);
3277 b1
= gen_portatom(2, (bpf_int32
)port
);
3282 tmp
= gen_portatom(0, (bpf_int32
)port
);
3283 b1
= gen_portatom(2, (bpf_int32
)port
);
3288 tmp
= gen_portatom(0, (bpf_int32
)port
);
3289 b1
= gen_portatom(2, (bpf_int32
)port
);
3301 static struct block
*
3302 gen_port(port
, ip_proto
, dir
)
3307 struct block
*b0
, *b1
, *tmp
;
3312 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3313 * not LLC encapsulation with LLCSAP_IP.
3315 * For IEEE 802 networks - which includes 802.5 token ring
3316 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3317 * says that SNAP encapsulation is used, not LLC encapsulation
3320 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3321 * RFC 2225 say that SNAP encapsulation is used, not LLC
3322 * encapsulation with LLCSAP_IP.
3324 * So we always check for ETHERTYPE_IP.
3326 b0
= gen_linktype(ETHERTYPE_IP
);
3332 b1
= gen_portop(port
, ip_proto
, dir
);
3336 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3337 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3339 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3352 gen_portop6(port
, proto
, dir
)
3353 int port
, proto
, dir
;
3355 struct block
*b0
, *b1
, *tmp
;
3357 /* ip6 proto 'proto' */
3358 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3362 b1
= gen_portatom6(0, (bpf_int32
)port
);
3366 b1
= gen_portatom6(2, (bpf_int32
)port
);
3371 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3372 b1
= gen_portatom6(2, (bpf_int32
)port
);
3377 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3378 b1
= gen_portatom6(2, (bpf_int32
)port
);
3390 static struct block
*
3391 gen_port6(port
, ip_proto
, dir
)
3396 struct block
*b0
, *b1
, *tmp
;
3398 /* link proto ip6 */
3399 b0
= gen_linktype(ETHERTYPE_IPV6
);
3405 b1
= gen_portop6(port
, ip_proto
, dir
);
3409 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3410 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3412 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3424 /* gen_portrange code */
3425 static struct block
*
3426 gen_portrangeatom(off
, v1
, v2
)
3430 struct slist
*s1
, *s2
;
3431 struct block
*b1
, *b2
;
3435 * Reverse the order of the ports, so v1 is the lower one.
3444 s1
= gen_load_ipv4tlrel(off
, BPF_H
);
3446 b1
= new_block(JMP(BPF_JGE
));
3450 s2
= gen_load_ipv4tlrel(off
, BPF_H
);
3452 b2
= new_block(JMP(BPF_JGT
));
3463 gen_portrangeop(port1
, port2
, proto
, dir
)
3468 struct block
*b0
, *b1
, *tmp
;
3470 /* ip proto 'proto' */
3471 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3477 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3481 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3486 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3487 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3492 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3493 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3505 static struct block
*
3506 gen_portrange(port1
, port2
, ip_proto
, dir
)
3511 struct block
*b0
, *b1
, *tmp
;
3514 b0
= gen_linktype(ETHERTYPE_IP
);
3520 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3524 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3525 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3527 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3539 static struct block
*
3540 gen_portrangeatom6(off
, v1
, v2
)
3544 struct slist
*s1
, *s2
;
3545 struct block
*b1
, *b2
;
3549 * Reverse the order of the ports, so v1 is the lower one.
3558 s1
= gen_load_nlrel(40 + off
, BPF_H
);
3560 b1
= new_block(JMP(BPF_JGE
));
3564 s2
= gen_load_nlrel(40 + off
, BPF_H
);
3566 b2
= new_block(JMP(BPF_JGT
));
3577 gen_portrangeop6(port1
, port2
, proto
, dir
)
3582 struct block
*b0
, *b1
, *tmp
;
3584 /* ip6 proto 'proto' */
3585 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3589 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3593 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3598 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3599 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3604 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3605 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3617 static struct block
*
3618 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3623 struct block
*b0
, *b1
, *tmp
;
3625 /* link proto ip6 */
3626 b0
= gen_linktype(ETHERTYPE_IPV6
);
3632 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3636 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3637 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3639 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3652 lookup_proto(name
, proto
)
3653 register const char *name
;
3663 v
= pcap_nametoproto(name
);
3664 if (v
== PROTO_UNDEF
)
3665 bpf_error("unknown ip proto '%s'", name
);
3669 /* XXX should look up h/w protocol type based on linktype */
3670 v
= pcap_nametoeproto(name
);
3671 if (v
== PROTO_UNDEF
) {
3672 v
= pcap_nametollc(name
);
3673 if (v
== PROTO_UNDEF
)
3674 bpf_error("unknown ether proto '%s'", name
);
3679 if (strcmp(name
, "esis") == 0)
3681 else if (strcmp(name
, "isis") == 0)
3683 else if (strcmp(name
, "clnp") == 0)
3686 bpf_error("unknown osi proto '%s'", name
);
3706 static struct block
*
3707 gen_protochain(v
, proto
, dir
)
3712 #ifdef NO_PROTOCHAIN
3713 return gen_proto(v
, proto
, dir
);
3715 struct block
*b0
, *b
;
3716 struct slist
*s
[100];
3717 int fix2
, fix3
, fix4
, fix5
;
3718 int ahcheck
, again
, end
;
3720 int reg2
= alloc_reg();
3722 memset(s
, 0, sizeof(s
));
3723 fix2
= fix3
= fix4
= fix5
= 0;
3730 b0
= gen_protochain(v
, Q_IP
, dir
);
3731 b
= gen_protochain(v
, Q_IPV6
, dir
);
3735 bpf_error("bad protocol applied for 'protochain'");
3739 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3742 * s[0] is a dummy entry to protect other BPF insn from damage
3743 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3744 * hard to find interdependency made by jump table fixup.
3747 s
[i
] = new_stmt(0); /*dummy*/
3752 b0
= gen_linktype(ETHERTYPE_IP
);
3755 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3756 s
[i
]->s
.k
= off_nl
+ 9;
3758 /* X = ip->ip_hl << 2 */
3759 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3765 b0
= gen_linktype(ETHERTYPE_IPV6
);
3767 /* A = ip6->ip_nxt */
3768 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3769 s
[i
]->s
.k
= off_nl
+ 6;
3771 /* X = sizeof(struct ip6_hdr) */
3772 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3778 bpf_error("unsupported proto to gen_protochain");
3782 /* again: if (A == v) goto end; else fall through; */
3784 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3786 s
[i
]->s
.jt
= NULL
; /*later*/
3787 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3791 #ifndef IPPROTO_NONE
3792 #define IPPROTO_NONE 59
3794 /* if (A == IPPROTO_NONE) goto end */
3795 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3796 s
[i
]->s
.jt
= NULL
; /*later*/
3797 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3798 s
[i
]->s
.k
= IPPROTO_NONE
;
3799 s
[fix5
]->s
.jf
= s
[i
];
3804 if (proto
== Q_IPV6
) {
3805 int v6start
, v6end
, v6advance
, j
;
3808 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3809 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3810 s
[i
]->s
.jt
= NULL
; /*later*/
3811 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3812 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3813 s
[fix2
]->s
.jf
= s
[i
];
3815 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3816 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3817 s
[i
]->s
.jt
= NULL
; /*later*/
3818 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3819 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3821 /* if (A == IPPROTO_ROUTING) goto v6advance */
3822 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3823 s
[i
]->s
.jt
= NULL
; /*later*/
3824 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3825 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3827 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3828 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3829 s
[i
]->s
.jt
= NULL
; /*later*/
3830 s
[i
]->s
.jf
= NULL
; /*later*/
3831 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3842 * X = X + (P[X + 1] + 1) * 8;
3845 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3847 /* A = P[X + packet head] */
3848 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3852 s
[i
] = new_stmt(BPF_ST
);
3856 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3859 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3863 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3865 /* A = P[X + packet head]; */
3866 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3870 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3874 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3878 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3881 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3885 /* goto again; (must use BPF_JA for backward jump) */
3886 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3887 s
[i
]->s
.k
= again
- i
- 1;
3888 s
[i
- 1]->s
.jf
= s
[i
];
3892 for (j
= v6start
; j
<= v6end
; j
++)
3893 s
[j
]->s
.jt
= s
[v6advance
];
3898 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3900 s
[fix2
]->s
.jf
= s
[i
];
3906 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3907 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3908 s
[i
]->s
.jt
= NULL
; /*later*/
3909 s
[i
]->s
.jf
= NULL
; /*later*/
3910 s
[i
]->s
.k
= IPPROTO_AH
;
3912 s
[fix3
]->s
.jf
= s
[ahcheck
];
3919 * X = X + (P[X + 1] + 2) * 4;
3922 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3924 /* A = P[X + packet head]; */
3925 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3929 s
[i
] = new_stmt(BPF_ST
);
3933 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3936 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3940 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3942 /* A = P[X + packet head] */
3943 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3947 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3951 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3955 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3958 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3962 /* goto again; (must use BPF_JA for backward jump) */
3963 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3964 s
[i
]->s
.k
= again
- i
- 1;
3969 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3971 s
[fix2
]->s
.jt
= s
[end
];
3972 s
[fix4
]->s
.jf
= s
[end
];
3973 s
[fix5
]->s
.jt
= s
[end
];
3980 for (i
= 0; i
< max
- 1; i
++)
3981 s
[i
]->next
= s
[i
+ 1];
3982 s
[max
- 1]->next
= NULL
;
3987 b
= new_block(JMP(BPF_JEQ
));
3988 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3999 * Generate code that checks whether the packet is a packet for protocol
4000 * <proto> and whether the type field in that protocol's header has
4001 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
4002 * IP packet and checks the protocol number in the IP header against <v>.
4004 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
4005 * against Q_IP and Q_IPV6.
4007 static struct block
*
4008 gen_proto(v
, proto
, dir
)
4013 struct block
*b0
, *b1
;
4015 if (dir
!= Q_DEFAULT
)
4016 bpf_error("direction applied to 'proto'");
4021 b0
= gen_proto(v
, Q_IP
, dir
);
4022 b1
= gen_proto(v
, Q_IPV6
, dir
);
4030 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4031 * not LLC encapsulation with LLCSAP_IP.
4033 * For IEEE 802 networks - which includes 802.5 token ring
4034 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4035 * says that SNAP encapsulation is used, not LLC encapsulation
4038 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4039 * RFC 2225 say that SNAP encapsulation is used, not LLC
4040 * encapsulation with LLCSAP_IP.
4042 * So we always check for ETHERTYPE_IP.
4044 b0
= gen_linktype(ETHERTYPE_IP
);
4046 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
4048 b1
= gen_protochain(v
, Q_IP
);
4058 * Frame Relay packets typically have an OSI
4059 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4060 * generates code to check for all the OSI
4061 * NLPIDs, so calling it and then adding a check
4062 * for the particular NLPID for which we're
4063 * looking is bogus, as we can just check for
4066 * What we check for is the NLPID and a frame
4067 * control field value of UI, i.e. 0x03 followed
4070 * XXX - assumes a 2-byte Frame Relay header with
4071 * DLCI and flags. What if the address is longer?
4073 * XXX - what about SNAP-encapsulated frames?
4075 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
4081 * Cisco uses an Ethertype lookalike - for OSI,
4084 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4085 /* OSI in C-HDLC is stuffed with a fudge byte */
4086 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
4091 b0
= gen_linktype(LLCSAP_ISONS
);
4092 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
4098 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4100 * 4 is the offset of the PDU type relative to the IS-IS
4103 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
4108 bpf_error("arp does not encapsulate another protocol");
4112 bpf_error("rarp does not encapsulate another protocol");
4116 bpf_error("atalk encapsulation is not specifiable");
4120 bpf_error("decnet encapsulation is not specifiable");
4124 bpf_error("sca does not encapsulate another protocol");
4128 bpf_error("lat does not encapsulate another protocol");
4132 bpf_error("moprc does not encapsulate another protocol");
4136 bpf_error("mopdl does not encapsulate another protocol");
4140 return gen_linktype(v
);
4143 bpf_error("'udp proto' is bogus");
4147 bpf_error("'tcp proto' is bogus");
4151 bpf_error("'sctp proto' is bogus");
4155 bpf_error("'icmp proto' is bogus");
4159 bpf_error("'igmp proto' is bogus");
4163 bpf_error("'igrp proto' is bogus");
4167 bpf_error("'pim proto' is bogus");
4171 bpf_error("'vrrp proto' is bogus");
4176 b0
= gen_linktype(ETHERTYPE_IPV6
);
4178 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
4180 b1
= gen_protochain(v
, Q_IPV6
);
4186 bpf_error("'icmp6 proto' is bogus");
4190 bpf_error("'ah proto' is bogus");
4193 bpf_error("'ah proto' is bogus");
4196 bpf_error("'stp proto' is bogus");
4199 bpf_error("'ipx proto' is bogus");
4202 bpf_error("'netbeui proto' is bogus");
4213 register const char *name
;
4216 int proto
= q
.proto
;
4220 bpf_u_int32 mask
, addr
;
4222 bpf_u_int32
**alist
;
4225 struct sockaddr_in
*sin
;
4226 struct sockaddr_in6
*sin6
;
4227 struct addrinfo
*res
, *res0
;
4228 struct in6_addr mask128
;
4230 struct block
*b
, *tmp
;
4231 int port
, real_proto
;
4237 addr
= pcap_nametonetaddr(name
);
4239 bpf_error("unknown network '%s'", name
);
4240 /* Left justify network addr and calculate its network mask */
4242 while (addr
&& (addr
& 0xff000000) == 0) {
4246 return gen_host(addr
, mask
, proto
, dir
);
4250 if (proto
== Q_LINK
) {
4254 eaddr
= pcap_ether_hostton(name
);
4257 "unknown ether host '%s'", name
);
4258 b
= gen_ehostop(eaddr
, dir
);
4263 eaddr
= pcap_ether_hostton(name
);
4266 "unknown FDDI host '%s'", name
);
4267 b
= gen_fhostop(eaddr
, dir
);
4272 eaddr
= pcap_ether_hostton(name
);
4275 "unknown token ring host '%s'", name
);
4276 b
= gen_thostop(eaddr
, dir
);
4280 case DLT_IEEE802_11
:
4281 eaddr
= pcap_ether_hostton(name
);
4284 "unknown 802.11 host '%s'", name
);
4285 b
= gen_wlanhostop(eaddr
, dir
);
4289 case DLT_IP_OVER_FC
:
4290 eaddr
= pcap_ether_hostton(name
);
4293 "unknown Fibre Channel host '%s'", name
);
4294 b
= gen_ipfchostop(eaddr
, dir
);
4303 * Check that the packet doesn't begin
4304 * with an LE Control marker. (We've
4305 * already generated a test for LANE.)
4307 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4311 eaddr
= pcap_ether_hostton(name
);
4314 "unknown ether host '%s'", name
);
4315 b
= gen_ehostop(eaddr
, dir
);
4321 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4322 } else if (proto
== Q_DECNET
) {
4323 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4325 * I don't think DECNET hosts can be multihomed, so
4326 * there is no need to build up a list of addresses
4328 return (gen_host(dn_addr
, 0, proto
, dir
));
4331 alist
= pcap_nametoaddr(name
);
4332 if (alist
== NULL
|| *alist
== NULL
)
4333 bpf_error("unknown host '%s'", name
);
4335 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4337 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4339 tmp
= gen_host(**alist
++, 0xffffffff,
4346 memset(&mask128
, 0xff, sizeof(mask128
));
4347 res0
= res
= pcap_nametoaddrinfo(name
);
4349 bpf_error("unknown host '%s'", name
);
4351 tproto
= tproto6
= proto
;
4352 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4356 for (res
= res0
; res
; res
= res
->ai_next
) {
4357 switch (res
->ai_family
) {
4359 if (tproto
== Q_IPV6
)
4362 sin
= (struct sockaddr_in
*)
4364 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4365 0xffffffff, tproto
, dir
);
4368 if (tproto6
== Q_IP
)
4371 sin6
= (struct sockaddr_in6
*)
4373 tmp
= gen_host6(&sin6
->sin6_addr
,
4374 &mask128
, tproto6
, dir
);
4385 bpf_error("unknown host '%s'%s", name
,
4386 (proto
== Q_DEFAULT
)
4388 : " for specified address family");
4395 if (proto
!= Q_DEFAULT
&&
4396 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4397 bpf_error("illegal qualifier of 'port'");
4398 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4399 bpf_error("unknown port '%s'", name
);
4400 if (proto
== Q_UDP
) {
4401 if (real_proto
== IPPROTO_TCP
)
4402 bpf_error("port '%s' is tcp", name
);
4403 else if (real_proto
== IPPROTO_SCTP
)
4404 bpf_error("port '%s' is sctp", name
);
4406 /* override PROTO_UNDEF */
4407 real_proto
= IPPROTO_UDP
;
4409 if (proto
== Q_TCP
) {
4410 if (real_proto
== IPPROTO_UDP
)
4411 bpf_error("port '%s' is udp", name
);
4413 else if (real_proto
== IPPROTO_SCTP
)
4414 bpf_error("port '%s' is sctp", name
);
4416 /* override PROTO_UNDEF */
4417 real_proto
= IPPROTO_TCP
;
4419 if (proto
== Q_SCTP
) {
4420 if (real_proto
== IPPROTO_UDP
)
4421 bpf_error("port '%s' is udp", name
);
4423 else if (real_proto
== IPPROTO_TCP
)
4424 bpf_error("port '%s' is tcp", name
);
4426 /* override PROTO_UNDEF */
4427 real_proto
= IPPROTO_SCTP
;
4430 return gen_port(port
, real_proto
, dir
);
4434 b
= gen_port(port
, real_proto
, dir
);
4435 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4441 if (proto
!= Q_DEFAULT
&&
4442 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4443 bpf_error("illegal qualifier of 'portrange'");
4444 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4445 bpf_error("unknown port in range '%s'", name
);
4446 if (proto
== Q_UDP
) {
4447 if (real_proto
== IPPROTO_TCP
)
4448 bpf_error("port in range '%s' is tcp", name
);
4449 else if (real_proto
== IPPROTO_SCTP
)
4450 bpf_error("port in range '%s' is sctp", name
);
4452 /* override PROTO_UNDEF */
4453 real_proto
= IPPROTO_UDP
;
4455 if (proto
== Q_TCP
) {
4456 if (real_proto
== IPPROTO_UDP
)
4457 bpf_error("port in range '%s' is udp", name
);
4458 else if (real_proto
== IPPROTO_SCTP
)
4459 bpf_error("port in range '%s' is sctp", name
);
4461 /* override PROTO_UNDEF */
4462 real_proto
= IPPROTO_TCP
;
4464 if (proto
== Q_SCTP
) {
4465 if (real_proto
== IPPROTO_UDP
)
4466 bpf_error("port in range '%s' is udp", name
);
4467 else if (real_proto
== IPPROTO_TCP
)
4468 bpf_error("port in range '%s' is tcp", name
);
4470 /* override PROTO_UNDEF */
4471 real_proto
= IPPROTO_SCTP
;
4474 return gen_portrange(port1
, port2
, real_proto
, dir
);
4478 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4479 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4486 eaddr
= pcap_ether_hostton(name
);
4488 bpf_error("unknown ether host: %s", name
);
4490 alist
= pcap_nametoaddr(name
);
4491 if (alist
== NULL
|| *alist
== NULL
)
4492 bpf_error("unknown host '%s'", name
);
4493 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4497 bpf_error("'gateway' not supported in this configuration");
4501 real_proto
= lookup_proto(name
, proto
);
4502 if (real_proto
>= 0)
4503 return gen_proto(real_proto
, proto
, dir
);
4505 bpf_error("unknown protocol: %s", name
);
4508 real_proto
= lookup_proto(name
, proto
);
4509 if (real_proto
>= 0)
4510 return gen_protochain(real_proto
, proto
, dir
);
4512 bpf_error("unknown protocol: %s", name
);
4524 gen_mcode(s1
, s2
, masklen
, q
)
4525 register const char *s1
, *s2
;
4526 register int masklen
;
4529 register int nlen
, mlen
;
4532 nlen
= __pcap_atoin(s1
, &n
);
4533 /* Promote short ipaddr */
4537 mlen
= __pcap_atoin(s2
, &m
);
4538 /* Promote short ipaddr */
4541 bpf_error("non-network bits set in \"%s mask %s\"",
4544 /* Convert mask len to mask */
4546 bpf_error("mask length must be <= 32");
4547 m
= 0xffffffff << (32 - masklen
);
4549 bpf_error("non-network bits set in \"%s/%d\"",
4556 return gen_host(n
, m
, q
.proto
, q
.dir
);
4559 bpf_error("Mask syntax for networks only");
4567 register const char *s
;
4572 int proto
= q
.proto
;
4578 else if (q
.proto
== Q_DECNET
)
4579 vlen
= __pcap_atodn(s
, &v
);
4581 vlen
= __pcap_atoin(s
, &v
);
4588 if (proto
== Q_DECNET
)
4589 return gen_host(v
, 0, proto
, dir
);
4590 else if (proto
== Q_LINK
) {
4591 bpf_error("illegal link layer address");
4594 if (s
== NULL
&& q
.addr
== Q_NET
) {
4595 /* Promote short net number */
4596 while (v
&& (v
& 0xff000000) == 0) {
4601 /* Promote short ipaddr */
4605 return gen_host(v
, mask
, proto
, dir
);
4610 proto
= IPPROTO_UDP
;
4611 else if (proto
== Q_TCP
)
4612 proto
= IPPROTO_TCP
;
4613 else if (proto
== Q_SCTP
)
4614 proto
= IPPROTO_SCTP
;
4615 else if (proto
== Q_DEFAULT
)
4616 proto
= PROTO_UNDEF
;
4618 bpf_error("illegal qualifier of 'port'");
4621 return gen_port((int)v
, proto
, dir
);
4625 b
= gen_port((int)v
, proto
, dir
);
4626 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4633 proto
= IPPROTO_UDP
;
4634 else if (proto
== Q_TCP
)
4635 proto
= IPPROTO_TCP
;
4636 else if (proto
== Q_SCTP
)
4637 proto
= IPPROTO_SCTP
;
4638 else if (proto
== Q_DEFAULT
)
4639 proto
= PROTO_UNDEF
;
4641 bpf_error("illegal qualifier of 'portrange'");
4644 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4648 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4649 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4655 bpf_error("'gateway' requires a name");
4659 return gen_proto((int)v
, proto
, dir
);
4662 return gen_protochain((int)v
, proto
, dir
);
4677 gen_mcode6(s1
, s2
, masklen
, q
)
4678 register const char *s1
, *s2
;
4679 register int masklen
;
4682 struct addrinfo
*res
;
4683 struct in6_addr
*addr
;
4684 struct in6_addr mask
;
4689 bpf_error("no mask %s supported", s2
);
4691 res
= pcap_nametoaddrinfo(s1
);
4693 bpf_error("invalid ip6 address %s", s1
);
4695 bpf_error("%s resolved to multiple address", s1
);
4696 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4698 if (sizeof(mask
) * 8 < masklen
)
4699 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4700 memset(&mask
, 0, sizeof(mask
));
4701 memset(&mask
, 0xff, masklen
/ 8);
4703 mask
.s6_addr
[masklen
/ 8] =
4704 (0xff << (8 - masklen
% 8)) & 0xff;
4707 a
= (u_int32_t
*)addr
;
4708 m
= (u_int32_t
*)&mask
;
4709 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4710 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4711 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4719 bpf_error("Mask syntax for networks only");
4723 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4728 bpf_error("invalid qualifier against IPv6 address");
4736 register const u_char
*eaddr
;
4739 struct block
*b
, *tmp
;
4741 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4742 if (linktype
== DLT_EN10MB
)
4743 return gen_ehostop(eaddr
, (int)q
.dir
);
4744 if (linktype
== DLT_FDDI
)
4745 return gen_fhostop(eaddr
, (int)q
.dir
);
4746 if (linktype
== DLT_IEEE802
)
4747 return gen_thostop(eaddr
, (int)q
.dir
);
4748 if (linktype
== DLT_IEEE802_11
)
4749 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4750 if (linktype
== DLT_SUNATM
&& is_lane
) {
4752 * Check that the packet doesn't begin with an
4753 * LE Control marker. (We've already generated
4756 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4760 * Now check the MAC address.
4762 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4766 if (linktype
== DLT_IP_OVER_FC
)
4767 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4768 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4770 bpf_error("ethernet address used in non-ether expression");
4776 struct slist
*s0
, *s1
;
4779 * This is definitely not the best way to do this, but the
4780 * lists will rarely get long.
4787 static struct slist
*
4793 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4798 static struct slist
*
4804 s
= new_stmt(BPF_LD
|BPF_MEM
);
4810 gen_load(proto
, index
, size
)
4815 struct slist
*s
, *tmp
;
4817 int regno
= alloc_reg();
4819 free_reg(index
->regno
);
4823 bpf_error("data size must be 1, 2, or 4");
4839 bpf_error("unsupported index operation");
4843 * XXX - what about ATM LANE? Should the index be
4844 * relative to the beginning of the AAL5 frame, so
4845 * that 0 refers to the beginning of the LE Control
4846 * field, or relative to the beginning of the LAN
4847 * frame, so that 0 refers, for Ethernet LANE, to
4848 * the beginning of the destination address?
4850 s
= xfer_to_x(index
);
4851 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4853 sappend(index
->s
, s
);
4868 /* XXX Note that we assume a fixed link header here. */
4869 s
= xfer_to_x(index
);
4870 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4873 sappend(index
->s
, s
);
4875 b
= gen_proto_abbrev(proto
);
4877 gen_and(index
->b
, b
);
4889 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4891 sappend(s
, xfer_to_a(index
));
4892 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4893 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4894 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4896 sappend(index
->s
, s
);
4898 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4900 gen_and(index
->b
, b
);
4902 gen_and(gen_proto_abbrev(Q_IP
), b
);
4908 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4912 index
->regno
= regno
;
4913 s
= new_stmt(BPF_ST
);
4915 sappend(index
->s
, s
);
4921 gen_relation(code
, a0
, a1
, reversed
)
4923 struct arth
*a0
, *a1
;
4926 struct slist
*s0
, *s1
, *s2
;
4927 struct block
*b
, *tmp
;
4931 if (code
== BPF_JEQ
) {
4932 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4933 b
= new_block(JMP(code
));
4937 b
= new_block(BPF_JMP
|code
|BPF_X
);
4943 sappend(a0
->s
, a1
->s
);
4947 free_reg(a0
->regno
);
4948 free_reg(a1
->regno
);
4950 /* 'and' together protocol checks */
4953 gen_and(a0
->b
, tmp
= a1
->b
);
4969 int regno
= alloc_reg();
4970 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4973 s
= new_stmt(BPF_LD
|BPF_LEN
);
4974 s
->next
= new_stmt(BPF_ST
);
4975 s
->next
->s
.k
= regno
;
4990 a
= (struct arth
*)newchunk(sizeof(*a
));
4994 s
= new_stmt(BPF_LD
|BPF_IMM
);
4996 s
->next
= new_stmt(BPF_ST
);
5012 s
= new_stmt(BPF_ALU
|BPF_NEG
);
5015 s
= new_stmt(BPF_ST
);
5023 gen_arth(code
, a0
, a1
)
5025 struct arth
*a0
, *a1
;
5027 struct slist
*s0
, *s1
, *s2
;
5031 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5036 sappend(a0
->s
, a1
->s
);
5038 free_reg(a0
->regno
);
5039 free_reg(a1
->regno
);
5041 s0
= new_stmt(BPF_ST
);
5042 a0
->regno
= s0
->s
.k
= alloc_reg();
5049 * Here we handle simple allocation of the scratch registers.
5050 * If too many registers are alloc'd, the allocator punts.
5052 static int regused
[BPF_MEMWORDS
];
5056 * Return the next free register.
5061 int n
= BPF_MEMWORDS
;
5064 if (regused
[curreg
])
5065 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5067 regused
[curreg
] = 1;
5071 bpf_error("too many registers needed to evaluate expression");
5076 * Return a register to the table so it can
5086 static struct block
*
5093 s
= new_stmt(BPF_LD
|BPF_LEN
);
5094 b
= new_block(JMP(jmp
));
5105 return gen_len(BPF_JGE
, n
);
5109 * Actually, this is less than or equal.
5117 b
= gen_len(BPF_JGT
, n
);
5124 gen_byteop(op
, idx
, val
)
5135 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5138 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5139 b
->s
.code
= JMP(BPF_JGE
);
5144 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5145 b
->s
.code
= JMP(BPF_JGT
);
5149 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5153 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5157 b
= new_block(JMP(BPF_JEQ
));
5164 static u_char abroadcast
[] = { 0x0 };
5167 gen_broadcast(proto
)
5170 bpf_u_int32 hostmask
;
5171 struct block
*b0
, *b1
, *b2
;
5172 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5178 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5179 return gen_ahostop(abroadcast
, Q_DST
);
5180 if (linktype
== DLT_EN10MB
)
5181 return gen_ehostop(ebroadcast
, Q_DST
);
5182 if (linktype
== DLT_FDDI
)
5183 return gen_fhostop(ebroadcast
, Q_DST
);
5184 if (linktype
== DLT_IEEE802
)
5185 return gen_thostop(ebroadcast
, Q_DST
);
5186 if (linktype
== DLT_IEEE802_11
)
5187 return gen_wlanhostop(ebroadcast
, Q_DST
);
5188 if (linktype
== DLT_IP_OVER_FC
)
5189 return gen_ipfchostop(ebroadcast
, Q_DST
);
5190 if (linktype
== DLT_SUNATM
&& is_lane
) {
5192 * Check that the packet doesn't begin with an
5193 * LE Control marker. (We've already generated
5196 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5200 * Now check the MAC address.
5202 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5206 bpf_error("not a broadcast link");
5210 b0
= gen_linktype(ETHERTYPE_IP
);
5211 hostmask
= ~netmask
;
5212 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
5213 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
5214 (bpf_int32
)(~0 & hostmask
), hostmask
);
5219 bpf_error("only link-layer/IP broadcast filters supported");
5224 * Generate code to test the low-order bit of a MAC address (that's
5225 * the bottom bit of the *first* byte).
5227 static struct block
*
5228 gen_mac_multicast(offset
)
5231 register struct block
*b0
;
5232 register struct slist
*s
;
5234 /* link[offset] & 1 != 0 */
5235 s
= gen_load_llrel(offset
, BPF_B
);
5236 b0
= new_block(JMP(BPF_JSET
));
5243 gen_multicast(proto
)
5246 register struct block
*b0
, *b1
, *b2
;
5247 register struct slist
*s
;
5253 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5254 /* all ARCnet multicasts use the same address */
5255 return gen_ahostop(abroadcast
, Q_DST
);
5257 if (linktype
== DLT_EN10MB
) {
5258 /* ether[0] & 1 != 0 */
5259 return gen_mac_multicast(0);
5262 if (linktype
== DLT_FDDI
) {
5264 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5266 * XXX - was that referring to bit-order issues?
5268 /* fddi[1] & 1 != 0 */
5269 return gen_mac_multicast(1);
5272 if (linktype
== DLT_IEEE802
) {
5273 /* tr[2] & 1 != 0 */
5274 return gen_mac_multicast(2);
5277 if (linktype
== DLT_IEEE802_11
) {
5281 * For control frames, there is no DA.
5283 * For management frames, DA is at an
5284 * offset of 4 from the beginning of
5287 * For data frames, DA is at an offset
5288 * of 4 from the beginning of the packet
5289 * if To DS is clear and at an offset of
5290 * 16 from the beginning of the packet
5295 * Generate the tests to be done for data frames.
5297 * First, check for To DS set, i.e. "link[1] & 0x01".
5299 s
= gen_load_llrel(1, BPF_B
);
5300 b1
= new_block(JMP(BPF_JSET
));
5301 b1
->s
.k
= 0x01; /* To DS */
5305 * If To DS is set, the DA is at 16.
5307 b0
= gen_mac_multicast(16);
5311 * Now, check for To DS not set, i.e. check
5312 * "!(link[1] & 0x01)".
5314 s
= gen_load_llrel(1, BPF_B
);
5315 b2
= new_block(JMP(BPF_JSET
));
5316 b2
->s
.k
= 0x01; /* To DS */
5321 * If To DS is not set, the DA is at 4.
5323 b1
= gen_mac_multicast(4);
5327 * Now OR together the last two checks. That gives
5328 * the complete set of checks for data frames.
5333 * Now check for a data frame.
5334 * I.e, check "link[0] & 0x08".
5336 s
= gen_load_llrel(0, BPF_B
);
5337 b1
= new_block(JMP(BPF_JSET
));
5342 * AND that with the checks done for data frames.
5347 * If the high-order bit of the type value is 0, this
5348 * is a management frame.
5349 * I.e, check "!(link[0] & 0x08)".
5351 s
= gen_load_llrel(0, BPF_B
);
5352 b2
= new_block(JMP(BPF_JSET
));
5358 * For management frames, the DA is at 4.
5360 b1
= gen_mac_multicast(4);
5364 * OR that with the checks done for data frames.
5365 * That gives the checks done for management and
5371 * If the low-order bit of the type value is 1,
5372 * this is either a control frame or a frame
5373 * with a reserved type, and thus not a
5376 * I.e., check "!(link[0] & 0x04)".
5378 s
= gen_load_llrel(0, BPF_B
);
5379 b1
= new_block(JMP(BPF_JSET
));
5385 * AND that with the checks for data and management
5392 if (linktype
== DLT_IP_OVER_FC
) {
5393 b0
= gen_mac_multicast(2);
5397 if (linktype
== DLT_SUNATM
&& is_lane
) {
5399 * Check that the packet doesn't begin with an
5400 * LE Control marker. (We've already generated
5403 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5406 /* ether[off_mac] & 1 != 0 */
5407 b0
= gen_mac_multicast(off_mac
);
5412 /* Link not known to support multicasts */
5416 b0
= gen_linktype(ETHERTYPE_IP
);
5417 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5418 b1
->s
.code
= JMP(BPF_JGE
);
5424 b0
= gen_linktype(ETHERTYPE_IPV6
);
5425 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5430 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5435 * generate command for inbound/outbound. It's here so we can
5436 * make it link-type specific. 'dir' = 0 implies "inbound",
5437 * = 1 implies "outbound".
5443 register struct block
*b0
;
5446 * Only some data link types support inbound/outbound qualifiers.
5450 b0
= gen_relation(BPF_JEQ
,
5451 gen_load(Q_LINK
, gen_loadi(0), 1),
5459 * Match packets sent by this machine.
5461 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5464 * Match packets sent to this machine.
5465 * (No broadcast or multicast packets, or
5466 * packets sent to some other machine and
5467 * received promiscuously.)
5469 * XXX - packets sent to other machines probably
5470 * shouldn't be matched, but what about broadcast
5471 * or multicast packets we received?
5473 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5478 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5479 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5484 /* match outgoing packets */
5485 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5487 /* match incoming packets */
5488 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5492 case DLT_JUNIPER_MLFR
:
5493 case DLT_JUNIPER_MLPPP
:
5494 case DLT_JUNIPER_ATM1
:
5495 case DLT_JUNIPER_ATM2
:
5496 /* juniper flags (including direction) are stored
5497 * the byte after the 3-byte magic number */
5499 /* match outgoing packets */
5500 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5502 /* match incoming packets */
5503 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5508 bpf_error("inbound/outbound not supported on linktype %d",
5516 /* PF firewall log matched interface */
5518 gen_pf_ifname(const char *ifname
)
5523 if (linktype
== DLT_PFLOG
) {
5524 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5525 off
= offsetof(struct pfloghdr
, ifname
);
5527 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5530 if (strlen(ifname
) >= len
) {
5531 bpf_error("ifname interface names can only be %d characters",
5535 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5539 /* PF firewall log matched interface */
5541 gen_pf_ruleset(char *ruleset
)
5545 if (linktype
!= DLT_PFLOG
) {
5546 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5549 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5550 bpf_error("ruleset names can only be %ld characters",
5551 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5554 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5555 strlen(ruleset
), (const u_char
*)ruleset
);
5559 /* PF firewall log rule number */
5565 if (linktype
== DLT_PFLOG
) {
5566 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5569 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5576 /* PF firewall log sub-rule number */
5578 gen_pf_srnr(int srnr
)
5582 if (linktype
!= DLT_PFLOG
) {
5583 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5587 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5592 /* PF firewall log reason code */
5594 gen_pf_reason(int reason
)
5598 if (linktype
== DLT_PFLOG
) {
5599 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5602 bpf_error("reason not supported on linktype 0x%x", linktype
);
5609 /* PF firewall log action */
5611 gen_pf_action(int action
)
5615 if (linktype
== DLT_PFLOG
) {
5616 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5619 bpf_error("action not supported on linktype 0x%x", linktype
);
5628 register const u_char
*eaddr
;
5631 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5632 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5633 return gen_ahostop(eaddr
, (int)q
.dir
);
5635 bpf_error("ARCnet address used in non-arc expression");
5639 static struct block
*
5640 gen_ahostop(eaddr
, dir
)
5641 register const u_char
*eaddr
;
5644 register struct block
*b0
, *b1
;
5647 /* src comes first, different from Ethernet */
5649 return gen_bcmp(0, 1, eaddr
);
5652 return gen_bcmp(1, 1, eaddr
);
5655 b0
= gen_ahostop(eaddr
, Q_SRC
);
5656 b1
= gen_ahostop(eaddr
, Q_DST
);
5662 b0
= gen_ahostop(eaddr
, Q_SRC
);
5663 b1
= gen_ahostop(eaddr
, Q_DST
);
5672 * support IEEE 802.1Q VLAN trunk over ethernet
5681 * Change the offsets to point to the type and data fields within
5682 * the VLAN packet. Just increment the offsets, so that we
5683 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5684 * capture VLAN 200 encapsulated within VLAN 100.
5686 * XXX - this is a bit of a kludge. If we were to split the
5687 * compiler into a parser that parses an expression and
5688 * generates an expression tree, and a code generator that
5689 * takes an expression tree (which could come from our
5690 * parser or from some other parser) and generates BPF code,
5691 * we could perhaps make the offsets parameters of routines
5692 * and, in the handler for an "AND" node, pass to subnodes
5693 * other than the VLAN node the adjusted offsets.
5695 * This would mean that "vlan" would, instead of changing the
5696 * behavior of *all* tests after it, change only the behavior
5697 * of tests ANDed with it. That would change the documented
5698 * semantics of "vlan", which might break some expressions.
5699 * However, it would mean that "(vlan and ip) or ip" would check
5700 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5701 * checking only for VLAN-encapsulated IP, so that could still
5702 * be considered worth doing; it wouldn't break expressions
5703 * that are of the form "vlan and ..." or "vlan N and ...",
5704 * which I suspect are the most common expressions involving
5705 * "vlan". "vlan or ..." doesn't necessarily do what the user
5706 * would really want, now, as all the "or ..." tests would
5707 * be done assuming a VLAN, even though the "or" could be viewed
5708 * as meaning "or, if this isn't a VLAN packet...".
5710 orig_linktype
= off_linktype
; /* save original values */
5722 bpf_error("no VLAN support for data link type %d",
5727 /* check for VLAN */
5728 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5730 /* If a specific VLAN is requested, check VLAN id */
5731 if (vlan_num
>= 0) {
5734 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5752 * Change the offsets to point to the type and data fields within
5753 * the MPLS packet. Just increment the offsets, so that we
5754 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5755 * capture packets with an outer label of 100000 and an inner
5758 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5760 orig_linktype
= off_linktype
; /* save original values */
5765 case DLT_C_HDLC
: /* fall through */
5771 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5779 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5782 /* FIXME add other DLT_s ...
5783 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5784 * leave it for now */
5787 bpf_error("no MPLS support for data link type %d",
5794 /* If a specific MPLS label is requested, check it */
5795 if (label_num
>= 0) {
5798 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5799 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5808 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5820 bpf_error("'vpi' supported only on raw ATM");
5821 if (off_vpi
== (u_int
)-1)
5823 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5824 (u_int
)jvalue
, reverse
);
5829 bpf_error("'vci' supported only on raw ATM");
5830 if (off_vci
== (u_int
)-1)
5832 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5833 (u_int
)jvalue
, reverse
);
5837 if (off_proto
== (u_int
)-1)
5838 abort(); /* XXX - this isn't on FreeBSD */
5839 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5840 (u_int
)jvalue
, reverse
);
5844 if (off_payload
== (u_int
)-1)
5846 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5847 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5852 bpf_error("'callref' supported only on raw ATM");
5853 if (off_proto
== (u_int
)-1)
5855 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5856 (u_int
)jvalue
, reverse
);
5866 gen_atmtype_abbrev(type
)
5869 struct block
*b0
, *b1
;
5874 /* Get all packets in Meta signalling Circuit */
5876 bpf_error("'metac' supported only on raw ATM");
5877 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5878 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5883 /* Get all packets in Broadcast Circuit*/
5885 bpf_error("'bcc' supported only on raw ATM");
5886 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5887 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5892 /* Get all cells in Segment OAM F4 circuit*/
5894 bpf_error("'oam4sc' supported only on raw ATM");
5895 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5896 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5901 /* Get all cells in End-to-End OAM F4 Circuit*/
5903 bpf_error("'oam4ec' supported only on raw ATM");
5904 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5905 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5910 /* Get all packets in connection Signalling Circuit */
5912 bpf_error("'sc' supported only on raw ATM");
5913 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5914 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5919 /* Get all packets in ILMI Circuit */
5921 bpf_error("'ilmic' supported only on raw ATM");
5922 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5923 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5928 /* Get all LANE packets */
5930 bpf_error("'lane' supported only on raw ATM");
5931 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5934 * Arrange that all subsequent tests assume LANE
5935 * rather than LLC-encapsulated packets, and set
5936 * the offsets appropriately for LANE-encapsulated
5939 * "off_mac" is the offset of the Ethernet header,
5940 * which is 2 bytes past the ATM pseudo-header
5941 * (skipping the pseudo-header and 2-byte LE Client
5942 * field). The other offsets are Ethernet offsets
5943 * relative to "off_mac".
5946 off_mac
= off_payload
+ 2; /* MAC header */
5947 off_linktype
= off_mac
+ 12;
5948 off_nl
= off_mac
+ 14; /* Ethernet II */
5949 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5953 /* Get all LLC-encapsulated packets */
5955 bpf_error("'llc' supported only on raw ATM");
5956 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5967 static struct block
*
5968 gen_msg_abbrev(type
)
5974 * Q.2931 signalling protocol messages for handling virtual circuits
5975 * establishment and teardown
5980 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5984 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5988 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5992 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5996 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5999 case A_RELEASE_DONE
:
6000 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
6010 gen_atmmulti_abbrev(type
)
6013 struct block
*b0
, *b1
;
6019 bpf_error("'oam' supported only on raw ATM");
6020 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6025 bpf_error("'oamf4' supported only on raw ATM");
6027 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6028 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6030 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6036 * Get Q.2931 signalling messages for switched
6037 * virtual connection
6040 bpf_error("'connectmsg' supported only on raw ATM");
6041 b0
= gen_msg_abbrev(A_SETUP
);
6042 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6044 b0
= gen_msg_abbrev(A_CONNECT
);
6046 b0
= gen_msg_abbrev(A_CONNECTACK
);
6048 b0
= gen_msg_abbrev(A_RELEASE
);
6050 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6052 b0
= gen_atmtype_abbrev(A_SC
);
6058 bpf_error("'metaconnect' supported only on raw ATM");
6059 b0
= gen_msg_abbrev(A_SETUP
);
6060 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6062 b0
= gen_msg_abbrev(A_CONNECT
);
6064 b0
= gen_msg_abbrev(A_RELEASE
);
6066 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6068 b0
= gen_atmtype_abbrev(A_METAC
);