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.8 2005-04-25 18:59:20 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 block
*gen_uncond(int);
167 static inline struct block
*gen_true(void);
168 static inline struct block
*gen_false(void);
169 static struct block
*gen_ether_linktype(int);
170 static struct block
*gen_linux_sll_linktype(int);
171 static struct block
*gen_linktype(int);
172 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
173 static struct block
*gen_llc_linktype(int);
174 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
176 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
178 static struct block
*gen_ahostop(const u_char
*, int);
179 static struct block
*gen_ehostop(const u_char
*, int);
180 static struct block
*gen_fhostop(const u_char
*, int);
181 static struct block
*gen_thostop(const u_char
*, int);
182 static struct block
*gen_wlanhostop(const u_char
*, int);
183 static struct block
*gen_ipfchostop(const u_char
*, int);
184 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
185 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
187 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
190 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
192 static struct block
*gen_ipfrag(void);
193 static struct block
*gen_portatom(int, bpf_int32
);
194 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
196 static struct block
*gen_portatom6(int, bpf_int32
);
197 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
199 struct block
*gen_portop(int, int, int);
200 static struct block
*gen_port(int, int, int);
201 struct block
*gen_portrangeop(int, int, int, int);
202 static struct block
*gen_portrange(int, int, int, int);
204 struct block
*gen_portop6(int, int, int);
205 static struct block
*gen_port6(int, int, int);
206 struct block
*gen_portrangeop6(int, int, int, int);
207 static struct block
*gen_portrange6(int, int, int, int);
209 static int lookup_proto(const char *, int);
210 static struct block
*gen_protochain(int, int, int);
211 static struct block
*gen_proto(int, int, int);
212 static struct slist
*xfer_to_x(struct arth
*);
213 static struct slist
*xfer_to_a(struct arth
*);
214 static struct block
*gen_mac_multicast(int);
215 static struct block
*gen_len(int, int);
217 static struct block
*gen_msg_abbrev(int type
);
228 /* XXX Round up to nearest long. */
229 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
231 /* XXX Round up to structure boundary. */
235 cp
= &chunks
[cur_chunk
];
236 if (n
> cp
->n_left
) {
237 ++cp
, k
= ++cur_chunk
;
239 bpf_error("out of memory");
240 size
= CHUNK0SIZE
<< k
;
241 cp
->m
= (void *)malloc(size
);
243 bpf_error("out of memory");
244 memset((char *)cp
->m
, 0, size
);
247 bpf_error("out of memory");
250 return (void *)((char *)cp
->m
+ cp
->n_left
);
259 for (i
= 0; i
< NCHUNKS
; ++i
)
260 if (chunks
[i
].m
!= NULL
) {
267 * A strdup whose allocations are freed after code generation is over.
271 register const char *s
;
273 int n
= strlen(s
) + 1;
274 char *cp
= newchunk(n
);
280 static inline struct block
*
286 p
= (struct block
*)newchunk(sizeof(*p
));
293 static inline struct slist
*
299 p
= (struct slist
*)newchunk(sizeof(*p
));
305 static struct block
*
309 struct block
*b
= new_block(BPF_RET
|BPF_K
);
318 bpf_error("syntax error in filter expression");
321 static bpf_u_int32 netmask
;
326 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
327 char *buf
, int optimize
, bpf_u_int32 mask
)
336 if (setjmp(top_ctx
)) {
344 snaplen
= pcap_snapshot(p
);
346 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
347 "snaplen of 0 rejects all packets");
351 lex_init(buf
? buf
: "");
359 root
= gen_retblk(snaplen
);
361 if (optimize
&& !no_optimize
) {
364 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
365 bpf_error("expression rejects all packets");
367 program
->bf_insns
= icode_to_fcode(root
, &len
);
368 program
->bf_len
= len
;
376 * entry point for using the compiler with no pcap open
377 * pass in all the stuff that is needed explicitly instead.
380 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
381 struct bpf_program
*program
,
382 char *buf
, int optimize
, bpf_u_int32 mask
)
387 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
390 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
396 * Clean up a "struct bpf_program" by freeing all the memory allocated
400 pcap_freecode(struct bpf_program
*program
)
403 if (program
->bf_insns
!= NULL
) {
404 free((char *)program
->bf_insns
);
405 program
->bf_insns
= NULL
;
410 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
411 * which of the jt and jf fields has been resolved and which is a pointer
412 * back to another unresolved block (or nil). At least one of the fields
413 * in each block is already resolved.
416 backpatch(list
, target
)
417 struct block
*list
, *target
;
434 * Merge the lists in b0 and b1, using the 'sense' field to indicate
435 * which of jt and jf is the link.
439 struct block
*b0
, *b1
;
441 register struct block
**p
= &b0
;
443 /* Find end of list. */
445 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
447 /* Concatenate the lists. */
455 backpatch(p
, gen_retblk(snaplen
));
456 p
->sense
= !p
->sense
;
457 backpatch(p
, gen_retblk(0));
463 struct block
*b0
, *b1
;
465 backpatch(b0
, b1
->head
);
466 b0
->sense
= !b0
->sense
;
467 b1
->sense
= !b1
->sense
;
469 b1
->sense
= !b1
->sense
;
475 struct block
*b0
, *b1
;
477 b0
->sense
= !b0
->sense
;
478 backpatch(b0
, b1
->head
);
479 b0
->sense
= !b0
->sense
;
488 b
->sense
= !b
->sense
;
491 static struct block
*
492 gen_cmp(offset
, size
, v
)
499 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
502 b
= new_block(JMP(BPF_JEQ
));
509 static struct block
*
510 gen_cmp_gt(offset
, size
, v
)
517 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
520 b
= new_block(JMP(BPF_JGT
));
527 static struct block
*
528 gen_mcmp(offset
, size
, v
, mask
)
533 struct block
*b
= gen_cmp(offset
, size
, v
);
536 if (mask
!= 0xffffffff) {
537 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
544 static struct block
*
545 gen_bcmp(offset
, size
, v
)
546 register u_int offset
, size
;
547 register const u_char
*v
;
549 register struct block
*b
, *tmp
;
553 register const u_char
*p
= &v
[size
- 4];
554 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
555 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
557 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
564 register const u_char
*p
= &v
[size
- 2];
565 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
567 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
574 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
582 static struct block
*
583 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
584 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
590 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
593 if (mask
!= 0xffffffff) {
594 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
598 b
= new_block(JMP(jtype
));
601 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
607 * Various code constructs need to know the layout of the data link
608 * layer. These variables give the necessary offsets.
612 * This is the offset of the beginning of the MAC-layer header.
613 * It's usually 0, except for ATM LANE.
615 static u_int off_mac
;
618 * "off_linktype" is the offset to information in the link-layer header
619 * giving the packet type.
621 * For Ethernet, it's the offset of the Ethernet type field.
623 * For link-layer types that always use 802.2 headers, it's the
624 * offset of the LLC header.
626 * For PPP, it's the offset of the PPP type field.
628 * For Cisco HDLC, it's the offset of the CHDLC type field.
630 * For BSD loopback, it's the offset of the AF_ value.
632 * For Linux cooked sockets, it's the offset of the type field.
634 * It's set to -1 for no encapsulation, in which case, IP is assumed.
636 static u_int off_linktype
;
639 * TRUE if the link layer includes an ATM pseudo-header.
641 static int is_atm
= 0;
644 * TRUE if "lane" appeared in the filter; it causes us to generate
645 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
647 static int is_lane
= 0;
650 * These are offsets for the ATM pseudo-header.
652 static u_int off_vpi
;
653 static u_int off_vci
;
654 static u_int off_proto
;
657 * This is the offset of the first byte after the ATM pseudo_header,
658 * or -1 if there is no ATM pseudo-header.
660 static u_int off_payload
;
663 * These are offsets to the beginning of the network-layer header.
665 * If the link layer never uses 802.2 LLC:
667 * "off_nl" and "off_nl_nosnap" are the same.
669 * If the link layer always uses 802.2 LLC:
671 * "off_nl" is the offset if there's a SNAP header following
674 * "off_nl_nosnap" is the offset if there's no SNAP header.
676 * If the link layer is Ethernet:
678 * "off_nl" is the offset if the packet is an Ethernet II packet
679 * (we assume no 802.3+802.2+SNAP);
681 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
682 * with an 802.2 header following it.
685 static u_int off_nl_nosnap
;
693 linktype
= pcap_datalink(p
);
695 pcap_fddipad
= p
->fddipad
;
699 * Assume it's not raw ATM with a pseudo-header, for now.
716 off_nl
= 6; /* XXX in reality, variable! */
717 off_nl_nosnap
= 6; /* no 802.2 LLC */
720 case DLT_ARCNET_LINUX
:
722 off_nl
= 8; /* XXX in reality, variable! */
723 off_nl_nosnap
= 8; /* no 802.2 LLC */
728 off_nl
= 14; /* Ethernet II */
729 off_nl_nosnap
= 17; /* 802.3+802.2 */
734 * SLIP doesn't have a link level type. The 16 byte
735 * header is hacked into our SLIP driver.
739 off_nl_nosnap
= 16; /* no 802.2 LLC */
743 /* XXX this may be the same as the DLT_PPP_BSDOS case */
747 off_nl_nosnap
= 24; /* no 802.2 LLC */
754 off_nl_nosnap
= 4; /* no 802.2 LLC */
760 off_nl_nosnap
= 12; /* no 802.2 LLC */
765 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
766 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
769 off_nl_nosnap
= 4; /* no 802.2 LLC */
774 * This does no include the Ethernet header, and
775 * only covers session state.
779 off_nl_nosnap
= 8; /* no 802.2 LLC */
785 off_nl_nosnap
= 24; /* no 802.2 LLC */
790 * FDDI doesn't really have a link-level type field.
791 * We set "off_linktype" to the offset of the LLC header.
793 * To check for Ethernet types, we assume that SSAP = SNAP
794 * is being used and pick out the encapsulated Ethernet type.
795 * XXX - should we generate code to check for SNAP?
799 off_linktype
+= pcap_fddipad
;
801 off_nl
= 21; /* FDDI+802.2+SNAP */
802 off_nl_nosnap
= 16; /* FDDI+802.2 */
804 off_nl
+= pcap_fddipad
;
805 off_nl_nosnap
+= pcap_fddipad
;
811 * Token Ring doesn't really have a link-level type field.
812 * We set "off_linktype" to the offset of the LLC header.
814 * To check for Ethernet types, we assume that SSAP = SNAP
815 * is being used and pick out the encapsulated Ethernet type.
816 * XXX - should we generate code to check for SNAP?
818 * XXX - the header is actually variable-length.
819 * Some various Linux patched versions gave 38
820 * as "off_linktype" and 40 as "off_nl"; however,
821 * if a token ring packet has *no* routing
822 * information, i.e. is not source-routed, the correct
823 * values are 20 and 22, as they are in the vanilla code.
825 * A packet is source-routed iff the uppermost bit
826 * of the first byte of the source address, at an
827 * offset of 8, has the uppermost bit set. If the
828 * packet is source-routed, the total number of bytes
829 * of routing information is 2 plus bits 0x1F00 of
830 * the 16-bit value at an offset of 14 (shifted right
831 * 8 - figure out which byte that is).
834 off_nl
= 22; /* Token Ring+802.2+SNAP */
835 off_nl_nosnap
= 17; /* Token Ring+802.2 */
840 * 802.11 doesn't really have a link-level type field.
841 * We set "off_linktype" to the offset of the LLC header.
843 * To check for Ethernet types, we assume that SSAP = SNAP
844 * is being used and pick out the encapsulated Ethernet type.
845 * XXX - should we generate code to check for SNAP?
847 * XXX - the header is actually variable-length. We
848 * assume a 24-byte link-layer header, as appears in
849 * data frames in networks with no bridges. If the
850 * fromds and tods 802.11 header bits are both set,
851 * it's actually supposed to be 30 bytes.
854 off_nl
= 32; /* 802.11+802.2+SNAP */
855 off_nl_nosnap
= 27; /* 802.11+802.2 */
858 case DLT_PRISM_HEADER
:
860 * Same as 802.11, but with an additional header before
861 * the 802.11 header, containing a bunch of additional
862 * information including radio-level information.
864 * The header is 144 bytes long.
866 * XXX - same variable-length header problem; at least
867 * the Prism header is fixed-length.
869 off_linktype
= 144+24;
870 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
871 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
874 case DLT_IEEE802_11_RADIO_AVS
:
876 * Same as 802.11, but with an additional header before
877 * the 802.11 header, containing a bunch of additional
878 * information including radio-level information.
880 * The header is 64 bytes long, at least in its
881 * current incarnation.
883 * XXX - same variable-length header problem, only
884 * more so; this header is also variable-length,
885 * with the length being the 32-bit big-endian
886 * number at an offset of 4 from the beginning
887 * of the radio header.
889 off_linktype
= 64+24;
890 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
891 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
894 case DLT_IEEE802_11_RADIO
:
896 * Same as 802.11, but with an additional header before
897 * the 802.11 header, containing a bunch of additional
898 * information including radio-level information.
900 * XXX - same variable-length header problem, only
901 * even *more* so; this header is also variable-length,
902 * with the length being the 16-bit number at an offset
903 * of 2 from the beginning of the radio header, and it's
904 * device-dependent (different devices might supply
905 * different amounts of information), so we can't even
906 * assume a fixed length for the current version of the
909 * Therefore, currently, only raw "link[N:M]" filtering is
917 case DLT_ATM_RFC1483
:
918 case DLT_ATM_CLIP
: /* Linux ATM defines this */
920 * assume routed, non-ISO PDUs
921 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
924 off_nl
= 8; /* 802.2+SNAP */
925 off_nl_nosnap
= 3; /* 802.2 */
930 * Full Frontal ATM; you get AALn PDUs with an ATM
934 off_vpi
= SUNATM_VPI_POS
;
935 off_vci
= SUNATM_VCI_POS
;
936 off_proto
= PROTO_POS
;
937 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
938 off_payload
= SUNATM_PKT_BEGIN_POS
;
939 off_linktype
= off_payload
;
940 off_nl
= off_payload
+8; /* 802.2+SNAP */
941 off_nl_nosnap
= off_payload
+3; /* 802.2 */
947 off_nl_nosnap
= 0; /* no 802.2 LLC */
950 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
953 off_nl_nosnap
= 16; /* no 802.2 LLC */
958 * LocalTalk does have a 1-byte type field in the LLAP header,
959 * but really it just indicates whether there is a "short" or
960 * "long" DDP packet following.
964 off_nl_nosnap
= 0; /* no 802.2 LLC */
969 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
970 * link-level type field. We set "off_linktype" to the
971 * offset of the LLC header.
973 * To check for Ethernet types, we assume that SSAP = SNAP
974 * is being used and pick out the encapsulated Ethernet type.
975 * XXX - should we generate code to check for SNAP? RFC
976 * 2625 says SNAP should be used.
979 off_nl
= 24; /* IPFC+802.2+SNAP */
980 off_nl_nosnap
= 19; /* IPFC+802.2 */
985 * XXX - we should set this to handle SNAP-encapsulated
986 * frames (NLPID of 0x80).
990 off_nl_nosnap
= 0; /* no 802.2 LLC */
993 case DLT_APPLE_IP_OVER_IEEE1394
:
996 off_nl_nosnap
= 18; /* no 802.2 LLC */
1001 * Currently, only raw "link[N:M]" filtering is supported.
1010 * Currently, only raw "link[N:M]" filtering is supported.
1017 case DLT_SYMANTEC_FIREWALL
:
1019 off_nl
= 44; /* Ethernet II */
1020 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1025 /* XXX read this from pf.h? */
1026 off_nl
= PFLOG_HDRLEN
;
1027 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1030 case DLT_JUNIPER_MLFR
:
1031 case DLT_JUNIPER_MLPPP
:
1034 off_nl_nosnap
= -1; /* no 802.2 LLC */
1037 case DLT_JUNIPER_ATM1
:
1038 off_linktype
= 4; /* in reality variable between 4-8 */
1043 case DLT_JUNIPER_ATM2
:
1044 off_linktype
= 8; /* in reality variable between 8-12 */
1057 case DLT_LINUX_LAPD
:
1059 * Currently, only raw "link[N:M]" filtering is supported.
1066 bpf_error("unknown data link type %d", linktype
);
1070 static struct block
*
1077 s
= new_stmt(BPF_LD
|BPF_IMM
);
1079 b
= new_block(JMP(BPF_JEQ
));
1085 static inline struct block
*
1088 return gen_uncond(1);
1091 static inline struct block
*
1094 return gen_uncond(0);
1098 * Byte-swap a 32-bit number.
1099 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1100 * big-endian platforms.)
1102 #define SWAPLONG(y) \
1103 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1106 * Generate code to match a particular packet type.
1108 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1109 * value, if <= ETHERMTU. We use that to determine whether to
1110 * match the type/length field or to check the type/length field for
1111 * a value <= ETHERMTU to see whether it's a type field and then do
1112 * the appropriate test.
1114 static struct block
*
1115 gen_ether_linktype(proto
)
1118 struct block
*b0
, *b1
;
1124 case LLCSAP_NETBEUI
:
1126 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1127 * so we check the DSAP and SSAP.
1129 * LLCSAP_IP checks for IP-over-802.2, rather
1130 * than IP-over-Ethernet or IP-over-SNAP.
1132 * XXX - should we check both the DSAP and the
1133 * SSAP, like this, or should we check just the
1134 * DSAP, as we do for other types <= ETHERMTU
1135 * (i.e., other SAP values)?
1137 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1139 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1140 ((proto
<< 8) | proto
));
1148 * Ethernet_II frames, which are Ethernet
1149 * frames with a frame type of ETHERTYPE_IPX;
1151 * Ethernet_802.3 frames, which are 802.3
1152 * frames (i.e., the type/length field is
1153 * a length field, <= ETHERMTU, rather than
1154 * a type field) with the first two bytes
1155 * after the Ethernet/802.3 header being
1158 * Ethernet_802.2 frames, which are 802.3
1159 * frames with an 802.2 LLC header and
1160 * with the IPX LSAP as the DSAP in the LLC
1163 * Ethernet_SNAP frames, which are 802.3
1164 * frames with an LLC header and a SNAP
1165 * header and with an OUI of 0x000000
1166 * (encapsulated Ethernet) and a protocol
1167 * ID of ETHERTYPE_IPX in the SNAP header.
1169 * XXX - should we generate the same code both
1170 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1174 * This generates code to check both for the
1175 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1177 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1178 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1182 * Now we add code to check for SNAP frames with
1183 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1185 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1189 * Now we generate code to check for 802.3
1190 * frames in general.
1192 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1196 * Now add the check for 802.3 frames before the
1197 * check for Ethernet_802.2 and Ethernet_802.3,
1198 * as those checks should only be done on 802.3
1199 * frames, not on Ethernet frames.
1204 * Now add the check for Ethernet_II frames, and
1205 * do that before checking for the other frame
1208 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1212 case ETHERTYPE_ATALK
:
1213 case ETHERTYPE_AARP
:
1215 * EtherTalk (AppleTalk protocols on Ethernet link
1216 * layer) may use 802.2 encapsulation.
1220 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1221 * we check for an Ethernet type field less than
1222 * 1500, which means it's an 802.3 length field.
1224 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1228 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1229 * SNAP packets with an organization code of
1230 * 0x080007 (Apple, for Appletalk) and a protocol
1231 * type of ETHERTYPE_ATALK (Appletalk).
1233 * 802.2-encapsulated ETHERTYPE_AARP packets are
1234 * SNAP packets with an organization code of
1235 * 0x000000 (encapsulated Ethernet) and a protocol
1236 * type of ETHERTYPE_AARP (Appletalk ARP).
1238 if (proto
== ETHERTYPE_ATALK
)
1239 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1240 else /* proto == ETHERTYPE_AARP */
1241 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1245 * Check for Ethernet encapsulation (Ethertalk
1246 * phase 1?); we just check for the Ethernet
1249 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1255 if (proto
<= ETHERMTU
) {
1257 * This is an LLC SAP value, so the frames
1258 * that match would be 802.2 frames.
1259 * Check that the frame is an 802.2 frame
1260 * (i.e., that the length/type field is
1261 * a length field, <= ETHERMTU) and
1262 * then check the DSAP.
1264 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1266 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1271 * This is an Ethernet type, so compare
1272 * the length/type field with it (if
1273 * the frame is an 802.2 frame, the length
1274 * field will be <= ETHERMTU, and, as
1275 * "proto" is > ETHERMTU, this test
1276 * will fail and the frame won't match,
1277 * which is what we want).
1279 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1285 * Generate code to match a particular packet type.
1287 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1288 * value, if <= ETHERMTU. We use that to determine whether to
1289 * match the type field or to check the type field for the special
1290 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1292 static struct block
*
1293 gen_linux_sll_linktype(proto
)
1296 struct block
*b0
, *b1
;
1302 case LLCSAP_NETBEUI
:
1304 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1305 * so we check the DSAP and SSAP.
1307 * LLCSAP_IP checks for IP-over-802.2, rather
1308 * than IP-over-Ethernet or IP-over-SNAP.
1310 * XXX - should we check both the DSAP and the
1311 * SSAP, like this, or should we check just the
1312 * DSAP, as we do for other types <= ETHERMTU
1313 * (i.e., other SAP values)?
1315 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1316 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1317 ((proto
<< 8) | proto
));
1323 * Ethernet_II frames, which are Ethernet
1324 * frames with a frame type of ETHERTYPE_IPX;
1326 * Ethernet_802.3 frames, which have a frame
1327 * type of LINUX_SLL_P_802_3;
1329 * Ethernet_802.2 frames, which are 802.3
1330 * frames with an 802.2 LLC header (i.e, have
1331 * a frame type of LINUX_SLL_P_802_2) and
1332 * with the IPX LSAP as the DSAP in the LLC
1335 * Ethernet_SNAP frames, which are 802.3
1336 * frames with an LLC header and a SNAP
1337 * header and with an OUI of 0x000000
1338 * (encapsulated Ethernet) and a protocol
1339 * ID of ETHERTYPE_IPX in the SNAP header.
1341 * First, do the checks on LINUX_SLL_P_802_2
1342 * frames; generate the check for either
1343 * Ethernet_802.2 or Ethernet_SNAP frames, and
1344 * then put a check for LINUX_SLL_P_802_2 frames
1347 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1348 (bpf_int32
)LLCSAP_IPX
);
1349 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1352 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1356 * Now check for 802.3 frames and OR that with
1357 * the previous test.
1359 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1363 * Now add the check for Ethernet_II frames, and
1364 * do that before checking for the other frame
1367 b0
= gen_cmp(off_linktype
, BPF_H
,
1368 (bpf_int32
)ETHERTYPE_IPX
);
1372 case ETHERTYPE_ATALK
:
1373 case ETHERTYPE_AARP
:
1375 * EtherTalk (AppleTalk protocols on Ethernet link
1376 * layer) may use 802.2 encapsulation.
1380 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1381 * we check for the 802.2 protocol type in the
1382 * "Ethernet type" field.
1384 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1387 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1388 * SNAP packets with an organization code of
1389 * 0x080007 (Apple, for Appletalk) and a protocol
1390 * type of ETHERTYPE_ATALK (Appletalk).
1392 * 802.2-encapsulated ETHERTYPE_AARP packets are
1393 * SNAP packets with an organization code of
1394 * 0x000000 (encapsulated Ethernet) and a protocol
1395 * type of ETHERTYPE_AARP (Appletalk ARP).
1397 if (proto
== ETHERTYPE_ATALK
)
1398 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1400 else /* proto == ETHERTYPE_AARP */
1401 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1406 * Check for Ethernet encapsulation (Ethertalk
1407 * phase 1?); we just check for the Ethernet
1410 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1416 if (proto
<= ETHERMTU
) {
1418 * This is an LLC SAP value, so the frames
1419 * that match would be 802.2 frames.
1420 * Check for the 802.2 protocol type
1421 * in the "Ethernet type" field, and
1422 * then check the DSAP.
1424 b0
= gen_cmp(off_linktype
, BPF_H
,
1426 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1432 * This is an Ethernet type, so compare
1433 * the length/type field with it (if
1434 * the frame is an 802.2 frame, the length
1435 * field will be <= ETHERMTU, and, as
1436 * "proto" is > ETHERMTU, this test
1437 * will fail and the frame won't match,
1438 * which is what we want).
1440 return gen_cmp(off_linktype
, BPF_H
,
1447 * Generate code to match a particular packet type by matching the
1448 * link-layer type field or fields in the 802.2 LLC header.
1450 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1451 * value, if <= ETHERMTU.
1453 static struct block
*
1457 struct block
*b0
, *b1
, *b2
;
1462 return gen_ether_linktype(proto
);
1470 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1474 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1480 case DLT_IEEE802_11
:
1481 case DLT_PRISM_HEADER
:
1482 case DLT_IEEE802_11_RADIO
:
1485 case DLT_ATM_RFC1483
:
1487 case DLT_IP_OVER_FC
:
1488 return gen_llc_linktype(proto
);
1494 * If "is_lane" is set, check for a LANE-encapsulated
1495 * version of this protocol, otherwise check for an
1496 * LLC-encapsulated version of this protocol.
1498 * We assume LANE means Ethernet, not Token Ring.
1502 * Check that the packet doesn't begin with an
1503 * LE Control marker. (We've already generated
1506 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1510 * Now generate an Ethernet test.
1512 b1
= gen_ether_linktype(proto
);
1517 * Check for LLC encapsulation and then check the
1520 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1521 b1
= gen_llc_linktype(proto
);
1529 return gen_linux_sll_linktype(proto
);
1534 case DLT_SLIP_BSDOS
:
1537 * These types don't provide any type field; packets
1540 * XXX - for IPv4, check for a version number of 4, and,
1541 * for IPv6, check for a version number of 6?
1547 case ETHERTYPE_IPV6
:
1549 return gen_true(); /* always true */
1552 return gen_false(); /* always false */
1559 case DLT_PPP_SERIAL
:
1562 * We use Ethernet protocol types inside libpcap;
1563 * map them to the corresponding PPP protocol types.
1572 case ETHERTYPE_IPV6
:
1581 case ETHERTYPE_ATALK
:
1595 * I'm assuming the "Bridging PDU"s that go
1596 * over PPP are Spanning Tree Protocol
1610 * We use Ethernet protocol types inside libpcap;
1611 * map them to the corresponding PPP protocol types.
1616 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1617 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1619 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1624 case ETHERTYPE_IPV6
:
1634 case ETHERTYPE_ATALK
:
1648 * I'm assuming the "Bridging PDU"s that go
1649 * over PPP are Spanning Tree Protocol
1665 * For DLT_NULL, the link-layer header is a 32-bit
1666 * word containing an AF_ value in *host* byte order,
1667 * and for DLT_ENC, the link-layer header begins
1668 * with a 32-bit work containing an AF_ value in
1671 * In addition, if we're reading a saved capture file,
1672 * the host byte order in the capture may not be the
1673 * same as the host byte order on this machine.
1675 * For DLT_LOOP, the link-layer header is a 32-bit
1676 * word containing an AF_ value in *network* byte order.
1678 * XXX - AF_ values may, unfortunately, be platform-
1679 * dependent; for example, FreeBSD's AF_INET6 is 24
1680 * whilst NetBSD's and OpenBSD's is 26.
1682 * This means that, when reading a capture file, just
1683 * checking for our AF_INET6 value won't work if the
1684 * capture file came from another OS.
1693 case ETHERTYPE_IPV6
:
1700 * Not a type on which we support filtering.
1701 * XXX - support those that have AF_ values
1702 * #defined on this platform, at least?
1707 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1709 * The AF_ value is in host byte order, but
1710 * the BPF interpreter will convert it to
1711 * network byte order.
1713 * If this is a save file, and it's from a
1714 * machine with the opposite byte order to
1715 * ours, we byte-swap the AF_ value.
1717 * Then we run it through "htonl()", and
1718 * generate code to compare against the result.
1720 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1721 bpf_pcap
->sf
.swapped
)
1722 proto
= SWAPLONG(proto
);
1723 proto
= htonl(proto
);
1725 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1729 * af field is host byte order in contrast to the rest of
1732 if (proto
== ETHERTYPE_IP
)
1733 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1734 (bpf_int32
)AF_INET
));
1736 else if (proto
== ETHERTYPE_IPV6
)
1737 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1738 (bpf_int32
)AF_INET6
));
1746 case DLT_ARCNET_LINUX
:
1748 * XXX should we check for first fragment if the protocol
1757 case ETHERTYPE_IPV6
:
1758 return (gen_cmp(off_linktype
, BPF_B
,
1759 (bpf_int32
)ARCTYPE_INET6
));
1763 b0
= gen_cmp(off_linktype
, BPF_B
,
1764 (bpf_int32
)ARCTYPE_IP
);
1765 b1
= gen_cmp(off_linktype
, BPF_B
,
1766 (bpf_int32
)ARCTYPE_IP_OLD
);
1771 b0
= gen_cmp(off_linktype
, BPF_B
,
1772 (bpf_int32
)ARCTYPE_ARP
);
1773 b1
= gen_cmp(off_linktype
, BPF_B
,
1774 (bpf_int32
)ARCTYPE_ARP_OLD
);
1778 case ETHERTYPE_REVARP
:
1779 return (gen_cmp(off_linktype
, BPF_B
,
1780 (bpf_int32
)ARCTYPE_REVARP
));
1782 case ETHERTYPE_ATALK
:
1783 return (gen_cmp(off_linktype
, BPF_B
,
1784 (bpf_int32
)ARCTYPE_ATALK
));
1791 case ETHERTYPE_ATALK
:
1801 * XXX - assumes a 2-byte Frame Relay header with
1802 * DLCI and flags. What if the address is longer?
1808 * Check for the special NLPID for IP.
1810 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1813 case ETHERTYPE_IPV6
:
1815 * Check for the special NLPID for IPv6.
1817 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1822 * Check for several OSI protocols.
1824 * Frame Relay packets typically have an OSI
1825 * NLPID at the beginning; we check for each
1828 * What we check for is the NLPID and a frame
1829 * control field of UI, i.e. 0x03 followed
1832 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1833 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1834 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1845 case DLT_JUNIPER_MLFR
:
1846 case DLT_JUNIPER_MLPPP
:
1847 case DLT_JUNIPER_ATM1
:
1848 case DLT_JUNIPER_ATM2
:
1849 /* just lets verify the magic number for now -
1850 * on ATM we may have up to 6 different encapsulations on the wire
1851 * and need a lot of heuristics to figure out that the payload
1854 * FIXME encapsulation specific BPF_ filters
1856 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1858 case DLT_LINUX_IRDA
:
1859 bpf_error("IrDA link-layer type filtering not implemented");
1862 bpf_error("DOCSIS link-layer type filtering not implemented");
1864 case DLT_LINUX_LAPD
:
1865 bpf_error("LAPD link-layer type filtering not implemented");
1869 * All the types that have no encapsulation should either be
1870 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1871 * all packets are IP packets, or should be handled in some
1872 * special case, if none of them are (if some are and some
1873 * aren't, the lack of encapsulation is a problem, as we'd
1874 * have to find some other way of determining the packet type).
1876 * Therefore, if "off_linktype" is -1, there's an error.
1878 if (off_linktype
== (u_int
)-1)
1882 * Any type not handled above should always have an Ethernet
1883 * type at an offset of "off_linktype". (PPP is partially
1884 * handled above - the protocol type is mapped from the
1885 * Ethernet and LLC types we use internally to the corresponding
1886 * PPP type - but the PPP type is always specified by a value
1887 * at "off_linktype", so we don't have to do the code generation
1890 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1894 * Check for an LLC SNAP packet with a given organization code and
1895 * protocol type; we check the entire contents of the 802.2 LLC and
1896 * snap headers, checking for DSAP and SSAP of SNAP and a control
1897 * field of 0x03 in the LLC header, and for the specified organization
1898 * code and protocol type in the SNAP header.
1900 static struct block
*
1901 gen_snap(orgcode
, ptype
, offset
)
1902 bpf_u_int32 orgcode
;
1906 u_char snapblock
[8];
1908 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1909 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1910 snapblock
[2] = 0x03; /* control = UI */
1911 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1912 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1913 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1914 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1915 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1916 return gen_bcmp(offset
, 8, snapblock
);
1920 * Generate code to match a particular packet type, for link-layer types
1921 * using 802.2 LLC headers.
1923 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1924 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1926 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1927 * value, if <= ETHERMTU. We use that to determine whether to
1928 * match the DSAP or both DSAP and LSAP or to check the OUI and
1929 * protocol ID in a SNAP header.
1931 static struct block
*
1932 gen_llc_linktype(proto
)
1936 * XXX - handle token-ring variable-length header.
1942 case LLCSAP_NETBEUI
:
1944 * XXX - should we check both the DSAP and the
1945 * SSAP, like this, or should we check just the
1946 * DSAP, as we do for other types <= ETHERMTU
1947 * (i.e., other SAP values)?
1949 return gen_cmp(off_linktype
, BPF_H
, (long)
1950 ((proto
<< 8) | proto
));
1954 * XXX - are there ever SNAP frames for IPX on
1955 * non-Ethernet 802.x networks?
1957 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1959 case ETHERTYPE_ATALK
:
1961 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1962 * SNAP packets with an organization code of
1963 * 0x080007 (Apple, for Appletalk) and a protocol
1964 * type of ETHERTYPE_ATALK (Appletalk).
1966 * XXX - check for an organization code of
1967 * encapsulated Ethernet as well?
1969 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1973 * XXX - we don't have to check for IPX 802.3
1974 * here, but should we check for the IPX Ethertype?
1976 if (proto
<= ETHERMTU
) {
1978 * This is an LLC SAP value, so check
1981 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1984 * This is an Ethernet type; we assume that it's
1985 * unlikely that it'll appear in the right place
1986 * at random, and therefore check only the
1987 * location that would hold the Ethernet type
1988 * in a SNAP frame with an organization code of
1989 * 0x000000 (encapsulated Ethernet).
1991 * XXX - if we were to check for the SNAP DSAP and
1992 * LSAP, as per XXX, and were also to check for an
1993 * organization code of 0x000000 (encapsulated
1994 * Ethernet), we'd do
1996 * return gen_snap(0x000000, proto,
1999 * here; for now, we don't, as per the above.
2000 * I don't know whether it's worth the extra CPU
2001 * time to do the right check or not.
2003 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2008 static struct block
*
2009 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2013 u_int src_off
, dst_off
;
2015 struct block
*b0
, *b1
;
2029 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2030 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2036 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2037 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2044 b0
= gen_linktype(proto
);
2045 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2051 static struct block
*
2052 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2053 struct in6_addr
*addr
;
2054 struct in6_addr
*mask
;
2056 u_int src_off
, dst_off
;
2058 struct block
*b0
, *b1
;
2073 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2074 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2080 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2081 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2088 /* this order is important */
2089 a
= (u_int32_t
*)addr
;
2090 m
= (u_int32_t
*)mask
;
2091 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2092 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2094 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2096 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2098 b0
= gen_linktype(proto
);
2104 static struct block
*
2105 gen_ehostop(eaddr
, dir
)
2106 register const u_char
*eaddr
;
2109 register struct block
*b0
, *b1
;
2113 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2116 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2119 b0
= gen_ehostop(eaddr
, Q_SRC
);
2120 b1
= gen_ehostop(eaddr
, Q_DST
);
2126 b0
= gen_ehostop(eaddr
, Q_SRC
);
2127 b1
= gen_ehostop(eaddr
, Q_DST
);
2136 * Like gen_ehostop, but for DLT_FDDI
2138 static struct block
*
2139 gen_fhostop(eaddr
, dir
)
2140 register const u_char
*eaddr
;
2143 struct block
*b0
, *b1
;
2148 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2150 return gen_bcmp(6 + 1, 6, eaddr
);
2155 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2157 return gen_bcmp(0 + 1, 6, eaddr
);
2161 b0
= gen_fhostop(eaddr
, Q_SRC
);
2162 b1
= gen_fhostop(eaddr
, Q_DST
);
2168 b0
= gen_fhostop(eaddr
, Q_SRC
);
2169 b1
= gen_fhostop(eaddr
, Q_DST
);
2178 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2180 static struct block
*
2181 gen_thostop(eaddr
, dir
)
2182 register const u_char
*eaddr
;
2185 register struct block
*b0
, *b1
;
2189 return gen_bcmp(8, 6, eaddr
);
2192 return gen_bcmp(2, 6, eaddr
);
2195 b0
= gen_thostop(eaddr
, Q_SRC
);
2196 b1
= gen_thostop(eaddr
, Q_DST
);
2202 b0
= gen_thostop(eaddr
, Q_SRC
);
2203 b1
= gen_thostop(eaddr
, Q_DST
);
2212 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2214 static struct block
*
2215 gen_wlanhostop(eaddr
, dir
)
2216 register const u_char
*eaddr
;
2219 register struct block
*b0
, *b1
, *b2
;
2220 register struct slist
*s
;
2227 * For control frames, there is no SA.
2229 * For management frames, SA is at an
2230 * offset of 10 from the beginning of
2233 * For data frames, SA is at an offset
2234 * of 10 from the beginning of the packet
2235 * if From DS is clear, at an offset of
2236 * 16 from the beginning of the packet
2237 * if From DS is set and To DS is clear,
2238 * and an offset of 24 from the beginning
2239 * of the packet if From DS is set and To DS
2244 * Generate the tests to be done for data frames
2247 * First, check for To DS set, i.e. check "link[1] & 0x01".
2249 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2251 b1
= new_block(JMP(BPF_JSET
));
2252 b1
->s
.k
= 0x01; /* To DS */
2256 * If To DS is set, the SA is at 24.
2258 b0
= gen_bcmp(24, 6, eaddr
);
2262 * Now, check for To DS not set, i.e. check
2263 * "!(link[1] & 0x01)".
2265 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2267 b2
= new_block(JMP(BPF_JSET
));
2268 b2
->s
.k
= 0x01; /* To DS */
2273 * If To DS is not set, the SA is at 16.
2275 b1
= gen_bcmp(16, 6, eaddr
);
2279 * Now OR together the last two checks. That gives
2280 * the complete set of checks for data frames with
2286 * Now check for From DS being set, and AND that with
2287 * the ORed-together checks.
2289 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2291 b1
= new_block(JMP(BPF_JSET
));
2292 b1
->s
.k
= 0x02; /* From DS */
2297 * Now check for data frames with From DS not set.
2299 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2301 b2
= new_block(JMP(BPF_JSET
));
2302 b2
->s
.k
= 0x02; /* From DS */
2307 * If From DS isn't set, the SA is at 10.
2309 b1
= gen_bcmp(10, 6, eaddr
);
2313 * Now OR together the checks for data frames with
2314 * From DS not set and for data frames with From DS
2315 * set; that gives the checks done for data frames.
2320 * Now check for a data frame.
2321 * I.e, check "link[0] & 0x08".
2323 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2325 b1
= new_block(JMP(BPF_JSET
));
2330 * AND that with the checks done for data frames.
2335 * If the high-order bit of the type value is 0, this
2336 * is a management frame.
2337 * I.e, check "!(link[0] & 0x08)".
2339 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2341 b2
= new_block(JMP(BPF_JSET
));
2347 * For management frames, the SA is at 10.
2349 b1
= gen_bcmp(10, 6, eaddr
);
2353 * OR that with the checks done for data frames.
2354 * That gives the checks done for management and
2360 * If the low-order bit of the type value is 1,
2361 * this is either a control frame or a frame
2362 * with a reserved type, and thus not a
2365 * I.e., check "!(link[0] & 0x04)".
2367 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2369 b1
= new_block(JMP(BPF_JSET
));
2375 * AND that with the checks for data and management
2385 * For control frames, there is no DA.
2387 * For management frames, DA is at an
2388 * offset of 4 from the beginning of
2391 * For data frames, DA is at an offset
2392 * of 4 from the beginning of the packet
2393 * if To DS is clear and at an offset of
2394 * 16 from the beginning of the packet
2399 * Generate the tests to be done for data frames.
2401 * First, check for To DS set, i.e. "link[1] & 0x01".
2403 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2405 b1
= new_block(JMP(BPF_JSET
));
2406 b1
->s
.k
= 0x01; /* To DS */
2410 * If To DS is set, the DA is at 16.
2412 b0
= gen_bcmp(16, 6, eaddr
);
2416 * Now, check for To DS not set, i.e. check
2417 * "!(link[1] & 0x01)".
2419 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2421 b2
= new_block(JMP(BPF_JSET
));
2422 b2
->s
.k
= 0x01; /* To DS */
2427 * If To DS is not set, the DA is at 4.
2429 b1
= gen_bcmp(4, 6, eaddr
);
2433 * Now OR together the last two checks. That gives
2434 * the complete set of checks for data frames.
2439 * Now check for a data frame.
2440 * I.e, check "link[0] & 0x08".
2442 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2444 b1
= new_block(JMP(BPF_JSET
));
2449 * AND that with the checks done for data frames.
2454 * If the high-order bit of the type value is 0, this
2455 * is a management frame.
2456 * I.e, check "!(link[0] & 0x08)".
2458 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2460 b2
= new_block(JMP(BPF_JSET
));
2466 * For management frames, the DA is at 4.
2468 b1
= gen_bcmp(4, 6, eaddr
);
2472 * OR that with the checks done for data frames.
2473 * That gives the checks done for management and
2479 * If the low-order bit of the type value is 1,
2480 * this is either a control frame or a frame
2481 * with a reserved type, and thus not a
2484 * I.e., check "!(link[0] & 0x04)".
2486 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2488 b1
= new_block(JMP(BPF_JSET
));
2494 * AND that with the checks for data and management
2501 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2502 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2508 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2509 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2518 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2519 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2520 * as the RFC states.)
2522 static struct block
*
2523 gen_ipfchostop(eaddr
, dir
)
2524 register const u_char
*eaddr
;
2527 register struct block
*b0
, *b1
;
2531 return gen_bcmp(10, 6, eaddr
);
2534 return gen_bcmp(2, 6, eaddr
);
2537 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2538 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2544 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2545 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2554 * This is quite tricky because there may be pad bytes in front of the
2555 * DECNET header, and then there are two possible data packet formats that
2556 * carry both src and dst addresses, plus 5 packet types in a format that
2557 * carries only the src node, plus 2 types that use a different format and
2558 * also carry just the src node.
2562 * Instead of doing those all right, we just look for data packets with
2563 * 0 or 1 bytes of padding. If you want to look at other packets, that
2564 * will require a lot more hacking.
2566 * To add support for filtering on DECNET "areas" (network numbers)
2567 * one would want to add a "mask" argument to this routine. That would
2568 * make the filter even more inefficient, although one could be clever
2569 * and not generate masking instructions if the mask is 0xFFFF.
2571 static struct block
*
2572 gen_dnhostop(addr
, dir
, base_off
)
2577 struct block
*b0
, *b1
, *b2
, *tmp
;
2578 u_int offset_lh
; /* offset if long header is received */
2579 u_int offset_sh
; /* offset if short header is received */
2584 offset_sh
= 1; /* follows flags */
2585 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2589 offset_sh
= 3; /* follows flags, dstnode */
2590 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2594 /* Inefficient because we do our Calvinball dance twice */
2595 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2596 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2602 /* Inefficient because we do our Calvinball dance twice */
2603 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2604 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2609 bpf_error("ISO host filtering not implemented");
2614 b0
= gen_linktype(ETHERTYPE_DN
);
2615 /* Check for pad = 1, long header case */
2616 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2617 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2618 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2619 BPF_H
, (bpf_int32
)ntohs(addr
));
2621 /* Check for pad = 0, long header case */
2622 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2623 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2626 /* Check for pad = 1, short header case */
2627 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2628 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2629 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2630 BPF_H
, (bpf_int32
)ntohs(addr
));
2633 /* Check for pad = 0, short header case */
2634 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2635 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2639 /* Combine with test for linktype */
2644 static struct block
*
2645 gen_host(addr
, mask
, proto
, dir
)
2651 struct block
*b0
, *b1
;
2656 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2657 if (off_linktype
!= (u_int
)-1) {
2658 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2660 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2666 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2667 off_nl
+ 12, off_nl
+ 16);
2670 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2671 off_nl
+ 14, off_nl
+ 24);
2674 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2675 off_nl
+ 14, off_nl
+ 24);
2678 bpf_error("'tcp' modifier applied to host");
2681 bpf_error("'sctp' modifier applied to host");
2684 bpf_error("'udp' modifier applied to host");
2687 bpf_error("'icmp' modifier applied to host");
2690 bpf_error("'igmp' modifier applied to host");
2693 bpf_error("'igrp' modifier applied to host");
2696 bpf_error("'pim' modifier applied to host");
2699 bpf_error("'vrrp' modifier applied to host");
2702 bpf_error("ATALK host filtering not implemented");
2705 bpf_error("AARP host filtering not implemented");
2708 return gen_dnhostop(addr
, dir
, off_nl
);
2711 bpf_error("SCA host filtering not implemented");
2714 bpf_error("LAT host filtering not implemented");
2717 bpf_error("MOPDL host filtering not implemented");
2720 bpf_error("MOPRC host filtering not implemented");
2724 bpf_error("'ip6' modifier applied to ip host");
2727 bpf_error("'icmp6' modifier applied to host");
2731 bpf_error("'ah' modifier applied to host");
2734 bpf_error("'esp' modifier applied to host");
2737 bpf_error("ISO host filtering not implemented");
2740 bpf_error("'esis' modifier applied to host");
2743 bpf_error("'isis' modifier applied to host");
2746 bpf_error("'clnp' modifier applied to host");
2749 bpf_error("'stp' modifier applied to host");
2752 bpf_error("IPX host filtering not implemented");
2755 bpf_error("'netbeui' modifier applied to host");
2764 static struct block
*
2765 gen_host6(addr
, mask
, proto
, dir
)
2766 struct in6_addr
*addr
;
2767 struct in6_addr
*mask
;
2774 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2777 bpf_error("'ip' modifier applied to ip6 host");
2780 bpf_error("'rarp' modifier applied to ip6 host");
2783 bpf_error("'arp' modifier applied to ip6 host");
2786 bpf_error("'sctp' modifier applied to host");
2789 bpf_error("'tcp' modifier applied to host");
2792 bpf_error("'udp' modifier applied to host");
2795 bpf_error("'icmp' modifier applied to host");
2798 bpf_error("'igmp' modifier applied to host");
2801 bpf_error("'igrp' modifier applied to host");
2804 bpf_error("'pim' modifier applied to host");
2807 bpf_error("'vrrp' modifier applied to host");
2810 bpf_error("ATALK host filtering not implemented");
2813 bpf_error("AARP host filtering not implemented");
2816 bpf_error("'decnet' modifier applied to ip6 host");
2819 bpf_error("SCA host filtering not implemented");
2822 bpf_error("LAT host filtering not implemented");
2825 bpf_error("MOPDL host filtering not implemented");
2828 bpf_error("MOPRC host filtering not implemented");
2831 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2832 off_nl
+ 8, off_nl
+ 24);
2835 bpf_error("'icmp6' modifier applied to host");
2838 bpf_error("'ah' modifier applied to host");
2841 bpf_error("'esp' modifier applied to host");
2844 bpf_error("ISO host filtering not implemented");
2847 bpf_error("'esis' modifier applied to host");
2850 bpf_error("'isis' modifier applied to host");
2853 bpf_error("'clnp' modifier applied to host");
2856 bpf_error("'stp' modifier applied to host");
2859 bpf_error("IPX host filtering not implemented");
2862 bpf_error("'netbeui' modifier applied to host");
2872 static struct block
*
2873 gen_gateway(eaddr
, alist
, proto
, dir
)
2874 const u_char
*eaddr
;
2875 bpf_u_int32
**alist
;
2879 struct block
*b0
, *b1
, *tmp
;
2882 bpf_error("direction applied to 'gateway'");
2889 if (linktype
== DLT_EN10MB
)
2890 b0
= gen_ehostop(eaddr
, Q_OR
);
2891 else if (linktype
== DLT_FDDI
)
2892 b0
= gen_fhostop(eaddr
, Q_OR
);
2893 else if (linktype
== DLT_IEEE802
)
2894 b0
= gen_thostop(eaddr
, Q_OR
);
2895 else if (linktype
== DLT_IEEE802_11
)
2896 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2897 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2899 * Check that the packet doesn't begin with an
2900 * LE Control marker. (We've already generated
2903 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2907 * Now check the MAC address.
2909 b0
= gen_ehostop(eaddr
, Q_OR
);
2911 } else if (linktype
== DLT_IP_OVER_FC
)
2912 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2915 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2917 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2919 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2927 bpf_error("illegal modifier of 'gateway'");
2933 gen_proto_abbrev(proto
)
2942 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2944 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2950 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2952 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2958 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2960 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2966 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2969 #ifndef IPPROTO_IGMP
2970 #define IPPROTO_IGMP 2
2974 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2977 #ifndef IPPROTO_IGRP
2978 #define IPPROTO_IGRP 9
2981 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2985 #define IPPROTO_PIM 103
2989 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2991 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2996 #ifndef IPPROTO_VRRP
2997 #define IPPROTO_VRRP 112
3001 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3005 b1
= gen_linktype(ETHERTYPE_IP
);
3009 b1
= gen_linktype(ETHERTYPE_ARP
);
3013 b1
= gen_linktype(ETHERTYPE_REVARP
);
3017 bpf_error("link layer applied in wrong context");
3020 b1
= gen_linktype(ETHERTYPE_ATALK
);
3024 b1
= gen_linktype(ETHERTYPE_AARP
);
3028 b1
= gen_linktype(ETHERTYPE_DN
);
3032 b1
= gen_linktype(ETHERTYPE_SCA
);
3036 b1
= gen_linktype(ETHERTYPE_LAT
);
3040 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3044 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3049 b1
= gen_linktype(ETHERTYPE_IPV6
);
3052 #ifndef IPPROTO_ICMPV6
3053 #define IPPROTO_ICMPV6 58
3056 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3061 #define IPPROTO_AH 51
3064 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3066 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3072 #define IPPROTO_ESP 50
3075 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3077 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3083 b1
= gen_linktype(LLCSAP_ISONS
);
3087 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3091 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3094 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3095 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3096 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3098 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3100 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3102 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3106 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3107 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3108 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3110 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3112 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3114 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3118 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3119 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3120 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3122 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3127 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3128 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3133 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3134 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3136 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3138 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3143 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3144 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3149 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3150 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3155 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3159 b1
= gen_linktype(LLCSAP_8021D
);
3163 b1
= gen_linktype(LLCSAP_IPX
);
3167 b1
= gen_linktype(LLCSAP_NETBEUI
);
3176 static struct block
*
3183 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3184 s
->s
.k
= off_nl
+ 6;
3185 b
= new_block(JMP(BPF_JSET
));
3193 static struct block
*
3194 gen_portatom(off
, v
)
3201 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3204 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3205 s
->next
->s
.k
= off_nl
+ off
;
3207 b
= new_block(JMP(BPF_JEQ
));
3215 static struct block
*
3216 gen_portatom6(off
, v
)
3220 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3225 gen_portop(port
, proto
, dir
)
3226 int port
, proto
, dir
;
3228 struct block
*b0
, *b1
, *tmp
;
3230 /* ip proto 'proto' */
3231 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3237 b1
= gen_portatom(0, (bpf_int32
)port
);
3241 b1
= gen_portatom(2, (bpf_int32
)port
);
3246 tmp
= gen_portatom(0, (bpf_int32
)port
);
3247 b1
= gen_portatom(2, (bpf_int32
)port
);
3252 tmp
= gen_portatom(0, (bpf_int32
)port
);
3253 b1
= gen_portatom(2, (bpf_int32
)port
);
3265 static struct block
*
3266 gen_port(port
, ip_proto
, dir
)
3271 struct block
*b0
, *b1
, *tmp
;
3276 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3277 * not LLC encapsulation with LLCSAP_IP.
3279 * For IEEE 802 networks - which includes 802.5 token ring
3280 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3281 * says that SNAP encapsulation is used, not LLC encapsulation
3284 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3285 * RFC 2225 say that SNAP encapsulation is used, not LLC
3286 * encapsulation with LLCSAP_IP.
3288 * So we always check for ETHERTYPE_IP.
3290 b0
= gen_linktype(ETHERTYPE_IP
);
3296 b1
= gen_portop(port
, ip_proto
, dir
);
3300 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3301 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3303 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3316 gen_portop6(port
, proto
, dir
)
3317 int port
, proto
, dir
;
3319 struct block
*b0
, *b1
, *tmp
;
3321 /* ip6 proto 'proto' */
3322 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3326 b1
= gen_portatom6(0, (bpf_int32
)port
);
3330 b1
= gen_portatom6(2, (bpf_int32
)port
);
3335 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3336 b1
= gen_portatom6(2, (bpf_int32
)port
);
3341 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3342 b1
= gen_portatom6(2, (bpf_int32
)port
);
3354 static struct block
*
3355 gen_port6(port
, ip_proto
, dir
)
3360 struct block
*b0
, *b1
, *tmp
;
3362 /* link proto ip6 */
3363 b0
= gen_linktype(ETHERTYPE_IPV6
);
3369 b1
= gen_portop6(port
, ip_proto
, dir
);
3373 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3374 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3376 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3388 /* gen_portrange code */
3389 static struct block
*
3390 gen_portrangeatom(off
, v1
, v2
)
3394 struct slist
*s1
, *s2
;
3395 struct block
*b1
, *b2
;
3399 * Reverse the order of the ports, so v1 is the lower one.
3407 s1
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3410 s1
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3411 s1
->next
->s
.k
= off_nl
+ off
;
3413 b1
= new_block(JMP(BPF_JGE
));
3417 s2
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3420 s2
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3421 s2
->next
->s
.k
= off_nl
+ off
;
3423 b2
= new_block(JMP(BPF_JGT
));
3434 gen_portrangeop(port1
, port2
, proto
, dir
)
3439 struct block
*b0
, *b1
, *tmp
;
3441 /* ip proto 'proto' */
3442 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3448 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3452 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3457 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3458 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3463 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3464 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3476 static struct block
*
3477 gen_portrange(port1
, port2
, ip_proto
, dir
)
3482 struct block
*b0
, *b1
, *tmp
;
3485 b0
= gen_linktype(ETHERTYPE_IP
);
3491 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3495 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3496 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3498 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3510 static struct block
*
3511 gen_portrangeatom6(off
, v1
, v2
)
3515 struct slist
*s1
, *s2
;
3516 struct block
*b1
, *b2
;
3520 * Reverse the order of the ports, so v1 is the lower one.
3529 s1
= new_stmt(BPF_LD
|BPF_ABS
|BPF_H
);
3530 s1
->s
.k
= off_nl
+ 40 + off
;
3532 b1
= new_block(JMP(BPF_JGE
));
3536 s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_H
);
3537 s2
->s
.k
= off_nl
+ 40 + off
;
3539 b2
= new_block(JMP(BPF_JGT
));
3550 gen_portrangeop6(port1
, port2
, proto
, dir
)
3555 struct block
*b0
, *b1
, *tmp
;
3557 /* ip6 proto 'proto' */
3558 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3562 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3566 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3571 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3572 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3577 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3578 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3590 static struct block
*
3591 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3596 struct block
*b0
, *b1
, *tmp
;
3598 /* link proto ip6 */
3599 b0
= gen_linktype(ETHERTYPE_IPV6
);
3605 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3609 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3610 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3612 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3625 lookup_proto(name
, proto
)
3626 register const char *name
;
3636 v
= pcap_nametoproto(name
);
3637 if (v
== PROTO_UNDEF
)
3638 bpf_error("unknown ip proto '%s'", name
);
3642 /* XXX should look up h/w protocol type based on linktype */
3643 v
= pcap_nametoeproto(name
);
3644 if (v
== PROTO_UNDEF
) {
3645 v
= pcap_nametollc(name
);
3646 if (v
== PROTO_UNDEF
)
3647 bpf_error("unknown ether proto '%s'", name
);
3652 if (strcmp(name
, "esis") == 0)
3654 else if (strcmp(name
, "isis") == 0)
3656 else if (strcmp(name
, "clnp") == 0)
3659 bpf_error("unknown osi proto '%s'", name
);
3679 static struct block
*
3680 gen_protochain(v
, proto
, dir
)
3685 #ifdef NO_PROTOCHAIN
3686 return gen_proto(v
, proto
, dir
);
3688 struct block
*b0
, *b
;
3689 struct slist
*s
[100];
3690 int fix2
, fix3
, fix4
, fix5
;
3691 int ahcheck
, again
, end
;
3693 int reg2
= alloc_reg();
3695 memset(s
, 0, sizeof(s
));
3696 fix2
= fix3
= fix4
= fix5
= 0;
3703 b0
= gen_protochain(v
, Q_IP
, dir
);
3704 b
= gen_protochain(v
, Q_IPV6
, dir
);
3708 bpf_error("bad protocol applied for 'protochain'");
3712 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3715 * s[0] is a dummy entry to protect other BPF insn from damage
3716 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3717 * hard to find interdependency made by jump table fixup.
3720 s
[i
] = new_stmt(0); /*dummy*/
3725 b0
= gen_linktype(ETHERTYPE_IP
);
3728 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3729 s
[i
]->s
.k
= off_nl
+ 9;
3731 /* X = ip->ip_hl << 2 */
3732 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3738 b0
= gen_linktype(ETHERTYPE_IPV6
);
3740 /* A = ip6->ip_nxt */
3741 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3742 s
[i
]->s
.k
= off_nl
+ 6;
3744 /* X = sizeof(struct ip6_hdr) */
3745 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3751 bpf_error("unsupported proto to gen_protochain");
3755 /* again: if (A == v) goto end; else fall through; */
3757 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3759 s
[i
]->s
.jt
= NULL
; /*later*/
3760 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3764 #ifndef IPPROTO_NONE
3765 #define IPPROTO_NONE 59
3767 /* if (A == IPPROTO_NONE) goto end */
3768 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3769 s
[i
]->s
.jt
= NULL
; /*later*/
3770 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3771 s
[i
]->s
.k
= IPPROTO_NONE
;
3772 s
[fix5
]->s
.jf
= s
[i
];
3777 if (proto
== Q_IPV6
) {
3778 int v6start
, v6end
, v6advance
, j
;
3781 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3782 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3783 s
[i
]->s
.jt
= NULL
; /*later*/
3784 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3785 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3786 s
[fix2
]->s
.jf
= s
[i
];
3788 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3789 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3790 s
[i
]->s
.jt
= NULL
; /*later*/
3791 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3792 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3794 /* if (A == IPPROTO_ROUTING) goto v6advance */
3795 s
[i
- 1]->s
.jf
= 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_ROUTING
;
3800 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3801 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3802 s
[i
]->s
.jt
= NULL
; /*later*/
3803 s
[i
]->s
.jf
= NULL
; /*later*/
3804 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3815 * X = X + (P[X + 1] + 1) * 8;
3818 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3820 /* A = P[X + packet head] */
3821 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3825 s
[i
] = new_stmt(BPF_ST
);
3829 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3832 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3836 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3838 /* A = P[X + packet head]; */
3839 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3843 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3847 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3851 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3854 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3858 /* goto again; (must use BPF_JA for backward jump) */
3859 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3860 s
[i
]->s
.k
= again
- i
- 1;
3861 s
[i
- 1]->s
.jf
= s
[i
];
3865 for (j
= v6start
; j
<= v6end
; j
++)
3866 s
[j
]->s
.jt
= s
[v6advance
];
3871 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3873 s
[fix2
]->s
.jf
= s
[i
];
3879 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3880 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3881 s
[i
]->s
.jt
= NULL
; /*later*/
3882 s
[i
]->s
.jf
= NULL
; /*later*/
3883 s
[i
]->s
.k
= IPPROTO_AH
;
3885 s
[fix3
]->s
.jf
= s
[ahcheck
];
3892 * X = X + (P[X + 1] + 2) * 4;
3895 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3897 /* A = P[X + packet head]; */
3898 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3902 s
[i
] = new_stmt(BPF_ST
);
3906 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3909 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3913 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3915 /* A = P[X + packet head] */
3916 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3920 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3924 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3928 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3931 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3935 /* goto again; (must use BPF_JA for backward jump) */
3936 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3937 s
[i
]->s
.k
= again
- i
- 1;
3942 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3944 s
[fix2
]->s
.jt
= s
[end
];
3945 s
[fix4
]->s
.jf
= s
[end
];
3946 s
[fix5
]->s
.jt
= s
[end
];
3953 for (i
= 0; i
< max
- 1; i
++)
3954 s
[i
]->next
= s
[i
+ 1];
3955 s
[max
- 1]->next
= NULL
;
3960 b
= new_block(JMP(BPF_JEQ
));
3961 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3972 * Generate code that checks whether the packet is a packet for protocol
3973 * <proto> and whether the type field in that protocol's header has
3974 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
3975 * IP packet and checks the protocol number in the IP header against <v>.
3977 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
3978 * against Q_IP and Q_IPV6.
3980 static struct block
*
3981 gen_proto(v
, proto
, dir
)
3986 struct block
*b0
, *b1
;
3988 if (dir
!= Q_DEFAULT
)
3989 bpf_error("direction applied to 'proto'");
3994 b0
= gen_proto(v
, Q_IP
, dir
);
3995 b1
= gen_proto(v
, Q_IPV6
, dir
);
4003 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4004 * not LLC encapsulation with LLCSAP_IP.
4006 * For IEEE 802 networks - which includes 802.5 token ring
4007 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4008 * says that SNAP encapsulation is used, not LLC encapsulation
4011 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4012 * RFC 2225 say that SNAP encapsulation is used, not LLC
4013 * encapsulation with LLCSAP_IP.
4015 * So we always check for ETHERTYPE_IP.
4017 b0
= gen_linktype(ETHERTYPE_IP
);
4019 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
4021 b1
= gen_protochain(v
, Q_IP
);
4031 * Frame Relay packets typically have an OSI
4032 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4033 * generates code to check for all the OSI
4034 * NLPIDs, so calling it and then adding a check
4035 * for the particular NLPID for which we're
4036 * looking is bogus, as we can just check for
4039 * What we check for is the NLPID and a frame
4040 * control field value of UI, i.e. 0x03 followed
4043 * XXX - assumes a 2-byte Frame Relay header with
4044 * DLCI and flags. What if the address is longer?
4046 * XXX - what about SNAP-encapsulated frames?
4048 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
4054 * Cisco uses an Ethertype lookalike - for OSI,
4057 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4058 /* OSI in C-HDLC is stuffed with a fudge byte */
4059 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
4064 b0
= gen_linktype(LLCSAP_ISONS
);
4065 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
4071 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4073 * 4 is the offset of the PDU type relative to the IS-IS
4076 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
4081 bpf_error("arp does not encapsulate another protocol");
4085 bpf_error("rarp does not encapsulate another protocol");
4089 bpf_error("atalk encapsulation is not specifiable");
4093 bpf_error("decnet encapsulation is not specifiable");
4097 bpf_error("sca does not encapsulate another protocol");
4101 bpf_error("lat does not encapsulate another protocol");
4105 bpf_error("moprc does not encapsulate another protocol");
4109 bpf_error("mopdl does not encapsulate another protocol");
4113 return gen_linktype(v
);
4116 bpf_error("'udp proto' is bogus");
4120 bpf_error("'tcp proto' is bogus");
4124 bpf_error("'sctp proto' is bogus");
4128 bpf_error("'icmp proto' is bogus");
4132 bpf_error("'igmp proto' is bogus");
4136 bpf_error("'igrp proto' is bogus");
4140 bpf_error("'pim proto' is bogus");
4144 bpf_error("'vrrp proto' is bogus");
4149 b0
= gen_linktype(ETHERTYPE_IPV6
);
4151 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
4153 b1
= gen_protochain(v
, Q_IPV6
);
4159 bpf_error("'icmp6 proto' is bogus");
4163 bpf_error("'ah proto' is bogus");
4166 bpf_error("'ah proto' is bogus");
4169 bpf_error("'stp proto' is bogus");
4172 bpf_error("'ipx proto' is bogus");
4175 bpf_error("'netbeui proto' is bogus");
4186 register const char *name
;
4189 int proto
= q
.proto
;
4193 bpf_u_int32 mask
, addr
;
4195 bpf_u_int32
**alist
;
4198 struct sockaddr_in
*sin
;
4199 struct sockaddr_in6
*sin6
;
4200 struct addrinfo
*res
, *res0
;
4201 struct in6_addr mask128
;
4203 struct block
*b
, *tmp
;
4204 int port
, real_proto
;
4210 addr
= pcap_nametonetaddr(name
);
4212 bpf_error("unknown network '%s'", name
);
4213 /* Left justify network addr and calculate its network mask */
4215 while (addr
&& (addr
& 0xff000000) == 0) {
4219 return gen_host(addr
, mask
, proto
, dir
);
4223 if (proto
== Q_LINK
) {
4227 eaddr
= pcap_ether_hostton(name
);
4230 "unknown ether host '%s'", name
);
4231 b
= gen_ehostop(eaddr
, dir
);
4236 eaddr
= pcap_ether_hostton(name
);
4239 "unknown FDDI host '%s'", name
);
4240 b
= gen_fhostop(eaddr
, dir
);
4245 eaddr
= pcap_ether_hostton(name
);
4248 "unknown token ring host '%s'", name
);
4249 b
= gen_thostop(eaddr
, dir
);
4253 case DLT_IEEE802_11
:
4254 eaddr
= pcap_ether_hostton(name
);
4257 "unknown 802.11 host '%s'", name
);
4258 b
= gen_wlanhostop(eaddr
, dir
);
4262 case DLT_IP_OVER_FC
:
4263 eaddr
= pcap_ether_hostton(name
);
4266 "unknown Fibre Channel host '%s'", name
);
4267 b
= gen_ipfchostop(eaddr
, dir
);
4276 * Check that the packet doesn't begin
4277 * with an LE Control marker. (We've
4278 * already generated a test for LANE.)
4280 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4284 eaddr
= pcap_ether_hostton(name
);
4287 "unknown ether host '%s'", name
);
4288 b
= gen_ehostop(eaddr
, dir
);
4294 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4295 } else if (proto
== Q_DECNET
) {
4296 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4298 * I don't think DECNET hosts can be multihomed, so
4299 * there is no need to build up a list of addresses
4301 return (gen_host(dn_addr
, 0, proto
, dir
));
4304 alist
= pcap_nametoaddr(name
);
4305 if (alist
== NULL
|| *alist
== NULL
)
4306 bpf_error("unknown host '%s'", name
);
4308 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4310 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4312 tmp
= gen_host(**alist
++, 0xffffffff,
4319 memset(&mask128
, 0xff, sizeof(mask128
));
4320 res0
= res
= pcap_nametoaddrinfo(name
);
4322 bpf_error("unknown host '%s'", name
);
4324 tproto
= tproto6
= proto
;
4325 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4329 for (res
= res0
; res
; res
= res
->ai_next
) {
4330 switch (res
->ai_family
) {
4332 if (tproto
== Q_IPV6
)
4335 sin
= (struct sockaddr_in
*)
4337 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4338 0xffffffff, tproto
, dir
);
4341 if (tproto6
== Q_IP
)
4344 sin6
= (struct sockaddr_in6
*)
4346 tmp
= gen_host6(&sin6
->sin6_addr
,
4347 &mask128
, tproto6
, dir
);
4358 bpf_error("unknown host '%s'%s", name
,
4359 (proto
== Q_DEFAULT
)
4361 : " for specified address family");
4368 if (proto
!= Q_DEFAULT
&&
4369 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4370 bpf_error("illegal qualifier of 'port'");
4371 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4372 bpf_error("unknown port '%s'", name
);
4373 if (proto
== Q_UDP
) {
4374 if (real_proto
== IPPROTO_TCP
)
4375 bpf_error("port '%s' is tcp", name
);
4376 else if (real_proto
== IPPROTO_SCTP
)
4377 bpf_error("port '%s' is sctp", name
);
4379 /* override PROTO_UNDEF */
4380 real_proto
= IPPROTO_UDP
;
4382 if (proto
== Q_TCP
) {
4383 if (real_proto
== IPPROTO_UDP
)
4384 bpf_error("port '%s' is udp", name
);
4386 else if (real_proto
== IPPROTO_SCTP
)
4387 bpf_error("port '%s' is sctp", name
);
4389 /* override PROTO_UNDEF */
4390 real_proto
= IPPROTO_TCP
;
4392 if (proto
== Q_SCTP
) {
4393 if (real_proto
== IPPROTO_UDP
)
4394 bpf_error("port '%s' is udp", name
);
4396 else if (real_proto
== IPPROTO_TCP
)
4397 bpf_error("port '%s' is tcp", name
);
4399 /* override PROTO_UNDEF */
4400 real_proto
= IPPROTO_SCTP
;
4403 return gen_port(port
, real_proto
, dir
);
4407 b
= gen_port(port
, real_proto
, dir
);
4408 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4414 if (proto
!= Q_DEFAULT
&&
4415 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4416 bpf_error("illegal qualifier of 'portrange'");
4417 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4418 bpf_error("unknown port in range '%s'", name
);
4419 if (proto
== Q_UDP
) {
4420 if (real_proto
== IPPROTO_TCP
)
4421 bpf_error("port in range '%s' is tcp", name
);
4422 else if (real_proto
== IPPROTO_SCTP
)
4423 bpf_error("port in range '%s' is sctp", name
);
4425 /* override PROTO_UNDEF */
4426 real_proto
= IPPROTO_UDP
;
4428 if (proto
== Q_TCP
) {
4429 if (real_proto
== IPPROTO_UDP
)
4430 bpf_error("port in range '%s' is udp", name
);
4431 else if (real_proto
== IPPROTO_SCTP
)
4432 bpf_error("port in range '%s' is sctp", name
);
4434 /* override PROTO_UNDEF */
4435 real_proto
= IPPROTO_TCP
;
4437 if (proto
== Q_SCTP
) {
4438 if (real_proto
== IPPROTO_UDP
)
4439 bpf_error("port in range '%s' is udp", name
);
4440 else if (real_proto
== IPPROTO_TCP
)
4441 bpf_error("port in range '%s' is tcp", name
);
4443 /* override PROTO_UNDEF */
4444 real_proto
= IPPROTO_SCTP
;
4447 return gen_portrange(port1
, port2
, real_proto
, dir
);
4451 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4452 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4459 eaddr
= pcap_ether_hostton(name
);
4461 bpf_error("unknown ether host: %s", name
);
4463 alist
= pcap_nametoaddr(name
);
4464 if (alist
== NULL
|| *alist
== NULL
)
4465 bpf_error("unknown host '%s'", name
);
4466 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4470 bpf_error("'gateway' not supported in this configuration");
4474 real_proto
= lookup_proto(name
, proto
);
4475 if (real_proto
>= 0)
4476 return gen_proto(real_proto
, proto
, dir
);
4478 bpf_error("unknown protocol: %s", name
);
4481 real_proto
= lookup_proto(name
, proto
);
4482 if (real_proto
>= 0)
4483 return gen_protochain(real_proto
, proto
, dir
);
4485 bpf_error("unknown protocol: %s", name
);
4497 gen_mcode(s1
, s2
, masklen
, q
)
4498 register const char *s1
, *s2
;
4499 register int masklen
;
4502 register int nlen
, mlen
;
4505 nlen
= __pcap_atoin(s1
, &n
);
4506 /* Promote short ipaddr */
4510 mlen
= __pcap_atoin(s2
, &m
);
4511 /* Promote short ipaddr */
4514 bpf_error("non-network bits set in \"%s mask %s\"",
4517 /* Convert mask len to mask */
4519 bpf_error("mask length must be <= 32");
4520 m
= 0xffffffff << (32 - masklen
);
4522 bpf_error("non-network bits set in \"%s/%d\"",
4529 return gen_host(n
, m
, q
.proto
, q
.dir
);
4532 bpf_error("Mask syntax for networks only");
4540 register const char *s
;
4545 int proto
= q
.proto
;
4551 else if (q
.proto
== Q_DECNET
)
4552 vlen
= __pcap_atodn(s
, &v
);
4554 vlen
= __pcap_atoin(s
, &v
);
4561 if (proto
== Q_DECNET
)
4562 return gen_host(v
, 0, proto
, dir
);
4563 else if (proto
== Q_LINK
) {
4564 bpf_error("illegal link layer address");
4567 if (s
== NULL
&& q
.addr
== Q_NET
) {
4568 /* Promote short net number */
4569 while (v
&& (v
& 0xff000000) == 0) {
4574 /* Promote short ipaddr */
4578 return gen_host(v
, mask
, proto
, dir
);
4583 proto
= IPPROTO_UDP
;
4584 else if (proto
== Q_TCP
)
4585 proto
= IPPROTO_TCP
;
4586 else if (proto
== Q_SCTP
)
4587 proto
= IPPROTO_SCTP
;
4588 else if (proto
== Q_DEFAULT
)
4589 proto
= PROTO_UNDEF
;
4591 bpf_error("illegal qualifier of 'port'");
4594 return gen_port((int)v
, proto
, dir
);
4598 b
= gen_port((int)v
, proto
, dir
);
4599 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4606 proto
= IPPROTO_UDP
;
4607 else if (proto
== Q_TCP
)
4608 proto
= IPPROTO_TCP
;
4609 else if (proto
== Q_SCTP
)
4610 proto
= IPPROTO_SCTP
;
4611 else if (proto
== Q_DEFAULT
)
4612 proto
= PROTO_UNDEF
;
4614 bpf_error("illegal qualifier of 'portrange'");
4617 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4621 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4622 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4628 bpf_error("'gateway' requires a name");
4632 return gen_proto((int)v
, proto
, dir
);
4635 return gen_protochain((int)v
, proto
, dir
);
4650 gen_mcode6(s1
, s2
, masklen
, q
)
4651 register const char *s1
, *s2
;
4652 register int masklen
;
4655 struct addrinfo
*res
;
4656 struct in6_addr
*addr
;
4657 struct in6_addr mask
;
4662 bpf_error("no mask %s supported", s2
);
4664 res
= pcap_nametoaddrinfo(s1
);
4666 bpf_error("invalid ip6 address %s", s1
);
4668 bpf_error("%s resolved to multiple address", s1
);
4669 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4671 if (sizeof(mask
) * 8 < masklen
)
4672 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4673 memset(&mask
, 0, sizeof(mask
));
4674 memset(&mask
, 0xff, masklen
/ 8);
4676 mask
.s6_addr
[masklen
/ 8] =
4677 (0xff << (8 - masklen
% 8)) & 0xff;
4680 a
= (u_int32_t
*)addr
;
4681 m
= (u_int32_t
*)&mask
;
4682 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4683 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4684 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4692 bpf_error("Mask syntax for networks only");
4696 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4701 bpf_error("invalid qualifier against IPv6 address");
4709 register const u_char
*eaddr
;
4712 struct block
*b
, *tmp
;
4714 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4715 if (linktype
== DLT_EN10MB
)
4716 return gen_ehostop(eaddr
, (int)q
.dir
);
4717 if (linktype
== DLT_FDDI
)
4718 return gen_fhostop(eaddr
, (int)q
.dir
);
4719 if (linktype
== DLT_IEEE802
)
4720 return gen_thostop(eaddr
, (int)q
.dir
);
4721 if (linktype
== DLT_IEEE802_11
)
4722 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4723 if (linktype
== DLT_SUNATM
&& is_lane
) {
4725 * Check that the packet doesn't begin with an
4726 * LE Control marker. (We've already generated
4729 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4733 * Now check the MAC address.
4735 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4739 if (linktype
== DLT_IP_OVER_FC
)
4740 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4741 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4743 bpf_error("ethernet address used in non-ether expression");
4749 struct slist
*s0
, *s1
;
4752 * This is definitely not the best way to do this, but the
4753 * lists will rarely get long.
4760 static struct slist
*
4766 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4771 static struct slist
*
4777 s
= new_stmt(BPF_LD
|BPF_MEM
);
4783 gen_load(proto
, index
, size
)
4788 struct slist
*s
, *tmp
;
4790 int regno
= alloc_reg();
4792 free_reg(index
->regno
);
4796 bpf_error("data size must be 1, 2, or 4");
4812 bpf_error("unsupported index operation");
4816 * XXX - what about ATM LANE? Should the index be
4817 * relative to the beginning of the AAL5 frame, so
4818 * that 0 refers to the beginning of the LE Control
4819 * field, or relative to the beginning of the LAN
4820 * frame, so that 0 refers, for Ethernet LANE, to
4821 * the beginning of the destination address?
4823 s
= xfer_to_x(index
);
4824 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4826 sappend(index
->s
, s
);
4841 /* XXX Note that we assume a fixed link header here. */
4842 s
= xfer_to_x(index
);
4843 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4846 sappend(index
->s
, s
);
4848 b
= gen_proto_abbrev(proto
);
4850 gen_and(index
->b
, b
);
4862 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4864 sappend(s
, xfer_to_a(index
));
4865 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4866 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4867 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4869 sappend(index
->s
, s
);
4871 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4873 gen_and(index
->b
, b
);
4875 gen_and(gen_proto_abbrev(Q_IP
), b
);
4881 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4885 index
->regno
= regno
;
4886 s
= new_stmt(BPF_ST
);
4888 sappend(index
->s
, s
);
4894 gen_relation(code
, a0
, a1
, reversed
)
4896 struct arth
*a0
, *a1
;
4899 struct slist
*s0
, *s1
, *s2
;
4900 struct block
*b
, *tmp
;
4904 if (code
== BPF_JEQ
) {
4905 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4906 b
= new_block(JMP(code
));
4910 b
= new_block(BPF_JMP
|code
|BPF_X
);
4916 sappend(a0
->s
, a1
->s
);
4920 free_reg(a0
->regno
);
4921 free_reg(a1
->regno
);
4923 /* 'and' together protocol checks */
4926 gen_and(a0
->b
, tmp
= a1
->b
);
4942 int regno
= alloc_reg();
4943 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4946 s
= new_stmt(BPF_LD
|BPF_LEN
);
4947 s
->next
= new_stmt(BPF_ST
);
4948 s
->next
->s
.k
= regno
;
4963 a
= (struct arth
*)newchunk(sizeof(*a
));
4967 s
= new_stmt(BPF_LD
|BPF_IMM
);
4969 s
->next
= new_stmt(BPF_ST
);
4985 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4988 s
= new_stmt(BPF_ST
);
4996 gen_arth(code
, a0
, a1
)
4998 struct arth
*a0
, *a1
;
5000 struct slist
*s0
, *s1
, *s2
;
5004 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5009 sappend(a0
->s
, a1
->s
);
5011 free_reg(a0
->regno
);
5012 free_reg(a1
->regno
);
5014 s0
= new_stmt(BPF_ST
);
5015 a0
->regno
= s0
->s
.k
= alloc_reg();
5022 * Here we handle simple allocation of the scratch registers.
5023 * If too many registers are alloc'd, the allocator punts.
5025 static int regused
[BPF_MEMWORDS
];
5029 * Return the next free register.
5034 int n
= BPF_MEMWORDS
;
5037 if (regused
[curreg
])
5038 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5040 regused
[curreg
] = 1;
5044 bpf_error("too many registers needed to evaluate expression");
5049 * Return a register to the table so it can
5059 static struct block
*
5066 s
= new_stmt(BPF_LD
|BPF_LEN
);
5067 b
= new_block(JMP(jmp
));
5078 return gen_len(BPF_JGE
, n
);
5082 * Actually, this is less than or equal.
5090 b
= gen_len(BPF_JGT
, n
);
5097 gen_byteop(op
, idx
, val
)
5108 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5111 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5112 b
->s
.code
= JMP(BPF_JGE
);
5117 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5118 b
->s
.code
= JMP(BPF_JGT
);
5122 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5126 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5130 b
= new_block(JMP(BPF_JEQ
));
5137 static u_char abroadcast
[] = { 0x0 };
5140 gen_broadcast(proto
)
5143 bpf_u_int32 hostmask
;
5144 struct block
*b0
, *b1
, *b2
;
5145 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5151 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5152 return gen_ahostop(abroadcast
, Q_DST
);
5153 if (linktype
== DLT_EN10MB
)
5154 return gen_ehostop(ebroadcast
, Q_DST
);
5155 if (linktype
== DLT_FDDI
)
5156 return gen_fhostop(ebroadcast
, Q_DST
);
5157 if (linktype
== DLT_IEEE802
)
5158 return gen_thostop(ebroadcast
, Q_DST
);
5159 if (linktype
== DLT_IEEE802_11
)
5160 return gen_wlanhostop(ebroadcast
, Q_DST
);
5161 if (linktype
== DLT_IP_OVER_FC
)
5162 return gen_ipfchostop(ebroadcast
, Q_DST
);
5163 if (linktype
== DLT_SUNATM
&& is_lane
) {
5165 * Check that the packet doesn't begin with an
5166 * LE Control marker. (We've already generated
5169 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5173 * Now check the MAC address.
5175 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5179 bpf_error("not a broadcast link");
5183 b0
= gen_linktype(ETHERTYPE_IP
);
5184 hostmask
= ~netmask
;
5185 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
5186 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
5187 (bpf_int32
)(~0 & hostmask
), hostmask
);
5192 bpf_error("only link-layer/IP broadcast filters supported");
5197 * Generate code to test the low-order bit of a MAC address (that's
5198 * the bottom bit of the *first* byte).
5200 static struct block
*
5201 gen_mac_multicast(offset
)
5204 register struct block
*b0
;
5205 register struct slist
*s
;
5207 /* link[offset] & 1 != 0 */
5208 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5210 b0
= new_block(JMP(BPF_JSET
));
5217 gen_multicast(proto
)
5220 register struct block
*b0
, *b1
, *b2
;
5221 register struct slist
*s
;
5227 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5228 /* all ARCnet multicasts use the same address */
5229 return gen_ahostop(abroadcast
, Q_DST
);
5231 if (linktype
== DLT_EN10MB
) {
5232 /* ether[0] & 1 != 0 */
5233 return gen_mac_multicast(0);
5236 if (linktype
== DLT_FDDI
) {
5238 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5240 * XXX - was that referring to bit-order issues?
5242 /* fddi[1] & 1 != 0 */
5243 return gen_mac_multicast(1);
5246 if (linktype
== DLT_IEEE802
) {
5247 /* tr[2] & 1 != 0 */
5248 return gen_mac_multicast(2);
5251 if (linktype
== DLT_IEEE802_11
) {
5255 * For control frames, there is no DA.
5257 * For management frames, DA is at an
5258 * offset of 4 from the beginning of
5261 * For data frames, DA is at an offset
5262 * of 4 from the beginning of the packet
5263 * if To DS is clear and at an offset of
5264 * 16 from the beginning of the packet
5269 * Generate the tests to be done for data frames.
5271 * First, check for To DS set, i.e. "link[1] & 0x01".
5273 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5275 b1
= new_block(JMP(BPF_JSET
));
5276 b1
->s
.k
= 0x01; /* To DS */
5280 * If To DS is set, the DA is at 16.
5282 b0
= gen_mac_multicast(16);
5286 * Now, check for To DS not set, i.e. check
5287 * "!(link[1] & 0x01)".
5289 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5291 b2
= new_block(JMP(BPF_JSET
));
5292 b2
->s
.k
= 0x01; /* To DS */
5297 * If To DS is not set, the DA is at 4.
5299 b1
= gen_mac_multicast(4);
5303 * Now OR together the last two checks. That gives
5304 * the complete set of checks for data frames.
5309 * Now check for a data frame.
5310 * I.e, check "link[0] & 0x08".
5312 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5314 b1
= new_block(JMP(BPF_JSET
));
5319 * AND that with the checks done for data frames.
5324 * If the high-order bit of the type value is 0, this
5325 * is a management frame.
5326 * I.e, check "!(link[0] & 0x08)".
5328 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5330 b2
= new_block(JMP(BPF_JSET
));
5336 * For management frames, the DA is at 4.
5338 b1
= gen_mac_multicast(4);
5342 * OR that with the checks done for data frames.
5343 * That gives the checks done for management and
5349 * If the low-order bit of the type value is 1,
5350 * this is either a control frame or a frame
5351 * with a reserved type, and thus not a
5354 * I.e., check "!(link[0] & 0x04)".
5356 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5358 b1
= new_block(JMP(BPF_JSET
));
5364 * AND that with the checks for data and management
5371 if (linktype
== DLT_IP_OVER_FC
) {
5372 b0
= gen_mac_multicast(2);
5376 if (linktype
== DLT_SUNATM
&& is_lane
) {
5378 * Check that the packet doesn't begin with an
5379 * LE Control marker. (We've already generated
5382 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5385 /* ether[off_mac] & 1 != 0 */
5386 b0
= gen_mac_multicast(off_mac
);
5391 /* Link not known to support multicasts */
5395 b0
= gen_linktype(ETHERTYPE_IP
);
5396 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5397 b1
->s
.code
= JMP(BPF_JGE
);
5403 b0
= gen_linktype(ETHERTYPE_IPV6
);
5404 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5409 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5414 * generate command for inbound/outbound. It's here so we can
5415 * make it link-type specific. 'dir' = 0 implies "inbound",
5416 * = 1 implies "outbound".
5422 register struct block
*b0
;
5425 * Only some data link types support inbound/outbound qualifiers.
5429 b0
= gen_relation(BPF_JEQ
,
5430 gen_load(Q_LINK
, gen_loadi(0), 1),
5438 * Match packets sent by this machine.
5440 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5443 * Match packets sent to this machine.
5444 * (No broadcast or multicast packets, or
5445 * packets sent to some other machine and
5446 * received promiscuously.)
5448 * XXX - packets sent to other machines probably
5449 * shouldn't be matched, but what about broadcast
5450 * or multicast packets we received?
5452 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5457 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5458 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5463 /* match outgoing packets */
5464 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5466 /* match incoming packets */
5467 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5471 case DLT_JUNIPER_MLFR
:
5472 case DLT_JUNIPER_MLPPP
:
5473 case DLT_JUNIPER_ATM1
:
5474 case DLT_JUNIPER_ATM2
:
5475 /* juniper flags (including direction) are stored
5476 * the byte after the 3-byte magic number */
5478 /* match outgoing packets */
5479 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5481 /* match incoming packets */
5482 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5487 bpf_error("inbound/outbound not supported on linktype %d",
5495 /* PF firewall log matched interface */
5497 gen_pf_ifname(const char *ifname
)
5502 if (linktype
== DLT_PFLOG
) {
5503 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5504 off
= offsetof(struct pfloghdr
, ifname
);
5506 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5509 if (strlen(ifname
) >= len
) {
5510 bpf_error("ifname interface names can only be %d characters",
5514 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5518 /* PF firewall log matched interface */
5520 gen_pf_ruleset(char *ruleset
)
5524 if (linktype
!= DLT_PFLOG
) {
5525 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5528 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5529 bpf_error("ruleset names can only be %ld characters",
5530 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5533 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5534 strlen(ruleset
), (const u_char
*)ruleset
);
5538 /* PF firewall log rule number */
5544 if (linktype
== DLT_PFLOG
) {
5545 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5548 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5555 /* PF firewall log sub-rule number */
5557 gen_pf_srnr(int srnr
)
5561 if (linktype
!= DLT_PFLOG
) {
5562 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5566 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5571 /* PF firewall log reason code */
5573 gen_pf_reason(int reason
)
5577 if (linktype
== DLT_PFLOG
) {
5578 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5581 bpf_error("reason not supported on linktype 0x%x", linktype
);
5588 /* PF firewall log action */
5590 gen_pf_action(int action
)
5594 if (linktype
== DLT_PFLOG
) {
5595 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5598 bpf_error("action not supported on linktype 0x%x", linktype
);
5607 register const u_char
*eaddr
;
5610 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5611 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5612 return gen_ahostop(eaddr
, (int)q
.dir
);
5614 bpf_error("ARCnet address used in non-arc expression");
5618 static struct block
*
5619 gen_ahostop(eaddr
, dir
)
5620 register const u_char
*eaddr
;
5623 register struct block
*b0
, *b1
;
5626 /* src comes first, different from Ethernet */
5628 return gen_bcmp(0, 1, eaddr
);
5631 return gen_bcmp(1, 1, eaddr
);
5634 b0
= gen_ahostop(eaddr
, Q_SRC
);
5635 b1
= gen_ahostop(eaddr
, Q_DST
);
5641 b0
= gen_ahostop(eaddr
, Q_SRC
);
5642 b1
= gen_ahostop(eaddr
, Q_DST
);
5651 * support IEEE 802.1Q VLAN trunk over ethernet
5660 * Change the offsets to point to the type and data fields within
5661 * the VLAN packet. Just increment the offsets, so that we
5662 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5663 * capture VLAN 200 encapsulated within VLAN 100.
5665 * XXX - this is a bit of a kludge. If we were to split the
5666 * compiler into a parser that parses an expression and
5667 * generates an expression tree, and a code generator that
5668 * takes an expression tree (which could come from our
5669 * parser or from some other parser) and generates BPF code,
5670 * we could perhaps make the offsets parameters of routines
5671 * and, in the handler for an "AND" node, pass to subnodes
5672 * other than the VLAN node the adjusted offsets.
5674 * This would mean that "vlan" would, instead of changing the
5675 * behavior of *all* tests after it, change only the behavior
5676 * of tests ANDed with it. That would change the documented
5677 * semantics of "vlan", which might break some expressions.
5678 * However, it would mean that "(vlan and ip) or ip" would check
5679 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5680 * checking only for VLAN-encapsulated IP, so that could still
5681 * be considered worth doing; it wouldn't break expressions
5682 * that are of the form "vlan and ..." or "vlan N and ...",
5683 * which I suspect are the most common expressions involving
5684 * "vlan". "vlan or ..." doesn't necessarily do what the user
5685 * would really want, now, as all the "or ..." tests would
5686 * be done assuming a VLAN, even though the "or" could be viewed
5687 * as meaning "or, if this isn't a VLAN packet...".
5689 orig_linktype
= off_linktype
; /* save original values */
5701 bpf_error("no VLAN support for data link type %d",
5706 /* check for VLAN */
5707 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5709 /* If a specific VLAN is requested, check VLAN id */
5710 if (vlan_num
>= 0) {
5713 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5731 * Change the offsets to point to the type and data fields within
5732 * the MPLS packet. Just increment the offsets, so that we
5733 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5734 * capture packets with an outer label of 100000 and an inner
5737 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5739 orig_linktype
= off_linktype
; /* save original values */
5744 case DLT_C_HDLC
: /* fall through */
5750 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5758 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5761 /* FIXME add other DLT_s ...
5762 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5763 * leave it for now */
5766 bpf_error("no MPLS support for data link type %d",
5773 /* If a specific MPLS label is requested, check it */
5774 if (label_num
>= 0) {
5777 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5778 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5787 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5799 bpf_error("'vpi' supported only on raw ATM");
5800 if (off_vpi
== (u_int
)-1)
5802 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5803 (u_int
)jvalue
, reverse
);
5808 bpf_error("'vci' supported only on raw ATM");
5809 if (off_vci
== (u_int
)-1)
5811 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5812 (u_int
)jvalue
, reverse
);
5816 if (off_proto
== (u_int
)-1)
5817 abort(); /* XXX - this isn't on FreeBSD */
5818 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5819 (u_int
)jvalue
, reverse
);
5823 if (off_payload
== (u_int
)-1)
5825 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5826 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5831 bpf_error("'callref' supported only on raw ATM");
5832 if (off_proto
== (u_int
)-1)
5834 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5835 (u_int
)jvalue
, reverse
);
5845 gen_atmtype_abbrev(type
)
5848 struct block
*b0
, *b1
;
5853 /* Get all packets in Meta signalling Circuit */
5855 bpf_error("'metac' supported only on raw ATM");
5856 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5857 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5862 /* Get all packets in Broadcast Circuit*/
5864 bpf_error("'bcc' supported only on raw ATM");
5865 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5866 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5871 /* Get all cells in Segment OAM F4 circuit*/
5873 bpf_error("'oam4sc' supported only on raw ATM");
5874 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5875 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5880 /* Get all cells in End-to-End OAM F4 Circuit*/
5882 bpf_error("'oam4ec' supported only on raw ATM");
5883 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5884 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5889 /* Get all packets in connection Signalling Circuit */
5891 bpf_error("'sc' supported only on raw ATM");
5892 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5893 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5898 /* Get all packets in ILMI Circuit */
5900 bpf_error("'ilmic' supported only on raw ATM");
5901 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5902 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5907 /* Get all LANE packets */
5909 bpf_error("'lane' supported only on raw ATM");
5910 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5913 * Arrange that all subsequent tests assume LANE
5914 * rather than LLC-encapsulated packets, and set
5915 * the offsets appropriately for LANE-encapsulated
5918 * "off_mac" is the offset of the Ethernet header,
5919 * which is 2 bytes past the ATM pseudo-header
5920 * (skipping the pseudo-header and 2-byte LE Client
5921 * field). The other offsets are Ethernet offsets
5922 * relative to "off_mac".
5925 off_mac
= off_payload
+ 2; /* MAC header */
5926 off_linktype
= off_mac
+ 12;
5927 off_nl
= off_mac
+ 14; /* Ethernet II */
5928 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5932 /* Get all LLC-encapsulated packets */
5934 bpf_error("'llc' supported only on raw ATM");
5935 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5946 static struct block
*
5947 gen_msg_abbrev(type
)
5953 * Q.2931 signalling protocol messages for handling virtual circuits
5954 * establishment and teardown
5959 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5963 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5967 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5971 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5975 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5978 case A_RELEASE_DONE
:
5979 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5989 gen_atmmulti_abbrev(type
)
5992 struct block
*b0
, *b1
;
5998 bpf_error("'oam' supported only on raw ATM");
5999 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6004 bpf_error("'oamf4' supported only on raw ATM");
6006 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6007 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6009 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6015 * Get Q.2931 signalling messages for switched
6016 * virtual connection
6019 bpf_error("'connectmsg' supported only on raw ATM");
6020 b0
= gen_msg_abbrev(A_SETUP
);
6021 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6023 b0
= gen_msg_abbrev(A_CONNECT
);
6025 b0
= gen_msg_abbrev(A_CONNECTACK
);
6027 b0
= gen_msg_abbrev(A_RELEASE
);
6029 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6031 b0
= gen_atmtype_abbrev(A_SC
);
6037 bpf_error("'metaconnect' supported only on raw ATM");
6038 b0
= gen_msg_abbrev(A_SETUP
);
6039 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6041 b0
= gen_msg_abbrev(A_CONNECT
);
6043 b0
= gen_msg_abbrev(A_RELEASE
);
6045 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6047 b0
= gen_atmtype_abbrev(A_METAC
);