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.222 2005-04-07 20:42:45 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
;
113 bpf_error(const char *fmt
, ...)
119 if (bpf_pcap
!= NULL
)
120 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
127 static void init_linktype(pcap_t
*);
129 static int alloc_reg(void);
130 static void free_reg(int);
132 static struct block
*root
;
135 * We divy out chunks of memory rather than call malloc each time so
136 * we don't have to worry about leaking memory. It's probably
137 * not a big deal if all this memory was wasted but if this ever
138 * goes into a library that would probably not be a good idea.
140 * XXX - this *is* in a library....
143 #define CHUNK0SIZE 1024
149 static struct chunk chunks
[NCHUNKS
];
150 static int cur_chunk
;
152 static void *newchunk(u_int
);
153 static void freechunks(void);
154 static inline struct block
*new_block(int);
155 static inline struct slist
*new_stmt(int);
156 static struct block
*gen_retblk(int);
157 static inline void syntax(void);
159 static void backpatch(struct block
*, struct block
*);
160 static void merge(struct block
*, struct block
*);
161 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
162 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
163 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
164 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
165 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
166 bpf_u_int32
, bpf_u_int32
, int);
167 static struct block
*gen_uncond(int);
168 static inline struct block
*gen_true(void);
169 static inline struct block
*gen_false(void);
170 static struct block
*gen_ether_linktype(int);
171 static struct block
*gen_linux_sll_linktype(int);
172 static struct block
*gen_linktype(int);
173 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
174 static struct block
*gen_llc(int);
175 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
177 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
179 static struct block
*gen_ahostop(const u_char
*, int);
180 static struct block
*gen_ehostop(const u_char
*, int);
181 static struct block
*gen_fhostop(const u_char
*, int);
182 static struct block
*gen_thostop(const u_char
*, int);
183 static struct block
*gen_wlanhostop(const u_char
*, int);
184 static struct block
*gen_ipfchostop(const u_char
*, int);
185 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
186 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
188 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
191 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
193 static struct block
*gen_ipfrag(void);
194 static struct block
*gen_portatom(int, bpf_int32
);
196 static struct block
*gen_portatom6(int, bpf_int32
);
198 struct block
*gen_portop(int, int, int);
199 static struct block
*gen_port(int, int, int);
201 struct block
*gen_portop6(int, int, int);
202 static struct block
*gen_port6(int, int, int);
204 static int lookup_proto(const char *, int);
205 static struct block
*gen_protochain(int, int, int);
206 static struct block
*gen_proto(int, int, int);
207 static struct slist
*xfer_to_x(struct arth
*);
208 static struct slist
*xfer_to_a(struct arth
*);
209 static struct block
*gen_mac_multicast(int);
210 static struct block
*gen_len(int, int);
212 static struct block
*gen_msg_abbrev(int type
);
223 /* XXX Round up to nearest long. */
224 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
226 /* XXX Round up to structure boundary. */
230 cp
= &chunks
[cur_chunk
];
231 if (n
> cp
->n_left
) {
232 ++cp
, k
= ++cur_chunk
;
234 bpf_error("out of memory");
235 size
= CHUNK0SIZE
<< k
;
236 cp
->m
= (void *)malloc(size
);
238 bpf_error("out of memory");
239 memset((char *)cp
->m
, 0, size
);
242 bpf_error("out of memory");
245 return (void *)((char *)cp
->m
+ cp
->n_left
);
254 for (i
= 0; i
< NCHUNKS
; ++i
)
255 if (chunks
[i
].m
!= NULL
) {
262 * A strdup whose allocations are freed after code generation is over.
266 register const char *s
;
268 int n
= strlen(s
) + 1;
269 char *cp
= newchunk(n
);
275 static inline struct block
*
281 p
= (struct block
*)newchunk(sizeof(*p
));
288 static inline struct slist
*
294 p
= (struct slist
*)newchunk(sizeof(*p
));
300 static struct block
*
304 struct block
*b
= new_block(BPF_RET
|BPF_K
);
313 bpf_error("syntax error in filter expression");
316 static bpf_u_int32 netmask
;
321 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
322 char *buf
, int optimize
, bpf_u_int32 mask
)
331 if (setjmp(top_ctx
)) {
339 snaplen
= pcap_snapshot(p
);
341 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
342 "snaplen of 0 rejects all packets");
346 lex_init(buf
? buf
: "");
354 root
= gen_retblk(snaplen
);
356 if (optimize
&& !no_optimize
) {
359 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
360 bpf_error("expression rejects all packets");
362 program
->bf_insns
= icode_to_fcode(root
, &len
);
363 program
->bf_len
= len
;
371 * entry point for using the compiler with no pcap open
372 * pass in all the stuff that is needed explicitly instead.
375 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
376 struct bpf_program
*program
,
377 char *buf
, int optimize
, bpf_u_int32 mask
)
382 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
385 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
391 * Clean up a "struct bpf_program" by freeing all the memory allocated
395 pcap_freecode(struct bpf_program
*program
)
398 if (program
->bf_insns
!= NULL
) {
399 free((char *)program
->bf_insns
);
400 program
->bf_insns
= NULL
;
405 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
406 * which of the jt and jf fields has been resolved and which is a pointer
407 * back to another unresolved block (or nil). At least one of the fields
408 * in each block is already resolved.
411 backpatch(list
, target
)
412 struct block
*list
, *target
;
429 * Merge the lists in b0 and b1, using the 'sense' field to indicate
430 * which of jt and jf is the link.
434 struct block
*b0
, *b1
;
436 register struct block
**p
= &b0
;
438 /* Find end of list. */
440 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
442 /* Concatenate the lists. */
450 backpatch(p
, gen_retblk(snaplen
));
451 p
->sense
= !p
->sense
;
452 backpatch(p
, gen_retblk(0));
458 struct block
*b0
, *b1
;
460 backpatch(b0
, b1
->head
);
461 b0
->sense
= !b0
->sense
;
462 b1
->sense
= !b1
->sense
;
464 b1
->sense
= !b1
->sense
;
470 struct block
*b0
, *b1
;
472 b0
->sense
= !b0
->sense
;
473 backpatch(b0
, b1
->head
);
474 b0
->sense
= !b0
->sense
;
483 b
->sense
= !b
->sense
;
486 static struct block
*
487 gen_cmp(offset
, size
, v
)
494 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
497 b
= new_block(JMP(BPF_JEQ
));
504 static struct block
*
505 gen_cmp_gt(offset
, size
, v
)
512 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
515 b
= new_block(JMP(BPF_JGT
));
522 static struct block
*
523 gen_mcmp(offset
, size
, v
, mask
)
528 struct block
*b
= gen_cmp(offset
, size
, v
);
531 if (mask
!= 0xffffffff) {
532 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
539 static struct block
*
540 gen_bcmp(offset
, size
, v
)
541 register u_int offset
, size
;
542 register const u_char
*v
;
544 register struct block
*b
, *tmp
;
548 register const u_char
*p
= &v
[size
- 4];
549 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
550 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
552 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
559 register const u_char
*p
= &v
[size
- 2];
560 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
562 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
569 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
577 static struct block
*
578 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
579 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
585 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
588 if (mask
!= 0xffffffff) {
589 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
593 b
= new_block(JMP(jtype
));
596 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
602 * Various code constructs need to know the layout of the data link
603 * layer. These variables give the necessary offsets.
607 * This is the offset of the beginning of the MAC-layer header.
608 * It's usually 0, except for ATM LANE.
610 static u_int off_mac
;
613 * "off_linktype" is the offset to information in the link-layer header
614 * giving the packet type.
616 * For Ethernet, it's the offset of the Ethernet type field.
618 * For link-layer types that always use 802.2 headers, it's the
619 * offset of the LLC header.
621 * For PPP, it's the offset of the PPP type field.
623 * For Cisco HDLC, it's the offset of the CHDLC type field.
625 * For BSD loopback, it's the offset of the AF_ value.
627 * For Linux cooked sockets, it's the offset of the type field.
629 * It's set to -1 for no encapsulation, in which case, IP is assumed.
631 static u_int off_linktype
;
634 * TRUE if the link layer includes an ATM pseudo-header.
636 static int is_atm
= 0;
639 * TRUE if "lane" appeared in the filter; it causes us to generate
640 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
642 static int is_lane
= 0;
645 * These are offsets for the ATM pseudo-header.
647 static u_int off_vpi
;
648 static u_int off_vci
;
649 static u_int off_proto
;
652 * This is the offset of the first byte after the ATM pseudo_header,
653 * or -1 if there is no ATM pseudo-header.
655 static u_int off_payload
;
658 * These are offsets to the beginning of the network-layer header.
660 * If the link layer never uses 802.2 LLC:
662 * "off_nl" and "off_nl_nosnap" are the same.
664 * If the link layer always uses 802.2 LLC:
666 * "off_nl" is the offset if there's a SNAP header following
669 * "off_nl_nosnap" is the offset if there's no SNAP header.
671 * If the link layer is Ethernet:
673 * "off_nl" is the offset if the packet is an Ethernet II packet
674 * (we assume no 802.3+802.2+SNAP);
676 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
677 * with an 802.2 header following it.
680 static u_int off_nl_nosnap
;
688 linktype
= pcap_datalink(p
);
690 pcap_fddipad
= p
->fddipad
;
694 * Assume it's not raw ATM with a pseudo-header, for now.
712 off_nl
= 6; /* XXX in reality, variable! */
713 off_nl_nosnap
= 6; /* no 802.2 LLC */
716 case DLT_ARCNET_LINUX
:
718 off_nl
= 8; /* XXX in reality, variable! */
719 off_nl_nosnap
= 8; /* no 802.2 LLC */
724 off_nl
= 14; /* Ethernet II */
725 off_nl_nosnap
= 17; /* 802.3+802.2 */
730 * SLIP doesn't have a link level type. The 16 byte
731 * header is hacked into our SLIP driver.
735 off_nl_nosnap
= 16; /* no 802.2 LLC */
739 /* XXX this may be the same as the DLT_PPP_BSDOS case */
743 off_nl_nosnap
= 24; /* no 802.2 LLC */
750 off_nl_nosnap
= 4; /* no 802.2 LLC */
756 off_nl_nosnap
= 12; /* no 802.2 LLC */
761 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
762 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
765 off_nl_nosnap
= 4; /* no 802.2 LLC */
770 * This does no include the Ethernet header, and
771 * only covers session state.
775 off_nl_nosnap
= 8; /* no 802.2 LLC */
781 off_nl_nosnap
= 24; /* no 802.2 LLC */
786 * FDDI doesn't really have a link-level type field.
787 * We set "off_linktype" to the offset of the LLC header.
789 * To check for Ethernet types, we assume that SSAP = SNAP
790 * is being used and pick out the encapsulated Ethernet type.
791 * XXX - should we generate code to check for SNAP?
795 off_linktype
+= pcap_fddipad
;
797 off_nl
= 21; /* FDDI+802.2+SNAP */
798 off_nl_nosnap
= 16; /* FDDI+802.2 */
800 off_nl
+= pcap_fddipad
;
801 off_nl_nosnap
+= pcap_fddipad
;
807 * Token Ring doesn't really have a link-level type field.
808 * We set "off_linktype" to the offset of the LLC header.
810 * To check for Ethernet types, we assume that SSAP = SNAP
811 * is being used and pick out the encapsulated Ethernet type.
812 * XXX - should we generate code to check for SNAP?
814 * XXX - the header is actually variable-length.
815 * Some various Linux patched versions gave 38
816 * as "off_linktype" and 40 as "off_nl"; however,
817 * if a token ring packet has *no* routing
818 * information, i.e. is not source-routed, the correct
819 * values are 20 and 22, as they are in the vanilla code.
821 * A packet is source-routed iff the uppermost bit
822 * of the first byte of the source address, at an
823 * offset of 8, has the uppermost bit set. If the
824 * packet is source-routed, the total number of bytes
825 * of routing information is 2 plus bits 0x1F00 of
826 * the 16-bit value at an offset of 14 (shifted right
827 * 8 - figure out which byte that is).
830 off_nl
= 22; /* Token Ring+802.2+SNAP */
831 off_nl_nosnap
= 17; /* Token Ring+802.2 */
836 * 802.11 doesn't really have a link-level type field.
837 * We set "off_linktype" to the offset of the LLC header.
839 * To check for Ethernet types, we assume that SSAP = SNAP
840 * is being used and pick out the encapsulated Ethernet type.
841 * XXX - should we generate code to check for SNAP?
843 * XXX - the header is actually variable-length. We
844 * assume a 24-byte link-layer header, as appears in
845 * data frames in networks with no bridges. If the
846 * fromds and tods 802.11 header bits are both set,
847 * it's actually supposed to be 30 bytes.
850 off_nl
= 32; /* 802.11+802.2+SNAP */
851 off_nl_nosnap
= 27; /* 802.11+802.2 */
854 case DLT_PRISM_HEADER
:
856 * Same as 802.11, but with an additional header before
857 * the 802.11 header, containing a bunch of additional
858 * information including radio-level information.
860 * The header is 144 bytes long.
862 * XXX - same variable-length header problem; at least
863 * the Prism header is fixed-length.
865 off_linktype
= 144+24;
866 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
867 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
870 case DLT_IEEE802_11_RADIO_AVS
:
872 * Same as 802.11, but with an additional header before
873 * the 802.11 header, containing a bunch of additional
874 * information including radio-level information.
876 * The header is 64 bytes long, at least in its
877 * current incarnation.
879 * XXX - same variable-length header problem, only
880 * more so; this header is also variable-length,
881 * with the length being the 32-bit big-endian
882 * number at an offset of 4 from the beginning
883 * of the radio header.
885 off_linktype
= 64+24;
886 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
887 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
890 case DLT_IEEE802_11_RADIO
:
892 * Same as 802.11, but with an additional header before
893 * the 802.11 header, containing a bunch of additional
894 * information including radio-level information.
896 * XXX - same variable-length header problem, only
897 * even *more* so; this header is also variable-length,
898 * with the length being the 16-bit number at an offset
899 * of 2 from the beginning of the radio header, and it's
900 * device-dependent (different devices might supply
901 * different amounts of information), so we can't even
902 * assume a fixed length for the current version of the
905 * Therefore, currently, only raw "link[N:M]" filtering is
913 case DLT_ATM_RFC1483
:
914 case DLT_ATM_CLIP
: /* Linux ATM defines this */
916 * assume routed, non-ISO PDUs
917 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
920 off_nl
= 8; /* 802.2+SNAP */
921 off_nl_nosnap
= 3; /* 802.2 */
926 * Full Frontal ATM; you get AALn PDUs with an ATM
930 off_vpi
= SUNATM_VPI_POS
;
931 off_vci
= SUNATM_VCI_POS
;
932 off_proto
= PROTO_POS
;
933 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
934 off_payload
= SUNATM_PKT_BEGIN_POS
;
935 off_linktype
= off_payload
;
936 off_nl
= off_payload
+8; /* 802.2+SNAP */
937 off_nl_nosnap
= off_payload
+3; /* 802.2 */
943 off_nl_nosnap
= 0; /* no 802.2 LLC */
946 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
949 off_nl_nosnap
= 16; /* no 802.2 LLC */
954 * LocalTalk does have a 1-byte type field in the LLAP header,
955 * but really it just indicates whether there is a "short" or
956 * "long" DDP packet following.
960 off_nl_nosnap
= 0; /* no 802.2 LLC */
965 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
966 * link-level type field. We set "off_linktype" to the
967 * offset of the LLC header.
969 * To check for Ethernet types, we assume that SSAP = SNAP
970 * is being used and pick out the encapsulated Ethernet type.
971 * XXX - should we generate code to check for SNAP? RFC
972 * 2625 says SNAP should be used.
975 off_nl
= 24; /* IPFC+802.2+SNAP */
976 off_nl_nosnap
= 19; /* IPFC+802.2 */
981 * XXX - we should set this to handle SNAP-encapsulated
982 * frames (NLPID of 0x80).
986 off_nl_nosnap
= 0; /* no 802.2 LLC */
989 case DLT_APPLE_IP_OVER_IEEE1394
:
992 off_nl_nosnap
= 0; /* no 802.2 LLC */
997 * Currently, only raw "link[N:M]" filtering is supported.
1006 * Currently, only raw "link[N:M]" filtering is supported.
1013 case DLT_SYMANTEC_FIREWALL
:
1015 off_nl
= 44; /* Ethernet II */
1016 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1021 /* XXX read from header? */
1022 off_nl
= PFLOG_HDRLEN
;
1023 off_nl_nosnap
= PFLOG_HDRLEN
;
1026 case DLT_JUNIPER_MLFR
:
1027 case DLT_JUNIPER_MLPPP
:
1033 case DLT_JUNIPER_ATM1
:
1034 off_linktype
= 4; /* in reality variable between 4-8 */
1039 case DLT_JUNIPER_ATM2
:
1040 off_linktype
= 8; /* in reality variable between 8-12 */
1055 * Currently, only raw "link[N:M]" filtering is supported.
1062 bpf_error("unknown data link type %d", linktype
);
1066 static struct block
*
1073 s
= new_stmt(BPF_LD
|BPF_IMM
);
1075 b
= new_block(JMP(BPF_JEQ
));
1081 static inline struct block
*
1084 return gen_uncond(1);
1087 static inline struct block
*
1090 return gen_uncond(0);
1094 * Byte-swap a 32-bit number.
1095 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1096 * big-endian platforms.)
1098 #define SWAPLONG(y) \
1099 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1101 static struct block
*
1102 gen_ether_linktype(proto
)
1105 struct block
*b0
, *b1
;
1111 * OSI protocols always use 802.2 encapsulation.
1112 * XXX - should we check both the DSAP and the
1113 * SSAP, like this, or should we check just the
1116 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1118 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1119 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1124 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1126 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1127 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1131 case LLCSAP_NETBEUI
:
1133 * NetBEUI always uses 802.2 encapsulation.
1134 * XXX - should we check both the DSAP and the
1135 * SSAP, like this, or should we check just the
1138 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1140 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1141 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1149 * Ethernet_II frames, which are Ethernet
1150 * frames with a frame type of ETHERTYPE_IPX;
1152 * Ethernet_802.3 frames, which are 802.3
1153 * frames (i.e., the type/length field is
1154 * a length field, <= ETHERMTU, rather than
1155 * a type field) with the first two bytes
1156 * after the Ethernet/802.3 header being
1159 * Ethernet_802.2 frames, which are 802.3
1160 * frames with an 802.2 LLC header and
1161 * with the IPX LSAP as the DSAP in the LLC
1164 * Ethernet_SNAP frames, which are 802.3
1165 * frames with an LLC header and a SNAP
1166 * header and with an OUI of 0x000000
1167 * (encapsulated Ethernet) and a protocol
1168 * ID of ETHERTYPE_IPX in the SNAP header.
1170 * XXX - should we generate the same code both
1171 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1175 * This generates code to check both for the
1176 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1178 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1179 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1183 * Now we add code to check for SNAP frames with
1184 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1186 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1190 * Now we generate code to check for 802.3
1191 * frames in general.
1193 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1197 * Now add the check for 802.3 frames before the
1198 * check for Ethernet_802.2 and Ethernet_802.3,
1199 * as those checks should only be done on 802.3
1200 * frames, not on Ethernet frames.
1205 * Now add the check for Ethernet_II frames, and
1206 * do that before checking for the other frame
1209 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1213 case ETHERTYPE_ATALK
:
1214 case ETHERTYPE_AARP
:
1216 * EtherTalk (AppleTalk protocols on Ethernet link
1217 * layer) may use 802.2 encapsulation.
1221 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1222 * we check for an Ethernet type field less than
1223 * 1500, which means it's an 802.3 length field.
1225 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1229 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1230 * SNAP packets with an organization code of
1231 * 0x080007 (Apple, for Appletalk) and a protocol
1232 * type of ETHERTYPE_ATALK (Appletalk).
1234 * 802.2-encapsulated ETHERTYPE_AARP packets are
1235 * SNAP packets with an organization code of
1236 * 0x000000 (encapsulated Ethernet) and a protocol
1237 * type of ETHERTYPE_AARP (Appletalk ARP).
1239 if (proto
== ETHERTYPE_ATALK
)
1240 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1241 else /* proto == ETHERTYPE_AARP */
1242 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1246 * Check for Ethernet encapsulation (Ethertalk
1247 * phase 1?); we just check for the Ethernet
1250 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1256 if (proto
<= ETHERMTU
) {
1258 * This is an LLC SAP value, so the frames
1259 * that match would be 802.2 frames.
1260 * Check that the frame is an 802.2 frame
1261 * (i.e., that the length/type field is
1262 * a length field, <= ETHERMTU) and
1263 * then check the DSAP.
1265 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1267 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1272 * This is an Ethernet type, so compare
1273 * the length/type field with it (if
1274 * the frame is an 802.2 frame, the length
1275 * field will be <= ETHERMTU, and, as
1276 * "proto" is > ETHERMTU, this test
1277 * will fail and the frame won't match,
1278 * which is what we want).
1280 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1285 static struct block
*
1286 gen_linux_sll_linktype(proto
)
1289 struct block
*b0
, *b1
;
1294 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1295 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1296 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1302 * OSI protocols always use 802.2 encapsulation.
1303 * XXX - should we check both the DSAP and the
1304 * SSAP, like this, or should we check just the
1307 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1308 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1309 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1313 case LLCSAP_NETBEUI
:
1315 * NetBEUI always uses 802.2 encapsulation.
1316 * XXX - should we check both the DSAP and the
1317 * LSAP, like this, or should we check just the
1320 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1321 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1322 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1328 * Ethernet_II frames, which are Ethernet
1329 * frames with a frame type of ETHERTYPE_IPX;
1331 * Ethernet_802.3 frames, which have a frame
1332 * type of LINUX_SLL_P_802_3;
1334 * Ethernet_802.2 frames, which are 802.3
1335 * frames with an 802.2 LLC header (i.e, have
1336 * a frame type of LINUX_SLL_P_802_2) and
1337 * with the IPX LSAP as the DSAP in the LLC
1340 * Ethernet_SNAP frames, which are 802.3
1341 * frames with an LLC header and a SNAP
1342 * header and with an OUI of 0x000000
1343 * (encapsulated Ethernet) and a protocol
1344 * ID of ETHERTYPE_IPX in the SNAP header.
1346 * First, do the checks on LINUX_SLL_P_802_2
1347 * frames; generate the check for either
1348 * Ethernet_802.2 or Ethernet_SNAP frames, and
1349 * then put a check for LINUX_SLL_P_802_2 frames
1352 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1353 (bpf_int32
)LLCSAP_IPX
);
1354 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1357 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1361 * Now check for 802.3 frames and OR that with
1362 * the previous test.
1364 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1368 * Now add the check for Ethernet_II frames, and
1369 * do that before checking for the other frame
1372 b0
= gen_cmp(off_linktype
, BPF_H
,
1373 (bpf_int32
)ETHERTYPE_IPX
);
1377 case ETHERTYPE_ATALK
:
1378 case ETHERTYPE_AARP
:
1380 * EtherTalk (AppleTalk protocols on Ethernet link
1381 * layer) may use 802.2 encapsulation.
1385 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1386 * we check for the 802.2 protocol type in the
1387 * "Ethernet type" field.
1389 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1392 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1393 * SNAP packets with an organization code of
1394 * 0x080007 (Apple, for Appletalk) and a protocol
1395 * type of ETHERTYPE_ATALK (Appletalk).
1397 * 802.2-encapsulated ETHERTYPE_AARP packets are
1398 * SNAP packets with an organization code of
1399 * 0x000000 (encapsulated Ethernet) and a protocol
1400 * type of ETHERTYPE_AARP (Appletalk ARP).
1402 if (proto
== ETHERTYPE_ATALK
)
1403 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1405 else /* proto == ETHERTYPE_AARP */
1406 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1411 * Check for Ethernet encapsulation (Ethertalk
1412 * phase 1?); we just check for the Ethernet
1415 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1421 if (proto
<= ETHERMTU
) {
1423 * This is an LLC SAP value, so the frames
1424 * that match would be 802.2 frames.
1425 * Check for the 802.2 protocol type
1426 * in the "Ethernet type" field, and
1427 * then check the DSAP.
1429 b0
= gen_cmp(off_linktype
, BPF_H
,
1431 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1437 * This is an Ethernet type, so compare
1438 * the length/type field with it (if
1439 * the frame is an 802.2 frame, the length
1440 * field will be <= ETHERMTU, and, as
1441 * "proto" is > ETHERMTU, this test
1442 * will fail and the frame won't match,
1443 * which is what we want).
1445 return gen_cmp(off_linktype
, BPF_H
,
1451 static struct block
*
1455 struct block
*b0
, *b1
, *b2
;
1460 return gen_ether_linktype(proto
);
1468 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1472 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1478 case DLT_IEEE802_11
:
1479 case DLT_PRISM_HEADER
:
1480 case DLT_IEEE802_11_RADIO
:
1483 case DLT_ATM_RFC1483
:
1485 case DLT_IP_OVER_FC
:
1486 return gen_llc(proto
);
1492 * If "is_lane" is set, check for a LANE-encapsulated
1493 * version of this protocol, otherwise check for an
1494 * LLC-encapsulated version of this protocol.
1496 * We assume LANE means Ethernet, not Token Ring.
1500 * Check that the packet doesn't begin with an
1501 * LE Control marker. (We've already generated
1504 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1508 * Now generate an Ethernet test.
1510 b1
= gen_ether_linktype(proto
);
1515 * Check for LLC encapsulation and then check the
1518 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1519 b1
= gen_llc(proto
);
1525 return gen_linux_sll_linktype(proto
);
1530 case DLT_SLIP_BSDOS
:
1533 * These types don't provide any type field; packets
1536 * XXX - for IPv4, check for a version number of 4, and,
1537 * for IPv6, check for a version number of 6?
1543 case ETHERTYPE_IPV6
:
1545 return gen_true(); /* always true */
1548 return gen_false(); /* always false */
1555 case DLT_PPP_SERIAL
:
1558 * We use Ethernet protocol types inside libpcap;
1559 * map them to the corresponding PPP protocol types.
1568 case ETHERTYPE_IPV6
:
1577 case ETHERTYPE_ATALK
:
1591 * I'm assuming the "Bridging PDU"s that go
1592 * over PPP are Spanning Tree Protocol
1606 * We use Ethernet protocol types inside libpcap;
1607 * map them to the corresponding PPP protocol types.
1612 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1613 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1615 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1620 case ETHERTYPE_IPV6
:
1630 case ETHERTYPE_ATALK
:
1644 * I'm assuming the "Bridging PDU"s that go
1645 * over PPP are Spanning Tree Protocol
1661 * For DLT_NULL, the link-layer header is a 32-bit
1662 * word containing an AF_ value in *host* byte order,
1663 * and for DLT_ENC, the link-layer header begins
1664 * with a 32-bit work containing an AF_ value in
1667 * In addition, if we're reading a saved capture file,
1668 * the host byte order in the capture may not be the
1669 * same as the host byte order on this machine.
1671 * For DLT_LOOP, the link-layer header is a 32-bit
1672 * word containing an AF_ value in *network* byte order.
1674 * XXX - AF_ values may, unfortunately, be platform-
1675 * dependent; for example, FreeBSD's AF_INET6 is 24
1676 * whilst NetBSD's and OpenBSD's is 26.
1678 * This means that, when reading a capture file, just
1679 * checking for our AF_INET6 value won't work if the
1680 * capture file came from another OS.
1689 case ETHERTYPE_IPV6
:
1696 * Not a type on which we support filtering.
1697 * XXX - support those that have AF_ values
1698 * #defined on this platform, at least?
1703 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1705 * The AF_ value is in host byte order, but
1706 * the BPF interpreter will convert it to
1707 * network byte order.
1709 * If this is a save file, and it's from a
1710 * machine with the opposite byte order to
1711 * ours, we byte-swap the AF_ value.
1713 * Then we run it through "htonl()", and
1714 * generate code to compare against the result.
1716 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1717 bpf_pcap
->sf
.swapped
)
1718 proto
= SWAPLONG(proto
);
1719 proto
= htonl(proto
);
1721 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1725 * af field is host byte order in contrast to the rest of
1728 if (proto
== ETHERTYPE_IP
)
1729 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1730 (bpf_int32
)AF_INET
));
1732 else if (proto
== ETHERTYPE_IPV6
)
1733 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1734 (bpf_int32
)AF_INET6
));
1742 case DLT_ARCNET_LINUX
:
1744 * XXX should we check for first fragment if the protocol
1753 case ETHERTYPE_IPV6
:
1754 return (gen_cmp(off_linktype
, BPF_B
,
1755 (bpf_int32
)ARCTYPE_INET6
));
1759 b0
= gen_cmp(off_linktype
, BPF_B
,
1760 (bpf_int32
)ARCTYPE_IP
);
1761 b1
= gen_cmp(off_linktype
, BPF_B
,
1762 (bpf_int32
)ARCTYPE_IP_OLD
);
1767 b0
= gen_cmp(off_linktype
, BPF_B
,
1768 (bpf_int32
)ARCTYPE_ARP
);
1769 b1
= gen_cmp(off_linktype
, BPF_B
,
1770 (bpf_int32
)ARCTYPE_ARP_OLD
);
1774 case ETHERTYPE_REVARP
:
1775 return (gen_cmp(off_linktype
, BPF_B
,
1776 (bpf_int32
)ARCTYPE_REVARP
));
1778 case ETHERTYPE_ATALK
:
1779 return (gen_cmp(off_linktype
, BPF_B
,
1780 (bpf_int32
)ARCTYPE_ATALK
));
1787 case ETHERTYPE_ATALK
:
1797 * XXX - assumes a 2-byte Frame Relay header with
1798 * DLCI and flags. What if the address is longer?
1804 * Check for the special NLPID for IP.
1806 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1809 case ETHERTYPE_IPV6
:
1811 * Check for the special NLPID for IPv6.
1813 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1818 * Check for several OSI protocols.
1820 * Frame Relay packets typically have an OSI
1821 * NLPID at the beginning; we check for each
1824 * What we check for is the NLPID and a frame
1825 * control field of UI, i.e. 0x03 followed
1828 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1829 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1830 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1841 case DLT_JUNIPER_MLFR
:
1842 case DLT_JUNIPER_MLPPP
:
1843 case DLT_JUNIPER_ATM1
:
1844 case DLT_JUNIPER_ATM2
:
1845 /* just lets verify the magic number for now -
1846 * on ATM we may have up to 6 different encapsulations on the wire
1847 * and need a lot of heuristics to figure out that the payload
1850 * FIXME encapsulation specific BPF_ filters
1852 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1854 case DLT_LINUX_IRDA
:
1855 bpf_error("IrDA link-layer type filtering not implemented");
1858 bpf_error("DOCSIS link-layer type filtering not implemented");
1861 bpf_error("LAPD link-layer type filtering not implemented");
1865 * All the types that have no encapsulation should either be
1866 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1867 * all packets are IP packets, or should be handled in some
1868 * special case, if none of them are (if some are and some
1869 * aren't, the lack of encapsulation is a problem, as we'd
1870 * have to find some other way of determining the packet type).
1872 * Therefore, if "off_linktype" is -1, there's an error.
1874 if (off_linktype
== (u_int
)-1)
1878 * Any type not handled above should always have an Ethernet
1879 * type at an offset of "off_linktype". (PPP is partially
1880 * handled above - the protocol type is mapped from the
1881 * Ethernet and LLC types we use internally to the corresponding
1882 * PPP type - but the PPP type is always specified by a value
1883 * at "off_linktype", so we don't have to do the code generation
1886 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1890 * Check for an LLC SNAP packet with a given organization code and
1891 * protocol type; we check the entire contents of the 802.2 LLC and
1892 * snap headers, checking for DSAP and SSAP of SNAP and a control
1893 * field of 0x03 in the LLC header, and for the specified organization
1894 * code and protocol type in the SNAP header.
1896 static struct block
*
1897 gen_snap(orgcode
, ptype
, offset
)
1898 bpf_u_int32 orgcode
;
1902 u_char snapblock
[8];
1904 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1905 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1906 snapblock
[2] = 0x03; /* control = UI */
1907 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1908 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1909 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1910 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1911 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1912 return gen_bcmp(offset
, 8, snapblock
);
1916 * Check for a given protocol value assuming an 802.2 LLC header.
1918 static struct block
*
1923 * XXX - handle token-ring variable-length header.
1928 return gen_cmp(off_linktype
, BPF_H
, (long)
1929 ((LLCSAP_IP
<< 8) | LLCSAP_IP
));
1932 return gen_cmp(off_linktype
, BPF_H
, (long)
1933 ((LLCSAP_ISONS
<< 8) | LLCSAP_ISONS
));
1935 case LLCSAP_NETBEUI
:
1936 return gen_cmp(off_linktype
, BPF_H
, (long)
1937 ((LLCSAP_NETBEUI
<< 8) | LLCSAP_NETBEUI
));
1941 * XXX - are there ever SNAP frames for IPX on
1942 * non-Ethernet 802.x networks?
1944 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1946 case ETHERTYPE_ATALK
:
1948 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1949 * SNAP packets with an organization code of
1950 * 0x080007 (Apple, for Appletalk) and a protocol
1951 * type of ETHERTYPE_ATALK (Appletalk).
1953 * XXX - check for an organization code of
1954 * encapsulated Ethernet as well?
1956 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1960 * XXX - we don't have to check for IPX 802.3
1961 * here, but should we check for the IPX Ethertype?
1963 if (proto
<= ETHERMTU
) {
1965 * This is an LLC SAP value, so check
1968 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1971 * This is an Ethernet type; we assume that it's
1972 * unlikely that it'll appear in the right place
1973 * at random, and therefore check only the
1974 * location that would hold the Ethernet type
1975 * in a SNAP frame with an organization code of
1976 * 0x000000 (encapsulated Ethernet).
1978 * XXX - if we were to check for the SNAP DSAP and
1979 * LSAP, as per XXX, and were also to check for an
1980 * organization code of 0x000000 (encapsulated
1981 * Ethernet), we'd do
1983 * return gen_snap(0x000000, proto,
1986 * here; for now, we don't, as per the above.
1987 * I don't know whether it's worth the extra CPU
1988 * time to do the right check or not.
1990 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
1995 static struct block
*
1996 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2000 u_int src_off
, dst_off
;
2002 struct block
*b0
, *b1
;
2016 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2017 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2023 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2024 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2031 b0
= gen_linktype(proto
);
2032 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2038 static struct block
*
2039 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2040 struct in6_addr
*addr
;
2041 struct in6_addr
*mask
;
2043 u_int src_off
, dst_off
;
2045 struct block
*b0
, *b1
;
2060 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2061 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2067 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2068 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2075 /* this order is important */
2076 a
= (u_int32_t
*)addr
;
2077 m
= (u_int32_t
*)mask
;
2078 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2079 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2081 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2083 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2085 b0
= gen_linktype(proto
);
2091 static struct block
*
2092 gen_ehostop(eaddr
, dir
)
2093 register const u_char
*eaddr
;
2096 register struct block
*b0
, *b1
;
2100 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2103 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2106 b0
= gen_ehostop(eaddr
, Q_SRC
);
2107 b1
= gen_ehostop(eaddr
, Q_DST
);
2113 b0
= gen_ehostop(eaddr
, Q_SRC
);
2114 b1
= gen_ehostop(eaddr
, Q_DST
);
2123 * Like gen_ehostop, but for DLT_FDDI
2125 static struct block
*
2126 gen_fhostop(eaddr
, dir
)
2127 register const u_char
*eaddr
;
2130 struct block
*b0
, *b1
;
2135 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2137 return gen_bcmp(6 + 1, 6, eaddr
);
2142 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2144 return gen_bcmp(0 + 1, 6, eaddr
);
2148 b0
= gen_fhostop(eaddr
, Q_SRC
);
2149 b1
= gen_fhostop(eaddr
, Q_DST
);
2155 b0
= gen_fhostop(eaddr
, Q_SRC
);
2156 b1
= gen_fhostop(eaddr
, Q_DST
);
2165 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2167 static struct block
*
2168 gen_thostop(eaddr
, dir
)
2169 register const u_char
*eaddr
;
2172 register struct block
*b0
, *b1
;
2176 return gen_bcmp(8, 6, eaddr
);
2179 return gen_bcmp(2, 6, eaddr
);
2182 b0
= gen_thostop(eaddr
, Q_SRC
);
2183 b1
= gen_thostop(eaddr
, Q_DST
);
2189 b0
= gen_thostop(eaddr
, Q_SRC
);
2190 b1
= gen_thostop(eaddr
, Q_DST
);
2199 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2201 static struct block
*
2202 gen_wlanhostop(eaddr
, dir
)
2203 register const u_char
*eaddr
;
2206 register struct block
*b0
, *b1
, *b2
;
2207 register struct slist
*s
;
2214 * For control frames, there is no SA.
2216 * For management frames, SA is at an
2217 * offset of 10 from the beginning of
2220 * For data frames, SA is at an offset
2221 * of 10 from the beginning of the packet
2222 * if From DS is clear, at an offset of
2223 * 16 from the beginning of the packet
2224 * if From DS is set and To DS is clear,
2225 * and an offset of 24 from the beginning
2226 * of the packet if From DS is set and To DS
2231 * Generate the tests to be done for data frames
2234 * First, check for To DS set, i.e. check "link[1] & 0x01".
2236 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2238 b1
= new_block(JMP(BPF_JSET
));
2239 b1
->s
.k
= 0x01; /* To DS */
2243 * If To DS is set, the SA is at 24.
2245 b0
= gen_bcmp(24, 6, eaddr
);
2249 * Now, check for To DS not set, i.e. check
2250 * "!(link[1] & 0x01)".
2252 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2254 b2
= new_block(JMP(BPF_JSET
));
2255 b2
->s
.k
= 0x01; /* To DS */
2260 * If To DS is not set, the SA is at 16.
2262 b1
= gen_bcmp(16, 6, eaddr
);
2266 * Now OR together the last two checks. That gives
2267 * the complete set of checks for data frames with
2273 * Now check for From DS being set, and AND that with
2274 * the ORed-together checks.
2276 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2278 b1
= new_block(JMP(BPF_JSET
));
2279 b1
->s
.k
= 0x02; /* From DS */
2284 * Now check for data frames with From DS not set.
2286 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2288 b2
= new_block(JMP(BPF_JSET
));
2289 b2
->s
.k
= 0x02; /* From DS */
2294 * If From DS isn't set, the SA is at 10.
2296 b1
= gen_bcmp(10, 6, eaddr
);
2300 * Now OR together the checks for data frames with
2301 * From DS not set and for data frames with From DS
2302 * set; that gives the checks done for data frames.
2307 * Now check for a data frame.
2308 * I.e, check "link[0] & 0x08".
2310 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2312 b1
= new_block(JMP(BPF_JSET
));
2317 * AND that with the checks done for data frames.
2322 * If the high-order bit of the type value is 0, this
2323 * is a management frame.
2324 * I.e, check "!(link[0] & 0x08)".
2326 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2328 b2
= new_block(JMP(BPF_JSET
));
2334 * For management frames, the SA is at 10.
2336 b1
= gen_bcmp(10, 6, eaddr
);
2340 * OR that with the checks done for data frames.
2341 * That gives the checks done for management and
2347 * If the low-order bit of the type value is 1,
2348 * this is either a control frame or a frame
2349 * with a reserved type, and thus not a
2352 * I.e., check "!(link[0] & 0x04)".
2354 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2356 b1
= new_block(JMP(BPF_JSET
));
2362 * AND that with the checks for data and management
2372 * For control frames, there is no DA.
2374 * For management frames, DA is at an
2375 * offset of 4 from the beginning of
2378 * For data frames, DA is at an offset
2379 * of 4 from the beginning of the packet
2380 * if To DS is clear and at an offset of
2381 * 16 from the beginning of the packet
2386 * Generate the tests to be done for data frames.
2388 * First, check for To DS set, i.e. "link[1] & 0x01".
2390 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2392 b1
= new_block(JMP(BPF_JSET
));
2393 b1
->s
.k
= 0x01; /* To DS */
2397 * If To DS is set, the DA is at 16.
2399 b0
= gen_bcmp(16, 6, eaddr
);
2403 * Now, check for To DS not set, i.e. check
2404 * "!(link[1] & 0x01)".
2406 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2408 b2
= new_block(JMP(BPF_JSET
));
2409 b2
->s
.k
= 0x01; /* To DS */
2414 * If To DS is not set, the DA is at 4.
2416 b1
= gen_bcmp(4, 6, eaddr
);
2420 * Now OR together the last two checks. That gives
2421 * the complete set of checks for data frames.
2426 * Now check for a data frame.
2427 * I.e, check "link[0] & 0x08".
2429 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2431 b1
= new_block(JMP(BPF_JSET
));
2436 * AND that with the checks done for data frames.
2441 * If the high-order bit of the type value is 0, this
2442 * is a management frame.
2443 * I.e, check "!(link[0] & 0x08)".
2445 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2447 b2
= new_block(JMP(BPF_JSET
));
2453 * For management frames, the DA is at 4.
2455 b1
= gen_bcmp(4, 6, eaddr
);
2459 * OR that with the checks done for data frames.
2460 * That gives the checks done for management and
2466 * If the low-order bit of the type value is 1,
2467 * this is either a control frame or a frame
2468 * with a reserved type, and thus not a
2471 * I.e., check "!(link[0] & 0x04)".
2473 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2475 b1
= new_block(JMP(BPF_JSET
));
2481 * AND that with the checks for data and management
2488 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2489 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2495 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2496 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2505 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2506 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2507 * as the RFC states.)
2509 static struct block
*
2510 gen_ipfchostop(eaddr
, dir
)
2511 register const u_char
*eaddr
;
2514 register struct block
*b0
, *b1
;
2518 return gen_bcmp(10, 6, eaddr
);
2521 return gen_bcmp(2, 6, eaddr
);
2524 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2525 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2531 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2532 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2541 * This is quite tricky because there may be pad bytes in front of the
2542 * DECNET header, and then there are two possible data packet formats that
2543 * carry both src and dst addresses, plus 5 packet types in a format that
2544 * carries only the src node, plus 2 types that use a different format and
2545 * also carry just the src node.
2549 * Instead of doing those all right, we just look for data packets with
2550 * 0 or 1 bytes of padding. If you want to look at other packets, that
2551 * will require a lot more hacking.
2553 * To add support for filtering on DECNET "areas" (network numbers)
2554 * one would want to add a "mask" argument to this routine. That would
2555 * make the filter even more inefficient, although one could be clever
2556 * and not generate masking instructions if the mask is 0xFFFF.
2558 static struct block
*
2559 gen_dnhostop(addr
, dir
, base_off
)
2564 struct block
*b0
, *b1
, *b2
, *tmp
;
2565 u_int offset_lh
; /* offset if long header is received */
2566 u_int offset_sh
; /* offset if short header is received */
2571 offset_sh
= 1; /* follows flags */
2572 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2576 offset_sh
= 3; /* follows flags, dstnode */
2577 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2581 /* Inefficient because we do our Calvinball dance twice */
2582 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2583 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2589 /* Inefficient because we do our Calvinball dance twice */
2590 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2591 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2596 bpf_error("ISO host filtering not implemented");
2601 b0
= gen_linktype(ETHERTYPE_DN
);
2602 /* Check for pad = 1, long header case */
2603 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2604 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2605 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2606 BPF_H
, (bpf_int32
)ntohs(addr
));
2608 /* Check for pad = 0, long header case */
2609 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2610 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2613 /* Check for pad = 1, short header case */
2614 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2615 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2616 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2617 BPF_H
, (bpf_int32
)ntohs(addr
));
2620 /* Check for pad = 0, short header case */
2621 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2622 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2626 /* Combine with test for linktype */
2631 static struct block
*
2632 gen_host(addr
, mask
, proto
, dir
)
2638 struct block
*b0
, *b1
;
2643 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2644 if (off_linktype
!= (u_int
)-1) {
2645 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2647 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2653 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2654 off_nl
+ 12, off_nl
+ 16);
2657 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2658 off_nl
+ 14, off_nl
+ 24);
2661 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2662 off_nl
+ 14, off_nl
+ 24);
2665 bpf_error("'tcp' modifier applied to host");
2668 bpf_error("'sctp' modifier applied to host");
2671 bpf_error("'udp' modifier applied to host");
2674 bpf_error("'icmp' modifier applied to host");
2677 bpf_error("'igmp' modifier applied to host");
2680 bpf_error("'igrp' modifier applied to host");
2683 bpf_error("'pim' modifier applied to host");
2686 bpf_error("'vrrp' modifier applied to host");
2689 bpf_error("ATALK host filtering not implemented");
2692 bpf_error("AARP host filtering not implemented");
2695 return gen_dnhostop(addr
, dir
, off_nl
);
2698 bpf_error("SCA host filtering not implemented");
2701 bpf_error("LAT host filtering not implemented");
2704 bpf_error("MOPDL host filtering not implemented");
2707 bpf_error("MOPRC host filtering not implemented");
2711 bpf_error("'ip6' modifier applied to ip host");
2714 bpf_error("'icmp6' modifier applied to host");
2718 bpf_error("'ah' modifier applied to host");
2721 bpf_error("'esp' modifier applied to host");
2724 bpf_error("ISO host filtering not implemented");
2727 bpf_error("'esis' modifier applied to host");
2730 bpf_error("'isis' modifier applied to host");
2733 bpf_error("'clnp' modifier applied to host");
2736 bpf_error("'stp' modifier applied to host");
2739 bpf_error("IPX host filtering not implemented");
2742 bpf_error("'netbeui' modifier applied to host");
2751 static struct block
*
2752 gen_host6(addr
, mask
, proto
, dir
)
2753 struct in6_addr
*addr
;
2754 struct in6_addr
*mask
;
2761 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2764 bpf_error("'ip' modifier applied to ip6 host");
2767 bpf_error("'rarp' modifier applied to ip6 host");
2770 bpf_error("'arp' modifier applied to ip6 host");
2773 bpf_error("'sctp' modifier applied to host");
2776 bpf_error("'tcp' modifier applied to host");
2779 bpf_error("'udp' modifier applied to host");
2782 bpf_error("'icmp' modifier applied to host");
2785 bpf_error("'igmp' modifier applied to host");
2788 bpf_error("'igrp' modifier applied to host");
2791 bpf_error("'pim' modifier applied to host");
2794 bpf_error("'vrrp' modifier applied to host");
2797 bpf_error("ATALK host filtering not implemented");
2800 bpf_error("AARP host filtering not implemented");
2803 bpf_error("'decnet' modifier applied to ip6 host");
2806 bpf_error("SCA host filtering not implemented");
2809 bpf_error("LAT host filtering not implemented");
2812 bpf_error("MOPDL host filtering not implemented");
2815 bpf_error("MOPRC host filtering not implemented");
2818 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2819 off_nl
+ 8, off_nl
+ 24);
2822 bpf_error("'icmp6' modifier applied to host");
2825 bpf_error("'ah' modifier applied to host");
2828 bpf_error("'esp' modifier applied to host");
2831 bpf_error("ISO host filtering not implemented");
2834 bpf_error("'esis' modifier applied to host");
2837 bpf_error("'isis' modifier applied to host");
2840 bpf_error("'clnp' modifier applied to host");
2843 bpf_error("'stp' modifier applied to host");
2846 bpf_error("IPX host filtering not implemented");
2849 bpf_error("'netbeui' modifier applied to host");
2859 static struct block
*
2860 gen_gateway(eaddr
, alist
, proto
, dir
)
2861 const u_char
*eaddr
;
2862 bpf_u_int32
**alist
;
2866 struct block
*b0
, *b1
, *tmp
;
2869 bpf_error("direction applied to 'gateway'");
2876 if (linktype
== DLT_EN10MB
)
2877 b0
= gen_ehostop(eaddr
, Q_OR
);
2878 else if (linktype
== DLT_FDDI
)
2879 b0
= gen_fhostop(eaddr
, Q_OR
);
2880 else if (linktype
== DLT_IEEE802
)
2881 b0
= gen_thostop(eaddr
, Q_OR
);
2882 else if (linktype
== DLT_IEEE802_11
)
2883 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2884 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2886 * Check that the packet doesn't begin with an
2887 * LE Control marker. (We've already generated
2890 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2894 * Now check the MAC address.
2896 b0
= gen_ehostop(eaddr
, Q_OR
);
2898 } else if (linktype
== DLT_IP_OVER_FC
)
2899 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2902 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2904 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2906 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2914 bpf_error("illegal modifier of 'gateway'");
2920 gen_proto_abbrev(proto
)
2929 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2931 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2937 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2939 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2945 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2947 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2953 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2956 #ifndef IPPROTO_IGMP
2957 #define IPPROTO_IGMP 2
2961 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2964 #ifndef IPPROTO_IGRP
2965 #define IPPROTO_IGRP 9
2968 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2972 #define IPPROTO_PIM 103
2976 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2978 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2983 #ifndef IPPROTO_VRRP
2984 #define IPPROTO_VRRP 112
2988 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
2992 b1
= gen_linktype(ETHERTYPE_IP
);
2996 b1
= gen_linktype(ETHERTYPE_ARP
);
3000 b1
= gen_linktype(ETHERTYPE_REVARP
);
3004 bpf_error("link layer applied in wrong context");
3007 b1
= gen_linktype(ETHERTYPE_ATALK
);
3011 b1
= gen_linktype(ETHERTYPE_AARP
);
3015 b1
= gen_linktype(ETHERTYPE_DN
);
3019 b1
= gen_linktype(ETHERTYPE_SCA
);
3023 b1
= gen_linktype(ETHERTYPE_LAT
);
3027 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3031 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3036 b1
= gen_linktype(ETHERTYPE_IPV6
);
3039 #ifndef IPPROTO_ICMPV6
3040 #define IPPROTO_ICMPV6 58
3043 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3048 #define IPPROTO_AH 51
3051 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3053 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3059 #define IPPROTO_ESP 50
3062 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3064 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3070 b1
= gen_linktype(LLCSAP_ISONS
);
3074 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3078 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3081 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3082 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3083 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3085 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3087 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3089 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3093 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3094 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3095 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3097 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3099 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3101 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3105 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3106 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3107 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3109 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3114 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3115 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3120 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3121 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3123 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3125 b0
= gen_proto(ISIS_L2_PSNP
, 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
);
3136 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3137 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3142 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3146 b1
= gen_linktype(LLCSAP_8021D
);
3150 b1
= gen_linktype(LLCSAP_IPX
);
3154 b1
= gen_linktype(LLCSAP_NETBEUI
);
3163 static struct block
*
3170 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3171 s
->s
.k
= off_nl
+ 6;
3172 b
= new_block(JMP(BPF_JSET
));
3180 static struct block
*
3181 gen_portatom(off
, v
)
3188 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3191 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3192 s
->next
->s
.k
= off_nl
+ off
;
3194 b
= new_block(JMP(BPF_JEQ
));
3202 static struct block
*
3203 gen_portatom6(off
, v
)
3207 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3212 gen_portop(port
, proto
, dir
)
3213 int port
, proto
, dir
;
3215 struct block
*b0
, *b1
, *tmp
;
3217 /* ip proto 'proto' */
3218 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3224 b1
= gen_portatom(0, (bpf_int32
)port
);
3228 b1
= gen_portatom(2, (bpf_int32
)port
);
3233 tmp
= gen_portatom(0, (bpf_int32
)port
);
3234 b1
= gen_portatom(2, (bpf_int32
)port
);
3239 tmp
= gen_portatom(0, (bpf_int32
)port
);
3240 b1
= gen_portatom(2, (bpf_int32
)port
);
3252 static struct block
*
3253 gen_port(port
, ip_proto
, dir
)
3258 struct block
*b0
, *b1
, *tmp
;
3263 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3264 * not LLC encapsulation with LLCSAP_IP.
3266 * For IEEE 802 networks - which includes 802.5 token ring
3267 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3268 * says that SNAP encapsulation is used, not LLC encapsulation
3271 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3272 * RFC 2225 say that SNAP encapsulation is used, not LLC
3273 * encapsulation with LLCSAP_IP.
3275 * So we always check for ETHERTYPE_IP.
3277 b0
= gen_linktype(ETHERTYPE_IP
);
3283 b1
= gen_portop(port
, ip_proto
, dir
);
3287 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3288 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3290 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3303 gen_portop6(port
, proto
, dir
)
3304 int port
, proto
, dir
;
3306 struct block
*b0
, *b1
, *tmp
;
3308 /* ip proto 'proto' */
3309 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3313 b1
= gen_portatom6(0, (bpf_int32
)port
);
3317 b1
= gen_portatom6(2, (bpf_int32
)port
);
3322 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3323 b1
= gen_portatom6(2, (bpf_int32
)port
);
3328 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3329 b1
= gen_portatom6(2, (bpf_int32
)port
);
3341 static struct block
*
3342 gen_port6(port
, ip_proto
, dir
)
3347 struct block
*b0
, *b1
, *tmp
;
3349 /* ether proto ip */
3350 b0
= gen_linktype(ETHERTYPE_IPV6
);
3356 b1
= gen_portop6(port
, ip_proto
, dir
);
3360 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3361 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3363 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3376 lookup_proto(name
, proto
)
3377 register const char *name
;
3387 v
= pcap_nametoproto(name
);
3388 if (v
== PROTO_UNDEF
)
3389 bpf_error("unknown ip proto '%s'", name
);
3393 /* XXX should look up h/w protocol type based on linktype */
3394 v
= pcap_nametoeproto(name
);
3395 if (v
== PROTO_UNDEF
) {
3396 v
= pcap_nametollc(name
);
3397 if (v
== PROTO_UNDEF
)
3398 bpf_error("unknown ether proto '%s'", name
);
3403 if (strcmp(name
, "esis") == 0)
3405 else if (strcmp(name
, "isis") == 0)
3407 else if (strcmp(name
, "clnp") == 0)
3410 bpf_error("unknown osi proto '%s'", name
);
3430 static struct block
*
3431 gen_protochain(v
, proto
, dir
)
3436 #ifdef NO_PROTOCHAIN
3437 return gen_proto(v
, proto
, dir
);
3439 struct block
*b0
, *b
;
3440 struct slist
*s
[100];
3441 int fix2
, fix3
, fix4
, fix5
;
3442 int ahcheck
, again
, end
;
3444 int reg2
= alloc_reg();
3446 memset(s
, 0, sizeof(s
));
3447 fix2
= fix3
= fix4
= fix5
= 0;
3454 b0
= gen_protochain(v
, Q_IP
, dir
);
3455 b
= gen_protochain(v
, Q_IPV6
, dir
);
3459 bpf_error("bad protocol applied for 'protochain'");
3463 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3466 * s[0] is a dummy entry to protect other BPF insn from damaged
3467 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3468 * hard to find interdependency made by jump table fixup.
3471 s
[i
] = new_stmt(0); /*dummy*/
3476 b0
= gen_linktype(ETHERTYPE_IP
);
3479 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3480 s
[i
]->s
.k
= off_nl
+ 9;
3482 /* X = ip->ip_hl << 2 */
3483 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3489 b0
= gen_linktype(ETHERTYPE_IPV6
);
3491 /* A = ip6->ip_nxt */
3492 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3493 s
[i
]->s
.k
= off_nl
+ 6;
3495 /* X = sizeof(struct ip6_hdr) */
3496 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3502 bpf_error("unsupported proto to gen_protochain");
3506 /* again: if (A == v) goto end; else fall through; */
3508 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3510 s
[i
]->s
.jt
= NULL
; /*later*/
3511 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3515 #ifndef IPPROTO_NONE
3516 #define IPPROTO_NONE 59
3518 /* if (A == IPPROTO_NONE) goto end */
3519 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*/
3522 s
[i
]->s
.k
= IPPROTO_NONE
;
3523 s
[fix5
]->s
.jf
= s
[i
];
3528 if (proto
== Q_IPV6
) {
3529 int v6start
, v6end
, v6advance
, j
;
3532 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3533 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3534 s
[i
]->s
.jt
= NULL
; /*later*/
3535 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3536 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3537 s
[fix2
]->s
.jf
= s
[i
];
3539 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3540 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3541 s
[i
]->s
.jt
= NULL
; /*later*/
3542 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3543 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3545 /* if (A == IPPROTO_ROUTING) goto v6advance */
3546 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3547 s
[i
]->s
.jt
= NULL
; /*later*/
3548 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3549 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3551 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3552 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3553 s
[i
]->s
.jt
= NULL
; /*later*/
3554 s
[i
]->s
.jf
= NULL
; /*later*/
3555 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3566 * X = X + (P[X + 1] + 1) * 8;
3569 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3571 /* A = P[X + packet head] */
3572 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3576 s
[i
] = new_stmt(BPF_ST
);
3580 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3583 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3587 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3589 /* A = P[X + packet head]; */
3590 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3594 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3598 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3602 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3605 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3609 /* goto again; (must use BPF_JA for backward jump) */
3610 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3611 s
[i
]->s
.k
= again
- i
- 1;
3612 s
[i
- 1]->s
.jf
= s
[i
];
3616 for (j
= v6start
; j
<= v6end
; j
++)
3617 s
[j
]->s
.jt
= s
[v6advance
];
3622 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3624 s
[fix2
]->s
.jf
= s
[i
];
3630 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3631 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3632 s
[i
]->s
.jt
= NULL
; /*later*/
3633 s
[i
]->s
.jf
= NULL
; /*later*/
3634 s
[i
]->s
.k
= IPPROTO_AH
;
3636 s
[fix3
]->s
.jf
= s
[ahcheck
];
3643 * X = X + (P[X + 1] + 2) * 4;
3646 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3648 /* A = P[X + packet head]; */
3649 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3653 s
[i
] = new_stmt(BPF_ST
);
3657 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3660 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3664 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3666 /* A = P[X + packet head] */
3667 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3671 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3675 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3679 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3682 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3686 /* goto again; (must use BPF_JA for backward jump) */
3687 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3688 s
[i
]->s
.k
= again
- i
- 1;
3693 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3695 s
[fix2
]->s
.jt
= s
[end
];
3696 s
[fix4
]->s
.jf
= s
[end
];
3697 s
[fix5
]->s
.jt
= s
[end
];
3704 for (i
= 0; i
< max
- 1; i
++)
3705 s
[i
]->next
= s
[i
+ 1];
3706 s
[max
- 1]->next
= NULL
;
3711 b
= new_block(JMP(BPF_JEQ
));
3712 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3722 static struct block
*
3723 gen_proto(v
, proto
, dir
)
3728 struct block
*b0
, *b1
;
3730 if (dir
!= Q_DEFAULT
)
3731 bpf_error("direction applied to 'proto'");
3736 b0
= gen_proto(v
, Q_IP
, dir
);
3737 b1
= gen_proto(v
, Q_IPV6
, dir
);
3745 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3746 * not LLC encapsulation with LLCSAP_IP.
3748 * For IEEE 802 networks - which includes 802.5 token ring
3749 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3750 * says that SNAP encapsulation is used, not LLC encapsulation
3753 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3754 * RFC 2225 say that SNAP encapsulation is used, not LLC
3755 * encapsulation with LLCSAP_IP.
3757 * So we always check for ETHERTYPE_IP.
3759 b0
= gen_linktype(ETHERTYPE_IP
);
3761 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
3763 b1
= gen_protochain(v
, Q_IP
);
3773 * Frame Relay packets typically have an OSI
3774 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3775 * generates code to check for all the OSI
3776 * NLPIDs, so calling it and then adding a check
3777 * for the particular NLPID for which we're
3778 * looking is bogus, as we can just check for
3781 * What we check for is the NLPID and a frame
3782 * control field value of UI, i.e. 0x03 followed
3785 * XXX - assumes a 2-byte Frame Relay header with
3786 * DLCI and flags. What if the address is longer?
3788 * XXX - what about SNAP-encapsulated frames?
3790 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
3796 * Cisco uses an Ethertype lookalike - for OSI,
3799 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
3800 /* OSI in C-HDLC is stuffed with a fudge byte */
3801 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
3806 b0
= gen_linktype(LLCSAP_ISONS
);
3807 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
3813 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3815 * 4 is the offset of the PDU type relative to the IS-IS
3818 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
3823 bpf_error("arp does not encapsulate another protocol");
3827 bpf_error("rarp does not encapsulate another protocol");
3831 bpf_error("atalk encapsulation is not specifiable");
3835 bpf_error("decnet encapsulation is not specifiable");
3839 bpf_error("sca does not encapsulate another protocol");
3843 bpf_error("lat does not encapsulate another protocol");
3847 bpf_error("moprc does not encapsulate another protocol");
3851 bpf_error("mopdl does not encapsulate another protocol");
3855 return gen_linktype(v
);
3858 bpf_error("'udp proto' is bogus");
3862 bpf_error("'tcp proto' is bogus");
3866 bpf_error("'sctp proto' is bogus");
3870 bpf_error("'icmp proto' is bogus");
3874 bpf_error("'igmp proto' is bogus");
3878 bpf_error("'igrp proto' is bogus");
3882 bpf_error("'pim proto' is bogus");
3886 bpf_error("'vrrp proto' is bogus");
3891 b0
= gen_linktype(ETHERTYPE_IPV6
);
3893 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
3895 b1
= gen_protochain(v
, Q_IPV6
);
3901 bpf_error("'icmp6 proto' is bogus");
3905 bpf_error("'ah proto' is bogus");
3908 bpf_error("'ah proto' is bogus");
3911 bpf_error("'stp proto' is bogus");
3914 bpf_error("'ipx proto' is bogus");
3917 bpf_error("'netbeui proto' is bogus");
3928 register const char *name
;
3931 int proto
= q
.proto
;
3935 bpf_u_int32 mask
, addr
;
3937 bpf_u_int32
**alist
;
3940 struct sockaddr_in
*sin
;
3941 struct sockaddr_in6
*sin6
;
3942 struct addrinfo
*res
, *res0
;
3943 struct in6_addr mask128
;
3945 struct block
*b
, *tmp
;
3946 int port
, real_proto
;
3951 addr
= pcap_nametonetaddr(name
);
3953 bpf_error("unknown network '%s'", name
);
3954 /* Left justify network addr and calculate its network mask */
3956 while (addr
&& (addr
& 0xff000000) == 0) {
3960 return gen_host(addr
, mask
, proto
, dir
);
3964 if (proto
== Q_LINK
) {
3968 eaddr
= pcap_ether_hostton(name
);
3971 "unknown ether host '%s'", name
);
3972 b
= gen_ehostop(eaddr
, dir
);
3977 eaddr
= pcap_ether_hostton(name
);
3980 "unknown FDDI host '%s'", name
);
3981 b
= gen_fhostop(eaddr
, dir
);
3986 eaddr
= pcap_ether_hostton(name
);
3989 "unknown token ring host '%s'", name
);
3990 b
= gen_thostop(eaddr
, dir
);
3994 case DLT_IEEE802_11
:
3995 eaddr
= pcap_ether_hostton(name
);
3998 "unknown 802.11 host '%s'", name
);
3999 b
= gen_wlanhostop(eaddr
, dir
);
4003 case DLT_IP_OVER_FC
:
4004 eaddr
= pcap_ether_hostton(name
);
4007 "unknown Fibre Channel host '%s'", name
);
4008 b
= gen_ipfchostop(eaddr
, dir
);
4017 * Check that the packet doesn't begin
4018 * with an LE Control marker. (We've
4019 * already generated a test for LANE.)
4021 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4025 eaddr
= pcap_ether_hostton(name
);
4028 "unknown ether host '%s'", name
);
4029 b
= gen_ehostop(eaddr
, dir
);
4035 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4036 } else if (proto
== Q_DECNET
) {
4037 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4039 * I don't think DECNET hosts can be multihomed, so
4040 * there is no need to build up a list of addresses
4042 return (gen_host(dn_addr
, 0, proto
, dir
));
4045 alist
= pcap_nametoaddr(name
);
4046 if (alist
== NULL
|| *alist
== NULL
)
4047 bpf_error("unknown host '%s'", name
);
4049 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4051 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4053 tmp
= gen_host(**alist
++, 0xffffffff,
4060 memset(&mask128
, 0xff, sizeof(mask128
));
4061 res0
= res
= pcap_nametoaddrinfo(name
);
4063 bpf_error("unknown host '%s'", name
);
4065 tproto
= tproto6
= proto
;
4066 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4070 for (res
= res0
; res
; res
= res
->ai_next
) {
4071 switch (res
->ai_family
) {
4073 if (tproto
== Q_IPV6
)
4076 sin
= (struct sockaddr_in
*)
4078 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4079 0xffffffff, tproto
, dir
);
4082 if (tproto6
== Q_IP
)
4085 sin6
= (struct sockaddr_in6
*)
4087 tmp
= gen_host6(&sin6
->sin6_addr
,
4088 &mask128
, tproto6
, dir
);
4099 bpf_error("unknown host '%s'%s", name
,
4100 (proto
== Q_DEFAULT
)
4102 : " for specified address family");
4109 if (proto
!= Q_DEFAULT
&&
4110 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4111 bpf_error("illegal qualifier of 'port'");
4112 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4113 bpf_error("unknown port '%s'", name
);
4114 if (proto
== Q_UDP
) {
4115 if (real_proto
== IPPROTO_TCP
)
4116 bpf_error("port '%s' is tcp", name
);
4117 else if (real_proto
== IPPROTO_SCTP
)
4118 bpf_error("port '%s' is sctp", name
);
4120 /* override PROTO_UNDEF */
4121 real_proto
= IPPROTO_UDP
;
4123 if (proto
== Q_TCP
) {
4124 if (real_proto
== IPPROTO_UDP
)
4125 bpf_error("port '%s' is udp", name
);
4127 else if (real_proto
== IPPROTO_SCTP
)
4128 bpf_error("port '%s' is sctp", name
);
4130 /* override PROTO_UNDEF */
4131 real_proto
= IPPROTO_TCP
;
4133 if (proto
== Q_SCTP
) {
4134 if (real_proto
== IPPROTO_UDP
)
4135 bpf_error("port '%s' is udp", name
);
4137 else if (real_proto
== IPPROTO_TCP
)
4138 bpf_error("port '%s' is tcp", name
);
4140 /* override PROTO_UNDEF */
4141 real_proto
= IPPROTO_SCTP
;
4144 return gen_port(port
, real_proto
, dir
);
4148 b
= gen_port(port
, real_proto
, dir
);
4149 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4156 eaddr
= pcap_ether_hostton(name
);
4158 bpf_error("unknown ether host: %s", name
);
4160 alist
= pcap_nametoaddr(name
);
4161 if (alist
== NULL
|| *alist
== NULL
)
4162 bpf_error("unknown host '%s'", name
);
4163 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4167 bpf_error("'gateway' not supported in this configuration");
4171 real_proto
= lookup_proto(name
, proto
);
4172 if (real_proto
>= 0)
4173 return gen_proto(real_proto
, proto
, dir
);
4175 bpf_error("unknown protocol: %s", name
);
4178 real_proto
= lookup_proto(name
, proto
);
4179 if (real_proto
>= 0)
4180 return gen_protochain(real_proto
, proto
, dir
);
4182 bpf_error("unknown protocol: %s", name
);
4194 gen_mcode(s1
, s2
, masklen
, q
)
4195 register const char *s1
, *s2
;
4196 register int masklen
;
4199 register int nlen
, mlen
;
4202 nlen
= __pcap_atoin(s1
, &n
);
4203 /* Promote short ipaddr */
4207 mlen
= __pcap_atoin(s2
, &m
);
4208 /* Promote short ipaddr */
4211 bpf_error("non-network bits set in \"%s mask %s\"",
4214 /* Convert mask len to mask */
4216 bpf_error("mask length must be <= 32");
4217 m
= 0xffffffff << (32 - masklen
);
4219 bpf_error("non-network bits set in \"%s/%d\"",
4226 return gen_host(n
, m
, q
.proto
, q
.dir
);
4229 bpf_error("Mask syntax for networks only");
4237 register const char *s
;
4242 int proto
= q
.proto
;
4248 else if (q
.proto
== Q_DECNET
)
4249 vlen
= __pcap_atodn(s
, &v
);
4251 vlen
= __pcap_atoin(s
, &v
);
4258 if (proto
== Q_DECNET
)
4259 return gen_host(v
, 0, proto
, dir
);
4260 else if (proto
== Q_LINK
) {
4261 bpf_error("illegal link layer address");
4264 if (s
== NULL
&& q
.addr
== Q_NET
) {
4265 /* Promote short net number */
4266 while (v
&& (v
& 0xff000000) == 0) {
4271 /* Promote short ipaddr */
4275 return gen_host(v
, mask
, proto
, dir
);
4280 proto
= IPPROTO_UDP
;
4281 else if (proto
== Q_TCP
)
4282 proto
= IPPROTO_TCP
;
4283 else if (proto
== Q_SCTP
)
4284 proto
= IPPROTO_SCTP
;
4285 else if (proto
== Q_DEFAULT
)
4286 proto
= PROTO_UNDEF
;
4288 bpf_error("illegal qualifier of 'port'");
4291 return gen_port((int)v
, proto
, dir
);
4295 b
= gen_port((int)v
, proto
, dir
);
4296 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4302 bpf_error("'gateway' requires a name");
4306 return gen_proto((int)v
, proto
, dir
);
4309 return gen_protochain((int)v
, proto
, dir
);
4324 gen_mcode6(s1
, s2
, masklen
, q
)
4325 register const char *s1
, *s2
;
4326 register int masklen
;
4329 struct addrinfo
*res
;
4330 struct in6_addr
*addr
;
4331 struct in6_addr mask
;
4336 bpf_error("no mask %s supported", s2
);
4338 res
= pcap_nametoaddrinfo(s1
);
4340 bpf_error("invalid ip6 address %s", s1
);
4342 bpf_error("%s resolved to multiple address", s1
);
4343 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4345 if (sizeof(mask
) * 8 < masklen
)
4346 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4347 memset(&mask
, 0, sizeof(mask
));
4348 memset(&mask
, 0xff, masklen
/ 8);
4350 mask
.s6_addr
[masklen
/ 8] =
4351 (0xff << (8 - masklen
% 8)) & 0xff;
4354 a
= (u_int32_t
*)addr
;
4355 m
= (u_int32_t
*)&mask
;
4356 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4357 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4358 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4366 bpf_error("Mask syntax for networks only");
4370 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4375 bpf_error("invalid qualifier against IPv6 address");
4383 register const u_char
*eaddr
;
4386 struct block
*b
, *tmp
;
4388 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4389 if (linktype
== DLT_EN10MB
)
4390 return gen_ehostop(eaddr
, (int)q
.dir
);
4391 if (linktype
== DLT_FDDI
)
4392 return gen_fhostop(eaddr
, (int)q
.dir
);
4393 if (linktype
== DLT_IEEE802
)
4394 return gen_thostop(eaddr
, (int)q
.dir
);
4395 if (linktype
== DLT_IEEE802_11
)
4396 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4397 if (linktype
== DLT_SUNATM
&& is_lane
) {
4399 * Check that the packet doesn't begin with an
4400 * LE Control marker. (We've already generated
4403 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4407 * Now check the MAC address.
4409 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4413 if (linktype
== DLT_IP_OVER_FC
)
4414 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4415 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4417 bpf_error("ethernet address used in non-ether expression");
4423 struct slist
*s0
, *s1
;
4426 * This is definitely not the best way to do this, but the
4427 * lists will rarely get long.
4434 static struct slist
*
4440 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4445 static struct slist
*
4451 s
= new_stmt(BPF_LD
|BPF_MEM
);
4457 gen_load(proto
, index
, size
)
4462 struct slist
*s
, *tmp
;
4464 int regno
= alloc_reg();
4466 free_reg(index
->regno
);
4470 bpf_error("data size must be 1, 2, or 4");
4486 bpf_error("unsupported index operation");
4490 * XXX - what about ATM LANE? Should the index be
4491 * relative to the beginning of the AAL5 frame, so
4492 * that 0 refers to the beginning of the LE Control
4493 * field, or relative to the beginning of the LAN
4494 * frame, so that 0 refers, for Ethernet LANE, to
4495 * the beginning of the destination address?
4497 s
= xfer_to_x(index
);
4498 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4500 sappend(index
->s
, s
);
4515 /* XXX Note that we assume a fixed link header here. */
4516 s
= xfer_to_x(index
);
4517 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4520 sappend(index
->s
, s
);
4522 b
= gen_proto_abbrev(proto
);
4524 gen_and(index
->b
, b
);
4536 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4538 sappend(s
, xfer_to_a(index
));
4539 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4540 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4541 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4543 sappend(index
->s
, s
);
4545 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4547 gen_and(index
->b
, b
);
4549 gen_and(gen_proto_abbrev(Q_IP
), b
);
4555 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4559 index
->regno
= regno
;
4560 s
= new_stmt(BPF_ST
);
4562 sappend(index
->s
, s
);
4568 gen_relation(code
, a0
, a1
, reversed
)
4570 struct arth
*a0
, *a1
;
4573 struct slist
*s0
, *s1
, *s2
;
4574 struct block
*b
, *tmp
;
4578 if (code
== BPF_JEQ
) {
4579 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4580 b
= new_block(JMP(code
));
4584 b
= new_block(BPF_JMP
|code
|BPF_X
);
4590 sappend(a0
->s
, a1
->s
);
4594 free_reg(a0
->regno
);
4595 free_reg(a1
->regno
);
4597 /* 'and' together protocol checks */
4600 gen_and(a0
->b
, tmp
= a1
->b
);
4616 int regno
= alloc_reg();
4617 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4620 s
= new_stmt(BPF_LD
|BPF_LEN
);
4621 s
->next
= new_stmt(BPF_ST
);
4622 s
->next
->s
.k
= regno
;
4637 a
= (struct arth
*)newchunk(sizeof(*a
));
4641 s
= new_stmt(BPF_LD
|BPF_IMM
);
4643 s
->next
= new_stmt(BPF_ST
);
4659 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4662 s
= new_stmt(BPF_ST
);
4670 gen_arth(code
, a0
, a1
)
4672 struct arth
*a0
, *a1
;
4674 struct slist
*s0
, *s1
, *s2
;
4678 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
4683 sappend(a0
->s
, a1
->s
);
4685 free_reg(a0
->regno
);
4686 free_reg(a1
->regno
);
4688 s0
= new_stmt(BPF_ST
);
4689 a0
->regno
= s0
->s
.k
= alloc_reg();
4696 * Here we handle simple allocation of the scratch registers.
4697 * If too many registers are alloc'd, the allocator punts.
4699 static int regused
[BPF_MEMWORDS
];
4703 * Return the next free register.
4708 int n
= BPF_MEMWORDS
;
4711 if (regused
[curreg
])
4712 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
4714 regused
[curreg
] = 1;
4718 bpf_error("too many registers needed to evaluate expression");
4723 * Return a register to the table so it can
4733 static struct block
*
4740 s
= new_stmt(BPF_LD
|BPF_LEN
);
4741 b
= new_block(JMP(jmp
));
4752 return gen_len(BPF_JGE
, n
);
4756 * Actually, this is less than or equal.
4764 b
= gen_len(BPF_JGT
, n
);
4771 gen_byteop(op
, idx
, val
)
4782 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4785 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4786 b
->s
.code
= JMP(BPF_JGE
);
4791 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
4792 b
->s
.code
= JMP(BPF_JGT
);
4796 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
4800 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
4804 b
= new_block(JMP(BPF_JEQ
));
4811 static u_char abroadcast
[] = { 0x0 };
4814 gen_broadcast(proto
)
4817 bpf_u_int32 hostmask
;
4818 struct block
*b0
, *b1
, *b2
;
4819 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4825 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4826 return gen_ahostop(abroadcast
, Q_DST
);
4827 if (linktype
== DLT_EN10MB
)
4828 return gen_ehostop(ebroadcast
, Q_DST
);
4829 if (linktype
== DLT_FDDI
)
4830 return gen_fhostop(ebroadcast
, Q_DST
);
4831 if (linktype
== DLT_IEEE802
)
4832 return gen_thostop(ebroadcast
, Q_DST
);
4833 if (linktype
== DLT_IEEE802_11
)
4834 return gen_wlanhostop(ebroadcast
, Q_DST
);
4835 if (linktype
== DLT_IP_OVER_FC
)
4836 return gen_ipfchostop(ebroadcast
, Q_DST
);
4837 if (linktype
== DLT_SUNATM
&& is_lane
) {
4839 * Check that the packet doesn't begin with an
4840 * LE Control marker. (We've already generated
4843 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4847 * Now check the MAC address.
4849 b0
= gen_ehostop(ebroadcast
, Q_DST
);
4853 bpf_error("not a broadcast link");
4857 b0
= gen_linktype(ETHERTYPE_IP
);
4858 hostmask
= ~netmask
;
4859 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
4860 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
4861 (bpf_int32
)(~0 & hostmask
), hostmask
);
4866 bpf_error("only link-layer/IP broadcast filters supported");
4871 * Generate code to test the low-order bit of a MAC address (that's
4872 * the bottom bit of the *first* byte).
4874 static struct block
*
4875 gen_mac_multicast(offset
)
4878 register struct block
*b0
;
4879 register struct slist
*s
;
4881 /* link[offset] & 1 != 0 */
4882 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4884 b0
= new_block(JMP(BPF_JSET
));
4891 gen_multicast(proto
)
4894 register struct block
*b0
, *b1
, *b2
;
4895 register struct slist
*s
;
4901 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
4902 /* all ARCnet multicasts use the same address */
4903 return gen_ahostop(abroadcast
, Q_DST
);
4905 if (linktype
== DLT_EN10MB
) {
4906 /* ether[0] & 1 != 0 */
4907 return gen_mac_multicast(0);
4910 if (linktype
== DLT_FDDI
) {
4912 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4914 * XXX - was that referring to bit-order issues?
4916 /* fddi[1] & 1 != 0 */
4917 return gen_mac_multicast(1);
4920 if (linktype
== DLT_IEEE802
) {
4921 /* tr[2] & 1 != 0 */
4922 return gen_mac_multicast(2);
4925 if (linktype
== DLT_IEEE802_11
) {
4929 * For control frames, there is no DA.
4931 * For management frames, DA is at an
4932 * offset of 4 from the beginning of
4935 * For data frames, DA is at an offset
4936 * of 4 from the beginning of the packet
4937 * if To DS is clear and at an offset of
4938 * 16 from the beginning of the packet
4943 * Generate the tests to be done for data frames.
4945 * First, check for To DS set, i.e. "link[1] & 0x01".
4947 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4949 b1
= new_block(JMP(BPF_JSET
));
4950 b1
->s
.k
= 0x01; /* To DS */
4954 * If To DS is set, the DA is at 16.
4956 b0
= gen_mac_multicast(16);
4960 * Now, check for To DS not set, i.e. check
4961 * "!(link[1] & 0x01)".
4963 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4965 b2
= new_block(JMP(BPF_JSET
));
4966 b2
->s
.k
= 0x01; /* To DS */
4971 * If To DS is not set, the DA is at 4.
4973 b1
= gen_mac_multicast(4);
4977 * Now OR together the last two checks. That gives
4978 * the complete set of checks for data frames.
4983 * Now check for a data frame.
4984 * I.e, check "link[0] & 0x08".
4986 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
4988 b1
= new_block(JMP(BPF_JSET
));
4993 * AND that with the checks done for data frames.
4998 * If the high-order bit of the type value is 0, this
4999 * is a management frame.
5000 * I.e, check "!(link[0] & 0x08)".
5002 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5004 b2
= new_block(JMP(BPF_JSET
));
5010 * For management frames, the DA is at 4.
5012 b1
= gen_mac_multicast(4);
5016 * OR that with the checks done for data frames.
5017 * That gives the checks done for management and
5023 * If the low-order bit of the type value is 1,
5024 * this is either a control frame or a frame
5025 * with a reserved type, and thus not a
5028 * I.e., check "!(link[0] & 0x04)".
5030 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5032 b1
= new_block(JMP(BPF_JSET
));
5038 * AND that with the checks for data and management
5045 if (linktype
== DLT_IP_OVER_FC
) {
5046 b0
= gen_mac_multicast(2);
5050 if (linktype
== DLT_SUNATM
&& is_lane
) {
5052 * Check that the packet doesn't begin with an
5053 * LE Control marker. (We've already generated
5056 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5059 /* ether[off_mac] & 1 != 0 */
5060 b0
= gen_mac_multicast(off_mac
);
5065 /* Link not known to support multicasts */
5069 b0
= gen_linktype(ETHERTYPE_IP
);
5070 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5071 b1
->s
.code
= JMP(BPF_JGE
);
5077 b0
= gen_linktype(ETHERTYPE_IPV6
);
5078 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5083 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5088 * generate command for inbound/outbound. It's here so we can
5089 * make it link-type specific. 'dir' = 0 implies "inbound",
5090 * = 1 implies "outbound".
5096 register struct block
*b0
;
5099 * Only some data link types support inbound/outbound qualifiers.
5103 b0
= gen_relation(BPF_JEQ
,
5104 gen_load(Q_LINK
, gen_loadi(0), 1),
5112 * Match packets sent by this machine.
5114 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5117 * Match packets sent to this machine.
5118 * (No broadcast or multicast packets, or
5119 * packets sent to some other machine and
5120 * received promiscuously.)
5122 * XXX - packets sent to other machines probably
5123 * shouldn't be matched, but what about broadcast
5124 * or multicast packets we received?
5126 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5131 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5132 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5137 /* match outgoing packets */
5138 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5140 /* match incoming packets */
5141 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5145 case DLT_JUNIPER_MLFR
:
5146 case DLT_JUNIPER_MLPPP
:
5147 case DLT_JUNIPER_ATM1
:
5148 case DLT_JUNIPER_ATM2
:
5149 /* juniper flags (including direction) are stored
5150 * the byte after the 3-byte magic number */
5152 /* match outgoing packets */
5153 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5155 /* match incoming packets */
5156 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5161 bpf_error("inbound/outbound not supported on linktype %d",
5169 /* PF firewall log matched interface */
5171 gen_pf_ifname(const char *ifname
)
5176 if (linktype
== DLT_PFLOG
) {
5177 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5178 off
= offsetof(struct pfloghdr
, ifname
);
5180 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5183 if (strlen(ifname
) >= len
) {
5184 bpf_error("ifname interface names can only be %d characters",
5188 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5192 /* PF firewall log matched interface */
5194 gen_pf_ruleset(char *ruleset
)
5198 if (linktype
!= DLT_PFLOG
) {
5199 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5202 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5203 bpf_error("ruleset names can only be %ld characters",
5204 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5207 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5208 strlen(ruleset
), (const u_char
*)ruleset
);
5212 /* PF firewall log rule number */
5218 if (linktype
== DLT_PFLOG
) {
5219 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5222 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5229 /* PF firewall log sub-rule number */
5231 gen_pf_srnr(int srnr
)
5235 if (linktype
!= DLT_PFLOG
) {
5236 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5240 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5245 /* PF firewall log reason code */
5247 gen_pf_reason(int reason
)
5251 if (linktype
== DLT_PFLOG
) {
5252 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5255 bpf_error("reason not supported on linktype 0x%x", linktype
);
5262 /* PF firewall log action */
5264 gen_pf_action(int action
)
5268 if (linktype
== DLT_PFLOG
) {
5269 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5272 bpf_error("action not supported on linktype 0x%x", linktype
);
5281 register const u_char
*eaddr
;
5284 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5285 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5286 return gen_ahostop(eaddr
, (int)q
.dir
);
5288 bpf_error("ARCnet address used in non-arc expression");
5292 static struct block
*
5293 gen_ahostop(eaddr
, dir
)
5294 register const u_char
*eaddr
;
5297 register struct block
*b0
, *b1
;
5300 /* src comes first, different from Ethernet */
5302 return gen_bcmp(0, 1, eaddr
);
5305 return gen_bcmp(1, 1, eaddr
);
5308 b0
= gen_ahostop(eaddr
, Q_SRC
);
5309 b1
= gen_ahostop(eaddr
, Q_DST
);
5315 b0
= gen_ahostop(eaddr
, Q_SRC
);
5316 b1
= gen_ahostop(eaddr
, Q_DST
);
5325 * support IEEE 802.1Q VLAN trunk over ethernet
5334 * Change the offsets to point to the type and data fields within
5335 * the VLAN packet. This is somewhat of a kludge.
5337 if (orig_nl
== (u_int
)-1) {
5338 orig_linktype
= off_linktype
; /* save original values */
5340 orig_nl_nosnap
= off_nl_nosnap
;
5351 bpf_error("no VLAN support for data link type %d",
5357 /* check for VLAN */
5358 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5360 /* If a specific VLAN is requested, check VLAN id */
5361 if (vlan_num
>= 0) {
5364 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5382 * Change the offsets to point to the type and data fields within
5383 * the MPLS packet. This is somewhat of a kludge.
5385 if (orig_nl
== (u_int
)-1) {
5386 orig_linktype
= off_linktype
; /* save original values */
5388 orig_nl_nosnap
= off_nl_nosnap
;
5397 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5405 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5413 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5416 /* FIXME add other DLT_s ...
5417 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5418 * leave it for now */
5421 bpf_error("no MPLS support for data link type %d",
5427 bpf_error("'mpls' can't be combined with 'vlan' or another 'mpls'");
5432 /* If a specific MPLS label is requested, check it */
5433 if (label_num
>= 0) {
5436 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5437 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5446 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5458 bpf_error("'vpi' supported only on raw ATM");
5459 if (off_vpi
== (u_int
)-1)
5461 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5462 (u_int
)jvalue
, reverse
);
5467 bpf_error("'vci' supported only on raw ATM");
5468 if (off_vci
== (u_int
)-1)
5470 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5471 (u_int
)jvalue
, reverse
);
5475 if (off_proto
== (u_int
)-1)
5476 abort(); /* XXX - this isn't on FreeBSD */
5477 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5478 (u_int
)jvalue
, reverse
);
5482 if (off_payload
== (u_int
)-1)
5484 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5485 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5490 bpf_error("'callref' supported only on raw ATM");
5491 if (off_proto
== (u_int
)-1)
5493 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5494 (u_int
)jvalue
, reverse
);
5504 gen_atmtype_abbrev(type
)
5507 struct block
*b0
, *b1
;
5512 /* Get all packets in Meta signalling Circuit */
5514 bpf_error("'metac' supported only on raw ATM");
5515 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5516 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5521 /* Get all packets in Broadcast Circuit*/
5523 bpf_error("'bcc' supported only on raw ATM");
5524 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5525 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5530 /* Get all cells in Segment OAM F4 circuit*/
5532 bpf_error("'oam4sc' supported only on raw ATM");
5533 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5534 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5539 /* Get all cells in End-to-End OAM F4 Circuit*/
5541 bpf_error("'oam4ec' supported only on raw ATM");
5542 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5543 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5548 /* Get all packets in connection Signalling Circuit */
5550 bpf_error("'sc' supported only on raw ATM");
5551 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5552 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5557 /* Get all packets in ILMI Circuit */
5559 bpf_error("'ilmic' supported only on raw ATM");
5560 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5561 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5566 /* Get all LANE packets */
5568 bpf_error("'lane' supported only on raw ATM");
5569 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5572 * Arrange that all subsequent tests assume LANE
5573 * rather than LLC-encapsulated packets, and set
5574 * the offsets appropriately for LANE-encapsulated
5577 * "off_mac" is the offset of the Ethernet header,
5578 * which is 2 bytes past the ATM pseudo-header
5579 * (skipping the pseudo-header and 2-byte LE Client
5580 * field). The other offsets are Ethernet offsets
5581 * relative to "off_mac".
5584 off_mac
= off_payload
+ 2; /* MAC header */
5585 off_linktype
= off_mac
+ 12;
5586 off_nl
= off_mac
+ 14; /* Ethernet II */
5587 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5591 /* Get all LLC-encapsulated packets */
5593 bpf_error("'llc' supported only on raw ATM");
5594 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5605 static struct block
*
5606 gen_msg_abbrev(type
)
5612 * Q.2931 signalling protocol messages for handling virtual circuits
5613 * establishment and teardown
5618 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5622 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5626 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5630 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5634 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5637 case A_RELEASE_DONE
:
5638 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5648 gen_atmmulti_abbrev(type
)
5651 struct block
*b0
, *b1
;
5657 bpf_error("'oam' supported only on raw ATM");
5658 b1
= gen_atmmulti_abbrev(A_OAMF4
);
5663 bpf_error("'oamf4' supported only on raw ATM");
5665 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5666 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5668 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5674 * Get Q.2931 signalling messages for switched
5675 * virtual connection
5678 bpf_error("'connectmsg' supported only on raw ATM");
5679 b0
= gen_msg_abbrev(A_SETUP
);
5680 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5682 b0
= gen_msg_abbrev(A_CONNECT
);
5684 b0
= gen_msg_abbrev(A_CONNECTACK
);
5686 b0
= gen_msg_abbrev(A_RELEASE
);
5688 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5690 b0
= gen_atmtype_abbrev(A_SC
);
5696 bpf_error("'metaconnect' supported only on raw ATM");
5697 b0
= gen_msg_abbrev(A_SETUP
);
5698 b1
= gen_msg_abbrev(A_CALLPROCEED
);
5700 b0
= gen_msg_abbrev(A_CONNECT
);
5702 b0
= gen_msg_abbrev(A_RELEASE
);
5704 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
5706 b0
= gen_atmtype_abbrev(A_METAC
);