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.2 2005-04-10 18:04:50 hannes 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, orig_nl_nosnap
= -1U;
108 static int pcap_fddipad
;
113 bpf_error(const char *fmt
, ...)
119 if (bpf_pcap
!= NULL
)
120 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
127 static void init_linktype(pcap_t
*);
129 static int alloc_reg(void);
130 static void free_reg(int);
132 static struct block
*root
;
135 * We divy out chunks of memory rather than call malloc each time so
136 * we don't have to worry about leaking memory. It's probably
137 * not a big deal if all this memory was wasted but if this ever
138 * goes into a library that would probably not be a good idea.
140 * XXX - this *is* in a library....
143 #define CHUNK0SIZE 1024
149 static struct chunk chunks
[NCHUNKS
];
150 static int cur_chunk
;
152 static void *newchunk(u_int
);
153 static void freechunks(void);
154 static inline struct block
*new_block(int);
155 static inline struct slist
*new_stmt(int);
156 static struct block
*gen_retblk(int);
157 static inline void syntax(void);
159 static void backpatch(struct block
*, struct block
*);
160 static void merge(struct block
*, struct block
*);
161 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
162 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
163 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
164 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
165 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
166 bpf_u_int32
, bpf_u_int32
, int);
167 static struct block
*gen_uncond(int);
168 static inline struct block
*gen_true(void);
169 static inline struct block
*gen_false(void);
170 static struct block
*gen_ether_linktype(int);
171 static struct block
*gen_linux_sll_linktype(int);
172 static struct block
*gen_linktype(int);
173 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
174 static struct block
*gen_llc_linktype(int);
175 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
177 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
179 static struct block
*gen_ahostop(const u_char
*, int);
180 static struct block
*gen_ehostop(const u_char
*, int);
181 static struct block
*gen_fhostop(const u_char
*, int);
182 static struct block
*gen_thostop(const u_char
*, int);
183 static struct block
*gen_wlanhostop(const u_char
*, int);
184 static struct block
*gen_ipfchostop(const u_char
*, int);
185 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
186 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
188 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
191 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
193 static struct block
*gen_ipfrag(void);
194 static struct block
*gen_portatom(int, bpf_int32
);
196 static struct block
*gen_portatom6(int, bpf_int32
);
198 struct block
*gen_portop(int, int, int);
199 static struct block
*gen_port(int, int, int);
201 struct block
*gen_portop6(int, int, int);
202 static struct block
*gen_port6(int, int, int);
204 static int lookup_proto(const char *, int);
205 static struct block
*gen_protochain(int, int, int);
206 static struct block
*gen_proto(int, int, int);
207 static struct slist
*xfer_to_x(struct arth
*);
208 static struct slist
*xfer_to_a(struct arth
*);
209 static struct block
*gen_mac_multicast(int);
210 static struct block
*gen_len(int, int);
212 static struct block
*gen_msg_abbrev(int type
);
223 /* XXX Round up to nearest long. */
224 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
226 /* XXX Round up to structure boundary. */
230 cp
= &chunks
[cur_chunk
];
231 if (n
> cp
->n_left
) {
232 ++cp
, k
= ++cur_chunk
;
234 bpf_error("out of memory");
235 size
= CHUNK0SIZE
<< k
;
236 cp
->m
= (void *)malloc(size
);
238 bpf_error("out of memory");
239 memset((char *)cp
->m
, 0, size
);
242 bpf_error("out of memory");
245 return (void *)((char *)cp
->m
+ cp
->n_left
);
254 for (i
= 0; i
< NCHUNKS
; ++i
)
255 if (chunks
[i
].m
!= NULL
) {
262 * A strdup whose allocations are freed after code generation is over.
266 register const char *s
;
268 int n
= strlen(s
) + 1;
269 char *cp
= newchunk(n
);
275 static inline struct block
*
281 p
= (struct block
*)newchunk(sizeof(*p
));
288 static inline struct slist
*
294 p
= (struct slist
*)newchunk(sizeof(*p
));
300 static struct block
*
304 struct block
*b
= new_block(BPF_RET
|BPF_K
);
313 bpf_error("syntax error in filter expression");
316 static bpf_u_int32 netmask
;
321 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
322 char *buf
, int optimize
, bpf_u_int32 mask
)
331 if (setjmp(top_ctx
)) {
339 snaplen
= pcap_snapshot(p
);
341 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
342 "snaplen of 0 rejects all packets");
346 lex_init(buf
? buf
: "");
354 root
= gen_retblk(snaplen
);
356 if (optimize
&& !no_optimize
) {
359 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
360 bpf_error("expression rejects all packets");
362 program
->bf_insns
= icode_to_fcode(root
, &len
);
363 program
->bf_len
= len
;
371 * entry point for using the compiler with no pcap open
372 * pass in all the stuff that is needed explicitly instead.
375 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
376 struct bpf_program
*program
,
377 char *buf
, int optimize
, bpf_u_int32 mask
)
382 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
385 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
391 * Clean up a "struct bpf_program" by freeing all the memory allocated
395 pcap_freecode(struct bpf_program
*program
)
398 if (program
->bf_insns
!= NULL
) {
399 free((char *)program
->bf_insns
);
400 program
->bf_insns
= NULL
;
405 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
406 * which of the jt and jf fields has been resolved and which is a pointer
407 * back to another unresolved block (or nil). At least one of the fields
408 * in each block is already resolved.
411 backpatch(list
, target
)
412 struct block
*list
, *target
;
429 * Merge the lists in b0 and b1, using the 'sense' field to indicate
430 * which of jt and jf is the link.
434 struct block
*b0
, *b1
;
436 register struct block
**p
= &b0
;
438 /* Find end of list. */
440 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
442 /* Concatenate the lists. */
450 backpatch(p
, gen_retblk(snaplen
));
451 p
->sense
= !p
->sense
;
452 backpatch(p
, gen_retblk(0));
458 struct block
*b0
, *b1
;
460 backpatch(b0
, b1
->head
);
461 b0
->sense
= !b0
->sense
;
462 b1
->sense
= !b1
->sense
;
464 b1
->sense
= !b1
->sense
;
470 struct block
*b0
, *b1
;
472 b0
->sense
= !b0
->sense
;
473 backpatch(b0
, b1
->head
);
474 b0
->sense
= !b0
->sense
;
483 b
->sense
= !b
->sense
;
486 static struct block
*
487 gen_cmp(offset
, size
, v
)
494 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
497 b
= new_block(JMP(BPF_JEQ
));
504 static struct block
*
505 gen_cmp_gt(offset
, size
, v
)
512 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
515 b
= new_block(JMP(BPF_JGT
));
522 static struct block
*
523 gen_mcmp(offset
, size
, v
, mask
)
528 struct block
*b
= gen_cmp(offset
, size
, v
);
531 if (mask
!= 0xffffffff) {
532 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
539 static struct block
*
540 gen_bcmp(offset
, size
, v
)
541 register u_int offset
, size
;
542 register const u_char
*v
;
544 register struct block
*b
, *tmp
;
548 register const u_char
*p
= &v
[size
- 4];
549 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
550 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
552 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
559 register const u_char
*p
= &v
[size
- 2];
560 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
562 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
569 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
577 static struct block
*
578 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
579 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
585 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
588 if (mask
!= 0xffffffff) {
589 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
593 b
= new_block(JMP(jtype
));
596 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
602 * Various code constructs need to know the layout of the data link
603 * layer. These variables give the necessary offsets.
607 * This is the offset of the beginning of the MAC-layer header.
608 * It's usually 0, except for ATM LANE.
610 static u_int off_mac
;
613 * "off_linktype" is the offset to information in the link-layer header
614 * giving the packet type.
616 * For Ethernet, it's the offset of the Ethernet type field.
618 * For link-layer types that always use 802.2 headers, it's the
619 * offset of the LLC header.
621 * For PPP, it's the offset of the PPP type field.
623 * For Cisco HDLC, it's the offset of the CHDLC type field.
625 * For BSD loopback, it's the offset of the AF_ value.
627 * For Linux cooked sockets, it's the offset of the type field.
629 * It's set to -1 for no encapsulation, in which case, IP is assumed.
631 static u_int off_linktype
;
634 * TRUE if the link layer includes an ATM pseudo-header.
636 static int is_atm
= 0;
639 * TRUE if "lane" appeared in the filter; it causes us to generate
640 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
642 static int is_lane
= 0;
645 * These are offsets for the ATM pseudo-header.
647 static u_int off_vpi
;
648 static u_int off_vci
;
649 static u_int off_proto
;
652 * This is the offset of the first byte after the ATM pseudo_header,
653 * or -1 if there is no ATM pseudo-header.
655 static u_int off_payload
;
658 * These are offsets to the beginning of the network-layer header.
660 * If the link layer never uses 802.2 LLC:
662 * "off_nl" and "off_nl_nosnap" are the same.
664 * If the link layer always uses 802.2 LLC:
666 * "off_nl" is the offset if there's a SNAP header following
669 * "off_nl_nosnap" is the offset if there's no SNAP header.
671 * If the link layer is Ethernet:
673 * "off_nl" is the offset if the packet is an Ethernet II packet
674 * (we assume no 802.3+802.2+SNAP);
676 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
677 * with an 802.2 header following it.
680 static u_int off_nl_nosnap
;
688 linktype
= pcap_datalink(p
);
690 pcap_fddipad
= p
->fddipad
;
694 * Assume it's not raw ATM with a pseudo-header, for now.
712 off_nl
= 6; /* XXX in reality, variable! */
713 off_nl_nosnap
= 6; /* no 802.2 LLC */
716 case DLT_ARCNET_LINUX
:
718 off_nl
= 8; /* XXX in reality, variable! */
719 off_nl_nosnap
= 8; /* no 802.2 LLC */
724 off_nl
= 14; /* Ethernet II */
725 off_nl_nosnap
= 17; /* 802.3+802.2 */
730 * SLIP doesn't have a link level type. The 16 byte
731 * header is hacked into our SLIP driver.
735 off_nl_nosnap
= 16; /* no 802.2 LLC */
739 /* XXX this may be the same as the DLT_PPP_BSDOS case */
743 off_nl_nosnap
= 24; /* no 802.2 LLC */
750 off_nl_nosnap
= 4; /* no 802.2 LLC */
756 off_nl_nosnap
= 12; /* no 802.2 LLC */
761 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
762 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
765 off_nl_nosnap
= 4; /* no 802.2 LLC */
770 * This does no include the Ethernet header, and
771 * only covers session state.
775 off_nl_nosnap
= 8; /* no 802.2 LLC */
781 off_nl_nosnap
= 24; /* no 802.2 LLC */
786 * FDDI doesn't really have a link-level type field.
787 * We set "off_linktype" to the offset of the LLC header.
789 * To check for Ethernet types, we assume that SSAP = SNAP
790 * is being used and pick out the encapsulated Ethernet type.
791 * XXX - should we generate code to check for SNAP?
795 off_linktype
+= pcap_fddipad
;
797 off_nl
= 21; /* FDDI+802.2+SNAP */
798 off_nl_nosnap
= 16; /* FDDI+802.2 */
800 off_nl
+= pcap_fddipad
;
801 off_nl_nosnap
+= pcap_fddipad
;
807 * Token Ring doesn't really have a link-level type field.
808 * We set "off_linktype" to the offset of the LLC header.
810 * To check for Ethernet types, we assume that SSAP = SNAP
811 * is being used and pick out the encapsulated Ethernet type.
812 * XXX - should we generate code to check for SNAP?
814 * XXX - the header is actually variable-length.
815 * Some various Linux patched versions gave 38
816 * as "off_linktype" and 40 as "off_nl"; however,
817 * if a token ring packet has *no* routing
818 * information, i.e. is not source-routed, the correct
819 * values are 20 and 22, as they are in the vanilla code.
821 * A packet is source-routed iff the uppermost bit
822 * of the first byte of the source address, at an
823 * offset of 8, has the uppermost bit set. If the
824 * packet is source-routed, the total number of bytes
825 * of routing information is 2 plus bits 0x1F00 of
826 * the 16-bit value at an offset of 14 (shifted right
827 * 8 - figure out which byte that is).
830 off_nl
= 22; /* Token Ring+802.2+SNAP */
831 off_nl_nosnap
= 17; /* Token Ring+802.2 */
836 * 802.11 doesn't really have a link-level type field.
837 * We set "off_linktype" to the offset of the LLC header.
839 * To check for Ethernet types, we assume that SSAP = SNAP
840 * is being used and pick out the encapsulated Ethernet type.
841 * XXX - should we generate code to check for SNAP?
843 * XXX - the header is actually variable-length. We
844 * assume a 24-byte link-layer header, as appears in
845 * data frames in networks with no bridges. If the
846 * fromds and tods 802.11 header bits are both set,
847 * it's actually supposed to be 30 bytes.
850 off_nl
= 32; /* 802.11+802.2+SNAP */
851 off_nl_nosnap
= 27; /* 802.11+802.2 */
854 case DLT_PRISM_HEADER
:
856 * Same as 802.11, but with an additional header before
857 * the 802.11 header, containing a bunch of additional
858 * information including radio-level information.
860 * The header is 144 bytes long.
862 * XXX - same variable-length header problem; at least
863 * the Prism header is fixed-length.
865 off_linktype
= 144+24;
866 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
867 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
870 case DLT_IEEE802_11_RADIO_AVS
:
872 * Same as 802.11, but with an additional header before
873 * the 802.11 header, containing a bunch of additional
874 * information including radio-level information.
876 * The header is 64 bytes long, at least in its
877 * current incarnation.
879 * XXX - same variable-length header problem, only
880 * more so; this header is also variable-length,
881 * with the length being the 32-bit big-endian
882 * number at an offset of 4 from the beginning
883 * of the radio header.
885 off_linktype
= 64+24;
886 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
887 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
890 case DLT_IEEE802_11_RADIO
:
892 * Same as 802.11, but with an additional header before
893 * the 802.11 header, containing a bunch of additional
894 * information including radio-level information.
896 * XXX - same variable-length header problem, only
897 * even *more* so; this header is also variable-length,
898 * with the length being the 16-bit number at an offset
899 * of 2 from the beginning of the radio header, and it's
900 * device-dependent (different devices might supply
901 * different amounts of information), so we can't even
902 * assume a fixed length for the current version of the
905 * Therefore, currently, only raw "link[N:M]" filtering is
913 case DLT_ATM_RFC1483
:
914 case DLT_ATM_CLIP
: /* Linux ATM defines this */
916 * assume routed, non-ISO PDUs
917 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
920 off_nl
= 8; /* 802.2+SNAP */
921 off_nl_nosnap
= 3; /* 802.2 */
926 * Full Frontal ATM; you get AALn PDUs with an ATM
930 off_vpi
= SUNATM_VPI_POS
;
931 off_vci
= SUNATM_VCI_POS
;
932 off_proto
= PROTO_POS
;
933 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
934 off_payload
= SUNATM_PKT_BEGIN_POS
;
935 off_linktype
= off_payload
;
936 off_nl
= off_payload
+8; /* 802.2+SNAP */
937 off_nl_nosnap
= off_payload
+3; /* 802.2 */
943 off_nl_nosnap
= 0; /* no 802.2 LLC */
946 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
949 off_nl_nosnap
= 16; /* no 802.2 LLC */
954 * LocalTalk does have a 1-byte type field in the LLAP header,
955 * but really it just indicates whether there is a "short" or
956 * "long" DDP packet following.
960 off_nl_nosnap
= 0; /* no 802.2 LLC */
965 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
966 * link-level type field. We set "off_linktype" to the
967 * offset of the LLC header.
969 * To check for Ethernet types, we assume that SSAP = SNAP
970 * is being used and pick out the encapsulated Ethernet type.
971 * XXX - should we generate code to check for SNAP? RFC
972 * 2625 says SNAP should be used.
975 off_nl
= 24; /* IPFC+802.2+SNAP */
976 off_nl_nosnap
= 19; /* IPFC+802.2 */
981 * XXX - we should set this to handle SNAP-encapsulated
982 * frames (NLPID of 0x80).
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
989 case DLT_APPLE_IP_OVER_IEEE1394
:
992 off_nl_nosnap
= 18; /* no 802.2 LLC */
997 * Currently, only raw "link[N:M]" filtering is supported.
1006 * Currently, only raw "link[N:M]" filtering is supported.
1013 case DLT_SYMANTEC_FIREWALL
:
1015 off_nl
= 44; /* Ethernet II */
1016 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1021 /* XXX read this from pf.h? */
1022 off_nl
= PFLOG_HDRLEN
;
1023 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1026 case DLT_JUNIPER_MLFR
:
1027 case DLT_JUNIPER_MLPPP
:
1030 off_nl_nosnap
= -1; /* no 802.2 LLC */
1033 case DLT_JUNIPER_ATM1
:
1034 off_linktype
= 4; /* in reality variable between 4-8 */
1039 case DLT_JUNIPER_ATM2
:
1040 off_linktype
= 8; /* in reality variable between 8-12 */
1053 case DLT_LINUX_LAPD
:
1055 * Currently, only raw "link[N:M]" filtering is supported.
1062 bpf_error("unknown data link type %d", linktype
);
1066 static struct block
*
1073 s
= new_stmt(BPF_LD
|BPF_IMM
);
1075 b
= new_block(JMP(BPF_JEQ
));
1081 static inline struct block
*
1084 return gen_uncond(1);
1087 static inline struct block
*
1090 return gen_uncond(0);
1094 * Byte-swap a 32-bit number.
1095 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1096 * big-endian platforms.)
1098 #define SWAPLONG(y) \
1099 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1102 * Generate code to match a particular packet type.
1104 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1105 * value, if <= ETHERMTU. We use that to determine whether to
1106 * match the type/length field or to check the type/length field for
1107 * a value <= ETHERMTU to see whether it's a type field and then do
1108 * the appropriate test.
1110 static struct block
*
1111 gen_ether_linktype(proto
)
1114 struct block
*b0
, *b1
;
1120 case LLCSAP_NETBEUI
:
1122 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1123 * so we check the DSAP and SSAP.
1125 * LLCSAP_IP checks for IP-over-802.2, rather
1126 * than IP-over-Ethernet or IP-over-SNAP.
1128 * XXX - should we check both the DSAP and the
1129 * SSAP, like this, or should we check just the
1130 * DSAP, as we do for other types <= ETHERMTU
1131 * (i.e., other SAP values)?
1133 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1135 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1136 ((proto
<< 8) | proto
));
1144 * Ethernet_II frames, which are Ethernet
1145 * frames with a frame type of ETHERTYPE_IPX;
1147 * Ethernet_802.3 frames, which are 802.3
1148 * frames (i.e., the type/length field is
1149 * a length field, <= ETHERMTU, rather than
1150 * a type field) with the first two bytes
1151 * after the Ethernet/802.3 header being
1154 * Ethernet_802.2 frames, which are 802.3
1155 * frames with an 802.2 LLC header and
1156 * with the IPX LSAP as the DSAP in the LLC
1159 * Ethernet_SNAP frames, which are 802.3
1160 * frames with an LLC header and a SNAP
1161 * header and with an OUI of 0x000000
1162 * (encapsulated Ethernet) and a protocol
1163 * ID of ETHERTYPE_IPX in the SNAP header.
1165 * XXX - should we generate the same code both
1166 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1170 * This generates code to check both for the
1171 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1173 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1174 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1178 * Now we add code to check for SNAP frames with
1179 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1181 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1185 * Now we generate code to check for 802.3
1186 * frames in general.
1188 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1192 * Now add the check for 802.3 frames before the
1193 * check for Ethernet_802.2 and Ethernet_802.3,
1194 * as those checks should only be done on 802.3
1195 * frames, not on Ethernet frames.
1200 * Now add the check for Ethernet_II frames, and
1201 * do that before checking for the other frame
1204 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1208 case ETHERTYPE_ATALK
:
1209 case ETHERTYPE_AARP
:
1211 * EtherTalk (AppleTalk protocols on Ethernet link
1212 * layer) may use 802.2 encapsulation.
1216 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1217 * we check for an Ethernet type field less than
1218 * 1500, which means it's an 802.3 length field.
1220 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1224 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1225 * SNAP packets with an organization code of
1226 * 0x080007 (Apple, for Appletalk) and a protocol
1227 * type of ETHERTYPE_ATALK (Appletalk).
1229 * 802.2-encapsulated ETHERTYPE_AARP packets are
1230 * SNAP packets with an organization code of
1231 * 0x000000 (encapsulated Ethernet) and a protocol
1232 * type of ETHERTYPE_AARP (Appletalk ARP).
1234 if (proto
== ETHERTYPE_ATALK
)
1235 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1236 else /* proto == ETHERTYPE_AARP */
1237 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1241 * Check for Ethernet encapsulation (Ethertalk
1242 * phase 1?); we just check for the Ethernet
1245 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1251 if (proto
<= ETHERMTU
) {
1253 * This is an LLC SAP value, so the frames
1254 * that match would be 802.2 frames.
1255 * Check that the frame is an 802.2 frame
1256 * (i.e., that the length/type field is
1257 * a length field, <= ETHERMTU) and
1258 * then check the DSAP.
1260 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1262 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1267 * This is an Ethernet type, so compare
1268 * the length/type field with it (if
1269 * the frame is an 802.2 frame, the length
1270 * field will be <= ETHERMTU, and, as
1271 * "proto" is > ETHERMTU, this test
1272 * will fail and the frame won't match,
1273 * which is what we want).
1275 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1281 * Generate code to match a particular packet type.
1283 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1284 * value, if <= ETHERMTU. We use that to determine whether to
1285 * match the type field or to check the type field for the special
1286 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1288 static struct block
*
1289 gen_linux_sll_linktype(proto
)
1292 struct block
*b0
, *b1
;
1298 case LLCSAP_NETBEUI
:
1300 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1301 * so we check the DSAP and SSAP.
1303 * LLCSAP_IP checks for IP-over-802.2, rather
1304 * than IP-over-Ethernet or IP-over-SNAP.
1306 * XXX - should we check both the DSAP and the
1307 * SSAP, like this, or should we check just the
1308 * DSAP, as we do for other types <= ETHERMTU
1309 * (i.e., other SAP values)?
1311 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1312 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1313 ((proto
<< 8) | proto
));
1319 * Ethernet_II frames, which are Ethernet
1320 * frames with a frame type of ETHERTYPE_IPX;
1322 * Ethernet_802.3 frames, which have a frame
1323 * type of LINUX_SLL_P_802_3;
1325 * Ethernet_802.2 frames, which are 802.3
1326 * frames with an 802.2 LLC header (i.e, have
1327 * a frame type of LINUX_SLL_P_802_2) and
1328 * with the IPX LSAP as the DSAP in the LLC
1331 * Ethernet_SNAP frames, which are 802.3
1332 * frames with an LLC header and a SNAP
1333 * header and with an OUI of 0x000000
1334 * (encapsulated Ethernet) and a protocol
1335 * ID of ETHERTYPE_IPX in the SNAP header.
1337 * First, do the checks on LINUX_SLL_P_802_2
1338 * frames; generate the check for either
1339 * Ethernet_802.2 or Ethernet_SNAP frames, and
1340 * then put a check for LINUX_SLL_P_802_2 frames
1343 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1344 (bpf_int32
)LLCSAP_IPX
);
1345 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1348 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1352 * Now check for 802.3 frames and OR that with
1353 * the previous test.
1355 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1359 * Now add the check for Ethernet_II frames, and
1360 * do that before checking for the other frame
1363 b0
= gen_cmp(off_linktype
, BPF_H
,
1364 (bpf_int32
)ETHERTYPE_IPX
);
1368 case ETHERTYPE_ATALK
:
1369 case ETHERTYPE_AARP
:
1371 * EtherTalk (AppleTalk protocols on Ethernet link
1372 * layer) may use 802.2 encapsulation.
1376 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1377 * we check for the 802.2 protocol type in the
1378 * "Ethernet type" field.
1380 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1383 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1384 * SNAP packets with an organization code of
1385 * 0x080007 (Apple, for Appletalk) and a protocol
1386 * type of ETHERTYPE_ATALK (Appletalk).
1388 * 802.2-encapsulated ETHERTYPE_AARP packets are
1389 * SNAP packets with an organization code of
1390 * 0x000000 (encapsulated Ethernet) and a protocol
1391 * type of ETHERTYPE_AARP (Appletalk ARP).
1393 if (proto
== ETHERTYPE_ATALK
)
1394 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1396 else /* proto == ETHERTYPE_AARP */
1397 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1402 * Check for Ethernet encapsulation (Ethertalk
1403 * phase 1?); we just check for the Ethernet
1406 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1412 if (proto
<= ETHERMTU
) {
1414 * This is an LLC SAP value, so the frames
1415 * that match would be 802.2 frames.
1416 * Check for the 802.2 protocol type
1417 * in the "Ethernet type" field, and
1418 * then check the DSAP.
1420 b0
= gen_cmp(off_linktype
, BPF_H
,
1422 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1428 * This is an Ethernet type, so compare
1429 * the length/type field with it (if
1430 * the frame is an 802.2 frame, the length
1431 * field will be <= ETHERMTU, and, as
1432 * "proto" is > ETHERMTU, this test
1433 * will fail and the frame won't match,
1434 * which is what we want).
1436 return gen_cmp(off_linktype
, BPF_H
,
1443 * Generate code to match a particular packet type by matching the
1444 * link-layer type field or fields in the 802.2 LLC header.
1446 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1447 * value, if <= ETHERMTU.
1449 static struct block
*
1453 struct block
*b0
, *b1
, *b2
;
1458 return gen_ether_linktype(proto
);
1466 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1470 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1476 case DLT_IEEE802_11
:
1477 case DLT_PRISM_HEADER
:
1478 case DLT_IEEE802_11_RADIO
:
1481 case DLT_ATM_RFC1483
:
1483 case DLT_IP_OVER_FC
:
1484 return gen_llc_linktype(proto
);
1490 * If "is_lane" is set, check for a LANE-encapsulated
1491 * version of this protocol, otherwise check for an
1492 * LLC-encapsulated version of this protocol.
1494 * We assume LANE means Ethernet, not Token Ring.
1498 * Check that the packet doesn't begin with an
1499 * LE Control marker. (We've already generated
1502 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1506 * Now generate an Ethernet test.
1508 b1
= gen_ether_linktype(proto
);
1513 * Check for LLC encapsulation and then check the
1516 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1517 b1
= gen_llc_linktype(proto
);
1525 return gen_linux_sll_linktype(proto
);
1530 case DLT_SLIP_BSDOS
:
1533 * These types don't provide any type field; packets
1536 * XXX - for IPv4, check for a version number of 4, and,
1537 * for IPv6, check for a version number of 6?
1543 case ETHERTYPE_IPV6
:
1545 return gen_true(); /* always true */
1548 return gen_false(); /* always false */
1555 case DLT_PPP_SERIAL
:
1558 * We use Ethernet protocol types inside libpcap;
1559 * map them to the corresponding PPP protocol types.
1568 case ETHERTYPE_IPV6
:
1577 case ETHERTYPE_ATALK
:
1591 * I'm assuming the "Bridging PDU"s that go
1592 * over PPP are Spanning Tree Protocol
1606 * We use Ethernet protocol types inside libpcap;
1607 * map them to the corresponding PPP protocol types.
1612 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1613 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1615 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1620 case ETHERTYPE_IPV6
:
1630 case ETHERTYPE_ATALK
:
1644 * I'm assuming the "Bridging PDU"s that go
1645 * over PPP are Spanning Tree Protocol
1661 * For DLT_NULL, the link-layer header is a 32-bit
1662 * word containing an AF_ value in *host* byte order,
1663 * and for DLT_ENC, the link-layer header begins
1664 * with a 32-bit work containing an AF_ value in
1667 * In addition, if we're reading a saved capture file,
1668 * the host byte order in the capture may not be the
1669 * same as the host byte order on this machine.
1671 * For DLT_LOOP, the link-layer header is a 32-bit
1672 * word containing an AF_ value in *network* byte order.
1674 * XXX - AF_ values may, unfortunately, be platform-
1675 * dependent; for example, FreeBSD's AF_INET6 is 24
1676 * whilst NetBSD's and OpenBSD's is 26.
1678 * This means that, when reading a capture file, just
1679 * checking for our AF_INET6 value won't work if the
1680 * capture file came from another OS.
1689 case ETHERTYPE_IPV6
:
1696 * Not a type on which we support filtering.
1697 * XXX - support those that have AF_ values
1698 * #defined on this platform, at least?
1703 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1705 * The AF_ value is in host byte order, but
1706 * the BPF interpreter will convert it to
1707 * network byte order.
1709 * If this is a save file, and it's from a
1710 * machine with the opposite byte order to
1711 * ours, we byte-swap the AF_ value.
1713 * Then we run it through "htonl()", and
1714 * generate code to compare against the result.
1716 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1717 bpf_pcap
->sf
.swapped
)
1718 proto
= SWAPLONG(proto
);
1719 proto
= htonl(proto
);
1721 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1725 * af field is host byte order in contrast to the rest of
1728 if (proto
== ETHERTYPE_IP
)
1729 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1730 (bpf_int32
)AF_INET
));
1732 else if (proto
== ETHERTYPE_IPV6
)
1733 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1734 (bpf_int32
)AF_INET6
));
1742 case DLT_ARCNET_LINUX
:
1744 * XXX should we check for first fragment if the protocol
1753 case ETHERTYPE_IPV6
:
1754 return (gen_cmp(off_linktype
, BPF_B
,
1755 (bpf_int32
)ARCTYPE_INET6
));
1759 b0
= gen_cmp(off_linktype
, BPF_B
,
1760 (bpf_int32
)ARCTYPE_IP
);
1761 b1
= gen_cmp(off_linktype
, BPF_B
,
1762 (bpf_int32
)ARCTYPE_IP_OLD
);
1767 b0
= gen_cmp(off_linktype
, BPF_B
,
1768 (bpf_int32
)ARCTYPE_ARP
);
1769 b1
= gen_cmp(off_linktype
, BPF_B
,
1770 (bpf_int32
)ARCTYPE_ARP_OLD
);
1774 case ETHERTYPE_REVARP
:
1775 return (gen_cmp(off_linktype
, BPF_B
,
1776 (bpf_int32
)ARCTYPE_REVARP
));
1778 case ETHERTYPE_ATALK
:
1779 return (gen_cmp(off_linktype
, BPF_B
,
1780 (bpf_int32
)ARCTYPE_ATALK
));
1787 case ETHERTYPE_ATALK
:
1797 * XXX - assumes a 2-byte Frame Relay header with
1798 * DLCI and flags. What if the address is longer?
1804 * Check for the special NLPID for IP.
1806 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1809 case ETHERTYPE_IPV6
:
1811 * Check for the special NLPID for IPv6.
1813 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1818 * Check for several OSI protocols.
1820 * Frame Relay packets typically have an OSI
1821 * NLPID at the beginning; we check for each
1824 * What we check for is the NLPID and a frame
1825 * control field of UI, i.e. 0x03 followed
1828 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1829 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1830 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1841 case DLT_JUNIPER_MLFR
:
1842 case DLT_JUNIPER_MLPPP
:
1843 case DLT_JUNIPER_ATM1
:
1844 case DLT_JUNIPER_ATM2
:
1845 /* just lets verify the magic number for now -
1846 * on ATM we may have up to 6 different encapsulations on the wire
1847 * and need a lot of heuristics to figure out that the payload
1850 * FIXME encapsulation specific BPF_ filters
1852 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1854 case DLT_LINUX_IRDA
:
1855 bpf_error("IrDA link-layer type filtering not implemented");
1858 bpf_error("DOCSIS link-layer type filtering not implemented");
1860 case DLT_LINUX_LAPD
:
1861 bpf_error("LAPD link-layer type filtering not implemented");
1865 * All the types that have no encapsulation should either be
1866 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1867 * all packets are IP packets, or should be handled in some
1868 * special case, if none of them are (if some are and some
1869 * aren't, the lack of encapsulation is a problem, as we'd
1870 * have to find some other way of determining the packet type).
1872 * Therefore, if "off_linktype" is -1, there's an error.
1874 if (off_linktype
== (u_int
)-1)
1878 * Any type not handled above should always have an Ethernet
1879 * type at an offset of "off_linktype". (PPP is partially
1880 * handled above - the protocol type is mapped from the
1881 * Ethernet and LLC types we use internally to the corresponding
1882 * PPP type - but the PPP type is always specified by a value
1883 * at "off_linktype", so we don't have to do the code generation
1886 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1890 * Check for an LLC SNAP packet with a given organization code and
1891 * protocol type; we check the entire contents of the 802.2 LLC and
1892 * snap headers, checking for DSAP and SSAP of SNAP and a control
1893 * field of 0x03 in the LLC header, and for the specified organization
1894 * code and protocol type in the SNAP header.
1896 static struct block
*
1897 gen_snap(orgcode
, ptype
, offset
)
1898 bpf_u_int32 orgcode
;
1902 u_char snapblock
[8];
1904 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1905 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1906 snapblock
[2] = 0x03; /* control = UI */
1907 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1908 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1909 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1910 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1911 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1912 return gen_bcmp(offset
, 8, snapblock
);
1916 * Generate code to match a particular packet type, for link-layer types
1917 * using 802.2 LLC headers.
1919 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1920 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1922 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1923 * value, if <= ETHERMTU. We use that to determine whether to
1924 * match the DSAP or both DSAP and LSAP or to check the OUI and
1925 * protocol ID in a SNAP header.
1927 static struct block
*
1928 gen_llc_linktype(proto
)
1932 * XXX - handle token-ring variable-length header.
1938 case LLCSAP_NETBEUI
:
1940 * XXX - should we check both the DSAP and the
1941 * SSAP, like this, or should we check just the
1942 * DSAP, as we do for other types <= ETHERMTU
1943 * (i.e., other SAP values)?
1945 return gen_cmp(off_linktype
, BPF_H
, (long)
1946 ((proto
<< 8) | proto
));
1950 * XXX - are there ever SNAP frames for IPX on
1951 * non-Ethernet 802.x networks?
1953 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1955 case ETHERTYPE_ATALK
:
1957 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1958 * SNAP packets with an organization code of
1959 * 0x080007 (Apple, for Appletalk) and a protocol
1960 * type of ETHERTYPE_ATALK (Appletalk).
1962 * XXX - check for an organization code of
1963 * encapsulated Ethernet as well?
1965 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1969 * XXX - we don't have to check for IPX 802.3
1970 * here, but should we check for the IPX Ethertype?
1972 if (proto
<= ETHERMTU
) {
1974 * This is an LLC SAP value, so check
1977 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1980 * This is an Ethernet type; we assume that it's
1981 * unlikely that it'll appear in the right place
1982 * at random, and therefore check only the
1983 * location that would hold the Ethernet type
1984 * in a SNAP frame with an organization code of
1985 * 0x000000 (encapsulated Ethernet).
1987 * XXX - if we were to check for the SNAP DSAP and
1988 * LSAP, as per XXX, and were also to check for an
1989 * organization code of 0x000000 (encapsulated
1990 * Ethernet), we'd do
1992 * return gen_snap(0x000000, proto,
1995 * here; for now, we don't, as per the above.
1996 * I don't know whether it's worth the extra CPU
1997 * time to do the right check or not.
1999 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2004 static struct block
*
2005 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2009 u_int src_off
, dst_off
;
2011 struct block
*b0
, *b1
;
2025 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2026 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2032 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2033 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2040 b0
= gen_linktype(proto
);
2041 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2047 static struct block
*
2048 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2049 struct in6_addr
*addr
;
2050 struct in6_addr
*mask
;
2052 u_int src_off
, dst_off
;
2054 struct block
*b0
, *b1
;
2069 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2070 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2076 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2077 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2084 /* this order is important */
2085 a
= (u_int32_t
*)addr
;
2086 m
= (u_int32_t
*)mask
;
2087 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2088 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2090 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2092 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2094 b0
= gen_linktype(proto
);
2100 static struct block
*
2101 gen_ehostop(eaddr
, dir
)
2102 register const u_char
*eaddr
;
2105 register struct block
*b0
, *b1
;
2109 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2112 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2115 b0
= gen_ehostop(eaddr
, Q_SRC
);
2116 b1
= gen_ehostop(eaddr
, Q_DST
);
2122 b0
= gen_ehostop(eaddr
, Q_SRC
);
2123 b1
= gen_ehostop(eaddr
, Q_DST
);
2132 * Like gen_ehostop, but for DLT_FDDI
2134 static struct block
*
2135 gen_fhostop(eaddr
, dir
)
2136 register const u_char
*eaddr
;
2139 struct block
*b0
, *b1
;
2144 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2146 return gen_bcmp(6 + 1, 6, eaddr
);
2151 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2153 return gen_bcmp(0 + 1, 6, eaddr
);
2157 b0
= gen_fhostop(eaddr
, Q_SRC
);
2158 b1
= gen_fhostop(eaddr
, Q_DST
);
2164 b0
= gen_fhostop(eaddr
, Q_SRC
);
2165 b1
= gen_fhostop(eaddr
, Q_DST
);
2174 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2176 static struct block
*
2177 gen_thostop(eaddr
, dir
)
2178 register const u_char
*eaddr
;
2181 register struct block
*b0
, *b1
;
2185 return gen_bcmp(8, 6, eaddr
);
2188 return gen_bcmp(2, 6, eaddr
);
2191 b0
= gen_thostop(eaddr
, Q_SRC
);
2192 b1
= gen_thostop(eaddr
, Q_DST
);
2198 b0
= gen_thostop(eaddr
, Q_SRC
);
2199 b1
= gen_thostop(eaddr
, Q_DST
);
2208 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2210 static struct block
*
2211 gen_wlanhostop(eaddr
, dir
)
2212 register const u_char
*eaddr
;
2215 register struct block
*b0
, *b1
, *b2
;
2216 register struct slist
*s
;
2223 * For control frames, there is no SA.
2225 * For management frames, SA is at an
2226 * offset of 10 from the beginning of
2229 * For data frames, SA is at an offset
2230 * of 10 from the beginning of the packet
2231 * if From DS is clear, at an offset of
2232 * 16 from the beginning of the packet
2233 * if From DS is set and To DS is clear,
2234 * and an offset of 24 from the beginning
2235 * of the packet if From DS is set and To DS
2240 * Generate the tests to be done for data frames
2243 * First, check for To DS set, i.e. check "link[1] & 0x01".
2245 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2247 b1
= new_block(JMP(BPF_JSET
));
2248 b1
->s
.k
= 0x01; /* To DS */
2252 * If To DS is set, the SA is at 24.
2254 b0
= gen_bcmp(24, 6, eaddr
);
2258 * Now, check for To DS not set, i.e. check
2259 * "!(link[1] & 0x01)".
2261 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2263 b2
= new_block(JMP(BPF_JSET
));
2264 b2
->s
.k
= 0x01; /* To DS */
2269 * If To DS is not set, the SA is at 16.
2271 b1
= gen_bcmp(16, 6, eaddr
);
2275 * Now OR together the last two checks. That gives
2276 * the complete set of checks for data frames with
2282 * Now check for From DS being set, and AND that with
2283 * the ORed-together checks.
2285 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2287 b1
= new_block(JMP(BPF_JSET
));
2288 b1
->s
.k
= 0x02; /* From DS */
2293 * Now check for data frames with From DS not set.
2295 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2297 b2
= new_block(JMP(BPF_JSET
));
2298 b2
->s
.k
= 0x02; /* From DS */
2303 * If From DS isn't set, the SA is at 10.
2305 b1
= gen_bcmp(10, 6, eaddr
);
2309 * Now OR together the checks for data frames with
2310 * From DS not set and for data frames with From DS
2311 * set; that gives the checks done for data frames.
2316 * Now check for a data frame.
2317 * I.e, check "link[0] & 0x08".
2319 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2321 b1
= new_block(JMP(BPF_JSET
));
2326 * AND that with the checks done for data frames.
2331 * If the high-order bit of the type value is 0, this
2332 * is a management frame.
2333 * I.e, check "!(link[0] & 0x08)".
2335 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2337 b2
= new_block(JMP(BPF_JSET
));
2343 * For management frames, the SA is at 10.
2345 b1
= gen_bcmp(10, 6, eaddr
);
2349 * OR that with the checks done for data frames.
2350 * That gives the checks done for management and
2356 * If the low-order bit of the type value is 1,
2357 * this is either a control frame or a frame
2358 * with a reserved type, and thus not a
2361 * I.e., check "!(link[0] & 0x04)".
2363 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2365 b1
= new_block(JMP(BPF_JSET
));
2371 * AND that with the checks for data and management
2381 * For control frames, there is no DA.
2383 * For management frames, DA is at an
2384 * offset of 4 from the beginning of
2387 * For data frames, DA is at an offset
2388 * of 4 from the beginning of the packet
2389 * if To DS is clear and at an offset of
2390 * 16 from the beginning of the packet
2395 * Generate the tests to be done for data frames.
2397 * First, check for To DS set, i.e. "link[1] & 0x01".
2399 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2401 b1
= new_block(JMP(BPF_JSET
));
2402 b1
->s
.k
= 0x01; /* To DS */
2406 * If To DS is set, the DA is at 16.
2408 b0
= gen_bcmp(16, 6, eaddr
);
2412 * Now, check for To DS not set, i.e. check
2413 * "!(link[1] & 0x01)".
2415 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2417 b2
= new_block(JMP(BPF_JSET
));
2418 b2
->s
.k
= 0x01; /* To DS */
2423 * If To DS is not set, the DA is at 4.
2425 b1
= gen_bcmp(4, 6, eaddr
);
2429 * Now OR together the last two checks. That gives
2430 * the complete set of checks for data frames.
2435 * Now check for a data frame.
2436 * I.e, check "link[0] & 0x08".
2438 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2440 b1
= new_block(JMP(BPF_JSET
));
2445 * AND that with the checks done for data frames.
2450 * If the high-order bit of the type value is 0, this
2451 * is a management frame.
2452 * I.e, check "!(link[0] & 0x08)".
2454 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2456 b2
= new_block(JMP(BPF_JSET
));
2462 * For management frames, the DA is at 4.
2464 b1
= gen_bcmp(4, 6, eaddr
);
2468 * OR that with the checks done for data frames.
2469 * That gives the checks done for management and
2475 * If the low-order bit of the type value is 1,
2476 * this is either a control frame or a frame
2477 * with a reserved type, and thus not a
2480 * I.e., check "!(link[0] & 0x04)".
2482 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2484 b1
= new_block(JMP(BPF_JSET
));
2490 * AND that with the checks for data and management
2497 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2498 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2504 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2505 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2514 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2515 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2516 * as the RFC states.)
2518 static struct block
*
2519 gen_ipfchostop(eaddr
, dir
)
2520 register const u_char
*eaddr
;
2523 register struct block
*b0
, *b1
;
2527 return gen_bcmp(10, 6, eaddr
);
2530 return gen_bcmp(2, 6, eaddr
);
2533 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2534 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2540 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2541 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2550 * This is quite tricky because there may be pad bytes in front of the
2551 * DECNET header, and then there are two possible data packet formats that
2552 * carry both src and dst addresses, plus 5 packet types in a format that
2553 * carries only the src node, plus 2 types that use a different format and
2554 * also carry just the src node.
2558 * Instead of doing those all right, we just look for data packets with
2559 * 0 or 1 bytes of padding. If you want to look at other packets, that
2560 * will require a lot more hacking.
2562 * To add support for filtering on DECNET "areas" (network numbers)
2563 * one would want to add a "mask" argument to this routine. That would
2564 * make the filter even more inefficient, although one could be clever
2565 * and not generate masking instructions if the mask is 0xFFFF.
2567 static struct block
*
2568 gen_dnhostop(addr
, dir
, base_off
)
2573 struct block
*b0
, *b1
, *b2
, *tmp
;
2574 u_int offset_lh
; /* offset if long header is received */
2575 u_int offset_sh
; /* offset if short header is received */
2580 offset_sh
= 1; /* follows flags */
2581 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2585 offset_sh
= 3; /* follows flags, dstnode */
2586 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2590 /* Inefficient because we do our Calvinball dance twice */
2591 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2592 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2598 /* Inefficient because we do our Calvinball dance twice */
2599 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2600 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2605 bpf_error("ISO host filtering not implemented");
2610 b0
= gen_linktype(ETHERTYPE_DN
);
2611 /* Check for pad = 1, long header case */
2612 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2613 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2614 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2615 BPF_H
, (bpf_int32
)ntohs(addr
));
2617 /* Check for pad = 0, long header case */
2618 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2619 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2622 /* Check for pad = 1, short header case */
2623 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2624 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2625 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2626 BPF_H
, (bpf_int32
)ntohs(addr
));
2629 /* Check for pad = 0, short header case */
2630 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2631 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2635 /* Combine with test for linktype */
2640 static struct block
*
2641 gen_host(addr
, mask
, proto
, dir
)
2647 struct block
*b0
, *b1
;
2652 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2653 if (off_linktype
!= (u_int
)-1) {
2654 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2656 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2662 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2663 off_nl
+ 12, off_nl
+ 16);
2666 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2667 off_nl
+ 14, off_nl
+ 24);
2670 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2671 off_nl
+ 14, off_nl
+ 24);
2674 bpf_error("'tcp' modifier applied to host");
2677 bpf_error("'sctp' modifier applied to host");
2680 bpf_error("'udp' modifier applied to host");
2683 bpf_error("'icmp' modifier applied to host");
2686 bpf_error("'igmp' modifier applied to host");
2689 bpf_error("'igrp' modifier applied to host");
2692 bpf_error("'pim' modifier applied to host");
2695 bpf_error("'vrrp' modifier applied to host");
2698 bpf_error("ATALK host filtering not implemented");
2701 bpf_error("AARP host filtering not implemented");
2704 return gen_dnhostop(addr
, dir
, off_nl
);
2707 bpf_error("SCA host filtering not implemented");
2710 bpf_error("LAT host filtering not implemented");
2713 bpf_error("MOPDL host filtering not implemented");
2716 bpf_error("MOPRC host filtering not implemented");
2720 bpf_error("'ip6' modifier applied to ip host");
2723 bpf_error("'icmp6' modifier applied to host");
2727 bpf_error("'ah' modifier applied to host");
2730 bpf_error("'esp' modifier applied to host");
2733 bpf_error("ISO host filtering not implemented");
2736 bpf_error("'esis' modifier applied to host");
2739 bpf_error("'isis' modifier applied to host");
2742 bpf_error("'clnp' modifier applied to host");
2745 bpf_error("'stp' modifier applied to host");
2748 bpf_error("IPX host filtering not implemented");
2751 bpf_error("'netbeui' modifier applied to host");
2760 static struct block
*
2761 gen_host6(addr
, mask
, proto
, dir
)
2762 struct in6_addr
*addr
;
2763 struct in6_addr
*mask
;
2770 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2773 bpf_error("'ip' modifier applied to ip6 host");
2776 bpf_error("'rarp' modifier applied to ip6 host");
2779 bpf_error("'arp' modifier applied to ip6 host");
2782 bpf_error("'sctp' modifier applied to host");
2785 bpf_error("'tcp' modifier applied to host");
2788 bpf_error("'udp' modifier applied to host");
2791 bpf_error("'icmp' modifier applied to host");
2794 bpf_error("'igmp' modifier applied to host");
2797 bpf_error("'igrp' modifier applied to host");
2800 bpf_error("'pim' modifier applied to host");
2803 bpf_error("'vrrp' modifier applied to host");
2806 bpf_error("ATALK host filtering not implemented");
2809 bpf_error("AARP host filtering not implemented");
2812 bpf_error("'decnet' modifier applied to ip6 host");
2815 bpf_error("SCA host filtering not implemented");
2818 bpf_error("LAT host filtering not implemented");
2821 bpf_error("MOPDL host filtering not implemented");
2824 bpf_error("MOPRC host filtering not implemented");
2827 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2828 off_nl
+ 8, off_nl
+ 24);
2831 bpf_error("'icmp6' modifier applied to host");
2834 bpf_error("'ah' modifier applied to host");
2837 bpf_error("'esp' modifier applied to host");
2840 bpf_error("ISO host filtering not implemented");
2843 bpf_error("'esis' modifier applied to host");
2846 bpf_error("'isis' modifier applied to host");
2849 bpf_error("'clnp' modifier applied to host");
2852 bpf_error("'stp' modifier applied to host");
2855 bpf_error("IPX host filtering not implemented");
2858 bpf_error("'netbeui' modifier applied to host");
2868 static struct block
*
2869 gen_gateway(eaddr
, alist
, proto
, dir
)
2870 const u_char
*eaddr
;
2871 bpf_u_int32
**alist
;
2875 struct block
*b0
, *b1
, *tmp
;
2878 bpf_error("direction applied to 'gateway'");
2885 if (linktype
== DLT_EN10MB
)
2886 b0
= gen_ehostop(eaddr
, Q_OR
);
2887 else if (linktype
== DLT_FDDI
)
2888 b0
= gen_fhostop(eaddr
, Q_OR
);
2889 else if (linktype
== DLT_IEEE802
)
2890 b0
= gen_thostop(eaddr
, Q_OR
);
2891 else if (linktype
== DLT_IEEE802_11
)
2892 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2893 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2895 * Check that the packet doesn't begin with an
2896 * LE Control marker. (We've already generated
2899 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2903 * Now check the MAC address.
2905 b0
= gen_ehostop(eaddr
, Q_OR
);
2907 } else if (linktype
== DLT_IP_OVER_FC
)
2908 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2911 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2913 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2915 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2923 bpf_error("illegal modifier of 'gateway'");
2929 gen_proto_abbrev(proto
)
2938 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2940 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2946 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2948 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2954 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2956 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2962 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2965 #ifndef IPPROTO_IGMP
2966 #define IPPROTO_IGMP 2
2970 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2973 #ifndef IPPROTO_IGRP
2974 #define IPPROTO_IGRP 9
2977 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2981 #define IPPROTO_PIM 103
2985 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2987 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2992 #ifndef IPPROTO_VRRP
2993 #define IPPROTO_VRRP 112
2997 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3001 b1
= gen_linktype(ETHERTYPE_IP
);
3005 b1
= gen_linktype(ETHERTYPE_ARP
);
3009 b1
= gen_linktype(ETHERTYPE_REVARP
);
3013 bpf_error("link layer applied in wrong context");
3016 b1
= gen_linktype(ETHERTYPE_ATALK
);
3020 b1
= gen_linktype(ETHERTYPE_AARP
);
3024 b1
= gen_linktype(ETHERTYPE_DN
);
3028 b1
= gen_linktype(ETHERTYPE_SCA
);
3032 b1
= gen_linktype(ETHERTYPE_LAT
);
3036 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3040 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3045 b1
= gen_linktype(ETHERTYPE_IPV6
);
3048 #ifndef IPPROTO_ICMPV6
3049 #define IPPROTO_ICMPV6 58
3052 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3057 #define IPPROTO_AH 51
3060 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3062 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3068 #define IPPROTO_ESP 50
3071 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3073 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3079 b1
= gen_linktype(LLCSAP_ISONS
);
3083 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3087 b0
= gen_linktype(LLCSAP_ISONS
);
3088 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3092 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3093 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3094 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3096 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3098 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3100 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3104 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3105 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3106 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3108 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3110 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3112 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3116 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3117 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3118 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3120 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3125 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3126 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3131 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3132 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3134 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3136 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3141 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3142 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3147 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3148 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3153 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3157 b1
= gen_linktype(LLCSAP_8021D
);
3161 b1
= gen_linktype(LLCSAP_IPX
);
3165 b1
= gen_linktype(LLCSAP_NETBEUI
);
3174 static struct block
*
3181 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3182 s
->s
.k
= off_nl
+ 6;
3183 b
= new_block(JMP(BPF_JSET
));
3191 static struct block
*
3192 gen_portatom(off
, v
)
3199 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3202 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3203 s
->next
->s
.k
= off_nl
+ off
;
3205 b
= new_block(JMP(BPF_JEQ
));
3213 static struct block
*
3214 gen_portatom6(off
, v
)
3218 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3223 gen_portop(port
, proto
, dir
)
3224 int port
, proto
, dir
;
3226 struct block
*b0
, *b1
, *tmp
;
3228 /* ip proto 'proto' */
3229 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3235 b1
= gen_portatom(0, (bpf_int32
)port
);
3239 b1
= gen_portatom(2, (bpf_int32
)port
);
3244 tmp
= gen_portatom(0, (bpf_int32
)port
);
3245 b1
= gen_portatom(2, (bpf_int32
)port
);
3250 tmp
= gen_portatom(0, (bpf_int32
)port
);
3251 b1
= gen_portatom(2, (bpf_int32
)port
);
3263 static struct block
*
3264 gen_port(port
, ip_proto
, dir
)
3269 struct block
*b0
, *b1
, *tmp
;
3274 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3275 * not LLC encapsulation with LLCSAP_IP.
3277 * For IEEE 802 networks - which includes 802.5 token ring
3278 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3279 * says that SNAP encapsulation is used, not LLC encapsulation
3282 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3283 * RFC 2225 say that SNAP encapsulation is used, not LLC
3284 * encapsulation with LLCSAP_IP.
3286 * So we always check for ETHERTYPE_IP.
3288 b0
= gen_linktype(ETHERTYPE_IP
);
3294 b1
= gen_portop(port
, ip_proto
, dir
);
3298 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3299 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3301 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3314 gen_portop6(port
, proto
, dir
)
3315 int port
, proto
, dir
;
3317 struct block
*b0
, *b1
, *tmp
;
3319 /* ip proto 'proto' */
3320 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3324 b1
= gen_portatom6(0, (bpf_int32
)port
);
3328 b1
= gen_portatom6(2, (bpf_int32
)port
);
3333 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3334 b1
= gen_portatom6(2, (bpf_int32
)port
);
3339 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3340 b1
= gen_portatom6(2, (bpf_int32
)port
);
3352 static struct block
*
3353 gen_port6(port
, ip_proto
, dir
)
3358 struct block
*b0
, *b1
, *tmp
;
3360 /* ether proto ip */
3361 b0
= gen_linktype(ETHERTYPE_IPV6
);
3367 b1
= gen_portop6(port
, ip_proto
, dir
);
3371 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3372 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3374 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3387 lookup_proto(name
, proto
)
3388 register const char *name
;
3398 v
= pcap_nametoproto(name
);
3399 if (v
== PROTO_UNDEF
)
3400 bpf_error("unknown ip proto '%s'", name
);
3404 /* XXX should look up h/w protocol type based on linktype */
3405 v
= pcap_nametoeproto(name
);
3406 if (v
== PROTO_UNDEF
) {
3407 v
= pcap_nametollc(name
);
3408 if (v
== PROTO_UNDEF
)
3409 bpf_error("unknown ether proto '%s'", name
);
3414 if (strcmp(name
, "esis") == 0)
3416 else if (strcmp(name
, "isis") == 0)
3418 else if (strcmp(name
, "clnp") == 0)
3421 bpf_error("unknown osi proto '%s'", name
);
3441 static struct block
*
3442 gen_protochain(v
, proto
, dir
)
3447 #ifdef NO_PROTOCHAIN
3448 return gen_proto(v
, proto
, dir
);
3450 struct block
*b0
, *b
;
3451 struct slist
*s
[100];
3452 int fix2
, fix3
, fix4
, fix5
;
3453 int ahcheck
, again
, end
;
3455 int reg2
= alloc_reg();
3457 memset(s
, 0, sizeof(s
));
3458 fix2
= fix3
= fix4
= fix5
= 0;
3465 b0
= gen_protochain(v
, Q_IP
, dir
);
3466 b
= gen_protochain(v
, Q_IPV6
, dir
);
3470 bpf_error("bad protocol applied for 'protochain'");
3474 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3477 * s[0] is a dummy entry to protect other BPF insn from damaged
3478 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3479 * hard to find interdependency made by jump table fixup.
3482 s
[i
] = new_stmt(0); /*dummy*/
3487 b0
= gen_linktype(ETHERTYPE_IP
);
3490 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3491 s
[i
]->s
.k
= off_nl
+ 9;
3493 /* X = ip->ip_hl << 2 */
3494 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3500 b0
= gen_linktype(ETHERTYPE_IPV6
);
3502 /* A = ip6->ip_nxt */
3503 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3504 s
[i
]->s
.k
= off_nl
+ 6;
3506 /* X = sizeof(struct ip6_hdr) */
3507 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3513 bpf_error("unsupported proto to gen_protochain");
3517 /* again: if (A == v) goto end; else fall through; */
3519 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3521 s
[i
]->s
.jt
= NULL
; /*later*/
3522 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3526 #ifndef IPPROTO_NONE
3527 #define IPPROTO_NONE 59
3529 /* if (A == IPPROTO_NONE) goto end */
3530 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3531 s
[i
]->s
.jt
= NULL
; /*later*/
3532 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3533 s
[i
]->s
.k
= IPPROTO_NONE
;
3534 s
[fix5
]->s
.jf
= s
[i
];
3539 if (proto
== Q_IPV6
) {
3540 int v6start
, v6end
, v6advance
, j
;
3543 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3544 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3545 s
[i
]->s
.jt
= NULL
; /*later*/
3546 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3547 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3548 s
[fix2
]->s
.jf
= s
[i
];
3550 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3551 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3552 s
[i
]->s
.jt
= NULL
; /*later*/
3553 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3554 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3556 /* if (A == IPPROTO_ROUTING) goto v6advance */
3557 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3558 s
[i
]->s
.jt
= NULL
; /*later*/
3559 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3560 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3562 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3563 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3564 s
[i
]->s
.jt
= NULL
; /*later*/
3565 s
[i
]->s
.jf
= NULL
; /*later*/
3566 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3577 * X = X + (P[X + 1] + 1) * 8;
3580 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3582 /* A = P[X + packet head] */
3583 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3587 s
[i
] = new_stmt(BPF_ST
);
3591 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3594 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3598 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3600 /* A = P[X + packet head]; */
3601 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3605 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3609 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3613 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3616 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3620 /* goto again; (must use BPF_JA for backward jump) */
3621 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3622 s
[i
]->s
.k
= again
- i
- 1;
3623 s
[i
- 1]->s
.jf
= s
[i
];
3627 for (j
= v6start
; j
<= v6end
; j
++)
3628 s
[j
]->s
.jt
= s
[v6advance
];
3633 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3635 s
[fix2
]->s
.jf
= s
[i
];
3641 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3642 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3643 s
[i
]->s
.jt
= NULL
; /*later*/
3644 s
[i
]->s
.jf
= NULL
; /*later*/
3645 s
[i
]->s
.k
= IPPROTO_AH
;
3647 s
[fix3
]->s
.jf
= s
[ahcheck
];
3654 * X = X + (P[X + 1] + 2) * 4;
3657 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3659 /* A = P[X + packet head]; */
3660 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3664 s
[i
] = new_stmt(BPF_ST
);
3668 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3671 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3675 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3677 /* A = P[X + packet head] */
3678 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3682 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3686 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3690 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3693 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3697 /* goto again; (must use BPF_JA for backward jump) */
3698 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3699 s
[i
]->s
.k
= again
- i
- 1;
3704 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3706 s
[fix2
]->s
.jt
= s
[end
];
3707 s
[fix4
]->s
.jf
= s
[end
];
3708 s
[fix5
]->s
.jt
= s
[end
];
3715 for (i
= 0; i
< max
- 1; i
++)
3716 s
[i
]->next
= s
[i
+ 1];
3717 s
[max
- 1]->next
= NULL
;
3722 b
= new_block(JMP(BPF_JEQ
));
3723 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3733 static struct block
*
3734 gen_proto(v
, proto
, dir
)
3739 struct block
*b0
, *b1
;
3741 if (dir
!= Q_DEFAULT
)
3742 bpf_error("direction applied to 'proto'");
3747 b0
= gen_proto(v
, Q_IP
, dir
);
3748 b1
= gen_proto(v
, Q_IPV6
, dir
);
3756 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3757 * not LLC encapsulation with LLCSAP_IP.
3759 * For IEEE 802 networks - which includes 802.5 token ring
3760 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3761 * says that SNAP encapsulation is used, not LLC encapsulation
3764 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3765 * RFC 2225 say that SNAP encapsulation is used, not LLC
3766 * encapsulation with LLCSAP_IP.
3768 * So we always check for ETHERTYPE_IP.
3770 b0
= gen_linktype(ETHERTYPE_IP
);
3772 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3774 b1
= gen_protochain(v
, Q_IP
);
3784 * Frame Relay packets typically have an OSI
3785 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3786 * generates code to check for all the OSI
3787 * NLPIDs, so calling it and then adding a check
3788 * for the particular NLPID for which we're
3789 * looking is bogus, as we can just check for
3792 * What we check for is the NLPID and a frame
3793 * control field value of UI, i.e. 0x03 followed
3796 * XXX - assumes a 2-byte Frame Relay header with
3797 * DLCI and flags. What if the address is longer?
3799 * XXX - what about SNAP-encapsulated frames?
3801 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3807 * Cisco uses an Ethertype lookalike - for OSI,
3810 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3811 /* OSI in C-HDLC is stuffed with a fudge byte */
3812 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3817 b0
= gen_linktype(LLCSAP_ISONS
);
3818 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3824 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3826 * 4 is the offset of the PDU type relative to the IS-IS
3829 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3834 bpf_error("arp does not encapsulate another protocol");
3838 bpf_error("rarp does not encapsulate another protocol");
3842 bpf_error("atalk encapsulation is not specifiable");
3846 bpf_error("decnet encapsulation is not specifiable");
3850 bpf_error("sca does not encapsulate another protocol");
3854 bpf_error("lat does not encapsulate another protocol");
3858 bpf_error("moprc does not encapsulate another protocol");
3862 bpf_error("mopdl does not encapsulate another protocol");
3866 return gen_linktype(v
);
3869 bpf_error("'udp proto' is bogus");
3873 bpf_error("'tcp proto' is bogus");
3877 bpf_error("'sctp proto' is bogus");
3881 bpf_error("'icmp proto' is bogus");
3885 bpf_error("'igmp proto' is bogus");
3889 bpf_error("'igrp proto' is bogus");
3893 bpf_error("'pim proto' is bogus");
3897 bpf_error("'vrrp proto' is bogus");
3902 b0
= gen_linktype(ETHERTYPE_IPV6
);
3904 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3906 b1
= gen_protochain(v
, Q_IPV6
);
3912 bpf_error("'icmp6 proto' is bogus");
3916 bpf_error("'ah proto' is bogus");
3919 bpf_error("'ah proto' is bogus");
3922 bpf_error("'stp proto' is bogus");
3925 bpf_error("'ipx proto' is bogus");
3928 bpf_error("'netbeui proto' is bogus");
3939 register const char *name
;
3942 int proto
= q
.proto
;
3946 bpf_u_int32 mask
, addr
;
3948 bpf_u_int32
**alist
;
3951 struct sockaddr_in
*sin
;
3952 struct sockaddr_in6
*sin6
;
3953 struct addrinfo
*res
, *res0
;
3954 struct in6_addr mask128
;
3956 struct block
*b
, *tmp
;
3957 int port
, real_proto
;
3962 addr
= pcap_nametonetaddr(name
);
3964 bpf_error("unknown network '%s'", name
);
3965 /* Left justify network addr and calculate its network mask */
3967 while (addr
&& (addr
& 0xff000000) == 0) {
3971 return gen_host(addr
, mask
, proto
, dir
);
3975 if (proto
== Q_LINK
) {
3979 eaddr
= pcap_ether_hostton(name
);
3982 "unknown ether host '%s'", name
);
3983 b
= gen_ehostop(eaddr
, dir
);
3988 eaddr
= pcap_ether_hostton(name
);
3991 "unknown FDDI host '%s'", name
);
3992 b
= gen_fhostop(eaddr
, dir
);
3997 eaddr
= pcap_ether_hostton(name
);
4000 "unknown token ring host '%s'", name
);
4001 b
= gen_thostop(eaddr
, dir
);
4005 case DLT_IEEE802_11
:
4006 eaddr
= pcap_ether_hostton(name
);
4009 "unknown 802.11 host '%s'", name
);
4010 b
= gen_wlanhostop(eaddr
, dir
);
4014 case DLT_IP_OVER_FC
:
4015 eaddr
= pcap_ether_hostton(name
);
4018 "unknown Fibre Channel host '%s'", name
);
4019 b
= gen_ipfchostop(eaddr
, dir
);
4028 * Check that the packet doesn't begin
4029 * with an LE Control marker. (We've
4030 * already generated a test for LANE.)
4032 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4036 eaddr
= pcap_ether_hostton(name
);
4039 "unknown ether host '%s'", name
);
4040 b
= gen_ehostop(eaddr
, dir
);
4046 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4047 } else if (proto
== Q_DECNET
) {
4048 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4050 * I don't think DECNET hosts can be multihomed, so
4051 * there is no need to build up a list of addresses
4053 return (gen_host(dn_addr
, 0, proto
, dir
));
4056 alist
= pcap_nametoaddr(name
);
4057 if (alist
== NULL
|| *alist
== NULL
)
4058 bpf_error("unknown host '%s'", name
);
4060 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4062 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4064 tmp
= gen_host(**alist
++, 0xffffffff,
4071 memset(&mask128
, 0xff, sizeof(mask128
));
4072 res0
= res
= pcap_nametoaddrinfo(name
);
4074 bpf_error("unknown host '%s'", name
);
4076 tproto
= tproto6
= proto
;
4077 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4081 for (res
= res0
; res
; res
= res
->ai_next
) {
4082 switch (res
->ai_family
) {
4084 if (tproto
== Q_IPV6
)
4087 sin
= (struct sockaddr_in
*)
4089 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4090 0xffffffff, tproto
, dir
);
4093 if (tproto6
== Q_IP
)
4096 sin6
= (struct sockaddr_in6
*)
4098 tmp
= gen_host6(&sin6
->sin6_addr
,
4099 &mask128
, tproto6
, dir
);
4110 bpf_error("unknown host '%s'%s", name
,
4111 (proto
== Q_DEFAULT
)
4113 : " for specified address family");
4120 if (proto
!= Q_DEFAULT
&&
4121 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4122 bpf_error("illegal qualifier of 'port'");
4123 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4124 bpf_error("unknown port '%s'", name
);
4125 if (proto
== Q_UDP
) {
4126 if (real_proto
== IPPROTO_TCP
)
4127 bpf_error("port '%s' is tcp", name
);
4128 else if (real_proto
== IPPROTO_SCTP
)
4129 bpf_error("port '%s' is sctp", name
);
4131 /* override PROTO_UNDEF */
4132 real_proto
= IPPROTO_UDP
;
4134 if (proto
== Q_TCP
) {
4135 if (real_proto
== IPPROTO_UDP
)
4136 bpf_error("port '%s' is udp", name
);
4138 else if (real_proto
== IPPROTO_SCTP
)
4139 bpf_error("port '%s' is sctp", name
);
4141 /* override PROTO_UNDEF */
4142 real_proto
= IPPROTO_TCP
;
4144 if (proto
== Q_SCTP
) {
4145 if (real_proto
== IPPROTO_UDP
)
4146 bpf_error("port '%s' is udp", name
);
4148 else if (real_proto
== IPPROTO_TCP
)
4149 bpf_error("port '%s' is tcp", name
);
4151 /* override PROTO_UNDEF */
4152 real_proto
= IPPROTO_SCTP
;
4155 return gen_port(port
, real_proto
, dir
);
4159 b
= gen_port(port
, real_proto
, dir
);
4160 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4167 eaddr
= pcap_ether_hostton(name
);
4169 bpf_error("unknown ether host: %s", name
);
4171 alist
= pcap_nametoaddr(name
);
4172 if (alist
== NULL
|| *alist
== NULL
)
4173 bpf_error("unknown host '%s'", name
);
4174 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4178 bpf_error("'gateway' not supported in this configuration");
4182 real_proto
= lookup_proto(name
, proto
);
4183 if (real_proto
>= 0)
4184 return gen_proto(real_proto
, proto
, dir
);
4186 bpf_error("unknown protocol: %s", name
);
4189 real_proto
= lookup_proto(name
, proto
);
4190 if (real_proto
>= 0)
4191 return gen_protochain(real_proto
, proto
, dir
);
4193 bpf_error("unknown protocol: %s", name
);
4205 gen_mcode(s1
, s2
, masklen
, q
)
4206 register const char *s1
, *s2
;
4207 register int masklen
;
4210 register int nlen
, mlen
;
4213 nlen
= __pcap_atoin(s1
, &n
);
4214 /* Promote short ipaddr */
4218 mlen
= __pcap_atoin(s2
, &m
);
4219 /* Promote short ipaddr */
4222 bpf_error("non-network bits set in \"%s mask %s\"",
4225 /* Convert mask len to mask */
4227 bpf_error("mask length must be <= 32");
4228 m
= 0xffffffff << (32 - masklen
);
4230 bpf_error("non-network bits set in \"%s/%d\"",
4237 return gen_host(n
, m
, q
.proto
, q
.dir
);
4240 bpf_error("Mask syntax for networks only");
4248 register const char *s
;
4253 int proto
= q
.proto
;
4259 else if (q
.proto
== Q_DECNET
)
4260 vlen
= __pcap_atodn(s
, &v
);
4262 vlen
= __pcap_atoin(s
, &v
);
4269 if (proto
== Q_DECNET
)
4270 return gen_host(v
, 0, proto
, dir
);
4271 else if (proto
== Q_LINK
) {
4272 bpf_error("illegal link layer address");
4275 if (s
== NULL
&& q
.addr
== Q_NET
) {
4276 /* Promote short net number */
4277 while (v
&& (v
& 0xff000000) == 0) {
4282 /* Promote short ipaddr */
4286 return gen_host(v
, mask
, proto
, dir
);
4291 proto
= IPPROTO_UDP
;
4292 else if (proto
== Q_TCP
)
4293 proto
= IPPROTO_TCP
;
4294 else if (proto
== Q_SCTP
)
4295 proto
= IPPROTO_SCTP
;
4296 else if (proto
== Q_DEFAULT
)
4297 proto
= PROTO_UNDEF
;
4299 bpf_error("illegal qualifier of 'port'");
4302 return gen_port((int)v
, proto
, dir
);
4306 b
= gen_port((int)v
, proto
, dir
);
4307 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4313 bpf_error("'gateway' requires a name");
4317 return gen_proto((int)v
, proto
, dir
);
4320 return gen_protochain((int)v
, proto
, dir
);
4335 gen_mcode6(s1
, s2
, masklen
, q
)
4336 register const char *s1
, *s2
;
4337 register int masklen
;
4340 struct addrinfo
*res
;
4341 struct in6_addr
*addr
;
4342 struct in6_addr mask
;
4347 bpf_error("no mask %s supported", s2
);
4349 res
= pcap_nametoaddrinfo(s1
);
4351 bpf_error("invalid ip6 address %s", s1
);
4353 bpf_error("%s resolved to multiple address", s1
);
4354 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4356 if (sizeof(mask
) * 8 < masklen
)
4357 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4358 memset(&mask
, 0, sizeof(mask
));
4359 memset(&mask
, 0xff, masklen
/ 8);
4361 mask
.s6_addr
[masklen
/ 8] =
4362 (0xff << (8 - masklen
% 8)) & 0xff;
4365 a
= (u_int32_t
*)addr
;
4366 m
= (u_int32_t
*)&mask
;
4367 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4368 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4369 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4377 bpf_error("Mask syntax for networks only");
4381 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4386 bpf_error("invalid qualifier against IPv6 address");
4394 register const u_char
*eaddr
;
4397 struct block
*b
, *tmp
;
4399 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4400 if (linktype
== DLT_EN10MB
)
4401 return gen_ehostop(eaddr
, (int)q
.dir
);
4402 if (linktype
== DLT_FDDI
)
4403 return gen_fhostop(eaddr
, (int)q
.dir
);
4404 if (linktype
== DLT_IEEE802
)
4405 return gen_thostop(eaddr
, (int)q
.dir
);
4406 if (linktype
== DLT_IEEE802_11
)
4407 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4408 if (linktype
== DLT_SUNATM
&& is_lane
) {
4410 * Check that the packet doesn't begin with an
4411 * LE Control marker. (We've already generated
4414 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4418 * Now check the MAC address.
4420 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4424 if (linktype
== DLT_IP_OVER_FC
)
4425 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4426 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4428 bpf_error("ethernet address used in non-ether expression");
4434 struct slist
*s0
, *s1
;
4437 * This is definitely not the best way to do this, but the
4438 * lists will rarely get long.
4445 static struct slist
*
4451 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4456 static struct slist
*
4462 s
= new_stmt(BPF_LD
|BPF_MEM
);
4468 gen_load(proto
, index
, size
)
4473 struct slist
*s
, *tmp
;
4475 int regno
= alloc_reg();
4477 free_reg(index
->regno
);
4481 bpf_error("data size must be 1, 2, or 4");
4497 bpf_error("unsupported index operation");
4501 * XXX - what about ATM LANE? Should the index be
4502 * relative to the beginning of the AAL5 frame, so
4503 * that 0 refers to the beginning of the LE Control
4504 * field, or relative to the beginning of the LAN
4505 * frame, so that 0 refers, for Ethernet LANE, to
4506 * the beginning of the destination address?
4508 s
= xfer_to_x(index
);
4509 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4511 sappend(index
->s
, s
);
4526 /* XXX Note that we assume a fixed link header here. */
4527 s
= xfer_to_x(index
);
4528 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4531 sappend(index
->s
, s
);
4533 b
= gen_proto_abbrev(proto
);
4535 gen_and(index
->b
, b
);
4547 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4549 sappend(s
, xfer_to_a(index
));
4550 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4551 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4552 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4554 sappend(index
->s
, s
);
4556 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4558 gen_and(index
->b
, b
);
4560 gen_and(gen_proto_abbrev(Q_IP
), b
);
4566 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4570 index
->regno
= regno
;
4571 s
= new_stmt(BPF_ST
);
4573 sappend(index
->s
, s
);
4579 gen_relation(code
, a0
, a1
, reversed
)
4581 struct arth
*a0
, *a1
;
4584 struct slist
*s0
, *s1
, *s2
;
4585 struct block
*b
, *tmp
;
4589 if (code
== BPF_JEQ
) {
4590 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4591 b
= new_block(JMP(code
));
4595 b
= new_block(BPF_JMP
|code
|BPF_X
);
4601 sappend(a0
->s
, a1
->s
);
4605 free_reg(a0
->regno
);
4606 free_reg(a1
->regno
);
4608 /* 'and' together protocol checks */
4611 gen_and(a0
->b
, tmp
= a1
->b
);
4627 int regno
= alloc_reg();
4628 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4631 s
= new_stmt(BPF_LD
|BPF_LEN
);
4632 s
->next
= new_stmt(BPF_ST
);
4633 s
->next
->s
.k
= regno
;
4648 a
= (struct arth
*)newchunk(sizeof(*a
));
4652 s
= new_stmt(BPF_LD
|BPF_IMM
);
4654 s
->next
= new_stmt(BPF_ST
);
4670 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4673 s
= new_stmt(BPF_ST
);
4681 gen_arth(code
, a0
, a1
)
4683 struct arth
*a0
, *a1
;
4685 struct slist
*s0
, *s1
, *s2
;
4689 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4694 sappend(a0
->s
, a1
->s
);
4696 free_reg(a0
->regno
);
4697 free_reg(a1
->regno
);
4699 s0
= new_stmt(BPF_ST
);
4700 a0
->regno
= s0
->s
.k
= alloc_reg();
4707 * Here we handle simple allocation of the scratch registers.
4708 * If too many registers are alloc'd, the allocator punts.
4710 static int regused
[BPF_MEMWORDS
];
4714 * Return the next free register.
4719 int n
= BPF_MEMWORDS
;
4722 if (regused
[curreg
])
4723 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4725 regused
[curreg
] = 1;
4729 bpf_error("too many registers needed to evaluate expression");
4734 * Return a register to the table so it can
4744 static struct block
*
4751 s
= new_stmt(BPF_LD
|BPF_LEN
);
4752 b
= new_block(JMP(jmp
));
4763 return gen_len(BPF_JGE
, n
);
4767 * Actually, this is less than or equal.
4775 b
= gen_len(BPF_JGT
, n
);
4782 gen_byteop(op
, idx
, val
)
4793 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4796 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4797 b
->s
.code
= JMP(BPF_JGE
);
4802 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4803 b
->s
.code
= JMP(BPF_JGT
);
4807 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4811 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4815 b
= new_block(JMP(BPF_JEQ
));
4822 static u_char abroadcast
[] = { 0x0 };
4825 gen_broadcast(proto
)
4828 bpf_u_int32 hostmask
;
4829 struct block
*b0
, *b1
, *b2
;
4830 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4836 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4837 return gen_ahostop(abroadcast
, Q_DST
);
4838 if (linktype
== DLT_EN10MB
)
4839 return gen_ehostop(ebroadcast
, Q_DST
);
4840 if (linktype
== DLT_FDDI
)
4841 return gen_fhostop(ebroadcast
, Q_DST
);
4842 if (linktype
== DLT_IEEE802
)
4843 return gen_thostop(ebroadcast
, Q_DST
);
4844 if (linktype
== DLT_IEEE802_11
)
4845 return gen_wlanhostop(ebroadcast
, Q_DST
);
4846 if (linktype
== DLT_IP_OVER_FC
)
4847 return gen_ipfchostop(ebroadcast
, Q_DST
);
4848 if (linktype
== DLT_SUNATM
&& is_lane
) {
4850 * Check that the packet doesn't begin with an
4851 * LE Control marker. (We've already generated
4854 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4858 * Now check the MAC address.
4860 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4864 bpf_error("not a broadcast link");
4868 b0
= gen_linktype(ETHERTYPE_IP
);
4869 hostmask
= ~netmask
;
4870 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4871 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4872 (bpf_int32
)(~0 & hostmask
), hostmask
);
4877 bpf_error("only link-layer/IP broadcast filters supported");
4882 * Generate code to test the low-order bit of a MAC address (that's
4883 * the bottom bit of the *first* byte).
4885 static struct block
*
4886 gen_mac_multicast(offset
)
4889 register struct block
*b0
;
4890 register struct slist
*s
;
4892 /* link[offset] & 1 != 0 */
4893 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4895 b0
= new_block(JMP(BPF_JSET
));
4902 gen_multicast(proto
)
4905 register struct block
*b0
, *b1
, *b2
;
4906 register struct slist
*s
;
4912 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4913 /* all ARCnet multicasts use the same address */
4914 return gen_ahostop(abroadcast
, Q_DST
);
4916 if (linktype
== DLT_EN10MB
) {
4917 /* ether[0] & 1 != 0 */
4918 return gen_mac_multicast(0);
4921 if (linktype
== DLT_FDDI
) {
4923 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4925 * XXX - was that referring to bit-order issues?
4927 /* fddi[1] & 1 != 0 */
4928 return gen_mac_multicast(1);
4931 if (linktype
== DLT_IEEE802
) {
4932 /* tr[2] & 1 != 0 */
4933 return gen_mac_multicast(2);
4936 if (linktype
== DLT_IEEE802_11
) {
4940 * For control frames, there is no DA.
4942 * For management frames, DA is at an
4943 * offset of 4 from the beginning of
4946 * For data frames, DA is at an offset
4947 * of 4 from the beginning of the packet
4948 * if To DS is clear and at an offset of
4949 * 16 from the beginning of the packet
4954 * Generate the tests to be done for data frames.
4956 * First, check for To DS set, i.e. "link[1] & 0x01".
4958 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4960 b1
= new_block(JMP(BPF_JSET
));
4961 b1
->s
.k
= 0x01; /* To DS */
4965 * If To DS is set, the DA is at 16.
4967 b0
= gen_mac_multicast(16);
4971 * Now, check for To DS not set, i.e. check
4972 * "!(link[1] & 0x01)".
4974 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4976 b2
= new_block(JMP(BPF_JSET
));
4977 b2
->s
.k
= 0x01; /* To DS */
4982 * If To DS is not set, the DA is at 4.
4984 b1
= gen_mac_multicast(4);
4988 * Now OR together the last two checks. That gives
4989 * the complete set of checks for data frames.
4994 * Now check for a data frame.
4995 * I.e, check "link[0] & 0x08".
4997 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4999 b1
= new_block(JMP(BPF_JSET
));
5004 * AND that with the checks done for data frames.
5009 * If the high-order bit of the type value is 0, this
5010 * is a management frame.
5011 * I.e, check "!(link[0] & 0x08)".
5013 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5015 b2
= new_block(JMP(BPF_JSET
));
5021 * For management frames, the DA is at 4.
5023 b1
= gen_mac_multicast(4);
5027 * OR that with the checks done for data frames.
5028 * That gives the checks done for management and
5034 * If the low-order bit of the type value is 1,
5035 * this is either a control frame or a frame
5036 * with a reserved type, and thus not a
5039 * I.e., check "!(link[0] & 0x04)".
5041 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5043 b1
= new_block(JMP(BPF_JSET
));
5049 * AND that with the checks for data and management
5056 if (linktype
== DLT_IP_OVER_FC
) {
5057 b0
= gen_mac_multicast(2);
5061 if (linktype
== DLT_SUNATM
&& is_lane
) {
5063 * Check that the packet doesn't begin with an
5064 * LE Control marker. (We've already generated
5067 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5070 /* ether[off_mac] & 1 != 0 */
5071 b0
= gen_mac_multicast(off_mac
);
5076 /* Link not known to support multicasts */
5080 b0
= gen_linktype(ETHERTYPE_IP
);
5081 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5082 b1
->s
.code
= JMP(BPF_JGE
);
5088 b0
= gen_linktype(ETHERTYPE_IPV6
);
5089 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5094 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5099 * generate command for inbound/outbound. It's here so we can
5100 * make it link-type specific. 'dir' = 0 implies "inbound",
5101 * = 1 implies "outbound".
5107 register struct block
*b0
;
5110 * Only some data link types support inbound/outbound qualifiers.
5114 b0
= gen_relation(BPF_JEQ
,
5115 gen_load(Q_LINK
, gen_loadi(0), 1),
5123 * Match packets sent by this machine.
5125 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5128 * Match packets sent to this machine.
5129 * (No broadcast or multicast packets, or
5130 * packets sent to some other machine and
5131 * received promiscuously.)
5133 * XXX - packets sent to other machines probably
5134 * shouldn't be matched, but what about broadcast
5135 * or multicast packets we received?
5137 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5142 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5143 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5148 /* match outgoing packets */
5149 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5151 /* match incoming packets */
5152 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5156 case DLT_JUNIPER_MLFR
:
5157 case DLT_JUNIPER_MLPPP
:
5158 case DLT_JUNIPER_ATM1
:
5159 case DLT_JUNIPER_ATM2
:
5160 /* juniper flags (including direction) are stored
5161 * the byte after the 3-byte magic number */
5163 /* match outgoing packets */
5164 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5166 /* match incoming packets */
5167 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5172 bpf_error("inbound/outbound not supported on linktype %d",
5180 /* PF firewall log matched interface */
5182 gen_pf_ifname(const char *ifname
)
5187 if (linktype
== DLT_PFLOG
) {
5188 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5189 off
= offsetof(struct pfloghdr
, ifname
);
5191 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5194 if (strlen(ifname
) >= len
) {
5195 bpf_error("ifname interface names can only be %d characters",
5199 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5203 /* PF firewall log matched interface */
5205 gen_pf_ruleset(char *ruleset
)
5209 if (linktype
!= DLT_PFLOG
) {
5210 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5213 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5214 bpf_error("ruleset names can only be %ld characters",
5215 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5218 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5219 strlen(ruleset
), (const u_char
*)ruleset
);
5223 /* PF firewall log rule number */
5229 if (linktype
== DLT_PFLOG
) {
5230 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5233 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5240 /* PF firewall log sub-rule number */
5242 gen_pf_srnr(int srnr
)
5246 if (linktype
!= DLT_PFLOG
) {
5247 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5251 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5256 /* PF firewall log reason code */
5258 gen_pf_reason(int reason
)
5262 if (linktype
== DLT_PFLOG
) {
5263 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5266 bpf_error("reason not supported on linktype 0x%x", linktype
);
5273 /* PF firewall log action */
5275 gen_pf_action(int action
)
5279 if (linktype
== DLT_PFLOG
) {
5280 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5283 bpf_error("action not supported on linktype 0x%x", linktype
);
5292 register const u_char
*eaddr
;
5295 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5296 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5297 return gen_ahostop(eaddr
, (int)q
.dir
);
5299 bpf_error("ARCnet address used in non-arc expression");
5303 static struct block
*
5304 gen_ahostop(eaddr
, dir
)
5305 register const u_char
*eaddr
;
5308 register struct block
*b0
, *b1
;
5311 /* src comes first, different from Ethernet */
5313 return gen_bcmp(0, 1, eaddr
);
5316 return gen_bcmp(1, 1, eaddr
);
5319 b0
= gen_ahostop(eaddr
, Q_SRC
);
5320 b1
= gen_ahostop(eaddr
, Q_DST
);
5326 b0
= gen_ahostop(eaddr
, Q_SRC
);
5327 b1
= gen_ahostop(eaddr
, Q_DST
);
5336 * support IEEE 802.1Q VLAN trunk over ethernet
5345 * Change the offsets to point to the type and data fields within
5346 * the VLAN packet. Just increment the offsets, so that we
5347 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5348 * capture VLAN 200 encapsulated within VLAN 100.
5350 * XXX - this is a bit of a kludge. If we were to split the
5351 * compiler into a parser that parses an expression and
5352 * generates an expression tree, and a code generator that
5353 * takes an expression tree (which could come from our
5354 * parser or from some other parser) and generates BPF code,
5355 * we could perhaps make the offsets parameters of routines
5356 * and, in the handler for an "AND" node, pass to subnodes
5357 * other than the VLAN node the adjusted offsets.
5359 * This would mean that "vlan" would, instead of changing the
5360 * behavior of *all* tests after it, change only the behavior
5361 * of tests ANDed with it. That would change the documented
5362 * semantics of "vlan", which might break some expressions.
5363 * However, it would mean that "(vlan and ip) or ip" would check
5364 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5365 * checking only for VLAN-encapsulated IP, so that could still
5366 * be considered worth doing; it wouldn't break expressions
5367 * that are of the form "vlan and ..." or "vlan N and ...",
5368 * which I suspect are the most common expressions involving
5369 * "vlan". "vlan or ..." doesn't necessarily do what the user
5370 * would really want, now, as all the "or ..." tests would
5371 * be done assuming a VLAN, even though the "or" could be viewed
5372 * as meaning "or, if this isn't a VLAN packet...".
5374 orig_linktype
= off_linktype
; /* save original values */
5376 orig_nl_nosnap
= off_nl_nosnap
;
5387 bpf_error("no VLAN support for data link type %d",
5392 /* check for VLAN */
5393 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5395 /* If a specific VLAN is requested, check VLAN id */
5396 if (vlan_num
>= 0) {
5399 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5417 * Change the offsets to point to the type and data fields within
5418 * the MPLS packet. Just increment the offsets, so that we
5419 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5420 * capture packets with an outer label of 100000 and an inner
5423 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5425 orig_linktype
= off_linktype
; /* save original values */
5427 orig_nl_nosnap
= off_nl_nosnap
;
5431 case DLT_C_HDLC
: /* fall through */
5437 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5445 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5448 /* FIXME add other DLT_s ...
5449 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5450 * leave it for now */
5453 bpf_error("no MPLS support for data link type %d",
5460 /* If a specific MPLS label is requested, check it */
5461 if (label_num
>= 0) {
5464 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5465 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5474 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5486 bpf_error("'vpi' supported only on raw ATM");
5487 if (off_vpi
== (u_int
)-1)
5489 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5490 (u_int
)jvalue
, reverse
);
5495 bpf_error("'vci' supported only on raw ATM");
5496 if (off_vci
== (u_int
)-1)
5498 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5499 (u_int
)jvalue
, reverse
);
5503 if (off_proto
== (u_int
)-1)
5504 abort(); /* XXX - this isn't on FreeBSD */
5505 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5506 (u_int
)jvalue
, reverse
);
5510 if (off_payload
== (u_int
)-1)
5512 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5513 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5518 bpf_error("'callref' supported only on raw ATM");
5519 if (off_proto
== (u_int
)-1)
5521 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5522 (u_int
)jvalue
, reverse
);
5532 gen_atmtype_abbrev(type
)
5535 struct block
*b0
, *b1
;
5540 /* Get all packets in Meta signalling Circuit */
5542 bpf_error("'metac' supported only on raw ATM");
5543 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5544 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5549 /* Get all packets in Broadcast Circuit*/
5551 bpf_error("'bcc' supported only on raw ATM");
5552 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5553 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5558 /* Get all cells in Segment OAM F4 circuit*/
5560 bpf_error("'oam4sc' supported only on raw ATM");
5561 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5562 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5567 /* Get all cells in End-to-End OAM F4 Circuit*/
5569 bpf_error("'oam4ec' supported only on raw ATM");
5570 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5571 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5576 /* Get all packets in connection Signalling Circuit */
5578 bpf_error("'sc' supported only on raw ATM");
5579 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5580 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5585 /* Get all packets in ILMI Circuit */
5587 bpf_error("'ilmic' supported only on raw ATM");
5588 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5589 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5594 /* Get all LANE packets */
5596 bpf_error("'lane' supported only on raw ATM");
5597 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5600 * Arrange that all subsequent tests assume LANE
5601 * rather than LLC-encapsulated packets, and set
5602 * the offsets appropriately for LANE-encapsulated
5605 * "off_mac" is the offset of the Ethernet header,
5606 * which is 2 bytes past the ATM pseudo-header
5607 * (skipping the pseudo-header and 2-byte LE Client
5608 * field). The other offsets are Ethernet offsets
5609 * relative to "off_mac".
5612 off_mac
= off_payload
+ 2; /* MAC header */
5613 off_linktype
= off_mac
+ 12;
5614 off_nl
= off_mac
+ 14; /* Ethernet II */
5615 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5619 /* Get all LLC-encapsulated packets */
5621 bpf_error("'llc' supported only on raw ATM");
5622 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5633 static struct block
*
5634 gen_msg_abbrev(type
)
5640 * Q.2931 signalling protocol messages for handling virtual circuits
5641 * establishment and teardown
5646 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5650 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5654 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5658 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5662 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5665 case A_RELEASE_DONE
:
5666 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5676 gen_atmmulti_abbrev(type
)
5679 struct block
*b0
, *b1
;
5685 bpf_error("'oam' supported only on raw ATM");
5686 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5691 bpf_error("'oamf4' supported only on raw ATM");
5693 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5694 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5696 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5702 * Get Q.2931 signalling messages for switched
5703 * virtual connection
5706 bpf_error("'connectmsg' supported only on raw ATM");
5707 b0
= gen_msg_abbrev(A_SETUP
);
5708 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5710 b0
= gen_msg_abbrev(A_CONNECT
);
5712 b0
= gen_msg_abbrev(A_CONNECTACK
);
5714 b0
= gen_msg_abbrev(A_RELEASE
);
5716 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5718 b0
= gen_atmtype_abbrev(A_SC
);
5724 bpf_error("'metaconnect' supported only on raw ATM");
5725 b0
= gen_msg_abbrev(A_SETUP
);
5726 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5728 b0
= gen_msg_abbrev(A_CONNECT
);
5730 b0
= gen_msg_abbrev(A_RELEASE
);
5732 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5734 b0
= gen_atmtype_abbrev(A_METAC
);