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.231 2005-04-19 04:25:00 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 * XXX - why was this included even on UNIX?
48 #include <sys/param.h>
51 #include <netinet/in.h>
67 #include "ethertype.h"
72 #include "sunatmpos.h"
78 #define offsetof(s, e) ((size_t)&((s *)0)->e)
82 #include <netdb.h> /* for "struct addrinfo" */
85 #include <pcap-namedb.h>
90 #define IPPROTO_SCTP 132
93 #ifdef HAVE_OS_PROTO_H
97 #define JMP(c) ((c)|BPF_JMP|BPF_K)
100 static jmp_buf top_ctx
;
101 static pcap_t
*bpf_pcap
;
103 /* Hack for updating VLAN, MPLS offsets. */
104 static u_int orig_linktype
= -1U, orig_nl
= -1U, orig_nl_nosnap
= -1U;
108 static int pcap_fddipad
;
111 void bpf_error(const char *fmt
, ...) __attribute__((noreturn
));
115 bpf_error(const char *fmt
, ...)
120 if (bpf_pcap
!= NULL
)
121 (void)vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
128 static void init_linktype(pcap_t
*);
130 static int alloc_reg(void);
131 static void free_reg(int);
133 static struct block
*root
;
136 * We divy out chunks of memory rather than call malloc each time so
137 * we don't have to worry about leaking memory. It's probably
138 * not a big deal if all this memory was wasted but if this ever
139 * goes into a library that would probably not be a good idea.
141 * XXX - this *is* in a library....
144 #define CHUNK0SIZE 1024
150 static struct chunk chunks
[NCHUNKS
];
151 static int cur_chunk
;
153 static void *newchunk(u_int
);
154 static void freechunks(void);
155 static inline struct block
*new_block(int);
156 static inline struct slist
*new_stmt(int);
157 static struct block
*gen_retblk(int);
158 static inline void syntax(void);
160 static void backpatch(struct block
*, struct block
*);
161 static void merge(struct block
*, struct block
*);
162 static struct block
*gen_cmp(u_int
, u_int
, bpf_int32
);
163 static struct block
*gen_cmp_gt(u_int
, u_int
, bpf_int32
);
164 static struct block
*gen_mcmp(u_int
, u_int
, bpf_int32
, bpf_u_int32
);
165 static struct block
*gen_bcmp(u_int
, u_int
, const u_char
*);
166 static struct block
*gen_ncmp(bpf_u_int32
, bpf_u_int32
, bpf_u_int32
,
167 bpf_u_int32
, bpf_u_int32
, int);
168 static struct block
*gen_uncond(int);
169 static inline struct block
*gen_true(void);
170 static inline struct block
*gen_false(void);
171 static struct block
*gen_ether_linktype(int);
172 static struct block
*gen_linux_sll_linktype(int);
173 static struct block
*gen_linktype(int);
174 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
, u_int
);
175 static struct block
*gen_llc_linktype(int);
176 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
178 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
180 static struct block
*gen_ahostop(const u_char
*, int);
181 static struct block
*gen_ehostop(const u_char
*, int);
182 static struct block
*gen_fhostop(const u_char
*, int);
183 static struct block
*gen_thostop(const u_char
*, int);
184 static struct block
*gen_wlanhostop(const u_char
*, int);
185 static struct block
*gen_ipfchostop(const u_char
*, int);
186 static struct block
*gen_dnhostop(bpf_u_int32
, int, u_int
);
187 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int);
189 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int);
192 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
194 static struct block
*gen_ipfrag(void);
195 static struct block
*gen_portatom(int, bpf_int32
);
196 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
198 static struct block
*gen_portatom6(int, bpf_int32
);
199 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
201 struct block
*gen_portop(int, int, int);
202 static struct block
*gen_port(int, int, int);
203 struct block
*gen_portrangeop(int, int, int, int);
204 static struct block
*gen_portrange(int, int, int, int);
206 struct block
*gen_portop6(int, int, int);
207 static struct block
*gen_port6(int, int, int);
208 struct block
*gen_portrangeop6(int, int, int, int);
209 static struct block
*gen_portrange6(int, int, int, int);
211 static int lookup_proto(const char *, int);
212 static struct block
*gen_protochain(int, int, int);
213 static struct block
*gen_proto(int, int, int);
214 static struct slist
*xfer_to_x(struct arth
*);
215 static struct slist
*xfer_to_a(struct arth
*);
216 static struct block
*gen_mac_multicast(int);
217 static struct block
*gen_len(int, int);
219 static struct block
*gen_msg_abbrev(int type
);
230 /* XXX Round up to nearest long. */
231 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
233 /* XXX Round up to structure boundary. */
237 cp
= &chunks
[cur_chunk
];
238 if (n
> cp
->n_left
) {
239 ++cp
, k
= ++cur_chunk
;
241 bpf_error("out of memory");
242 size
= CHUNK0SIZE
<< k
;
243 cp
->m
= (void *)malloc(size
);
245 bpf_error("out of memory");
246 memset((char *)cp
->m
, 0, size
);
249 bpf_error("out of memory");
252 return (void *)((char *)cp
->m
+ cp
->n_left
);
261 for (i
= 0; i
< NCHUNKS
; ++i
)
262 if (chunks
[i
].m
!= NULL
) {
269 * A strdup whose allocations are freed after code generation is over.
273 register const char *s
;
275 int n
= strlen(s
) + 1;
276 char *cp
= newchunk(n
);
282 static inline struct block
*
288 p
= (struct block
*)newchunk(sizeof(*p
));
295 static inline struct slist
*
301 p
= (struct slist
*)newchunk(sizeof(*p
));
307 static struct block
*
311 struct block
*b
= new_block(BPF_RET
|BPF_K
);
320 bpf_error("syntax error in filter expression");
323 static bpf_u_int32 netmask
;
328 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
329 char *buf
, int optimize
, bpf_u_int32 mask
)
338 if (setjmp(top_ctx
)) {
346 snaplen
= pcap_snapshot(p
);
348 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
349 "snaplen of 0 rejects all packets");
353 lex_init(buf
? buf
: "");
361 root
= gen_retblk(snaplen
);
363 if (optimize
&& !no_optimize
) {
366 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
367 bpf_error("expression rejects all packets");
369 program
->bf_insns
= icode_to_fcode(root
, &len
);
370 program
->bf_len
= len
;
378 * entry point for using the compiler with no pcap open
379 * pass in all the stuff that is needed explicitly instead.
382 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
383 struct bpf_program
*program
,
384 char *buf
, int optimize
, bpf_u_int32 mask
)
389 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
392 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
398 * Clean up a "struct bpf_program" by freeing all the memory allocated
402 pcap_freecode(struct bpf_program
*program
)
405 if (program
->bf_insns
!= NULL
) {
406 free((char *)program
->bf_insns
);
407 program
->bf_insns
= NULL
;
412 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
413 * which of the jt and jf fields has been resolved and which is a pointer
414 * back to another unresolved block (or nil). At least one of the fields
415 * in each block is already resolved.
418 backpatch(list
, target
)
419 struct block
*list
, *target
;
436 * Merge the lists in b0 and b1, using the 'sense' field to indicate
437 * which of jt and jf is the link.
441 struct block
*b0
, *b1
;
443 register struct block
**p
= &b0
;
445 /* Find end of list. */
447 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
449 /* Concatenate the lists. */
457 backpatch(p
, gen_retblk(snaplen
));
458 p
->sense
= !p
->sense
;
459 backpatch(p
, gen_retblk(0));
465 struct block
*b0
, *b1
;
467 backpatch(b0
, b1
->head
);
468 b0
->sense
= !b0
->sense
;
469 b1
->sense
= !b1
->sense
;
471 b1
->sense
= !b1
->sense
;
477 struct block
*b0
, *b1
;
479 b0
->sense
= !b0
->sense
;
480 backpatch(b0
, b1
->head
);
481 b0
->sense
= !b0
->sense
;
490 b
->sense
= !b
->sense
;
493 static struct block
*
494 gen_cmp(offset
, size
, v
)
501 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
504 b
= new_block(JMP(BPF_JEQ
));
511 static struct block
*
512 gen_cmp_gt(offset
, size
, v
)
519 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
522 b
= new_block(JMP(BPF_JGT
));
529 static struct block
*
530 gen_mcmp(offset
, size
, v
, mask
)
535 struct block
*b
= gen_cmp(offset
, size
, v
);
538 if (mask
!= 0xffffffff) {
539 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
546 static struct block
*
547 gen_bcmp(offset
, size
, v
)
548 register u_int offset
, size
;
549 register const u_char
*v
;
551 register struct block
*b
, *tmp
;
555 register const u_char
*p
= &v
[size
- 4];
556 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
557 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
559 tmp
= gen_cmp(offset
+ size
- 4, BPF_W
, w
);
566 register const u_char
*p
= &v
[size
- 2];
567 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
569 tmp
= gen_cmp(offset
+ size
- 2, BPF_H
, w
);
576 tmp
= gen_cmp(offset
, BPF_B
, (bpf_int32
)v
[0]);
584 static struct block
*
585 gen_ncmp(datasize
, offset
, mask
, jtype
, jvalue
, reverse
)
586 bpf_u_int32 datasize
, offset
, mask
, jtype
, jvalue
;
592 s
= new_stmt(BPF_LD
|datasize
|BPF_ABS
);
595 if (mask
!= 0xffffffff) {
596 s
->next
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
600 b
= new_block(JMP(jtype
));
603 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
609 * Various code constructs need to know the layout of the data link
610 * layer. These variables give the necessary offsets.
614 * This is the offset of the beginning of the MAC-layer header.
615 * It's usually 0, except for ATM LANE.
617 static u_int off_mac
;
620 * "off_linktype" is the offset to information in the link-layer header
621 * giving the packet type.
623 * For Ethernet, it's the offset of the Ethernet type field.
625 * For link-layer types that always use 802.2 headers, it's the
626 * offset of the LLC header.
628 * For PPP, it's the offset of the PPP type field.
630 * For Cisco HDLC, it's the offset of the CHDLC type field.
632 * For BSD loopback, it's the offset of the AF_ value.
634 * For Linux cooked sockets, it's the offset of the type field.
636 * It's set to -1 for no encapsulation, in which case, IP is assumed.
638 static u_int off_linktype
;
641 * TRUE if the link layer includes an ATM pseudo-header.
643 static int is_atm
= 0;
646 * TRUE if "lane" appeared in the filter; it causes us to generate
647 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
649 static int is_lane
= 0;
652 * These are offsets for the ATM pseudo-header.
654 static u_int off_vpi
;
655 static u_int off_vci
;
656 static u_int off_proto
;
659 * This is the offset of the first byte after the ATM pseudo_header,
660 * or -1 if there is no ATM pseudo-header.
662 static u_int off_payload
;
665 * These are offsets to the beginning of the network-layer header.
667 * If the link layer never uses 802.2 LLC:
669 * "off_nl" and "off_nl_nosnap" are the same.
671 * If the link layer always uses 802.2 LLC:
673 * "off_nl" is the offset if there's a SNAP header following
676 * "off_nl_nosnap" is the offset if there's no SNAP header.
678 * If the link layer is Ethernet:
680 * "off_nl" is the offset if the packet is an Ethernet II packet
681 * (we assume no 802.3+802.2+SNAP);
683 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
684 * with an 802.2 header following it.
687 static u_int off_nl_nosnap
;
695 linktype
= pcap_datalink(p
);
697 pcap_fddipad
= p
->fddipad
;
701 * Assume it's not raw ATM with a pseudo-header, for now.
719 off_nl
= 6; /* XXX in reality, variable! */
720 off_nl_nosnap
= 6; /* no 802.2 LLC */
723 case DLT_ARCNET_LINUX
:
725 off_nl
= 8; /* XXX in reality, variable! */
726 off_nl_nosnap
= 8; /* no 802.2 LLC */
731 off_nl
= 14; /* Ethernet II */
732 off_nl_nosnap
= 17; /* 802.3+802.2 */
737 * SLIP doesn't have a link level type. The 16 byte
738 * header is hacked into our SLIP driver.
742 off_nl_nosnap
= 16; /* no 802.2 LLC */
746 /* XXX this may be the same as the DLT_PPP_BSDOS case */
750 off_nl_nosnap
= 24; /* no 802.2 LLC */
757 off_nl_nosnap
= 4; /* no 802.2 LLC */
763 off_nl_nosnap
= 12; /* no 802.2 LLC */
768 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
769 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
772 off_nl_nosnap
= 4; /* no 802.2 LLC */
777 * This does no include the Ethernet header, and
778 * only covers session state.
782 off_nl_nosnap
= 8; /* no 802.2 LLC */
788 off_nl_nosnap
= 24; /* no 802.2 LLC */
793 * FDDI doesn't really have a link-level type field.
794 * We set "off_linktype" to the offset of the LLC header.
796 * To check for Ethernet types, we assume that SSAP = SNAP
797 * is being used and pick out the encapsulated Ethernet type.
798 * XXX - should we generate code to check for SNAP?
802 off_linktype
+= pcap_fddipad
;
804 off_nl
= 21; /* FDDI+802.2+SNAP */
805 off_nl_nosnap
= 16; /* FDDI+802.2 */
807 off_nl
+= pcap_fddipad
;
808 off_nl_nosnap
+= pcap_fddipad
;
814 * Token Ring doesn't really have a link-level type field.
815 * We set "off_linktype" to the offset of the LLC header.
817 * To check for Ethernet types, we assume that SSAP = SNAP
818 * is being used and pick out the encapsulated Ethernet type.
819 * XXX - should we generate code to check for SNAP?
821 * XXX - the header is actually variable-length.
822 * Some various Linux patched versions gave 38
823 * as "off_linktype" and 40 as "off_nl"; however,
824 * if a token ring packet has *no* routing
825 * information, i.e. is not source-routed, the correct
826 * values are 20 and 22, as they are in the vanilla code.
828 * A packet is source-routed iff the uppermost bit
829 * of the first byte of the source address, at an
830 * offset of 8, has the uppermost bit set. If the
831 * packet is source-routed, the total number of bytes
832 * of routing information is 2 plus bits 0x1F00 of
833 * the 16-bit value at an offset of 14 (shifted right
834 * 8 - figure out which byte that is).
837 off_nl
= 22; /* Token Ring+802.2+SNAP */
838 off_nl_nosnap
= 17; /* Token Ring+802.2 */
843 * 802.11 doesn't really have a link-level type field.
844 * We set "off_linktype" to the offset of the LLC header.
846 * To check for Ethernet types, we assume that SSAP = SNAP
847 * is being used and pick out the encapsulated Ethernet type.
848 * XXX - should we generate code to check for SNAP?
850 * XXX - the header is actually variable-length. We
851 * assume a 24-byte link-layer header, as appears in
852 * data frames in networks with no bridges. If the
853 * fromds and tods 802.11 header bits are both set,
854 * it's actually supposed to be 30 bytes.
857 off_nl
= 32; /* 802.11+802.2+SNAP */
858 off_nl_nosnap
= 27; /* 802.11+802.2 */
861 case DLT_PRISM_HEADER
:
863 * Same as 802.11, but with an additional header before
864 * the 802.11 header, containing a bunch of additional
865 * information including radio-level information.
867 * The header is 144 bytes long.
869 * XXX - same variable-length header problem; at least
870 * the Prism header is fixed-length.
872 off_linktype
= 144+24;
873 off_nl
= 144+32; /* Prism+802.11+802.2+SNAP */
874 off_nl_nosnap
= 144+27; /* Prism+802.11+802.2 */
877 case DLT_IEEE802_11_RADIO_AVS
:
879 * Same as 802.11, but with an additional header before
880 * the 802.11 header, containing a bunch of additional
881 * information including radio-level information.
883 * The header is 64 bytes long, at least in its
884 * current incarnation.
886 * XXX - same variable-length header problem, only
887 * more so; this header is also variable-length,
888 * with the length being the 32-bit big-endian
889 * number at an offset of 4 from the beginning
890 * of the radio header.
892 off_linktype
= 64+24;
893 off_nl
= 64+32; /* Radio+802.11+802.2+SNAP */
894 off_nl_nosnap
= 64+27; /* Radio+802.11+802.2 */
897 case DLT_IEEE802_11_RADIO
:
899 * Same as 802.11, but with an additional header before
900 * the 802.11 header, containing a bunch of additional
901 * information including radio-level information.
903 * XXX - same variable-length header problem, only
904 * even *more* so; this header is also variable-length,
905 * with the length being the 16-bit number at an offset
906 * of 2 from the beginning of the radio header, and it's
907 * device-dependent (different devices might supply
908 * different amounts of information), so we can't even
909 * assume a fixed length for the current version of the
912 * Therefore, currently, only raw "link[N:M]" filtering is
920 case DLT_ATM_RFC1483
:
921 case DLT_ATM_CLIP
: /* Linux ATM defines this */
923 * assume routed, non-ISO PDUs
924 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
927 off_nl
= 8; /* 802.2+SNAP */
928 off_nl_nosnap
= 3; /* 802.2 */
933 * Full Frontal ATM; you get AALn PDUs with an ATM
937 off_vpi
= SUNATM_VPI_POS
;
938 off_vci
= SUNATM_VCI_POS
;
939 off_proto
= PROTO_POS
;
940 off_mac
= -1; /* LLC-encapsulated, so no MAC-layer header */
941 off_payload
= SUNATM_PKT_BEGIN_POS
;
942 off_linktype
= off_payload
;
943 off_nl
= off_payload
+8; /* 802.2+SNAP */
944 off_nl_nosnap
= off_payload
+3; /* 802.2 */
950 off_nl_nosnap
= 0; /* no 802.2 LLC */
953 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
956 off_nl_nosnap
= 16; /* no 802.2 LLC */
961 * LocalTalk does have a 1-byte type field in the LLAP header,
962 * but really it just indicates whether there is a "short" or
963 * "long" DDP packet following.
967 off_nl_nosnap
= 0; /* no 802.2 LLC */
972 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
973 * link-level type field. We set "off_linktype" to the
974 * offset of the LLC header.
976 * To check for Ethernet types, we assume that SSAP = SNAP
977 * is being used and pick out the encapsulated Ethernet type.
978 * XXX - should we generate code to check for SNAP? RFC
979 * 2625 says SNAP should be used.
982 off_nl
= 24; /* IPFC+802.2+SNAP */
983 off_nl_nosnap
= 19; /* IPFC+802.2 */
988 * XXX - we should set this to handle SNAP-encapsulated
989 * frames (NLPID of 0x80).
993 off_nl_nosnap
= 0; /* no 802.2 LLC */
996 case DLT_APPLE_IP_OVER_IEEE1394
:
999 off_nl_nosnap
= 18; /* no 802.2 LLC */
1002 case DLT_LINUX_IRDA
:
1004 * Currently, only raw "link[N:M]" filtering is supported.
1013 * Currently, only raw "link[N:M]" filtering is supported.
1020 case DLT_SYMANTEC_FIREWALL
:
1022 off_nl
= 44; /* Ethernet II */
1023 off_nl_nosnap
= 44; /* XXX - what does it do with 802.3 packets? */
1028 /* XXX read this from pf.h? */
1029 off_nl
= PFLOG_HDRLEN
;
1030 off_nl_nosnap
= PFLOG_HDRLEN
; /* no 802.2 LLC */
1033 case DLT_JUNIPER_MLFR
:
1034 case DLT_JUNIPER_MLPPP
:
1037 off_nl_nosnap
= -1; /* no 802.2 LLC */
1040 case DLT_JUNIPER_ATM1
:
1041 off_linktype
= 4; /* in reality variable between 4-8 */
1046 case DLT_JUNIPER_ATM2
:
1047 off_linktype
= 8; /* in reality variable between 8-12 */
1060 case DLT_LINUX_LAPD
:
1062 * Currently, only raw "link[N:M]" filtering is supported.
1069 bpf_error("unknown data link type %d", linktype
);
1073 static struct block
*
1080 s
= new_stmt(BPF_LD
|BPF_IMM
);
1082 b
= new_block(JMP(BPF_JEQ
));
1088 static inline struct block
*
1091 return gen_uncond(1);
1094 static inline struct block
*
1097 return gen_uncond(0);
1101 * Byte-swap a 32-bit number.
1102 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1103 * big-endian platforms.)
1105 #define SWAPLONG(y) \
1106 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1109 * Generate code to match a particular packet type.
1111 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1112 * value, if <= ETHERMTU. We use that to determine whether to
1113 * match the type/length field or to check the type/length field for
1114 * a value <= ETHERMTU to see whether it's a type field and then do
1115 * the appropriate test.
1117 static struct block
*
1118 gen_ether_linktype(proto
)
1121 struct block
*b0
, *b1
;
1127 case LLCSAP_NETBEUI
:
1129 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1130 * so we check the DSAP and SSAP.
1132 * LLCSAP_IP checks for IP-over-802.2, rather
1133 * than IP-over-Ethernet or IP-over-SNAP.
1135 * XXX - should we check both the DSAP and the
1136 * SSAP, like this, or should we check just the
1137 * DSAP, as we do for other types <= ETHERMTU
1138 * (i.e., other SAP values)?
1140 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1142 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1143 ((proto
<< 8) | proto
));
1151 * Ethernet_II frames, which are Ethernet
1152 * frames with a frame type of ETHERTYPE_IPX;
1154 * Ethernet_802.3 frames, which are 802.3
1155 * frames (i.e., the type/length field is
1156 * a length field, <= ETHERMTU, rather than
1157 * a type field) with the first two bytes
1158 * after the Ethernet/802.3 header being
1161 * Ethernet_802.2 frames, which are 802.3
1162 * frames with an 802.2 LLC header and
1163 * with the IPX LSAP as the DSAP in the LLC
1166 * Ethernet_SNAP frames, which are 802.3
1167 * frames with an LLC header and a SNAP
1168 * header and with an OUI of 0x000000
1169 * (encapsulated Ethernet) and a protocol
1170 * ID of ETHERTYPE_IPX in the SNAP header.
1172 * XXX - should we generate the same code both
1173 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1177 * This generates code to check both for the
1178 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1180 b0
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1181 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)0xFFFF);
1185 * Now we add code to check for SNAP frames with
1186 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1188 b0
= gen_snap(0x000000, ETHERTYPE_IPX
, 14);
1192 * Now we generate code to check for 802.3
1193 * frames in general.
1195 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1199 * Now add the check for 802.3 frames before the
1200 * check for Ethernet_802.2 and Ethernet_802.3,
1201 * as those checks should only be done on 802.3
1202 * frames, not on Ethernet frames.
1207 * Now add the check for Ethernet_II frames, and
1208 * do that before checking for the other frame
1211 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1215 case ETHERTYPE_ATALK
:
1216 case ETHERTYPE_AARP
:
1218 * EtherTalk (AppleTalk protocols on Ethernet link
1219 * layer) may use 802.2 encapsulation.
1223 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1224 * we check for an Ethernet type field less than
1225 * 1500, which means it's an 802.3 length field.
1227 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1231 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1232 * SNAP packets with an organization code of
1233 * 0x080007 (Apple, for Appletalk) and a protocol
1234 * type of ETHERTYPE_ATALK (Appletalk).
1236 * 802.2-encapsulated ETHERTYPE_AARP packets are
1237 * SNAP packets with an organization code of
1238 * 0x000000 (encapsulated Ethernet) and a protocol
1239 * type of ETHERTYPE_AARP (Appletalk ARP).
1241 if (proto
== ETHERTYPE_ATALK
)
1242 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
, 14);
1243 else /* proto == ETHERTYPE_AARP */
1244 b1
= gen_snap(0x000000, ETHERTYPE_AARP
, 14);
1248 * Check for Ethernet encapsulation (Ethertalk
1249 * phase 1?); we just check for the Ethernet
1252 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1258 if (proto
<= ETHERMTU
) {
1260 * This is an LLC SAP value, so the frames
1261 * that match would be 802.2 frames.
1262 * Check that the frame is an 802.2 frame
1263 * (i.e., that the length/type field is
1264 * a length field, <= ETHERMTU) and
1265 * then check the DSAP.
1267 b0
= gen_cmp_gt(off_linktype
, BPF_H
, ETHERMTU
);
1269 b1
= gen_cmp(off_linktype
+ 2, BPF_B
, (bpf_int32
)proto
);
1274 * This is an Ethernet type, so compare
1275 * the length/type field with it (if
1276 * the frame is an 802.2 frame, the length
1277 * field will be <= ETHERMTU, and, as
1278 * "proto" is > ETHERMTU, this test
1279 * will fail and the frame won't match,
1280 * which is what we want).
1282 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1288 * Generate code to match a particular packet type.
1290 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1291 * value, if <= ETHERMTU. We use that to determine whether to
1292 * match the type field or to check the type field for the special
1293 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1295 static struct block
*
1296 gen_linux_sll_linktype(proto
)
1299 struct block
*b0
, *b1
;
1305 case LLCSAP_NETBEUI
:
1307 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1308 * so we check the DSAP and SSAP.
1310 * LLCSAP_IP checks for IP-over-802.2, rather
1311 * than IP-over-Ethernet or IP-over-SNAP.
1313 * XXX - should we check both the DSAP and the
1314 * SSAP, like this, or should we check just the
1315 * DSAP, as we do for other types <= ETHERMTU
1316 * (i.e., other SAP values)?
1318 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1319 b1
= gen_cmp(off_linktype
+ 2, BPF_H
, (bpf_int32
)
1320 ((proto
<< 8) | proto
));
1326 * Ethernet_II frames, which are Ethernet
1327 * frames with a frame type of ETHERTYPE_IPX;
1329 * Ethernet_802.3 frames, which have a frame
1330 * type of LINUX_SLL_P_802_3;
1332 * Ethernet_802.2 frames, which are 802.3
1333 * frames with an 802.2 LLC header (i.e, have
1334 * a frame type of LINUX_SLL_P_802_2) and
1335 * with the IPX LSAP as the DSAP in the LLC
1338 * Ethernet_SNAP frames, which are 802.3
1339 * frames with an LLC header and a SNAP
1340 * header and with an OUI of 0x000000
1341 * (encapsulated Ethernet) and a protocol
1342 * ID of ETHERTYPE_IPX in the SNAP header.
1344 * First, do the checks on LINUX_SLL_P_802_2
1345 * frames; generate the check for either
1346 * Ethernet_802.2 or Ethernet_SNAP frames, and
1347 * then put a check for LINUX_SLL_P_802_2 frames
1350 b0
= gen_cmp(off_linktype
+ 2, BPF_B
,
1351 (bpf_int32
)LLCSAP_IPX
);
1352 b1
= gen_snap(0x000000, ETHERTYPE_IPX
,
1355 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1359 * Now check for 802.3 frames and OR that with
1360 * the previous test.
1362 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_3
);
1366 * Now add the check for Ethernet_II frames, and
1367 * do that before checking for the other frame
1370 b0
= gen_cmp(off_linktype
, BPF_H
,
1371 (bpf_int32
)ETHERTYPE_IPX
);
1375 case ETHERTYPE_ATALK
:
1376 case ETHERTYPE_AARP
:
1378 * EtherTalk (AppleTalk protocols on Ethernet link
1379 * layer) may use 802.2 encapsulation.
1383 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1384 * we check for the 802.2 protocol type in the
1385 * "Ethernet type" field.
1387 b0
= gen_cmp(off_linktype
, BPF_H
, LINUX_SLL_P_802_2
);
1390 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1391 * SNAP packets with an organization code of
1392 * 0x080007 (Apple, for Appletalk) and a protocol
1393 * type of ETHERTYPE_ATALK (Appletalk).
1395 * 802.2-encapsulated ETHERTYPE_AARP packets are
1396 * SNAP packets with an organization code of
1397 * 0x000000 (encapsulated Ethernet) and a protocol
1398 * type of ETHERTYPE_AARP (Appletalk ARP).
1400 if (proto
== ETHERTYPE_ATALK
)
1401 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
,
1403 else /* proto == ETHERTYPE_AARP */
1404 b1
= gen_snap(0x000000, ETHERTYPE_AARP
,
1409 * Check for Ethernet encapsulation (Ethertalk
1410 * phase 1?); we just check for the Ethernet
1413 b0
= gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1419 if (proto
<= ETHERMTU
) {
1421 * This is an LLC SAP value, so the frames
1422 * that match would be 802.2 frames.
1423 * Check for the 802.2 protocol type
1424 * in the "Ethernet type" field, and
1425 * then check the DSAP.
1427 b0
= gen_cmp(off_linktype
, BPF_H
,
1429 b1
= gen_cmp(off_linktype
+ 2, BPF_B
,
1435 * This is an Ethernet type, so compare
1436 * the length/type field with it (if
1437 * the frame is an 802.2 frame, the length
1438 * field will be <= ETHERMTU, and, as
1439 * "proto" is > ETHERMTU, this test
1440 * will fail and the frame won't match,
1441 * which is what we want).
1443 return gen_cmp(off_linktype
, BPF_H
,
1450 * Generate code to match a particular packet type by matching the
1451 * link-layer type field or fields in the 802.2 LLC header.
1453 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1454 * value, if <= ETHERMTU.
1456 static struct block
*
1460 struct block
*b0
, *b1
, *b2
;
1465 return gen_ether_linktype(proto
);
1473 proto
= (proto
<< 8 | LLCSAP_ISONS
);
1477 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1483 case DLT_IEEE802_11
:
1484 case DLT_PRISM_HEADER
:
1485 case DLT_IEEE802_11_RADIO
:
1488 case DLT_ATM_RFC1483
:
1490 case DLT_IP_OVER_FC
:
1491 return gen_llc_linktype(proto
);
1497 * If "is_lane" is set, check for a LANE-encapsulated
1498 * version of this protocol, otherwise check for an
1499 * LLC-encapsulated version of this protocol.
1501 * We assume LANE means Ethernet, not Token Ring.
1505 * Check that the packet doesn't begin with an
1506 * LE Control marker. (We've already generated
1509 b0
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
1513 * Now generate an Ethernet test.
1515 b1
= gen_ether_linktype(proto
);
1520 * Check for LLC encapsulation and then check the
1523 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
1524 b1
= gen_llc_linktype(proto
);
1532 return gen_linux_sll_linktype(proto
);
1537 case DLT_SLIP_BSDOS
:
1540 * These types don't provide any type field; packets
1543 * XXX - for IPv4, check for a version number of 4, and,
1544 * for IPv6, check for a version number of 6?
1550 case ETHERTYPE_IPV6
:
1552 return gen_true(); /* always true */
1555 return gen_false(); /* always false */
1562 case DLT_PPP_SERIAL
:
1565 * We use Ethernet protocol types inside libpcap;
1566 * map them to the corresponding PPP protocol types.
1575 case ETHERTYPE_IPV6
:
1584 case ETHERTYPE_ATALK
:
1598 * I'm assuming the "Bridging PDU"s that go
1599 * over PPP are Spanning Tree Protocol
1613 * We use Ethernet protocol types inside libpcap;
1614 * map them to the corresponding PPP protocol types.
1619 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_IP
);
1620 b1
= gen_cmp(off_linktype
, BPF_H
, PPP_VJC
);
1622 b0
= gen_cmp(off_linktype
, BPF_H
, PPP_VJNC
);
1627 case ETHERTYPE_IPV6
:
1637 case ETHERTYPE_ATALK
:
1651 * I'm assuming the "Bridging PDU"s that go
1652 * over PPP are Spanning Tree Protocol
1668 * For DLT_NULL, the link-layer header is a 32-bit
1669 * word containing an AF_ value in *host* byte order,
1670 * and for DLT_ENC, the link-layer header begins
1671 * with a 32-bit work containing an AF_ value in
1674 * In addition, if we're reading a saved capture file,
1675 * the host byte order in the capture may not be the
1676 * same as the host byte order on this machine.
1678 * For DLT_LOOP, the link-layer header is a 32-bit
1679 * word containing an AF_ value in *network* byte order.
1681 * XXX - AF_ values may, unfortunately, be platform-
1682 * dependent; for example, FreeBSD's AF_INET6 is 24
1683 * whilst NetBSD's and OpenBSD's is 26.
1685 * This means that, when reading a capture file, just
1686 * checking for our AF_INET6 value won't work if the
1687 * capture file came from another OS.
1696 case ETHERTYPE_IPV6
:
1703 * Not a type on which we support filtering.
1704 * XXX - support those that have AF_ values
1705 * #defined on this platform, at least?
1710 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
1712 * The AF_ value is in host byte order, but
1713 * the BPF interpreter will convert it to
1714 * network byte order.
1716 * If this is a save file, and it's from a
1717 * machine with the opposite byte order to
1718 * ours, we byte-swap the AF_ value.
1720 * Then we run it through "htonl()", and
1721 * generate code to compare against the result.
1723 if (bpf_pcap
->sf
.rfile
!= NULL
&&
1724 bpf_pcap
->sf
.swapped
)
1725 proto
= SWAPLONG(proto
);
1726 proto
= htonl(proto
);
1728 return (gen_cmp(0, BPF_W
, (bpf_int32
)proto
));
1732 * af field is host byte order in contrast to the rest of
1735 if (proto
== ETHERTYPE_IP
)
1736 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1737 (bpf_int32
)AF_INET
));
1739 else if (proto
== ETHERTYPE_IPV6
)
1740 return (gen_cmp(offsetof(struct pfloghdr
, af
), BPF_B
,
1741 (bpf_int32
)AF_INET6
));
1749 case DLT_ARCNET_LINUX
:
1751 * XXX should we check for first fragment if the protocol
1760 case ETHERTYPE_IPV6
:
1761 return (gen_cmp(off_linktype
, BPF_B
,
1762 (bpf_int32
)ARCTYPE_INET6
));
1766 b0
= gen_cmp(off_linktype
, BPF_B
,
1767 (bpf_int32
)ARCTYPE_IP
);
1768 b1
= gen_cmp(off_linktype
, BPF_B
,
1769 (bpf_int32
)ARCTYPE_IP_OLD
);
1774 b0
= gen_cmp(off_linktype
, BPF_B
,
1775 (bpf_int32
)ARCTYPE_ARP
);
1776 b1
= gen_cmp(off_linktype
, BPF_B
,
1777 (bpf_int32
)ARCTYPE_ARP_OLD
);
1781 case ETHERTYPE_REVARP
:
1782 return (gen_cmp(off_linktype
, BPF_B
,
1783 (bpf_int32
)ARCTYPE_REVARP
));
1785 case ETHERTYPE_ATALK
:
1786 return (gen_cmp(off_linktype
, BPF_B
,
1787 (bpf_int32
)ARCTYPE_ATALK
));
1794 case ETHERTYPE_ATALK
:
1804 * XXX - assumes a 2-byte Frame Relay header with
1805 * DLCI and flags. What if the address is longer?
1811 * Check for the special NLPID for IP.
1813 return gen_cmp(2, BPF_H
, (0x03<<8) | 0xcc);
1816 case ETHERTYPE_IPV6
:
1818 * Check for the special NLPID for IPv6.
1820 return gen_cmp(2, BPF_H
, (0x03<<8) | 0x8e);
1825 * Check for several OSI protocols.
1827 * Frame Relay packets typically have an OSI
1828 * NLPID at the beginning; we check for each
1831 * What we check for is the NLPID and a frame
1832 * control field of UI, i.e. 0x03 followed
1835 b0
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
1836 b1
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
1837 b2
= gen_cmp(2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
1848 case DLT_JUNIPER_MLFR
:
1849 case DLT_JUNIPER_MLPPP
:
1850 case DLT_JUNIPER_ATM1
:
1851 case DLT_JUNIPER_ATM2
:
1852 /* just lets verify the magic number for now -
1853 * on ATM we may have up to 6 different encapsulations on the wire
1854 * and need a lot of heuristics to figure out that the payload
1857 * FIXME encapsulation specific BPF_ filters
1859 return gen_mcmp(0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
1861 case DLT_LINUX_IRDA
:
1862 bpf_error("IrDA link-layer type filtering not implemented");
1865 bpf_error("DOCSIS link-layer type filtering not implemented");
1867 case DLT_LINUX_LAPD
:
1868 bpf_error("LAPD link-layer type filtering not implemented");
1872 * All the types that have no encapsulation should either be
1873 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1874 * all packets are IP packets, or should be handled in some
1875 * special case, if none of them are (if some are and some
1876 * aren't, the lack of encapsulation is a problem, as we'd
1877 * have to find some other way of determining the packet type).
1879 * Therefore, if "off_linktype" is -1, there's an error.
1881 if (off_linktype
== (u_int
)-1)
1885 * Any type not handled above should always have an Ethernet
1886 * type at an offset of "off_linktype". (PPP is partially
1887 * handled above - the protocol type is mapped from the
1888 * Ethernet and LLC types we use internally to the corresponding
1889 * PPP type - but the PPP type is always specified by a value
1890 * at "off_linktype", so we don't have to do the code generation
1893 return gen_cmp(off_linktype
, BPF_H
, (bpf_int32
)proto
);
1897 * Check for an LLC SNAP packet with a given organization code and
1898 * protocol type; we check the entire contents of the 802.2 LLC and
1899 * snap headers, checking for DSAP and SSAP of SNAP and a control
1900 * field of 0x03 in the LLC header, and for the specified organization
1901 * code and protocol type in the SNAP header.
1903 static struct block
*
1904 gen_snap(orgcode
, ptype
, offset
)
1905 bpf_u_int32 orgcode
;
1909 u_char snapblock
[8];
1911 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
1912 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
1913 snapblock
[2] = 0x03; /* control = UI */
1914 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
1915 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
1916 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
1917 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
1918 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
1919 return gen_bcmp(offset
, 8, snapblock
);
1923 * Generate code to match a particular packet type, for link-layer types
1924 * using 802.2 LLC headers.
1926 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
1927 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
1929 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1930 * value, if <= ETHERMTU. We use that to determine whether to
1931 * match the DSAP or both DSAP and LSAP or to check the OUI and
1932 * protocol ID in a SNAP header.
1934 static struct block
*
1935 gen_llc_linktype(proto
)
1939 * XXX - handle token-ring variable-length header.
1945 case LLCSAP_NETBEUI
:
1947 * XXX - should we check both the DSAP and the
1948 * SSAP, like this, or should we check just the
1949 * DSAP, as we do for other types <= ETHERMTU
1950 * (i.e., other SAP values)?
1952 return gen_cmp(off_linktype
, BPF_H
, (long)
1953 ((proto
<< 8) | proto
));
1957 * XXX - are there ever SNAP frames for IPX on
1958 * non-Ethernet 802.x networks?
1960 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1962 case ETHERTYPE_ATALK
:
1964 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1965 * SNAP packets with an organization code of
1966 * 0x080007 (Apple, for Appletalk) and a protocol
1967 * type of ETHERTYPE_ATALK (Appletalk).
1969 * XXX - check for an organization code of
1970 * encapsulated Ethernet as well?
1972 return gen_snap(0x080007, ETHERTYPE_ATALK
, off_linktype
);
1976 * XXX - we don't have to check for IPX 802.3
1977 * here, but should we check for the IPX Ethertype?
1979 if (proto
<= ETHERMTU
) {
1981 * This is an LLC SAP value, so check
1984 return gen_cmp(off_linktype
, BPF_B
, (bpf_int32
)proto
);
1987 * This is an Ethernet type; we assume that it's
1988 * unlikely that it'll appear in the right place
1989 * at random, and therefore check only the
1990 * location that would hold the Ethernet type
1991 * in a SNAP frame with an organization code of
1992 * 0x000000 (encapsulated Ethernet).
1994 * XXX - if we were to check for the SNAP DSAP and
1995 * LSAP, as per XXX, and were also to check for an
1996 * organization code of 0x000000 (encapsulated
1997 * Ethernet), we'd do
1999 * return gen_snap(0x000000, proto,
2002 * here; for now, we don't, as per the above.
2003 * I don't know whether it's worth the extra CPU
2004 * time to do the right check or not.
2006 return gen_cmp(off_linktype
+6, BPF_H
, (bpf_int32
)proto
);
2011 static struct block
*
2012 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2016 u_int src_off
, dst_off
;
2018 struct block
*b0
, *b1
;
2032 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2033 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2039 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2040 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2047 b0
= gen_linktype(proto
);
2048 b1
= gen_mcmp(offset
, BPF_W
, (bpf_int32
)addr
, mask
);
2054 static struct block
*
2055 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
2056 struct in6_addr
*addr
;
2057 struct in6_addr
*mask
;
2059 u_int src_off
, dst_off
;
2061 struct block
*b0
, *b1
;
2076 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2077 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2083 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
2084 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
2091 /* this order is important */
2092 a
= (u_int32_t
*)addr
;
2093 m
= (u_int32_t
*)mask
;
2094 b1
= gen_mcmp(offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
2095 b0
= gen_mcmp(offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
2097 b0
= gen_mcmp(offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
2099 b0
= gen_mcmp(offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
2101 b0
= gen_linktype(proto
);
2107 static struct block
*
2108 gen_ehostop(eaddr
, dir
)
2109 register const u_char
*eaddr
;
2112 register struct block
*b0
, *b1
;
2116 return gen_bcmp(off_mac
+ 6, 6, eaddr
);
2119 return gen_bcmp(off_mac
+ 0, 6, eaddr
);
2122 b0
= gen_ehostop(eaddr
, Q_SRC
);
2123 b1
= gen_ehostop(eaddr
, Q_DST
);
2129 b0
= gen_ehostop(eaddr
, Q_SRC
);
2130 b1
= gen_ehostop(eaddr
, Q_DST
);
2139 * Like gen_ehostop, but for DLT_FDDI
2141 static struct block
*
2142 gen_fhostop(eaddr
, dir
)
2143 register const u_char
*eaddr
;
2146 struct block
*b0
, *b1
;
2151 return gen_bcmp(6 + 1 + pcap_fddipad
, 6, eaddr
);
2153 return gen_bcmp(6 + 1, 6, eaddr
);
2158 return gen_bcmp(0 + 1 + pcap_fddipad
, 6, eaddr
);
2160 return gen_bcmp(0 + 1, 6, eaddr
);
2164 b0
= gen_fhostop(eaddr
, Q_SRC
);
2165 b1
= gen_fhostop(eaddr
, Q_DST
);
2171 b0
= gen_fhostop(eaddr
, Q_SRC
);
2172 b1
= gen_fhostop(eaddr
, Q_DST
);
2181 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2183 static struct block
*
2184 gen_thostop(eaddr
, dir
)
2185 register const u_char
*eaddr
;
2188 register struct block
*b0
, *b1
;
2192 return gen_bcmp(8, 6, eaddr
);
2195 return gen_bcmp(2, 6, eaddr
);
2198 b0
= gen_thostop(eaddr
, Q_SRC
);
2199 b1
= gen_thostop(eaddr
, Q_DST
);
2205 b0
= gen_thostop(eaddr
, Q_SRC
);
2206 b1
= gen_thostop(eaddr
, Q_DST
);
2215 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2217 static struct block
*
2218 gen_wlanhostop(eaddr
, dir
)
2219 register const u_char
*eaddr
;
2222 register struct block
*b0
, *b1
, *b2
;
2223 register struct slist
*s
;
2230 * For control frames, there is no SA.
2232 * For management frames, SA is at an
2233 * offset of 10 from the beginning of
2236 * For data frames, SA is at an offset
2237 * of 10 from the beginning of the packet
2238 * if From DS is clear, at an offset of
2239 * 16 from the beginning of the packet
2240 * if From DS is set and To DS is clear,
2241 * and an offset of 24 from the beginning
2242 * of the packet if From DS is set and To DS
2247 * Generate the tests to be done for data frames
2250 * First, check for To DS set, i.e. check "link[1] & 0x01".
2252 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2254 b1
= new_block(JMP(BPF_JSET
));
2255 b1
->s
.k
= 0x01; /* To DS */
2259 * If To DS is set, the SA is at 24.
2261 b0
= gen_bcmp(24, 6, eaddr
);
2265 * Now, check for To DS not set, i.e. check
2266 * "!(link[1] & 0x01)".
2268 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2270 b2
= new_block(JMP(BPF_JSET
));
2271 b2
->s
.k
= 0x01; /* To DS */
2276 * If To DS is not set, the SA is at 16.
2278 b1
= gen_bcmp(16, 6, eaddr
);
2282 * Now OR together the last two checks. That gives
2283 * the complete set of checks for data frames with
2289 * Now check for From DS being set, and AND that with
2290 * the ORed-together checks.
2292 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2294 b1
= new_block(JMP(BPF_JSET
));
2295 b1
->s
.k
= 0x02; /* From DS */
2300 * Now check for data frames with From DS not set.
2302 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2304 b2
= new_block(JMP(BPF_JSET
));
2305 b2
->s
.k
= 0x02; /* From DS */
2310 * If From DS isn't set, the SA is at 10.
2312 b1
= gen_bcmp(10, 6, eaddr
);
2316 * Now OR together the checks for data frames with
2317 * From DS not set and for data frames with From DS
2318 * set; that gives the checks done for data frames.
2323 * Now check for a data frame.
2324 * I.e, check "link[0] & 0x08".
2326 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2328 b1
= new_block(JMP(BPF_JSET
));
2333 * AND that with the checks done for data frames.
2338 * If the high-order bit of the type value is 0, this
2339 * is a management frame.
2340 * I.e, check "!(link[0] & 0x08)".
2342 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2344 b2
= new_block(JMP(BPF_JSET
));
2350 * For management frames, the SA is at 10.
2352 b1
= gen_bcmp(10, 6, eaddr
);
2356 * OR that with the checks done for data frames.
2357 * That gives the checks done for management and
2363 * If the low-order bit of the type value is 1,
2364 * this is either a control frame or a frame
2365 * with a reserved type, and thus not a
2368 * I.e., check "!(link[0] & 0x04)".
2370 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2372 b1
= new_block(JMP(BPF_JSET
));
2378 * AND that with the checks for data and management
2388 * For control frames, there is no DA.
2390 * For management frames, DA is at an
2391 * offset of 4 from the beginning of
2394 * For data frames, DA is at an offset
2395 * of 4 from the beginning of the packet
2396 * if To DS is clear and at an offset of
2397 * 16 from the beginning of the packet
2402 * Generate the tests to be done for data frames.
2404 * First, check for To DS set, i.e. "link[1] & 0x01".
2406 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2408 b1
= new_block(JMP(BPF_JSET
));
2409 b1
->s
.k
= 0x01; /* To DS */
2413 * If To DS is set, the DA is at 16.
2415 b0
= gen_bcmp(16, 6, eaddr
);
2419 * Now, check for To DS not set, i.e. check
2420 * "!(link[1] & 0x01)".
2422 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2424 b2
= new_block(JMP(BPF_JSET
));
2425 b2
->s
.k
= 0x01; /* To DS */
2430 * If To DS is not set, the DA is at 4.
2432 b1
= gen_bcmp(4, 6, eaddr
);
2436 * Now OR together the last two checks. That gives
2437 * the complete set of checks for data frames.
2442 * Now check for a data frame.
2443 * I.e, check "link[0] & 0x08".
2445 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2447 b1
= new_block(JMP(BPF_JSET
));
2452 * AND that with the checks done for data frames.
2457 * If the high-order bit of the type value is 0, this
2458 * is a management frame.
2459 * I.e, check "!(link[0] & 0x08)".
2461 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2463 b2
= new_block(JMP(BPF_JSET
));
2469 * For management frames, the DA is at 4.
2471 b1
= gen_bcmp(4, 6, eaddr
);
2475 * OR that with the checks done for data frames.
2476 * That gives the checks done for management and
2482 * If the low-order bit of the type value is 1,
2483 * this is either a control frame or a frame
2484 * with a reserved type, and thus not a
2487 * I.e., check "!(link[0] & 0x04)".
2489 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2491 b1
= new_block(JMP(BPF_JSET
));
2497 * AND that with the checks for data and management
2504 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2505 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2511 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
2512 b1
= gen_wlanhostop(eaddr
, Q_DST
);
2521 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2522 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2523 * as the RFC states.)
2525 static struct block
*
2526 gen_ipfchostop(eaddr
, dir
)
2527 register const u_char
*eaddr
;
2530 register struct block
*b0
, *b1
;
2534 return gen_bcmp(10, 6, eaddr
);
2537 return gen_bcmp(2, 6, eaddr
);
2540 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2541 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2547 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
2548 b1
= gen_ipfchostop(eaddr
, Q_DST
);
2557 * This is quite tricky because there may be pad bytes in front of the
2558 * DECNET header, and then there are two possible data packet formats that
2559 * carry both src and dst addresses, plus 5 packet types in a format that
2560 * carries only the src node, plus 2 types that use a different format and
2561 * also carry just the src node.
2565 * Instead of doing those all right, we just look for data packets with
2566 * 0 or 1 bytes of padding. If you want to look at other packets, that
2567 * will require a lot more hacking.
2569 * To add support for filtering on DECNET "areas" (network numbers)
2570 * one would want to add a "mask" argument to this routine. That would
2571 * make the filter even more inefficient, although one could be clever
2572 * and not generate masking instructions if the mask is 0xFFFF.
2574 static struct block
*
2575 gen_dnhostop(addr
, dir
, base_off
)
2580 struct block
*b0
, *b1
, *b2
, *tmp
;
2581 u_int offset_lh
; /* offset if long header is received */
2582 u_int offset_sh
; /* offset if short header is received */
2587 offset_sh
= 1; /* follows flags */
2588 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
2592 offset_sh
= 3; /* follows flags, dstnode */
2593 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2597 /* Inefficient because we do our Calvinball dance twice */
2598 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2599 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2605 /* Inefficient because we do our Calvinball dance twice */
2606 b0
= gen_dnhostop(addr
, Q_SRC
, base_off
);
2607 b1
= gen_dnhostop(addr
, Q_DST
, base_off
);
2612 bpf_error("ISO host filtering not implemented");
2617 b0
= gen_linktype(ETHERTYPE_DN
);
2618 /* Check for pad = 1, long header case */
2619 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2620 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
2621 b1
= gen_cmp(base_off
+ 2 + 1 + offset_lh
,
2622 BPF_H
, (bpf_int32
)ntohs(addr
));
2624 /* Check for pad = 0, long header case */
2625 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
2626 b2
= gen_cmp(base_off
+ 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2629 /* Check for pad = 1, short header case */
2630 tmp
= gen_mcmp(base_off
+ 2, BPF_H
,
2631 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
2632 b2
= gen_cmp(base_off
+ 2 + 1 + offset_sh
,
2633 BPF_H
, (bpf_int32
)ntohs(addr
));
2636 /* Check for pad = 0, short header case */
2637 tmp
= gen_mcmp(base_off
+ 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
2638 b2
= gen_cmp(base_off
+ 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs(addr
));
2642 /* Combine with test for linktype */
2647 static struct block
*
2648 gen_host(addr
, mask
, proto
, dir
)
2654 struct block
*b0
, *b1
;
2659 b0
= gen_host(addr
, mask
, Q_IP
, dir
);
2660 if (off_linktype
!= (u_int
)-1) {
2661 b1
= gen_host(addr
, mask
, Q_ARP
, dir
);
2663 b0
= gen_host(addr
, mask
, Q_RARP
, dir
);
2669 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
,
2670 off_nl
+ 12, off_nl
+ 16);
2673 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
,
2674 off_nl
+ 14, off_nl
+ 24);
2677 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
,
2678 off_nl
+ 14, off_nl
+ 24);
2681 bpf_error("'tcp' modifier applied to host");
2684 bpf_error("'sctp' modifier applied to host");
2687 bpf_error("'udp' modifier applied to host");
2690 bpf_error("'icmp' modifier applied to host");
2693 bpf_error("'igmp' modifier applied to host");
2696 bpf_error("'igrp' modifier applied to host");
2699 bpf_error("'pim' modifier applied to host");
2702 bpf_error("'vrrp' modifier applied to host");
2705 bpf_error("ATALK host filtering not implemented");
2708 bpf_error("AARP host filtering not implemented");
2711 return gen_dnhostop(addr
, dir
, off_nl
);
2714 bpf_error("SCA host filtering not implemented");
2717 bpf_error("LAT host filtering not implemented");
2720 bpf_error("MOPDL host filtering not implemented");
2723 bpf_error("MOPRC host filtering not implemented");
2727 bpf_error("'ip6' modifier applied to ip host");
2730 bpf_error("'icmp6' modifier applied to host");
2734 bpf_error("'ah' modifier applied to host");
2737 bpf_error("'esp' modifier applied to host");
2740 bpf_error("ISO host filtering not implemented");
2743 bpf_error("'esis' modifier applied to host");
2746 bpf_error("'isis' modifier applied to host");
2749 bpf_error("'clnp' modifier applied to host");
2752 bpf_error("'stp' modifier applied to host");
2755 bpf_error("IPX host filtering not implemented");
2758 bpf_error("'netbeui' modifier applied to host");
2767 static struct block
*
2768 gen_host6(addr
, mask
, proto
, dir
)
2769 struct in6_addr
*addr
;
2770 struct in6_addr
*mask
;
2777 return gen_host6(addr
, mask
, Q_IPV6
, dir
);
2780 bpf_error("'ip' modifier applied to ip6 host");
2783 bpf_error("'rarp' modifier applied to ip6 host");
2786 bpf_error("'arp' modifier applied to ip6 host");
2789 bpf_error("'sctp' modifier applied to host");
2792 bpf_error("'tcp' modifier applied to host");
2795 bpf_error("'udp' modifier applied to host");
2798 bpf_error("'icmp' modifier applied to host");
2801 bpf_error("'igmp' modifier applied to host");
2804 bpf_error("'igrp' modifier applied to host");
2807 bpf_error("'pim' modifier applied to host");
2810 bpf_error("'vrrp' modifier applied to host");
2813 bpf_error("ATALK host filtering not implemented");
2816 bpf_error("AARP host filtering not implemented");
2819 bpf_error("'decnet' modifier applied to ip6 host");
2822 bpf_error("SCA host filtering not implemented");
2825 bpf_error("LAT host filtering not implemented");
2828 bpf_error("MOPDL host filtering not implemented");
2831 bpf_error("MOPRC host filtering not implemented");
2834 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
,
2835 off_nl
+ 8, off_nl
+ 24);
2838 bpf_error("'icmp6' modifier applied to host");
2841 bpf_error("'ah' modifier applied to host");
2844 bpf_error("'esp' modifier applied to host");
2847 bpf_error("ISO host filtering not implemented");
2850 bpf_error("'esis' modifier applied to host");
2853 bpf_error("'isis' modifier applied to host");
2856 bpf_error("'clnp' modifier applied to host");
2859 bpf_error("'stp' modifier applied to host");
2862 bpf_error("IPX host filtering not implemented");
2865 bpf_error("'netbeui' modifier applied to host");
2875 static struct block
*
2876 gen_gateway(eaddr
, alist
, proto
, dir
)
2877 const u_char
*eaddr
;
2878 bpf_u_int32
**alist
;
2882 struct block
*b0
, *b1
, *tmp
;
2885 bpf_error("direction applied to 'gateway'");
2892 if (linktype
== DLT_EN10MB
)
2893 b0
= gen_ehostop(eaddr
, Q_OR
);
2894 else if (linktype
== DLT_FDDI
)
2895 b0
= gen_fhostop(eaddr
, Q_OR
);
2896 else if (linktype
== DLT_IEEE802
)
2897 b0
= gen_thostop(eaddr
, Q_OR
);
2898 else if (linktype
== DLT_IEEE802_11
)
2899 b0
= gen_wlanhostop(eaddr
, Q_OR
);
2900 else if (linktype
== DLT_SUNATM
&& is_lane
) {
2902 * Check that the packet doesn't begin with an
2903 * LE Control marker. (We've already generated
2906 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2910 * Now check the MAC address.
2912 b0
= gen_ehostop(eaddr
, Q_OR
);
2914 } else if (linktype
== DLT_IP_OVER_FC
)
2915 b0
= gen_ipfchostop(eaddr
, Q_OR
);
2918 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2920 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2922 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
);
2930 bpf_error("illegal modifier of 'gateway'");
2936 gen_proto_abbrev(proto
)
2945 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
2947 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
2953 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
2955 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
2961 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
2963 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
2969 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
2972 #ifndef IPPROTO_IGMP
2973 #define IPPROTO_IGMP 2
2977 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
2980 #ifndef IPPROTO_IGRP
2981 #define IPPROTO_IGRP 9
2984 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
2988 #define IPPROTO_PIM 103
2992 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
2994 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
2999 #ifndef IPPROTO_VRRP
3000 #define IPPROTO_VRRP 112
3004 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
3008 b1
= gen_linktype(ETHERTYPE_IP
);
3012 b1
= gen_linktype(ETHERTYPE_ARP
);
3016 b1
= gen_linktype(ETHERTYPE_REVARP
);
3020 bpf_error("link layer applied in wrong context");
3023 b1
= gen_linktype(ETHERTYPE_ATALK
);
3027 b1
= gen_linktype(ETHERTYPE_AARP
);
3031 b1
= gen_linktype(ETHERTYPE_DN
);
3035 b1
= gen_linktype(ETHERTYPE_SCA
);
3039 b1
= gen_linktype(ETHERTYPE_LAT
);
3043 b1
= gen_linktype(ETHERTYPE_MOPDL
);
3047 b1
= gen_linktype(ETHERTYPE_MOPRC
);
3052 b1
= gen_linktype(ETHERTYPE_IPV6
);
3055 #ifndef IPPROTO_ICMPV6
3056 #define IPPROTO_ICMPV6 58
3059 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
3064 #define IPPROTO_AH 51
3067 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
3069 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
3075 #define IPPROTO_ESP 50
3078 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
3080 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
3086 b1
= gen_linktype(LLCSAP_ISONS
);
3090 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
3094 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
3097 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
3098 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3099 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3101 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3103 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3105 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3109 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
3110 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3111 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
3113 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3115 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3117 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3121 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
3122 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3123 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
3125 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
3130 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
3131 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
3136 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3137 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3139 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3141 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3146 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
3147 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
3152 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
3153 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
3158 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
3162 b1
= gen_linktype(LLCSAP_8021D
);
3166 b1
= gen_linktype(LLCSAP_IPX
);
3170 b1
= gen_linktype(LLCSAP_NETBEUI
);
3179 static struct block
*
3186 s
= new_stmt(BPF_LD
|BPF_H
|BPF_ABS
);
3187 s
->s
.k
= off_nl
+ 6;
3188 b
= new_block(JMP(BPF_JSET
));
3196 static struct block
*
3197 gen_portatom(off
, v
)
3204 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3207 s
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3208 s
->next
->s
.k
= off_nl
+ off
;
3210 b
= new_block(JMP(BPF_JEQ
));
3218 static struct block
*
3219 gen_portatom6(off
, v
)
3223 return gen_cmp(off_nl
+ 40 + off
, BPF_H
, v
);
3228 gen_portop(port
, proto
, dir
)
3229 int port
, proto
, dir
;
3231 struct block
*b0
, *b1
, *tmp
;
3233 /* ip proto 'proto' */
3234 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3240 b1
= gen_portatom(0, (bpf_int32
)port
);
3244 b1
= gen_portatom(2, (bpf_int32
)port
);
3249 tmp
= gen_portatom(0, (bpf_int32
)port
);
3250 b1
= gen_portatom(2, (bpf_int32
)port
);
3255 tmp
= gen_portatom(0, (bpf_int32
)port
);
3256 b1
= gen_portatom(2, (bpf_int32
)port
);
3268 static struct block
*
3269 gen_port(port
, ip_proto
, dir
)
3274 struct block
*b0
, *b1
, *tmp
;
3279 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3280 * not LLC encapsulation with LLCSAP_IP.
3282 * For IEEE 802 networks - which includes 802.5 token ring
3283 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3284 * says that SNAP encapsulation is used, not LLC encapsulation
3287 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3288 * RFC 2225 say that SNAP encapsulation is used, not LLC
3289 * encapsulation with LLCSAP_IP.
3291 * So we always check for ETHERTYPE_IP.
3293 b0
= gen_linktype(ETHERTYPE_IP
);
3299 b1
= gen_portop(port
, ip_proto
, dir
);
3303 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
3304 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
3306 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
3319 gen_portop6(port
, proto
, dir
)
3320 int port
, proto
, dir
;
3322 struct block
*b0
, *b1
, *tmp
;
3324 /* ip6 proto 'proto' */
3325 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3329 b1
= gen_portatom6(0, (bpf_int32
)port
);
3333 b1
= gen_portatom6(2, (bpf_int32
)port
);
3338 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3339 b1
= gen_portatom6(2, (bpf_int32
)port
);
3344 tmp
= gen_portatom6(0, (bpf_int32
)port
);
3345 b1
= gen_portatom6(2, (bpf_int32
)port
);
3357 static struct block
*
3358 gen_port6(port
, ip_proto
, dir
)
3363 struct block
*b0
, *b1
, *tmp
;
3365 /* link proto ip6 */
3366 b0
= gen_linktype(ETHERTYPE_IPV6
);
3372 b1
= gen_portop6(port
, ip_proto
, dir
);
3376 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
3377 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
3379 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
3391 /* gen_portrange code */
3393 gen_portrangeatom(off
, v1
, v2
)
3397 struct slist
*s1
, *s2
;
3398 struct block
*b1
, *b2
;
3402 * Reverse the order of the ports, so v1 is the lower one.
3410 s1
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3413 s1
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3414 s1
->next
->s
.k
= off_nl
+ off
;
3416 b1
= new_block(JMP(BPF_JGE
));
3420 s2
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3423 s2
->next
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
3424 s2
->next
->s
.k
= off_nl
+ off
;
3426 b2
= new_block(JMP(BPF_JGT
));
3437 gen_portrangeop(port1
, port2
, proto
, dir
)
3442 struct block
*b0
, *b1
, *tmp
;
3444 /* ip proto 'proto' */
3445 tmp
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)proto
);
3451 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3455 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3460 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3461 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3466 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3467 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3479 static struct block
*
3480 gen_portrange(port1
, port2
, ip_proto
, dir
)
3485 struct block
*b0
, *b1
, *tmp
;
3488 b0
= gen_linktype(ETHERTYPE_IP
);
3494 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
3498 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
3499 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
3501 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
3514 gen_portrangeatom6(off
, v1
, v2
)
3518 struct slist
*s1
, *s2
;
3519 struct block
*b1
, *b2
;
3523 * Reverse the order of the ports, so v1 is the lower one.
3532 s1
= new_stmt(BPF_LD
|BPF_ABS
|BPF_H
);
3533 s1
->s
.k
= off_nl
+ 40 + off
;
3535 b1
= new_block(JMP(BPF_JGE
));
3539 s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_H
);
3540 s2
->s
.k
= off_nl
+ 40 + off
;
3542 b2
= new_block(JMP(BPF_JGT
));
3553 gen_portrangeop6(port1
, port2
, proto
, dir
)
3558 struct block
*b0
, *b1
, *tmp
;
3560 /* ip6 proto 'proto' */
3561 b0
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)proto
);
3565 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3569 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3574 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3575 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3580 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
3581 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
3593 static struct block
*
3594 gen_portrange6(port1
, port2
, ip_proto
, dir
)
3599 struct block
*b0
, *b1
, *tmp
;
3601 /* link proto ip6 */
3602 b0
= gen_linktype(ETHERTYPE_IPV6
);
3608 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
3612 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
3613 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
3615 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
3628 lookup_proto(name
, proto
)
3629 register const char *name
;
3639 v
= pcap_nametoproto(name
);
3640 if (v
== PROTO_UNDEF
)
3641 bpf_error("unknown ip proto '%s'", name
);
3645 /* XXX should look up h/w protocol type based on linktype */
3646 v
= pcap_nametoeproto(name
);
3647 if (v
== PROTO_UNDEF
) {
3648 v
= pcap_nametollc(name
);
3649 if (v
== PROTO_UNDEF
)
3650 bpf_error("unknown ether proto '%s'", name
);
3655 if (strcmp(name
, "esis") == 0)
3657 else if (strcmp(name
, "isis") == 0)
3659 else if (strcmp(name
, "clnp") == 0)
3662 bpf_error("unknown osi proto '%s'", name
);
3682 static struct block
*
3683 gen_protochain(v
, proto
, dir
)
3688 #ifdef NO_PROTOCHAIN
3689 return gen_proto(v
, proto
, dir
);
3691 struct block
*b0
, *b
;
3692 struct slist
*s
[100];
3693 int fix2
, fix3
, fix4
, fix5
;
3694 int ahcheck
, again
, end
;
3696 int reg2
= alloc_reg();
3698 memset(s
, 0, sizeof(s
));
3699 fix2
= fix3
= fix4
= fix5
= 0;
3706 b0
= gen_protochain(v
, Q_IP
, dir
);
3707 b
= gen_protochain(v
, Q_IPV6
, dir
);
3711 bpf_error("bad protocol applied for 'protochain'");
3715 no_optimize
= 1; /*this code is not compatible with optimzer yet */
3718 * s[0] is a dummy entry to protect other BPF insn from damage
3719 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3720 * hard to find interdependency made by jump table fixup.
3723 s
[i
] = new_stmt(0); /*dummy*/
3728 b0
= gen_linktype(ETHERTYPE_IP
);
3731 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3732 s
[i
]->s
.k
= off_nl
+ 9;
3734 /* X = ip->ip_hl << 2 */
3735 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
3741 b0
= gen_linktype(ETHERTYPE_IPV6
);
3743 /* A = ip6->ip_nxt */
3744 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
3745 s
[i
]->s
.k
= off_nl
+ 6;
3747 /* X = sizeof(struct ip6_hdr) */
3748 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
3754 bpf_error("unsupported proto to gen_protochain");
3758 /* again: if (A == v) goto end; else fall through; */
3760 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3762 s
[i
]->s
.jt
= NULL
; /*later*/
3763 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3767 #ifndef IPPROTO_NONE
3768 #define IPPROTO_NONE 59
3770 /* if (A == IPPROTO_NONE) goto end */
3771 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3772 s
[i
]->s
.jt
= NULL
; /*later*/
3773 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3774 s
[i
]->s
.k
= IPPROTO_NONE
;
3775 s
[fix5
]->s
.jf
= s
[i
];
3780 if (proto
== Q_IPV6
) {
3781 int v6start
, v6end
, v6advance
, j
;
3784 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3785 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3786 s
[i
]->s
.jt
= NULL
; /*later*/
3787 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3788 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
3789 s
[fix2
]->s
.jf
= s
[i
];
3791 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3792 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3793 s
[i
]->s
.jt
= NULL
; /*later*/
3794 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3795 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
3797 /* if (A == IPPROTO_ROUTING) goto v6advance */
3798 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3799 s
[i
]->s
.jt
= NULL
; /*later*/
3800 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
3801 s
[i
]->s
.k
= IPPROTO_ROUTING
;
3803 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3804 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3805 s
[i
]->s
.jt
= NULL
; /*later*/
3806 s
[i
]->s
.jf
= NULL
; /*later*/
3807 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
3818 * X = X + (P[X + 1] + 1) * 8;
3821 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3823 /* A = P[X + packet head] */
3824 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3828 s
[i
] = new_stmt(BPF_ST
);
3832 s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3835 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3839 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3841 /* A = P[X + packet head]; */
3842 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3846 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3850 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3854 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3857 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3861 /* goto again; (must use BPF_JA for backward jump) */
3862 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3863 s
[i
]->s
.k
= again
- i
- 1;
3864 s
[i
- 1]->s
.jf
= s
[i
];
3868 for (j
= v6start
; j
<= v6end
; j
++)
3869 s
[j
]->s
.jt
= s
[v6advance
];
3874 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3876 s
[fix2
]->s
.jf
= s
[i
];
3882 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3883 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
3884 s
[i
]->s
.jt
= NULL
; /*later*/
3885 s
[i
]->s
.jf
= NULL
; /*later*/
3886 s
[i
]->s
.k
= IPPROTO_AH
;
3888 s
[fix3
]->s
.jf
= s
[ahcheck
];
3895 * X = X + (P[X + 1] + 2) * 4;
3898 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3900 /* A = P[X + packet head]; */
3901 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3905 s
[i
] = new_stmt(BPF_ST
);
3909 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
3912 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3916 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3918 /* A = P[X + packet head] */
3919 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
3923 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3927 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
3931 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
3934 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
3938 /* goto again; (must use BPF_JA for backward jump) */
3939 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
3940 s
[i
]->s
.k
= again
- i
- 1;
3945 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
3947 s
[fix2
]->s
.jt
= s
[end
];
3948 s
[fix4
]->s
.jf
= s
[end
];
3949 s
[fix5
]->s
.jt
= s
[end
];
3956 for (i
= 0; i
< max
- 1; i
++)
3957 s
[i
]->next
= s
[i
+ 1];
3958 s
[max
- 1]->next
= NULL
;
3963 b
= new_block(JMP(BPF_JEQ
));
3964 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
3975 * Generate code that checks whether the packet is a packet for protocol
3976 * <proto> and whether the type field in that protocol's header has
3977 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
3978 * IP packet and checks the protocol number in the IP header against <v>.
3980 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
3981 * against Q_IP and Q_IPV6.
3983 static struct block
*
3984 gen_proto(v
, proto
, dir
)
3989 struct block
*b0
, *b1
;
3991 if (dir
!= Q_DEFAULT
)
3992 bpf_error("direction applied to 'proto'");
3997 b0
= gen_proto(v
, Q_IP
, dir
);
3998 b1
= gen_proto(v
, Q_IPV6
, dir
);
4006 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
4007 * not LLC encapsulation with LLCSAP_IP.
4009 * For IEEE 802 networks - which includes 802.5 token ring
4010 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
4011 * says that SNAP encapsulation is used, not LLC encapsulation
4014 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
4015 * RFC 2225 say that SNAP encapsulation is used, not LLC
4016 * encapsulation with LLCSAP_IP.
4018 * So we always check for ETHERTYPE_IP.
4020 b0
= gen_linktype(ETHERTYPE_IP
);
4022 b1
= gen_cmp(off_nl
+ 9, BPF_B
, (bpf_int32
)v
);
4024 b1
= gen_protochain(v
, Q_IP
);
4034 * Frame Relay packets typically have an OSI
4035 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
4036 * generates code to check for all the OSI
4037 * NLPIDs, so calling it and then adding a check
4038 * for the particular NLPID for which we're
4039 * looking is bogus, as we can just check for
4042 * What we check for is the NLPID and a frame
4043 * control field value of UI, i.e. 0x03 followed
4046 * XXX - assumes a 2-byte Frame Relay header with
4047 * DLCI and flags. What if the address is longer?
4049 * XXX - what about SNAP-encapsulated frames?
4051 return gen_cmp(2, BPF_H
, (0x03<<8) | v
);
4057 * Cisco uses an Ethertype lookalike - for OSI,
4060 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
4061 /* OSI in C-HDLC is stuffed with a fudge byte */
4062 b1
= gen_cmp(off_nl_nosnap
+1, BPF_B
, (long)v
);
4067 b0
= gen_linktype(LLCSAP_ISONS
);
4068 b1
= gen_cmp(off_nl_nosnap
, BPF_B
, (long)v
);
4074 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
4076 * 4 is the offset of the PDU type relative to the IS-IS
4079 b1
= gen_cmp(off_nl_nosnap
+4, BPF_B
, (long)v
);
4084 bpf_error("arp does not encapsulate another protocol");
4088 bpf_error("rarp does not encapsulate another protocol");
4092 bpf_error("atalk encapsulation is not specifiable");
4096 bpf_error("decnet encapsulation is not specifiable");
4100 bpf_error("sca does not encapsulate another protocol");
4104 bpf_error("lat does not encapsulate another protocol");
4108 bpf_error("moprc does not encapsulate another protocol");
4112 bpf_error("mopdl does not encapsulate another protocol");
4116 return gen_linktype(v
);
4119 bpf_error("'udp proto' is bogus");
4123 bpf_error("'tcp proto' is bogus");
4127 bpf_error("'sctp proto' is bogus");
4131 bpf_error("'icmp proto' is bogus");
4135 bpf_error("'igmp proto' is bogus");
4139 bpf_error("'igrp proto' is bogus");
4143 bpf_error("'pim proto' is bogus");
4147 bpf_error("'vrrp proto' is bogus");
4152 b0
= gen_linktype(ETHERTYPE_IPV6
);
4154 b1
= gen_cmp(off_nl
+ 6, BPF_B
, (bpf_int32
)v
);
4156 b1
= gen_protochain(v
, Q_IPV6
);
4162 bpf_error("'icmp6 proto' is bogus");
4166 bpf_error("'ah proto' is bogus");
4169 bpf_error("'ah proto' is bogus");
4172 bpf_error("'stp proto' is bogus");
4175 bpf_error("'ipx proto' is bogus");
4178 bpf_error("'netbeui proto' is bogus");
4189 register const char *name
;
4192 int proto
= q
.proto
;
4196 bpf_u_int32 mask
, addr
;
4198 bpf_u_int32
**alist
;
4201 struct sockaddr_in
*sin
;
4202 struct sockaddr_in6
*sin6
;
4203 struct addrinfo
*res
, *res0
;
4204 struct in6_addr mask128
;
4206 struct block
*b
, *tmp
;
4207 int port
, real_proto
;
4213 addr
= pcap_nametonetaddr(name
);
4215 bpf_error("unknown network '%s'", name
);
4216 /* Left justify network addr and calculate its network mask */
4218 while (addr
&& (addr
& 0xff000000) == 0) {
4222 return gen_host(addr
, mask
, proto
, dir
);
4226 if (proto
== Q_LINK
) {
4230 eaddr
= pcap_ether_hostton(name
);
4233 "unknown ether host '%s'", name
);
4234 b
= gen_ehostop(eaddr
, dir
);
4239 eaddr
= pcap_ether_hostton(name
);
4242 "unknown FDDI host '%s'", name
);
4243 b
= gen_fhostop(eaddr
, dir
);
4248 eaddr
= pcap_ether_hostton(name
);
4251 "unknown token ring host '%s'", name
);
4252 b
= gen_thostop(eaddr
, dir
);
4256 case DLT_IEEE802_11
:
4257 eaddr
= pcap_ether_hostton(name
);
4260 "unknown 802.11 host '%s'", name
);
4261 b
= gen_wlanhostop(eaddr
, dir
);
4265 case DLT_IP_OVER_FC
:
4266 eaddr
= pcap_ether_hostton(name
);
4269 "unknown Fibre Channel host '%s'", name
);
4270 b
= gen_ipfchostop(eaddr
, dir
);
4279 * Check that the packet doesn't begin
4280 * with an LE Control marker. (We've
4281 * already generated a test for LANE.)
4283 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
,
4287 eaddr
= pcap_ether_hostton(name
);
4290 "unknown ether host '%s'", name
);
4291 b
= gen_ehostop(eaddr
, dir
);
4297 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
4298 } else if (proto
== Q_DECNET
) {
4299 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
4301 * I don't think DECNET hosts can be multihomed, so
4302 * there is no need to build up a list of addresses
4304 return (gen_host(dn_addr
, 0, proto
, dir
));
4307 alist
= pcap_nametoaddr(name
);
4308 if (alist
== NULL
|| *alist
== NULL
)
4309 bpf_error("unknown host '%s'", name
);
4311 if (off_linktype
== (u_int
)-1 && tproto
== Q_DEFAULT
)
4313 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
);
4315 tmp
= gen_host(**alist
++, 0xffffffff,
4322 memset(&mask128
, 0xff, sizeof(mask128
));
4323 res0
= res
= pcap_nametoaddrinfo(name
);
4325 bpf_error("unknown host '%s'", name
);
4327 tproto
= tproto6
= proto
;
4328 if (off_linktype
== -1 && tproto
== Q_DEFAULT
) {
4332 for (res
= res0
; res
; res
= res
->ai_next
) {
4333 switch (res
->ai_family
) {
4335 if (tproto
== Q_IPV6
)
4338 sin
= (struct sockaddr_in
*)
4340 tmp
= gen_host(ntohl(sin
->sin_addr
.s_addr
),
4341 0xffffffff, tproto
, dir
);
4344 if (tproto6
== Q_IP
)
4347 sin6
= (struct sockaddr_in6
*)
4349 tmp
= gen_host6(&sin6
->sin6_addr
,
4350 &mask128
, tproto6
, dir
);
4361 bpf_error("unknown host '%s'%s", name
,
4362 (proto
== Q_DEFAULT
)
4364 : " for specified address family");
4371 if (proto
!= Q_DEFAULT
&&
4372 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4373 bpf_error("illegal qualifier of 'port'");
4374 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
4375 bpf_error("unknown port '%s'", name
);
4376 if (proto
== Q_UDP
) {
4377 if (real_proto
== IPPROTO_TCP
)
4378 bpf_error("port '%s' is tcp", name
);
4379 else if (real_proto
== IPPROTO_SCTP
)
4380 bpf_error("port '%s' is sctp", name
);
4382 /* override PROTO_UNDEF */
4383 real_proto
= IPPROTO_UDP
;
4385 if (proto
== Q_TCP
) {
4386 if (real_proto
== IPPROTO_UDP
)
4387 bpf_error("port '%s' is udp", name
);
4389 else if (real_proto
== IPPROTO_SCTP
)
4390 bpf_error("port '%s' is sctp", name
);
4392 /* override PROTO_UNDEF */
4393 real_proto
= IPPROTO_TCP
;
4395 if (proto
== Q_SCTP
) {
4396 if (real_proto
== IPPROTO_UDP
)
4397 bpf_error("port '%s' is udp", name
);
4399 else if (real_proto
== IPPROTO_TCP
)
4400 bpf_error("port '%s' is tcp", name
);
4402 /* override PROTO_UNDEF */
4403 real_proto
= IPPROTO_SCTP
;
4406 return gen_port(port
, real_proto
, dir
);
4410 b
= gen_port(port
, real_proto
, dir
);
4411 gen_or(gen_port6(port
, real_proto
, dir
), b
);
4417 if (proto
!= Q_DEFAULT
&&
4418 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
4419 bpf_error("illegal qualifier of 'portrange'");
4420 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
4421 bpf_error("unknown port in range '%s'", name
);
4422 if (proto
== Q_UDP
) {
4423 if (real_proto
== IPPROTO_TCP
)
4424 bpf_error("port in range '%s' is tcp", name
);
4425 else if (real_proto
== IPPROTO_SCTP
)
4426 bpf_error("port in range '%s' is sctp", name
);
4428 /* override PROTO_UNDEF */
4429 real_proto
= IPPROTO_UDP
;
4431 if (proto
== Q_TCP
) {
4432 if (real_proto
== IPPROTO_UDP
)
4433 bpf_error("port in range '%s' is udp", name
);
4434 else if (real_proto
== IPPROTO_SCTP
)
4435 bpf_error("port in range '%s' is sctp", name
);
4437 /* override PROTO_UNDEF */
4438 real_proto
= IPPROTO_TCP
;
4440 if (proto
== Q_SCTP
) {
4441 if (real_proto
== IPPROTO_UDP
)
4442 bpf_error("port in range '%s' is udp", name
);
4443 else if (real_proto
== IPPROTO_TCP
)
4444 bpf_error("port in range '%s' is tcp", name
);
4446 /* override PROTO_UNDEF */
4447 real_proto
= IPPROTO_SCTP
;
4450 return gen_portrange(port1
, port2
, real_proto
, dir
);
4454 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
4455 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
4462 eaddr
= pcap_ether_hostton(name
);
4464 bpf_error("unknown ether host: %s", name
);
4466 alist
= pcap_nametoaddr(name
);
4467 if (alist
== NULL
|| *alist
== NULL
)
4468 bpf_error("unknown host '%s'", name
);
4469 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
4473 bpf_error("'gateway' not supported in this configuration");
4477 real_proto
= lookup_proto(name
, proto
);
4478 if (real_proto
>= 0)
4479 return gen_proto(real_proto
, proto
, dir
);
4481 bpf_error("unknown protocol: %s", name
);
4484 real_proto
= lookup_proto(name
, proto
);
4485 if (real_proto
>= 0)
4486 return gen_protochain(real_proto
, proto
, dir
);
4488 bpf_error("unknown protocol: %s", name
);
4500 gen_mcode(s1
, s2
, masklen
, q
)
4501 register const char *s1
, *s2
;
4502 register int masklen
;
4505 register int nlen
, mlen
;
4508 nlen
= __pcap_atoin(s1
, &n
);
4509 /* Promote short ipaddr */
4513 mlen
= __pcap_atoin(s2
, &m
);
4514 /* Promote short ipaddr */
4517 bpf_error("non-network bits set in \"%s mask %s\"",
4520 /* Convert mask len to mask */
4522 bpf_error("mask length must be <= 32");
4523 m
= 0xffffffff << (32 - masklen
);
4525 bpf_error("non-network bits set in \"%s/%d\"",
4532 return gen_host(n
, m
, q
.proto
, q
.dir
);
4535 bpf_error("Mask syntax for networks only");
4543 register const char *s
;
4548 int proto
= q
.proto
;
4554 else if (q
.proto
== Q_DECNET
)
4555 vlen
= __pcap_atodn(s
, &v
);
4557 vlen
= __pcap_atoin(s
, &v
);
4564 if (proto
== Q_DECNET
)
4565 return gen_host(v
, 0, proto
, dir
);
4566 else if (proto
== Q_LINK
) {
4567 bpf_error("illegal link layer address");
4570 if (s
== NULL
&& q
.addr
== Q_NET
) {
4571 /* Promote short net number */
4572 while (v
&& (v
& 0xff000000) == 0) {
4577 /* Promote short ipaddr */
4581 return gen_host(v
, mask
, proto
, dir
);
4586 proto
= IPPROTO_UDP
;
4587 else if (proto
== Q_TCP
)
4588 proto
= IPPROTO_TCP
;
4589 else if (proto
== Q_SCTP
)
4590 proto
= IPPROTO_SCTP
;
4591 else if (proto
== Q_DEFAULT
)
4592 proto
= PROTO_UNDEF
;
4594 bpf_error("illegal qualifier of 'port'");
4597 return gen_port((int)v
, proto
, dir
);
4601 b
= gen_port((int)v
, proto
, dir
);
4602 gen_or(gen_port6((int)v
, proto
, dir
), b
);
4609 proto
= IPPROTO_UDP
;
4610 else if (proto
== Q_TCP
)
4611 proto
= IPPROTO_TCP
;
4612 else if (proto
== Q_SCTP
)
4613 proto
= IPPROTO_SCTP
;
4614 else if (proto
== Q_DEFAULT
)
4615 proto
= PROTO_UNDEF
;
4617 bpf_error("illegal qualifier of 'portrange'");
4620 return gen_portrange((int)v
, (int)v
, proto
, dir
);
4624 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
4625 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
4631 bpf_error("'gateway' requires a name");
4635 return gen_proto((int)v
, proto
, dir
);
4638 return gen_protochain((int)v
, proto
, dir
);
4653 gen_mcode6(s1
, s2
, masklen
, q
)
4654 register const char *s1
, *s2
;
4655 register int masklen
;
4658 struct addrinfo
*res
;
4659 struct in6_addr
*addr
;
4660 struct in6_addr mask
;
4665 bpf_error("no mask %s supported", s2
);
4667 res
= pcap_nametoaddrinfo(s1
);
4669 bpf_error("invalid ip6 address %s", s1
);
4671 bpf_error("%s resolved to multiple address", s1
);
4672 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
4674 if (sizeof(mask
) * 8 < masklen
)
4675 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
4676 memset(&mask
, 0, sizeof(mask
));
4677 memset(&mask
, 0xff, masklen
/ 8);
4679 mask
.s6_addr
[masklen
/ 8] =
4680 (0xff << (8 - masklen
% 8)) & 0xff;
4683 a
= (u_int32_t
*)addr
;
4684 m
= (u_int32_t
*)&mask
;
4685 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
4686 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
4687 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
4695 bpf_error("Mask syntax for networks only");
4699 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
);
4704 bpf_error("invalid qualifier against IPv6 address");
4712 register const u_char
*eaddr
;
4715 struct block
*b
, *tmp
;
4717 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
4718 if (linktype
== DLT_EN10MB
)
4719 return gen_ehostop(eaddr
, (int)q
.dir
);
4720 if (linktype
== DLT_FDDI
)
4721 return gen_fhostop(eaddr
, (int)q
.dir
);
4722 if (linktype
== DLT_IEEE802
)
4723 return gen_thostop(eaddr
, (int)q
.dir
);
4724 if (linktype
== DLT_IEEE802_11
)
4725 return gen_wlanhostop(eaddr
, (int)q
.dir
);
4726 if (linktype
== DLT_SUNATM
&& is_lane
) {
4728 * Check that the packet doesn't begin with an
4729 * LE Control marker. (We've already generated
4732 tmp
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
4736 * Now check the MAC address.
4738 b
= gen_ehostop(eaddr
, (int)q
.dir
);
4742 if (linktype
== DLT_IP_OVER_FC
)
4743 return gen_ipfchostop(eaddr
, (int)q
.dir
);
4744 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4746 bpf_error("ethernet address used in non-ether expression");
4752 struct slist
*s0
, *s1
;
4755 * This is definitely not the best way to do this, but the
4756 * lists will rarely get long.
4763 static struct slist
*
4769 s
= new_stmt(BPF_LDX
|BPF_MEM
);
4774 static struct slist
*
4780 s
= new_stmt(BPF_LD
|BPF_MEM
);
4786 gen_load(proto
, index
, size
)
4791 struct slist
*s
, *tmp
;
4793 int regno
= alloc_reg();
4795 free_reg(index
->regno
);
4799 bpf_error("data size must be 1, 2, or 4");
4815 bpf_error("unsupported index operation");
4819 * XXX - what about ATM LANE? Should the index be
4820 * relative to the beginning of the AAL5 frame, so
4821 * that 0 refers to the beginning of the LE Control
4822 * field, or relative to the beginning of the LAN
4823 * frame, so that 0 refers, for Ethernet LANE, to
4824 * the beginning of the destination address?
4826 s
= xfer_to_x(index
);
4827 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4829 sappend(index
->s
, s
);
4844 /* XXX Note that we assume a fixed link header here. */
4845 s
= xfer_to_x(index
);
4846 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
4849 sappend(index
->s
, s
);
4851 b
= gen_proto_abbrev(proto
);
4853 gen_and(index
->b
, b
);
4865 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
4867 sappend(s
, xfer_to_a(index
));
4868 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
4869 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
4870 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
4872 sappend(index
->s
, s
);
4874 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
4876 gen_and(index
->b
, b
);
4878 gen_and(gen_proto_abbrev(Q_IP
), b
);
4884 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4888 index
->regno
= regno
;
4889 s
= new_stmt(BPF_ST
);
4891 sappend(index
->s
, s
);
4897 gen_relation(code
, a0
, a1
, reversed
)
4899 struct arth
*a0
, *a1
;
4902 struct slist
*s0
, *s1
, *s2
;
4903 struct block
*b
, *tmp
;
4907 if (code
== BPF_JEQ
) {
4908 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
4909 b
= new_block(JMP(code
));
4913 b
= new_block(BPF_JMP
|code
|BPF_X
);
4919 sappend(a0
->s
, a1
->s
);
4923 free_reg(a0
->regno
);
4924 free_reg(a1
->regno
);
4926 /* 'and' together protocol checks */
4929 gen_and(a0
->b
, tmp
= a1
->b
);
4945 int regno
= alloc_reg();
4946 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
4949 s
= new_stmt(BPF_LD
|BPF_LEN
);
4950 s
->next
= new_stmt(BPF_ST
);
4951 s
->next
->s
.k
= regno
;
4966 a
= (struct arth
*)newchunk(sizeof(*a
));
4970 s
= new_stmt(BPF_LD
|BPF_IMM
);
4972 s
->next
= new_stmt(BPF_ST
);
4988 s
= new_stmt(BPF_ALU
|BPF_NEG
);
4991 s
= new_stmt(BPF_ST
);
4999 gen_arth(code
, a0
, a1
)
5001 struct arth
*a0
, *a1
;
5003 struct slist
*s0
, *s1
, *s2
;
5007 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
5012 sappend(a0
->s
, a1
->s
);
5014 free_reg(a0
->regno
);
5015 free_reg(a1
->regno
);
5017 s0
= new_stmt(BPF_ST
);
5018 a0
->regno
= s0
->s
.k
= alloc_reg();
5025 * Here we handle simple allocation of the scratch registers.
5026 * If too many registers are alloc'd, the allocator punts.
5028 static int regused
[BPF_MEMWORDS
];
5032 * Return the next free register.
5037 int n
= BPF_MEMWORDS
;
5040 if (regused
[curreg
])
5041 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
5043 regused
[curreg
] = 1;
5047 bpf_error("too many registers needed to evaluate expression");
5052 * Return a register to the table so it can
5062 static struct block
*
5069 s
= new_stmt(BPF_LD
|BPF_LEN
);
5070 b
= new_block(JMP(jmp
));
5081 return gen_len(BPF_JGE
, n
);
5085 * Actually, this is less than or equal.
5093 b
= gen_len(BPF_JGT
, n
);
5100 gen_byteop(op
, idx
, val
)
5111 return gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5114 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5115 b
->s
.code
= JMP(BPF_JGE
);
5120 b
= gen_cmp((u_int
)idx
, BPF_B
, (bpf_int32
)val
);
5121 b
->s
.code
= JMP(BPF_JGT
);
5125 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
5129 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
5133 b
= new_block(JMP(BPF_JEQ
));
5140 static u_char abroadcast
[] = { 0x0 };
5143 gen_broadcast(proto
)
5146 bpf_u_int32 hostmask
;
5147 struct block
*b0
, *b1
, *b2
;
5148 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
5154 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5155 return gen_ahostop(abroadcast
, Q_DST
);
5156 if (linktype
== DLT_EN10MB
)
5157 return gen_ehostop(ebroadcast
, Q_DST
);
5158 if (linktype
== DLT_FDDI
)
5159 return gen_fhostop(ebroadcast
, Q_DST
);
5160 if (linktype
== DLT_IEEE802
)
5161 return gen_thostop(ebroadcast
, Q_DST
);
5162 if (linktype
== DLT_IEEE802_11
)
5163 return gen_wlanhostop(ebroadcast
, Q_DST
);
5164 if (linktype
== DLT_IP_OVER_FC
)
5165 return gen_ipfchostop(ebroadcast
, Q_DST
);
5166 if (linktype
== DLT_SUNATM
&& is_lane
) {
5168 * Check that the packet doesn't begin with an
5169 * LE Control marker. (We've already generated
5172 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5176 * Now check the MAC address.
5178 b0
= gen_ehostop(ebroadcast
, Q_DST
);
5182 bpf_error("not a broadcast link");
5186 b0
= gen_linktype(ETHERTYPE_IP
);
5187 hostmask
= ~netmask
;
5188 b1
= gen_mcmp(off_nl
+ 16, BPF_W
, (bpf_int32
)0, hostmask
);
5189 b2
= gen_mcmp(off_nl
+ 16, BPF_W
,
5190 (bpf_int32
)(~0 & hostmask
), hostmask
);
5195 bpf_error("only link-layer/IP broadcast filters supported");
5200 * Generate code to test the low-order bit of a MAC address (that's
5201 * the bottom bit of the *first* byte).
5203 static struct block
*
5204 gen_mac_multicast(offset
)
5207 register struct block
*b0
;
5208 register struct slist
*s
;
5210 /* link[offset] & 1 != 0 */
5211 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5213 b0
= new_block(JMP(BPF_JSET
));
5220 gen_multicast(proto
)
5223 register struct block
*b0
, *b1
, *b2
;
5224 register struct slist
*s
;
5230 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5231 /* all ARCnet multicasts use the same address */
5232 return gen_ahostop(abroadcast
, Q_DST
);
5234 if (linktype
== DLT_EN10MB
) {
5235 /* ether[0] & 1 != 0 */
5236 return gen_mac_multicast(0);
5239 if (linktype
== DLT_FDDI
) {
5241 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
5243 * XXX - was that referring to bit-order issues?
5245 /* fddi[1] & 1 != 0 */
5246 return gen_mac_multicast(1);
5249 if (linktype
== DLT_IEEE802
) {
5250 /* tr[2] & 1 != 0 */
5251 return gen_mac_multicast(2);
5254 if (linktype
== DLT_IEEE802_11
) {
5258 * For control frames, there is no DA.
5260 * For management frames, DA is at an
5261 * offset of 4 from the beginning of
5264 * For data frames, DA is at an offset
5265 * of 4 from the beginning of the packet
5266 * if To DS is clear and at an offset of
5267 * 16 from the beginning of the packet
5272 * Generate the tests to be done for data frames.
5274 * First, check for To DS set, i.e. "link[1] & 0x01".
5276 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5278 b1
= new_block(JMP(BPF_JSET
));
5279 b1
->s
.k
= 0x01; /* To DS */
5283 * If To DS is set, the DA is at 16.
5285 b0
= gen_mac_multicast(16);
5289 * Now, check for To DS not set, i.e. check
5290 * "!(link[1] & 0x01)".
5292 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5294 b2
= new_block(JMP(BPF_JSET
));
5295 b2
->s
.k
= 0x01; /* To DS */
5300 * If To DS is not set, the DA is at 4.
5302 b1
= gen_mac_multicast(4);
5306 * Now OR together the last two checks. That gives
5307 * the complete set of checks for data frames.
5312 * Now check for a data frame.
5313 * I.e, check "link[0] & 0x08".
5315 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5317 b1
= new_block(JMP(BPF_JSET
));
5322 * AND that with the checks done for data frames.
5327 * If the high-order bit of the type value is 0, this
5328 * is a management frame.
5329 * I.e, check "!(link[0] & 0x08)".
5331 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5333 b2
= new_block(JMP(BPF_JSET
));
5339 * For management frames, the DA is at 4.
5341 b1
= gen_mac_multicast(4);
5345 * OR that with the checks done for data frames.
5346 * That gives the checks done for management and
5352 * If the low-order bit of the type value is 1,
5353 * this is either a control frame or a frame
5354 * with a reserved type, and thus not a
5357 * I.e., check "!(link[0] & 0x04)".
5359 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
5361 b1
= new_block(JMP(BPF_JSET
));
5367 * AND that with the checks for data and management
5374 if (linktype
== DLT_IP_OVER_FC
) {
5375 b0
= gen_mac_multicast(2);
5379 if (linktype
== DLT_SUNATM
&& is_lane
) {
5381 * Check that the packet doesn't begin with an
5382 * LE Control marker. (We've already generated
5385 b1
= gen_cmp(SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
5388 /* ether[off_mac] & 1 != 0 */
5389 b0
= gen_mac_multicast(off_mac
);
5394 /* Link not known to support multicasts */
5398 b0
= gen_linktype(ETHERTYPE_IP
);
5399 b1
= gen_cmp(off_nl
+ 16, BPF_B
, (bpf_int32
)224);
5400 b1
->s
.code
= JMP(BPF_JGE
);
5406 b0
= gen_linktype(ETHERTYPE_IPV6
);
5407 b1
= gen_cmp(off_nl
+ 24, BPF_B
, (bpf_int32
)255);
5412 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
5417 * generate command for inbound/outbound. It's here so we can
5418 * make it link-type specific. 'dir' = 0 implies "inbound",
5419 * = 1 implies "outbound".
5425 register struct block
*b0
;
5428 * Only some data link types support inbound/outbound qualifiers.
5432 b0
= gen_relation(BPF_JEQ
,
5433 gen_load(Q_LINK
, gen_loadi(0), 1),
5441 * Match packets sent by this machine.
5443 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_OUTGOING
);
5446 * Match packets sent to this machine.
5447 * (No broadcast or multicast packets, or
5448 * packets sent to some other machine and
5449 * received promiscuously.)
5451 * XXX - packets sent to other machines probably
5452 * shouldn't be matched, but what about broadcast
5453 * or multicast packets we received?
5455 b0
= gen_cmp(0, BPF_H
, LINUX_SLL_HOST
);
5460 b0
= gen_cmp(offsetof(struct pfloghdr
, dir
), BPF_B
,
5461 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
5466 /* match outgoing packets */
5467 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_OUT
);
5469 /* match incoming packets */
5470 b0
= gen_cmp(0, BPF_B
, PPP_PPPD_IN
);
5474 case DLT_JUNIPER_MLFR
:
5475 case DLT_JUNIPER_MLPPP
:
5476 case DLT_JUNIPER_ATM1
:
5477 case DLT_JUNIPER_ATM2
:
5478 /* juniper flags (including direction) are stored
5479 * the byte after the 3-byte magic number */
5481 /* match outgoing packets */
5482 b0
= gen_mcmp(3, BPF_B
, 0, 0x01);
5484 /* match incoming packets */
5485 b0
= gen_mcmp(3, BPF_B
, 1, 0x01);
5490 bpf_error("inbound/outbound not supported on linktype %d",
5498 /* PF firewall log matched interface */
5500 gen_pf_ifname(const char *ifname
)
5505 if (linktype
== DLT_PFLOG
) {
5506 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
5507 off
= offsetof(struct pfloghdr
, ifname
);
5509 bpf_error("ifname not supported on linktype 0x%x", linktype
);
5512 if (strlen(ifname
) >= len
) {
5513 bpf_error("ifname interface names can only be %d characters",
5517 b0
= gen_bcmp(off
, strlen(ifname
), (const u_char
*)ifname
);
5521 /* PF firewall log matched interface */
5523 gen_pf_ruleset(char *ruleset
)
5527 if (linktype
!= DLT_PFLOG
) {
5528 bpf_error("ruleset not supported on linktype 0x%x", linktype
);
5531 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
5532 bpf_error("ruleset names can only be %ld characters",
5533 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
5536 b0
= gen_bcmp(offsetof(struct pfloghdr
, ruleset
),
5537 strlen(ruleset
), (const u_char
*)ruleset
);
5541 /* PF firewall log rule number */
5547 if (linktype
== DLT_PFLOG
) {
5548 b0
= gen_cmp(offsetof(struct pfloghdr
, rulenr
), BPF_W
,
5551 bpf_error("rnr not supported on linktype 0x%x", linktype
);
5558 /* PF firewall log sub-rule number */
5560 gen_pf_srnr(int srnr
)
5564 if (linktype
!= DLT_PFLOG
) {
5565 bpf_error("srnr not supported on linktype 0x%x", linktype
);
5569 b0
= gen_cmp(offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
5574 /* PF firewall log reason code */
5576 gen_pf_reason(int reason
)
5580 if (linktype
== DLT_PFLOG
) {
5581 b0
= gen_cmp(offsetof(struct pfloghdr
, reason
), BPF_B
,
5584 bpf_error("reason not supported on linktype 0x%x", linktype
);
5591 /* PF firewall log action */
5593 gen_pf_action(int action
)
5597 if (linktype
== DLT_PFLOG
) {
5598 b0
= gen_cmp(offsetof(struct pfloghdr
, action
), BPF_B
,
5601 bpf_error("action not supported on linktype 0x%x", linktype
);
5610 register const u_char
*eaddr
;
5613 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
5614 if (linktype
== DLT_ARCNET
|| linktype
== DLT_ARCNET_LINUX
)
5615 return gen_ahostop(eaddr
, (int)q
.dir
);
5617 bpf_error("ARCnet address used in non-arc expression");
5621 static struct block
*
5622 gen_ahostop(eaddr
, dir
)
5623 register const u_char
*eaddr
;
5626 register struct block
*b0
, *b1
;
5629 /* src comes first, different from Ethernet */
5631 return gen_bcmp(0, 1, eaddr
);
5634 return gen_bcmp(1, 1, eaddr
);
5637 b0
= gen_ahostop(eaddr
, Q_SRC
);
5638 b1
= gen_ahostop(eaddr
, Q_DST
);
5644 b0
= gen_ahostop(eaddr
, Q_SRC
);
5645 b1
= gen_ahostop(eaddr
, Q_DST
);
5654 * support IEEE 802.1Q VLAN trunk over ethernet
5663 * Change the offsets to point to the type and data fields within
5664 * the VLAN packet. Just increment the offsets, so that we
5665 * can support a hierarchy, e.g. "vlan 300 && vlan 200" to
5666 * capture VLAN 200 encapsulated within VLAN 100.
5668 * XXX - this is a bit of a kludge. If we were to split the
5669 * compiler into a parser that parses an expression and
5670 * generates an expression tree, and a code generator that
5671 * takes an expression tree (which could come from our
5672 * parser or from some other parser) and generates BPF code,
5673 * we could perhaps make the offsets parameters of routines
5674 * and, in the handler for an "AND" node, pass to subnodes
5675 * other than the VLAN node the adjusted offsets.
5677 * This would mean that "vlan" would, instead of changing the
5678 * behavior of *all* tests after it, change only the behavior
5679 * of tests ANDed with it. That would change the documented
5680 * semantics of "vlan", which might break some expressions.
5681 * However, it would mean that "(vlan and ip) or ip" would check
5682 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
5683 * checking only for VLAN-encapsulated IP, so that could still
5684 * be considered worth doing; it wouldn't break expressions
5685 * that are of the form "vlan and ..." or "vlan N and ...",
5686 * which I suspect are the most common expressions involving
5687 * "vlan". "vlan or ..." doesn't necessarily do what the user
5688 * would really want, now, as all the "or ..." tests would
5689 * be done assuming a VLAN, even though the "or" could be viewed
5690 * as meaning "or, if this isn't a VLAN packet...".
5692 orig_linktype
= off_linktype
; /* save original values */
5694 orig_nl_nosnap
= off_nl_nosnap
;
5705 bpf_error("no VLAN support for data link type %d",
5710 /* check for VLAN */
5711 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_8021Q
);
5713 /* If a specific VLAN is requested, check VLAN id */
5714 if (vlan_num
>= 0) {
5717 b1
= gen_mcmp(orig_nl
, BPF_H
, (bpf_int32
)vlan_num
, 0x0fff);
5735 * Change the offsets to point to the type and data fields within
5736 * the MPLS packet. Just increment the offsets, so that we
5737 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
5738 * capture packets with an outer label of 100000 and an inner
5741 * XXX - this is a bit of a kludge. See comments in gen_vlan().
5743 orig_linktype
= off_linktype
; /* save original values */
5745 orig_nl_nosnap
= off_nl_nosnap
;
5749 case DLT_C_HDLC
: /* fall through */
5755 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)ETHERTYPE_MPLS
);
5763 b0
= gen_cmp(orig_linktype
, BPF_H
, (bpf_int32
)PPP_MPLS_UCAST
);
5766 /* FIXME add other DLT_s ...
5767 * for Frame-Relay/and ATM this may get messy due to SNAP headers
5768 * leave it for now */
5771 bpf_error("no MPLS support for data link type %d",
5778 /* If a specific MPLS label is requested, check it */
5779 if (label_num
>= 0) {
5782 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
5783 b1
= gen_mcmp(orig_nl
, BPF_W
, (bpf_int32
)label_num
, 0xfffff000); /* only compare the first 20 bits */
5792 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
5804 bpf_error("'vpi' supported only on raw ATM");
5805 if (off_vpi
== (u_int
)-1)
5807 b0
= gen_ncmp(BPF_B
, off_vpi
, 0xffffffff, (u_int
)jtype
,
5808 (u_int
)jvalue
, reverse
);
5813 bpf_error("'vci' supported only on raw ATM");
5814 if (off_vci
== (u_int
)-1)
5816 b0
= gen_ncmp(BPF_H
, off_vci
, 0xffffffff, (u_int
)jtype
,
5817 (u_int
)jvalue
, reverse
);
5821 if (off_proto
== (u_int
)-1)
5822 abort(); /* XXX - this isn't on FreeBSD */
5823 b0
= gen_ncmp(BPF_B
, off_proto
, 0x0f, (u_int
)jtype
,
5824 (u_int
)jvalue
, reverse
);
5828 if (off_payload
== (u_int
)-1)
5830 b0
= gen_ncmp(BPF_B
, off_payload
+ MSG_TYPE_POS
, 0xffffffff,
5831 (u_int
)jtype
, (u_int
)jvalue
, reverse
);
5836 bpf_error("'callref' supported only on raw ATM");
5837 if (off_proto
== (u_int
)-1)
5839 b0
= gen_ncmp(BPF_B
, off_proto
, 0xffffffff, (u_int
)jtype
,
5840 (u_int
)jvalue
, reverse
);
5850 gen_atmtype_abbrev(type
)
5853 struct block
*b0
, *b1
;
5858 /* Get all packets in Meta signalling Circuit */
5860 bpf_error("'metac' supported only on raw ATM");
5861 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5862 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
5867 /* Get all packets in Broadcast Circuit*/
5869 bpf_error("'bcc' supported only on raw ATM");
5870 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5871 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
5876 /* Get all cells in Segment OAM F4 circuit*/
5878 bpf_error("'oam4sc' supported only on raw ATM");
5879 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5880 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
5885 /* Get all cells in End-to-End OAM F4 Circuit*/
5887 bpf_error("'oam4ec' supported only on raw ATM");
5888 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5889 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
5894 /* Get all packets in connection Signalling Circuit */
5896 bpf_error("'sc' supported only on raw ATM");
5897 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5898 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
5903 /* Get all packets in ILMI Circuit */
5905 bpf_error("'ilmic' supported only on raw ATM");
5906 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
5907 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
5912 /* Get all LANE packets */
5914 bpf_error("'lane' supported only on raw ATM");
5915 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
5918 * Arrange that all subsequent tests assume LANE
5919 * rather than LLC-encapsulated packets, and set
5920 * the offsets appropriately for LANE-encapsulated
5923 * "off_mac" is the offset of the Ethernet header,
5924 * which is 2 bytes past the ATM pseudo-header
5925 * (skipping the pseudo-header and 2-byte LE Client
5926 * field). The other offsets are Ethernet offsets
5927 * relative to "off_mac".
5930 off_mac
= off_payload
+ 2; /* MAC header */
5931 off_linktype
= off_mac
+ 12;
5932 off_nl
= off_mac
+ 14; /* Ethernet II */
5933 off_nl_nosnap
= off_mac
+ 17; /* 802.3+802.2 */
5937 /* Get all LLC-encapsulated packets */
5939 bpf_error("'llc' supported only on raw ATM");
5940 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
5951 static struct block
*
5952 gen_msg_abbrev(type
)
5958 * Q.2931 signalling protocol messages for handling virtual circuits
5959 * establishment and teardown
5964 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
5968 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
5972 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
5976 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
5980 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
5983 case A_RELEASE_DONE
:
5984 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
5994 gen_atmmulti_abbrev(type
)
5997 struct block
*b0
, *b1
;
6003 bpf_error("'oam' supported only on raw ATM");
6004 b1
= gen_atmmulti_abbrev(A_OAMF4
);
6009 bpf_error("'oamf4' supported only on raw ATM");
6011 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
6012 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
6014 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
6020 * Get Q.2931 signalling messages for switched
6021 * virtual connection
6024 bpf_error("'connectmsg' supported only on raw ATM");
6025 b0
= gen_msg_abbrev(A_SETUP
);
6026 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6028 b0
= gen_msg_abbrev(A_CONNECT
);
6030 b0
= gen_msg_abbrev(A_CONNECTACK
);
6032 b0
= gen_msg_abbrev(A_RELEASE
);
6034 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6036 b0
= gen_atmtype_abbrev(A_SC
);
6042 bpf_error("'metaconnect' supported only on raw ATM");
6043 b0
= gen_msg_abbrev(A_SETUP
);
6044 b1
= gen_msg_abbrev(A_CALLPROCEED
);
6046 b0
= gen_msg_abbrev(A_CONNECT
);
6048 b0
= gen_msg_abbrev(A_RELEASE
);
6050 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
6052 b0
= gen_atmtype_abbrev(A_METAC
);