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.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
65 #include "ethertype.h"
69 #include "ieee80211.h"
71 #include "sunatmpos.h"
74 #include "pcap/ipnet.h"
77 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
78 #include <linux/types.h>
79 #include <linux/if_packet.h>
80 #include <linux/filter.h>
83 #ifdef HAVE_NET_PFVAR_H
84 #include <sys/socket.h>
86 #include <net/pfvar.h>
87 #include <net/if_pflog.h>
91 #define offsetof(s, e) ((size_t)&((s *)0)->e)
96 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
102 u_int8_t u6_addr8
[16];
103 u_int16_t u6_addr16
[8];
104 u_int32_t u6_addr32
[4];
106 #define s6_addr in6_u.u6_addr8
107 #define s6_addr16 in6_u.u6_addr16
108 #define s6_addr32 in6_u.u6_addr32
109 #define s6_addr64 in6_u.u6_addr64
112 typedef unsigned short sa_family_t
;
114 #define __SOCKADDR_COMMON(sa_prefix) \
115 sa_family_t sa_prefix##family
117 /* Ditto, for IPv6. */
120 __SOCKADDR_COMMON (sin6_
);
121 u_int16_t sin6_port
; /* Transport layer port # */
122 u_int32_t sin6_flowinfo
; /* IPv6 flow information */
123 struct in6_addr sin6_addr
; /* IPv6 address */
126 #ifndef EAI_ADDRFAMILY
128 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
129 int ai_family
; /* PF_xxx */
130 int ai_socktype
; /* SOCK_xxx */
131 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
132 size_t ai_addrlen
; /* length of ai_addr */
133 char *ai_canonname
; /* canonical name for hostname */
134 struct sockaddr
*ai_addr
; /* binary address */
135 struct addrinfo
*ai_next
; /* next structure in linked list */
137 #endif /* EAI_ADDRFAMILY */
138 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
140 #include <netdb.h> /* for "struct addrinfo" */
143 #include <pcap/namedb.h>
145 #define ETHERMTU 1500
147 #ifndef ETHERTYPE_TEB
148 #define ETHERTYPE_TEB 0x6558
151 #ifndef IPPROTO_HOPOPTS
152 #define IPPROTO_HOPOPTS 0
154 #ifndef IPPROTO_ROUTING
155 #define IPPROTO_ROUTING 43
157 #ifndef IPPROTO_FRAGMENT
158 #define IPPROTO_FRAGMENT 44
160 #ifndef IPPROTO_DSTOPTS
161 #define IPPROTO_DSTOPTS 60
164 #define IPPROTO_SCTP 132
167 #define GENEVE_PORT 6081
169 #ifdef HAVE_OS_PROTO_H
170 #include "os-proto.h"
173 #define JMP(c) ((c)|BPF_JMP|BPF_K)
176 static jmp_buf top_ctx
;
177 static pcap_t
*bpf_pcap
;
179 /* Hack for handling VLAN and MPLS stacks. */
181 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
183 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
187 static int pcap_fddipad
;
191 bpf_error(const char *fmt
, ...)
196 if (bpf_pcap
!= NULL
)
197 (void)pcap_vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
204 static void init_linktype(pcap_t
*);
206 static void init_regs(void);
207 static int alloc_reg(void);
208 static void free_reg(int);
210 static struct block
*root
;
213 * Absolute offsets, which are offsets from the beginning of the raw
214 * packet data, are, in the general case, the sum of a variable value
215 * and a constant value; the variable value may be absent, in which
216 * case the offset is only the constant value, and the constant value
217 * may be zero, in which case the offset is only the variable value.
219 * bpf_abs_offset is a structure containing all that information:
221 * is_variable is 1 if there's a variable part.
223 * constant_part is the constant part of the value, possibly zero;
225 * if is_variable is 1, reg is the register number for a register
226 * containing the variable value if the register has been assigned,
236 * Value passed to gen_load_a() to indicate what the offset argument
237 * is relative to the beginning of.
240 OR_PACKET
, /* full packet data */
241 OR_LINKHDR
, /* link-layer header */
242 OR_PREVLINKHDR
, /* previous link-layer header */
243 OR_LLC
, /* 802.2 LLC header */
244 OR_PREVMPLSHDR
, /* previous MPLS header */
245 OR_LINKTYPE
, /* link-layer type */
246 OR_LINKPL
, /* link-layer payload */
247 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
248 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
249 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
254 * As errors are handled by a longjmp, anything allocated must be freed
255 * in the longjmp handler, so it must be reachable from that handler.
256 * One thing that's allocated is the result of pcap_nametoaddrinfo();
257 * it must be freed with freeaddrinfo(). This variable points to any
258 * addrinfo structure that would need to be freed.
260 static struct addrinfo
*ai
;
264 * We divy out chunks of memory rather than call malloc each time so
265 * we don't have to worry about leaking memory. It's probably
266 * not a big deal if all this memory was wasted but if this ever
267 * goes into a library that would probably not be a good idea.
269 * XXX - this *is* in a library....
272 #define CHUNK0SIZE 1024
278 static struct chunk chunks
[NCHUNKS
];
279 static int cur_chunk
;
281 static void *newchunk(size_t);
282 static void freechunks(void);
283 static inline struct block
*new_block(int);
284 static inline struct slist
*new_stmt(int);
285 static struct block
*gen_retblk(int);
286 static inline void syntax(void);
288 static void backpatch(struct block
*, struct block
*);
289 static void merge(struct block
*, struct block
*);
290 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
291 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
292 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
293 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
294 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
295 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
297 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
298 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
299 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
300 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
301 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
302 static struct slist
*gen_loadx_iphdrlen(void);
303 static struct block
*gen_uncond(int);
304 static inline struct block
*gen_true(void);
305 static inline struct block
*gen_false(void);
306 static struct block
*gen_ether_linktype(int);
307 static struct block
*gen_ipnet_linktype(int);
308 static struct block
*gen_linux_sll_linktype(int);
309 static struct slist
*gen_load_prism_llprefixlen(void);
310 static struct slist
*gen_load_avs_llprefixlen(void);
311 static struct slist
*gen_load_radiotap_llprefixlen(void);
312 static struct slist
*gen_load_ppi_llprefixlen(void);
313 static void insert_compute_vloffsets(struct block
*);
314 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
315 static int ethertype_to_ppptype(int);
316 static struct block
*gen_linktype(int);
317 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
318 static struct block
*gen_llc_linktype(int);
319 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
321 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
323 static struct block
*gen_ahostop(const u_char
*, int);
324 static struct block
*gen_ehostop(const u_char
*, int);
325 static struct block
*gen_fhostop(const u_char
*, int);
326 static struct block
*gen_thostop(const u_char
*, int);
327 static struct block
*gen_wlanhostop(const u_char
*, int);
328 static struct block
*gen_ipfchostop(const u_char
*, int);
329 static struct block
*gen_dnhostop(bpf_u_int32
, int);
330 static struct block
*gen_mpls_linktype(int);
331 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
333 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
336 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
338 static struct block
*gen_ipfrag(void);
339 static struct block
*gen_portatom(int, bpf_int32
);
340 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
341 static struct block
*gen_portatom6(int, bpf_int32
);
342 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
343 struct block
*gen_portop(int, int, int);
344 static struct block
*gen_port(int, int, int);
345 struct block
*gen_portrangeop(int, int, int, int);
346 static struct block
*gen_portrange(int, int, int, int);
347 struct block
*gen_portop6(int, int, int);
348 static struct block
*gen_port6(int, int, int);
349 struct block
*gen_portrangeop6(int, int, int, int);
350 static struct block
*gen_portrange6(int, int, int, int);
351 static int lookup_proto(const char *, int);
352 static struct block
*gen_protochain(int, int, int);
353 static struct block
*gen_proto(int, int, int);
354 static struct slist
*xfer_to_x(struct arth
*);
355 static struct slist
*xfer_to_a(struct arth
*);
356 static struct block
*gen_mac_multicast(int);
357 static struct block
*gen_len(int, int);
358 static struct block
*gen_check_802_11_data_frame(void);
359 static struct block
*gen_geneve_ll_check(void);
361 static struct block
*gen_ppi_dlt_check(void);
362 static struct block
*gen_msg_abbrev(int type
);
372 /* XXX Round up to nearest long. */
373 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
375 /* XXX Round up to structure boundary. */
379 cp
= &chunks
[cur_chunk
];
380 if (n
> cp
->n_left
) {
381 ++cp
, k
= ++cur_chunk
;
383 bpf_error("out of memory");
384 size
= CHUNK0SIZE
<< k
;
385 cp
->m
= (void *)malloc(size
);
387 bpf_error("out of memory");
388 memset((char *)cp
->m
, 0, size
);
391 bpf_error("out of memory");
394 return (void *)((char *)cp
->m
+ cp
->n_left
);
403 for (i
= 0; i
< NCHUNKS
; ++i
)
404 if (chunks
[i
].m
!= NULL
) {
411 * A strdup whose allocations are freed after code generation is over.
415 register const char *s
;
417 size_t n
= strlen(s
) + 1;
418 char *cp
= newchunk(n
);
424 static inline struct block
*
430 p
= (struct block
*)newchunk(sizeof(*p
));
437 static inline struct slist
*
443 p
= (struct slist
*)newchunk(sizeof(*p
));
449 static struct block
*
453 struct block
*b
= new_block(BPF_RET
|BPF_K
);
462 bpf_error("syntax error in filter expression");
465 static bpf_u_int32 netmask
;
470 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
471 const char *buf
, int optimize
, bpf_u_int32 mask
)
474 const char * volatile xbuf
= buf
;
479 * XXX - single-thread this code path with pthread calls on
480 * UN*X, if the platform supports pthreads? If that requires
481 * a separate -lpthread, we might not want to do that.
489 EnterCriticalSection(&g_PcapCompileCriticalSection
);
493 * If this pcap_t hasn't been activated, it doesn't have a
494 * link-layer type, so we can't use it.
497 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
498 "not-yet-activated pcap_t passed to pcap_compile");
508 if (setjmp(top_ctx
)) {
523 snaplen
= pcap_snapshot(p
);
525 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
526 "snaplen of 0 rejects all packets");
531 lex_init(xbuf
? xbuf
: "");
539 root
= gen_retblk(snaplen
);
541 if (optimize
&& !no_optimize
) {
544 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
545 bpf_error("expression rejects all packets");
547 program
->bf_insns
= icode_to_fcode(root
, &len
);
548 program
->bf_len
= len
;
553 rc
= 0; /* We're all okay */
558 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
565 * entry point for using the compiler with no pcap open
566 * pass in all the stuff that is needed explicitly instead.
569 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
570 struct bpf_program
*program
,
571 const char *buf
, int optimize
, bpf_u_int32 mask
)
576 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
579 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
585 * Clean up a "struct bpf_program" by freeing all the memory allocated
589 pcap_freecode(struct bpf_program
*program
)
592 if (program
->bf_insns
!= NULL
) {
593 free((char *)program
->bf_insns
);
594 program
->bf_insns
= NULL
;
599 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
600 * which of the jt and jf fields has been resolved and which is a pointer
601 * back to another unresolved block (or nil). At least one of the fields
602 * in each block is already resolved.
605 backpatch(list
, target
)
606 struct block
*list
, *target
;
623 * Merge the lists in b0 and b1, using the 'sense' field to indicate
624 * which of jt and jf is the link.
628 struct block
*b0
, *b1
;
630 register struct block
**p
= &b0
;
632 /* Find end of list. */
634 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
636 /* Concatenate the lists. */
644 struct block
*ppi_dlt_check
;
647 * Insert before the statements of the first (root) block any
648 * statements needed to load the lengths of any variable-length
649 * headers into registers.
651 * XXX - a fancier strategy would be to insert those before the
652 * statements of all blocks that use those lengths and that
653 * have no predecessors that use them, so that we only compute
654 * the lengths if we need them. There might be even better
655 * approaches than that.
657 * However, those strategies would be more complicated, and
658 * as we don't generate code to compute a length if the
659 * program has no tests that use the length, and as most
660 * tests will probably use those lengths, we would just
661 * postpone computing the lengths so that it's not done
662 * for tests that fail early, and it's not clear that's
665 insert_compute_vloffsets(p
->head
);
668 * For DLT_PPI captures, generate a check of the per-packet
669 * DLT value to make sure it's DLT_IEEE802_11.
671 * XXX - TurboCap cards use DLT_PPI for Ethernet.
672 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
673 * with appropriate Ethernet information and use that rather
674 * than using something such as DLT_PPI where you don't know
675 * the link-layer header type until runtime, which, in the
676 * general case, would force us to generate both Ethernet *and*
677 * 802.11 code (*and* anything else for which PPI is used)
678 * and choose between them early in the BPF program?
680 ppi_dlt_check
= gen_ppi_dlt_check();
681 if (ppi_dlt_check
!= NULL
)
682 gen_and(ppi_dlt_check
, p
);
684 backpatch(p
, gen_retblk(snaplen
));
685 p
->sense
= !p
->sense
;
686 backpatch(p
, gen_retblk(0));
692 struct block
*b0
, *b1
;
694 backpatch(b0
, b1
->head
);
695 b0
->sense
= !b0
->sense
;
696 b1
->sense
= !b1
->sense
;
698 b1
->sense
= !b1
->sense
;
704 struct block
*b0
, *b1
;
706 b0
->sense
= !b0
->sense
;
707 backpatch(b0
, b1
->head
);
708 b0
->sense
= !b0
->sense
;
717 b
->sense
= !b
->sense
;
720 static struct block
*
721 gen_cmp(offrel
, offset
, size
, v
)
722 enum e_offrel offrel
;
726 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
729 static struct block
*
730 gen_cmp_gt(offrel
, offset
, size
, v
)
731 enum e_offrel offrel
;
735 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
738 static struct block
*
739 gen_cmp_ge(offrel
, offset
, size
, v
)
740 enum e_offrel offrel
;
744 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
747 static struct block
*
748 gen_cmp_lt(offrel
, offset
, size
, v
)
749 enum e_offrel offrel
;
753 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
756 static struct block
*
757 gen_cmp_le(offrel
, offset
, size
, v
)
758 enum e_offrel offrel
;
762 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
765 static struct block
*
766 gen_mcmp(offrel
, offset
, size
, v
, mask
)
767 enum e_offrel offrel
;
772 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
775 static struct block
*
776 gen_bcmp(offrel
, offset
, size
, v
)
777 enum e_offrel offrel
;
778 register u_int offset
, size
;
779 register const u_char
*v
;
781 register struct block
*b
, *tmp
;
785 register const u_char
*p
= &v
[size
- 4];
786 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
787 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
789 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
796 register const u_char
*p
= &v
[size
- 2];
797 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
799 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
806 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
815 * AND the field of size "size" at offset "offset" relative to the header
816 * specified by "offrel" with "mask", and compare it with the value "v"
817 * with the test specified by "jtype"; if "reverse" is true, the test
818 * should test the opposite of "jtype".
820 static struct block
*
821 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
822 enum e_offrel offrel
;
824 bpf_u_int32 offset
, size
, mask
, jtype
;
827 struct slist
*s
, *s2
;
830 s
= gen_load_a(offrel
, offset
, size
);
832 if (mask
!= 0xffffffff) {
833 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
838 b
= new_block(JMP(jtype
));
841 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
847 * Various code constructs need to know the layout of the packet.
848 * These variables give the necessary offsets from the beginning
849 * of the packet data.
853 * Absolute offset of the beginning of the link-layer header.
855 static bpf_abs_offset off_linkhdr
;
858 * If we're checking a link-layer header for a packet encapsulated in
859 * another protocol layer, this is the equivalent information for the
860 * previous layers' link-layer header from the beginning of the raw
863 static bpf_abs_offset off_prevlinkhdr
;
866 * This is the equivalent information for the outermost layers' link-layer
869 static bpf_abs_offset off_outermostlinkhdr
;
872 * "Push" the current value of the link-layer header type and link-layer
873 * header offset onto a "stack", and set a new value. (It's not a
874 * full-blown stack; we keep only the top two items.)
876 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
878 prevlinktype = new_linktype; \
879 off_prevlinkhdr = off_linkhdr; \
880 linktype = new_linktype; \
881 off_linkhdr.is_variable = new_is_variable; \
882 off_linkhdr.constant_part = new_constant_part; \
883 off_linkhdr.reg = new_reg; \
888 * Absolute offset of the beginning of the link-layer payload.
890 static bpf_abs_offset off_linkpl
;
893 * "off_linktype" is the offset to information in the link-layer header
894 * giving the packet type. This is an absolute offset from the beginning
897 * For Ethernet, it's the offset of the Ethernet type field; this
898 * means that it must have a value that skips VLAN tags.
900 * For link-layer types that always use 802.2 headers, it's the
901 * offset of the LLC header; this means that it must have a value
902 * that skips VLAN tags.
904 * For PPP, it's the offset of the PPP type field.
906 * For Cisco HDLC, it's the offset of the CHDLC type field.
908 * For BSD loopback, it's the offset of the AF_ value.
910 * For Linux cooked sockets, it's the offset of the type field.
912 * off_linktype.constant_part is set to -1 for no encapsulation,
913 * in which case, IP is assumed.
915 static bpf_abs_offset off_linktype
;
918 * TRUE if the link layer includes an ATM pseudo-header.
920 static int is_atm
= 0;
923 * TRUE if "geneve" appeared in the filter; it causes us to generate
924 * code that checks for a Geneve header and assume that later filters
925 * apply to the encapsulated payload.
927 static int is_geneve
= 0;
930 * These are offsets for the ATM pseudo-header.
932 static u_int off_vpi
;
933 static u_int off_vci
;
934 static u_int off_proto
;
937 * These are offsets for the MTP2 fields.
940 static u_int off_li_hsl
;
943 * These are offsets for the MTP3 fields.
945 static u_int off_sio
;
946 static u_int off_opc
;
947 static u_int off_dpc
;
948 static u_int off_sls
;
951 * This is the offset of the first byte after the ATM pseudo_header,
952 * or -1 if there is no ATM pseudo-header.
954 static u_int off_payload
;
957 * These are offsets to the beginning of the network-layer header.
958 * They are relative to the beginning of the link-layer payload (i.e.,
959 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
961 * If the link layer never uses 802.2 LLC:
963 * "off_nl" and "off_nl_nosnap" are the same.
965 * If the link layer always uses 802.2 LLC:
967 * "off_nl" is the offset if there's a SNAP header following
970 * "off_nl_nosnap" is the offset if there's no SNAP header.
972 * If the link layer is Ethernet:
974 * "off_nl" is the offset if the packet is an Ethernet II packet
975 * (we assume no 802.3+802.2+SNAP);
977 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
978 * with an 802.2 header following it.
981 static u_int off_nl_nosnap
;
984 static int prevlinktype
;
985 static int outermostlinktype
;
991 pcap_fddipad
= p
->fddipad
;
994 * We start out with only one link-layer header.
996 outermostlinktype
= pcap_datalink(p
);
997 off_outermostlinkhdr
.constant_part
= 0;
998 off_outermostlinkhdr
.is_variable
= 0;
999 off_outermostlinkhdr
.reg
= -1;
1001 prevlinktype
= outermostlinktype
;
1002 off_prevlinkhdr
.constant_part
= 0;
1003 off_prevlinkhdr
.is_variable
= 0;
1004 off_prevlinkhdr
.reg
= -1;
1006 linktype
= outermostlinktype
;
1007 off_linkhdr
.constant_part
= 0;
1008 off_linkhdr
.is_variable
= 0;
1009 off_linkhdr
.reg
= -1;
1014 off_linkpl
.constant_part
= 0;
1015 off_linkpl
.is_variable
= 0;
1016 off_linkpl
.reg
= -1;
1018 off_linktype
.constant_part
= 0;
1019 off_linktype
.is_variable
= 0;
1020 off_linktype
.reg
= -1;
1023 * Assume it's not raw ATM with a pseudo-header, for now.
1037 * And assume we're not doing SS7.
1046 label_stack_depth
= 0;
1047 vlan_stack_depth
= 0;
1052 off_linktype
.constant_part
= 2;
1053 off_linkpl
.constant_part
= 6;
1054 off_nl
= 0; /* XXX in reality, variable! */
1055 off_nl_nosnap
= 0; /* no 802.2 LLC */
1058 case DLT_ARCNET_LINUX
:
1059 off_linktype
.constant_part
= 4;
1060 off_linkpl
.constant_part
= 8;
1061 off_nl
= 0; /* XXX in reality, variable! */
1062 off_nl_nosnap
= 0; /* no 802.2 LLC */
1066 off_linktype
.constant_part
= 12;
1067 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1068 off_nl
= 0; /* Ethernet II */
1069 off_nl_nosnap
= 3; /* 802.3+802.2 */
1074 * SLIP doesn't have a link level type. The 16 byte
1075 * header is hacked into our SLIP driver.
1077 off_linktype
.constant_part
= -1;
1078 off_linkpl
.constant_part
= 16;
1080 off_nl_nosnap
= 0; /* no 802.2 LLC */
1083 case DLT_SLIP_BSDOS
:
1084 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1085 off_linktype
.constant_part
= -1;
1087 off_linkpl
.constant_part
= 24;
1089 off_nl_nosnap
= 0; /* no 802.2 LLC */
1094 off_linktype
.constant_part
= 0;
1095 off_linkpl
.constant_part
= 4;
1097 off_nl_nosnap
= 0; /* no 802.2 LLC */
1101 off_linktype
.constant_part
= 0;
1102 off_linkpl
.constant_part
= 12;
1104 off_nl_nosnap
= 0; /* no 802.2 LLC */
1109 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1110 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1111 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1112 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1114 off_nl_nosnap
= 0; /* no 802.2 LLC */
1119 * This does no include the Ethernet header, and
1120 * only covers session state.
1122 off_linktype
.constant_part
= 6;
1123 off_linkpl
.constant_part
= 8;
1125 off_nl_nosnap
= 0; /* no 802.2 LLC */
1129 off_linktype
.constant_part
= 5;
1130 off_linkpl
.constant_part
= 24;
1132 off_nl_nosnap
= 0; /* no 802.2 LLC */
1137 * FDDI doesn't really have a link-level type field.
1138 * We set "off_linktype" to the offset of the LLC header.
1140 * To check for Ethernet types, we assume that SSAP = SNAP
1141 * is being used and pick out the encapsulated Ethernet type.
1142 * XXX - should we generate code to check for SNAP?
1144 off_linktype
.constant_part
= 13;
1145 off_linktype
.constant_part
+= pcap_fddipad
;
1146 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1147 off_linkpl
.constant_part
+= pcap_fddipad
;
1148 off_nl
= 8; /* 802.2+SNAP */
1149 off_nl_nosnap
= 3; /* 802.2 */
1154 * Token Ring doesn't really have a link-level type field.
1155 * We set "off_linktype" to the offset of the LLC header.
1157 * To check for Ethernet types, we assume that SSAP = SNAP
1158 * is being used and pick out the encapsulated Ethernet type.
1159 * XXX - should we generate code to check for SNAP?
1161 * XXX - the header is actually variable-length.
1162 * Some various Linux patched versions gave 38
1163 * as "off_linktype" and 40 as "off_nl"; however,
1164 * if a token ring packet has *no* routing
1165 * information, i.e. is not source-routed, the correct
1166 * values are 20 and 22, as they are in the vanilla code.
1168 * A packet is source-routed iff the uppermost bit
1169 * of the first byte of the source address, at an
1170 * offset of 8, has the uppermost bit set. If the
1171 * packet is source-routed, the total number of bytes
1172 * of routing information is 2 plus bits 0x1F00 of
1173 * the 16-bit value at an offset of 14 (shifted right
1174 * 8 - figure out which byte that is).
1176 off_linktype
.constant_part
= 14;
1177 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1178 off_nl
= 8; /* 802.2+SNAP */
1179 off_nl_nosnap
= 3; /* 802.2 */
1182 case DLT_PRISM_HEADER
:
1183 case DLT_IEEE802_11_RADIO_AVS
:
1184 case DLT_IEEE802_11_RADIO
:
1185 off_linkhdr
.is_variable
= 1;
1186 /* Fall through, 802.11 doesn't have a variable link
1187 * prefix but is otherwise the same. */
1189 case DLT_IEEE802_11
:
1191 * 802.11 doesn't really have a link-level type field.
1192 * We set "off_linktype.constant_part" to the offset of
1195 * To check for Ethernet types, we assume that SSAP = SNAP
1196 * is being used and pick out the encapsulated Ethernet type.
1197 * XXX - should we generate code to check for SNAP?
1199 * We also handle variable-length radio headers here.
1200 * The Prism header is in theory variable-length, but in
1201 * practice it's always 144 bytes long. However, some
1202 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1203 * sometimes or always supply an AVS header, so we
1204 * have to check whether the radio header is a Prism
1205 * header or an AVS header, so, in practice, it's
1208 off_linktype
.constant_part
= 24;
1209 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1210 off_linkpl
.is_variable
= 1;
1211 off_nl
= 8; /* 802.2+SNAP */
1212 off_nl_nosnap
= 3; /* 802.2 */
1217 * At the moment we treat PPI the same way that we treat
1218 * normal Radiotap encoded packets. The difference is in
1219 * the function that generates the code at the beginning
1220 * to compute the header length. Since this code generator
1221 * of PPI supports bare 802.11 encapsulation only (i.e.
1222 * the encapsulated DLT should be DLT_IEEE802_11) we
1223 * generate code to check for this too.
1225 off_linktype
.constant_part
= 24;
1226 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1227 off_linkpl
.is_variable
= 1;
1228 off_linkhdr
.is_variable
= 1;
1229 off_nl
= 8; /* 802.2+SNAP */
1230 off_nl_nosnap
= 3; /* 802.2 */
1233 case DLT_ATM_RFC1483
:
1234 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1236 * assume routed, non-ISO PDUs
1237 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1239 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1240 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1241 * latter would presumably be treated the way PPPoE
1242 * should be, so you can do "pppoe and udp port 2049"
1243 * or "pppoa and tcp port 80" and have it check for
1244 * PPPo{A,E} and a PPP protocol of IP and....
1246 off_linktype
.constant_part
= 0;
1247 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1248 off_nl
= 8; /* 802.2+SNAP */
1249 off_nl_nosnap
= 3; /* 802.2 */
1254 * Full Frontal ATM; you get AALn PDUs with an ATM
1258 off_vpi
= SUNATM_VPI_POS
;
1259 off_vci
= SUNATM_VCI_POS
;
1260 off_proto
= PROTO_POS
;
1261 off_payload
= SUNATM_PKT_BEGIN_POS
;
1262 off_linktype
.constant_part
= off_payload
;
1263 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1264 off_nl
= 8; /* 802.2+SNAP */
1265 off_nl_nosnap
= 3; /* 802.2 */
1271 off_linktype
.constant_part
= -1;
1272 off_linkpl
.constant_part
= 0;
1274 off_nl_nosnap
= 0; /* no 802.2 LLC */
1277 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1278 off_linktype
.constant_part
= 14;
1279 off_linkpl
.constant_part
= 16;
1281 off_nl_nosnap
= 0; /* no 802.2 LLC */
1286 * LocalTalk does have a 1-byte type field in the LLAP header,
1287 * but really it just indicates whether there is a "short" or
1288 * "long" DDP packet following.
1290 off_linktype
.constant_part
= -1;
1291 off_linkpl
.constant_part
= 0;
1293 off_nl_nosnap
= 0; /* no 802.2 LLC */
1296 case DLT_IP_OVER_FC
:
1298 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1299 * link-level type field. We set "off_linktype" to the
1300 * offset of the LLC header.
1302 * To check for Ethernet types, we assume that SSAP = SNAP
1303 * is being used and pick out the encapsulated Ethernet type.
1304 * XXX - should we generate code to check for SNAP? RFC
1305 * 2625 says SNAP should be used.
1307 off_linktype
.constant_part
= 16;
1308 off_linkpl
.constant_part
= 16;
1309 off_nl
= 8; /* 802.2+SNAP */
1310 off_nl_nosnap
= 3; /* 802.2 */
1315 * XXX - we should set this to handle SNAP-encapsulated
1316 * frames (NLPID of 0x80).
1318 off_linktype
.constant_part
= -1;
1319 off_linkpl
.constant_part
= 0;
1321 off_nl_nosnap
= 0; /* no 802.2 LLC */
1325 * the only BPF-interesting FRF.16 frames are non-control frames;
1326 * Frame Relay has a variable length link-layer
1327 * so lets start with offset 4 for now and increments later on (FIXME);
1330 off_linktype
.constant_part
= -1;
1331 off_linkpl
.constant_part
= 0;
1333 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1336 case DLT_APPLE_IP_OVER_IEEE1394
:
1337 off_linktype
.constant_part
= 16;
1338 off_linkpl
.constant_part
= 18;
1340 off_nl_nosnap
= 0; /* no 802.2 LLC */
1343 case DLT_SYMANTEC_FIREWALL
:
1344 off_linktype
.constant_part
= 6;
1345 off_linkpl
.constant_part
= 44;
1346 off_nl
= 0; /* Ethernet II */
1347 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1350 #ifdef HAVE_NET_PFVAR_H
1352 off_linktype
.constant_part
= 0;
1353 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1355 off_nl_nosnap
= 0; /* no 802.2 LLC */
1359 case DLT_JUNIPER_MFR
:
1360 case DLT_JUNIPER_MLFR
:
1361 case DLT_JUNIPER_MLPPP
:
1362 case DLT_JUNIPER_PPP
:
1363 case DLT_JUNIPER_CHDLC
:
1364 case DLT_JUNIPER_FRELAY
:
1365 off_linktype
.constant_part
= 4;
1366 off_linkpl
.constant_part
= 4;
1368 off_nl_nosnap
= -1; /* no 802.2 LLC */
1371 case DLT_JUNIPER_ATM1
:
1372 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1373 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1378 case DLT_JUNIPER_ATM2
:
1379 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1380 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1385 /* frames captured on a Juniper PPPoE service PIC
1386 * contain raw ethernet frames */
1387 case DLT_JUNIPER_PPPOE
:
1388 case DLT_JUNIPER_ETHER
:
1389 off_linkpl
.constant_part
= 14;
1390 off_linktype
.constant_part
= 16;
1391 off_nl
= 18; /* Ethernet II */
1392 off_nl_nosnap
= 21; /* 802.3+802.2 */
1395 case DLT_JUNIPER_PPPOE_ATM
:
1396 off_linktype
.constant_part
= 4;
1397 off_linkpl
.constant_part
= 6;
1399 off_nl_nosnap
= -1; /* no 802.2 LLC */
1402 case DLT_JUNIPER_GGSN
:
1403 off_linktype
.constant_part
= 6;
1404 off_linkpl
.constant_part
= 12;
1406 off_nl_nosnap
= -1; /* no 802.2 LLC */
1409 case DLT_JUNIPER_ES
:
1410 off_linktype
.constant_part
= 6;
1411 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1412 off_nl
= -1; /* not really a network layer but raw IP addresses */
1413 off_nl_nosnap
= -1; /* no 802.2 LLC */
1416 case DLT_JUNIPER_MONITOR
:
1417 off_linktype
.constant_part
= 12;
1418 off_linkpl
.constant_part
= 12;
1419 off_nl
= 0; /* raw IP/IP6 header */
1420 off_nl_nosnap
= -1; /* no 802.2 LLC */
1423 case DLT_BACNET_MS_TP
:
1424 off_linktype
.constant_part
= -1;
1425 off_linkpl
.constant_part
= -1;
1430 case DLT_JUNIPER_SERVICES
:
1431 off_linktype
.constant_part
= 12;
1432 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1433 off_nl
= -1; /* L3 proto location dep. on cookie type */
1434 off_nl_nosnap
= -1; /* no 802.2 LLC */
1437 case DLT_JUNIPER_VP
:
1438 off_linktype
.constant_part
= 18;
1439 off_linkpl
.constant_part
= -1;
1444 case DLT_JUNIPER_ST
:
1445 off_linktype
.constant_part
= 18;
1446 off_linkpl
.constant_part
= -1;
1451 case DLT_JUNIPER_ISM
:
1452 off_linktype
.constant_part
= 8;
1453 off_linkpl
.constant_part
= -1;
1458 case DLT_JUNIPER_VS
:
1459 case DLT_JUNIPER_SRX_E2E
:
1460 case DLT_JUNIPER_FIBRECHANNEL
:
1461 case DLT_JUNIPER_ATM_CEMIC
:
1462 off_linktype
.constant_part
= 8;
1463 off_linkpl
.constant_part
= -1;
1475 off_linktype
.constant_part
= -1;
1476 off_linkpl
.constant_part
= -1;
1481 case DLT_MTP2_WITH_PHDR
:
1488 off_linktype
.constant_part
= -1;
1489 off_linkpl
.constant_part
= -1;
1501 off_linktype
.constant_part
= -1;
1502 off_linkpl
.constant_part
= -1;
1508 off_linktype
.constant_part
= -1;
1509 off_linkpl
.constant_part
= 4;
1516 * Currently, only raw "link[N:M]" filtering is supported.
1518 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1519 off_linkpl
.constant_part
= -1;
1520 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1521 off_nl_nosnap
= -1; /* no 802.2 LLC */
1525 off_linktype
.constant_part
= 1;
1526 off_linkpl
.constant_part
= 24; /* ipnet header length */
1531 case DLT_NETANALYZER
:
1532 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1533 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1534 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1535 off_nl
= 0; /* Ethernet II */
1536 off_nl_nosnap
= 3; /* 802.3+802.2 */
1539 case DLT_NETANALYZER_TRANSPARENT
:
1540 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1541 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1542 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1543 off_nl
= 0; /* Ethernet II */
1544 off_nl_nosnap
= 3; /* 802.3+802.2 */
1549 * For values in the range in which we've assigned new
1550 * DLT_ values, only raw "link[N:M]" filtering is supported.
1552 if (linktype
>= DLT_MATCHING_MIN
&&
1553 linktype
<= DLT_MATCHING_MAX
) {
1554 off_linktype
.constant_part
= -1;
1555 off_linkpl
.constant_part
= -1;
1559 bpf_error("unknown data link type %d", linktype
);
1564 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1568 * Load a value relative to the specified absolute offset.
1570 static struct slist
*
1571 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1573 struct slist
*s
, *s2
;
1575 s
= gen_abs_offset_varpart(abs_offset
);
1578 * If "s" is non-null, it has code to arrange that the X register
1579 * contains the variable part of the absolute offset, so we
1580 * generate a load relative to that, with an offset of
1581 * abs_offset->constant_part + offset.
1583 * Otherwise, we can do an absolute load with an offset of
1584 * abs_offset->constant_part + offset.
1588 * "s" points to a list of statements that puts the
1589 * variable part of the absolute offset into the X register.
1590 * Do an indirect load, to use the X register as an offset.
1592 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1593 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1597 * There is no variable part of the absolute offset, so
1598 * just do an absolute load.
1600 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1601 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1607 * Load a value relative to the beginning of the specified header.
1609 static struct slist
*
1610 gen_load_a(offrel
, offset
, size
)
1611 enum e_offrel offrel
;
1614 struct slist
*s
, *s2
;
1619 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1624 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1627 case OR_PREVLINKHDR
:
1628 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1632 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1635 case OR_PREVMPLSHDR
:
1636 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1640 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1643 case OR_LINKPL_NOSNAP
:
1644 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1648 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1653 * Load the X register with the length of the IPv4 header
1654 * (plus the offset of the link-layer header, if it's
1655 * preceded by a variable-length header such as a radio
1656 * header), in bytes.
1658 s
= gen_loadx_iphdrlen();
1661 * Load the item at {offset of the link-layer payload} +
1662 * {offset, relative to the start of the link-layer
1663 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1664 * {specified offset}.
1666 * If the offset of the link-layer payload is variable,
1667 * the variable part of that offset is included in the
1668 * value in the X register, and we include the constant
1669 * part in the offset of the load.
1671 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1672 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1677 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1688 * Generate code to load into the X register the sum of the length of
1689 * the IPv4 header and the variable part of the offset of the link-layer
1692 static struct slist
*
1693 gen_loadx_iphdrlen()
1695 struct slist
*s
, *s2
;
1697 s
= gen_abs_offset_varpart(&off_linkpl
);
1700 * The offset of the link-layer payload has a variable
1701 * part. "s" points to a list of statements that put
1702 * the variable part of that offset into the X register.
1704 * The 4*([k]&0xf) addressing mode can't be used, as we
1705 * don't have a constant offset, so we have to load the
1706 * value in question into the A register and add to it
1707 * the value from the X register.
1709 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1710 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1712 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1715 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1720 * The A register now contains the length of the IP header.
1721 * We need to add to it the variable part of the offset of
1722 * the link-layer payload, which is still in the X
1723 * register, and move the result into the X register.
1725 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1726 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1729 * The offset of the link-layer payload is a constant,
1730 * so no code was generated to load the (non-existent)
1731 * variable part of that offset.
1733 * This means we can use the 4*([k]&0xf) addressing
1734 * mode. Load the length of the IPv4 header, which
1735 * is at an offset of off_nl from the beginning of
1736 * the link-layer payload, and thus at an offset of
1737 * off_linkpl.constant_part + off_nl from the beginning
1738 * of the raw packet data, using that addressing mode.
1740 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1741 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1746 static struct block
*
1753 s
= new_stmt(BPF_LD
|BPF_IMM
);
1755 b
= new_block(JMP(BPF_JEQ
));
1761 static inline struct block
*
1764 return gen_uncond(1);
1767 static inline struct block
*
1770 return gen_uncond(0);
1774 * Byte-swap a 32-bit number.
1775 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1776 * big-endian platforms.)
1778 #define SWAPLONG(y) \
1779 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1782 * Generate code to match a particular packet type.
1784 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1785 * value, if <= ETHERMTU. We use that to determine whether to
1786 * match the type/length field or to check the type/length field for
1787 * a value <= ETHERMTU to see whether it's a type field and then do
1788 * the appropriate test.
1790 static struct block
*
1791 gen_ether_linktype(proto
)
1794 struct block
*b0
, *b1
;
1800 case LLCSAP_NETBEUI
:
1802 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1803 * so we check the DSAP and SSAP.
1805 * LLCSAP_IP checks for IP-over-802.2, rather
1806 * than IP-over-Ethernet or IP-over-SNAP.
1808 * XXX - should we check both the DSAP and the
1809 * SSAP, like this, or should we check just the
1810 * DSAP, as we do for other types <= ETHERMTU
1811 * (i.e., other SAP values)?
1813 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1815 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1816 ((proto
<< 8) | proto
));
1824 * Ethernet_II frames, which are Ethernet
1825 * frames with a frame type of ETHERTYPE_IPX;
1827 * Ethernet_802.3 frames, which are 802.3
1828 * frames (i.e., the type/length field is
1829 * a length field, <= ETHERMTU, rather than
1830 * a type field) with the first two bytes
1831 * after the Ethernet/802.3 header being
1834 * Ethernet_802.2 frames, which are 802.3
1835 * frames with an 802.2 LLC header and
1836 * with the IPX LSAP as the DSAP in the LLC
1839 * Ethernet_SNAP frames, which are 802.3
1840 * frames with an LLC header and a SNAP
1841 * header and with an OUI of 0x000000
1842 * (encapsulated Ethernet) and a protocol
1843 * ID of ETHERTYPE_IPX in the SNAP header.
1845 * XXX - should we generate the same code both
1846 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1850 * This generates code to check both for the
1851 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1853 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1854 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1858 * Now we add code to check for SNAP frames with
1859 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1861 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1865 * Now we generate code to check for 802.3
1866 * frames in general.
1868 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1872 * Now add the check for 802.3 frames before the
1873 * check for Ethernet_802.2 and Ethernet_802.3,
1874 * as those checks should only be done on 802.3
1875 * frames, not on Ethernet frames.
1880 * Now add the check for Ethernet_II frames, and
1881 * do that before checking for the other frame
1884 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1888 case ETHERTYPE_ATALK
:
1889 case ETHERTYPE_AARP
:
1891 * EtherTalk (AppleTalk protocols on Ethernet link
1892 * layer) may use 802.2 encapsulation.
1896 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1897 * we check for an Ethernet type field less than
1898 * 1500, which means it's an 802.3 length field.
1900 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1904 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1905 * SNAP packets with an organization code of
1906 * 0x080007 (Apple, for Appletalk) and a protocol
1907 * type of ETHERTYPE_ATALK (Appletalk).
1909 * 802.2-encapsulated ETHERTYPE_AARP packets are
1910 * SNAP packets with an organization code of
1911 * 0x000000 (encapsulated Ethernet) and a protocol
1912 * type of ETHERTYPE_AARP (Appletalk ARP).
1914 if (proto
== ETHERTYPE_ATALK
)
1915 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1916 else /* proto == ETHERTYPE_AARP */
1917 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1921 * Check for Ethernet encapsulation (Ethertalk
1922 * phase 1?); we just check for the Ethernet
1925 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1931 if (proto
<= ETHERMTU
) {
1933 * This is an LLC SAP value, so the frames
1934 * that match would be 802.2 frames.
1935 * Check that the frame is an 802.2 frame
1936 * (i.e., that the length/type field is
1937 * a length field, <= ETHERMTU) and
1938 * then check the DSAP.
1940 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1942 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1947 * This is an Ethernet type, so compare
1948 * the length/type field with it (if
1949 * the frame is an 802.2 frame, the length
1950 * field will be <= ETHERMTU, and, as
1951 * "proto" is > ETHERMTU, this test
1952 * will fail and the frame won't match,
1953 * which is what we want).
1955 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1962 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1963 * or IPv6 then we have an error.
1965 static struct block
*
1966 gen_ipnet_linktype(proto
)
1972 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1975 case ETHERTYPE_IPV6
:
1976 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1977 (bpf_int32
)IPH_AF_INET6
);
1988 * Generate code to match a particular packet type.
1990 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1991 * value, if <= ETHERMTU. We use that to determine whether to
1992 * match the type field or to check the type field for the special
1993 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1995 static struct block
*
1996 gen_linux_sll_linktype(proto
)
1999 struct block
*b0
, *b1
;
2005 case LLCSAP_NETBEUI
:
2007 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2008 * so we check the DSAP and SSAP.
2010 * LLCSAP_IP checks for IP-over-802.2, rather
2011 * than IP-over-Ethernet or IP-over-SNAP.
2013 * XXX - should we check both the DSAP and the
2014 * SSAP, like this, or should we check just the
2015 * DSAP, as we do for other types <= ETHERMTU
2016 * (i.e., other SAP values)?
2018 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2019 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
2020 ((proto
<< 8) | proto
));
2026 * Ethernet_II frames, which are Ethernet
2027 * frames with a frame type of ETHERTYPE_IPX;
2029 * Ethernet_802.3 frames, which have a frame
2030 * type of LINUX_SLL_P_802_3;
2032 * Ethernet_802.2 frames, which are 802.3
2033 * frames with an 802.2 LLC header (i.e, have
2034 * a frame type of LINUX_SLL_P_802_2) and
2035 * with the IPX LSAP as the DSAP in the LLC
2038 * Ethernet_SNAP frames, which are 802.3
2039 * frames with an LLC header and a SNAP
2040 * header and with an OUI of 0x000000
2041 * (encapsulated Ethernet) and a protocol
2042 * ID of ETHERTYPE_IPX in the SNAP header.
2044 * First, do the checks on LINUX_SLL_P_802_2
2045 * frames; generate the check for either
2046 * Ethernet_802.2 or Ethernet_SNAP frames, and
2047 * then put a check for LINUX_SLL_P_802_2 frames
2050 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2051 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2053 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2057 * Now check for 802.3 frames and OR that with
2058 * the previous test.
2060 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2064 * Now add the check for Ethernet_II frames, and
2065 * do that before checking for the other frame
2068 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2072 case ETHERTYPE_ATALK
:
2073 case ETHERTYPE_AARP
:
2075 * EtherTalk (AppleTalk protocols on Ethernet link
2076 * layer) may use 802.2 encapsulation.
2080 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2081 * we check for the 802.2 protocol type in the
2082 * "Ethernet type" field.
2084 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2087 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2088 * SNAP packets with an organization code of
2089 * 0x080007 (Apple, for Appletalk) and a protocol
2090 * type of ETHERTYPE_ATALK (Appletalk).
2092 * 802.2-encapsulated ETHERTYPE_AARP packets are
2093 * SNAP packets with an organization code of
2094 * 0x000000 (encapsulated Ethernet) and a protocol
2095 * type of ETHERTYPE_AARP (Appletalk ARP).
2097 if (proto
== ETHERTYPE_ATALK
)
2098 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2099 else /* proto == ETHERTYPE_AARP */
2100 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2104 * Check for Ethernet encapsulation (Ethertalk
2105 * phase 1?); we just check for the Ethernet
2108 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2114 if (proto
<= ETHERMTU
) {
2116 * This is an LLC SAP value, so the frames
2117 * that match would be 802.2 frames.
2118 * Check for the 802.2 protocol type
2119 * in the "Ethernet type" field, and
2120 * then check the DSAP.
2122 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2123 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2129 * This is an Ethernet type, so compare
2130 * the length/type field with it (if
2131 * the frame is an 802.2 frame, the length
2132 * field will be <= ETHERMTU, and, as
2133 * "proto" is > ETHERMTU, this test
2134 * will fail and the frame won't match,
2135 * which is what we want).
2137 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2142 static struct slist
*
2143 gen_load_prism_llprefixlen()
2145 struct slist
*s1
, *s2
;
2146 struct slist
*sjeq_avs_cookie
;
2147 struct slist
*sjcommon
;
2150 * This code is not compatible with the optimizer, as
2151 * we are generating jmp instructions within a normal
2152 * slist of instructions
2157 * Generate code to load the length of the radio header into
2158 * the register assigned to hold that length, if one has been
2159 * assigned. (If one hasn't been assigned, no code we've
2160 * generated uses that prefix, so we don't need to generate any
2163 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2164 * or always use the AVS header rather than the Prism header.
2165 * We load a 4-byte big-endian value at the beginning of the
2166 * raw packet data, and see whether, when masked with 0xFFFFF000,
2167 * it's equal to 0x80211000. If so, that indicates that it's
2168 * an AVS header (the masked-out bits are the version number).
2169 * Otherwise, it's a Prism header.
2171 * XXX - the Prism header is also, in theory, variable-length,
2172 * but no known software generates headers that aren't 144
2175 if (off_linkhdr
.reg
!= -1) {
2179 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2183 * AND it with 0xFFFFF000.
2185 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2186 s2
->s
.k
= 0xFFFFF000;
2190 * Compare with 0x80211000.
2192 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2193 sjeq_avs_cookie
->s
.k
= 0x80211000;
2194 sappend(s1
, sjeq_avs_cookie
);
2199 * The 4 bytes at an offset of 4 from the beginning of
2200 * the AVS header are the length of the AVS header.
2201 * That field is big-endian.
2203 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2206 sjeq_avs_cookie
->s
.jt
= s2
;
2209 * Now jump to the code to allocate a register
2210 * into which to save the header length and
2211 * store the length there. (The "jump always"
2212 * instruction needs to have the k field set;
2213 * it's added to the PC, so, as we're jumping
2214 * over a single instruction, it should be 1.)
2216 sjcommon
= new_stmt(JMP(BPF_JA
));
2218 sappend(s1
, sjcommon
);
2221 * Now for the code that handles the Prism header.
2222 * Just load the length of the Prism header (144)
2223 * into the A register. Have the test for an AVS
2224 * header branch here if we don't have an AVS header.
2226 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2229 sjeq_avs_cookie
->s
.jf
= s2
;
2232 * Now allocate a register to hold that value and store
2233 * it. The code for the AVS header will jump here after
2234 * loading the length of the AVS header.
2236 s2
= new_stmt(BPF_ST
);
2237 s2
->s
.k
= off_linkhdr
.reg
;
2239 sjcommon
->s
.jf
= s2
;
2242 * Now move it into the X register.
2244 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2252 static struct slist
*
2253 gen_load_avs_llprefixlen()
2255 struct slist
*s1
, *s2
;
2258 * Generate code to load the length of the AVS header into
2259 * the register assigned to hold that length, if one has been
2260 * assigned. (If one hasn't been assigned, no code we've
2261 * generated uses that prefix, so we don't need to generate any
2264 if (off_linkhdr
.reg
!= -1) {
2266 * The 4 bytes at an offset of 4 from the beginning of
2267 * the AVS header are the length of the AVS header.
2268 * That field is big-endian.
2270 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2274 * Now allocate a register to hold that value and store
2277 s2
= new_stmt(BPF_ST
);
2278 s2
->s
.k
= off_linkhdr
.reg
;
2282 * Now move it into the X register.
2284 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2292 static struct slist
*
2293 gen_load_radiotap_llprefixlen()
2295 struct slist
*s1
, *s2
;
2298 * Generate code to load the length of the radiotap header into
2299 * the register assigned to hold that length, if one has been
2300 * assigned. (If one hasn't been assigned, no code we've
2301 * generated uses that prefix, so we don't need to generate any
2304 if (off_linkhdr
.reg
!= -1) {
2306 * The 2 bytes at offsets of 2 and 3 from the beginning
2307 * of the radiotap header are the length of the radiotap
2308 * header; unfortunately, it's little-endian, so we have
2309 * to load it a byte at a time and construct the value.
2313 * Load the high-order byte, at an offset of 3, shift it
2314 * left a byte, and put the result in the X register.
2316 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2318 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2321 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2325 * Load the next byte, at an offset of 2, and OR the
2326 * value from the X register into it.
2328 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2331 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2335 * Now allocate a register to hold that value and store
2338 s2
= new_stmt(BPF_ST
);
2339 s2
->s
.k
= off_linkhdr
.reg
;
2343 * Now move it into the X register.
2345 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2354 * At the moment we treat PPI as normal Radiotap encoded
2355 * packets. The difference is in the function that generates
2356 * the code at the beginning to compute the header length.
2357 * Since this code generator of PPI supports bare 802.11
2358 * encapsulation only (i.e. the encapsulated DLT should be
2359 * DLT_IEEE802_11) we generate code to check for this too;
2360 * that's done in finish_parse().
2362 static struct slist
*
2363 gen_load_ppi_llprefixlen()
2365 struct slist
*s1
, *s2
;
2368 * Generate code to load the length of the radiotap header
2369 * into the register assigned to hold that length, if one has
2372 if (off_linkhdr
.reg
!= -1) {
2374 * The 2 bytes at offsets of 2 and 3 from the beginning
2375 * of the radiotap header are the length of the radiotap
2376 * header; unfortunately, it's little-endian, so we have
2377 * to load it a byte at a time and construct the value.
2381 * Load the high-order byte, at an offset of 3, shift it
2382 * left a byte, and put the result in the X register.
2384 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2386 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2389 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2393 * Load the next byte, at an offset of 2, and OR the
2394 * value from the X register into it.
2396 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2399 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2403 * Now allocate a register to hold that value and store
2406 s2
= new_stmt(BPF_ST
);
2407 s2
->s
.k
= off_linkhdr
.reg
;
2411 * Now move it into the X register.
2413 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2422 * Load a value relative to the beginning of the link-layer header after the 802.11
2423 * header, i.e. LLC_SNAP.
2424 * The link-layer header doesn't necessarily begin at the beginning
2425 * of the packet data; there might be a variable-length prefix containing
2426 * radio information.
2428 static struct slist
*
2429 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2432 struct slist
*sjset_data_frame_1
;
2433 struct slist
*sjset_data_frame_2
;
2434 struct slist
*sjset_qos
;
2435 struct slist
*sjset_radiotap_flags
;
2436 struct slist
*sjset_radiotap_tsft
;
2437 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2438 struct slist
*s_roundup
;
2440 if (off_linkpl
.reg
== -1) {
2442 * No register has been assigned to the offset of
2443 * the link-layer payload, which means nobody needs
2444 * it; don't bother computing it - just return
2445 * what we already have.
2451 * This code is not compatible with the optimizer, as
2452 * we are generating jmp instructions within a normal
2453 * slist of instructions
2458 * If "s" is non-null, it has code to arrange that the X register
2459 * contains the length of the prefix preceding the link-layer
2462 * Otherwise, the length of the prefix preceding the link-layer
2463 * header is "off_outermostlinkhdr.constant_part".
2467 * There is no variable-length header preceding the
2468 * link-layer header.
2470 * Load the length of the fixed-length prefix preceding
2471 * the link-layer header (if any) into the X register,
2472 * and store it in the off_linkpl.reg register.
2473 * That length is off_outermostlinkhdr.constant_part.
2475 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2476 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2480 * The X register contains the offset of the beginning of the
2481 * link-layer header; add 24, which is the minimum length
2482 * of the MAC header for a data frame, to that, and store it
2483 * in off_linkpl.reg, and then load the Frame Control field,
2484 * which is at the offset in the X register, with an indexed load.
2486 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2488 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2491 s2
= new_stmt(BPF_ST
);
2492 s2
->s
.k
= off_linkpl
.reg
;
2495 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2500 * Check the Frame Control field to see if this is a data frame;
2501 * a data frame has the 0x08 bit (b3) in that field set and the
2502 * 0x04 bit (b2) clear.
2504 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2505 sjset_data_frame_1
->s
.k
= 0x08;
2506 sappend(s
, sjset_data_frame_1
);
2509 * If b3 is set, test b2, otherwise go to the first statement of
2510 * the rest of the program.
2512 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2513 sjset_data_frame_2
->s
.k
= 0x04;
2514 sappend(s
, sjset_data_frame_2
);
2515 sjset_data_frame_1
->s
.jf
= snext
;
2518 * If b2 is not set, this is a data frame; test the QoS bit.
2519 * Otherwise, go to the first statement of the rest of the
2522 sjset_data_frame_2
->s
.jt
= snext
;
2523 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2524 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2525 sappend(s
, sjset_qos
);
2528 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2530 * Otherwise, go to the first statement of the rest of the
2533 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2534 s2
->s
.k
= off_linkpl
.reg
;
2536 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2539 s2
= new_stmt(BPF_ST
);
2540 s2
->s
.k
= off_linkpl
.reg
;
2544 * If we have a radiotap header, look at it to see whether
2545 * there's Atheros padding between the MAC-layer header
2548 * Note: all of the fields in the radiotap header are
2549 * little-endian, so we byte-swap all of the values
2550 * we test against, as they will be loaded as big-endian
2553 if (linktype
== DLT_IEEE802_11_RADIO
) {
2555 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2556 * in the presence flag?
2558 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2562 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2563 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2564 sappend(s
, sjset_radiotap_flags
);
2567 * If not, skip all of this.
2569 sjset_radiotap_flags
->s
.jf
= snext
;
2572 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2574 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2575 new_stmt(JMP(BPF_JSET
));
2576 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2577 sappend(s
, sjset_radiotap_tsft
);
2580 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2581 * at an offset of 16 from the beginning of the raw packet
2582 * data (8 bytes for the radiotap header and 8 bytes for
2585 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2588 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2592 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2593 sjset_tsft_datapad
->s
.k
= 0x20;
2594 sappend(s
, sjset_tsft_datapad
);
2597 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2598 * at an offset of 8 from the beginning of the raw packet
2599 * data (8 bytes for the radiotap header).
2601 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2604 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2608 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2609 sjset_notsft_datapad
->s
.k
= 0x20;
2610 sappend(s
, sjset_notsft_datapad
);
2613 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2614 * set, round the length of the 802.11 header to
2615 * a multiple of 4. Do that by adding 3 and then
2616 * dividing by and multiplying by 4, which we do by
2619 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2620 s_roundup
->s
.k
= off_linkpl
.reg
;
2621 sappend(s
, s_roundup
);
2622 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2625 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2628 s2
= new_stmt(BPF_ST
);
2629 s2
->s
.k
= off_linkpl
.reg
;
2632 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2633 sjset_tsft_datapad
->s
.jf
= snext
;
2634 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2635 sjset_notsft_datapad
->s
.jf
= snext
;
2637 sjset_qos
->s
.jf
= snext
;
2643 insert_compute_vloffsets(b
)
2648 /* There is an implicit dependency between the link
2649 * payload and link header since the payload computation
2650 * includes the variable part of the header. Therefore,
2651 * if nobody else has allocated a register for the link
2652 * header and we need it, do it now. */
2653 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2654 off_linkhdr
.reg
== -1)
2655 off_linkhdr
.reg
= alloc_reg();
2658 * For link-layer types that have a variable-length header
2659 * preceding the link-layer header, generate code to load
2660 * the offset of the link-layer header into the register
2661 * assigned to that offset, if any.
2663 * XXX - this, and the next switch statement, won't handle
2664 * encapsulation of 802.11 or 802.11+radio information in
2665 * some other protocol stack. That's significantly more
2668 switch (outermostlinktype
) {
2670 case DLT_PRISM_HEADER
:
2671 s
= gen_load_prism_llprefixlen();
2674 case DLT_IEEE802_11_RADIO_AVS
:
2675 s
= gen_load_avs_llprefixlen();
2678 case DLT_IEEE802_11_RADIO
:
2679 s
= gen_load_radiotap_llprefixlen();
2683 s
= gen_load_ppi_llprefixlen();
2692 * For link-layer types that have a variable-length link-layer
2693 * header, generate code to load the offset of the link-layer
2694 * payload into the register assigned to that offset, if any.
2696 switch (outermostlinktype
) {
2698 case DLT_IEEE802_11
:
2699 case DLT_PRISM_HEADER
:
2700 case DLT_IEEE802_11_RADIO_AVS
:
2701 case DLT_IEEE802_11_RADIO
:
2703 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2708 * If we have any offset-loading code, append all the
2709 * existing statements in the block to those statements,
2710 * and make the resulting list the list of statements
2714 sappend(s
, b
->stmts
);
2719 static struct block
*
2720 gen_ppi_dlt_check(void)
2722 struct slist
*s_load_dlt
;
2725 if (linktype
== DLT_PPI
)
2727 /* Create the statements that check for the DLT
2729 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2730 s_load_dlt
->s
.k
= 4;
2732 b
= new_block(JMP(BPF_JEQ
));
2734 b
->stmts
= s_load_dlt
;
2735 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2746 * Take an absolute offset, and:
2748 * if it has no variable part, return NULL;
2750 * if it has a variable part, generate code to load the register
2751 * containing that variable part into the X register, returning
2752 * a pointer to that code - if no register for that offset has
2753 * been allocated, allocate it first.
2755 * (The code to set that register will be generated later, but will
2756 * be placed earlier in the code sequence.)
2758 static struct slist
*
2759 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2763 if (off
->is_variable
) {
2764 if (off
->reg
== -1) {
2766 * We haven't yet assigned a register for the
2767 * variable part of the offset of the link-layer
2768 * header; allocate one.
2770 off
->reg
= alloc_reg();
2774 * Load the register containing the variable part of the
2775 * offset of the link-layer header into the X register.
2777 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2782 * That offset isn't variable, there's no variable part,
2783 * so we don't need to generate any code.
2790 * Map an Ethernet type to the equivalent PPP type.
2793 ethertype_to_ppptype(proto
)
2802 case ETHERTYPE_IPV6
:
2810 case ETHERTYPE_ATALK
:
2824 * I'm assuming the "Bridging PDU"s that go
2825 * over PPP are Spanning Tree Protocol
2839 * Generate any tests that, for encapsulation of a link-layer packet
2840 * inside another protocol stack, need to be done to check for those
2841 * link-layer packets (and that haven't already been done by a check
2842 * for that encapsulation).
2844 static struct block
*
2845 gen_prevlinkhdr_check(void)
2850 return gen_geneve_ll_check();
2852 switch (prevlinktype
) {
2856 * This is LANE-encapsulated Ethernet; check that the LANE
2857 * packet doesn't begin with an LE Control marker, i.e.
2858 * that it's data, not a control message.
2860 * (We've already generated a test for LANE.)
2862 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2868 * No such tests are necessary.
2876 * Generate code to match a particular packet type by matching the
2877 * link-layer type field or fields in the 802.2 LLC header.
2879 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2880 * value, if <= ETHERMTU.
2882 static struct block
*
2886 struct block
*b0
, *b1
, *b2
;
2887 const char *description
;
2889 /* are we checking MPLS-encapsulated packets? */
2890 if (label_stack_depth
> 0) {
2894 /* FIXME add other L3 proto IDs */
2895 return gen_mpls_linktype(Q_IP
);
2897 case ETHERTYPE_IPV6
:
2899 /* FIXME add other L3 proto IDs */
2900 return gen_mpls_linktype(Q_IPV6
);
2903 bpf_error("unsupported protocol over mpls");
2911 case DLT_NETANALYZER
:
2912 case DLT_NETANALYZER_TRANSPARENT
:
2913 /* Geneve has an EtherType regardless of whether there is an
2916 b0
= gen_prevlinkhdr_check();
2920 b1
= gen_ether_linktype(proto
);
2931 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2935 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2941 case DLT_IEEE802_11
:
2942 case DLT_PRISM_HEADER
:
2943 case DLT_IEEE802_11_RADIO_AVS
:
2944 case DLT_IEEE802_11_RADIO
:
2947 * Check that we have a data frame.
2949 b0
= gen_check_802_11_data_frame();
2952 * Now check for the specified link-layer type.
2954 b1
= gen_llc_linktype(proto
);
2962 * XXX - check for LLC frames.
2964 return gen_llc_linktype(proto
);
2970 * XXX - check for LLC PDUs, as per IEEE 802.5.
2972 return gen_llc_linktype(proto
);
2976 case DLT_ATM_RFC1483
:
2978 case DLT_IP_OVER_FC
:
2979 return gen_llc_linktype(proto
);
2985 * Check for an LLC-encapsulated version of this protocol;
2986 * if we were checking for LANE, linktype would no longer
2989 * Check for LLC encapsulation and then check the protocol.
2991 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2992 b1
= gen_llc_linktype(proto
);
2999 return gen_linux_sll_linktype(proto
);
3004 case DLT_SLIP_BSDOS
:
3007 * These types don't provide any type field; packets
3008 * are always IPv4 or IPv6.
3010 * XXX - for IPv4, check for a version number of 4, and,
3011 * for IPv6, check for a version number of 6?
3016 /* Check for a version number of 4. */
3017 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3019 case ETHERTYPE_IPV6
:
3020 /* Check for a version number of 6. */
3021 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3024 return gen_false(); /* always false */
3031 * Raw IPv4, so no type field.
3033 if (proto
== ETHERTYPE_IP
)
3034 return gen_true(); /* always true */
3036 /* Checking for something other than IPv4; always false */
3043 * Raw IPv6, so no type field.
3045 if (proto
== ETHERTYPE_IPV6
)
3046 return gen_true(); /* always true */
3048 /* Checking for something other than IPv6; always false */
3055 case DLT_PPP_SERIAL
:
3058 * We use Ethernet protocol types inside libpcap;
3059 * map them to the corresponding PPP protocol types.
3061 proto
= ethertype_to_ppptype(proto
);
3062 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3068 * We use Ethernet protocol types inside libpcap;
3069 * map them to the corresponding PPP protocol types.
3075 * Also check for Van Jacobson-compressed IP.
3076 * XXX - do this for other forms of PPP?
3078 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3079 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3081 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3086 proto
= ethertype_to_ppptype(proto
);
3087 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3097 * For DLT_NULL, the link-layer header is a 32-bit
3098 * word containing an AF_ value in *host* byte order,
3099 * and for DLT_ENC, the link-layer header begins
3100 * with a 32-bit work containing an AF_ value in
3103 * In addition, if we're reading a saved capture file,
3104 * the host byte order in the capture may not be the
3105 * same as the host byte order on this machine.
3107 * For DLT_LOOP, the link-layer header is a 32-bit
3108 * word containing an AF_ value in *network* byte order.
3110 * XXX - AF_ values may, unfortunately, be platform-
3111 * dependent; for example, FreeBSD's AF_INET6 is 24
3112 * whilst NetBSD's and OpenBSD's is 26.
3114 * This means that, when reading a capture file, just
3115 * checking for our AF_INET6 value won't work if the
3116 * capture file came from another OS.
3125 case ETHERTYPE_IPV6
:
3132 * Not a type on which we support filtering.
3133 * XXX - support those that have AF_ values
3134 * #defined on this platform, at least?
3139 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3141 * The AF_ value is in host byte order, but
3142 * the BPF interpreter will convert it to
3143 * network byte order.
3145 * If this is a save file, and it's from a
3146 * machine with the opposite byte order to
3147 * ours, we byte-swap the AF_ value.
3149 * Then we run it through "htonl()", and
3150 * generate code to compare against the result.
3152 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3153 proto
= SWAPLONG(proto
);
3154 proto
= htonl(proto
);
3156 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3158 #ifdef HAVE_NET_PFVAR_H
3161 * af field is host byte order in contrast to the rest of
3164 if (proto
== ETHERTYPE_IP
)
3165 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3166 BPF_B
, (bpf_int32
)AF_INET
));
3167 else if (proto
== ETHERTYPE_IPV6
)
3168 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3169 BPF_B
, (bpf_int32
)AF_INET6
));
3174 #endif /* HAVE_NET_PFVAR_H */
3177 case DLT_ARCNET_LINUX
:
3179 * XXX should we check for first fragment if the protocol
3187 case ETHERTYPE_IPV6
:
3188 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3189 (bpf_int32
)ARCTYPE_INET6
));
3192 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3193 (bpf_int32
)ARCTYPE_IP
);
3194 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3195 (bpf_int32
)ARCTYPE_IP_OLD
);
3200 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3201 (bpf_int32
)ARCTYPE_ARP
);
3202 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3203 (bpf_int32
)ARCTYPE_ARP_OLD
);
3207 case ETHERTYPE_REVARP
:
3208 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3209 (bpf_int32
)ARCTYPE_REVARP
));
3211 case ETHERTYPE_ATALK
:
3212 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3213 (bpf_int32
)ARCTYPE_ATALK
));
3220 case ETHERTYPE_ATALK
:
3230 * XXX - assumes a 2-byte Frame Relay header with
3231 * DLCI and flags. What if the address is longer?
3237 * Check for the special NLPID for IP.
3239 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3241 case ETHERTYPE_IPV6
:
3243 * Check for the special NLPID for IPv6.
3245 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3249 * Check for several OSI protocols.
3251 * Frame Relay packets typically have an OSI
3252 * NLPID at the beginning; we check for each
3255 * What we check for is the NLPID and a frame
3256 * control field of UI, i.e. 0x03 followed
3259 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3260 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3261 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3273 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3275 case DLT_JUNIPER_MFR
:
3276 case DLT_JUNIPER_MLFR
:
3277 case DLT_JUNIPER_MLPPP
:
3278 case DLT_JUNIPER_ATM1
:
3279 case DLT_JUNIPER_ATM2
:
3280 case DLT_JUNIPER_PPPOE
:
3281 case DLT_JUNIPER_PPPOE_ATM
:
3282 case DLT_JUNIPER_GGSN
:
3283 case DLT_JUNIPER_ES
:
3284 case DLT_JUNIPER_MONITOR
:
3285 case DLT_JUNIPER_SERVICES
:
3286 case DLT_JUNIPER_ETHER
:
3287 case DLT_JUNIPER_PPP
:
3288 case DLT_JUNIPER_FRELAY
:
3289 case DLT_JUNIPER_CHDLC
:
3290 case DLT_JUNIPER_VP
:
3291 case DLT_JUNIPER_ST
:
3292 case DLT_JUNIPER_ISM
:
3293 case DLT_JUNIPER_VS
:
3294 case DLT_JUNIPER_SRX_E2E
:
3295 case DLT_JUNIPER_FIBRECHANNEL
:
3296 case DLT_JUNIPER_ATM_CEMIC
:
3298 /* just lets verify the magic number for now -
3299 * on ATM we may have up to 6 different encapsulations on the wire
3300 * and need a lot of heuristics to figure out that the payload
3303 * FIXME encapsulation specific BPF_ filters
3305 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3307 case DLT_BACNET_MS_TP
:
3308 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3311 return gen_ipnet_linktype(proto
);
3313 case DLT_LINUX_IRDA
:
3314 bpf_error("IrDA link-layer type filtering not implemented");
3317 bpf_error("DOCSIS link-layer type filtering not implemented");
3320 case DLT_MTP2_WITH_PHDR
:
3321 bpf_error("MTP2 link-layer type filtering not implemented");
3324 bpf_error("ERF link-layer type filtering not implemented");
3327 bpf_error("PFSYNC link-layer type filtering not implemented");
3329 case DLT_LINUX_LAPD
:
3330 bpf_error("LAPD link-layer type filtering not implemented");
3334 case DLT_USB_LINUX_MMAPPED
:
3335 bpf_error("USB link-layer type filtering not implemented");
3337 case DLT_BLUETOOTH_HCI_H4
:
3338 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3339 bpf_error("Bluetooth link-layer type filtering not implemented");
3342 case DLT_CAN_SOCKETCAN
:
3343 bpf_error("CAN link-layer type filtering not implemented");
3345 case DLT_IEEE802_15_4
:
3346 case DLT_IEEE802_15_4_LINUX
:
3347 case DLT_IEEE802_15_4_NONASK_PHY
:
3348 case DLT_IEEE802_15_4_NOFCS
:
3349 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3351 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3352 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3355 bpf_error("SITA link-layer type filtering not implemented");
3358 bpf_error("RAIF1 link-layer type filtering not implemented");
3361 bpf_error("IPMB link-layer type filtering not implemented");
3364 bpf_error("AX.25 link-layer type filtering not implemented");
3367 /* Using the fixed-size NFLOG header it is possible to tell only
3368 * the address family of the packet, other meaningful data is
3369 * either missing or behind TLVs.
3371 bpf_error("NFLOG link-layer type filtering not implemented");
3375 * Does this link-layer header type have a field
3376 * indicating the type of the next protocol? If
3377 * so, off_linktype.constant_part will be the offset of that
3378 * field in the packet; if not, it will be -1.
3380 if (off_linktype
.constant_part
!= (u_int
)-1) {
3382 * Yes; assume it's an Ethernet type. (If
3383 * it's not, it needs to be handled specially
3386 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3389 * No; report an error.
3391 description
= pcap_datalink_val_to_description(linktype
);
3392 if (description
!= NULL
) {
3393 bpf_error("%s link-layer type filtering not implemented",
3396 bpf_error("DLT %u link-layer type filtering not implemented",
3405 * Check for an LLC SNAP packet with a given organization code and
3406 * protocol type; we check the entire contents of the 802.2 LLC and
3407 * snap headers, checking for DSAP and SSAP of SNAP and a control
3408 * field of 0x03 in the LLC header, and for the specified organization
3409 * code and protocol type in the SNAP header.
3411 static struct block
*
3412 gen_snap(orgcode
, ptype
)
3413 bpf_u_int32 orgcode
;
3416 u_char snapblock
[8];
3418 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3419 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3420 snapblock
[2] = 0x03; /* control = UI */
3421 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3422 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3423 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3424 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3425 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3426 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3430 * Generate code to match frames with an LLC header.
3435 struct block
*b0
, *b1
;
3441 * We check for an Ethernet type field less than
3442 * 1500, which means it's an 802.3 length field.
3444 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3448 * Now check for the purported DSAP and SSAP not being
3449 * 0xFF, to rule out NetWare-over-802.3.
3451 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3458 * We check for LLC traffic.
3460 b0
= gen_atmtype_abbrev(A_LLC
);
3463 case DLT_IEEE802
: /* Token Ring */
3465 * XXX - check for LLC frames.
3471 * XXX - check for LLC frames.
3475 case DLT_ATM_RFC1483
:
3477 * For LLC encapsulation, these are defined to have an
3480 * For VC encapsulation, they don't, but there's no
3481 * way to check for that; the protocol used on the VC
3482 * is negotiated out of band.
3486 case DLT_IEEE802_11
:
3487 case DLT_PRISM_HEADER
:
3488 case DLT_IEEE802_11_RADIO
:
3489 case DLT_IEEE802_11_RADIO_AVS
:
3492 * Check that we have a data frame.
3494 b0
= gen_check_802_11_data_frame();
3498 bpf_error("'llc' not supported for linktype %d", linktype
);
3506 struct block
*b0
, *b1
;
3510 * Check whether this is an LLC frame.
3515 * Load the control byte and test the low-order bit; it must
3516 * be clear for I frames.
3518 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3519 b1
= new_block(JMP(BPF_JSET
));
3530 struct block
*b0
, *b1
;
3533 * Check whether this is an LLC frame.
3538 * Now compare the low-order 2 bit of the control byte against
3539 * the appropriate value for S frames.
3541 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3549 struct block
*b0
, *b1
;
3552 * Check whether this is an LLC frame.
3557 * Now compare the low-order 2 bit of the control byte against
3558 * the appropriate value for U frames.
3560 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3566 gen_llc_s_subtype(bpf_u_int32 subtype
)
3568 struct block
*b0
, *b1
;
3571 * Check whether this is an LLC frame.
3576 * Now check for an S frame with the appropriate type.
3578 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3584 gen_llc_u_subtype(bpf_u_int32 subtype
)
3586 struct block
*b0
, *b1
;
3589 * Check whether this is an LLC frame.
3594 * Now check for a U frame with the appropriate type.
3596 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3602 * Generate code to match a particular packet type, for link-layer types
3603 * using 802.2 LLC headers.
3605 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3606 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3608 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3609 * value, if <= ETHERMTU. We use that to determine whether to
3610 * match the DSAP or both DSAP and LSAP or to check the OUI and
3611 * protocol ID in a SNAP header.
3613 static struct block
*
3614 gen_llc_linktype(proto
)
3618 * XXX - handle token-ring variable-length header.
3624 case LLCSAP_NETBEUI
:
3626 * XXX - should we check both the DSAP and the
3627 * SSAP, like this, or should we check just the
3628 * DSAP, as we do for other SAP values?
3630 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3631 ((proto
<< 8) | proto
));
3635 * XXX - are there ever SNAP frames for IPX on
3636 * non-Ethernet 802.x networks?
3638 return gen_cmp(OR_LLC
, 0, BPF_B
,
3639 (bpf_int32
)LLCSAP_IPX
);
3641 case ETHERTYPE_ATALK
:
3643 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3644 * SNAP packets with an organization code of
3645 * 0x080007 (Apple, for Appletalk) and a protocol
3646 * type of ETHERTYPE_ATALK (Appletalk).
3648 * XXX - check for an organization code of
3649 * encapsulated Ethernet as well?
3651 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3655 * XXX - we don't have to check for IPX 802.3
3656 * here, but should we check for the IPX Ethertype?
3658 if (proto
<= ETHERMTU
) {
3660 * This is an LLC SAP value, so check
3663 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3666 * This is an Ethernet type; we assume that it's
3667 * unlikely that it'll appear in the right place
3668 * at random, and therefore check only the
3669 * location that would hold the Ethernet type
3670 * in a SNAP frame with an organization code of
3671 * 0x000000 (encapsulated Ethernet).
3673 * XXX - if we were to check for the SNAP DSAP and
3674 * LSAP, as per XXX, and were also to check for an
3675 * organization code of 0x000000 (encapsulated
3676 * Ethernet), we'd do
3678 * return gen_snap(0x000000, proto);
3680 * here; for now, we don't, as per the above.
3681 * I don't know whether it's worth the extra CPU
3682 * time to do the right check or not.
3684 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3689 static struct block
*
3690 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3694 u_int src_off
, dst_off
;
3696 struct block
*b0
, *b1
;
3710 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3711 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3717 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3718 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3725 b0
= gen_linktype(proto
);
3726 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3732 static struct block
*
3733 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3734 struct in6_addr
*addr
;
3735 struct in6_addr
*mask
;
3737 u_int src_off
, dst_off
;
3739 struct block
*b0
, *b1
;
3754 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3755 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3761 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3762 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3769 /* this order is important */
3770 a
= (u_int32_t
*)addr
;
3771 m
= (u_int32_t
*)mask
;
3772 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3773 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3775 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3777 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3779 b0
= gen_linktype(proto
);
3785 static struct block
*
3786 gen_ehostop(eaddr
, dir
)
3787 register const u_char
*eaddr
;
3790 register struct block
*b0
, *b1
;
3794 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3797 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3800 b0
= gen_ehostop(eaddr
, Q_SRC
);
3801 b1
= gen_ehostop(eaddr
, Q_DST
);
3807 b0
= gen_ehostop(eaddr
, Q_SRC
);
3808 b1
= gen_ehostop(eaddr
, Q_DST
);
3813 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3817 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3821 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3825 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3829 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3833 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3841 * Like gen_ehostop, but for DLT_FDDI
3843 static struct block
*
3844 gen_fhostop(eaddr
, dir
)
3845 register const u_char
*eaddr
;
3848 struct block
*b0
, *b1
;
3852 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3855 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3858 b0
= gen_fhostop(eaddr
, Q_SRC
);
3859 b1
= gen_fhostop(eaddr
, Q_DST
);
3865 b0
= gen_fhostop(eaddr
, Q_SRC
);
3866 b1
= gen_fhostop(eaddr
, Q_DST
);
3871 bpf_error("'addr1' is only supported on 802.11");
3875 bpf_error("'addr2' is only supported on 802.11");
3879 bpf_error("'addr3' is only supported on 802.11");
3883 bpf_error("'addr4' is only supported on 802.11");
3887 bpf_error("'ra' is only supported on 802.11");
3891 bpf_error("'ta' is only supported on 802.11");
3899 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3901 static struct block
*
3902 gen_thostop(eaddr
, dir
)
3903 register const u_char
*eaddr
;
3906 register struct block
*b0
, *b1
;
3910 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3913 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3916 b0
= gen_thostop(eaddr
, Q_SRC
);
3917 b1
= gen_thostop(eaddr
, Q_DST
);
3923 b0
= gen_thostop(eaddr
, Q_SRC
);
3924 b1
= gen_thostop(eaddr
, Q_DST
);
3929 bpf_error("'addr1' is only supported on 802.11");
3933 bpf_error("'addr2' is only supported on 802.11");
3937 bpf_error("'addr3' is only supported on 802.11");
3941 bpf_error("'addr4' is only supported on 802.11");
3945 bpf_error("'ra' is only supported on 802.11");
3949 bpf_error("'ta' is only supported on 802.11");
3957 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3958 * various 802.11 + radio headers.
3960 static struct block
*
3961 gen_wlanhostop(eaddr
, dir
)
3962 register const u_char
*eaddr
;
3965 register struct block
*b0
, *b1
, *b2
;
3966 register struct slist
*s
;
3968 #ifdef ENABLE_WLAN_FILTERING_PATCH
3971 * We need to disable the optimizer because the optimizer is buggy
3972 * and wipes out some LD instructions generated by the below
3973 * code to validate the Frame Control bits
3976 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3983 * For control frames, there is no SA.
3985 * For management frames, SA is at an
3986 * offset of 10 from the beginning of
3989 * For data frames, SA is at an offset
3990 * of 10 from the beginning of the packet
3991 * if From DS is clear, at an offset of
3992 * 16 from the beginning of the packet
3993 * if From DS is set and To DS is clear,
3994 * and an offset of 24 from the beginning
3995 * of the packet if From DS is set and To DS
4000 * Generate the tests to be done for data frames
4003 * First, check for To DS set, i.e. check "link[1] & 0x01".
4005 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4006 b1
= new_block(JMP(BPF_JSET
));
4007 b1
->s
.k
= 0x01; /* To DS */
4011 * If To DS is set, the SA is at 24.
4013 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4017 * Now, check for To DS not set, i.e. check
4018 * "!(link[1] & 0x01)".
4020 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4021 b2
= new_block(JMP(BPF_JSET
));
4022 b2
->s
.k
= 0x01; /* To DS */
4027 * If To DS is not set, the SA is at 16.
4029 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4033 * Now OR together the last two checks. That gives
4034 * the complete set of checks for data frames with
4040 * Now check for From DS being set, and AND that with
4041 * the ORed-together checks.
4043 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4044 b1
= new_block(JMP(BPF_JSET
));
4045 b1
->s
.k
= 0x02; /* From DS */
4050 * Now check for data frames with From DS not set.
4052 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4053 b2
= new_block(JMP(BPF_JSET
));
4054 b2
->s
.k
= 0x02; /* From DS */
4059 * If From DS isn't set, the SA is at 10.
4061 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4065 * Now OR together the checks for data frames with
4066 * From DS not set and for data frames with From DS
4067 * set; that gives the checks done for data frames.
4072 * Now check for a data frame.
4073 * I.e, check "link[0] & 0x08".
4075 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4076 b1
= new_block(JMP(BPF_JSET
));
4081 * AND that with the checks done for data frames.
4086 * If the high-order bit of the type value is 0, this
4087 * is a management frame.
4088 * I.e, check "!(link[0] & 0x08)".
4090 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4091 b2
= new_block(JMP(BPF_JSET
));
4097 * For management frames, the SA is at 10.
4099 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4103 * OR that with the checks done for data frames.
4104 * That gives the checks done for management and
4110 * If the low-order bit of the type value is 1,
4111 * this is either a control frame or a frame
4112 * with a reserved type, and thus not a
4115 * I.e., check "!(link[0] & 0x04)".
4117 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4118 b1
= new_block(JMP(BPF_JSET
));
4124 * AND that with the checks for data and management
4134 * For control frames, there is no DA.
4136 * For management frames, DA is at an
4137 * offset of 4 from the beginning of
4140 * For data frames, DA is at an offset
4141 * of 4 from the beginning of the packet
4142 * if To DS is clear and at an offset of
4143 * 16 from the beginning of the packet
4148 * Generate the tests to be done for data frames.
4150 * First, check for To DS set, i.e. "link[1] & 0x01".
4152 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4153 b1
= new_block(JMP(BPF_JSET
));
4154 b1
->s
.k
= 0x01; /* To DS */
4158 * If To DS is set, the DA is at 16.
4160 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4164 * Now, check for To DS not set, i.e. check
4165 * "!(link[1] & 0x01)".
4167 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4168 b2
= new_block(JMP(BPF_JSET
));
4169 b2
->s
.k
= 0x01; /* To DS */
4174 * If To DS is not set, the DA is at 4.
4176 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4180 * Now OR together the last two checks. That gives
4181 * the complete set of checks for data frames.
4186 * Now check for a data frame.
4187 * I.e, check "link[0] & 0x08".
4189 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4190 b1
= new_block(JMP(BPF_JSET
));
4195 * AND that with the checks done for data frames.
4200 * If the high-order bit of the type value is 0, this
4201 * is a management frame.
4202 * I.e, check "!(link[0] & 0x08)".
4204 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4205 b2
= new_block(JMP(BPF_JSET
));
4211 * For management frames, the DA is at 4.
4213 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4217 * OR that with the checks done for data frames.
4218 * That gives the checks done for management and
4224 * If the low-order bit of the type value is 1,
4225 * this is either a control frame or a frame
4226 * with a reserved type, and thus not a
4229 * I.e., check "!(link[0] & 0x04)".
4231 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4232 b1
= new_block(JMP(BPF_JSET
));
4238 * AND that with the checks for data and management
4246 * Not present in management frames; addr1 in other
4251 * If the high-order bit of the type value is 0, this
4252 * is a management frame.
4253 * I.e, check "(link[0] & 0x08)".
4255 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4256 b1
= new_block(JMP(BPF_JSET
));
4263 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4266 * AND that with the check of addr1.
4273 * Not present in management frames; addr2, if present,
4278 * Not present in CTS or ACK control frames.
4280 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4281 IEEE80211_FC0_TYPE_MASK
);
4283 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4284 IEEE80211_FC0_SUBTYPE_MASK
);
4286 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4287 IEEE80211_FC0_SUBTYPE_MASK
);
4293 * If the high-order bit of the type value is 0, this
4294 * is a management frame.
4295 * I.e, check "(link[0] & 0x08)".
4297 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4298 b1
= new_block(JMP(BPF_JSET
));
4303 * AND that with the check for frames other than
4304 * CTS and ACK frames.
4311 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4316 * XXX - add BSSID keyword?
4319 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4323 * Not present in CTS or ACK control frames.
4325 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4326 IEEE80211_FC0_TYPE_MASK
);
4328 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4329 IEEE80211_FC0_SUBTYPE_MASK
);
4331 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4332 IEEE80211_FC0_SUBTYPE_MASK
);
4336 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4342 * Not present in control frames.
4344 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4345 IEEE80211_FC0_TYPE_MASK
);
4347 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4353 * Present only if the direction mask has both "From DS"
4354 * and "To DS" set. Neither control frames nor management
4355 * frames should have both of those set, so we don't
4356 * check the frame type.
4358 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4359 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4360 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4365 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4366 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4372 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4373 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4382 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4383 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4384 * as the RFC states.)
4386 static struct block
*
4387 gen_ipfchostop(eaddr
, dir
)
4388 register const u_char
*eaddr
;
4391 register struct block
*b0
, *b1
;
4395 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4398 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4401 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4402 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4408 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4409 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4414 bpf_error("'addr1' is only supported on 802.11");
4418 bpf_error("'addr2' is only supported on 802.11");
4422 bpf_error("'addr3' is only supported on 802.11");
4426 bpf_error("'addr4' is only supported on 802.11");
4430 bpf_error("'ra' is only supported on 802.11");
4434 bpf_error("'ta' is only supported on 802.11");
4442 * This is quite tricky because there may be pad bytes in front of the
4443 * DECNET header, and then there are two possible data packet formats that
4444 * carry both src and dst addresses, plus 5 packet types in a format that
4445 * carries only the src node, plus 2 types that use a different format and
4446 * also carry just the src node.
4450 * Instead of doing those all right, we just look for data packets with
4451 * 0 or 1 bytes of padding. If you want to look at other packets, that
4452 * will require a lot more hacking.
4454 * To add support for filtering on DECNET "areas" (network numbers)
4455 * one would want to add a "mask" argument to this routine. That would
4456 * make the filter even more inefficient, although one could be clever
4457 * and not generate masking instructions if the mask is 0xFFFF.
4459 static struct block
*
4460 gen_dnhostop(addr
, dir
)
4464 struct block
*b0
, *b1
, *b2
, *tmp
;
4465 u_int offset_lh
; /* offset if long header is received */
4466 u_int offset_sh
; /* offset if short header is received */
4471 offset_sh
= 1; /* follows flags */
4472 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4476 offset_sh
= 3; /* follows flags, dstnode */
4477 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4481 /* Inefficient because we do our Calvinball dance twice */
4482 b0
= gen_dnhostop(addr
, Q_SRC
);
4483 b1
= gen_dnhostop(addr
, Q_DST
);
4489 /* Inefficient because we do our Calvinball dance twice */
4490 b0
= gen_dnhostop(addr
, Q_SRC
);
4491 b1
= gen_dnhostop(addr
, Q_DST
);
4496 bpf_error("ISO host filtering not implemented");
4501 b0
= gen_linktype(ETHERTYPE_DN
);
4502 /* Check for pad = 1, long header case */
4503 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4504 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4505 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4506 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4508 /* Check for pad = 0, long header case */
4509 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4510 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4513 /* Check for pad = 1, short header case */
4514 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4515 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4516 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4519 /* Check for pad = 0, short header case */
4520 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4521 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4525 /* Combine with test for linktype */
4531 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4532 * test the bottom-of-stack bit, and then check the version number
4533 * field in the IP header.
4535 static struct block
*
4536 gen_mpls_linktype(proto
)
4539 struct block
*b0
, *b1
;
4544 /* match the bottom-of-stack bit */
4545 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4546 /* match the IPv4 version number */
4547 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4552 /* match the bottom-of-stack bit */
4553 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4554 /* match the IPv4 version number */
4555 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4564 static struct block
*
4565 gen_host(addr
, mask
, proto
, dir
, type
)
4572 struct block
*b0
, *b1
;
4573 const char *typestr
;
4583 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4585 * Only check for non-IPv4 addresses if we're not
4586 * checking MPLS-encapsulated packets.
4588 if (label_stack_depth
== 0) {
4589 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4591 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4597 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4600 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4603 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4606 bpf_error("'tcp' modifier applied to %s", typestr
);
4609 bpf_error("'sctp' modifier applied to %s", typestr
);
4612 bpf_error("'udp' modifier applied to %s", typestr
);
4615 bpf_error("'icmp' modifier applied to %s", typestr
);
4618 bpf_error("'igmp' modifier applied to %s", typestr
);
4621 bpf_error("'igrp' modifier applied to %s", typestr
);
4624 bpf_error("'pim' modifier applied to %s", typestr
);
4627 bpf_error("'vrrp' modifier applied to %s", typestr
);
4630 bpf_error("'carp' modifier applied to %s", typestr
);
4633 bpf_error("ATALK host filtering not implemented");
4636 bpf_error("AARP host filtering not implemented");
4639 return gen_dnhostop(addr
, dir
);
4642 bpf_error("SCA host filtering not implemented");
4645 bpf_error("LAT host filtering not implemented");
4648 bpf_error("MOPDL host filtering not implemented");
4651 bpf_error("MOPRC host filtering not implemented");
4654 bpf_error("'ip6' modifier applied to ip host");
4657 bpf_error("'icmp6' modifier applied to %s", typestr
);
4660 bpf_error("'ah' modifier applied to %s", typestr
);
4663 bpf_error("'esp' modifier applied to %s", typestr
);
4666 bpf_error("ISO host filtering not implemented");
4669 bpf_error("'esis' modifier applied to %s", typestr
);
4672 bpf_error("'isis' modifier applied to %s", typestr
);
4675 bpf_error("'clnp' modifier applied to %s", typestr
);
4678 bpf_error("'stp' modifier applied to %s", typestr
);
4681 bpf_error("IPX host filtering not implemented");
4684 bpf_error("'netbeui' modifier applied to %s", typestr
);
4687 bpf_error("'radio' modifier applied to %s", typestr
);
4696 static struct block
*
4697 gen_host6(addr
, mask
, proto
, dir
, type
)
4698 struct in6_addr
*addr
;
4699 struct in6_addr
*mask
;
4704 const char *typestr
;
4714 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4717 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4720 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4723 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4726 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4729 bpf_error("'sctp' modifier applied to %s", typestr
);
4732 bpf_error("'tcp' modifier applied to %s", typestr
);
4735 bpf_error("'udp' modifier applied to %s", typestr
);
4738 bpf_error("'icmp' modifier applied to %s", typestr
);
4741 bpf_error("'igmp' modifier applied to %s", typestr
);
4744 bpf_error("'igrp' modifier applied to %s", typestr
);
4747 bpf_error("'pim' modifier applied to %s", typestr
);
4750 bpf_error("'vrrp' modifier applied to %s", typestr
);
4753 bpf_error("'carp' modifier applied to %s", typestr
);
4756 bpf_error("ATALK host filtering not implemented");
4759 bpf_error("AARP host filtering not implemented");
4762 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4765 bpf_error("SCA host filtering not implemented");
4768 bpf_error("LAT host filtering not implemented");
4771 bpf_error("MOPDL host filtering not implemented");
4774 bpf_error("MOPRC host filtering not implemented");
4777 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4780 bpf_error("'icmp6' modifier applied to %s", typestr
);
4783 bpf_error("'ah' modifier applied to %s", typestr
);
4786 bpf_error("'esp' modifier applied to %s", typestr
);
4789 bpf_error("ISO host filtering not implemented");
4792 bpf_error("'esis' modifier applied to %s", typestr
);
4795 bpf_error("'isis' modifier applied to %s", typestr
);
4798 bpf_error("'clnp' modifier applied to %s", typestr
);
4801 bpf_error("'stp' modifier applied to %s", typestr
);
4804 bpf_error("IPX host filtering not implemented");
4807 bpf_error("'netbeui' modifier applied to %s", typestr
);
4810 bpf_error("'radio' modifier applied to %s", typestr
);
4820 static struct block
*
4821 gen_gateway(eaddr
, alist
, proto
, dir
)
4822 const u_char
*eaddr
;
4823 bpf_u_int32
**alist
;
4827 struct block
*b0
, *b1
, *tmp
;
4830 bpf_error("direction applied to 'gateway'");
4839 case DLT_NETANALYZER
:
4840 case DLT_NETANALYZER_TRANSPARENT
:
4841 b1
= gen_prevlinkhdr_check();
4842 b0
= gen_ehostop(eaddr
, Q_OR
);
4847 b0
= gen_fhostop(eaddr
, Q_OR
);
4850 b0
= gen_thostop(eaddr
, Q_OR
);
4852 case DLT_IEEE802_11
:
4853 case DLT_PRISM_HEADER
:
4854 case DLT_IEEE802_11_RADIO_AVS
:
4855 case DLT_IEEE802_11_RADIO
:
4857 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4861 * This is LLC-multiplexed traffic; if it were
4862 * LANE, linktype would have been set to
4866 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4868 case DLT_IP_OVER_FC
:
4869 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4873 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4875 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4877 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4886 bpf_error("illegal modifier of 'gateway'");
4892 gen_proto_abbrev(proto
)
4901 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4902 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4907 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4908 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4913 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4914 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4919 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4922 #ifndef IPPROTO_IGMP
4923 #define IPPROTO_IGMP 2
4927 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4930 #ifndef IPPROTO_IGRP
4931 #define IPPROTO_IGRP 9
4934 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4938 #define IPPROTO_PIM 103
4942 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4943 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4947 #ifndef IPPROTO_VRRP
4948 #define IPPROTO_VRRP 112
4952 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4955 #ifndef IPPROTO_CARP
4956 #define IPPROTO_CARP 112
4960 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4964 b1
= gen_linktype(ETHERTYPE_IP
);
4968 b1
= gen_linktype(ETHERTYPE_ARP
);
4972 b1
= gen_linktype(ETHERTYPE_REVARP
);
4976 bpf_error("link layer applied in wrong context");
4979 b1
= gen_linktype(ETHERTYPE_ATALK
);
4983 b1
= gen_linktype(ETHERTYPE_AARP
);
4987 b1
= gen_linktype(ETHERTYPE_DN
);
4991 b1
= gen_linktype(ETHERTYPE_SCA
);
4995 b1
= gen_linktype(ETHERTYPE_LAT
);
4999 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5003 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5007 b1
= gen_linktype(ETHERTYPE_IPV6
);
5010 #ifndef IPPROTO_ICMPV6
5011 #define IPPROTO_ICMPV6 58
5014 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5018 #define IPPROTO_AH 51
5021 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5022 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5027 #define IPPROTO_ESP 50
5030 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5031 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5036 b1
= gen_linktype(LLCSAP_ISONS
);
5040 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5044 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5047 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5048 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5049 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5051 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5053 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5055 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5059 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5060 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5061 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5063 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5065 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5067 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5071 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5072 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5073 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5075 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5080 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5081 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5086 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5087 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5089 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5091 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5096 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5097 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5102 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5103 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5108 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5112 b1
= gen_linktype(LLCSAP_8021D
);
5116 b1
= gen_linktype(LLCSAP_IPX
);
5120 b1
= gen_linktype(LLCSAP_NETBEUI
);
5124 bpf_error("'radio' is not a valid protocol type");
5132 static struct block
*
5138 /* not IPv4 frag other than the first frag */
5139 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5140 b
= new_block(JMP(BPF_JSET
));
5149 * Generate a comparison to a port value in the transport-layer header
5150 * at the specified offset from the beginning of that header.
5152 * XXX - this handles a variable-length prefix preceding the link-layer
5153 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5154 * variable-length link-layer headers (such as Token Ring or 802.11
5157 static struct block
*
5158 gen_portatom(off
, v
)
5162 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5165 static struct block
*
5166 gen_portatom6(off
, v
)
5170 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5174 gen_portop(port
, proto
, dir
)
5175 int port
, proto
, dir
;
5177 struct block
*b0
, *b1
, *tmp
;
5179 /* ip proto 'proto' and not a fragment other than the first fragment */
5180 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5186 b1
= gen_portatom(0, (bpf_int32
)port
);
5190 b1
= gen_portatom(2, (bpf_int32
)port
);
5195 tmp
= gen_portatom(0, (bpf_int32
)port
);
5196 b1
= gen_portatom(2, (bpf_int32
)port
);
5201 tmp
= gen_portatom(0, (bpf_int32
)port
);
5202 b1
= gen_portatom(2, (bpf_int32
)port
);
5214 static struct block
*
5215 gen_port(port
, ip_proto
, dir
)
5220 struct block
*b0
, *b1
, *tmp
;
5225 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5226 * not LLC encapsulation with LLCSAP_IP.
5228 * For IEEE 802 networks - which includes 802.5 token ring
5229 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5230 * says that SNAP encapsulation is used, not LLC encapsulation
5233 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5234 * RFC 2225 say that SNAP encapsulation is used, not LLC
5235 * encapsulation with LLCSAP_IP.
5237 * So we always check for ETHERTYPE_IP.
5239 b0
= gen_linktype(ETHERTYPE_IP
);
5245 b1
= gen_portop(port
, ip_proto
, dir
);
5249 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5250 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5252 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5264 gen_portop6(port
, proto
, dir
)
5265 int port
, proto
, dir
;
5267 struct block
*b0
, *b1
, *tmp
;
5269 /* ip6 proto 'proto' */
5270 /* XXX - catch the first fragment of a fragmented packet? */
5271 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5275 b1
= gen_portatom6(0, (bpf_int32
)port
);
5279 b1
= gen_portatom6(2, (bpf_int32
)port
);
5284 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5285 b1
= gen_portatom6(2, (bpf_int32
)port
);
5290 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5291 b1
= gen_portatom6(2, (bpf_int32
)port
);
5303 static struct block
*
5304 gen_port6(port
, ip_proto
, dir
)
5309 struct block
*b0
, *b1
, *tmp
;
5311 /* link proto ip6 */
5312 b0
= gen_linktype(ETHERTYPE_IPV6
);
5318 b1
= gen_portop6(port
, ip_proto
, dir
);
5322 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5323 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5325 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5336 /* gen_portrange code */
5337 static struct block
*
5338 gen_portrangeatom(off
, v1
, v2
)
5342 struct block
*b1
, *b2
;
5346 * Reverse the order of the ports, so v1 is the lower one.
5355 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5356 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5364 gen_portrangeop(port1
, port2
, proto
, dir
)
5369 struct block
*b0
, *b1
, *tmp
;
5371 /* ip proto 'proto' and not a fragment other than the first fragment */
5372 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5378 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5382 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5387 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5388 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5393 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5394 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5406 static struct block
*
5407 gen_portrange(port1
, port2
, ip_proto
, dir
)
5412 struct block
*b0
, *b1
, *tmp
;
5415 b0
= gen_linktype(ETHERTYPE_IP
);
5421 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5425 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5426 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5428 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5439 static struct block
*
5440 gen_portrangeatom6(off
, v1
, v2
)
5444 struct block
*b1
, *b2
;
5448 * Reverse the order of the ports, so v1 is the lower one.
5457 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5458 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5466 gen_portrangeop6(port1
, port2
, proto
, dir
)
5471 struct block
*b0
, *b1
, *tmp
;
5473 /* ip6 proto 'proto' */
5474 /* XXX - catch the first fragment of a fragmented packet? */
5475 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5479 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5483 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5488 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5489 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5494 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5495 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5507 static struct block
*
5508 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5513 struct block
*b0
, *b1
, *tmp
;
5515 /* link proto ip6 */
5516 b0
= gen_linktype(ETHERTYPE_IPV6
);
5522 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5526 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5527 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5529 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5541 lookup_proto(name
, proto
)
5542 register const char *name
;
5552 v
= pcap_nametoproto(name
);
5553 if (v
== PROTO_UNDEF
)
5554 bpf_error("unknown ip proto '%s'", name
);
5558 /* XXX should look up h/w protocol type based on linktype */
5559 v
= pcap_nametoeproto(name
);
5560 if (v
== PROTO_UNDEF
) {
5561 v
= pcap_nametollc(name
);
5562 if (v
== PROTO_UNDEF
)
5563 bpf_error("unknown ether proto '%s'", name
);
5568 if (strcmp(name
, "esis") == 0)
5570 else if (strcmp(name
, "isis") == 0)
5572 else if (strcmp(name
, "clnp") == 0)
5575 bpf_error("unknown osi proto '%s'", name
);
5595 static struct block
*
5596 gen_protochain(v
, proto
, dir
)
5601 #ifdef NO_PROTOCHAIN
5602 return gen_proto(v
, proto
, dir
);
5604 struct block
*b0
, *b
;
5605 struct slist
*s
[100];
5606 int fix2
, fix3
, fix4
, fix5
;
5607 int ahcheck
, again
, end
;
5609 int reg2
= alloc_reg();
5611 memset(s
, 0, sizeof(s
));
5612 fix3
= fix4
= fix5
= 0;
5619 b0
= gen_protochain(v
, Q_IP
, dir
);
5620 b
= gen_protochain(v
, Q_IPV6
, dir
);
5624 bpf_error("bad protocol applied for 'protochain'");
5629 * We don't handle variable-length prefixes before the link-layer
5630 * header, or variable-length link-layer headers, here yet.
5631 * We might want to add BPF instructions to do the protochain
5632 * work, to simplify that and, on platforms that have a BPF
5633 * interpreter with the new instructions, let the filtering
5634 * be done in the kernel. (We already require a modified BPF
5635 * engine to do the protochain stuff, to support backward
5636 * branches, and backward branch support is unlikely to appear
5637 * in kernel BPF engines.)
5639 if (off_linkpl
.is_variable
)
5640 bpf_error("'protochain' not supported with variable length headers");
5642 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5645 * s[0] is a dummy entry to protect other BPF insn from damage
5646 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5647 * hard to find interdependency made by jump table fixup.
5650 s
[i
] = new_stmt(0); /*dummy*/
5655 b0
= gen_linktype(ETHERTYPE_IP
);
5658 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5659 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5661 /* X = ip->ip_hl << 2 */
5662 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5663 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5668 b0
= gen_linktype(ETHERTYPE_IPV6
);
5670 /* A = ip6->ip_nxt */
5671 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5672 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5674 /* X = sizeof(struct ip6_hdr) */
5675 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5681 bpf_error("unsupported proto to gen_protochain");
5685 /* again: if (A == v) goto end; else fall through; */
5687 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5689 s
[i
]->s
.jt
= NULL
; /*later*/
5690 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5694 #ifndef IPPROTO_NONE
5695 #define IPPROTO_NONE 59
5697 /* if (A == IPPROTO_NONE) goto end */
5698 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5699 s
[i
]->s
.jt
= NULL
; /*later*/
5700 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5701 s
[i
]->s
.k
= IPPROTO_NONE
;
5702 s
[fix5
]->s
.jf
= s
[i
];
5706 if (proto
== Q_IPV6
) {
5707 int v6start
, v6end
, v6advance
, j
;
5710 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5711 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5712 s
[i
]->s
.jt
= NULL
; /*later*/
5713 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5714 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5715 s
[fix2
]->s
.jf
= s
[i
];
5717 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5718 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5719 s
[i
]->s
.jt
= NULL
; /*later*/
5720 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5721 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5723 /* if (A == IPPROTO_ROUTING) goto v6advance */
5724 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5725 s
[i
]->s
.jt
= NULL
; /*later*/
5726 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5727 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5729 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5730 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5731 s
[i
]->s
.jt
= NULL
; /*later*/
5732 s
[i
]->s
.jf
= NULL
; /*later*/
5733 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5743 * A = P[X + packet head];
5744 * X = X + (P[X + packet head + 1] + 1) * 8;
5746 /* A = P[X + packet head] */
5747 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5748 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5751 s
[i
] = new_stmt(BPF_ST
);
5754 /* A = P[X + packet head + 1]; */
5755 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5756 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5759 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5763 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5767 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5771 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5774 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5778 /* goto again; (must use BPF_JA for backward jump) */
5779 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5780 s
[i
]->s
.k
= again
- i
- 1;
5781 s
[i
- 1]->s
.jf
= s
[i
];
5785 for (j
= v6start
; j
<= v6end
; j
++)
5786 s
[j
]->s
.jt
= s
[v6advance
];
5789 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5791 s
[fix2
]->s
.jf
= s
[i
];
5797 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5798 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5799 s
[i
]->s
.jt
= NULL
; /*later*/
5800 s
[i
]->s
.jf
= NULL
; /*later*/
5801 s
[i
]->s
.k
= IPPROTO_AH
;
5803 s
[fix3
]->s
.jf
= s
[ahcheck
];
5810 * X = X + (P[X + 1] + 2) * 4;
5813 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5815 /* A = P[X + packet head]; */
5816 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5817 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5820 s
[i
] = new_stmt(BPF_ST
);
5824 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5827 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5831 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5833 /* A = P[X + packet head] */
5834 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5835 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5838 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5842 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5846 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5849 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5853 /* goto again; (must use BPF_JA for backward jump) */
5854 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5855 s
[i
]->s
.k
= again
- i
- 1;
5860 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5862 s
[fix2
]->s
.jt
= s
[end
];
5863 s
[fix4
]->s
.jf
= s
[end
];
5864 s
[fix5
]->s
.jt
= s
[end
];
5871 for (i
= 0; i
< max
- 1; i
++)
5872 s
[i
]->next
= s
[i
+ 1];
5873 s
[max
- 1]->next
= NULL
;
5878 b
= new_block(JMP(BPF_JEQ
));
5879 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5889 static struct block
*
5890 gen_check_802_11_data_frame()
5893 struct block
*b0
, *b1
;
5896 * A data frame has the 0x08 bit (b3) in the frame control field set
5897 * and the 0x04 bit (b2) clear.
5899 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5900 b0
= new_block(JMP(BPF_JSET
));
5904 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5905 b1
= new_block(JMP(BPF_JSET
));
5916 * Generate code that checks whether the packet is a packet for protocol
5917 * <proto> and whether the type field in that protocol's header has
5918 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5919 * IP packet and checks the protocol number in the IP header against <v>.
5921 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5922 * against Q_IP and Q_IPV6.
5924 static struct block
*
5925 gen_proto(v
, proto
, dir
)
5930 struct block
*b0
, *b1
;
5935 if (dir
!= Q_DEFAULT
)
5936 bpf_error("direction applied to 'proto'");
5940 b0
= gen_proto(v
, Q_IP
, dir
);
5941 b1
= gen_proto(v
, Q_IPV6
, dir
);
5947 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5948 * not LLC encapsulation with LLCSAP_IP.
5950 * For IEEE 802 networks - which includes 802.5 token ring
5951 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5952 * says that SNAP encapsulation is used, not LLC encapsulation
5955 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5956 * RFC 2225 say that SNAP encapsulation is used, not LLC
5957 * encapsulation with LLCSAP_IP.
5959 * So we always check for ETHERTYPE_IP.
5961 b0
= gen_linktype(ETHERTYPE_IP
);
5963 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5965 b1
= gen_protochain(v
, Q_IP
);
5975 * Frame Relay packets typically have an OSI
5976 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5977 * generates code to check for all the OSI
5978 * NLPIDs, so calling it and then adding a check
5979 * for the particular NLPID for which we're
5980 * looking is bogus, as we can just check for
5983 * What we check for is the NLPID and a frame
5984 * control field value of UI, i.e. 0x03 followed
5987 * XXX - assumes a 2-byte Frame Relay header with
5988 * DLCI and flags. What if the address is longer?
5990 * XXX - what about SNAP-encapsulated frames?
5992 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5998 * Cisco uses an Ethertype lookalike - for OSI,
6001 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6002 /* OSI in C-HDLC is stuffed with a fudge byte */
6003 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6008 b0
= gen_linktype(LLCSAP_ISONS
);
6009 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6015 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6017 * 4 is the offset of the PDU type relative to the IS-IS
6020 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6025 bpf_error("arp does not encapsulate another protocol");
6029 bpf_error("rarp does not encapsulate another protocol");
6033 bpf_error("atalk encapsulation is not specifiable");
6037 bpf_error("decnet encapsulation is not specifiable");
6041 bpf_error("sca does not encapsulate another protocol");
6045 bpf_error("lat does not encapsulate another protocol");
6049 bpf_error("moprc does not encapsulate another protocol");
6053 bpf_error("mopdl does not encapsulate another protocol");
6057 return gen_linktype(v
);
6060 bpf_error("'udp proto' is bogus");
6064 bpf_error("'tcp proto' is bogus");
6068 bpf_error("'sctp proto' is bogus");
6072 bpf_error("'icmp proto' is bogus");
6076 bpf_error("'igmp proto' is bogus");
6080 bpf_error("'igrp proto' is bogus");
6084 bpf_error("'pim proto' is bogus");
6088 bpf_error("'vrrp proto' is bogus");
6092 bpf_error("'carp proto' is bogus");
6096 b0
= gen_linktype(ETHERTYPE_IPV6
);
6099 * Also check for a fragment header before the final
6102 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6103 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6105 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6108 b1
= gen_protochain(v
, Q_IPV6
);
6114 bpf_error("'icmp6 proto' is bogus");
6117 bpf_error("'ah proto' is bogus");
6120 bpf_error("'ah proto' is bogus");
6123 bpf_error("'stp proto' is bogus");
6126 bpf_error("'ipx proto' is bogus");
6129 bpf_error("'netbeui proto' is bogus");
6132 bpf_error("'radio proto' is bogus");
6143 register const char *name
;
6146 int proto
= q
.proto
;
6150 bpf_u_int32 mask
, addr
;
6152 bpf_u_int32
**alist
;
6155 struct sockaddr_in
*sin4
;
6156 struct sockaddr_in6
*sin6
;
6157 struct addrinfo
*res
, *res0
;
6158 struct in6_addr mask128
;
6160 struct block
*b
, *tmp
;
6161 int port
, real_proto
;
6167 addr
= pcap_nametonetaddr(name
);
6169 bpf_error("unknown network '%s'", name
);
6170 /* Left justify network addr and calculate its network mask */
6172 while (addr
&& (addr
& 0xff000000) == 0) {
6176 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6180 if (proto
== Q_LINK
) {
6184 case DLT_NETANALYZER
:
6185 case DLT_NETANALYZER_TRANSPARENT
:
6186 eaddr
= pcap_ether_hostton(name
);
6189 "unknown ether host '%s'", name
);
6190 tmp
= gen_prevlinkhdr_check();
6191 b
= gen_ehostop(eaddr
, dir
);
6198 eaddr
= pcap_ether_hostton(name
);
6201 "unknown FDDI host '%s'", name
);
6202 b
= gen_fhostop(eaddr
, dir
);
6207 eaddr
= pcap_ether_hostton(name
);
6210 "unknown token ring host '%s'", name
);
6211 b
= gen_thostop(eaddr
, dir
);
6215 case DLT_IEEE802_11
:
6216 case DLT_PRISM_HEADER
:
6217 case DLT_IEEE802_11_RADIO_AVS
:
6218 case DLT_IEEE802_11_RADIO
:
6220 eaddr
= pcap_ether_hostton(name
);
6223 "unknown 802.11 host '%s'", name
);
6224 b
= gen_wlanhostop(eaddr
, dir
);
6228 case DLT_IP_OVER_FC
:
6229 eaddr
= pcap_ether_hostton(name
);
6232 "unknown Fibre Channel host '%s'", name
);
6233 b
= gen_ipfchostop(eaddr
, dir
);
6238 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6239 } else if (proto
== Q_DECNET
) {
6240 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6242 * I don't think DECNET hosts can be multihomed, so
6243 * there is no need to build up a list of addresses
6245 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6248 alist
= pcap_nametoaddr(name
);
6249 if (alist
== NULL
|| *alist
== NULL
)
6250 bpf_error("unknown host '%s'", name
);
6252 if (off_linktype
.constant_part
== (u_int
)-1 &&
6253 tproto
== Q_DEFAULT
)
6255 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6257 tmp
= gen_host(**alist
++, 0xffffffff,
6258 tproto
, dir
, q
.addr
);
6264 memset(&mask128
, 0xff, sizeof(mask128
));
6265 res0
= res
= pcap_nametoaddrinfo(name
);
6267 bpf_error("unknown host '%s'", name
);
6270 tproto
= tproto6
= proto
;
6271 if (off_linktype
.constant_part
== -1 &&
6272 tproto
== Q_DEFAULT
) {
6276 for (res
= res0
; res
; res
= res
->ai_next
) {
6277 switch (res
->ai_family
) {
6279 if (tproto
== Q_IPV6
)
6282 sin4
= (struct sockaddr_in
*)
6284 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6285 0xffffffff, tproto
, dir
, q
.addr
);
6288 if (tproto6
== Q_IP
)
6291 sin6
= (struct sockaddr_in6
*)
6293 tmp
= gen_host6(&sin6
->sin6_addr
,
6294 &mask128
, tproto6
, dir
, q
.addr
);
6306 bpf_error("unknown host '%s'%s", name
,
6307 (proto
== Q_DEFAULT
)
6309 : " for specified address family");
6316 if (proto
!= Q_DEFAULT
&&
6317 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6318 bpf_error("illegal qualifier of 'port'");
6319 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6320 bpf_error("unknown port '%s'", name
);
6321 if (proto
== Q_UDP
) {
6322 if (real_proto
== IPPROTO_TCP
)
6323 bpf_error("port '%s' is tcp", name
);
6324 else if (real_proto
== IPPROTO_SCTP
)
6325 bpf_error("port '%s' is sctp", name
);
6327 /* override PROTO_UNDEF */
6328 real_proto
= IPPROTO_UDP
;
6330 if (proto
== Q_TCP
) {
6331 if (real_proto
== IPPROTO_UDP
)
6332 bpf_error("port '%s' is udp", name
);
6334 else if (real_proto
== IPPROTO_SCTP
)
6335 bpf_error("port '%s' is sctp", name
);
6337 /* override PROTO_UNDEF */
6338 real_proto
= IPPROTO_TCP
;
6340 if (proto
== Q_SCTP
) {
6341 if (real_proto
== IPPROTO_UDP
)
6342 bpf_error("port '%s' is udp", name
);
6344 else if (real_proto
== IPPROTO_TCP
)
6345 bpf_error("port '%s' is tcp", name
);
6347 /* override PROTO_UNDEF */
6348 real_proto
= IPPROTO_SCTP
;
6351 bpf_error("illegal port number %d < 0", port
);
6353 bpf_error("illegal port number %d > 65535", port
);
6354 b
= gen_port(port
, real_proto
, dir
);
6355 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6359 if (proto
!= Q_DEFAULT
&&
6360 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6361 bpf_error("illegal qualifier of 'portrange'");
6362 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6363 bpf_error("unknown port in range '%s'", name
);
6364 if (proto
== Q_UDP
) {
6365 if (real_proto
== IPPROTO_TCP
)
6366 bpf_error("port in range '%s' is tcp", name
);
6367 else if (real_proto
== IPPROTO_SCTP
)
6368 bpf_error("port in range '%s' is sctp", name
);
6370 /* override PROTO_UNDEF */
6371 real_proto
= IPPROTO_UDP
;
6373 if (proto
== Q_TCP
) {
6374 if (real_proto
== IPPROTO_UDP
)
6375 bpf_error("port in range '%s' is udp", name
);
6376 else if (real_proto
== IPPROTO_SCTP
)
6377 bpf_error("port in range '%s' is sctp", name
);
6379 /* override PROTO_UNDEF */
6380 real_proto
= IPPROTO_TCP
;
6382 if (proto
== Q_SCTP
) {
6383 if (real_proto
== IPPROTO_UDP
)
6384 bpf_error("port in range '%s' is udp", name
);
6385 else if (real_proto
== IPPROTO_TCP
)
6386 bpf_error("port in range '%s' is tcp", name
);
6388 /* override PROTO_UNDEF */
6389 real_proto
= IPPROTO_SCTP
;
6392 bpf_error("illegal port number %d < 0", port1
);
6394 bpf_error("illegal port number %d > 65535", port1
);
6396 bpf_error("illegal port number %d < 0", port2
);
6398 bpf_error("illegal port number %d > 65535", port2
);
6400 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6401 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6406 eaddr
= pcap_ether_hostton(name
);
6408 bpf_error("unknown ether host: %s", name
);
6410 alist
= pcap_nametoaddr(name
);
6411 if (alist
== NULL
|| *alist
== NULL
)
6412 bpf_error("unknown host '%s'", name
);
6413 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6417 bpf_error("'gateway' not supported in this configuration");
6421 real_proto
= lookup_proto(name
, proto
);
6422 if (real_proto
>= 0)
6423 return gen_proto(real_proto
, proto
, dir
);
6425 bpf_error("unknown protocol: %s", name
);
6428 real_proto
= lookup_proto(name
, proto
);
6429 if (real_proto
>= 0)
6430 return gen_protochain(real_proto
, proto
, dir
);
6432 bpf_error("unknown protocol: %s", name
);
6443 gen_mcode(s1
, s2
, masklen
, q
)
6444 register const char *s1
, *s2
;
6445 register unsigned int masklen
;
6448 register int nlen
, mlen
;
6451 nlen
= __pcap_atoin(s1
, &n
);
6452 /* Promote short ipaddr */
6456 mlen
= __pcap_atoin(s2
, &m
);
6457 /* Promote short ipaddr */
6460 bpf_error("non-network bits set in \"%s mask %s\"",
6463 /* Convert mask len to mask */
6465 bpf_error("mask length must be <= 32");
6468 * X << 32 is not guaranteed by C to be 0; it's
6473 m
= 0xffffffff << (32 - masklen
);
6475 bpf_error("non-network bits set in \"%s/%d\"",
6482 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6485 bpf_error("Mask syntax for networks only");
6494 register const char *s
;
6499 int proto
= q
.proto
;
6505 else if (q
.proto
== Q_DECNET
)
6506 vlen
= __pcap_atodn(s
, &v
);
6508 vlen
= __pcap_atoin(s
, &v
);
6515 if (proto
== Q_DECNET
)
6516 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6517 else if (proto
== Q_LINK
) {
6518 bpf_error("illegal link layer address");
6521 if (s
== NULL
&& q
.addr
== Q_NET
) {
6522 /* Promote short net number */
6523 while (v
&& (v
& 0xff000000) == 0) {
6528 /* Promote short ipaddr */
6532 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6537 proto
= IPPROTO_UDP
;
6538 else if (proto
== Q_TCP
)
6539 proto
= IPPROTO_TCP
;
6540 else if (proto
== Q_SCTP
)
6541 proto
= IPPROTO_SCTP
;
6542 else if (proto
== Q_DEFAULT
)
6543 proto
= PROTO_UNDEF
;
6545 bpf_error("illegal qualifier of 'port'");
6548 bpf_error("illegal port number %u > 65535", v
);
6552 b
= gen_port((int)v
, proto
, dir
);
6553 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6559 proto
= IPPROTO_UDP
;
6560 else if (proto
== Q_TCP
)
6561 proto
= IPPROTO_TCP
;
6562 else if (proto
== Q_SCTP
)
6563 proto
= IPPROTO_SCTP
;
6564 else if (proto
== Q_DEFAULT
)
6565 proto
= PROTO_UNDEF
;
6567 bpf_error("illegal qualifier of 'portrange'");
6570 bpf_error("illegal port number %u > 65535", v
);
6574 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6575 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6580 bpf_error("'gateway' requires a name");
6584 return gen_proto((int)v
, proto
, dir
);
6587 return gen_protochain((int)v
, proto
, dir
);
6602 gen_mcode6(s1
, s2
, masklen
, q
)
6603 register const char *s1
, *s2
;
6604 register unsigned int masklen
;
6607 struct addrinfo
*res
;
6608 struct in6_addr
*addr
;
6609 struct in6_addr mask
;
6614 bpf_error("no mask %s supported", s2
);
6616 res
= pcap_nametoaddrinfo(s1
);
6618 bpf_error("invalid ip6 address %s", s1
);
6621 bpf_error("%s resolved to multiple address", s1
);
6622 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6624 if (sizeof(mask
) * 8 < masklen
)
6625 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6626 memset(&mask
, 0, sizeof(mask
));
6627 memset(&mask
, 0xff, masklen
/ 8);
6629 mask
.s6_addr
[masklen
/ 8] =
6630 (0xff << (8 - masklen
% 8)) & 0xff;
6633 a
= (u_int32_t
*)addr
;
6634 m
= (u_int32_t
*)&mask
;
6635 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6636 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6637 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6645 bpf_error("Mask syntax for networks only");
6649 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6655 bpf_error("invalid qualifier against IPv6 address");
6664 register const u_char
*eaddr
;
6667 struct block
*b
, *tmp
;
6669 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6672 case DLT_NETANALYZER
:
6673 case DLT_NETANALYZER_TRANSPARENT
:
6674 tmp
= gen_prevlinkhdr_check();
6675 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6680 return gen_fhostop(eaddr
, (int)q
.dir
);
6682 return gen_thostop(eaddr
, (int)q
.dir
);
6683 case DLT_IEEE802_11
:
6684 case DLT_PRISM_HEADER
:
6685 case DLT_IEEE802_11_RADIO_AVS
:
6686 case DLT_IEEE802_11_RADIO
:
6688 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6689 case DLT_IP_OVER_FC
:
6690 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6692 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6696 bpf_error("ethernet address used in non-ether expression");
6703 struct slist
*s0
, *s1
;
6706 * This is definitely not the best way to do this, but the
6707 * lists will rarely get long.
6714 static struct slist
*
6720 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6725 static struct slist
*
6731 s
= new_stmt(BPF_LD
|BPF_MEM
);
6737 * Modify "index" to use the value stored into its register as an
6738 * offset relative to the beginning of the header for the protocol
6739 * "proto", and allocate a register and put an item "size" bytes long
6740 * (1, 2, or 4) at that offset into that register, making it the register
6744 gen_load(proto
, inst
, size
)
6749 struct slist
*s
, *tmp
;
6751 int regno
= alloc_reg();
6753 free_reg(inst
->regno
);
6757 bpf_error("data size must be 1, 2, or 4");
6773 bpf_error("unsupported index operation");
6777 * The offset is relative to the beginning of the packet
6778 * data, if we have a radio header. (If we don't, this
6781 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6782 linktype
!= DLT_IEEE802_11_RADIO
&&
6783 linktype
!= DLT_PRISM_HEADER
)
6784 bpf_error("radio information not present in capture");
6787 * Load into the X register the offset computed into the
6788 * register specified by "index".
6790 s
= xfer_to_x(inst
);
6793 * Load the item at that offset.
6795 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6797 sappend(inst
->s
, s
);
6802 * The offset is relative to the beginning of
6803 * the link-layer header.
6805 * XXX - what about ATM LANE? Should the index be
6806 * relative to the beginning of the AAL5 frame, so
6807 * that 0 refers to the beginning of the LE Control
6808 * field, or relative to the beginning of the LAN
6809 * frame, so that 0 refers, for Ethernet LANE, to
6810 * the beginning of the destination address?
6812 s
= gen_abs_offset_varpart(&off_linkhdr
);
6815 * If "s" is non-null, it has code to arrange that the
6816 * X register contains the length of the prefix preceding
6817 * the link-layer header. Add to it the offset computed
6818 * into the register specified by "index", and move that
6819 * into the X register. Otherwise, just load into the X
6820 * register the offset computed into the register specified
6824 sappend(s
, xfer_to_a(inst
));
6825 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6826 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6828 s
= xfer_to_x(inst
);
6831 * Load the item at the sum of the offset we've put in the
6832 * X register and the offset of the start of the link
6833 * layer header (which is 0 if the radio header is
6834 * variable-length; that header length is what we put
6835 * into the X register and then added to the index).
6837 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6838 tmp
->s
.k
= off_linkhdr
.constant_part
;
6840 sappend(inst
->s
, s
);
6854 * The offset is relative to the beginning of
6855 * the network-layer header.
6856 * XXX - are there any cases where we want
6859 s
= gen_abs_offset_varpart(&off_linkpl
);
6862 * If "s" is non-null, it has code to arrange that the
6863 * X register contains the variable part of the offset
6864 * of the link-layer payload. Add to it the offset
6865 * computed into the register specified by "index",
6866 * and move that into the X register. Otherwise, just
6867 * load into the X register the offset computed into
6868 * the register specified by "index".
6871 sappend(s
, xfer_to_a(inst
));
6872 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6873 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6875 s
= xfer_to_x(inst
);
6878 * Load the item at the sum of the offset we've put in the
6879 * X register, the offset of the start of the network
6880 * layer header from the beginning of the link-layer
6881 * payload, and the constant part of the offset of the
6882 * start of the link-layer payload.
6884 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6885 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6887 sappend(inst
->s
, s
);
6890 * Do the computation only if the packet contains
6891 * the protocol in question.
6893 b
= gen_proto_abbrev(proto
);
6895 gen_and(inst
->b
, b
);
6909 * The offset is relative to the beginning of
6910 * the transport-layer header.
6912 * Load the X register with the length of the IPv4 header
6913 * (plus the offset of the link-layer header, if it's
6914 * a variable-length header), in bytes.
6916 * XXX - are there any cases where we want
6918 * XXX - we should, if we're built with
6919 * IPv6 support, generate code to load either
6920 * IPv4, IPv6, or both, as appropriate.
6922 s
= gen_loadx_iphdrlen();
6925 * The X register now contains the sum of the variable
6926 * part of the offset of the link-layer payload and the
6927 * length of the network-layer header.
6929 * Load into the A register the offset relative to
6930 * the beginning of the transport layer header,
6931 * add the X register to that, move that to the
6932 * X register, and load with an offset from the
6933 * X register equal to the sum of the constant part of
6934 * the offset of the link-layer payload and the offset,
6935 * relative to the beginning of the link-layer payload,
6936 * of the network-layer header.
6938 sappend(s
, xfer_to_a(inst
));
6939 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6940 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6941 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6942 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6943 sappend(inst
->s
, s
);
6946 * Do the computation only if the packet contains
6947 * the protocol in question - which is true only
6948 * if this is an IP datagram and is the first or
6949 * only fragment of that datagram.
6951 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6953 gen_and(inst
->b
, b
);
6954 gen_and(gen_proto_abbrev(Q_IP
), b
);
6958 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6961 inst
->regno
= regno
;
6962 s
= new_stmt(BPF_ST
);
6964 sappend(inst
->s
, s
);
6970 gen_relation(code
, a0
, a1
, reversed
)
6972 struct arth
*a0
, *a1
;
6975 struct slist
*s0
, *s1
, *s2
;
6976 struct block
*b
, *tmp
;
6980 if (code
== BPF_JEQ
) {
6981 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6982 b
= new_block(JMP(code
));
6986 b
= new_block(BPF_JMP
|code
|BPF_X
);
6992 sappend(a0
->s
, a1
->s
);
6996 free_reg(a0
->regno
);
6997 free_reg(a1
->regno
);
6999 /* 'and' together protocol checks */
7002 gen_and(a0
->b
, tmp
= a1
->b
);
7018 int regno
= alloc_reg();
7019 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7022 s
= new_stmt(BPF_LD
|BPF_LEN
);
7023 s
->next
= new_stmt(BPF_ST
);
7024 s
->next
->s
.k
= regno
;
7039 a
= (struct arth
*)newchunk(sizeof(*a
));
7043 s
= new_stmt(BPF_LD
|BPF_IMM
);
7045 s
->next
= new_stmt(BPF_ST
);
7061 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7064 s
= new_stmt(BPF_ST
);
7072 gen_arth(code
, a0
, a1
)
7074 struct arth
*a0
, *a1
;
7076 struct slist
*s0
, *s1
, *s2
;
7079 * Disallow division by, or modulus by, zero; we do this here
7080 * so that it gets done even if the optimizer is disabled.
7082 if (code
== BPF_DIV
) {
7083 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7084 bpf_error("division by zero");
7085 } else if (code
== BPF_MOD
) {
7086 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7087 bpf_error("modulus by zero");
7091 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7096 sappend(a0
->s
, a1
->s
);
7098 free_reg(a0
->regno
);
7099 free_reg(a1
->regno
);
7101 s0
= new_stmt(BPF_ST
);
7102 a0
->regno
= s0
->s
.k
= alloc_reg();
7109 * Here we handle simple allocation of the scratch registers.
7110 * If too many registers are alloc'd, the allocator punts.
7112 static int regused
[BPF_MEMWORDS
];
7116 * Initialize the table of used registers and the current register.
7122 memset(regused
, 0, sizeof regused
);
7126 * Return the next free register.
7131 int n
= BPF_MEMWORDS
;
7134 if (regused
[curreg
])
7135 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7137 regused
[curreg
] = 1;
7141 bpf_error("too many registers needed to evaluate expression");
7147 * Return a register to the table so it can
7157 static struct block
*
7164 s
= new_stmt(BPF_LD
|BPF_LEN
);
7165 b
= new_block(JMP(jmp
));
7176 return gen_len(BPF_JGE
, n
);
7180 * Actually, this is less than or equal.
7188 b
= gen_len(BPF_JGT
, n
);
7195 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7196 * the beginning of the link-layer header.
7197 * XXX - that means you can't test values in the radiotap header, but
7198 * as that header is difficult if not impossible to parse generally
7199 * without a loop, that might not be a severe problem. A new keyword
7200 * "radio" could be added for that, although what you'd really want
7201 * would be a way of testing particular radio header values, which
7202 * would generate code appropriate to the radio header in question.
7205 gen_byteop(op
, idx
, val
)
7216 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7219 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7223 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7227 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7231 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7235 b
= new_block(JMP(BPF_JEQ
));
7242 static u_char abroadcast
[] = { 0x0 };
7245 gen_broadcast(proto
)
7248 bpf_u_int32 hostmask
;
7249 struct block
*b0
, *b1
, *b2
;
7250 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7258 case DLT_ARCNET_LINUX
:
7259 return gen_ahostop(abroadcast
, Q_DST
);
7261 case DLT_NETANALYZER
:
7262 case DLT_NETANALYZER_TRANSPARENT
:
7263 b1
= gen_prevlinkhdr_check();
7264 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7269 return gen_fhostop(ebroadcast
, Q_DST
);
7271 return gen_thostop(ebroadcast
, Q_DST
);
7272 case DLT_IEEE802_11
:
7273 case DLT_PRISM_HEADER
:
7274 case DLT_IEEE802_11_RADIO_AVS
:
7275 case DLT_IEEE802_11_RADIO
:
7277 return gen_wlanhostop(ebroadcast
, Q_DST
);
7278 case DLT_IP_OVER_FC
:
7279 return gen_ipfchostop(ebroadcast
, Q_DST
);
7281 bpf_error("not a broadcast link");
7287 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7288 * as an indication that we don't know the netmask, and fail
7291 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7292 bpf_error("netmask not known, so 'ip broadcast' not supported");
7293 b0
= gen_linktype(ETHERTYPE_IP
);
7294 hostmask
= ~netmask
;
7295 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7296 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7297 (bpf_int32
)(~0 & hostmask
), hostmask
);
7302 bpf_error("only link-layer/IP broadcast filters supported");
7308 * Generate code to test the low-order bit of a MAC address (that's
7309 * the bottom bit of the *first* byte).
7311 static struct block
*
7312 gen_mac_multicast(offset
)
7315 register struct block
*b0
;
7316 register struct slist
*s
;
7318 /* link[offset] & 1 != 0 */
7319 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7320 b0
= new_block(JMP(BPF_JSET
));
7327 gen_multicast(proto
)
7330 register struct block
*b0
, *b1
, *b2
;
7331 register struct slist
*s
;
7339 case DLT_ARCNET_LINUX
:
7340 /* all ARCnet multicasts use the same address */
7341 return gen_ahostop(abroadcast
, Q_DST
);
7343 case DLT_NETANALYZER
:
7344 case DLT_NETANALYZER_TRANSPARENT
:
7345 b1
= gen_prevlinkhdr_check();
7346 /* ether[0] & 1 != 0 */
7347 b0
= gen_mac_multicast(0);
7353 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7355 * XXX - was that referring to bit-order issues?
7357 /* fddi[1] & 1 != 0 */
7358 return gen_mac_multicast(1);
7360 /* tr[2] & 1 != 0 */
7361 return gen_mac_multicast(2);
7362 case DLT_IEEE802_11
:
7363 case DLT_PRISM_HEADER
:
7364 case DLT_IEEE802_11_RADIO_AVS
:
7365 case DLT_IEEE802_11_RADIO
:
7370 * For control frames, there is no DA.
7372 * For management frames, DA is at an
7373 * offset of 4 from the beginning of
7376 * For data frames, DA is at an offset
7377 * of 4 from the beginning of the packet
7378 * if To DS is clear and at an offset of
7379 * 16 from the beginning of the packet
7384 * Generate the tests to be done for data frames.
7386 * First, check for To DS set, i.e. "link[1] & 0x01".
7388 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7389 b1
= new_block(JMP(BPF_JSET
));
7390 b1
->s
.k
= 0x01; /* To DS */
7394 * If To DS is set, the DA is at 16.
7396 b0
= gen_mac_multicast(16);
7400 * Now, check for To DS not set, i.e. check
7401 * "!(link[1] & 0x01)".
7403 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7404 b2
= new_block(JMP(BPF_JSET
));
7405 b2
->s
.k
= 0x01; /* To DS */
7410 * If To DS is not set, the DA is at 4.
7412 b1
= gen_mac_multicast(4);
7416 * Now OR together the last two checks. That gives
7417 * the complete set of checks for data frames.
7422 * Now check for a data frame.
7423 * I.e, check "link[0] & 0x08".
7425 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7426 b1
= new_block(JMP(BPF_JSET
));
7431 * AND that with the checks done for data frames.
7436 * If the high-order bit of the type value is 0, this
7437 * is a management frame.
7438 * I.e, check "!(link[0] & 0x08)".
7440 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7441 b2
= new_block(JMP(BPF_JSET
));
7447 * For management frames, the DA is at 4.
7449 b1
= gen_mac_multicast(4);
7453 * OR that with the checks done for data frames.
7454 * That gives the checks done for management and
7460 * If the low-order bit of the type value is 1,
7461 * this is either a control frame or a frame
7462 * with a reserved type, and thus not a
7465 * I.e., check "!(link[0] & 0x04)".
7467 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7468 b1
= new_block(JMP(BPF_JSET
));
7474 * AND that with the checks for data and management
7479 case DLT_IP_OVER_FC
:
7480 b0
= gen_mac_multicast(2);
7485 /* Link not known to support multicasts */
7489 b0
= gen_linktype(ETHERTYPE_IP
);
7490 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7495 b0
= gen_linktype(ETHERTYPE_IPV6
);
7496 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7500 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7506 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7507 * Outbound traffic is sent by this machine, while inbound traffic is
7508 * sent by a remote machine (and may include packets destined for a
7509 * unicast or multicast link-layer address we are not subscribing to).
7510 * These are the same definitions implemented by pcap_setdirection().
7511 * Capturing only unicast traffic destined for this host is probably
7512 * better accomplished using a higher-layer filter.
7518 register struct block
*b0
;
7521 * Only some data link types support inbound/outbound qualifiers.
7525 b0
= gen_relation(BPF_JEQ
,
7526 gen_load(Q_LINK
, gen_loadi(0), 1),
7533 /* match outgoing packets */
7534 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7536 /* match incoming packets */
7537 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7542 /* match outgoing packets */
7543 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7545 /* to filter on inbound traffic, invert the match */
7550 #ifdef HAVE_NET_PFVAR_H
7552 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7553 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7559 /* match outgoing packets */
7560 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7562 /* match incoming packets */
7563 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7567 case DLT_JUNIPER_MFR
:
7568 case DLT_JUNIPER_MLFR
:
7569 case DLT_JUNIPER_MLPPP
:
7570 case DLT_JUNIPER_ATM1
:
7571 case DLT_JUNIPER_ATM2
:
7572 case DLT_JUNIPER_PPPOE
:
7573 case DLT_JUNIPER_PPPOE_ATM
:
7574 case DLT_JUNIPER_GGSN
:
7575 case DLT_JUNIPER_ES
:
7576 case DLT_JUNIPER_MONITOR
:
7577 case DLT_JUNIPER_SERVICES
:
7578 case DLT_JUNIPER_ETHER
:
7579 case DLT_JUNIPER_PPP
:
7580 case DLT_JUNIPER_FRELAY
:
7581 case DLT_JUNIPER_CHDLC
:
7582 case DLT_JUNIPER_VP
:
7583 case DLT_JUNIPER_ST
:
7584 case DLT_JUNIPER_ISM
:
7585 case DLT_JUNIPER_VS
:
7586 case DLT_JUNIPER_SRX_E2E
:
7587 case DLT_JUNIPER_FIBRECHANNEL
:
7588 case DLT_JUNIPER_ATM_CEMIC
:
7590 /* juniper flags (including direction) are stored
7591 * the byte after the 3-byte magic number */
7593 /* match outgoing packets */
7594 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7596 /* match incoming packets */
7597 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7603 * If we have packet meta-data indicating a direction,
7604 * check it, otherwise give up as this link-layer type
7605 * has nothing in the packet data.
7607 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7609 * This is Linux with PF_PACKET support.
7610 * If this is a *live* capture, we can look at
7611 * special meta-data in the filter expression;
7612 * if it's a savefile, we can't.
7614 if (bpf_pcap
->rfile
!= NULL
) {
7615 /* We have a FILE *, so this is a savefile */
7616 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7621 /* match outgoing packets */
7622 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7625 /* to filter on inbound traffic, invert the match */
7628 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7629 bpf_error("inbound/outbound not supported on linktype %d",
7633 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7638 #ifdef HAVE_NET_PFVAR_H
7639 /* PF firewall log matched interface */
7641 gen_pf_ifname(const char *ifname
)
7646 if (linktype
!= DLT_PFLOG
) {
7647 bpf_error("ifname supported only on PF linktype");
7650 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7651 off
= offsetof(struct pfloghdr
, ifname
);
7652 if (strlen(ifname
) >= len
) {
7653 bpf_error("ifname interface names can only be %d characters",
7657 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7661 /* PF firewall log ruleset name */
7663 gen_pf_ruleset(char *ruleset
)
7667 if (linktype
!= DLT_PFLOG
) {
7668 bpf_error("ruleset supported only on PF linktype");
7672 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7673 bpf_error("ruleset names can only be %ld characters",
7674 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7678 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7679 strlen(ruleset
), (const u_char
*)ruleset
);
7683 /* PF firewall log rule number */
7689 if (linktype
!= DLT_PFLOG
) {
7690 bpf_error("rnr supported only on PF linktype");
7694 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7699 /* PF firewall log sub-rule number */
7701 gen_pf_srnr(int srnr
)
7705 if (linktype
!= DLT_PFLOG
) {
7706 bpf_error("srnr supported only on PF linktype");
7710 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7715 /* PF firewall log reason code */
7717 gen_pf_reason(int reason
)
7721 if (linktype
!= DLT_PFLOG
) {
7722 bpf_error("reason supported only on PF linktype");
7726 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7731 /* PF firewall log action */
7733 gen_pf_action(int action
)
7737 if (linktype
!= DLT_PFLOG
) {
7738 bpf_error("action supported only on PF linktype");
7742 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7746 #else /* !HAVE_NET_PFVAR_H */
7748 gen_pf_ifname(const char *ifname
)
7750 bpf_error("libpcap was compiled without pf support");
7756 gen_pf_ruleset(char *ruleset
)
7758 bpf_error("libpcap was compiled on a machine without pf support");
7766 bpf_error("libpcap was compiled on a machine without pf support");
7772 gen_pf_srnr(int srnr
)
7774 bpf_error("libpcap was compiled on a machine without pf support");
7780 gen_pf_reason(int reason
)
7782 bpf_error("libpcap was compiled on a machine without pf support");
7788 gen_pf_action(int action
)
7790 bpf_error("libpcap was compiled on a machine without pf support");
7794 #endif /* HAVE_NET_PFVAR_H */
7796 /* IEEE 802.11 wireless header */
7798 gen_p80211_type(int type
, int mask
)
7804 case DLT_IEEE802_11
:
7805 case DLT_PRISM_HEADER
:
7806 case DLT_IEEE802_11_RADIO_AVS
:
7807 case DLT_IEEE802_11_RADIO
:
7808 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7813 bpf_error("802.11 link-layer types supported only on 802.11");
7821 gen_p80211_fcdir(int fcdir
)
7827 case DLT_IEEE802_11
:
7828 case DLT_PRISM_HEADER
:
7829 case DLT_IEEE802_11_RADIO_AVS
:
7830 case DLT_IEEE802_11_RADIO
:
7834 bpf_error("frame direction supported only with 802.11 headers");
7838 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7839 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7846 register const u_char
*eaddr
;
7852 case DLT_ARCNET_LINUX
:
7853 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7855 return (gen_ahostop(eaddr
, (int)q
.dir
));
7857 bpf_error("ARCnet address used in non-arc expression");
7863 bpf_error("aid supported only on ARCnet");
7866 bpf_error("ARCnet address used in non-arc expression");
7871 static struct block
*
7872 gen_ahostop(eaddr
, dir
)
7873 register const u_char
*eaddr
;
7876 register struct block
*b0
, *b1
;
7879 /* src comes first, different from Ethernet */
7881 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7884 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7887 b0
= gen_ahostop(eaddr
, Q_SRC
);
7888 b1
= gen_ahostop(eaddr
, Q_DST
);
7894 b0
= gen_ahostop(eaddr
, Q_SRC
);
7895 b1
= gen_ahostop(eaddr
, Q_DST
);
7900 bpf_error("'addr1' is only supported on 802.11");
7904 bpf_error("'addr2' is only supported on 802.11");
7908 bpf_error("'addr3' is only supported on 802.11");
7912 bpf_error("'addr4' is only supported on 802.11");
7916 bpf_error("'ra' is only supported on 802.11");
7920 bpf_error("'ta' is only supported on 802.11");
7927 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7928 static struct block
*
7929 gen_vlan_bpf_extensions(int vlan_num
)
7931 struct block
*b0
, *b1
;
7934 /* generate new filter code based on extracting packet
7936 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7937 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7939 b0
= new_block(JMP(BPF_JEQ
));
7943 if (vlan_num
>= 0) {
7944 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7945 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7947 b1
= new_block(JMP(BPF_JEQ
));
7949 b1
->s
.k
= (bpf_int32
) vlan_num
;
7959 static struct block
*
7960 gen_vlan_no_bpf_extensions(int vlan_num
)
7962 struct block
*b0
, *b1
;
7964 /* check for VLAN, including QinQ */
7965 b0
= gen_linktype(ETHERTYPE_8021Q
);
7966 b1
= gen_linktype(ETHERTYPE_8021AD
);
7969 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7973 /* If a specific VLAN is requested, check VLAN id */
7974 if (vlan_num
>= 0) {
7975 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7976 (bpf_int32
)vlan_num
, 0x0fff);
7982 * The payload follows the full header, including the
7983 * VLAN tags, so skip past this VLAN tag.
7985 off_linkpl
.constant_part
+= 4;
7988 * The link-layer type information follows the VLAN tags, so
7989 * skip past this VLAN tag.
7991 off_linktype
.constant_part
+= 4;
7997 * support IEEE 802.1Q VLAN trunk over ethernet
8005 /* can't check for VLAN-encapsulated packets inside MPLS */
8006 if (label_stack_depth
> 0)
8007 bpf_error("no VLAN match after MPLS");
8010 * Check for a VLAN packet, and then change the offsets to point
8011 * to the type and data fields within the VLAN packet. Just
8012 * increment the offsets, so that we can support a hierarchy, e.g.
8013 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8016 * XXX - this is a bit of a kludge. If we were to split the
8017 * compiler into a parser that parses an expression and
8018 * generates an expression tree, and a code generator that
8019 * takes an expression tree (which could come from our
8020 * parser or from some other parser) and generates BPF code,
8021 * we could perhaps make the offsets parameters of routines
8022 * and, in the handler for an "AND" node, pass to subnodes
8023 * other than the VLAN node the adjusted offsets.
8025 * This would mean that "vlan" would, instead of changing the
8026 * behavior of *all* tests after it, change only the behavior
8027 * of tests ANDed with it. That would change the documented
8028 * semantics of "vlan", which might break some expressions.
8029 * However, it would mean that "(vlan and ip) or ip" would check
8030 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8031 * checking only for VLAN-encapsulated IP, so that could still
8032 * be considered worth doing; it wouldn't break expressions
8033 * that are of the form "vlan and ..." or "vlan N and ...",
8034 * which I suspect are the most common expressions involving
8035 * "vlan". "vlan or ..." doesn't necessarily do what the user
8036 * would really want, now, as all the "or ..." tests would
8037 * be done assuming a VLAN, even though the "or" could be viewed
8038 * as meaning "or, if this isn't a VLAN packet...".
8043 case DLT_NETANALYZER
:
8044 case DLT_NETANALYZER_TRANSPARENT
:
8045 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8046 /* Verify that this is the outer part of the packet and
8047 * not encapsulated somehow. */
8048 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
8049 off_linkhdr
.constant_part
==
8050 off_outermostlinkhdr
.constant_part
) {
8052 * Do we need special VLAN handling?
8054 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8055 b0
= gen_vlan_bpf_extensions(vlan_num
);
8057 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8060 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8063 case DLT_IEEE802_11
:
8064 case DLT_PRISM_HEADER
:
8065 case DLT_IEEE802_11_RADIO_AVS
:
8066 case DLT_IEEE802_11_RADIO
:
8067 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8071 bpf_error("no VLAN support for data link type %d",
8088 struct block
*b0
, *b1
;
8090 if (label_stack_depth
> 0) {
8091 /* just match the bottom-of-stack bit clear */
8092 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8095 * We're not in an MPLS stack yet, so check the link-layer
8096 * type against MPLS.
8100 case DLT_C_HDLC
: /* fall through */
8102 case DLT_NETANALYZER
:
8103 case DLT_NETANALYZER_TRANSPARENT
:
8104 b0
= gen_linktype(ETHERTYPE_MPLS
);
8108 b0
= gen_linktype(PPP_MPLS_UCAST
);
8111 /* FIXME add other DLT_s ...
8112 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8113 * leave it for now */
8116 bpf_error("no MPLS support for data link type %d",
8124 /* If a specific MPLS label is requested, check it */
8125 if (label_num
>= 0) {
8126 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8127 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8128 0xfffff000); /* only compare the first 20 bits */
8134 * Change the offsets to point to the type and data fields within
8135 * the MPLS packet. Just increment the offsets, so that we
8136 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8137 * capture packets with an outer label of 100000 and an inner
8140 * Increment the MPLS stack depth as well; this indicates that
8141 * we're checking MPLS-encapsulated headers, to make sure higher
8142 * level code generators don't try to match against IP-related
8143 * protocols such as Q_ARP, Q_RARP etc.
8145 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8149 label_stack_depth
++;
8154 * Support PPPOE discovery and session.
8159 /* check for PPPoE discovery */
8160 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8164 gen_pppoes(sess_num
)
8167 struct block
*b0
, *b1
;
8170 * Test against the PPPoE session link-layer type.
8172 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8174 /* If a specific session is requested, check PPPoE session id */
8175 if (sess_num
>= 0) {
8176 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8177 (bpf_int32
)sess_num
, 0x0000ffff);
8183 * Change the offsets to point to the type and data fields within
8184 * the PPP packet, and note that this is PPPoE rather than
8187 * XXX - this is a bit of a kludge. If we were to split the
8188 * compiler into a parser that parses an expression and
8189 * generates an expression tree, and a code generator that
8190 * takes an expression tree (which could come from our
8191 * parser or from some other parser) and generates BPF code,
8192 * we could perhaps make the offsets parameters of routines
8193 * and, in the handler for an "AND" node, pass to subnodes
8194 * other than the PPPoE node the adjusted offsets.
8196 * This would mean that "pppoes" would, instead of changing the
8197 * behavior of *all* tests after it, change only the behavior
8198 * of tests ANDed with it. That would change the documented
8199 * semantics of "pppoes", which might break some expressions.
8200 * However, it would mean that "(pppoes and ip) or ip" would check
8201 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8202 * checking only for VLAN-encapsulated IP, so that could still
8203 * be considered worth doing; it wouldn't break expressions
8204 * that are of the form "pppoes and ..." which I suspect are the
8205 * most common expressions involving "pppoes". "pppoes or ..."
8206 * doesn't necessarily do what the user would really want, now,
8207 * as all the "or ..." tests would be done assuming PPPoE, even
8208 * though the "or" could be viewed as meaning "or, if this isn't
8209 * a PPPoE packet...".
8211 * The "network-layer" protocol is PPPoE, which has a 6-byte
8212 * PPPoE header, followed by a PPP packet.
8214 * There is no HDLC encapsulation for the PPP packet (it's
8215 * encapsulated in PPPoES instead), so the link-layer type
8216 * starts at the first byte of the PPP packet. For PPPoE,
8217 * that offset is relative to the beginning of the total
8218 * link-layer payload, including any 802.2 LLC header, so
8219 * it's 6 bytes past off_nl.
8221 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8222 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8225 off_linktype
= off_linkhdr
;
8226 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8229 off_nl_nosnap
= 0; /* no 802.2 LLC */
8234 /* Check that this is Geneve and the VNI is correct if
8235 * specified. Parameterized to handle both IPv4 and IPv6. */
8236 static struct block
*
8237 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8238 enum e_offrel offrel
, int vni
)
8240 struct block
*b0
, *b1
;
8242 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8244 /* Check that we are operating on version 0. Otherwise, we
8245 * can't decode the rest of the fields. The version is 2 bits
8246 * in the first byte of the Geneve header. */
8247 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8252 vni
<<= 8; /* VNI is in the upper 3 bytes */
8253 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8262 /* The IPv4 and IPv6 Geneve checks need to do two things:
8263 * - Verify that this actually is Geneve with the right VNI.
8264 * - Place the IP header length (plus variable link prefix if
8265 * needed) into register A to be used later to compute
8266 * the inner packet offsets. */
8267 static struct block
*
8268 gen_geneve4(int vni
)
8270 struct block
*b0
, *b1
;
8271 struct slist
*s
, *s1
;
8273 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8275 /* Load the IP header length into A. */
8276 s
= gen_loadx_iphdrlen();
8278 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8281 /* Forcibly append these statements to the true condition
8282 * of the protocol check by creating a new block that is
8283 * always true and ANDing them. */
8284 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8293 static struct block
*
8294 gen_geneve6(int vni
)
8296 struct block
*b0
, *b1
;
8297 struct slist
*s
, *s1
;
8299 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8301 /* Load the IP header length. We need to account for a
8302 * variable length link prefix if there is one. */
8303 s
= gen_abs_offset_varpart(&off_linkpl
);
8305 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8309 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8313 s
= new_stmt(BPF_LD
|BPF_IMM
);
8317 /* Forcibly append these statements to the true condition
8318 * of the protocol check by creating a new block that is
8319 * always true and ANDing them. */
8320 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8323 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8332 /* We need to store three values based on the Geneve header::
8333 * - The offset of the linktype.
8334 * - The offset of the end of the Geneve header.
8335 * - The offset of the end of the encapsulated MAC header. */
8336 static struct slist
*
8337 gen_geneve_offsets(void)
8339 struct slist
*s
, *s1
, *s_proto
;
8341 /* First we need to calculate the offset of the Geneve header
8342 * itself. This is composed of the IP header previously calculated
8343 * (include any variable link prefix) and stored in A plus the
8344 * fixed sized headers (fixed link prefix, MAC length, and UDP
8346 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8347 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8349 /* Stash this in X since we'll need it later. */
8350 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8353 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8355 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8359 off_linktype
.reg
= alloc_reg();
8360 off_linktype
.is_variable
= 1;
8361 off_linktype
.constant_part
= 0;
8363 s1
= new_stmt(BPF_ST
);
8364 s1
->s
.k
= off_linktype
.reg
;
8367 /* Load the Geneve option length and mask and shift to get the
8368 * number of bytes. It is stored in the first byte of the Geneve
8370 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8374 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8378 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8382 /* Add in the rest of the Geneve base header. */
8383 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8387 /* Add the Geneve header length to its offset and store. */
8388 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8392 /* Set the encapsulated type as Ethernet. Even though we may
8393 * not actually have Ethernet inside there are two reasons this
8395 * - The linktype field is always in EtherType format regardless
8396 * of whether it is in Geneve or an inner Ethernet frame.
8397 * - The only link layer that we have specific support for is
8398 * Ethernet. We will confirm that the packet actually is
8399 * Ethernet at runtime before executing these checks. */
8400 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8402 s1
= new_stmt(BPF_ST
);
8403 s1
->s
.k
= off_linkhdr
.reg
;
8406 /* Calculate whether we have an Ethernet header or just raw IP/
8407 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8408 * and linktype by 14 bytes so that the network header can be found
8409 * seamlessly. Otherwise, keep what we've calculated already. */
8411 /* We have a bare jmp so we can't use the optimizer. */
8414 /* Load the EtherType in the Geneve header, 2 bytes in. */
8415 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8419 /* Load X with the end of the Geneve header. */
8420 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8421 s1
->s
.k
= off_linkhdr
.reg
;
8424 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8425 * end of this check, we should have the total length in X. In
8426 * the non-Ethernet case, it's already there. */
8427 s_proto
= new_stmt(JMP(BPF_JEQ
));
8428 s_proto
->s
.k
= ETHERTYPE_TEB
;
8429 sappend(s
, s_proto
);
8431 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8435 /* Since this is Ethernet, use the EtherType of the payload
8436 * directly as the linktype. Overwrite what we already have. */
8437 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8441 s1
= new_stmt(BPF_ST
);
8442 s1
->s
.k
= off_linktype
.reg
;
8445 /* Advance two bytes further to get the end of the Ethernet
8447 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8451 /* Move the result to X. */
8452 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8455 /* Store the final result of our linkpl calculation. */
8456 off_linkpl
.reg
= alloc_reg();
8457 off_linkpl
.is_variable
= 1;
8458 off_linkpl
.constant_part
= 0;
8460 s1
= new_stmt(BPF_STX
);
8461 s1
->s
.k
= off_linkpl
.reg
;
8470 /* Check to see if this is a Geneve packet. */
8474 struct block
*b0
, *b1
;
8477 b0
= gen_geneve4(vni
);
8478 b1
= gen_geneve6(vni
);
8483 /* Later filters should act on the payload of the Geneve frame,
8484 * update all of the header pointers. Attach this code so that
8485 * it gets executed in the event that the Geneve filter matches. */
8486 s
= gen_geneve_offsets();
8489 sappend(s
, b1
->stmts
);
8499 /* Check that the encapsulated frame has a link layer header
8500 * for Ethernet filters. */
8501 static struct block
*
8502 gen_geneve_ll_check()
8505 struct slist
*s
, *s1
;
8507 /* The easiest way to see if there is a link layer present
8508 * is to check if the link layer header and payload are not
8511 /* Geneve always generates pure variable offsets so we can
8512 * compare only the registers. */
8513 s
= new_stmt(BPF_LD
|BPF_MEM
);
8514 s
->s
.k
= off_linkhdr
.reg
;
8516 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8517 s1
->s
.k
= off_linkpl
.reg
;
8520 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8529 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8541 bpf_error("'vpi' supported only on raw ATM");
8542 if (off_vpi
== (u_int
)-1)
8544 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8550 bpf_error("'vci' supported only on raw ATM");
8551 if (off_vci
== (u_int
)-1)
8553 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8558 if (off_proto
== (u_int
)-1)
8559 abort(); /* XXX - this isn't on FreeBSD */
8560 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8565 if (off_payload
== (u_int
)-1)
8567 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8568 0xffffffff, jtype
, reverse
, jvalue
);
8573 bpf_error("'callref' supported only on raw ATM");
8574 if (off_proto
== (u_int
)-1)
8576 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8577 jtype
, reverse
, jvalue
);
8587 gen_atmtype_abbrev(type
)
8590 struct block
*b0
, *b1
;
8595 /* Get all packets in Meta signalling Circuit */
8597 bpf_error("'metac' supported only on raw ATM");
8598 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8599 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8604 /* Get all packets in Broadcast Circuit*/
8606 bpf_error("'bcc' supported only on raw ATM");
8607 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8608 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8613 /* Get all cells in Segment OAM F4 circuit*/
8615 bpf_error("'oam4sc' supported only on raw ATM");
8616 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8617 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8622 /* Get all cells in End-to-End OAM F4 Circuit*/
8624 bpf_error("'oam4ec' supported only on raw ATM");
8625 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8626 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8631 /* Get all packets in connection Signalling Circuit */
8633 bpf_error("'sc' supported only on raw ATM");
8634 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8635 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8640 /* Get all packets in ILMI Circuit */
8642 bpf_error("'ilmic' supported only on raw ATM");
8643 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8644 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8649 /* Get all LANE packets */
8651 bpf_error("'lane' supported only on raw ATM");
8652 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8655 * Arrange that all subsequent tests assume LANE
8656 * rather than LLC-encapsulated packets, and set
8657 * the offsets appropriately for LANE-encapsulated
8660 * We assume LANE means Ethernet, not Token Ring.
8662 PUSH_LINKHDR(DLT_EN10MB
, 0,
8663 off_payload
+ 2, /* Ethernet header */
8665 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8666 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8667 off_nl
= 0; /* Ethernet II */
8668 off_nl_nosnap
= 3; /* 802.3+802.2 */
8672 /* Get all LLC-encapsulated packets */
8674 bpf_error("'llc' supported only on raw ATM");
8675 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8676 linktype
= prevlinktype
;
8686 * Filtering for MTP2 messages based on li value
8687 * FISU, length is null
8688 * LSSU, length is 1 or 2
8689 * MSU, length is 3 or more
8690 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8693 gen_mtp2type_abbrev(type
)
8696 struct block
*b0
, *b1
;
8701 if ( (linktype
!= DLT_MTP2
) &&
8702 (linktype
!= DLT_ERF
) &&
8703 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8704 bpf_error("'fisu' supported only on MTP2");
8705 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8706 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8710 if ( (linktype
!= DLT_MTP2
) &&
8711 (linktype
!= DLT_ERF
) &&
8712 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8713 bpf_error("'lssu' supported only on MTP2");
8714 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8715 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8720 if ( (linktype
!= DLT_MTP2
) &&
8721 (linktype
!= DLT_ERF
) &&
8722 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8723 bpf_error("'msu' supported only on MTP2");
8724 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8728 if ( (linktype
!= DLT_MTP2
) &&
8729 (linktype
!= DLT_ERF
) &&
8730 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8731 bpf_error("'hfisu' supported only on MTP2_HSL");
8732 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8733 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8737 if ( (linktype
!= DLT_MTP2
) &&
8738 (linktype
!= DLT_ERF
) &&
8739 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8740 bpf_error("'hlssu' supported only on MTP2_HSL");
8741 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8742 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8747 if ( (linktype
!= DLT_MTP2
) &&
8748 (linktype
!= DLT_ERF
) &&
8749 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8750 bpf_error("'hmsu' supported only on MTP2_HSL");
8751 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8761 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8768 bpf_u_int32 val1
, val2
, val3
;
8769 u_int newoff_sio
=off_sio
;
8770 u_int newoff_opc
=off_opc
;
8771 u_int newoff_dpc
=off_dpc
;
8772 u_int newoff_sls
=off_sls
;
8774 switch (mtp3field
) {
8777 newoff_sio
+= 3; /* offset for MTP2_HSL */
8781 if (off_sio
== (u_int
)-1)
8782 bpf_error("'sio' supported only on SS7");
8783 /* sio coded on 1 byte so max value 255 */
8785 bpf_error("sio value %u too big; max value = 255",
8787 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8788 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8794 if (off_opc
== (u_int
)-1)
8795 bpf_error("'opc' supported only on SS7");
8796 /* opc coded on 14 bits so max value 16383 */
8798 bpf_error("opc value %u too big; max value = 16383",
8800 /* the following instructions are made to convert jvalue
8801 * to the form used to write opc in an ss7 message*/
8802 val1
= jvalue
& 0x00003c00;
8804 val2
= jvalue
& 0x000003fc;
8806 val3
= jvalue
& 0x00000003;
8808 jvalue
= val1
+ val2
+ val3
;
8809 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8810 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8818 if (off_dpc
== (u_int
)-1)
8819 bpf_error("'dpc' supported only on SS7");
8820 /* dpc coded on 14 bits so max value 16383 */
8822 bpf_error("dpc value %u too big; max value = 16383",
8824 /* the following instructions are made to convert jvalue
8825 * to the forme used to write dpc in an ss7 message*/
8826 val1
= jvalue
& 0x000000ff;
8828 val2
= jvalue
& 0x00003f00;
8830 jvalue
= val1
+ val2
;
8831 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8832 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8838 if (off_sls
== (u_int
)-1)
8839 bpf_error("'sls' supported only on SS7");
8840 /* sls coded on 4 bits so max value 15 */
8842 bpf_error("sls value %u too big; max value = 15",
8844 /* the following instruction is made to convert jvalue
8845 * to the forme used to write sls in an ss7 message*/
8846 jvalue
= jvalue
<< 4;
8847 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8848 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8857 static struct block
*
8858 gen_msg_abbrev(type
)
8864 * Q.2931 signalling protocol messages for handling virtual circuits
8865 * establishment and teardown
8870 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8874 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8878 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8882 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8886 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8889 case A_RELEASE_DONE
:
8890 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8900 gen_atmmulti_abbrev(type
)
8903 struct block
*b0
, *b1
;
8909 bpf_error("'oam' supported only on raw ATM");
8910 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8915 bpf_error("'oamf4' supported only on raw ATM");
8917 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8918 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8920 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8926 * Get Q.2931 signalling messages for switched
8927 * virtual connection
8930 bpf_error("'connectmsg' supported only on raw ATM");
8931 b0
= gen_msg_abbrev(A_SETUP
);
8932 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8934 b0
= gen_msg_abbrev(A_CONNECT
);
8936 b0
= gen_msg_abbrev(A_CONNECTACK
);
8938 b0
= gen_msg_abbrev(A_RELEASE
);
8940 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8942 b0
= gen_atmtype_abbrev(A_SC
);
8948 bpf_error("'metaconnect' supported only on raw ATM");
8949 b0
= gen_msg_abbrev(A_SETUP
);
8950 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8952 b0
= gen_msg_abbrev(A_CONNECT
);
8954 b0
= gen_msg_abbrev(A_RELEASE
);
8956 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8958 b0
= gen_atmtype_abbrev(A_METAC
);