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 #define ETHERMTU 1500
150 #ifndef ETHERTYPE_TEB
151 #define ETHERTYPE_TEB 0x6558
154 #ifndef IPPROTO_HOPOPTS
155 #define IPPROTO_HOPOPTS 0
157 #ifndef IPPROTO_ROUTING
158 #define IPPROTO_ROUTING 43
160 #ifndef IPPROTO_FRAGMENT
161 #define IPPROTO_FRAGMENT 44
163 #ifndef IPPROTO_DSTOPTS
164 #define IPPROTO_DSTOPTS 60
167 #define IPPROTO_SCTP 132
170 #define GENEVE_PORT 6081
172 #ifdef HAVE_OS_PROTO_H
173 #include "os-proto.h"
176 #define JMP(c) ((c)|BPF_JMP|BPF_K)
179 static jmp_buf top_ctx
;
180 static pcap_t
*bpf_pcap
;
182 /* Hack for handling VLAN and MPLS stacks. */
184 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
186 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
190 static int pcap_fddipad
;
194 bpf_error(const char *fmt
, ...)
199 if (bpf_pcap
!= NULL
)
200 (void)pcap_vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
207 static void init_linktype(pcap_t
*);
209 static void init_regs(void);
210 static int alloc_reg(void);
211 static void free_reg(int);
213 static struct block
*root
;
216 * Absolute offsets, which are offsets from the beginning of the raw
217 * packet data, are, in the general case, the sum of a variable value
218 * and a constant value; the variable value may be absent, in which
219 * case the offset is only the constant value, and the constant value
220 * may be zero, in which case the offset is only the variable value.
222 * bpf_abs_offset is a structure containing all that information:
224 * is_variable is 1 if there's a variable part.
226 * constant_part is the constant part of the value, possibly zero;
228 * if is_variable is 1, reg is the register number for a register
229 * containing the variable value if the register has been assigned,
239 * Value passed to gen_load_a() to indicate what the offset argument
240 * is relative to the beginning of.
243 OR_PACKET
, /* full packet data */
244 OR_LINKHDR
, /* link-layer header */
245 OR_PREVLINKHDR
, /* previous link-layer header */
246 OR_LLC
, /* 802.2 LLC header */
247 OR_PREVMPLSHDR
, /* previous MPLS header */
248 OR_LINKTYPE
, /* link-layer type */
249 OR_LINKPL
, /* link-layer payload */
250 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
251 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
252 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
257 * As errors are handled by a longjmp, anything allocated must be freed
258 * in the longjmp handler, so it must be reachable from that handler.
259 * One thing that's allocated is the result of pcap_nametoaddrinfo();
260 * it must be freed with freeaddrinfo(). This variable points to any
261 * addrinfo structure that would need to be freed.
263 static struct addrinfo
*ai
;
267 * We divy out chunks of memory rather than call malloc each time so
268 * we don't have to worry about leaking memory. It's probably
269 * not a big deal if all this memory was wasted but if this ever
270 * goes into a library that would probably not be a good idea.
272 * XXX - this *is* in a library....
275 #define CHUNK0SIZE 1024
281 static struct chunk chunks
[NCHUNKS
];
282 static int cur_chunk
;
284 static void *newchunk(size_t);
285 static void freechunks(void);
286 static inline struct block
*new_block(int);
287 static inline struct slist
*new_stmt(int);
288 static struct block
*gen_retblk(int);
289 static inline void syntax(void);
291 static void backpatch(struct block
*, struct block
*);
292 static void merge(struct block
*, struct block
*);
293 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
294 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
295 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
296 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
297 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
298 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
300 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
301 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
302 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
303 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
304 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
305 static struct slist
*gen_loadx_iphdrlen(void);
306 static struct block
*gen_uncond(int);
307 static inline struct block
*gen_true(void);
308 static inline struct block
*gen_false(void);
309 static struct block
*gen_ether_linktype(int);
310 static struct block
*gen_ipnet_linktype(int);
311 static struct block
*gen_linux_sll_linktype(int);
312 static struct slist
*gen_load_prism_llprefixlen(void);
313 static struct slist
*gen_load_avs_llprefixlen(void);
314 static struct slist
*gen_load_radiotap_llprefixlen(void);
315 static struct slist
*gen_load_ppi_llprefixlen(void);
316 static void insert_compute_vloffsets(struct block
*);
317 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
318 static int ethertype_to_ppptype(int);
319 static struct block
*gen_linktype(int);
320 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
321 static struct block
*gen_llc_linktype(int);
322 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
324 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
326 static struct block
*gen_ahostop(const u_char
*, int);
327 static struct block
*gen_ehostop(const u_char
*, int);
328 static struct block
*gen_fhostop(const u_char
*, int);
329 static struct block
*gen_thostop(const u_char
*, int);
330 static struct block
*gen_wlanhostop(const u_char
*, int);
331 static struct block
*gen_ipfchostop(const u_char
*, int);
332 static struct block
*gen_dnhostop(bpf_u_int32
, int);
333 static struct block
*gen_mpls_linktype(int);
334 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
336 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
339 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
341 static struct block
*gen_ipfrag(void);
342 static struct block
*gen_portatom(int, bpf_int32
);
343 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
344 static struct block
*gen_portatom6(int, bpf_int32
);
345 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
346 struct block
*gen_portop(int, int, int);
347 static struct block
*gen_port(int, int, int);
348 struct block
*gen_portrangeop(int, int, int, int);
349 static struct block
*gen_portrange(int, int, int, int);
350 struct block
*gen_portop6(int, int, int);
351 static struct block
*gen_port6(int, int, int);
352 struct block
*gen_portrangeop6(int, int, int, int);
353 static struct block
*gen_portrange6(int, int, int, int);
354 static int lookup_proto(const char *, int);
355 static struct block
*gen_protochain(int, int, int);
356 static struct block
*gen_proto(int, int, int);
357 static struct slist
*xfer_to_x(struct arth
*);
358 static struct slist
*xfer_to_a(struct arth
*);
359 static struct block
*gen_mac_multicast(int);
360 static struct block
*gen_len(int, int);
361 static struct block
*gen_check_802_11_data_frame(void);
362 static struct block
*gen_geneve_ll_check(void);
364 static struct block
*gen_ppi_dlt_check(void);
365 static struct block
*gen_msg_abbrev(int type
);
375 /* XXX Round up to nearest long. */
376 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
378 /* XXX Round up to structure boundary. */
382 cp
= &chunks
[cur_chunk
];
383 if (n
> cp
->n_left
) {
384 ++cp
, k
= ++cur_chunk
;
386 bpf_error("out of memory");
387 size
= CHUNK0SIZE
<< k
;
388 cp
->m
= (void *)malloc(size
);
390 bpf_error("out of memory");
391 memset((char *)cp
->m
, 0, size
);
394 bpf_error("out of memory");
397 return (void *)((char *)cp
->m
+ cp
->n_left
);
406 for (i
= 0; i
< NCHUNKS
; ++i
)
407 if (chunks
[i
].m
!= NULL
) {
414 * A strdup whose allocations are freed after code generation is over.
418 register const char *s
;
420 size_t n
= strlen(s
) + 1;
421 char *cp
= newchunk(n
);
427 static inline struct block
*
433 p
= (struct block
*)newchunk(sizeof(*p
));
440 static inline struct slist
*
446 p
= (struct slist
*)newchunk(sizeof(*p
));
452 static struct block
*
456 struct block
*b
= new_block(BPF_RET
|BPF_K
);
465 bpf_error("syntax error in filter expression");
468 static bpf_u_int32 netmask
;
473 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
474 const char *buf
, int optimize
, bpf_u_int32 mask
)
477 const char * volatile xbuf
= buf
;
478 yyscan_t scanner
= NULL
;
479 YY_BUFFER_STATE in_buffer
= NULL
;
484 * XXX - single-thread this code path with pthread calls on
485 * UN*X, if the platform supports pthreads? If that requires
486 * a separate -lpthread, we might not want to do that.
494 EnterCriticalSection(&g_PcapCompileCriticalSection
);
498 * If this pcap_t hasn't been activated, it doesn't have a
499 * link-layer type, so we can't use it.
502 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
503 "not-yet-activated pcap_t passed to pcap_compile");
513 if (setjmp(top_ctx
)) {
526 snaplen
= pcap_snapshot(p
);
528 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
529 "snaplen of 0 rejects all packets");
534 if (pcap_lex_init(&scanner
) != 0)
535 bpf_error("can't initialize scanner: %s", pcap_strerror(errno
));
536 in_buffer
= pcap__scan_string(xbuf
? xbuf
: "", scanner
);
538 (void)pcap_parse(scanner
);
544 root
= gen_retblk(snaplen
);
546 if (optimize
&& !no_optimize
) {
549 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
550 bpf_error("expression rejects all packets");
552 program
->bf_insns
= icode_to_fcode(root
, &len
);
553 program
->bf_len
= len
;
555 rc
= 0; /* We're all okay */
559 * Clean up everything for the lexical analyzer.
561 if (in_buffer
!= NULL
)
562 pcap__delete_buffer(in_buffer
, scanner
);
564 pcap_lex_destroy(scanner
);
567 * Clean up our own allocated memory.
572 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
579 * entry point for using the compiler with no pcap open
580 * pass in all the stuff that is needed explicitly instead.
583 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
584 struct bpf_program
*program
,
585 const char *buf
, int optimize
, bpf_u_int32 mask
)
590 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
593 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
599 * Clean up a "struct bpf_program" by freeing all the memory allocated
603 pcap_freecode(struct bpf_program
*program
)
606 if (program
->bf_insns
!= NULL
) {
607 free((char *)program
->bf_insns
);
608 program
->bf_insns
= NULL
;
613 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
614 * which of the jt and jf fields has been resolved and which is a pointer
615 * back to another unresolved block (or nil). At least one of the fields
616 * in each block is already resolved.
619 backpatch(list
, target
)
620 struct block
*list
, *target
;
637 * Merge the lists in b0 and b1, using the 'sense' field to indicate
638 * which of jt and jf is the link.
642 struct block
*b0
, *b1
;
644 register struct block
**p
= &b0
;
646 /* Find end of list. */
648 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
650 /* Concatenate the lists. */
658 struct block
*ppi_dlt_check
;
661 * Insert before the statements of the first (root) block any
662 * statements needed to load the lengths of any variable-length
663 * headers into registers.
665 * XXX - a fancier strategy would be to insert those before the
666 * statements of all blocks that use those lengths and that
667 * have no predecessors that use them, so that we only compute
668 * the lengths if we need them. There might be even better
669 * approaches than that.
671 * However, those strategies would be more complicated, and
672 * as we don't generate code to compute a length if the
673 * program has no tests that use the length, and as most
674 * tests will probably use those lengths, we would just
675 * postpone computing the lengths so that it's not done
676 * for tests that fail early, and it's not clear that's
679 insert_compute_vloffsets(p
->head
);
682 * For DLT_PPI captures, generate a check of the per-packet
683 * DLT value to make sure it's DLT_IEEE802_11.
685 * XXX - TurboCap cards use DLT_PPI for Ethernet.
686 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
687 * with appropriate Ethernet information and use that rather
688 * than using something such as DLT_PPI where you don't know
689 * the link-layer header type until runtime, which, in the
690 * general case, would force us to generate both Ethernet *and*
691 * 802.11 code (*and* anything else for which PPI is used)
692 * and choose between them early in the BPF program?
694 ppi_dlt_check
= gen_ppi_dlt_check();
695 if (ppi_dlt_check
!= NULL
)
696 gen_and(ppi_dlt_check
, p
);
698 backpatch(p
, gen_retblk(snaplen
));
699 p
->sense
= !p
->sense
;
700 backpatch(p
, gen_retblk(0));
706 struct block
*b0
, *b1
;
708 backpatch(b0
, b1
->head
);
709 b0
->sense
= !b0
->sense
;
710 b1
->sense
= !b1
->sense
;
712 b1
->sense
= !b1
->sense
;
718 struct block
*b0
, *b1
;
720 b0
->sense
= !b0
->sense
;
721 backpatch(b0
, b1
->head
);
722 b0
->sense
= !b0
->sense
;
731 b
->sense
= !b
->sense
;
734 static struct block
*
735 gen_cmp(offrel
, offset
, size
, v
)
736 enum e_offrel offrel
;
740 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
743 static struct block
*
744 gen_cmp_gt(offrel
, offset
, size
, v
)
745 enum e_offrel offrel
;
749 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
752 static struct block
*
753 gen_cmp_ge(offrel
, offset
, size
, v
)
754 enum e_offrel offrel
;
758 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
761 static struct block
*
762 gen_cmp_lt(offrel
, offset
, size
, v
)
763 enum e_offrel offrel
;
767 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
770 static struct block
*
771 gen_cmp_le(offrel
, offset
, size
, v
)
772 enum e_offrel offrel
;
776 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
779 static struct block
*
780 gen_mcmp(offrel
, offset
, size
, v
, mask
)
781 enum e_offrel offrel
;
786 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
789 static struct block
*
790 gen_bcmp(offrel
, offset
, size
, v
)
791 enum e_offrel offrel
;
792 register u_int offset
, size
;
793 register const u_char
*v
;
795 register struct block
*b
, *tmp
;
799 register const u_char
*p
= &v
[size
- 4];
800 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
801 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
803 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
810 register const u_char
*p
= &v
[size
- 2];
811 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
813 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
820 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
829 * AND the field of size "size" at offset "offset" relative to the header
830 * specified by "offrel" with "mask", and compare it with the value "v"
831 * with the test specified by "jtype"; if "reverse" is true, the test
832 * should test the opposite of "jtype".
834 static struct block
*
835 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
836 enum e_offrel offrel
;
838 bpf_u_int32 offset
, size
, mask
, jtype
;
841 struct slist
*s
, *s2
;
844 s
= gen_load_a(offrel
, offset
, size
);
846 if (mask
!= 0xffffffff) {
847 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
852 b
= new_block(JMP(jtype
));
855 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
861 * Various code constructs need to know the layout of the packet.
862 * These variables give the necessary offsets from the beginning
863 * of the packet data.
867 * Absolute offset of the beginning of the link-layer header.
869 static bpf_abs_offset off_linkhdr
;
872 * If we're checking a link-layer header for a packet encapsulated in
873 * another protocol layer, this is the equivalent information for the
874 * previous layers' link-layer header from the beginning of the raw
877 static bpf_abs_offset off_prevlinkhdr
;
880 * This is the equivalent information for the outermost layers' link-layer
883 static bpf_abs_offset off_outermostlinkhdr
;
886 * "Push" the current value of the link-layer header type and link-layer
887 * header offset onto a "stack", and set a new value. (It's not a
888 * full-blown stack; we keep only the top two items.)
890 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
892 prevlinktype = new_linktype; \
893 off_prevlinkhdr = off_linkhdr; \
894 linktype = new_linktype; \
895 off_linkhdr.is_variable = new_is_variable; \
896 off_linkhdr.constant_part = new_constant_part; \
897 off_linkhdr.reg = new_reg; \
902 * Absolute offset of the beginning of the link-layer payload.
904 static bpf_abs_offset off_linkpl
;
907 * "off_linktype" is the offset to information in the link-layer header
908 * giving the packet type. This is an absolute offset from the beginning
911 * For Ethernet, it's the offset of the Ethernet type field; this
912 * means that it must have a value that skips VLAN tags.
914 * For link-layer types that always use 802.2 headers, it's the
915 * offset of the LLC header; this means that it must have a value
916 * that skips VLAN tags.
918 * For PPP, it's the offset of the PPP type field.
920 * For Cisco HDLC, it's the offset of the CHDLC type field.
922 * For BSD loopback, it's the offset of the AF_ value.
924 * For Linux cooked sockets, it's the offset of the type field.
926 * off_linktype.constant_part is set to -1 for no encapsulation,
927 * in which case, IP is assumed.
929 static bpf_abs_offset off_linktype
;
932 * TRUE if the link layer includes an ATM pseudo-header.
934 static int is_atm
= 0;
937 * TRUE if "geneve" appeared in the filter; it causes us to generate
938 * code that checks for a Geneve header and assume that later filters
939 * apply to the encapsulated payload.
941 static int is_geneve
= 0;
944 * These are offsets for the ATM pseudo-header.
946 static u_int off_vpi
;
947 static u_int off_vci
;
948 static u_int off_proto
;
951 * These are offsets for the MTP2 fields.
954 static u_int off_li_hsl
;
957 * These are offsets for the MTP3 fields.
959 static u_int off_sio
;
960 static u_int off_opc
;
961 static u_int off_dpc
;
962 static u_int off_sls
;
965 * This is the offset of the first byte after the ATM pseudo_header,
966 * or -1 if there is no ATM pseudo-header.
968 static u_int off_payload
;
971 * These are offsets to the beginning of the network-layer header.
972 * They are relative to the beginning of the link-layer payload (i.e.,
973 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
975 * If the link layer never uses 802.2 LLC:
977 * "off_nl" and "off_nl_nosnap" are the same.
979 * If the link layer always uses 802.2 LLC:
981 * "off_nl" is the offset if there's a SNAP header following
984 * "off_nl_nosnap" is the offset if there's no SNAP header.
986 * If the link layer is Ethernet:
988 * "off_nl" is the offset if the packet is an Ethernet II packet
989 * (we assume no 802.3+802.2+SNAP);
991 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
992 * with an 802.2 header following it.
995 static u_int off_nl_nosnap
;
998 static int prevlinktype
;
999 static int outermostlinktype
;
1005 pcap_fddipad
= p
->fddipad
;
1008 * We start out with only one link-layer header.
1010 outermostlinktype
= pcap_datalink(p
);
1011 off_outermostlinkhdr
.constant_part
= 0;
1012 off_outermostlinkhdr
.is_variable
= 0;
1013 off_outermostlinkhdr
.reg
= -1;
1015 prevlinktype
= outermostlinktype
;
1016 off_prevlinkhdr
.constant_part
= 0;
1017 off_prevlinkhdr
.is_variable
= 0;
1018 off_prevlinkhdr
.reg
= -1;
1020 linktype
= outermostlinktype
;
1021 off_linkhdr
.constant_part
= 0;
1022 off_linkhdr
.is_variable
= 0;
1023 off_linkhdr
.reg
= -1;
1028 off_linkpl
.constant_part
= 0;
1029 off_linkpl
.is_variable
= 0;
1030 off_linkpl
.reg
= -1;
1032 off_linktype
.constant_part
= 0;
1033 off_linktype
.is_variable
= 0;
1034 off_linktype
.reg
= -1;
1037 * Assume it's not raw ATM with a pseudo-header, for now.
1051 * And assume we're not doing SS7.
1060 label_stack_depth
= 0;
1061 vlan_stack_depth
= 0;
1066 off_linktype
.constant_part
= 2;
1067 off_linkpl
.constant_part
= 6;
1068 off_nl
= 0; /* XXX in reality, variable! */
1069 off_nl_nosnap
= 0; /* no 802.2 LLC */
1072 case DLT_ARCNET_LINUX
:
1073 off_linktype
.constant_part
= 4;
1074 off_linkpl
.constant_part
= 8;
1075 off_nl
= 0; /* XXX in reality, variable! */
1076 off_nl_nosnap
= 0; /* no 802.2 LLC */
1080 off_linktype
.constant_part
= 12;
1081 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1082 off_nl
= 0; /* Ethernet II */
1083 off_nl_nosnap
= 3; /* 802.3+802.2 */
1088 * SLIP doesn't have a link level type. The 16 byte
1089 * header is hacked into our SLIP driver.
1091 off_linktype
.constant_part
= -1;
1092 off_linkpl
.constant_part
= 16;
1094 off_nl_nosnap
= 0; /* no 802.2 LLC */
1097 case DLT_SLIP_BSDOS
:
1098 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1099 off_linktype
.constant_part
= -1;
1101 off_linkpl
.constant_part
= 24;
1103 off_nl_nosnap
= 0; /* no 802.2 LLC */
1108 off_linktype
.constant_part
= 0;
1109 off_linkpl
.constant_part
= 4;
1111 off_nl_nosnap
= 0; /* no 802.2 LLC */
1115 off_linktype
.constant_part
= 0;
1116 off_linkpl
.constant_part
= 12;
1118 off_nl_nosnap
= 0; /* no 802.2 LLC */
1123 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1124 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1125 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1126 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1128 off_nl_nosnap
= 0; /* no 802.2 LLC */
1133 * This does no include the Ethernet header, and
1134 * only covers session state.
1136 off_linktype
.constant_part
= 6;
1137 off_linkpl
.constant_part
= 8;
1139 off_nl_nosnap
= 0; /* no 802.2 LLC */
1143 off_linktype
.constant_part
= 5;
1144 off_linkpl
.constant_part
= 24;
1146 off_nl_nosnap
= 0; /* no 802.2 LLC */
1151 * FDDI doesn't really have a link-level type field.
1152 * We set "off_linktype" to the offset of the LLC header.
1154 * To check for Ethernet types, we assume that SSAP = SNAP
1155 * is being used and pick out the encapsulated Ethernet type.
1156 * XXX - should we generate code to check for SNAP?
1158 off_linktype
.constant_part
= 13;
1159 off_linktype
.constant_part
+= pcap_fddipad
;
1160 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1161 off_linkpl
.constant_part
+= pcap_fddipad
;
1162 off_nl
= 8; /* 802.2+SNAP */
1163 off_nl_nosnap
= 3; /* 802.2 */
1168 * Token Ring doesn't really have a link-level type field.
1169 * We set "off_linktype" to the offset of the LLC header.
1171 * To check for Ethernet types, we assume that SSAP = SNAP
1172 * is being used and pick out the encapsulated Ethernet type.
1173 * XXX - should we generate code to check for SNAP?
1175 * XXX - the header is actually variable-length.
1176 * Some various Linux patched versions gave 38
1177 * as "off_linktype" and 40 as "off_nl"; however,
1178 * if a token ring packet has *no* routing
1179 * information, i.e. is not source-routed, the correct
1180 * values are 20 and 22, as they are in the vanilla code.
1182 * A packet is source-routed iff the uppermost bit
1183 * of the first byte of the source address, at an
1184 * offset of 8, has the uppermost bit set. If the
1185 * packet is source-routed, the total number of bytes
1186 * of routing information is 2 plus bits 0x1F00 of
1187 * the 16-bit value at an offset of 14 (shifted right
1188 * 8 - figure out which byte that is).
1190 off_linktype
.constant_part
= 14;
1191 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1192 off_nl
= 8; /* 802.2+SNAP */
1193 off_nl_nosnap
= 3; /* 802.2 */
1196 case DLT_PRISM_HEADER
:
1197 case DLT_IEEE802_11_RADIO_AVS
:
1198 case DLT_IEEE802_11_RADIO
:
1199 off_linkhdr
.is_variable
= 1;
1200 /* Fall through, 802.11 doesn't have a variable link
1201 * prefix but is otherwise the same. */
1203 case DLT_IEEE802_11
:
1205 * 802.11 doesn't really have a link-level type field.
1206 * We set "off_linktype.constant_part" to the offset of
1209 * To check for Ethernet types, we assume that SSAP = SNAP
1210 * is being used and pick out the encapsulated Ethernet type.
1211 * XXX - should we generate code to check for SNAP?
1213 * We also handle variable-length radio headers here.
1214 * The Prism header is in theory variable-length, but in
1215 * practice it's always 144 bytes long. However, some
1216 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1217 * sometimes or always supply an AVS header, so we
1218 * have to check whether the radio header is a Prism
1219 * header or an AVS header, so, in practice, it's
1222 off_linktype
.constant_part
= 24;
1223 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1224 off_linkpl
.is_variable
= 1;
1225 off_nl
= 8; /* 802.2+SNAP */
1226 off_nl_nosnap
= 3; /* 802.2 */
1231 * At the moment we treat PPI the same way that we treat
1232 * normal Radiotap encoded packets. The difference is in
1233 * the function that generates the code at the beginning
1234 * to compute the header length. Since this code generator
1235 * of PPI supports bare 802.11 encapsulation only (i.e.
1236 * the encapsulated DLT should be DLT_IEEE802_11) we
1237 * generate code to check for this too.
1239 off_linktype
.constant_part
= 24;
1240 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1241 off_linkpl
.is_variable
= 1;
1242 off_linkhdr
.is_variable
= 1;
1243 off_nl
= 8; /* 802.2+SNAP */
1244 off_nl_nosnap
= 3; /* 802.2 */
1247 case DLT_ATM_RFC1483
:
1248 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1250 * assume routed, non-ISO PDUs
1251 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1253 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1254 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1255 * latter would presumably be treated the way PPPoE
1256 * should be, so you can do "pppoe and udp port 2049"
1257 * or "pppoa and tcp port 80" and have it check for
1258 * PPPo{A,E} and a PPP protocol of IP and....
1260 off_linktype
.constant_part
= 0;
1261 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1262 off_nl
= 8; /* 802.2+SNAP */
1263 off_nl_nosnap
= 3; /* 802.2 */
1268 * Full Frontal ATM; you get AALn PDUs with an ATM
1272 off_vpi
= SUNATM_VPI_POS
;
1273 off_vci
= SUNATM_VCI_POS
;
1274 off_proto
= PROTO_POS
;
1275 off_payload
= SUNATM_PKT_BEGIN_POS
;
1276 off_linktype
.constant_part
= off_payload
;
1277 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1278 off_nl
= 8; /* 802.2+SNAP */
1279 off_nl_nosnap
= 3; /* 802.2 */
1285 off_linktype
.constant_part
= -1;
1286 off_linkpl
.constant_part
= 0;
1288 off_nl_nosnap
= 0; /* no 802.2 LLC */
1291 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1292 off_linktype
.constant_part
= 14;
1293 off_linkpl
.constant_part
= 16;
1295 off_nl_nosnap
= 0; /* no 802.2 LLC */
1300 * LocalTalk does have a 1-byte type field in the LLAP header,
1301 * but really it just indicates whether there is a "short" or
1302 * "long" DDP packet following.
1304 off_linktype
.constant_part
= -1;
1305 off_linkpl
.constant_part
= 0;
1307 off_nl_nosnap
= 0; /* no 802.2 LLC */
1310 case DLT_IP_OVER_FC
:
1312 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1313 * link-level type field. We set "off_linktype" to the
1314 * offset of the LLC header.
1316 * To check for Ethernet types, we assume that SSAP = SNAP
1317 * is being used and pick out the encapsulated Ethernet type.
1318 * XXX - should we generate code to check for SNAP? RFC
1319 * 2625 says SNAP should be used.
1321 off_linktype
.constant_part
= 16;
1322 off_linkpl
.constant_part
= 16;
1323 off_nl
= 8; /* 802.2+SNAP */
1324 off_nl_nosnap
= 3; /* 802.2 */
1329 * XXX - we should set this to handle SNAP-encapsulated
1330 * frames (NLPID of 0x80).
1332 off_linktype
.constant_part
= -1;
1333 off_linkpl
.constant_part
= 0;
1335 off_nl_nosnap
= 0; /* no 802.2 LLC */
1339 * the only BPF-interesting FRF.16 frames are non-control frames;
1340 * Frame Relay has a variable length link-layer
1341 * so lets start with offset 4 for now and increments later on (FIXME);
1344 off_linktype
.constant_part
= -1;
1345 off_linkpl
.constant_part
= 0;
1347 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1350 case DLT_APPLE_IP_OVER_IEEE1394
:
1351 off_linktype
.constant_part
= 16;
1352 off_linkpl
.constant_part
= 18;
1354 off_nl_nosnap
= 0; /* no 802.2 LLC */
1357 case DLT_SYMANTEC_FIREWALL
:
1358 off_linktype
.constant_part
= 6;
1359 off_linkpl
.constant_part
= 44;
1360 off_nl
= 0; /* Ethernet II */
1361 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1364 #ifdef HAVE_NET_PFVAR_H
1366 off_linktype
.constant_part
= 0;
1367 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1369 off_nl_nosnap
= 0; /* no 802.2 LLC */
1373 case DLT_JUNIPER_MFR
:
1374 case DLT_JUNIPER_MLFR
:
1375 case DLT_JUNIPER_MLPPP
:
1376 case DLT_JUNIPER_PPP
:
1377 case DLT_JUNIPER_CHDLC
:
1378 case DLT_JUNIPER_FRELAY
:
1379 off_linktype
.constant_part
= 4;
1380 off_linkpl
.constant_part
= 4;
1382 off_nl_nosnap
= -1; /* no 802.2 LLC */
1385 case DLT_JUNIPER_ATM1
:
1386 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1387 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1392 case DLT_JUNIPER_ATM2
:
1393 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1394 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1399 /* frames captured on a Juniper PPPoE service PIC
1400 * contain raw ethernet frames */
1401 case DLT_JUNIPER_PPPOE
:
1402 case DLT_JUNIPER_ETHER
:
1403 off_linkpl
.constant_part
= 14;
1404 off_linktype
.constant_part
= 16;
1405 off_nl
= 18; /* Ethernet II */
1406 off_nl_nosnap
= 21; /* 802.3+802.2 */
1409 case DLT_JUNIPER_PPPOE_ATM
:
1410 off_linktype
.constant_part
= 4;
1411 off_linkpl
.constant_part
= 6;
1413 off_nl_nosnap
= -1; /* no 802.2 LLC */
1416 case DLT_JUNIPER_GGSN
:
1417 off_linktype
.constant_part
= 6;
1418 off_linkpl
.constant_part
= 12;
1420 off_nl_nosnap
= -1; /* no 802.2 LLC */
1423 case DLT_JUNIPER_ES
:
1424 off_linktype
.constant_part
= 6;
1425 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1426 off_nl
= -1; /* not really a network layer but raw IP addresses */
1427 off_nl_nosnap
= -1; /* no 802.2 LLC */
1430 case DLT_JUNIPER_MONITOR
:
1431 off_linktype
.constant_part
= 12;
1432 off_linkpl
.constant_part
= 12;
1433 off_nl
= 0; /* raw IP/IP6 header */
1434 off_nl_nosnap
= -1; /* no 802.2 LLC */
1437 case DLT_BACNET_MS_TP
:
1438 off_linktype
.constant_part
= -1;
1439 off_linkpl
.constant_part
= -1;
1444 case DLT_JUNIPER_SERVICES
:
1445 off_linktype
.constant_part
= 12;
1446 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1447 off_nl
= -1; /* L3 proto location dep. on cookie type */
1448 off_nl_nosnap
= -1; /* no 802.2 LLC */
1451 case DLT_JUNIPER_VP
:
1452 off_linktype
.constant_part
= 18;
1453 off_linkpl
.constant_part
= -1;
1458 case DLT_JUNIPER_ST
:
1459 off_linktype
.constant_part
= 18;
1460 off_linkpl
.constant_part
= -1;
1465 case DLT_JUNIPER_ISM
:
1466 off_linktype
.constant_part
= 8;
1467 off_linkpl
.constant_part
= -1;
1472 case DLT_JUNIPER_VS
:
1473 case DLT_JUNIPER_SRX_E2E
:
1474 case DLT_JUNIPER_FIBRECHANNEL
:
1475 case DLT_JUNIPER_ATM_CEMIC
:
1476 off_linktype
.constant_part
= 8;
1477 off_linkpl
.constant_part
= -1;
1489 off_linktype
.constant_part
= -1;
1490 off_linkpl
.constant_part
= -1;
1495 case DLT_MTP2_WITH_PHDR
:
1502 off_linktype
.constant_part
= -1;
1503 off_linkpl
.constant_part
= -1;
1515 off_linktype
.constant_part
= -1;
1516 off_linkpl
.constant_part
= -1;
1522 off_linktype
.constant_part
= -1;
1523 off_linkpl
.constant_part
= 4;
1530 * Currently, only raw "link[N:M]" filtering is supported.
1532 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1533 off_linkpl
.constant_part
= -1;
1534 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1535 off_nl_nosnap
= -1; /* no 802.2 LLC */
1539 off_linktype
.constant_part
= 1;
1540 off_linkpl
.constant_part
= 24; /* ipnet header length */
1545 case DLT_NETANALYZER
:
1546 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1547 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1548 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1549 off_nl
= 0; /* Ethernet II */
1550 off_nl_nosnap
= 3; /* 802.3+802.2 */
1553 case DLT_NETANALYZER_TRANSPARENT
:
1554 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1555 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1556 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1557 off_nl
= 0; /* Ethernet II */
1558 off_nl_nosnap
= 3; /* 802.3+802.2 */
1563 * For values in the range in which we've assigned new
1564 * DLT_ values, only raw "link[N:M]" filtering is supported.
1566 if (linktype
>= DLT_MATCHING_MIN
&&
1567 linktype
<= DLT_MATCHING_MAX
) {
1568 off_linktype
.constant_part
= -1;
1569 off_linkpl
.constant_part
= -1;
1573 bpf_error("unknown data link type %d", linktype
);
1578 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1582 * Load a value relative to the specified absolute offset.
1584 static struct slist
*
1585 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1587 struct slist
*s
, *s2
;
1589 s
= gen_abs_offset_varpart(abs_offset
);
1592 * If "s" is non-null, it has code to arrange that the X register
1593 * contains the variable part of the absolute offset, so we
1594 * generate a load relative to that, with an offset of
1595 * abs_offset->constant_part + offset.
1597 * Otherwise, we can do an absolute load with an offset of
1598 * abs_offset->constant_part + offset.
1602 * "s" points to a list of statements that puts the
1603 * variable part of the absolute offset into the X register.
1604 * Do an indirect load, to use the X register as an offset.
1606 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1607 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1611 * There is no variable part of the absolute offset, so
1612 * just do an absolute load.
1614 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1615 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1621 * Load a value relative to the beginning of the specified header.
1623 static struct slist
*
1624 gen_load_a(offrel
, offset
, size
)
1625 enum e_offrel offrel
;
1628 struct slist
*s
, *s2
;
1633 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1638 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1641 case OR_PREVLINKHDR
:
1642 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1646 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1649 case OR_PREVMPLSHDR
:
1650 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1654 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1657 case OR_LINKPL_NOSNAP
:
1658 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1662 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1667 * Load the X register with the length of the IPv4 header
1668 * (plus the offset of the link-layer header, if it's
1669 * preceded by a variable-length header such as a radio
1670 * header), in bytes.
1672 s
= gen_loadx_iphdrlen();
1675 * Load the item at {offset of the link-layer payload} +
1676 * {offset, relative to the start of the link-layer
1677 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1678 * {specified offset}.
1680 * If the offset of the link-layer payload is variable,
1681 * the variable part of that offset is included in the
1682 * value in the X register, and we include the constant
1683 * part in the offset of the load.
1685 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1686 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1691 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1702 * Generate code to load into the X register the sum of the length of
1703 * the IPv4 header and the variable part of the offset of the link-layer
1706 static struct slist
*
1707 gen_loadx_iphdrlen()
1709 struct slist
*s
, *s2
;
1711 s
= gen_abs_offset_varpart(&off_linkpl
);
1714 * The offset of the link-layer payload has a variable
1715 * part. "s" points to a list of statements that put
1716 * the variable part of that offset into the X register.
1718 * The 4*([k]&0xf) addressing mode can't be used, as we
1719 * don't have a constant offset, so we have to load the
1720 * value in question into the A register and add to it
1721 * the value from the X register.
1723 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1724 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1726 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1729 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1734 * The A register now contains the length of the IP header.
1735 * We need to add to it the variable part of the offset of
1736 * the link-layer payload, which is still in the X
1737 * register, and move the result into the X register.
1739 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1740 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1743 * The offset of the link-layer payload is a constant,
1744 * so no code was generated to load the (non-existent)
1745 * variable part of that offset.
1747 * This means we can use the 4*([k]&0xf) addressing
1748 * mode. Load the length of the IPv4 header, which
1749 * is at an offset of off_nl from the beginning of
1750 * the link-layer payload, and thus at an offset of
1751 * off_linkpl.constant_part + off_nl from the beginning
1752 * of the raw packet data, using that addressing mode.
1754 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1755 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1760 static struct block
*
1767 s
= new_stmt(BPF_LD
|BPF_IMM
);
1769 b
= new_block(JMP(BPF_JEQ
));
1775 static inline struct block
*
1778 return gen_uncond(1);
1781 static inline struct block
*
1784 return gen_uncond(0);
1788 * Byte-swap a 32-bit number.
1789 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1790 * big-endian platforms.)
1792 #define SWAPLONG(y) \
1793 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1796 * Generate code to match a particular packet type.
1798 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1799 * value, if <= ETHERMTU. We use that to determine whether to
1800 * match the type/length field or to check the type/length field for
1801 * a value <= ETHERMTU to see whether it's a type field and then do
1802 * the appropriate test.
1804 static struct block
*
1805 gen_ether_linktype(proto
)
1808 struct block
*b0
, *b1
;
1814 case LLCSAP_NETBEUI
:
1816 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1817 * so we check the DSAP and SSAP.
1819 * LLCSAP_IP checks for IP-over-802.2, rather
1820 * than IP-over-Ethernet or IP-over-SNAP.
1822 * XXX - should we check both the DSAP and the
1823 * SSAP, like this, or should we check just the
1824 * DSAP, as we do for other types <= ETHERMTU
1825 * (i.e., other SAP values)?
1827 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1829 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1830 ((proto
<< 8) | proto
));
1838 * Ethernet_II frames, which are Ethernet
1839 * frames with a frame type of ETHERTYPE_IPX;
1841 * Ethernet_802.3 frames, which are 802.3
1842 * frames (i.e., the type/length field is
1843 * a length field, <= ETHERMTU, rather than
1844 * a type field) with the first two bytes
1845 * after the Ethernet/802.3 header being
1848 * Ethernet_802.2 frames, which are 802.3
1849 * frames with an 802.2 LLC header and
1850 * with the IPX LSAP as the DSAP in the LLC
1853 * Ethernet_SNAP frames, which are 802.3
1854 * frames with an LLC header and a SNAP
1855 * header and with an OUI of 0x000000
1856 * (encapsulated Ethernet) and a protocol
1857 * ID of ETHERTYPE_IPX in the SNAP header.
1859 * XXX - should we generate the same code both
1860 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1864 * This generates code to check both for the
1865 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1867 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1868 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1872 * Now we add code to check for SNAP frames with
1873 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1875 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1879 * Now we generate code to check for 802.3
1880 * frames in general.
1882 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1886 * Now add the check for 802.3 frames before the
1887 * check for Ethernet_802.2 and Ethernet_802.3,
1888 * as those checks should only be done on 802.3
1889 * frames, not on Ethernet frames.
1894 * Now add the check for Ethernet_II frames, and
1895 * do that before checking for the other frame
1898 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1902 case ETHERTYPE_ATALK
:
1903 case ETHERTYPE_AARP
:
1905 * EtherTalk (AppleTalk protocols on Ethernet link
1906 * layer) may use 802.2 encapsulation.
1910 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1911 * we check for an Ethernet type field less than
1912 * 1500, which means it's an 802.3 length field.
1914 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1918 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1919 * SNAP packets with an organization code of
1920 * 0x080007 (Apple, for Appletalk) and a protocol
1921 * type of ETHERTYPE_ATALK (Appletalk).
1923 * 802.2-encapsulated ETHERTYPE_AARP packets are
1924 * SNAP packets with an organization code of
1925 * 0x000000 (encapsulated Ethernet) and a protocol
1926 * type of ETHERTYPE_AARP (Appletalk ARP).
1928 if (proto
== ETHERTYPE_ATALK
)
1929 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1930 else /* proto == ETHERTYPE_AARP */
1931 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1935 * Check for Ethernet encapsulation (Ethertalk
1936 * phase 1?); we just check for the Ethernet
1939 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1945 if (proto
<= ETHERMTU
) {
1947 * This is an LLC SAP value, so the frames
1948 * that match would be 802.2 frames.
1949 * Check that the frame is an 802.2 frame
1950 * (i.e., that the length/type field is
1951 * a length field, <= ETHERMTU) and
1952 * then check the DSAP.
1954 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1956 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1961 * This is an Ethernet type, so compare
1962 * the length/type field with it (if
1963 * the frame is an 802.2 frame, the length
1964 * field will be <= ETHERMTU, and, as
1965 * "proto" is > ETHERMTU, this test
1966 * will fail and the frame won't match,
1967 * which is what we want).
1969 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1976 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1977 * or IPv6 then we have an error.
1979 static struct block
*
1980 gen_ipnet_linktype(proto
)
1986 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1989 case ETHERTYPE_IPV6
:
1990 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1991 (bpf_int32
)IPH_AF_INET6
);
2002 * Generate code to match a particular packet type.
2004 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2005 * value, if <= ETHERMTU. We use that to determine whether to
2006 * match the type field or to check the type field for the special
2007 * LINUX_SLL_P_802_2 value and then do the appropriate test.
2009 static struct block
*
2010 gen_linux_sll_linktype(proto
)
2013 struct block
*b0
, *b1
;
2019 case LLCSAP_NETBEUI
:
2021 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2022 * so we check the DSAP and SSAP.
2024 * LLCSAP_IP checks for IP-over-802.2, rather
2025 * than IP-over-Ethernet or IP-over-SNAP.
2027 * XXX - should we check both the DSAP and the
2028 * SSAP, like this, or should we check just the
2029 * DSAP, as we do for other types <= ETHERMTU
2030 * (i.e., other SAP values)?
2032 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2033 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
2034 ((proto
<< 8) | proto
));
2040 * Ethernet_II frames, which are Ethernet
2041 * frames with a frame type of ETHERTYPE_IPX;
2043 * Ethernet_802.3 frames, which have a frame
2044 * type of LINUX_SLL_P_802_3;
2046 * Ethernet_802.2 frames, which are 802.3
2047 * frames with an 802.2 LLC header (i.e, have
2048 * a frame type of LINUX_SLL_P_802_2) and
2049 * with the IPX LSAP as the DSAP in the LLC
2052 * Ethernet_SNAP frames, which are 802.3
2053 * frames with an LLC header and a SNAP
2054 * header and with an OUI of 0x000000
2055 * (encapsulated Ethernet) and a protocol
2056 * ID of ETHERTYPE_IPX in the SNAP header.
2058 * First, do the checks on LINUX_SLL_P_802_2
2059 * frames; generate the check for either
2060 * Ethernet_802.2 or Ethernet_SNAP frames, and
2061 * then put a check for LINUX_SLL_P_802_2 frames
2064 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2065 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2067 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2071 * Now check for 802.3 frames and OR that with
2072 * the previous test.
2074 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2078 * Now add the check for Ethernet_II frames, and
2079 * do that before checking for the other frame
2082 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2086 case ETHERTYPE_ATALK
:
2087 case ETHERTYPE_AARP
:
2089 * EtherTalk (AppleTalk protocols on Ethernet link
2090 * layer) may use 802.2 encapsulation.
2094 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2095 * we check for the 802.2 protocol type in the
2096 * "Ethernet type" field.
2098 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2101 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2102 * SNAP packets with an organization code of
2103 * 0x080007 (Apple, for Appletalk) and a protocol
2104 * type of ETHERTYPE_ATALK (Appletalk).
2106 * 802.2-encapsulated ETHERTYPE_AARP packets are
2107 * SNAP packets with an organization code of
2108 * 0x000000 (encapsulated Ethernet) and a protocol
2109 * type of ETHERTYPE_AARP (Appletalk ARP).
2111 if (proto
== ETHERTYPE_ATALK
)
2112 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2113 else /* proto == ETHERTYPE_AARP */
2114 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2118 * Check for Ethernet encapsulation (Ethertalk
2119 * phase 1?); we just check for the Ethernet
2122 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2128 if (proto
<= ETHERMTU
) {
2130 * This is an LLC SAP value, so the frames
2131 * that match would be 802.2 frames.
2132 * Check for the 802.2 protocol type
2133 * in the "Ethernet type" field, and
2134 * then check the DSAP.
2136 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2137 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2143 * This is an Ethernet type, so compare
2144 * the length/type field with it (if
2145 * the frame is an 802.2 frame, the length
2146 * field will be <= ETHERMTU, and, as
2147 * "proto" is > ETHERMTU, this test
2148 * will fail and the frame won't match,
2149 * which is what we want).
2151 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2156 static struct slist
*
2157 gen_load_prism_llprefixlen()
2159 struct slist
*s1
, *s2
;
2160 struct slist
*sjeq_avs_cookie
;
2161 struct slist
*sjcommon
;
2164 * This code is not compatible with the optimizer, as
2165 * we are generating jmp instructions within a normal
2166 * slist of instructions
2171 * Generate code to load the length of the radio header into
2172 * the register assigned to hold that length, if one has been
2173 * assigned. (If one hasn't been assigned, no code we've
2174 * generated uses that prefix, so we don't need to generate any
2177 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2178 * or always use the AVS header rather than the Prism header.
2179 * We load a 4-byte big-endian value at the beginning of the
2180 * raw packet data, and see whether, when masked with 0xFFFFF000,
2181 * it's equal to 0x80211000. If so, that indicates that it's
2182 * an AVS header (the masked-out bits are the version number).
2183 * Otherwise, it's a Prism header.
2185 * XXX - the Prism header is also, in theory, variable-length,
2186 * but no known software generates headers that aren't 144
2189 if (off_linkhdr
.reg
!= -1) {
2193 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2197 * AND it with 0xFFFFF000.
2199 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2200 s2
->s
.k
= 0xFFFFF000;
2204 * Compare with 0x80211000.
2206 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2207 sjeq_avs_cookie
->s
.k
= 0x80211000;
2208 sappend(s1
, sjeq_avs_cookie
);
2213 * The 4 bytes at an offset of 4 from the beginning of
2214 * the AVS header are the length of the AVS header.
2215 * That field is big-endian.
2217 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2220 sjeq_avs_cookie
->s
.jt
= s2
;
2223 * Now jump to the code to allocate a register
2224 * into which to save the header length and
2225 * store the length there. (The "jump always"
2226 * instruction needs to have the k field set;
2227 * it's added to the PC, so, as we're jumping
2228 * over a single instruction, it should be 1.)
2230 sjcommon
= new_stmt(JMP(BPF_JA
));
2232 sappend(s1
, sjcommon
);
2235 * Now for the code that handles the Prism header.
2236 * Just load the length of the Prism header (144)
2237 * into the A register. Have the test for an AVS
2238 * header branch here if we don't have an AVS header.
2240 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2243 sjeq_avs_cookie
->s
.jf
= s2
;
2246 * Now allocate a register to hold that value and store
2247 * it. The code for the AVS header will jump here after
2248 * loading the length of the AVS header.
2250 s2
= new_stmt(BPF_ST
);
2251 s2
->s
.k
= off_linkhdr
.reg
;
2253 sjcommon
->s
.jf
= s2
;
2256 * Now move it into the X register.
2258 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2266 static struct slist
*
2267 gen_load_avs_llprefixlen()
2269 struct slist
*s1
, *s2
;
2272 * Generate code to load the length of the AVS header into
2273 * the register assigned to hold that length, if one has been
2274 * assigned. (If one hasn't been assigned, no code we've
2275 * generated uses that prefix, so we don't need to generate any
2278 if (off_linkhdr
.reg
!= -1) {
2280 * The 4 bytes at an offset of 4 from the beginning of
2281 * the AVS header are the length of the AVS header.
2282 * That field is big-endian.
2284 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2288 * Now allocate a register to hold that value and store
2291 s2
= new_stmt(BPF_ST
);
2292 s2
->s
.k
= off_linkhdr
.reg
;
2296 * Now move it into the X register.
2298 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2306 static struct slist
*
2307 gen_load_radiotap_llprefixlen()
2309 struct slist
*s1
, *s2
;
2312 * Generate code to load the length of the radiotap header into
2313 * the register assigned to hold that length, if one has been
2314 * assigned. (If one hasn't been assigned, no code we've
2315 * generated uses that prefix, so we don't need to generate any
2318 if (off_linkhdr
.reg
!= -1) {
2320 * The 2 bytes at offsets of 2 and 3 from the beginning
2321 * of the radiotap header are the length of the radiotap
2322 * header; unfortunately, it's little-endian, so we have
2323 * to load it a byte at a time and construct the value.
2327 * Load the high-order byte, at an offset of 3, shift it
2328 * left a byte, and put the result in the X register.
2330 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2332 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2335 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2339 * Load the next byte, at an offset of 2, and OR the
2340 * value from the X register into it.
2342 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2345 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2349 * Now allocate a register to hold that value and store
2352 s2
= new_stmt(BPF_ST
);
2353 s2
->s
.k
= off_linkhdr
.reg
;
2357 * Now move it into the X register.
2359 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2368 * At the moment we treat PPI as normal Radiotap encoded
2369 * packets. The difference is in the function that generates
2370 * the code at the beginning to compute the header length.
2371 * Since this code generator of PPI supports bare 802.11
2372 * encapsulation only (i.e. the encapsulated DLT should be
2373 * DLT_IEEE802_11) we generate code to check for this too;
2374 * that's done in finish_parse().
2376 static struct slist
*
2377 gen_load_ppi_llprefixlen()
2379 struct slist
*s1
, *s2
;
2382 * Generate code to load the length of the radiotap header
2383 * into the register assigned to hold that length, if one has
2386 if (off_linkhdr
.reg
!= -1) {
2388 * The 2 bytes at offsets of 2 and 3 from the beginning
2389 * of the radiotap header are the length of the radiotap
2390 * header; unfortunately, it's little-endian, so we have
2391 * to load it a byte at a time and construct the value.
2395 * Load the high-order byte, at an offset of 3, shift it
2396 * left a byte, and put the result in the X register.
2398 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2400 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2403 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2407 * Load the next byte, at an offset of 2, and OR the
2408 * value from the X register into it.
2410 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2413 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2417 * Now allocate a register to hold that value and store
2420 s2
= new_stmt(BPF_ST
);
2421 s2
->s
.k
= off_linkhdr
.reg
;
2425 * Now move it into the X register.
2427 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2436 * Load a value relative to the beginning of the link-layer header after the 802.11
2437 * header, i.e. LLC_SNAP.
2438 * The link-layer header doesn't necessarily begin at the beginning
2439 * of the packet data; there might be a variable-length prefix containing
2440 * radio information.
2442 static struct slist
*
2443 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2446 struct slist
*sjset_data_frame_1
;
2447 struct slist
*sjset_data_frame_2
;
2448 struct slist
*sjset_qos
;
2449 struct slist
*sjset_radiotap_flags
;
2450 struct slist
*sjset_radiotap_tsft
;
2451 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2452 struct slist
*s_roundup
;
2454 if (off_linkpl
.reg
== -1) {
2456 * No register has been assigned to the offset of
2457 * the link-layer payload, which means nobody needs
2458 * it; don't bother computing it - just return
2459 * what we already have.
2465 * This code is not compatible with the optimizer, as
2466 * we are generating jmp instructions within a normal
2467 * slist of instructions
2472 * If "s" is non-null, it has code to arrange that the X register
2473 * contains the length of the prefix preceding the link-layer
2476 * Otherwise, the length of the prefix preceding the link-layer
2477 * header is "off_outermostlinkhdr.constant_part".
2481 * There is no variable-length header preceding the
2482 * link-layer header.
2484 * Load the length of the fixed-length prefix preceding
2485 * the link-layer header (if any) into the X register,
2486 * and store it in the off_linkpl.reg register.
2487 * That length is off_outermostlinkhdr.constant_part.
2489 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2490 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2494 * The X register contains the offset of the beginning of the
2495 * link-layer header; add 24, which is the minimum length
2496 * of the MAC header for a data frame, to that, and store it
2497 * in off_linkpl.reg, and then load the Frame Control field,
2498 * which is at the offset in the X register, with an indexed load.
2500 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2502 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2505 s2
= new_stmt(BPF_ST
);
2506 s2
->s
.k
= off_linkpl
.reg
;
2509 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2514 * Check the Frame Control field to see if this is a data frame;
2515 * a data frame has the 0x08 bit (b3) in that field set and the
2516 * 0x04 bit (b2) clear.
2518 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2519 sjset_data_frame_1
->s
.k
= 0x08;
2520 sappend(s
, sjset_data_frame_1
);
2523 * If b3 is set, test b2, otherwise go to the first statement of
2524 * the rest of the program.
2526 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2527 sjset_data_frame_2
->s
.k
= 0x04;
2528 sappend(s
, sjset_data_frame_2
);
2529 sjset_data_frame_1
->s
.jf
= snext
;
2532 * If b2 is not set, this is a data frame; test the QoS bit.
2533 * Otherwise, go to the first statement of the rest of the
2536 sjset_data_frame_2
->s
.jt
= snext
;
2537 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2538 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2539 sappend(s
, sjset_qos
);
2542 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2544 * Otherwise, go to the first statement of the rest of the
2547 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2548 s2
->s
.k
= off_linkpl
.reg
;
2550 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2553 s2
= new_stmt(BPF_ST
);
2554 s2
->s
.k
= off_linkpl
.reg
;
2558 * If we have a radiotap header, look at it to see whether
2559 * there's Atheros padding between the MAC-layer header
2562 * Note: all of the fields in the radiotap header are
2563 * little-endian, so we byte-swap all of the values
2564 * we test against, as they will be loaded as big-endian
2567 if (linktype
== DLT_IEEE802_11_RADIO
) {
2569 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2570 * in the presence flag?
2572 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2576 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2577 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2578 sappend(s
, sjset_radiotap_flags
);
2581 * If not, skip all of this.
2583 sjset_radiotap_flags
->s
.jf
= snext
;
2586 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2588 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2589 new_stmt(JMP(BPF_JSET
));
2590 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2591 sappend(s
, sjset_radiotap_tsft
);
2594 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2595 * at an offset of 16 from the beginning of the raw packet
2596 * data (8 bytes for the radiotap header and 8 bytes for
2599 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2602 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2606 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2607 sjset_tsft_datapad
->s
.k
= 0x20;
2608 sappend(s
, sjset_tsft_datapad
);
2611 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2612 * at an offset of 8 from the beginning of the raw packet
2613 * data (8 bytes for the radiotap header).
2615 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2618 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2622 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2623 sjset_notsft_datapad
->s
.k
= 0x20;
2624 sappend(s
, sjset_notsft_datapad
);
2627 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2628 * set, round the length of the 802.11 header to
2629 * a multiple of 4. Do that by adding 3 and then
2630 * dividing by and multiplying by 4, which we do by
2633 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2634 s_roundup
->s
.k
= off_linkpl
.reg
;
2635 sappend(s
, s_roundup
);
2636 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2639 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2642 s2
= new_stmt(BPF_ST
);
2643 s2
->s
.k
= off_linkpl
.reg
;
2646 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2647 sjset_tsft_datapad
->s
.jf
= snext
;
2648 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2649 sjset_notsft_datapad
->s
.jf
= snext
;
2651 sjset_qos
->s
.jf
= snext
;
2657 insert_compute_vloffsets(b
)
2662 /* There is an implicit dependency between the link
2663 * payload and link header since the payload computation
2664 * includes the variable part of the header. Therefore,
2665 * if nobody else has allocated a register for the link
2666 * header and we need it, do it now. */
2667 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2668 off_linkhdr
.reg
== -1)
2669 off_linkhdr
.reg
= alloc_reg();
2672 * For link-layer types that have a variable-length header
2673 * preceding the link-layer header, generate code to load
2674 * the offset of the link-layer header into the register
2675 * assigned to that offset, if any.
2677 * XXX - this, and the next switch statement, won't handle
2678 * encapsulation of 802.11 or 802.11+radio information in
2679 * some other protocol stack. That's significantly more
2682 switch (outermostlinktype
) {
2684 case DLT_PRISM_HEADER
:
2685 s
= gen_load_prism_llprefixlen();
2688 case DLT_IEEE802_11_RADIO_AVS
:
2689 s
= gen_load_avs_llprefixlen();
2692 case DLT_IEEE802_11_RADIO
:
2693 s
= gen_load_radiotap_llprefixlen();
2697 s
= gen_load_ppi_llprefixlen();
2706 * For link-layer types that have a variable-length link-layer
2707 * header, generate code to load the offset of the link-layer
2708 * payload into the register assigned to that offset, if any.
2710 switch (outermostlinktype
) {
2712 case DLT_IEEE802_11
:
2713 case DLT_PRISM_HEADER
:
2714 case DLT_IEEE802_11_RADIO_AVS
:
2715 case DLT_IEEE802_11_RADIO
:
2717 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2722 * If we have any offset-loading code, append all the
2723 * existing statements in the block to those statements,
2724 * and make the resulting list the list of statements
2728 sappend(s
, b
->stmts
);
2733 static struct block
*
2734 gen_ppi_dlt_check(void)
2736 struct slist
*s_load_dlt
;
2739 if (linktype
== DLT_PPI
)
2741 /* Create the statements that check for the DLT
2743 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2744 s_load_dlt
->s
.k
= 4;
2746 b
= new_block(JMP(BPF_JEQ
));
2748 b
->stmts
= s_load_dlt
;
2749 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2760 * Take an absolute offset, and:
2762 * if it has no variable part, return NULL;
2764 * if it has a variable part, generate code to load the register
2765 * containing that variable part into the X register, returning
2766 * a pointer to that code - if no register for that offset has
2767 * been allocated, allocate it first.
2769 * (The code to set that register will be generated later, but will
2770 * be placed earlier in the code sequence.)
2772 static struct slist
*
2773 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2777 if (off
->is_variable
) {
2778 if (off
->reg
== -1) {
2780 * We haven't yet assigned a register for the
2781 * variable part of the offset of the link-layer
2782 * header; allocate one.
2784 off
->reg
= alloc_reg();
2788 * Load the register containing the variable part of the
2789 * offset of the link-layer header into the X register.
2791 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2796 * That offset isn't variable, there's no variable part,
2797 * so we don't need to generate any code.
2804 * Map an Ethernet type to the equivalent PPP type.
2807 ethertype_to_ppptype(proto
)
2816 case ETHERTYPE_IPV6
:
2824 case ETHERTYPE_ATALK
:
2838 * I'm assuming the "Bridging PDU"s that go
2839 * over PPP are Spanning Tree Protocol
2853 * Generate any tests that, for encapsulation of a link-layer packet
2854 * inside another protocol stack, need to be done to check for those
2855 * link-layer packets (and that haven't already been done by a check
2856 * for that encapsulation).
2858 static struct block
*
2859 gen_prevlinkhdr_check(void)
2864 return gen_geneve_ll_check();
2866 switch (prevlinktype
) {
2870 * This is LANE-encapsulated Ethernet; check that the LANE
2871 * packet doesn't begin with an LE Control marker, i.e.
2872 * that it's data, not a control message.
2874 * (We've already generated a test for LANE.)
2876 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2882 * No such tests are necessary.
2890 * Generate code to match a particular packet type by matching the
2891 * link-layer type field or fields in the 802.2 LLC header.
2893 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2894 * value, if <= ETHERMTU.
2896 static struct block
*
2900 struct block
*b0
, *b1
, *b2
;
2901 const char *description
;
2903 /* are we checking MPLS-encapsulated packets? */
2904 if (label_stack_depth
> 0) {
2908 /* FIXME add other L3 proto IDs */
2909 return gen_mpls_linktype(Q_IP
);
2911 case ETHERTYPE_IPV6
:
2913 /* FIXME add other L3 proto IDs */
2914 return gen_mpls_linktype(Q_IPV6
);
2917 bpf_error("unsupported protocol over mpls");
2925 case DLT_NETANALYZER
:
2926 case DLT_NETANALYZER_TRANSPARENT
:
2927 /* Geneve has an EtherType regardless of whether there is an
2930 b0
= gen_prevlinkhdr_check();
2934 b1
= gen_ether_linktype(proto
);
2945 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2949 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2955 case DLT_IEEE802_11
:
2956 case DLT_PRISM_HEADER
:
2957 case DLT_IEEE802_11_RADIO_AVS
:
2958 case DLT_IEEE802_11_RADIO
:
2961 * Check that we have a data frame.
2963 b0
= gen_check_802_11_data_frame();
2966 * Now check for the specified link-layer type.
2968 b1
= gen_llc_linktype(proto
);
2976 * XXX - check for LLC frames.
2978 return gen_llc_linktype(proto
);
2984 * XXX - check for LLC PDUs, as per IEEE 802.5.
2986 return gen_llc_linktype(proto
);
2990 case DLT_ATM_RFC1483
:
2992 case DLT_IP_OVER_FC
:
2993 return gen_llc_linktype(proto
);
2999 * Check for an LLC-encapsulated version of this protocol;
3000 * if we were checking for LANE, linktype would no longer
3003 * Check for LLC encapsulation and then check the protocol.
3005 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
3006 b1
= gen_llc_linktype(proto
);
3013 return gen_linux_sll_linktype(proto
);
3018 case DLT_SLIP_BSDOS
:
3021 * These types don't provide any type field; packets
3022 * are always IPv4 or IPv6.
3024 * XXX - for IPv4, check for a version number of 4, and,
3025 * for IPv6, check for a version number of 6?
3030 /* Check for a version number of 4. */
3031 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3033 case ETHERTYPE_IPV6
:
3034 /* Check for a version number of 6. */
3035 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3038 return gen_false(); /* always false */
3045 * Raw IPv4, so no type field.
3047 if (proto
== ETHERTYPE_IP
)
3048 return gen_true(); /* always true */
3050 /* Checking for something other than IPv4; always false */
3057 * Raw IPv6, so no type field.
3059 if (proto
== ETHERTYPE_IPV6
)
3060 return gen_true(); /* always true */
3062 /* Checking for something other than IPv6; always false */
3069 case DLT_PPP_SERIAL
:
3072 * We use Ethernet protocol types inside libpcap;
3073 * map them to the corresponding PPP protocol types.
3075 proto
= ethertype_to_ppptype(proto
);
3076 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3082 * We use Ethernet protocol types inside libpcap;
3083 * map them to the corresponding PPP protocol types.
3089 * Also check for Van Jacobson-compressed IP.
3090 * XXX - do this for other forms of PPP?
3092 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3093 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3095 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3100 proto
= ethertype_to_ppptype(proto
);
3101 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3111 * For DLT_NULL, the link-layer header is a 32-bit
3112 * word containing an AF_ value in *host* byte order,
3113 * and for DLT_ENC, the link-layer header begins
3114 * with a 32-bit work containing an AF_ value in
3117 * In addition, if we're reading a saved capture file,
3118 * the host byte order in the capture may not be the
3119 * same as the host byte order on this machine.
3121 * For DLT_LOOP, the link-layer header is a 32-bit
3122 * word containing an AF_ value in *network* byte order.
3124 * XXX - AF_ values may, unfortunately, be platform-
3125 * dependent; for example, FreeBSD's AF_INET6 is 24
3126 * whilst NetBSD's and OpenBSD's is 26.
3128 * This means that, when reading a capture file, just
3129 * checking for our AF_INET6 value won't work if the
3130 * capture file came from another OS.
3139 case ETHERTYPE_IPV6
:
3146 * Not a type on which we support filtering.
3147 * XXX - support those that have AF_ values
3148 * #defined on this platform, at least?
3153 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3155 * The AF_ value is in host byte order, but
3156 * the BPF interpreter will convert it to
3157 * network byte order.
3159 * If this is a save file, and it's from a
3160 * machine with the opposite byte order to
3161 * ours, we byte-swap the AF_ value.
3163 * Then we run it through "htonl()", and
3164 * generate code to compare against the result.
3166 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3167 proto
= SWAPLONG(proto
);
3168 proto
= htonl(proto
);
3170 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3172 #ifdef HAVE_NET_PFVAR_H
3175 * af field is host byte order in contrast to the rest of
3178 if (proto
== ETHERTYPE_IP
)
3179 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3180 BPF_B
, (bpf_int32
)AF_INET
));
3181 else if (proto
== ETHERTYPE_IPV6
)
3182 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3183 BPF_B
, (bpf_int32
)AF_INET6
));
3188 #endif /* HAVE_NET_PFVAR_H */
3191 case DLT_ARCNET_LINUX
:
3193 * XXX should we check for first fragment if the protocol
3201 case ETHERTYPE_IPV6
:
3202 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3203 (bpf_int32
)ARCTYPE_INET6
));
3206 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3207 (bpf_int32
)ARCTYPE_IP
);
3208 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3209 (bpf_int32
)ARCTYPE_IP_OLD
);
3214 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3215 (bpf_int32
)ARCTYPE_ARP
);
3216 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3217 (bpf_int32
)ARCTYPE_ARP_OLD
);
3221 case ETHERTYPE_REVARP
:
3222 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3223 (bpf_int32
)ARCTYPE_REVARP
));
3225 case ETHERTYPE_ATALK
:
3226 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3227 (bpf_int32
)ARCTYPE_ATALK
));
3234 case ETHERTYPE_ATALK
:
3244 * XXX - assumes a 2-byte Frame Relay header with
3245 * DLCI and flags. What if the address is longer?
3251 * Check for the special NLPID for IP.
3253 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3255 case ETHERTYPE_IPV6
:
3257 * Check for the special NLPID for IPv6.
3259 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3263 * Check for several OSI protocols.
3265 * Frame Relay packets typically have an OSI
3266 * NLPID at the beginning; we check for each
3269 * What we check for is the NLPID and a frame
3270 * control field of UI, i.e. 0x03 followed
3273 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3274 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3275 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3287 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3289 case DLT_JUNIPER_MFR
:
3290 case DLT_JUNIPER_MLFR
:
3291 case DLT_JUNIPER_MLPPP
:
3292 case DLT_JUNIPER_ATM1
:
3293 case DLT_JUNIPER_ATM2
:
3294 case DLT_JUNIPER_PPPOE
:
3295 case DLT_JUNIPER_PPPOE_ATM
:
3296 case DLT_JUNIPER_GGSN
:
3297 case DLT_JUNIPER_ES
:
3298 case DLT_JUNIPER_MONITOR
:
3299 case DLT_JUNIPER_SERVICES
:
3300 case DLT_JUNIPER_ETHER
:
3301 case DLT_JUNIPER_PPP
:
3302 case DLT_JUNIPER_FRELAY
:
3303 case DLT_JUNIPER_CHDLC
:
3304 case DLT_JUNIPER_VP
:
3305 case DLT_JUNIPER_ST
:
3306 case DLT_JUNIPER_ISM
:
3307 case DLT_JUNIPER_VS
:
3308 case DLT_JUNIPER_SRX_E2E
:
3309 case DLT_JUNIPER_FIBRECHANNEL
:
3310 case DLT_JUNIPER_ATM_CEMIC
:
3312 /* just lets verify the magic number for now -
3313 * on ATM we may have up to 6 different encapsulations on the wire
3314 * and need a lot of heuristics to figure out that the payload
3317 * FIXME encapsulation specific BPF_ filters
3319 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3321 case DLT_BACNET_MS_TP
:
3322 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3325 return gen_ipnet_linktype(proto
);
3327 case DLT_LINUX_IRDA
:
3328 bpf_error("IrDA link-layer type filtering not implemented");
3331 bpf_error("DOCSIS link-layer type filtering not implemented");
3334 case DLT_MTP2_WITH_PHDR
:
3335 bpf_error("MTP2 link-layer type filtering not implemented");
3338 bpf_error("ERF link-layer type filtering not implemented");
3341 bpf_error("PFSYNC link-layer type filtering not implemented");
3343 case DLT_LINUX_LAPD
:
3344 bpf_error("LAPD link-layer type filtering not implemented");
3348 case DLT_USB_LINUX_MMAPPED
:
3349 bpf_error("USB link-layer type filtering not implemented");
3351 case DLT_BLUETOOTH_HCI_H4
:
3352 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3353 bpf_error("Bluetooth link-layer type filtering not implemented");
3356 case DLT_CAN_SOCKETCAN
:
3357 bpf_error("CAN link-layer type filtering not implemented");
3359 case DLT_IEEE802_15_4
:
3360 case DLT_IEEE802_15_4_LINUX
:
3361 case DLT_IEEE802_15_4_NONASK_PHY
:
3362 case DLT_IEEE802_15_4_NOFCS
:
3363 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3365 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3366 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3369 bpf_error("SITA link-layer type filtering not implemented");
3372 bpf_error("RAIF1 link-layer type filtering not implemented");
3375 bpf_error("IPMB link-layer type filtering not implemented");
3378 bpf_error("AX.25 link-layer type filtering not implemented");
3381 /* Using the fixed-size NFLOG header it is possible to tell only
3382 * the address family of the packet, other meaningful data is
3383 * either missing or behind TLVs.
3385 bpf_error("NFLOG link-layer type filtering not implemented");
3389 * Does this link-layer header type have a field
3390 * indicating the type of the next protocol? If
3391 * so, off_linktype.constant_part will be the offset of that
3392 * field in the packet; if not, it will be -1.
3394 if (off_linktype
.constant_part
!= (u_int
)-1) {
3396 * Yes; assume it's an Ethernet type. (If
3397 * it's not, it needs to be handled specially
3400 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3403 * No; report an error.
3405 description
= pcap_datalink_val_to_description(linktype
);
3406 if (description
!= NULL
) {
3407 bpf_error("%s link-layer type filtering not implemented",
3410 bpf_error("DLT %u link-layer type filtering not implemented",
3419 * Check for an LLC SNAP packet with a given organization code and
3420 * protocol type; we check the entire contents of the 802.2 LLC and
3421 * snap headers, checking for DSAP and SSAP of SNAP and a control
3422 * field of 0x03 in the LLC header, and for the specified organization
3423 * code and protocol type in the SNAP header.
3425 static struct block
*
3426 gen_snap(orgcode
, ptype
)
3427 bpf_u_int32 orgcode
;
3430 u_char snapblock
[8];
3432 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3433 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3434 snapblock
[2] = 0x03; /* control = UI */
3435 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3436 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3437 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3438 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3439 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3440 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3444 * Generate code to match frames with an LLC header.
3449 struct block
*b0
, *b1
;
3455 * We check for an Ethernet type field less than
3456 * 1500, which means it's an 802.3 length field.
3458 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3462 * Now check for the purported DSAP and SSAP not being
3463 * 0xFF, to rule out NetWare-over-802.3.
3465 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3472 * We check for LLC traffic.
3474 b0
= gen_atmtype_abbrev(A_LLC
);
3477 case DLT_IEEE802
: /* Token Ring */
3479 * XXX - check for LLC frames.
3485 * XXX - check for LLC frames.
3489 case DLT_ATM_RFC1483
:
3491 * For LLC encapsulation, these are defined to have an
3494 * For VC encapsulation, they don't, but there's no
3495 * way to check for that; the protocol used on the VC
3496 * is negotiated out of band.
3500 case DLT_IEEE802_11
:
3501 case DLT_PRISM_HEADER
:
3502 case DLT_IEEE802_11_RADIO
:
3503 case DLT_IEEE802_11_RADIO_AVS
:
3506 * Check that we have a data frame.
3508 b0
= gen_check_802_11_data_frame();
3512 bpf_error("'llc' not supported for linktype %d", linktype
);
3520 struct block
*b0
, *b1
;
3524 * Check whether this is an LLC frame.
3529 * Load the control byte and test the low-order bit; it must
3530 * be clear for I frames.
3532 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3533 b1
= new_block(JMP(BPF_JSET
));
3544 struct block
*b0
, *b1
;
3547 * Check whether this is an LLC frame.
3552 * Now compare the low-order 2 bit of the control byte against
3553 * the appropriate value for S frames.
3555 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3563 struct block
*b0
, *b1
;
3566 * Check whether this is an LLC frame.
3571 * Now compare the low-order 2 bit of the control byte against
3572 * the appropriate value for U frames.
3574 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3580 gen_llc_s_subtype(bpf_u_int32 subtype
)
3582 struct block
*b0
, *b1
;
3585 * Check whether this is an LLC frame.
3590 * Now check for an S frame with the appropriate type.
3592 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3598 gen_llc_u_subtype(bpf_u_int32 subtype
)
3600 struct block
*b0
, *b1
;
3603 * Check whether this is an LLC frame.
3608 * Now check for a U frame with the appropriate type.
3610 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3616 * Generate code to match a particular packet type, for link-layer types
3617 * using 802.2 LLC headers.
3619 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3620 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3622 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3623 * value, if <= ETHERMTU. We use that to determine whether to
3624 * match the DSAP or both DSAP and LSAP or to check the OUI and
3625 * protocol ID in a SNAP header.
3627 static struct block
*
3628 gen_llc_linktype(proto
)
3632 * XXX - handle token-ring variable-length header.
3638 case LLCSAP_NETBEUI
:
3640 * XXX - should we check both the DSAP and the
3641 * SSAP, like this, or should we check just the
3642 * DSAP, as we do for other SAP values?
3644 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3645 ((proto
<< 8) | proto
));
3649 * XXX - are there ever SNAP frames for IPX on
3650 * non-Ethernet 802.x networks?
3652 return gen_cmp(OR_LLC
, 0, BPF_B
,
3653 (bpf_int32
)LLCSAP_IPX
);
3655 case ETHERTYPE_ATALK
:
3657 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3658 * SNAP packets with an organization code of
3659 * 0x080007 (Apple, for Appletalk) and a protocol
3660 * type of ETHERTYPE_ATALK (Appletalk).
3662 * XXX - check for an organization code of
3663 * encapsulated Ethernet as well?
3665 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3669 * XXX - we don't have to check for IPX 802.3
3670 * here, but should we check for the IPX Ethertype?
3672 if (proto
<= ETHERMTU
) {
3674 * This is an LLC SAP value, so check
3677 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3680 * This is an Ethernet type; we assume that it's
3681 * unlikely that it'll appear in the right place
3682 * at random, and therefore check only the
3683 * location that would hold the Ethernet type
3684 * in a SNAP frame with an organization code of
3685 * 0x000000 (encapsulated Ethernet).
3687 * XXX - if we were to check for the SNAP DSAP and
3688 * LSAP, as per XXX, and were also to check for an
3689 * organization code of 0x000000 (encapsulated
3690 * Ethernet), we'd do
3692 * return gen_snap(0x000000, proto);
3694 * here; for now, we don't, as per the above.
3695 * I don't know whether it's worth the extra CPU
3696 * time to do the right check or not.
3698 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3703 static struct block
*
3704 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3708 u_int src_off
, dst_off
;
3710 struct block
*b0
, *b1
;
3724 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3725 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3731 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3732 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3739 b0
= gen_linktype(proto
);
3740 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3746 static struct block
*
3747 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3748 struct in6_addr
*addr
;
3749 struct in6_addr
*mask
;
3751 u_int src_off
, dst_off
;
3753 struct block
*b0
, *b1
;
3768 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3769 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3775 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3776 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3783 /* this order is important */
3784 a
= (u_int32_t
*)addr
;
3785 m
= (u_int32_t
*)mask
;
3786 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3787 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3789 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3791 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3793 b0
= gen_linktype(proto
);
3799 static struct block
*
3800 gen_ehostop(eaddr
, dir
)
3801 register const u_char
*eaddr
;
3804 register struct block
*b0
, *b1
;
3808 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3811 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3814 b0
= gen_ehostop(eaddr
, Q_SRC
);
3815 b1
= gen_ehostop(eaddr
, Q_DST
);
3821 b0
= gen_ehostop(eaddr
, Q_SRC
);
3822 b1
= gen_ehostop(eaddr
, Q_DST
);
3827 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3831 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3835 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3839 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3843 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3847 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3855 * Like gen_ehostop, but for DLT_FDDI
3857 static struct block
*
3858 gen_fhostop(eaddr
, dir
)
3859 register const u_char
*eaddr
;
3862 struct block
*b0
, *b1
;
3866 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3869 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3872 b0
= gen_fhostop(eaddr
, Q_SRC
);
3873 b1
= gen_fhostop(eaddr
, Q_DST
);
3879 b0
= gen_fhostop(eaddr
, Q_SRC
);
3880 b1
= gen_fhostop(eaddr
, Q_DST
);
3885 bpf_error("'addr1' is only supported on 802.11");
3889 bpf_error("'addr2' is only supported on 802.11");
3893 bpf_error("'addr3' is only supported on 802.11");
3897 bpf_error("'addr4' is only supported on 802.11");
3901 bpf_error("'ra' is only supported on 802.11");
3905 bpf_error("'ta' is only supported on 802.11");
3913 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3915 static struct block
*
3916 gen_thostop(eaddr
, dir
)
3917 register const u_char
*eaddr
;
3920 register struct block
*b0
, *b1
;
3924 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3927 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3930 b0
= gen_thostop(eaddr
, Q_SRC
);
3931 b1
= gen_thostop(eaddr
, Q_DST
);
3937 b0
= gen_thostop(eaddr
, Q_SRC
);
3938 b1
= gen_thostop(eaddr
, Q_DST
);
3943 bpf_error("'addr1' is only supported on 802.11");
3947 bpf_error("'addr2' is only supported on 802.11");
3951 bpf_error("'addr3' is only supported on 802.11");
3955 bpf_error("'addr4' is only supported on 802.11");
3959 bpf_error("'ra' is only supported on 802.11");
3963 bpf_error("'ta' is only supported on 802.11");
3971 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3972 * various 802.11 + radio headers.
3974 static struct block
*
3975 gen_wlanhostop(eaddr
, dir
)
3976 register const u_char
*eaddr
;
3979 register struct block
*b0
, *b1
, *b2
;
3980 register struct slist
*s
;
3982 #ifdef ENABLE_WLAN_FILTERING_PATCH
3985 * We need to disable the optimizer because the optimizer is buggy
3986 * and wipes out some LD instructions generated by the below
3987 * code to validate the Frame Control bits
3990 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3997 * For control frames, there is no SA.
3999 * For management frames, SA is at an
4000 * offset of 10 from the beginning of
4003 * For data frames, SA is at an offset
4004 * of 10 from the beginning of the packet
4005 * if From DS is clear, at an offset of
4006 * 16 from the beginning of the packet
4007 * if From DS is set and To DS is clear,
4008 * and an offset of 24 from the beginning
4009 * of the packet if From DS is set and To DS
4014 * Generate the tests to be done for data frames
4017 * First, check for To DS set, i.e. check "link[1] & 0x01".
4019 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4020 b1
= new_block(JMP(BPF_JSET
));
4021 b1
->s
.k
= 0x01; /* To DS */
4025 * If To DS is set, the SA is at 24.
4027 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4031 * Now, check for To DS not set, i.e. check
4032 * "!(link[1] & 0x01)".
4034 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4035 b2
= new_block(JMP(BPF_JSET
));
4036 b2
->s
.k
= 0x01; /* To DS */
4041 * If To DS is not set, the SA is at 16.
4043 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4047 * Now OR together the last two checks. That gives
4048 * the complete set of checks for data frames with
4054 * Now check for From DS being set, and AND that with
4055 * the ORed-together checks.
4057 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4058 b1
= new_block(JMP(BPF_JSET
));
4059 b1
->s
.k
= 0x02; /* From DS */
4064 * Now check for data frames with From DS not set.
4066 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4067 b2
= new_block(JMP(BPF_JSET
));
4068 b2
->s
.k
= 0x02; /* From DS */
4073 * If From DS isn't set, the SA is at 10.
4075 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4079 * Now OR together the checks for data frames with
4080 * From DS not set and for data frames with From DS
4081 * set; that gives the checks done for data frames.
4086 * Now check for a data frame.
4087 * I.e, check "link[0] & 0x08".
4089 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4090 b1
= new_block(JMP(BPF_JSET
));
4095 * AND that with the checks done for data frames.
4100 * If the high-order bit of the type value is 0, this
4101 * is a management frame.
4102 * I.e, check "!(link[0] & 0x08)".
4104 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4105 b2
= new_block(JMP(BPF_JSET
));
4111 * For management frames, the SA is at 10.
4113 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4117 * OR that with the checks done for data frames.
4118 * That gives the checks done for management and
4124 * If the low-order bit of the type value is 1,
4125 * this is either a control frame or a frame
4126 * with a reserved type, and thus not a
4129 * I.e., check "!(link[0] & 0x04)".
4131 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4132 b1
= new_block(JMP(BPF_JSET
));
4138 * AND that with the checks for data and management
4148 * For control frames, there is no DA.
4150 * For management frames, DA is at an
4151 * offset of 4 from the beginning of
4154 * For data frames, DA is at an offset
4155 * of 4 from the beginning of the packet
4156 * if To DS is clear and at an offset of
4157 * 16 from the beginning of the packet
4162 * Generate the tests to be done for data frames.
4164 * First, check for To DS set, i.e. "link[1] & 0x01".
4166 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4167 b1
= new_block(JMP(BPF_JSET
));
4168 b1
->s
.k
= 0x01; /* To DS */
4172 * If To DS is set, the DA is at 16.
4174 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4178 * Now, check for To DS not set, i.e. check
4179 * "!(link[1] & 0x01)".
4181 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4182 b2
= new_block(JMP(BPF_JSET
));
4183 b2
->s
.k
= 0x01; /* To DS */
4188 * If To DS is not set, the DA is at 4.
4190 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4194 * Now OR together the last two checks. That gives
4195 * the complete set of checks for data frames.
4200 * Now check for a data frame.
4201 * I.e, check "link[0] & 0x08".
4203 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4204 b1
= new_block(JMP(BPF_JSET
));
4209 * AND that with the checks done for data frames.
4214 * If the high-order bit of the type value is 0, this
4215 * is a management frame.
4216 * I.e, check "!(link[0] & 0x08)".
4218 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4219 b2
= new_block(JMP(BPF_JSET
));
4225 * For management frames, the DA is at 4.
4227 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4231 * OR that with the checks done for data frames.
4232 * That gives the checks done for management and
4238 * If the low-order bit of the type value is 1,
4239 * this is either a control frame or a frame
4240 * with a reserved type, and thus not a
4243 * I.e., check "!(link[0] & 0x04)".
4245 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4246 b1
= new_block(JMP(BPF_JSET
));
4252 * AND that with the checks for data and management
4260 * Not present in management frames; addr1 in other
4265 * If the high-order bit of the type value is 0, this
4266 * is a management frame.
4267 * I.e, check "(link[0] & 0x08)".
4269 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4270 b1
= new_block(JMP(BPF_JSET
));
4277 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4280 * AND that with the check of addr1.
4287 * Not present in management frames; addr2, if present,
4292 * Not present in CTS or ACK control frames.
4294 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4295 IEEE80211_FC0_TYPE_MASK
);
4297 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4298 IEEE80211_FC0_SUBTYPE_MASK
);
4300 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4301 IEEE80211_FC0_SUBTYPE_MASK
);
4307 * If the high-order bit of the type value is 0, this
4308 * is a management frame.
4309 * I.e, check "(link[0] & 0x08)".
4311 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4312 b1
= new_block(JMP(BPF_JSET
));
4317 * AND that with the check for frames other than
4318 * CTS and ACK frames.
4325 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4330 * XXX - add BSSID keyword?
4333 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4337 * Not present in CTS or ACK control frames.
4339 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4340 IEEE80211_FC0_TYPE_MASK
);
4342 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4343 IEEE80211_FC0_SUBTYPE_MASK
);
4345 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4346 IEEE80211_FC0_SUBTYPE_MASK
);
4350 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4356 * Not present in control frames.
4358 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4359 IEEE80211_FC0_TYPE_MASK
);
4361 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4367 * Present only if the direction mask has both "From DS"
4368 * and "To DS" set. Neither control frames nor management
4369 * frames should have both of those set, so we don't
4370 * check the frame type.
4372 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4373 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4374 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4379 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4380 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4386 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4387 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4396 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4397 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4398 * as the RFC states.)
4400 static struct block
*
4401 gen_ipfchostop(eaddr
, dir
)
4402 register const u_char
*eaddr
;
4405 register struct block
*b0
, *b1
;
4409 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4412 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4415 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4416 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4422 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4423 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4428 bpf_error("'addr1' is only supported on 802.11");
4432 bpf_error("'addr2' is only supported on 802.11");
4436 bpf_error("'addr3' is only supported on 802.11");
4440 bpf_error("'addr4' is only supported on 802.11");
4444 bpf_error("'ra' is only supported on 802.11");
4448 bpf_error("'ta' is only supported on 802.11");
4456 * This is quite tricky because there may be pad bytes in front of the
4457 * DECNET header, and then there are two possible data packet formats that
4458 * carry both src and dst addresses, plus 5 packet types in a format that
4459 * carries only the src node, plus 2 types that use a different format and
4460 * also carry just the src node.
4464 * Instead of doing those all right, we just look for data packets with
4465 * 0 or 1 bytes of padding. If you want to look at other packets, that
4466 * will require a lot more hacking.
4468 * To add support for filtering on DECNET "areas" (network numbers)
4469 * one would want to add a "mask" argument to this routine. That would
4470 * make the filter even more inefficient, although one could be clever
4471 * and not generate masking instructions if the mask is 0xFFFF.
4473 static struct block
*
4474 gen_dnhostop(addr
, dir
)
4478 struct block
*b0
, *b1
, *b2
, *tmp
;
4479 u_int offset_lh
; /* offset if long header is received */
4480 u_int offset_sh
; /* offset if short header is received */
4485 offset_sh
= 1; /* follows flags */
4486 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4490 offset_sh
= 3; /* follows flags, dstnode */
4491 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4495 /* Inefficient because we do our Calvinball dance twice */
4496 b0
= gen_dnhostop(addr
, Q_SRC
);
4497 b1
= gen_dnhostop(addr
, Q_DST
);
4503 /* Inefficient because we do our Calvinball dance twice */
4504 b0
= gen_dnhostop(addr
, Q_SRC
);
4505 b1
= gen_dnhostop(addr
, Q_DST
);
4510 bpf_error("ISO host filtering not implemented");
4515 b0
= gen_linktype(ETHERTYPE_DN
);
4516 /* Check for pad = 1, long header case */
4517 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4518 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4519 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4520 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4522 /* Check for pad = 0, long header case */
4523 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4524 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4527 /* Check for pad = 1, short header case */
4528 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4529 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4530 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4533 /* Check for pad = 0, short header case */
4534 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4535 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4539 /* Combine with test for linktype */
4545 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4546 * test the bottom-of-stack bit, and then check the version number
4547 * field in the IP header.
4549 static struct block
*
4550 gen_mpls_linktype(proto
)
4553 struct block
*b0
, *b1
;
4558 /* match the bottom-of-stack bit */
4559 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4560 /* match the IPv4 version number */
4561 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4566 /* match the bottom-of-stack bit */
4567 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4568 /* match the IPv4 version number */
4569 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4578 static struct block
*
4579 gen_host(addr
, mask
, proto
, dir
, type
)
4586 struct block
*b0
, *b1
;
4587 const char *typestr
;
4597 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4599 * Only check for non-IPv4 addresses if we're not
4600 * checking MPLS-encapsulated packets.
4602 if (label_stack_depth
== 0) {
4603 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4605 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4611 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4614 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4617 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4620 bpf_error("'tcp' modifier applied to %s", typestr
);
4623 bpf_error("'sctp' modifier applied to %s", typestr
);
4626 bpf_error("'udp' modifier applied to %s", typestr
);
4629 bpf_error("'icmp' modifier applied to %s", typestr
);
4632 bpf_error("'igmp' modifier applied to %s", typestr
);
4635 bpf_error("'igrp' modifier applied to %s", typestr
);
4638 bpf_error("'pim' modifier applied to %s", typestr
);
4641 bpf_error("'vrrp' modifier applied to %s", typestr
);
4644 bpf_error("'carp' modifier applied to %s", typestr
);
4647 bpf_error("ATALK host filtering not implemented");
4650 bpf_error("AARP host filtering not implemented");
4653 return gen_dnhostop(addr
, dir
);
4656 bpf_error("SCA host filtering not implemented");
4659 bpf_error("LAT host filtering not implemented");
4662 bpf_error("MOPDL host filtering not implemented");
4665 bpf_error("MOPRC host filtering not implemented");
4668 bpf_error("'ip6' modifier applied to ip host");
4671 bpf_error("'icmp6' modifier applied to %s", typestr
);
4674 bpf_error("'ah' modifier applied to %s", typestr
);
4677 bpf_error("'esp' modifier applied to %s", typestr
);
4680 bpf_error("ISO host filtering not implemented");
4683 bpf_error("'esis' modifier applied to %s", typestr
);
4686 bpf_error("'isis' modifier applied to %s", typestr
);
4689 bpf_error("'clnp' modifier applied to %s", typestr
);
4692 bpf_error("'stp' modifier applied to %s", typestr
);
4695 bpf_error("IPX host filtering not implemented");
4698 bpf_error("'netbeui' modifier applied to %s", typestr
);
4701 bpf_error("'radio' modifier applied to %s", typestr
);
4710 static struct block
*
4711 gen_host6(addr
, mask
, proto
, dir
, type
)
4712 struct in6_addr
*addr
;
4713 struct in6_addr
*mask
;
4718 const char *typestr
;
4728 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4731 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4734 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4737 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4740 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4743 bpf_error("'sctp' modifier applied to %s", typestr
);
4746 bpf_error("'tcp' modifier applied to %s", typestr
);
4749 bpf_error("'udp' modifier applied to %s", typestr
);
4752 bpf_error("'icmp' modifier applied to %s", typestr
);
4755 bpf_error("'igmp' modifier applied to %s", typestr
);
4758 bpf_error("'igrp' modifier applied to %s", typestr
);
4761 bpf_error("'pim' modifier applied to %s", typestr
);
4764 bpf_error("'vrrp' modifier applied to %s", typestr
);
4767 bpf_error("'carp' modifier applied to %s", typestr
);
4770 bpf_error("ATALK host filtering not implemented");
4773 bpf_error("AARP host filtering not implemented");
4776 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4779 bpf_error("SCA host filtering not implemented");
4782 bpf_error("LAT host filtering not implemented");
4785 bpf_error("MOPDL host filtering not implemented");
4788 bpf_error("MOPRC host filtering not implemented");
4791 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4794 bpf_error("'icmp6' modifier applied to %s", typestr
);
4797 bpf_error("'ah' modifier applied to %s", typestr
);
4800 bpf_error("'esp' modifier applied to %s", typestr
);
4803 bpf_error("ISO host filtering not implemented");
4806 bpf_error("'esis' modifier applied to %s", typestr
);
4809 bpf_error("'isis' modifier applied to %s", typestr
);
4812 bpf_error("'clnp' modifier applied to %s", typestr
);
4815 bpf_error("'stp' modifier applied to %s", typestr
);
4818 bpf_error("IPX host filtering not implemented");
4821 bpf_error("'netbeui' modifier applied to %s", typestr
);
4824 bpf_error("'radio' modifier applied to %s", typestr
);
4834 static struct block
*
4835 gen_gateway(eaddr
, alist
, proto
, dir
)
4836 const u_char
*eaddr
;
4837 bpf_u_int32
**alist
;
4841 struct block
*b0
, *b1
, *tmp
;
4844 bpf_error("direction applied to 'gateway'");
4853 case DLT_NETANALYZER
:
4854 case DLT_NETANALYZER_TRANSPARENT
:
4855 b1
= gen_prevlinkhdr_check();
4856 b0
= gen_ehostop(eaddr
, Q_OR
);
4861 b0
= gen_fhostop(eaddr
, Q_OR
);
4864 b0
= gen_thostop(eaddr
, Q_OR
);
4866 case DLT_IEEE802_11
:
4867 case DLT_PRISM_HEADER
:
4868 case DLT_IEEE802_11_RADIO_AVS
:
4869 case DLT_IEEE802_11_RADIO
:
4871 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4875 * This is LLC-multiplexed traffic; if it were
4876 * LANE, linktype would have been set to
4880 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4882 case DLT_IP_OVER_FC
:
4883 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4887 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4889 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4891 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4900 bpf_error("illegal modifier of 'gateway'");
4906 gen_proto_abbrev(proto
)
4915 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4916 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4921 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4922 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4927 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4928 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4933 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4936 #ifndef IPPROTO_IGMP
4937 #define IPPROTO_IGMP 2
4941 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4944 #ifndef IPPROTO_IGRP
4945 #define IPPROTO_IGRP 9
4948 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4952 #define IPPROTO_PIM 103
4956 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4957 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4961 #ifndef IPPROTO_VRRP
4962 #define IPPROTO_VRRP 112
4966 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4969 #ifndef IPPROTO_CARP
4970 #define IPPROTO_CARP 112
4974 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4978 b1
= gen_linktype(ETHERTYPE_IP
);
4982 b1
= gen_linktype(ETHERTYPE_ARP
);
4986 b1
= gen_linktype(ETHERTYPE_REVARP
);
4990 bpf_error("link layer applied in wrong context");
4993 b1
= gen_linktype(ETHERTYPE_ATALK
);
4997 b1
= gen_linktype(ETHERTYPE_AARP
);
5001 b1
= gen_linktype(ETHERTYPE_DN
);
5005 b1
= gen_linktype(ETHERTYPE_SCA
);
5009 b1
= gen_linktype(ETHERTYPE_LAT
);
5013 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5017 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5021 b1
= gen_linktype(ETHERTYPE_IPV6
);
5024 #ifndef IPPROTO_ICMPV6
5025 #define IPPROTO_ICMPV6 58
5028 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5032 #define IPPROTO_AH 51
5035 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5036 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5041 #define IPPROTO_ESP 50
5044 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5045 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5050 b1
= gen_linktype(LLCSAP_ISONS
);
5054 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5058 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5061 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5062 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5063 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5065 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5067 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5069 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5073 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5074 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5075 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5077 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5079 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5081 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5085 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5086 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5087 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5089 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5094 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5095 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5100 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5101 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5103 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5105 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5110 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5111 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5116 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5117 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5122 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5126 b1
= gen_linktype(LLCSAP_8021D
);
5130 b1
= gen_linktype(LLCSAP_IPX
);
5134 b1
= gen_linktype(LLCSAP_NETBEUI
);
5138 bpf_error("'radio' is not a valid protocol type");
5146 static struct block
*
5152 /* not IPv4 frag other than the first frag */
5153 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5154 b
= new_block(JMP(BPF_JSET
));
5163 * Generate a comparison to a port value in the transport-layer header
5164 * at the specified offset from the beginning of that header.
5166 * XXX - this handles a variable-length prefix preceding the link-layer
5167 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5168 * variable-length link-layer headers (such as Token Ring or 802.11
5171 static struct block
*
5172 gen_portatom(off
, v
)
5176 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5179 static struct block
*
5180 gen_portatom6(off
, v
)
5184 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5188 gen_portop(port
, proto
, dir
)
5189 int port
, proto
, dir
;
5191 struct block
*b0
, *b1
, *tmp
;
5193 /* ip proto 'proto' and not a fragment other than the first fragment */
5194 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5200 b1
= gen_portatom(0, (bpf_int32
)port
);
5204 b1
= gen_portatom(2, (bpf_int32
)port
);
5209 tmp
= gen_portatom(0, (bpf_int32
)port
);
5210 b1
= gen_portatom(2, (bpf_int32
)port
);
5215 tmp
= gen_portatom(0, (bpf_int32
)port
);
5216 b1
= gen_portatom(2, (bpf_int32
)port
);
5228 static struct block
*
5229 gen_port(port
, ip_proto
, dir
)
5234 struct block
*b0
, *b1
, *tmp
;
5239 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5240 * not LLC encapsulation with LLCSAP_IP.
5242 * For IEEE 802 networks - which includes 802.5 token ring
5243 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5244 * says that SNAP encapsulation is used, not LLC encapsulation
5247 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5248 * RFC 2225 say that SNAP encapsulation is used, not LLC
5249 * encapsulation with LLCSAP_IP.
5251 * So we always check for ETHERTYPE_IP.
5253 b0
= gen_linktype(ETHERTYPE_IP
);
5259 b1
= gen_portop(port
, ip_proto
, dir
);
5263 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5264 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5266 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5278 gen_portop6(port
, proto
, dir
)
5279 int port
, proto
, dir
;
5281 struct block
*b0
, *b1
, *tmp
;
5283 /* ip6 proto 'proto' */
5284 /* XXX - catch the first fragment of a fragmented packet? */
5285 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5289 b1
= gen_portatom6(0, (bpf_int32
)port
);
5293 b1
= gen_portatom6(2, (bpf_int32
)port
);
5298 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5299 b1
= gen_portatom6(2, (bpf_int32
)port
);
5304 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5305 b1
= gen_portatom6(2, (bpf_int32
)port
);
5317 static struct block
*
5318 gen_port6(port
, ip_proto
, dir
)
5323 struct block
*b0
, *b1
, *tmp
;
5325 /* link proto ip6 */
5326 b0
= gen_linktype(ETHERTYPE_IPV6
);
5332 b1
= gen_portop6(port
, ip_proto
, dir
);
5336 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5337 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5339 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5350 /* gen_portrange code */
5351 static struct block
*
5352 gen_portrangeatom(off
, v1
, v2
)
5356 struct block
*b1
, *b2
;
5360 * Reverse the order of the ports, so v1 is the lower one.
5369 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5370 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5378 gen_portrangeop(port1
, port2
, proto
, dir
)
5383 struct block
*b0
, *b1
, *tmp
;
5385 /* ip proto 'proto' and not a fragment other than the first fragment */
5386 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5392 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5396 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5401 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5402 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5407 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5408 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5420 static struct block
*
5421 gen_portrange(port1
, port2
, ip_proto
, dir
)
5426 struct block
*b0
, *b1
, *tmp
;
5429 b0
= gen_linktype(ETHERTYPE_IP
);
5435 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5439 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5440 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5442 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5453 static struct block
*
5454 gen_portrangeatom6(off
, v1
, v2
)
5458 struct block
*b1
, *b2
;
5462 * Reverse the order of the ports, so v1 is the lower one.
5471 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5472 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5480 gen_portrangeop6(port1
, port2
, proto
, dir
)
5485 struct block
*b0
, *b1
, *tmp
;
5487 /* ip6 proto 'proto' */
5488 /* XXX - catch the first fragment of a fragmented packet? */
5489 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5493 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5497 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5502 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5503 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5508 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5509 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5521 static struct block
*
5522 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5527 struct block
*b0
, *b1
, *tmp
;
5529 /* link proto ip6 */
5530 b0
= gen_linktype(ETHERTYPE_IPV6
);
5536 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5540 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5541 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5543 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5555 lookup_proto(name
, proto
)
5556 register const char *name
;
5566 v
= pcap_nametoproto(name
);
5567 if (v
== PROTO_UNDEF
)
5568 bpf_error("unknown ip proto '%s'", name
);
5572 /* XXX should look up h/w protocol type based on linktype */
5573 v
= pcap_nametoeproto(name
);
5574 if (v
== PROTO_UNDEF
) {
5575 v
= pcap_nametollc(name
);
5576 if (v
== PROTO_UNDEF
)
5577 bpf_error("unknown ether proto '%s'", name
);
5582 if (strcmp(name
, "esis") == 0)
5584 else if (strcmp(name
, "isis") == 0)
5586 else if (strcmp(name
, "clnp") == 0)
5589 bpf_error("unknown osi proto '%s'", name
);
5609 static struct block
*
5610 gen_protochain(v
, proto
, dir
)
5615 #ifdef NO_PROTOCHAIN
5616 return gen_proto(v
, proto
, dir
);
5618 struct block
*b0
, *b
;
5619 struct slist
*s
[100];
5620 int fix2
, fix3
, fix4
, fix5
;
5621 int ahcheck
, again
, end
;
5623 int reg2
= alloc_reg();
5625 memset(s
, 0, sizeof(s
));
5626 fix3
= fix4
= fix5
= 0;
5633 b0
= gen_protochain(v
, Q_IP
, dir
);
5634 b
= gen_protochain(v
, Q_IPV6
, dir
);
5638 bpf_error("bad protocol applied for 'protochain'");
5643 * We don't handle variable-length prefixes before the link-layer
5644 * header, or variable-length link-layer headers, here yet.
5645 * We might want to add BPF instructions to do the protochain
5646 * work, to simplify that and, on platforms that have a BPF
5647 * interpreter with the new instructions, let the filtering
5648 * be done in the kernel. (We already require a modified BPF
5649 * engine to do the protochain stuff, to support backward
5650 * branches, and backward branch support is unlikely to appear
5651 * in kernel BPF engines.)
5653 if (off_linkpl
.is_variable
)
5654 bpf_error("'protochain' not supported with variable length headers");
5656 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5659 * s[0] is a dummy entry to protect other BPF insn from damage
5660 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5661 * hard to find interdependency made by jump table fixup.
5664 s
[i
] = new_stmt(0); /*dummy*/
5669 b0
= gen_linktype(ETHERTYPE_IP
);
5672 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5673 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5675 /* X = ip->ip_hl << 2 */
5676 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5677 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5682 b0
= gen_linktype(ETHERTYPE_IPV6
);
5684 /* A = ip6->ip_nxt */
5685 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5686 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5688 /* X = sizeof(struct ip6_hdr) */
5689 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5695 bpf_error("unsupported proto to gen_protochain");
5699 /* again: if (A == v) goto end; else fall through; */
5701 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5703 s
[i
]->s
.jt
= NULL
; /*later*/
5704 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5708 #ifndef IPPROTO_NONE
5709 #define IPPROTO_NONE 59
5711 /* if (A == IPPROTO_NONE) goto end */
5712 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5713 s
[i
]->s
.jt
= NULL
; /*later*/
5714 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5715 s
[i
]->s
.k
= IPPROTO_NONE
;
5716 s
[fix5
]->s
.jf
= s
[i
];
5720 if (proto
== Q_IPV6
) {
5721 int v6start
, v6end
, v6advance
, j
;
5724 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5725 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5726 s
[i
]->s
.jt
= NULL
; /*later*/
5727 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5728 s
[i
]->s
.k
= IPPROTO_HOPOPTS
;
5729 s
[fix2
]->s
.jf
= s
[i
];
5731 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5732 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5733 s
[i
]->s
.jt
= NULL
; /*later*/
5734 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5735 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5737 /* if (A == IPPROTO_ROUTING) goto v6advance */
5738 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5739 s
[i
]->s
.jt
= NULL
; /*later*/
5740 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5741 s
[i
]->s
.k
= IPPROTO_ROUTING
;
5743 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5744 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5745 s
[i
]->s
.jt
= NULL
; /*later*/
5746 s
[i
]->s
.jf
= NULL
; /*later*/
5747 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5757 * A = P[X + packet head];
5758 * X = X + (P[X + packet head + 1] + 1) * 8;
5760 /* A = P[X + packet head] */
5761 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5762 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5765 s
[i
] = new_stmt(BPF_ST
);
5768 /* A = P[X + packet head + 1]; */
5769 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5770 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5773 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5777 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5781 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5785 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5788 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5792 /* goto again; (must use BPF_JA for backward jump) */
5793 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5794 s
[i
]->s
.k
= again
- i
- 1;
5795 s
[i
- 1]->s
.jf
= s
[i
];
5799 for (j
= v6start
; j
<= v6end
; j
++)
5800 s
[j
]->s
.jt
= s
[v6advance
];
5803 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5805 s
[fix2
]->s
.jf
= s
[i
];
5811 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5812 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5813 s
[i
]->s
.jt
= NULL
; /*later*/
5814 s
[i
]->s
.jf
= NULL
; /*later*/
5815 s
[i
]->s
.k
= IPPROTO_AH
;
5817 s
[fix3
]->s
.jf
= s
[ahcheck
];
5824 * X = X + (P[X + 1] + 2) * 4;
5827 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5829 /* A = P[X + packet head]; */
5830 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5831 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5834 s
[i
] = new_stmt(BPF_ST
);
5838 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5841 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5845 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5847 /* A = P[X + packet head] */
5848 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5849 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5852 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5856 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5860 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5863 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5867 /* goto again; (must use BPF_JA for backward jump) */
5868 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5869 s
[i
]->s
.k
= again
- i
- 1;
5874 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5876 s
[fix2
]->s
.jt
= s
[end
];
5877 s
[fix4
]->s
.jf
= s
[end
];
5878 s
[fix5
]->s
.jt
= s
[end
];
5885 for (i
= 0; i
< max
- 1; i
++)
5886 s
[i
]->next
= s
[i
+ 1];
5887 s
[max
- 1]->next
= NULL
;
5892 b
= new_block(JMP(BPF_JEQ
));
5893 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5903 static struct block
*
5904 gen_check_802_11_data_frame()
5907 struct block
*b0
, *b1
;
5910 * A data frame has the 0x08 bit (b3) in the frame control field set
5911 * and the 0x04 bit (b2) clear.
5913 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5914 b0
= new_block(JMP(BPF_JSET
));
5918 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5919 b1
= new_block(JMP(BPF_JSET
));
5930 * Generate code that checks whether the packet is a packet for protocol
5931 * <proto> and whether the type field in that protocol's header has
5932 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5933 * IP packet and checks the protocol number in the IP header against <v>.
5935 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5936 * against Q_IP and Q_IPV6.
5938 static struct block
*
5939 gen_proto(v
, proto
, dir
)
5944 struct block
*b0
, *b1
;
5949 if (dir
!= Q_DEFAULT
)
5950 bpf_error("direction applied to 'proto'");
5954 b0
= gen_proto(v
, Q_IP
, dir
);
5955 b1
= gen_proto(v
, Q_IPV6
, dir
);
5961 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5962 * not LLC encapsulation with LLCSAP_IP.
5964 * For IEEE 802 networks - which includes 802.5 token ring
5965 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5966 * says that SNAP encapsulation is used, not LLC encapsulation
5969 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5970 * RFC 2225 say that SNAP encapsulation is used, not LLC
5971 * encapsulation with LLCSAP_IP.
5973 * So we always check for ETHERTYPE_IP.
5975 b0
= gen_linktype(ETHERTYPE_IP
);
5977 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5979 b1
= gen_protochain(v
, Q_IP
);
5989 * Frame Relay packets typically have an OSI
5990 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5991 * generates code to check for all the OSI
5992 * NLPIDs, so calling it and then adding a check
5993 * for the particular NLPID for which we're
5994 * looking is bogus, as we can just check for
5997 * What we check for is the NLPID and a frame
5998 * control field value of UI, i.e. 0x03 followed
6001 * XXX - assumes a 2-byte Frame Relay header with
6002 * DLCI and flags. What if the address is longer?
6004 * XXX - what about SNAP-encapsulated frames?
6006 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
6012 * Cisco uses an Ethertype lookalike - for OSI,
6015 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6016 /* OSI in C-HDLC is stuffed with a fudge byte */
6017 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6022 b0
= gen_linktype(LLCSAP_ISONS
);
6023 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6029 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6031 * 4 is the offset of the PDU type relative to the IS-IS
6034 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6039 bpf_error("arp does not encapsulate another protocol");
6043 bpf_error("rarp does not encapsulate another protocol");
6047 bpf_error("atalk encapsulation is not specifiable");
6051 bpf_error("decnet encapsulation is not specifiable");
6055 bpf_error("sca does not encapsulate another protocol");
6059 bpf_error("lat does not encapsulate another protocol");
6063 bpf_error("moprc does not encapsulate another protocol");
6067 bpf_error("mopdl does not encapsulate another protocol");
6071 return gen_linktype(v
);
6074 bpf_error("'udp proto' is bogus");
6078 bpf_error("'tcp proto' is bogus");
6082 bpf_error("'sctp proto' is bogus");
6086 bpf_error("'icmp proto' is bogus");
6090 bpf_error("'igmp proto' is bogus");
6094 bpf_error("'igrp proto' is bogus");
6098 bpf_error("'pim proto' is bogus");
6102 bpf_error("'vrrp proto' is bogus");
6106 bpf_error("'carp proto' is bogus");
6110 b0
= gen_linktype(ETHERTYPE_IPV6
);
6113 * Also check for a fragment header before the final
6116 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6117 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6119 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6122 b1
= gen_protochain(v
, Q_IPV6
);
6128 bpf_error("'icmp6 proto' is bogus");
6131 bpf_error("'ah proto' is bogus");
6134 bpf_error("'ah proto' is bogus");
6137 bpf_error("'stp proto' is bogus");
6140 bpf_error("'ipx proto' is bogus");
6143 bpf_error("'netbeui proto' is bogus");
6146 bpf_error("'radio proto' is bogus");
6157 register const char *name
;
6160 int proto
= q
.proto
;
6164 bpf_u_int32 mask
, addr
;
6166 bpf_u_int32
**alist
;
6169 struct sockaddr_in
*sin4
;
6170 struct sockaddr_in6
*sin6
;
6171 struct addrinfo
*res
, *res0
;
6172 struct in6_addr mask128
;
6174 struct block
*b
, *tmp
;
6175 int port
, real_proto
;
6181 addr
= pcap_nametonetaddr(name
);
6183 bpf_error("unknown network '%s'", name
);
6184 /* Left justify network addr and calculate its network mask */
6186 while (addr
&& (addr
& 0xff000000) == 0) {
6190 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6194 if (proto
== Q_LINK
) {
6198 case DLT_NETANALYZER
:
6199 case DLT_NETANALYZER_TRANSPARENT
:
6200 eaddr
= pcap_ether_hostton(name
);
6203 "unknown ether host '%s'", name
);
6204 tmp
= gen_prevlinkhdr_check();
6205 b
= gen_ehostop(eaddr
, dir
);
6212 eaddr
= pcap_ether_hostton(name
);
6215 "unknown FDDI host '%s'", name
);
6216 b
= gen_fhostop(eaddr
, dir
);
6221 eaddr
= pcap_ether_hostton(name
);
6224 "unknown token ring host '%s'", name
);
6225 b
= gen_thostop(eaddr
, dir
);
6229 case DLT_IEEE802_11
:
6230 case DLT_PRISM_HEADER
:
6231 case DLT_IEEE802_11_RADIO_AVS
:
6232 case DLT_IEEE802_11_RADIO
:
6234 eaddr
= pcap_ether_hostton(name
);
6237 "unknown 802.11 host '%s'", name
);
6238 b
= gen_wlanhostop(eaddr
, dir
);
6242 case DLT_IP_OVER_FC
:
6243 eaddr
= pcap_ether_hostton(name
);
6246 "unknown Fibre Channel host '%s'", name
);
6247 b
= gen_ipfchostop(eaddr
, dir
);
6252 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6253 } else if (proto
== Q_DECNET
) {
6254 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6256 * I don't think DECNET hosts can be multihomed, so
6257 * there is no need to build up a list of addresses
6259 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6262 alist
= pcap_nametoaddr(name
);
6263 if (alist
== NULL
|| *alist
== NULL
)
6264 bpf_error("unknown host '%s'", name
);
6266 if (off_linktype
.constant_part
== (u_int
)-1 &&
6267 tproto
== Q_DEFAULT
)
6269 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6271 tmp
= gen_host(**alist
++, 0xffffffff,
6272 tproto
, dir
, q
.addr
);
6278 memset(&mask128
, 0xff, sizeof(mask128
));
6279 res0
= res
= pcap_nametoaddrinfo(name
);
6281 bpf_error("unknown host '%s'", name
);
6284 tproto
= tproto6
= proto
;
6285 if (off_linktype
.constant_part
== -1 &&
6286 tproto
== Q_DEFAULT
) {
6290 for (res
= res0
; res
; res
= res
->ai_next
) {
6291 switch (res
->ai_family
) {
6293 if (tproto
== Q_IPV6
)
6296 sin4
= (struct sockaddr_in
*)
6298 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6299 0xffffffff, tproto
, dir
, q
.addr
);
6302 if (tproto6
== Q_IP
)
6305 sin6
= (struct sockaddr_in6
*)
6307 tmp
= gen_host6(&sin6
->sin6_addr
,
6308 &mask128
, tproto6
, dir
, q
.addr
);
6320 bpf_error("unknown host '%s'%s", name
,
6321 (proto
== Q_DEFAULT
)
6323 : " for specified address family");
6330 if (proto
!= Q_DEFAULT
&&
6331 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6332 bpf_error("illegal qualifier of 'port'");
6333 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6334 bpf_error("unknown port '%s'", name
);
6335 if (proto
== Q_UDP
) {
6336 if (real_proto
== IPPROTO_TCP
)
6337 bpf_error("port '%s' is tcp", name
);
6338 else if (real_proto
== IPPROTO_SCTP
)
6339 bpf_error("port '%s' is sctp", name
);
6341 /* override PROTO_UNDEF */
6342 real_proto
= IPPROTO_UDP
;
6344 if (proto
== Q_TCP
) {
6345 if (real_proto
== IPPROTO_UDP
)
6346 bpf_error("port '%s' is udp", name
);
6348 else if (real_proto
== IPPROTO_SCTP
)
6349 bpf_error("port '%s' is sctp", name
);
6351 /* override PROTO_UNDEF */
6352 real_proto
= IPPROTO_TCP
;
6354 if (proto
== Q_SCTP
) {
6355 if (real_proto
== IPPROTO_UDP
)
6356 bpf_error("port '%s' is udp", name
);
6358 else if (real_proto
== IPPROTO_TCP
)
6359 bpf_error("port '%s' is tcp", name
);
6361 /* override PROTO_UNDEF */
6362 real_proto
= IPPROTO_SCTP
;
6365 bpf_error("illegal port number %d < 0", port
);
6367 bpf_error("illegal port number %d > 65535", port
);
6368 b
= gen_port(port
, real_proto
, dir
);
6369 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6373 if (proto
!= Q_DEFAULT
&&
6374 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6375 bpf_error("illegal qualifier of 'portrange'");
6376 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6377 bpf_error("unknown port in range '%s'", name
);
6378 if (proto
== Q_UDP
) {
6379 if (real_proto
== IPPROTO_TCP
)
6380 bpf_error("port in range '%s' is tcp", name
);
6381 else if (real_proto
== IPPROTO_SCTP
)
6382 bpf_error("port in range '%s' is sctp", name
);
6384 /* override PROTO_UNDEF */
6385 real_proto
= IPPROTO_UDP
;
6387 if (proto
== Q_TCP
) {
6388 if (real_proto
== IPPROTO_UDP
)
6389 bpf_error("port in range '%s' is udp", name
);
6390 else if (real_proto
== IPPROTO_SCTP
)
6391 bpf_error("port in range '%s' is sctp", name
);
6393 /* override PROTO_UNDEF */
6394 real_proto
= IPPROTO_TCP
;
6396 if (proto
== Q_SCTP
) {
6397 if (real_proto
== IPPROTO_UDP
)
6398 bpf_error("port in range '%s' is udp", name
);
6399 else if (real_proto
== IPPROTO_TCP
)
6400 bpf_error("port in range '%s' is tcp", name
);
6402 /* override PROTO_UNDEF */
6403 real_proto
= IPPROTO_SCTP
;
6406 bpf_error("illegal port number %d < 0", port1
);
6408 bpf_error("illegal port number %d > 65535", port1
);
6410 bpf_error("illegal port number %d < 0", port2
);
6412 bpf_error("illegal port number %d > 65535", port2
);
6414 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6415 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6420 eaddr
= pcap_ether_hostton(name
);
6422 bpf_error("unknown ether host: %s", name
);
6424 alist
= pcap_nametoaddr(name
);
6425 if (alist
== NULL
|| *alist
== NULL
)
6426 bpf_error("unknown host '%s'", name
);
6427 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6431 bpf_error("'gateway' not supported in this configuration");
6435 real_proto
= lookup_proto(name
, proto
);
6436 if (real_proto
>= 0)
6437 return gen_proto(real_proto
, proto
, dir
);
6439 bpf_error("unknown protocol: %s", name
);
6442 real_proto
= lookup_proto(name
, proto
);
6443 if (real_proto
>= 0)
6444 return gen_protochain(real_proto
, proto
, dir
);
6446 bpf_error("unknown protocol: %s", name
);
6457 gen_mcode(s1
, s2
, masklen
, q
)
6458 register const char *s1
, *s2
;
6459 register unsigned int masklen
;
6462 register int nlen
, mlen
;
6465 nlen
= __pcap_atoin(s1
, &n
);
6466 /* Promote short ipaddr */
6470 mlen
= __pcap_atoin(s2
, &m
);
6471 /* Promote short ipaddr */
6474 bpf_error("non-network bits set in \"%s mask %s\"",
6477 /* Convert mask len to mask */
6479 bpf_error("mask length must be <= 32");
6482 * X << 32 is not guaranteed by C to be 0; it's
6487 m
= 0xffffffff << (32 - masklen
);
6489 bpf_error("non-network bits set in \"%s/%d\"",
6496 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6499 bpf_error("Mask syntax for networks only");
6508 register const char *s
;
6513 int proto
= q
.proto
;
6519 else if (q
.proto
== Q_DECNET
)
6520 vlen
= __pcap_atodn(s
, &v
);
6522 vlen
= __pcap_atoin(s
, &v
);
6529 if (proto
== Q_DECNET
)
6530 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6531 else if (proto
== Q_LINK
) {
6532 bpf_error("illegal link layer address");
6535 if (s
== NULL
&& q
.addr
== Q_NET
) {
6536 /* Promote short net number */
6537 while (v
&& (v
& 0xff000000) == 0) {
6542 /* Promote short ipaddr */
6546 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6551 proto
= IPPROTO_UDP
;
6552 else if (proto
== Q_TCP
)
6553 proto
= IPPROTO_TCP
;
6554 else if (proto
== Q_SCTP
)
6555 proto
= IPPROTO_SCTP
;
6556 else if (proto
== Q_DEFAULT
)
6557 proto
= PROTO_UNDEF
;
6559 bpf_error("illegal qualifier of 'port'");
6562 bpf_error("illegal port number %u > 65535", v
);
6566 b
= gen_port((int)v
, proto
, dir
);
6567 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6573 proto
= IPPROTO_UDP
;
6574 else if (proto
== Q_TCP
)
6575 proto
= IPPROTO_TCP
;
6576 else if (proto
== Q_SCTP
)
6577 proto
= IPPROTO_SCTP
;
6578 else if (proto
== Q_DEFAULT
)
6579 proto
= PROTO_UNDEF
;
6581 bpf_error("illegal qualifier of 'portrange'");
6584 bpf_error("illegal port number %u > 65535", v
);
6588 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6589 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6594 bpf_error("'gateway' requires a name");
6598 return gen_proto((int)v
, proto
, dir
);
6601 return gen_protochain((int)v
, proto
, dir
);
6616 gen_mcode6(s1
, s2
, masklen
, q
)
6617 register const char *s1
, *s2
;
6618 register unsigned int masklen
;
6621 struct addrinfo
*res
;
6622 struct in6_addr
*addr
;
6623 struct in6_addr mask
;
6628 bpf_error("no mask %s supported", s2
);
6630 res
= pcap_nametoaddrinfo(s1
);
6632 bpf_error("invalid ip6 address %s", s1
);
6635 bpf_error("%s resolved to multiple address", s1
);
6636 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6638 if (sizeof(mask
) * 8 < masklen
)
6639 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6640 memset(&mask
, 0, sizeof(mask
));
6641 memset(&mask
, 0xff, masklen
/ 8);
6643 mask
.s6_addr
[masklen
/ 8] =
6644 (0xff << (8 - masklen
% 8)) & 0xff;
6647 a
= (u_int32_t
*)addr
;
6648 m
= (u_int32_t
*)&mask
;
6649 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6650 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6651 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6659 bpf_error("Mask syntax for networks only");
6663 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6669 bpf_error("invalid qualifier against IPv6 address");
6678 register const u_char
*eaddr
;
6681 struct block
*b
, *tmp
;
6683 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6686 case DLT_NETANALYZER
:
6687 case DLT_NETANALYZER_TRANSPARENT
:
6688 tmp
= gen_prevlinkhdr_check();
6689 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6694 return gen_fhostop(eaddr
, (int)q
.dir
);
6696 return gen_thostop(eaddr
, (int)q
.dir
);
6697 case DLT_IEEE802_11
:
6698 case DLT_PRISM_HEADER
:
6699 case DLT_IEEE802_11_RADIO_AVS
:
6700 case DLT_IEEE802_11_RADIO
:
6702 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6703 case DLT_IP_OVER_FC
:
6704 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6706 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6710 bpf_error("ethernet address used in non-ether expression");
6717 struct slist
*s0
, *s1
;
6720 * This is definitely not the best way to do this, but the
6721 * lists will rarely get long.
6728 static struct slist
*
6734 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6739 static struct slist
*
6745 s
= new_stmt(BPF_LD
|BPF_MEM
);
6751 * Modify "index" to use the value stored into its register as an
6752 * offset relative to the beginning of the header for the protocol
6753 * "proto", and allocate a register and put an item "size" bytes long
6754 * (1, 2, or 4) at that offset into that register, making it the register
6758 gen_load(proto
, inst
, size
)
6763 struct slist
*s
, *tmp
;
6765 int regno
= alloc_reg();
6767 free_reg(inst
->regno
);
6771 bpf_error("data size must be 1, 2, or 4");
6787 bpf_error("unsupported index operation");
6791 * The offset is relative to the beginning of the packet
6792 * data, if we have a radio header. (If we don't, this
6795 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6796 linktype
!= DLT_IEEE802_11_RADIO
&&
6797 linktype
!= DLT_PRISM_HEADER
)
6798 bpf_error("radio information not present in capture");
6801 * Load into the X register the offset computed into the
6802 * register specified by "index".
6804 s
= xfer_to_x(inst
);
6807 * Load the item at that offset.
6809 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6811 sappend(inst
->s
, s
);
6816 * The offset is relative to the beginning of
6817 * the link-layer header.
6819 * XXX - what about ATM LANE? Should the index be
6820 * relative to the beginning of the AAL5 frame, so
6821 * that 0 refers to the beginning of the LE Control
6822 * field, or relative to the beginning of the LAN
6823 * frame, so that 0 refers, for Ethernet LANE, to
6824 * the beginning of the destination address?
6826 s
= gen_abs_offset_varpart(&off_linkhdr
);
6829 * If "s" is non-null, it has code to arrange that the
6830 * X register contains the length of the prefix preceding
6831 * the link-layer header. Add to it the offset computed
6832 * into the register specified by "index", and move that
6833 * into the X register. Otherwise, just load into the X
6834 * register the offset computed into the register specified
6838 sappend(s
, xfer_to_a(inst
));
6839 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6840 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6842 s
= xfer_to_x(inst
);
6845 * Load the item at the sum of the offset we've put in the
6846 * X register and the offset of the start of the link
6847 * layer header (which is 0 if the radio header is
6848 * variable-length; that header length is what we put
6849 * into the X register and then added to the index).
6851 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6852 tmp
->s
.k
= off_linkhdr
.constant_part
;
6854 sappend(inst
->s
, s
);
6868 * The offset is relative to the beginning of
6869 * the network-layer header.
6870 * XXX - are there any cases where we want
6873 s
= gen_abs_offset_varpart(&off_linkpl
);
6876 * If "s" is non-null, it has code to arrange that the
6877 * X register contains the variable part of the offset
6878 * of the link-layer payload. Add to it the offset
6879 * computed into the register specified by "index",
6880 * and move that into the X register. Otherwise, just
6881 * load into the X register the offset computed into
6882 * the register specified by "index".
6885 sappend(s
, xfer_to_a(inst
));
6886 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6887 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6889 s
= xfer_to_x(inst
);
6892 * Load the item at the sum of the offset we've put in the
6893 * X register, the offset of the start of the network
6894 * layer header from the beginning of the link-layer
6895 * payload, and the constant part of the offset of the
6896 * start of the link-layer payload.
6898 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6899 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6901 sappend(inst
->s
, s
);
6904 * Do the computation only if the packet contains
6905 * the protocol in question.
6907 b
= gen_proto_abbrev(proto
);
6909 gen_and(inst
->b
, b
);
6923 * The offset is relative to the beginning of
6924 * the transport-layer header.
6926 * Load the X register with the length of the IPv4 header
6927 * (plus the offset of the link-layer header, if it's
6928 * a variable-length header), in bytes.
6930 * XXX - are there any cases where we want
6932 * XXX - we should, if we're built with
6933 * IPv6 support, generate code to load either
6934 * IPv4, IPv6, or both, as appropriate.
6936 s
= gen_loadx_iphdrlen();
6939 * The X register now contains the sum of the variable
6940 * part of the offset of the link-layer payload and the
6941 * length of the network-layer header.
6943 * Load into the A register the offset relative to
6944 * the beginning of the transport layer header,
6945 * add the X register to that, move that to the
6946 * X register, and load with an offset from the
6947 * X register equal to the sum of the constant part of
6948 * the offset of the link-layer payload and the offset,
6949 * relative to the beginning of the link-layer payload,
6950 * of the network-layer header.
6952 sappend(s
, xfer_to_a(inst
));
6953 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6954 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6955 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6956 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6957 sappend(inst
->s
, s
);
6960 * Do the computation only if the packet contains
6961 * the protocol in question - which is true only
6962 * if this is an IP datagram and is the first or
6963 * only fragment of that datagram.
6965 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6967 gen_and(inst
->b
, b
);
6968 gen_and(gen_proto_abbrev(Q_IP
), b
);
6972 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6975 inst
->regno
= regno
;
6976 s
= new_stmt(BPF_ST
);
6978 sappend(inst
->s
, s
);
6984 gen_relation(code
, a0
, a1
, reversed
)
6986 struct arth
*a0
, *a1
;
6989 struct slist
*s0
, *s1
, *s2
;
6990 struct block
*b
, *tmp
;
6994 if (code
== BPF_JEQ
) {
6995 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6996 b
= new_block(JMP(code
));
7000 b
= new_block(BPF_JMP
|code
|BPF_X
);
7006 sappend(a0
->s
, a1
->s
);
7010 free_reg(a0
->regno
);
7011 free_reg(a1
->regno
);
7013 /* 'and' together protocol checks */
7016 gen_and(a0
->b
, tmp
= a1
->b
);
7032 int regno
= alloc_reg();
7033 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7036 s
= new_stmt(BPF_LD
|BPF_LEN
);
7037 s
->next
= new_stmt(BPF_ST
);
7038 s
->next
->s
.k
= regno
;
7053 a
= (struct arth
*)newchunk(sizeof(*a
));
7057 s
= new_stmt(BPF_LD
|BPF_IMM
);
7059 s
->next
= new_stmt(BPF_ST
);
7075 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7078 s
= new_stmt(BPF_ST
);
7086 gen_arth(code
, a0
, a1
)
7088 struct arth
*a0
, *a1
;
7090 struct slist
*s0
, *s1
, *s2
;
7093 * Disallow division by, or modulus by, zero; we do this here
7094 * so that it gets done even if the optimizer is disabled.
7096 if (code
== BPF_DIV
) {
7097 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7098 bpf_error("division by zero");
7099 } else if (code
== BPF_MOD
) {
7100 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7101 bpf_error("modulus by zero");
7105 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7110 sappend(a0
->s
, a1
->s
);
7112 free_reg(a0
->regno
);
7113 free_reg(a1
->regno
);
7115 s0
= new_stmt(BPF_ST
);
7116 a0
->regno
= s0
->s
.k
= alloc_reg();
7123 * Here we handle simple allocation of the scratch registers.
7124 * If too many registers are alloc'd, the allocator punts.
7126 static int regused
[BPF_MEMWORDS
];
7130 * Initialize the table of used registers and the current register.
7136 memset(regused
, 0, sizeof regused
);
7140 * Return the next free register.
7145 int n
= BPF_MEMWORDS
;
7148 if (regused
[curreg
])
7149 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7151 regused
[curreg
] = 1;
7155 bpf_error("too many registers needed to evaluate expression");
7161 * Return a register to the table so it can
7171 static struct block
*
7178 s
= new_stmt(BPF_LD
|BPF_LEN
);
7179 b
= new_block(JMP(jmp
));
7190 return gen_len(BPF_JGE
, n
);
7194 * Actually, this is less than or equal.
7202 b
= gen_len(BPF_JGT
, n
);
7209 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7210 * the beginning of the link-layer header.
7211 * XXX - that means you can't test values in the radiotap header, but
7212 * as that header is difficult if not impossible to parse generally
7213 * without a loop, that might not be a severe problem. A new keyword
7214 * "radio" could be added for that, although what you'd really want
7215 * would be a way of testing particular radio header values, which
7216 * would generate code appropriate to the radio header in question.
7219 gen_byteop(op
, idx
, val
)
7230 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7233 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7237 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7241 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7245 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7249 b
= new_block(JMP(BPF_JEQ
));
7256 static u_char abroadcast
[] = { 0x0 };
7259 gen_broadcast(proto
)
7262 bpf_u_int32 hostmask
;
7263 struct block
*b0
, *b1
, *b2
;
7264 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7272 case DLT_ARCNET_LINUX
:
7273 return gen_ahostop(abroadcast
, Q_DST
);
7275 case DLT_NETANALYZER
:
7276 case DLT_NETANALYZER_TRANSPARENT
:
7277 b1
= gen_prevlinkhdr_check();
7278 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7283 return gen_fhostop(ebroadcast
, Q_DST
);
7285 return gen_thostop(ebroadcast
, Q_DST
);
7286 case DLT_IEEE802_11
:
7287 case DLT_PRISM_HEADER
:
7288 case DLT_IEEE802_11_RADIO_AVS
:
7289 case DLT_IEEE802_11_RADIO
:
7291 return gen_wlanhostop(ebroadcast
, Q_DST
);
7292 case DLT_IP_OVER_FC
:
7293 return gen_ipfchostop(ebroadcast
, Q_DST
);
7295 bpf_error("not a broadcast link");
7301 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7302 * as an indication that we don't know the netmask, and fail
7305 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7306 bpf_error("netmask not known, so 'ip broadcast' not supported");
7307 b0
= gen_linktype(ETHERTYPE_IP
);
7308 hostmask
= ~netmask
;
7309 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7310 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7311 (bpf_int32
)(~0 & hostmask
), hostmask
);
7316 bpf_error("only link-layer/IP broadcast filters supported");
7322 * Generate code to test the low-order bit of a MAC address (that's
7323 * the bottom bit of the *first* byte).
7325 static struct block
*
7326 gen_mac_multicast(offset
)
7329 register struct block
*b0
;
7330 register struct slist
*s
;
7332 /* link[offset] & 1 != 0 */
7333 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7334 b0
= new_block(JMP(BPF_JSET
));
7341 gen_multicast(proto
)
7344 register struct block
*b0
, *b1
, *b2
;
7345 register struct slist
*s
;
7353 case DLT_ARCNET_LINUX
:
7354 /* all ARCnet multicasts use the same address */
7355 return gen_ahostop(abroadcast
, Q_DST
);
7357 case DLT_NETANALYZER
:
7358 case DLT_NETANALYZER_TRANSPARENT
:
7359 b1
= gen_prevlinkhdr_check();
7360 /* ether[0] & 1 != 0 */
7361 b0
= gen_mac_multicast(0);
7367 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7369 * XXX - was that referring to bit-order issues?
7371 /* fddi[1] & 1 != 0 */
7372 return gen_mac_multicast(1);
7374 /* tr[2] & 1 != 0 */
7375 return gen_mac_multicast(2);
7376 case DLT_IEEE802_11
:
7377 case DLT_PRISM_HEADER
:
7378 case DLT_IEEE802_11_RADIO_AVS
:
7379 case DLT_IEEE802_11_RADIO
:
7384 * For control frames, there is no DA.
7386 * For management frames, DA is at an
7387 * offset of 4 from the beginning of
7390 * For data frames, DA is at an offset
7391 * of 4 from the beginning of the packet
7392 * if To DS is clear and at an offset of
7393 * 16 from the beginning of the packet
7398 * Generate the tests to be done for data frames.
7400 * First, check for To DS set, i.e. "link[1] & 0x01".
7402 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7403 b1
= new_block(JMP(BPF_JSET
));
7404 b1
->s
.k
= 0x01; /* To DS */
7408 * If To DS is set, the DA is at 16.
7410 b0
= gen_mac_multicast(16);
7414 * Now, check for To DS not set, i.e. check
7415 * "!(link[1] & 0x01)".
7417 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7418 b2
= new_block(JMP(BPF_JSET
));
7419 b2
->s
.k
= 0x01; /* To DS */
7424 * If To DS is not set, the DA is at 4.
7426 b1
= gen_mac_multicast(4);
7430 * Now OR together the last two checks. That gives
7431 * the complete set of checks for data frames.
7436 * Now check for a data frame.
7437 * I.e, check "link[0] & 0x08".
7439 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7440 b1
= new_block(JMP(BPF_JSET
));
7445 * AND that with the checks done for data frames.
7450 * If the high-order bit of the type value is 0, this
7451 * is a management frame.
7452 * I.e, check "!(link[0] & 0x08)".
7454 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7455 b2
= new_block(JMP(BPF_JSET
));
7461 * For management frames, the DA is at 4.
7463 b1
= gen_mac_multicast(4);
7467 * OR that with the checks done for data frames.
7468 * That gives the checks done for management and
7474 * If the low-order bit of the type value is 1,
7475 * this is either a control frame or a frame
7476 * with a reserved type, and thus not a
7479 * I.e., check "!(link[0] & 0x04)".
7481 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7482 b1
= new_block(JMP(BPF_JSET
));
7488 * AND that with the checks for data and management
7493 case DLT_IP_OVER_FC
:
7494 b0
= gen_mac_multicast(2);
7499 /* Link not known to support multicasts */
7503 b0
= gen_linktype(ETHERTYPE_IP
);
7504 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7509 b0
= gen_linktype(ETHERTYPE_IPV6
);
7510 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7514 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7520 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7521 * Outbound traffic is sent by this machine, while inbound traffic is
7522 * sent by a remote machine (and may include packets destined for a
7523 * unicast or multicast link-layer address we are not subscribing to).
7524 * These are the same definitions implemented by pcap_setdirection().
7525 * Capturing only unicast traffic destined for this host is probably
7526 * better accomplished using a higher-layer filter.
7532 register struct block
*b0
;
7535 * Only some data link types support inbound/outbound qualifiers.
7539 b0
= gen_relation(BPF_JEQ
,
7540 gen_load(Q_LINK
, gen_loadi(0), 1),
7547 /* match outgoing packets */
7548 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7550 /* match incoming packets */
7551 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7556 /* match outgoing packets */
7557 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7559 /* to filter on inbound traffic, invert the match */
7564 #ifdef HAVE_NET_PFVAR_H
7566 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7567 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7573 /* match outgoing packets */
7574 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7576 /* match incoming packets */
7577 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7581 case DLT_JUNIPER_MFR
:
7582 case DLT_JUNIPER_MLFR
:
7583 case DLT_JUNIPER_MLPPP
:
7584 case DLT_JUNIPER_ATM1
:
7585 case DLT_JUNIPER_ATM2
:
7586 case DLT_JUNIPER_PPPOE
:
7587 case DLT_JUNIPER_PPPOE_ATM
:
7588 case DLT_JUNIPER_GGSN
:
7589 case DLT_JUNIPER_ES
:
7590 case DLT_JUNIPER_MONITOR
:
7591 case DLT_JUNIPER_SERVICES
:
7592 case DLT_JUNIPER_ETHER
:
7593 case DLT_JUNIPER_PPP
:
7594 case DLT_JUNIPER_FRELAY
:
7595 case DLT_JUNIPER_CHDLC
:
7596 case DLT_JUNIPER_VP
:
7597 case DLT_JUNIPER_ST
:
7598 case DLT_JUNIPER_ISM
:
7599 case DLT_JUNIPER_VS
:
7600 case DLT_JUNIPER_SRX_E2E
:
7601 case DLT_JUNIPER_FIBRECHANNEL
:
7602 case DLT_JUNIPER_ATM_CEMIC
:
7604 /* juniper flags (including direction) are stored
7605 * the byte after the 3-byte magic number */
7607 /* match outgoing packets */
7608 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7610 /* match incoming packets */
7611 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7617 * If we have packet meta-data indicating a direction,
7618 * check it, otherwise give up as this link-layer type
7619 * has nothing in the packet data.
7621 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7623 * This is Linux with PF_PACKET support.
7624 * If this is a *live* capture, we can look at
7625 * special meta-data in the filter expression;
7626 * if it's a savefile, we can't.
7628 if (bpf_pcap
->rfile
!= NULL
) {
7629 /* We have a FILE *, so this is a savefile */
7630 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7635 /* match outgoing packets */
7636 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7639 /* to filter on inbound traffic, invert the match */
7642 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7643 bpf_error("inbound/outbound not supported on linktype %d",
7647 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7652 #ifdef HAVE_NET_PFVAR_H
7653 /* PF firewall log matched interface */
7655 gen_pf_ifname(const char *ifname
)
7660 if (linktype
!= DLT_PFLOG
) {
7661 bpf_error("ifname supported only on PF linktype");
7664 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7665 off
= offsetof(struct pfloghdr
, ifname
);
7666 if (strlen(ifname
) >= len
) {
7667 bpf_error("ifname interface names can only be %d characters",
7671 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7675 /* PF firewall log ruleset name */
7677 gen_pf_ruleset(char *ruleset
)
7681 if (linktype
!= DLT_PFLOG
) {
7682 bpf_error("ruleset supported only on PF linktype");
7686 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7687 bpf_error("ruleset names can only be %ld characters",
7688 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7692 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7693 strlen(ruleset
), (const u_char
*)ruleset
);
7697 /* PF firewall log rule number */
7703 if (linktype
!= DLT_PFLOG
) {
7704 bpf_error("rnr supported only on PF linktype");
7708 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7713 /* PF firewall log sub-rule number */
7715 gen_pf_srnr(int srnr
)
7719 if (linktype
!= DLT_PFLOG
) {
7720 bpf_error("srnr supported only on PF linktype");
7724 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7729 /* PF firewall log reason code */
7731 gen_pf_reason(int reason
)
7735 if (linktype
!= DLT_PFLOG
) {
7736 bpf_error("reason supported only on PF linktype");
7740 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7745 /* PF firewall log action */
7747 gen_pf_action(int action
)
7751 if (linktype
!= DLT_PFLOG
) {
7752 bpf_error("action supported only on PF linktype");
7756 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7760 #else /* !HAVE_NET_PFVAR_H */
7762 gen_pf_ifname(const char *ifname
)
7764 bpf_error("libpcap was compiled without pf support");
7770 gen_pf_ruleset(char *ruleset
)
7772 bpf_error("libpcap was compiled on a machine without pf support");
7780 bpf_error("libpcap was compiled on a machine without pf support");
7786 gen_pf_srnr(int srnr
)
7788 bpf_error("libpcap was compiled on a machine without pf support");
7794 gen_pf_reason(int reason
)
7796 bpf_error("libpcap was compiled on a machine without pf support");
7802 gen_pf_action(int action
)
7804 bpf_error("libpcap was compiled on a machine without pf support");
7808 #endif /* HAVE_NET_PFVAR_H */
7810 /* IEEE 802.11 wireless header */
7812 gen_p80211_type(int type
, int mask
)
7818 case DLT_IEEE802_11
:
7819 case DLT_PRISM_HEADER
:
7820 case DLT_IEEE802_11_RADIO_AVS
:
7821 case DLT_IEEE802_11_RADIO
:
7822 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7827 bpf_error("802.11 link-layer types supported only on 802.11");
7835 gen_p80211_fcdir(int fcdir
)
7841 case DLT_IEEE802_11
:
7842 case DLT_PRISM_HEADER
:
7843 case DLT_IEEE802_11_RADIO_AVS
:
7844 case DLT_IEEE802_11_RADIO
:
7848 bpf_error("frame direction supported only with 802.11 headers");
7852 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7853 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7860 register const u_char
*eaddr
;
7866 case DLT_ARCNET_LINUX
:
7867 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7869 return (gen_ahostop(eaddr
, (int)q
.dir
));
7871 bpf_error("ARCnet address used in non-arc expression");
7877 bpf_error("aid supported only on ARCnet");
7880 bpf_error("ARCnet address used in non-arc expression");
7885 static struct block
*
7886 gen_ahostop(eaddr
, dir
)
7887 register const u_char
*eaddr
;
7890 register struct block
*b0
, *b1
;
7893 /* src comes first, different from Ethernet */
7895 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7898 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7901 b0
= gen_ahostop(eaddr
, Q_SRC
);
7902 b1
= gen_ahostop(eaddr
, Q_DST
);
7908 b0
= gen_ahostop(eaddr
, Q_SRC
);
7909 b1
= gen_ahostop(eaddr
, Q_DST
);
7914 bpf_error("'addr1' is only supported on 802.11");
7918 bpf_error("'addr2' is only supported on 802.11");
7922 bpf_error("'addr3' is only supported on 802.11");
7926 bpf_error("'addr4' is only supported on 802.11");
7930 bpf_error("'ra' is only supported on 802.11");
7934 bpf_error("'ta' is only supported on 802.11");
7941 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7942 static struct block
*
7943 gen_vlan_bpf_extensions(int vlan_num
)
7945 struct block
*b0
, *b1
;
7948 /* generate new filter code based on extracting packet
7950 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7951 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7953 b0
= new_block(JMP(BPF_JEQ
));
7957 if (vlan_num
>= 0) {
7958 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7959 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7961 b1
= new_block(JMP(BPF_JEQ
));
7963 b1
->s
.k
= (bpf_int32
) vlan_num
;
7973 static struct block
*
7974 gen_vlan_no_bpf_extensions(int vlan_num
)
7976 struct block
*b0
, *b1
;
7978 /* check for VLAN, including QinQ */
7979 b0
= gen_linktype(ETHERTYPE_8021Q
);
7980 b1
= gen_linktype(ETHERTYPE_8021AD
);
7983 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7987 /* If a specific VLAN is requested, check VLAN id */
7988 if (vlan_num
>= 0) {
7989 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7990 (bpf_int32
)vlan_num
, 0x0fff);
7996 * The payload follows the full header, including the
7997 * VLAN tags, so skip past this VLAN tag.
7999 off_linkpl
.constant_part
+= 4;
8002 * The link-layer type information follows the VLAN tags, so
8003 * skip past this VLAN tag.
8005 off_linktype
.constant_part
+= 4;
8011 * support IEEE 802.1Q VLAN trunk over ethernet
8019 /* can't check for VLAN-encapsulated packets inside MPLS */
8020 if (label_stack_depth
> 0)
8021 bpf_error("no VLAN match after MPLS");
8024 * Check for a VLAN packet, and then change the offsets to point
8025 * to the type and data fields within the VLAN packet. Just
8026 * increment the offsets, so that we can support a hierarchy, e.g.
8027 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8030 * XXX - this is a bit of a kludge. If we were to split the
8031 * compiler into a parser that parses an expression and
8032 * generates an expression tree, and a code generator that
8033 * takes an expression tree (which could come from our
8034 * parser or from some other parser) and generates BPF code,
8035 * we could perhaps make the offsets parameters of routines
8036 * and, in the handler for an "AND" node, pass to subnodes
8037 * other than the VLAN node the adjusted offsets.
8039 * This would mean that "vlan" would, instead of changing the
8040 * behavior of *all* tests after it, change only the behavior
8041 * of tests ANDed with it. That would change the documented
8042 * semantics of "vlan", which might break some expressions.
8043 * However, it would mean that "(vlan and ip) or ip" would check
8044 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8045 * checking only for VLAN-encapsulated IP, so that could still
8046 * be considered worth doing; it wouldn't break expressions
8047 * that are of the form "vlan and ..." or "vlan N and ...",
8048 * which I suspect are the most common expressions involving
8049 * "vlan". "vlan or ..." doesn't necessarily do what the user
8050 * would really want, now, as all the "or ..." tests would
8051 * be done assuming a VLAN, even though the "or" could be viewed
8052 * as meaning "or, if this isn't a VLAN packet...".
8057 case DLT_NETANALYZER
:
8058 case DLT_NETANALYZER_TRANSPARENT
:
8059 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8060 /* Verify that this is the outer part of the packet and
8061 * not encapsulated somehow. */
8062 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
8063 off_linkhdr
.constant_part
==
8064 off_outermostlinkhdr
.constant_part
) {
8066 * Do we need special VLAN handling?
8068 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8069 b0
= gen_vlan_bpf_extensions(vlan_num
);
8071 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8074 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8077 case DLT_IEEE802_11
:
8078 case DLT_PRISM_HEADER
:
8079 case DLT_IEEE802_11_RADIO_AVS
:
8080 case DLT_IEEE802_11_RADIO
:
8081 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8085 bpf_error("no VLAN support for data link type %d",
8102 struct block
*b0
, *b1
;
8104 if (label_stack_depth
> 0) {
8105 /* just match the bottom-of-stack bit clear */
8106 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8109 * We're not in an MPLS stack yet, so check the link-layer
8110 * type against MPLS.
8114 case DLT_C_HDLC
: /* fall through */
8116 case DLT_NETANALYZER
:
8117 case DLT_NETANALYZER_TRANSPARENT
:
8118 b0
= gen_linktype(ETHERTYPE_MPLS
);
8122 b0
= gen_linktype(PPP_MPLS_UCAST
);
8125 /* FIXME add other DLT_s ...
8126 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8127 * leave it for now */
8130 bpf_error("no MPLS support for data link type %d",
8138 /* If a specific MPLS label is requested, check it */
8139 if (label_num
>= 0) {
8140 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8141 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8142 0xfffff000); /* only compare the first 20 bits */
8148 * Change the offsets to point to the type and data fields within
8149 * the MPLS packet. Just increment the offsets, so that we
8150 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8151 * capture packets with an outer label of 100000 and an inner
8154 * Increment the MPLS stack depth as well; this indicates that
8155 * we're checking MPLS-encapsulated headers, to make sure higher
8156 * level code generators don't try to match against IP-related
8157 * protocols such as Q_ARP, Q_RARP etc.
8159 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8163 label_stack_depth
++;
8168 * Support PPPOE discovery and session.
8173 /* check for PPPoE discovery */
8174 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8178 gen_pppoes(sess_num
)
8181 struct block
*b0
, *b1
;
8184 * Test against the PPPoE session link-layer type.
8186 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8188 /* If a specific session is requested, check PPPoE session id */
8189 if (sess_num
>= 0) {
8190 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8191 (bpf_int32
)sess_num
, 0x0000ffff);
8197 * Change the offsets to point to the type and data fields within
8198 * the PPP packet, and note that this is PPPoE rather than
8201 * XXX - this is a bit of a kludge. If we were to split the
8202 * compiler into a parser that parses an expression and
8203 * generates an expression tree, and a code generator that
8204 * takes an expression tree (which could come from our
8205 * parser or from some other parser) and generates BPF code,
8206 * we could perhaps make the offsets parameters of routines
8207 * and, in the handler for an "AND" node, pass to subnodes
8208 * other than the PPPoE node the adjusted offsets.
8210 * This would mean that "pppoes" would, instead of changing the
8211 * behavior of *all* tests after it, change only the behavior
8212 * of tests ANDed with it. That would change the documented
8213 * semantics of "pppoes", which might break some expressions.
8214 * However, it would mean that "(pppoes and ip) or ip" would check
8215 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8216 * checking only for VLAN-encapsulated IP, so that could still
8217 * be considered worth doing; it wouldn't break expressions
8218 * that are of the form "pppoes and ..." which I suspect are the
8219 * most common expressions involving "pppoes". "pppoes or ..."
8220 * doesn't necessarily do what the user would really want, now,
8221 * as all the "or ..." tests would be done assuming PPPoE, even
8222 * though the "or" could be viewed as meaning "or, if this isn't
8223 * a PPPoE packet...".
8225 * The "network-layer" protocol is PPPoE, which has a 6-byte
8226 * PPPoE header, followed by a PPP packet.
8228 * There is no HDLC encapsulation for the PPP packet (it's
8229 * encapsulated in PPPoES instead), so the link-layer type
8230 * starts at the first byte of the PPP packet. For PPPoE,
8231 * that offset is relative to the beginning of the total
8232 * link-layer payload, including any 802.2 LLC header, so
8233 * it's 6 bytes past off_nl.
8235 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8236 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8239 off_linktype
= off_linkhdr
;
8240 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8243 off_nl_nosnap
= 0; /* no 802.2 LLC */
8248 /* Check that this is Geneve and the VNI is correct if
8249 * specified. Parameterized to handle both IPv4 and IPv6. */
8250 static struct block
*
8251 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8252 enum e_offrel offrel
, int vni
)
8254 struct block
*b0
, *b1
;
8256 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8258 /* Check that we are operating on version 0. Otherwise, we
8259 * can't decode the rest of the fields. The version is 2 bits
8260 * in the first byte of the Geneve header. */
8261 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8266 vni
<<= 8; /* VNI is in the upper 3 bytes */
8267 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8276 /* The IPv4 and IPv6 Geneve checks need to do two things:
8277 * - Verify that this actually is Geneve with the right VNI.
8278 * - Place the IP header length (plus variable link prefix if
8279 * needed) into register A to be used later to compute
8280 * the inner packet offsets. */
8281 static struct block
*
8282 gen_geneve4(int vni
)
8284 struct block
*b0
, *b1
;
8285 struct slist
*s
, *s1
;
8287 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8289 /* Load the IP header length into A. */
8290 s
= gen_loadx_iphdrlen();
8292 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8295 /* Forcibly append these statements to the true condition
8296 * of the protocol check by creating a new block that is
8297 * always true and ANDing them. */
8298 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8307 static struct block
*
8308 gen_geneve6(int vni
)
8310 struct block
*b0
, *b1
;
8311 struct slist
*s
, *s1
;
8313 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8315 /* Load the IP header length. We need to account for a
8316 * variable length link prefix if there is one. */
8317 s
= gen_abs_offset_varpart(&off_linkpl
);
8319 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8323 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8327 s
= new_stmt(BPF_LD
|BPF_IMM
);
8331 /* Forcibly append these statements to the true condition
8332 * of the protocol check by creating a new block that is
8333 * always true and ANDing them. */
8334 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8337 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8346 /* We need to store three values based on the Geneve header::
8347 * - The offset of the linktype.
8348 * - The offset of the end of the Geneve header.
8349 * - The offset of the end of the encapsulated MAC header. */
8350 static struct slist
*
8351 gen_geneve_offsets(void)
8353 struct slist
*s
, *s1
, *s_proto
;
8355 /* First we need to calculate the offset of the Geneve header
8356 * itself. This is composed of the IP header previously calculated
8357 * (include any variable link prefix) and stored in A plus the
8358 * fixed sized headers (fixed link prefix, MAC length, and UDP
8360 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8361 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8363 /* Stash this in X since we'll need it later. */
8364 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8367 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8369 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8373 off_linktype
.reg
= alloc_reg();
8374 off_linktype
.is_variable
= 1;
8375 off_linktype
.constant_part
= 0;
8377 s1
= new_stmt(BPF_ST
);
8378 s1
->s
.k
= off_linktype
.reg
;
8381 /* Load the Geneve option length and mask and shift to get the
8382 * number of bytes. It is stored in the first byte of the Geneve
8384 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8388 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8392 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8396 /* Add in the rest of the Geneve base header. */
8397 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8401 /* Add the Geneve header length to its offset and store. */
8402 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8406 /* Set the encapsulated type as Ethernet. Even though we may
8407 * not actually have Ethernet inside there are two reasons this
8409 * - The linktype field is always in EtherType format regardless
8410 * of whether it is in Geneve or an inner Ethernet frame.
8411 * - The only link layer that we have specific support for is
8412 * Ethernet. We will confirm that the packet actually is
8413 * Ethernet at runtime before executing these checks. */
8414 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8416 s1
= new_stmt(BPF_ST
);
8417 s1
->s
.k
= off_linkhdr
.reg
;
8420 /* Calculate whether we have an Ethernet header or just raw IP/
8421 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8422 * and linktype by 14 bytes so that the network header can be found
8423 * seamlessly. Otherwise, keep what we've calculated already. */
8425 /* We have a bare jmp so we can't use the optimizer. */
8428 /* Load the EtherType in the Geneve header, 2 bytes in. */
8429 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8433 /* Load X with the end of the Geneve header. */
8434 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8435 s1
->s
.k
= off_linkhdr
.reg
;
8438 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8439 * end of this check, we should have the total length in X. In
8440 * the non-Ethernet case, it's already there. */
8441 s_proto
= new_stmt(JMP(BPF_JEQ
));
8442 s_proto
->s
.k
= ETHERTYPE_TEB
;
8443 sappend(s
, s_proto
);
8445 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8449 /* Since this is Ethernet, use the EtherType of the payload
8450 * directly as the linktype. Overwrite what we already have. */
8451 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8455 s1
= new_stmt(BPF_ST
);
8456 s1
->s
.k
= off_linktype
.reg
;
8459 /* Advance two bytes further to get the end of the Ethernet
8461 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8465 /* Move the result to X. */
8466 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8469 /* Store the final result of our linkpl calculation. */
8470 off_linkpl
.reg
= alloc_reg();
8471 off_linkpl
.is_variable
= 1;
8472 off_linkpl
.constant_part
= 0;
8474 s1
= new_stmt(BPF_STX
);
8475 s1
->s
.k
= off_linkpl
.reg
;
8484 /* Check to see if this is a Geneve packet. */
8488 struct block
*b0
, *b1
;
8491 b0
= gen_geneve4(vni
);
8492 b1
= gen_geneve6(vni
);
8497 /* Later filters should act on the payload of the Geneve frame,
8498 * update all of the header pointers. Attach this code so that
8499 * it gets executed in the event that the Geneve filter matches. */
8500 s
= gen_geneve_offsets();
8503 sappend(s
, b1
->stmts
);
8513 /* Check that the encapsulated frame has a link layer header
8514 * for Ethernet filters. */
8515 static struct block
*
8516 gen_geneve_ll_check()
8519 struct slist
*s
, *s1
;
8521 /* The easiest way to see if there is a link layer present
8522 * is to check if the link layer header and payload are not
8525 /* Geneve always generates pure variable offsets so we can
8526 * compare only the registers. */
8527 s
= new_stmt(BPF_LD
|BPF_MEM
);
8528 s
->s
.k
= off_linkhdr
.reg
;
8530 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8531 s1
->s
.k
= off_linkpl
.reg
;
8534 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8543 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8555 bpf_error("'vpi' supported only on raw ATM");
8556 if (off_vpi
== (u_int
)-1)
8558 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8564 bpf_error("'vci' supported only on raw ATM");
8565 if (off_vci
== (u_int
)-1)
8567 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8572 if (off_proto
== (u_int
)-1)
8573 abort(); /* XXX - this isn't on FreeBSD */
8574 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8579 if (off_payload
== (u_int
)-1)
8581 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8582 0xffffffff, jtype
, reverse
, jvalue
);
8587 bpf_error("'callref' supported only on raw ATM");
8588 if (off_proto
== (u_int
)-1)
8590 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8591 jtype
, reverse
, jvalue
);
8601 gen_atmtype_abbrev(type
)
8604 struct block
*b0
, *b1
;
8609 /* Get all packets in Meta signalling Circuit */
8611 bpf_error("'metac' supported only on raw ATM");
8612 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8613 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8618 /* Get all packets in Broadcast Circuit*/
8620 bpf_error("'bcc' supported only on raw ATM");
8621 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8622 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8627 /* Get all cells in Segment OAM F4 circuit*/
8629 bpf_error("'oam4sc' supported only on raw ATM");
8630 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8631 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8636 /* Get all cells in End-to-End OAM F4 Circuit*/
8638 bpf_error("'oam4ec' supported only on raw ATM");
8639 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8640 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8645 /* Get all packets in connection Signalling Circuit */
8647 bpf_error("'sc' supported only on raw ATM");
8648 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8649 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8654 /* Get all packets in ILMI Circuit */
8656 bpf_error("'ilmic' supported only on raw ATM");
8657 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8658 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8663 /* Get all LANE packets */
8665 bpf_error("'lane' supported only on raw ATM");
8666 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8669 * Arrange that all subsequent tests assume LANE
8670 * rather than LLC-encapsulated packets, and set
8671 * the offsets appropriately for LANE-encapsulated
8674 * We assume LANE means Ethernet, not Token Ring.
8676 PUSH_LINKHDR(DLT_EN10MB
, 0,
8677 off_payload
+ 2, /* Ethernet header */
8679 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8680 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8681 off_nl
= 0; /* Ethernet II */
8682 off_nl_nosnap
= 3; /* 802.3+802.2 */
8686 /* Get all LLC-encapsulated packets */
8688 bpf_error("'llc' supported only on raw ATM");
8689 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8690 linktype
= prevlinktype
;
8700 * Filtering for MTP2 messages based on li value
8701 * FISU, length is null
8702 * LSSU, length is 1 or 2
8703 * MSU, length is 3 or more
8704 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8707 gen_mtp2type_abbrev(type
)
8710 struct block
*b0
, *b1
;
8715 if ( (linktype
!= DLT_MTP2
) &&
8716 (linktype
!= DLT_ERF
) &&
8717 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8718 bpf_error("'fisu' supported only on MTP2");
8719 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8720 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8724 if ( (linktype
!= DLT_MTP2
) &&
8725 (linktype
!= DLT_ERF
) &&
8726 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8727 bpf_error("'lssu' supported only on MTP2");
8728 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8729 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8734 if ( (linktype
!= DLT_MTP2
) &&
8735 (linktype
!= DLT_ERF
) &&
8736 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8737 bpf_error("'msu' supported only on MTP2");
8738 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8742 if ( (linktype
!= DLT_MTP2
) &&
8743 (linktype
!= DLT_ERF
) &&
8744 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8745 bpf_error("'hfisu' supported only on MTP2_HSL");
8746 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8747 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8751 if ( (linktype
!= DLT_MTP2
) &&
8752 (linktype
!= DLT_ERF
) &&
8753 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8754 bpf_error("'hlssu' supported only on MTP2_HSL");
8755 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8756 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8761 if ( (linktype
!= DLT_MTP2
) &&
8762 (linktype
!= DLT_ERF
) &&
8763 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8764 bpf_error("'hmsu' supported only on MTP2_HSL");
8765 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8775 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8782 bpf_u_int32 val1
, val2
, val3
;
8783 u_int newoff_sio
=off_sio
;
8784 u_int newoff_opc
=off_opc
;
8785 u_int newoff_dpc
=off_dpc
;
8786 u_int newoff_sls
=off_sls
;
8788 switch (mtp3field
) {
8791 newoff_sio
+= 3; /* offset for MTP2_HSL */
8795 if (off_sio
== (u_int
)-1)
8796 bpf_error("'sio' supported only on SS7");
8797 /* sio coded on 1 byte so max value 255 */
8799 bpf_error("sio value %u too big; max value = 255",
8801 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8802 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8808 if (off_opc
== (u_int
)-1)
8809 bpf_error("'opc' supported only on SS7");
8810 /* opc coded on 14 bits so max value 16383 */
8812 bpf_error("opc value %u too big; max value = 16383",
8814 /* the following instructions are made to convert jvalue
8815 * to the form used to write opc in an ss7 message*/
8816 val1
= jvalue
& 0x00003c00;
8818 val2
= jvalue
& 0x000003fc;
8820 val3
= jvalue
& 0x00000003;
8822 jvalue
= val1
+ val2
+ val3
;
8823 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8824 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8832 if (off_dpc
== (u_int
)-1)
8833 bpf_error("'dpc' supported only on SS7");
8834 /* dpc coded on 14 bits so max value 16383 */
8836 bpf_error("dpc value %u too big; max value = 16383",
8838 /* the following instructions are made to convert jvalue
8839 * to the forme used to write dpc in an ss7 message*/
8840 val1
= jvalue
& 0x000000ff;
8842 val2
= jvalue
& 0x00003f00;
8844 jvalue
= val1
+ val2
;
8845 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8846 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8852 if (off_sls
== (u_int
)-1)
8853 bpf_error("'sls' supported only on SS7");
8854 /* sls coded on 4 bits so max value 15 */
8856 bpf_error("sls value %u too big; max value = 15",
8858 /* the following instruction is made to convert jvalue
8859 * to the forme used to write sls in an ss7 message*/
8860 jvalue
= jvalue
<< 4;
8861 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8862 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8871 static struct block
*
8872 gen_msg_abbrev(type
)
8878 * Q.2931 signalling protocol messages for handling virtual circuits
8879 * establishment and teardown
8884 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8888 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8892 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8896 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8900 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8903 case A_RELEASE_DONE
:
8904 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8914 gen_atmmulti_abbrev(type
)
8917 struct block
*b0
, *b1
;
8923 bpf_error("'oam' supported only on raw ATM");
8924 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8929 bpf_error("'oamf4' supported only on raw ATM");
8931 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8932 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8934 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8940 * Get Q.2931 signalling messages for switched
8941 * virtual connection
8944 bpf_error("'connectmsg' supported only on raw ATM");
8945 b0
= gen_msg_abbrev(A_SETUP
);
8946 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8948 b0
= gen_msg_abbrev(A_CONNECT
);
8950 b0
= gen_msg_abbrev(A_CONNECTACK
);
8952 b0
= gen_msg_abbrev(A_RELEASE
);
8954 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8956 b0
= gen_atmtype_abbrev(A_SC
);
8962 bpf_error("'metaconnect' supported only on raw ATM");
8963 b0
= gen_msg_abbrev(A_SETUP
);
8964 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8966 b0
= gen_msg_abbrev(A_CONNECT
);
8968 b0
= gen_msg_abbrev(A_RELEASE
);
8970 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8972 b0
= gen_atmtype_abbrev(A_METAC
);