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.4 2005-04-18 22:40:14 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, orig_nl_nosnap
= -1U;
108 static int pcap_fddipad
;
111 void bpf_error(const char *fmt
, ...) __attribute__((noreturn
));
115 bpf_error(const char *fmt
, ...)
120 if (bpf_pcap
!= NULL
)
121 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
128 static void init_linktype(pcap_t
*);
130 static int alloc_reg(void);
131 static void free_reg(int);
133 static struct block
*root
;
136 * We divy out chunks of memory rather than call malloc each time so
137 * we don't have to worry about leaking memory. It's probably
138 * not a big deal if all this memory was wasted but if this ever
139 * goes into a library that would probably not be a good idea.
141 * XXX - this *is* in a library....
144 #define CHUNK0SIZE 1024
150 static struct chunk chunks
[NCHUNKS
];
151 static int cur_chunk
;
153 static void *newchunk(u_int
);
154 static void freechunks(void);
155 static inline struct block
*new_block(int);
156 static inline struct slist
*new_stmt(int);
157 static struct block
*gen_retblk(int);
158 static inline void syntax(void);
160 static void backpatch(struct block
*, struct block
*);
161 static void merge(struct block
*, struct block
*);
162 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
163 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
164 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
165 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
166 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
167 bpf_u_int32
, bpf_u_int32
, int);
168 static struct block
*gen_uncond(int);
169 static inline struct block
*gen_true(void);
170 static inline struct block
*gen_false(void);
171 static struct block
*gen_ether_linktype(int);
172 static struct block
*gen_linux_sll_linktype(int);
173 static struct block
*gen_linktype(int);
174 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
175 static struct block
*gen_llc_linktype(int);
176 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
178 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
180 static struct block
*gen_ahostop(const u_char
*, int);
181 static struct block
*gen_ehostop(const u_char
*, int);
182 static struct block
*gen_fhostop(const u_char
*, int);
183 static struct block
*gen_thostop(const u_char
*, int);
184 static struct block
*gen_wlanhostop(const u_char
*, int);
185 static struct block
*gen_ipfchostop(const u_char
*, int);
186 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
187 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
189 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
192 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
194 static struct block
*gen_ipfrag(void);
195 static struct block
*gen_portatom(int, bpf_int32
);
197 static struct block
*gen_portatom6(int, bpf_int32
);
199 struct block
*gen_portop(int, int, int);
200 static struct block
*gen_port(int, int, int);
202 struct block
*gen_portop6(int, int, int);
203 static struct block
*gen_port6(int, int, int);
205 static int lookup_proto(const char *, int);
206 static struct block
*gen_protochain(int, int, int);
207 static struct block
*gen_proto(int, int, int);
208 static struct slist
*xfer_to_x(struct arth
*);
209 static struct slist
*xfer_to_a(struct arth
*);
210 static struct block
*gen_mac_multicast(int);
211 static struct block
*gen_len(int, int);
213 static struct block
*gen_msg_abbrev(int type
);
224 /* XXX Round up to nearest long. */
225 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
227 /* XXX Round up to structure boundary. */
231 cp
= &chunks
[cur_chunk
];
232 if (n
> cp
->n_left
) {
233 ++cp
, k
= ++cur_chunk
;
235 bpf_error("out of memory");
236 size
= CHUNK0SIZE
<< k
;
237 cp
->m
= (void *)malloc(size
);
239 bpf_error("out of memory");
240 memset((char *)cp
->m
, 0, size
);
243 bpf_error("out of memory");
246 return (void *)((char *)cp
->m
+ cp
->n_left
);
255 for (i
= 0; i
< NCHUNKS
; ++i
)
256 if (chunks
[i
].m
!= NULL
) {
263 * A strdup whose allocations are freed after code generation is over.
267 register const char *s
;
269 int n
= strlen(s
) + 1;
270 char *cp
= newchunk(n
);
276 static inline struct block
*
282 p
= (struct block
*)newchunk(sizeof(*p
));
289 static inline struct slist
*
295 p
= (struct slist
*)newchunk(sizeof(*p
));
301 static struct block
*
305 struct block
*b
= new_block(BPF_RET
|BPF_K
);
314 bpf_error("syntax error in filter expression");
317 static bpf_u_int32 netmask
;
322 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
323 char *buf
, int optimize
, bpf_u_int32 mask
)
332 if (setjmp(top_ctx
)) {
340 snaplen
= pcap_snapshot(p
);
342 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
343 "snaplen of 0 rejects all packets");
347 lex_init(buf
? buf
: "");
355 root
= gen_retblk(snaplen
);
357 if (optimize
&& !no_optimize
) {
360 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
361 bpf_error("expression rejects all packets");
363 program
->bf_insns
= icode_to_fcode(root
, &len
);
364 program
->bf_len
= len
;
372 * entry point for using the compiler with no pcap open
373 * pass in all the stuff that is needed explicitly instead.
376 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
377 struct bpf_program
*program
,
378 char *buf
, int optimize
, bpf_u_int32 mask
)
383 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
386 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
392 * Clean up a "struct bpf_program" by freeing all the memory allocated
396 pcap_freecode(struct bpf_program
*program
)
399 if (program
->bf_insns
!= NULL
) {
400 free((char *)program
->bf_insns
);
401 program
->bf_insns
= NULL
;
406 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
407 * which of the jt and jf fields has been resolved and which is a pointer
408 * back to another unresolved block (or nil). At least one of the fields
409 * in each block is already resolved.
412 backpatch(list
, target
)
413 struct block
*list
, *target
;
430 * Merge the lists in b0 and b1, using the 'sense' field to indicate
431 * which of jt and jf is the link.
435 struct block
*b0
, *b1
;
437 register struct block
**p
= &b0
;
439 /* Find end of list. */
441 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
443 /* Concatenate the lists. */
451 backpatch(p
, gen_retblk(snaplen
));
452 p
->sense
= !p
->sense
;
453 backpatch(p
, gen_retblk(0));
459 struct block
*b0
, *b1
;
461 backpatch(b0
, b1
->head
);
462 b0
->sense
= !b0
->sense
;
463 b1
->sense
= !b1
->sense
;
465 b1
->sense
= !b1
->sense
;
471 struct block
*b0
, *b1
;
473 b0
->sense
= !b0
->sense
;
474 backpatch(b0
, b1
->head
);
475 b0
->sense
= !b0
->sense
;
484 b
->sense
= !b
->sense
;
487 static struct block
*
488 gen_cmp(offset
, size
, v
)
495 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
498 b
= new_block(JMP(BPF_JEQ
));
505 static struct block
*
506 gen_cmp_gt(offset
, size
, v
)
513 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
516 b
= new_block(JMP(BPF_JGT
));
523 static struct block
*
524 gen_mcmp(offset
, size
, v
, mask
)
529 struct block
*b
= gen_cmp(offset
, size
, v
);
532 if (mask
!= 0xffffffff) {
533 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
540 static struct block
*
541 gen_bcmp(offset
, size
, v
)
542 register u_int offset
, size
;
543 register const u_char
*v
;
545 register struct block
*b
, *tmp
;
549 register const u_char
*p
= &v
[size
- 4];
550 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
551 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
553 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
560 register const u_char
*p
= &v
[size
- 2];
561 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
563 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
570 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
578 static struct block
*
579 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
580 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
586 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
589 if (mask
!= 0xffffffff) {
590 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
594 b
= new_block(JMP(jtype
));
597 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
603 * Various code constructs need to know the layout of the data link
604 * layer. These variables give the necessary offsets.
608 * This is the offset of the beginning of the MAC-layer header.
609 * It's usually 0, except for ATM LANE.
611 static u_int off_mac
;
614 * "off_linktype" is the offset to information in the link-layer header
615 * giving the packet type.
617 * For Ethernet, it's the offset of the Ethernet type field.
619 * For link-layer types that always use 802.2 headers, it's the
620 * offset of the LLC header.
622 * For PPP, it's the offset of the PPP type field.
624 * For Cisco HDLC, it's the offset of the CHDLC type field.
626 * For BSD loopback, it's the offset of the AF_ value.
628 * For Linux cooked sockets, it's the offset of the type field.
630 * It's set to -1 for no encapsulation, in which case, IP is assumed.
632 static u_int off_linktype
;
635 * TRUE if the link layer includes an ATM pseudo-header.
637 static int is_atm
= 0;
640 * TRUE if "lane" appeared in the filter; it causes us to generate
641 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
643 static int is_lane
= 0;
646 * These are offsets for the ATM pseudo-header.
648 static u_int off_vpi
;
649 static u_int off_vci
;
650 static u_int off_proto
;
653 * This is the offset of the first byte after the ATM pseudo_header,
654 * or -1 if there is no ATM pseudo-header.
656 static u_int off_payload
;
659 * These are offsets to the beginning of the network-layer header.
661 * If the link layer never uses 802.2 LLC:
663 * "off_nl" and "off_nl_nosnap" are the same.
665 * If the link layer always uses 802.2 LLC:
667 * "off_nl" is the offset if there's a SNAP header following
670 * "off_nl_nosnap" is the offset if there's no SNAP header.
672 * If the link layer is Ethernet:
674 * "off_nl" is the offset if the packet is an Ethernet II packet
675 * (we assume no 802.3+802.2+SNAP);
677 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
678 * with an 802.2 header following it.
681 static u_int off_nl_nosnap
;
689 linktype
= pcap_datalink(p
);
691 pcap_fddipad
= p
->fddipad
;
695 * Assume it's not raw ATM with a pseudo-header, for now.
713 off_nl
= 6; /* XXX in reality, variable! */
714 off_nl_nosnap
= 6; /* no 802.2 LLC */
717 case DLT_ARCNET_LINUX
:
719 off_nl
= 8; /* XXX in reality, variable! */
720 off_nl_nosnap
= 8; /* no 802.2 LLC */
725 off_nl
= 14; /* Ethernet II */
726 off_nl_nosnap
= 17; /* 802.3+802.2 */
731 * SLIP doesn't have a link level type. The 16 byte
732 * header is hacked into our SLIP driver.
736 off_nl_nosnap
= 16; /* no 802.2 LLC */
740 /* XXX this may be the same as the DLT_PPP_BSDOS case */
744 off_nl_nosnap
= 24; /* no 802.2 LLC */
751 off_nl_nosnap
= 4; /* no 802.2 LLC */
757 off_nl_nosnap
= 12; /* no 802.2 LLC */
762 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
763 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
766 off_nl_nosnap
= 4; /* no 802.2 LLC */
771 * This does no include the Ethernet header, and
772 * only covers session state.
776 off_nl_nosnap
= 8; /* no 802.2 LLC */
782 off_nl_nosnap
= 24; /* no 802.2 LLC */
787 * FDDI doesn't really have a link-level type field.
788 * We set "off_linktype" to the offset of the LLC header.
790 * To check for Ethernet types, we assume that SSAP = SNAP
791 * is being used and pick out the encapsulated Ethernet type.
792 * XXX - should we generate code to check for SNAP?
796 off_linktype
+= pcap_fddipad
;
798 off_nl
= 21; /* FDDI+802.2+SNAP */
799 off_nl_nosnap
= 16; /* FDDI+802.2 */
801 off_nl
+= pcap_fddipad
;
802 off_nl_nosnap
+= pcap_fddipad
;
808 * Token Ring doesn't really have a link-level type field.
809 * We set "off_linktype" to the offset of the LLC header.
811 * To check for Ethernet types, we assume that SSAP = SNAP
812 * is being used and pick out the encapsulated Ethernet type.
813 * XXX - should we generate code to check for SNAP?
815 * XXX - the header is actually variable-length.
816 * Some various Linux patched versions gave 38
817 * as "off_linktype" and 40 as "off_nl"; however,
818 * if a token ring packet has *no* routing
819 * information, i.e. is not source-routed, the correct
820 * values are 20 and 22, as they are in the vanilla code.
822 * A packet is source-routed iff the uppermost bit
823 * of the first byte of the source address, at an
824 * offset of 8, has the uppermost bit set. If the
825 * packet is source-routed, the total number of bytes
826 * of routing information is 2 plus bits 0x1F00 of
827 * the 16-bit value at an offset of 14 (shifted right
828 * 8 - figure out which byte that is).
831 off_nl
= 22; /* Token Ring+802.2+SNAP */
832 off_nl_nosnap
= 17; /* Token Ring+802.2 */
837 * 802.11 doesn't really have a link-level type field.
838 * We set "off_linktype" to the offset of the LLC header.
840 * To check for Ethernet types, we assume that SSAP = SNAP
841 * is being used and pick out the encapsulated Ethernet type.
842 * XXX - should we generate code to check for SNAP?
844 * XXX - the header is actually variable-length. We
845 * assume a 24-byte link-layer header, as appears in
846 * data frames in networks with no bridges. If the
847 * fromds and tods 802.11 header bits are both set,
848 * it's actually supposed to be 30 bytes.
851 off_nl
= 32; /* 802.11+802.2+SNAP */
852 off_nl_nosnap
= 27; /* 802.11+802.2 */
855 case DLT_PRISM_HEADER
:
857 * Same as 802.11, but with an additional header before
858 * the 802.11 header, containing a bunch of additional
859 * information including radio-level information.
861 * The header is 144 bytes long.
863 * XXX - same variable-length header problem; at least
864 * the Prism header is fixed-length.
866 off_linktype
= 144+24;
867 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
868 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
871 case DLT_IEEE802_11_RADIO_AVS
:
873 * Same as 802.11, but with an additional header before
874 * the 802.11 header, containing a bunch of additional
875 * information including radio-level information.
877 * The header is 64 bytes long, at least in its
878 * current incarnation.
880 * XXX - same variable-length header problem, only
881 * more so; this header is also variable-length,
882 * with the length being the 32-bit big-endian
883 * number at an offset of 4 from the beginning
884 * of the radio header.
886 off_linktype
= 64+24;
887 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
888 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
891 case DLT_IEEE802_11_RADIO
:
893 * Same as 802.11, but with an additional header before
894 * the 802.11 header, containing a bunch of additional
895 * information including radio-level information.
897 * XXX - same variable-length header problem, only
898 * even *more* so; this header is also variable-length,
899 * with the length being the 16-bit number at an offset
900 * of 2 from the beginning of the radio header, and it's
901 * device-dependent (different devices might supply
902 * different amounts of information), so we can't even
903 * assume a fixed length for the current version of the
906 * Therefore, currently, only raw "link[N:M]" filtering is
914 case DLT_ATM_RFC1483
:
915 case DLT_ATM_CLIP
: /* Linux ATM defines this */
917 * assume routed, non-ISO PDUs
918 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
921 off_nl
= 8; /* 802.2+SNAP */
922 off_nl_nosnap
= 3; /* 802.2 */
927 * Full Frontal ATM; you get AALn PDUs with an ATM
931 off_vpi
= SUNATM_VPI_POS
;
932 off_vci
= SUNATM_VCI_POS
;
933 off_proto
= PROTO_POS
;
934 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
935 off_payload
= SUNATM_PKT_BEGIN_POS
;
936 off_linktype
= off_payload
;
937 off_nl
= off_payload
+8; /* 802.2+SNAP */
938 off_nl_nosnap
= off_payload
+3; /* 802.2 */
944 off_nl_nosnap
= 0; /* no 802.2 LLC */
947 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
950 off_nl_nosnap
= 16; /* no 802.2 LLC */
955 * LocalTalk does have a 1-byte type field in the LLAP header,
956 * but really it just indicates whether there is a "short" or
957 * "long" DDP packet following.
961 off_nl_nosnap
= 0; /* no 802.2 LLC */
966 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
967 * link-level type field. We set "off_linktype" to the
968 * offset of the LLC header.
970 * To check for Ethernet types, we assume that SSAP = SNAP
971 * is being used and pick out the encapsulated Ethernet type.
972 * XXX - should we generate code to check for SNAP? RFC
973 * 2625 says SNAP should be used.
976 off_nl
= 24; /* IPFC+802.2+SNAP */
977 off_nl_nosnap
= 19; /* IPFC+802.2 */
982 * XXX - we should set this to handle SNAP-encapsulated
983 * frames (NLPID of 0x80).
987 off_nl_nosnap
= 0; /* no 802.2 LLC */
990 case DLT_APPLE_IP_OVER_IEEE1394
:
993 off_nl_nosnap
= 18; /* no 802.2 LLC */
998 * Currently, only raw "link[N:M]" filtering is supported.
1007 * Currently, only raw "link[N:M]" filtering is supported.
1014 case DLT_SYMANTEC_FIREWALL
:
1016 off_nl
= 44; /* Ethernet II */
1017 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1022 /* XXX read this from pf.h? */
1023 off_nl
= PFLOG_HDRLEN
;
1024 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1027 case DLT_JUNIPER_MLFR
:
1028 case DLT_JUNIPER_MLPPP
:
1031 off_nl_nosnap
= -1; /* no 802.2 LLC */
1034 case DLT_JUNIPER_ATM1
:
1035 off_linktype
= 4; /* in reality variable between 4-8 */
1040 case DLT_JUNIPER_ATM2
:
1041 off_linktype
= 8; /* in reality variable between 8-12 */
1054 case DLT_LINUX_LAPD
:
1056 * Currently, only raw "link[N:M]" filtering is supported.
1063 bpf_error("unknown data link type %d", linktype
);
1067 static struct block
*
1074 s
= new_stmt(BPF_LD
|BPF_IMM
);
1076 b
= new_block(JMP(BPF_JEQ
));
1082 static inline struct block
*
1085 return gen_uncond(1);
1088 static inline struct block
*
1091 return gen_uncond(0);
1095 * Byte-swap a 32-bit number.
1096 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1097 * big-endian platforms.)
1099 #define SWAPLONG(y) \
1100 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1103 * Generate code to match a particular packet type.
1105 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1106 * value, if <= ETHERMTU. We use that to determine whether to
1107 * match the type/length field or to check the type/length field for
1108 * a value <= ETHERMTU to see whether it's a type field and then do
1109 * the appropriate test.
1111 static struct block
*
1112 gen_ether_linktype(proto
)
1115 struct block
*b0
, *b1
;
1121 case LLCSAP_NETBEUI
:
1123 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1124 * so we check the DSAP and SSAP.
1126 * LLCSAP_IP checks for IP-over-802.2, rather
1127 * than IP-over-Ethernet or IP-over-SNAP.
1129 * XXX - should we check both the DSAP and the
1130 * SSAP, like this, or should we check just the
1131 * DSAP, as we do for other types <= ETHERMTU
1132 * (i.e., other SAP values)?
1134 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1136 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1137 ((proto
<< 8) | proto
));
1145 * Ethernet_II frames, which are Ethernet
1146 * frames with a frame type of ETHERTYPE_IPX;
1148 * Ethernet_802.3 frames, which are 802.3
1149 * frames (i.e., the type/length field is
1150 * a length field, <= ETHERMTU, rather than
1151 * a type field) with the first two bytes
1152 * after the Ethernet/802.3 header being
1155 * Ethernet_802.2 frames, which are 802.3
1156 * frames with an 802.2 LLC header and
1157 * with the IPX LSAP as the DSAP in the LLC
1160 * Ethernet_SNAP frames, which are 802.3
1161 * frames with an LLC header and a SNAP
1162 * header and with an OUI of 0x000000
1163 * (encapsulated Ethernet) and a protocol
1164 * ID of ETHERTYPE_IPX in the SNAP header.
1166 * XXX - should we generate the same code both
1167 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1171 * This generates code to check both for the
1172 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1174 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1175 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1179 * Now we add code to check for SNAP frames with
1180 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1182 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1186 * Now we generate code to check for 802.3
1187 * frames in general.
1189 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1193 * Now add the check for 802.3 frames before the
1194 * check for Ethernet_802.2 and Ethernet_802.3,
1195 * as those checks should only be done on 802.3
1196 * frames, not on Ethernet frames.
1201 * Now add the check for Ethernet_II frames, and
1202 * do that before checking for the other frame
1205 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1209 case ETHERTYPE_ATALK
:
1210 case ETHERTYPE_AARP
:
1212 * EtherTalk (AppleTalk protocols on Ethernet link
1213 * layer) may use 802.2 encapsulation.
1217 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1218 * we check for an Ethernet type field less than
1219 * 1500, which means it's an 802.3 length field.
1221 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1225 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1226 * SNAP packets with an organization code of
1227 * 0x080007 (Apple, for Appletalk) and a protocol
1228 * type of ETHERTYPE_ATALK (Appletalk).
1230 * 802.2-encapsulated ETHERTYPE_AARP packets are
1231 * SNAP packets with an organization code of
1232 * 0x000000 (encapsulated Ethernet) and a protocol
1233 * type of ETHERTYPE_AARP (Appletalk ARP).
1235 if (proto
== ETHERTYPE_ATALK
)
1236 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1237 else /* proto == ETHERTYPE_AARP */
1238 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1242 * Check for Ethernet encapsulation (Ethertalk
1243 * phase 1?); we just check for the Ethernet
1246 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1252 if (proto
<= ETHERMTU
) {
1254 * This is an LLC SAP value, so the frames
1255 * that match would be 802.2 frames.
1256 * Check that the frame is an 802.2 frame
1257 * (i.e., that the length/type field is
1258 * a length field, <= ETHERMTU) and
1259 * then check the DSAP.
1261 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1263 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1268 * This is an Ethernet type, so compare
1269 * the length/type field with it (if
1270 * the frame is an 802.2 frame, the length
1271 * field will be <= ETHERMTU, and, as
1272 * "proto" is > ETHERMTU, this test
1273 * will fail and the frame won't match,
1274 * which is what we want).
1276 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1282 * Generate code to match a particular packet type.
1284 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1285 * value, if <= ETHERMTU. We use that to determine whether to
1286 * match the type field or to check the type field for the special
1287 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1289 static struct block
*
1290 gen_linux_sll_linktype(proto
)
1293 struct block
*b0
, *b1
;
1299 case LLCSAP_NETBEUI
:
1301 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1302 * so we check the DSAP and SSAP.
1304 * LLCSAP_IP checks for IP-over-802.2, rather
1305 * than IP-over-Ethernet or IP-over-SNAP.
1307 * XXX - should we check both the DSAP and the
1308 * SSAP, like this, or should we check just the
1309 * DSAP, as we do for other types <= ETHERMTU
1310 * (i.e., other SAP values)?
1312 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1313 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1314 ((proto
<< 8) | proto
));
1320 * Ethernet_II frames, which are Ethernet
1321 * frames with a frame type of ETHERTYPE_IPX;
1323 * Ethernet_802.3 frames, which have a frame
1324 * type of LINUX_SLL_P_802_3;
1326 * Ethernet_802.2 frames, which are 802.3
1327 * frames with an 802.2 LLC header (i.e, have
1328 * a frame type of LINUX_SLL_P_802_2) and
1329 * with the IPX LSAP as the DSAP in the LLC
1332 * Ethernet_SNAP frames, which are 802.3
1333 * frames with an LLC header and a SNAP
1334 * header and with an OUI of 0x000000
1335 * (encapsulated Ethernet) and a protocol
1336 * ID of ETHERTYPE_IPX in the SNAP header.
1338 * First, do the checks on LINUX_SLL_P_802_2
1339 * frames; generate the check for either
1340 * Ethernet_802.2 or Ethernet_SNAP frames, and
1341 * then put a check for LINUX_SLL_P_802_2 frames
1344 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1345 (bpf_int32
)LLCSAP_IPX
);
1346 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1349 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1353 * Now check for 802.3 frames and OR that with
1354 * the previous test.
1356 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1360 * Now add the check for Ethernet_II frames, and
1361 * do that before checking for the other frame
1364 b0
= gen_cmp(off_linktype
, BPF_H
,
1365 (bpf_int32
)ETHERTYPE_IPX
);
1369 case ETHERTYPE_ATALK
:
1370 case ETHERTYPE_AARP
:
1372 * EtherTalk (AppleTalk protocols on Ethernet link
1373 * layer) may use 802.2 encapsulation.
1377 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1378 * we check for the 802.2 protocol type in the
1379 * "Ethernet type" field.
1381 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1384 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1385 * SNAP packets with an organization code of
1386 * 0x080007 (Apple, for Appletalk) and a protocol
1387 * type of ETHERTYPE_ATALK (Appletalk).
1389 * 802.2-encapsulated ETHERTYPE_AARP packets are
1390 * SNAP packets with an organization code of
1391 * 0x000000 (encapsulated Ethernet) and a protocol
1392 * type of ETHERTYPE_AARP (Appletalk ARP).
1394 if (proto
== ETHERTYPE_ATALK
)
1395 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1397 else /* proto == ETHERTYPE_AARP */
1398 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1403 * Check for Ethernet encapsulation (Ethertalk
1404 * phase 1?); we just check for the Ethernet
1407 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1413 if (proto
<= ETHERMTU
) {
1415 * This is an LLC SAP value, so the frames
1416 * that match would be 802.2 frames.
1417 * Check for the 802.2 protocol type
1418 * in the "Ethernet type" field, and
1419 * then check the DSAP.
1421 b0
= gen_cmp(off_linktype
, BPF_H
,
1423 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1429 * This is an Ethernet type, so compare
1430 * the length/type field with it (if
1431 * the frame is an 802.2 frame, the length
1432 * field will be <= ETHERMTU, and, as
1433 * "proto" is > ETHERMTU, this test
1434 * will fail and the frame won't match,
1435 * which is what we want).
1437 return gen_cmp(off_linktype
, BPF_H
,
1444 * Generate code to match a particular packet type by matching the
1445 * link-layer type field or fields in the 802.2 LLC header.
1447 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1448 * value, if <= ETHERMTU.
1450 static struct block
*
1454 struct block
*b0
, *b1
, *b2
;
1459 return gen_ether_linktype(proto
);
1467 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1471 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1477 case DLT_IEEE802_11
:
1478 case DLT_PRISM_HEADER
:
1479 case DLT_IEEE802_11_RADIO
:
1482 case DLT_ATM_RFC1483
:
1484 case DLT_IP_OVER_FC
:
1485 return gen_llc_linktype(proto
);
1491 * If "is_lane" is set, check for a LANE-encapsulated
1492 * version of this protocol, otherwise check for an
1493 * LLC-encapsulated version of this protocol.
1495 * We assume LANE means Ethernet, not Token Ring.
1499 * Check that the packet doesn't begin with an
1500 * LE Control marker. (We've already generated
1503 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1507 * Now generate an Ethernet test.
1509 b1
= gen_ether_linktype(proto
);
1514 * Check for LLC encapsulation and then check the
1517 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1518 b1
= gen_llc_linktype(proto
);
1526 return gen_linux_sll_linktype(proto
);
1531 case DLT_SLIP_BSDOS
:
1534 * These types don't provide any type field; packets
1537 * XXX - for IPv4, check for a version number of 4, and,
1538 * for IPv6, check for a version number of 6?
1544 case ETHERTYPE_IPV6
:
1546 return gen_true(); /* always true */
1549 return gen_false(); /* always false */
1556 case DLT_PPP_SERIAL
:
1559 * We use Ethernet protocol types inside libpcap;
1560 * map them to the corresponding PPP protocol types.
1569 case ETHERTYPE_IPV6
:
1578 case ETHERTYPE_ATALK
:
1592 * I'm assuming the "Bridging PDU"s that go
1593 * over PPP are Spanning Tree Protocol
1607 * We use Ethernet protocol types inside libpcap;
1608 * map them to the corresponding PPP protocol types.
1613 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1614 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1616 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1621 case ETHERTYPE_IPV6
:
1631 case ETHERTYPE_ATALK
:
1645 * I'm assuming the "Bridging PDU"s that go
1646 * over PPP are Spanning Tree Protocol
1662 * For DLT_NULL, the link-layer header is a 32-bit
1663 * word containing an AF_ value in *host* byte order,
1664 * and for DLT_ENC, the link-layer header begins
1665 * with a 32-bit work containing an AF_ value in
1668 * In addition, if we're reading a saved capture file,
1669 * the host byte order in the capture may not be the
1670 * same as the host byte order on this machine.
1672 * For DLT_LOOP, the link-layer header is a 32-bit
1673 * word containing an AF_ value in *network* byte order.
1675 * XXX - AF_ values may, unfortunately, be platform-
1676 * dependent; for example, FreeBSD's AF_INET6 is 24
1677 * whilst NetBSD's and OpenBSD's is 26.
1679 * This means that, when reading a capture file, just
1680 * checking for our AF_INET6 value won't work if the
1681 * capture file came from another OS.
1690 case ETHERTYPE_IPV6
:
1697 * Not a type on which we support filtering.
1698 * XXX - support those that have AF_ values
1699 * #defined on this platform, at least?
1704 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1706 * The AF_ value is in host byte order, but
1707 * the BPF interpreter will convert it to
1708 * network byte order.
1710 * If this is a save file, and it's from a
1711 * machine with the opposite byte order to
1712 * ours, we byte-swap the AF_ value.
1714 * Then we run it through "htonl()", and
1715 * generate code to compare against the result.
1717 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1718 bpf_pcap
->sf
.swapped
)
1719 proto
= SWAPLONG(proto
);
1720 proto
= htonl(proto
);
1722 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1726 * af field is host byte order in contrast to the rest of
1729 if (proto
== ETHERTYPE_IP
)
1730 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1731 (bpf_int32
)AF_INET
));
1733 else if (proto
== ETHERTYPE_IPV6
)
1734 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1735 (bpf_int32
)AF_INET6
));
1743 case DLT_ARCNET_LINUX
:
1745 * XXX should we check for first fragment if the protocol
1754 case ETHERTYPE_IPV6
:
1755 return (gen_cmp(off_linktype
, BPF_B
,
1756 (bpf_int32
)ARCTYPE_INET6
));
1760 b0
= gen_cmp(off_linktype
, BPF_B
,
1761 (bpf_int32
)ARCTYPE_IP
);
1762 b1
= gen_cmp(off_linktype
, BPF_B
,
1763 (bpf_int32
)ARCTYPE_IP_OLD
);
1768 b0
= gen_cmp(off_linktype
, BPF_B
,
1769 (bpf_int32
)ARCTYPE_ARP
);
1770 b1
= gen_cmp(off_linktype
, BPF_B
,
1771 (bpf_int32
)ARCTYPE_ARP_OLD
);
1775 case ETHERTYPE_REVARP
:
1776 return (gen_cmp(off_linktype
, BPF_B
,
1777 (bpf_int32
)ARCTYPE_REVARP
));
1779 case ETHERTYPE_ATALK
:
1780 return (gen_cmp(off_linktype
, BPF_B
,
1781 (bpf_int32
)ARCTYPE_ATALK
));
1788 case ETHERTYPE_ATALK
:
1798 * XXX - assumes a 2-byte Frame Relay header with
1799 * DLCI and flags. What if the address is longer?
1805 * Check for the special NLPID for IP.
1807 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1810 case ETHERTYPE_IPV6
:
1812 * Check for the special NLPID for IPv6.
1814 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1819 * Check for several OSI protocols.
1821 * Frame Relay packets typically have an OSI
1822 * NLPID at the beginning; we check for each
1825 * What we check for is the NLPID and a frame
1826 * control field of UI, i.e. 0x03 followed
1829 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1830 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1831 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1842 case DLT_JUNIPER_MLFR
:
1843 case DLT_JUNIPER_MLPPP
:
1844 case DLT_JUNIPER_ATM1
:
1845 case DLT_JUNIPER_ATM2
:
1846 /* just lets verify the magic number for now -
1847 * on ATM we may have up to 6 different encapsulations on the wire
1848 * and need a lot of heuristics to figure out that the payload
1851 * FIXME encapsulation specific BPF_ filters
1853 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1855 case DLT_LINUX_IRDA
:
1856 bpf_error("IrDA link-layer type filtering not implemented");
1859 bpf_error("DOCSIS link-layer type filtering not implemented");
1861 case DLT_LINUX_LAPD
:
1862 bpf_error("LAPD link-layer type filtering not implemented");
1866 * All the types that have no encapsulation should either be
1867 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1868 * all packets are IP packets, or should be handled in some
1869 * special case, if none of them are (if some are and some
1870 * aren't, the lack of encapsulation is a problem, as we'd
1871 * have to find some other way of determining the packet type).
1873 * Therefore, if "off_linktype" is -1, there's an error.
1875 if (off_linktype
== (u_int
)-1)
1879 * Any type not handled above should always have an Ethernet
1880 * type at an offset of "off_linktype". (PPP is partially
1881 * handled above - the protocol type is mapped from the
1882 * Ethernet and LLC types we use internally to the corresponding
1883 * PPP type - but the PPP type is always specified by a value
1884 * at "off_linktype", so we don't have to do the code generation
1887 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1891 * Check for an LLC SNAP packet with a given organization code and
1892 * protocol type; we check the entire contents of the 802.2 LLC and
1893 * snap headers, checking for DSAP and SSAP of SNAP and a control
1894 * field of 0x03 in the LLC header, and for the specified organization
1895 * code and protocol type in the SNAP header.
1897 static struct block
*
1898 gen_snap(orgcode
, ptype
, offset
)
1899 bpf_u_int32 orgcode
;
1903 u_char snapblock
[8];
1905 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1906 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1907 snapblock
[2] = 0x03; /* control = UI */
1908 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1909 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1910 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1911 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1912 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1913 return gen_bcmp(offset
, 8, snapblock
);
1917 * Generate code to match a particular packet type, for link-layer types
1918 * using 802.2 LLC headers.
1920 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1921 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1923 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1924 * value, if <= ETHERMTU. We use that to determine whether to
1925 * match the DSAP or both DSAP and LSAP or to check the OUI and
1926 * protocol ID in a SNAP header.
1928 static struct block
*
1929 gen_llc_linktype(proto
)
1933 * XXX - handle token-ring variable-length header.
1939 case LLCSAP_NETBEUI
:
1941 * XXX - should we check both the DSAP and the
1942 * SSAP, like this, or should we check just the
1943 * DSAP, as we do for other types <= ETHERMTU
1944 * (i.e., other SAP values)?
1946 return gen_cmp(off_linktype
, BPF_H
, (long)
1947 ((proto
<< 8) | proto
));
1951 * XXX - are there ever SNAP frames for IPX on
1952 * non-Ethernet 802.x networks?
1954 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1956 case ETHERTYPE_ATALK
:
1958 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1959 * SNAP packets with an organization code of
1960 * 0x080007 (Apple, for Appletalk) and a protocol
1961 * type of ETHERTYPE_ATALK (Appletalk).
1963 * XXX - check for an organization code of
1964 * encapsulated Ethernet as well?
1966 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1970 * XXX - we don't have to check for IPX 802.3
1971 * here, but should we check for the IPX Ethertype?
1973 if (proto
<= ETHERMTU
) {
1975 * This is an LLC SAP value, so check
1978 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1981 * This is an Ethernet type; we assume that it's
1982 * unlikely that it'll appear in the right place
1983 * at random, and therefore check only the
1984 * location that would hold the Ethernet type
1985 * in a SNAP frame with an organization code of
1986 * 0x000000 (encapsulated Ethernet).
1988 * XXX - if we were to check for the SNAP DSAP and
1989 * LSAP, as per XXX, and were also to check for an
1990 * organization code of 0x000000 (encapsulated
1991 * Ethernet), we'd do
1993 * return gen_snap(0x000000, proto,
1996 * here; for now, we don't, as per the above.
1997 * I don't know whether it's worth the extra CPU
1998 * time to do the right check or not.
2000 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2005 static struct block
*
2006 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2010 u_int src_off
, dst_off
;
2012 struct block
*b0
, *b1
;
2026 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2027 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2033 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2034 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2041 b0
= gen_linktype(proto
);
2042 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2048 static struct block
*
2049 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2050 struct in6_addr
*addr
;
2051 struct in6_addr
*mask
;
2053 u_int src_off
, dst_off
;
2055 struct block
*b0
, *b1
;
2070 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2071 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2077 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2078 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2085 /* this order is important */
2086 a
= (u_int32_t
*)addr
;
2087 m
= (u_int32_t
*)mask
;
2088 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2089 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2091 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2093 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2095 b0
= gen_linktype(proto
);
2101 static struct block
*
2102 gen_ehostop(eaddr
, dir
)
2103 register const u_char
*eaddr
;
2106 register struct block
*b0
, *b1
;
2110 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2113 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2116 b0
= gen_ehostop(eaddr
, Q_SRC
);
2117 b1
= gen_ehostop(eaddr
, Q_DST
);
2123 b0
= gen_ehostop(eaddr
, Q_SRC
);
2124 b1
= gen_ehostop(eaddr
, Q_DST
);
2133 * Like gen_ehostop, but for DLT_FDDI
2135 static struct block
*
2136 gen_fhostop(eaddr
, dir
)
2137 register const u_char
*eaddr
;
2140 struct block
*b0
, *b1
;
2145 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2147 return gen_bcmp(6 + 1, 6, eaddr
);
2152 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2154 return gen_bcmp(0 + 1, 6, eaddr
);
2158 b0
= gen_fhostop(eaddr
, Q_SRC
);
2159 b1
= gen_fhostop(eaddr
, Q_DST
);
2165 b0
= gen_fhostop(eaddr
, Q_SRC
);
2166 b1
= gen_fhostop(eaddr
, Q_DST
);
2175 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2177 static struct block
*
2178 gen_thostop(eaddr
, dir
)
2179 register const u_char
*eaddr
;
2182 register struct block
*b0
, *b1
;
2186 return gen_bcmp(8, 6, eaddr
);
2189 return gen_bcmp(2, 6, eaddr
);
2192 b0
= gen_thostop(eaddr
, Q_SRC
);
2193 b1
= gen_thostop(eaddr
, Q_DST
);
2199 b0
= gen_thostop(eaddr
, Q_SRC
);
2200 b1
= gen_thostop(eaddr
, Q_DST
);
2209 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2211 static struct block
*
2212 gen_wlanhostop(eaddr
, dir
)
2213 register const u_char
*eaddr
;
2216 register struct block
*b0
, *b1
, *b2
;
2217 register struct slist
*s
;
2224 * For control frames, there is no SA.
2226 * For management frames, SA is at an
2227 * offset of 10 from the beginning of
2230 * For data frames, SA is at an offset
2231 * of 10 from the beginning of the packet
2232 * if From DS is clear, at an offset of
2233 * 16 from the beginning of the packet
2234 * if From DS is set and To DS is clear,
2235 * and an offset of 24 from the beginning
2236 * of the packet if From DS is set and To DS
2241 * Generate the tests to be done for data frames
2244 * First, check for To DS set, i.e. check "link[1] & 0x01".
2246 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2248 b1
= new_block(JMP(BPF_JSET
));
2249 b1
->s
.k
= 0x01; /* To DS */
2253 * If To DS is set, the SA is at 24.
2255 b0
= gen_bcmp(24, 6, eaddr
);
2259 * Now, check for To DS not set, i.e. check
2260 * "!(link[1] & 0x01)".
2262 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2264 b2
= new_block(JMP(BPF_JSET
));
2265 b2
->s
.k
= 0x01; /* To DS */
2270 * If To DS is not set, the SA is at 16.
2272 b1
= gen_bcmp(16, 6, eaddr
);
2276 * Now OR together the last two checks. That gives
2277 * the complete set of checks for data frames with
2283 * Now check for From DS being set, and AND that with
2284 * the ORed-together checks.
2286 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2288 b1
= new_block(JMP(BPF_JSET
));
2289 b1
->s
.k
= 0x02; /* From DS */
2294 * Now check for data frames with From DS not set.
2296 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2298 b2
= new_block(JMP(BPF_JSET
));
2299 b2
->s
.k
= 0x02; /* From DS */
2304 * If From DS isn't set, the SA is at 10.
2306 b1
= gen_bcmp(10, 6, eaddr
);
2310 * Now OR together the checks for data frames with
2311 * From DS not set and for data frames with From DS
2312 * set; that gives the checks done for data frames.
2317 * Now check for a data frame.
2318 * I.e, check "link[0] & 0x08".
2320 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2322 b1
= new_block(JMP(BPF_JSET
));
2327 * AND that with the checks done for data frames.
2332 * If the high-order bit of the type value is 0, this
2333 * is a management frame.
2334 * I.e, check "!(link[0] & 0x08)".
2336 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2338 b2
= new_block(JMP(BPF_JSET
));
2344 * For management frames, the SA is at 10.
2346 b1
= gen_bcmp(10, 6, eaddr
);
2350 * OR that with the checks done for data frames.
2351 * That gives the checks done for management and
2357 * If the low-order bit of the type value is 1,
2358 * this is either a control frame or a frame
2359 * with a reserved type, and thus not a
2362 * I.e., check "!(link[0] & 0x04)".
2364 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2366 b1
= new_block(JMP(BPF_JSET
));
2372 * AND that with the checks for data and management
2382 * For control frames, there is no DA.
2384 * For management frames, DA is at an
2385 * offset of 4 from the beginning of
2388 * For data frames, DA is at an offset
2389 * of 4 from the beginning of the packet
2390 * if To DS is clear and at an offset of
2391 * 16 from the beginning of the packet
2396 * Generate the tests to be done for data frames.
2398 * First, check for To DS set, i.e. "link[1] & 0x01".
2400 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2402 b1
= new_block(JMP(BPF_JSET
));
2403 b1
->s
.k
= 0x01; /* To DS */
2407 * If To DS is set, the DA is at 16.
2409 b0
= gen_bcmp(16, 6, eaddr
);
2413 * Now, check for To DS not set, i.e. check
2414 * "!(link[1] & 0x01)".
2416 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2418 b2
= new_block(JMP(BPF_JSET
));
2419 b2
->s
.k
= 0x01; /* To DS */
2424 * If To DS is not set, the DA is at 4.
2426 b1
= gen_bcmp(4, 6, eaddr
);
2430 * Now OR together the last two checks. That gives
2431 * the complete set of checks for data frames.
2436 * Now check for a data frame.
2437 * I.e, check "link[0] & 0x08".
2439 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2441 b1
= new_block(JMP(BPF_JSET
));
2446 * AND that with the checks done for data frames.
2451 * If the high-order bit of the type value is 0, this
2452 * is a management frame.
2453 * I.e, check "!(link[0] & 0x08)".
2455 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2457 b2
= new_block(JMP(BPF_JSET
));
2463 * For management frames, the DA is at 4.
2465 b1
= gen_bcmp(4, 6, eaddr
);
2469 * OR that with the checks done for data frames.
2470 * That gives the checks done for management and
2476 * If the low-order bit of the type value is 1,
2477 * this is either a control frame or a frame
2478 * with a reserved type, and thus not a
2481 * I.e., check "!(link[0] & 0x04)".
2483 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2485 b1
= new_block(JMP(BPF_JSET
));
2491 * AND that with the checks for data and management
2498 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2499 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2505 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2506 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2515 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2516 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2517 * as the RFC states.)
2519 static struct block
*
2520 gen_ipfchostop(eaddr
, dir
)
2521 register const u_char
*eaddr
;
2524 register struct block
*b0
, *b1
;
2528 return gen_bcmp(10, 6, eaddr
);
2531 return gen_bcmp(2, 6, eaddr
);
2534 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2535 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2541 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2542 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2551 * This is quite tricky because there may be pad bytes in front of the
2552 * DECNET header, and then there are two possible data packet formats that
2553 * carry both src and dst addresses, plus 5 packet types in a format that
2554 * carries only the src node, plus 2 types that use a different format and
2555 * also carry just the src node.
2559 * Instead of doing those all right, we just look for data packets with
2560 * 0 or 1 bytes of padding. If you want to look at other packets, that
2561 * will require a lot more hacking.
2563 * To add support for filtering on DECNET "areas" (network numbers)
2564 * one would want to add a "mask" argument to this routine. That would
2565 * make the filter even more inefficient, although one could be clever
2566 * and not generate masking instructions if the mask is 0xFFFF.
2568 static struct block
*
2569 gen_dnhostop(addr
, dir
, base_off
)
2574 struct block
*b0
, *b1
, *b2
, *tmp
;
2575 u_int offset_lh
; /* offset if long header is received */
2576 u_int offset_sh
; /* offset if short header is received */
2581 offset_sh
= 1; /* follows flags */
2582 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2586 offset_sh
= 3; /* follows flags, dstnode */
2587 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2591 /* Inefficient because we do our Calvinball dance twice */
2592 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2593 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2599 /* Inefficient because we do our Calvinball dance twice */
2600 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2601 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2606 bpf_error("ISO host filtering not implemented");
2611 b0
= gen_linktype(ETHERTYPE_DN
);
2612 /* Check for pad = 1, long header case */
2613 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2614 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2615 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2616 BPF_H
, (bpf_int32
)ntohs(addr
));
2618 /* Check for pad = 0, long header case */
2619 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2620 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2623 /* Check for pad = 1, short header case */
2624 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2625 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2626 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2627 BPF_H
, (bpf_int32
)ntohs(addr
));
2630 /* Check for pad = 0, short header case */
2631 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2632 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2636 /* Combine with test for linktype */
2641 static struct block
*
2642 gen_host(addr
, mask
, proto
, dir
)
2648 struct block
*b0
, *b1
;
2653 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2654 if (off_linktype
!= (u_int
)-1) {
2655 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2657 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2663 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2664 off_nl
+ 12, off_nl
+ 16);
2667 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2668 off_nl
+ 14, off_nl
+ 24);
2671 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2672 off_nl
+ 14, off_nl
+ 24);
2675 bpf_error("'tcp' modifier applied to host");
2678 bpf_error("'sctp' modifier applied to host");
2681 bpf_error("'udp' modifier applied to host");
2684 bpf_error("'icmp' modifier applied to host");
2687 bpf_error("'igmp' modifier applied to host");
2690 bpf_error("'igrp' modifier applied to host");
2693 bpf_error("'pim' modifier applied to host");
2696 bpf_error("'vrrp' modifier applied to host");
2699 bpf_error("ATALK host filtering not implemented");
2702 bpf_error("AARP host filtering not implemented");
2705 return gen_dnhostop(addr
, dir
, off_nl
);
2708 bpf_error("SCA host filtering not implemented");
2711 bpf_error("LAT host filtering not implemented");
2714 bpf_error("MOPDL host filtering not implemented");
2717 bpf_error("MOPRC host filtering not implemented");
2721 bpf_error("'ip6' modifier applied to ip host");
2724 bpf_error("'icmp6' modifier applied to host");
2728 bpf_error("'ah' modifier applied to host");
2731 bpf_error("'esp' modifier applied to host");
2734 bpf_error("ISO host filtering not implemented");
2737 bpf_error("'esis' modifier applied to host");
2740 bpf_error("'isis' modifier applied to host");
2743 bpf_error("'clnp' modifier applied to host");
2746 bpf_error("'stp' modifier applied to host");
2749 bpf_error("IPX host filtering not implemented");
2752 bpf_error("'netbeui' modifier applied to host");
2761 static struct block
*
2762 gen_host6(addr
, mask
, proto
, dir
)
2763 struct in6_addr
*addr
;
2764 struct in6_addr
*mask
;
2771 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2774 bpf_error("'ip' modifier applied to ip6 host");
2777 bpf_error("'rarp' modifier applied to ip6 host");
2780 bpf_error("'arp' modifier applied to ip6 host");
2783 bpf_error("'sctp' modifier applied to host");
2786 bpf_error("'tcp' modifier applied to host");
2789 bpf_error("'udp' modifier applied to host");
2792 bpf_error("'icmp' modifier applied to host");
2795 bpf_error("'igmp' modifier applied to host");
2798 bpf_error("'igrp' modifier applied to host");
2801 bpf_error("'pim' modifier applied to host");
2804 bpf_error("'vrrp' modifier applied to host");
2807 bpf_error("ATALK host filtering not implemented");
2810 bpf_error("AARP host filtering not implemented");
2813 bpf_error("'decnet' modifier applied to ip6 host");
2816 bpf_error("SCA host filtering not implemented");
2819 bpf_error("LAT host filtering not implemented");
2822 bpf_error("MOPDL host filtering not implemented");
2825 bpf_error("MOPRC host filtering not implemented");
2828 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2829 off_nl
+ 8, off_nl
+ 24);
2832 bpf_error("'icmp6' modifier applied to host");
2835 bpf_error("'ah' modifier applied to host");
2838 bpf_error("'esp' modifier applied to host");
2841 bpf_error("ISO host filtering not implemented");
2844 bpf_error("'esis' modifier applied to host");
2847 bpf_error("'isis' modifier applied to host");
2850 bpf_error("'clnp' modifier applied to host");
2853 bpf_error("'stp' modifier applied to host");
2856 bpf_error("IPX host filtering not implemented");
2859 bpf_error("'netbeui' modifier applied to host");
2869 static struct block
*
2870 gen_gateway(eaddr
, alist
, proto
, dir
)
2871 const u_char
*eaddr
;
2872 bpf_u_int32
**alist
;
2876 struct block
*b0
, *b1
, *tmp
;
2879 bpf_error("direction applied to 'gateway'");
2886 if (linktype
== DLT_EN10MB
)
2887 b0
= gen_ehostop(eaddr
, Q_OR
);
2888 else if (linktype
== DLT_FDDI
)
2889 b0
= gen_fhostop(eaddr
, Q_OR
);
2890 else if (linktype
== DLT_IEEE802
)
2891 b0
= gen_thostop(eaddr
, Q_OR
);
2892 else if (linktype
== DLT_IEEE802_11
)
2893 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2894 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2896 * Check that the packet doesn't begin with an
2897 * LE Control marker. (We've already generated
2900 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2904 * Now check the MAC address.
2906 b0
= gen_ehostop(eaddr
, Q_OR
);
2908 } else if (linktype
== DLT_IP_OVER_FC
)
2909 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2912 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2914 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2916 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2924 bpf_error("illegal modifier of 'gateway'");
2930 gen_proto_abbrev(proto
)
2939 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2941 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2947 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2949 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2955 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2957 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2963 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2966 #ifndef IPPROTO_IGMP
2967 #define IPPROTO_IGMP 2
2971 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2974 #ifndef IPPROTO_IGRP
2975 #define IPPROTO_IGRP 9
2978 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2982 #define IPPROTO_PIM 103
2986 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2988 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2993 #ifndef IPPROTO_VRRP
2994 #define IPPROTO_VRRP 112
2998 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3002 b1
= gen_linktype(ETHERTYPE_IP
);
3006 b1
= gen_linktype(ETHERTYPE_ARP
);
3010 b1
= gen_linktype(ETHERTYPE_REVARP
);
3014 bpf_error("link layer applied in wrong context");
3017 b1
= gen_linktype(ETHERTYPE_ATALK
);
3021 b1
= gen_linktype(ETHERTYPE_AARP
);
3025 b1
= gen_linktype(ETHERTYPE_DN
);
3029 b1
= gen_linktype(ETHERTYPE_SCA
);
3033 b1
= gen_linktype(ETHERTYPE_LAT
);
3037 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3041 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3046 b1
= gen_linktype(ETHERTYPE_IPV6
);
3049 #ifndef IPPROTO_ICMPV6
3050 #define IPPROTO_ICMPV6 58
3053 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3058 #define IPPROTO_AH 51
3061 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3063 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3069 #define IPPROTO_ESP 50
3072 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3074 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3080 b1
= gen_linktype(LLCSAP_ISONS
);
3084 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3088 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3091 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3092 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3093 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3095 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3097 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3099 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3103 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3104 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3105 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3107 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3109 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3111 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3115 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3116 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3117 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3119 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3124 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3125 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3130 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3131 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3133 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3135 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3140 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3141 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3146 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3147 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3152 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3156 b1
= gen_linktype(LLCSAP_8021D
);
3160 b1
= gen_linktype(LLCSAP_IPX
);
3164 b1
= gen_linktype(LLCSAP_NETBEUI
);
3173 static struct block
*
3180 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3181 s
->s
.k
= off_nl
+ 6;
3182 b
= new_block(JMP(BPF_JSET
));
3190 static struct block
*
3191 gen_portatom(off
, v
)
3198 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3201 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3202 s
->next
->s
.k
= off_nl
+ off
;
3204 b
= new_block(JMP(BPF_JEQ
));
3212 static struct block
*
3213 gen_portatom6(off
, v
)
3217 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3222 gen_portop(port
, proto
, dir
)
3223 int port
, proto
, dir
;
3225 struct block
*b0
, *b1
, *tmp
;
3227 /* ip proto 'proto' */
3228 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3234 b1
= gen_portatom(0, (bpf_int32
)port
);
3238 b1
= gen_portatom(2, (bpf_int32
)port
);
3243 tmp
= gen_portatom(0, (bpf_int32
)port
);
3244 b1
= gen_portatom(2, (bpf_int32
)port
);
3249 tmp
= gen_portatom(0, (bpf_int32
)port
);
3250 b1
= gen_portatom(2, (bpf_int32
)port
);
3262 static struct block
*
3263 gen_port(port
, ip_proto
, dir
)
3268 struct block
*b0
, *b1
, *tmp
;
3273 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3274 * not LLC encapsulation with LLCSAP_IP.
3276 * For IEEE 802 networks - which includes 802.5 token ring
3277 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3278 * says that SNAP encapsulation is used, not LLC encapsulation
3281 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3282 * RFC 2225 say that SNAP encapsulation is used, not LLC
3283 * encapsulation with LLCSAP_IP.
3285 * So we always check for ETHERTYPE_IP.
3287 b0
= gen_linktype(ETHERTYPE_IP
);
3293 b1
= gen_portop(port
, ip_proto
, dir
);
3297 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3298 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3300 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3313 gen_portop6(port
, proto
, dir
)
3314 int port
, proto
, dir
;
3316 struct block
*b0
, *b1
, *tmp
;
3318 /* ip proto 'proto' */
3319 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3323 b1
= gen_portatom6(0, (bpf_int32
)port
);
3327 b1
= gen_portatom6(2, (bpf_int32
)port
);
3332 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3333 b1
= gen_portatom6(2, (bpf_int32
)port
);
3338 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3339 b1
= gen_portatom6(2, (bpf_int32
)port
);
3351 static struct block
*
3352 gen_port6(port
, ip_proto
, dir
)
3357 struct block
*b0
, *b1
, *tmp
;
3359 /* ether proto ip */
3360 b0
= gen_linktype(ETHERTYPE_IPV6
);
3366 b1
= gen_portop6(port
, ip_proto
, dir
);
3370 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3371 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3373 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3386 lookup_proto(name
, proto
)
3387 register const char *name
;
3397 v
= pcap_nametoproto(name
);
3398 if (v
== PROTO_UNDEF
)
3399 bpf_error("unknown ip proto '%s'", name
);
3403 /* XXX should look up h/w protocol type based on linktype */
3404 v
= pcap_nametoeproto(name
);
3405 if (v
== PROTO_UNDEF
) {
3406 v
= pcap_nametollc(name
);
3407 if (v
== PROTO_UNDEF
)
3408 bpf_error("unknown ether proto '%s'", name
);
3413 if (strcmp(name
, "esis") == 0)
3415 else if (strcmp(name
, "isis") == 0)
3417 else if (strcmp(name
, "clnp") == 0)
3420 bpf_error("unknown osi proto '%s'", name
);
3440 static struct block
*
3441 gen_protochain(v
, proto
, dir
)
3446 #ifdef NO_PROTOCHAIN
3447 return gen_proto(v
, proto
, dir
);
3449 struct block
*b0
, *b
;
3450 struct slist
*s
[100];
3451 int fix2
, fix3
, fix4
, fix5
;
3452 int ahcheck
, again
, end
;
3454 int reg2
= alloc_reg();
3456 memset(s
, 0, sizeof(s
));
3457 fix2
= fix3
= fix4
= fix5
= 0;
3464 b0
= gen_protochain(v
, Q_IP
, dir
);
3465 b
= gen_protochain(v
, Q_IPV6
, dir
);
3469 bpf_error("bad protocol applied for 'protochain'");
3473 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3476 * s[0] is a dummy entry to protect other BPF insn from damage
3477 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3478 * hard to find interdependency made by jump table fixup.
3481 s
[i
] = new_stmt(0); /*dummy*/
3486 b0
= gen_linktype(ETHERTYPE_IP
);
3489 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3490 s
[i
]->s
.k
= off_nl
+ 9;
3492 /* X = ip->ip_hl << 2 */
3493 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3499 b0
= gen_linktype(ETHERTYPE_IPV6
);
3501 /* A = ip6->ip_nxt */
3502 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3503 s
[i
]->s
.k
= off_nl
+ 6;
3505 /* X = sizeof(struct ip6_hdr) */
3506 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3512 bpf_error("unsupported proto to gen_protochain");
3516 /* again: if (A == v) goto end; else fall through; */
3518 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3520 s
[i
]->s
.jt
= NULL
; /*later*/
3521 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3525 #ifndef IPPROTO_NONE
3526 #define IPPROTO_NONE 59
3528 /* if (A == IPPROTO_NONE) goto end */
3529 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3530 s
[i
]->s
.jt
= NULL
; /*later*/
3531 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3532 s
[i
]->s
.k
= IPPROTO_NONE
;
3533 s
[fix5
]->s
.jf
= s
[i
];
3538 if (proto
== Q_IPV6
) {
3539 int v6start
, v6end
, v6advance
, j
;
3542 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3543 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3544 s
[i
]->s
.jt
= NULL
; /*later*/
3545 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3546 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3547 s
[fix2
]->s
.jf
= s
[i
];
3549 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3550 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3551 s
[i
]->s
.jt
= NULL
; /*later*/
3552 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3553 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3555 /* if (A == IPPROTO_ROUTING) goto v6advance */
3556 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3557 s
[i
]->s
.jt
= NULL
; /*later*/
3558 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3559 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3561 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3562 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3563 s
[i
]->s
.jt
= NULL
; /*later*/
3564 s
[i
]->s
.jf
= NULL
; /*later*/
3565 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3576 * X = X + (P[X + 1] + 1) * 8;
3579 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3581 /* A = P[X + packet head] */
3582 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3586 s
[i
] = new_stmt(BPF_ST
);
3590 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3593 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3597 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3599 /* A = P[X + packet head]; */
3600 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3604 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3608 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3612 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3615 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3619 /* goto again; (must use BPF_JA for backward jump) */
3620 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3621 s
[i
]->s
.k
= again
- i
- 1;
3622 s
[i
- 1]->s
.jf
= s
[i
];
3626 for (j
= v6start
; j
<= v6end
; j
++)
3627 s
[j
]->s
.jt
= s
[v6advance
];
3632 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3634 s
[fix2
]->s
.jf
= s
[i
];
3640 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3641 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3642 s
[i
]->s
.jt
= NULL
; /*later*/
3643 s
[i
]->s
.jf
= NULL
; /*later*/
3644 s
[i
]->s
.k
= IPPROTO_AH
;
3646 s
[fix3
]->s
.jf
= s
[ahcheck
];
3653 * X = X + (P[X + 1] + 2) * 4;
3656 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3658 /* A = P[X + packet head]; */
3659 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3663 s
[i
] = new_stmt(BPF_ST
);
3667 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3670 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3674 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3676 /* A = P[X + packet head] */
3677 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3681 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3685 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3689 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3692 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3696 /* goto again; (must use BPF_JA for backward jump) */
3697 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3698 s
[i
]->s
.k
= again
- i
- 1;
3703 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3705 s
[fix2
]->s
.jt
= s
[end
];
3706 s
[fix4
]->s
.jf
= s
[end
];
3707 s
[fix5
]->s
.jt
= s
[end
];
3714 for (i
= 0; i
< max
- 1; i
++)
3715 s
[i
]->next
= s
[i
+ 1];
3716 s
[max
- 1]->next
= NULL
;
3721 b
= new_block(JMP(BPF_JEQ
));
3722 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3733 * Generate code that checks whether the packet is a packet for protocol
3734 * <proto> and whether the type field in that protocol's header has
3735 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
3736 * IP packet and checks the protocol number in the IP header against <v>.
3738 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
3739 * against Q_IP and Q_IPV6.
3741 static struct block
*
3742 gen_proto(v
, proto
, dir
)
3747 struct block
*b0
, *b1
;
3749 if (dir
!= Q_DEFAULT
)
3750 bpf_error("direction applied to 'proto'");
3755 b0
= gen_proto(v
, Q_IP
, dir
);
3756 b1
= gen_proto(v
, Q_IPV6
, dir
);
3764 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3765 * not LLC encapsulation with LLCSAP_IP.
3767 * For IEEE 802 networks - which includes 802.5 token ring
3768 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3769 * says that SNAP encapsulation is used, not LLC encapsulation
3772 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3773 * RFC 2225 say that SNAP encapsulation is used, not LLC
3774 * encapsulation with LLCSAP_IP.
3776 * So we always check for ETHERTYPE_IP.
3778 b0
= gen_linktype(ETHERTYPE_IP
);
3780 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3782 b1
= gen_protochain(v
, Q_IP
);
3792 * Frame Relay packets typically have an OSI
3793 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3794 * generates code to check for all the OSI
3795 * NLPIDs, so calling it and then adding a check
3796 * for the particular NLPID for which we're
3797 * looking is bogus, as we can just check for
3800 * What we check for is the NLPID and a frame
3801 * control field value of UI, i.e. 0x03 followed
3804 * XXX - assumes a 2-byte Frame Relay header with
3805 * DLCI and flags. What if the address is longer?
3807 * XXX - what about SNAP-encapsulated frames?
3809 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3815 * Cisco uses an Ethertype lookalike - for OSI,
3818 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3819 /* OSI in C-HDLC is stuffed with a fudge byte */
3820 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3825 b0
= gen_linktype(LLCSAP_ISONS
);
3826 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3832 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3834 * 4 is the offset of the PDU type relative to the IS-IS
3837 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3842 bpf_error("arp does not encapsulate another protocol");
3846 bpf_error("rarp does not encapsulate another protocol");
3850 bpf_error("atalk encapsulation is not specifiable");
3854 bpf_error("decnet encapsulation is not specifiable");
3858 bpf_error("sca does not encapsulate another protocol");
3862 bpf_error("lat does not encapsulate another protocol");
3866 bpf_error("moprc does not encapsulate another protocol");
3870 bpf_error("mopdl does not encapsulate another protocol");
3874 return gen_linktype(v
);
3877 bpf_error("'udp proto' is bogus");
3881 bpf_error("'tcp proto' is bogus");
3885 bpf_error("'sctp proto' is bogus");
3889 bpf_error("'icmp proto' is bogus");
3893 bpf_error("'igmp proto' is bogus");
3897 bpf_error("'igrp proto' is bogus");
3901 bpf_error("'pim proto' is bogus");
3905 bpf_error("'vrrp proto' is bogus");
3910 b0
= gen_linktype(ETHERTYPE_IPV6
);
3912 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3914 b1
= gen_protochain(v
, Q_IPV6
);
3920 bpf_error("'icmp6 proto' is bogus");
3924 bpf_error("'ah proto' is bogus");
3927 bpf_error("'ah proto' is bogus");
3930 bpf_error("'stp proto' is bogus");
3933 bpf_error("'ipx proto' is bogus");
3936 bpf_error("'netbeui proto' is bogus");
3947 register const char *name
;
3950 int proto
= q
.proto
;
3954 bpf_u_int32 mask
, addr
;
3956 bpf_u_int32
**alist
;
3959 struct sockaddr_in
*sin
;
3960 struct sockaddr_in6
*sin6
;
3961 struct addrinfo
*res
, *res0
;
3962 struct in6_addr mask128
;
3964 struct block
*b
, *tmp
;
3965 int port
, real_proto
;
3970 addr
= pcap_nametonetaddr(name
);
3972 bpf_error("unknown network '%s'", name
);
3973 /* Left justify network addr and calculate its network mask */
3975 while (addr
&& (addr
& 0xff000000) == 0) {
3979 return gen_host(addr
, mask
, proto
, dir
);
3983 if (proto
== Q_LINK
) {
3987 eaddr
= pcap_ether_hostton(name
);
3990 "unknown ether host '%s'", name
);
3991 b
= gen_ehostop(eaddr
, dir
);
3996 eaddr
= pcap_ether_hostton(name
);
3999 "unknown FDDI host '%s'", name
);
4000 b
= gen_fhostop(eaddr
, dir
);
4005 eaddr
= pcap_ether_hostton(name
);
4008 "unknown token ring host '%s'", name
);
4009 b
= gen_thostop(eaddr
, dir
);
4013 case DLT_IEEE802_11
:
4014 eaddr
= pcap_ether_hostton(name
);
4017 "unknown 802.11 host '%s'", name
);
4018 b
= gen_wlanhostop(eaddr
, dir
);
4022 case DLT_IP_OVER_FC
:
4023 eaddr
= pcap_ether_hostton(name
);
4026 "unknown Fibre Channel host '%s'", name
);
4027 b
= gen_ipfchostop(eaddr
, dir
);
4036 * Check that the packet doesn't begin
4037 * with an LE Control marker. (We've
4038 * already generated a test for LANE.)
4040 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4044 eaddr
= pcap_ether_hostton(name
);
4047 "unknown ether host '%s'", name
);
4048 b
= gen_ehostop(eaddr
, dir
);
4054 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4055 } else if (proto
== Q_DECNET
) {
4056 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4058 * I don't think DECNET hosts can be multihomed, so
4059 * there is no need to build up a list of addresses
4061 return (gen_host(dn_addr
, 0, proto
, dir
));
4064 alist
= pcap_nametoaddr(name
);
4065 if (alist
== NULL
|| *alist
== NULL
)
4066 bpf_error("unknown host '%s'", name
);
4068 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4070 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4072 tmp
= gen_host(**alist
++, 0xffffffff,
4079 memset(&mask128
, 0xff, sizeof(mask128
));
4080 res0
= res
= pcap_nametoaddrinfo(name
);
4082 bpf_error("unknown host '%s'", name
);
4084 tproto
= tproto6
= proto
;
4085 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4089 for (res
= res0
; res
; res
= res
->ai_next
) {
4090 switch (res
->ai_family
) {
4092 if (tproto
== Q_IPV6
)
4095 sin
= (struct sockaddr_in
*)
4097 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4098 0xffffffff, tproto
, dir
);
4101 if (tproto6
== Q_IP
)
4104 sin6
= (struct sockaddr_in6
*)
4106 tmp
= gen_host6(&sin6
->sin6_addr
,
4107 &mask128
, tproto6
, dir
);
4118 bpf_error("unknown host '%s'%s", name
,
4119 (proto
== Q_DEFAULT
)
4121 : " for specified address family");
4128 if (proto
!= Q_DEFAULT
&&
4129 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4130 bpf_error("illegal qualifier of 'port'");
4131 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4132 bpf_error("unknown port '%s'", name
);
4133 if (proto
== Q_UDP
) {
4134 if (real_proto
== IPPROTO_TCP
)
4135 bpf_error("port '%s' is tcp", name
);
4136 else if (real_proto
== IPPROTO_SCTP
)
4137 bpf_error("port '%s' is sctp", name
);
4139 /* override PROTO_UNDEF */
4140 real_proto
= IPPROTO_UDP
;
4142 if (proto
== Q_TCP
) {
4143 if (real_proto
== IPPROTO_UDP
)
4144 bpf_error("port '%s' is udp", name
);
4146 else if (real_proto
== IPPROTO_SCTP
)
4147 bpf_error("port '%s' is sctp", name
);
4149 /* override PROTO_UNDEF */
4150 real_proto
= IPPROTO_TCP
;
4152 if (proto
== Q_SCTP
) {
4153 if (real_proto
== IPPROTO_UDP
)
4154 bpf_error("port '%s' is udp", name
);
4156 else if (real_proto
== IPPROTO_TCP
)
4157 bpf_error("port '%s' is tcp", name
);
4159 /* override PROTO_UNDEF */
4160 real_proto
= IPPROTO_SCTP
;
4163 return gen_port(port
, real_proto
, dir
);
4167 b
= gen_port(port
, real_proto
, dir
);
4168 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4175 eaddr
= pcap_ether_hostton(name
);
4177 bpf_error("unknown ether host: %s", name
);
4179 alist
= pcap_nametoaddr(name
);
4180 if (alist
== NULL
|| *alist
== NULL
)
4181 bpf_error("unknown host '%s'", name
);
4182 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4186 bpf_error("'gateway' not supported in this configuration");
4190 real_proto
= lookup_proto(name
, proto
);
4191 if (real_proto
>= 0)
4192 return gen_proto(real_proto
, proto
, dir
);
4194 bpf_error("unknown protocol: %s", name
);
4197 real_proto
= lookup_proto(name
, proto
);
4198 if (real_proto
>= 0)
4199 return gen_protochain(real_proto
, proto
, dir
);
4201 bpf_error("unknown protocol: %s", name
);
4213 gen_mcode(s1
, s2
, masklen
, q
)
4214 register const char *s1
, *s2
;
4215 register int masklen
;
4218 register int nlen
, mlen
;
4221 nlen
= __pcap_atoin(s1
, &n
);
4222 /* Promote short ipaddr */
4226 mlen
= __pcap_atoin(s2
, &m
);
4227 /* Promote short ipaddr */
4230 bpf_error("non-network bits set in \"%s mask %s\"",
4233 /* Convert mask len to mask */
4235 bpf_error("mask length must be <= 32");
4236 m
= 0xffffffff << (32 - masklen
);
4238 bpf_error("non-network bits set in \"%s/%d\"",
4245 return gen_host(n
, m
, q
.proto
, q
.dir
);
4248 bpf_error("Mask syntax for networks only");
4256 register const char *s
;
4261 int proto
= q
.proto
;
4267 else if (q
.proto
== Q_DECNET
)
4268 vlen
= __pcap_atodn(s
, &v
);
4270 vlen
= __pcap_atoin(s
, &v
);
4277 if (proto
== Q_DECNET
)
4278 return gen_host(v
, 0, proto
, dir
);
4279 else if (proto
== Q_LINK
) {
4280 bpf_error("illegal link layer address");
4283 if (s
== NULL
&& q
.addr
== Q_NET
) {
4284 /* Promote short net number */
4285 while (v
&& (v
& 0xff000000) == 0) {
4290 /* Promote short ipaddr */
4294 return gen_host(v
, mask
, proto
, dir
);
4299 proto
= IPPROTO_UDP
;
4300 else if (proto
== Q_TCP
)
4301 proto
= IPPROTO_TCP
;
4302 else if (proto
== Q_SCTP
)
4303 proto
= IPPROTO_SCTP
;
4304 else if (proto
== Q_DEFAULT
)
4305 proto
= PROTO_UNDEF
;
4307 bpf_error("illegal qualifier of 'port'");
4310 return gen_port((int)v
, proto
, dir
);
4314 b
= gen_port((int)v
, proto
, dir
);
4315 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4321 bpf_error("'gateway' requires a name");
4325 return gen_proto((int)v
, proto
, dir
);
4328 return gen_protochain((int)v
, proto
, dir
);
4343 gen_mcode6(s1
, s2
, masklen
, q
)
4344 register const char *s1
, *s2
;
4345 register int masklen
;
4348 struct addrinfo
*res
;
4349 struct in6_addr
*addr
;
4350 struct in6_addr mask
;
4355 bpf_error("no mask %s supported", s2
);
4357 res
= pcap_nametoaddrinfo(s1
);
4359 bpf_error("invalid ip6 address %s", s1
);
4361 bpf_error("%s resolved to multiple address", s1
);
4362 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4364 if (sizeof(mask
) * 8 < masklen
)
4365 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4366 memset(&mask
, 0, sizeof(mask
));
4367 memset(&mask
, 0xff, masklen
/ 8);
4369 mask
.s6_addr
[masklen
/ 8] =
4370 (0xff << (8 - masklen
% 8)) & 0xff;
4373 a
= (u_int32_t
*)addr
;
4374 m
= (u_int32_t
*)&mask
;
4375 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4376 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4377 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4385 bpf_error("Mask syntax for networks only");
4389 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4394 bpf_error("invalid qualifier against IPv6 address");
4402 register const u_char
*eaddr
;
4405 struct block
*b
, *tmp
;
4407 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4408 if (linktype
== DLT_EN10MB
)
4409 return gen_ehostop(eaddr
, (int)q
.dir
);
4410 if (linktype
== DLT_FDDI
)
4411 return gen_fhostop(eaddr
, (int)q
.dir
);
4412 if (linktype
== DLT_IEEE802
)
4413 return gen_thostop(eaddr
, (int)q
.dir
);
4414 if (linktype
== DLT_IEEE802_11
)
4415 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4416 if (linktype
== DLT_SUNATM
&& is_lane
) {
4418 * Check that the packet doesn't begin with an
4419 * LE Control marker. (We've already generated
4422 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4426 * Now check the MAC address.
4428 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4432 if (linktype
== DLT_IP_OVER_FC
)
4433 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4434 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4436 bpf_error("ethernet address used in non-ether expression");
4442 struct slist
*s0
, *s1
;
4445 * This is definitely not the best way to do this, but the
4446 * lists will rarely get long.
4453 static struct slist
*
4459 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4464 static struct slist
*
4470 s
= new_stmt(BPF_LD
|BPF_MEM
);
4476 gen_load(proto
, index
, size
)
4481 struct slist
*s
, *tmp
;
4483 int regno
= alloc_reg();
4485 free_reg(index
->regno
);
4489 bpf_error("data size must be 1, 2, or 4");
4505 bpf_error("unsupported index operation");
4509 * XXX - what about ATM LANE? Should the index be
4510 * relative to the beginning of the AAL5 frame, so
4511 * that 0 refers to the beginning of the LE Control
4512 * field, or relative to the beginning of the LAN
4513 * frame, so that 0 refers, for Ethernet LANE, to
4514 * the beginning of the destination address?
4516 s
= xfer_to_x(index
);
4517 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4519 sappend(index
->s
, s
);
4534 /* XXX Note that we assume a fixed link header here. */
4535 s
= xfer_to_x(index
);
4536 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4539 sappend(index
->s
, s
);
4541 b
= gen_proto_abbrev(proto
);
4543 gen_and(index
->b
, b
);
4555 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4557 sappend(s
, xfer_to_a(index
));
4558 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4559 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4560 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4562 sappend(index
->s
, s
);
4564 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4566 gen_and(index
->b
, b
);
4568 gen_and(gen_proto_abbrev(Q_IP
), b
);
4574 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4578 index
->regno
= regno
;
4579 s
= new_stmt(BPF_ST
);
4581 sappend(index
->s
, s
);
4587 gen_relation(code
, a0
, a1
, reversed
)
4589 struct arth
*a0
, *a1
;
4592 struct slist
*s0
, *s1
, *s2
;
4593 struct block
*b
, *tmp
;
4597 if (code
== BPF_JEQ
) {
4598 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4599 b
= new_block(JMP(code
));
4603 b
= new_block(BPF_JMP
|code
|BPF_X
);
4609 sappend(a0
->s
, a1
->s
);
4613 free_reg(a0
->regno
);
4614 free_reg(a1
->regno
);
4616 /* 'and' together protocol checks */
4619 gen_and(a0
->b
, tmp
= a1
->b
);
4635 int regno
= alloc_reg();
4636 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4639 s
= new_stmt(BPF_LD
|BPF_LEN
);
4640 s
->next
= new_stmt(BPF_ST
);
4641 s
->next
->s
.k
= regno
;
4656 a
= (struct arth
*)newchunk(sizeof(*a
));
4660 s
= new_stmt(BPF_LD
|BPF_IMM
);
4662 s
->next
= new_stmt(BPF_ST
);
4678 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4681 s
= new_stmt(BPF_ST
);
4689 gen_arth(code
, a0
, a1
)
4691 struct arth
*a0
, *a1
;
4693 struct slist
*s0
, *s1
, *s2
;
4697 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4702 sappend(a0
->s
, a1
->s
);
4704 free_reg(a0
->regno
);
4705 free_reg(a1
->regno
);
4707 s0
= new_stmt(BPF_ST
);
4708 a0
->regno
= s0
->s
.k
= alloc_reg();
4715 * Here we handle simple allocation of the scratch registers.
4716 * If too many registers are alloc'd, the allocator punts.
4718 static int regused
[BPF_MEMWORDS
];
4722 * Return the next free register.
4727 int n
= BPF_MEMWORDS
;
4730 if (regused
[curreg
])
4731 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4733 regused
[curreg
] = 1;
4737 bpf_error("too many registers needed to evaluate expression");
4742 * Return a register to the table so it can
4752 static struct block
*
4759 s
= new_stmt(BPF_LD
|BPF_LEN
);
4760 b
= new_block(JMP(jmp
));
4771 return gen_len(BPF_JGE
, n
);
4775 * Actually, this is less than or equal.
4783 b
= gen_len(BPF_JGT
, n
);
4790 gen_byteop(op
, idx
, val
)
4801 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4804 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4805 b
->s
.code
= JMP(BPF_JGE
);
4810 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4811 b
->s
.code
= JMP(BPF_JGT
);
4815 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4819 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4823 b
= new_block(JMP(BPF_JEQ
));
4830 static u_char abroadcast
[] = { 0x0 };
4833 gen_broadcast(proto
)
4836 bpf_u_int32 hostmask
;
4837 struct block
*b0
, *b1
, *b2
;
4838 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4844 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4845 return gen_ahostop(abroadcast
, Q_DST
);
4846 if (linktype
== DLT_EN10MB
)
4847 return gen_ehostop(ebroadcast
, Q_DST
);
4848 if (linktype
== DLT_FDDI
)
4849 return gen_fhostop(ebroadcast
, Q_DST
);
4850 if (linktype
== DLT_IEEE802
)
4851 return gen_thostop(ebroadcast
, Q_DST
);
4852 if (linktype
== DLT_IEEE802_11
)
4853 return gen_wlanhostop(ebroadcast
, Q_DST
);
4854 if (linktype
== DLT_IP_OVER_FC
)
4855 return gen_ipfchostop(ebroadcast
, Q_DST
);
4856 if (linktype
== DLT_SUNATM
&& is_lane
) {
4858 * Check that the packet doesn't begin with an
4859 * LE Control marker. (We've already generated
4862 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4866 * Now check the MAC address.
4868 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4872 bpf_error("not a broadcast link");
4876 b0
= gen_linktype(ETHERTYPE_IP
);
4877 hostmask
= ~netmask
;
4878 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4879 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4880 (bpf_int32
)(~0 & hostmask
), hostmask
);
4885 bpf_error("only link-layer/IP broadcast filters supported");
4890 * Generate code to test the low-order bit of a MAC address (that's
4891 * the bottom bit of the *first* byte).
4893 static struct block
*
4894 gen_mac_multicast(offset
)
4897 register struct block
*b0
;
4898 register struct slist
*s
;
4900 /* link[offset] & 1 != 0 */
4901 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4903 b0
= new_block(JMP(BPF_JSET
));
4910 gen_multicast(proto
)
4913 register struct block
*b0
, *b1
, *b2
;
4914 register struct slist
*s
;
4920 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4921 /* all ARCnet multicasts use the same address */
4922 return gen_ahostop(abroadcast
, Q_DST
);
4924 if (linktype
== DLT_EN10MB
) {
4925 /* ether[0] & 1 != 0 */
4926 return gen_mac_multicast(0);
4929 if (linktype
== DLT_FDDI
) {
4931 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4933 * XXX - was that referring to bit-order issues?
4935 /* fddi[1] & 1 != 0 */
4936 return gen_mac_multicast(1);
4939 if (linktype
== DLT_IEEE802
) {
4940 /* tr[2] & 1 != 0 */
4941 return gen_mac_multicast(2);
4944 if (linktype
== DLT_IEEE802_11
) {
4948 * For control frames, there is no DA.
4950 * For management frames, DA is at an
4951 * offset of 4 from the beginning of
4954 * For data frames, DA is at an offset
4955 * of 4 from the beginning of the packet
4956 * if To DS is clear and at an offset of
4957 * 16 from the beginning of the packet
4962 * Generate the tests to be done for data frames.
4964 * First, check for To DS set, i.e. "link[1] & 0x01".
4966 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4968 b1
= new_block(JMP(BPF_JSET
));
4969 b1
->s
.k
= 0x01; /* To DS */
4973 * If To DS is set, the DA is at 16.
4975 b0
= gen_mac_multicast(16);
4979 * Now, check for To DS not set, i.e. check
4980 * "!(link[1] & 0x01)".
4982 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4984 b2
= new_block(JMP(BPF_JSET
));
4985 b2
->s
.k
= 0x01; /* To DS */
4990 * If To DS is not set, the DA is at 4.
4992 b1
= gen_mac_multicast(4);
4996 * Now OR together the last two checks. That gives
4997 * the complete set of checks for data frames.
5002 * Now check for a data frame.
5003 * I.e, check "link[0] & 0x08".
5005 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5007 b1
= new_block(JMP(BPF_JSET
));
5012 * AND that with the checks done for data frames.
5017 * If the high-order bit of the type value is 0, this
5018 * is a management frame.
5019 * I.e, check "!(link[0] & 0x08)".
5021 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5023 b2
= new_block(JMP(BPF_JSET
));
5029 * For management frames, the DA is at 4.
5031 b1
= gen_mac_multicast(4);
5035 * OR that with the checks done for data frames.
5036 * That gives the checks done for management and
5042 * If the low-order bit of the type value is 1,
5043 * this is either a control frame or a frame
5044 * with a reserved type, and thus not a
5047 * I.e., check "!(link[0] & 0x04)".
5049 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5051 b1
= new_block(JMP(BPF_JSET
));
5057 * AND that with the checks for data and management
5064 if (linktype
== DLT_IP_OVER_FC
) {
5065 b0
= gen_mac_multicast(2);
5069 if (linktype
== DLT_SUNATM
&& is_lane
) {
5071 * Check that the packet doesn't begin with an
5072 * LE Control marker. (We've already generated
5075 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5078 /* ether[off_mac] & 1 != 0 */
5079 b0
= gen_mac_multicast(off_mac
);
5084 /* Link not known to support multicasts */
5088 b0
= gen_linktype(ETHERTYPE_IP
);
5089 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5090 b1
->s
.code
= JMP(BPF_JGE
);
5096 b0
= gen_linktype(ETHERTYPE_IPV6
);
5097 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5102 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5107 * generate command for inbound/outbound. It's here so we can
5108 * make it link-type specific. 'dir' = 0 implies "inbound",
5109 * = 1 implies "outbound".
5115 register struct block
*b0
;
5118 * Only some data link types support inbound/outbound qualifiers.
5122 b0
= gen_relation(BPF_JEQ
,
5123 gen_load(Q_LINK
, gen_loadi(0), 1),
5131 * Match packets sent by this machine.
5133 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5136 * Match packets sent to this machine.
5137 * (No broadcast or multicast packets, or
5138 * packets sent to some other machine and
5139 * received promiscuously.)
5141 * XXX - packets sent to other machines probably
5142 * shouldn't be matched, but what about broadcast
5143 * or multicast packets we received?
5145 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5150 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5151 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5156 /* match outgoing packets */
5157 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5159 /* match incoming packets */
5160 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5164 case DLT_JUNIPER_MLFR
:
5165 case DLT_JUNIPER_MLPPP
:
5166 case DLT_JUNIPER_ATM1
:
5167 case DLT_JUNIPER_ATM2
:
5168 /* juniper flags (including direction) are stored
5169 * the byte after the 3-byte magic number */
5171 /* match outgoing packets */
5172 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5174 /* match incoming packets */
5175 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5180 bpf_error("inbound/outbound not supported on linktype %d",
5188 /* PF firewall log matched interface */
5190 gen_pf_ifname(const char *ifname
)
5195 if (linktype
== DLT_PFLOG
) {
5196 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5197 off
= offsetof(struct pfloghdr
, ifname
);
5199 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5202 if (strlen(ifname
) >= len
) {
5203 bpf_error("ifname interface names can only be %d characters",
5207 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5211 /* PF firewall log matched interface */
5213 gen_pf_ruleset(char *ruleset
)
5217 if (linktype
!= DLT_PFLOG
) {
5218 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5221 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5222 bpf_error("ruleset names can only be %ld characters",
5223 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5226 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5227 strlen(ruleset
), (const u_char
*)ruleset
);
5231 /* PF firewall log rule number */
5237 if (linktype
== DLT_PFLOG
) {
5238 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5241 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5248 /* PF firewall log sub-rule number */
5250 gen_pf_srnr(int srnr
)
5254 if (linktype
!= DLT_PFLOG
) {
5255 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5259 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5264 /* PF firewall log reason code */
5266 gen_pf_reason(int reason
)
5270 if (linktype
== DLT_PFLOG
) {
5271 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5274 bpf_error("reason not supported on linktype 0x%x", linktype
);
5281 /* PF firewall log action */
5283 gen_pf_action(int action
)
5287 if (linktype
== DLT_PFLOG
) {
5288 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5291 bpf_error("action not supported on linktype 0x%x", linktype
);
5300 register const u_char
*eaddr
;
5303 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5304 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5305 return gen_ahostop(eaddr
, (int)q
.dir
);
5307 bpf_error("ARCnet address used in non-arc expression");
5311 static struct block
*
5312 gen_ahostop(eaddr
, dir
)
5313 register const u_char
*eaddr
;
5316 register struct block
*b0
, *b1
;
5319 /* src comes first, different from Ethernet */
5321 return gen_bcmp(0, 1, eaddr
);
5324 return gen_bcmp(1, 1, eaddr
);
5327 b0
= gen_ahostop(eaddr
, Q_SRC
);
5328 b1
= gen_ahostop(eaddr
, Q_DST
);
5334 b0
= gen_ahostop(eaddr
, Q_SRC
);
5335 b1
= gen_ahostop(eaddr
, Q_DST
);
5344 * support IEEE 802.1Q VLAN trunk over ethernet
5353 * Change the offsets to point to the type and data fields within
5354 * the VLAN packet. Just increment the offsets, so that we
5355 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5356 * capture VLAN 200 encapsulated within VLAN 100.
5358 * XXX - this is a bit of a kludge. If we were to split the
5359 * compiler into a parser that parses an expression and
5360 * generates an expression tree, and a code generator that
5361 * takes an expression tree (which could come from our
5362 * parser or from some other parser) and generates BPF code,
5363 * we could perhaps make the offsets parameters of routines
5364 * and, in the handler for an "AND" node, pass to subnodes
5365 * other than the VLAN node the adjusted offsets.
5367 * This would mean that "vlan" would, instead of changing the
5368 * behavior of *all* tests after it, change only the behavior
5369 * of tests ANDed with it. That would change the documented
5370 * semantics of "vlan", which might break some expressions.
5371 * However, it would mean that "(vlan and ip) or ip" would check
5372 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5373 * checking only for VLAN-encapsulated IP, so that could still
5374 * be considered worth doing; it wouldn't break expressions
5375 * that are of the form "vlan and ..." or "vlan N and ...",
5376 * which I suspect are the most common expressions involving
5377 * "vlan". "vlan or ..." doesn't necessarily do what the user
5378 * would really want, now, as all the "or ..." tests would
5379 * be done assuming a VLAN, even though the "or" could be viewed
5380 * as meaning "or, if this isn't a VLAN packet...".
5382 orig_linktype
= off_linktype
; /* save original values */
5384 orig_nl_nosnap
= off_nl_nosnap
;
5395 bpf_error("no VLAN support for data link type %d",
5400 /* check for VLAN */
5401 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5403 /* If a specific VLAN is requested, check VLAN id */
5404 if (vlan_num
>= 0) {
5407 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5425 * Change the offsets to point to the type and data fields within
5426 * the MPLS packet. Just increment the offsets, so that we
5427 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5428 * capture packets with an outer label of 100000 and an inner
5431 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5433 orig_linktype
= off_linktype
; /* save original values */
5435 orig_nl_nosnap
= off_nl_nosnap
;
5439 case DLT_C_HDLC
: /* fall through */
5445 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5453 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5456 /* FIXME add other DLT_s ...
5457 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5458 * leave it for now */
5461 bpf_error("no MPLS support for data link type %d",
5468 /* If a specific MPLS label is requested, check it */
5469 if (label_num
>= 0) {
5472 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5473 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5482 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5494 bpf_error("'vpi' supported only on raw ATM");
5495 if (off_vpi
== (u_int
)-1)
5497 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5498 (u_int
)jvalue
, reverse
);
5503 bpf_error("'vci' supported only on raw ATM");
5504 if (off_vci
== (u_int
)-1)
5506 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5507 (u_int
)jvalue
, reverse
);
5511 if (off_proto
== (u_int
)-1)
5512 abort(); /* XXX - this isn't on FreeBSD */
5513 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5514 (u_int
)jvalue
, reverse
);
5518 if (off_payload
== (u_int
)-1)
5520 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5521 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5526 bpf_error("'callref' supported only on raw ATM");
5527 if (off_proto
== (u_int
)-1)
5529 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5530 (u_int
)jvalue
, reverse
);
5540 gen_atmtype_abbrev(type
)
5543 struct block
*b0
, *b1
;
5548 /* Get all packets in Meta signalling Circuit */
5550 bpf_error("'metac' supported only on raw ATM");
5551 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5552 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5557 /* Get all packets in Broadcast Circuit*/
5559 bpf_error("'bcc' supported only on raw ATM");
5560 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5561 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5566 /* Get all cells in Segment OAM F4 circuit*/
5568 bpf_error("'oam4sc' supported only on raw ATM");
5569 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5570 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5575 /* Get all cells in End-to-End OAM F4 Circuit*/
5577 bpf_error("'oam4ec' supported only on raw ATM");
5578 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5579 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5584 /* Get all packets in connection Signalling Circuit */
5586 bpf_error("'sc' supported only on raw ATM");
5587 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5588 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5593 /* Get all packets in ILMI Circuit */
5595 bpf_error("'ilmic' supported only on raw ATM");
5596 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5597 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5602 /* Get all LANE packets */
5604 bpf_error("'lane' supported only on raw ATM");
5605 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5608 * Arrange that all subsequent tests assume LANE
5609 * rather than LLC-encapsulated packets, and set
5610 * the offsets appropriately for LANE-encapsulated
5613 * "off_mac" is the offset of the Ethernet header,
5614 * which is 2 bytes past the ATM pseudo-header
5615 * (skipping the pseudo-header and 2-byte LE Client
5616 * field). The other offsets are Ethernet offsets
5617 * relative to "off_mac".
5620 off_mac
= off_payload
+ 2; /* MAC header */
5621 off_linktype
= off_mac
+ 12;
5622 off_nl
= off_mac
+ 14; /* Ethernet II */
5623 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5627 /* Get all LLC-encapsulated packets */
5629 bpf_error("'llc' supported only on raw ATM");
5630 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5641 static struct block
*
5642 gen_msg_abbrev(type
)
5648 * Q.2931 signalling protocol messages for handling virtual circuits
5649 * establishment and teardown
5654 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5658 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5662 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5666 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5670 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5673 case A_RELEASE_DONE
:
5674 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5684 gen_atmmulti_abbrev(type
)
5687 struct block
*b0
, *b1
;
5693 bpf_error("'oam' supported only on raw ATM");
5694 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5699 bpf_error("'oamf4' supported only on raw ATM");
5701 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5702 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5704 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5710 * Get Q.2931 signalling messages for switched
5711 * virtual connection
5714 bpf_error("'connectmsg' supported only on raw ATM");
5715 b0
= gen_msg_abbrev(A_SETUP
);
5716 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5718 b0
= gen_msg_abbrev(A_CONNECT
);
5720 b0
= gen_msg_abbrev(A_CONNECTACK
);
5722 b0
= gen_msg_abbrev(A_RELEASE
);
5724 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5726 b0
= gen_atmtype_abbrev(A_SC
);
5732 bpf_error("'metaconnect' supported only on raw ATM");
5733 b0
= gen_msg_abbrev(A_SETUP
);
5734 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5736 b0
= gen_msg_abbrev(A_CONNECT
);
5738 b0
= gen_msg_abbrev(A_RELEASE
);
5740 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5742 b0
= gen_atmtype_abbrev(A_METAC
);