1 /*#define CHASE_CHAIN*/
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that: (1) source code distributions
8 * retain the above copyright notice and this paragraph in its entirety, (2)
9 * distributions including binary code include the above copyright notice and
10 * this paragraph in its entirety in the documentation or other materials
11 * provided with the distribution, and (3) all advertising materials mentioning
12 * features or use of this software display the following acknowledgement:
13 * ``This product includes software developed by the University of California,
14 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
15 * the University nor the names of its contributors may be used to endorse
16 * or promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
19 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #include <pcap-stdinc.h>
35 #ifdef HAVE_SYS_BITYPES_H
36 #include <sys/bitypes.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include <sys/param.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
65 #include "ethertype.h"
69 #include "ieee80211.h"
71 #include "sunatmpos.h"
74 #include "pcap/ipnet.h"
77 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
78 #include <linux/types.h>
79 #include <linux/if_packet.h>
80 #include <linux/filter.h>
83 #ifdef HAVE_NET_PFVAR_H
84 #include <sys/socket.h>
86 #include <net/pfvar.h>
87 #include <net/if_pflog.h>
91 #define offsetof(s, e) ((size_t)&((s *)0)->e)
96 #if defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF)
102 u_int8_t u6_addr8
[16];
103 u_int16_t u6_addr16
[8];
104 u_int32_t u6_addr32
[4];
106 #define s6_addr in6_u.u6_addr8
107 #define s6_addr16 in6_u.u6_addr16
108 #define s6_addr32 in6_u.u6_addr32
109 #define s6_addr64 in6_u.u6_addr64
112 typedef unsigned short sa_family_t
;
114 #define __SOCKADDR_COMMON(sa_prefix) \
115 sa_family_t sa_prefix##family
117 /* Ditto, for IPv6. */
120 __SOCKADDR_COMMON (sin6_
);
121 u_int16_t sin6_port
; /* Transport layer port # */
122 u_int32_t sin6_flowinfo
; /* IPv6 flow information */
123 struct in6_addr sin6_addr
; /* IPv6 address */
126 #ifndef EAI_ADDRFAMILY
128 int ai_flags
; /* AI_PASSIVE, AI_CANONNAME */
129 int ai_family
; /* PF_xxx */
130 int ai_socktype
; /* SOCK_xxx */
131 int ai_protocol
; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
132 size_t ai_addrlen
; /* length of ai_addr */
133 char *ai_canonname
; /* canonical name for hostname */
134 struct sockaddr
*ai_addr
; /* binary address */
135 struct addrinfo
*ai_next
; /* next structure in linked list */
137 #endif /* EAI_ADDRFAMILY */
138 #endif /* defined(__MINGW32__) && defined(DEFINE_ADDITIONAL_IPV6_STUFF) */
140 #include <netdb.h> /* for "struct addrinfo" */
143 #include <pcap/namedb.h>
145 #define ETHERMTU 1500
147 #ifndef ETHERTYPE_TEB
148 #define ETHERTYPE_TEB 0x6558
151 #ifndef IPPROTO_HOPOPTS
152 #define IPPROTO_HOPOPTS 0
154 #ifndef IPPROTO_ROUTING
155 #define IPPROTO_ROUTING 43
157 #ifndef IPPROTO_FRAGMENT
158 #define IPPROTO_FRAGMENT 44
160 #ifndef IPPROTO_DSTOPTS
161 #define IPPROTO_DSTOPTS 60
164 #define IPPROTO_SCTP 132
167 #define GENEVE_PORT 6081
169 #ifdef HAVE_OS_PROTO_H
170 #include "os-proto.h"
173 #define JMP(c) ((c)|BPF_JMP|BPF_K)
176 static jmp_buf top_ctx
;
177 static pcap_t
*bpf_pcap
;
179 /* Hack for handling VLAN and MPLS stacks. */
181 static u_int label_stack_depth
= (u_int
)-1, vlan_stack_depth
= (u_int
)-1;
183 static u_int label_stack_depth
= -1U, vlan_stack_depth
= -1U;
187 static int pcap_fddipad
;
191 bpf_error(const char *fmt
, ...)
196 if (bpf_pcap
!= NULL
)
197 (void)pcap_vsnprintf(pcap_geterr(bpf_pcap
), PCAP_ERRBUF_SIZE
,
204 static void init_linktype(pcap_t
*);
206 static void init_regs(void);
207 static int alloc_reg(void);
208 static void free_reg(int);
210 static struct block
*root
;
213 * Absolute offsets, which are offsets from the beginning of the raw
214 * packet data, are, in the general case, the sum of a variable value
215 * and a constant value; the variable value may be absent, in which
216 * case the offset is only the constant value, and the constant value
217 * may be zero, in which case the offset is only the variable value.
219 * bpf_abs_offset is a structure containing all that information:
221 * is_variable is 1 if there's a variable part.
223 * constant_part is the constant part of the value, possibly zero;
225 * if is_variable is 1, reg is the register number for a register
226 * containing the variable value if the register has been assigned,
236 * Value passed to gen_load_a() to indicate what the offset argument
237 * is relative to the beginning of.
240 OR_PACKET
, /* full packet data */
241 OR_LINKHDR
, /* link-layer header */
242 OR_PREVLINKHDR
, /* previous link-layer header */
243 OR_LLC
, /* 802.2 LLC header */
244 OR_PREVMPLSHDR
, /* previous MPLS header */
245 OR_LINKTYPE
, /* link-layer type */
246 OR_LINKPL
, /* link-layer payload */
247 OR_LINKPL_NOSNAP
, /* link-layer payload, with no SNAP header at the link layer */
248 OR_TRAN_IPV4
, /* transport-layer header, with IPv4 network layer */
249 OR_TRAN_IPV6
/* transport-layer header, with IPv6 network layer */
254 * As errors are handled by a longjmp, anything allocated must be freed
255 * in the longjmp handler, so it must be reachable from that handler.
256 * One thing that's allocated is the result of pcap_nametoaddrinfo();
257 * it must be freed with freeaddrinfo(). This variable points to any
258 * addrinfo structure that would need to be freed.
260 static struct addrinfo
*ai
;
264 * We divy out chunks of memory rather than call malloc each time so
265 * we don't have to worry about leaking memory. It's probably
266 * not a big deal if all this memory was wasted but if this ever
267 * goes into a library that would probably not be a good idea.
269 * XXX - this *is* in a library....
272 #define CHUNK0SIZE 1024
278 static struct chunk chunks
[NCHUNKS
];
279 static int cur_chunk
;
281 static void *newchunk(size_t);
282 static void freechunks(void);
283 static inline struct block
*new_block(int);
284 static inline struct slist
*new_stmt(int);
285 static struct block
*gen_retblk(int);
286 static inline void syntax(void);
288 static void backpatch(struct block
*, struct block
*);
289 static void merge(struct block
*, struct block
*);
290 static struct block
*gen_cmp(enum e_offrel
, u_int
, u_int
, bpf_int32
);
291 static struct block
*gen_cmp_gt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
292 static struct block
*gen_cmp_ge(enum e_offrel
, u_int
, u_int
, bpf_int32
);
293 static struct block
*gen_cmp_lt(enum e_offrel
, u_int
, u_int
, bpf_int32
);
294 static struct block
*gen_cmp_le(enum e_offrel
, u_int
, u_int
, bpf_int32
);
295 static struct block
*gen_mcmp(enum e_offrel
, u_int
, u_int
, bpf_int32
,
297 static struct block
*gen_bcmp(enum e_offrel
, u_int
, u_int
, const u_char
*);
298 static struct block
*gen_ncmp(enum e_offrel
, bpf_u_int32
, bpf_u_int32
,
299 bpf_u_int32
, bpf_u_int32
, int, bpf_int32
);
300 static struct slist
*gen_load_absoffsetrel(bpf_abs_offset
*, u_int
, u_int
);
301 static struct slist
*gen_load_a(enum e_offrel
, u_int
, u_int
);
302 static struct slist
*gen_loadx_iphdrlen(void);
303 static struct block
*gen_uncond(int);
304 static inline struct block
*gen_true(void);
305 static inline struct block
*gen_false(void);
306 static struct block
*gen_ether_linktype(int);
307 static struct block
*gen_ipnet_linktype(int);
308 static struct block
*gen_linux_sll_linktype(int);
309 static struct slist
*gen_load_prism_llprefixlen(void);
310 static struct slist
*gen_load_avs_llprefixlen(void);
311 static struct slist
*gen_load_radiotap_llprefixlen(void);
312 static struct slist
*gen_load_ppi_llprefixlen(void);
313 static void insert_compute_vloffsets(struct block
*);
314 static struct slist
*gen_abs_offset_varpart(bpf_abs_offset
*);
315 static int ethertype_to_ppptype(int);
316 static struct block
*gen_linktype(int);
317 static struct block
*gen_snap(bpf_u_int32
, bpf_u_int32
);
318 static struct block
*gen_llc_linktype(int);
319 static struct block
*gen_hostop(bpf_u_int32
, bpf_u_int32
, int, int, u_int
, u_int
);
321 static struct block
*gen_hostop6(struct in6_addr
*, struct in6_addr
*, int, int, u_int
, u_int
);
323 static struct block
*gen_ahostop(const u_char
*, int);
324 static struct block
*gen_ehostop(const u_char
*, int);
325 static struct block
*gen_fhostop(const u_char
*, int);
326 static struct block
*gen_thostop(const u_char
*, int);
327 static struct block
*gen_wlanhostop(const u_char
*, int);
328 static struct block
*gen_ipfchostop(const u_char
*, int);
329 static struct block
*gen_dnhostop(bpf_u_int32
, int);
330 static struct block
*gen_mpls_linktype(int);
331 static struct block
*gen_host(bpf_u_int32
, bpf_u_int32
, int, int, int);
333 static struct block
*gen_host6(struct in6_addr
*, struct in6_addr
*, int, int, int);
336 static struct block
*gen_gateway(const u_char
*, bpf_u_int32
**, int, int);
338 static struct block
*gen_ipfrag(void);
339 static struct block
*gen_portatom(int, bpf_int32
);
340 static struct block
*gen_portrangeatom(int, bpf_int32
, bpf_int32
);
341 static struct block
*gen_portatom6(int, bpf_int32
);
342 static struct block
*gen_portrangeatom6(int, bpf_int32
, bpf_int32
);
343 struct block
*gen_portop(int, int, int);
344 static struct block
*gen_port(int, int, int);
345 struct block
*gen_portrangeop(int, int, int, int);
346 static struct block
*gen_portrange(int, int, int, int);
347 struct block
*gen_portop6(int, int, int);
348 static struct block
*gen_port6(int, int, int);
349 struct block
*gen_portrangeop6(int, int, int, int);
350 static struct block
*gen_portrange6(int, int, int, int);
351 static int lookup_proto(const char *, int);
352 static struct block
*gen_protochain(int, int, int);
353 static struct block
*gen_proto(int, int, int);
354 static struct slist
*xfer_to_x(struct arth
*);
355 static struct slist
*xfer_to_a(struct arth
*);
356 static struct block
*gen_mac_multicast(int);
357 static struct block
*gen_len(int, int);
358 static struct block
*gen_check_802_11_data_frame(void);
359 static struct block
*gen_geneve_ll_check(void);
361 static struct block
*gen_ppi_dlt_check(void);
362 static struct block
*gen_msg_abbrev(int type
);
372 /* XXX Round up to nearest long. */
373 n
= (n
+ sizeof(long) - 1) & ~(sizeof(long) - 1);
375 /* XXX Round up to structure boundary. */
379 cp
= &chunks
[cur_chunk
];
380 if (n
> cp
->n_left
) {
381 ++cp
, k
= ++cur_chunk
;
383 bpf_error("out of memory");
384 size
= CHUNK0SIZE
<< k
;
385 cp
->m
= (void *)malloc(size
);
387 bpf_error("out of memory");
388 memset((char *)cp
->m
, 0, size
);
391 bpf_error("out of memory");
394 return (void *)((char *)cp
->m
+ cp
->n_left
);
403 for (i
= 0; i
< NCHUNKS
; ++i
)
404 if (chunks
[i
].m
!= NULL
) {
411 * A strdup whose allocations are freed after code generation is over.
415 register const char *s
;
417 size_t n
= strlen(s
) + 1;
418 char *cp
= newchunk(n
);
424 static inline struct block
*
430 p
= (struct block
*)newchunk(sizeof(*p
));
437 static inline struct slist
*
443 p
= (struct slist
*)newchunk(sizeof(*p
));
449 static struct block
*
453 struct block
*b
= new_block(BPF_RET
|BPF_K
);
462 bpf_error("syntax error in filter expression");
465 static bpf_u_int32 netmask
;
470 pcap_compile(pcap_t
*p
, struct bpf_program
*program
,
471 const char *buf
, int optimize
, bpf_u_int32 mask
)
474 const char * volatile xbuf
= buf
;
479 * XXX - single-thread this code path with pthread calls on
480 * UN*X, if the platform supports pthreads? If that requires
481 * a separate -lpthread, we might not want to do that.
489 EnterCriticalSection(&g_PcapCompileCriticalSection
);
493 * If this pcap_t hasn't been activated, it doesn't have a
494 * link-layer type, so we can't use it.
497 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
498 "not-yet-activated pcap_t passed to pcap_compile");
508 if (setjmp(top_ctx
)) {
523 snaplen
= pcap_snapshot(p
);
525 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
526 "snaplen of 0 rejects all packets");
531 lex_init(xbuf
? xbuf
: "");
539 root
= gen_retblk(snaplen
);
541 if (optimize
&& !no_optimize
) {
544 (root
->s
.code
== (BPF_RET
|BPF_K
) && root
->s
.k
== 0))
545 bpf_error("expression rejects all packets");
547 program
->bf_insns
= icode_to_fcode(root
, &len
);
548 program
->bf_len
= len
;
554 rc
= 0; /* We're all okay */
559 LeaveCriticalSection(&g_PcapCompileCriticalSection
);
566 * entry point for using the compiler with no pcap open
567 * pass in all the stuff that is needed explicitly instead.
570 pcap_compile_nopcap(int snaplen_arg
, int linktype_arg
,
571 struct bpf_program
*program
,
572 const char *buf
, int optimize
, bpf_u_int32 mask
)
577 p
= pcap_open_dead(linktype_arg
, snaplen_arg
);
580 ret
= pcap_compile(p
, program
, buf
, optimize
, mask
);
586 * Clean up a "struct bpf_program" by freeing all the memory allocated
590 pcap_freecode(struct bpf_program
*program
)
593 if (program
->bf_insns
!= NULL
) {
594 free((char *)program
->bf_insns
);
595 program
->bf_insns
= NULL
;
600 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
601 * which of the jt and jf fields has been resolved and which is a pointer
602 * back to another unresolved block (or nil). At least one of the fields
603 * in each block is already resolved.
606 backpatch(list
, target
)
607 struct block
*list
, *target
;
624 * Merge the lists in b0 and b1, using the 'sense' field to indicate
625 * which of jt and jf is the link.
629 struct block
*b0
, *b1
;
631 register struct block
**p
= &b0
;
633 /* Find end of list. */
635 p
= !((*p
)->sense
) ? &JT(*p
) : &JF(*p
);
637 /* Concatenate the lists. */
645 struct block
*ppi_dlt_check
;
648 * Insert before the statements of the first (root) block any
649 * statements needed to load the lengths of any variable-length
650 * headers into registers.
652 * XXX - a fancier strategy would be to insert those before the
653 * statements of all blocks that use those lengths and that
654 * have no predecessors that use them, so that we only compute
655 * the lengths if we need them. There might be even better
656 * approaches than that.
658 * However, those strategies would be more complicated, and
659 * as we don't generate code to compute a length if the
660 * program has no tests that use the length, and as most
661 * tests will probably use those lengths, we would just
662 * postpone computing the lengths so that it's not done
663 * for tests that fail early, and it's not clear that's
666 insert_compute_vloffsets(p
->head
);
669 * For DLT_PPI captures, generate a check of the per-packet
670 * DLT value to make sure it's DLT_IEEE802_11.
672 * XXX - TurboCap cards use DLT_PPI for Ethernet.
673 * Can we just define some DLT_ETHERNET_WITH_PHDR pseudo-header
674 * with appropriate Ethernet information and use that rather
675 * than using something such as DLT_PPI where you don't know
676 * the link-layer header type until runtime, which, in the
677 * general case, would force us to generate both Ethernet *and*
678 * 802.11 code (*and* anything else for which PPI is used)
679 * and choose between them early in the BPF program?
681 ppi_dlt_check
= gen_ppi_dlt_check();
682 if (ppi_dlt_check
!= NULL
)
683 gen_and(ppi_dlt_check
, p
);
685 backpatch(p
, gen_retblk(snaplen
));
686 p
->sense
= !p
->sense
;
687 backpatch(p
, gen_retblk(0));
693 struct block
*b0
, *b1
;
695 backpatch(b0
, b1
->head
);
696 b0
->sense
= !b0
->sense
;
697 b1
->sense
= !b1
->sense
;
699 b1
->sense
= !b1
->sense
;
705 struct block
*b0
, *b1
;
707 b0
->sense
= !b0
->sense
;
708 backpatch(b0
, b1
->head
);
709 b0
->sense
= !b0
->sense
;
718 b
->sense
= !b
->sense
;
721 static struct block
*
722 gen_cmp(offrel
, offset
, size
, v
)
723 enum e_offrel offrel
;
727 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JEQ
, 0, v
);
730 static struct block
*
731 gen_cmp_gt(offrel
, offset
, size
, v
)
732 enum e_offrel offrel
;
736 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 0, v
);
739 static struct block
*
740 gen_cmp_ge(offrel
, offset
, size
, v
)
741 enum e_offrel offrel
;
745 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 0, v
);
748 static struct block
*
749 gen_cmp_lt(offrel
, offset
, size
, v
)
750 enum e_offrel offrel
;
754 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGE
, 1, v
);
757 static struct block
*
758 gen_cmp_le(offrel
, offset
, size
, v
)
759 enum e_offrel offrel
;
763 return gen_ncmp(offrel
, offset
, size
, 0xffffffff, BPF_JGT
, 1, v
);
766 static struct block
*
767 gen_mcmp(offrel
, offset
, size
, v
, mask
)
768 enum e_offrel offrel
;
773 return gen_ncmp(offrel
, offset
, size
, mask
, BPF_JEQ
, 0, v
);
776 static struct block
*
777 gen_bcmp(offrel
, offset
, size
, v
)
778 enum e_offrel offrel
;
779 register u_int offset
, size
;
780 register const u_char
*v
;
782 register struct block
*b
, *tmp
;
786 register const u_char
*p
= &v
[size
- 4];
787 bpf_int32 w
= ((bpf_int32
)p
[0] << 24) |
788 ((bpf_int32
)p
[1] << 16) | ((bpf_int32
)p
[2] << 8) | p
[3];
790 tmp
= gen_cmp(offrel
, offset
+ size
- 4, BPF_W
, w
);
797 register const u_char
*p
= &v
[size
- 2];
798 bpf_int32 w
= ((bpf_int32
)p
[0] << 8) | p
[1];
800 tmp
= gen_cmp(offrel
, offset
+ size
- 2, BPF_H
, w
);
807 tmp
= gen_cmp(offrel
, offset
, BPF_B
, (bpf_int32
)v
[0]);
816 * AND the field of size "size" at offset "offset" relative to the header
817 * specified by "offrel" with "mask", and compare it with the value "v"
818 * with the test specified by "jtype"; if "reverse" is true, the test
819 * should test the opposite of "jtype".
821 static struct block
*
822 gen_ncmp(offrel
, offset
, size
, mask
, jtype
, reverse
, v
)
823 enum e_offrel offrel
;
825 bpf_u_int32 offset
, size
, mask
, jtype
;
828 struct slist
*s
, *s2
;
831 s
= gen_load_a(offrel
, offset
, size
);
833 if (mask
!= 0xffffffff) {
834 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
839 b
= new_block(JMP(jtype
));
842 if (reverse
&& (jtype
== BPF_JGT
|| jtype
== BPF_JGE
))
848 * Various code constructs need to know the layout of the packet.
849 * These variables give the necessary offsets from the beginning
850 * of the packet data.
854 * Absolute offset of the beginning of the link-layer header.
856 static bpf_abs_offset off_linkhdr
;
859 * If we're checking a link-layer header for a packet encapsulated in
860 * another protocol layer, this is the equivalent information for the
861 * previous layers' link-layer header from the beginning of the raw
864 static bpf_abs_offset off_prevlinkhdr
;
867 * This is the equivalent information for the outermost layers' link-layer
870 static bpf_abs_offset off_outermostlinkhdr
;
873 * "Push" the current value of the link-layer header type and link-layer
874 * header offset onto a "stack", and set a new value. (It's not a
875 * full-blown stack; we keep only the top two items.)
877 #define PUSH_LINKHDR(new_linktype, new_is_variable, new_constant_part, new_reg) \
879 prevlinktype = new_linktype; \
880 off_prevlinkhdr = off_linkhdr; \
881 linktype = new_linktype; \
882 off_linkhdr.is_variable = new_is_variable; \
883 off_linkhdr.constant_part = new_constant_part; \
884 off_linkhdr.reg = new_reg; \
889 * Absolute offset of the beginning of the link-layer payload.
891 static bpf_abs_offset off_linkpl
;
894 * "off_linktype" is the offset to information in the link-layer header
895 * giving the packet type. This is an absolute offset from the beginning
898 * For Ethernet, it's the offset of the Ethernet type field; this
899 * means that it must have a value that skips VLAN tags.
901 * For link-layer types that always use 802.2 headers, it's the
902 * offset of the LLC header; this means that it must have a value
903 * that skips VLAN tags.
905 * For PPP, it's the offset of the PPP type field.
907 * For Cisco HDLC, it's the offset of the CHDLC type field.
909 * For BSD loopback, it's the offset of the AF_ value.
911 * For Linux cooked sockets, it's the offset of the type field.
913 * off_linktype.constant_part is set to -1 for no encapsulation,
914 * in which case, IP is assumed.
916 static bpf_abs_offset off_linktype
;
919 * TRUE if the link layer includes an ATM pseudo-header.
921 static int is_atm
= 0;
924 * TRUE if "geneve" appeared in the filter; it causes us to generate
925 * code that checks for a Geneve header and assume that later filters
926 * apply to the encapsulated payload.
928 static int is_geneve
= 0;
931 * These are offsets for the ATM pseudo-header.
933 static u_int off_vpi
;
934 static u_int off_vci
;
935 static u_int off_proto
;
938 * These are offsets for the MTP2 fields.
941 static u_int off_li_hsl
;
944 * These are offsets for the MTP3 fields.
946 static u_int off_sio
;
947 static u_int off_opc
;
948 static u_int off_dpc
;
949 static u_int off_sls
;
952 * This is the offset of the first byte after the ATM pseudo_header,
953 * or -1 if there is no ATM pseudo-header.
955 static u_int off_payload
;
958 * These are offsets to the beginning of the network-layer header.
959 * They are relative to the beginning of the link-layer payload (i.e.,
960 * they don't include off_linkhdr.constant_part or off_linkpl.constant_part).
962 * If the link layer never uses 802.2 LLC:
964 * "off_nl" and "off_nl_nosnap" are the same.
966 * If the link layer always uses 802.2 LLC:
968 * "off_nl" is the offset if there's a SNAP header following
971 * "off_nl_nosnap" is the offset if there's no SNAP header.
973 * If the link layer is Ethernet:
975 * "off_nl" is the offset if the packet is an Ethernet II packet
976 * (we assume no 802.3+802.2+SNAP);
978 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
979 * with an 802.2 header following it.
982 static u_int off_nl_nosnap
;
985 static int prevlinktype
;
986 static int outermostlinktype
;
992 pcap_fddipad
= p
->fddipad
;
995 * We start out with only one link-layer header.
997 outermostlinktype
= pcap_datalink(p
);
998 off_outermostlinkhdr
.constant_part
= 0;
999 off_outermostlinkhdr
.is_variable
= 0;
1000 off_outermostlinkhdr
.reg
= -1;
1002 prevlinktype
= outermostlinktype
;
1003 off_prevlinkhdr
.constant_part
= 0;
1004 off_prevlinkhdr
.is_variable
= 0;
1005 off_prevlinkhdr
.reg
= -1;
1007 linktype
= outermostlinktype
;
1008 off_linkhdr
.constant_part
= 0;
1009 off_linkhdr
.is_variable
= 0;
1010 off_linkhdr
.reg
= -1;
1015 off_linkpl
.constant_part
= 0;
1016 off_linkpl
.is_variable
= 0;
1017 off_linkpl
.reg
= -1;
1019 off_linktype
.constant_part
= 0;
1020 off_linktype
.is_variable
= 0;
1021 off_linktype
.reg
= -1;
1024 * Assume it's not raw ATM with a pseudo-header, for now.
1038 * And assume we're not doing SS7.
1047 label_stack_depth
= 0;
1048 vlan_stack_depth
= 0;
1053 off_linktype
.constant_part
= 2;
1054 off_linkpl
.constant_part
= 6;
1055 off_nl
= 0; /* XXX in reality, variable! */
1056 off_nl_nosnap
= 0; /* no 802.2 LLC */
1059 case DLT_ARCNET_LINUX
:
1060 off_linktype
.constant_part
= 4;
1061 off_linkpl
.constant_part
= 8;
1062 off_nl
= 0; /* XXX in reality, variable! */
1063 off_nl_nosnap
= 0; /* no 802.2 LLC */
1067 off_linktype
.constant_part
= 12;
1068 off_linkpl
.constant_part
= 14; /* Ethernet header length */
1069 off_nl
= 0; /* Ethernet II */
1070 off_nl_nosnap
= 3; /* 802.3+802.2 */
1075 * SLIP doesn't have a link level type. The 16 byte
1076 * header is hacked into our SLIP driver.
1078 off_linktype
.constant_part
= -1;
1079 off_linkpl
.constant_part
= 16;
1081 off_nl_nosnap
= 0; /* no 802.2 LLC */
1084 case DLT_SLIP_BSDOS
:
1085 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1086 off_linktype
.constant_part
= -1;
1088 off_linkpl
.constant_part
= 24;
1090 off_nl_nosnap
= 0; /* no 802.2 LLC */
1095 off_linktype
.constant_part
= 0;
1096 off_linkpl
.constant_part
= 4;
1098 off_nl_nosnap
= 0; /* no 802.2 LLC */
1102 off_linktype
.constant_part
= 0;
1103 off_linkpl
.constant_part
= 12;
1105 off_nl_nosnap
= 0; /* no 802.2 LLC */
1110 case DLT_C_HDLC
: /* BSD/OS Cisco HDLC */
1111 case DLT_PPP_SERIAL
: /* NetBSD sync/async serial PPP */
1112 off_linktype
.constant_part
= 2; /* skip HDLC-like framing */
1113 off_linkpl
.constant_part
= 4; /* skip HDLC-like framing and protocol field */
1115 off_nl_nosnap
= 0; /* no 802.2 LLC */
1120 * This does no include the Ethernet header, and
1121 * only covers session state.
1123 off_linktype
.constant_part
= 6;
1124 off_linkpl
.constant_part
= 8;
1126 off_nl_nosnap
= 0; /* no 802.2 LLC */
1130 off_linktype
.constant_part
= 5;
1131 off_linkpl
.constant_part
= 24;
1133 off_nl_nosnap
= 0; /* no 802.2 LLC */
1138 * FDDI doesn't really have a link-level type field.
1139 * We set "off_linktype" to the offset of the LLC header.
1141 * To check for Ethernet types, we assume that SSAP = SNAP
1142 * is being used and pick out the encapsulated Ethernet type.
1143 * XXX - should we generate code to check for SNAP?
1145 off_linktype
.constant_part
= 13;
1146 off_linktype
.constant_part
+= pcap_fddipad
;
1147 off_linkpl
.constant_part
= 13; /* FDDI MAC header length */
1148 off_linkpl
.constant_part
+= pcap_fddipad
;
1149 off_nl
= 8; /* 802.2+SNAP */
1150 off_nl_nosnap
= 3; /* 802.2 */
1155 * Token Ring doesn't really have a link-level type field.
1156 * We set "off_linktype" to the offset of the LLC header.
1158 * To check for Ethernet types, we assume that SSAP = SNAP
1159 * is being used and pick out the encapsulated Ethernet type.
1160 * XXX - should we generate code to check for SNAP?
1162 * XXX - the header is actually variable-length.
1163 * Some various Linux patched versions gave 38
1164 * as "off_linktype" and 40 as "off_nl"; however,
1165 * if a token ring packet has *no* routing
1166 * information, i.e. is not source-routed, the correct
1167 * values are 20 and 22, as they are in the vanilla code.
1169 * A packet is source-routed iff the uppermost bit
1170 * of the first byte of the source address, at an
1171 * offset of 8, has the uppermost bit set. If the
1172 * packet is source-routed, the total number of bytes
1173 * of routing information is 2 plus bits 0x1F00 of
1174 * the 16-bit value at an offset of 14 (shifted right
1175 * 8 - figure out which byte that is).
1177 off_linktype
.constant_part
= 14;
1178 off_linkpl
.constant_part
= 14; /* Token Ring MAC header length */
1179 off_nl
= 8; /* 802.2+SNAP */
1180 off_nl_nosnap
= 3; /* 802.2 */
1183 case DLT_PRISM_HEADER
:
1184 case DLT_IEEE802_11_RADIO_AVS
:
1185 case DLT_IEEE802_11_RADIO
:
1186 off_linkhdr
.is_variable
= 1;
1187 /* Fall through, 802.11 doesn't have a variable link
1188 * prefix but is otherwise the same. */
1190 case DLT_IEEE802_11
:
1192 * 802.11 doesn't really have a link-level type field.
1193 * We set "off_linktype.constant_part" to the offset of
1196 * To check for Ethernet types, we assume that SSAP = SNAP
1197 * is being used and pick out the encapsulated Ethernet type.
1198 * XXX - should we generate code to check for SNAP?
1200 * We also handle variable-length radio headers here.
1201 * The Prism header is in theory variable-length, but in
1202 * practice it's always 144 bytes long. However, some
1203 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1204 * sometimes or always supply an AVS header, so we
1205 * have to check whether the radio header is a Prism
1206 * header or an AVS header, so, in practice, it's
1209 off_linktype
.constant_part
= 24;
1210 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1211 off_linkpl
.is_variable
= 1;
1212 off_nl
= 8; /* 802.2+SNAP */
1213 off_nl_nosnap
= 3; /* 802.2 */
1218 * At the moment we treat PPI the same way that we treat
1219 * normal Radiotap encoded packets. The difference is in
1220 * the function that generates the code at the beginning
1221 * to compute the header length. Since this code generator
1222 * of PPI supports bare 802.11 encapsulation only (i.e.
1223 * the encapsulated DLT should be DLT_IEEE802_11) we
1224 * generate code to check for this too.
1226 off_linktype
.constant_part
= 24;
1227 off_linkpl
.constant_part
= 0; /* link-layer header is variable-length */
1228 off_linkpl
.is_variable
= 1;
1229 off_linkhdr
.is_variable
= 1;
1230 off_nl
= 8; /* 802.2+SNAP */
1231 off_nl_nosnap
= 3; /* 802.2 */
1234 case DLT_ATM_RFC1483
:
1235 case DLT_ATM_CLIP
: /* Linux ATM defines this */
1237 * assume routed, non-ISO PDUs
1238 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1240 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1241 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1242 * latter would presumably be treated the way PPPoE
1243 * should be, so you can do "pppoe and udp port 2049"
1244 * or "pppoa and tcp port 80" and have it check for
1245 * PPPo{A,E} and a PPP protocol of IP and....
1247 off_linktype
.constant_part
= 0;
1248 off_linkpl
.constant_part
= 0; /* packet begins with LLC header */
1249 off_nl
= 8; /* 802.2+SNAP */
1250 off_nl_nosnap
= 3; /* 802.2 */
1255 * Full Frontal ATM; you get AALn PDUs with an ATM
1259 off_vpi
= SUNATM_VPI_POS
;
1260 off_vci
= SUNATM_VCI_POS
;
1261 off_proto
= PROTO_POS
;
1262 off_payload
= SUNATM_PKT_BEGIN_POS
;
1263 off_linktype
.constant_part
= off_payload
;
1264 off_linkpl
.constant_part
= off_payload
; /* if LLC-encapsulated */
1265 off_nl
= 8; /* 802.2+SNAP */
1266 off_nl_nosnap
= 3; /* 802.2 */
1272 off_linktype
.constant_part
= -1;
1273 off_linkpl
.constant_part
= 0;
1275 off_nl_nosnap
= 0; /* no 802.2 LLC */
1278 case DLT_LINUX_SLL
: /* fake header for Linux cooked socket */
1279 off_linktype
.constant_part
= 14;
1280 off_linkpl
.constant_part
= 16;
1282 off_nl_nosnap
= 0; /* no 802.2 LLC */
1287 * LocalTalk does have a 1-byte type field in the LLAP header,
1288 * but really it just indicates whether there is a "short" or
1289 * "long" DDP packet following.
1291 off_linktype
.constant_part
= -1;
1292 off_linkpl
.constant_part
= 0;
1294 off_nl_nosnap
= 0; /* no 802.2 LLC */
1297 case DLT_IP_OVER_FC
:
1299 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1300 * link-level type field. We set "off_linktype" to the
1301 * offset of the LLC header.
1303 * To check for Ethernet types, we assume that SSAP = SNAP
1304 * is being used and pick out the encapsulated Ethernet type.
1305 * XXX - should we generate code to check for SNAP? RFC
1306 * 2625 says SNAP should be used.
1308 off_linktype
.constant_part
= 16;
1309 off_linkpl
.constant_part
= 16;
1310 off_nl
= 8; /* 802.2+SNAP */
1311 off_nl_nosnap
= 3; /* 802.2 */
1316 * XXX - we should set this to handle SNAP-encapsulated
1317 * frames (NLPID of 0x80).
1319 off_linktype
.constant_part
= -1;
1320 off_linkpl
.constant_part
= 0;
1322 off_nl_nosnap
= 0; /* no 802.2 LLC */
1326 * the only BPF-interesting FRF.16 frames are non-control frames;
1327 * Frame Relay has a variable length link-layer
1328 * so lets start with offset 4 for now and increments later on (FIXME);
1331 off_linktype
.constant_part
= -1;
1332 off_linkpl
.constant_part
= 0;
1334 off_nl_nosnap
= 0; /* XXX - for now -> no 802.2 LLC */
1337 case DLT_APPLE_IP_OVER_IEEE1394
:
1338 off_linktype
.constant_part
= 16;
1339 off_linkpl
.constant_part
= 18;
1341 off_nl_nosnap
= 0; /* no 802.2 LLC */
1344 case DLT_SYMANTEC_FIREWALL
:
1345 off_linktype
.constant_part
= 6;
1346 off_linkpl
.constant_part
= 44;
1347 off_nl
= 0; /* Ethernet II */
1348 off_nl_nosnap
= 0; /* XXX - what does it do with 802.3 packets? */
1351 #ifdef HAVE_NET_PFVAR_H
1353 off_linktype
.constant_part
= 0;
1354 off_linkpl
.constant_part
= PFLOG_HDRLEN
;
1356 off_nl_nosnap
= 0; /* no 802.2 LLC */
1360 case DLT_JUNIPER_MFR
:
1361 case DLT_JUNIPER_MLFR
:
1362 case DLT_JUNIPER_MLPPP
:
1363 case DLT_JUNIPER_PPP
:
1364 case DLT_JUNIPER_CHDLC
:
1365 case DLT_JUNIPER_FRELAY
:
1366 off_linktype
.constant_part
= 4;
1367 off_linkpl
.constant_part
= 4;
1369 off_nl_nosnap
= -1; /* no 802.2 LLC */
1372 case DLT_JUNIPER_ATM1
:
1373 off_linktype
.constant_part
= 4; /* in reality variable between 4-8 */
1374 off_linkpl
.constant_part
= 4; /* in reality variable between 4-8 */
1379 case DLT_JUNIPER_ATM2
:
1380 off_linktype
.constant_part
= 8; /* in reality variable between 8-12 */
1381 off_linkpl
.constant_part
= 8; /* in reality variable between 8-12 */
1386 /* frames captured on a Juniper PPPoE service PIC
1387 * contain raw ethernet frames */
1388 case DLT_JUNIPER_PPPOE
:
1389 case DLT_JUNIPER_ETHER
:
1390 off_linkpl
.constant_part
= 14;
1391 off_linktype
.constant_part
= 16;
1392 off_nl
= 18; /* Ethernet II */
1393 off_nl_nosnap
= 21; /* 802.3+802.2 */
1396 case DLT_JUNIPER_PPPOE_ATM
:
1397 off_linktype
.constant_part
= 4;
1398 off_linkpl
.constant_part
= 6;
1400 off_nl_nosnap
= -1; /* no 802.2 LLC */
1403 case DLT_JUNIPER_GGSN
:
1404 off_linktype
.constant_part
= 6;
1405 off_linkpl
.constant_part
= 12;
1407 off_nl_nosnap
= -1; /* no 802.2 LLC */
1410 case DLT_JUNIPER_ES
:
1411 off_linktype
.constant_part
= 6;
1412 off_linkpl
.constant_part
= -1; /* not really a network layer but raw IP addresses */
1413 off_nl
= -1; /* not really a network layer but raw IP addresses */
1414 off_nl_nosnap
= -1; /* no 802.2 LLC */
1417 case DLT_JUNIPER_MONITOR
:
1418 off_linktype
.constant_part
= 12;
1419 off_linkpl
.constant_part
= 12;
1420 off_nl
= 0; /* raw IP/IP6 header */
1421 off_nl_nosnap
= -1; /* no 802.2 LLC */
1424 case DLT_BACNET_MS_TP
:
1425 off_linktype
.constant_part
= -1;
1426 off_linkpl
.constant_part
= -1;
1431 case DLT_JUNIPER_SERVICES
:
1432 off_linktype
.constant_part
= 12;
1433 off_linkpl
.constant_part
= -1; /* L3 proto location dep. on cookie type */
1434 off_nl
= -1; /* L3 proto location dep. on cookie type */
1435 off_nl_nosnap
= -1; /* no 802.2 LLC */
1438 case DLT_JUNIPER_VP
:
1439 off_linktype
.constant_part
= 18;
1440 off_linkpl
.constant_part
= -1;
1445 case DLT_JUNIPER_ST
:
1446 off_linktype
.constant_part
= 18;
1447 off_linkpl
.constant_part
= -1;
1452 case DLT_JUNIPER_ISM
:
1453 off_linktype
.constant_part
= 8;
1454 off_linkpl
.constant_part
= -1;
1459 case DLT_JUNIPER_VS
:
1460 case DLT_JUNIPER_SRX_E2E
:
1461 case DLT_JUNIPER_FIBRECHANNEL
:
1462 case DLT_JUNIPER_ATM_CEMIC
:
1463 off_linktype
.constant_part
= 8;
1464 off_linkpl
.constant_part
= -1;
1476 off_linktype
.constant_part
= -1;
1477 off_linkpl
.constant_part
= -1;
1482 case DLT_MTP2_WITH_PHDR
:
1489 off_linktype
.constant_part
= -1;
1490 off_linkpl
.constant_part
= -1;
1502 off_linktype
.constant_part
= -1;
1503 off_linkpl
.constant_part
= -1;
1509 off_linktype
.constant_part
= -1;
1510 off_linkpl
.constant_part
= 4;
1517 * Currently, only raw "link[N:M]" filtering is supported.
1519 off_linktype
.constant_part
= -1; /* variable, min 15, max 71 steps of 7 */
1520 off_linkpl
.constant_part
= -1;
1521 off_nl
= -1; /* variable, min 16, max 71 steps of 7 */
1522 off_nl_nosnap
= -1; /* no 802.2 LLC */
1526 off_linktype
.constant_part
= 1;
1527 off_linkpl
.constant_part
= 24; /* ipnet header length */
1532 case DLT_NETANALYZER
:
1533 off_linkhdr
.constant_part
= 4; /* Ethernet header is past 4-byte pseudo-header */
1534 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1535 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+Ethernet header length */
1536 off_nl
= 0; /* Ethernet II */
1537 off_nl_nosnap
= 3; /* 802.3+802.2 */
1540 case DLT_NETANALYZER_TRANSPARENT
:
1541 off_linkhdr
.constant_part
= 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1542 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
1543 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* pseudo-header+preamble+SFD+Ethernet header length */
1544 off_nl
= 0; /* Ethernet II */
1545 off_nl_nosnap
= 3; /* 802.3+802.2 */
1550 * For values in the range in which we've assigned new
1551 * DLT_ values, only raw "link[N:M]" filtering is supported.
1553 if (linktype
>= DLT_MATCHING_MIN
&&
1554 linktype
<= DLT_MATCHING_MAX
) {
1555 off_linktype
.constant_part
= -1;
1556 off_linkpl
.constant_part
= -1;
1560 bpf_error("unknown data link type %d", linktype
);
1565 off_outermostlinkhdr
= off_prevlinkhdr
= off_linkhdr
;
1569 * Load a value relative to the specified absolute offset.
1571 static struct slist
*
1572 gen_load_absoffsetrel(bpf_abs_offset
*abs_offset
, u_int offset
, u_int size
)
1574 struct slist
*s
, *s2
;
1576 s
= gen_abs_offset_varpart(abs_offset
);
1579 * If "s" is non-null, it has code to arrange that the X register
1580 * contains the variable part of the absolute offset, so we
1581 * generate a load relative to that, with an offset of
1582 * abs_offset->constant_part + offset.
1584 * Otherwise, we can do an absolute load with an offset of
1585 * abs_offset->constant_part + offset.
1589 * "s" points to a list of statements that puts the
1590 * variable part of the absolute offset into the X register.
1591 * Do an indirect load, to use the X register as an offset.
1593 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1594 s2
->s
.k
= abs_offset
->constant_part
+ offset
;
1598 * There is no variable part of the absolute offset, so
1599 * just do an absolute load.
1601 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1602 s
->s
.k
= abs_offset
->constant_part
+ offset
;
1608 * Load a value relative to the beginning of the specified header.
1610 static struct slist
*
1611 gen_load_a(offrel
, offset
, size
)
1612 enum e_offrel offrel
;
1615 struct slist
*s
, *s2
;
1620 s
= new_stmt(BPF_LD
|BPF_ABS
|size
);
1625 s
= gen_load_absoffsetrel(&off_linkhdr
, offset
, size
);
1628 case OR_PREVLINKHDR
:
1629 s
= gen_load_absoffsetrel(&off_prevlinkhdr
, offset
, size
);
1633 s
= gen_load_absoffsetrel(&off_linkpl
, offset
, size
);
1636 case OR_PREVMPLSHDR
:
1637 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
- 4 + offset
, size
);
1641 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ offset
, size
);
1644 case OR_LINKPL_NOSNAP
:
1645 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl_nosnap
+ offset
, size
);
1649 s
= gen_load_absoffsetrel(&off_linktype
, offset
, size
);
1654 * Load the X register with the length of the IPv4 header
1655 * (plus the offset of the link-layer header, if it's
1656 * preceded by a variable-length header such as a radio
1657 * header), in bytes.
1659 s
= gen_loadx_iphdrlen();
1662 * Load the item at {offset of the link-layer payload} +
1663 * {offset, relative to the start of the link-layer
1664 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1665 * {specified offset}.
1667 * If the offset of the link-layer payload is variable,
1668 * the variable part of that offset is included in the
1669 * value in the X register, and we include the constant
1670 * part in the offset of the load.
1672 s2
= new_stmt(BPF_LD
|BPF_IND
|size
);
1673 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ offset
;
1678 s
= gen_load_absoffsetrel(&off_linkpl
, off_nl
+ 40 + offset
, size
);
1689 * Generate code to load into the X register the sum of the length of
1690 * the IPv4 header and the variable part of the offset of the link-layer
1693 static struct slist
*
1694 gen_loadx_iphdrlen()
1696 struct slist
*s
, *s2
;
1698 s
= gen_abs_offset_varpart(&off_linkpl
);
1701 * The offset of the link-layer payload has a variable
1702 * part. "s" points to a list of statements that put
1703 * the variable part of that offset into the X register.
1705 * The 4*([k]&0xf) addressing mode can't be used, as we
1706 * don't have a constant offset, so we have to load the
1707 * value in question into the A register and add to it
1708 * the value from the X register.
1710 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
1711 s2
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1713 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
1716 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
1721 * The A register now contains the length of the IP header.
1722 * We need to add to it the variable part of the offset of
1723 * the link-layer payload, which is still in the X
1724 * register, and move the result into the X register.
1726 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
1727 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
1730 * The offset of the link-layer payload is a constant,
1731 * so no code was generated to load the (non-existent)
1732 * variable part of that offset.
1734 * This means we can use the 4*([k]&0xf) addressing
1735 * mode. Load the length of the IPv4 header, which
1736 * is at an offset of off_nl from the beginning of
1737 * the link-layer payload, and thus at an offset of
1738 * off_linkpl.constant_part + off_nl from the beginning
1739 * of the raw packet data, using that addressing mode.
1741 s
= new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
1742 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
1747 static struct block
*
1754 s
= new_stmt(BPF_LD
|BPF_IMM
);
1756 b
= new_block(JMP(BPF_JEQ
));
1762 static inline struct block
*
1765 return gen_uncond(1);
1768 static inline struct block
*
1771 return gen_uncond(0);
1775 * Byte-swap a 32-bit number.
1776 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1777 * big-endian platforms.)
1779 #define SWAPLONG(y) \
1780 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1783 * Generate code to match a particular packet type.
1785 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1786 * value, if <= ETHERMTU. We use that to determine whether to
1787 * match the type/length field or to check the type/length field for
1788 * a value <= ETHERMTU to see whether it's a type field and then do
1789 * the appropriate test.
1791 static struct block
*
1792 gen_ether_linktype(proto
)
1795 struct block
*b0
, *b1
;
1801 case LLCSAP_NETBEUI
:
1803 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1804 * so we check the DSAP and SSAP.
1806 * LLCSAP_IP checks for IP-over-802.2, rather
1807 * than IP-over-Ethernet or IP-over-SNAP.
1809 * XXX - should we check both the DSAP and the
1810 * SSAP, like this, or should we check just the
1811 * DSAP, as we do for other types <= ETHERMTU
1812 * (i.e., other SAP values)?
1814 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1816 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
1817 ((proto
<< 8) | proto
));
1825 * Ethernet_II frames, which are Ethernet
1826 * frames with a frame type of ETHERTYPE_IPX;
1828 * Ethernet_802.3 frames, which are 802.3
1829 * frames (i.e., the type/length field is
1830 * a length field, <= ETHERMTU, rather than
1831 * a type field) with the first two bytes
1832 * after the Ethernet/802.3 header being
1835 * Ethernet_802.2 frames, which are 802.3
1836 * frames with an 802.2 LLC header and
1837 * with the IPX LSAP as the DSAP in the LLC
1840 * Ethernet_SNAP frames, which are 802.3
1841 * frames with an LLC header and a SNAP
1842 * header and with an OUI of 0x000000
1843 * (encapsulated Ethernet) and a protocol
1844 * ID of ETHERTYPE_IPX in the SNAP header.
1846 * XXX - should we generate the same code both
1847 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1851 * This generates code to check both for the
1852 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1854 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
1855 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
1859 * Now we add code to check for SNAP frames with
1860 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1862 b0
= gen_snap(0x000000, ETHERTYPE_IPX
);
1866 * Now we generate code to check for 802.3
1867 * frames in general.
1869 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1873 * Now add the check for 802.3 frames before the
1874 * check for Ethernet_802.2 and Ethernet_802.3,
1875 * as those checks should only be done on 802.3
1876 * frames, not on Ethernet frames.
1881 * Now add the check for Ethernet_II frames, and
1882 * do that before checking for the other frame
1885 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
1889 case ETHERTYPE_ATALK
:
1890 case ETHERTYPE_AARP
:
1892 * EtherTalk (AppleTalk protocols on Ethernet link
1893 * layer) may use 802.2 encapsulation.
1897 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1898 * we check for an Ethernet type field less than
1899 * 1500, which means it's an 802.3 length field.
1901 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1905 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1906 * SNAP packets with an organization code of
1907 * 0x080007 (Apple, for Appletalk) and a protocol
1908 * type of ETHERTYPE_ATALK (Appletalk).
1910 * 802.2-encapsulated ETHERTYPE_AARP packets are
1911 * SNAP packets with an organization code of
1912 * 0x000000 (encapsulated Ethernet) and a protocol
1913 * type of ETHERTYPE_AARP (Appletalk ARP).
1915 if (proto
== ETHERTYPE_ATALK
)
1916 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
1917 else /* proto == ETHERTYPE_AARP */
1918 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
1922 * Check for Ethernet encapsulation (Ethertalk
1923 * phase 1?); we just check for the Ethernet
1926 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
1932 if (proto
<= ETHERMTU
) {
1934 * This is an LLC SAP value, so the frames
1935 * that match would be 802.2 frames.
1936 * Check that the frame is an 802.2 frame
1937 * (i.e., that the length/type field is
1938 * a length field, <= ETHERMTU) and
1939 * then check the DSAP.
1941 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
1943 b1
= gen_cmp(OR_LINKTYPE
, 2, BPF_B
, (bpf_int32
)proto
);
1948 * This is an Ethernet type, so compare
1949 * the length/type field with it (if
1950 * the frame is an 802.2 frame, the length
1951 * field will be <= ETHERMTU, and, as
1952 * "proto" is > ETHERMTU, this test
1953 * will fail and the frame won't match,
1954 * which is what we want).
1956 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
1963 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1964 * or IPv6 then we have an error.
1966 static struct block
*
1967 gen_ipnet_linktype(proto
)
1973 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
, (bpf_int32
)IPH_AF_INET
);
1976 case ETHERTYPE_IPV6
:
1977 return gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
1978 (bpf_int32
)IPH_AF_INET6
);
1989 * Generate code to match a particular packet type.
1991 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1992 * value, if <= ETHERMTU. We use that to determine whether to
1993 * match the type field or to check the type field for the special
1994 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1996 static struct block
*
1997 gen_linux_sll_linktype(proto
)
2000 struct block
*b0
, *b1
;
2006 case LLCSAP_NETBEUI
:
2008 * OSI protocols and NetBEUI always use 802.2 encapsulation,
2009 * so we check the DSAP and SSAP.
2011 * LLCSAP_IP checks for IP-over-802.2, rather
2012 * than IP-over-Ethernet or IP-over-SNAP.
2014 * XXX - should we check both the DSAP and the
2015 * SSAP, like this, or should we check just the
2016 * DSAP, as we do for other types <= ETHERMTU
2017 * (i.e., other SAP values)?
2019 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2020 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)
2021 ((proto
<< 8) | proto
));
2027 * Ethernet_II frames, which are Ethernet
2028 * frames with a frame type of ETHERTYPE_IPX;
2030 * Ethernet_802.3 frames, which have a frame
2031 * type of LINUX_SLL_P_802_3;
2033 * Ethernet_802.2 frames, which are 802.3
2034 * frames with an 802.2 LLC header (i.e, have
2035 * a frame type of LINUX_SLL_P_802_2) and
2036 * with the IPX LSAP as the DSAP in the LLC
2039 * Ethernet_SNAP frames, which are 802.3
2040 * frames with an LLC header and a SNAP
2041 * header and with an OUI of 0x000000
2042 * (encapsulated Ethernet) and a protocol
2043 * ID of ETHERTYPE_IPX in the SNAP header.
2045 * First, do the checks on LINUX_SLL_P_802_2
2046 * frames; generate the check for either
2047 * Ethernet_802.2 or Ethernet_SNAP frames, and
2048 * then put a check for LINUX_SLL_P_802_2 frames
2051 b0
= gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)LLCSAP_IPX
);
2052 b1
= gen_snap(0x000000, ETHERTYPE_IPX
);
2054 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2058 * Now check for 802.3 frames and OR that with
2059 * the previous test.
2061 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_3
);
2065 * Now add the check for Ethernet_II frames, and
2066 * do that before checking for the other frame
2069 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)ETHERTYPE_IPX
);
2073 case ETHERTYPE_ATALK
:
2074 case ETHERTYPE_AARP
:
2076 * EtherTalk (AppleTalk protocols on Ethernet link
2077 * layer) may use 802.2 encapsulation.
2081 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2082 * we check for the 802.2 protocol type in the
2083 * "Ethernet type" field.
2085 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2088 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2089 * SNAP packets with an organization code of
2090 * 0x080007 (Apple, for Appletalk) and a protocol
2091 * type of ETHERTYPE_ATALK (Appletalk).
2093 * 802.2-encapsulated ETHERTYPE_AARP packets are
2094 * SNAP packets with an organization code of
2095 * 0x000000 (encapsulated Ethernet) and a protocol
2096 * type of ETHERTYPE_AARP (Appletalk ARP).
2098 if (proto
== ETHERTYPE_ATALK
)
2099 b1
= gen_snap(0x080007, ETHERTYPE_ATALK
);
2100 else /* proto == ETHERTYPE_AARP */
2101 b1
= gen_snap(0x000000, ETHERTYPE_AARP
);
2105 * Check for Ethernet encapsulation (Ethertalk
2106 * phase 1?); we just check for the Ethernet
2109 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2115 if (proto
<= ETHERMTU
) {
2117 * This is an LLC SAP value, so the frames
2118 * that match would be 802.2 frames.
2119 * Check for the 802.2 protocol type
2120 * in the "Ethernet type" field, and
2121 * then check the DSAP.
2123 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, LINUX_SLL_P_802_2
);
2124 b1
= gen_cmp(OR_LINKHDR
, off_linkpl
.constant_part
, BPF_B
,
2130 * This is an Ethernet type, so compare
2131 * the length/type field with it (if
2132 * the frame is an 802.2 frame, the length
2133 * field will be <= ETHERMTU, and, as
2134 * "proto" is > ETHERMTU, this test
2135 * will fail and the frame won't match,
2136 * which is what we want).
2138 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2143 static struct slist
*
2144 gen_load_prism_llprefixlen()
2146 struct slist
*s1
, *s2
;
2147 struct slist
*sjeq_avs_cookie
;
2148 struct slist
*sjcommon
;
2151 * This code is not compatible with the optimizer, as
2152 * we are generating jmp instructions within a normal
2153 * slist of instructions
2158 * Generate code to load the length of the radio header into
2159 * the register assigned to hold that length, if one has been
2160 * assigned. (If one hasn't been assigned, no code we've
2161 * generated uses that prefix, so we don't need to generate any
2164 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2165 * or always use the AVS header rather than the Prism header.
2166 * We load a 4-byte big-endian value at the beginning of the
2167 * raw packet data, and see whether, when masked with 0xFFFFF000,
2168 * it's equal to 0x80211000. If so, that indicates that it's
2169 * an AVS header (the masked-out bits are the version number).
2170 * Otherwise, it's a Prism header.
2172 * XXX - the Prism header is also, in theory, variable-length,
2173 * but no known software generates headers that aren't 144
2176 if (off_linkhdr
.reg
!= -1) {
2180 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2184 * AND it with 0xFFFFF000.
2186 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
2187 s2
->s
.k
= 0xFFFFF000;
2191 * Compare with 0x80211000.
2193 sjeq_avs_cookie
= new_stmt(JMP(BPF_JEQ
));
2194 sjeq_avs_cookie
->s
.k
= 0x80211000;
2195 sappend(s1
, sjeq_avs_cookie
);
2200 * The 4 bytes at an offset of 4 from the beginning of
2201 * the AVS header are the length of the AVS header.
2202 * That field is big-endian.
2204 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2207 sjeq_avs_cookie
->s
.jt
= s2
;
2210 * Now jump to the code to allocate a register
2211 * into which to save the header length and
2212 * store the length there. (The "jump always"
2213 * instruction needs to have the k field set;
2214 * it's added to the PC, so, as we're jumping
2215 * over a single instruction, it should be 1.)
2217 sjcommon
= new_stmt(JMP(BPF_JA
));
2219 sappend(s1
, sjcommon
);
2222 * Now for the code that handles the Prism header.
2223 * Just load the length of the Prism header (144)
2224 * into the A register. Have the test for an AVS
2225 * header branch here if we don't have an AVS header.
2227 s2
= new_stmt(BPF_LD
|BPF_W
|BPF_IMM
);
2230 sjeq_avs_cookie
->s
.jf
= s2
;
2233 * Now allocate a register to hold that value and store
2234 * it. The code for the AVS header will jump here after
2235 * loading the length of the AVS header.
2237 s2
= new_stmt(BPF_ST
);
2238 s2
->s
.k
= off_linkhdr
.reg
;
2240 sjcommon
->s
.jf
= s2
;
2243 * Now move it into the X register.
2245 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2253 static struct slist
*
2254 gen_load_avs_llprefixlen()
2256 struct slist
*s1
, *s2
;
2259 * Generate code to load the length of the AVS header into
2260 * the register assigned to hold that length, if one has been
2261 * assigned. (If one hasn't been assigned, no code we've
2262 * generated uses that prefix, so we don't need to generate any
2265 if (off_linkhdr
.reg
!= -1) {
2267 * The 4 bytes at an offset of 4 from the beginning of
2268 * the AVS header are the length of the AVS header.
2269 * That field is big-endian.
2271 s1
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2275 * Now allocate a register to hold that value and store
2278 s2
= new_stmt(BPF_ST
);
2279 s2
->s
.k
= off_linkhdr
.reg
;
2283 * Now move it into the X register.
2285 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2293 static struct slist
*
2294 gen_load_radiotap_llprefixlen()
2296 struct slist
*s1
, *s2
;
2299 * Generate code to load the length of the radiotap header into
2300 * the register assigned to hold that length, if one has been
2301 * assigned. (If one hasn't been assigned, no code we've
2302 * generated uses that prefix, so we don't need to generate any
2305 if (off_linkhdr
.reg
!= -1) {
2307 * The 2 bytes at offsets of 2 and 3 from the beginning
2308 * of the radiotap header are the length of the radiotap
2309 * header; unfortunately, it's little-endian, so we have
2310 * to load it a byte at a time and construct the value.
2314 * Load the high-order byte, at an offset of 3, shift it
2315 * left a byte, and put the result in the X register.
2317 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2319 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2322 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2326 * Load the next byte, at an offset of 2, and OR the
2327 * value from the X register into it.
2329 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2332 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2336 * Now allocate a register to hold that value and store
2339 s2
= new_stmt(BPF_ST
);
2340 s2
->s
.k
= off_linkhdr
.reg
;
2344 * Now move it into the X register.
2346 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2355 * At the moment we treat PPI as normal Radiotap encoded
2356 * packets. The difference is in the function that generates
2357 * the code at the beginning to compute the header length.
2358 * Since this code generator of PPI supports bare 802.11
2359 * encapsulation only (i.e. the encapsulated DLT should be
2360 * DLT_IEEE802_11) we generate code to check for this too;
2361 * that's done in finish_parse().
2363 static struct slist
*
2364 gen_load_ppi_llprefixlen()
2366 struct slist
*s1
, *s2
;
2369 * Generate code to load the length of the radiotap header
2370 * into the register assigned to hold that length, if one has
2373 if (off_linkhdr
.reg
!= -1) {
2375 * The 2 bytes at offsets of 2 and 3 from the beginning
2376 * of the radiotap header are the length of the radiotap
2377 * header; unfortunately, it's little-endian, so we have
2378 * to load it a byte at a time and construct the value.
2382 * Load the high-order byte, at an offset of 3, shift it
2383 * left a byte, and put the result in the X register.
2385 s1
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2387 s2
= new_stmt(BPF_ALU
|BPF_LSH
|BPF_K
);
2390 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2394 * Load the next byte, at an offset of 2, and OR the
2395 * value from the X register into it.
2397 s2
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
2400 s2
= new_stmt(BPF_ALU
|BPF_OR
|BPF_X
);
2404 * Now allocate a register to hold that value and store
2407 s2
= new_stmt(BPF_ST
);
2408 s2
->s
.k
= off_linkhdr
.reg
;
2412 * Now move it into the X register.
2414 s2
= new_stmt(BPF_MISC
|BPF_TAX
);
2423 * Load a value relative to the beginning of the link-layer header after the 802.11
2424 * header, i.e. LLC_SNAP.
2425 * The link-layer header doesn't necessarily begin at the beginning
2426 * of the packet data; there might be a variable-length prefix containing
2427 * radio information.
2429 static struct slist
*
2430 gen_load_802_11_header_len(struct slist
*s
, struct slist
*snext
)
2433 struct slist
*sjset_data_frame_1
;
2434 struct slist
*sjset_data_frame_2
;
2435 struct slist
*sjset_qos
;
2436 struct slist
*sjset_radiotap_flags
;
2437 struct slist
*sjset_radiotap_tsft
;
2438 struct slist
*sjset_tsft_datapad
, *sjset_notsft_datapad
;
2439 struct slist
*s_roundup
;
2441 if (off_linkpl
.reg
== -1) {
2443 * No register has been assigned to the offset of
2444 * the link-layer payload, which means nobody needs
2445 * it; don't bother computing it - just return
2446 * what we already have.
2452 * This code is not compatible with the optimizer, as
2453 * we are generating jmp instructions within a normal
2454 * slist of instructions
2459 * If "s" is non-null, it has code to arrange that the X register
2460 * contains the length of the prefix preceding the link-layer
2463 * Otherwise, the length of the prefix preceding the link-layer
2464 * header is "off_outermostlinkhdr.constant_part".
2468 * There is no variable-length header preceding the
2469 * link-layer header.
2471 * Load the length of the fixed-length prefix preceding
2472 * the link-layer header (if any) into the X register,
2473 * and store it in the off_linkpl.reg register.
2474 * That length is off_outermostlinkhdr.constant_part.
2476 s
= new_stmt(BPF_LDX
|BPF_IMM
);
2477 s
->s
.k
= off_outermostlinkhdr
.constant_part
;
2481 * The X register contains the offset of the beginning of the
2482 * link-layer header; add 24, which is the minimum length
2483 * of the MAC header for a data frame, to that, and store it
2484 * in off_linkpl.reg, and then load the Frame Control field,
2485 * which is at the offset in the X register, with an indexed load.
2487 s2
= new_stmt(BPF_MISC
|BPF_TXA
);
2489 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
2492 s2
= new_stmt(BPF_ST
);
2493 s2
->s
.k
= off_linkpl
.reg
;
2496 s2
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
2501 * Check the Frame Control field to see if this is a data frame;
2502 * a data frame has the 0x08 bit (b3) in that field set and the
2503 * 0x04 bit (b2) clear.
2505 sjset_data_frame_1
= new_stmt(JMP(BPF_JSET
));
2506 sjset_data_frame_1
->s
.k
= 0x08;
2507 sappend(s
, sjset_data_frame_1
);
2510 * If b3 is set, test b2, otherwise go to the first statement of
2511 * the rest of the program.
2513 sjset_data_frame_1
->s
.jt
= sjset_data_frame_2
= new_stmt(JMP(BPF_JSET
));
2514 sjset_data_frame_2
->s
.k
= 0x04;
2515 sappend(s
, sjset_data_frame_2
);
2516 sjset_data_frame_1
->s
.jf
= snext
;
2519 * If b2 is not set, this is a data frame; test the QoS bit.
2520 * Otherwise, go to the first statement of the rest of the
2523 sjset_data_frame_2
->s
.jt
= snext
;
2524 sjset_data_frame_2
->s
.jf
= sjset_qos
= new_stmt(JMP(BPF_JSET
));
2525 sjset_qos
->s
.k
= 0x80; /* QoS bit */
2526 sappend(s
, sjset_qos
);
2529 * If it's set, add 2 to off_linkpl.reg, to skip the QoS
2531 * Otherwise, go to the first statement of the rest of the
2534 sjset_qos
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_MEM
);
2535 s2
->s
.k
= off_linkpl
.reg
;
2537 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2540 s2
= new_stmt(BPF_ST
);
2541 s2
->s
.k
= off_linkpl
.reg
;
2545 * If we have a radiotap header, look at it to see whether
2546 * there's Atheros padding between the MAC-layer header
2549 * Note: all of the fields in the radiotap header are
2550 * little-endian, so we byte-swap all of the values
2551 * we test against, as they will be loaded as big-endian
2554 if (linktype
== DLT_IEEE802_11_RADIO
) {
2556 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2557 * in the presence flag?
2559 sjset_qos
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_W
);
2563 sjset_radiotap_flags
= new_stmt(JMP(BPF_JSET
));
2564 sjset_radiotap_flags
->s
.k
= SWAPLONG(0x00000002);
2565 sappend(s
, sjset_radiotap_flags
);
2568 * If not, skip all of this.
2570 sjset_radiotap_flags
->s
.jf
= snext
;
2573 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2575 sjset_radiotap_tsft
= sjset_radiotap_flags
->s
.jt
=
2576 new_stmt(JMP(BPF_JSET
));
2577 sjset_radiotap_tsft
->s
.k
= SWAPLONG(0x00000001);
2578 sappend(s
, sjset_radiotap_tsft
);
2581 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2582 * at an offset of 16 from the beginning of the raw packet
2583 * data (8 bytes for the radiotap header and 8 bytes for
2586 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2589 sjset_radiotap_tsft
->s
.jt
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2593 sjset_tsft_datapad
= new_stmt(JMP(BPF_JSET
));
2594 sjset_tsft_datapad
->s
.k
= 0x20;
2595 sappend(s
, sjset_tsft_datapad
);
2598 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2599 * at an offset of 8 from the beginning of the raw packet
2600 * data (8 bytes for the radiotap header).
2602 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2605 sjset_radiotap_tsft
->s
.jf
= s2
= new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
2609 sjset_notsft_datapad
= new_stmt(JMP(BPF_JSET
));
2610 sjset_notsft_datapad
->s
.k
= 0x20;
2611 sappend(s
, sjset_notsft_datapad
);
2614 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2615 * set, round the length of the 802.11 header to
2616 * a multiple of 4. Do that by adding 3 and then
2617 * dividing by and multiplying by 4, which we do by
2620 s_roundup
= new_stmt(BPF_LD
|BPF_MEM
);
2621 s_roundup
->s
.k
= off_linkpl
.reg
;
2622 sappend(s
, s_roundup
);
2623 s2
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_IMM
);
2626 s2
= new_stmt(BPF_ALU
|BPF_AND
|BPF_IMM
);
2629 s2
= new_stmt(BPF_ST
);
2630 s2
->s
.k
= off_linkpl
.reg
;
2633 sjset_tsft_datapad
->s
.jt
= s_roundup
;
2634 sjset_tsft_datapad
->s
.jf
= snext
;
2635 sjset_notsft_datapad
->s
.jt
= s_roundup
;
2636 sjset_notsft_datapad
->s
.jf
= snext
;
2638 sjset_qos
->s
.jf
= snext
;
2644 insert_compute_vloffsets(b
)
2649 /* There is an implicit dependency between the link
2650 * payload and link header since the payload computation
2651 * includes the variable part of the header. Therefore,
2652 * if nobody else has allocated a register for the link
2653 * header and we need it, do it now. */
2654 if (off_linkpl
.reg
!= -1 && off_linkhdr
.is_variable
&&
2655 off_linkhdr
.reg
== -1)
2656 off_linkhdr
.reg
= alloc_reg();
2659 * For link-layer types that have a variable-length header
2660 * preceding the link-layer header, generate code to load
2661 * the offset of the link-layer header into the register
2662 * assigned to that offset, if any.
2664 * XXX - this, and the next switch statement, won't handle
2665 * encapsulation of 802.11 or 802.11+radio information in
2666 * some other protocol stack. That's significantly more
2669 switch (outermostlinktype
) {
2671 case DLT_PRISM_HEADER
:
2672 s
= gen_load_prism_llprefixlen();
2675 case DLT_IEEE802_11_RADIO_AVS
:
2676 s
= gen_load_avs_llprefixlen();
2679 case DLT_IEEE802_11_RADIO
:
2680 s
= gen_load_radiotap_llprefixlen();
2684 s
= gen_load_ppi_llprefixlen();
2693 * For link-layer types that have a variable-length link-layer
2694 * header, generate code to load the offset of the link-layer
2695 * payload into the register assigned to that offset, if any.
2697 switch (outermostlinktype
) {
2699 case DLT_IEEE802_11
:
2700 case DLT_PRISM_HEADER
:
2701 case DLT_IEEE802_11_RADIO_AVS
:
2702 case DLT_IEEE802_11_RADIO
:
2704 s
= gen_load_802_11_header_len(s
, b
->stmts
);
2709 * If we have any offset-loading code, append all the
2710 * existing statements in the block to those statements,
2711 * and make the resulting list the list of statements
2715 sappend(s
, b
->stmts
);
2720 static struct block
*
2721 gen_ppi_dlt_check(void)
2723 struct slist
*s_load_dlt
;
2726 if (linktype
== DLT_PPI
)
2728 /* Create the statements that check for the DLT
2730 s_load_dlt
= new_stmt(BPF_LD
|BPF_W
|BPF_ABS
);
2731 s_load_dlt
->s
.k
= 4;
2733 b
= new_block(JMP(BPF_JEQ
));
2735 b
->stmts
= s_load_dlt
;
2736 b
->s
.k
= SWAPLONG(DLT_IEEE802_11
);
2747 * Take an absolute offset, and:
2749 * if it has no variable part, return NULL;
2751 * if it has a variable part, generate code to load the register
2752 * containing that variable part into the X register, returning
2753 * a pointer to that code - if no register for that offset has
2754 * been allocated, allocate it first.
2756 * (The code to set that register will be generated later, but will
2757 * be placed earlier in the code sequence.)
2759 static struct slist
*
2760 gen_abs_offset_varpart(bpf_abs_offset
*off
)
2764 if (off
->is_variable
) {
2765 if (off
->reg
== -1) {
2767 * We haven't yet assigned a register for the
2768 * variable part of the offset of the link-layer
2769 * header; allocate one.
2771 off
->reg
= alloc_reg();
2775 * Load the register containing the variable part of the
2776 * offset of the link-layer header into the X register.
2778 s
= new_stmt(BPF_LDX
|BPF_MEM
);
2783 * That offset isn't variable, there's no variable part,
2784 * so we don't need to generate any code.
2791 * Map an Ethernet type to the equivalent PPP type.
2794 ethertype_to_ppptype(proto
)
2803 case ETHERTYPE_IPV6
:
2811 case ETHERTYPE_ATALK
:
2825 * I'm assuming the "Bridging PDU"s that go
2826 * over PPP are Spanning Tree Protocol
2840 * Generate any tests that, for encapsulation of a link-layer packet
2841 * inside another protocol stack, need to be done to check for those
2842 * link-layer packets (and that haven't already been done by a check
2843 * for that encapsulation).
2845 static struct block
*
2846 gen_prevlinkhdr_check(void)
2851 return gen_geneve_ll_check();
2853 switch (prevlinktype
) {
2857 * This is LANE-encapsulated Ethernet; check that the LANE
2858 * packet doesn't begin with an LE Control marker, i.e.
2859 * that it's data, not a control message.
2861 * (We've already generated a test for LANE.)
2863 b0
= gen_cmp(OR_PREVLINKHDR
, SUNATM_PKT_BEGIN_POS
, BPF_H
, 0xFF00);
2869 * No such tests are necessary.
2877 * Generate code to match a particular packet type by matching the
2878 * link-layer type field or fields in the 802.2 LLC header.
2880 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2881 * value, if <= ETHERMTU.
2883 static struct block
*
2887 struct block
*b0
, *b1
, *b2
;
2888 const char *description
;
2890 /* are we checking MPLS-encapsulated packets? */
2891 if (label_stack_depth
> 0) {
2895 /* FIXME add other L3 proto IDs */
2896 return gen_mpls_linktype(Q_IP
);
2898 case ETHERTYPE_IPV6
:
2900 /* FIXME add other L3 proto IDs */
2901 return gen_mpls_linktype(Q_IPV6
);
2904 bpf_error("unsupported protocol over mpls");
2912 case DLT_NETANALYZER
:
2913 case DLT_NETANALYZER_TRANSPARENT
:
2914 /* Geneve has an EtherType regardless of whether there is an
2917 b0
= gen_prevlinkhdr_check();
2921 b1
= gen_ether_linktype(proto
);
2932 proto
= (proto
<< 8 | LLCSAP_ISONS
);
2936 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
2942 case DLT_IEEE802_11
:
2943 case DLT_PRISM_HEADER
:
2944 case DLT_IEEE802_11_RADIO_AVS
:
2945 case DLT_IEEE802_11_RADIO
:
2948 * Check that we have a data frame.
2950 b0
= gen_check_802_11_data_frame();
2953 * Now check for the specified link-layer type.
2955 b1
= gen_llc_linktype(proto
);
2963 * XXX - check for LLC frames.
2965 return gen_llc_linktype(proto
);
2971 * XXX - check for LLC PDUs, as per IEEE 802.5.
2973 return gen_llc_linktype(proto
);
2977 case DLT_ATM_RFC1483
:
2979 case DLT_IP_OVER_FC
:
2980 return gen_llc_linktype(proto
);
2986 * Check for an LLC-encapsulated version of this protocol;
2987 * if we were checking for LANE, linktype would no longer
2990 * Check for LLC encapsulation and then check the protocol.
2992 b0
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
2993 b1
= gen_llc_linktype(proto
);
3000 return gen_linux_sll_linktype(proto
);
3005 case DLT_SLIP_BSDOS
:
3008 * These types don't provide any type field; packets
3009 * are always IPv4 or IPv6.
3011 * XXX - for IPv4, check for a version number of 4, and,
3012 * for IPv6, check for a version number of 6?
3017 /* Check for a version number of 4. */
3018 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x40, 0xF0);
3020 case ETHERTYPE_IPV6
:
3021 /* Check for a version number of 6. */
3022 return gen_mcmp(OR_LINKHDR
, 0, BPF_B
, 0x60, 0xF0);
3025 return gen_false(); /* always false */
3032 * Raw IPv4, so no type field.
3034 if (proto
== ETHERTYPE_IP
)
3035 return gen_true(); /* always true */
3037 /* Checking for something other than IPv4; always false */
3044 * Raw IPv6, so no type field.
3046 if (proto
== ETHERTYPE_IPV6
)
3047 return gen_true(); /* always true */
3049 /* Checking for something other than IPv6; always false */
3056 case DLT_PPP_SERIAL
:
3059 * We use Ethernet protocol types inside libpcap;
3060 * map them to the corresponding PPP protocol types.
3062 proto
= ethertype_to_ppptype(proto
);
3063 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3069 * We use Ethernet protocol types inside libpcap;
3070 * map them to the corresponding PPP protocol types.
3076 * Also check for Van Jacobson-compressed IP.
3077 * XXX - do this for other forms of PPP?
3079 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_IP
);
3080 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJC
);
3082 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_H
, PPP_VJNC
);
3087 proto
= ethertype_to_ppptype(proto
);
3088 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
,
3098 * For DLT_NULL, the link-layer header is a 32-bit
3099 * word containing an AF_ value in *host* byte order,
3100 * and for DLT_ENC, the link-layer header begins
3101 * with a 32-bit work containing an AF_ value in
3104 * In addition, if we're reading a saved capture file,
3105 * the host byte order in the capture may not be the
3106 * same as the host byte order on this machine.
3108 * For DLT_LOOP, the link-layer header is a 32-bit
3109 * word containing an AF_ value in *network* byte order.
3111 * XXX - AF_ values may, unfortunately, be platform-
3112 * dependent; for example, FreeBSD's AF_INET6 is 24
3113 * whilst NetBSD's and OpenBSD's is 26.
3115 * This means that, when reading a capture file, just
3116 * checking for our AF_INET6 value won't work if the
3117 * capture file came from another OS.
3126 case ETHERTYPE_IPV6
:
3133 * Not a type on which we support filtering.
3134 * XXX - support those that have AF_ values
3135 * #defined on this platform, at least?
3140 if (linktype
== DLT_NULL
|| linktype
== DLT_ENC
) {
3142 * The AF_ value is in host byte order, but
3143 * the BPF interpreter will convert it to
3144 * network byte order.
3146 * If this is a save file, and it's from a
3147 * machine with the opposite byte order to
3148 * ours, we byte-swap the AF_ value.
3150 * Then we run it through "htonl()", and
3151 * generate code to compare against the result.
3153 if (bpf_pcap
->rfile
!= NULL
&& bpf_pcap
->swapped
)
3154 proto
= SWAPLONG(proto
);
3155 proto
= htonl(proto
);
3157 return (gen_cmp(OR_LINKHDR
, 0, BPF_W
, (bpf_int32
)proto
));
3159 #ifdef HAVE_NET_PFVAR_H
3162 * af field is host byte order in contrast to the rest of
3165 if (proto
== ETHERTYPE_IP
)
3166 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3167 BPF_B
, (bpf_int32
)AF_INET
));
3168 else if (proto
== ETHERTYPE_IPV6
)
3169 return (gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, af
),
3170 BPF_B
, (bpf_int32
)AF_INET6
));
3175 #endif /* HAVE_NET_PFVAR_H */
3178 case DLT_ARCNET_LINUX
:
3180 * XXX should we check for first fragment if the protocol
3188 case ETHERTYPE_IPV6
:
3189 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3190 (bpf_int32
)ARCTYPE_INET6
));
3193 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3194 (bpf_int32
)ARCTYPE_IP
);
3195 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3196 (bpf_int32
)ARCTYPE_IP_OLD
);
3201 b0
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3202 (bpf_int32
)ARCTYPE_ARP
);
3203 b1
= gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3204 (bpf_int32
)ARCTYPE_ARP_OLD
);
3208 case ETHERTYPE_REVARP
:
3209 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3210 (bpf_int32
)ARCTYPE_REVARP
));
3212 case ETHERTYPE_ATALK
:
3213 return (gen_cmp(OR_LINKTYPE
, 0, BPF_B
,
3214 (bpf_int32
)ARCTYPE_ATALK
));
3221 case ETHERTYPE_ATALK
:
3231 * XXX - assumes a 2-byte Frame Relay header with
3232 * DLCI and flags. What if the address is longer?
3238 * Check for the special NLPID for IP.
3240 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0xcc);
3242 case ETHERTYPE_IPV6
:
3244 * Check for the special NLPID for IPv6.
3246 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | 0x8e);
3250 * Check for several OSI protocols.
3252 * Frame Relay packets typically have an OSI
3253 * NLPID at the beginning; we check for each
3256 * What we check for is the NLPID and a frame
3257 * control field of UI, i.e. 0x03 followed
3260 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO8473_CLNP
);
3261 b1
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO9542_ESIS
);
3262 b2
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | ISO10589_ISIS
);
3274 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3276 case DLT_JUNIPER_MFR
:
3277 case DLT_JUNIPER_MLFR
:
3278 case DLT_JUNIPER_MLPPP
:
3279 case DLT_JUNIPER_ATM1
:
3280 case DLT_JUNIPER_ATM2
:
3281 case DLT_JUNIPER_PPPOE
:
3282 case DLT_JUNIPER_PPPOE_ATM
:
3283 case DLT_JUNIPER_GGSN
:
3284 case DLT_JUNIPER_ES
:
3285 case DLT_JUNIPER_MONITOR
:
3286 case DLT_JUNIPER_SERVICES
:
3287 case DLT_JUNIPER_ETHER
:
3288 case DLT_JUNIPER_PPP
:
3289 case DLT_JUNIPER_FRELAY
:
3290 case DLT_JUNIPER_CHDLC
:
3291 case DLT_JUNIPER_VP
:
3292 case DLT_JUNIPER_ST
:
3293 case DLT_JUNIPER_ISM
:
3294 case DLT_JUNIPER_VS
:
3295 case DLT_JUNIPER_SRX_E2E
:
3296 case DLT_JUNIPER_FIBRECHANNEL
:
3297 case DLT_JUNIPER_ATM_CEMIC
:
3299 /* just lets verify the magic number for now -
3300 * on ATM we may have up to 6 different encapsulations on the wire
3301 * and need a lot of heuristics to figure out that the payload
3304 * FIXME encapsulation specific BPF_ filters
3306 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x4d474300, 0xffffff00); /* compare the magic number */
3308 case DLT_BACNET_MS_TP
:
3309 return gen_mcmp(OR_LINKHDR
, 0, BPF_W
, 0x55FF0000, 0xffff0000);
3312 return gen_ipnet_linktype(proto
);
3314 case DLT_LINUX_IRDA
:
3315 bpf_error("IrDA link-layer type filtering not implemented");
3318 bpf_error("DOCSIS link-layer type filtering not implemented");
3321 case DLT_MTP2_WITH_PHDR
:
3322 bpf_error("MTP2 link-layer type filtering not implemented");
3325 bpf_error("ERF link-layer type filtering not implemented");
3328 bpf_error("PFSYNC link-layer type filtering not implemented");
3330 case DLT_LINUX_LAPD
:
3331 bpf_error("LAPD link-layer type filtering not implemented");
3335 case DLT_USB_LINUX_MMAPPED
:
3336 bpf_error("USB link-layer type filtering not implemented");
3338 case DLT_BLUETOOTH_HCI_H4
:
3339 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR
:
3340 bpf_error("Bluetooth link-layer type filtering not implemented");
3343 case DLT_CAN_SOCKETCAN
:
3344 bpf_error("CAN link-layer type filtering not implemented");
3346 case DLT_IEEE802_15_4
:
3347 case DLT_IEEE802_15_4_LINUX
:
3348 case DLT_IEEE802_15_4_NONASK_PHY
:
3349 case DLT_IEEE802_15_4_NOFCS
:
3350 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3352 case DLT_IEEE802_16_MAC_CPS_RADIO
:
3353 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3356 bpf_error("SITA link-layer type filtering not implemented");
3359 bpf_error("RAIF1 link-layer type filtering not implemented");
3362 bpf_error("IPMB link-layer type filtering not implemented");
3365 bpf_error("AX.25 link-layer type filtering not implemented");
3368 /* Using the fixed-size NFLOG header it is possible to tell only
3369 * the address family of the packet, other meaningful data is
3370 * either missing or behind TLVs.
3372 bpf_error("NFLOG link-layer type filtering not implemented");
3376 * Does this link-layer header type have a field
3377 * indicating the type of the next protocol? If
3378 * so, off_linktype.constant_part will be the offset of that
3379 * field in the packet; if not, it will be -1.
3381 if (off_linktype
.constant_part
!= (u_int
)-1) {
3383 * Yes; assume it's an Ethernet type. (If
3384 * it's not, it needs to be handled specially
3387 return gen_cmp(OR_LINKTYPE
, 0, BPF_H
, (bpf_int32
)proto
);
3390 * No; report an error.
3392 description
= pcap_datalink_val_to_description(linktype
);
3393 if (description
!= NULL
) {
3394 bpf_error("%s link-layer type filtering not implemented",
3397 bpf_error("DLT %u link-layer type filtering not implemented",
3406 * Check for an LLC SNAP packet with a given organization code and
3407 * protocol type; we check the entire contents of the 802.2 LLC and
3408 * snap headers, checking for DSAP and SSAP of SNAP and a control
3409 * field of 0x03 in the LLC header, and for the specified organization
3410 * code and protocol type in the SNAP header.
3412 static struct block
*
3413 gen_snap(orgcode
, ptype
)
3414 bpf_u_int32 orgcode
;
3417 u_char snapblock
[8];
3419 snapblock
[0] = LLCSAP_SNAP
; /* DSAP = SNAP */
3420 snapblock
[1] = LLCSAP_SNAP
; /* SSAP = SNAP */
3421 snapblock
[2] = 0x03; /* control = UI */
3422 snapblock
[3] = (orgcode
>> 16); /* upper 8 bits of organization code */
3423 snapblock
[4] = (orgcode
>> 8); /* middle 8 bits of organization code */
3424 snapblock
[5] = (orgcode
>> 0); /* lower 8 bits of organization code */
3425 snapblock
[6] = (ptype
>> 8); /* upper 8 bits of protocol type */
3426 snapblock
[7] = (ptype
>> 0); /* lower 8 bits of protocol type */
3427 return gen_bcmp(OR_LLC
, 0, 8, snapblock
);
3431 * Generate code to match frames with an LLC header.
3436 struct block
*b0
, *b1
;
3442 * We check for an Ethernet type field less than
3443 * 1500, which means it's an 802.3 length field.
3445 b0
= gen_cmp_gt(OR_LINKTYPE
, 0, BPF_H
, ETHERMTU
);
3449 * Now check for the purported DSAP and SSAP not being
3450 * 0xFF, to rule out NetWare-over-802.3.
3452 b1
= gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_int32
)0xFFFF);
3459 * We check for LLC traffic.
3461 b0
= gen_atmtype_abbrev(A_LLC
);
3464 case DLT_IEEE802
: /* Token Ring */
3466 * XXX - check for LLC frames.
3472 * XXX - check for LLC frames.
3476 case DLT_ATM_RFC1483
:
3478 * For LLC encapsulation, these are defined to have an
3481 * For VC encapsulation, they don't, but there's no
3482 * way to check for that; the protocol used on the VC
3483 * is negotiated out of band.
3487 case DLT_IEEE802_11
:
3488 case DLT_PRISM_HEADER
:
3489 case DLT_IEEE802_11_RADIO
:
3490 case DLT_IEEE802_11_RADIO_AVS
:
3493 * Check that we have a data frame.
3495 b0
= gen_check_802_11_data_frame();
3499 bpf_error("'llc' not supported for linktype %d", linktype
);
3507 struct block
*b0
, *b1
;
3511 * Check whether this is an LLC frame.
3516 * Load the control byte and test the low-order bit; it must
3517 * be clear for I frames.
3519 s
= gen_load_a(OR_LLC
, 2, BPF_B
);
3520 b1
= new_block(JMP(BPF_JSET
));
3531 struct block
*b0
, *b1
;
3534 * Check whether this is an LLC frame.
3539 * Now compare the low-order 2 bit of the control byte against
3540 * the appropriate value for S frames.
3542 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_S_FMT
, 0x03);
3550 struct block
*b0
, *b1
;
3553 * Check whether this is an LLC frame.
3558 * Now compare the low-order 2 bit of the control byte against
3559 * the appropriate value for U frames.
3561 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, LLC_U_FMT
, 0x03);
3567 gen_llc_s_subtype(bpf_u_int32 subtype
)
3569 struct block
*b0
, *b1
;
3572 * Check whether this is an LLC frame.
3577 * Now check for an S frame with the appropriate type.
3579 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_S_CMD_MASK
);
3585 gen_llc_u_subtype(bpf_u_int32 subtype
)
3587 struct block
*b0
, *b1
;
3590 * Check whether this is an LLC frame.
3595 * Now check for a U frame with the appropriate type.
3597 b1
= gen_mcmp(OR_LLC
, 2, BPF_B
, subtype
, LLC_U_CMD_MASK
);
3603 * Generate code to match a particular packet type, for link-layer types
3604 * using 802.2 LLC headers.
3606 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3607 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3609 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3610 * value, if <= ETHERMTU. We use that to determine whether to
3611 * match the DSAP or both DSAP and LSAP or to check the OUI and
3612 * protocol ID in a SNAP header.
3614 static struct block
*
3615 gen_llc_linktype(proto
)
3619 * XXX - handle token-ring variable-length header.
3625 case LLCSAP_NETBEUI
:
3627 * XXX - should we check both the DSAP and the
3628 * SSAP, like this, or should we check just the
3629 * DSAP, as we do for other SAP values?
3631 return gen_cmp(OR_LLC
, 0, BPF_H
, (bpf_u_int32
)
3632 ((proto
<< 8) | proto
));
3636 * XXX - are there ever SNAP frames for IPX on
3637 * non-Ethernet 802.x networks?
3639 return gen_cmp(OR_LLC
, 0, BPF_B
,
3640 (bpf_int32
)LLCSAP_IPX
);
3642 case ETHERTYPE_ATALK
:
3644 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3645 * SNAP packets with an organization code of
3646 * 0x080007 (Apple, for Appletalk) and a protocol
3647 * type of ETHERTYPE_ATALK (Appletalk).
3649 * XXX - check for an organization code of
3650 * encapsulated Ethernet as well?
3652 return gen_snap(0x080007, ETHERTYPE_ATALK
);
3656 * XXX - we don't have to check for IPX 802.3
3657 * here, but should we check for the IPX Ethertype?
3659 if (proto
<= ETHERMTU
) {
3661 * This is an LLC SAP value, so check
3664 return gen_cmp(OR_LLC
, 0, BPF_B
, (bpf_int32
)proto
);
3667 * This is an Ethernet type; we assume that it's
3668 * unlikely that it'll appear in the right place
3669 * at random, and therefore check only the
3670 * location that would hold the Ethernet type
3671 * in a SNAP frame with an organization code of
3672 * 0x000000 (encapsulated Ethernet).
3674 * XXX - if we were to check for the SNAP DSAP and
3675 * LSAP, as per XXX, and were also to check for an
3676 * organization code of 0x000000 (encapsulated
3677 * Ethernet), we'd do
3679 * return gen_snap(0x000000, proto);
3681 * here; for now, we don't, as per the above.
3682 * I don't know whether it's worth the extra CPU
3683 * time to do the right check or not.
3685 return gen_cmp(OR_LLC
, 6, BPF_H
, (bpf_int32
)proto
);
3690 static struct block
*
3691 gen_hostop(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3695 u_int src_off
, dst_off
;
3697 struct block
*b0
, *b1
;
3711 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3712 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3718 b0
= gen_hostop(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3719 b1
= gen_hostop(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3726 b0
= gen_linktype(proto
);
3727 b1
= gen_mcmp(OR_LINKPL
, offset
, BPF_W
, (bpf_int32
)addr
, mask
);
3733 static struct block
*
3734 gen_hostop6(addr
, mask
, dir
, proto
, src_off
, dst_off
)
3735 struct in6_addr
*addr
;
3736 struct in6_addr
*mask
;
3738 u_int src_off
, dst_off
;
3740 struct block
*b0
, *b1
;
3755 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3756 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3762 b0
= gen_hostop6(addr
, mask
, Q_SRC
, proto
, src_off
, dst_off
);
3763 b1
= gen_hostop6(addr
, mask
, Q_DST
, proto
, src_off
, dst_off
);
3770 /* this order is important */
3771 a
= (u_int32_t
*)addr
;
3772 m
= (u_int32_t
*)mask
;
3773 b1
= gen_mcmp(OR_LINKPL
, offset
+ 12, BPF_W
, ntohl(a
[3]), ntohl(m
[3]));
3774 b0
= gen_mcmp(OR_LINKPL
, offset
+ 8, BPF_W
, ntohl(a
[2]), ntohl(m
[2]));
3776 b0
= gen_mcmp(OR_LINKPL
, offset
+ 4, BPF_W
, ntohl(a
[1]), ntohl(m
[1]));
3778 b0
= gen_mcmp(OR_LINKPL
, offset
+ 0, BPF_W
, ntohl(a
[0]), ntohl(m
[0]));
3780 b0
= gen_linktype(proto
);
3786 static struct block
*
3787 gen_ehostop(eaddr
, dir
)
3788 register const u_char
*eaddr
;
3791 register struct block
*b0
, *b1
;
3795 return gen_bcmp(OR_LINKHDR
, 6, 6, eaddr
);
3798 return gen_bcmp(OR_LINKHDR
, 0, 6, eaddr
);
3801 b0
= gen_ehostop(eaddr
, Q_SRC
);
3802 b1
= gen_ehostop(eaddr
, Q_DST
);
3808 b0
= gen_ehostop(eaddr
, Q_SRC
);
3809 b1
= gen_ehostop(eaddr
, Q_DST
);
3814 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3818 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3822 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3826 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3830 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3834 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3842 * Like gen_ehostop, but for DLT_FDDI
3844 static struct block
*
3845 gen_fhostop(eaddr
, dir
)
3846 register const u_char
*eaddr
;
3849 struct block
*b0
, *b1
;
3853 return gen_bcmp(OR_LINKHDR
, 6 + 1 + pcap_fddipad
, 6, eaddr
);
3856 return gen_bcmp(OR_LINKHDR
, 0 + 1 + pcap_fddipad
, 6, eaddr
);
3859 b0
= gen_fhostop(eaddr
, Q_SRC
);
3860 b1
= gen_fhostop(eaddr
, Q_DST
);
3866 b0
= gen_fhostop(eaddr
, Q_SRC
);
3867 b1
= gen_fhostop(eaddr
, Q_DST
);
3872 bpf_error("'addr1' is only supported on 802.11");
3876 bpf_error("'addr2' is only supported on 802.11");
3880 bpf_error("'addr3' is only supported on 802.11");
3884 bpf_error("'addr4' is only supported on 802.11");
3888 bpf_error("'ra' is only supported on 802.11");
3892 bpf_error("'ta' is only supported on 802.11");
3900 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3902 static struct block
*
3903 gen_thostop(eaddr
, dir
)
3904 register const u_char
*eaddr
;
3907 register struct block
*b0
, *b1
;
3911 return gen_bcmp(OR_LINKHDR
, 8, 6, eaddr
);
3914 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
3917 b0
= gen_thostop(eaddr
, Q_SRC
);
3918 b1
= gen_thostop(eaddr
, Q_DST
);
3924 b0
= gen_thostop(eaddr
, Q_SRC
);
3925 b1
= gen_thostop(eaddr
, Q_DST
);
3930 bpf_error("'addr1' is only supported on 802.11");
3934 bpf_error("'addr2' is only supported on 802.11");
3938 bpf_error("'addr3' is only supported on 802.11");
3942 bpf_error("'addr4' is only supported on 802.11");
3946 bpf_error("'ra' is only supported on 802.11");
3950 bpf_error("'ta' is only supported on 802.11");
3958 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3959 * various 802.11 + radio headers.
3961 static struct block
*
3962 gen_wlanhostop(eaddr
, dir
)
3963 register const u_char
*eaddr
;
3966 register struct block
*b0
, *b1
, *b2
;
3967 register struct slist
*s
;
3969 #ifdef ENABLE_WLAN_FILTERING_PATCH
3972 * We need to disable the optimizer because the optimizer is buggy
3973 * and wipes out some LD instructions generated by the below
3974 * code to validate the Frame Control bits
3977 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3984 * For control frames, there is no SA.
3986 * For management frames, SA is at an
3987 * offset of 10 from the beginning of
3990 * For data frames, SA is at an offset
3991 * of 10 from the beginning of the packet
3992 * if From DS is clear, at an offset of
3993 * 16 from the beginning of the packet
3994 * if From DS is set and To DS is clear,
3995 * and an offset of 24 from the beginning
3996 * of the packet if From DS is set and To DS
4001 * Generate the tests to be done for data frames
4004 * First, check for To DS set, i.e. check "link[1] & 0x01".
4006 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4007 b1
= new_block(JMP(BPF_JSET
));
4008 b1
->s
.k
= 0x01; /* To DS */
4012 * If To DS is set, the SA is at 24.
4014 b0
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4018 * Now, check for To DS not set, i.e. check
4019 * "!(link[1] & 0x01)".
4021 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4022 b2
= new_block(JMP(BPF_JSET
));
4023 b2
->s
.k
= 0x01; /* To DS */
4028 * If To DS is not set, the SA is at 16.
4030 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4034 * Now OR together the last two checks. That gives
4035 * the complete set of checks for data frames with
4041 * Now check for From DS being set, and AND that with
4042 * the ORed-together checks.
4044 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4045 b1
= new_block(JMP(BPF_JSET
));
4046 b1
->s
.k
= 0x02; /* From DS */
4051 * Now check for data frames with From DS not set.
4053 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4054 b2
= new_block(JMP(BPF_JSET
));
4055 b2
->s
.k
= 0x02; /* From DS */
4060 * If From DS isn't set, the SA is at 10.
4062 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4066 * Now OR together the checks for data frames with
4067 * From DS not set and for data frames with From DS
4068 * set; that gives the checks done for data frames.
4073 * Now check for a data frame.
4074 * I.e, check "link[0] & 0x08".
4076 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4077 b1
= new_block(JMP(BPF_JSET
));
4082 * AND that with the checks done for data frames.
4087 * If the high-order bit of the type value is 0, this
4088 * is a management frame.
4089 * I.e, check "!(link[0] & 0x08)".
4091 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4092 b2
= new_block(JMP(BPF_JSET
));
4098 * For management frames, the SA is at 10.
4100 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4104 * OR that with the checks done for data frames.
4105 * That gives the checks done for management and
4111 * If the low-order bit of the type value is 1,
4112 * this is either a control frame or a frame
4113 * with a reserved type, and thus not a
4116 * I.e., check "!(link[0] & 0x04)".
4118 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4119 b1
= new_block(JMP(BPF_JSET
));
4125 * AND that with the checks for data and management
4135 * For control frames, there is no DA.
4137 * For management frames, DA is at an
4138 * offset of 4 from the beginning of
4141 * For data frames, DA is at an offset
4142 * of 4 from the beginning of the packet
4143 * if To DS is clear and at an offset of
4144 * 16 from the beginning of the packet
4149 * Generate the tests to be done for data frames.
4151 * First, check for To DS set, i.e. "link[1] & 0x01".
4153 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4154 b1
= new_block(JMP(BPF_JSET
));
4155 b1
->s
.k
= 0x01; /* To DS */
4159 * If To DS is set, the DA is at 16.
4161 b0
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4165 * Now, check for To DS not set, i.e. check
4166 * "!(link[1] & 0x01)".
4168 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
4169 b2
= new_block(JMP(BPF_JSET
));
4170 b2
->s
.k
= 0x01; /* To DS */
4175 * If To DS is not set, the DA is at 4.
4177 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4181 * Now OR together the last two checks. That gives
4182 * the complete set of checks for data frames.
4187 * Now check for a data frame.
4188 * I.e, check "link[0] & 0x08".
4190 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4191 b1
= new_block(JMP(BPF_JSET
));
4196 * AND that with the checks done for data frames.
4201 * If the high-order bit of the type value is 0, this
4202 * is a management frame.
4203 * I.e, check "!(link[0] & 0x08)".
4205 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4206 b2
= new_block(JMP(BPF_JSET
));
4212 * For management frames, the DA is at 4.
4214 b1
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4218 * OR that with the checks done for data frames.
4219 * That gives the checks done for management and
4225 * If the low-order bit of the type value is 1,
4226 * this is either a control frame or a frame
4227 * with a reserved type, and thus not a
4230 * I.e., check "!(link[0] & 0x04)".
4232 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4233 b1
= new_block(JMP(BPF_JSET
));
4239 * AND that with the checks for data and management
4247 * Not present in management frames; addr1 in other
4252 * If the high-order bit of the type value is 0, this
4253 * is a management frame.
4254 * I.e, check "(link[0] & 0x08)".
4256 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4257 b1
= new_block(JMP(BPF_JSET
));
4264 b0
= gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
);
4267 * AND that with the check of addr1.
4274 * Not present in management frames; addr2, if present,
4279 * Not present in CTS or ACK control frames.
4281 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4282 IEEE80211_FC0_TYPE_MASK
);
4284 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4285 IEEE80211_FC0_SUBTYPE_MASK
);
4287 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4288 IEEE80211_FC0_SUBTYPE_MASK
);
4294 * If the high-order bit of the type value is 0, this
4295 * is a management frame.
4296 * I.e, check "(link[0] & 0x08)".
4298 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
4299 b1
= new_block(JMP(BPF_JSET
));
4304 * AND that with the check for frames other than
4305 * CTS and ACK frames.
4312 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4317 * XXX - add BSSID keyword?
4320 return (gen_bcmp(OR_LINKHDR
, 4, 6, eaddr
));
4324 * Not present in CTS or ACK control frames.
4326 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4327 IEEE80211_FC0_TYPE_MASK
);
4329 b1
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_CTS
,
4330 IEEE80211_FC0_SUBTYPE_MASK
);
4332 b2
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_SUBTYPE_ACK
,
4333 IEEE80211_FC0_SUBTYPE_MASK
);
4337 b1
= gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4343 * Not present in control frames.
4345 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, IEEE80211_FC0_TYPE_CTL
,
4346 IEEE80211_FC0_TYPE_MASK
);
4348 b1
= gen_bcmp(OR_LINKHDR
, 16, 6, eaddr
);
4354 * Present only if the direction mask has both "From DS"
4355 * and "To DS" set. Neither control frames nor management
4356 * frames should have both of those set, so we don't
4357 * check the frame type.
4359 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
,
4360 IEEE80211_FC1_DIR_DSTODS
, IEEE80211_FC1_DIR_MASK
);
4361 b1
= gen_bcmp(OR_LINKHDR
, 24, 6, eaddr
);
4366 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4367 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4373 b0
= gen_wlanhostop(eaddr
, Q_SRC
);
4374 b1
= gen_wlanhostop(eaddr
, Q_DST
);
4383 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4384 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4385 * as the RFC states.)
4387 static struct block
*
4388 gen_ipfchostop(eaddr
, dir
)
4389 register const u_char
*eaddr
;
4392 register struct block
*b0
, *b1
;
4396 return gen_bcmp(OR_LINKHDR
, 10, 6, eaddr
);
4399 return gen_bcmp(OR_LINKHDR
, 2, 6, eaddr
);
4402 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4403 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4409 b0
= gen_ipfchostop(eaddr
, Q_SRC
);
4410 b1
= gen_ipfchostop(eaddr
, Q_DST
);
4415 bpf_error("'addr1' is only supported on 802.11");
4419 bpf_error("'addr2' is only supported on 802.11");
4423 bpf_error("'addr3' is only supported on 802.11");
4427 bpf_error("'addr4' is only supported on 802.11");
4431 bpf_error("'ra' is only supported on 802.11");
4435 bpf_error("'ta' is only supported on 802.11");
4443 * This is quite tricky because there may be pad bytes in front of the
4444 * DECNET header, and then there are two possible data packet formats that
4445 * carry both src and dst addresses, plus 5 packet types in a format that
4446 * carries only the src node, plus 2 types that use a different format and
4447 * also carry just the src node.
4451 * Instead of doing those all right, we just look for data packets with
4452 * 0 or 1 bytes of padding. If you want to look at other packets, that
4453 * will require a lot more hacking.
4455 * To add support for filtering on DECNET "areas" (network numbers)
4456 * one would want to add a "mask" argument to this routine. That would
4457 * make the filter even more inefficient, although one could be clever
4458 * and not generate masking instructions if the mask is 0xFFFF.
4460 static struct block
*
4461 gen_dnhostop(addr
, dir
)
4465 struct block
*b0
, *b1
, *b2
, *tmp
;
4466 u_int offset_lh
; /* offset if long header is received */
4467 u_int offset_sh
; /* offset if short header is received */
4472 offset_sh
= 1; /* follows flags */
4473 offset_lh
= 7; /* flgs,darea,dsubarea,HIORD */
4477 offset_sh
= 3; /* follows flags, dstnode */
4478 offset_lh
= 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4482 /* Inefficient because we do our Calvinball dance twice */
4483 b0
= gen_dnhostop(addr
, Q_SRC
);
4484 b1
= gen_dnhostop(addr
, Q_DST
);
4490 /* Inefficient because we do our Calvinball dance twice */
4491 b0
= gen_dnhostop(addr
, Q_SRC
);
4492 b1
= gen_dnhostop(addr
, Q_DST
);
4497 bpf_error("ISO host filtering not implemented");
4502 b0
= gen_linktype(ETHERTYPE_DN
);
4503 /* Check for pad = 1, long header case */
4504 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4505 (bpf_int32
)ntohs(0x0681), (bpf_int32
)ntohs(0x07FF));
4506 b1
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_lh
,
4507 BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4509 /* Check for pad = 0, long header case */
4510 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x06, (bpf_int32
)0x7);
4511 b2
= gen_cmp(OR_LINKPL
, 2 + offset_lh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4514 /* Check for pad = 1, short header case */
4515 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_H
,
4516 (bpf_int32
)ntohs(0x0281), (bpf_int32
)ntohs(0x07FF));
4517 b2
= gen_cmp(OR_LINKPL
, 2 + 1 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4520 /* Check for pad = 0, short header case */
4521 tmp
= gen_mcmp(OR_LINKPL
, 2, BPF_B
, (bpf_int32
)0x02, (bpf_int32
)0x7);
4522 b2
= gen_cmp(OR_LINKPL
, 2 + offset_sh
, BPF_H
, (bpf_int32
)ntohs((u_short
)addr
));
4526 /* Combine with test for linktype */
4532 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4533 * test the bottom-of-stack bit, and then check the version number
4534 * field in the IP header.
4536 static struct block
*
4537 gen_mpls_linktype(proto
)
4540 struct block
*b0
, *b1
;
4545 /* match the bottom-of-stack bit */
4546 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4547 /* match the IPv4 version number */
4548 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x40, 0xf0);
4553 /* match the bottom-of-stack bit */
4554 b0
= gen_mcmp(OR_LINKPL
, -2, BPF_B
, 0x01, 0x01);
4555 /* match the IPv4 version number */
4556 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_B
, 0x60, 0xf0);
4565 static struct block
*
4566 gen_host(addr
, mask
, proto
, dir
, type
)
4573 struct block
*b0
, *b1
;
4574 const char *typestr
;
4584 b0
= gen_host(addr
, mask
, Q_IP
, dir
, type
);
4586 * Only check for non-IPv4 addresses if we're not
4587 * checking MPLS-encapsulated packets.
4589 if (label_stack_depth
== 0) {
4590 b1
= gen_host(addr
, mask
, Q_ARP
, dir
, type
);
4592 b0
= gen_host(addr
, mask
, Q_RARP
, dir
, type
);
4598 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_IP
, 12, 16);
4601 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_REVARP
, 14, 24);
4604 return gen_hostop(addr
, mask
, dir
, ETHERTYPE_ARP
, 14, 24);
4607 bpf_error("'tcp' modifier applied to %s", typestr
);
4610 bpf_error("'sctp' modifier applied to %s", typestr
);
4613 bpf_error("'udp' modifier applied to %s", typestr
);
4616 bpf_error("'icmp' modifier applied to %s", typestr
);
4619 bpf_error("'igmp' modifier applied to %s", typestr
);
4622 bpf_error("'igrp' modifier applied to %s", typestr
);
4625 bpf_error("'pim' modifier applied to %s", typestr
);
4628 bpf_error("'vrrp' modifier applied to %s", typestr
);
4631 bpf_error("'carp' modifier applied to %s", typestr
);
4634 bpf_error("ATALK host filtering not implemented");
4637 bpf_error("AARP host filtering not implemented");
4640 return gen_dnhostop(addr
, dir
);
4643 bpf_error("SCA host filtering not implemented");
4646 bpf_error("LAT host filtering not implemented");
4649 bpf_error("MOPDL host filtering not implemented");
4652 bpf_error("MOPRC host filtering not implemented");
4655 bpf_error("'ip6' modifier applied to ip host");
4658 bpf_error("'icmp6' modifier applied to %s", typestr
);
4661 bpf_error("'ah' modifier applied to %s", typestr
);
4664 bpf_error("'esp' modifier applied to %s", typestr
);
4667 bpf_error("ISO host filtering not implemented");
4670 bpf_error("'esis' modifier applied to %s", typestr
);
4673 bpf_error("'isis' modifier applied to %s", typestr
);
4676 bpf_error("'clnp' modifier applied to %s", typestr
);
4679 bpf_error("'stp' modifier applied to %s", typestr
);
4682 bpf_error("IPX host filtering not implemented");
4685 bpf_error("'netbeui' modifier applied to %s", typestr
);
4688 bpf_error("'radio' modifier applied to %s", typestr
);
4697 static struct block
*
4698 gen_host6(addr
, mask
, proto
, dir
, type
)
4699 struct in6_addr
*addr
;
4700 struct in6_addr
*mask
;
4705 const char *typestr
;
4715 return gen_host6(addr
, mask
, Q_IPV6
, dir
, type
);
4718 bpf_error("link-layer modifier applied to ip6 %s", typestr
);
4721 bpf_error("'ip' modifier applied to ip6 %s", typestr
);
4724 bpf_error("'rarp' modifier applied to ip6 %s", typestr
);
4727 bpf_error("'arp' modifier applied to ip6 %s", typestr
);
4730 bpf_error("'sctp' modifier applied to %s", typestr
);
4733 bpf_error("'tcp' modifier applied to %s", typestr
);
4736 bpf_error("'udp' modifier applied to %s", typestr
);
4739 bpf_error("'icmp' modifier applied to %s", typestr
);
4742 bpf_error("'igmp' modifier applied to %s", typestr
);
4745 bpf_error("'igrp' modifier applied to %s", typestr
);
4748 bpf_error("'pim' modifier applied to %s", typestr
);
4751 bpf_error("'vrrp' modifier applied to %s", typestr
);
4754 bpf_error("'carp' modifier applied to %s", typestr
);
4757 bpf_error("ATALK host filtering not implemented");
4760 bpf_error("AARP host filtering not implemented");
4763 bpf_error("'decnet' modifier applied to ip6 %s", typestr
);
4766 bpf_error("SCA host filtering not implemented");
4769 bpf_error("LAT host filtering not implemented");
4772 bpf_error("MOPDL host filtering not implemented");
4775 bpf_error("MOPRC host filtering not implemented");
4778 return gen_hostop6(addr
, mask
, dir
, ETHERTYPE_IPV6
, 8, 24);
4781 bpf_error("'icmp6' modifier applied to %s", typestr
);
4784 bpf_error("'ah' modifier applied to %s", typestr
);
4787 bpf_error("'esp' modifier applied to %s", typestr
);
4790 bpf_error("ISO host filtering not implemented");
4793 bpf_error("'esis' modifier applied to %s", typestr
);
4796 bpf_error("'isis' modifier applied to %s", typestr
);
4799 bpf_error("'clnp' modifier applied to %s", typestr
);
4802 bpf_error("'stp' modifier applied to %s", typestr
);
4805 bpf_error("IPX host filtering not implemented");
4808 bpf_error("'netbeui' modifier applied to %s", typestr
);
4811 bpf_error("'radio' modifier applied to %s", typestr
);
4821 static struct block
*
4822 gen_gateway(eaddr
, alist
, proto
, dir
)
4823 const u_char
*eaddr
;
4824 bpf_u_int32
**alist
;
4828 struct block
*b0
, *b1
, *tmp
;
4831 bpf_error("direction applied to 'gateway'");
4840 case DLT_NETANALYZER
:
4841 case DLT_NETANALYZER_TRANSPARENT
:
4842 b1
= gen_prevlinkhdr_check();
4843 b0
= gen_ehostop(eaddr
, Q_OR
);
4848 b0
= gen_fhostop(eaddr
, Q_OR
);
4851 b0
= gen_thostop(eaddr
, Q_OR
);
4853 case DLT_IEEE802_11
:
4854 case DLT_PRISM_HEADER
:
4855 case DLT_IEEE802_11_RADIO_AVS
:
4856 case DLT_IEEE802_11_RADIO
:
4858 b0
= gen_wlanhostop(eaddr
, Q_OR
);
4862 * This is LLC-multiplexed traffic; if it were
4863 * LANE, linktype would have been set to
4867 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4869 case DLT_IP_OVER_FC
:
4870 b0
= gen_ipfchostop(eaddr
, Q_OR
);
4874 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4876 b1
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
, Q_HOST
);
4878 tmp
= gen_host(**alist
++, 0xffffffff, proto
, Q_OR
,
4887 bpf_error("illegal modifier of 'gateway'");
4893 gen_proto_abbrev(proto
)
4902 b1
= gen_proto(IPPROTO_SCTP
, Q_IP
, Q_DEFAULT
);
4903 b0
= gen_proto(IPPROTO_SCTP
, Q_IPV6
, Q_DEFAULT
);
4908 b1
= gen_proto(IPPROTO_TCP
, Q_IP
, Q_DEFAULT
);
4909 b0
= gen_proto(IPPROTO_TCP
, Q_IPV6
, Q_DEFAULT
);
4914 b1
= gen_proto(IPPROTO_UDP
, Q_IP
, Q_DEFAULT
);
4915 b0
= gen_proto(IPPROTO_UDP
, Q_IPV6
, Q_DEFAULT
);
4920 b1
= gen_proto(IPPROTO_ICMP
, Q_IP
, Q_DEFAULT
);
4923 #ifndef IPPROTO_IGMP
4924 #define IPPROTO_IGMP 2
4928 b1
= gen_proto(IPPROTO_IGMP
, Q_IP
, Q_DEFAULT
);
4931 #ifndef IPPROTO_IGRP
4932 #define IPPROTO_IGRP 9
4935 b1
= gen_proto(IPPROTO_IGRP
, Q_IP
, Q_DEFAULT
);
4939 #define IPPROTO_PIM 103
4943 b1
= gen_proto(IPPROTO_PIM
, Q_IP
, Q_DEFAULT
);
4944 b0
= gen_proto(IPPROTO_PIM
, Q_IPV6
, Q_DEFAULT
);
4948 #ifndef IPPROTO_VRRP
4949 #define IPPROTO_VRRP 112
4953 b1
= gen_proto(IPPROTO_VRRP
, Q_IP
, Q_DEFAULT
);
4956 #ifndef IPPROTO_CARP
4957 #define IPPROTO_CARP 112
4961 b1
= gen_proto(IPPROTO_CARP
, Q_IP
, Q_DEFAULT
);
4965 b1
= gen_linktype(ETHERTYPE_IP
);
4969 b1
= gen_linktype(ETHERTYPE_ARP
);
4973 b1
= gen_linktype(ETHERTYPE_REVARP
);
4977 bpf_error("link layer applied in wrong context");
4980 b1
= gen_linktype(ETHERTYPE_ATALK
);
4984 b1
= gen_linktype(ETHERTYPE_AARP
);
4988 b1
= gen_linktype(ETHERTYPE_DN
);
4992 b1
= gen_linktype(ETHERTYPE_SCA
);
4996 b1
= gen_linktype(ETHERTYPE_LAT
);
5000 b1
= gen_linktype(ETHERTYPE_MOPDL
);
5004 b1
= gen_linktype(ETHERTYPE_MOPRC
);
5008 b1
= gen_linktype(ETHERTYPE_IPV6
);
5011 #ifndef IPPROTO_ICMPV6
5012 #define IPPROTO_ICMPV6 58
5015 b1
= gen_proto(IPPROTO_ICMPV6
, Q_IPV6
, Q_DEFAULT
);
5019 #define IPPROTO_AH 51
5022 b1
= gen_proto(IPPROTO_AH
, Q_IP
, Q_DEFAULT
);
5023 b0
= gen_proto(IPPROTO_AH
, Q_IPV6
, Q_DEFAULT
);
5028 #define IPPROTO_ESP 50
5031 b1
= gen_proto(IPPROTO_ESP
, Q_IP
, Q_DEFAULT
);
5032 b0
= gen_proto(IPPROTO_ESP
, Q_IPV6
, Q_DEFAULT
);
5037 b1
= gen_linktype(LLCSAP_ISONS
);
5041 b1
= gen_proto(ISO9542_ESIS
, Q_ISO
, Q_DEFAULT
);
5045 b1
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
5048 case Q_ISIS_L1
: /* all IS-IS Level1 PDU-Types */
5049 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5050 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5052 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5054 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5056 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5060 case Q_ISIS_L2
: /* all IS-IS Level2 PDU-Types */
5061 b0
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5062 b1
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
); /* FIXME extract the circuit-type bits */
5064 b0
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5066 b0
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5068 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5072 case Q_ISIS_IIH
: /* all IS-IS Hello PDU-Types */
5073 b0
= gen_proto(ISIS_L1_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5074 b1
= gen_proto(ISIS_L2_LAN_IIH
, Q_ISIS
, Q_DEFAULT
);
5076 b0
= gen_proto(ISIS_PTP_IIH
, Q_ISIS
, Q_DEFAULT
);
5081 b0
= gen_proto(ISIS_L1_LSP
, Q_ISIS
, Q_DEFAULT
);
5082 b1
= gen_proto(ISIS_L2_LSP
, Q_ISIS
, Q_DEFAULT
);
5087 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5088 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5090 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5092 b0
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5097 b0
= gen_proto(ISIS_L1_CSNP
, Q_ISIS
, Q_DEFAULT
);
5098 b1
= gen_proto(ISIS_L2_CSNP
, Q_ISIS
, Q_DEFAULT
);
5103 b0
= gen_proto(ISIS_L1_PSNP
, Q_ISIS
, Q_DEFAULT
);
5104 b1
= gen_proto(ISIS_L2_PSNP
, Q_ISIS
, Q_DEFAULT
);
5109 b1
= gen_proto(ISO8473_CLNP
, Q_ISO
, Q_DEFAULT
);
5113 b1
= gen_linktype(LLCSAP_8021D
);
5117 b1
= gen_linktype(LLCSAP_IPX
);
5121 b1
= gen_linktype(LLCSAP_NETBEUI
);
5125 bpf_error("'radio' is not a valid protocol type");
5133 static struct block
*
5139 /* not IPv4 frag other than the first frag */
5140 s
= gen_load_a(OR_LINKPL
, 6, BPF_H
);
5141 b
= new_block(JMP(BPF_JSET
));
5150 * Generate a comparison to a port value in the transport-layer header
5151 * at the specified offset from the beginning of that header.
5153 * XXX - this handles a variable-length prefix preceding the link-layer
5154 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5155 * variable-length link-layer headers (such as Token Ring or 802.11
5158 static struct block
*
5159 gen_portatom(off
, v
)
5163 return gen_cmp(OR_TRAN_IPV4
, off
, BPF_H
, v
);
5166 static struct block
*
5167 gen_portatom6(off
, v
)
5171 return gen_cmp(OR_TRAN_IPV6
, off
, BPF_H
, v
);
5175 gen_portop(port
, proto
, dir
)
5176 int port
, proto
, dir
;
5178 struct block
*b0
, *b1
, *tmp
;
5180 /* ip proto 'proto' and not a fragment other than the first fragment */
5181 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5187 b1
= gen_portatom(0, (bpf_int32
)port
);
5191 b1
= gen_portatom(2, (bpf_int32
)port
);
5196 tmp
= gen_portatom(0, (bpf_int32
)port
);
5197 b1
= gen_portatom(2, (bpf_int32
)port
);
5202 tmp
= gen_portatom(0, (bpf_int32
)port
);
5203 b1
= gen_portatom(2, (bpf_int32
)port
);
5215 static struct block
*
5216 gen_port(port
, ip_proto
, dir
)
5221 struct block
*b0
, *b1
, *tmp
;
5226 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5227 * not LLC encapsulation with LLCSAP_IP.
5229 * For IEEE 802 networks - which includes 802.5 token ring
5230 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5231 * says that SNAP encapsulation is used, not LLC encapsulation
5234 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5235 * RFC 2225 say that SNAP encapsulation is used, not LLC
5236 * encapsulation with LLCSAP_IP.
5238 * So we always check for ETHERTYPE_IP.
5240 b0
= gen_linktype(ETHERTYPE_IP
);
5246 b1
= gen_portop(port
, ip_proto
, dir
);
5250 tmp
= gen_portop(port
, IPPROTO_TCP
, dir
);
5251 b1
= gen_portop(port
, IPPROTO_UDP
, dir
);
5253 tmp
= gen_portop(port
, IPPROTO_SCTP
, dir
);
5265 gen_portop6(port
, proto
, dir
)
5266 int port
, proto
, dir
;
5268 struct block
*b0
, *b1
, *tmp
;
5270 /* ip6 proto 'proto' */
5271 /* XXX - catch the first fragment of a fragmented packet? */
5272 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5276 b1
= gen_portatom6(0, (bpf_int32
)port
);
5280 b1
= gen_portatom6(2, (bpf_int32
)port
);
5285 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5286 b1
= gen_portatom6(2, (bpf_int32
)port
);
5291 tmp
= gen_portatom6(0, (bpf_int32
)port
);
5292 b1
= gen_portatom6(2, (bpf_int32
)port
);
5304 static struct block
*
5305 gen_port6(port
, ip_proto
, dir
)
5310 struct block
*b0
, *b1
, *tmp
;
5312 /* link proto ip6 */
5313 b0
= gen_linktype(ETHERTYPE_IPV6
);
5319 b1
= gen_portop6(port
, ip_proto
, dir
);
5323 tmp
= gen_portop6(port
, IPPROTO_TCP
, dir
);
5324 b1
= gen_portop6(port
, IPPROTO_UDP
, dir
);
5326 tmp
= gen_portop6(port
, IPPROTO_SCTP
, dir
);
5337 /* gen_portrange code */
5338 static struct block
*
5339 gen_portrangeatom(off
, v1
, v2
)
5343 struct block
*b1
, *b2
;
5347 * Reverse the order of the ports, so v1 is the lower one.
5356 b1
= gen_cmp_ge(OR_TRAN_IPV4
, off
, BPF_H
, v1
);
5357 b2
= gen_cmp_le(OR_TRAN_IPV4
, off
, BPF_H
, v2
);
5365 gen_portrangeop(port1
, port2
, proto
, dir
)
5370 struct block
*b0
, *b1
, *tmp
;
5372 /* ip proto 'proto' and not a fragment other than the first fragment */
5373 tmp
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)proto
);
5379 b1
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5383 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5388 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5389 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5394 tmp
= gen_portrangeatom(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5395 b1
= gen_portrangeatom(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5407 static struct block
*
5408 gen_portrange(port1
, port2
, ip_proto
, dir
)
5413 struct block
*b0
, *b1
, *tmp
;
5416 b0
= gen_linktype(ETHERTYPE_IP
);
5422 b1
= gen_portrangeop(port1
, port2
, ip_proto
, dir
);
5426 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_TCP
, dir
);
5427 b1
= gen_portrangeop(port1
, port2
, IPPROTO_UDP
, dir
);
5429 tmp
= gen_portrangeop(port1
, port2
, IPPROTO_SCTP
, dir
);
5440 static struct block
*
5441 gen_portrangeatom6(off
, v1
, v2
)
5445 struct block
*b1
, *b2
;
5449 * Reverse the order of the ports, so v1 is the lower one.
5458 b1
= gen_cmp_ge(OR_TRAN_IPV6
, off
, BPF_H
, v1
);
5459 b2
= gen_cmp_le(OR_TRAN_IPV6
, off
, BPF_H
, v2
);
5467 gen_portrangeop6(port1
, port2
, proto
, dir
)
5472 struct block
*b0
, *b1
, *tmp
;
5474 /* ip6 proto 'proto' */
5475 /* XXX - catch the first fragment of a fragmented packet? */
5476 b0
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)proto
);
5480 b1
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5484 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5489 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5490 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5495 tmp
= gen_portrangeatom6(0, (bpf_int32
)port1
, (bpf_int32
)port2
);
5496 b1
= gen_portrangeatom6(2, (bpf_int32
)port1
, (bpf_int32
)port2
);
5508 static struct block
*
5509 gen_portrange6(port1
, port2
, ip_proto
, dir
)
5514 struct block
*b0
, *b1
, *tmp
;
5516 /* link proto ip6 */
5517 b0
= gen_linktype(ETHERTYPE_IPV6
);
5523 b1
= gen_portrangeop6(port1
, port2
, ip_proto
, dir
);
5527 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_TCP
, dir
);
5528 b1
= gen_portrangeop6(port1
, port2
, IPPROTO_UDP
, dir
);
5530 tmp
= gen_portrangeop6(port1
, port2
, IPPROTO_SCTP
, dir
);
5542 lookup_proto(name
, proto
)
5543 register const char *name
;
5553 v
= pcap_nametoproto(name
);
5554 if (v
== PROTO_UNDEF
)
5555 bpf_error("unknown ip proto '%s'", name
);
5559 /* XXX should look up h/w protocol type based on linktype */
5560 v
= pcap_nametoeproto(name
);
5561 if (v
== PROTO_UNDEF
) {
5562 v
= pcap_nametollc(name
);
5563 if (v
== PROTO_UNDEF
)
5564 bpf_error("unknown ether proto '%s'", name
);
5569 if (strcmp(name
, "esis") == 0)
5571 else if (strcmp(name
, "isis") == 0)
5573 else if (strcmp(name
, "clnp") == 0)
5576 bpf_error("unknown osi proto '%s'", name
);
5596 static struct block
*
5597 gen_protochain(v
, proto
, dir
)
5602 #ifdef NO_PROTOCHAIN
5603 return gen_proto(v
, proto
, dir
);
5605 struct block
*b0
, *b
;
5606 struct slist
*s
[100];
5607 int fix2
, fix3
, fix4
, fix5
;
5608 int ahcheck
, again
, end
;
5610 int reg2
= alloc_reg();
5612 memset(s
, 0, sizeof(s
));
5613 fix3
= fix4
= fix5
= 0;
5620 b0
= gen_protochain(v
, Q_IP
, dir
);
5621 b
= gen_protochain(v
, Q_IPV6
, dir
);
5625 bpf_error("bad protocol applied for 'protochain'");
5630 * We don't handle variable-length prefixes before the link-layer
5631 * header, or variable-length link-layer headers, here yet.
5632 * We might want to add BPF instructions to do the protochain
5633 * work, to simplify that and, on platforms that have a BPF
5634 * interpreter with the new instructions, let the filtering
5635 * be done in the kernel. (We already require a modified BPF
5636 * engine to do the protochain stuff, to support backward
5637 * branches, and backward branch support is unlikely to appear
5638 * in kernel BPF engines.)
5640 if (off_linkpl
.is_variable
)
5641 bpf_error("'protochain' not supported with variable length headers");
5643 no_optimize
= 1; /*this code is not compatible with optimzer yet */
5646 * s[0] is a dummy entry to protect other BPF insn from damage
5647 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5648 * hard to find interdependency made by jump table fixup.
5651 s
[i
] = new_stmt(0); /*dummy*/
5656 b0
= gen_linktype(ETHERTYPE_IP
);
5659 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5660 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 9;
5662 /* X = ip->ip_hl << 2 */
5663 s
[i
] = new_stmt(BPF_LDX
|BPF_MSH
|BPF_B
);
5664 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5669 b0
= gen_linktype(ETHERTYPE_IPV6
);
5671 /* A = ip6->ip_nxt */
5672 s
[i
] = new_stmt(BPF_LD
|BPF_ABS
|BPF_B
);
5673 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 6;
5675 /* X = sizeof(struct ip6_hdr) */
5676 s
[i
] = new_stmt(BPF_LDX
|BPF_IMM
);
5682 bpf_error("unsupported proto to gen_protochain");
5686 /* again: if (A == v) goto end; else fall through; */
5688 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5690 s
[i
]->s
.jt
= NULL
; /*later*/
5691 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5695 #ifndef IPPROTO_NONE
5696 #define IPPROTO_NONE 59
5698 /* if (A == IPPROTO_NONE) goto end */
5699 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5700 s
[i
]->s
.jt
= NULL
; /*later*/
5701 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5702 s
[i
]->s
.k
= IPPROTO_NONE
;
5703 s
[fix5
]->s
.jf
= s
[i
];
5707 if (proto
== Q_IPV6
) {
5708 int v6start
, v6end
, v6advance
, j
;
5711 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
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_HOPOPTS
;
5716 s
[fix2
]->s
.jf
= s
[i
];
5718 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5719 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5720 s
[i
]->s
.jt
= NULL
; /*later*/
5721 s
[i
]->s
.jf
= NULL
; /*update in next stmt*/
5722 s
[i
]->s
.k
= IPPROTO_DSTOPTS
;
5724 /* if (A == IPPROTO_ROUTING) goto v6advance */
5725 s
[i
- 1]->s
.jf
= 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_ROUTING
;
5730 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5731 s
[i
- 1]->s
.jf
= s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5732 s
[i
]->s
.jt
= NULL
; /*later*/
5733 s
[i
]->s
.jf
= NULL
; /*later*/
5734 s
[i
]->s
.k
= IPPROTO_FRAGMENT
;
5744 * A = P[X + packet head];
5745 * X = X + (P[X + packet head + 1] + 1) * 8;
5747 /* A = P[X + packet head] */
5748 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5749 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5752 s
[i
] = new_stmt(BPF_ST
);
5755 /* A = P[X + packet head + 1]; */
5756 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5757 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 1;
5760 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5764 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5768 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
5772 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5775 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5779 /* goto again; (must use BPF_JA for backward jump) */
5780 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5781 s
[i
]->s
.k
= again
- i
- 1;
5782 s
[i
- 1]->s
.jf
= s
[i
];
5786 for (j
= v6start
; j
<= v6end
; j
++)
5787 s
[j
]->s
.jt
= s
[v6advance
];
5790 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5792 s
[fix2
]->s
.jf
= s
[i
];
5798 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5799 s
[i
] = new_stmt(BPF_JMP
|BPF_JEQ
|BPF_K
);
5800 s
[i
]->s
.jt
= NULL
; /*later*/
5801 s
[i
]->s
.jf
= NULL
; /*later*/
5802 s
[i
]->s
.k
= IPPROTO_AH
;
5804 s
[fix3
]->s
.jf
= s
[ahcheck
];
5811 * X = X + (P[X + 1] + 2) * 4;
5814 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5816 /* A = P[X + packet head]; */
5817 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5818 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5821 s
[i
] = new_stmt(BPF_ST
);
5825 s
[i
- 1]->s
.jt
= s
[i
] = new_stmt(BPF_MISC
|BPF_TXA
);
5828 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5832 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5834 /* A = P[X + packet head] */
5835 s
[i
] = new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
5836 s
[i
]->s
.k
= off_linkpl
.constant_part
+ off_nl
;
5839 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5843 s
[i
] = new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
5847 s
[i
] = new_stmt(BPF_MISC
|BPF_TAX
);
5850 s
[i
] = new_stmt(BPF_LD
|BPF_MEM
);
5854 /* goto again; (must use BPF_JA for backward jump) */
5855 s
[i
] = new_stmt(BPF_JMP
|BPF_JA
);
5856 s
[i
]->s
.k
= again
- i
- 1;
5861 s
[i
] = new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
5863 s
[fix2
]->s
.jt
= s
[end
];
5864 s
[fix4
]->s
.jf
= s
[end
];
5865 s
[fix5
]->s
.jt
= s
[end
];
5872 for (i
= 0; i
< max
- 1; i
++)
5873 s
[i
]->next
= s
[i
+ 1];
5874 s
[max
- 1]->next
= NULL
;
5879 b
= new_block(JMP(BPF_JEQ
));
5880 b
->stmts
= s
[1]; /*remember, s[0] is dummy*/
5890 static struct block
*
5891 gen_check_802_11_data_frame()
5894 struct block
*b0
, *b1
;
5897 * A data frame has the 0x08 bit (b3) in the frame control field set
5898 * and the 0x04 bit (b2) clear.
5900 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5901 b0
= new_block(JMP(BPF_JSET
));
5905 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
5906 b1
= new_block(JMP(BPF_JSET
));
5917 * Generate code that checks whether the packet is a packet for protocol
5918 * <proto> and whether the type field in that protocol's header has
5919 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5920 * IP packet and checks the protocol number in the IP header against <v>.
5922 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5923 * against Q_IP and Q_IPV6.
5925 static struct block
*
5926 gen_proto(v
, proto
, dir
)
5931 struct block
*b0
, *b1
;
5936 if (dir
!= Q_DEFAULT
)
5937 bpf_error("direction applied to 'proto'");
5941 b0
= gen_proto(v
, Q_IP
, dir
);
5942 b1
= gen_proto(v
, Q_IPV6
, dir
);
5948 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5949 * not LLC encapsulation with LLCSAP_IP.
5951 * For IEEE 802 networks - which includes 802.5 token ring
5952 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5953 * says that SNAP encapsulation is used, not LLC encapsulation
5956 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5957 * RFC 2225 say that SNAP encapsulation is used, not LLC
5958 * encapsulation with LLCSAP_IP.
5960 * So we always check for ETHERTYPE_IP.
5962 b0
= gen_linktype(ETHERTYPE_IP
);
5964 b1
= gen_cmp(OR_LINKPL
, 9, BPF_B
, (bpf_int32
)v
);
5966 b1
= gen_protochain(v
, Q_IP
);
5976 * Frame Relay packets typically have an OSI
5977 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5978 * generates code to check for all the OSI
5979 * NLPIDs, so calling it and then adding a check
5980 * for the particular NLPID for which we're
5981 * looking is bogus, as we can just check for
5984 * What we check for is the NLPID and a frame
5985 * control field value of UI, i.e. 0x03 followed
5988 * XXX - assumes a 2-byte Frame Relay header with
5989 * DLCI and flags. What if the address is longer?
5991 * XXX - what about SNAP-encapsulated frames?
5993 return gen_cmp(OR_LINKHDR
, 2, BPF_H
, (0x03<<8) | v
);
5999 * Cisco uses an Ethertype lookalike - for OSI,
6002 b0
= gen_linktype(LLCSAP_ISONS
<<8 | LLCSAP_ISONS
);
6003 /* OSI in C-HDLC is stuffed with a fudge byte */
6004 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 1, BPF_B
, (long)v
);
6009 b0
= gen_linktype(LLCSAP_ISONS
);
6010 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 0, BPF_B
, (long)v
);
6016 b0
= gen_proto(ISO10589_ISIS
, Q_ISO
, Q_DEFAULT
);
6018 * 4 is the offset of the PDU type relative to the IS-IS
6021 b1
= gen_cmp(OR_LINKPL_NOSNAP
, 4, BPF_B
, (long)v
);
6026 bpf_error("arp does not encapsulate another protocol");
6030 bpf_error("rarp does not encapsulate another protocol");
6034 bpf_error("atalk encapsulation is not specifiable");
6038 bpf_error("decnet encapsulation is not specifiable");
6042 bpf_error("sca does not encapsulate another protocol");
6046 bpf_error("lat does not encapsulate another protocol");
6050 bpf_error("moprc does not encapsulate another protocol");
6054 bpf_error("mopdl does not encapsulate another protocol");
6058 return gen_linktype(v
);
6061 bpf_error("'udp proto' is bogus");
6065 bpf_error("'tcp proto' is bogus");
6069 bpf_error("'sctp proto' is bogus");
6073 bpf_error("'icmp proto' is bogus");
6077 bpf_error("'igmp proto' is bogus");
6081 bpf_error("'igrp proto' is bogus");
6085 bpf_error("'pim proto' is bogus");
6089 bpf_error("'vrrp proto' is bogus");
6093 bpf_error("'carp proto' is bogus");
6097 b0
= gen_linktype(ETHERTYPE_IPV6
);
6100 * Also check for a fragment header before the final
6103 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, IPPROTO_FRAGMENT
);
6104 b1
= gen_cmp(OR_LINKPL
, 40, BPF_B
, (bpf_int32
)v
);
6106 b2
= gen_cmp(OR_LINKPL
, 6, BPF_B
, (bpf_int32
)v
);
6109 b1
= gen_protochain(v
, Q_IPV6
);
6115 bpf_error("'icmp6 proto' is bogus");
6118 bpf_error("'ah proto' is bogus");
6121 bpf_error("'ah proto' is bogus");
6124 bpf_error("'stp proto' is bogus");
6127 bpf_error("'ipx proto' is bogus");
6130 bpf_error("'netbeui proto' is bogus");
6133 bpf_error("'radio proto' is bogus");
6144 register const char *name
;
6147 int proto
= q
.proto
;
6151 bpf_u_int32 mask
, addr
;
6153 bpf_u_int32
**alist
;
6156 struct sockaddr_in
*sin4
;
6157 struct sockaddr_in6
*sin6
;
6158 struct addrinfo
*res
, *res0
;
6159 struct in6_addr mask128
;
6161 struct block
*b
, *tmp
;
6162 int port
, real_proto
;
6168 addr
= pcap_nametonetaddr(name
);
6170 bpf_error("unknown network '%s'", name
);
6171 /* Left justify network addr and calculate its network mask */
6173 while (addr
&& (addr
& 0xff000000) == 0) {
6177 return gen_host(addr
, mask
, proto
, dir
, q
.addr
);
6181 if (proto
== Q_LINK
) {
6185 case DLT_NETANALYZER
:
6186 case DLT_NETANALYZER_TRANSPARENT
:
6187 eaddr
= pcap_ether_hostton(name
);
6190 "unknown ether host '%s'", name
);
6191 tmp
= gen_prevlinkhdr_check();
6192 b
= gen_ehostop(eaddr
, dir
);
6199 eaddr
= pcap_ether_hostton(name
);
6202 "unknown FDDI host '%s'", name
);
6203 b
= gen_fhostop(eaddr
, dir
);
6208 eaddr
= pcap_ether_hostton(name
);
6211 "unknown token ring host '%s'", name
);
6212 b
= gen_thostop(eaddr
, dir
);
6216 case DLT_IEEE802_11
:
6217 case DLT_PRISM_HEADER
:
6218 case DLT_IEEE802_11_RADIO_AVS
:
6219 case DLT_IEEE802_11_RADIO
:
6221 eaddr
= pcap_ether_hostton(name
);
6224 "unknown 802.11 host '%s'", name
);
6225 b
= gen_wlanhostop(eaddr
, dir
);
6229 case DLT_IP_OVER_FC
:
6230 eaddr
= pcap_ether_hostton(name
);
6233 "unknown Fibre Channel host '%s'", name
);
6234 b
= gen_ipfchostop(eaddr
, dir
);
6239 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6240 } else if (proto
== Q_DECNET
) {
6241 unsigned short dn_addr
= __pcap_nametodnaddr(name
);
6243 * I don't think DECNET hosts can be multihomed, so
6244 * there is no need to build up a list of addresses
6246 return (gen_host(dn_addr
, 0, proto
, dir
, q
.addr
));
6249 alist
= pcap_nametoaddr(name
);
6250 if (alist
== NULL
|| *alist
== NULL
)
6251 bpf_error("unknown host '%s'", name
);
6253 if (off_linktype
.constant_part
== (u_int
)-1 &&
6254 tproto
== Q_DEFAULT
)
6256 b
= gen_host(**alist
++, 0xffffffff, tproto
, dir
, q
.addr
);
6258 tmp
= gen_host(**alist
++, 0xffffffff,
6259 tproto
, dir
, q
.addr
);
6265 memset(&mask128
, 0xff, sizeof(mask128
));
6266 res0
= res
= pcap_nametoaddrinfo(name
);
6268 bpf_error("unknown host '%s'", name
);
6271 tproto
= tproto6
= proto
;
6272 if (off_linktype
.constant_part
== -1 &&
6273 tproto
== Q_DEFAULT
) {
6277 for (res
= res0
; res
; res
= res
->ai_next
) {
6278 switch (res
->ai_family
) {
6280 if (tproto
== Q_IPV6
)
6283 sin4
= (struct sockaddr_in
*)
6285 tmp
= gen_host(ntohl(sin4
->sin_addr
.s_addr
),
6286 0xffffffff, tproto
, dir
, q
.addr
);
6289 if (tproto6
== Q_IP
)
6292 sin6
= (struct sockaddr_in6
*)
6294 tmp
= gen_host6(&sin6
->sin6_addr
,
6295 &mask128
, tproto6
, dir
, q
.addr
);
6307 bpf_error("unknown host '%s'%s", name
,
6308 (proto
== Q_DEFAULT
)
6310 : " for specified address family");
6317 if (proto
!= Q_DEFAULT
&&
6318 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6319 bpf_error("illegal qualifier of 'port'");
6320 if (pcap_nametoport(name
, &port
, &real_proto
) == 0)
6321 bpf_error("unknown port '%s'", name
);
6322 if (proto
== Q_UDP
) {
6323 if (real_proto
== IPPROTO_TCP
)
6324 bpf_error("port '%s' is tcp", name
);
6325 else if (real_proto
== IPPROTO_SCTP
)
6326 bpf_error("port '%s' is sctp", name
);
6328 /* override PROTO_UNDEF */
6329 real_proto
= IPPROTO_UDP
;
6331 if (proto
== Q_TCP
) {
6332 if (real_proto
== IPPROTO_UDP
)
6333 bpf_error("port '%s' is udp", name
);
6335 else if (real_proto
== IPPROTO_SCTP
)
6336 bpf_error("port '%s' is sctp", name
);
6338 /* override PROTO_UNDEF */
6339 real_proto
= IPPROTO_TCP
;
6341 if (proto
== Q_SCTP
) {
6342 if (real_proto
== IPPROTO_UDP
)
6343 bpf_error("port '%s' is udp", name
);
6345 else if (real_proto
== IPPROTO_TCP
)
6346 bpf_error("port '%s' is tcp", name
);
6348 /* override PROTO_UNDEF */
6349 real_proto
= IPPROTO_SCTP
;
6352 bpf_error("illegal port number %d < 0", port
);
6354 bpf_error("illegal port number %d > 65535", port
);
6355 b
= gen_port(port
, real_proto
, dir
);
6356 gen_or(gen_port6(port
, real_proto
, dir
), b
);
6360 if (proto
!= Q_DEFAULT
&&
6361 proto
!= Q_UDP
&& proto
!= Q_TCP
&& proto
!= Q_SCTP
)
6362 bpf_error("illegal qualifier of 'portrange'");
6363 if (pcap_nametoportrange(name
, &port1
, &port2
, &real_proto
) == 0)
6364 bpf_error("unknown port in range '%s'", name
);
6365 if (proto
== Q_UDP
) {
6366 if (real_proto
== IPPROTO_TCP
)
6367 bpf_error("port in range '%s' is tcp", name
);
6368 else if (real_proto
== IPPROTO_SCTP
)
6369 bpf_error("port in range '%s' is sctp", name
);
6371 /* override PROTO_UNDEF */
6372 real_proto
= IPPROTO_UDP
;
6374 if (proto
== Q_TCP
) {
6375 if (real_proto
== IPPROTO_UDP
)
6376 bpf_error("port in range '%s' is udp", name
);
6377 else if (real_proto
== IPPROTO_SCTP
)
6378 bpf_error("port in range '%s' is sctp", name
);
6380 /* override PROTO_UNDEF */
6381 real_proto
= IPPROTO_TCP
;
6383 if (proto
== Q_SCTP
) {
6384 if (real_proto
== IPPROTO_UDP
)
6385 bpf_error("port in range '%s' is udp", name
);
6386 else if (real_proto
== IPPROTO_TCP
)
6387 bpf_error("port in range '%s' is tcp", name
);
6389 /* override PROTO_UNDEF */
6390 real_proto
= IPPROTO_SCTP
;
6393 bpf_error("illegal port number %d < 0", port1
);
6395 bpf_error("illegal port number %d > 65535", port1
);
6397 bpf_error("illegal port number %d < 0", port2
);
6399 bpf_error("illegal port number %d > 65535", port2
);
6401 b
= gen_portrange(port1
, port2
, real_proto
, dir
);
6402 gen_or(gen_portrange6(port1
, port2
, real_proto
, dir
), b
);
6407 eaddr
= pcap_ether_hostton(name
);
6409 bpf_error("unknown ether host: %s", name
);
6411 alist
= pcap_nametoaddr(name
);
6412 if (alist
== NULL
|| *alist
== NULL
)
6413 bpf_error("unknown host '%s'", name
);
6414 b
= gen_gateway(eaddr
, alist
, proto
, dir
);
6418 bpf_error("'gateway' not supported in this configuration");
6422 real_proto
= lookup_proto(name
, proto
);
6423 if (real_proto
>= 0)
6424 return gen_proto(real_proto
, proto
, dir
);
6426 bpf_error("unknown protocol: %s", name
);
6429 real_proto
= lookup_proto(name
, proto
);
6430 if (real_proto
>= 0)
6431 return gen_protochain(real_proto
, proto
, dir
);
6433 bpf_error("unknown protocol: %s", name
);
6444 gen_mcode(s1
, s2
, masklen
, q
)
6445 register const char *s1
, *s2
;
6446 register unsigned int masklen
;
6449 register int nlen
, mlen
;
6452 nlen
= __pcap_atoin(s1
, &n
);
6453 /* Promote short ipaddr */
6457 mlen
= __pcap_atoin(s2
, &m
);
6458 /* Promote short ipaddr */
6461 bpf_error("non-network bits set in \"%s mask %s\"",
6464 /* Convert mask len to mask */
6466 bpf_error("mask length must be <= 32");
6469 * X << 32 is not guaranteed by C to be 0; it's
6474 m
= 0xffffffff << (32 - masklen
);
6476 bpf_error("non-network bits set in \"%s/%d\"",
6483 return gen_host(n
, m
, q
.proto
, q
.dir
, q
.addr
);
6486 bpf_error("Mask syntax for networks only");
6495 register const char *s
;
6500 int proto
= q
.proto
;
6506 else if (q
.proto
== Q_DECNET
)
6507 vlen
= __pcap_atodn(s
, &v
);
6509 vlen
= __pcap_atoin(s
, &v
);
6516 if (proto
== Q_DECNET
)
6517 return gen_host(v
, 0, proto
, dir
, q
.addr
);
6518 else if (proto
== Q_LINK
) {
6519 bpf_error("illegal link layer address");
6522 if (s
== NULL
&& q
.addr
== Q_NET
) {
6523 /* Promote short net number */
6524 while (v
&& (v
& 0xff000000) == 0) {
6529 /* Promote short ipaddr */
6533 return gen_host(v
, mask
, proto
, dir
, q
.addr
);
6538 proto
= IPPROTO_UDP
;
6539 else if (proto
== Q_TCP
)
6540 proto
= IPPROTO_TCP
;
6541 else if (proto
== Q_SCTP
)
6542 proto
= IPPROTO_SCTP
;
6543 else if (proto
== Q_DEFAULT
)
6544 proto
= PROTO_UNDEF
;
6546 bpf_error("illegal qualifier of 'port'");
6549 bpf_error("illegal port number %u > 65535", v
);
6553 b
= gen_port((int)v
, proto
, dir
);
6554 gen_or(gen_port6((int)v
, proto
, dir
), b
);
6560 proto
= IPPROTO_UDP
;
6561 else if (proto
== Q_TCP
)
6562 proto
= IPPROTO_TCP
;
6563 else if (proto
== Q_SCTP
)
6564 proto
= IPPROTO_SCTP
;
6565 else if (proto
== Q_DEFAULT
)
6566 proto
= PROTO_UNDEF
;
6568 bpf_error("illegal qualifier of 'portrange'");
6571 bpf_error("illegal port number %u > 65535", v
);
6575 b
= gen_portrange((int)v
, (int)v
, proto
, dir
);
6576 gen_or(gen_portrange6((int)v
, (int)v
, proto
, dir
), b
);
6581 bpf_error("'gateway' requires a name");
6585 return gen_proto((int)v
, proto
, dir
);
6588 return gen_protochain((int)v
, proto
, dir
);
6603 gen_mcode6(s1
, s2
, masklen
, q
)
6604 register const char *s1
, *s2
;
6605 register unsigned int masklen
;
6608 struct addrinfo
*res
;
6609 struct in6_addr
*addr
;
6610 struct in6_addr mask
;
6615 bpf_error("no mask %s supported", s2
);
6617 res
= pcap_nametoaddrinfo(s1
);
6619 bpf_error("invalid ip6 address %s", s1
);
6622 bpf_error("%s resolved to multiple address", s1
);
6623 addr
= &((struct sockaddr_in6
*)res
->ai_addr
)->sin6_addr
;
6625 if (sizeof(mask
) * 8 < masklen
)
6626 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask
) * 8));
6627 memset(&mask
, 0, sizeof(mask
));
6628 memset(&mask
, 0xff, masklen
/ 8);
6630 mask
.s6_addr
[masklen
/ 8] =
6631 (0xff << (8 - masklen
% 8)) & 0xff;
6634 a
= (u_int32_t
*)addr
;
6635 m
= (u_int32_t
*)&mask
;
6636 if ((a
[0] & ~m
[0]) || (a
[1] & ~m
[1])
6637 || (a
[2] & ~m
[2]) || (a
[3] & ~m
[3])) {
6638 bpf_error("non-network bits set in \"%s/%d\"", s1
, masklen
);
6646 bpf_error("Mask syntax for networks only");
6650 b
= gen_host6(addr
, &mask
, q
.proto
, q
.dir
, q
.addr
);
6656 bpf_error("invalid qualifier against IPv6 address");
6665 register const u_char
*eaddr
;
6668 struct block
*b
, *tmp
;
6670 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) && q
.proto
== Q_LINK
) {
6673 case DLT_NETANALYZER
:
6674 case DLT_NETANALYZER_TRANSPARENT
:
6675 tmp
= gen_prevlinkhdr_check();
6676 b
= gen_ehostop(eaddr
, (int)q
.dir
);
6681 return gen_fhostop(eaddr
, (int)q
.dir
);
6683 return gen_thostop(eaddr
, (int)q
.dir
);
6684 case DLT_IEEE802_11
:
6685 case DLT_PRISM_HEADER
:
6686 case DLT_IEEE802_11_RADIO_AVS
:
6687 case DLT_IEEE802_11_RADIO
:
6689 return gen_wlanhostop(eaddr
, (int)q
.dir
);
6690 case DLT_IP_OVER_FC
:
6691 return gen_ipfchostop(eaddr
, (int)q
.dir
);
6693 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6697 bpf_error("ethernet address used in non-ether expression");
6704 struct slist
*s0
, *s1
;
6707 * This is definitely not the best way to do this, but the
6708 * lists will rarely get long.
6715 static struct slist
*
6721 s
= new_stmt(BPF_LDX
|BPF_MEM
);
6726 static struct slist
*
6732 s
= new_stmt(BPF_LD
|BPF_MEM
);
6738 * Modify "index" to use the value stored into its register as an
6739 * offset relative to the beginning of the header for the protocol
6740 * "proto", and allocate a register and put an item "size" bytes long
6741 * (1, 2, or 4) at that offset into that register, making it the register
6745 gen_load(proto
, inst
, size
)
6750 struct slist
*s
, *tmp
;
6752 int regno
= alloc_reg();
6754 free_reg(inst
->regno
);
6758 bpf_error("data size must be 1, 2, or 4");
6774 bpf_error("unsupported index operation");
6778 * The offset is relative to the beginning of the packet
6779 * data, if we have a radio header. (If we don't, this
6782 if (linktype
!= DLT_IEEE802_11_RADIO_AVS
&&
6783 linktype
!= DLT_IEEE802_11_RADIO
&&
6784 linktype
!= DLT_PRISM_HEADER
)
6785 bpf_error("radio information not present in capture");
6788 * Load into the X register the offset computed into the
6789 * register specified by "index".
6791 s
= xfer_to_x(inst
);
6794 * Load the item at that offset.
6796 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6798 sappend(inst
->s
, s
);
6803 * The offset is relative to the beginning of
6804 * the link-layer header.
6806 * XXX - what about ATM LANE? Should the index be
6807 * relative to the beginning of the AAL5 frame, so
6808 * that 0 refers to the beginning of the LE Control
6809 * field, or relative to the beginning of the LAN
6810 * frame, so that 0 refers, for Ethernet LANE, to
6811 * the beginning of the destination address?
6813 s
= gen_abs_offset_varpart(&off_linkhdr
);
6816 * If "s" is non-null, it has code to arrange that the
6817 * X register contains the length of the prefix preceding
6818 * the link-layer header. Add to it the offset computed
6819 * into the register specified by "index", and move that
6820 * into the X register. Otherwise, just load into the X
6821 * register the offset computed into the register specified
6825 sappend(s
, xfer_to_a(inst
));
6826 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6827 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6829 s
= xfer_to_x(inst
);
6832 * Load the item at the sum of the offset we've put in the
6833 * X register and the offset of the start of the link
6834 * layer header (which is 0 if the radio header is
6835 * variable-length; that header length is what we put
6836 * into the X register and then added to the index).
6838 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6839 tmp
->s
.k
= off_linkhdr
.constant_part
;
6841 sappend(inst
->s
, s
);
6855 * The offset is relative to the beginning of
6856 * the network-layer header.
6857 * XXX - are there any cases where we want
6860 s
= gen_abs_offset_varpart(&off_linkpl
);
6863 * If "s" is non-null, it has code to arrange that the
6864 * X register contains the variable part of the offset
6865 * of the link-layer payload. Add to it the offset
6866 * computed into the register specified by "index",
6867 * and move that into the X register. Otherwise, just
6868 * load into the X register the offset computed into
6869 * the register specified by "index".
6872 sappend(s
, xfer_to_a(inst
));
6873 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6874 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6876 s
= xfer_to_x(inst
);
6879 * Load the item at the sum of the offset we've put in the
6880 * X register, the offset of the start of the network
6881 * layer header from the beginning of the link-layer
6882 * payload, and the constant part of the offset of the
6883 * start of the link-layer payload.
6885 tmp
= new_stmt(BPF_LD
|BPF_IND
|size
);
6886 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6888 sappend(inst
->s
, s
);
6891 * Do the computation only if the packet contains
6892 * the protocol in question.
6894 b
= gen_proto_abbrev(proto
);
6896 gen_and(inst
->b
, b
);
6910 * The offset is relative to the beginning of
6911 * the transport-layer header.
6913 * Load the X register with the length of the IPv4 header
6914 * (plus the offset of the link-layer header, if it's
6915 * a variable-length header), in bytes.
6917 * XXX - are there any cases where we want
6919 * XXX - we should, if we're built with
6920 * IPv6 support, generate code to load either
6921 * IPv4, IPv6, or both, as appropriate.
6923 s
= gen_loadx_iphdrlen();
6926 * The X register now contains the sum of the variable
6927 * part of the offset of the link-layer payload and the
6928 * length of the network-layer header.
6930 * Load into the A register the offset relative to
6931 * the beginning of the transport layer header,
6932 * add the X register to that, move that to the
6933 * X register, and load with an offset from the
6934 * X register equal to the sum of the constant part of
6935 * the offset of the link-layer payload and the offset,
6936 * relative to the beginning of the link-layer payload,
6937 * of the network-layer header.
6939 sappend(s
, xfer_to_a(inst
));
6940 sappend(s
, new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
));
6941 sappend(s
, new_stmt(BPF_MISC
|BPF_TAX
));
6942 sappend(s
, tmp
= new_stmt(BPF_LD
|BPF_IND
|size
));
6943 tmp
->s
.k
= off_linkpl
.constant_part
+ off_nl
;
6944 sappend(inst
->s
, s
);
6947 * Do the computation only if the packet contains
6948 * the protocol in question - which is true only
6949 * if this is an IP datagram and is the first or
6950 * only fragment of that datagram.
6952 gen_and(gen_proto_abbrev(proto
), b
= gen_ipfrag());
6954 gen_and(inst
->b
, b
);
6955 gen_and(gen_proto_abbrev(Q_IP
), b
);
6959 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6962 inst
->regno
= regno
;
6963 s
= new_stmt(BPF_ST
);
6965 sappend(inst
->s
, s
);
6971 gen_relation(code
, a0
, a1
, reversed
)
6973 struct arth
*a0
, *a1
;
6976 struct slist
*s0
, *s1
, *s2
;
6977 struct block
*b
, *tmp
;
6981 if (code
== BPF_JEQ
) {
6982 s2
= new_stmt(BPF_ALU
|BPF_SUB
|BPF_X
);
6983 b
= new_block(JMP(code
));
6987 b
= new_block(BPF_JMP
|code
|BPF_X
);
6993 sappend(a0
->s
, a1
->s
);
6997 free_reg(a0
->regno
);
6998 free_reg(a1
->regno
);
7000 /* 'and' together protocol checks */
7003 gen_and(a0
->b
, tmp
= a1
->b
);
7019 int regno
= alloc_reg();
7020 struct arth
*a
= (struct arth
*)newchunk(sizeof(*a
));
7023 s
= new_stmt(BPF_LD
|BPF_LEN
);
7024 s
->next
= new_stmt(BPF_ST
);
7025 s
->next
->s
.k
= regno
;
7040 a
= (struct arth
*)newchunk(sizeof(*a
));
7044 s
= new_stmt(BPF_LD
|BPF_IMM
);
7046 s
->next
= new_stmt(BPF_ST
);
7062 s
= new_stmt(BPF_ALU
|BPF_NEG
);
7065 s
= new_stmt(BPF_ST
);
7073 gen_arth(code
, a0
, a1
)
7075 struct arth
*a0
, *a1
;
7077 struct slist
*s0
, *s1
, *s2
;
7080 * Disallow division by, or modulus by, zero; we do this here
7081 * so that it gets done even if the optimizer is disabled.
7083 if (code
== BPF_DIV
) {
7084 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7085 bpf_error("division by zero");
7086 } else if (code
== BPF_MOD
) {
7087 if (a1
->s
->s
.code
== (BPF_LD
|BPF_IMM
) && a1
->s
->s
.k
== 0)
7088 bpf_error("modulus by zero");
7092 s2
= new_stmt(BPF_ALU
|BPF_X
|code
);
7097 sappend(a0
->s
, a1
->s
);
7099 free_reg(a0
->regno
);
7100 free_reg(a1
->regno
);
7102 s0
= new_stmt(BPF_ST
);
7103 a0
->regno
= s0
->s
.k
= alloc_reg();
7110 * Here we handle simple allocation of the scratch registers.
7111 * If too many registers are alloc'd, the allocator punts.
7113 static int regused
[BPF_MEMWORDS
];
7117 * Initialize the table of used registers and the current register.
7123 memset(regused
, 0, sizeof regused
);
7127 * Return the next free register.
7132 int n
= BPF_MEMWORDS
;
7135 if (regused
[curreg
])
7136 curreg
= (curreg
+ 1) % BPF_MEMWORDS
;
7138 regused
[curreg
] = 1;
7142 bpf_error("too many registers needed to evaluate expression");
7148 * Return a register to the table so it can
7158 static struct block
*
7165 s
= new_stmt(BPF_LD
|BPF_LEN
);
7166 b
= new_block(JMP(jmp
));
7177 return gen_len(BPF_JGE
, n
);
7181 * Actually, this is less than or equal.
7189 b
= gen_len(BPF_JGT
, n
);
7196 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7197 * the beginning of the link-layer header.
7198 * XXX - that means you can't test values in the radiotap header, but
7199 * as that header is difficult if not impossible to parse generally
7200 * without a loop, that might not be a severe problem. A new keyword
7201 * "radio" could be added for that, although what you'd really want
7202 * would be a way of testing particular radio header values, which
7203 * would generate code appropriate to the radio header in question.
7206 gen_byteop(op
, idx
, val
)
7217 return gen_cmp(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7220 b
= gen_cmp_lt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7224 b
= gen_cmp_gt(OR_LINKHDR
, (u_int
)idx
, BPF_B
, (bpf_int32
)val
);
7228 s
= new_stmt(BPF_ALU
|BPF_OR
|BPF_K
);
7232 s
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
7236 b
= new_block(JMP(BPF_JEQ
));
7243 static u_char abroadcast
[] = { 0x0 };
7246 gen_broadcast(proto
)
7249 bpf_u_int32 hostmask
;
7250 struct block
*b0
, *b1
, *b2
;
7251 static u_char ebroadcast
[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7259 case DLT_ARCNET_LINUX
:
7260 return gen_ahostop(abroadcast
, Q_DST
);
7262 case DLT_NETANALYZER
:
7263 case DLT_NETANALYZER_TRANSPARENT
:
7264 b1
= gen_prevlinkhdr_check();
7265 b0
= gen_ehostop(ebroadcast
, Q_DST
);
7270 return gen_fhostop(ebroadcast
, Q_DST
);
7272 return gen_thostop(ebroadcast
, Q_DST
);
7273 case DLT_IEEE802_11
:
7274 case DLT_PRISM_HEADER
:
7275 case DLT_IEEE802_11_RADIO_AVS
:
7276 case DLT_IEEE802_11_RADIO
:
7278 return gen_wlanhostop(ebroadcast
, Q_DST
);
7279 case DLT_IP_OVER_FC
:
7280 return gen_ipfchostop(ebroadcast
, Q_DST
);
7282 bpf_error("not a broadcast link");
7288 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7289 * as an indication that we don't know the netmask, and fail
7292 if (netmask
== PCAP_NETMASK_UNKNOWN
)
7293 bpf_error("netmask not known, so 'ip broadcast' not supported");
7294 b0
= gen_linktype(ETHERTYPE_IP
);
7295 hostmask
= ~netmask
;
7296 b1
= gen_mcmp(OR_LINKPL
, 16, BPF_W
, (bpf_int32
)0, hostmask
);
7297 b2
= gen_mcmp(OR_LINKPL
, 16, BPF_W
,
7298 (bpf_int32
)(~0 & hostmask
), hostmask
);
7303 bpf_error("only link-layer/IP broadcast filters supported");
7309 * Generate code to test the low-order bit of a MAC address (that's
7310 * the bottom bit of the *first* byte).
7312 static struct block
*
7313 gen_mac_multicast(offset
)
7316 register struct block
*b0
;
7317 register struct slist
*s
;
7319 /* link[offset] & 1 != 0 */
7320 s
= gen_load_a(OR_LINKHDR
, offset
, BPF_B
);
7321 b0
= new_block(JMP(BPF_JSET
));
7328 gen_multicast(proto
)
7331 register struct block
*b0
, *b1
, *b2
;
7332 register struct slist
*s
;
7340 case DLT_ARCNET_LINUX
:
7341 /* all ARCnet multicasts use the same address */
7342 return gen_ahostop(abroadcast
, Q_DST
);
7344 case DLT_NETANALYZER
:
7345 case DLT_NETANALYZER_TRANSPARENT
:
7346 b1
= gen_prevlinkhdr_check();
7347 /* ether[0] & 1 != 0 */
7348 b0
= gen_mac_multicast(0);
7354 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7356 * XXX - was that referring to bit-order issues?
7358 /* fddi[1] & 1 != 0 */
7359 return gen_mac_multicast(1);
7361 /* tr[2] & 1 != 0 */
7362 return gen_mac_multicast(2);
7363 case DLT_IEEE802_11
:
7364 case DLT_PRISM_HEADER
:
7365 case DLT_IEEE802_11_RADIO_AVS
:
7366 case DLT_IEEE802_11_RADIO
:
7371 * For control frames, there is no DA.
7373 * For management frames, DA is at an
7374 * offset of 4 from the beginning of
7377 * For data frames, DA is at an offset
7378 * of 4 from the beginning of the packet
7379 * if To DS is clear and at an offset of
7380 * 16 from the beginning of the packet
7385 * Generate the tests to be done for data frames.
7387 * First, check for To DS set, i.e. "link[1] & 0x01".
7389 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7390 b1
= new_block(JMP(BPF_JSET
));
7391 b1
->s
.k
= 0x01; /* To DS */
7395 * If To DS is set, the DA is at 16.
7397 b0
= gen_mac_multicast(16);
7401 * Now, check for To DS not set, i.e. check
7402 * "!(link[1] & 0x01)".
7404 s
= gen_load_a(OR_LINKHDR
, 1, BPF_B
);
7405 b2
= new_block(JMP(BPF_JSET
));
7406 b2
->s
.k
= 0x01; /* To DS */
7411 * If To DS is not set, the DA is at 4.
7413 b1
= gen_mac_multicast(4);
7417 * Now OR together the last two checks. That gives
7418 * the complete set of checks for data frames.
7423 * Now check for a data frame.
7424 * I.e, check "link[0] & 0x08".
7426 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7427 b1
= new_block(JMP(BPF_JSET
));
7432 * AND that with the checks done for data frames.
7437 * If the high-order bit of the type value is 0, this
7438 * is a management frame.
7439 * I.e, check "!(link[0] & 0x08)".
7441 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7442 b2
= new_block(JMP(BPF_JSET
));
7448 * For management frames, the DA is at 4.
7450 b1
= gen_mac_multicast(4);
7454 * OR that with the checks done for data frames.
7455 * That gives the checks done for management and
7461 * If the low-order bit of the type value is 1,
7462 * this is either a control frame or a frame
7463 * with a reserved type, and thus not a
7466 * I.e., check "!(link[0] & 0x04)".
7468 s
= gen_load_a(OR_LINKHDR
, 0, BPF_B
);
7469 b1
= new_block(JMP(BPF_JSET
));
7475 * AND that with the checks for data and management
7480 case DLT_IP_OVER_FC
:
7481 b0
= gen_mac_multicast(2);
7486 /* Link not known to support multicasts */
7490 b0
= gen_linktype(ETHERTYPE_IP
);
7491 b1
= gen_cmp_ge(OR_LINKPL
, 16, BPF_B
, (bpf_int32
)224);
7496 b0
= gen_linktype(ETHERTYPE_IPV6
);
7497 b1
= gen_cmp(OR_LINKPL
, 24, BPF_B
, (bpf_int32
)255);
7501 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7507 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7508 * Outbound traffic is sent by this machine, while inbound traffic is
7509 * sent by a remote machine (and may include packets destined for a
7510 * unicast or multicast link-layer address we are not subscribing to).
7511 * These are the same definitions implemented by pcap_setdirection().
7512 * Capturing only unicast traffic destined for this host is probably
7513 * better accomplished using a higher-layer filter.
7519 register struct block
*b0
;
7522 * Only some data link types support inbound/outbound qualifiers.
7526 b0
= gen_relation(BPF_JEQ
,
7527 gen_load(Q_LINK
, gen_loadi(0), 1),
7534 /* match outgoing packets */
7535 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_OUTBOUND
);
7537 /* match incoming packets */
7538 b0
= gen_cmp(OR_LINKHDR
, 2, BPF_H
, IPNET_INBOUND
);
7543 /* match outgoing packets */
7544 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_H
, LINUX_SLL_OUTGOING
);
7546 /* to filter on inbound traffic, invert the match */
7551 #ifdef HAVE_NET_PFVAR_H
7553 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, dir
), BPF_B
,
7554 (bpf_int32
)((dir
== 0) ? PF_IN
: PF_OUT
));
7560 /* match outgoing packets */
7561 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_OUT
);
7563 /* match incoming packets */
7564 b0
= gen_cmp(OR_LINKHDR
, 0, BPF_B
, PPP_PPPD_IN
);
7568 case DLT_JUNIPER_MFR
:
7569 case DLT_JUNIPER_MLFR
:
7570 case DLT_JUNIPER_MLPPP
:
7571 case DLT_JUNIPER_ATM1
:
7572 case DLT_JUNIPER_ATM2
:
7573 case DLT_JUNIPER_PPPOE
:
7574 case DLT_JUNIPER_PPPOE_ATM
:
7575 case DLT_JUNIPER_GGSN
:
7576 case DLT_JUNIPER_ES
:
7577 case DLT_JUNIPER_MONITOR
:
7578 case DLT_JUNIPER_SERVICES
:
7579 case DLT_JUNIPER_ETHER
:
7580 case DLT_JUNIPER_PPP
:
7581 case DLT_JUNIPER_FRELAY
:
7582 case DLT_JUNIPER_CHDLC
:
7583 case DLT_JUNIPER_VP
:
7584 case DLT_JUNIPER_ST
:
7585 case DLT_JUNIPER_ISM
:
7586 case DLT_JUNIPER_VS
:
7587 case DLT_JUNIPER_SRX_E2E
:
7588 case DLT_JUNIPER_FIBRECHANNEL
:
7589 case DLT_JUNIPER_ATM_CEMIC
:
7591 /* juniper flags (including direction) are stored
7592 * the byte after the 3-byte magic number */
7594 /* match outgoing packets */
7595 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 0, 0x01);
7597 /* match incoming packets */
7598 b0
= gen_mcmp(OR_LINKHDR
, 3, BPF_B
, 1, 0x01);
7604 * If we have packet meta-data indicating a direction,
7605 * check it, otherwise give up as this link-layer type
7606 * has nothing in the packet data.
7608 #if defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7610 * This is Linux with PF_PACKET support.
7611 * If this is a *live* capture, we can look at
7612 * special meta-data in the filter expression;
7613 * if it's a savefile, we can't.
7615 if (bpf_pcap
->rfile
!= NULL
) {
7616 /* We have a FILE *, so this is a savefile */
7617 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7622 /* match outgoing packets */
7623 b0
= gen_cmp(OR_LINKHDR
, SKF_AD_OFF
+ SKF_AD_PKTTYPE
, BPF_H
,
7626 /* to filter on inbound traffic, invert the match */
7629 #else /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7630 bpf_error("inbound/outbound not supported on linktype %d",
7634 #endif /* defined(linux) && defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7639 #ifdef HAVE_NET_PFVAR_H
7640 /* PF firewall log matched interface */
7642 gen_pf_ifname(const char *ifname
)
7647 if (linktype
!= DLT_PFLOG
) {
7648 bpf_error("ifname supported only on PF linktype");
7651 len
= sizeof(((struct pfloghdr
*)0)->ifname
);
7652 off
= offsetof(struct pfloghdr
, ifname
);
7653 if (strlen(ifname
) >= len
) {
7654 bpf_error("ifname interface names can only be %d characters",
7658 b0
= gen_bcmp(OR_LINKHDR
, off
, strlen(ifname
), (const u_char
*)ifname
);
7662 /* PF firewall log ruleset name */
7664 gen_pf_ruleset(char *ruleset
)
7668 if (linktype
!= DLT_PFLOG
) {
7669 bpf_error("ruleset supported only on PF linktype");
7673 if (strlen(ruleset
) >= sizeof(((struct pfloghdr
*)0)->ruleset
)) {
7674 bpf_error("ruleset names can only be %ld characters",
7675 (long)(sizeof(((struct pfloghdr
*)0)->ruleset
) - 1));
7679 b0
= gen_bcmp(OR_LINKHDR
, offsetof(struct pfloghdr
, ruleset
),
7680 strlen(ruleset
), (const u_char
*)ruleset
);
7684 /* PF firewall log rule number */
7690 if (linktype
!= DLT_PFLOG
) {
7691 bpf_error("rnr supported only on PF linktype");
7695 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, rulenr
), BPF_W
,
7700 /* PF firewall log sub-rule number */
7702 gen_pf_srnr(int srnr
)
7706 if (linktype
!= DLT_PFLOG
) {
7707 bpf_error("srnr supported only on PF linktype");
7711 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, subrulenr
), BPF_W
,
7716 /* PF firewall log reason code */
7718 gen_pf_reason(int reason
)
7722 if (linktype
!= DLT_PFLOG
) {
7723 bpf_error("reason supported only on PF linktype");
7727 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, reason
), BPF_B
,
7732 /* PF firewall log action */
7734 gen_pf_action(int action
)
7738 if (linktype
!= DLT_PFLOG
) {
7739 bpf_error("action supported only on PF linktype");
7743 b0
= gen_cmp(OR_LINKHDR
, offsetof(struct pfloghdr
, action
), BPF_B
,
7747 #else /* !HAVE_NET_PFVAR_H */
7749 gen_pf_ifname(const char *ifname
)
7751 bpf_error("libpcap was compiled without pf support");
7757 gen_pf_ruleset(char *ruleset
)
7759 bpf_error("libpcap was compiled on a machine without pf support");
7767 bpf_error("libpcap was compiled on a machine without pf support");
7773 gen_pf_srnr(int srnr
)
7775 bpf_error("libpcap was compiled on a machine without pf support");
7781 gen_pf_reason(int reason
)
7783 bpf_error("libpcap was compiled on a machine without pf support");
7789 gen_pf_action(int action
)
7791 bpf_error("libpcap was compiled on a machine without pf support");
7795 #endif /* HAVE_NET_PFVAR_H */
7797 /* IEEE 802.11 wireless header */
7799 gen_p80211_type(int type
, int mask
)
7805 case DLT_IEEE802_11
:
7806 case DLT_PRISM_HEADER
:
7807 case DLT_IEEE802_11_RADIO_AVS
:
7808 case DLT_IEEE802_11_RADIO
:
7809 b0
= gen_mcmp(OR_LINKHDR
, 0, BPF_B
, (bpf_int32
)type
,
7814 bpf_error("802.11 link-layer types supported only on 802.11");
7822 gen_p80211_fcdir(int fcdir
)
7828 case DLT_IEEE802_11
:
7829 case DLT_PRISM_HEADER
:
7830 case DLT_IEEE802_11_RADIO_AVS
:
7831 case DLT_IEEE802_11_RADIO
:
7835 bpf_error("frame direction supported only with 802.11 headers");
7839 b0
= gen_mcmp(OR_LINKHDR
, 1, BPF_B
, (bpf_int32
)fcdir
,
7840 (bpf_u_int32
)IEEE80211_FC1_DIR_MASK
);
7847 register const u_char
*eaddr
;
7853 case DLT_ARCNET_LINUX
:
7854 if ((q
.addr
== Q_HOST
|| q
.addr
== Q_DEFAULT
) &&
7856 return (gen_ahostop(eaddr
, (int)q
.dir
));
7858 bpf_error("ARCnet address used in non-arc expression");
7864 bpf_error("aid supported only on ARCnet");
7867 bpf_error("ARCnet address used in non-arc expression");
7872 static struct block
*
7873 gen_ahostop(eaddr
, dir
)
7874 register const u_char
*eaddr
;
7877 register struct block
*b0
, *b1
;
7880 /* src comes first, different from Ethernet */
7882 return gen_bcmp(OR_LINKHDR
, 0, 1, eaddr
);
7885 return gen_bcmp(OR_LINKHDR
, 1, 1, eaddr
);
7888 b0
= gen_ahostop(eaddr
, Q_SRC
);
7889 b1
= gen_ahostop(eaddr
, Q_DST
);
7895 b0
= gen_ahostop(eaddr
, Q_SRC
);
7896 b1
= gen_ahostop(eaddr
, Q_DST
);
7901 bpf_error("'addr1' is only supported on 802.11");
7905 bpf_error("'addr2' is only supported on 802.11");
7909 bpf_error("'addr3' is only supported on 802.11");
7913 bpf_error("'addr4' is only supported on 802.11");
7917 bpf_error("'ra' is only supported on 802.11");
7921 bpf_error("'ta' is only supported on 802.11");
7928 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
7929 static struct block
*
7930 gen_vlan_bpf_extensions(int vlan_num
)
7932 struct block
*b0
, *b1
;
7935 /* generate new filter code based on extracting packet
7937 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7938 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG_PRESENT
;
7940 b0
= new_block(JMP(BPF_JEQ
));
7944 if (vlan_num
>= 0) {
7945 s
= new_stmt(BPF_LD
|BPF_B
|BPF_ABS
);
7946 s
->s
.k
= SKF_AD_OFF
+ SKF_AD_VLAN_TAG
;
7948 b1
= new_block(JMP(BPF_JEQ
));
7950 b1
->s
.k
= (bpf_int32
) vlan_num
;
7960 static struct block
*
7961 gen_vlan_no_bpf_extensions(int vlan_num
)
7963 struct block
*b0
, *b1
;
7965 /* check for VLAN, including QinQ */
7966 b0
= gen_linktype(ETHERTYPE_8021Q
);
7967 b1
= gen_linktype(ETHERTYPE_8021AD
);
7970 b1
= gen_linktype(ETHERTYPE_8021QINQ
);
7974 /* If a specific VLAN is requested, check VLAN id */
7975 if (vlan_num
>= 0) {
7976 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_H
,
7977 (bpf_int32
)vlan_num
, 0x0fff);
7983 * The payload follows the full header, including the
7984 * VLAN tags, so skip past this VLAN tag.
7986 off_linkpl
.constant_part
+= 4;
7989 * The link-layer type information follows the VLAN tags, so
7990 * skip past this VLAN tag.
7992 off_linktype
.constant_part
+= 4;
7998 * support IEEE 802.1Q VLAN trunk over ethernet
8006 /* can't check for VLAN-encapsulated packets inside MPLS */
8007 if (label_stack_depth
> 0)
8008 bpf_error("no VLAN match after MPLS");
8011 * Check for a VLAN packet, and then change the offsets to point
8012 * to the type and data fields within the VLAN packet. Just
8013 * increment the offsets, so that we can support a hierarchy, e.g.
8014 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
8017 * XXX - this is a bit of a kludge. If we were to split the
8018 * compiler into a parser that parses an expression and
8019 * generates an expression tree, and a code generator that
8020 * takes an expression tree (which could come from our
8021 * parser or from some other parser) and generates BPF code,
8022 * we could perhaps make the offsets parameters of routines
8023 * and, in the handler for an "AND" node, pass to subnodes
8024 * other than the VLAN node the adjusted offsets.
8026 * This would mean that "vlan" would, instead of changing the
8027 * behavior of *all* tests after it, change only the behavior
8028 * of tests ANDed with it. That would change the documented
8029 * semantics of "vlan", which might break some expressions.
8030 * However, it would mean that "(vlan and ip) or ip" would check
8031 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8032 * checking only for VLAN-encapsulated IP, so that could still
8033 * be considered worth doing; it wouldn't break expressions
8034 * that are of the form "vlan and ..." or "vlan N and ...",
8035 * which I suspect are the most common expressions involving
8036 * "vlan". "vlan or ..." doesn't necessarily do what the user
8037 * would really want, now, as all the "or ..." tests would
8038 * be done assuming a VLAN, even though the "or" could be viewed
8039 * as meaning "or, if this isn't a VLAN packet...".
8044 case DLT_NETANALYZER
:
8045 case DLT_NETANALYZER_TRANSPARENT
:
8046 #if defined(SKF_AD_VLAN_TAG) && defined(SKF_AD_VLAN_TAG_PRESENT)
8047 /* Verify that this is the outer part of the packet and
8048 * not encapsulated somehow. */
8049 if (vlan_stack_depth
== 0 && !off_linkhdr
.is_variable
&&
8050 off_linkhdr
.constant_part
==
8051 off_outermostlinkhdr
.constant_part
) {
8053 * Do we need special VLAN handling?
8055 if (bpf_pcap
->bpf_codegen_flags
& BPF_SPECIAL_VLAN_HANDLING
)
8056 b0
= gen_vlan_bpf_extensions(vlan_num
);
8058 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8061 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8064 case DLT_IEEE802_11
:
8065 case DLT_PRISM_HEADER
:
8066 case DLT_IEEE802_11_RADIO_AVS
:
8067 case DLT_IEEE802_11_RADIO
:
8068 b0
= gen_vlan_no_bpf_extensions(vlan_num
);
8072 bpf_error("no VLAN support for data link type %d",
8089 struct block
*b0
, *b1
;
8091 if (label_stack_depth
> 0) {
8092 /* just match the bottom-of-stack bit clear */
8093 b0
= gen_mcmp(OR_PREVMPLSHDR
, 2, BPF_B
, 0, 0x01);
8096 * We're not in an MPLS stack yet, so check the link-layer
8097 * type against MPLS.
8101 case DLT_C_HDLC
: /* fall through */
8103 case DLT_NETANALYZER
:
8104 case DLT_NETANALYZER_TRANSPARENT
:
8105 b0
= gen_linktype(ETHERTYPE_MPLS
);
8109 b0
= gen_linktype(PPP_MPLS_UCAST
);
8112 /* FIXME add other DLT_s ...
8113 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8114 * leave it for now */
8117 bpf_error("no MPLS support for data link type %d",
8125 /* If a specific MPLS label is requested, check it */
8126 if (label_num
>= 0) {
8127 label_num
= label_num
<< 12; /* label is shifted 12 bits on the wire */
8128 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
, (bpf_int32
)label_num
,
8129 0xfffff000); /* only compare the first 20 bits */
8135 * Change the offsets to point to the type and data fields within
8136 * the MPLS packet. Just increment the offsets, so that we
8137 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8138 * capture packets with an outer label of 100000 and an inner
8141 * Increment the MPLS stack depth as well; this indicates that
8142 * we're checking MPLS-encapsulated headers, to make sure higher
8143 * level code generators don't try to match against IP-related
8144 * protocols such as Q_ARP, Q_RARP etc.
8146 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8150 label_stack_depth
++;
8155 * Support PPPOE discovery and session.
8160 /* check for PPPoE discovery */
8161 return gen_linktype((bpf_int32
)ETHERTYPE_PPPOED
);
8165 gen_pppoes(sess_num
)
8168 struct block
*b0
, *b1
;
8171 * Test against the PPPoE session link-layer type.
8173 b0
= gen_linktype((bpf_int32
)ETHERTYPE_PPPOES
);
8175 /* If a specific session is requested, check PPPoE session id */
8176 if (sess_num
>= 0) {
8177 b1
= gen_mcmp(OR_LINKPL
, 0, BPF_W
,
8178 (bpf_int32
)sess_num
, 0x0000ffff);
8184 * Change the offsets to point to the type and data fields within
8185 * the PPP packet, and note that this is PPPoE rather than
8188 * XXX - this is a bit of a kludge. If we were to split the
8189 * compiler into a parser that parses an expression and
8190 * generates an expression tree, and a code generator that
8191 * takes an expression tree (which could come from our
8192 * parser or from some other parser) and generates BPF code,
8193 * we could perhaps make the offsets parameters of routines
8194 * and, in the handler for an "AND" node, pass to subnodes
8195 * other than the PPPoE node the adjusted offsets.
8197 * This would mean that "pppoes" would, instead of changing the
8198 * behavior of *all* tests after it, change only the behavior
8199 * of tests ANDed with it. That would change the documented
8200 * semantics of "pppoes", which might break some expressions.
8201 * However, it would mean that "(pppoes and ip) or ip" would check
8202 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8203 * checking only for VLAN-encapsulated IP, so that could still
8204 * be considered worth doing; it wouldn't break expressions
8205 * that are of the form "pppoes and ..." which I suspect are the
8206 * most common expressions involving "pppoes". "pppoes or ..."
8207 * doesn't necessarily do what the user would really want, now,
8208 * as all the "or ..." tests would be done assuming PPPoE, even
8209 * though the "or" could be viewed as meaning "or, if this isn't
8210 * a PPPoE packet...".
8212 * The "network-layer" protocol is PPPoE, which has a 6-byte
8213 * PPPoE header, followed by a PPP packet.
8215 * There is no HDLC encapsulation for the PPP packet (it's
8216 * encapsulated in PPPoES instead), so the link-layer type
8217 * starts at the first byte of the PPP packet. For PPPoE,
8218 * that offset is relative to the beginning of the total
8219 * link-layer payload, including any 802.2 LLC header, so
8220 * it's 6 bytes past off_nl.
8222 PUSH_LINKHDR(DLT_PPP
, off_linkpl
.is_variable
,
8223 off_linkpl
.constant_part
+ off_nl
+ 6, /* 6 bytes past the PPPoE header */
8226 off_linktype
= off_linkhdr
;
8227 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 2;
8230 off_nl_nosnap
= 0; /* no 802.2 LLC */
8235 /* Check that this is Geneve and the VNI is correct if
8236 * specified. Parameterized to handle both IPv4 and IPv6. */
8237 static struct block
*
8238 gen_geneve_check(struct block
*(*gen_portfn
)(int, int, int),
8239 enum e_offrel offrel
, int vni
)
8241 struct block
*b0
, *b1
;
8243 b0
= gen_portfn(GENEVE_PORT
, IPPROTO_UDP
, Q_DST
);
8245 /* Check that we are operating on version 0. Otherwise, we
8246 * can't decode the rest of the fields. The version is 2 bits
8247 * in the first byte of the Geneve header. */
8248 b1
= gen_mcmp(offrel
, 8, BPF_B
, (bpf_int32
)0, 0xc0);
8253 vni
<<= 8; /* VNI is in the upper 3 bytes */
8254 b1
= gen_mcmp(offrel
, 12, BPF_W
, (bpf_int32
)vni
,
8263 /* The IPv4 and IPv6 Geneve checks need to do two things:
8264 * - Verify that this actually is Geneve with the right VNI.
8265 * - Place the IP header length (plus variable link prefix if
8266 * needed) into register A to be used later to compute
8267 * the inner packet offsets. */
8268 static struct block
*
8269 gen_geneve4(int vni
)
8271 struct block
*b0
, *b1
;
8272 struct slist
*s
, *s1
;
8274 b0
= gen_geneve_check(gen_port
, OR_TRAN_IPV4
, vni
);
8276 /* Load the IP header length into A. */
8277 s
= gen_loadx_iphdrlen();
8279 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8282 /* Forcibly append these statements to the true condition
8283 * of the protocol check by creating a new block that is
8284 * always true and ANDing them. */
8285 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8294 static struct block
*
8295 gen_geneve6(int vni
)
8297 struct block
*b0
, *b1
;
8298 struct slist
*s
, *s1
;
8300 b0
= gen_geneve_check(gen_port6
, OR_TRAN_IPV6
, vni
);
8302 /* Load the IP header length. We need to account for a
8303 * variable length link prefix if there is one. */
8304 s
= gen_abs_offset_varpart(&off_linkpl
);
8306 s1
= new_stmt(BPF_LD
|BPF_IMM
);
8310 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8314 s
= new_stmt(BPF_LD
|BPF_IMM
);
8318 /* Forcibly append these statements to the true condition
8319 * of the protocol check by creating a new block that is
8320 * always true and ANDing them. */
8321 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8324 b1
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8333 /* We need to store three values based on the Geneve header::
8334 * - The offset of the linktype.
8335 * - The offset of the end of the Geneve header.
8336 * - The offset of the end of the encapsulated MAC header. */
8337 static struct slist
*
8338 gen_geneve_offsets(void)
8340 struct slist
*s
, *s1
, *s_proto
;
8342 /* First we need to calculate the offset of the Geneve header
8343 * itself. This is composed of the IP header previously calculated
8344 * (include any variable link prefix) and stored in A plus the
8345 * fixed sized headers (fixed link prefix, MAC length, and UDP
8347 s
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8348 s
->s
.k
= off_linkpl
.constant_part
+ off_nl
+ 8;
8350 /* Stash this in X since we'll need it later. */
8351 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8354 /* The EtherType in Geneve is 2 bytes in. Calculate this and
8356 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8360 off_linktype
.reg
= alloc_reg();
8361 off_linktype
.is_variable
= 1;
8362 off_linktype
.constant_part
= 0;
8364 s1
= new_stmt(BPF_ST
);
8365 s1
->s
.k
= off_linktype
.reg
;
8368 /* Load the Geneve option length and mask and shift to get the
8369 * number of bytes. It is stored in the first byte of the Geneve
8371 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_B
);
8375 s1
= new_stmt(BPF_ALU
|BPF_AND
|BPF_K
);
8379 s1
= new_stmt(BPF_ALU
|BPF_MUL
|BPF_K
);
8383 /* Add in the rest of the Geneve base header. */
8384 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8388 /* Add the Geneve header length to its offset and store. */
8389 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_X
);
8393 /* Set the encapsulated type as Ethernet. Even though we may
8394 * not actually have Ethernet inside there are two reasons this
8396 * - The linktype field is always in EtherType format regardless
8397 * of whether it is in Geneve or an inner Ethernet frame.
8398 * - The only link layer that we have specific support for is
8399 * Ethernet. We will confirm that the packet actually is
8400 * Ethernet at runtime before executing these checks. */
8401 PUSH_LINKHDR(DLT_EN10MB
, 1, 0, alloc_reg());
8403 s1
= new_stmt(BPF_ST
);
8404 s1
->s
.k
= off_linkhdr
.reg
;
8407 /* Calculate whether we have an Ethernet header or just raw IP/
8408 * MPLS/etc. If we have Ethernet, advance the end of the MAC offset
8409 * and linktype by 14 bytes so that the network header can be found
8410 * seamlessly. Otherwise, keep what we've calculated already. */
8412 /* We have a bare jmp so we can't use the optimizer. */
8415 /* Load the EtherType in the Geneve header, 2 bytes in. */
8416 s1
= new_stmt(BPF_LD
|BPF_IND
|BPF_H
);
8420 /* Load X with the end of the Geneve header. */
8421 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8422 s1
->s
.k
= off_linkhdr
.reg
;
8425 /* Check if the EtherType is Transparent Ethernet Bridging. At the
8426 * end of this check, we should have the total length in X. In
8427 * the non-Ethernet case, it's already there. */
8428 s_proto
= new_stmt(JMP(BPF_JEQ
));
8429 s_proto
->s
.k
= ETHERTYPE_TEB
;
8430 sappend(s
, s_proto
);
8432 s1
= new_stmt(BPF_MISC
|BPF_TXA
);
8436 /* Since this is Ethernet, use the EtherType of the payload
8437 * directly as the linktype. Overwrite what we already have. */
8438 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8442 s1
= new_stmt(BPF_ST
);
8443 s1
->s
.k
= off_linktype
.reg
;
8446 /* Advance two bytes further to get the end of the Ethernet
8448 s1
= new_stmt(BPF_ALU
|BPF_ADD
|BPF_K
);
8452 /* Move the result to X. */
8453 s1
= new_stmt(BPF_MISC
|BPF_TAX
);
8456 /* Store the final result of our linkpl calculation. */
8457 off_linkpl
.reg
= alloc_reg();
8458 off_linkpl
.is_variable
= 1;
8459 off_linkpl
.constant_part
= 0;
8461 s1
= new_stmt(BPF_STX
);
8462 s1
->s
.k
= off_linkpl
.reg
;
8471 /* Check to see if this is a Geneve packet. */
8475 struct block
*b0
, *b1
;
8478 b0
= gen_geneve4(vni
);
8479 b1
= gen_geneve6(vni
);
8484 /* Later filters should act on the payload of the Geneve frame,
8485 * update all of the header pointers. Attach this code so that
8486 * it gets executed in the event that the Geneve filter matches. */
8487 s
= gen_geneve_offsets();
8490 sappend(s
, b1
->stmts
);
8500 /* Check that the encapsulated frame has a link layer header
8501 * for Ethernet filters. */
8502 static struct block
*
8503 gen_geneve_ll_check()
8506 struct slist
*s
, *s1
;
8508 /* The easiest way to see if there is a link layer present
8509 * is to check if the link layer header and payload are not
8512 /* Geneve always generates pure variable offsets so we can
8513 * compare only the registers. */
8514 s
= new_stmt(BPF_LD
|BPF_MEM
);
8515 s
->s
.k
= off_linkhdr
.reg
;
8517 s1
= new_stmt(BPF_LDX
|BPF_MEM
);
8518 s1
->s
.k
= off_linkpl
.reg
;
8521 b0
= new_block(BPF_JMP
|BPF_JEQ
|BPF_X
);
8530 gen_atmfield_code(atmfield
, jvalue
, jtype
, reverse
)
8542 bpf_error("'vpi' supported only on raw ATM");
8543 if (off_vpi
== (u_int
)-1)
8545 b0
= gen_ncmp(OR_LINKHDR
, off_vpi
, BPF_B
, 0xffffffff, jtype
,
8551 bpf_error("'vci' supported only on raw ATM");
8552 if (off_vci
== (u_int
)-1)
8554 b0
= gen_ncmp(OR_LINKHDR
, off_vci
, BPF_H
, 0xffffffff, jtype
,
8559 if (off_proto
== (u_int
)-1)
8560 abort(); /* XXX - this isn't on FreeBSD */
8561 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0x0f, jtype
,
8566 if (off_payload
== (u_int
)-1)
8568 b0
= gen_ncmp(OR_LINKHDR
, off_payload
+ MSG_TYPE_POS
, BPF_B
,
8569 0xffffffff, jtype
, reverse
, jvalue
);
8574 bpf_error("'callref' supported only on raw ATM");
8575 if (off_proto
== (u_int
)-1)
8577 b0
= gen_ncmp(OR_LINKHDR
, off_proto
, BPF_B
, 0xffffffff,
8578 jtype
, reverse
, jvalue
);
8588 gen_atmtype_abbrev(type
)
8591 struct block
*b0
, *b1
;
8596 /* Get all packets in Meta signalling Circuit */
8598 bpf_error("'metac' supported only on raw ATM");
8599 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8600 b1
= gen_atmfield_code(A_VCI
, 1, BPF_JEQ
, 0);
8605 /* Get all packets in Broadcast Circuit*/
8607 bpf_error("'bcc' supported only on raw ATM");
8608 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8609 b1
= gen_atmfield_code(A_VCI
, 2, BPF_JEQ
, 0);
8614 /* Get all cells in Segment OAM F4 circuit*/
8616 bpf_error("'oam4sc' supported only on raw ATM");
8617 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8618 b1
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8623 /* Get all cells in End-to-End OAM F4 Circuit*/
8625 bpf_error("'oam4ec' supported only on raw ATM");
8626 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8627 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8632 /* Get all packets in connection Signalling Circuit */
8634 bpf_error("'sc' supported only on raw ATM");
8635 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8636 b1
= gen_atmfield_code(A_VCI
, 5, BPF_JEQ
, 0);
8641 /* Get all packets in ILMI Circuit */
8643 bpf_error("'ilmic' supported only on raw ATM");
8644 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8645 b1
= gen_atmfield_code(A_VCI
, 16, BPF_JEQ
, 0);
8650 /* Get all LANE packets */
8652 bpf_error("'lane' supported only on raw ATM");
8653 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LANE
, BPF_JEQ
, 0);
8656 * Arrange that all subsequent tests assume LANE
8657 * rather than LLC-encapsulated packets, and set
8658 * the offsets appropriately for LANE-encapsulated
8661 * We assume LANE means Ethernet, not Token Ring.
8663 PUSH_LINKHDR(DLT_EN10MB
, 0,
8664 off_payload
+ 2, /* Ethernet header */
8666 off_linktype
.constant_part
= off_linkhdr
.constant_part
+ 12;
8667 off_linkpl
.constant_part
= off_linkhdr
.constant_part
+ 14; /* Ethernet */
8668 off_nl
= 0; /* Ethernet II */
8669 off_nl_nosnap
= 3; /* 802.3+802.2 */
8673 /* Get all LLC-encapsulated packets */
8675 bpf_error("'llc' supported only on raw ATM");
8676 b1
= gen_atmfield_code(A_PROTOTYPE
, PT_LLC
, BPF_JEQ
, 0);
8677 linktype
= prevlinktype
;
8687 * Filtering for MTP2 messages based on li value
8688 * FISU, length is null
8689 * LSSU, length is 1 or 2
8690 * MSU, length is 3 or more
8691 * For MTP2_HSL, sequences are on 2 bytes, and length on 9 bits
8694 gen_mtp2type_abbrev(type
)
8697 struct block
*b0
, *b1
;
8702 if ( (linktype
!= DLT_MTP2
) &&
8703 (linktype
!= DLT_ERF
) &&
8704 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8705 bpf_error("'fisu' supported only on MTP2");
8706 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8707 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JEQ
, 0, 0);
8711 if ( (linktype
!= DLT_MTP2
) &&
8712 (linktype
!= DLT_ERF
) &&
8713 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8714 bpf_error("'lssu' supported only on MTP2");
8715 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 1, 2);
8716 b1
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 0);
8721 if ( (linktype
!= DLT_MTP2
) &&
8722 (linktype
!= DLT_ERF
) &&
8723 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8724 bpf_error("'msu' supported only on MTP2");
8725 b0
= gen_ncmp(OR_PACKET
, off_li
, BPF_B
, 0x3f, BPF_JGT
, 0, 2);
8729 if ( (linktype
!= DLT_MTP2
) &&
8730 (linktype
!= DLT_ERF
) &&
8731 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8732 bpf_error("'hfisu' supported only on MTP2_HSL");
8733 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8734 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JEQ
, 0, 0);
8738 if ( (linktype
!= DLT_MTP2
) &&
8739 (linktype
!= DLT_ERF
) &&
8740 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8741 bpf_error("'hlssu' supported only on MTP2_HSL");
8742 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 1, 0x0100);
8743 b1
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0);
8748 if ( (linktype
!= DLT_MTP2
) &&
8749 (linktype
!= DLT_ERF
) &&
8750 (linktype
!= DLT_MTP2_WITH_PHDR
) )
8751 bpf_error("'hmsu' supported only on MTP2_HSL");
8752 b0
= gen_ncmp(OR_PACKET
, off_li_hsl
, BPF_H
, 0xff80, BPF_JGT
, 0, 0x0100);
8762 gen_mtp3field_code(mtp3field
, jvalue
, jtype
, reverse
)
8769 bpf_u_int32 val1
, val2
, val3
;
8770 u_int newoff_sio
=off_sio
;
8771 u_int newoff_opc
=off_opc
;
8772 u_int newoff_dpc
=off_dpc
;
8773 u_int newoff_sls
=off_sls
;
8775 switch (mtp3field
) {
8778 newoff_sio
+= 3; /* offset for MTP2_HSL */
8782 if (off_sio
== (u_int
)-1)
8783 bpf_error("'sio' supported only on SS7");
8784 /* sio coded on 1 byte so max value 255 */
8786 bpf_error("sio value %u too big; max value = 255",
8788 b0
= gen_ncmp(OR_PACKET
, newoff_sio
, BPF_B
, 0xffffffff,
8789 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8795 if (off_opc
== (u_int
)-1)
8796 bpf_error("'opc' supported only on SS7");
8797 /* opc coded on 14 bits so max value 16383 */
8799 bpf_error("opc value %u too big; max value = 16383",
8801 /* the following instructions are made to convert jvalue
8802 * to the form used to write opc in an ss7 message*/
8803 val1
= jvalue
& 0x00003c00;
8805 val2
= jvalue
& 0x000003fc;
8807 val3
= jvalue
& 0x00000003;
8809 jvalue
= val1
+ val2
+ val3
;
8810 b0
= gen_ncmp(OR_PACKET
, newoff_opc
, BPF_W
, 0x00c0ff0f,
8811 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8819 if (off_dpc
== (u_int
)-1)
8820 bpf_error("'dpc' supported only on SS7");
8821 /* dpc coded on 14 bits so max value 16383 */
8823 bpf_error("dpc value %u too big; max value = 16383",
8825 /* the following instructions are made to convert jvalue
8826 * to the forme used to write dpc in an ss7 message*/
8827 val1
= jvalue
& 0x000000ff;
8829 val2
= jvalue
& 0x00003f00;
8831 jvalue
= val1
+ val2
;
8832 b0
= gen_ncmp(OR_PACKET
, newoff_dpc
, BPF_W
, 0xff3f0000,
8833 (u_int
)jtype
, reverse
, (u_int
)jvalue
);
8839 if (off_sls
== (u_int
)-1)
8840 bpf_error("'sls' supported only on SS7");
8841 /* sls coded on 4 bits so max value 15 */
8843 bpf_error("sls value %u too big; max value = 15",
8845 /* the following instruction is made to convert jvalue
8846 * to the forme used to write sls in an ss7 message*/
8847 jvalue
= jvalue
<< 4;
8848 b0
= gen_ncmp(OR_PACKET
, newoff_sls
, BPF_B
, 0xf0,
8849 (u_int
)jtype
,reverse
, (u_int
)jvalue
);
8858 static struct block
*
8859 gen_msg_abbrev(type
)
8865 * Q.2931 signalling protocol messages for handling virtual circuits
8866 * establishment and teardown
8871 b1
= gen_atmfield_code(A_MSGTYPE
, SETUP
, BPF_JEQ
, 0);
8875 b1
= gen_atmfield_code(A_MSGTYPE
, CALL_PROCEED
, BPF_JEQ
, 0);
8879 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT
, BPF_JEQ
, 0);
8883 b1
= gen_atmfield_code(A_MSGTYPE
, CONNECT_ACK
, BPF_JEQ
, 0);
8887 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE
, BPF_JEQ
, 0);
8890 case A_RELEASE_DONE
:
8891 b1
= gen_atmfield_code(A_MSGTYPE
, RELEASE_DONE
, BPF_JEQ
, 0);
8901 gen_atmmulti_abbrev(type
)
8904 struct block
*b0
, *b1
;
8910 bpf_error("'oam' supported only on raw ATM");
8911 b1
= gen_atmmulti_abbrev(A_OAMF4
);
8916 bpf_error("'oamf4' supported only on raw ATM");
8918 b0
= gen_atmfield_code(A_VCI
, 3, BPF_JEQ
, 0);
8919 b1
= gen_atmfield_code(A_VCI
, 4, BPF_JEQ
, 0);
8921 b0
= gen_atmfield_code(A_VPI
, 0, BPF_JEQ
, 0);
8927 * Get Q.2931 signalling messages for switched
8928 * virtual connection
8931 bpf_error("'connectmsg' supported only on raw ATM");
8932 b0
= gen_msg_abbrev(A_SETUP
);
8933 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8935 b0
= gen_msg_abbrev(A_CONNECT
);
8937 b0
= gen_msg_abbrev(A_CONNECTACK
);
8939 b0
= gen_msg_abbrev(A_RELEASE
);
8941 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8943 b0
= gen_atmtype_abbrev(A_SC
);
8949 bpf_error("'metaconnect' supported only on raw ATM");
8950 b0
= gen_msg_abbrev(A_SETUP
);
8951 b1
= gen_msg_abbrev(A_CALLPROCEED
);
8953 b0
= gen_msg_abbrev(A_CONNECT
);
8955 b0
= gen_msg_abbrev(A_RELEASE
);
8957 b0
= gen_msg_abbrev(A_RELEASE_DONE
);
8959 b0
= gen_atmtype_abbrev(A_METAC
);