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"
80 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
81 #include <linux/types.h>
82 #include <linux/if_packet.h>
83 #include <linux/filter.h>
86 #ifdef HAVE_NET_PFVAR_H
87 #include <sys/socket.h>
89 #include <net/pfvar.h>
90 #include <net/if_pflog.h>
94 #define offsetof(s, e) ((size_t)&((s *)0)->e)
99 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
105 u_int8_t u6_addr8
[16];
106 u_int16_t u6_addr16
[8];
107 u_int32_t u6_addr32
[4];
109 #define s6_addr in6_u.u6_addr8
110 #define s6_addr16 in6_u.u6_addr16
111 #define s6_addr32 in6_u.u6_addr32
112 #define s6_addr64 in6_u.u6_addr64
115 typedef unsigned short sa_family_t
;
117 #define __SOCKADDR_COMMON(sa_prefix) \
118 sa_family_t sa_prefix##family
120 /* Ditto, for IPv6. */
123 __SOCKADDR_COMMON (sin6_
);
124 u_int16_t sin6_port
; /* Transport layer port # */
125 u_int32_t sin6_flowinfo
; /* IPv6 flow information */
126 struct in6_addr sin6_addr
; /* IPv6 address */
129 #ifndef EAI_ADDRFAMILY
131 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
132 int ai_family
; /* PF_xxx */
133 int ai_socktype
; /* SOCK_xxx */
134 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
135 size_t ai_addrlen
; /* length of ai_addr */
136 char *ai_canonname
; /* canonical name for hostname */
137 struct sockaddr
*ai_addr
; /* binary address */
138 struct addrinfo
*ai_next
; /* next structure in linked list */
140 #endif /* EAI_ADDRFAMILY */
141 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
143 #include <netdb.h> /* for "struct addrinfo" */
146 #include <pcap/namedb.h>
148 #include "nametoaddr.h"
150 #define ETHERMTU 1500
152 #ifndef ETHERTYPE_TEB
153 #define ETHERTYPE_TEB 0x6558
156 #ifndef IPPROTO_HOPOPTS
157 #define IPPROTO_HOPOPTS 0
159 #ifndef IPPROTO_ROUTING
160 #define IPPROTO_ROUTING 43
162 #ifndef IPPROTO_FRAGMENT
163 #define IPPROTO_FRAGMENT 44
165 #ifndef IPPROTO_DSTOPTS
166 #define IPPROTO_DSTOPTS 60
169 #define IPPROTO_SCTP 132
172 #define GENEVE_PORT 6081
174 #ifdef HAVE_OS_PROTO_H
175 #include "os-proto.h"
178 #define JMP(c) ((c)|BPF_JMP|BPF_K)
181 static jmp_buf top_ctx
;
182 static pcap_t
*bpf_pcap
;
184 /* Hack for handling VLAN and MPLS stacks. */
186 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
188 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
192 static int pcap_fddipad
;
196 bpf_error(const char *fmt
, ...)
201 if (bpf_pcap
!= NULL
)
202 (void)pcap_vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
209 static void init_linktype(pcap_t
*);
211 static void init_regs(void);
212 static int alloc_reg(void);
213 static void free_reg(int);
215 static struct block
*root
;
218 * Absolute offsets, which are offsets from the beginning of the raw
219 * packet data, are, in the general case, the sum of a variable value
220 * and a constant value; the variable value may be absent, in which
221 * case the offset is only the constant value, and the constant value
222 * may be zero, in which case the offset is only the variable value.
224 * bpf_abs_offset is a structure containing all that information:
226 * is_variable is 1 if there's a variable part.
228 * constant_part is the constant part of the value, possibly zero;
230 * if is_variable is 1, reg is the register number for a register
231 * containing the variable value if the register has been assigned,
241 * Value passed to gen_load_a() to indicate what the offset argument
242 * is relative to the beginning of.
245 OR_PACKET
, /* full packet data */
246 OR_LINKHDR
, /* link-layer header */
247 OR_PREVLINKHDR
, /* previous link-layer header */
248 OR_LLC
, /* 802.2 LLC header */
249 OR_PREVMPLSHDR
, /* previous MPLS header */
250 OR_LINKTYPE
, /* link-layer type */
251 OR_LINKPL
, /* link-layer payload */
252 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
253 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
254 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
259 * As errors are handled by a longjmp, anything allocated must be freed
260 * in the longjmp handler, so it must be reachable from that handler.
261 * One thing that's allocated is the result of pcap_nametoaddrinfo();
262 * it must be freed with freeaddrinfo(). This variable points to any
263 * addrinfo structure that would need to be freed.
265 static struct addrinfo
*ai
;
269 * We divy out chunks of memory rather than call malloc each time so
270 * we don't have to worry about leaking memory. It's probably
271 * not a big deal if all this memory was wasted but if this ever
272 * goes into a library that would probably not be a good idea.
274 * XXX - this *is* in a library....
277 #define CHUNK0SIZE 1024
283 static struct chunk chunks
[NCHUNKS
];
284 static int cur_chunk
;
286 static void *newchunk(size_t);
287 static void freechunks(void);
288 static inline struct block
*new_block(int);
289 static inline struct slist
*new_stmt(int);
290 static struct block
*gen_retblk(int);
291 static inline void syntax(void);
293 static void backpatch(struct block
*, struct block
*);
294 static void merge(struct block
*, struct block
*);
295 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
296 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
297 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
298 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
299 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
300 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
302 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
303 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
304 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
305 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
306 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
307 static struct slist
*gen_loadx_iphdrlen(void);
308 static struct block
*gen_uncond(int);
309 static inline struct block
*gen_true(void);
310 static inline struct block
*gen_false(void);
311 static struct block
*gen_ether_linktype(int);
312 static struct block
*gen_ipnet_linktype(int);
313 static struct block
*gen_linux_sll_linktype(int);
314 static struct slist
*gen_load_prism_llprefixlen(void);
315 static struct slist
*gen_load_avs_llprefixlen(void);
316 static struct slist
*gen_load_radiotap_llprefixlen(void);
317 static struct slist
*gen_load_ppi_llprefixlen(void);
318 static void insert_compute_vloffsets(struct block
*);
319 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
320 static int ethertype_to_ppptype(int);
321 static struct block
*gen_linktype(int);
322 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
323 static struct block
*gen_llc_linktype(int);
324 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
326 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
328 static struct block
*gen_ahostop(const u_char
*, int);
329 static struct block
*gen_ehostop(const u_char
*, int);
330 static struct block
*gen_fhostop(const u_char
*, int);
331 static struct block
*gen_thostop(const u_char
*, int);
332 static struct block
*gen_wlanhostop(const u_char
*, int);
333 static struct block
*gen_ipfchostop(const u_char
*, int);
334 static struct block
*gen_dnhostop(bpf_u_int32
, int);
335 static struct block
*gen_mpls_linktype(int);
336 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
338 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
341 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
343 static struct block
*gen_ipfrag(void);
344 static struct block
*gen_portatom(int, bpf_int32
);
345 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
346 static struct block
*gen_portatom6(int, bpf_int32
);
347 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
348 struct block
*gen_portop(int, int, int);
349 static struct block
*gen_port(int, int, int);
350 struct block
*gen_portrangeop(int, int, int, int);
351 static struct block
*gen_portrange(int, int, int, int);
352 struct block
*gen_portop6(int, int, int);
353 static struct block
*gen_port6(int, int, int);
354 struct block
*gen_portrangeop6(int, int, int, int);
355 static struct block
*gen_portrange6(int, int, int, int);
356 static int lookup_proto(const char *, int);
357 static struct block
*gen_protochain(int, int, int);
358 static struct block
*gen_proto(int, int, int);
359 static struct slist
*xfer_to_x(struct arth
*);
360 static struct slist
*xfer_to_a(struct arth
*);
361 static struct block
*gen_mac_multicast(int);
362 static struct block
*gen_len(int, int);
363 static struct block
*gen_check_802_11_data_frame(void);
364 static struct block
*gen_geneve_ll_check(void);
366 static struct block
*gen_ppi_dlt_check(void);
367 static struct block
*gen_msg_abbrev(int type
);
377 /* XXX Round up to nearest long. */
378 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
380 /* XXX Round up to structure boundary. */
384 cp
= &chunks
[cur_chunk
];
385 if (n
> cp
->n_left
) {
386 ++cp
, k
= ++cur_chunk
;
388 bpf_error("out of memory");
389 size
= CHUNK0SIZE
<< k
;
390 cp
->m
= (void *)malloc(size
);
392 bpf_error("out of memory");
393 memset((char *)cp
->m
, 0, size
);
396 bpf_error("out of memory");
399 return (void *)((char *)cp
->m
+ cp
->n_left
);
408 for (i
= 0; i
< NCHUNKS
; ++i
)
409 if (chunks
[i
].m
!= NULL
) {
416 * A strdup whose allocations are freed after code generation is over.
420 register const char *s
;
422 size_t n
= strlen(s
) + 1;
423 char *cp
= newchunk(n
);
429 static inline struct block
*
435 p
= (struct block
*)newchunk(sizeof(*p
));
442 static inline struct slist
*
448 p
= (struct slist
*)newchunk(sizeof(*p
));
454 static struct block
*
458 struct block
*b
= new_block(BPF_RET
|BPF_K
);
467 bpf_error("syntax error in filter expression");
470 static bpf_u_int32 netmask
;
475 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
476 const char *buf
, int optimize
, bpf_u_int32 mask
)
479 const char * volatile xbuf
= buf
;
480 yyscan_t scanner
= NULL
;
481 YY_BUFFER_STATE in_buffer
= NULL
;
486 * XXX - single-thread this code path with pthread calls on
487 * UN*X, if the platform supports pthreads? If that requires
488 * a separate -lpthread, we might not want to do that.
496 EnterCriticalSection(&g_PcapCompileCriticalSection
);
500 * If this pcap_t hasn't been activated, it doesn't have a
501 * link-layer type, so we can't use it.
504 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
505 "not-yet-activated pcap_t passed to pcap_compile");
515 if (setjmp(top_ctx
)) {
528 snaplen
= pcap_snapshot(p
);
530 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
531 "snaplen of 0 rejects all packets");
536 if (pcap_lex_init(&scanner
) != 0)
537 bpf_error("can't initialize scanner: %s", pcap_strerror(errno
));
538 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
540 (void)pcap_parse(scanner
);
546 root
= gen_retblk(snaplen
);
548 if (optimize
&& !no_optimize
) {
551 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
552 bpf_error("expression rejects all packets");
554 program
->bf_insns
= icode_to_fcode(root
, &len
);
555 program
->bf_len
= len
;
557 rc
= 0; /* We're all okay */
561 * Clean up everything for the lexical analyzer.
563 if (in_buffer
!= NULL
)
564 pcap__delete_buffer(in_buffer
, scanner
);
566 pcap_lex_destroy(scanner
);
569 * Clean up our own allocated memory.
574 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
581 * entry point for using the compiler with no pcap open
582 * pass in all the stuff that is needed explicitly instead.
585 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
586 struct bpf_program
*program
,
587 const char *buf
, int optimize
, bpf_u_int32 mask
)
592 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
595 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
601 * Clean up a "struct bpf_program" by freeing all the memory allocated
605 pcap_freecode(struct bpf_program
*program
)
608 if (program
->bf_insns
!= NULL
) {
609 free((char *)program
->bf_insns
);
610 program
->bf_insns
= NULL
;
615 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
616 * which of the jt and jf fields has been resolved and which is a pointer
617 * back to another unresolved block (or nil). At least one of the fields
618 * in each block is already resolved.
621 backpatch(list
, target
)
622 struct block
*list
, *target
;
639 * Merge the lists in b0 and b1, using the 'sense' field to indicate
640 * which of jt and jf is the link.
644 struct block
*b0
, *b1
;
646 register struct block
**p
= &b0
;
648 /* Find end of list. */
650 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
652 /* Concatenate the lists. */
660 struct block
*ppi_dlt_check
;
663 * Insert before the statements of the first (root) block any
664 * statements needed to load the lengths of any variable-length
665 * headers into registers.
667 * XXX - a fancier strategy would be to insert those before the
668 * statements of all blocks that use those lengths and that
669 * have no predecessors that use them, so that we only compute
670 * the lengths if we need them. There might be even better
671 * approaches than that.
673 * However, those strategies would be more complicated, and
674 * as we don't generate code to compute a length if the
675 * program has no tests that use the length, and as most
676 * tests will probably use those lengths, we would just
677 * postpone computing the lengths so that it's not done
678 * for tests that fail early, and it's not clear that's
681 insert_compute_vloffsets(p
->head
);
684 * For DLT_PPI captures, generate a check of the per-packet
685 * DLT value to make sure it's DLT_IEEE802_11.
687 * XXX - TurboCap cards use DLT_PPI for Ethernet.
688 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
689 * with appropriate Ethernet information and use that rather
690 * than using something such as DLT_PPI where you don't know
691 * the link-layer header type until runtime, which, in the
692 * general case, would force us to generate both Ethernet *and*
693 * 802.11 code (*and* anything else for which PPI is used)
694 * and choose between them early in the BPF program?
696 ppi_dlt_check
= gen_ppi_dlt_check();
697 if (ppi_dlt_check
!= NULL
)
698 gen_and(ppi_dlt_check
, p
);
700 backpatch(p
, gen_retblk(snaplen
));
701 p
->sense
= !p
->sense
;
702 backpatch(p
, gen_retblk(0));
708 struct block
*b0
, *b1
;
710 backpatch(b0
, b1
->head
);
711 b0
->sense
= !b0
->sense
;
712 b1
->sense
= !b1
->sense
;
714 b1
->sense
= !b1
->sense
;
720 struct block
*b0
, *b1
;
722 b0
->sense
= !b0
->sense
;
723 backpatch(b0
, b1
->head
);
724 b0
->sense
= !b0
->sense
;
733 b
->sense
= !b
->sense
;
736 static struct block
*
737 gen_cmp(offrel
, offset
, size
, v
)
738 enum e_offrel offrel
;
742 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
745 static struct block
*
746 gen_cmp_gt(offrel
, offset
, size
, v
)
747 enum e_offrel offrel
;
751 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
754 static struct block
*
755 gen_cmp_ge(offrel
, offset
, size
, v
)
756 enum e_offrel offrel
;
760 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
763 static struct block
*
764 gen_cmp_lt(offrel
, offset
, size
, v
)
765 enum e_offrel offrel
;
769 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
772 static struct block
*
773 gen_cmp_le(offrel
, offset
, size
, v
)
774 enum e_offrel offrel
;
778 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
781 static struct block
*
782 gen_mcmp(offrel
, offset
, size
, v
, mask
)
783 enum e_offrel offrel
;
788 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
791 static struct block
*
792 gen_bcmp(offrel
, offset
, size
, v
)
793 enum e_offrel offrel
;
794 register u_int offset
, size
;
795 register const u_char
*v
;
797 register struct block
*b
, *tmp
;
801 register const u_char
*p
= &v
[size
- 4];
802 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
803 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
805 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
812 register const u_char
*p
= &v
[size
- 2];
813 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
815 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
822 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
831 * AND the field of size "size" at offset "offset" relative to the header
832 * specified by "offrel" with "mask", and compare it with the value "v"
833 * with the test specified by "jtype"; if "reverse" is true, the test
834 * should test the opposite of "jtype".
836 static struct block
*
837 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
838 enum e_offrel offrel
;
840 bpf_u_int32 offset
, size
, mask
, jtype
;
843 struct slist
*s
, *s2
;
846 s
= gen_load_a(offrel
, offset
, size
);
848 if (mask
!= 0xffffffff) {
849 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
854 b
= new_block(JMP(jtype
));
857 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
863 * Various code constructs need to know the layout of the packet.
864 * These variables give the necessary offsets from the beginning
865 * of the packet data.
869 * Absolute offset of the beginning of the link-layer header.
871 static bpf_abs_offset off_linkhdr
;
874 * If we're checking a link-layer header for a packet encapsulated in
875 * another protocol layer, this is the equivalent information for the
876 * previous layers' link-layer header from the beginning of the raw
879 static bpf_abs_offset off_prevlinkhdr
;
882 * This is the equivalent information for the outermost layers' link-layer
885 static bpf_abs_offset off_outermostlinkhdr
;
888 * "Push" the current value of the link-layer header type and link-layer
889 * header offset onto a "stack", and set a new value. (It's not a
890 * full-blown stack; we keep only the top two items.)
892 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
894 prevlinktype = new_linktype; \
895 off_prevlinkhdr = off_linkhdr; \
896 linktype = new_linktype; \
897 off_linkhdr.is_variable = new_is_variable; \
898 off_linkhdr.constant_part = new_constant_part; \
899 off_linkhdr.reg = new_reg; \
904 * Absolute offset of the beginning of the link-layer payload.
906 static bpf_abs_offset off_linkpl
;
909 * "off_linktype" is the offset to information in the link-layer header
910 * giving the packet type. This is an absolute offset from the beginning
913 * For Ethernet, it's the offset of the Ethernet type field; this
914 * means that it must have a value that skips VLAN tags.
916 * For link-layer types that always use 802.2 headers, it's the
917 * offset of the LLC header; this means that it must have a value
918 * that skips VLAN tags.
920 * For PPP, it's the offset of the PPP type field.
922 * For Cisco HDLC, it's the offset of the CHDLC type field.
924 * For BSD loopback, it's the offset of the AF_ value.
926 * For Linux cooked sockets, it's the offset of the type field.
928 * off_linktype.constant_part is set to -1 for no encapsulation,
929 * in which case, IP is assumed.
931 static bpf_abs_offset off_linktype
;
934 * TRUE if the link layer includes an ATM pseudo-header.
936 static int is_atm
= 0;
939 * TRUE if "geneve" appeared in the filter; it causes us to generate
940 * code that checks for a Geneve header and assume that later filters
941 * apply to the encapsulated payload.
943 static int is_geneve
= 0;
946 * These are offsets for the ATM pseudo-header.
948 static u_int off_vpi
;
949 static u_int off_vci
;
950 static u_int off_proto
;
953 * These are offsets for the MTP2 fields.
956 static u_int off_li_hsl
;
959 * These are offsets for the MTP3 fields.
961 static u_int off_sio
;
962 static u_int off_opc
;
963 static u_int off_dpc
;
964 static u_int off_sls
;
967 * This is the offset of the first byte after the ATM pseudo_header,
968 * or -1 if there is no ATM pseudo-header.
970 static u_int off_payload
;
973 * These are offsets to the beginning of the network-layer header.
974 * They are relative to the beginning of the link-layer payload (i.e.,
975 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
977 * If the link layer never uses 802.2 LLC:
979 * "off_nl" and "off_nl_nosnap" are the same.
981 * If the link layer always uses 802.2 LLC:
983 * "off_nl" is the offset if there's a SNAP header following
986 * "off_nl_nosnap" is the offset if there's no SNAP header.
988 * If the link layer is Ethernet:
990 * "off_nl" is the offset if the packet is an Ethernet II packet
991 * (we assume no 802.3+802.2+SNAP);
993 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
994 * with an 802.2 header following it.
997 static u_int off_nl_nosnap
;
1000 static int prevlinktype
;
1001 static int outermostlinktype
;
1007 pcap_fddipad
= p
->fddipad
;
1010 * We start out with only one link-layer header.
1012 outermostlinktype
= pcap_datalink(p
);
1013 off_outermostlinkhdr
.constant_part
= 0;
1014 off_outermostlinkhdr
.is_variable
= 0;
1015 off_outermostlinkhdr
.reg
= -1;
1017 prevlinktype
= outermostlinktype
;
1018 off_prevlinkhdr
.constant_part
= 0;
1019 off_prevlinkhdr
.is_variable
= 0;
1020 off_prevlinkhdr
.reg
= -1;
1022 linktype
= outermostlinktype
;
1023 off_linkhdr
.constant_part
= 0;
1024 off_linkhdr
.is_variable
= 0;
1025 off_linkhdr
.reg
= -1;
1030 off_linkpl
.constant_part
= 0;
1031 off_linkpl
.is_variable
= 0;
1032 off_linkpl
.reg
= -1;
1034 off_linktype
.constant_part
= 0;
1035 off_linktype
.is_variable
= 0;
1036 off_linktype
.reg
= -1;
1039 * Assume it's not raw ATM with a pseudo-header, for now.
1053 * And assume we're not doing SS7.
1062 label_stack_depth
= 0;
1063 vlan_stack_depth
= 0;
1068 off_linktype
.constant_part
= 2;
1069 off_linkpl
.constant_part
= 6;
1070 off_nl
= 0; /* XXX in reality, variable! */
1071 off_nl_nosnap
= 0; /* no 802.2 LLC */
1074 case DLT_ARCNET_LINUX
:
1075 off_linktype
.constant_part
= 4;
1076 off_linkpl
.constant_part
= 8;
1077 off_nl
= 0; /* XXX in reality, variable! */
1078 off_nl_nosnap
= 0; /* no 802.2 LLC */
1082 off_linktype
.constant_part
= 12;
1083 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1084 off_nl
= 0; /* Ethernet II */
1085 off_nl_nosnap
= 3; /* 802.3+802.2 */
1090 * SLIP doesn't have a link level type. The 16 byte
1091 * header is hacked into our SLIP driver.
1093 off_linktype
.constant_part
= -1;
1094 off_linkpl
.constant_part
= 16;
1096 off_nl_nosnap
= 0; /* no 802.2 LLC */
1099 case DLT_SLIP_BSDOS
:
1100 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1101 off_linktype
.constant_part
= -1;
1103 off_linkpl
.constant_part
= 24;
1105 off_nl_nosnap
= 0; /* no 802.2 LLC */
1110 off_linktype
.constant_part
= 0;
1111 off_linkpl
.constant_part
= 4;
1113 off_nl_nosnap
= 0; /* no 802.2 LLC */
1117 off_linktype
.constant_part
= 0;
1118 off_linkpl
.constant_part
= 12;
1120 off_nl_nosnap
= 0; /* no 802.2 LLC */
1125 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1126 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1127 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1128 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1130 off_nl_nosnap
= 0; /* no 802.2 LLC */
1135 * This does no include the Ethernet header, and
1136 * only covers session state.
1138 off_linktype
.constant_part
= 6;
1139 off_linkpl
.constant_part
= 8;
1141 off_nl_nosnap
= 0; /* no 802.2 LLC */
1145 off_linktype
.constant_part
= 5;
1146 off_linkpl
.constant_part
= 24;
1148 off_nl_nosnap
= 0; /* no 802.2 LLC */
1153 * FDDI doesn't really have a link-level type field.
1154 * We set "off_linktype" to the offset of the LLC header.
1156 * To check for Ethernet types, we assume that SSAP = SNAP
1157 * is being used and pick out the encapsulated Ethernet type.
1158 * XXX - should we generate code to check for SNAP?
1160 off_linktype
.constant_part
= 13;
1161 off_linktype
.constant_part
+= pcap_fddipad
;
1162 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1163 off_linkpl
.constant_part
+= pcap_fddipad
;
1164 off_nl
= 8; /* 802.2+SNAP */
1165 off_nl_nosnap
= 3; /* 802.2 */
1170 * Token Ring doesn't really have a link-level type field.
1171 * We set "off_linktype" to the offset of the LLC header.
1173 * To check for Ethernet types, we assume that SSAP = SNAP
1174 * is being used and pick out the encapsulated Ethernet type.
1175 * XXX - should we generate code to check for SNAP?
1177 * XXX - the header is actually variable-length.
1178 * Some various Linux patched versions gave 38
1179 * as "off_linktype" and 40 as "off_nl"; however,
1180 * if a token ring packet has *no* routing
1181 * information, i.e. is not source-routed, the correct
1182 * values are 20 and 22, as they are in the vanilla code.
1184 * A packet is source-routed iff the uppermost bit
1185 * of the first byte of the source address, at an
1186 * offset of 8, has the uppermost bit set. If the
1187 * packet is source-routed, the total number of bytes
1188 * of routing information is 2 plus bits 0x1F00 of
1189 * the 16-bit value at an offset of 14 (shifted right
1190 * 8 - figure out which byte that is).
1192 off_linktype
.constant_part
= 14;
1193 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1194 off_nl
= 8; /* 802.2+SNAP */
1195 off_nl_nosnap
= 3; /* 802.2 */
1198 case DLT_PRISM_HEADER
:
1199 case DLT_IEEE802_11_RADIO_AVS
:
1200 case DLT_IEEE802_11_RADIO
:
1201 off_linkhdr
.is_variable
= 1;
1202 /* Fall through, 802.11 doesn't have a variable link
1203 * prefix but is otherwise the same. */
1205 case DLT_IEEE802_11
:
1207 * 802.11 doesn't really have a link-level type field.
1208 * We set "off_linktype.constant_part" to the offset of
1211 * To check for Ethernet types, we assume that SSAP = SNAP
1212 * is being used and pick out the encapsulated Ethernet type.
1213 * XXX - should we generate code to check for SNAP?
1215 * We also handle variable-length radio headers here.
1216 * The Prism header is in theory variable-length, but in
1217 * practice it's always 144 bytes long. However, some
1218 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1219 * sometimes or always supply an AVS header, so we
1220 * have to check whether the radio header is a Prism
1221 * header or an AVS header, so, in practice, it's
1224 off_linktype
.constant_part
= 24;
1225 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1226 off_linkpl
.is_variable
= 1;
1227 off_nl
= 8; /* 802.2+SNAP */
1228 off_nl_nosnap
= 3; /* 802.2 */
1233 * At the moment we treat PPI the same way that we treat
1234 * normal Radiotap encoded packets. The difference is in
1235 * the function that generates the code at the beginning
1236 * to compute the header length. Since this code generator
1237 * of PPI supports bare 802.11 encapsulation only (i.e.
1238 * the encapsulated DLT should be DLT_IEEE802_11) we
1239 * generate code to check for this too.
1241 off_linktype
.constant_part
= 24;
1242 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1243 off_linkpl
.is_variable
= 1;
1244 off_linkhdr
.is_variable
= 1;
1245 off_nl
= 8; /* 802.2+SNAP */
1246 off_nl_nosnap
= 3; /* 802.2 */
1249 case DLT_ATM_RFC1483
:
1250 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1252 * assume routed, non-ISO PDUs
1253 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1255 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1256 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1257 * latter would presumably be treated the way PPPoE
1258 * should be, so you can do "pppoe and udp port 2049"
1259 * or "pppoa and tcp port 80" and have it check for
1260 * PPPo{A,E} and a PPP protocol of IP and....
1262 off_linktype
.constant_part
= 0;
1263 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1264 off_nl
= 8; /* 802.2+SNAP */
1265 off_nl_nosnap
= 3; /* 802.2 */
1270 * Full Frontal ATM; you get AALn PDUs with an ATM
1274 off_vpi
= SUNATM_VPI_POS
;
1275 off_vci
= SUNATM_VCI_POS
;
1276 off_proto
= PROTO_POS
;
1277 off_payload
= SUNATM_PKT_BEGIN_POS
;
1278 off_linktype
.constant_part
= off_payload
;
1279 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1280 off_nl
= 8; /* 802.2+SNAP */
1281 off_nl_nosnap
= 3; /* 802.2 */
1287 off_linktype
.constant_part
= -1;
1288 off_linkpl
.constant_part
= 0;
1290 off_nl_nosnap
= 0; /* no 802.2 LLC */
1293 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1294 off_linktype
.constant_part
= 14;
1295 off_linkpl
.constant_part
= 16;
1297 off_nl_nosnap
= 0; /* no 802.2 LLC */
1302 * LocalTalk does have a 1-byte type field in the LLAP header,
1303 * but really it just indicates whether there is a "short" or
1304 * "long" DDP packet following.
1306 off_linktype
.constant_part
= -1;
1307 off_linkpl
.constant_part
= 0;
1309 off_nl_nosnap
= 0; /* no 802.2 LLC */
1312 case DLT_IP_OVER_FC
:
1314 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1315 * link-level type field. We set "off_linktype" to the
1316 * offset of the LLC header.
1318 * To check for Ethernet types, we assume that SSAP = SNAP
1319 * is being used and pick out the encapsulated Ethernet type.
1320 * XXX - should we generate code to check for SNAP? RFC
1321 * 2625 says SNAP should be used.
1323 off_linktype
.constant_part
= 16;
1324 off_linkpl
.constant_part
= 16;
1325 off_nl
= 8; /* 802.2+SNAP */
1326 off_nl_nosnap
= 3; /* 802.2 */
1331 * XXX - we should set this to handle SNAP-encapsulated
1332 * frames (NLPID of 0x80).
1334 off_linktype
.constant_part
= -1;
1335 off_linkpl
.constant_part
= 0;
1337 off_nl_nosnap
= 0; /* no 802.2 LLC */
1341 * the only BPF-interesting FRF.16 frames are non-control frames;
1342 * Frame Relay has a variable length link-layer
1343 * so lets start with offset 4 for now and increments later on (FIXME);
1346 off_linktype
.constant_part
= -1;
1347 off_linkpl
.constant_part
= 0;
1349 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1352 case DLT_APPLE_IP_OVER_IEEE1394
:
1353 off_linktype
.constant_part
= 16;
1354 off_linkpl
.constant_part
= 18;
1356 off_nl_nosnap
= 0; /* no 802.2 LLC */
1359 case DLT_SYMANTEC_FIREWALL
:
1360 off_linktype
.constant_part
= 6;
1361 off_linkpl
.constant_part
= 44;
1362 off_nl
= 0; /* Ethernet II */
1363 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1366 #ifdef HAVE_NET_PFVAR_H
1368 off_linktype
.constant_part
= 0;
1369 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1371 off_nl_nosnap
= 0; /* no 802.2 LLC */
1375 case DLT_JUNIPER_MFR
:
1376 case DLT_JUNIPER_MLFR
:
1377 case DLT_JUNIPER_MLPPP
:
1378 case DLT_JUNIPER_PPP
:
1379 case DLT_JUNIPER_CHDLC
:
1380 case DLT_JUNIPER_FRELAY
:
1381 off_linktype
.constant_part
= 4;
1382 off_linkpl
.constant_part
= 4;
1384 off_nl_nosnap
= -1; /* no 802.2 LLC */
1387 case DLT_JUNIPER_ATM1
:
1388 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1389 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1394 case DLT_JUNIPER_ATM2
:
1395 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1396 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1401 /* frames captured on a Juniper PPPoE service PIC
1402 * contain raw ethernet frames */
1403 case DLT_JUNIPER_PPPOE
:
1404 case DLT_JUNIPER_ETHER
:
1405 off_linkpl
.constant_part
= 14;
1406 off_linktype
.constant_part
= 16;
1407 off_nl
= 18; /* Ethernet II */
1408 off_nl_nosnap
= 21; /* 802.3+802.2 */
1411 case DLT_JUNIPER_PPPOE_ATM
:
1412 off_linktype
.constant_part
= 4;
1413 off_linkpl
.constant_part
= 6;
1415 off_nl_nosnap
= -1; /* no 802.2 LLC */
1418 case DLT_JUNIPER_GGSN
:
1419 off_linktype
.constant_part
= 6;
1420 off_linkpl
.constant_part
= 12;
1422 off_nl_nosnap
= -1; /* no 802.2 LLC */
1425 case DLT_JUNIPER_ES
:
1426 off_linktype
.constant_part
= 6;
1427 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1428 off_nl
= -1; /* not really a network layer but raw IP addresses */
1429 off_nl_nosnap
= -1; /* no 802.2 LLC */
1432 case DLT_JUNIPER_MONITOR
:
1433 off_linktype
.constant_part
= 12;
1434 off_linkpl
.constant_part
= 12;
1435 off_nl
= 0; /* raw IP/IP6 header */
1436 off_nl_nosnap
= -1; /* no 802.2 LLC */
1439 case DLT_BACNET_MS_TP
:
1440 off_linktype
.constant_part
= -1;
1441 off_linkpl
.constant_part
= -1;
1446 case DLT_JUNIPER_SERVICES
:
1447 off_linktype
.constant_part
= 12;
1448 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1449 off_nl
= -1; /* L3 proto location dep. on cookie type */
1450 off_nl_nosnap
= -1; /* no 802.2 LLC */
1453 case DLT_JUNIPER_VP
:
1454 off_linktype
.constant_part
= 18;
1455 off_linkpl
.constant_part
= -1;
1460 case DLT_JUNIPER_ST
:
1461 off_linktype
.constant_part
= 18;
1462 off_linkpl
.constant_part
= -1;
1467 case DLT_JUNIPER_ISM
:
1468 off_linktype
.constant_part
= 8;
1469 off_linkpl
.constant_part
= -1;
1474 case DLT_JUNIPER_VS
:
1475 case DLT_JUNIPER_SRX_E2E
:
1476 case DLT_JUNIPER_FIBRECHANNEL
:
1477 case DLT_JUNIPER_ATM_CEMIC
:
1478 off_linktype
.constant_part
= 8;
1479 off_linkpl
.constant_part
= -1;
1491 off_linktype
.constant_part
= -1;
1492 off_linkpl
.constant_part
= -1;
1497 case DLT_MTP2_WITH_PHDR
:
1504 off_linktype
.constant_part
= -1;
1505 off_linkpl
.constant_part
= -1;
1517 off_linktype
.constant_part
= -1;
1518 off_linkpl
.constant_part
= -1;
1524 off_linktype
.constant_part
= -1;
1525 off_linkpl
.constant_part
= 4;
1532 * Currently, only raw "link[N:M]" filtering is supported.
1534 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1535 off_linkpl
.constant_part
= -1;
1536 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1537 off_nl_nosnap
= -1; /* no 802.2 LLC */
1541 off_linktype
.constant_part
= 1;
1542 off_linkpl
.constant_part
= 24; /* ipnet header length */
1547 case DLT_NETANALYZER
:
1548 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1549 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1550 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1551 off_nl
= 0; /* Ethernet II */
1552 off_nl_nosnap
= 3; /* 802.3+802.2 */
1555 case DLT_NETANALYZER_TRANSPARENT
:
1556 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1557 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1558 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1559 off_nl
= 0; /* Ethernet II */
1560 off_nl_nosnap
= 3; /* 802.3+802.2 */
1565 * For values in the range in which we've assigned new
1566 * DLT_ values, only raw "link[N:M]" filtering is supported.
1568 if (linktype
>= DLT_MATCHING_MIN
&&
1569 linktype
<= DLT_MATCHING_MAX
) {
1570 off_linktype
.constant_part
= -1;
1571 off_linkpl
.constant_part
= -1;
1575 bpf_error("unknown data link type %d", linktype
);
1580 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1584 * Load a value relative to the specified absolute offset.
1586 static struct slist
*
1587 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1589 struct slist
*s
, *s2
;
1591 s
= gen_abs_offset_varpart(abs_offset
);
1594 * If "s" is non-null, it has code to arrange that the X register
1595 * contains the variable part of the absolute offset, so we
1596 * generate a load relative to that, with an offset of
1597 * abs_offset->constant_part + offset.
1599 * Otherwise, we can do an absolute load with an offset of
1600 * abs_offset->constant_part + offset.
1604 * "s" points to a list of statements that puts the
1605 * variable part of the absolute offset into the X register.
1606 * Do an indirect load, to use the X register as an offset.
1608 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1609 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1613 * There is no variable part of the absolute offset, so
1614 * just do an absolute load.
1616 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1617 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1623 * Load a value relative to the beginning of the specified header.
1625 static struct slist
*
1626 gen_load_a(offrel
, offset
, size
)
1627 enum e_offrel offrel
;
1630 struct slist
*s
, *s2
;
1635 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1640 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1643 case OR_PREVLINKHDR
:
1644 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1648 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1651 case OR_PREVMPLSHDR
:
1652 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1656 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1659 case OR_LINKPL_NOSNAP
:
1660 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1664 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1669 * Load the X register with the length of the IPv4 header
1670 * (plus the offset of the link-layer header, if it's
1671 * preceded by a variable-length header such as a radio
1672 * header), in bytes.
1674 s
= gen_loadx_iphdrlen();
1677 * Load the item at {offset of the link-layer payload} +
1678 * {offset, relative to the start of the link-layer
1679 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1680 * {specified offset}.
1682 * If the offset of the link-layer payload is variable,
1683 * the variable part of that offset is included in the
1684 * value in the X register, and we include the constant
1685 * part in the offset of the load.
1687 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1688 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1693 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1704 * Generate code to load into the X register the sum of the length of
1705 * the IPv4 header and the variable part of the offset of the link-layer
1708 static struct slist
*
1709 gen_loadx_iphdrlen()
1711 struct slist
*s
, *s2
;
1713 s
= gen_abs_offset_varpart(&off_linkpl
);
1716 * The offset of the link-layer payload has a variable
1717 * part. "s" points to a list of statements that put
1718 * the variable part of that offset into the X register.
1720 * The 4*([k]&0xf) addressing mode can't be used, as we
1721 * don't have a constant offset, so we have to load the
1722 * value in question into the A register and add to it
1723 * the value from the X register.
1725 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1726 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1728 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1731 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1736 * The A register now contains the length of the IP header.
1737 * We need to add to it the variable part of the offset of
1738 * the link-layer payload, which is still in the X
1739 * register, and move the result into the X register.
1741 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1742 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1745 * The offset of the link-layer payload is a constant,
1746 * so no code was generated to load the (non-existent)
1747 * variable part of that offset.
1749 * This means we can use the 4*([k]&0xf) addressing
1750 * mode. Load the length of the IPv4 header, which
1751 * is at an offset of off_nl from the beginning of
1752 * the link-layer payload, and thus at an offset of
1753 * off_linkpl.constant_part + off_nl from the beginning
1754 * of the raw packet data, using that addressing mode.
1756 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1757 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1762 static struct block
*
1769 s
= new_stmt(BPF_LD
|BPF_IMM
);
1771 b
= new_block(JMP(BPF_JEQ
));
1777 static inline struct block
*
1780 return gen_uncond(1);
1783 static inline struct block
*
1786 return gen_uncond(0);
1790 * Byte-swap a 32-bit number.
1791 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1792 * big-endian platforms.)
1794 #define SWAPLONG(y) \
1795 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1798 * Generate code to match a particular packet type.
1800 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1801 * value, if <= ETHERMTU. We use that to determine whether to
1802 * match the type/length field or to check the type/length field for
1803 * a value <= ETHERMTU to see whether it's a type field and then do
1804 * the appropriate test.
1806 static struct block
*
1807 gen_ether_linktype(proto
)
1810 struct block
*b0
, *b1
;
1816 case LLCSAP_NETBEUI
:
1818 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1819 * so we check the DSAP and SSAP.
1821 * LLCSAP_IP checks for IP-over-802.2, rather
1822 * than IP-over-Ethernet or IP-over-SNAP.
1824 * XXX - should we check both the DSAP and the
1825 * SSAP, like this, or should we check just the
1826 * DSAP, as we do for other types <= ETHERMTU
1827 * (i.e., other SAP values)?
1829 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1831 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1832 ((proto
<< 8) | proto
));
1840 * Ethernet_II frames, which are Ethernet
1841 * frames with a frame type of ETHERTYPE_IPX;
1843 * Ethernet_802.3 frames, which are 802.3
1844 * frames (i.e., the type/length field is
1845 * a length field, <= ETHERMTU, rather than
1846 * a type field) with the first two bytes
1847 * after the Ethernet/802.3 header being
1850 * Ethernet_802.2 frames, which are 802.3
1851 * frames with an 802.2 LLC header and
1852 * with the IPX LSAP as the DSAP in the LLC
1855 * Ethernet_SNAP frames, which are 802.3
1856 * frames with an LLC header and a SNAP
1857 * header and with an OUI of 0x000000
1858 * (encapsulated Ethernet) and a protocol
1859 * ID of ETHERTYPE_IPX in the SNAP header.
1861 * XXX - should we generate the same code both
1862 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1866 * This generates code to check both for the
1867 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1869 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1870 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1874 * Now we add code to check for SNAP frames with
1875 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1877 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1881 * Now we generate code to check for 802.3
1882 * frames in general.
1884 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1888 * Now add the check for 802.3 frames before the
1889 * check for Ethernet_802.2 and Ethernet_802.3,
1890 * as those checks should only be done on 802.3
1891 * frames, not on Ethernet frames.
1896 * Now add the check for Ethernet_II frames, and
1897 * do that before checking for the other frame
1900 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1904 case ETHERTYPE_ATALK
:
1905 case ETHERTYPE_AARP
:
1907 * EtherTalk (AppleTalk protocols on Ethernet link
1908 * layer) may use 802.2 encapsulation.
1912 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1913 * we check for an Ethernet type field less than
1914 * 1500, which means it's an 802.3 length field.
1916 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1920 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1921 * SNAP packets with an organization code of
1922 * 0x080007 (Apple, for Appletalk) and a protocol
1923 * type of ETHERTYPE_ATALK (Appletalk).
1925 * 802.2-encapsulated ETHERTYPE_AARP packets are
1926 * SNAP packets with an organization code of
1927 * 0x000000 (encapsulated Ethernet) and a protocol
1928 * type of ETHERTYPE_AARP (Appletalk ARP).
1930 if (proto
== ETHERTYPE_ATALK
)
1931 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1932 else /* proto == ETHERTYPE_AARP */
1933 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1937 * Check for Ethernet encapsulation (Ethertalk
1938 * phase 1?); we just check for the Ethernet
1941 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1947 if (proto
<= ETHERMTU
) {
1949 * This is an LLC SAP value, so the frames
1950 * that match would be 802.2 frames.
1951 * Check that the frame is an 802.2 frame
1952 * (i.e., that the length/type field is
1953 * a length field, <= ETHERMTU) and
1954 * then check the DSAP.
1956 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1958 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1963 * This is an Ethernet type, so compare
1964 * the length/type field with it (if
1965 * the frame is an 802.2 frame, the length
1966 * field will be <= ETHERMTU, and, as
1967 * "proto" is > ETHERMTU, this test
1968 * will fail and the frame won't match,
1969 * which is what we want).
1971 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1978 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1979 * or IPv6 then we have an error.
1981 static struct block
*
1982 gen_ipnet_linktype(proto
)
1988 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1991 case ETHERTYPE_IPV6
:
1992 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1993 (bpf_int32
)IPH_AF_INET6
);
2004 * Generate code to match a particular packet type.
2006 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2007 * value, if <= ETHERMTU. We use that to determine whether to
2008 * match the type field or to check the type field for the special
2009 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2011 static struct block
*
2012 gen_linux_sll_linktype(proto
)
2015 struct block
*b0
, *b1
;
2021 case LLCSAP_NETBEUI
:
2023 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2024 * so we check the DSAP and SSAP.
2026 * LLCSAP_IP checks for IP-over-802.2, rather
2027 * than IP-over-Ethernet or IP-over-SNAP.
2029 * XXX - should we check both the DSAP and the
2030 * SSAP, like this, or should we check just the
2031 * DSAP, as we do for other types <= ETHERMTU
2032 * (i.e., other SAP values)?
2034 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2035 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
2036 ((proto
<< 8) | proto
));
2042 * Ethernet_II frames, which are Ethernet
2043 * frames with a frame type of ETHERTYPE_IPX;
2045 * Ethernet_802.3 frames, which have a frame
2046 * type of LINUX_SLL_P_802_3;
2048 * Ethernet_802.2 frames, which are 802.3
2049 * frames with an 802.2 LLC header (i.e, have
2050 * a frame type of LINUX_SLL_P_802_2) and
2051 * with the IPX LSAP as the DSAP in the LLC
2054 * Ethernet_SNAP frames, which are 802.3
2055 * frames with an LLC header and a SNAP
2056 * header and with an OUI of 0x000000
2057 * (encapsulated Ethernet) and a protocol
2058 * ID of ETHERTYPE_IPX in the SNAP header.
2060 * First, do the checks on LINUX_SLL_P_802_2
2061 * frames; generate the check for either
2062 * Ethernet_802.2 or Ethernet_SNAP frames, and
2063 * then put a check for LINUX_SLL_P_802_2 frames
2066 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2067 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2069 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2073 * Now check for 802.3 frames and OR that with
2074 * the previous test.
2076 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2080 * Now add the check for Ethernet_II frames, and
2081 * do that before checking for the other frame
2084 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2088 case ETHERTYPE_ATALK
:
2089 case ETHERTYPE_AARP
:
2091 * EtherTalk (AppleTalk protocols on Ethernet link
2092 * layer) may use 802.2 encapsulation.
2096 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2097 * we check for the 802.2 protocol type in the
2098 * "Ethernet type" field.
2100 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2103 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2104 * SNAP packets with an organization code of
2105 * 0x080007 (Apple, for Appletalk) and a protocol
2106 * type of ETHERTYPE_ATALK (Appletalk).
2108 * 802.2-encapsulated ETHERTYPE_AARP packets are
2109 * SNAP packets with an organization code of
2110 * 0x000000 (encapsulated Ethernet) and a protocol
2111 * type of ETHERTYPE_AARP (Appletalk ARP).
2113 if (proto
== ETHERTYPE_ATALK
)
2114 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2115 else /* proto == ETHERTYPE_AARP */
2116 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2120 * Check for Ethernet encapsulation (Ethertalk
2121 * phase 1?); we just check for the Ethernet
2124 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2130 if (proto
<= ETHERMTU
) {
2132 * This is an LLC SAP value, so the frames
2133 * that match would be 802.2 frames.
2134 * Check for the 802.2 protocol type
2135 * in the "Ethernet type" field, and
2136 * then check the DSAP.
2138 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2139 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2145 * This is an Ethernet type, so compare
2146 * the length/type field with it (if
2147 * the frame is an 802.2 frame, the length
2148 * field will be <= ETHERMTU, and, as
2149 * "proto" is > ETHERMTU, this test
2150 * will fail and the frame won't match,
2151 * which is what we want).
2153 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2158 static struct slist
*
2159 gen_load_prism_llprefixlen()
2161 struct slist
*s1
, *s2
;
2162 struct slist
*sjeq_avs_cookie
;
2163 struct slist
*sjcommon
;
2166 * This code is not compatible with the optimizer, as
2167 * we are generating jmp instructions within a normal
2168 * slist of instructions
2173 * Generate code to load the length of the radio header into
2174 * the register assigned to hold that length, if one has been
2175 * assigned. (If one hasn't been assigned, no code we've
2176 * generated uses that prefix, so we don't need to generate any
2179 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2180 * or always use the AVS header rather than the Prism header.
2181 * We load a 4-byte big-endian value at the beginning of the
2182 * raw packet data, and see whether, when masked with 0xFFFFF000,
2183 * it's equal to 0x80211000. If so, that indicates that it's
2184 * an AVS header (the masked-out bits are the version number).
2185 * Otherwise, it's a Prism header.
2187 * XXX - the Prism header is also, in theory, variable-length,
2188 * but no known software generates headers that aren't 144
2191 if (off_linkhdr
.reg
!= -1) {
2195 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2199 * AND it with 0xFFFFF000.
2201 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2202 s2
->s
.k
= 0xFFFFF000;
2206 * Compare with 0x80211000.
2208 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2209 sjeq_avs_cookie
->s
.k
= 0x80211000;
2210 sappend(s1
, sjeq_avs_cookie
);
2215 * The 4 bytes at an offset of 4 from the beginning of
2216 * the AVS header are the length of the AVS header.
2217 * That field is big-endian.
2219 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2222 sjeq_avs_cookie
->s
.jt
= s2
;
2225 * Now jump to the code to allocate a register
2226 * into which to save the header length and
2227 * store the length there. (The "jump always"
2228 * instruction needs to have the k field set;
2229 * it's added to the PC, so, as we're jumping
2230 * over a single instruction, it should be 1.)
2232 sjcommon
= new_stmt(JMP(BPF_JA
));
2234 sappend(s1
, sjcommon
);
2237 * Now for the code that handles the Prism header.
2238 * Just load the length of the Prism header (144)
2239 * into the A register. Have the test for an AVS
2240 * header branch here if we don't have an AVS header.
2242 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2245 sjeq_avs_cookie
->s
.jf
= s2
;
2248 * Now allocate a register to hold that value and store
2249 * it. The code for the AVS header will jump here after
2250 * loading the length of the AVS header.
2252 s2
= new_stmt(BPF_ST
);
2253 s2
->s
.k
= off_linkhdr
.reg
;
2255 sjcommon
->s
.jf
= s2
;
2258 * Now move it into the X register.
2260 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2268 static struct slist
*
2269 gen_load_avs_llprefixlen()
2271 struct slist
*s1
, *s2
;
2274 * Generate code to load the length of the AVS header into
2275 * the register assigned to hold that length, if one has been
2276 * assigned. (If one hasn't been assigned, no code we've
2277 * generated uses that prefix, so we don't need to generate any
2280 if (off_linkhdr
.reg
!= -1) {
2282 * The 4 bytes at an offset of 4 from the beginning of
2283 * the AVS header are the length of the AVS header.
2284 * That field is big-endian.
2286 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2290 * Now allocate a register to hold that value and store
2293 s2
= new_stmt(BPF_ST
);
2294 s2
->s
.k
= off_linkhdr
.reg
;
2298 * Now move it into the X register.
2300 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2308 static struct slist
*
2309 gen_load_radiotap_llprefixlen()
2311 struct slist
*s1
, *s2
;
2314 * Generate code to load the length of the radiotap header into
2315 * the register assigned to hold that length, if one has been
2316 * assigned. (If one hasn't been assigned, no code we've
2317 * generated uses that prefix, so we don't need to generate any
2320 if (off_linkhdr
.reg
!= -1) {
2322 * The 2 bytes at offsets of 2 and 3 from the beginning
2323 * of the radiotap header are the length of the radiotap
2324 * header; unfortunately, it's little-endian, so we have
2325 * to load it a byte at a time and construct the value.
2329 * Load the high-order byte, at an offset of 3, shift it
2330 * left a byte, and put the result in the X register.
2332 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2334 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2337 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2341 * Load the next byte, at an offset of 2, and OR the
2342 * value from the X register into it.
2344 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2347 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2351 * Now allocate a register to hold that value and store
2354 s2
= new_stmt(BPF_ST
);
2355 s2
->s
.k
= off_linkhdr
.reg
;
2359 * Now move it into the X register.
2361 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2370 * At the moment we treat PPI as normal Radiotap encoded
2371 * packets. The difference is in the function that generates
2372 * the code at the beginning to compute the header length.
2373 * Since this code generator of PPI supports bare 802.11
2374 * encapsulation only (i.e. the encapsulated DLT should be
2375 * DLT_IEEE802_11) we generate code to check for this too;
2376 * that's done in finish_parse().
2378 static struct slist
*
2379 gen_load_ppi_llprefixlen()
2381 struct slist
*s1
, *s2
;
2384 * Generate code to load the length of the radiotap header
2385 * into the register assigned to hold that length, if one has
2388 if (off_linkhdr
.reg
!= -1) {
2390 * The 2 bytes at offsets of 2 and 3 from the beginning
2391 * of the radiotap header are the length of the radiotap
2392 * header; unfortunately, it's little-endian, so we have
2393 * to load it a byte at a time and construct the value.
2397 * Load the high-order byte, at an offset of 3, shift it
2398 * left a byte, and put the result in the X register.
2400 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2402 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2405 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2409 * Load the next byte, at an offset of 2, and OR the
2410 * value from the X register into it.
2412 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2415 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2419 * Now allocate a register to hold that value and store
2422 s2
= new_stmt(BPF_ST
);
2423 s2
->s
.k
= off_linkhdr
.reg
;
2427 * Now move it into the X register.
2429 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2438 * Load a value relative to the beginning of the link-layer header after the 802.11
2439 * header, i.e. LLC_SNAP.
2440 * The link-layer header doesn't necessarily begin at the beginning
2441 * of the packet data; there might be a variable-length prefix containing
2442 * radio information.
2444 static struct slist
*
2445 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2448 struct slist
*sjset_data_frame_1
;
2449 struct slist
*sjset_data_frame_2
;
2450 struct slist
*sjset_qos
;
2451 struct slist
*sjset_radiotap_flags_present
;
2452 struct slist
*sjset_radiotap_ext_present
;
2453 struct slist
*sjset_radiotap_tsft_present
;
2454 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2455 struct slist
*s_roundup
;
2457 if (off_linkpl
.reg
== -1) {
2459 * No register has been assigned to the offset of
2460 * the link-layer payload, which means nobody needs
2461 * it; don't bother computing it - just return
2462 * what we already have.
2468 * This code is not compatible with the optimizer, as
2469 * we are generating jmp instructions within a normal
2470 * slist of instructions
2475 * If "s" is non-null, it has code to arrange that the X register
2476 * contains the length of the prefix preceding the link-layer
2479 * Otherwise, the length of the prefix preceding the link-layer
2480 * header is "off_outermostlinkhdr.constant_part".
2484 * There is no variable-length header preceding the
2485 * link-layer header.
2487 * Load the length of the fixed-length prefix preceding
2488 * the link-layer header (if any) into the X register,
2489 * and store it in the off_linkpl.reg register.
2490 * That length is off_outermostlinkhdr.constant_part.
2492 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2493 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2497 * The X register contains the offset of the beginning of the
2498 * link-layer header; add 24, which is the minimum length
2499 * of the MAC header for a data frame, to that, and store it
2500 * in off_linkpl.reg, and then load the Frame Control field,
2501 * which is at the offset in the X register, with an indexed load.
2503 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2505 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2508 s2
= new_stmt(BPF_ST
);
2509 s2
->s
.k
= off_linkpl
.reg
;
2512 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2517 * Check the Frame Control field to see if this is a data frame;
2518 * a data frame has the 0x08 bit (b3) in that field set and the
2519 * 0x04 bit (b2) clear.
2521 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2522 sjset_data_frame_1
->s
.k
= 0x08;
2523 sappend(s
, sjset_data_frame_1
);
2526 * If b3 is set, test b2, otherwise go to the first statement of
2527 * the rest of the program.
2529 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2530 sjset_data_frame_2
->s
.k
= 0x04;
2531 sappend(s
, sjset_data_frame_2
);
2532 sjset_data_frame_1
->s
.jf
= snext
;
2535 * If b2 is not set, this is a data frame; test the QoS bit.
2536 * Otherwise, go to the first statement of the rest of the
2539 sjset_data_frame_2
->s
.jt
= snext
;
2540 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2541 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2542 sappend(s
, sjset_qos
);
2545 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2547 * Otherwise, go to the first statement of the rest of the
2550 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2551 s2
->s
.k
= off_linkpl
.reg
;
2553 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2556 s2
= new_stmt(BPF_ST
);
2557 s2
->s
.k
= off_linkpl
.reg
;
2561 * If we have a radiotap header, look at it to see whether
2562 * there's Atheros padding between the MAC-layer header
2565 * Note: all of the fields in the radiotap header are
2566 * little-endian, so we byte-swap all of the values
2567 * we test against, as they will be loaded as big-endian
2570 * XXX - in the general case, we would have to scan through
2571 * *all* the presence bits, if there's more than one word of
2572 * presence bits. That would require a loop, meaning that
2573 * we wouldn't be able to run the filter in the kernel.
2575 * We assume here that the Atheros adapters that insert the
2576 * annoying padding don't have multiple antennae and therefore
2577 * do not generate radiotap headers with multiple presence words.
2579 if (linktype
== DLT_IEEE802_11_RADIO
) {
2581 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2582 * in the first presence flag word?
2584 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2588 sjset_radiotap_flags_present
= new_stmt(JMP(BPF_JSET
));
2589 sjset_radiotap_flags_present
->s
.k
= SWAPLONG(0x00000002);
2590 sappend(s
, sjset_radiotap_flags_present
);
2593 * If not, skip all of this.
2595 sjset_radiotap_flags_present
->s
.jf
= snext
;
2598 * Otherwise, is the "extension" bit set in that word?
2600 sjset_radiotap_ext_present
= new_stmt(JMP(BPF_JSET
));
2601 sjset_radiotap_ext_present
->s
.k
= SWAPLONG(0x80000000);
2602 sappend(s
, sjset_radiotap_ext_present
);
2603 sjset_radiotap_flags_present
->s
.jt
= sjset_radiotap_ext_present
;
2606 * If so, skip all of this.
2608 sjset_radiotap_ext_present
->s
.jt
= snext
;
2611 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2613 sjset_radiotap_tsft_present
= new_stmt(JMP(BPF_JSET
));
2614 sjset_radiotap_tsft_present
->s
.k
= SWAPLONG(0x00000001);
2615 sappend(s
, sjset_radiotap_tsft_present
);
2616 sjset_radiotap_ext_present
->s
.jf
= sjset_radiotap_tsft_present
;
2619 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2620 * at an offset of 16 from the beginning of the raw packet
2621 * data (8 bytes for the radiotap header and 8 bytes for
2624 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2627 s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2630 sjset_radiotap_tsft_present
->s
.jt
= s2
;
2632 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2633 sjset_tsft_datapad
->s
.k
= 0x20;
2634 sappend(s
, sjset_tsft_datapad
);
2637 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2638 * at an offset of 8 from the beginning of the raw packet
2639 * data (8 bytes for the radiotap header).
2641 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2644 s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2647 sjset_radiotap_tsft_present
->s
.jf
= s2
;
2649 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2650 sjset_notsft_datapad
->s
.k
= 0x20;
2651 sappend(s
, sjset_notsft_datapad
);
2654 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2655 * set, round the length of the 802.11 header to
2656 * a multiple of 4. Do that by adding 3 and then
2657 * dividing by and multiplying by 4, which we do by
2660 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2661 s_roundup
->s
.k
= off_linkpl
.reg
;
2662 sappend(s
, s_roundup
);
2663 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2666 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2669 s2
= new_stmt(BPF_ST
);
2670 s2
->s
.k
= off_linkpl
.reg
;
2673 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2674 sjset_tsft_datapad
->s
.jf
= snext
;
2675 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2676 sjset_notsft_datapad
->s
.jf
= snext
;
2678 sjset_qos
->s
.jf
= snext
;
2684 insert_compute_vloffsets(b
)
2689 /* There is an implicit dependency between the link
2690 * payload and link header since the payload computation
2691 * includes the variable part of the header. Therefore,
2692 * if nobody else has allocated a register for the link
2693 * header and we need it, do it now. */
2694 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2695 off_linkhdr
.reg
== -1)
2696 off_linkhdr
.reg
= alloc_reg();
2699 * For link-layer types that have a variable-length header
2700 * preceding the link-layer header, generate code to load
2701 * the offset of the link-layer header into the register
2702 * assigned to that offset, if any.
2704 * XXX - this, and the next switch statement, won't handle
2705 * encapsulation of 802.11 or 802.11+radio information in
2706 * some other protocol stack. That's significantly more
2709 switch (outermostlinktype
) {
2711 case DLT_PRISM_HEADER
:
2712 s
= gen_load_prism_llprefixlen();
2715 case DLT_IEEE802_11_RADIO_AVS
:
2716 s
= gen_load_avs_llprefixlen();
2719 case DLT_IEEE802_11_RADIO
:
2720 s
= gen_load_radiotap_llprefixlen();
2724 s
= gen_load_ppi_llprefixlen();
2733 * For link-layer types that have a variable-length link-layer
2734 * header, generate code to load the offset of the link-layer
2735 * payload into the register assigned to that offset, if any.
2737 switch (outermostlinktype
) {
2739 case DLT_IEEE802_11
:
2740 case DLT_PRISM_HEADER
:
2741 case DLT_IEEE802_11_RADIO_AVS
:
2742 case DLT_IEEE802_11_RADIO
:
2744 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2749 * If we have any offset-loading code, append all the
2750 * existing statements in the block to those statements,
2751 * and make the resulting list the list of statements
2755 sappend(s
, b
->stmts
);
2760 static struct block
*
2761 gen_ppi_dlt_check(void)
2763 struct slist
*s_load_dlt
;
2766 if (linktype
== DLT_PPI
)
2768 /* Create the statements that check for the DLT
2770 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2771 s_load_dlt
->s
.k
= 4;
2773 b
= new_block(JMP(BPF_JEQ
));
2775 b
->stmts
= s_load_dlt
;
2776 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2787 * Take an absolute offset, and:
2789 * if it has no variable part, return NULL;
2791 * if it has a variable part, generate code to load the register
2792 * containing that variable part into the X register, returning
2793 * a pointer to that code - if no register for that offset has
2794 * been allocated, allocate it first.
2796 * (The code to set that register will be generated later, but will
2797 * be placed earlier in the code sequence.)
2799 static struct slist
*
2800 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2804 if (off
->is_variable
) {
2805 if (off
->reg
== -1) {
2807 * We haven't yet assigned a register for the
2808 * variable part of the offset of the link-layer
2809 * header; allocate one.
2811 off
->reg
= alloc_reg();
2815 * Load the register containing the variable part of the
2816 * offset of the link-layer header into the X register.
2818 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2823 * That offset isn't variable, there's no variable part,
2824 * so we don't need to generate any code.
2831 * Map an Ethernet type to the equivalent PPP type.
2834 ethertype_to_ppptype(proto
)
2843 case ETHERTYPE_IPV6
:
2851 case ETHERTYPE_ATALK
:
2865 * I'm assuming the "Bridging PDU"s that go
2866 * over PPP are Spanning Tree Protocol
2880 * Generate any tests that, for encapsulation of a link-layer packet
2881 * inside another protocol stack, need to be done to check for those
2882 * link-layer packets (and that haven't already been done by a check
2883 * for that encapsulation).
2885 static struct block
*
2886 gen_prevlinkhdr_check(void)
2891 return gen_geneve_ll_check();
2893 switch (prevlinktype
) {
2897 * This is LANE-encapsulated Ethernet; check that the LANE
2898 * packet doesn't begin with an LE Control marker, i.e.
2899 * that it's data, not a control message.
2901 * (We've already generated a test for LANE.)
2903 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2909 * No such tests are necessary.
2917 * Generate code to match a particular packet type by matching the
2918 * link-layer type field or fields in the 802.2 LLC header.
2920 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2921 * value, if <= ETHERMTU.
2923 static struct block
*
2927 struct block
*b0
, *b1
, *b2
;
2928 const char *description
;
2930 /* are we checking MPLS-encapsulated packets? */
2931 if (label_stack_depth
> 0) {
2935 /* FIXME add other L3 proto IDs */
2936 return gen_mpls_linktype(Q_IP
);
2938 case ETHERTYPE_IPV6
:
2940 /* FIXME add other L3 proto IDs */
2941 return gen_mpls_linktype(Q_IPV6
);
2944 bpf_error("unsupported protocol over mpls");
2952 case DLT_NETANALYZER
:
2953 case DLT_NETANALYZER_TRANSPARENT
:
2954 /* Geneve has an EtherType regardless of whether there is an
2957 b0
= gen_prevlinkhdr_check();
2961 b1
= gen_ether_linktype(proto
);
2972 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2976 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2982 case DLT_IEEE802_11
:
2983 case DLT_PRISM_HEADER
:
2984 case DLT_IEEE802_11_RADIO_AVS
:
2985 case DLT_IEEE802_11_RADIO
:
2988 * Check that we have a data frame.
2990 b0
= gen_check_802_11_data_frame();
2993 * Now check for the specified link-layer type.
2995 b1
= gen_llc_linktype(proto
);
3003 * XXX - check for LLC frames.
3005 return gen_llc_linktype(proto
);
3011 * XXX - check for LLC PDUs, as per IEEE 802.5.
3013 return gen_llc_linktype(proto
);
3017 case DLT_ATM_RFC1483
:
3019 case DLT_IP_OVER_FC
:
3020 return gen_llc_linktype(proto
);
3026 * Check for an LLC-encapsulated version of this protocol;
3027 * if we were checking for LANE, linktype would no longer
3030 * Check for LLC encapsulation and then check the protocol.
3032 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3033 b1
= gen_llc_linktype(proto
);
3040 return gen_linux_sll_linktype(proto
);
3045 case DLT_SLIP_BSDOS
:
3048 * These types don't provide any type field; packets
3049 * are always IPv4 or IPv6.
3051 * XXX - for IPv4, check for a version number of 4, and,
3052 * for IPv6, check for a version number of 6?
3057 /* Check for a version number of 4. */
3058 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3060 case ETHERTYPE_IPV6
:
3061 /* Check for a version number of 6. */
3062 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3065 return gen_false(); /* always false */
3072 * Raw IPv4, so no type field.
3074 if (proto
== ETHERTYPE_IP
)
3075 return gen_true(); /* always true */
3077 /* Checking for something other than IPv4; always false */
3084 * Raw IPv6, so no type field.
3086 if (proto
== ETHERTYPE_IPV6
)
3087 return gen_true(); /* always true */
3089 /* Checking for something other than IPv6; always false */
3096 case DLT_PPP_SERIAL
:
3099 * We use Ethernet protocol types inside libpcap;
3100 * map them to the corresponding PPP protocol types.
3102 proto
= ethertype_to_ppptype(proto
);
3103 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3109 * We use Ethernet protocol types inside libpcap;
3110 * map them to the corresponding PPP protocol types.
3116 * Also check for Van Jacobson-compressed IP.
3117 * XXX - do this for other forms of PPP?
3119 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3120 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3122 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3127 proto
= ethertype_to_ppptype(proto
);
3128 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3138 * For DLT_NULL, the link-layer header is a 32-bit
3139 * word containing an AF_ value in *host* byte order,
3140 * and for DLT_ENC, the link-layer header begins
3141 * with a 32-bit work containing an AF_ value in
3144 * In addition, if we're reading a saved capture file,
3145 * the host byte order in the capture may not be the
3146 * same as the host byte order on this machine.
3148 * For DLT_LOOP, the link-layer header is a 32-bit
3149 * word containing an AF_ value in *network* byte order.
3151 * XXX - AF_ values may, unfortunately, be platform-
3152 * dependent; for example, FreeBSD's AF_INET6 is 24
3153 * whilst NetBSD's and OpenBSD's is 26.
3155 * This means that, when reading a capture file, just
3156 * checking for our AF_INET6 value won't work if the
3157 * capture file came from another OS.
3166 case ETHERTYPE_IPV6
:
3173 * Not a type on which we support filtering.
3174 * XXX - support those that have AF_ values
3175 * #defined on this platform, at least?
3180 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3182 * The AF_ value is in host byte order, but
3183 * the BPF interpreter will convert it to
3184 * network byte order.
3186 * If this is a save file, and it's from a
3187 * machine with the opposite byte order to
3188 * ours, we byte-swap the AF_ value.
3190 * Then we run it through "htonl()", and
3191 * generate code to compare against the result.
3193 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3194 proto
= SWAPLONG(proto
);
3195 proto
= htonl(proto
);
3197 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3199 #ifdef HAVE_NET_PFVAR_H
3202 * af field is host byte order in contrast to the rest of
3205 if (proto
== ETHERTYPE_IP
)
3206 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3207 BPF_B
, (bpf_int32
)AF_INET
));
3208 else if (proto
== ETHERTYPE_IPV6
)
3209 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3210 BPF_B
, (bpf_int32
)AF_INET6
));
3215 #endif /* HAVE_NET_PFVAR_H */
3218 case DLT_ARCNET_LINUX
:
3220 * XXX should we check for first fragment if the protocol
3228 case ETHERTYPE_IPV6
:
3229 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3230 (bpf_int32
)ARCTYPE_INET6
));
3233 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3234 (bpf_int32
)ARCTYPE_IP
);
3235 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3236 (bpf_int32
)ARCTYPE_IP_OLD
);
3241 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3242 (bpf_int32
)ARCTYPE_ARP
);
3243 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3244 (bpf_int32
)ARCTYPE_ARP_OLD
);
3248 case ETHERTYPE_REVARP
:
3249 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3250 (bpf_int32
)ARCTYPE_REVARP
));
3252 case ETHERTYPE_ATALK
:
3253 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3254 (bpf_int32
)ARCTYPE_ATALK
));
3261 case ETHERTYPE_ATALK
:
3271 * XXX - assumes a 2-byte Frame Relay header with
3272 * DLCI and flags. What if the address is longer?
3278 * Check for the special NLPID for IP.
3280 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3282 case ETHERTYPE_IPV6
:
3284 * Check for the special NLPID for IPv6.
3286 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3290 * Check for several OSI protocols.
3292 * Frame Relay packets typically have an OSI
3293 * NLPID at the beginning; we check for each
3296 * What we check for is the NLPID and a frame
3297 * control field of UI, i.e. 0x03 followed
3300 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3301 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3302 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3314 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3316 case DLT_JUNIPER_MFR
:
3317 case DLT_JUNIPER_MLFR
:
3318 case DLT_JUNIPER_MLPPP
:
3319 case DLT_JUNIPER_ATM1
:
3320 case DLT_JUNIPER_ATM2
:
3321 case DLT_JUNIPER_PPPOE
:
3322 case DLT_JUNIPER_PPPOE_ATM
:
3323 case DLT_JUNIPER_GGSN
:
3324 case DLT_JUNIPER_ES
:
3325 case DLT_JUNIPER_MONITOR
:
3326 case DLT_JUNIPER_SERVICES
:
3327 case DLT_JUNIPER_ETHER
:
3328 case DLT_JUNIPER_PPP
:
3329 case DLT_JUNIPER_FRELAY
:
3330 case DLT_JUNIPER_CHDLC
:
3331 case DLT_JUNIPER_VP
:
3332 case DLT_JUNIPER_ST
:
3333 case DLT_JUNIPER_ISM
:
3334 case DLT_JUNIPER_VS
:
3335 case DLT_JUNIPER_SRX_E2E
:
3336 case DLT_JUNIPER_FIBRECHANNEL
:
3337 case DLT_JUNIPER_ATM_CEMIC
:
3339 /* just lets verify the magic number for now -
3340 * on ATM we may have up to 6 different encapsulations on the wire
3341 * and need a lot of heuristics to figure out that the payload
3344 * FIXME encapsulation specific BPF_ filters
3346 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3348 case DLT_BACNET_MS_TP
:
3349 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3352 return gen_ipnet_linktype(proto
);
3354 case DLT_LINUX_IRDA
:
3355 bpf_error("IrDA link-layer type filtering not implemented");
3358 bpf_error("DOCSIS link-layer type filtering not implemented");
3361 case DLT_MTP2_WITH_PHDR
:
3362 bpf_error("MTP2 link-layer type filtering not implemented");
3365 bpf_error("ERF link-layer type filtering not implemented");
3368 bpf_error("PFSYNC link-layer type filtering not implemented");
3370 case DLT_LINUX_LAPD
:
3371 bpf_error("LAPD link-layer type filtering not implemented");
3375 case DLT_USB_LINUX_MMAPPED
:
3376 bpf_error("USB link-layer type filtering not implemented");
3378 case DLT_BLUETOOTH_HCI_H4
:
3379 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3380 bpf_error("Bluetooth link-layer type filtering not implemented");
3383 case DLT_CAN_SOCKETCAN
:
3384 bpf_error("CAN link-layer type filtering not implemented");
3386 case DLT_IEEE802_15_4
:
3387 case DLT_IEEE802_15_4_LINUX
:
3388 case DLT_IEEE802_15_4_NONASK_PHY
:
3389 case DLT_IEEE802_15_4_NOFCS
:
3390 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3392 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3393 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3396 bpf_error("SITA link-layer type filtering not implemented");
3399 bpf_error("RAIF1 link-layer type filtering not implemented");
3402 bpf_error("IPMB link-layer type filtering not implemented");
3405 bpf_error("AX.25 link-layer type filtering not implemented");
3408 /* Using the fixed-size NFLOG header it is possible to tell only
3409 * the address family of the packet, other meaningful data is
3410 * either missing or behind TLVs.
3412 bpf_error("NFLOG link-layer type filtering not implemented");
3416 * Does this link-layer header type have a field
3417 * indicating the type of the next protocol? If
3418 * so, off_linktype.constant_part will be the offset of that
3419 * field in the packet; if not, it will be -1.
3421 if (off_linktype
.constant_part
!= (u_int
)-1) {
3423 * Yes; assume it's an Ethernet type. (If
3424 * it's not, it needs to be handled specially
3427 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3430 * No; report an error.
3432 description
= pcap_datalink_val_to_description(linktype
);
3433 if (description
!= NULL
) {
3434 bpf_error("%s link-layer type filtering not implemented",
3437 bpf_error("DLT %u link-layer type filtering not implemented",
3446 * Check for an LLC SNAP packet with a given organization code and
3447 * protocol type; we check the entire contents of the 802.2 LLC and
3448 * snap headers, checking for DSAP and SSAP of SNAP and a control
3449 * field of 0x03 in the LLC header, and for the specified organization
3450 * code and protocol type in the SNAP header.
3452 static struct block
*
3453 gen_snap(orgcode
, ptype
)
3454 bpf_u_int32 orgcode
;
3457 u_char snapblock
[8];
3459 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3460 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3461 snapblock
[2] = 0x03; /* control = UI */
3462 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3463 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3464 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3465 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3466 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3467 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3471 * Generate code to match frames with an LLC header.
3476 struct block
*b0
, *b1
;
3482 * We check for an Ethernet type field less than
3483 * 1500, which means it's an 802.3 length field.
3485 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3489 * Now check for the purported DSAP and SSAP not being
3490 * 0xFF, to rule out NetWare-over-802.3.
3492 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3499 * We check for LLC traffic.
3501 b0
= gen_atmtype_abbrev(A_LLC
);
3504 case DLT_IEEE802
: /* Token Ring */
3506 * XXX - check for LLC frames.
3512 * XXX - check for LLC frames.
3516 case DLT_ATM_RFC1483
:
3518 * For LLC encapsulation, these are defined to have an
3521 * For VC encapsulation, they don't, but there's no
3522 * way to check for that; the protocol used on the VC
3523 * is negotiated out of band.
3527 case DLT_IEEE802_11
:
3528 case DLT_PRISM_HEADER
:
3529 case DLT_IEEE802_11_RADIO
:
3530 case DLT_IEEE802_11_RADIO_AVS
:
3533 * Check that we have a data frame.
3535 b0
= gen_check_802_11_data_frame();
3539 bpf_error("'llc' not supported for linktype %d", linktype
);
3547 struct block
*b0
, *b1
;
3551 * Check whether this is an LLC frame.
3556 * Load the control byte and test the low-order bit; it must
3557 * be clear for I frames.
3559 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3560 b1
= new_block(JMP(BPF_JSET
));
3571 struct block
*b0
, *b1
;
3574 * Check whether this is an LLC frame.
3579 * Now compare the low-order 2 bit of the control byte against
3580 * the appropriate value for S frames.
3582 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3590 struct block
*b0
, *b1
;
3593 * Check whether this is an LLC frame.
3598 * Now compare the low-order 2 bit of the control byte against
3599 * the appropriate value for U frames.
3601 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3607 gen_llc_s_subtype(bpf_u_int32 subtype
)
3609 struct block
*b0
, *b1
;
3612 * Check whether this is an LLC frame.
3617 * Now check for an S frame with the appropriate type.
3619 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3625 gen_llc_u_subtype(bpf_u_int32 subtype
)
3627 struct block
*b0
, *b1
;
3630 * Check whether this is an LLC frame.
3635 * Now check for a U frame with the appropriate type.
3637 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3643 * Generate code to match a particular packet type, for link-layer types
3644 * using 802.2 LLC headers.
3646 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3647 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3649 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3650 * value, if <= ETHERMTU. We use that to determine whether to
3651 * match the DSAP or both DSAP and LSAP or to check the OUI and
3652 * protocol ID in a SNAP header.
3654 static struct block
*
3655 gen_llc_linktype(proto
)
3659 * XXX - handle token-ring variable-length header.
3665 case LLCSAP_NETBEUI
:
3667 * XXX - should we check both the DSAP and the
3668 * SSAP, like this, or should we check just the
3669 * DSAP, as we do for other SAP values?
3671 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3672 ((proto
<< 8) | proto
));
3676 * XXX - are there ever SNAP frames for IPX on
3677 * non-Ethernet 802.x networks?
3679 return gen_cmp(OR_LLC
, 0, BPF_B
,
3680 (bpf_int32
)LLCSAP_IPX
);
3682 case ETHERTYPE_ATALK
:
3684 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3685 * SNAP packets with an organization code of
3686 * 0x080007 (Apple, for Appletalk) and a protocol
3687 * type of ETHERTYPE_ATALK (Appletalk).
3689 * XXX - check for an organization code of
3690 * encapsulated Ethernet as well?
3692 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3696 * XXX - we don't have to check for IPX 802.3
3697 * here, but should we check for the IPX Ethertype?
3699 if (proto
<= ETHERMTU
) {
3701 * This is an LLC SAP value, so check
3704 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3707 * This is an Ethernet type; we assume that it's
3708 * unlikely that it'll appear in the right place
3709 * at random, and therefore check only the
3710 * location that would hold the Ethernet type
3711 * in a SNAP frame with an organization code of
3712 * 0x000000 (encapsulated Ethernet).
3714 * XXX - if we were to check for the SNAP DSAP and
3715 * LSAP, as per XXX, and were also to check for an
3716 * organization code of 0x000000 (encapsulated
3717 * Ethernet), we'd do
3719 * return gen_snap(0x000000, proto);
3721 * here; for now, we don't, as per the above.
3722 * I don't know whether it's worth the extra CPU
3723 * time to do the right check or not.
3725 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3730 static struct block
*
3731 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3735 u_int src_off
, dst_off
;
3737 struct block
*b0
, *b1
;
3751 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3752 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3758 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3759 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3766 b0
= gen_linktype(proto
);
3767 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3773 static struct block
*
3774 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3775 struct in6_addr
*addr
;
3776 struct in6_addr
*mask
;
3778 u_int src_off
, dst_off
;
3780 struct block
*b0
, *b1
;
3795 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3796 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3802 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3803 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3810 /* this order is important */
3811 a
= (u_int32_t
*)addr
;
3812 m
= (u_int32_t
*)mask
;
3813 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3814 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3816 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3818 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3820 b0
= gen_linktype(proto
);
3826 static struct block
*
3827 gen_ehostop(eaddr
, dir
)
3828 register const u_char
*eaddr
;
3831 register struct block
*b0
, *b1
;
3835 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3838 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3841 b0
= gen_ehostop(eaddr
, Q_SRC
);
3842 b1
= gen_ehostop(eaddr
, Q_DST
);
3848 b0
= gen_ehostop(eaddr
, Q_SRC
);
3849 b1
= gen_ehostop(eaddr
, Q_DST
);
3854 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3858 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3862 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3866 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3870 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3874 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3882 * Like gen_ehostop, but for DLT_FDDI
3884 static struct block
*
3885 gen_fhostop(eaddr
, dir
)
3886 register const u_char
*eaddr
;
3889 struct block
*b0
, *b1
;
3893 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3896 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3899 b0
= gen_fhostop(eaddr
, Q_SRC
);
3900 b1
= gen_fhostop(eaddr
, Q_DST
);
3906 b0
= gen_fhostop(eaddr
, Q_SRC
);
3907 b1
= gen_fhostop(eaddr
, Q_DST
);
3912 bpf_error("'addr1' is only supported on 802.11");
3916 bpf_error("'addr2' is only supported on 802.11");
3920 bpf_error("'addr3' is only supported on 802.11");
3924 bpf_error("'addr4' is only supported on 802.11");
3928 bpf_error("'ra' is only supported on 802.11");
3932 bpf_error("'ta' is only supported on 802.11");
3940 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3942 static struct block
*
3943 gen_thostop(eaddr
, dir
)
3944 register const u_char
*eaddr
;
3947 register struct block
*b0
, *b1
;
3951 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3954 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3957 b0
= gen_thostop(eaddr
, Q_SRC
);
3958 b1
= gen_thostop(eaddr
, Q_DST
);
3964 b0
= gen_thostop(eaddr
, Q_SRC
);
3965 b1
= gen_thostop(eaddr
, Q_DST
);
3970 bpf_error("'addr1' is only supported on 802.11");
3974 bpf_error("'addr2' is only supported on 802.11");
3978 bpf_error("'addr3' is only supported on 802.11");
3982 bpf_error("'addr4' is only supported on 802.11");
3986 bpf_error("'ra' is only supported on 802.11");
3990 bpf_error("'ta' is only supported on 802.11");
3998 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3999 * various 802.11 + radio headers.
4001 static struct block
*
4002 gen_wlanhostop(eaddr
, dir
)
4003 register const u_char
*eaddr
;
4006 register struct block
*b0
, *b1
, *b2
;
4007 register struct slist
*s
;
4009 #ifdef ENABLE_WLAN_FILTERING_PATCH
4012 * We need to disable the optimizer because the optimizer is buggy
4013 * and wipes out some LD instructions generated by the below
4014 * code to validate the Frame Control bits
4017 #endif /* ENABLE_WLAN_FILTERING_PATCH */
4024 * For control frames, there is no SA.
4026 * For management frames, SA is at an
4027 * offset of 10 from the beginning of
4030 * For data frames, SA is at an offset
4031 * of 10 from the beginning of the packet
4032 * if From DS is clear, at an offset of
4033 * 16 from the beginning of the packet
4034 * if From DS is set and To DS is clear,
4035 * and an offset of 24 from the beginning
4036 * of the packet if From DS is set and To DS
4041 * Generate the tests to be done for data frames
4044 * First, check for To DS set, i.e. check "link[1] & 0x01".
4046 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4047 b1
= new_block(JMP(BPF_JSET
));
4048 b1
->s
.k
= 0x01; /* To DS */
4052 * If To DS is set, the SA is at 24.
4054 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4058 * Now, check for To DS not set, i.e. check
4059 * "!(link[1] & 0x01)".
4061 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4062 b2
= new_block(JMP(BPF_JSET
));
4063 b2
->s
.k
= 0x01; /* To DS */
4068 * If To DS is not set, the SA is at 16.
4070 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4074 * Now OR together the last two checks. That gives
4075 * the complete set of checks for data frames with
4081 * Now check for From DS being set, and AND that with
4082 * the ORed-together checks.
4084 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4085 b1
= new_block(JMP(BPF_JSET
));
4086 b1
->s
.k
= 0x02; /* From DS */
4091 * Now check for data frames with From DS not set.
4093 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4094 b2
= new_block(JMP(BPF_JSET
));
4095 b2
->s
.k
= 0x02; /* From DS */
4100 * If From DS isn't set, the SA is at 10.
4102 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4106 * Now OR together the checks for data frames with
4107 * From DS not set and for data frames with From DS
4108 * set; that gives the checks done for data frames.
4113 * Now check for a data frame.
4114 * I.e, check "link[0] & 0x08".
4116 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4117 b1
= new_block(JMP(BPF_JSET
));
4122 * AND that with the checks done for data frames.
4127 * If the high-order bit of the type value is 0, this
4128 * is a management frame.
4129 * I.e, check "!(link[0] & 0x08)".
4131 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4132 b2
= new_block(JMP(BPF_JSET
));
4138 * For management frames, the SA is at 10.
4140 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4144 * OR that with the checks done for data frames.
4145 * That gives the checks done for management and
4151 * If the low-order bit of the type value is 1,
4152 * this is either a control frame or a frame
4153 * with a reserved type, and thus not a
4156 * I.e., check "!(link[0] & 0x04)".
4158 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4159 b1
= new_block(JMP(BPF_JSET
));
4165 * AND that with the checks for data and management
4175 * For control frames, there is no DA.
4177 * For management frames, DA is at an
4178 * offset of 4 from the beginning of
4181 * For data frames, DA is at an offset
4182 * of 4 from the beginning of the packet
4183 * if To DS is clear and at an offset of
4184 * 16 from the beginning of the packet
4189 * Generate the tests to be done for data frames.
4191 * First, check for To DS set, i.e. "link[1] & 0x01".
4193 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4194 b1
= new_block(JMP(BPF_JSET
));
4195 b1
->s
.k
= 0x01; /* To DS */
4199 * If To DS is set, the DA is at 16.
4201 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4205 * Now, check for To DS not set, i.e. check
4206 * "!(link[1] & 0x01)".
4208 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4209 b2
= new_block(JMP(BPF_JSET
));
4210 b2
->s
.k
= 0x01; /* To DS */
4215 * If To DS is not set, the DA is at 4.
4217 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4221 * Now OR together the last two checks. That gives
4222 * the complete set of checks for data frames.
4227 * Now check for a data frame.
4228 * I.e, check "link[0] & 0x08".
4230 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4231 b1
= new_block(JMP(BPF_JSET
));
4236 * AND that with the checks done for data frames.
4241 * If the high-order bit of the type value is 0, this
4242 * is a management frame.
4243 * I.e, check "!(link[0] & 0x08)".
4245 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4246 b2
= new_block(JMP(BPF_JSET
));
4252 * For management frames, the DA is at 4.
4254 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4258 * OR that with the checks done for data frames.
4259 * That gives the checks done for management and
4265 * If the low-order bit of the type value is 1,
4266 * this is either a control frame or a frame
4267 * with a reserved type, and thus not a
4270 * I.e., check "!(link[0] & 0x04)".
4272 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4273 b1
= new_block(JMP(BPF_JSET
));
4279 * AND that with the checks for data and management
4287 * Not present in management frames; addr1 in other
4292 * If the high-order bit of the type value is 0, this
4293 * is a management frame.
4294 * I.e, check "(link[0] & 0x08)".
4296 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4297 b1
= new_block(JMP(BPF_JSET
));
4304 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4307 * AND that with the check of addr1.
4314 * Not present in management frames; addr2, if present,
4319 * Not present in CTS or ACK control frames.
4321 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4322 IEEE80211_FC0_TYPE_MASK
);
4324 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4325 IEEE80211_FC0_SUBTYPE_MASK
);
4327 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4328 IEEE80211_FC0_SUBTYPE_MASK
);
4334 * If the high-order bit of the type value is 0, this
4335 * is a management frame.
4336 * I.e, check "(link[0] & 0x08)".
4338 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4339 b1
= new_block(JMP(BPF_JSET
));
4344 * AND that with the check for frames other than
4345 * CTS and ACK frames.
4352 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4357 * XXX - add BSSID keyword?
4360 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4364 * Not present in CTS or ACK control frames.
4366 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4367 IEEE80211_FC0_TYPE_MASK
);
4369 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4370 IEEE80211_FC0_SUBTYPE_MASK
);
4372 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4373 IEEE80211_FC0_SUBTYPE_MASK
);
4377 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4383 * Not present in control frames.
4385 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4386 IEEE80211_FC0_TYPE_MASK
);
4388 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4394 * Present only if the direction mask has both "From DS"
4395 * and "To DS" set. Neither control frames nor management
4396 * frames should have both of those set, so we don't
4397 * check the frame type.
4399 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4400 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4401 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4406 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4407 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4413 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4414 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4423 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4424 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4425 * as the RFC states.)
4427 static struct block
*
4428 gen_ipfchostop(eaddr
, dir
)
4429 register const u_char
*eaddr
;
4432 register struct block
*b0
, *b1
;
4436 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4439 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4442 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4443 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4449 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4450 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4455 bpf_error("'addr1' is only supported on 802.11");
4459 bpf_error("'addr2' is only supported on 802.11");
4463 bpf_error("'addr3' is only supported on 802.11");
4467 bpf_error("'addr4' is only supported on 802.11");
4471 bpf_error("'ra' is only supported on 802.11");
4475 bpf_error("'ta' is only supported on 802.11");
4483 * This is quite tricky because there may be pad bytes in front of the
4484 * DECNET header, and then there are two possible data packet formats that
4485 * carry both src and dst addresses, plus 5 packet types in a format that
4486 * carries only the src node, plus 2 types that use a different format and
4487 * also carry just the src node.
4491 * Instead of doing those all right, we just look for data packets with
4492 * 0 or 1 bytes of padding. If you want to look at other packets, that
4493 * will require a lot more hacking.
4495 * To add support for filtering on DECNET "areas" (network numbers)
4496 * one would want to add a "mask" argument to this routine. That would
4497 * make the filter even more inefficient, although one could be clever
4498 * and not generate masking instructions if the mask is 0xFFFF.
4500 static struct block
*
4501 gen_dnhostop(addr
, dir
)
4505 struct block
*b0
, *b1
, *b2
, *tmp
;
4506 u_int offset_lh
; /* offset if long header is received */
4507 u_int offset_sh
; /* offset if short header is received */
4512 offset_sh
= 1; /* follows flags */
4513 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4517 offset_sh
= 3; /* follows flags, dstnode */
4518 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4522 /* Inefficient because we do our Calvinball dance twice */
4523 b0
= gen_dnhostop(addr
, Q_SRC
);
4524 b1
= gen_dnhostop(addr
, Q_DST
);
4530 /* Inefficient because we do our Calvinball dance twice */
4531 b0
= gen_dnhostop(addr
, Q_SRC
);
4532 b1
= gen_dnhostop(addr
, Q_DST
);
4537 bpf_error("ISO host filtering not implemented");
4542 b0
= gen_linktype(ETHERTYPE_DN
);
4543 /* Check for pad = 1, long header case */
4544 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4545 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4546 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4547 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4549 /* Check for pad = 0, long header case */
4550 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4551 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4554 /* Check for pad = 1, short header case */
4555 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4556 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4557 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4560 /* Check for pad = 0, short header case */
4561 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4562 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4566 /* Combine with test for linktype */
4572 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4573 * test the bottom-of-stack bit, and then check the version number
4574 * field in the IP header.
4576 static struct block
*
4577 gen_mpls_linktype(proto
)
4580 struct block
*b0
, *b1
;
4585 /* match the bottom-of-stack bit */
4586 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4587 /* match the IPv4 version number */
4588 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4593 /* match the bottom-of-stack bit */
4594 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4595 /* match the IPv4 version number */
4596 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4605 static struct block
*
4606 gen_host(addr
, mask
, proto
, dir
, type
)
4613 struct block
*b0
, *b1
;
4614 const char *typestr
;
4624 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4626 * Only check for non-IPv4 addresses if we're not
4627 * checking MPLS-encapsulated packets.
4629 if (label_stack_depth
== 0) {
4630 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4632 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4638 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4641 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4644 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4647 bpf_error("'tcp' modifier applied to %s", typestr
);
4650 bpf_error("'sctp' modifier applied to %s", typestr
);
4653 bpf_error("'udp' modifier applied to %s", typestr
);
4656 bpf_error("'icmp' modifier applied to %s", typestr
);
4659 bpf_error("'igmp' modifier applied to %s", typestr
);
4662 bpf_error("'igrp' modifier applied to %s", typestr
);
4665 bpf_error("'pim' modifier applied to %s", typestr
);
4668 bpf_error("'vrrp' modifier applied to %s", typestr
);
4671 bpf_error("'carp' modifier applied to %s", typestr
);
4674 bpf_error("ATALK host filtering not implemented");
4677 bpf_error("AARP host filtering not implemented");
4680 return gen_dnhostop(addr
, dir
);
4683 bpf_error("SCA host filtering not implemented");
4686 bpf_error("LAT host filtering not implemented");
4689 bpf_error("MOPDL host filtering not implemented");
4692 bpf_error("MOPRC host filtering not implemented");
4695 bpf_error("'ip6' modifier applied to ip host");
4698 bpf_error("'icmp6' modifier applied to %s", typestr
);
4701 bpf_error("'ah' modifier applied to %s", typestr
);
4704 bpf_error("'esp' modifier applied to %s", typestr
);
4707 bpf_error("ISO host filtering not implemented");
4710 bpf_error("'esis' modifier applied to %s", typestr
);
4713 bpf_error("'isis' modifier applied to %s", typestr
);
4716 bpf_error("'clnp' modifier applied to %s", typestr
);
4719 bpf_error("'stp' modifier applied to %s", typestr
);
4722 bpf_error("IPX host filtering not implemented");
4725 bpf_error("'netbeui' modifier applied to %s", typestr
);
4728 bpf_error("'radio' modifier applied to %s", typestr
);
4737 static struct block
*
4738 gen_host6(addr
, mask
, proto
, dir
, type
)
4739 struct in6_addr
*addr
;
4740 struct in6_addr
*mask
;
4745 const char *typestr
;
4755 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4758 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4761 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4764 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4767 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4770 bpf_error("'sctp' modifier applied to %s", typestr
);
4773 bpf_error("'tcp' modifier applied to %s", typestr
);
4776 bpf_error("'udp' modifier applied to %s", typestr
);
4779 bpf_error("'icmp' modifier applied to %s", typestr
);
4782 bpf_error("'igmp' modifier applied to %s", typestr
);
4785 bpf_error("'igrp' modifier applied to %s", typestr
);
4788 bpf_error("'pim' modifier applied to %s", typestr
);
4791 bpf_error("'vrrp' modifier applied to %s", typestr
);
4794 bpf_error("'carp' modifier applied to %s", typestr
);
4797 bpf_error("ATALK host filtering not implemented");
4800 bpf_error("AARP host filtering not implemented");
4803 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4806 bpf_error("SCA host filtering not implemented");
4809 bpf_error("LAT host filtering not implemented");
4812 bpf_error("MOPDL host filtering not implemented");
4815 bpf_error("MOPRC host filtering not implemented");
4818 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4821 bpf_error("'icmp6' modifier applied to %s", typestr
);
4824 bpf_error("'ah' modifier applied to %s", typestr
);
4827 bpf_error("'esp' modifier applied to %s", typestr
);
4830 bpf_error("ISO host filtering not implemented");
4833 bpf_error("'esis' modifier applied to %s", typestr
);
4836 bpf_error("'isis' modifier applied to %s", typestr
);
4839 bpf_error("'clnp' modifier applied to %s", typestr
);
4842 bpf_error("'stp' modifier applied to %s", typestr
);
4845 bpf_error("IPX host filtering not implemented");
4848 bpf_error("'netbeui' modifier applied to %s", typestr
);
4851 bpf_error("'radio' modifier applied to %s", typestr
);
4861 static struct block
*
4862 gen_gateway(eaddr
, alist
, proto
, dir
)
4863 const u_char
*eaddr
;
4864 bpf_u_int32
**alist
;
4868 struct block
*b0
, *b1
, *tmp
;
4871 bpf_error("direction applied to 'gateway'");
4880 case DLT_NETANALYZER
:
4881 case DLT_NETANALYZER_TRANSPARENT
:
4882 b1
= gen_prevlinkhdr_check();
4883 b0
= gen_ehostop(eaddr
, Q_OR
);
4888 b0
= gen_fhostop(eaddr
, Q_OR
);
4891 b0
= gen_thostop(eaddr
, Q_OR
);
4893 case DLT_IEEE802_11
:
4894 case DLT_PRISM_HEADER
:
4895 case DLT_IEEE802_11_RADIO_AVS
:
4896 case DLT_IEEE802_11_RADIO
:
4898 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4902 * This is LLC-multiplexed traffic; if it were
4903 * LANE, linktype would have been set to
4907 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4909 case DLT_IP_OVER_FC
:
4910 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4914 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4916 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4918 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4927 bpf_error("illegal modifier of 'gateway'");
4933 gen_proto_abbrev(proto
)
4942 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4943 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4948 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4949 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4954 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4955 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4960 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4963 #ifndef IPPROTO_IGMP
4964 #define IPPROTO_IGMP 2
4968 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4971 #ifndef IPPROTO_IGRP
4972 #define IPPROTO_IGRP 9
4975 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4979 #define IPPROTO_PIM 103
4983 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4984 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4988 #ifndef IPPROTO_VRRP
4989 #define IPPROTO_VRRP 112
4993 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4996 #ifndef IPPROTO_CARP
4997 #define IPPROTO_CARP 112
5001 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
5005 b1
= gen_linktype(ETHERTYPE_IP
);
5009 b1
= gen_linktype(ETHERTYPE_ARP
);
5013 b1
= gen_linktype(ETHERTYPE_REVARP
);
5017 bpf_error("link layer applied in wrong context");
5020 b1
= gen_linktype(ETHERTYPE_ATALK
);
5024 b1
= gen_linktype(ETHERTYPE_AARP
);
5028 b1
= gen_linktype(ETHERTYPE_DN
);
5032 b1
= gen_linktype(ETHERTYPE_SCA
);
5036 b1
= gen_linktype(ETHERTYPE_LAT
);
5040 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5044 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5048 b1
= gen_linktype(ETHERTYPE_IPV6
);
5051 #ifndef IPPROTO_ICMPV6
5052 #define IPPROTO_ICMPV6 58
5055 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5059 #define IPPROTO_AH 51
5062 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5063 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5068 #define IPPROTO_ESP 50
5071 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5072 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5077 b1
= gen_linktype(LLCSAP_ISONS
);
5081 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5085 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5088 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5089 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5090 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5092 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5094 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5096 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5100 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5101 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5102 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5104 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5106 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5108 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5112 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5113 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5114 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5116 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5121 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5122 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5127 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5128 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5130 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5132 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5137 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5138 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5143 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5144 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5149 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5153 b1
= gen_linktype(LLCSAP_8021D
);
5157 b1
= gen_linktype(LLCSAP_IPX
);
5161 b1
= gen_linktype(LLCSAP_NETBEUI
);
5165 bpf_error("'radio' is not a valid protocol type");
5173 static struct block
*
5179 /* not IPv4 frag other than the first frag */
5180 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5181 b
= new_block(JMP(BPF_JSET
));
5190 * Generate a comparison to a port value in the transport-layer header
5191 * at the specified offset from the beginning of that header.
5193 * XXX - this handles a variable-length prefix preceding the link-layer
5194 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5195 * variable-length link-layer headers (such as Token Ring or 802.11
5198 static struct block
*
5199 gen_portatom(off
, v
)
5203 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5206 static struct block
*
5207 gen_portatom6(off
, v
)
5211 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5215 gen_portop(port
, proto
, dir
)
5216 int port
, proto
, dir
;
5218 struct block
*b0
, *b1
, *tmp
;
5220 /* ip proto 'proto' and not a fragment other than the first fragment */
5221 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5227 b1
= gen_portatom(0, (bpf_int32
)port
);
5231 b1
= gen_portatom(2, (bpf_int32
)port
);
5236 tmp
= gen_portatom(0, (bpf_int32
)port
);
5237 b1
= gen_portatom(2, (bpf_int32
)port
);
5242 tmp
= gen_portatom(0, (bpf_int32
)port
);
5243 b1
= gen_portatom(2, (bpf_int32
)port
);
5255 static struct block
*
5256 gen_port(port
, ip_proto
, dir
)
5261 struct block
*b0
, *b1
, *tmp
;
5266 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5267 * not LLC encapsulation with LLCSAP_IP.
5269 * For IEEE 802 networks - which includes 802.5 token ring
5270 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5271 * says that SNAP encapsulation is used, not LLC encapsulation
5274 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5275 * RFC 2225 say that SNAP encapsulation is used, not LLC
5276 * encapsulation with LLCSAP_IP.
5278 * So we always check for ETHERTYPE_IP.
5280 b0
= gen_linktype(ETHERTYPE_IP
);
5286 b1
= gen_portop(port
, ip_proto
, dir
);
5290 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5291 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5293 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5305 gen_portop6(port
, proto
, dir
)
5306 int port
, proto
, dir
;
5308 struct block
*b0
, *b1
, *tmp
;
5310 /* ip6 proto 'proto' */
5311 /* XXX - catch the first fragment of a fragmented packet? */
5312 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5316 b1
= gen_portatom6(0, (bpf_int32
)port
);
5320 b1
= gen_portatom6(2, (bpf_int32
)port
);
5325 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5326 b1
= gen_portatom6(2, (bpf_int32
)port
);
5331 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5332 b1
= gen_portatom6(2, (bpf_int32
)port
);
5344 static struct block
*
5345 gen_port6(port
, ip_proto
, dir
)
5350 struct block
*b0
, *b1
, *tmp
;
5352 /* link proto ip6 */
5353 b0
= gen_linktype(ETHERTYPE_IPV6
);
5359 b1
= gen_portop6(port
, ip_proto
, dir
);
5363 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5364 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5366 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5377 /* gen_portrange code */
5378 static struct block
*
5379 gen_portrangeatom(off
, v1
, v2
)
5383 struct block
*b1
, *b2
;
5387 * Reverse the order of the ports, so v1 is the lower one.
5396 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5397 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5405 gen_portrangeop(port1
, port2
, proto
, dir
)
5410 struct block
*b0
, *b1
, *tmp
;
5412 /* ip proto 'proto' and not a fragment other than the first fragment */
5413 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5419 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5423 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5428 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5429 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5434 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5435 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5447 static struct block
*
5448 gen_portrange(port1
, port2
, ip_proto
, dir
)
5453 struct block
*b0
, *b1
, *tmp
;
5456 b0
= gen_linktype(ETHERTYPE_IP
);
5462 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5466 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5467 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5469 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5480 static struct block
*
5481 gen_portrangeatom6(off
, v1
, v2
)
5485 struct block
*b1
, *b2
;
5489 * Reverse the order of the ports, so v1 is the lower one.
5498 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5499 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5507 gen_portrangeop6(port1
, port2
, proto
, dir
)
5512 struct block
*b0
, *b1
, *tmp
;
5514 /* ip6 proto 'proto' */
5515 /* XXX - catch the first fragment of a fragmented packet? */
5516 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5520 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5524 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5529 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5530 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5535 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5536 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5548 static struct block
*
5549 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5554 struct block
*b0
, *b1
, *tmp
;
5556 /* link proto ip6 */
5557 b0
= gen_linktype(ETHERTYPE_IPV6
);
5563 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5567 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5568 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5570 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5582 lookup_proto(name
, proto
)
5583 register const char *name
;
5593 v
= pcap_nametoproto(name
);
5594 if (v
== PROTO_UNDEF
)
5595 bpf_error("unknown ip proto '%s'", name
);
5599 /* XXX should look up h/w protocol type based on linktype */
5600 v
= pcap_nametoeproto(name
);
5601 if (v
== PROTO_UNDEF
) {
5602 v
= pcap_nametollc(name
);
5603 if (v
== PROTO_UNDEF
)
5604 bpf_error("unknown ether proto '%s'", name
);
5609 if (strcmp(name
, "esis") == 0)
5611 else if (strcmp(name
, "isis") == 0)
5613 else if (strcmp(name
, "clnp") == 0)
5616 bpf_error("unknown osi proto '%s'", name
);
5636 static struct block
*
5637 gen_protochain(v
, proto
, dir
)
5642 #ifdef NO_PROTOCHAIN
5643 return gen_proto(v
, proto
, dir
);
5645 struct block
*b0
, *b
;
5646 struct slist
*s
[100];
5647 int fix2
, fix3
, fix4
, fix5
;
5648 int ahcheck
, again
, end
;
5650 int reg2
= alloc_reg();
5652 memset(s
, 0, sizeof(s
));
5653 fix3
= fix4
= fix5
= 0;
5660 b0
= gen_protochain(v
, Q_IP
, dir
);
5661 b
= gen_protochain(v
, Q_IPV6
, dir
);
5665 bpf_error("bad protocol applied for 'protochain'");
5670 * We don't handle variable-length prefixes before the link-layer
5671 * header, or variable-length link-layer headers, here yet.
5672 * We might want to add BPF instructions to do the protochain
5673 * work, to simplify that and, on platforms that have a BPF
5674 * interpreter with the new instructions, let the filtering
5675 * be done in the kernel. (We already require a modified BPF
5676 * engine to do the protochain stuff, to support backward
5677 * branches, and backward branch support is unlikely to appear
5678 * in kernel BPF engines.)
5680 if (off_linkpl
.is_variable
)
5681 bpf_error("'protochain' not supported with variable length headers");
5683 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5686 * s[0] is a dummy entry to protect other BPF insn from damage
5687 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5688 * hard to find interdependency made by jump table fixup.
5691 s
[i
] = new_stmt(0); /*dummy*/
5696 b0
= gen_linktype(ETHERTYPE_IP
);
5699 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5700 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5702 /* X = ip->ip_hl << 2 */
5703 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5704 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5709 b0
= gen_linktype(ETHERTYPE_IPV6
);
5711 /* A = ip6->ip_nxt */
5712 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5713 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5715 /* X = sizeof(struct ip6_hdr) */
5716 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5722 bpf_error("unsupported proto to gen_protochain");
5726 /* again: if (A == v) goto end; else fall through; */
5728 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5730 s
[i
]->s
.jt
= NULL
; /*later*/
5731 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5735 #ifndef IPPROTO_NONE
5736 #define IPPROTO_NONE 59
5738 /* if (A == IPPROTO_NONE) goto end */
5739 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5740 s
[i
]->s
.jt
= NULL
; /*later*/
5741 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5742 s
[i
]->s
.k
= IPPROTO_NONE
;
5743 s
[fix5
]->s
.jf
= s
[i
];
5747 if (proto
== Q_IPV6
) {
5748 int v6start
, v6end
, v6advance
, j
;
5751 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5752 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5753 s
[i
]->s
.jt
= NULL
; /*later*/
5754 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5755 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5756 s
[fix2
]->s
.jf
= s
[i
];
5758 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5759 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5760 s
[i
]->s
.jt
= NULL
; /*later*/
5761 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5762 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5764 /* if (A == IPPROTO_ROUTING) goto v6advance */
5765 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5766 s
[i
]->s
.jt
= NULL
; /*later*/
5767 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5768 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5770 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5771 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5772 s
[i
]->s
.jt
= NULL
; /*later*/
5773 s
[i
]->s
.jf
= NULL
; /*later*/
5774 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5784 * A = P[X + packet head];
5785 * X = X + (P[X + packet head + 1] + 1) * 8;
5787 /* A = P[X + packet head] */
5788 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5789 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5792 s
[i
] = new_stmt(BPF_ST
);
5795 /* A = P[X + packet head + 1]; */
5796 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5797 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5800 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5804 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5808 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5812 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5815 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5819 /* goto again; (must use BPF_JA for backward jump) */
5820 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5821 s
[i
]->s
.k
= again
- i
- 1;
5822 s
[i
- 1]->s
.jf
= s
[i
];
5826 for (j
= v6start
; j
<= v6end
; j
++)
5827 s
[j
]->s
.jt
= s
[v6advance
];
5830 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5832 s
[fix2
]->s
.jf
= s
[i
];
5838 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5839 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5840 s
[i
]->s
.jt
= NULL
; /*later*/
5841 s
[i
]->s
.jf
= NULL
; /*later*/
5842 s
[i
]->s
.k
= IPPROTO_AH
;
5844 s
[fix3
]->s
.jf
= s
[ahcheck
];
5851 * X = X + (P[X + 1] + 2) * 4;
5854 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5856 /* A = P[X + packet head]; */
5857 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5858 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5861 s
[i
] = new_stmt(BPF_ST
);
5865 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5868 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5872 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5874 /* A = P[X + packet head] */
5875 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5876 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5879 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5883 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5887 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5890 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5894 /* goto again; (must use BPF_JA for backward jump) */
5895 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5896 s
[i
]->s
.k
= again
- i
- 1;
5901 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5903 s
[fix2
]->s
.jt
= s
[end
];
5904 s
[fix4
]->s
.jf
= s
[end
];
5905 s
[fix5
]->s
.jt
= s
[end
];
5912 for (i
= 0; i
< max
- 1; i
++)
5913 s
[i
]->next
= s
[i
+ 1];
5914 s
[max
- 1]->next
= NULL
;
5919 b
= new_block(JMP(BPF_JEQ
));
5920 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5930 static struct block
*
5931 gen_check_802_11_data_frame()
5934 struct block
*b0
, *b1
;
5937 * A data frame has the 0x08 bit (b3) in the frame control field set
5938 * and the 0x04 bit (b2) clear.
5940 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5941 b0
= new_block(JMP(BPF_JSET
));
5945 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5946 b1
= new_block(JMP(BPF_JSET
));
5957 * Generate code that checks whether the packet is a packet for protocol
5958 * <proto> and whether the type field in that protocol's header has
5959 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5960 * IP packet and checks the protocol number in the IP header against <v>.
5962 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5963 * against Q_IP and Q_IPV6.
5965 static struct block
*
5966 gen_proto(v
, proto
, dir
)
5971 struct block
*b0
, *b1
;
5976 if (dir
!= Q_DEFAULT
)
5977 bpf_error("direction applied to 'proto'");
5981 b0
= gen_proto(v
, Q_IP
, dir
);
5982 b1
= gen_proto(v
, Q_IPV6
, dir
);
5988 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5989 * not LLC encapsulation with LLCSAP_IP.
5991 * For IEEE 802 networks - which includes 802.5 token ring
5992 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5993 * says that SNAP encapsulation is used, not LLC encapsulation
5996 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5997 * RFC 2225 say that SNAP encapsulation is used, not LLC
5998 * encapsulation with LLCSAP_IP.
6000 * So we always check for ETHERTYPE_IP.
6002 b0
= gen_linktype(ETHERTYPE_IP
);
6004 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
6006 b1
= gen_protochain(v
, Q_IP
);
6016 * Frame Relay packets typically have an OSI
6017 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
6018 * generates code to check for all the OSI
6019 * NLPIDs, so calling it and then adding a check
6020 * for the particular NLPID for which we're
6021 * looking is bogus, as we can just check for
6024 * What we check for is the NLPID and a frame
6025 * control field value of UI, i.e. 0x03 followed
6028 * XXX - assumes a 2-byte Frame Relay header with
6029 * DLCI and flags. What if the address is longer?
6031 * XXX - what about SNAP-encapsulated frames?
6033 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6039 * Cisco uses an Ethertype lookalike - for OSI,
6042 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6043 /* OSI in C-HDLC is stuffed with a fudge byte */
6044 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6049 b0
= gen_linktype(LLCSAP_ISONS
);
6050 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6056 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6058 * 4 is the offset of the PDU type relative to the IS-IS
6061 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6066 bpf_error("arp does not encapsulate another protocol");
6070 bpf_error("rarp does not encapsulate another protocol");
6074 bpf_error("atalk encapsulation is not specifiable");
6078 bpf_error("decnet encapsulation is not specifiable");
6082 bpf_error("sca does not encapsulate another protocol");
6086 bpf_error("lat does not encapsulate another protocol");
6090 bpf_error("moprc does not encapsulate another protocol");
6094 bpf_error("mopdl does not encapsulate another protocol");
6098 return gen_linktype(v
);
6101 bpf_error("'udp proto' is bogus");
6105 bpf_error("'tcp proto' is bogus");
6109 bpf_error("'sctp proto' is bogus");
6113 bpf_error("'icmp proto' is bogus");
6117 bpf_error("'igmp proto' is bogus");
6121 bpf_error("'igrp proto' is bogus");
6125 bpf_error("'pim proto' is bogus");
6129 bpf_error("'vrrp proto' is bogus");
6133 bpf_error("'carp proto' is bogus");
6137 b0
= gen_linktype(ETHERTYPE_IPV6
);
6140 * Also check for a fragment header before the final
6143 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6144 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6146 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6149 b1
= gen_protochain(v
, Q_IPV6
);
6155 bpf_error("'icmp6 proto' is bogus");
6158 bpf_error("'ah proto' is bogus");
6161 bpf_error("'ah proto' is bogus");
6164 bpf_error("'stp proto' is bogus");
6167 bpf_error("'ipx proto' is bogus");
6170 bpf_error("'netbeui proto' is bogus");
6173 bpf_error("'radio proto' is bogus");
6184 register const char *name
;
6187 int proto
= q
.proto
;
6191 bpf_u_int32 mask
, addr
;
6193 bpf_u_int32
**alist
;
6196 struct sockaddr_in
*sin4
;
6197 struct sockaddr_in6
*sin6
;
6198 struct addrinfo
*res
, *res0
;
6199 struct in6_addr mask128
;
6201 struct block
*b
, *tmp
;
6202 int port
, real_proto
;
6208 addr
= pcap_nametonetaddr(name
);
6210 bpf_error("unknown network '%s'", name
);
6211 /* Left justify network addr and calculate its network mask */
6213 while (addr
&& (addr
& 0xff000000) == 0) {
6217 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6221 if (proto
== Q_LINK
) {
6225 case DLT_NETANALYZER
:
6226 case DLT_NETANALYZER_TRANSPARENT
:
6227 eaddr
= pcap_ether_hostton(name
);
6230 "unknown ether host '%s'", name
);
6231 tmp
= gen_prevlinkhdr_check();
6232 b
= gen_ehostop(eaddr
, dir
);
6239 eaddr
= pcap_ether_hostton(name
);
6242 "unknown FDDI host '%s'", name
);
6243 b
= gen_fhostop(eaddr
, dir
);
6248 eaddr
= pcap_ether_hostton(name
);
6251 "unknown token ring host '%s'", name
);
6252 b
= gen_thostop(eaddr
, dir
);
6256 case DLT_IEEE802_11
:
6257 case DLT_PRISM_HEADER
:
6258 case DLT_IEEE802_11_RADIO_AVS
:
6259 case DLT_IEEE802_11_RADIO
:
6261 eaddr
= pcap_ether_hostton(name
);
6264 "unknown 802.11 host '%s'", name
);
6265 b
= gen_wlanhostop(eaddr
, dir
);
6269 case DLT_IP_OVER_FC
:
6270 eaddr
= pcap_ether_hostton(name
);
6273 "unknown Fibre Channel host '%s'", name
);
6274 b
= gen_ipfchostop(eaddr
, dir
);
6279 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6280 } else if (proto
== Q_DECNET
) {
6281 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6283 * I don't think DECNET hosts can be multihomed, so
6284 * there is no need to build up a list of addresses
6286 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6289 alist
= pcap_nametoaddr(name
);
6290 if (alist
== NULL
|| *alist
== NULL
)
6291 bpf_error("unknown host '%s'", name
);
6293 if (off_linktype
.constant_part
== (u_int
)-1 &&
6294 tproto
== Q_DEFAULT
)
6296 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6298 tmp
= gen_host(**alist
++, 0xffffffff,
6299 tproto
, dir
, q
.addr
);
6305 memset(&mask128
, 0xff, sizeof(mask128
));
6306 res0
= res
= pcap_nametoaddrinfo(name
);
6308 bpf_error("unknown host '%s'", name
);
6311 tproto
= tproto6
= proto
;
6312 if (off_linktype
.constant_part
== -1 &&
6313 tproto
== Q_DEFAULT
) {
6317 for (res
= res0
; res
; res
= res
->ai_next
) {
6318 switch (res
->ai_family
) {
6320 if (tproto
== Q_IPV6
)
6323 sin4
= (struct sockaddr_in
*)
6325 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6326 0xffffffff, tproto
, dir
, q
.addr
);
6329 if (tproto6
== Q_IP
)
6332 sin6
= (struct sockaddr_in6
*)
6334 tmp
= gen_host6(&sin6
->sin6_addr
,
6335 &mask128
, tproto6
, dir
, q
.addr
);
6347 bpf_error("unknown host '%s'%s", name
,
6348 (proto
== Q_DEFAULT
)
6350 : " for specified address family");
6357 if (proto
!= Q_DEFAULT
&&
6358 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6359 bpf_error("illegal qualifier of 'port'");
6360 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6361 bpf_error("unknown port '%s'", name
);
6362 if (proto
== Q_UDP
) {
6363 if (real_proto
== IPPROTO_TCP
)
6364 bpf_error("port '%s' is tcp", name
);
6365 else if (real_proto
== IPPROTO_SCTP
)
6366 bpf_error("port '%s' is sctp", name
);
6368 /* override PROTO_UNDEF */
6369 real_proto
= IPPROTO_UDP
;
6371 if (proto
== Q_TCP
) {
6372 if (real_proto
== IPPROTO_UDP
)
6373 bpf_error("port '%s' is udp", name
);
6375 else if (real_proto
== IPPROTO_SCTP
)
6376 bpf_error("port '%s' is sctp", name
);
6378 /* override PROTO_UNDEF */
6379 real_proto
= IPPROTO_TCP
;
6381 if (proto
== Q_SCTP
) {
6382 if (real_proto
== IPPROTO_UDP
)
6383 bpf_error("port '%s' is udp", name
);
6385 else if (real_proto
== IPPROTO_TCP
)
6386 bpf_error("port '%s' is tcp", name
);
6388 /* override PROTO_UNDEF */
6389 real_proto
= IPPROTO_SCTP
;
6392 bpf_error("illegal port number %d < 0", port
);
6394 bpf_error("illegal port number %d > 65535", port
);
6395 b
= gen_port(port
, real_proto
, dir
);
6396 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6400 if (proto
!= Q_DEFAULT
&&
6401 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6402 bpf_error("illegal qualifier of 'portrange'");
6403 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6404 bpf_error("unknown port in range '%s'", name
);
6405 if (proto
== Q_UDP
) {
6406 if (real_proto
== IPPROTO_TCP
)
6407 bpf_error("port in range '%s' is tcp", name
);
6408 else if (real_proto
== IPPROTO_SCTP
)
6409 bpf_error("port in range '%s' is sctp", name
);
6411 /* override PROTO_UNDEF */
6412 real_proto
= IPPROTO_UDP
;
6414 if (proto
== Q_TCP
) {
6415 if (real_proto
== IPPROTO_UDP
)
6416 bpf_error("port in range '%s' is udp", name
);
6417 else if (real_proto
== IPPROTO_SCTP
)
6418 bpf_error("port in range '%s' is sctp", name
);
6420 /* override PROTO_UNDEF */
6421 real_proto
= IPPROTO_TCP
;
6423 if (proto
== Q_SCTP
) {
6424 if (real_proto
== IPPROTO_UDP
)
6425 bpf_error("port in range '%s' is udp", name
);
6426 else if (real_proto
== IPPROTO_TCP
)
6427 bpf_error("port in range '%s' is tcp", name
);
6429 /* override PROTO_UNDEF */
6430 real_proto
= IPPROTO_SCTP
;
6433 bpf_error("illegal port number %d < 0", port1
);
6435 bpf_error("illegal port number %d > 65535", port1
);
6437 bpf_error("illegal port number %d < 0", port2
);
6439 bpf_error("illegal port number %d > 65535", port2
);
6441 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6442 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6447 eaddr
= pcap_ether_hostton(name
);
6449 bpf_error("unknown ether host: %s", name
);
6451 alist
= pcap_nametoaddr(name
);
6452 if (alist
== NULL
|| *alist
== NULL
)
6453 bpf_error("unknown host '%s'", name
);
6454 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6458 bpf_error("'gateway' not supported in this configuration");
6462 real_proto
= lookup_proto(name
, proto
);
6463 if (real_proto
>= 0)
6464 return gen_proto(real_proto
, proto
, dir
);
6466 bpf_error("unknown protocol: %s", name
);
6469 real_proto
= lookup_proto(name
, proto
);
6470 if (real_proto
>= 0)
6471 return gen_protochain(real_proto
, proto
, dir
);
6473 bpf_error("unknown protocol: %s", name
);
6484 gen_mcode(s1
, s2
, masklen
, q
)
6485 register const char *s1
, *s2
;
6486 register unsigned int masklen
;
6489 register int nlen
, mlen
;
6492 nlen
= __pcap_atoin(s1
, &n
);
6493 /* Promote short ipaddr */
6497 mlen
= __pcap_atoin(s2
, &m
);
6498 /* Promote short ipaddr */
6501 bpf_error("non-network bits set in \"%s mask %s\"",
6504 /* Convert mask len to mask */
6506 bpf_error("mask length must be <= 32");
6509 * X << 32 is not guaranteed by C to be 0; it's
6514 m
= 0xffffffff << (32 - masklen
);
6516 bpf_error("non-network bits set in \"%s/%d\"",
6523 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6526 bpf_error("Mask syntax for networks only");
6535 register const char *s
;
6540 int proto
= q
.proto
;
6546 else if (q
.proto
== Q_DECNET
)
6547 vlen
= __pcap_atodn(s
, &v
);
6549 vlen
= __pcap_atoin(s
, &v
);
6556 if (proto
== Q_DECNET
)
6557 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6558 else if (proto
== Q_LINK
) {
6559 bpf_error("illegal link layer address");
6562 if (s
== NULL
&& q
.addr
== Q_NET
) {
6563 /* Promote short net number */
6564 while (v
&& (v
& 0xff000000) == 0) {
6569 /* Promote short ipaddr */
6573 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6578 proto
= IPPROTO_UDP
;
6579 else if (proto
== Q_TCP
)
6580 proto
= IPPROTO_TCP
;
6581 else if (proto
== Q_SCTP
)
6582 proto
= IPPROTO_SCTP
;
6583 else if (proto
== Q_DEFAULT
)
6584 proto
= PROTO_UNDEF
;
6586 bpf_error("illegal qualifier of 'port'");
6589 bpf_error("illegal port number %u > 65535", v
);
6593 b
= gen_port((int)v
, proto
, dir
);
6594 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6600 proto
= IPPROTO_UDP
;
6601 else if (proto
== Q_TCP
)
6602 proto
= IPPROTO_TCP
;
6603 else if (proto
== Q_SCTP
)
6604 proto
= IPPROTO_SCTP
;
6605 else if (proto
== Q_DEFAULT
)
6606 proto
= PROTO_UNDEF
;
6608 bpf_error("illegal qualifier of 'portrange'");
6611 bpf_error("illegal port number %u > 65535", v
);
6615 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6616 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6621 bpf_error("'gateway' requires a name");
6625 return gen_proto((int)v
, proto
, dir
);
6628 return gen_protochain((int)v
, proto
, dir
);
6643 gen_mcode6(s1
, s2
, masklen
, q
)
6644 register const char *s1
, *s2
;
6645 register unsigned int masklen
;
6648 struct addrinfo
*res
;
6649 struct in6_addr
*addr
;
6650 struct in6_addr mask
;
6655 bpf_error("no mask %s supported", s2
);
6657 res
= pcap_nametoaddrinfo(s1
);
6659 bpf_error("invalid ip6 address %s", s1
);
6662 bpf_error("%s resolved to multiple address", s1
);
6663 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6665 if (sizeof(mask
) * 8 < masklen
)
6666 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6667 memset(&mask
, 0, sizeof(mask
));
6668 memset(&mask
, 0xff, masklen
/ 8);
6670 mask
.s6_addr
[masklen
/ 8] =
6671 (0xff << (8 - masklen
% 8)) & 0xff;
6674 a
= (u_int32_t
*)addr
;
6675 m
= (u_int32_t
*)&mask
;
6676 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6677 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6678 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6686 bpf_error("Mask syntax for networks only");
6690 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6696 bpf_error("invalid qualifier against IPv6 address");
6705 register const u_char
*eaddr
;
6708 struct block
*b
, *tmp
;
6710 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6713 case DLT_NETANALYZER
:
6714 case DLT_NETANALYZER_TRANSPARENT
:
6715 tmp
= gen_prevlinkhdr_check();
6716 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6721 return gen_fhostop(eaddr
, (int)q
.dir
);
6723 return gen_thostop(eaddr
, (int)q
.dir
);
6724 case DLT_IEEE802_11
:
6725 case DLT_PRISM_HEADER
:
6726 case DLT_IEEE802_11_RADIO_AVS
:
6727 case DLT_IEEE802_11_RADIO
:
6729 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6730 case DLT_IP_OVER_FC
:
6731 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6733 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6737 bpf_error("ethernet address used in non-ether expression");
6744 struct slist
*s0
, *s1
;
6747 * This is definitely not the best way to do this, but the
6748 * lists will rarely get long.
6755 static struct slist
*
6761 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6766 static struct slist
*
6772 s
= new_stmt(BPF_LD
|BPF_MEM
);
6778 * Modify "index" to use the value stored into its register as an
6779 * offset relative to the beginning of the header for the protocol
6780 * "proto", and allocate a register and put an item "size" bytes long
6781 * (1, 2, or 4) at that offset into that register, making it the register
6785 gen_load(proto
, inst
, size
)
6790 struct slist
*s
, *tmp
;
6792 int regno
= alloc_reg();
6794 free_reg(inst
->regno
);
6798 bpf_error("data size must be 1, 2, or 4");
6814 bpf_error("unsupported index operation");
6818 * The offset is relative to the beginning of the packet
6819 * data, if we have a radio header. (If we don't, this
6822 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6823 linktype
!= DLT_IEEE802_11_RADIO
&&
6824 linktype
!= DLT_PRISM_HEADER
)
6825 bpf_error("radio information not present in capture");
6828 * Load into the X register the offset computed into the
6829 * register specified by "index".
6831 s
= xfer_to_x(inst
);
6834 * Load the item at that offset.
6836 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6838 sappend(inst
->s
, s
);
6843 * The offset is relative to the beginning of
6844 * the link-layer header.
6846 * XXX - what about ATM LANE? Should the index be
6847 * relative to the beginning of the AAL5 frame, so
6848 * that 0 refers to the beginning of the LE Control
6849 * field, or relative to the beginning of the LAN
6850 * frame, so that 0 refers, for Ethernet LANE, to
6851 * the beginning of the destination address?
6853 s
= gen_abs_offset_varpart(&off_linkhdr
);
6856 * If "s" is non-null, it has code to arrange that the
6857 * X register contains the length of the prefix preceding
6858 * the link-layer header. Add to it the offset computed
6859 * into the register specified by "index", and move that
6860 * into the X register. Otherwise, just load into the X
6861 * register the offset computed into the register specified
6865 sappend(s
, xfer_to_a(inst
));
6866 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6867 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6869 s
= xfer_to_x(inst
);
6872 * Load the item at the sum of the offset we've put in the
6873 * X register and the offset of the start of the link
6874 * layer header (which is 0 if the radio header is
6875 * variable-length; that header length is what we put
6876 * into the X register and then added to the index).
6878 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6879 tmp
->s
.k
= off_linkhdr
.constant_part
;
6881 sappend(inst
->s
, s
);
6895 * The offset is relative to the beginning of
6896 * the network-layer header.
6897 * XXX - are there any cases where we want
6900 s
= gen_abs_offset_varpart(&off_linkpl
);
6903 * If "s" is non-null, it has code to arrange that the
6904 * X register contains the variable part of the offset
6905 * of the link-layer payload. Add to it the offset
6906 * computed into the register specified by "index",
6907 * and move that into the X register. Otherwise, just
6908 * load into the X register the offset computed into
6909 * the register specified by "index".
6912 sappend(s
, xfer_to_a(inst
));
6913 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6914 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6916 s
= xfer_to_x(inst
);
6919 * Load the item at the sum of the offset we've put in the
6920 * X register, the offset of the start of the network
6921 * layer header from the beginning of the link-layer
6922 * payload, and the constant part of the offset of the
6923 * start of the link-layer payload.
6925 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6926 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6928 sappend(inst
->s
, s
);
6931 * Do the computation only if the packet contains
6932 * the protocol in question.
6934 b
= gen_proto_abbrev(proto
);
6936 gen_and(inst
->b
, b
);
6950 * The offset is relative to the beginning of
6951 * the transport-layer header.
6953 * Load the X register with the length of the IPv4 header
6954 * (plus the offset of the link-layer header, if it's
6955 * a variable-length header), in bytes.
6957 * XXX - are there any cases where we want
6959 * XXX - we should, if we're built with
6960 * IPv6 support, generate code to load either
6961 * IPv4, IPv6, or both, as appropriate.
6963 s
= gen_loadx_iphdrlen();
6966 * The X register now contains the sum of the variable
6967 * part of the offset of the link-layer payload and the
6968 * length of the network-layer header.
6970 * Load into the A register the offset relative to
6971 * the beginning of the transport layer header,
6972 * add the X register to that, move that to the
6973 * X register, and load with an offset from the
6974 * X register equal to the sum of the constant part of
6975 * the offset of the link-layer payload and the offset,
6976 * relative to the beginning of the link-layer payload,
6977 * of the network-layer header.
6979 sappend(s
, xfer_to_a(inst
));
6980 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6981 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6982 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6983 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6984 sappend(inst
->s
, s
);
6987 * Do the computation only if the packet contains
6988 * the protocol in question - which is true only
6989 * if this is an IP datagram and is the first or
6990 * only fragment of that datagram.
6992 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6994 gen_and(inst
->b
, b
);
6995 gen_and(gen_proto_abbrev(Q_IP
), b
);
6999 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
7002 inst
->regno
= regno
;
7003 s
= new_stmt(BPF_ST
);
7005 sappend(inst
->s
, s
);
7011 gen_relation(code
, a0
, a1
, reversed
)
7013 struct arth
*a0
, *a1
;
7016 struct slist
*s0
, *s1
, *s2
;
7017 struct block
*b
, *tmp
;
7021 if (code
== BPF_JEQ
) {
7022 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
7023 b
= new_block(JMP(code
));
7027 b
= new_block(BPF_JMP
|code
|BPF_X
);
7033 sappend(a0
->s
, a1
->s
);
7037 free_reg(a0
->regno
);
7038 free_reg(a1
->regno
);
7040 /* 'and' together protocol checks */
7043 gen_and(a0
->b
, tmp
= a1
->b
);
7059 int regno
= alloc_reg();
7060 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7063 s
= new_stmt(BPF_LD
|BPF_LEN
);
7064 s
->next
= new_stmt(BPF_ST
);
7065 s
->next
->s
.k
= regno
;
7080 a
= (struct arth
*)newchunk(sizeof(*a
));
7084 s
= new_stmt(BPF_LD
|BPF_IMM
);
7086 s
->next
= new_stmt(BPF_ST
);
7102 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7105 s
= new_stmt(BPF_ST
);
7113 gen_arth(code
, a0
, a1
)
7115 struct arth
*a0
, *a1
;
7117 struct slist
*s0
, *s1
, *s2
;
7120 * Disallow division by, or modulus by, zero; we do this here
7121 * so that it gets done even if the optimizer is disabled.
7123 if (code
== BPF_DIV
) {
7124 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7125 bpf_error("division by zero");
7126 } else if (code
== BPF_MOD
) {
7127 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7128 bpf_error("modulus by zero");
7132 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7137 sappend(a0
->s
, a1
->s
);
7139 free_reg(a0
->regno
);
7140 free_reg(a1
->regno
);
7142 s0
= new_stmt(BPF_ST
);
7143 a0
->regno
= s0
->s
.k
= alloc_reg();
7150 * Here we handle simple allocation of the scratch registers.
7151 * If too many registers are alloc'd, the allocator punts.
7153 static int regused
[BPF_MEMWORDS
];
7157 * Initialize the table of used registers and the current register.
7163 memset(regused
, 0, sizeof regused
);
7167 * Return the next free register.
7172 int n
= BPF_MEMWORDS
;
7175 if (regused
[curreg
])
7176 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7178 regused
[curreg
] = 1;
7182 bpf_error("too many registers needed to evaluate expression");
7188 * Return a register to the table so it can
7198 static struct block
*
7205 s
= new_stmt(BPF_LD
|BPF_LEN
);
7206 b
= new_block(JMP(jmp
));
7217 return gen_len(BPF_JGE
, n
);
7221 * Actually, this is less than or equal.
7229 b
= gen_len(BPF_JGT
, n
);
7236 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7237 * the beginning of the link-layer header.
7238 * XXX - that means you can't test values in the radiotap header, but
7239 * as that header is difficult if not impossible to parse generally
7240 * without a loop, that might not be a severe problem. A new keyword
7241 * "radio" could be added for that, although what you'd really want
7242 * would be a way of testing particular radio header values, which
7243 * would generate code appropriate to the radio header in question.
7246 gen_byteop(op
, idx
, val
)
7257 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7260 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7264 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7268 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7272 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7276 b
= new_block(JMP(BPF_JEQ
));
7283 static u_char abroadcast
[] = { 0x0 };
7286 gen_broadcast(proto
)
7289 bpf_u_int32 hostmask
;
7290 struct block
*b0
, *b1
, *b2
;
7291 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7299 case DLT_ARCNET_LINUX
:
7300 return gen_ahostop(abroadcast
, Q_DST
);
7302 case DLT_NETANALYZER
:
7303 case DLT_NETANALYZER_TRANSPARENT
:
7304 b1
= gen_prevlinkhdr_check();
7305 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7310 return gen_fhostop(ebroadcast
, Q_DST
);
7312 return gen_thostop(ebroadcast
, Q_DST
);
7313 case DLT_IEEE802_11
:
7314 case DLT_PRISM_HEADER
:
7315 case DLT_IEEE802_11_RADIO_AVS
:
7316 case DLT_IEEE802_11_RADIO
:
7318 return gen_wlanhostop(ebroadcast
, Q_DST
);
7319 case DLT_IP_OVER_FC
:
7320 return gen_ipfchostop(ebroadcast
, Q_DST
);
7322 bpf_error("not a broadcast link");
7328 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7329 * as an indication that we don't know the netmask, and fail
7332 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7333 bpf_error("netmask not known, so 'ip broadcast' not supported");
7334 b0
= gen_linktype(ETHERTYPE_IP
);
7335 hostmask
= ~netmask
;
7336 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7337 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7338 (bpf_int32
)(~0 & hostmask
), hostmask
);
7343 bpf_error("only link-layer/IP broadcast filters supported");
7349 * Generate code to test the low-order bit of a MAC address (that's
7350 * the bottom bit of the *first* byte).
7352 static struct block
*
7353 gen_mac_multicast(offset
)
7356 register struct block
*b0
;
7357 register struct slist
*s
;
7359 /* link[offset] & 1 != 0 */
7360 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7361 b0
= new_block(JMP(BPF_JSET
));
7368 gen_multicast(proto
)
7371 register struct block
*b0
, *b1
, *b2
;
7372 register struct slist
*s
;
7380 case DLT_ARCNET_LINUX
:
7381 /* all ARCnet multicasts use the same address */
7382 return gen_ahostop(abroadcast
, Q_DST
);
7384 case DLT_NETANALYZER
:
7385 case DLT_NETANALYZER_TRANSPARENT
:
7386 b1
= gen_prevlinkhdr_check();
7387 /* ether[0] & 1 != 0 */
7388 b0
= gen_mac_multicast(0);
7394 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7396 * XXX - was that referring to bit-order issues?
7398 /* fddi[1] & 1 != 0 */
7399 return gen_mac_multicast(1);
7401 /* tr[2] & 1 != 0 */
7402 return gen_mac_multicast(2);
7403 case DLT_IEEE802_11
:
7404 case DLT_PRISM_HEADER
:
7405 case DLT_IEEE802_11_RADIO_AVS
:
7406 case DLT_IEEE802_11_RADIO
:
7411 * For control frames, there is no DA.
7413 * For management frames, DA is at an
7414 * offset of 4 from the beginning of
7417 * For data frames, DA is at an offset
7418 * of 4 from the beginning of the packet
7419 * if To DS is clear and at an offset of
7420 * 16 from the beginning of the packet
7425 * Generate the tests to be done for data frames.
7427 * First, check for To DS set, i.e. "link[1] & 0x01".
7429 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7430 b1
= new_block(JMP(BPF_JSET
));
7431 b1
->s
.k
= 0x01; /* To DS */
7435 * If To DS is set, the DA is at 16.
7437 b0
= gen_mac_multicast(16);
7441 * Now, check for To DS not set, i.e. check
7442 * "!(link[1] & 0x01)".
7444 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7445 b2
= new_block(JMP(BPF_JSET
));
7446 b2
->s
.k
= 0x01; /* To DS */
7451 * If To DS is not set, the DA is at 4.
7453 b1
= gen_mac_multicast(4);
7457 * Now OR together the last two checks. That gives
7458 * the complete set of checks for data frames.
7463 * Now check for a data frame.
7464 * I.e, check "link[0] & 0x08".
7466 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7467 b1
= new_block(JMP(BPF_JSET
));
7472 * AND that with the checks done for data frames.
7477 * If the high-order bit of the type value is 0, this
7478 * is a management frame.
7479 * I.e, check "!(link[0] & 0x08)".
7481 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7482 b2
= new_block(JMP(BPF_JSET
));
7488 * For management frames, the DA is at 4.
7490 b1
= gen_mac_multicast(4);
7494 * OR that with the checks done for data frames.
7495 * That gives the checks done for management and
7501 * If the low-order bit of the type value is 1,
7502 * this is either a control frame or a frame
7503 * with a reserved type, and thus not a
7506 * I.e., check "!(link[0] & 0x04)".
7508 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7509 b1
= new_block(JMP(BPF_JSET
));
7515 * AND that with the checks for data and management
7520 case DLT_IP_OVER_FC
:
7521 b0
= gen_mac_multicast(2);
7526 /* Link not known to support multicasts */
7530 b0
= gen_linktype(ETHERTYPE_IP
);
7531 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7536 b0
= gen_linktype(ETHERTYPE_IPV6
);
7537 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7541 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7547 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7548 * Outbound traffic is sent by this machine, while inbound traffic is
7549 * sent by a remote machine (and may include packets destined for a
7550 * unicast or multicast link-layer address we are not subscribing to).
7551 * These are the same definitions implemented by pcap_setdirection().
7552 * Capturing only unicast traffic destined for this host is probably
7553 * better accomplished using a higher-layer filter.
7559 register struct block
*b0
;
7562 * Only some data link types support inbound/outbound qualifiers.
7566 b0
= gen_relation(BPF_JEQ
,
7567 gen_load(Q_LINK
, gen_loadi(0), 1),
7574 /* match outgoing packets */
7575 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7577 /* match incoming packets */
7578 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7583 /* match outgoing packets */
7584 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7586 /* to filter on inbound traffic, invert the match */
7591 #ifdef HAVE_NET_PFVAR_H
7593 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7594 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7600 /* match outgoing packets */
7601 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7603 /* match incoming packets */
7604 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7608 case DLT_JUNIPER_MFR
:
7609 case DLT_JUNIPER_MLFR
:
7610 case DLT_JUNIPER_MLPPP
:
7611 case DLT_JUNIPER_ATM1
:
7612 case DLT_JUNIPER_ATM2
:
7613 case DLT_JUNIPER_PPPOE
:
7614 case DLT_JUNIPER_PPPOE_ATM
:
7615 case DLT_JUNIPER_GGSN
:
7616 case DLT_JUNIPER_ES
:
7617 case DLT_JUNIPER_MONITOR
:
7618 case DLT_JUNIPER_SERVICES
:
7619 case DLT_JUNIPER_ETHER
:
7620 case DLT_JUNIPER_PPP
:
7621 case DLT_JUNIPER_FRELAY
:
7622 case DLT_JUNIPER_CHDLC
:
7623 case DLT_JUNIPER_VP
:
7624 case DLT_JUNIPER_ST
:
7625 case DLT_JUNIPER_ISM
:
7626 case DLT_JUNIPER_VS
:
7627 case DLT_JUNIPER_SRX_E2E
:
7628 case DLT_JUNIPER_FIBRECHANNEL
:
7629 case DLT_JUNIPER_ATM_CEMIC
:
7631 /* juniper flags (including direction) are stored
7632 * the byte after the 3-byte magic number */
7634 /* match outgoing packets */
7635 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7637 /* match incoming packets */
7638 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7644 * If we have packet meta-data indicating a direction,
7645 * check it, otherwise give up as this link-layer type
7646 * has nothing in the packet data.
7648 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7650 * This is Linux with PF_PACKET support.
7651 * If this is a *live* capture, we can look at
7652 * special meta-data in the filter expression;
7653 * if it's a savefile, we can't.
7655 if (bpf_pcap
->rfile
!= NULL
) {
7656 /* We have a FILE *, so this is a savefile */
7657 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7662 /* match outgoing packets */
7663 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7666 /* to filter on inbound traffic, invert the match */
7669 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7670 bpf_error("inbound/outbound not supported on linktype %d",
7674 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7679 #ifdef HAVE_NET_PFVAR_H
7680 /* PF firewall log matched interface */
7682 gen_pf_ifname(const char *ifname
)
7687 if (linktype
!= DLT_PFLOG
) {
7688 bpf_error("ifname supported only on PF linktype");
7691 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7692 off
= offsetof(struct pfloghdr
, ifname
);
7693 if (strlen(ifname
) >= len
) {
7694 bpf_error("ifname interface names can only be %d characters",
7698 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7702 /* PF firewall log ruleset name */
7704 gen_pf_ruleset(char *ruleset
)
7708 if (linktype
!= DLT_PFLOG
) {
7709 bpf_error("ruleset supported only on PF linktype");
7713 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7714 bpf_error("ruleset names can only be %ld characters",
7715 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7719 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7720 strlen(ruleset
), (const u_char
*)ruleset
);
7724 /* PF firewall log rule number */
7730 if (linktype
!= DLT_PFLOG
) {
7731 bpf_error("rnr supported only on PF linktype");
7735 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7740 /* PF firewall log sub-rule number */
7742 gen_pf_srnr(int srnr
)
7746 if (linktype
!= DLT_PFLOG
) {
7747 bpf_error("srnr supported only on PF linktype");
7751 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7756 /* PF firewall log reason code */
7758 gen_pf_reason(int reason
)
7762 if (linktype
!= DLT_PFLOG
) {
7763 bpf_error("reason supported only on PF linktype");
7767 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7772 /* PF firewall log action */
7774 gen_pf_action(int action
)
7778 if (linktype
!= DLT_PFLOG
) {
7779 bpf_error("action supported only on PF linktype");
7783 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7787 #else /* !HAVE_NET_PFVAR_H */
7789 gen_pf_ifname(const char *ifname
)
7791 bpf_error("libpcap was compiled without pf support");
7797 gen_pf_ruleset(char *ruleset
)
7799 bpf_error("libpcap was compiled on a machine without pf support");
7807 bpf_error("libpcap was compiled on a machine without pf support");
7813 gen_pf_srnr(int srnr
)
7815 bpf_error("libpcap was compiled on a machine without pf support");
7821 gen_pf_reason(int reason
)
7823 bpf_error("libpcap was compiled on a machine without pf support");
7829 gen_pf_action(int action
)
7831 bpf_error("libpcap was compiled on a machine without pf support");
7835 #endif /* HAVE_NET_PFVAR_H */
7837 /* IEEE 802.11 wireless header */
7839 gen_p80211_type(int type
, int mask
)
7845 case DLT_IEEE802_11
:
7846 case DLT_PRISM_HEADER
:
7847 case DLT_IEEE802_11_RADIO_AVS
:
7848 case DLT_IEEE802_11_RADIO
:
7849 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7854 bpf_error("802.11 link-layer types supported only on 802.11");
7862 gen_p80211_fcdir(int fcdir
)
7868 case DLT_IEEE802_11
:
7869 case DLT_PRISM_HEADER
:
7870 case DLT_IEEE802_11_RADIO_AVS
:
7871 case DLT_IEEE802_11_RADIO
:
7875 bpf_error("frame direction supported only with 802.11 headers");
7879 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7880 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7887 register const u_char
*eaddr
;
7893 case DLT_ARCNET_LINUX
:
7894 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7896 return (gen_ahostop(eaddr
, (int)q
.dir
));
7898 bpf_error("ARCnet address used in non-arc expression");
7904 bpf_error("aid supported only on ARCnet");
7907 bpf_error("ARCnet address used in non-arc expression");
7912 static struct block
*
7913 gen_ahostop(eaddr
, dir
)
7914 register const u_char
*eaddr
;
7917 register struct block
*b0
, *b1
;
7920 /* src comes first, different from Ethernet */
7922 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7925 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7928 b0
= gen_ahostop(eaddr
, Q_SRC
);
7929 b1
= gen_ahostop(eaddr
, Q_DST
);
7935 b0
= gen_ahostop(eaddr
, Q_SRC
);
7936 b1
= gen_ahostop(eaddr
, Q_DST
);
7941 bpf_error("'addr1' is only supported on 802.11");
7945 bpf_error("'addr2' is only supported on 802.11");
7949 bpf_error("'addr3' is only supported on 802.11");
7953 bpf_error("'addr4' is only supported on 802.11");
7957 bpf_error("'ra' is only supported on 802.11");
7961 bpf_error("'ta' is only supported on 802.11");
7968 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7969 static struct block
*
7970 gen_vlan_bpf_extensions(int vlan_num
)
7972 struct block
*b0
, *b1
;
7975 /* generate new filter code based on extracting packet
7977 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7978 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7980 b0
= new_block(JMP(BPF_JEQ
));
7984 if (vlan_num
>= 0) {
7985 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7986 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7988 b1
= new_block(JMP(BPF_JEQ
));
7990 b1
->s
.k
= (bpf_int32
) vlan_num
;
8000 static struct block
*
8001 gen_vlan_no_bpf_extensions(int vlan_num
)
8003 struct block
*b0
, *b1
;
8005 /* check for VLAN, including QinQ */
8006 b0
= gen_linktype(ETHERTYPE_8021Q
);
8007 b1
= gen_linktype(ETHERTYPE_8021AD
);
8010 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
8014 /* If a specific VLAN is requested, check VLAN id */
8015 if (vlan_num
>= 0) {
8016 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
8017 (bpf_int32
)vlan_num
, 0x0fff);
8023 * The payload follows the full header, including the
8024 * VLAN tags, so skip past this VLAN tag.
8026 off_linkpl
.constant_part
+= 4;
8029 * The link-layer type information follows the VLAN tags, so
8030 * skip past this VLAN tag.
8032 off_linktype
.constant_part
+= 4;
8038 * support IEEE 802.1Q VLAN trunk over ethernet
8046 /* can't check for VLAN-encapsulated packets inside MPLS */
8047 if (label_stack_depth
> 0)
8048 bpf_error("no VLAN match after MPLS");
8051 * Check for a VLAN packet, and then change the offsets to point
8052 * to the type and data fields within the VLAN packet. Just
8053 * increment the offsets, so that we can support a hierarchy, e.g.
8054 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8057 * XXX - this is a bit of a kludge. If we were to split the
8058 * compiler into a parser that parses an expression and
8059 * generates an expression tree, and a code generator that
8060 * takes an expression tree (which could come from our
8061 * parser or from some other parser) and generates BPF code,
8062 * we could perhaps make the offsets parameters of routines
8063 * and, in the handler for an "AND" node, pass to subnodes
8064 * other than the VLAN node the adjusted offsets.
8066 * This would mean that "vlan" would, instead of changing the
8067 * behavior of *all* tests after it, change only the behavior
8068 * of tests ANDed with it. That would change the documented
8069 * semantics of "vlan", which might break some expressions.
8070 * However, it would mean that "(vlan and ip) or ip" would check
8071 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8072 * checking only for VLAN-encapsulated IP, so that could still
8073 * be considered worth doing; it wouldn't break expressions
8074 * that are of the form "vlan and ..." or "vlan N and ...",
8075 * which I suspect are the most common expressions involving
8076 * "vlan". "vlan or ..." doesn't necessarily do what the user
8077 * would really want, now, as all the "or ..." tests would
8078 * be done assuming a VLAN, even though the "or" could be viewed
8079 * as meaning "or, if this isn't a VLAN packet...".
8084 case DLT_NETANALYZER
:
8085 case DLT_NETANALYZER_TRANSPARENT
:
8086 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8087 /* Verify that this is the outer part of the packet and
8088 * not encapsulated somehow. */
8089 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
8090 off_linkhdr
.constant_part
==
8091 off_outermostlinkhdr
.constant_part
) {
8093 * Do we need special VLAN handling?
8095 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8096 b0
= gen_vlan_bpf_extensions(vlan_num
);
8098 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8101 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8104 case DLT_IEEE802_11
:
8105 case DLT_PRISM_HEADER
:
8106 case DLT_IEEE802_11_RADIO_AVS
:
8107 case DLT_IEEE802_11_RADIO
:
8108 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8112 bpf_error("no VLAN support for data link type %d",
8129 struct block
*b0
, *b1
;
8131 if (label_stack_depth
> 0) {
8132 /* just match the bottom-of-stack bit clear */
8133 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8136 * We're not in an MPLS stack yet, so check the link-layer
8137 * type against MPLS.
8141 case DLT_C_HDLC
: /* fall through */
8143 case DLT_NETANALYZER
:
8144 case DLT_NETANALYZER_TRANSPARENT
:
8145 b0
= gen_linktype(ETHERTYPE_MPLS
);
8149 b0
= gen_linktype(PPP_MPLS_UCAST
);
8152 /* FIXME add other DLT_s ...
8153 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8154 * leave it for now */
8157 bpf_error("no MPLS support for data link type %d",
8165 /* If a specific MPLS label is requested, check it */
8166 if (label_num
>= 0) {
8167 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8168 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8169 0xfffff000); /* only compare the first 20 bits */
8175 * Change the offsets to point to the type and data fields within
8176 * the MPLS packet. Just increment the offsets, so that we
8177 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8178 * capture packets with an outer label of 100000 and an inner
8181 * Increment the MPLS stack depth as well; this indicates that
8182 * we're checking MPLS-encapsulated headers, to make sure higher
8183 * level code generators don't try to match against IP-related
8184 * protocols such as Q_ARP, Q_RARP etc.
8186 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8190 label_stack_depth
++;
8195 * Support PPPOE discovery and session.
8200 /* check for PPPoE discovery */
8201 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8205 gen_pppoes(sess_num
)
8208 struct block
*b0
, *b1
;
8211 * Test against the PPPoE session link-layer type.
8213 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8215 /* If a specific session is requested, check PPPoE session id */
8216 if (sess_num
>= 0) {
8217 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8218 (bpf_int32
)sess_num
, 0x0000ffff);
8224 * Change the offsets to point to the type and data fields within
8225 * the PPP packet, and note that this is PPPoE rather than
8228 * XXX - this is a bit of a kludge. If we were to split the
8229 * compiler into a parser that parses an expression and
8230 * generates an expression tree, and a code generator that
8231 * takes an expression tree (which could come from our
8232 * parser or from some other parser) and generates BPF code,
8233 * we could perhaps make the offsets parameters of routines
8234 * and, in the handler for an "AND" node, pass to subnodes
8235 * other than the PPPoE node the adjusted offsets.
8237 * This would mean that "pppoes" would, instead of changing the
8238 * behavior of *all* tests after it, change only the behavior
8239 * of tests ANDed with it. That would change the documented
8240 * semantics of "pppoes", which might break some expressions.
8241 * However, it would mean that "(pppoes and ip) or ip" would check
8242 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8243 * checking only for VLAN-encapsulated IP, so that could still
8244 * be considered worth doing; it wouldn't break expressions
8245 * that are of the form "pppoes and ..." which I suspect are the
8246 * most common expressions involving "pppoes". "pppoes or ..."
8247 * doesn't necessarily do what the user would really want, now,
8248 * as all the "or ..." tests would be done assuming PPPoE, even
8249 * though the "or" could be viewed as meaning "or, if this isn't
8250 * a PPPoE packet...".
8252 * The "network-layer" protocol is PPPoE, which has a 6-byte
8253 * PPPoE header, followed by a PPP packet.
8255 * There is no HDLC encapsulation for the PPP packet (it's
8256 * encapsulated in PPPoES instead), so the link-layer type
8257 * starts at the first byte of the PPP packet. For PPPoE,
8258 * that offset is relative to the beginning of the total
8259 * link-layer payload, including any 802.2 LLC header, so
8260 * it's 6 bytes past off_nl.
8262 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8263 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8266 off_linktype
= off_linkhdr
;
8267 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8270 off_nl_nosnap
= 0; /* no 802.2 LLC */
8275 /* Check that this is Geneve and the VNI is correct if
8276 * specified. Parameterized to handle both IPv4 and IPv6. */
8277 static struct block
*
8278 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8279 enum e_offrel offrel
, int vni
)
8281 struct block
*b0
, *b1
;
8283 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8285 /* Check that we are operating on version 0. Otherwise, we
8286 * can't decode the rest of the fields. The version is 2 bits
8287 * in the first byte of the Geneve header. */
8288 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8293 vni
<<= 8; /* VNI is in the upper 3 bytes */
8294 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8303 /* The IPv4 and IPv6 Geneve checks need to do two things:
8304 * - Verify that this actually is Geneve with the right VNI.
8305 * - Place the IP header length (plus variable link prefix if
8306 * needed) into register A to be used later to compute
8307 * the inner packet offsets. */
8308 static struct block
*
8309 gen_geneve4(int vni
)
8311 struct block
*b0
, *b1
;
8312 struct slist
*s
, *s1
;
8314 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8316 /* Load the IP header length into A. */
8317 s
= gen_loadx_iphdrlen();
8319 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8322 /* Forcibly append these statements to the true condition
8323 * of the protocol check by creating a new block that is
8324 * always true and ANDing them. */
8325 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8334 static struct block
*
8335 gen_geneve6(int vni
)
8337 struct block
*b0
, *b1
;
8338 struct slist
*s
, *s1
;
8340 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8342 /* Load the IP header length. We need to account for a
8343 * variable length link prefix if there is one. */
8344 s
= gen_abs_offset_varpart(&off_linkpl
);
8346 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8350 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8354 s
= new_stmt(BPF_LD
|BPF_IMM
);
8358 /* Forcibly append these statements to the true condition
8359 * of the protocol check by creating a new block that is
8360 * always true and ANDing them. */
8361 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8364 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8373 /* We need to store three values based on the Geneve header::
8374 * - The offset of the linktype.
8375 * - The offset of the end of the Geneve header.
8376 * - The offset of the end of the encapsulated MAC header. */
8377 static struct slist
*
8378 gen_geneve_offsets(void)
8380 struct slist
*s
, *s1
, *s_proto
;
8382 /* First we need to calculate the offset of the Geneve header
8383 * itself. This is composed of the IP header previously calculated
8384 * (include any variable link prefix) and stored in A plus the
8385 * fixed sized headers (fixed link prefix, MAC length, and UDP
8387 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8388 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8390 /* Stash this in X since we'll need it later. */
8391 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8394 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8396 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8400 off_linktype
.reg
= alloc_reg();
8401 off_linktype
.is_variable
= 1;
8402 off_linktype
.constant_part
= 0;
8404 s1
= new_stmt(BPF_ST
);
8405 s1
->s
.k
= off_linktype
.reg
;
8408 /* Load the Geneve option length and mask and shift to get the
8409 * number of bytes. It is stored in the first byte of the Geneve
8411 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8415 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8419 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8423 /* Add in the rest of the Geneve base header. */
8424 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8428 /* Add the Geneve header length to its offset and store. */
8429 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8433 /* Set the encapsulated type as Ethernet. Even though we may
8434 * not actually have Ethernet inside there are two reasons this
8436 * - The linktype field is always in EtherType format regardless
8437 * of whether it is in Geneve or an inner Ethernet frame.
8438 * - The only link layer that we have specific support for is
8439 * Ethernet. We will confirm that the packet actually is
8440 * Ethernet at runtime before executing these checks. */
8441 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8443 s1
= new_stmt(BPF_ST
);
8444 s1
->s
.k
= off_linkhdr
.reg
;
8447 /* Calculate whether we have an Ethernet header or just raw IP/
8448 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8449 * and linktype by 14 bytes so that the network header can be found
8450 * seamlessly. Otherwise, keep what we've calculated already. */
8452 /* We have a bare jmp so we can't use the optimizer. */
8455 /* Load the EtherType in the Geneve header, 2 bytes in. */
8456 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8460 /* Load X with the end of the Geneve header. */
8461 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8462 s1
->s
.k
= off_linkhdr
.reg
;
8465 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8466 * end of this check, we should have the total length in X. In
8467 * the non-Ethernet case, it's already there. */
8468 s_proto
= new_stmt(JMP(BPF_JEQ
));
8469 s_proto
->s
.k
= ETHERTYPE_TEB
;
8470 sappend(s
, s_proto
);
8472 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8476 /* Since this is Ethernet, use the EtherType of the payload
8477 * directly as the linktype. Overwrite what we already have. */
8478 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8482 s1
= new_stmt(BPF_ST
);
8483 s1
->s
.k
= off_linktype
.reg
;
8486 /* Advance two bytes further to get the end of the Ethernet
8488 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8492 /* Move the result to X. */
8493 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8496 /* Store the final result of our linkpl calculation. */
8497 off_linkpl
.reg
= alloc_reg();
8498 off_linkpl
.is_variable
= 1;
8499 off_linkpl
.constant_part
= 0;
8501 s1
= new_stmt(BPF_STX
);
8502 s1
->s
.k
= off_linkpl
.reg
;
8511 /* Check to see if this is a Geneve packet. */
8515 struct block
*b0
, *b1
;
8518 b0
= gen_geneve4(vni
);
8519 b1
= gen_geneve6(vni
);
8524 /* Later filters should act on the payload of the Geneve frame,
8525 * update all of the header pointers. Attach this code so that
8526 * it gets executed in the event that the Geneve filter matches. */
8527 s
= gen_geneve_offsets();
8530 sappend(s
, b1
->stmts
);
8540 /* Check that the encapsulated frame has a link layer header
8541 * for Ethernet filters. */
8542 static struct block
*
8543 gen_geneve_ll_check()
8546 struct slist
*s
, *s1
;
8548 /* The easiest way to see if there is a link layer present
8549 * is to check if the link layer header and payload are not
8552 /* Geneve always generates pure variable offsets so we can
8553 * compare only the registers. */
8554 s
= new_stmt(BPF_LD
|BPF_MEM
);
8555 s
->s
.k
= off_linkhdr
.reg
;
8557 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8558 s1
->s
.k
= off_linkpl
.reg
;
8561 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8570 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8582 bpf_error("'vpi' supported only on raw ATM");
8583 if (off_vpi
== (u_int
)-1)
8585 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8591 bpf_error("'vci' supported only on raw ATM");
8592 if (off_vci
== (u_int
)-1)
8594 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8599 if (off_proto
== (u_int
)-1)
8600 abort(); /* XXX - this isn't on FreeBSD */
8601 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8606 if (off_payload
== (u_int
)-1)
8608 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8609 0xffffffff, jtype
, reverse
, jvalue
);
8614 bpf_error("'callref' supported only on raw ATM");
8615 if (off_proto
== (u_int
)-1)
8617 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8618 jtype
, reverse
, jvalue
);
8628 gen_atmtype_abbrev(type
)
8631 struct block
*b0
, *b1
;
8636 /* Get all packets in Meta signalling Circuit */
8638 bpf_error("'metac' supported only on raw ATM");
8639 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8640 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8645 /* Get all packets in Broadcast Circuit*/
8647 bpf_error("'bcc' supported only on raw ATM");
8648 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8649 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8654 /* Get all cells in Segment OAM F4 circuit*/
8656 bpf_error("'oam4sc' supported only on raw ATM");
8657 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8658 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8663 /* Get all cells in End-to-End OAM F4 Circuit*/
8665 bpf_error("'oam4ec' supported only on raw ATM");
8666 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8667 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8672 /* Get all packets in connection Signalling Circuit */
8674 bpf_error("'sc' supported only on raw ATM");
8675 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8676 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8681 /* Get all packets in ILMI Circuit */
8683 bpf_error("'ilmic' supported only on raw ATM");
8684 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8685 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8690 /* Get all LANE packets */
8692 bpf_error("'lane' supported only on raw ATM");
8693 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8696 * Arrange that all subsequent tests assume LANE
8697 * rather than LLC-encapsulated packets, and set
8698 * the offsets appropriately for LANE-encapsulated
8701 * We assume LANE means Ethernet, not Token Ring.
8703 PUSH_LINKHDR(DLT_EN10MB
, 0,
8704 off_payload
+ 2, /* Ethernet header */
8706 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8707 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8708 off_nl
= 0; /* Ethernet II */
8709 off_nl_nosnap
= 3; /* 802.3+802.2 */
8713 /* Get all LLC-encapsulated packets */
8715 bpf_error("'llc' supported only on raw ATM");
8716 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8717 linktype
= prevlinktype
;
8727 * Filtering for MTP2 messages based on li value
8728 * FISU, length is null
8729 * LSSU, length is 1 or 2
8730 * MSU, length is 3 or more
8731 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8734 gen_mtp2type_abbrev(type
)
8737 struct block
*b0
, *b1
;
8742 if ( (linktype
!= DLT_MTP2
) &&
8743 (linktype
!= DLT_ERF
) &&
8744 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8745 bpf_error("'fisu' supported only on MTP2");
8746 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8747 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8751 if ( (linktype
!= DLT_MTP2
) &&
8752 (linktype
!= DLT_ERF
) &&
8753 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8754 bpf_error("'lssu' supported only on MTP2");
8755 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8756 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8761 if ( (linktype
!= DLT_MTP2
) &&
8762 (linktype
!= DLT_ERF
) &&
8763 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8764 bpf_error("'msu' supported only on MTP2");
8765 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8769 if ( (linktype
!= DLT_MTP2
) &&
8770 (linktype
!= DLT_ERF
) &&
8771 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8772 bpf_error("'hfisu' supported only on MTP2_HSL");
8773 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8774 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8778 if ( (linktype
!= DLT_MTP2
) &&
8779 (linktype
!= DLT_ERF
) &&
8780 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8781 bpf_error("'hlssu' supported only on MTP2_HSL");
8782 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8783 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8788 if ( (linktype
!= DLT_MTP2
) &&
8789 (linktype
!= DLT_ERF
) &&
8790 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8791 bpf_error("'hmsu' supported only on MTP2_HSL");
8792 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8802 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8809 bpf_u_int32 val1
, val2
, val3
;
8810 u_int newoff_sio
=off_sio
;
8811 u_int newoff_opc
=off_opc
;
8812 u_int newoff_dpc
=off_dpc
;
8813 u_int newoff_sls
=off_sls
;
8815 switch (mtp3field
) {
8818 newoff_sio
+= 3; /* offset for MTP2_HSL */
8822 if (off_sio
== (u_int
)-1)
8823 bpf_error("'sio' supported only on SS7");
8824 /* sio coded on 1 byte so max value 255 */
8826 bpf_error("sio value %u too big; max value = 255",
8828 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8829 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8835 if (off_opc
== (u_int
)-1)
8836 bpf_error("'opc' supported only on SS7");
8837 /* opc coded on 14 bits so max value 16383 */
8839 bpf_error("opc value %u too big; max value = 16383",
8841 /* the following instructions are made to convert jvalue
8842 * to the form used to write opc in an ss7 message*/
8843 val1
= jvalue
& 0x00003c00;
8845 val2
= jvalue
& 0x000003fc;
8847 val3
= jvalue
& 0x00000003;
8849 jvalue
= val1
+ val2
+ val3
;
8850 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8851 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8859 if (off_dpc
== (u_int
)-1)
8860 bpf_error("'dpc' supported only on SS7");
8861 /* dpc coded on 14 bits so max value 16383 */
8863 bpf_error("dpc value %u too big; max value = 16383",
8865 /* the following instructions are made to convert jvalue
8866 * to the forme used to write dpc in an ss7 message*/
8867 val1
= jvalue
& 0x000000ff;
8869 val2
= jvalue
& 0x00003f00;
8871 jvalue
= val1
+ val2
;
8872 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8873 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8879 if (off_sls
== (u_int
)-1)
8880 bpf_error("'sls' supported only on SS7");
8881 /* sls coded on 4 bits so max value 15 */
8883 bpf_error("sls value %u too big; max value = 15",
8885 /* the following instruction is made to convert jvalue
8886 * to the forme used to write sls in an ss7 message*/
8887 jvalue
= jvalue
<< 4;
8888 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8889 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8898 static struct block
*
8899 gen_msg_abbrev(type
)
8905 * Q.2931 signalling protocol messages for handling virtual circuits
8906 * establishment and teardown
8911 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8915 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8919 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8923 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8927 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8930 case A_RELEASE_DONE
:
8931 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8941 gen_atmmulti_abbrev(type
)
8944 struct block
*b0
, *b1
;
8950 bpf_error("'oam' supported only on raw ATM");
8951 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8956 bpf_error("'oamf4' supported only on raw ATM");
8958 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8959 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8961 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8967 * Get Q.2931 signalling messages for switched
8968 * virtual connection
8971 bpf_error("'connectmsg' supported only on raw ATM");
8972 b0
= gen_msg_abbrev(A_SETUP
);
8973 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8975 b0
= gen_msg_abbrev(A_CONNECT
);
8977 b0
= gen_msg_abbrev(A_CONNECTACK
);
8979 b0
= gen_msg_abbrev(A_RELEASE
);
8981 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8983 b0
= gen_atmtype_abbrev(A_SC
);
8989 bpf_error("'metaconnect' supported only on raw ATM");
8990 b0
= gen_msg_abbrev(A_SETUP
);
8991 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8993 b0
= gen_msg_abbrev(A_CONNECT
);
8995 b0
= gen_msg_abbrev(A_RELEASE
);
8997 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8999 b0
= gen_atmtype_abbrev(A_METAC
);