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.6 2005-04-20 18:23:42 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U;
108 static int pcap_fddipad
;
111 void bpf_error(const char *fmt
, ...) __attribute__((noreturn
));
115 bpf_error(const char *fmt
, ...)
120 if (bpf_pcap
!= NULL
)
121 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
128 static void init_linktype(pcap_t
*);
130 static int alloc_reg(void);
131 static void free_reg(int);
133 static struct block
*root
;
136 * We divy out chunks of memory rather than call malloc each time so
137 * we don't have to worry about leaking memory. It's probably
138 * not a big deal if all this memory was wasted but if this ever
139 * goes into a library that would probably not be a good idea.
141 * XXX - this *is* in a library....
144 #define CHUNK0SIZE 1024
150 static struct chunk chunks
[NCHUNKS
];
151 static int cur_chunk
;
153 static void *newchunk(u_int
);
154 static void freechunks(void);
155 static inline struct block
*new_block(int);
156 static inline struct slist
*new_stmt(int);
157 static struct block
*gen_retblk(int);
158 static inline void syntax(void);
160 static void backpatch(struct block
*, struct block
*);
161 static void merge(struct block
*, struct block
*);
162 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
163 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
164 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
165 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
166 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
167 bpf_u_int32
, bpf_u_int32
, int);
168 static struct block
*gen_uncond(int);
169 static inline struct block
*gen_true(void);
170 static inline struct block
*gen_false(void);
171 static struct block
*gen_ether_linktype(int);
172 static struct block
*gen_linux_sll_linktype(int);
173 static struct block
*gen_linktype(int);
174 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
175 static struct block
*gen_llc_linktype(int);
176 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
178 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
180 static struct block
*gen_ahostop(const u_char
*, int);
181 static struct block
*gen_ehostop(const u_char
*, int);
182 static struct block
*gen_fhostop(const u_char
*, int);
183 static struct block
*gen_thostop(const u_char
*, int);
184 static struct block
*gen_wlanhostop(const u_char
*, int);
185 static struct block
*gen_ipfchostop(const u_char
*, int);
186 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
187 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
189 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
192 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
194 static struct block
*gen_ipfrag(void);
195 static struct block
*gen_portatom(int, bpf_int32
);
196 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
198 static struct block
*gen_portatom6(int, bpf_int32
);
199 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
201 struct block
*gen_portop(int, int, int);
202 static struct block
*gen_port(int, int, int);
203 struct block
*gen_portrangeop(int, int, int, int);
204 static struct block
*gen_portrange(int, int, int, int);
206 struct block
*gen_portop6(int, int, int);
207 static struct block
*gen_port6(int, int, int);
208 struct block
*gen_portrangeop6(int, int, int, int);
209 static struct block
*gen_portrange6(int, int, int, int);
211 static int lookup_proto(const char *, int);
212 static struct block
*gen_protochain(int, int, int);
213 static struct block
*gen_proto(int, int, int);
214 static struct slist
*xfer_to_x(struct arth
*);
215 static struct slist
*xfer_to_a(struct arth
*);
216 static struct block
*gen_mac_multicast(int);
217 static struct block
*gen_len(int, int);
219 static struct block
*gen_msg_abbrev(int type
);
230 /* XXX Round up to nearest long. */
231 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
233 /* XXX Round up to structure boundary. */
237 cp
= &chunks
[cur_chunk
];
238 if (n
> cp
->n_left
) {
239 ++cp
, k
= ++cur_chunk
;
241 bpf_error("out of memory");
242 size
= CHUNK0SIZE
<< k
;
243 cp
->m
= (void *)malloc(size
);
245 bpf_error("out of memory");
246 memset((char *)cp
->m
, 0, size
);
249 bpf_error("out of memory");
252 return (void *)((char *)cp
->m
+ cp
->n_left
);
261 for (i
= 0; i
< NCHUNKS
; ++i
)
262 if (chunks
[i
].m
!= NULL
) {
269 * A strdup whose allocations are freed after code generation is over.
273 register const char *s
;
275 int n
= strlen(s
) + 1;
276 char *cp
= newchunk(n
);
282 static inline struct block
*
288 p
= (struct block
*)newchunk(sizeof(*p
));
295 static inline struct slist
*
301 p
= (struct slist
*)newchunk(sizeof(*p
));
307 static struct block
*
311 struct block
*b
= new_block(BPF_RET
|BPF_K
);
320 bpf_error("syntax error in filter expression");
323 static bpf_u_int32 netmask
;
328 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
329 char *buf
, int optimize
, bpf_u_int32 mask
)
338 if (setjmp(top_ctx
)) {
346 snaplen
= pcap_snapshot(p
);
348 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
349 "snaplen of 0 rejects all packets");
353 lex_init(buf
? buf
: "");
361 root
= gen_retblk(snaplen
);
363 if (optimize
&& !no_optimize
) {
366 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
367 bpf_error("expression rejects all packets");
369 program
->bf_insns
= icode_to_fcode(root
, &len
);
370 program
->bf_len
= len
;
378 * entry point for using the compiler with no pcap open
379 * pass in all the stuff that is needed explicitly instead.
382 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
383 struct bpf_program
*program
,
384 char *buf
, int optimize
, bpf_u_int32 mask
)
389 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
392 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
398 * Clean up a "struct bpf_program" by freeing all the memory allocated
402 pcap_freecode(struct bpf_program
*program
)
405 if (program
->bf_insns
!= NULL
) {
406 free((char *)program
->bf_insns
);
407 program
->bf_insns
= NULL
;
412 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
413 * which of the jt and jf fields has been resolved and which is a pointer
414 * back to another unresolved block (or nil). At least one of the fields
415 * in each block is already resolved.
418 backpatch(list
, target
)
419 struct block
*list
, *target
;
436 * Merge the lists in b0 and b1, using the 'sense' field to indicate
437 * which of jt and jf is the link.
441 struct block
*b0
, *b1
;
443 register struct block
**p
= &b0
;
445 /* Find end of list. */
447 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
449 /* Concatenate the lists. */
457 backpatch(p
, gen_retblk(snaplen
));
458 p
->sense
= !p
->sense
;
459 backpatch(p
, gen_retblk(0));
465 struct block
*b0
, *b1
;
467 backpatch(b0
, b1
->head
);
468 b0
->sense
= !b0
->sense
;
469 b1
->sense
= !b1
->sense
;
471 b1
->sense
= !b1
->sense
;
477 struct block
*b0
, *b1
;
479 b0
->sense
= !b0
->sense
;
480 backpatch(b0
, b1
->head
);
481 b0
->sense
= !b0
->sense
;
490 b
->sense
= !b
->sense
;
493 static struct block
*
494 gen_cmp(offset
, size
, v
)
501 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
504 b
= new_block(JMP(BPF_JEQ
));
511 static struct block
*
512 gen_cmp_gt(offset
, size
, v
)
519 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
522 b
= new_block(JMP(BPF_JGT
));
529 static struct block
*
530 gen_mcmp(offset
, size
, v
, mask
)
535 struct block
*b
= gen_cmp(offset
, size
, v
);
538 if (mask
!= 0xffffffff) {
539 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
546 static struct block
*
547 gen_bcmp(offset
, size
, v
)
548 register u_int offset
, size
;
549 register const u_char
*v
;
551 register struct block
*b
, *tmp
;
555 register const u_char
*p
= &v
[size
- 4];
556 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
557 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
559 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
566 register const u_char
*p
= &v
[size
- 2];
567 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
569 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
576 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
584 static struct block
*
585 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
586 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
592 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
595 if (mask
!= 0xffffffff) {
596 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
600 b
= new_block(JMP(jtype
));
603 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
609 * Various code constructs need to know the layout of the data link
610 * layer. These variables give the necessary offsets.
614 * This is the offset of the beginning of the MAC-layer header.
615 * It's usually 0, except for ATM LANE.
617 static u_int off_mac
;
620 * "off_linktype" is the offset to information in the link-layer header
621 * giving the packet type.
623 * For Ethernet, it's the offset of the Ethernet type field.
625 * For link-layer types that always use 802.2 headers, it's the
626 * offset of the LLC header.
628 * For PPP, it's the offset of the PPP type field.
630 * For Cisco HDLC, it's the offset of the CHDLC type field.
632 * For BSD loopback, it's the offset of the AF_ value.
634 * For Linux cooked sockets, it's the offset of the type field.
636 * It's set to -1 for no encapsulation, in which case, IP is assumed.
638 static u_int off_linktype
;
641 * TRUE if the link layer includes an ATM pseudo-header.
643 static int is_atm
= 0;
646 * TRUE if "lane" appeared in the filter; it causes us to generate
647 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
649 static int is_lane
= 0;
652 * These are offsets for the ATM pseudo-header.
654 static u_int off_vpi
;
655 static u_int off_vci
;
656 static u_int off_proto
;
659 * This is the offset of the first byte after the ATM pseudo_header,
660 * or -1 if there is no ATM pseudo-header.
662 static u_int off_payload
;
665 * These are offsets to the beginning of the network-layer header.
667 * If the link layer never uses 802.2 LLC:
669 * "off_nl" and "off_nl_nosnap" are the same.
671 * If the link layer always uses 802.2 LLC:
673 * "off_nl" is the offset if there's a SNAP header following
676 * "off_nl_nosnap" is the offset if there's no SNAP header.
678 * If the link layer is Ethernet:
680 * "off_nl" is the offset if the packet is an Ethernet II packet
681 * (we assume no 802.3+802.2+SNAP);
683 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
684 * with an 802.2 header following it.
687 static u_int off_nl_nosnap
;
695 linktype
= pcap_datalink(p
);
697 pcap_fddipad
= p
->fddipad
;
701 * Assume it's not raw ATM with a pseudo-header, for now.
718 off_nl
= 6; /* XXX in reality, variable! */
719 off_nl_nosnap
= 6; /* no 802.2 LLC */
722 case DLT_ARCNET_LINUX
:
724 off_nl
= 8; /* XXX in reality, variable! */
725 off_nl_nosnap
= 8; /* no 802.2 LLC */
730 off_nl
= 14; /* Ethernet II */
731 off_nl_nosnap
= 17; /* 802.3+802.2 */
736 * SLIP doesn't have a link level type. The 16 byte
737 * header is hacked into our SLIP driver.
741 off_nl_nosnap
= 16; /* no 802.2 LLC */
745 /* XXX this may be the same as the DLT_PPP_BSDOS case */
749 off_nl_nosnap
= 24; /* no 802.2 LLC */
756 off_nl_nosnap
= 4; /* no 802.2 LLC */
762 off_nl_nosnap
= 12; /* no 802.2 LLC */
767 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
768 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
771 off_nl_nosnap
= 4; /* no 802.2 LLC */
776 * This does no include the Ethernet header, and
777 * only covers session state.
781 off_nl_nosnap
= 8; /* no 802.2 LLC */
787 off_nl_nosnap
= 24; /* no 802.2 LLC */
792 * FDDI doesn't really have a link-level type field.
793 * We set "off_linktype" to the offset of the LLC header.
795 * To check for Ethernet types, we assume that SSAP = SNAP
796 * is being used and pick out the encapsulated Ethernet type.
797 * XXX - should we generate code to check for SNAP?
801 off_linktype
+= pcap_fddipad
;
803 off_nl
= 21; /* FDDI+802.2+SNAP */
804 off_nl_nosnap
= 16; /* FDDI+802.2 */
806 off_nl
+= pcap_fddipad
;
807 off_nl_nosnap
+= pcap_fddipad
;
813 * Token Ring doesn't really have a link-level type field.
814 * We set "off_linktype" to the offset of the LLC header.
816 * To check for Ethernet types, we assume that SSAP = SNAP
817 * is being used and pick out the encapsulated Ethernet type.
818 * XXX - should we generate code to check for SNAP?
820 * XXX - the header is actually variable-length.
821 * Some various Linux patched versions gave 38
822 * as "off_linktype" and 40 as "off_nl"; however,
823 * if a token ring packet has *no* routing
824 * information, i.e. is not source-routed, the correct
825 * values are 20 and 22, as they are in the vanilla code.
827 * A packet is source-routed iff the uppermost bit
828 * of the first byte of the source address, at an
829 * offset of 8, has the uppermost bit set. If the
830 * packet is source-routed, the total number of bytes
831 * of routing information is 2 plus bits 0x1F00 of
832 * the 16-bit value at an offset of 14 (shifted right
833 * 8 - figure out which byte that is).
836 off_nl
= 22; /* Token Ring+802.2+SNAP */
837 off_nl_nosnap
= 17; /* Token Ring+802.2 */
842 * 802.11 doesn't really have a link-level type field.
843 * We set "off_linktype" to the offset of the LLC header.
845 * To check for Ethernet types, we assume that SSAP = SNAP
846 * is being used and pick out the encapsulated Ethernet type.
847 * XXX - should we generate code to check for SNAP?
849 * XXX - the header is actually variable-length. We
850 * assume a 24-byte link-layer header, as appears in
851 * data frames in networks with no bridges. If the
852 * fromds and tods 802.11 header bits are both set,
853 * it's actually supposed to be 30 bytes.
856 off_nl
= 32; /* 802.11+802.2+SNAP */
857 off_nl_nosnap
= 27; /* 802.11+802.2 */
860 case DLT_PRISM_HEADER
:
862 * Same as 802.11, but with an additional header before
863 * the 802.11 header, containing a bunch of additional
864 * information including radio-level information.
866 * The header is 144 bytes long.
868 * XXX - same variable-length header problem; at least
869 * the Prism header is fixed-length.
871 off_linktype
= 144+24;
872 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
873 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
876 case DLT_IEEE802_11_RADIO_AVS
:
878 * Same as 802.11, but with an additional header before
879 * the 802.11 header, containing a bunch of additional
880 * information including radio-level information.
882 * The header is 64 bytes long, at least in its
883 * current incarnation.
885 * XXX - same variable-length header problem, only
886 * more so; this header is also variable-length,
887 * with the length being the 32-bit big-endian
888 * number at an offset of 4 from the beginning
889 * of the radio header.
891 off_linktype
= 64+24;
892 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
893 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
896 case DLT_IEEE802_11_RADIO
:
898 * Same as 802.11, but with an additional header before
899 * the 802.11 header, containing a bunch of additional
900 * information including radio-level information.
902 * XXX - same variable-length header problem, only
903 * even *more* so; this header is also variable-length,
904 * with the length being the 16-bit number at an offset
905 * of 2 from the beginning of the radio header, and it's
906 * device-dependent (different devices might supply
907 * different amounts of information), so we can't even
908 * assume a fixed length for the current version of the
911 * Therefore, currently, only raw "link[N:M]" filtering is
919 case DLT_ATM_RFC1483
:
920 case DLT_ATM_CLIP
: /* Linux ATM defines this */
922 * assume routed, non-ISO PDUs
923 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
926 off_nl
= 8; /* 802.2+SNAP */
927 off_nl_nosnap
= 3; /* 802.2 */
932 * Full Frontal ATM; you get AALn PDUs with an ATM
936 off_vpi
= SUNATM_VPI_POS
;
937 off_vci
= SUNATM_VCI_POS
;
938 off_proto
= PROTO_POS
;
939 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
940 off_payload
= SUNATM_PKT_BEGIN_POS
;
941 off_linktype
= off_payload
;
942 off_nl
= off_payload
+8; /* 802.2+SNAP */
943 off_nl_nosnap
= off_payload
+3; /* 802.2 */
949 off_nl_nosnap
= 0; /* no 802.2 LLC */
952 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
955 off_nl_nosnap
= 16; /* no 802.2 LLC */
960 * LocalTalk does have a 1-byte type field in the LLAP header,
961 * but really it just indicates whether there is a "short" or
962 * "long" DDP packet following.
966 off_nl_nosnap
= 0; /* no 802.2 LLC */
971 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
972 * link-level type field. We set "off_linktype" to the
973 * offset of the LLC header.
975 * To check for Ethernet types, we assume that SSAP = SNAP
976 * is being used and pick out the encapsulated Ethernet type.
977 * XXX - should we generate code to check for SNAP? RFC
978 * 2625 says SNAP should be used.
981 off_nl
= 24; /* IPFC+802.2+SNAP */
982 off_nl_nosnap
= 19; /* IPFC+802.2 */
987 * XXX - we should set this to handle SNAP-encapsulated
988 * frames (NLPID of 0x80).
992 off_nl_nosnap
= 0; /* no 802.2 LLC */
995 case DLT_APPLE_IP_OVER_IEEE1394
:
998 off_nl_nosnap
= 18; /* no 802.2 LLC */
1001 case DLT_LINUX_IRDA
:
1003 * Currently, only raw "link[N:M]" filtering is supported.
1012 * Currently, only raw "link[N:M]" filtering is supported.
1019 case DLT_SYMANTEC_FIREWALL
:
1021 off_nl
= 44; /* Ethernet II */
1022 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1027 /* XXX read this from pf.h? */
1028 off_nl
= PFLOG_HDRLEN
;
1029 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1032 case DLT_JUNIPER_MLFR
:
1033 case DLT_JUNIPER_MLPPP
:
1036 off_nl_nosnap
= -1; /* no 802.2 LLC */
1039 case DLT_JUNIPER_ATM1
:
1040 off_linktype
= 4; /* in reality variable between 4-8 */
1045 case DLT_JUNIPER_ATM2
:
1046 off_linktype
= 8; /* in reality variable between 8-12 */
1059 case DLT_LINUX_LAPD
:
1061 * Currently, only raw "link[N:M]" filtering is supported.
1068 bpf_error("unknown data link type %d", linktype
);
1072 static struct block
*
1079 s
= new_stmt(BPF_LD
|BPF_IMM
);
1081 b
= new_block(JMP(BPF_JEQ
));
1087 static inline struct block
*
1090 return gen_uncond(1);
1093 static inline struct block
*
1096 return gen_uncond(0);
1100 * Byte-swap a 32-bit number.
1101 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1102 * big-endian platforms.)
1104 #define SWAPLONG(y) \
1105 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1108 * Generate code to match a particular packet type.
1110 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1111 * value, if <= ETHERMTU. We use that to determine whether to
1112 * match the type/length field or to check the type/length field for
1113 * a value <= ETHERMTU to see whether it's a type field and then do
1114 * the appropriate test.
1116 static struct block
*
1117 gen_ether_linktype(proto
)
1120 struct block
*b0
, *b1
;
1126 case LLCSAP_NETBEUI
:
1128 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1129 * so we check the DSAP and SSAP.
1131 * LLCSAP_IP checks for IP-over-802.2, rather
1132 * than IP-over-Ethernet or IP-over-SNAP.
1134 * XXX - should we check both the DSAP and the
1135 * SSAP, like this, or should we check just the
1136 * DSAP, as we do for other types <= ETHERMTU
1137 * (i.e., other SAP values)?
1139 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1141 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1142 ((proto
<< 8) | proto
));
1150 * Ethernet_II frames, which are Ethernet
1151 * frames with a frame type of ETHERTYPE_IPX;
1153 * Ethernet_802.3 frames, which are 802.3
1154 * frames (i.e., the type/length field is
1155 * a length field, <= ETHERMTU, rather than
1156 * a type field) with the first two bytes
1157 * after the Ethernet/802.3 header being
1160 * Ethernet_802.2 frames, which are 802.3
1161 * frames with an 802.2 LLC header and
1162 * with the IPX LSAP as the DSAP in the LLC
1165 * Ethernet_SNAP frames, which are 802.3
1166 * frames with an LLC header and a SNAP
1167 * header and with an OUI of 0x000000
1168 * (encapsulated Ethernet) and a protocol
1169 * ID of ETHERTYPE_IPX in the SNAP header.
1171 * XXX - should we generate the same code both
1172 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1176 * This generates code to check both for the
1177 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1179 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1180 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1184 * Now we add code to check for SNAP frames with
1185 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1187 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1191 * Now we generate code to check for 802.3
1192 * frames in general.
1194 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1198 * Now add the check for 802.3 frames before the
1199 * check for Ethernet_802.2 and Ethernet_802.3,
1200 * as those checks should only be done on 802.3
1201 * frames, not on Ethernet frames.
1206 * Now add the check for Ethernet_II frames, and
1207 * do that before checking for the other frame
1210 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1214 case ETHERTYPE_ATALK
:
1215 case ETHERTYPE_AARP
:
1217 * EtherTalk (AppleTalk protocols on Ethernet link
1218 * layer) may use 802.2 encapsulation.
1222 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1223 * we check for an Ethernet type field less than
1224 * 1500, which means it's an 802.3 length field.
1226 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1230 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1231 * SNAP packets with an organization code of
1232 * 0x080007 (Apple, for Appletalk) and a protocol
1233 * type of ETHERTYPE_ATALK (Appletalk).
1235 * 802.2-encapsulated ETHERTYPE_AARP packets are
1236 * SNAP packets with an organization code of
1237 * 0x000000 (encapsulated Ethernet) and a protocol
1238 * type of ETHERTYPE_AARP (Appletalk ARP).
1240 if (proto
== ETHERTYPE_ATALK
)
1241 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1242 else /* proto == ETHERTYPE_AARP */
1243 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1247 * Check for Ethernet encapsulation (Ethertalk
1248 * phase 1?); we just check for the Ethernet
1251 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1257 if (proto
<= ETHERMTU
) {
1259 * This is an LLC SAP value, so the frames
1260 * that match would be 802.2 frames.
1261 * Check that the frame is an 802.2 frame
1262 * (i.e., that the length/type field is
1263 * a length field, <= ETHERMTU) and
1264 * then check the DSAP.
1266 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1268 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1273 * This is an Ethernet type, so compare
1274 * the length/type field with it (if
1275 * the frame is an 802.2 frame, the length
1276 * field will be <= ETHERMTU, and, as
1277 * "proto" is > ETHERMTU, this test
1278 * will fail and the frame won't match,
1279 * which is what we want).
1281 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1287 * Generate code to match a particular packet type.
1289 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1290 * value, if <= ETHERMTU. We use that to determine whether to
1291 * match the type field or to check the type field for the special
1292 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1294 static struct block
*
1295 gen_linux_sll_linktype(proto
)
1298 struct block
*b0
, *b1
;
1304 case LLCSAP_NETBEUI
:
1306 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1307 * so we check the DSAP and SSAP.
1309 * LLCSAP_IP checks for IP-over-802.2, rather
1310 * than IP-over-Ethernet or IP-over-SNAP.
1312 * XXX - should we check both the DSAP and the
1313 * SSAP, like this, or should we check just the
1314 * DSAP, as we do for other types <= ETHERMTU
1315 * (i.e., other SAP values)?
1317 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1318 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1319 ((proto
<< 8) | proto
));
1325 * Ethernet_II frames, which are Ethernet
1326 * frames with a frame type of ETHERTYPE_IPX;
1328 * Ethernet_802.3 frames, which have a frame
1329 * type of LINUX_SLL_P_802_3;
1331 * Ethernet_802.2 frames, which are 802.3
1332 * frames with an 802.2 LLC header (i.e, have
1333 * a frame type of LINUX_SLL_P_802_2) and
1334 * with the IPX LSAP as the DSAP in the LLC
1337 * Ethernet_SNAP frames, which are 802.3
1338 * frames with an LLC header and a SNAP
1339 * header and with an OUI of 0x000000
1340 * (encapsulated Ethernet) and a protocol
1341 * ID of ETHERTYPE_IPX in the SNAP header.
1343 * First, do the checks on LINUX_SLL_P_802_2
1344 * frames; generate the check for either
1345 * Ethernet_802.2 or Ethernet_SNAP frames, and
1346 * then put a check for LINUX_SLL_P_802_2 frames
1349 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1350 (bpf_int32
)LLCSAP_IPX
);
1351 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1354 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1358 * Now check for 802.3 frames and OR that with
1359 * the previous test.
1361 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1365 * Now add the check for Ethernet_II frames, and
1366 * do that before checking for the other frame
1369 b0
= gen_cmp(off_linktype
, BPF_H
,
1370 (bpf_int32
)ETHERTYPE_IPX
);
1374 case ETHERTYPE_ATALK
:
1375 case ETHERTYPE_AARP
:
1377 * EtherTalk (AppleTalk protocols on Ethernet link
1378 * layer) may use 802.2 encapsulation.
1382 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1383 * we check for the 802.2 protocol type in the
1384 * "Ethernet type" field.
1386 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1389 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1390 * SNAP packets with an organization code of
1391 * 0x080007 (Apple, for Appletalk) and a protocol
1392 * type of ETHERTYPE_ATALK (Appletalk).
1394 * 802.2-encapsulated ETHERTYPE_AARP packets are
1395 * SNAP packets with an organization code of
1396 * 0x000000 (encapsulated Ethernet) and a protocol
1397 * type of ETHERTYPE_AARP (Appletalk ARP).
1399 if (proto
== ETHERTYPE_ATALK
)
1400 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1402 else /* proto == ETHERTYPE_AARP */
1403 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1408 * Check for Ethernet encapsulation (Ethertalk
1409 * phase 1?); we just check for the Ethernet
1412 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1418 if (proto
<= ETHERMTU
) {
1420 * This is an LLC SAP value, so the frames
1421 * that match would be 802.2 frames.
1422 * Check for the 802.2 protocol type
1423 * in the "Ethernet type" field, and
1424 * then check the DSAP.
1426 b0
= gen_cmp(off_linktype
, BPF_H
,
1428 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1434 * This is an Ethernet type, so compare
1435 * the length/type field with it (if
1436 * the frame is an 802.2 frame, the length
1437 * field will be <= ETHERMTU, and, as
1438 * "proto" is > ETHERMTU, this test
1439 * will fail and the frame won't match,
1440 * which is what we want).
1442 return gen_cmp(off_linktype
, BPF_H
,
1449 * Generate code to match a particular packet type by matching the
1450 * link-layer type field or fields in the 802.2 LLC header.
1452 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1453 * value, if <= ETHERMTU.
1455 static struct block
*
1459 struct block
*b0
, *b1
, *b2
;
1464 return gen_ether_linktype(proto
);
1472 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1476 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1482 case DLT_IEEE802_11
:
1483 case DLT_PRISM_HEADER
:
1484 case DLT_IEEE802_11_RADIO
:
1487 case DLT_ATM_RFC1483
:
1489 case DLT_IP_OVER_FC
:
1490 return gen_llc_linktype(proto
);
1496 * If "is_lane" is set, check for a LANE-encapsulated
1497 * version of this protocol, otherwise check for an
1498 * LLC-encapsulated version of this protocol.
1500 * We assume LANE means Ethernet, not Token Ring.
1504 * Check that the packet doesn't begin with an
1505 * LE Control marker. (We've already generated
1508 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1512 * Now generate an Ethernet test.
1514 b1
= gen_ether_linktype(proto
);
1519 * Check for LLC encapsulation and then check the
1522 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1523 b1
= gen_llc_linktype(proto
);
1531 return gen_linux_sll_linktype(proto
);
1536 case DLT_SLIP_BSDOS
:
1539 * These types don't provide any type field; packets
1542 * XXX - for IPv4, check for a version number of 4, and,
1543 * for IPv6, check for a version number of 6?
1549 case ETHERTYPE_IPV6
:
1551 return gen_true(); /* always true */
1554 return gen_false(); /* always false */
1561 case DLT_PPP_SERIAL
:
1564 * We use Ethernet protocol types inside libpcap;
1565 * map them to the corresponding PPP protocol types.
1574 case ETHERTYPE_IPV6
:
1583 case ETHERTYPE_ATALK
:
1597 * I'm assuming the "Bridging PDU"s that go
1598 * over PPP are Spanning Tree Protocol
1612 * We use Ethernet protocol types inside libpcap;
1613 * map them to the corresponding PPP protocol types.
1618 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1619 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1621 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1626 case ETHERTYPE_IPV6
:
1636 case ETHERTYPE_ATALK
:
1650 * I'm assuming the "Bridging PDU"s that go
1651 * over PPP are Spanning Tree Protocol
1667 * For DLT_NULL, the link-layer header is a 32-bit
1668 * word containing an AF_ value in *host* byte order,
1669 * and for DLT_ENC, the link-layer header begins
1670 * with a 32-bit work containing an AF_ value in
1673 * In addition, if we're reading a saved capture file,
1674 * the host byte order in the capture may not be the
1675 * same as the host byte order on this machine.
1677 * For DLT_LOOP, the link-layer header is a 32-bit
1678 * word containing an AF_ value in *network* byte order.
1680 * XXX - AF_ values may, unfortunately, be platform-
1681 * dependent; for example, FreeBSD's AF_INET6 is 24
1682 * whilst NetBSD's and OpenBSD's is 26.
1684 * This means that, when reading a capture file, just
1685 * checking for our AF_INET6 value won't work if the
1686 * capture file came from another OS.
1695 case ETHERTYPE_IPV6
:
1702 * Not a type on which we support filtering.
1703 * XXX - support those that have AF_ values
1704 * #defined on this platform, at least?
1709 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1711 * The AF_ value is in host byte order, but
1712 * the BPF interpreter will convert it to
1713 * network byte order.
1715 * If this is a save file, and it's from a
1716 * machine with the opposite byte order to
1717 * ours, we byte-swap the AF_ value.
1719 * Then we run it through "htonl()", and
1720 * generate code to compare against the result.
1722 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1723 bpf_pcap
->sf
.swapped
)
1724 proto
= SWAPLONG(proto
);
1725 proto
= htonl(proto
);
1727 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1731 * af field is host byte order in contrast to the rest of
1734 if (proto
== ETHERTYPE_IP
)
1735 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1736 (bpf_int32
)AF_INET
));
1738 else if (proto
== ETHERTYPE_IPV6
)
1739 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1740 (bpf_int32
)AF_INET6
));
1748 case DLT_ARCNET_LINUX
:
1750 * XXX should we check for first fragment if the protocol
1759 case ETHERTYPE_IPV6
:
1760 return (gen_cmp(off_linktype
, BPF_B
,
1761 (bpf_int32
)ARCTYPE_INET6
));
1765 b0
= gen_cmp(off_linktype
, BPF_B
,
1766 (bpf_int32
)ARCTYPE_IP
);
1767 b1
= gen_cmp(off_linktype
, BPF_B
,
1768 (bpf_int32
)ARCTYPE_IP_OLD
);
1773 b0
= gen_cmp(off_linktype
, BPF_B
,
1774 (bpf_int32
)ARCTYPE_ARP
);
1775 b1
= gen_cmp(off_linktype
, BPF_B
,
1776 (bpf_int32
)ARCTYPE_ARP_OLD
);
1780 case ETHERTYPE_REVARP
:
1781 return (gen_cmp(off_linktype
, BPF_B
,
1782 (bpf_int32
)ARCTYPE_REVARP
));
1784 case ETHERTYPE_ATALK
:
1785 return (gen_cmp(off_linktype
, BPF_B
,
1786 (bpf_int32
)ARCTYPE_ATALK
));
1793 case ETHERTYPE_ATALK
:
1803 * XXX - assumes a 2-byte Frame Relay header with
1804 * DLCI and flags. What if the address is longer?
1810 * Check for the special NLPID for IP.
1812 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1815 case ETHERTYPE_IPV6
:
1817 * Check for the special NLPID for IPv6.
1819 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1824 * Check for several OSI protocols.
1826 * Frame Relay packets typically have an OSI
1827 * NLPID at the beginning; we check for each
1830 * What we check for is the NLPID and a frame
1831 * control field of UI, i.e. 0x03 followed
1834 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1835 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1836 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1847 case DLT_JUNIPER_MLFR
:
1848 case DLT_JUNIPER_MLPPP
:
1849 case DLT_JUNIPER_ATM1
:
1850 case DLT_JUNIPER_ATM2
:
1851 /* just lets verify the magic number for now -
1852 * on ATM we may have up to 6 different encapsulations on the wire
1853 * and need a lot of heuristics to figure out that the payload
1856 * FIXME encapsulation specific BPF_ filters
1858 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1860 case DLT_LINUX_IRDA
:
1861 bpf_error("IrDA link-layer type filtering not implemented");
1864 bpf_error("DOCSIS link-layer type filtering not implemented");
1866 case DLT_LINUX_LAPD
:
1867 bpf_error("LAPD link-layer type filtering not implemented");
1871 * All the types that have no encapsulation should either be
1872 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1873 * all packets are IP packets, or should be handled in some
1874 * special case, if none of them are (if some are and some
1875 * aren't, the lack of encapsulation is a problem, as we'd
1876 * have to find some other way of determining the packet type).
1878 * Therefore, if "off_linktype" is -1, there's an error.
1880 if (off_linktype
== (u_int
)-1)
1884 * Any type not handled above should always have an Ethernet
1885 * type at an offset of "off_linktype". (PPP is partially
1886 * handled above - the protocol type is mapped from the
1887 * Ethernet and LLC types we use internally to the corresponding
1888 * PPP type - but the PPP type is always specified by a value
1889 * at "off_linktype", so we don't have to do the code generation
1892 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1896 * Check for an LLC SNAP packet with a given organization code and
1897 * protocol type; we check the entire contents of the 802.2 LLC and
1898 * snap headers, checking for DSAP and SSAP of SNAP and a control
1899 * field of 0x03 in the LLC header, and for the specified organization
1900 * code and protocol type in the SNAP header.
1902 static struct block
*
1903 gen_snap(orgcode
, ptype
, offset
)
1904 bpf_u_int32 orgcode
;
1908 u_char snapblock
[8];
1910 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1911 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1912 snapblock
[2] = 0x03; /* control = UI */
1913 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1914 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1915 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1916 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1917 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1918 return gen_bcmp(offset
, 8, snapblock
);
1922 * Generate code to match a particular packet type, for link-layer types
1923 * using 802.2 LLC headers.
1925 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1926 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1928 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1929 * value, if <= ETHERMTU. We use that to determine whether to
1930 * match the DSAP or both DSAP and LSAP or to check the OUI and
1931 * protocol ID in a SNAP header.
1933 static struct block
*
1934 gen_llc_linktype(proto
)
1938 * XXX - handle token-ring variable-length header.
1944 case LLCSAP_NETBEUI
:
1946 * XXX - should we check both the DSAP and the
1947 * SSAP, like this, or should we check just the
1948 * DSAP, as we do for other types <= ETHERMTU
1949 * (i.e., other SAP values)?
1951 return gen_cmp(off_linktype
, BPF_H
, (long)
1952 ((proto
<< 8) | proto
));
1956 * XXX - are there ever SNAP frames for IPX on
1957 * non-Ethernet 802.x networks?
1959 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1961 case ETHERTYPE_ATALK
:
1963 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1964 * SNAP packets with an organization code of
1965 * 0x080007 (Apple, for Appletalk) and a protocol
1966 * type of ETHERTYPE_ATALK (Appletalk).
1968 * XXX - check for an organization code of
1969 * encapsulated Ethernet as well?
1971 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1975 * XXX - we don't have to check for IPX 802.3
1976 * here, but should we check for the IPX Ethertype?
1978 if (proto
<= ETHERMTU
) {
1980 * This is an LLC SAP value, so check
1983 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1986 * This is an Ethernet type; we assume that it's
1987 * unlikely that it'll appear in the right place
1988 * at random, and therefore check only the
1989 * location that would hold the Ethernet type
1990 * in a SNAP frame with an organization code of
1991 * 0x000000 (encapsulated Ethernet).
1993 * XXX - if we were to check for the SNAP DSAP and
1994 * LSAP, as per XXX, and were also to check for an
1995 * organization code of 0x000000 (encapsulated
1996 * Ethernet), we'd do
1998 * return gen_snap(0x000000, proto,
2001 * here; for now, we don't, as per the above.
2002 * I don't know whether it's worth the extra CPU
2003 * time to do the right check or not.
2005 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2010 static struct block
*
2011 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2015 u_int src_off
, dst_off
;
2017 struct block
*b0
, *b1
;
2031 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2032 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2038 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2039 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2046 b0
= gen_linktype(proto
);
2047 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2053 static struct block
*
2054 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2055 struct in6_addr
*addr
;
2056 struct in6_addr
*mask
;
2058 u_int src_off
, dst_off
;
2060 struct block
*b0
, *b1
;
2075 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2076 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2082 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2083 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2090 /* this order is important */
2091 a
= (u_int32_t
*)addr
;
2092 m
= (u_int32_t
*)mask
;
2093 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2094 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2096 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2098 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2100 b0
= gen_linktype(proto
);
2106 static struct block
*
2107 gen_ehostop(eaddr
, dir
)
2108 register const u_char
*eaddr
;
2111 register struct block
*b0
, *b1
;
2115 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2118 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2121 b0
= gen_ehostop(eaddr
, Q_SRC
);
2122 b1
= gen_ehostop(eaddr
, Q_DST
);
2128 b0
= gen_ehostop(eaddr
, Q_SRC
);
2129 b1
= gen_ehostop(eaddr
, Q_DST
);
2138 * Like gen_ehostop, but for DLT_FDDI
2140 static struct block
*
2141 gen_fhostop(eaddr
, dir
)
2142 register const u_char
*eaddr
;
2145 struct block
*b0
, *b1
;
2150 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2152 return gen_bcmp(6 + 1, 6, eaddr
);
2157 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2159 return gen_bcmp(0 + 1, 6, eaddr
);
2163 b0
= gen_fhostop(eaddr
, Q_SRC
);
2164 b1
= gen_fhostop(eaddr
, Q_DST
);
2170 b0
= gen_fhostop(eaddr
, Q_SRC
);
2171 b1
= gen_fhostop(eaddr
, Q_DST
);
2180 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2182 static struct block
*
2183 gen_thostop(eaddr
, dir
)
2184 register const u_char
*eaddr
;
2187 register struct block
*b0
, *b1
;
2191 return gen_bcmp(8, 6, eaddr
);
2194 return gen_bcmp(2, 6, eaddr
);
2197 b0
= gen_thostop(eaddr
, Q_SRC
);
2198 b1
= gen_thostop(eaddr
, Q_DST
);
2204 b0
= gen_thostop(eaddr
, Q_SRC
);
2205 b1
= gen_thostop(eaddr
, Q_DST
);
2214 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2216 static struct block
*
2217 gen_wlanhostop(eaddr
, dir
)
2218 register const u_char
*eaddr
;
2221 register struct block
*b0
, *b1
, *b2
;
2222 register struct slist
*s
;
2229 * For control frames, there is no SA.
2231 * For management frames, SA is at an
2232 * offset of 10 from the beginning of
2235 * For data frames, SA is at an offset
2236 * of 10 from the beginning of the packet
2237 * if From DS is clear, at an offset of
2238 * 16 from the beginning of the packet
2239 * if From DS is set and To DS is clear,
2240 * and an offset of 24 from the beginning
2241 * of the packet if From DS is set and To DS
2246 * Generate the tests to be done for data frames
2249 * First, check for To DS set, i.e. check "link[1] & 0x01".
2251 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2253 b1
= new_block(JMP(BPF_JSET
));
2254 b1
->s
.k
= 0x01; /* To DS */
2258 * If To DS is set, the SA is at 24.
2260 b0
= gen_bcmp(24, 6, eaddr
);
2264 * Now, check for To DS not set, i.e. check
2265 * "!(link[1] & 0x01)".
2267 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2269 b2
= new_block(JMP(BPF_JSET
));
2270 b2
->s
.k
= 0x01; /* To DS */
2275 * If To DS is not set, the SA is at 16.
2277 b1
= gen_bcmp(16, 6, eaddr
);
2281 * Now OR together the last two checks. That gives
2282 * the complete set of checks for data frames with
2288 * Now check for From DS being set, and AND that with
2289 * the ORed-together checks.
2291 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2293 b1
= new_block(JMP(BPF_JSET
));
2294 b1
->s
.k
= 0x02; /* From DS */
2299 * Now check for data frames with From DS not set.
2301 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2303 b2
= new_block(JMP(BPF_JSET
));
2304 b2
->s
.k
= 0x02; /* From DS */
2309 * If From DS isn't set, the SA is at 10.
2311 b1
= gen_bcmp(10, 6, eaddr
);
2315 * Now OR together the checks for data frames with
2316 * From DS not set and for data frames with From DS
2317 * set; that gives the checks done for data frames.
2322 * Now check for a data frame.
2323 * I.e, check "link[0] & 0x08".
2325 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2327 b1
= new_block(JMP(BPF_JSET
));
2332 * AND that with the checks done for data frames.
2337 * If the high-order bit of the type value is 0, this
2338 * is a management frame.
2339 * I.e, check "!(link[0] & 0x08)".
2341 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2343 b2
= new_block(JMP(BPF_JSET
));
2349 * For management frames, the SA is at 10.
2351 b1
= gen_bcmp(10, 6, eaddr
);
2355 * OR that with the checks done for data frames.
2356 * That gives the checks done for management and
2362 * If the low-order bit of the type value is 1,
2363 * this is either a control frame or a frame
2364 * with a reserved type, and thus not a
2367 * I.e., check "!(link[0] & 0x04)".
2369 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2371 b1
= new_block(JMP(BPF_JSET
));
2377 * AND that with the checks for data and management
2387 * For control frames, there is no DA.
2389 * For management frames, DA is at an
2390 * offset of 4 from the beginning of
2393 * For data frames, DA is at an offset
2394 * of 4 from the beginning of the packet
2395 * if To DS is clear and at an offset of
2396 * 16 from the beginning of the packet
2401 * Generate the tests to be done for data frames.
2403 * First, check for To DS set, i.e. "link[1] & 0x01".
2405 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2407 b1
= new_block(JMP(BPF_JSET
));
2408 b1
->s
.k
= 0x01; /* To DS */
2412 * If To DS is set, the DA is at 16.
2414 b0
= gen_bcmp(16, 6, eaddr
);
2418 * Now, check for To DS not set, i.e. check
2419 * "!(link[1] & 0x01)".
2421 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2423 b2
= new_block(JMP(BPF_JSET
));
2424 b2
->s
.k
= 0x01; /* To DS */
2429 * If To DS is not set, the DA is at 4.
2431 b1
= gen_bcmp(4, 6, eaddr
);
2435 * Now OR together the last two checks. That gives
2436 * the complete set of checks for data frames.
2441 * Now check for a data frame.
2442 * I.e, check "link[0] & 0x08".
2444 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2446 b1
= new_block(JMP(BPF_JSET
));
2451 * AND that with the checks done for data frames.
2456 * If the high-order bit of the type value is 0, this
2457 * is a management frame.
2458 * I.e, check "!(link[0] & 0x08)".
2460 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2462 b2
= new_block(JMP(BPF_JSET
));
2468 * For management frames, the DA is at 4.
2470 b1
= gen_bcmp(4, 6, eaddr
);
2474 * OR that with the checks done for data frames.
2475 * That gives the checks done for management and
2481 * If the low-order bit of the type value is 1,
2482 * this is either a control frame or a frame
2483 * with a reserved type, and thus not a
2486 * I.e., check "!(link[0] & 0x04)".
2488 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2490 b1
= new_block(JMP(BPF_JSET
));
2496 * AND that with the checks for data and management
2503 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2504 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2510 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2511 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2520 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2521 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2522 * as the RFC states.)
2524 static struct block
*
2525 gen_ipfchostop(eaddr
, dir
)
2526 register const u_char
*eaddr
;
2529 register struct block
*b0
, *b1
;
2533 return gen_bcmp(10, 6, eaddr
);
2536 return gen_bcmp(2, 6, eaddr
);
2539 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2540 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2546 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2547 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2556 * This is quite tricky because there may be pad bytes in front of the
2557 * DECNET header, and then there are two possible data packet formats that
2558 * carry both src and dst addresses, plus 5 packet types in a format that
2559 * carries only the src node, plus 2 types that use a different format and
2560 * also carry just the src node.
2564 * Instead of doing those all right, we just look for data packets with
2565 * 0 or 1 bytes of padding. If you want to look at other packets, that
2566 * will require a lot more hacking.
2568 * To add support for filtering on DECNET "areas" (network numbers)
2569 * one would want to add a "mask" argument to this routine. That would
2570 * make the filter even more inefficient, although one could be clever
2571 * and not generate masking instructions if the mask is 0xFFFF.
2573 static struct block
*
2574 gen_dnhostop(addr
, dir
, base_off
)
2579 struct block
*b0
, *b1
, *b2
, *tmp
;
2580 u_int offset_lh
; /* offset if long header is received */
2581 u_int offset_sh
; /* offset if short header is received */
2586 offset_sh
= 1; /* follows flags */
2587 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2591 offset_sh
= 3; /* follows flags, dstnode */
2592 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2596 /* Inefficient because we do our Calvinball dance twice */
2597 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2598 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2604 /* Inefficient because we do our Calvinball dance twice */
2605 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2606 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2611 bpf_error("ISO host filtering not implemented");
2616 b0
= gen_linktype(ETHERTYPE_DN
);
2617 /* Check for pad = 1, long header case */
2618 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2619 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2620 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2621 BPF_H
, (bpf_int32
)ntohs(addr
));
2623 /* Check for pad = 0, long header case */
2624 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2625 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2628 /* Check for pad = 1, short header case */
2629 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2630 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2631 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2632 BPF_H
, (bpf_int32
)ntohs(addr
));
2635 /* Check for pad = 0, short header case */
2636 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2637 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2641 /* Combine with test for linktype */
2646 static struct block
*
2647 gen_host(addr
, mask
, proto
, dir
)
2653 struct block
*b0
, *b1
;
2658 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2659 if (off_linktype
!= (u_int
)-1) {
2660 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2662 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2668 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2669 off_nl
+ 12, off_nl
+ 16);
2672 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2673 off_nl
+ 14, off_nl
+ 24);
2676 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2677 off_nl
+ 14, off_nl
+ 24);
2680 bpf_error("'tcp' modifier applied to host");
2683 bpf_error("'sctp' modifier applied to host");
2686 bpf_error("'udp' modifier applied to host");
2689 bpf_error("'icmp' modifier applied to host");
2692 bpf_error("'igmp' modifier applied to host");
2695 bpf_error("'igrp' modifier applied to host");
2698 bpf_error("'pim' modifier applied to host");
2701 bpf_error("'vrrp' modifier applied to host");
2704 bpf_error("ATALK host filtering not implemented");
2707 bpf_error("AARP host filtering not implemented");
2710 return gen_dnhostop(addr
, dir
, off_nl
);
2713 bpf_error("SCA host filtering not implemented");
2716 bpf_error("LAT host filtering not implemented");
2719 bpf_error("MOPDL host filtering not implemented");
2722 bpf_error("MOPRC host filtering not implemented");
2726 bpf_error("'ip6' modifier applied to ip host");
2729 bpf_error("'icmp6' modifier applied to host");
2733 bpf_error("'ah' modifier applied to host");
2736 bpf_error("'esp' modifier applied to host");
2739 bpf_error("ISO host filtering not implemented");
2742 bpf_error("'esis' modifier applied to host");
2745 bpf_error("'isis' modifier applied to host");
2748 bpf_error("'clnp' modifier applied to host");
2751 bpf_error("'stp' modifier applied to host");
2754 bpf_error("IPX host filtering not implemented");
2757 bpf_error("'netbeui' modifier applied to host");
2766 static struct block
*
2767 gen_host6(addr
, mask
, proto
, dir
)
2768 struct in6_addr
*addr
;
2769 struct in6_addr
*mask
;
2776 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2779 bpf_error("'ip' modifier applied to ip6 host");
2782 bpf_error("'rarp' modifier applied to ip6 host");
2785 bpf_error("'arp' modifier applied to ip6 host");
2788 bpf_error("'sctp' modifier applied to host");
2791 bpf_error("'tcp' modifier applied to host");
2794 bpf_error("'udp' modifier applied to host");
2797 bpf_error("'icmp' modifier applied to host");
2800 bpf_error("'igmp' modifier applied to host");
2803 bpf_error("'igrp' modifier applied to host");
2806 bpf_error("'pim' modifier applied to host");
2809 bpf_error("'vrrp' modifier applied to host");
2812 bpf_error("ATALK host filtering not implemented");
2815 bpf_error("AARP host filtering not implemented");
2818 bpf_error("'decnet' modifier applied to ip6 host");
2821 bpf_error("SCA host filtering not implemented");
2824 bpf_error("LAT host filtering not implemented");
2827 bpf_error("MOPDL host filtering not implemented");
2830 bpf_error("MOPRC host filtering not implemented");
2833 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2834 off_nl
+ 8, off_nl
+ 24);
2837 bpf_error("'icmp6' modifier applied to host");
2840 bpf_error("'ah' modifier applied to host");
2843 bpf_error("'esp' modifier applied to host");
2846 bpf_error("ISO host filtering not implemented");
2849 bpf_error("'esis' modifier applied to host");
2852 bpf_error("'isis' modifier applied to host");
2855 bpf_error("'clnp' modifier applied to host");
2858 bpf_error("'stp' modifier applied to host");
2861 bpf_error("IPX host filtering not implemented");
2864 bpf_error("'netbeui' modifier applied to host");
2874 static struct block
*
2875 gen_gateway(eaddr
, alist
, proto
, dir
)
2876 const u_char
*eaddr
;
2877 bpf_u_int32
**alist
;
2881 struct block
*b0
, *b1
, *tmp
;
2884 bpf_error("direction applied to 'gateway'");
2891 if (linktype
== DLT_EN10MB
)
2892 b0
= gen_ehostop(eaddr
, Q_OR
);
2893 else if (linktype
== DLT_FDDI
)
2894 b0
= gen_fhostop(eaddr
, Q_OR
);
2895 else if (linktype
== DLT_IEEE802
)
2896 b0
= gen_thostop(eaddr
, Q_OR
);
2897 else if (linktype
== DLT_IEEE802_11
)
2898 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2899 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2901 * Check that the packet doesn't begin with an
2902 * LE Control marker. (We've already generated
2905 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2909 * Now check the MAC address.
2911 b0
= gen_ehostop(eaddr
, Q_OR
);
2913 } else if (linktype
== DLT_IP_OVER_FC
)
2914 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2917 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2919 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2921 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2929 bpf_error("illegal modifier of 'gateway'");
2935 gen_proto_abbrev(proto
)
2944 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2946 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2952 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2954 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2960 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2962 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2968 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2971 #ifndef IPPROTO_IGMP
2972 #define IPPROTO_IGMP 2
2976 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2979 #ifndef IPPROTO_IGRP
2980 #define IPPROTO_IGRP 9
2983 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2987 #define IPPROTO_PIM 103
2991 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2993 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2998 #ifndef IPPROTO_VRRP
2999 #define IPPROTO_VRRP 112
3003 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3007 b1
= gen_linktype(ETHERTYPE_IP
);
3011 b1
= gen_linktype(ETHERTYPE_ARP
);
3015 b1
= gen_linktype(ETHERTYPE_REVARP
);
3019 bpf_error("link layer applied in wrong context");
3022 b1
= gen_linktype(ETHERTYPE_ATALK
);
3026 b1
= gen_linktype(ETHERTYPE_AARP
);
3030 b1
= gen_linktype(ETHERTYPE_DN
);
3034 b1
= gen_linktype(ETHERTYPE_SCA
);
3038 b1
= gen_linktype(ETHERTYPE_LAT
);
3042 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3046 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3051 b1
= gen_linktype(ETHERTYPE_IPV6
);
3054 #ifndef IPPROTO_ICMPV6
3055 #define IPPROTO_ICMPV6 58
3058 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3063 #define IPPROTO_AH 51
3066 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3068 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3074 #define IPPROTO_ESP 50
3077 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3079 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3085 b1
= gen_linktype(LLCSAP_ISONS
);
3089 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3093 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3096 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3097 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3098 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3100 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3102 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3104 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3108 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3109 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3110 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3112 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3114 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3116 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3120 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3121 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3122 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3124 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3129 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3130 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3135 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3136 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3138 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3140 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3145 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3146 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3151 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3152 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3157 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3161 b1
= gen_linktype(LLCSAP_8021D
);
3165 b1
= gen_linktype(LLCSAP_IPX
);
3169 b1
= gen_linktype(LLCSAP_NETBEUI
);
3178 static struct block
*
3185 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3186 s
->s
.k
= off_nl
+ 6;
3187 b
= new_block(JMP(BPF_JSET
));
3195 static struct block
*
3196 gen_portatom(off
, v
)
3203 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3206 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3207 s
->next
->s
.k
= off_nl
+ off
;
3209 b
= new_block(JMP(BPF_JEQ
));
3217 static struct block
*
3218 gen_portatom6(off
, v
)
3222 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3227 gen_portop(port
, proto
, dir
)
3228 int port
, proto
, dir
;
3230 struct block
*b0
, *b1
, *tmp
;
3232 /* ip proto 'proto' */
3233 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3239 b1
= gen_portatom(0, (bpf_int32
)port
);
3243 b1
= gen_portatom(2, (bpf_int32
)port
);
3248 tmp
= gen_portatom(0, (bpf_int32
)port
);
3249 b1
= gen_portatom(2, (bpf_int32
)port
);
3254 tmp
= gen_portatom(0, (bpf_int32
)port
);
3255 b1
= gen_portatom(2, (bpf_int32
)port
);
3267 static struct block
*
3268 gen_port(port
, ip_proto
, dir
)
3273 struct block
*b0
, *b1
, *tmp
;
3278 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3279 * not LLC encapsulation with LLCSAP_IP.
3281 * For IEEE 802 networks - which includes 802.5 token ring
3282 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3283 * says that SNAP encapsulation is used, not LLC encapsulation
3286 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3287 * RFC 2225 say that SNAP encapsulation is used, not LLC
3288 * encapsulation with LLCSAP_IP.
3290 * So we always check for ETHERTYPE_IP.
3292 b0
= gen_linktype(ETHERTYPE_IP
);
3298 b1
= gen_portop(port
, ip_proto
, dir
);
3302 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3303 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3305 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3318 gen_portop6(port
, proto
, dir
)
3319 int port
, proto
, dir
;
3321 struct block
*b0
, *b1
, *tmp
;
3323 /* ip6 proto 'proto' */
3324 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3328 b1
= gen_portatom6(0, (bpf_int32
)port
);
3332 b1
= gen_portatom6(2, (bpf_int32
)port
);
3337 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3338 b1
= gen_portatom6(2, (bpf_int32
)port
);
3343 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3344 b1
= gen_portatom6(2, (bpf_int32
)port
);
3356 static struct block
*
3357 gen_port6(port
, ip_proto
, dir
)
3362 struct block
*b0
, *b1
, *tmp
;
3364 /* link proto ip6 */
3365 b0
= gen_linktype(ETHERTYPE_IPV6
);
3371 b1
= gen_portop6(port
, ip_proto
, dir
);
3375 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3376 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3378 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3390 /* gen_portrange code */
3392 gen_portrangeatom(off
, v1
, v2
)
3396 struct slist
*s1
, *s2
;
3397 struct block
*b1
, *b2
;
3401 * Reverse the order of the ports, so v1 is the lower one.
3409 s1
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3412 s1
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3413 s1
->next
->s
.k
= off_nl
+ off
;
3415 b1
= new_block(JMP(BPF_JGE
));
3419 s2
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3422 s2
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3423 s2
->next
->s
.k
= off_nl
+ off
;
3425 b2
= new_block(JMP(BPF_JGT
));
3436 gen_portrangeop(port1
, port2
, proto
, dir
)
3441 struct block
*b0
, *b1
, *tmp
;
3443 /* ip proto 'proto' */
3444 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3450 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3454 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3459 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3460 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3465 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3466 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3478 static struct block
*
3479 gen_portrange(port1
, port2
, ip_proto
, dir
)
3484 struct block
*b0
, *b1
, *tmp
;
3487 b0
= gen_linktype(ETHERTYPE_IP
);
3493 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3497 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3498 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3500 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3513 gen_portrangeatom6(off
, v1
, v2
)
3517 struct slist
*s1
, *s2
;
3518 struct block
*b1
, *b2
;
3522 * Reverse the order of the ports, so v1 is the lower one.
3531 s1
= new_stmt(BPF_LD
|BPF_ABS
|BPF_H
);
3532 s1
->s
.k
= off_nl
+ 40 + off
;
3534 b1
= new_block(JMP(BPF_JGE
));
3538 s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_H
);
3539 s2
->s
.k
= off_nl
+ 40 + off
;
3541 b2
= new_block(JMP(BPF_JGT
));
3552 gen_portrangeop6(port1
, port2
, proto
, dir
)
3557 struct block
*b0
, *b1
, *tmp
;
3559 /* ip6 proto 'proto' */
3560 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3564 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3568 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3573 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3574 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3579 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3580 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3592 static struct block
*
3593 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3598 struct block
*b0
, *b1
, *tmp
;
3600 /* link proto ip6 */
3601 b0
= gen_linktype(ETHERTYPE_IPV6
);
3607 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3611 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3612 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3614 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3627 lookup_proto(name
, proto
)
3628 register const char *name
;
3638 v
= pcap_nametoproto(name
);
3639 if (v
== PROTO_UNDEF
)
3640 bpf_error("unknown ip proto '%s'", name
);
3644 /* XXX should look up h/w protocol type based on linktype */
3645 v
= pcap_nametoeproto(name
);
3646 if (v
== PROTO_UNDEF
) {
3647 v
= pcap_nametollc(name
);
3648 if (v
== PROTO_UNDEF
)
3649 bpf_error("unknown ether proto '%s'", name
);
3654 if (strcmp(name
, "esis") == 0)
3656 else if (strcmp(name
, "isis") == 0)
3658 else if (strcmp(name
, "clnp") == 0)
3661 bpf_error("unknown osi proto '%s'", name
);
3681 static struct block
*
3682 gen_protochain(v
, proto
, dir
)
3687 #ifdef NO_PROTOCHAIN
3688 return gen_proto(v
, proto
, dir
);
3690 struct block
*b0
, *b
;
3691 struct slist
*s
[100];
3692 int fix2
, fix3
, fix4
, fix5
;
3693 int ahcheck
, again
, end
;
3695 int reg2
= alloc_reg();
3697 memset(s
, 0, sizeof(s
));
3698 fix2
= fix3
= fix4
= fix5
= 0;
3705 b0
= gen_protochain(v
, Q_IP
, dir
);
3706 b
= gen_protochain(v
, Q_IPV6
, dir
);
3710 bpf_error("bad protocol applied for 'protochain'");
3714 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3717 * s[0] is a dummy entry to protect other BPF insn from damage
3718 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3719 * hard to find interdependency made by jump table fixup.
3722 s
[i
] = new_stmt(0); /*dummy*/
3727 b0
= gen_linktype(ETHERTYPE_IP
);
3730 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3731 s
[i
]->s
.k
= off_nl
+ 9;
3733 /* X = ip->ip_hl << 2 */
3734 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3740 b0
= gen_linktype(ETHERTYPE_IPV6
);
3742 /* A = ip6->ip_nxt */
3743 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3744 s
[i
]->s
.k
= off_nl
+ 6;
3746 /* X = sizeof(struct ip6_hdr) */
3747 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3753 bpf_error("unsupported proto to gen_protochain");
3757 /* again: if (A == v) goto end; else fall through; */
3759 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3761 s
[i
]->s
.jt
= NULL
; /*later*/
3762 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3766 #ifndef IPPROTO_NONE
3767 #define IPPROTO_NONE 59
3769 /* if (A == IPPROTO_NONE) goto end */
3770 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3771 s
[i
]->s
.jt
= NULL
; /*later*/
3772 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3773 s
[i
]->s
.k
= IPPROTO_NONE
;
3774 s
[fix5
]->s
.jf
= s
[i
];
3779 if (proto
== Q_IPV6
) {
3780 int v6start
, v6end
, v6advance
, j
;
3783 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3784 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3785 s
[i
]->s
.jt
= NULL
; /*later*/
3786 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3787 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3788 s
[fix2
]->s
.jf
= s
[i
];
3790 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3791 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3792 s
[i
]->s
.jt
= NULL
; /*later*/
3793 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3794 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3796 /* if (A == IPPROTO_ROUTING) goto v6advance */
3797 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3798 s
[i
]->s
.jt
= NULL
; /*later*/
3799 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3800 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3802 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3803 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3804 s
[i
]->s
.jt
= NULL
; /*later*/
3805 s
[i
]->s
.jf
= NULL
; /*later*/
3806 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3817 * X = X + (P[X + 1] + 1) * 8;
3820 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3822 /* A = P[X + packet head] */
3823 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3827 s
[i
] = new_stmt(BPF_ST
);
3831 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3834 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3838 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3840 /* A = P[X + packet head]; */
3841 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3845 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3849 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3853 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3856 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3860 /* goto again; (must use BPF_JA for backward jump) */
3861 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3862 s
[i
]->s
.k
= again
- i
- 1;
3863 s
[i
- 1]->s
.jf
= s
[i
];
3867 for (j
= v6start
; j
<= v6end
; j
++)
3868 s
[j
]->s
.jt
= s
[v6advance
];
3873 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3875 s
[fix2
]->s
.jf
= s
[i
];
3881 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3882 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3883 s
[i
]->s
.jt
= NULL
; /*later*/
3884 s
[i
]->s
.jf
= NULL
; /*later*/
3885 s
[i
]->s
.k
= IPPROTO_AH
;
3887 s
[fix3
]->s
.jf
= s
[ahcheck
];
3894 * X = X + (P[X + 1] + 2) * 4;
3897 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3899 /* A = P[X + packet head]; */
3900 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3904 s
[i
] = new_stmt(BPF_ST
);
3908 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3911 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3915 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3917 /* A = P[X + packet head] */
3918 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3922 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3926 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3930 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3933 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3937 /* goto again; (must use BPF_JA for backward jump) */
3938 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3939 s
[i
]->s
.k
= again
- i
- 1;
3944 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3946 s
[fix2
]->s
.jt
= s
[end
];
3947 s
[fix4
]->s
.jf
= s
[end
];
3948 s
[fix5
]->s
.jt
= s
[end
];
3955 for (i
= 0; i
< max
- 1; i
++)
3956 s
[i
]->next
= s
[i
+ 1];
3957 s
[max
- 1]->next
= NULL
;
3962 b
= new_block(JMP(BPF_JEQ
));
3963 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3974 * Generate code that checks whether the packet is a packet for protocol
3975 * <proto> and whether the type field in that protocol's header has
3976 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
3977 * IP packet and checks the protocol number in the IP header against <v>.
3979 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
3980 * against Q_IP and Q_IPV6.
3982 static struct block
*
3983 gen_proto(v
, proto
, dir
)
3988 struct block
*b0
, *b1
;
3990 if (dir
!= Q_DEFAULT
)
3991 bpf_error("direction applied to 'proto'");
3996 b0
= gen_proto(v
, Q_IP
, dir
);
3997 b1
= gen_proto(v
, Q_IPV6
, dir
);
4005 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4006 * not LLC encapsulation with LLCSAP_IP.
4008 * For IEEE 802 networks - which includes 802.5 token ring
4009 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4010 * says that SNAP encapsulation is used, not LLC encapsulation
4013 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4014 * RFC 2225 say that SNAP encapsulation is used, not LLC
4015 * encapsulation with LLCSAP_IP.
4017 * So we always check for ETHERTYPE_IP.
4019 b0
= gen_linktype(ETHERTYPE_IP
);
4021 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
4023 b1
= gen_protochain(v
, Q_IP
);
4033 * Frame Relay packets typically have an OSI
4034 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4035 * generates code to check for all the OSI
4036 * NLPIDs, so calling it and then adding a check
4037 * for the particular NLPID for which we're
4038 * looking is bogus, as we can just check for
4041 * What we check for is the NLPID and a frame
4042 * control field value of UI, i.e. 0x03 followed
4045 * XXX - assumes a 2-byte Frame Relay header with
4046 * DLCI and flags. What if the address is longer?
4048 * XXX - what about SNAP-encapsulated frames?
4050 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
4056 * Cisco uses an Ethertype lookalike - for OSI,
4059 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4060 /* OSI in C-HDLC is stuffed with a fudge byte */
4061 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
4066 b0
= gen_linktype(LLCSAP_ISONS
);
4067 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
4073 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4075 * 4 is the offset of the PDU type relative to the IS-IS
4078 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
4083 bpf_error("arp does not encapsulate another protocol");
4087 bpf_error("rarp does not encapsulate another protocol");
4091 bpf_error("atalk encapsulation is not specifiable");
4095 bpf_error("decnet encapsulation is not specifiable");
4099 bpf_error("sca does not encapsulate another protocol");
4103 bpf_error("lat does not encapsulate another protocol");
4107 bpf_error("moprc does not encapsulate another protocol");
4111 bpf_error("mopdl does not encapsulate another protocol");
4115 return gen_linktype(v
);
4118 bpf_error("'udp proto' is bogus");
4122 bpf_error("'tcp proto' is bogus");
4126 bpf_error("'sctp proto' is bogus");
4130 bpf_error("'icmp proto' is bogus");
4134 bpf_error("'igmp proto' is bogus");
4138 bpf_error("'igrp proto' is bogus");
4142 bpf_error("'pim proto' is bogus");
4146 bpf_error("'vrrp proto' is bogus");
4151 b0
= gen_linktype(ETHERTYPE_IPV6
);
4153 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
4155 b1
= gen_protochain(v
, Q_IPV6
);
4161 bpf_error("'icmp6 proto' is bogus");
4165 bpf_error("'ah proto' is bogus");
4168 bpf_error("'ah proto' is bogus");
4171 bpf_error("'stp proto' is bogus");
4174 bpf_error("'ipx proto' is bogus");
4177 bpf_error("'netbeui proto' is bogus");
4188 register const char *name
;
4191 int proto
= q
.proto
;
4195 bpf_u_int32 mask
, addr
;
4197 bpf_u_int32
**alist
;
4200 struct sockaddr_in
*sin
;
4201 struct sockaddr_in6
*sin6
;
4202 struct addrinfo
*res
, *res0
;
4203 struct in6_addr mask128
;
4205 struct block
*b
, *tmp
;
4206 int port
, real_proto
;
4212 addr
= pcap_nametonetaddr(name
);
4214 bpf_error("unknown network '%s'", name
);
4215 /* Left justify network addr and calculate its network mask */
4217 while (addr
&& (addr
& 0xff000000) == 0) {
4221 return gen_host(addr
, mask
, proto
, dir
);
4225 if (proto
== Q_LINK
) {
4229 eaddr
= pcap_ether_hostton(name
);
4232 "unknown ether host '%s'", name
);
4233 b
= gen_ehostop(eaddr
, dir
);
4238 eaddr
= pcap_ether_hostton(name
);
4241 "unknown FDDI host '%s'", name
);
4242 b
= gen_fhostop(eaddr
, dir
);
4247 eaddr
= pcap_ether_hostton(name
);
4250 "unknown token ring host '%s'", name
);
4251 b
= gen_thostop(eaddr
, dir
);
4255 case DLT_IEEE802_11
:
4256 eaddr
= pcap_ether_hostton(name
);
4259 "unknown 802.11 host '%s'", name
);
4260 b
= gen_wlanhostop(eaddr
, dir
);
4264 case DLT_IP_OVER_FC
:
4265 eaddr
= pcap_ether_hostton(name
);
4268 "unknown Fibre Channel host '%s'", name
);
4269 b
= gen_ipfchostop(eaddr
, dir
);
4278 * Check that the packet doesn't begin
4279 * with an LE Control marker. (We've
4280 * already generated a test for LANE.)
4282 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4286 eaddr
= pcap_ether_hostton(name
);
4289 "unknown ether host '%s'", name
);
4290 b
= gen_ehostop(eaddr
, dir
);
4296 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4297 } else if (proto
== Q_DECNET
) {
4298 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4300 * I don't think DECNET hosts can be multihomed, so
4301 * there is no need to build up a list of addresses
4303 return (gen_host(dn_addr
, 0, proto
, dir
));
4306 alist
= pcap_nametoaddr(name
);
4307 if (alist
== NULL
|| *alist
== NULL
)
4308 bpf_error("unknown host '%s'", name
);
4310 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4312 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4314 tmp
= gen_host(**alist
++, 0xffffffff,
4321 memset(&mask128
, 0xff, sizeof(mask128
));
4322 res0
= res
= pcap_nametoaddrinfo(name
);
4324 bpf_error("unknown host '%s'", name
);
4326 tproto
= tproto6
= proto
;
4327 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4331 for (res
= res0
; res
; res
= res
->ai_next
) {
4332 switch (res
->ai_family
) {
4334 if (tproto
== Q_IPV6
)
4337 sin
= (struct sockaddr_in
*)
4339 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4340 0xffffffff, tproto
, dir
);
4343 if (tproto6
== Q_IP
)
4346 sin6
= (struct sockaddr_in6
*)
4348 tmp
= gen_host6(&sin6
->sin6_addr
,
4349 &mask128
, tproto6
, dir
);
4360 bpf_error("unknown host '%s'%s", name
,
4361 (proto
== Q_DEFAULT
)
4363 : " for specified address family");
4370 if (proto
!= Q_DEFAULT
&&
4371 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4372 bpf_error("illegal qualifier of 'port'");
4373 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4374 bpf_error("unknown port '%s'", name
);
4375 if (proto
== Q_UDP
) {
4376 if (real_proto
== IPPROTO_TCP
)
4377 bpf_error("port '%s' is tcp", name
);
4378 else if (real_proto
== IPPROTO_SCTP
)
4379 bpf_error("port '%s' is sctp", name
);
4381 /* override PROTO_UNDEF */
4382 real_proto
= IPPROTO_UDP
;
4384 if (proto
== Q_TCP
) {
4385 if (real_proto
== IPPROTO_UDP
)
4386 bpf_error("port '%s' is udp", name
);
4388 else if (real_proto
== IPPROTO_SCTP
)
4389 bpf_error("port '%s' is sctp", name
);
4391 /* override PROTO_UNDEF */
4392 real_proto
= IPPROTO_TCP
;
4394 if (proto
== Q_SCTP
) {
4395 if (real_proto
== IPPROTO_UDP
)
4396 bpf_error("port '%s' is udp", name
);
4398 else if (real_proto
== IPPROTO_TCP
)
4399 bpf_error("port '%s' is tcp", name
);
4401 /* override PROTO_UNDEF */
4402 real_proto
= IPPROTO_SCTP
;
4405 return gen_port(port
, real_proto
, dir
);
4409 b
= gen_port(port
, real_proto
, dir
);
4410 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4416 if (proto
!= Q_DEFAULT
&&
4417 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4418 bpf_error("illegal qualifier of 'portrange'");
4419 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4420 bpf_error("unknown port in range '%s'", name
);
4421 if (proto
== Q_UDP
) {
4422 if (real_proto
== IPPROTO_TCP
)
4423 bpf_error("port in range '%s' is tcp", name
);
4424 else if (real_proto
== IPPROTO_SCTP
)
4425 bpf_error("port in range '%s' is sctp", name
);
4427 /* override PROTO_UNDEF */
4428 real_proto
= IPPROTO_UDP
;
4430 if (proto
== Q_TCP
) {
4431 if (real_proto
== IPPROTO_UDP
)
4432 bpf_error("port in range '%s' is udp", name
);
4433 else if (real_proto
== IPPROTO_SCTP
)
4434 bpf_error("port in range '%s' is sctp", name
);
4436 /* override PROTO_UNDEF */
4437 real_proto
= IPPROTO_TCP
;
4439 if (proto
== Q_SCTP
) {
4440 if (real_proto
== IPPROTO_UDP
)
4441 bpf_error("port in range '%s' is udp", name
);
4442 else if (real_proto
== IPPROTO_TCP
)
4443 bpf_error("port in range '%s' is tcp", name
);
4445 /* override PROTO_UNDEF */
4446 real_proto
= IPPROTO_SCTP
;
4449 return gen_portrange(port1
, port2
, real_proto
, dir
);
4453 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4454 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4461 eaddr
= pcap_ether_hostton(name
);
4463 bpf_error("unknown ether host: %s", name
);
4465 alist
= pcap_nametoaddr(name
);
4466 if (alist
== NULL
|| *alist
== NULL
)
4467 bpf_error("unknown host '%s'", name
);
4468 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4472 bpf_error("'gateway' not supported in this configuration");
4476 real_proto
= lookup_proto(name
, proto
);
4477 if (real_proto
>= 0)
4478 return gen_proto(real_proto
, proto
, dir
);
4480 bpf_error("unknown protocol: %s", name
);
4483 real_proto
= lookup_proto(name
, proto
);
4484 if (real_proto
>= 0)
4485 return gen_protochain(real_proto
, proto
, dir
);
4487 bpf_error("unknown protocol: %s", name
);
4499 gen_mcode(s1
, s2
, masklen
, q
)
4500 register const char *s1
, *s2
;
4501 register int masklen
;
4504 register int nlen
, mlen
;
4507 nlen
= __pcap_atoin(s1
, &n
);
4508 /* Promote short ipaddr */
4512 mlen
= __pcap_atoin(s2
, &m
);
4513 /* Promote short ipaddr */
4516 bpf_error("non-network bits set in \"%s mask %s\"",
4519 /* Convert mask len to mask */
4521 bpf_error("mask length must be <= 32");
4522 m
= 0xffffffff << (32 - masklen
);
4524 bpf_error("non-network bits set in \"%s/%d\"",
4531 return gen_host(n
, m
, q
.proto
, q
.dir
);
4534 bpf_error("Mask syntax for networks only");
4542 register const char *s
;
4547 int proto
= q
.proto
;
4553 else if (q
.proto
== Q_DECNET
)
4554 vlen
= __pcap_atodn(s
, &v
);
4556 vlen
= __pcap_atoin(s
, &v
);
4563 if (proto
== Q_DECNET
)
4564 return gen_host(v
, 0, proto
, dir
);
4565 else if (proto
== Q_LINK
) {
4566 bpf_error("illegal link layer address");
4569 if (s
== NULL
&& q
.addr
== Q_NET
) {
4570 /* Promote short net number */
4571 while (v
&& (v
& 0xff000000) == 0) {
4576 /* Promote short ipaddr */
4580 return gen_host(v
, mask
, proto
, dir
);
4585 proto
= IPPROTO_UDP
;
4586 else if (proto
== Q_TCP
)
4587 proto
= IPPROTO_TCP
;
4588 else if (proto
== Q_SCTP
)
4589 proto
= IPPROTO_SCTP
;
4590 else if (proto
== Q_DEFAULT
)
4591 proto
= PROTO_UNDEF
;
4593 bpf_error("illegal qualifier of 'port'");
4596 return gen_port((int)v
, proto
, dir
);
4600 b
= gen_port((int)v
, proto
, dir
);
4601 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4608 proto
= IPPROTO_UDP
;
4609 else if (proto
== Q_TCP
)
4610 proto
= IPPROTO_TCP
;
4611 else if (proto
== Q_SCTP
)
4612 proto
= IPPROTO_SCTP
;
4613 else if (proto
== Q_DEFAULT
)
4614 proto
= PROTO_UNDEF
;
4616 bpf_error("illegal qualifier of 'portrange'");
4619 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4623 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4624 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4630 bpf_error("'gateway' requires a name");
4634 return gen_proto((int)v
, proto
, dir
);
4637 return gen_protochain((int)v
, proto
, dir
);
4652 gen_mcode6(s1
, s2
, masklen
, q
)
4653 register const char *s1
, *s2
;
4654 register int masklen
;
4657 struct addrinfo
*res
;
4658 struct in6_addr
*addr
;
4659 struct in6_addr mask
;
4664 bpf_error("no mask %s supported", s2
);
4666 res
= pcap_nametoaddrinfo(s1
);
4668 bpf_error("invalid ip6 address %s", s1
);
4670 bpf_error("%s resolved to multiple address", s1
);
4671 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4673 if (sizeof(mask
) * 8 < masklen
)
4674 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4675 memset(&mask
, 0, sizeof(mask
));
4676 memset(&mask
, 0xff, masklen
/ 8);
4678 mask
.s6_addr
[masklen
/ 8] =
4679 (0xff << (8 - masklen
% 8)) & 0xff;
4682 a
= (u_int32_t
*)addr
;
4683 m
= (u_int32_t
*)&mask
;
4684 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4685 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4686 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4694 bpf_error("Mask syntax for networks only");
4698 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4703 bpf_error("invalid qualifier against IPv6 address");
4711 register const u_char
*eaddr
;
4714 struct block
*b
, *tmp
;
4716 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4717 if (linktype
== DLT_EN10MB
)
4718 return gen_ehostop(eaddr
, (int)q
.dir
);
4719 if (linktype
== DLT_FDDI
)
4720 return gen_fhostop(eaddr
, (int)q
.dir
);
4721 if (linktype
== DLT_IEEE802
)
4722 return gen_thostop(eaddr
, (int)q
.dir
);
4723 if (linktype
== DLT_IEEE802_11
)
4724 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4725 if (linktype
== DLT_SUNATM
&& is_lane
) {
4727 * Check that the packet doesn't begin with an
4728 * LE Control marker. (We've already generated
4731 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4735 * Now check the MAC address.
4737 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4741 if (linktype
== DLT_IP_OVER_FC
)
4742 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4743 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4745 bpf_error("ethernet address used in non-ether expression");
4751 struct slist
*s0
, *s1
;
4754 * This is definitely not the best way to do this, but the
4755 * lists will rarely get long.
4762 static struct slist
*
4768 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4773 static struct slist
*
4779 s
= new_stmt(BPF_LD
|BPF_MEM
);
4785 gen_load(proto
, index
, size
)
4790 struct slist
*s
, *tmp
;
4792 int regno
= alloc_reg();
4794 free_reg(index
->regno
);
4798 bpf_error("data size must be 1, 2, or 4");
4814 bpf_error("unsupported index operation");
4818 * XXX - what about ATM LANE? Should the index be
4819 * relative to the beginning of the AAL5 frame, so
4820 * that 0 refers to the beginning of the LE Control
4821 * field, or relative to the beginning of the LAN
4822 * frame, so that 0 refers, for Ethernet LANE, to
4823 * the beginning of the destination address?
4825 s
= xfer_to_x(index
);
4826 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4828 sappend(index
->s
, s
);
4843 /* XXX Note that we assume a fixed link header here. */
4844 s
= xfer_to_x(index
);
4845 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4848 sappend(index
->s
, s
);
4850 b
= gen_proto_abbrev(proto
);
4852 gen_and(index
->b
, b
);
4864 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4866 sappend(s
, xfer_to_a(index
));
4867 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4868 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4869 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4871 sappend(index
->s
, s
);
4873 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4875 gen_and(index
->b
, b
);
4877 gen_and(gen_proto_abbrev(Q_IP
), b
);
4883 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4887 index
->regno
= regno
;
4888 s
= new_stmt(BPF_ST
);
4890 sappend(index
->s
, s
);
4896 gen_relation(code
, a0
, a1
, reversed
)
4898 struct arth
*a0
, *a1
;
4901 struct slist
*s0
, *s1
, *s2
;
4902 struct block
*b
, *tmp
;
4906 if (code
== BPF_JEQ
) {
4907 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4908 b
= new_block(JMP(code
));
4912 b
= new_block(BPF_JMP
|code
|BPF_X
);
4918 sappend(a0
->s
, a1
->s
);
4922 free_reg(a0
->regno
);
4923 free_reg(a1
->regno
);
4925 /* 'and' together protocol checks */
4928 gen_and(a0
->b
, tmp
= a1
->b
);
4944 int regno
= alloc_reg();
4945 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4948 s
= new_stmt(BPF_LD
|BPF_LEN
);
4949 s
->next
= new_stmt(BPF_ST
);
4950 s
->next
->s
.k
= regno
;
4965 a
= (struct arth
*)newchunk(sizeof(*a
));
4969 s
= new_stmt(BPF_LD
|BPF_IMM
);
4971 s
->next
= new_stmt(BPF_ST
);
4987 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4990 s
= new_stmt(BPF_ST
);
4998 gen_arth(code
, a0
, a1
)
5000 struct arth
*a0
, *a1
;
5002 struct slist
*s0
, *s1
, *s2
;
5006 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5011 sappend(a0
->s
, a1
->s
);
5013 free_reg(a0
->regno
);
5014 free_reg(a1
->regno
);
5016 s0
= new_stmt(BPF_ST
);
5017 a0
->regno
= s0
->s
.k
= alloc_reg();
5024 * Here we handle simple allocation of the scratch registers.
5025 * If too many registers are alloc'd, the allocator punts.
5027 static int regused
[BPF_MEMWORDS
];
5031 * Return the next free register.
5036 int n
= BPF_MEMWORDS
;
5039 if (regused
[curreg
])
5040 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5042 regused
[curreg
] = 1;
5046 bpf_error("too many registers needed to evaluate expression");
5051 * Return a register to the table so it can
5061 static struct block
*
5068 s
= new_stmt(BPF_LD
|BPF_LEN
);
5069 b
= new_block(JMP(jmp
));
5080 return gen_len(BPF_JGE
, n
);
5084 * Actually, this is less than or equal.
5092 b
= gen_len(BPF_JGT
, n
);
5099 gen_byteop(op
, idx
, val
)
5110 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5113 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5114 b
->s
.code
= JMP(BPF_JGE
);
5119 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5120 b
->s
.code
= JMP(BPF_JGT
);
5124 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5128 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5132 b
= new_block(JMP(BPF_JEQ
));
5139 static u_char abroadcast
[] = { 0x0 };
5142 gen_broadcast(proto
)
5145 bpf_u_int32 hostmask
;
5146 struct block
*b0
, *b1
, *b2
;
5147 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5153 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5154 return gen_ahostop(abroadcast
, Q_DST
);
5155 if (linktype
== DLT_EN10MB
)
5156 return gen_ehostop(ebroadcast
, Q_DST
);
5157 if (linktype
== DLT_FDDI
)
5158 return gen_fhostop(ebroadcast
, Q_DST
);
5159 if (linktype
== DLT_IEEE802
)
5160 return gen_thostop(ebroadcast
, Q_DST
);
5161 if (linktype
== DLT_IEEE802_11
)
5162 return gen_wlanhostop(ebroadcast
, Q_DST
);
5163 if (linktype
== DLT_IP_OVER_FC
)
5164 return gen_ipfchostop(ebroadcast
, Q_DST
);
5165 if (linktype
== DLT_SUNATM
&& is_lane
) {
5167 * Check that the packet doesn't begin with an
5168 * LE Control marker. (We've already generated
5171 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5175 * Now check the MAC address.
5177 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5181 bpf_error("not a broadcast link");
5185 b0
= gen_linktype(ETHERTYPE_IP
);
5186 hostmask
= ~netmask
;
5187 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
5188 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
5189 (bpf_int32
)(~0 & hostmask
), hostmask
);
5194 bpf_error("only link-layer/IP broadcast filters supported");
5199 * Generate code to test the low-order bit of a MAC address (that's
5200 * the bottom bit of the *first* byte).
5202 static struct block
*
5203 gen_mac_multicast(offset
)
5206 register struct block
*b0
;
5207 register struct slist
*s
;
5209 /* link[offset] & 1 != 0 */
5210 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5212 b0
= new_block(JMP(BPF_JSET
));
5219 gen_multicast(proto
)
5222 register struct block
*b0
, *b1
, *b2
;
5223 register struct slist
*s
;
5229 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5230 /* all ARCnet multicasts use the same address */
5231 return gen_ahostop(abroadcast
, Q_DST
);
5233 if (linktype
== DLT_EN10MB
) {
5234 /* ether[0] & 1 != 0 */
5235 return gen_mac_multicast(0);
5238 if (linktype
== DLT_FDDI
) {
5240 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5242 * XXX - was that referring to bit-order issues?
5244 /* fddi[1] & 1 != 0 */
5245 return gen_mac_multicast(1);
5248 if (linktype
== DLT_IEEE802
) {
5249 /* tr[2] & 1 != 0 */
5250 return gen_mac_multicast(2);
5253 if (linktype
== DLT_IEEE802_11
) {
5257 * For control frames, there is no DA.
5259 * For management frames, DA is at an
5260 * offset of 4 from the beginning of
5263 * For data frames, DA is at an offset
5264 * of 4 from the beginning of the packet
5265 * if To DS is clear and at an offset of
5266 * 16 from the beginning of the packet
5271 * Generate the tests to be done for data frames.
5273 * First, check for To DS set, i.e. "link[1] & 0x01".
5275 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5277 b1
= new_block(JMP(BPF_JSET
));
5278 b1
->s
.k
= 0x01; /* To DS */
5282 * If To DS is set, the DA is at 16.
5284 b0
= gen_mac_multicast(16);
5288 * Now, check for To DS not set, i.e. check
5289 * "!(link[1] & 0x01)".
5291 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5293 b2
= new_block(JMP(BPF_JSET
));
5294 b2
->s
.k
= 0x01; /* To DS */
5299 * If To DS is not set, the DA is at 4.
5301 b1
= gen_mac_multicast(4);
5305 * Now OR together the last two checks. That gives
5306 * the complete set of checks for data frames.
5311 * Now check for a data frame.
5312 * I.e, check "link[0] & 0x08".
5314 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5316 b1
= new_block(JMP(BPF_JSET
));
5321 * AND that with the checks done for data frames.
5326 * If the high-order bit of the type value is 0, this
5327 * is a management frame.
5328 * I.e, check "!(link[0] & 0x08)".
5330 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5332 b2
= new_block(JMP(BPF_JSET
));
5338 * For management frames, the DA is at 4.
5340 b1
= gen_mac_multicast(4);
5344 * OR that with the checks done for data frames.
5345 * That gives the checks done for management and
5351 * If the low-order bit of the type value is 1,
5352 * this is either a control frame or a frame
5353 * with a reserved type, and thus not a
5356 * I.e., check "!(link[0] & 0x04)".
5358 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5360 b1
= new_block(JMP(BPF_JSET
));
5366 * AND that with the checks for data and management
5373 if (linktype
== DLT_IP_OVER_FC
) {
5374 b0
= gen_mac_multicast(2);
5378 if (linktype
== DLT_SUNATM
&& is_lane
) {
5380 * Check that the packet doesn't begin with an
5381 * LE Control marker. (We've already generated
5384 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5387 /* ether[off_mac] & 1 != 0 */
5388 b0
= gen_mac_multicast(off_mac
);
5393 /* Link not known to support multicasts */
5397 b0
= gen_linktype(ETHERTYPE_IP
);
5398 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5399 b1
->s
.code
= JMP(BPF_JGE
);
5405 b0
= gen_linktype(ETHERTYPE_IPV6
);
5406 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5411 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5416 * generate command for inbound/outbound. It's here so we can
5417 * make it link-type specific. 'dir' = 0 implies "inbound",
5418 * = 1 implies "outbound".
5424 register struct block
*b0
;
5427 * Only some data link types support inbound/outbound qualifiers.
5431 b0
= gen_relation(BPF_JEQ
,
5432 gen_load(Q_LINK
, gen_loadi(0), 1),
5440 * Match packets sent by this machine.
5442 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5445 * Match packets sent to this machine.
5446 * (No broadcast or multicast packets, or
5447 * packets sent to some other machine and
5448 * received promiscuously.)
5450 * XXX - packets sent to other machines probably
5451 * shouldn't be matched, but what about broadcast
5452 * or multicast packets we received?
5454 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5459 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5460 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5465 /* match outgoing packets */
5466 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5468 /* match incoming packets */
5469 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5473 case DLT_JUNIPER_MLFR
:
5474 case DLT_JUNIPER_MLPPP
:
5475 case DLT_JUNIPER_ATM1
:
5476 case DLT_JUNIPER_ATM2
:
5477 /* juniper flags (including direction) are stored
5478 * the byte after the 3-byte magic number */
5480 /* match outgoing packets */
5481 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5483 /* match incoming packets */
5484 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5489 bpf_error("inbound/outbound not supported on linktype %d",
5497 /* PF firewall log matched interface */
5499 gen_pf_ifname(const char *ifname
)
5504 if (linktype
== DLT_PFLOG
) {
5505 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5506 off
= offsetof(struct pfloghdr
, ifname
);
5508 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5511 if (strlen(ifname
) >= len
) {
5512 bpf_error("ifname interface names can only be %d characters",
5516 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5520 /* PF firewall log matched interface */
5522 gen_pf_ruleset(char *ruleset
)
5526 if (linktype
!= DLT_PFLOG
) {
5527 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5530 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5531 bpf_error("ruleset names can only be %ld characters",
5532 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5535 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5536 strlen(ruleset
), (const u_char
*)ruleset
);
5540 /* PF firewall log rule number */
5546 if (linktype
== DLT_PFLOG
) {
5547 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5550 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5557 /* PF firewall log sub-rule number */
5559 gen_pf_srnr(int srnr
)
5563 if (linktype
!= DLT_PFLOG
) {
5564 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5568 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5573 /* PF firewall log reason code */
5575 gen_pf_reason(int reason
)
5579 if (linktype
== DLT_PFLOG
) {
5580 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5583 bpf_error("reason not supported on linktype 0x%x", linktype
);
5590 /* PF firewall log action */
5592 gen_pf_action(int action
)
5596 if (linktype
== DLT_PFLOG
) {
5597 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5600 bpf_error("action not supported on linktype 0x%x", linktype
);
5609 register const u_char
*eaddr
;
5612 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5613 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5614 return gen_ahostop(eaddr
, (int)q
.dir
);
5616 bpf_error("ARCnet address used in non-arc expression");
5620 static struct block
*
5621 gen_ahostop(eaddr
, dir
)
5622 register const u_char
*eaddr
;
5625 register struct block
*b0
, *b1
;
5628 /* src comes first, different from Ethernet */
5630 return gen_bcmp(0, 1, eaddr
);
5633 return gen_bcmp(1, 1, eaddr
);
5636 b0
= gen_ahostop(eaddr
, Q_SRC
);
5637 b1
= gen_ahostop(eaddr
, Q_DST
);
5643 b0
= gen_ahostop(eaddr
, Q_SRC
);
5644 b1
= gen_ahostop(eaddr
, Q_DST
);
5653 * support IEEE 802.1Q VLAN trunk over ethernet
5662 * Change the offsets to point to the type and data fields within
5663 * the VLAN packet. Just increment the offsets, so that we
5664 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5665 * capture VLAN 200 encapsulated within VLAN 100.
5667 * XXX - this is a bit of a kludge. If we were to split the
5668 * compiler into a parser that parses an expression and
5669 * generates an expression tree, and a code generator that
5670 * takes an expression tree (which could come from our
5671 * parser or from some other parser) and generates BPF code,
5672 * we could perhaps make the offsets parameters of routines
5673 * and, in the handler for an "AND" node, pass to subnodes
5674 * other than the VLAN node the adjusted offsets.
5676 * This would mean that "vlan" would, instead of changing the
5677 * behavior of *all* tests after it, change only the behavior
5678 * of tests ANDed with it. That would change the documented
5679 * semantics of "vlan", which might break some expressions.
5680 * However, it would mean that "(vlan and ip) or ip" would check
5681 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5682 * checking only for VLAN-encapsulated IP, so that could still
5683 * be considered worth doing; it wouldn't break expressions
5684 * that are of the form "vlan and ..." or "vlan N and ...",
5685 * which I suspect are the most common expressions involving
5686 * "vlan". "vlan or ..." doesn't necessarily do what the user
5687 * would really want, now, as all the "or ..." tests would
5688 * be done assuming a VLAN, even though the "or" could be viewed
5689 * as meaning "or, if this isn't a VLAN packet...".
5691 orig_linktype
= off_linktype
; /* save original values */
5703 bpf_error("no VLAN support for data link type %d",
5708 /* check for VLAN */
5709 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5711 /* If a specific VLAN is requested, check VLAN id */
5712 if (vlan_num
>= 0) {
5715 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5733 * Change the offsets to point to the type and data fields within
5734 * the MPLS packet. Just increment the offsets, so that we
5735 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5736 * capture packets with an outer label of 100000 and an inner
5739 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5741 orig_linktype
= off_linktype
; /* save original values */
5746 case DLT_C_HDLC
: /* fall through */
5752 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5760 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5763 /* FIXME add other DLT_s ...
5764 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5765 * leave it for now */
5768 bpf_error("no MPLS support for data link type %d",
5775 /* If a specific MPLS label is requested, check it */
5776 if (label_num
>= 0) {
5779 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5780 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5789 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5801 bpf_error("'vpi' supported only on raw ATM");
5802 if (off_vpi
== (u_int
)-1)
5804 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5805 (u_int
)jvalue
, reverse
);
5810 bpf_error("'vci' supported only on raw ATM");
5811 if (off_vci
== (u_int
)-1)
5813 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5814 (u_int
)jvalue
, reverse
);
5818 if (off_proto
== (u_int
)-1)
5819 abort(); /* XXX - this isn't on FreeBSD */
5820 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5821 (u_int
)jvalue
, reverse
);
5825 if (off_payload
== (u_int
)-1)
5827 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5828 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5833 bpf_error("'callref' supported only on raw ATM");
5834 if (off_proto
== (u_int
)-1)
5836 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5837 (u_int
)jvalue
, reverse
);
5847 gen_atmtype_abbrev(type
)
5850 struct block
*b0
, *b1
;
5855 /* Get all packets in Meta signalling Circuit */
5857 bpf_error("'metac' supported only on raw ATM");
5858 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5859 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5864 /* Get all packets in Broadcast Circuit*/
5866 bpf_error("'bcc' supported only on raw ATM");
5867 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5868 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5873 /* Get all cells in Segment OAM F4 circuit*/
5875 bpf_error("'oam4sc' supported only on raw ATM");
5876 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5877 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5882 /* Get all cells in End-to-End OAM F4 Circuit*/
5884 bpf_error("'oam4ec' supported only on raw ATM");
5885 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5886 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5891 /* Get all packets in connection Signalling Circuit */
5893 bpf_error("'sc' supported only on raw ATM");
5894 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5895 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5900 /* Get all packets in ILMI Circuit */
5902 bpf_error("'ilmic' supported only on raw ATM");
5903 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5904 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5909 /* Get all LANE packets */
5911 bpf_error("'lane' supported only on raw ATM");
5912 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5915 * Arrange that all subsequent tests assume LANE
5916 * rather than LLC-encapsulated packets, and set
5917 * the offsets appropriately for LANE-encapsulated
5920 * "off_mac" is the offset of the Ethernet header,
5921 * which is 2 bytes past the ATM pseudo-header
5922 * (skipping the pseudo-header and 2-byte LE Client
5923 * field). The other offsets are Ethernet offsets
5924 * relative to "off_mac".
5927 off_mac
= off_payload
+ 2; /* MAC header */
5928 off_linktype
= off_mac
+ 12;
5929 off_nl
= off_mac
+ 14; /* Ethernet II */
5930 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5934 /* Get all LLC-encapsulated packets */
5936 bpf_error("'llc' supported only on raw ATM");
5937 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5948 static struct block
*
5949 gen_msg_abbrev(type
)
5955 * Q.2931 signalling protocol messages for handling virtual circuits
5956 * establishment and teardown
5961 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5965 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5969 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5973 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5977 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5980 case A_RELEASE_DONE
:
5981 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5991 gen_atmmulti_abbrev(type
)
5994 struct block
*b0
, *b1
;
6000 bpf_error("'oam' supported only on raw ATM");
6001 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6006 bpf_error("'oamf4' supported only on raw ATM");
6008 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6009 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6011 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6017 * Get Q.2931 signalling messages for switched
6018 * virtual connection
6021 bpf_error("'connectmsg' supported only on raw ATM");
6022 b0
= gen_msg_abbrev(A_SETUP
);
6023 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6025 b0
= gen_msg_abbrev(A_CONNECT
);
6027 b0
= gen_msg_abbrev(A_CONNECTACK
);
6029 b0
= gen_msg_abbrev(A_RELEASE
);
6031 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6033 b0
= gen_atmtype_abbrev(A_SC
);
6039 bpf_error("'metaconnect' supported only on raw ATM");
6040 b0
= gen_msg_abbrev(A_SETUP
);
6041 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6043 b0
= gen_msg_abbrev(A_CONNECT
);
6045 b0
= gen_msg_abbrev(A_RELEASE
);
6047 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6049 b0
= gen_atmtype_abbrev(A_METAC
);