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
;
2452 struct slist
*sjset_radiotap_tsft
;
2453 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2454 struct slist
*s_roundup
;
2456 if (off_linkpl
.reg
== -1) {
2458 * No register has been assigned to the offset of
2459 * the link-layer payload, which means nobody needs
2460 * it; don't bother computing it - just return
2461 * what we already have.
2467 * This code is not compatible with the optimizer, as
2468 * we are generating jmp instructions within a normal
2469 * slist of instructions
2474 * If "s" is non-null, it has code to arrange that the X register
2475 * contains the length of the prefix preceding the link-layer
2478 * Otherwise, the length of the prefix preceding the link-layer
2479 * header is "off_outermostlinkhdr.constant_part".
2483 * There is no variable-length header preceding the
2484 * link-layer header.
2486 * Load the length of the fixed-length prefix preceding
2487 * the link-layer header (if any) into the X register,
2488 * and store it in the off_linkpl.reg register.
2489 * That length is off_outermostlinkhdr.constant_part.
2491 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2492 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2496 * The X register contains the offset of the beginning of the
2497 * link-layer header; add 24, which is the minimum length
2498 * of the MAC header for a data frame, to that, and store it
2499 * in off_linkpl.reg, and then load the Frame Control field,
2500 * which is at the offset in the X register, with an indexed load.
2502 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2504 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2507 s2
= new_stmt(BPF_ST
);
2508 s2
->s
.k
= off_linkpl
.reg
;
2511 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2516 * Check the Frame Control field to see if this is a data frame;
2517 * a data frame has the 0x08 bit (b3) in that field set and the
2518 * 0x04 bit (b2) clear.
2520 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2521 sjset_data_frame_1
->s
.k
= 0x08;
2522 sappend(s
, sjset_data_frame_1
);
2525 * If b3 is set, test b2, otherwise go to the first statement of
2526 * the rest of the program.
2528 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2529 sjset_data_frame_2
->s
.k
= 0x04;
2530 sappend(s
, sjset_data_frame_2
);
2531 sjset_data_frame_1
->s
.jf
= snext
;
2534 * If b2 is not set, this is a data frame; test the QoS bit.
2535 * Otherwise, go to the first statement of the rest of the
2538 sjset_data_frame_2
->s
.jt
= snext
;
2539 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2540 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2541 sappend(s
, sjset_qos
);
2544 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2546 * Otherwise, go to the first statement of the rest of the
2549 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2550 s2
->s
.k
= off_linkpl
.reg
;
2552 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2555 s2
= new_stmt(BPF_ST
);
2556 s2
->s
.k
= off_linkpl
.reg
;
2560 * If we have a radiotap header, look at it to see whether
2561 * there's Atheros padding between the MAC-layer header
2564 * Note: all of the fields in the radiotap header are
2565 * little-endian, so we byte-swap all of the values
2566 * we test against, as they will be loaded as big-endian
2569 if (linktype
== DLT_IEEE802_11_RADIO
) {
2571 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2572 * in the presence flag?
2574 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2578 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2579 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2580 sappend(s
, sjset_radiotap_flags
);
2583 * If not, skip all of this.
2585 sjset_radiotap_flags
->s
.jf
= snext
;
2588 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2590 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2591 new_stmt(JMP(BPF_JSET
));
2592 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2593 sappend(s
, sjset_radiotap_tsft
);
2596 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2597 * at an offset of 16 from the beginning of the raw packet
2598 * data (8 bytes for the radiotap header and 8 bytes for
2601 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2604 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2608 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2609 sjset_tsft_datapad
->s
.k
= 0x20;
2610 sappend(s
, sjset_tsft_datapad
);
2613 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2614 * at an offset of 8 from the beginning of the raw packet
2615 * data (8 bytes for the radiotap header).
2617 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2620 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2624 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2625 sjset_notsft_datapad
->s
.k
= 0x20;
2626 sappend(s
, sjset_notsft_datapad
);
2629 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2630 * set, round the length of the 802.11 header to
2631 * a multiple of 4. Do that by adding 3 and then
2632 * dividing by and multiplying by 4, which we do by
2635 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2636 s_roundup
->s
.k
= off_linkpl
.reg
;
2637 sappend(s
, s_roundup
);
2638 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2641 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2644 s2
= new_stmt(BPF_ST
);
2645 s2
->s
.k
= off_linkpl
.reg
;
2648 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2649 sjset_tsft_datapad
->s
.jf
= snext
;
2650 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2651 sjset_notsft_datapad
->s
.jf
= snext
;
2653 sjset_qos
->s
.jf
= snext
;
2659 insert_compute_vloffsets(b
)
2664 /* There is an implicit dependency between the link
2665 * payload and link header since the payload computation
2666 * includes the variable part of the header. Therefore,
2667 * if nobody else has allocated a register for the link
2668 * header and we need it, do it now. */
2669 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2670 off_linkhdr
.reg
== -1)
2671 off_linkhdr
.reg
= alloc_reg();
2674 * For link-layer types that have a variable-length header
2675 * preceding the link-layer header, generate code to load
2676 * the offset of the link-layer header into the register
2677 * assigned to that offset, if any.
2679 * XXX - this, and the next switch statement, won't handle
2680 * encapsulation of 802.11 or 802.11+radio information in
2681 * some other protocol stack. That's significantly more
2684 switch (outermostlinktype
) {
2686 case DLT_PRISM_HEADER
:
2687 s
= gen_load_prism_llprefixlen();
2690 case DLT_IEEE802_11_RADIO_AVS
:
2691 s
= gen_load_avs_llprefixlen();
2694 case DLT_IEEE802_11_RADIO
:
2695 s
= gen_load_radiotap_llprefixlen();
2699 s
= gen_load_ppi_llprefixlen();
2708 * For link-layer types that have a variable-length link-layer
2709 * header, generate code to load the offset of the link-layer
2710 * payload into the register assigned to that offset, if any.
2712 switch (outermostlinktype
) {
2714 case DLT_IEEE802_11
:
2715 case DLT_PRISM_HEADER
:
2716 case DLT_IEEE802_11_RADIO_AVS
:
2717 case DLT_IEEE802_11_RADIO
:
2719 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2724 * If we have any offset-loading code, append all the
2725 * existing statements in the block to those statements,
2726 * and make the resulting list the list of statements
2730 sappend(s
, b
->stmts
);
2735 static struct block
*
2736 gen_ppi_dlt_check(void)
2738 struct slist
*s_load_dlt
;
2741 if (linktype
== DLT_PPI
)
2743 /* Create the statements that check for the DLT
2745 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2746 s_load_dlt
->s
.k
= 4;
2748 b
= new_block(JMP(BPF_JEQ
));
2750 b
->stmts
= s_load_dlt
;
2751 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2762 * Take an absolute offset, and:
2764 * if it has no variable part, return NULL;
2766 * if it has a variable part, generate code to load the register
2767 * containing that variable part into the X register, returning
2768 * a pointer to that code - if no register for that offset has
2769 * been allocated, allocate it first.
2771 * (The code to set that register will be generated later, but will
2772 * be placed earlier in the code sequence.)
2774 static struct slist
*
2775 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2779 if (off
->is_variable
) {
2780 if (off
->reg
== -1) {
2782 * We haven't yet assigned a register for the
2783 * variable part of the offset of the link-layer
2784 * header; allocate one.
2786 off
->reg
= alloc_reg();
2790 * Load the register containing the variable part of the
2791 * offset of the link-layer header into the X register.
2793 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2798 * That offset isn't variable, there's no variable part,
2799 * so we don't need to generate any code.
2806 * Map an Ethernet type to the equivalent PPP type.
2809 ethertype_to_ppptype(proto
)
2818 case ETHERTYPE_IPV6
:
2826 case ETHERTYPE_ATALK
:
2840 * I'm assuming the "Bridging PDU"s that go
2841 * over PPP are Spanning Tree Protocol
2855 * Generate any tests that, for encapsulation of a link-layer packet
2856 * inside another protocol stack, need to be done to check for those
2857 * link-layer packets (and that haven't already been done by a check
2858 * for that encapsulation).
2860 static struct block
*
2861 gen_prevlinkhdr_check(void)
2866 return gen_geneve_ll_check();
2868 switch (prevlinktype
) {
2872 * This is LANE-encapsulated Ethernet; check that the LANE
2873 * packet doesn't begin with an LE Control marker, i.e.
2874 * that it's data, not a control message.
2876 * (We've already generated a test for LANE.)
2878 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2884 * No such tests are necessary.
2892 * Generate code to match a particular packet type by matching the
2893 * link-layer type field or fields in the 802.2 LLC header.
2895 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2896 * value, if <= ETHERMTU.
2898 static struct block
*
2902 struct block
*b0
, *b1
, *b2
;
2903 const char *description
;
2905 /* are we checking MPLS-encapsulated packets? */
2906 if (label_stack_depth
> 0) {
2910 /* FIXME add other L3 proto IDs */
2911 return gen_mpls_linktype(Q_IP
);
2913 case ETHERTYPE_IPV6
:
2915 /* FIXME add other L3 proto IDs */
2916 return gen_mpls_linktype(Q_IPV6
);
2919 bpf_error("unsupported protocol over mpls");
2927 case DLT_NETANALYZER
:
2928 case DLT_NETANALYZER_TRANSPARENT
:
2929 /* Geneve has an EtherType regardless of whether there is an
2932 b0
= gen_prevlinkhdr_check();
2936 b1
= gen_ether_linktype(proto
);
2947 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2951 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2957 case DLT_IEEE802_11
:
2958 case DLT_PRISM_HEADER
:
2959 case DLT_IEEE802_11_RADIO_AVS
:
2960 case DLT_IEEE802_11_RADIO
:
2963 * Check that we have a data frame.
2965 b0
= gen_check_802_11_data_frame();
2968 * Now check for the specified link-layer type.
2970 b1
= gen_llc_linktype(proto
);
2978 * XXX - check for LLC frames.
2980 return gen_llc_linktype(proto
);
2986 * XXX - check for LLC PDUs, as per IEEE 802.5.
2988 return gen_llc_linktype(proto
);
2992 case DLT_ATM_RFC1483
:
2994 case DLT_IP_OVER_FC
:
2995 return gen_llc_linktype(proto
);
3001 * Check for an LLC-encapsulated version of this protocol;
3002 * if we were checking for LANE, linktype would no longer
3005 * Check for LLC encapsulation and then check the protocol.
3007 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3008 b1
= gen_llc_linktype(proto
);
3015 return gen_linux_sll_linktype(proto
);
3020 case DLT_SLIP_BSDOS
:
3023 * These types don't provide any type field; packets
3024 * are always IPv4 or IPv6.
3026 * XXX - for IPv4, check for a version number of 4, and,
3027 * for IPv6, check for a version number of 6?
3032 /* Check for a version number of 4. */
3033 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3035 case ETHERTYPE_IPV6
:
3036 /* Check for a version number of 6. */
3037 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3040 return gen_false(); /* always false */
3047 * Raw IPv4, so no type field.
3049 if (proto
== ETHERTYPE_IP
)
3050 return gen_true(); /* always true */
3052 /* Checking for something other than IPv4; always false */
3059 * Raw IPv6, so no type field.
3061 if (proto
== ETHERTYPE_IPV6
)
3062 return gen_true(); /* always true */
3064 /* Checking for something other than IPv6; always false */
3071 case DLT_PPP_SERIAL
:
3074 * We use Ethernet protocol types inside libpcap;
3075 * map them to the corresponding PPP protocol types.
3077 proto
= ethertype_to_ppptype(proto
);
3078 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3084 * We use Ethernet protocol types inside libpcap;
3085 * map them to the corresponding PPP protocol types.
3091 * Also check for Van Jacobson-compressed IP.
3092 * XXX - do this for other forms of PPP?
3094 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3095 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3097 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3102 proto
= ethertype_to_ppptype(proto
);
3103 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3113 * For DLT_NULL, the link-layer header is a 32-bit
3114 * word containing an AF_ value in *host* byte order,
3115 * and for DLT_ENC, the link-layer header begins
3116 * with a 32-bit work containing an AF_ value in
3119 * In addition, if we're reading a saved capture file,
3120 * the host byte order in the capture may not be the
3121 * same as the host byte order on this machine.
3123 * For DLT_LOOP, the link-layer header is a 32-bit
3124 * word containing an AF_ value in *network* byte order.
3126 * XXX - AF_ values may, unfortunately, be platform-
3127 * dependent; for example, FreeBSD's AF_INET6 is 24
3128 * whilst NetBSD's and OpenBSD's is 26.
3130 * This means that, when reading a capture file, just
3131 * checking for our AF_INET6 value won't work if the
3132 * capture file came from another OS.
3141 case ETHERTYPE_IPV6
:
3148 * Not a type on which we support filtering.
3149 * XXX - support those that have AF_ values
3150 * #defined on this platform, at least?
3155 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3157 * The AF_ value is in host byte order, but
3158 * the BPF interpreter will convert it to
3159 * network byte order.
3161 * If this is a save file, and it's from a
3162 * machine with the opposite byte order to
3163 * ours, we byte-swap the AF_ value.
3165 * Then we run it through "htonl()", and
3166 * generate code to compare against the result.
3168 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3169 proto
= SWAPLONG(proto
);
3170 proto
= htonl(proto
);
3172 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3174 #ifdef HAVE_NET_PFVAR_H
3177 * af field is host byte order in contrast to the rest of
3180 if (proto
== ETHERTYPE_IP
)
3181 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3182 BPF_B
, (bpf_int32
)AF_INET
));
3183 else if (proto
== ETHERTYPE_IPV6
)
3184 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3185 BPF_B
, (bpf_int32
)AF_INET6
));
3190 #endif /* HAVE_NET_PFVAR_H */
3193 case DLT_ARCNET_LINUX
:
3195 * XXX should we check for first fragment if the protocol
3203 case ETHERTYPE_IPV6
:
3204 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3205 (bpf_int32
)ARCTYPE_INET6
));
3208 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3209 (bpf_int32
)ARCTYPE_IP
);
3210 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3211 (bpf_int32
)ARCTYPE_IP_OLD
);
3216 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3217 (bpf_int32
)ARCTYPE_ARP
);
3218 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3219 (bpf_int32
)ARCTYPE_ARP_OLD
);
3223 case ETHERTYPE_REVARP
:
3224 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3225 (bpf_int32
)ARCTYPE_REVARP
));
3227 case ETHERTYPE_ATALK
:
3228 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3229 (bpf_int32
)ARCTYPE_ATALK
));
3236 case ETHERTYPE_ATALK
:
3246 * XXX - assumes a 2-byte Frame Relay header with
3247 * DLCI and flags. What if the address is longer?
3253 * Check for the special NLPID for IP.
3255 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3257 case ETHERTYPE_IPV6
:
3259 * Check for the special NLPID for IPv6.
3261 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3265 * Check for several OSI protocols.
3267 * Frame Relay packets typically have an OSI
3268 * NLPID at the beginning; we check for each
3271 * What we check for is the NLPID and a frame
3272 * control field of UI, i.e. 0x03 followed
3275 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3276 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3277 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3289 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3291 case DLT_JUNIPER_MFR
:
3292 case DLT_JUNIPER_MLFR
:
3293 case DLT_JUNIPER_MLPPP
:
3294 case DLT_JUNIPER_ATM1
:
3295 case DLT_JUNIPER_ATM2
:
3296 case DLT_JUNIPER_PPPOE
:
3297 case DLT_JUNIPER_PPPOE_ATM
:
3298 case DLT_JUNIPER_GGSN
:
3299 case DLT_JUNIPER_ES
:
3300 case DLT_JUNIPER_MONITOR
:
3301 case DLT_JUNIPER_SERVICES
:
3302 case DLT_JUNIPER_ETHER
:
3303 case DLT_JUNIPER_PPP
:
3304 case DLT_JUNIPER_FRELAY
:
3305 case DLT_JUNIPER_CHDLC
:
3306 case DLT_JUNIPER_VP
:
3307 case DLT_JUNIPER_ST
:
3308 case DLT_JUNIPER_ISM
:
3309 case DLT_JUNIPER_VS
:
3310 case DLT_JUNIPER_SRX_E2E
:
3311 case DLT_JUNIPER_FIBRECHANNEL
:
3312 case DLT_JUNIPER_ATM_CEMIC
:
3314 /* just lets verify the magic number for now -
3315 * on ATM we may have up to 6 different encapsulations on the wire
3316 * and need a lot of heuristics to figure out that the payload
3319 * FIXME encapsulation specific BPF_ filters
3321 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3323 case DLT_BACNET_MS_TP
:
3324 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3327 return gen_ipnet_linktype(proto
);
3329 case DLT_LINUX_IRDA
:
3330 bpf_error("IrDA link-layer type filtering not implemented");
3333 bpf_error("DOCSIS link-layer type filtering not implemented");
3336 case DLT_MTP2_WITH_PHDR
:
3337 bpf_error("MTP2 link-layer type filtering not implemented");
3340 bpf_error("ERF link-layer type filtering not implemented");
3343 bpf_error("PFSYNC link-layer type filtering not implemented");
3345 case DLT_LINUX_LAPD
:
3346 bpf_error("LAPD link-layer type filtering not implemented");
3350 case DLT_USB_LINUX_MMAPPED
:
3351 bpf_error("USB link-layer type filtering not implemented");
3353 case DLT_BLUETOOTH_HCI_H4
:
3354 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3355 bpf_error("Bluetooth link-layer type filtering not implemented");
3358 case DLT_CAN_SOCKETCAN
:
3359 bpf_error("CAN link-layer type filtering not implemented");
3361 case DLT_IEEE802_15_4
:
3362 case DLT_IEEE802_15_4_LINUX
:
3363 case DLT_IEEE802_15_4_NONASK_PHY
:
3364 case DLT_IEEE802_15_4_NOFCS
:
3365 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3367 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3368 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3371 bpf_error("SITA link-layer type filtering not implemented");
3374 bpf_error("RAIF1 link-layer type filtering not implemented");
3377 bpf_error("IPMB link-layer type filtering not implemented");
3380 bpf_error("AX.25 link-layer type filtering not implemented");
3383 /* Using the fixed-size NFLOG header it is possible to tell only
3384 * the address family of the packet, other meaningful data is
3385 * either missing or behind TLVs.
3387 bpf_error("NFLOG link-layer type filtering not implemented");
3391 * Does this link-layer header type have a field
3392 * indicating the type of the next protocol? If
3393 * so, off_linktype.constant_part will be the offset of that
3394 * field in the packet; if not, it will be -1.
3396 if (off_linktype
.constant_part
!= (u_int
)-1) {
3398 * Yes; assume it's an Ethernet type. (If
3399 * it's not, it needs to be handled specially
3402 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3405 * No; report an error.
3407 description
= pcap_datalink_val_to_description(linktype
);
3408 if (description
!= NULL
) {
3409 bpf_error("%s link-layer type filtering not implemented",
3412 bpf_error("DLT %u link-layer type filtering not implemented",
3421 * Check for an LLC SNAP packet with a given organization code and
3422 * protocol type; we check the entire contents of the 802.2 LLC and
3423 * snap headers, checking for DSAP and SSAP of SNAP and a control
3424 * field of 0x03 in the LLC header, and for the specified organization
3425 * code and protocol type in the SNAP header.
3427 static struct block
*
3428 gen_snap(orgcode
, ptype
)
3429 bpf_u_int32 orgcode
;
3432 u_char snapblock
[8];
3434 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3435 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3436 snapblock
[2] = 0x03; /* control = UI */
3437 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3438 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3439 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3440 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3441 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3442 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3446 * Generate code to match frames with an LLC header.
3451 struct block
*b0
, *b1
;
3457 * We check for an Ethernet type field less than
3458 * 1500, which means it's an 802.3 length field.
3460 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3464 * Now check for the purported DSAP and SSAP not being
3465 * 0xFF, to rule out NetWare-over-802.3.
3467 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3474 * We check for LLC traffic.
3476 b0
= gen_atmtype_abbrev(A_LLC
);
3479 case DLT_IEEE802
: /* Token Ring */
3481 * XXX - check for LLC frames.
3487 * XXX - check for LLC frames.
3491 case DLT_ATM_RFC1483
:
3493 * For LLC encapsulation, these are defined to have an
3496 * For VC encapsulation, they don't, but there's no
3497 * way to check for that; the protocol used on the VC
3498 * is negotiated out of band.
3502 case DLT_IEEE802_11
:
3503 case DLT_PRISM_HEADER
:
3504 case DLT_IEEE802_11_RADIO
:
3505 case DLT_IEEE802_11_RADIO_AVS
:
3508 * Check that we have a data frame.
3510 b0
= gen_check_802_11_data_frame();
3514 bpf_error("'llc' not supported for linktype %d", linktype
);
3522 struct block
*b0
, *b1
;
3526 * Check whether this is an LLC frame.
3531 * Load the control byte and test the low-order bit; it must
3532 * be clear for I frames.
3534 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3535 b1
= new_block(JMP(BPF_JSET
));
3546 struct block
*b0
, *b1
;
3549 * Check whether this is an LLC frame.
3554 * Now compare the low-order 2 bit of the control byte against
3555 * the appropriate value for S frames.
3557 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3565 struct block
*b0
, *b1
;
3568 * Check whether this is an LLC frame.
3573 * Now compare the low-order 2 bit of the control byte against
3574 * the appropriate value for U frames.
3576 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3582 gen_llc_s_subtype(bpf_u_int32 subtype
)
3584 struct block
*b0
, *b1
;
3587 * Check whether this is an LLC frame.
3592 * Now check for an S frame with the appropriate type.
3594 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3600 gen_llc_u_subtype(bpf_u_int32 subtype
)
3602 struct block
*b0
, *b1
;
3605 * Check whether this is an LLC frame.
3610 * Now check for a U frame with the appropriate type.
3612 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3618 * Generate code to match a particular packet type, for link-layer types
3619 * using 802.2 LLC headers.
3621 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3622 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3624 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3625 * value, if <= ETHERMTU. We use that to determine whether to
3626 * match the DSAP or both DSAP and LSAP or to check the OUI and
3627 * protocol ID in a SNAP header.
3629 static struct block
*
3630 gen_llc_linktype(proto
)
3634 * XXX - handle token-ring variable-length header.
3640 case LLCSAP_NETBEUI
:
3642 * XXX - should we check both the DSAP and the
3643 * SSAP, like this, or should we check just the
3644 * DSAP, as we do for other SAP values?
3646 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3647 ((proto
<< 8) | proto
));
3651 * XXX - are there ever SNAP frames for IPX on
3652 * non-Ethernet 802.x networks?
3654 return gen_cmp(OR_LLC
, 0, BPF_B
,
3655 (bpf_int32
)LLCSAP_IPX
);
3657 case ETHERTYPE_ATALK
:
3659 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3660 * SNAP packets with an organization code of
3661 * 0x080007 (Apple, for Appletalk) and a protocol
3662 * type of ETHERTYPE_ATALK (Appletalk).
3664 * XXX - check for an organization code of
3665 * encapsulated Ethernet as well?
3667 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3671 * XXX - we don't have to check for IPX 802.3
3672 * here, but should we check for the IPX Ethertype?
3674 if (proto
<= ETHERMTU
) {
3676 * This is an LLC SAP value, so check
3679 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3682 * This is an Ethernet type; we assume that it's
3683 * unlikely that it'll appear in the right place
3684 * at random, and therefore check only the
3685 * location that would hold the Ethernet type
3686 * in a SNAP frame with an organization code of
3687 * 0x000000 (encapsulated Ethernet).
3689 * XXX - if we were to check for the SNAP DSAP and
3690 * LSAP, as per XXX, and were also to check for an
3691 * organization code of 0x000000 (encapsulated
3692 * Ethernet), we'd do
3694 * return gen_snap(0x000000, proto);
3696 * here; for now, we don't, as per the above.
3697 * I don't know whether it's worth the extra CPU
3698 * time to do the right check or not.
3700 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3705 static struct block
*
3706 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3710 u_int src_off
, dst_off
;
3712 struct block
*b0
, *b1
;
3726 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3727 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3733 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3734 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3741 b0
= gen_linktype(proto
);
3742 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3748 static struct block
*
3749 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3750 struct in6_addr
*addr
;
3751 struct in6_addr
*mask
;
3753 u_int src_off
, dst_off
;
3755 struct block
*b0
, *b1
;
3770 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3771 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3777 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3778 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3785 /* this order is important */
3786 a
= (u_int32_t
*)addr
;
3787 m
= (u_int32_t
*)mask
;
3788 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3789 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3791 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3793 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3795 b0
= gen_linktype(proto
);
3801 static struct block
*
3802 gen_ehostop(eaddr
, dir
)
3803 register const u_char
*eaddr
;
3806 register struct block
*b0
, *b1
;
3810 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3813 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3816 b0
= gen_ehostop(eaddr
, Q_SRC
);
3817 b1
= gen_ehostop(eaddr
, Q_DST
);
3823 b0
= gen_ehostop(eaddr
, Q_SRC
);
3824 b1
= gen_ehostop(eaddr
, Q_DST
);
3829 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3833 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3837 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3841 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3845 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3849 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3857 * Like gen_ehostop, but for DLT_FDDI
3859 static struct block
*
3860 gen_fhostop(eaddr
, dir
)
3861 register const u_char
*eaddr
;
3864 struct block
*b0
, *b1
;
3868 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3871 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3874 b0
= gen_fhostop(eaddr
, Q_SRC
);
3875 b1
= gen_fhostop(eaddr
, Q_DST
);
3881 b0
= gen_fhostop(eaddr
, Q_SRC
);
3882 b1
= gen_fhostop(eaddr
, Q_DST
);
3887 bpf_error("'addr1' is only supported on 802.11");
3891 bpf_error("'addr2' is only supported on 802.11");
3895 bpf_error("'addr3' is only supported on 802.11");
3899 bpf_error("'addr4' is only supported on 802.11");
3903 bpf_error("'ra' is only supported on 802.11");
3907 bpf_error("'ta' is only supported on 802.11");
3915 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3917 static struct block
*
3918 gen_thostop(eaddr
, dir
)
3919 register const u_char
*eaddr
;
3922 register struct block
*b0
, *b1
;
3926 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3929 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3932 b0
= gen_thostop(eaddr
, Q_SRC
);
3933 b1
= gen_thostop(eaddr
, Q_DST
);
3939 b0
= gen_thostop(eaddr
, Q_SRC
);
3940 b1
= gen_thostop(eaddr
, Q_DST
);
3945 bpf_error("'addr1' is only supported on 802.11");
3949 bpf_error("'addr2' is only supported on 802.11");
3953 bpf_error("'addr3' is only supported on 802.11");
3957 bpf_error("'addr4' is only supported on 802.11");
3961 bpf_error("'ra' is only supported on 802.11");
3965 bpf_error("'ta' is only supported on 802.11");
3973 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3974 * various 802.11 + radio headers.
3976 static struct block
*
3977 gen_wlanhostop(eaddr
, dir
)
3978 register const u_char
*eaddr
;
3981 register struct block
*b0
, *b1
, *b2
;
3982 register struct slist
*s
;
3984 #ifdef ENABLE_WLAN_FILTERING_PATCH
3987 * We need to disable the optimizer because the optimizer is buggy
3988 * and wipes out some LD instructions generated by the below
3989 * code to validate the Frame Control bits
3992 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3999 * For control frames, there is no SA.
4001 * For management frames, SA is at an
4002 * offset of 10 from the beginning of
4005 * For data frames, SA is at an offset
4006 * of 10 from the beginning of the packet
4007 * if From DS is clear, at an offset of
4008 * 16 from the beginning of the packet
4009 * if From DS is set and To DS is clear,
4010 * and an offset of 24 from the beginning
4011 * of the packet if From DS is set and To DS
4016 * Generate the tests to be done for data frames
4019 * First, check for To DS set, i.e. check "link[1] & 0x01".
4021 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4022 b1
= new_block(JMP(BPF_JSET
));
4023 b1
->s
.k
= 0x01; /* To DS */
4027 * If To DS is set, the SA is at 24.
4029 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4033 * Now, check for To DS not set, i.e. check
4034 * "!(link[1] & 0x01)".
4036 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4037 b2
= new_block(JMP(BPF_JSET
));
4038 b2
->s
.k
= 0x01; /* To DS */
4043 * If To DS is not set, the SA is at 16.
4045 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4049 * Now OR together the last two checks. That gives
4050 * the complete set of checks for data frames with
4056 * Now check for From DS being set, and AND that with
4057 * the ORed-together checks.
4059 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4060 b1
= new_block(JMP(BPF_JSET
));
4061 b1
->s
.k
= 0x02; /* From DS */
4066 * Now check for data frames with From DS not set.
4068 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4069 b2
= new_block(JMP(BPF_JSET
));
4070 b2
->s
.k
= 0x02; /* From DS */
4075 * If From DS isn't set, the SA is at 10.
4077 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4081 * Now OR together the checks for data frames with
4082 * From DS not set and for data frames with From DS
4083 * set; that gives the checks done for data frames.
4088 * Now check for a data frame.
4089 * I.e, check "link[0] & 0x08".
4091 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4092 b1
= new_block(JMP(BPF_JSET
));
4097 * AND that with the checks done for data frames.
4102 * If the high-order bit of the type value is 0, this
4103 * is a management frame.
4104 * I.e, check "!(link[0] & 0x08)".
4106 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4107 b2
= new_block(JMP(BPF_JSET
));
4113 * For management frames, the SA is at 10.
4115 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4119 * OR that with the checks done for data frames.
4120 * That gives the checks done for management and
4126 * If the low-order bit of the type value is 1,
4127 * this is either a control frame or a frame
4128 * with a reserved type, and thus not a
4131 * I.e., check "!(link[0] & 0x04)".
4133 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4134 b1
= new_block(JMP(BPF_JSET
));
4140 * AND that with the checks for data and management
4150 * For control frames, there is no DA.
4152 * For management frames, DA is at an
4153 * offset of 4 from the beginning of
4156 * For data frames, DA is at an offset
4157 * of 4 from the beginning of the packet
4158 * if To DS is clear and at an offset of
4159 * 16 from the beginning of the packet
4164 * Generate the tests to be done for data frames.
4166 * First, check for To DS set, i.e. "link[1] & 0x01".
4168 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4169 b1
= new_block(JMP(BPF_JSET
));
4170 b1
->s
.k
= 0x01; /* To DS */
4174 * If To DS is set, the DA is at 16.
4176 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4180 * Now, check for To DS not set, i.e. check
4181 * "!(link[1] & 0x01)".
4183 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4184 b2
= new_block(JMP(BPF_JSET
));
4185 b2
->s
.k
= 0x01; /* To DS */
4190 * If To DS is not set, the DA is at 4.
4192 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4196 * Now OR together the last two checks. That gives
4197 * the complete set of checks for data frames.
4202 * Now check for a data frame.
4203 * I.e, check "link[0] & 0x08".
4205 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4206 b1
= new_block(JMP(BPF_JSET
));
4211 * AND that with the checks done for data frames.
4216 * If the high-order bit of the type value is 0, this
4217 * is a management frame.
4218 * I.e, check "!(link[0] & 0x08)".
4220 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4221 b2
= new_block(JMP(BPF_JSET
));
4227 * For management frames, the DA is at 4.
4229 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4233 * OR that with the checks done for data frames.
4234 * That gives the checks done for management and
4240 * If the low-order bit of the type value is 1,
4241 * this is either a control frame or a frame
4242 * with a reserved type, and thus not a
4245 * I.e., check "!(link[0] & 0x04)".
4247 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4248 b1
= new_block(JMP(BPF_JSET
));
4254 * AND that with the checks for data and management
4262 * Not present in management frames; addr1 in other
4267 * If the high-order bit of the type value is 0, this
4268 * is a management frame.
4269 * I.e, check "(link[0] & 0x08)".
4271 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4272 b1
= new_block(JMP(BPF_JSET
));
4279 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4282 * AND that with the check of addr1.
4289 * Not present in management frames; addr2, if present,
4294 * Not present in CTS or ACK control frames.
4296 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4297 IEEE80211_FC0_TYPE_MASK
);
4299 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4300 IEEE80211_FC0_SUBTYPE_MASK
);
4302 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4303 IEEE80211_FC0_SUBTYPE_MASK
);
4309 * If the high-order bit of the type value is 0, this
4310 * is a management frame.
4311 * I.e, check "(link[0] & 0x08)".
4313 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4314 b1
= new_block(JMP(BPF_JSET
));
4319 * AND that with the check for frames other than
4320 * CTS and ACK frames.
4327 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4332 * XXX - add BSSID keyword?
4335 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4339 * Not present in CTS or ACK control frames.
4341 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4342 IEEE80211_FC0_TYPE_MASK
);
4344 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4345 IEEE80211_FC0_SUBTYPE_MASK
);
4347 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4348 IEEE80211_FC0_SUBTYPE_MASK
);
4352 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4358 * Not present in control frames.
4360 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4361 IEEE80211_FC0_TYPE_MASK
);
4363 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4369 * Present only if the direction mask has both "From DS"
4370 * and "To DS" set. Neither control frames nor management
4371 * frames should have both of those set, so we don't
4372 * check the frame type.
4374 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4375 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4376 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4381 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4382 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4388 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4389 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4398 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4399 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4400 * as the RFC states.)
4402 static struct block
*
4403 gen_ipfchostop(eaddr
, dir
)
4404 register const u_char
*eaddr
;
4407 register struct block
*b0
, *b1
;
4411 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4414 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4417 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4418 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4424 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4425 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4430 bpf_error("'addr1' is only supported on 802.11");
4434 bpf_error("'addr2' is only supported on 802.11");
4438 bpf_error("'addr3' is only supported on 802.11");
4442 bpf_error("'addr4' is only supported on 802.11");
4446 bpf_error("'ra' is only supported on 802.11");
4450 bpf_error("'ta' is only supported on 802.11");
4458 * This is quite tricky because there may be pad bytes in front of the
4459 * DECNET header, and then there are two possible data packet formats that
4460 * carry both src and dst addresses, plus 5 packet types in a format that
4461 * carries only the src node, plus 2 types that use a different format and
4462 * also carry just the src node.
4466 * Instead of doing those all right, we just look for data packets with
4467 * 0 or 1 bytes of padding. If you want to look at other packets, that
4468 * will require a lot more hacking.
4470 * To add support for filtering on DECNET "areas" (network numbers)
4471 * one would want to add a "mask" argument to this routine. That would
4472 * make the filter even more inefficient, although one could be clever
4473 * and not generate masking instructions if the mask is 0xFFFF.
4475 static struct block
*
4476 gen_dnhostop(addr
, dir
)
4480 struct block
*b0
, *b1
, *b2
, *tmp
;
4481 u_int offset_lh
; /* offset if long header is received */
4482 u_int offset_sh
; /* offset if short header is received */
4487 offset_sh
= 1; /* follows flags */
4488 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4492 offset_sh
= 3; /* follows flags, dstnode */
4493 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4497 /* Inefficient because we do our Calvinball dance twice */
4498 b0
= gen_dnhostop(addr
, Q_SRC
);
4499 b1
= gen_dnhostop(addr
, Q_DST
);
4505 /* Inefficient because we do our Calvinball dance twice */
4506 b0
= gen_dnhostop(addr
, Q_SRC
);
4507 b1
= gen_dnhostop(addr
, Q_DST
);
4512 bpf_error("ISO host filtering not implemented");
4517 b0
= gen_linktype(ETHERTYPE_DN
);
4518 /* Check for pad = 1, long header case */
4519 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4520 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4521 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4522 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4524 /* Check for pad = 0, long header case */
4525 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4526 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4529 /* Check for pad = 1, short header case */
4530 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4531 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4532 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4535 /* Check for pad = 0, short header case */
4536 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4537 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4541 /* Combine with test for linktype */
4547 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4548 * test the bottom-of-stack bit, and then check the version number
4549 * field in the IP header.
4551 static struct block
*
4552 gen_mpls_linktype(proto
)
4555 struct block
*b0
, *b1
;
4560 /* match the bottom-of-stack bit */
4561 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4562 /* match the IPv4 version number */
4563 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4568 /* match the bottom-of-stack bit */
4569 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4570 /* match the IPv4 version number */
4571 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4580 static struct block
*
4581 gen_host(addr
, mask
, proto
, dir
, type
)
4588 struct block
*b0
, *b1
;
4589 const char *typestr
;
4599 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4601 * Only check for non-IPv4 addresses if we're not
4602 * checking MPLS-encapsulated packets.
4604 if (label_stack_depth
== 0) {
4605 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4607 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4613 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4616 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4619 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4622 bpf_error("'tcp' modifier applied to %s", typestr
);
4625 bpf_error("'sctp' modifier applied to %s", typestr
);
4628 bpf_error("'udp' modifier applied to %s", typestr
);
4631 bpf_error("'icmp' modifier applied to %s", typestr
);
4634 bpf_error("'igmp' modifier applied to %s", typestr
);
4637 bpf_error("'igrp' modifier applied to %s", typestr
);
4640 bpf_error("'pim' modifier applied to %s", typestr
);
4643 bpf_error("'vrrp' modifier applied to %s", typestr
);
4646 bpf_error("'carp' modifier applied to %s", typestr
);
4649 bpf_error("ATALK host filtering not implemented");
4652 bpf_error("AARP host filtering not implemented");
4655 return gen_dnhostop(addr
, dir
);
4658 bpf_error("SCA host filtering not implemented");
4661 bpf_error("LAT host filtering not implemented");
4664 bpf_error("MOPDL host filtering not implemented");
4667 bpf_error("MOPRC host filtering not implemented");
4670 bpf_error("'ip6' modifier applied to ip host");
4673 bpf_error("'icmp6' modifier applied to %s", typestr
);
4676 bpf_error("'ah' modifier applied to %s", typestr
);
4679 bpf_error("'esp' modifier applied to %s", typestr
);
4682 bpf_error("ISO host filtering not implemented");
4685 bpf_error("'esis' modifier applied to %s", typestr
);
4688 bpf_error("'isis' modifier applied to %s", typestr
);
4691 bpf_error("'clnp' modifier applied to %s", typestr
);
4694 bpf_error("'stp' modifier applied to %s", typestr
);
4697 bpf_error("IPX host filtering not implemented");
4700 bpf_error("'netbeui' modifier applied to %s", typestr
);
4703 bpf_error("'radio' modifier applied to %s", typestr
);
4712 static struct block
*
4713 gen_host6(addr
, mask
, proto
, dir
, type
)
4714 struct in6_addr
*addr
;
4715 struct in6_addr
*mask
;
4720 const char *typestr
;
4730 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4733 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4736 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4739 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4742 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4745 bpf_error("'sctp' modifier applied to %s", typestr
);
4748 bpf_error("'tcp' modifier applied to %s", typestr
);
4751 bpf_error("'udp' modifier applied to %s", typestr
);
4754 bpf_error("'icmp' modifier applied to %s", typestr
);
4757 bpf_error("'igmp' modifier applied to %s", typestr
);
4760 bpf_error("'igrp' modifier applied to %s", typestr
);
4763 bpf_error("'pim' modifier applied to %s", typestr
);
4766 bpf_error("'vrrp' modifier applied to %s", typestr
);
4769 bpf_error("'carp' modifier applied to %s", typestr
);
4772 bpf_error("ATALK host filtering not implemented");
4775 bpf_error("AARP host filtering not implemented");
4778 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4781 bpf_error("SCA host filtering not implemented");
4784 bpf_error("LAT host filtering not implemented");
4787 bpf_error("MOPDL host filtering not implemented");
4790 bpf_error("MOPRC host filtering not implemented");
4793 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4796 bpf_error("'icmp6' modifier applied to %s", typestr
);
4799 bpf_error("'ah' modifier applied to %s", typestr
);
4802 bpf_error("'esp' modifier applied to %s", typestr
);
4805 bpf_error("ISO host filtering not implemented");
4808 bpf_error("'esis' modifier applied to %s", typestr
);
4811 bpf_error("'isis' modifier applied to %s", typestr
);
4814 bpf_error("'clnp' modifier applied to %s", typestr
);
4817 bpf_error("'stp' modifier applied to %s", typestr
);
4820 bpf_error("IPX host filtering not implemented");
4823 bpf_error("'netbeui' modifier applied to %s", typestr
);
4826 bpf_error("'radio' modifier applied to %s", typestr
);
4836 static struct block
*
4837 gen_gateway(eaddr
, alist
, proto
, dir
)
4838 const u_char
*eaddr
;
4839 bpf_u_int32
**alist
;
4843 struct block
*b0
, *b1
, *tmp
;
4846 bpf_error("direction applied to 'gateway'");
4855 case DLT_NETANALYZER
:
4856 case DLT_NETANALYZER_TRANSPARENT
:
4857 b1
= gen_prevlinkhdr_check();
4858 b0
= gen_ehostop(eaddr
, Q_OR
);
4863 b0
= gen_fhostop(eaddr
, Q_OR
);
4866 b0
= gen_thostop(eaddr
, Q_OR
);
4868 case DLT_IEEE802_11
:
4869 case DLT_PRISM_HEADER
:
4870 case DLT_IEEE802_11_RADIO_AVS
:
4871 case DLT_IEEE802_11_RADIO
:
4873 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4877 * This is LLC-multiplexed traffic; if it were
4878 * LANE, linktype would have been set to
4882 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4884 case DLT_IP_OVER_FC
:
4885 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4889 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4891 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4893 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4902 bpf_error("illegal modifier of 'gateway'");
4908 gen_proto_abbrev(proto
)
4917 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4918 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4923 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4924 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4929 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4930 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4935 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4938 #ifndef IPPROTO_IGMP
4939 #define IPPROTO_IGMP 2
4943 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4946 #ifndef IPPROTO_IGRP
4947 #define IPPROTO_IGRP 9
4950 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4954 #define IPPROTO_PIM 103
4958 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4959 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4963 #ifndef IPPROTO_VRRP
4964 #define IPPROTO_VRRP 112
4968 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4971 #ifndef IPPROTO_CARP
4972 #define IPPROTO_CARP 112
4976 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4980 b1
= gen_linktype(ETHERTYPE_IP
);
4984 b1
= gen_linktype(ETHERTYPE_ARP
);
4988 b1
= gen_linktype(ETHERTYPE_REVARP
);
4992 bpf_error("link layer applied in wrong context");
4995 b1
= gen_linktype(ETHERTYPE_ATALK
);
4999 b1
= gen_linktype(ETHERTYPE_AARP
);
5003 b1
= gen_linktype(ETHERTYPE_DN
);
5007 b1
= gen_linktype(ETHERTYPE_SCA
);
5011 b1
= gen_linktype(ETHERTYPE_LAT
);
5015 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5019 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5023 b1
= gen_linktype(ETHERTYPE_IPV6
);
5026 #ifndef IPPROTO_ICMPV6
5027 #define IPPROTO_ICMPV6 58
5030 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5034 #define IPPROTO_AH 51
5037 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5038 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5043 #define IPPROTO_ESP 50
5046 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5047 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5052 b1
= gen_linktype(LLCSAP_ISONS
);
5056 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5060 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5063 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5064 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5065 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5067 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5069 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5071 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5075 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5076 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5077 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5079 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5081 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5083 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5087 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5088 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5089 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5091 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5096 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5097 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5102 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5103 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5105 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5107 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5112 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5113 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5118 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5119 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5124 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5128 b1
= gen_linktype(LLCSAP_8021D
);
5132 b1
= gen_linktype(LLCSAP_IPX
);
5136 b1
= gen_linktype(LLCSAP_NETBEUI
);
5140 bpf_error("'radio' is not a valid protocol type");
5148 static struct block
*
5154 /* not IPv4 frag other than the first frag */
5155 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5156 b
= new_block(JMP(BPF_JSET
));
5165 * Generate a comparison to a port value in the transport-layer header
5166 * at the specified offset from the beginning of that header.
5168 * XXX - this handles a variable-length prefix preceding the link-layer
5169 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5170 * variable-length link-layer headers (such as Token Ring or 802.11
5173 static struct block
*
5174 gen_portatom(off
, v
)
5178 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5181 static struct block
*
5182 gen_portatom6(off
, v
)
5186 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5190 gen_portop(port
, proto
, dir
)
5191 int port
, proto
, dir
;
5193 struct block
*b0
, *b1
, *tmp
;
5195 /* ip proto 'proto' and not a fragment other than the first fragment */
5196 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5202 b1
= gen_portatom(0, (bpf_int32
)port
);
5206 b1
= gen_portatom(2, (bpf_int32
)port
);
5211 tmp
= gen_portatom(0, (bpf_int32
)port
);
5212 b1
= gen_portatom(2, (bpf_int32
)port
);
5217 tmp
= gen_portatom(0, (bpf_int32
)port
);
5218 b1
= gen_portatom(2, (bpf_int32
)port
);
5230 static struct block
*
5231 gen_port(port
, ip_proto
, dir
)
5236 struct block
*b0
, *b1
, *tmp
;
5241 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5242 * not LLC encapsulation with LLCSAP_IP.
5244 * For IEEE 802 networks - which includes 802.5 token ring
5245 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5246 * says that SNAP encapsulation is used, not LLC encapsulation
5249 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5250 * RFC 2225 say that SNAP encapsulation is used, not LLC
5251 * encapsulation with LLCSAP_IP.
5253 * So we always check for ETHERTYPE_IP.
5255 b0
= gen_linktype(ETHERTYPE_IP
);
5261 b1
= gen_portop(port
, ip_proto
, dir
);
5265 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5266 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5268 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5280 gen_portop6(port
, proto
, dir
)
5281 int port
, proto
, dir
;
5283 struct block
*b0
, *b1
, *tmp
;
5285 /* ip6 proto 'proto' */
5286 /* XXX - catch the first fragment of a fragmented packet? */
5287 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5291 b1
= gen_portatom6(0, (bpf_int32
)port
);
5295 b1
= gen_portatom6(2, (bpf_int32
)port
);
5300 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5301 b1
= gen_portatom6(2, (bpf_int32
)port
);
5306 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5307 b1
= gen_portatom6(2, (bpf_int32
)port
);
5319 static struct block
*
5320 gen_port6(port
, ip_proto
, dir
)
5325 struct block
*b0
, *b1
, *tmp
;
5327 /* link proto ip6 */
5328 b0
= gen_linktype(ETHERTYPE_IPV6
);
5334 b1
= gen_portop6(port
, ip_proto
, dir
);
5338 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5339 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5341 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5352 /* gen_portrange code */
5353 static struct block
*
5354 gen_portrangeatom(off
, v1
, v2
)
5358 struct block
*b1
, *b2
;
5362 * Reverse the order of the ports, so v1 is the lower one.
5371 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5372 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5380 gen_portrangeop(port1
, port2
, proto
, dir
)
5385 struct block
*b0
, *b1
, *tmp
;
5387 /* ip proto 'proto' and not a fragment other than the first fragment */
5388 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5394 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5398 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5403 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5404 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5409 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5410 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5422 static struct block
*
5423 gen_portrange(port1
, port2
, ip_proto
, dir
)
5428 struct block
*b0
, *b1
, *tmp
;
5431 b0
= gen_linktype(ETHERTYPE_IP
);
5437 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5441 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5442 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5444 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5455 static struct block
*
5456 gen_portrangeatom6(off
, v1
, v2
)
5460 struct block
*b1
, *b2
;
5464 * Reverse the order of the ports, so v1 is the lower one.
5473 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5474 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5482 gen_portrangeop6(port1
, port2
, proto
, dir
)
5487 struct block
*b0
, *b1
, *tmp
;
5489 /* ip6 proto 'proto' */
5490 /* XXX - catch the first fragment of a fragmented packet? */
5491 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5495 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5499 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5504 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5505 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5510 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5511 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5523 static struct block
*
5524 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5529 struct block
*b0
, *b1
, *tmp
;
5531 /* link proto ip6 */
5532 b0
= gen_linktype(ETHERTYPE_IPV6
);
5538 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5542 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5543 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5545 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5557 lookup_proto(name
, proto
)
5558 register const char *name
;
5568 v
= pcap_nametoproto(name
);
5569 if (v
== PROTO_UNDEF
)
5570 bpf_error("unknown ip proto '%s'", name
);
5574 /* XXX should look up h/w protocol type based on linktype */
5575 v
= pcap_nametoeproto(name
);
5576 if (v
== PROTO_UNDEF
) {
5577 v
= pcap_nametollc(name
);
5578 if (v
== PROTO_UNDEF
)
5579 bpf_error("unknown ether proto '%s'", name
);
5584 if (strcmp(name
, "esis") == 0)
5586 else if (strcmp(name
, "isis") == 0)
5588 else if (strcmp(name
, "clnp") == 0)
5591 bpf_error("unknown osi proto '%s'", name
);
5611 static struct block
*
5612 gen_protochain(v
, proto
, dir
)
5617 #ifdef NO_PROTOCHAIN
5618 return gen_proto(v
, proto
, dir
);
5620 struct block
*b0
, *b
;
5621 struct slist
*s
[100];
5622 int fix2
, fix3
, fix4
, fix5
;
5623 int ahcheck
, again
, end
;
5625 int reg2
= alloc_reg();
5627 memset(s
, 0, sizeof(s
));
5628 fix3
= fix4
= fix5
= 0;
5635 b0
= gen_protochain(v
, Q_IP
, dir
);
5636 b
= gen_protochain(v
, Q_IPV6
, dir
);
5640 bpf_error("bad protocol applied for 'protochain'");
5645 * We don't handle variable-length prefixes before the link-layer
5646 * header, or variable-length link-layer headers, here yet.
5647 * We might want to add BPF instructions to do the protochain
5648 * work, to simplify that and, on platforms that have a BPF
5649 * interpreter with the new instructions, let the filtering
5650 * be done in the kernel. (We already require a modified BPF
5651 * engine to do the protochain stuff, to support backward
5652 * branches, and backward branch support is unlikely to appear
5653 * in kernel BPF engines.)
5655 if (off_linkpl
.is_variable
)
5656 bpf_error("'protochain' not supported with variable length headers");
5658 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5661 * s[0] is a dummy entry to protect other BPF insn from damage
5662 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5663 * hard to find interdependency made by jump table fixup.
5666 s
[i
] = new_stmt(0); /*dummy*/
5671 b0
= gen_linktype(ETHERTYPE_IP
);
5674 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5675 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5677 /* X = ip->ip_hl << 2 */
5678 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5679 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5684 b0
= gen_linktype(ETHERTYPE_IPV6
);
5686 /* A = ip6->ip_nxt */
5687 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5688 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5690 /* X = sizeof(struct ip6_hdr) */
5691 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5697 bpf_error("unsupported proto to gen_protochain");
5701 /* again: if (A == v) goto end; else fall through; */
5703 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5705 s
[i
]->s
.jt
= NULL
; /*later*/
5706 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5710 #ifndef IPPROTO_NONE
5711 #define IPPROTO_NONE 59
5713 /* if (A == IPPROTO_NONE) goto end */
5714 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5715 s
[i
]->s
.jt
= NULL
; /*later*/
5716 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5717 s
[i
]->s
.k
= IPPROTO_NONE
;
5718 s
[fix5
]->s
.jf
= s
[i
];
5722 if (proto
== Q_IPV6
) {
5723 int v6start
, v6end
, v6advance
, j
;
5726 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5727 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5728 s
[i
]->s
.jt
= NULL
; /*later*/
5729 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5730 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5731 s
[fix2
]->s
.jf
= s
[i
];
5733 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5734 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5735 s
[i
]->s
.jt
= NULL
; /*later*/
5736 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5737 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5739 /* if (A == IPPROTO_ROUTING) goto v6advance */
5740 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5741 s
[i
]->s
.jt
= NULL
; /*later*/
5742 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5743 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5745 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5746 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5747 s
[i
]->s
.jt
= NULL
; /*later*/
5748 s
[i
]->s
.jf
= NULL
; /*later*/
5749 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5759 * A = P[X + packet head];
5760 * X = X + (P[X + packet head + 1] + 1) * 8;
5762 /* A = P[X + packet head] */
5763 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5764 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5767 s
[i
] = new_stmt(BPF_ST
);
5770 /* A = P[X + packet head + 1]; */
5771 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5772 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5775 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5779 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5783 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5787 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5790 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5794 /* goto again; (must use BPF_JA for backward jump) */
5795 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5796 s
[i
]->s
.k
= again
- i
- 1;
5797 s
[i
- 1]->s
.jf
= s
[i
];
5801 for (j
= v6start
; j
<= v6end
; j
++)
5802 s
[j
]->s
.jt
= s
[v6advance
];
5805 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5807 s
[fix2
]->s
.jf
= s
[i
];
5813 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5814 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5815 s
[i
]->s
.jt
= NULL
; /*later*/
5816 s
[i
]->s
.jf
= NULL
; /*later*/
5817 s
[i
]->s
.k
= IPPROTO_AH
;
5819 s
[fix3
]->s
.jf
= s
[ahcheck
];
5826 * X = X + (P[X + 1] + 2) * 4;
5829 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5831 /* A = P[X + packet head]; */
5832 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5833 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5836 s
[i
] = new_stmt(BPF_ST
);
5840 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5843 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5847 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5849 /* A = P[X + packet head] */
5850 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5851 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5854 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5858 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5862 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5865 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5869 /* goto again; (must use BPF_JA for backward jump) */
5870 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5871 s
[i
]->s
.k
= again
- i
- 1;
5876 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5878 s
[fix2
]->s
.jt
= s
[end
];
5879 s
[fix4
]->s
.jf
= s
[end
];
5880 s
[fix5
]->s
.jt
= s
[end
];
5887 for (i
= 0; i
< max
- 1; i
++)
5888 s
[i
]->next
= s
[i
+ 1];
5889 s
[max
- 1]->next
= NULL
;
5894 b
= new_block(JMP(BPF_JEQ
));
5895 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5905 static struct block
*
5906 gen_check_802_11_data_frame()
5909 struct block
*b0
, *b1
;
5912 * A data frame has the 0x08 bit (b3) in the frame control field set
5913 * and the 0x04 bit (b2) clear.
5915 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5916 b0
= new_block(JMP(BPF_JSET
));
5920 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5921 b1
= new_block(JMP(BPF_JSET
));
5932 * Generate code that checks whether the packet is a packet for protocol
5933 * <proto> and whether the type field in that protocol's header has
5934 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5935 * IP packet and checks the protocol number in the IP header against <v>.
5937 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5938 * against Q_IP and Q_IPV6.
5940 static struct block
*
5941 gen_proto(v
, proto
, dir
)
5946 struct block
*b0
, *b1
;
5951 if (dir
!= Q_DEFAULT
)
5952 bpf_error("direction applied to 'proto'");
5956 b0
= gen_proto(v
, Q_IP
, dir
);
5957 b1
= gen_proto(v
, Q_IPV6
, dir
);
5963 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5964 * not LLC encapsulation with LLCSAP_IP.
5966 * For IEEE 802 networks - which includes 802.5 token ring
5967 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5968 * says that SNAP encapsulation is used, not LLC encapsulation
5971 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5972 * RFC 2225 say that SNAP encapsulation is used, not LLC
5973 * encapsulation with LLCSAP_IP.
5975 * So we always check for ETHERTYPE_IP.
5977 b0
= gen_linktype(ETHERTYPE_IP
);
5979 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5981 b1
= gen_protochain(v
, Q_IP
);
5991 * Frame Relay packets typically have an OSI
5992 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5993 * generates code to check for all the OSI
5994 * NLPIDs, so calling it and then adding a check
5995 * for the particular NLPID for which we're
5996 * looking is bogus, as we can just check for
5999 * What we check for is the NLPID and a frame
6000 * control field value of UI, i.e. 0x03 followed
6003 * XXX - assumes a 2-byte Frame Relay header with
6004 * DLCI and flags. What if the address is longer?
6006 * XXX - what about SNAP-encapsulated frames?
6008 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6014 * Cisco uses an Ethertype lookalike - for OSI,
6017 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6018 /* OSI in C-HDLC is stuffed with a fudge byte */
6019 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6024 b0
= gen_linktype(LLCSAP_ISONS
);
6025 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6031 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6033 * 4 is the offset of the PDU type relative to the IS-IS
6036 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6041 bpf_error("arp does not encapsulate another protocol");
6045 bpf_error("rarp does not encapsulate another protocol");
6049 bpf_error("atalk encapsulation is not specifiable");
6053 bpf_error("decnet encapsulation is not specifiable");
6057 bpf_error("sca does not encapsulate another protocol");
6061 bpf_error("lat does not encapsulate another protocol");
6065 bpf_error("moprc does not encapsulate another protocol");
6069 bpf_error("mopdl does not encapsulate another protocol");
6073 return gen_linktype(v
);
6076 bpf_error("'udp proto' is bogus");
6080 bpf_error("'tcp proto' is bogus");
6084 bpf_error("'sctp proto' is bogus");
6088 bpf_error("'icmp proto' is bogus");
6092 bpf_error("'igmp proto' is bogus");
6096 bpf_error("'igrp proto' is bogus");
6100 bpf_error("'pim proto' is bogus");
6104 bpf_error("'vrrp proto' is bogus");
6108 bpf_error("'carp proto' is bogus");
6112 b0
= gen_linktype(ETHERTYPE_IPV6
);
6115 * Also check for a fragment header before the final
6118 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6119 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6121 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6124 b1
= gen_protochain(v
, Q_IPV6
);
6130 bpf_error("'icmp6 proto' is bogus");
6133 bpf_error("'ah proto' is bogus");
6136 bpf_error("'ah proto' is bogus");
6139 bpf_error("'stp proto' is bogus");
6142 bpf_error("'ipx proto' is bogus");
6145 bpf_error("'netbeui proto' is bogus");
6148 bpf_error("'radio proto' is bogus");
6159 register const char *name
;
6162 int proto
= q
.proto
;
6166 bpf_u_int32 mask
, addr
;
6168 bpf_u_int32
**alist
;
6171 struct sockaddr_in
*sin4
;
6172 struct sockaddr_in6
*sin6
;
6173 struct addrinfo
*res
, *res0
;
6174 struct in6_addr mask128
;
6176 struct block
*b
, *tmp
;
6177 int port
, real_proto
;
6183 addr
= pcap_nametonetaddr(name
);
6185 bpf_error("unknown network '%s'", name
);
6186 /* Left justify network addr and calculate its network mask */
6188 while (addr
&& (addr
& 0xff000000) == 0) {
6192 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6196 if (proto
== Q_LINK
) {
6200 case DLT_NETANALYZER
:
6201 case DLT_NETANALYZER_TRANSPARENT
:
6202 eaddr
= pcap_ether_hostton(name
);
6205 "unknown ether host '%s'", name
);
6206 tmp
= gen_prevlinkhdr_check();
6207 b
= gen_ehostop(eaddr
, dir
);
6214 eaddr
= pcap_ether_hostton(name
);
6217 "unknown FDDI host '%s'", name
);
6218 b
= gen_fhostop(eaddr
, dir
);
6223 eaddr
= pcap_ether_hostton(name
);
6226 "unknown token ring host '%s'", name
);
6227 b
= gen_thostop(eaddr
, dir
);
6231 case DLT_IEEE802_11
:
6232 case DLT_PRISM_HEADER
:
6233 case DLT_IEEE802_11_RADIO_AVS
:
6234 case DLT_IEEE802_11_RADIO
:
6236 eaddr
= pcap_ether_hostton(name
);
6239 "unknown 802.11 host '%s'", name
);
6240 b
= gen_wlanhostop(eaddr
, dir
);
6244 case DLT_IP_OVER_FC
:
6245 eaddr
= pcap_ether_hostton(name
);
6248 "unknown Fibre Channel host '%s'", name
);
6249 b
= gen_ipfchostop(eaddr
, dir
);
6254 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6255 } else if (proto
== Q_DECNET
) {
6256 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6258 * I don't think DECNET hosts can be multihomed, so
6259 * there is no need to build up a list of addresses
6261 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6264 alist
= pcap_nametoaddr(name
);
6265 if (alist
== NULL
|| *alist
== NULL
)
6266 bpf_error("unknown host '%s'", name
);
6268 if (off_linktype
.constant_part
== (u_int
)-1 &&
6269 tproto
== Q_DEFAULT
)
6271 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6273 tmp
= gen_host(**alist
++, 0xffffffff,
6274 tproto
, dir
, q
.addr
);
6280 memset(&mask128
, 0xff, sizeof(mask128
));
6281 res0
= res
= pcap_nametoaddrinfo(name
);
6283 bpf_error("unknown host '%s'", name
);
6286 tproto
= tproto6
= proto
;
6287 if (off_linktype
.constant_part
== -1 &&
6288 tproto
== Q_DEFAULT
) {
6292 for (res
= res0
; res
; res
= res
->ai_next
) {
6293 switch (res
->ai_family
) {
6295 if (tproto
== Q_IPV6
)
6298 sin4
= (struct sockaddr_in
*)
6300 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6301 0xffffffff, tproto
, dir
, q
.addr
);
6304 if (tproto6
== Q_IP
)
6307 sin6
= (struct sockaddr_in6
*)
6309 tmp
= gen_host6(&sin6
->sin6_addr
,
6310 &mask128
, tproto6
, dir
, q
.addr
);
6322 bpf_error("unknown host '%s'%s", name
,
6323 (proto
== Q_DEFAULT
)
6325 : " for specified address family");
6332 if (proto
!= Q_DEFAULT
&&
6333 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6334 bpf_error("illegal qualifier of 'port'");
6335 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6336 bpf_error("unknown port '%s'", name
);
6337 if (proto
== Q_UDP
) {
6338 if (real_proto
== IPPROTO_TCP
)
6339 bpf_error("port '%s' is tcp", name
);
6340 else if (real_proto
== IPPROTO_SCTP
)
6341 bpf_error("port '%s' is sctp", name
);
6343 /* override PROTO_UNDEF */
6344 real_proto
= IPPROTO_UDP
;
6346 if (proto
== Q_TCP
) {
6347 if (real_proto
== IPPROTO_UDP
)
6348 bpf_error("port '%s' is udp", name
);
6350 else if (real_proto
== IPPROTO_SCTP
)
6351 bpf_error("port '%s' is sctp", name
);
6353 /* override PROTO_UNDEF */
6354 real_proto
= IPPROTO_TCP
;
6356 if (proto
== Q_SCTP
) {
6357 if (real_proto
== IPPROTO_UDP
)
6358 bpf_error("port '%s' is udp", name
);
6360 else if (real_proto
== IPPROTO_TCP
)
6361 bpf_error("port '%s' is tcp", name
);
6363 /* override PROTO_UNDEF */
6364 real_proto
= IPPROTO_SCTP
;
6367 bpf_error("illegal port number %d < 0", port
);
6369 bpf_error("illegal port number %d > 65535", port
);
6370 b
= gen_port(port
, real_proto
, dir
);
6371 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6375 if (proto
!= Q_DEFAULT
&&
6376 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6377 bpf_error("illegal qualifier of 'portrange'");
6378 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6379 bpf_error("unknown port in range '%s'", name
);
6380 if (proto
== Q_UDP
) {
6381 if (real_proto
== IPPROTO_TCP
)
6382 bpf_error("port in range '%s' is tcp", name
);
6383 else if (real_proto
== IPPROTO_SCTP
)
6384 bpf_error("port in range '%s' is sctp", name
);
6386 /* override PROTO_UNDEF */
6387 real_proto
= IPPROTO_UDP
;
6389 if (proto
== Q_TCP
) {
6390 if (real_proto
== IPPROTO_UDP
)
6391 bpf_error("port in range '%s' is udp", name
);
6392 else if (real_proto
== IPPROTO_SCTP
)
6393 bpf_error("port in range '%s' is sctp", name
);
6395 /* override PROTO_UNDEF */
6396 real_proto
= IPPROTO_TCP
;
6398 if (proto
== Q_SCTP
) {
6399 if (real_proto
== IPPROTO_UDP
)
6400 bpf_error("port in range '%s' is udp", name
);
6401 else if (real_proto
== IPPROTO_TCP
)
6402 bpf_error("port in range '%s' is tcp", name
);
6404 /* override PROTO_UNDEF */
6405 real_proto
= IPPROTO_SCTP
;
6408 bpf_error("illegal port number %d < 0", port1
);
6410 bpf_error("illegal port number %d > 65535", port1
);
6412 bpf_error("illegal port number %d < 0", port2
);
6414 bpf_error("illegal port number %d > 65535", port2
);
6416 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6417 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6422 eaddr
= pcap_ether_hostton(name
);
6424 bpf_error("unknown ether host: %s", name
);
6426 alist
= pcap_nametoaddr(name
);
6427 if (alist
== NULL
|| *alist
== NULL
)
6428 bpf_error("unknown host '%s'", name
);
6429 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6433 bpf_error("'gateway' not supported in this configuration");
6437 real_proto
= lookup_proto(name
, proto
);
6438 if (real_proto
>= 0)
6439 return gen_proto(real_proto
, proto
, dir
);
6441 bpf_error("unknown protocol: %s", name
);
6444 real_proto
= lookup_proto(name
, proto
);
6445 if (real_proto
>= 0)
6446 return gen_protochain(real_proto
, proto
, dir
);
6448 bpf_error("unknown protocol: %s", name
);
6459 gen_mcode(s1
, s2
, masklen
, q
)
6460 register const char *s1
, *s2
;
6461 register unsigned int masklen
;
6464 register int nlen
, mlen
;
6467 nlen
= __pcap_atoin(s1
, &n
);
6468 /* Promote short ipaddr */
6472 mlen
= __pcap_atoin(s2
, &m
);
6473 /* Promote short ipaddr */
6476 bpf_error("non-network bits set in \"%s mask %s\"",
6479 /* Convert mask len to mask */
6481 bpf_error("mask length must be <= 32");
6484 * X << 32 is not guaranteed by C to be 0; it's
6489 m
= 0xffffffff << (32 - masklen
);
6491 bpf_error("non-network bits set in \"%s/%d\"",
6498 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6501 bpf_error("Mask syntax for networks only");
6510 register const char *s
;
6515 int proto
= q
.proto
;
6521 else if (q
.proto
== Q_DECNET
)
6522 vlen
= __pcap_atodn(s
, &v
);
6524 vlen
= __pcap_atoin(s
, &v
);
6531 if (proto
== Q_DECNET
)
6532 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6533 else if (proto
== Q_LINK
) {
6534 bpf_error("illegal link layer address");
6537 if (s
== NULL
&& q
.addr
== Q_NET
) {
6538 /* Promote short net number */
6539 while (v
&& (v
& 0xff000000) == 0) {
6544 /* Promote short ipaddr */
6548 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6553 proto
= IPPROTO_UDP
;
6554 else if (proto
== Q_TCP
)
6555 proto
= IPPROTO_TCP
;
6556 else if (proto
== Q_SCTP
)
6557 proto
= IPPROTO_SCTP
;
6558 else if (proto
== Q_DEFAULT
)
6559 proto
= PROTO_UNDEF
;
6561 bpf_error("illegal qualifier of 'port'");
6564 bpf_error("illegal port number %u > 65535", v
);
6568 b
= gen_port((int)v
, proto
, dir
);
6569 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6575 proto
= IPPROTO_UDP
;
6576 else if (proto
== Q_TCP
)
6577 proto
= IPPROTO_TCP
;
6578 else if (proto
== Q_SCTP
)
6579 proto
= IPPROTO_SCTP
;
6580 else if (proto
== Q_DEFAULT
)
6581 proto
= PROTO_UNDEF
;
6583 bpf_error("illegal qualifier of 'portrange'");
6586 bpf_error("illegal port number %u > 65535", v
);
6590 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6591 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6596 bpf_error("'gateway' requires a name");
6600 return gen_proto((int)v
, proto
, dir
);
6603 return gen_protochain((int)v
, proto
, dir
);
6618 gen_mcode6(s1
, s2
, masklen
, q
)
6619 register const char *s1
, *s2
;
6620 register unsigned int masklen
;
6623 struct addrinfo
*res
;
6624 struct in6_addr
*addr
;
6625 struct in6_addr mask
;
6630 bpf_error("no mask %s supported", s2
);
6632 res
= pcap_nametoaddrinfo(s1
);
6634 bpf_error("invalid ip6 address %s", s1
);
6637 bpf_error("%s resolved to multiple address", s1
);
6638 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6640 if (sizeof(mask
) * 8 < masklen
)
6641 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6642 memset(&mask
, 0, sizeof(mask
));
6643 memset(&mask
, 0xff, masklen
/ 8);
6645 mask
.s6_addr
[masklen
/ 8] =
6646 (0xff << (8 - masklen
% 8)) & 0xff;
6649 a
= (u_int32_t
*)addr
;
6650 m
= (u_int32_t
*)&mask
;
6651 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6652 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6653 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6661 bpf_error("Mask syntax for networks only");
6665 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6671 bpf_error("invalid qualifier against IPv6 address");
6680 register const u_char
*eaddr
;
6683 struct block
*b
, *tmp
;
6685 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6688 case DLT_NETANALYZER
:
6689 case DLT_NETANALYZER_TRANSPARENT
:
6690 tmp
= gen_prevlinkhdr_check();
6691 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6696 return gen_fhostop(eaddr
, (int)q
.dir
);
6698 return gen_thostop(eaddr
, (int)q
.dir
);
6699 case DLT_IEEE802_11
:
6700 case DLT_PRISM_HEADER
:
6701 case DLT_IEEE802_11_RADIO_AVS
:
6702 case DLT_IEEE802_11_RADIO
:
6704 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6705 case DLT_IP_OVER_FC
:
6706 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6708 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6712 bpf_error("ethernet address used in non-ether expression");
6719 struct slist
*s0
, *s1
;
6722 * This is definitely not the best way to do this, but the
6723 * lists will rarely get long.
6730 static struct slist
*
6736 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6741 static struct slist
*
6747 s
= new_stmt(BPF_LD
|BPF_MEM
);
6753 * Modify "index" to use the value stored into its register as an
6754 * offset relative to the beginning of the header for the protocol
6755 * "proto", and allocate a register and put an item "size" bytes long
6756 * (1, 2, or 4) at that offset into that register, making it the register
6760 gen_load(proto
, inst
, size
)
6765 struct slist
*s
, *tmp
;
6767 int regno
= alloc_reg();
6769 free_reg(inst
->regno
);
6773 bpf_error("data size must be 1, 2, or 4");
6789 bpf_error("unsupported index operation");
6793 * The offset is relative to the beginning of the packet
6794 * data, if we have a radio header. (If we don't, this
6797 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6798 linktype
!= DLT_IEEE802_11_RADIO
&&
6799 linktype
!= DLT_PRISM_HEADER
)
6800 bpf_error("radio information not present in capture");
6803 * Load into the X register the offset computed into the
6804 * register specified by "index".
6806 s
= xfer_to_x(inst
);
6809 * Load the item at that offset.
6811 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6813 sappend(inst
->s
, s
);
6818 * The offset is relative to the beginning of
6819 * the link-layer header.
6821 * XXX - what about ATM LANE? Should the index be
6822 * relative to the beginning of the AAL5 frame, so
6823 * that 0 refers to the beginning of the LE Control
6824 * field, or relative to the beginning of the LAN
6825 * frame, so that 0 refers, for Ethernet LANE, to
6826 * the beginning of the destination address?
6828 s
= gen_abs_offset_varpart(&off_linkhdr
);
6831 * If "s" is non-null, it has code to arrange that the
6832 * X register contains the length of the prefix preceding
6833 * the link-layer header. Add to it the offset computed
6834 * into the register specified by "index", and move that
6835 * into the X register. Otherwise, just load into the X
6836 * register the offset computed into the register specified
6840 sappend(s
, xfer_to_a(inst
));
6841 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6842 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6844 s
= xfer_to_x(inst
);
6847 * Load the item at the sum of the offset we've put in the
6848 * X register and the offset of the start of the link
6849 * layer header (which is 0 if the radio header is
6850 * variable-length; that header length is what we put
6851 * into the X register and then added to the index).
6853 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6854 tmp
->s
.k
= off_linkhdr
.constant_part
;
6856 sappend(inst
->s
, s
);
6870 * The offset is relative to the beginning of
6871 * the network-layer header.
6872 * XXX - are there any cases where we want
6875 s
= gen_abs_offset_varpart(&off_linkpl
);
6878 * If "s" is non-null, it has code to arrange that the
6879 * X register contains the variable part of the offset
6880 * of the link-layer payload. Add to it the offset
6881 * computed into the register specified by "index",
6882 * and move that into the X register. Otherwise, just
6883 * load into the X register the offset computed into
6884 * the register specified by "index".
6887 sappend(s
, xfer_to_a(inst
));
6888 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6889 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6891 s
= xfer_to_x(inst
);
6894 * Load the item at the sum of the offset we've put in the
6895 * X register, the offset of the start of the network
6896 * layer header from the beginning of the link-layer
6897 * payload, and the constant part of the offset of the
6898 * start of the link-layer payload.
6900 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6901 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6903 sappend(inst
->s
, s
);
6906 * Do the computation only if the packet contains
6907 * the protocol in question.
6909 b
= gen_proto_abbrev(proto
);
6911 gen_and(inst
->b
, b
);
6925 * The offset is relative to the beginning of
6926 * the transport-layer header.
6928 * Load the X register with the length of the IPv4 header
6929 * (plus the offset of the link-layer header, if it's
6930 * a variable-length header), in bytes.
6932 * XXX - are there any cases where we want
6934 * XXX - we should, if we're built with
6935 * IPv6 support, generate code to load either
6936 * IPv4, IPv6, or both, as appropriate.
6938 s
= gen_loadx_iphdrlen();
6941 * The X register now contains the sum of the variable
6942 * part of the offset of the link-layer payload and the
6943 * length of the network-layer header.
6945 * Load into the A register the offset relative to
6946 * the beginning of the transport layer header,
6947 * add the X register to that, move that to the
6948 * X register, and load with an offset from the
6949 * X register equal to the sum of the constant part of
6950 * the offset of the link-layer payload and the offset,
6951 * relative to the beginning of the link-layer payload,
6952 * of the network-layer header.
6954 sappend(s
, xfer_to_a(inst
));
6955 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6956 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6957 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6958 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6959 sappend(inst
->s
, s
);
6962 * Do the computation only if the packet contains
6963 * the protocol in question - which is true only
6964 * if this is an IP datagram and is the first or
6965 * only fragment of that datagram.
6967 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6969 gen_and(inst
->b
, b
);
6970 gen_and(gen_proto_abbrev(Q_IP
), b
);
6974 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6977 inst
->regno
= regno
;
6978 s
= new_stmt(BPF_ST
);
6980 sappend(inst
->s
, s
);
6986 gen_relation(code
, a0
, a1
, reversed
)
6988 struct arth
*a0
, *a1
;
6991 struct slist
*s0
, *s1
, *s2
;
6992 struct block
*b
, *tmp
;
6996 if (code
== BPF_JEQ
) {
6997 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6998 b
= new_block(JMP(code
));
7002 b
= new_block(BPF_JMP
|code
|BPF_X
);
7008 sappend(a0
->s
, a1
->s
);
7012 free_reg(a0
->regno
);
7013 free_reg(a1
->regno
);
7015 /* 'and' together protocol checks */
7018 gen_and(a0
->b
, tmp
= a1
->b
);
7034 int regno
= alloc_reg();
7035 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7038 s
= new_stmt(BPF_LD
|BPF_LEN
);
7039 s
->next
= new_stmt(BPF_ST
);
7040 s
->next
->s
.k
= regno
;
7055 a
= (struct arth
*)newchunk(sizeof(*a
));
7059 s
= new_stmt(BPF_LD
|BPF_IMM
);
7061 s
->next
= new_stmt(BPF_ST
);
7077 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7080 s
= new_stmt(BPF_ST
);
7088 gen_arth(code
, a0
, a1
)
7090 struct arth
*a0
, *a1
;
7092 struct slist
*s0
, *s1
, *s2
;
7095 * Disallow division by, or modulus by, zero; we do this here
7096 * so that it gets done even if the optimizer is disabled.
7098 if (code
== BPF_DIV
) {
7099 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7100 bpf_error("division by zero");
7101 } else if (code
== BPF_MOD
) {
7102 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7103 bpf_error("modulus by zero");
7107 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7112 sappend(a0
->s
, a1
->s
);
7114 free_reg(a0
->regno
);
7115 free_reg(a1
->regno
);
7117 s0
= new_stmt(BPF_ST
);
7118 a0
->regno
= s0
->s
.k
= alloc_reg();
7125 * Here we handle simple allocation of the scratch registers.
7126 * If too many registers are alloc'd, the allocator punts.
7128 static int regused
[BPF_MEMWORDS
];
7132 * Initialize the table of used registers and the current register.
7138 memset(regused
, 0, sizeof regused
);
7142 * Return the next free register.
7147 int n
= BPF_MEMWORDS
;
7150 if (regused
[curreg
])
7151 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7153 regused
[curreg
] = 1;
7157 bpf_error("too many registers needed to evaluate expression");
7163 * Return a register to the table so it can
7173 static struct block
*
7180 s
= new_stmt(BPF_LD
|BPF_LEN
);
7181 b
= new_block(JMP(jmp
));
7192 return gen_len(BPF_JGE
, n
);
7196 * Actually, this is less than or equal.
7204 b
= gen_len(BPF_JGT
, n
);
7211 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7212 * the beginning of the link-layer header.
7213 * XXX - that means you can't test values in the radiotap header, but
7214 * as that header is difficult if not impossible to parse generally
7215 * without a loop, that might not be a severe problem. A new keyword
7216 * "radio" could be added for that, although what you'd really want
7217 * would be a way of testing particular radio header values, which
7218 * would generate code appropriate to the radio header in question.
7221 gen_byteop(op
, idx
, val
)
7232 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7235 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7239 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7243 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7247 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7251 b
= new_block(JMP(BPF_JEQ
));
7258 static u_char abroadcast
[] = { 0x0 };
7261 gen_broadcast(proto
)
7264 bpf_u_int32 hostmask
;
7265 struct block
*b0
, *b1
, *b2
;
7266 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7274 case DLT_ARCNET_LINUX
:
7275 return gen_ahostop(abroadcast
, Q_DST
);
7277 case DLT_NETANALYZER
:
7278 case DLT_NETANALYZER_TRANSPARENT
:
7279 b1
= gen_prevlinkhdr_check();
7280 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7285 return gen_fhostop(ebroadcast
, Q_DST
);
7287 return gen_thostop(ebroadcast
, Q_DST
);
7288 case DLT_IEEE802_11
:
7289 case DLT_PRISM_HEADER
:
7290 case DLT_IEEE802_11_RADIO_AVS
:
7291 case DLT_IEEE802_11_RADIO
:
7293 return gen_wlanhostop(ebroadcast
, Q_DST
);
7294 case DLT_IP_OVER_FC
:
7295 return gen_ipfchostop(ebroadcast
, Q_DST
);
7297 bpf_error("not a broadcast link");
7303 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7304 * as an indication that we don't know the netmask, and fail
7307 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7308 bpf_error("netmask not known, so 'ip broadcast' not supported");
7309 b0
= gen_linktype(ETHERTYPE_IP
);
7310 hostmask
= ~netmask
;
7311 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7312 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7313 (bpf_int32
)(~0 & hostmask
), hostmask
);
7318 bpf_error("only link-layer/IP broadcast filters supported");
7324 * Generate code to test the low-order bit of a MAC address (that's
7325 * the bottom bit of the *first* byte).
7327 static struct block
*
7328 gen_mac_multicast(offset
)
7331 register struct block
*b0
;
7332 register struct slist
*s
;
7334 /* link[offset] & 1 != 0 */
7335 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7336 b0
= new_block(JMP(BPF_JSET
));
7343 gen_multicast(proto
)
7346 register struct block
*b0
, *b1
, *b2
;
7347 register struct slist
*s
;
7355 case DLT_ARCNET_LINUX
:
7356 /* all ARCnet multicasts use the same address */
7357 return gen_ahostop(abroadcast
, Q_DST
);
7359 case DLT_NETANALYZER
:
7360 case DLT_NETANALYZER_TRANSPARENT
:
7361 b1
= gen_prevlinkhdr_check();
7362 /* ether[0] & 1 != 0 */
7363 b0
= gen_mac_multicast(0);
7369 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7371 * XXX - was that referring to bit-order issues?
7373 /* fddi[1] & 1 != 0 */
7374 return gen_mac_multicast(1);
7376 /* tr[2] & 1 != 0 */
7377 return gen_mac_multicast(2);
7378 case DLT_IEEE802_11
:
7379 case DLT_PRISM_HEADER
:
7380 case DLT_IEEE802_11_RADIO_AVS
:
7381 case DLT_IEEE802_11_RADIO
:
7386 * For control frames, there is no DA.
7388 * For management frames, DA is at an
7389 * offset of 4 from the beginning of
7392 * For data frames, DA is at an offset
7393 * of 4 from the beginning of the packet
7394 * if To DS is clear and at an offset of
7395 * 16 from the beginning of the packet
7400 * Generate the tests to be done for data frames.
7402 * First, check for To DS set, i.e. "link[1] & 0x01".
7404 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7405 b1
= new_block(JMP(BPF_JSET
));
7406 b1
->s
.k
= 0x01; /* To DS */
7410 * If To DS is set, the DA is at 16.
7412 b0
= gen_mac_multicast(16);
7416 * Now, check for To DS not set, i.e. check
7417 * "!(link[1] & 0x01)".
7419 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7420 b2
= new_block(JMP(BPF_JSET
));
7421 b2
->s
.k
= 0x01; /* To DS */
7426 * If To DS is not set, the DA is at 4.
7428 b1
= gen_mac_multicast(4);
7432 * Now OR together the last two checks. That gives
7433 * the complete set of checks for data frames.
7438 * Now check for a data frame.
7439 * I.e, check "link[0] & 0x08".
7441 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7442 b1
= new_block(JMP(BPF_JSET
));
7447 * AND that with the checks done for data frames.
7452 * If the high-order bit of the type value is 0, this
7453 * is a management frame.
7454 * I.e, check "!(link[0] & 0x08)".
7456 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7457 b2
= new_block(JMP(BPF_JSET
));
7463 * For management frames, the DA is at 4.
7465 b1
= gen_mac_multicast(4);
7469 * OR that with the checks done for data frames.
7470 * That gives the checks done for management and
7476 * If the low-order bit of the type value is 1,
7477 * this is either a control frame or a frame
7478 * with a reserved type, and thus not a
7481 * I.e., check "!(link[0] & 0x04)".
7483 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7484 b1
= new_block(JMP(BPF_JSET
));
7490 * AND that with the checks for data and management
7495 case DLT_IP_OVER_FC
:
7496 b0
= gen_mac_multicast(2);
7501 /* Link not known to support multicasts */
7505 b0
= gen_linktype(ETHERTYPE_IP
);
7506 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7511 b0
= gen_linktype(ETHERTYPE_IPV6
);
7512 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7516 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7522 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7523 * Outbound traffic is sent by this machine, while inbound traffic is
7524 * sent by a remote machine (and may include packets destined for a
7525 * unicast or multicast link-layer address we are not subscribing to).
7526 * These are the same definitions implemented by pcap_setdirection().
7527 * Capturing only unicast traffic destined for this host is probably
7528 * better accomplished using a higher-layer filter.
7534 register struct block
*b0
;
7537 * Only some data link types support inbound/outbound qualifiers.
7541 b0
= gen_relation(BPF_JEQ
,
7542 gen_load(Q_LINK
, gen_loadi(0), 1),
7549 /* match outgoing packets */
7550 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7552 /* match incoming packets */
7553 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7558 /* match outgoing packets */
7559 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7561 /* to filter on inbound traffic, invert the match */
7566 #ifdef HAVE_NET_PFVAR_H
7568 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7569 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7575 /* match outgoing packets */
7576 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7578 /* match incoming packets */
7579 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7583 case DLT_JUNIPER_MFR
:
7584 case DLT_JUNIPER_MLFR
:
7585 case DLT_JUNIPER_MLPPP
:
7586 case DLT_JUNIPER_ATM1
:
7587 case DLT_JUNIPER_ATM2
:
7588 case DLT_JUNIPER_PPPOE
:
7589 case DLT_JUNIPER_PPPOE_ATM
:
7590 case DLT_JUNIPER_GGSN
:
7591 case DLT_JUNIPER_ES
:
7592 case DLT_JUNIPER_MONITOR
:
7593 case DLT_JUNIPER_SERVICES
:
7594 case DLT_JUNIPER_ETHER
:
7595 case DLT_JUNIPER_PPP
:
7596 case DLT_JUNIPER_FRELAY
:
7597 case DLT_JUNIPER_CHDLC
:
7598 case DLT_JUNIPER_VP
:
7599 case DLT_JUNIPER_ST
:
7600 case DLT_JUNIPER_ISM
:
7601 case DLT_JUNIPER_VS
:
7602 case DLT_JUNIPER_SRX_E2E
:
7603 case DLT_JUNIPER_FIBRECHANNEL
:
7604 case DLT_JUNIPER_ATM_CEMIC
:
7606 /* juniper flags (including direction) are stored
7607 * the byte after the 3-byte magic number */
7609 /* match outgoing packets */
7610 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7612 /* match incoming packets */
7613 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7619 * If we have packet meta-data indicating a direction,
7620 * check it, otherwise give up as this link-layer type
7621 * has nothing in the packet data.
7623 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7625 * This is Linux with PF_PACKET support.
7626 * If this is a *live* capture, we can look at
7627 * special meta-data in the filter expression;
7628 * if it's a savefile, we can't.
7630 if (bpf_pcap
->rfile
!= NULL
) {
7631 /* We have a FILE *, so this is a savefile */
7632 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7637 /* match outgoing packets */
7638 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7641 /* to filter on inbound traffic, invert the match */
7644 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7645 bpf_error("inbound/outbound not supported on linktype %d",
7649 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7654 #ifdef HAVE_NET_PFVAR_H
7655 /* PF firewall log matched interface */
7657 gen_pf_ifname(const char *ifname
)
7662 if (linktype
!= DLT_PFLOG
) {
7663 bpf_error("ifname supported only on PF linktype");
7666 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7667 off
= offsetof(struct pfloghdr
, ifname
);
7668 if (strlen(ifname
) >= len
) {
7669 bpf_error("ifname interface names can only be %d characters",
7673 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7677 /* PF firewall log ruleset name */
7679 gen_pf_ruleset(char *ruleset
)
7683 if (linktype
!= DLT_PFLOG
) {
7684 bpf_error("ruleset supported only on PF linktype");
7688 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7689 bpf_error("ruleset names can only be %ld characters",
7690 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7694 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7695 strlen(ruleset
), (const u_char
*)ruleset
);
7699 /* PF firewall log rule number */
7705 if (linktype
!= DLT_PFLOG
) {
7706 bpf_error("rnr supported only on PF linktype");
7710 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7715 /* PF firewall log sub-rule number */
7717 gen_pf_srnr(int srnr
)
7721 if (linktype
!= DLT_PFLOG
) {
7722 bpf_error("srnr supported only on PF linktype");
7726 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7731 /* PF firewall log reason code */
7733 gen_pf_reason(int reason
)
7737 if (linktype
!= DLT_PFLOG
) {
7738 bpf_error("reason supported only on PF linktype");
7742 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7747 /* PF firewall log action */
7749 gen_pf_action(int action
)
7753 if (linktype
!= DLT_PFLOG
) {
7754 bpf_error("action supported only on PF linktype");
7758 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7762 #else /* !HAVE_NET_PFVAR_H */
7764 gen_pf_ifname(const char *ifname
)
7766 bpf_error("libpcap was compiled without pf support");
7772 gen_pf_ruleset(char *ruleset
)
7774 bpf_error("libpcap was compiled on a machine without pf support");
7782 bpf_error("libpcap was compiled on a machine without pf support");
7788 gen_pf_srnr(int srnr
)
7790 bpf_error("libpcap was compiled on a machine without pf support");
7796 gen_pf_reason(int reason
)
7798 bpf_error("libpcap was compiled on a machine without pf support");
7804 gen_pf_action(int action
)
7806 bpf_error("libpcap was compiled on a machine without pf support");
7810 #endif /* HAVE_NET_PFVAR_H */
7812 /* IEEE 802.11 wireless header */
7814 gen_p80211_type(int type
, int mask
)
7820 case DLT_IEEE802_11
:
7821 case DLT_PRISM_HEADER
:
7822 case DLT_IEEE802_11_RADIO_AVS
:
7823 case DLT_IEEE802_11_RADIO
:
7824 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7829 bpf_error("802.11 link-layer types supported only on 802.11");
7837 gen_p80211_fcdir(int fcdir
)
7843 case DLT_IEEE802_11
:
7844 case DLT_PRISM_HEADER
:
7845 case DLT_IEEE802_11_RADIO_AVS
:
7846 case DLT_IEEE802_11_RADIO
:
7850 bpf_error("frame direction supported only with 802.11 headers");
7854 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7855 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7862 register const u_char
*eaddr
;
7868 case DLT_ARCNET_LINUX
:
7869 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7871 return (gen_ahostop(eaddr
, (int)q
.dir
));
7873 bpf_error("ARCnet address used in non-arc expression");
7879 bpf_error("aid supported only on ARCnet");
7882 bpf_error("ARCnet address used in non-arc expression");
7887 static struct block
*
7888 gen_ahostop(eaddr
, dir
)
7889 register const u_char
*eaddr
;
7892 register struct block
*b0
, *b1
;
7895 /* src comes first, different from Ethernet */
7897 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7900 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7903 b0
= gen_ahostop(eaddr
, Q_SRC
);
7904 b1
= gen_ahostop(eaddr
, Q_DST
);
7910 b0
= gen_ahostop(eaddr
, Q_SRC
);
7911 b1
= gen_ahostop(eaddr
, Q_DST
);
7916 bpf_error("'addr1' is only supported on 802.11");
7920 bpf_error("'addr2' is only supported on 802.11");
7924 bpf_error("'addr3' is only supported on 802.11");
7928 bpf_error("'addr4' is only supported on 802.11");
7932 bpf_error("'ra' is only supported on 802.11");
7936 bpf_error("'ta' is only supported on 802.11");
7943 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7944 static struct block
*
7945 gen_vlan_bpf_extensions(int vlan_num
)
7947 struct block
*b0
, *b1
;
7950 /* generate new filter code based on extracting packet
7952 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7953 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7955 b0
= new_block(JMP(BPF_JEQ
));
7959 if (vlan_num
>= 0) {
7960 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7961 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7963 b1
= new_block(JMP(BPF_JEQ
));
7965 b1
->s
.k
= (bpf_int32
) vlan_num
;
7975 static struct block
*
7976 gen_vlan_no_bpf_extensions(int vlan_num
)
7978 struct block
*b0
, *b1
;
7980 /* check for VLAN, including QinQ */
7981 b0
= gen_linktype(ETHERTYPE_8021Q
);
7982 b1
= gen_linktype(ETHERTYPE_8021AD
);
7985 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7989 /* If a specific VLAN is requested, check VLAN id */
7990 if (vlan_num
>= 0) {
7991 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7992 (bpf_int32
)vlan_num
, 0x0fff);
7998 * The payload follows the full header, including the
7999 * VLAN tags, so skip past this VLAN tag.
8001 off_linkpl
.constant_part
+= 4;
8004 * The link-layer type information follows the VLAN tags, so
8005 * skip past this VLAN tag.
8007 off_linktype
.constant_part
+= 4;
8013 * support IEEE 802.1Q VLAN trunk over ethernet
8021 /* can't check for VLAN-encapsulated packets inside MPLS */
8022 if (label_stack_depth
> 0)
8023 bpf_error("no VLAN match after MPLS");
8026 * Check for a VLAN packet, and then change the offsets to point
8027 * to the type and data fields within the VLAN packet. Just
8028 * increment the offsets, so that we can support a hierarchy, e.g.
8029 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8032 * XXX - this is a bit of a kludge. If we were to split the
8033 * compiler into a parser that parses an expression and
8034 * generates an expression tree, and a code generator that
8035 * takes an expression tree (which could come from our
8036 * parser or from some other parser) and generates BPF code,
8037 * we could perhaps make the offsets parameters of routines
8038 * and, in the handler for an "AND" node, pass to subnodes
8039 * other than the VLAN node the adjusted offsets.
8041 * This would mean that "vlan" would, instead of changing the
8042 * behavior of *all* tests after it, change only the behavior
8043 * of tests ANDed with it. That would change the documented
8044 * semantics of "vlan", which might break some expressions.
8045 * However, it would mean that "(vlan and ip) or ip" would check
8046 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8047 * checking only for VLAN-encapsulated IP, so that could still
8048 * be considered worth doing; it wouldn't break expressions
8049 * that are of the form "vlan and ..." or "vlan N and ...",
8050 * which I suspect are the most common expressions involving
8051 * "vlan". "vlan or ..." doesn't necessarily do what the user
8052 * would really want, now, as all the "or ..." tests would
8053 * be done assuming a VLAN, even though the "or" could be viewed
8054 * as meaning "or, if this isn't a VLAN packet...".
8059 case DLT_NETANALYZER
:
8060 case DLT_NETANALYZER_TRANSPARENT
:
8061 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8062 /* Verify that this is the outer part of the packet and
8063 * not encapsulated somehow. */
8064 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
8065 off_linkhdr
.constant_part
==
8066 off_outermostlinkhdr
.constant_part
) {
8068 * Do we need special VLAN handling?
8070 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8071 b0
= gen_vlan_bpf_extensions(vlan_num
);
8073 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8076 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8079 case DLT_IEEE802_11
:
8080 case DLT_PRISM_HEADER
:
8081 case DLT_IEEE802_11_RADIO_AVS
:
8082 case DLT_IEEE802_11_RADIO
:
8083 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8087 bpf_error("no VLAN support for data link type %d",
8104 struct block
*b0
, *b1
;
8106 if (label_stack_depth
> 0) {
8107 /* just match the bottom-of-stack bit clear */
8108 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8111 * We're not in an MPLS stack yet, so check the link-layer
8112 * type against MPLS.
8116 case DLT_C_HDLC
: /* fall through */
8118 case DLT_NETANALYZER
:
8119 case DLT_NETANALYZER_TRANSPARENT
:
8120 b0
= gen_linktype(ETHERTYPE_MPLS
);
8124 b0
= gen_linktype(PPP_MPLS_UCAST
);
8127 /* FIXME add other DLT_s ...
8128 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8129 * leave it for now */
8132 bpf_error("no MPLS support for data link type %d",
8140 /* If a specific MPLS label is requested, check it */
8141 if (label_num
>= 0) {
8142 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8143 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8144 0xfffff000); /* only compare the first 20 bits */
8150 * Change the offsets to point to the type and data fields within
8151 * the MPLS packet. Just increment the offsets, so that we
8152 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8153 * capture packets with an outer label of 100000 and an inner
8156 * Increment the MPLS stack depth as well; this indicates that
8157 * we're checking MPLS-encapsulated headers, to make sure higher
8158 * level code generators don't try to match against IP-related
8159 * protocols such as Q_ARP, Q_RARP etc.
8161 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8165 label_stack_depth
++;
8170 * Support PPPOE discovery and session.
8175 /* check for PPPoE discovery */
8176 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8180 gen_pppoes(sess_num
)
8183 struct block
*b0
, *b1
;
8186 * Test against the PPPoE session link-layer type.
8188 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8190 /* If a specific session is requested, check PPPoE session id */
8191 if (sess_num
>= 0) {
8192 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8193 (bpf_int32
)sess_num
, 0x0000ffff);
8199 * Change the offsets to point to the type and data fields within
8200 * the PPP packet, and note that this is PPPoE rather than
8203 * XXX - this is a bit of a kludge. If we were to split the
8204 * compiler into a parser that parses an expression and
8205 * generates an expression tree, and a code generator that
8206 * takes an expression tree (which could come from our
8207 * parser or from some other parser) and generates BPF code,
8208 * we could perhaps make the offsets parameters of routines
8209 * and, in the handler for an "AND" node, pass to subnodes
8210 * other than the PPPoE node the adjusted offsets.
8212 * This would mean that "pppoes" would, instead of changing the
8213 * behavior of *all* tests after it, change only the behavior
8214 * of tests ANDed with it. That would change the documented
8215 * semantics of "pppoes", which might break some expressions.
8216 * However, it would mean that "(pppoes and ip) or ip" would check
8217 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8218 * checking only for VLAN-encapsulated IP, so that could still
8219 * be considered worth doing; it wouldn't break expressions
8220 * that are of the form "pppoes and ..." which I suspect are the
8221 * most common expressions involving "pppoes". "pppoes or ..."
8222 * doesn't necessarily do what the user would really want, now,
8223 * as all the "or ..." tests would be done assuming PPPoE, even
8224 * though the "or" could be viewed as meaning "or, if this isn't
8225 * a PPPoE packet...".
8227 * The "network-layer" protocol is PPPoE, which has a 6-byte
8228 * PPPoE header, followed by a PPP packet.
8230 * There is no HDLC encapsulation for the PPP packet (it's
8231 * encapsulated in PPPoES instead), so the link-layer type
8232 * starts at the first byte of the PPP packet. For PPPoE,
8233 * that offset is relative to the beginning of the total
8234 * link-layer payload, including any 802.2 LLC header, so
8235 * it's 6 bytes past off_nl.
8237 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8238 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8241 off_linktype
= off_linkhdr
;
8242 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8245 off_nl_nosnap
= 0; /* no 802.2 LLC */
8250 /* Check that this is Geneve and the VNI is correct if
8251 * specified. Parameterized to handle both IPv4 and IPv6. */
8252 static struct block
*
8253 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8254 enum e_offrel offrel
, int vni
)
8256 struct block
*b0
, *b1
;
8258 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8260 /* Check that we are operating on version 0. Otherwise, we
8261 * can't decode the rest of the fields. The version is 2 bits
8262 * in the first byte of the Geneve header. */
8263 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8268 vni
<<= 8; /* VNI is in the upper 3 bytes */
8269 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8278 /* The IPv4 and IPv6 Geneve checks need to do two things:
8279 * - Verify that this actually is Geneve with the right VNI.
8280 * - Place the IP header length (plus variable link prefix if
8281 * needed) into register A to be used later to compute
8282 * the inner packet offsets. */
8283 static struct block
*
8284 gen_geneve4(int vni
)
8286 struct block
*b0
, *b1
;
8287 struct slist
*s
, *s1
;
8289 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8291 /* Load the IP header length into A. */
8292 s
= gen_loadx_iphdrlen();
8294 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8297 /* Forcibly append these statements to the true condition
8298 * of the protocol check by creating a new block that is
8299 * always true and ANDing them. */
8300 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8309 static struct block
*
8310 gen_geneve6(int vni
)
8312 struct block
*b0
, *b1
;
8313 struct slist
*s
, *s1
;
8315 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8317 /* Load the IP header length. We need to account for a
8318 * variable length link prefix if there is one. */
8319 s
= gen_abs_offset_varpart(&off_linkpl
);
8321 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8325 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8329 s
= new_stmt(BPF_LD
|BPF_IMM
);
8333 /* Forcibly append these statements to the true condition
8334 * of the protocol check by creating a new block that is
8335 * always true and ANDing them. */
8336 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8339 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8348 /* We need to store three values based on the Geneve header::
8349 * - The offset of the linktype.
8350 * - The offset of the end of the Geneve header.
8351 * - The offset of the end of the encapsulated MAC header. */
8352 static struct slist
*
8353 gen_geneve_offsets(void)
8355 struct slist
*s
, *s1
, *s_proto
;
8357 /* First we need to calculate the offset of the Geneve header
8358 * itself. This is composed of the IP header previously calculated
8359 * (include any variable link prefix) and stored in A plus the
8360 * fixed sized headers (fixed link prefix, MAC length, and UDP
8362 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8363 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8365 /* Stash this in X since we'll need it later. */
8366 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8369 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8371 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8375 off_linktype
.reg
= alloc_reg();
8376 off_linktype
.is_variable
= 1;
8377 off_linktype
.constant_part
= 0;
8379 s1
= new_stmt(BPF_ST
);
8380 s1
->s
.k
= off_linktype
.reg
;
8383 /* Load the Geneve option length and mask and shift to get the
8384 * number of bytes. It is stored in the first byte of the Geneve
8386 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8390 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8394 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8398 /* Add in the rest of the Geneve base header. */
8399 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8403 /* Add the Geneve header length to its offset and store. */
8404 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8408 /* Set the encapsulated type as Ethernet. Even though we may
8409 * not actually have Ethernet inside there are two reasons this
8411 * - The linktype field is always in EtherType format regardless
8412 * of whether it is in Geneve or an inner Ethernet frame.
8413 * - The only link layer that we have specific support for is
8414 * Ethernet. We will confirm that the packet actually is
8415 * Ethernet at runtime before executing these checks. */
8416 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8418 s1
= new_stmt(BPF_ST
);
8419 s1
->s
.k
= off_linkhdr
.reg
;
8422 /* Calculate whether we have an Ethernet header or just raw IP/
8423 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8424 * and linktype by 14 bytes so that the network header can be found
8425 * seamlessly. Otherwise, keep what we've calculated already. */
8427 /* We have a bare jmp so we can't use the optimizer. */
8430 /* Load the EtherType in the Geneve header, 2 bytes in. */
8431 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8435 /* Load X with the end of the Geneve header. */
8436 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8437 s1
->s
.k
= off_linkhdr
.reg
;
8440 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8441 * end of this check, we should have the total length in X. In
8442 * the non-Ethernet case, it's already there. */
8443 s_proto
= new_stmt(JMP(BPF_JEQ
));
8444 s_proto
->s
.k
= ETHERTYPE_TEB
;
8445 sappend(s
, s_proto
);
8447 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8451 /* Since this is Ethernet, use the EtherType of the payload
8452 * directly as the linktype. Overwrite what we already have. */
8453 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8457 s1
= new_stmt(BPF_ST
);
8458 s1
->s
.k
= off_linktype
.reg
;
8461 /* Advance two bytes further to get the end of the Ethernet
8463 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8467 /* Move the result to X. */
8468 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8471 /* Store the final result of our linkpl calculation. */
8472 off_linkpl
.reg
= alloc_reg();
8473 off_linkpl
.is_variable
= 1;
8474 off_linkpl
.constant_part
= 0;
8476 s1
= new_stmt(BPF_STX
);
8477 s1
->s
.k
= off_linkpl
.reg
;
8486 /* Check to see if this is a Geneve packet. */
8490 struct block
*b0
, *b1
;
8493 b0
= gen_geneve4(vni
);
8494 b1
= gen_geneve6(vni
);
8499 /* Later filters should act on the payload of the Geneve frame,
8500 * update all of the header pointers. Attach this code so that
8501 * it gets executed in the event that the Geneve filter matches. */
8502 s
= gen_geneve_offsets();
8505 sappend(s
, b1
->stmts
);
8515 /* Check that the encapsulated frame has a link layer header
8516 * for Ethernet filters. */
8517 static struct block
*
8518 gen_geneve_ll_check()
8521 struct slist
*s
, *s1
;
8523 /* The easiest way to see if there is a link layer present
8524 * is to check if the link layer header and payload are not
8527 /* Geneve always generates pure variable offsets so we can
8528 * compare only the registers. */
8529 s
= new_stmt(BPF_LD
|BPF_MEM
);
8530 s
->s
.k
= off_linkhdr
.reg
;
8532 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8533 s1
->s
.k
= off_linkpl
.reg
;
8536 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8545 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8557 bpf_error("'vpi' supported only on raw ATM");
8558 if (off_vpi
== (u_int
)-1)
8560 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8566 bpf_error("'vci' supported only on raw ATM");
8567 if (off_vci
== (u_int
)-1)
8569 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8574 if (off_proto
== (u_int
)-1)
8575 abort(); /* XXX - this isn't on FreeBSD */
8576 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8581 if (off_payload
== (u_int
)-1)
8583 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8584 0xffffffff, jtype
, reverse
, jvalue
);
8589 bpf_error("'callref' supported only on raw ATM");
8590 if (off_proto
== (u_int
)-1)
8592 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8593 jtype
, reverse
, jvalue
);
8603 gen_atmtype_abbrev(type
)
8606 struct block
*b0
, *b1
;
8611 /* Get all packets in Meta signalling Circuit */
8613 bpf_error("'metac' supported only on raw ATM");
8614 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8615 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8620 /* Get all packets in Broadcast Circuit*/
8622 bpf_error("'bcc' supported only on raw ATM");
8623 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8624 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8629 /* Get all cells in Segment OAM F4 circuit*/
8631 bpf_error("'oam4sc' supported only on raw ATM");
8632 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8633 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8638 /* Get all cells in End-to-End OAM F4 Circuit*/
8640 bpf_error("'oam4ec' supported only on raw ATM");
8641 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8642 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8647 /* Get all packets in connection Signalling Circuit */
8649 bpf_error("'sc' supported only on raw ATM");
8650 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8651 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8656 /* Get all packets in ILMI Circuit */
8658 bpf_error("'ilmic' supported only on raw ATM");
8659 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8660 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8665 /* Get all LANE packets */
8667 bpf_error("'lane' supported only on raw ATM");
8668 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8671 * Arrange that all subsequent tests assume LANE
8672 * rather than LLC-encapsulated packets, and set
8673 * the offsets appropriately for LANE-encapsulated
8676 * We assume LANE means Ethernet, not Token Ring.
8678 PUSH_LINKHDR(DLT_EN10MB
, 0,
8679 off_payload
+ 2, /* Ethernet header */
8681 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8682 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8683 off_nl
= 0; /* Ethernet II */
8684 off_nl_nosnap
= 3; /* 802.3+802.2 */
8688 /* Get all LLC-encapsulated packets */
8690 bpf_error("'llc' supported only on raw ATM");
8691 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8692 linktype
= prevlinktype
;
8702 * Filtering for MTP2 messages based on li value
8703 * FISU, length is null
8704 * LSSU, length is 1 or 2
8705 * MSU, length is 3 or more
8706 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8709 gen_mtp2type_abbrev(type
)
8712 struct block
*b0
, *b1
;
8717 if ( (linktype
!= DLT_MTP2
) &&
8718 (linktype
!= DLT_ERF
) &&
8719 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8720 bpf_error("'fisu' supported only on MTP2");
8721 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8722 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8726 if ( (linktype
!= DLT_MTP2
) &&
8727 (linktype
!= DLT_ERF
) &&
8728 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8729 bpf_error("'lssu' supported only on MTP2");
8730 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8731 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8736 if ( (linktype
!= DLT_MTP2
) &&
8737 (linktype
!= DLT_ERF
) &&
8738 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8739 bpf_error("'msu' supported only on MTP2");
8740 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8744 if ( (linktype
!= DLT_MTP2
) &&
8745 (linktype
!= DLT_ERF
) &&
8746 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8747 bpf_error("'hfisu' supported only on MTP2_HSL");
8748 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8749 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8753 if ( (linktype
!= DLT_MTP2
) &&
8754 (linktype
!= DLT_ERF
) &&
8755 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8756 bpf_error("'hlssu' supported only on MTP2_HSL");
8757 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8758 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8763 if ( (linktype
!= DLT_MTP2
) &&
8764 (linktype
!= DLT_ERF
) &&
8765 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8766 bpf_error("'hmsu' supported only on MTP2_HSL");
8767 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8777 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8784 bpf_u_int32 val1
, val2
, val3
;
8785 u_int newoff_sio
=off_sio
;
8786 u_int newoff_opc
=off_opc
;
8787 u_int newoff_dpc
=off_dpc
;
8788 u_int newoff_sls
=off_sls
;
8790 switch (mtp3field
) {
8793 newoff_sio
+= 3; /* offset for MTP2_HSL */
8797 if (off_sio
== (u_int
)-1)
8798 bpf_error("'sio' supported only on SS7");
8799 /* sio coded on 1 byte so max value 255 */
8801 bpf_error("sio value %u too big; max value = 255",
8803 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8804 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8810 if (off_opc
== (u_int
)-1)
8811 bpf_error("'opc' supported only on SS7");
8812 /* opc coded on 14 bits so max value 16383 */
8814 bpf_error("opc value %u too big; max value = 16383",
8816 /* the following instructions are made to convert jvalue
8817 * to the form used to write opc in an ss7 message*/
8818 val1
= jvalue
& 0x00003c00;
8820 val2
= jvalue
& 0x000003fc;
8822 val3
= jvalue
& 0x00000003;
8824 jvalue
= val1
+ val2
+ val3
;
8825 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8826 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8834 if (off_dpc
== (u_int
)-1)
8835 bpf_error("'dpc' supported only on SS7");
8836 /* dpc coded on 14 bits so max value 16383 */
8838 bpf_error("dpc value %u too big; max value = 16383",
8840 /* the following instructions are made to convert jvalue
8841 * to the forme used to write dpc in an ss7 message*/
8842 val1
= jvalue
& 0x000000ff;
8844 val2
= jvalue
& 0x00003f00;
8846 jvalue
= val1
+ val2
;
8847 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8848 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8854 if (off_sls
== (u_int
)-1)
8855 bpf_error("'sls' supported only on SS7");
8856 /* sls coded on 4 bits so max value 15 */
8858 bpf_error("sls value %u too big; max value = 15",
8860 /* the following instruction is made to convert jvalue
8861 * to the forme used to write sls in an ss7 message*/
8862 jvalue
= jvalue
<< 4;
8863 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8864 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8873 static struct block
*
8874 gen_msg_abbrev(type
)
8880 * Q.2931 signalling protocol messages for handling virtual circuits
8881 * establishment and teardown
8886 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8890 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8894 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8898 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8902 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8905 case A_RELEASE_DONE
:
8906 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8916 gen_atmmulti_abbrev(type
)
8919 struct block
*b0
, *b1
;
8925 bpf_error("'oam' supported only on raw ATM");
8926 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8931 bpf_error("'oamf4' supported only on raw ATM");
8933 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8934 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8936 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8942 * Get Q.2931 signalling messages for switched
8943 * virtual connection
8946 bpf_error("'connectmsg' supported only on raw ATM");
8947 b0
= gen_msg_abbrev(A_SETUP
);
8948 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8950 b0
= gen_msg_abbrev(A_CONNECT
);
8952 b0
= gen_msg_abbrev(A_CONNECTACK
);
8954 b0
= gen_msg_abbrev(A_RELEASE
);
8956 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8958 b0
= gen_atmtype_abbrev(A_SC
);
8964 bpf_error("'metaconnect' supported only on raw ATM");
8965 b0
= gen_msg_abbrev(A_SETUP
);
8966 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8968 b0
= gen_msg_abbrev(A_CONNECT
);
8970 b0
= gen_msg_abbrev(A_RELEASE
);
8972 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8974 b0
= gen_atmtype_abbrev(A_METAC
);