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.
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
32 #include <pcap-stdinc.h>
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
47 * XXX - why was this included even on UNIX?
56 #include <sys/param.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
76 #include "ethertype.h"
80 #include "ieee80211.h"
82 #include "sunatmpos.h"
85 #include "pcap/ipnet.h"
87 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
88 #include <linux/types.h>
89 #include <linux/if_packet.h>
90 #include <linux/filter.h>
92 #ifdef HAVE_NET_PFVAR_H
93 #include <sys/socket.h>
95 #include <net/pf/pfvar.h>
96 #include <net/pf/if_pflog.h>
99 #define offsetof(s, e) ((size_t)&((s *)0)->e)
103 #include <netdb.h> /* for "struct addrinfo" */
106 #include <pcap/namedb.h>
108 #define ETHERMTU 1500
110 #ifndef IPPROTO_HOPOPTS
111 #define IPPROTO_HOPOPTS 0
113 #ifndef IPPROTO_ROUTING
114 #define IPPROTO_ROUTING 43
116 #ifndef IPPROTO_FRAGMENT
117 #define IPPROTO_FRAGMENT 44
119 #ifndef IPPROTO_DSTOPTS
120 #define IPPROTO_DSTOPTS 60
123 #define IPPROTO_SCTP 132
126 #ifdef HAVE_OS_PROTO_H
127 #include "os-proto.h"
130 #define JMP(c) ((c)|BPF_JMP|BPF_K)
133 static jmp_buf top_ctx;
134 static pcap_t *bpf_pcap;
136 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
138 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
140 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
145 static int pcap_fddipad;
150 bpf_error(const char *fmt, ...)
155 if (bpf_pcap != NULL)
156 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
163 static void init_linktype(pcap_t *);
165 static void init_regs(void);
166 static int alloc_reg(void);
167 static void free_reg(int);
169 static struct block *root;
172 * Value passed to gen_load_a() to indicate what the offset argument
176 OR_PACKET, /* relative to the beginning of the packet */
177 OR_LINK, /* relative to the beginning of the link-layer header */
178 OR_MACPL, /* relative to the end of the MAC-layer header */
179 OR_NET, /* relative to the network-layer header */
180 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
181 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
182 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
187 * As errors are handled by a longjmp, anything allocated must be freed
188 * in the longjmp handler, so it must be reachable from that handler.
189 * One thing that's allocated is the result of pcap_nametoaddrinfo();
190 * it must be freed with freeaddrinfo(). This variable points to any
191 * addrinfo structure that would need to be freed.
193 static struct addrinfo *ai;
197 * We divy out chunks of memory rather than call malloc each time so
198 * we don't have to worry about leaking memory. It's probably
199 * not a big deal if all this memory was wasted but if this ever
200 * goes into a library that would probably not be a good idea.
202 * XXX - this *is* in a library....
205 #define CHUNK0SIZE 1024
211 static struct chunk chunks[NCHUNKS];
212 static int cur_chunk;
214 static void *newchunk(u_int);
215 static void freechunks(void);
216 static inline struct block *new_block(int);
217 static inline struct slist *new_stmt(int);
218 static struct block *gen_retblk(int);
219 static inline void syntax(void);
221 static void backpatch(struct block *, struct block *);
222 static void merge(struct block *, struct block *);
223 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
224 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
225 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
226 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
227 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
228 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
230 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
231 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
232 bpf_u_int32, bpf_u_int32, int, bpf_int32);
233 static struct slist *gen_load_llrel(u_int, u_int);
234 static struct slist *gen_load_macplrel(u_int, u_int);
235 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
236 static struct slist *gen_loadx_iphdrlen(void);
237 static struct block *gen_uncond(int);
238 static inline struct block *gen_true(void);
239 static inline struct block *gen_false(void);
240 static struct block *gen_ether_linktype(int);
241 static struct block *gen_ipnet_linktype(int);
242 static struct block *gen_linux_sll_linktype(int);
243 static struct slist *gen_load_prism_llprefixlen(void);
244 static struct slist *gen_load_avs_llprefixlen(void);
245 static struct slist *gen_load_radiotap_llprefixlen(void);
246 static struct slist *gen_load_ppi_llprefixlen(void);
247 static void insert_compute_vloffsets(struct block *);
248 static struct slist *gen_llprefixlen(void);
249 static struct slist *gen_off_macpl(void);
250 static int ethertype_to_ppptype(int);
251 static struct block *gen_linktype(int);
252 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
253 static struct block *gen_llc_linktype(int);
254 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
256 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
258 static struct block *gen_ahostop(const u_char *, int);
259 static struct block *gen_ehostop(const u_char *, int);
260 static struct block *gen_fhostop(const u_char *, int);
261 static struct block *gen_thostop(const u_char *, int);
262 static struct block *gen_wlanhostop(const u_char *, int);
263 static struct block *gen_ipfchostop(const u_char *, int);
264 static struct block *gen_dnhostop(bpf_u_int32, int);
265 static struct block *gen_mpls_linktype(int);
266 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
268 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
271 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
273 static struct block *gen_ipfrag(void);
274 static struct block *gen_portatom(int, bpf_int32);
275 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
276 static struct block *gen_portatom6(int, bpf_int32);
277 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
278 struct block *gen_portop(int, int, int);
279 static struct block *gen_port(int, int, int);
280 struct block *gen_portrangeop(int, int, int, int);
281 static struct block *gen_portrange(int, int, int, int);
282 struct block *gen_portop6(int, int, int);
283 static struct block *gen_port6(int, int, int);
284 struct block *gen_portrangeop6(int, int, int, int);
285 static struct block *gen_portrange6(int, int, int, int);
286 static int lookup_proto(const char *, int);
287 static struct block *gen_protochain(int, int, int);
288 static struct block *gen_proto(int, int, int);
289 static struct slist *xfer_to_x(struct arth *);
290 static struct slist *xfer_to_a(struct arth *);
291 static struct block *gen_mac_multicast(int);
292 static struct block *gen_len(int, int);
293 static struct block *gen_check_802_11_data_frame(void);
295 static struct block *gen_ppi_dlt_check(void);
296 static struct block *gen_msg_abbrev(int type);
307 /* XXX Round up to nearest long. */
308 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
310 /* XXX Round up to structure boundary. */
314 cp = &chunks[cur_chunk];
315 if (n > cp->n_left) {
316 ++cp, k = ++cur_chunk;
318 bpf_error("out of memory");
319 size = CHUNK0SIZE << k;
320 cp->m = (void *)malloc(size);
322 bpf_error("out of memory");
323 memset((char *)cp->m, 0, size);
326 bpf_error("out of memory");
329 return (void *)((char *)cp->m + cp->n_left);
338 for (i = 0; i < NCHUNKS; ++i)
339 if (chunks[i].m != NULL) {
346 * A strdup whose allocations are freed after code generation is over.
350 register const char *s;
352 int n = strlen(s) + 1;
353 char *cp = newchunk(n);
359 static inline struct block *
365 p = (struct block *)newchunk(sizeof(*p));
372 static inline struct slist *
378 p = (struct slist *)newchunk(sizeof(*p));
384 static struct block *
388 struct block *b = new_block(BPF_RET|BPF_K);
397 bpf_error("syntax error in filter expression");
400 static bpf_u_int32 netmask;
405 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
406 const char *buf, int optimize, bpf_u_int32 mask);
409 pcap_compile(pcap_t *p, struct bpf_program *program,
410 const char *buf, int optimize, bpf_u_int32 mask)
414 EnterCriticalSection(&g_PcapCompileCriticalSection);
416 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
418 LeaveCriticalSection(&g_PcapCompileCriticalSection);
424 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
425 const char *buf, int optimize, bpf_u_int32 mask)
428 pcap_compile(pcap_t *p, struct bpf_program *program,
429 const char *buf, int optimize, bpf_u_int32 mask)
433 const char * volatile xbuf = buf;
437 * If this pcap_t hasn't been activated, it doesn't have a
438 * link-layer type, so we can't use it.
441 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
442 "not-yet-activated pcap_t passed to pcap_compile");
450 if (setjmp(top_ctx)) {
464 snaplen = pcap_snapshot(p);
466 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
467 "snaplen of 0 rejects all packets");
471 lex_init(xbuf ? xbuf : "");
479 root = gen_retblk(snaplen);
481 if (optimize && !no_optimize) {
484 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
485 bpf_error("expression rejects all packets");
487 program->bf_insns = icode_to_fcode(root, &len);
488 program->bf_len = len;
496 * entry point for using the compiler with no pcap open
497 * pass in all the stuff that is needed explicitly instead.
500 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
501 struct bpf_program *program,
502 const char *buf, int optimize, bpf_u_int32 mask)
507 p = pcap_open_dead(linktype_arg, snaplen_arg);
510 ret = pcap_compile(p, program, buf, optimize, mask);
516 * Clean up a "struct bpf_program" by freeing all the memory allocated
520 pcap_freecode(struct bpf_program *program)
523 if (program->bf_insns != NULL) {
524 free((char *)program->bf_insns);
525 program->bf_insns = NULL;
530 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
531 * which of the jt and jf fields has been resolved and which is a pointer
532 * back to another unresolved block (or nil). At least one of the fields
533 * in each block is already resolved.
536 backpatch(list, target)
537 struct block *list, *target;
554 * Merge the lists in b0 and b1, using the 'sense' field to indicate
555 * which of jt and jf is the link.
559 struct block *b0, *b1;
561 register struct block **p = &b0;
563 /* Find end of list. */
565 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
567 /* Concatenate the lists. */
575 struct block *ppi_dlt_check;
578 * Insert before the statements of the first (root) block any
579 * statements needed to load the lengths of any variable-length
580 * headers into registers.
582 * XXX - a fancier strategy would be to insert those before the
583 * statements of all blocks that use those lengths and that
584 * have no predecessors that use them, so that we only compute
585 * the lengths if we need them. There might be even better
586 * approaches than that.
588 * However, those strategies would be more complicated, and
589 * as we don't generate code to compute a length if the
590 * program has no tests that use the length, and as most
591 * tests will probably use those lengths, we would just
592 * postpone computing the lengths so that it's not done
593 * for tests that fail early, and it's not clear that's
596 insert_compute_vloffsets(p->head);
599 * For DLT_PPI captures, generate a check of the per-packet
600 * DLT value to make sure it's DLT_IEEE802_11.
602 ppi_dlt_check = gen_ppi_dlt_check();
603 if (ppi_dlt_check != NULL)
604 gen_and(ppi_dlt_check, p);
606 backpatch(p, gen_retblk(snaplen));
607 p->sense = !p->sense;
608 backpatch(p, gen_retblk(0));
614 struct block *b0, *b1;
616 backpatch(b0, b1->head);
617 b0->sense = !b0->sense;
618 b1->sense = !b1->sense;
620 b1->sense = !b1->sense;
626 struct block *b0, *b1;
628 b0->sense = !b0->sense;
629 backpatch(b0, b1->head);
630 b0->sense = !b0->sense;
639 b->sense = !b->sense;
642 static struct block *
643 gen_cmp(offrel, offset, size, v)
644 enum e_offrel offrel;
648 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
651 static struct block *
652 gen_cmp_gt(offrel, offset, size, v)
653 enum e_offrel offrel;
657 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
660 static struct block *
661 gen_cmp_ge(offrel, offset, size, v)
662 enum e_offrel offrel;
666 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
669 static struct block *
670 gen_cmp_lt(offrel, offset, size, v)
671 enum e_offrel offrel;
675 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
678 static struct block *
679 gen_cmp_le(offrel, offset, size, v)
680 enum e_offrel offrel;
684 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
687 static struct block *
688 gen_mcmp(offrel, offset, size, v, mask)
689 enum e_offrel offrel;
694 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
697 static struct block *
698 gen_bcmp(offrel, offset, size, v)
699 enum e_offrel offrel;
700 register u_int offset, size;
701 register const u_char *v;
703 register struct block *b, *tmp;
707 register const u_char *p = &v[size - 4];
708 bpf_int32 w = ((bpf_int32)p[0] << 24) |
709 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
711 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
718 register const u_char *p = &v[size - 2];
719 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
721 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
728 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
737 * AND the field of size "size" at offset "offset" relative to the header
738 * specified by "offrel" with "mask", and compare it with the value "v"
739 * with the test specified by "jtype"; if "reverse" is true, the test
740 * should test the opposite of "jtype".
742 static struct block *
743 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
744 enum e_offrel offrel;
746 bpf_u_int32 offset, size, mask, jtype;
749 struct slist *s, *s2;
752 s = gen_load_a(offrel, offset, size);
754 if (mask != 0xffffffff) {
755 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
760 b = new_block(JMP(jtype));
763 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
769 * Various code constructs need to know the layout of the data link
770 * layer. These variables give the necessary offsets from the beginning
771 * of the packet data.
775 * This is the offset of the beginning of the link-layer header from
776 * the beginning of the raw packet data.
778 * It's usually 0, except for 802.11 with a fixed-length radio header.
779 * (For 802.11 with a variable-length radio header, we have to generate
780 * code to compute that offset; off_ll is 0 in that case.)
785 * If there's a variable-length header preceding the link-layer header,
786 * "reg_off_ll" is the register number for a register containing the
787 * length of that header, and therefore the offset of the link-layer
788 * header from the beginning of the raw packet data. Otherwise,
789 * "reg_off_ll" is -1.
791 static int reg_off_ll;
794 * This is the offset of the beginning of the MAC-layer header from
795 * the beginning of the link-layer header.
796 * It's usually 0, except for ATM LANE, where it's the offset, relative
797 * to the beginning of the raw packet data, of the Ethernet header, and
798 * for Ethernet with various additional information.
800 static u_int off_mac;
803 * This is the offset of the beginning of the MAC-layer payload,
804 * from the beginning of the raw packet data.
806 * I.e., it's the sum of the length of the link-layer header (without,
807 * for example, any 802.2 LLC header, so it's the MAC-layer
808 * portion of that header), plus any prefix preceding the
811 static u_int off_macpl;
814 * This is 1 if the offset of the beginning of the MAC-layer payload
815 * from the beginning of the link-layer header is variable-length.
817 static int off_macpl_is_variable;
820 * If the link layer has variable_length headers, "reg_off_macpl"
821 * is the register number for a register containing the length of the
822 * link-layer header plus the length of any variable-length header
823 * preceding the link-layer header. Otherwise, "reg_off_macpl"
826 static int reg_off_macpl;
829 * "off_linktype" is the offset to information in the link-layer header
830 * giving the packet type. This offset is relative to the beginning
831 * of the link-layer header (i.e., it doesn't include off_ll).
833 * For Ethernet, it's the offset of the Ethernet type field.
835 * For link-layer types that always use 802.2 headers, it's the
836 * offset of the LLC header.
838 * For PPP, it's the offset of the PPP type field.
840 * For Cisco HDLC, it's the offset of the CHDLC type field.
842 * For BSD loopback, it's the offset of the AF_ value.
844 * For Linux cooked sockets, it's the offset of the type field.
846 * It's set to -1 for no encapsulation, in which case, IP is assumed.
848 static u_int off_linktype;
851 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
852 * checks to check the PPP header, assumed to follow a LAN-style link-
853 * layer header and a PPPoE session header.
855 static int is_pppoes = 0;
858 * TRUE if the link layer includes an ATM pseudo-header.
860 static int is_atm = 0;
863 * TRUE if "lane" appeared in the filter; it causes us to generate
864 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
866 static int is_lane = 0;
869 * These are offsets for the ATM pseudo-header.
871 static u_int off_vpi;
872 static u_int off_vci;
873 static u_int off_proto;
876 * These are offsets for the MTP2 fields.
881 * These are offsets for the MTP3 fields.
883 static u_int off_sio;
884 static u_int off_opc;
885 static u_int off_dpc;
886 static u_int off_sls;
889 * This is the offset of the first byte after the ATM pseudo_header,
890 * or -1 if there is no ATM pseudo-header.
892 static u_int off_payload;
895 * These are offsets to the beginning of the network-layer header.
896 * They are relative to the beginning of the MAC-layer payload (i.e.,
897 * they don't include off_ll or off_macpl).
899 * If the link layer never uses 802.2 LLC:
901 * "off_nl" and "off_nl_nosnap" are the same.
903 * If the link layer always uses 802.2 LLC:
905 * "off_nl" is the offset if there's a SNAP header following
908 * "off_nl_nosnap" is the offset if there's no SNAP header.
910 * If the link layer is Ethernet:
912 * "off_nl" is the offset if the packet is an Ethernet II packet
913 * (we assume no 802.3+802.2+SNAP);
915 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
916 * with an 802.2 header following it.
919 static u_int off_nl_nosnap;
927 linktype = pcap_datalink(p);
929 pcap_fddipad = p->fddipad;
933 * Assume it's not raw ATM with a pseudo-header, for now.
944 * And that we're not doing PPPoE.
949 * And assume we're not doing SS7.
958 * Also assume it's not 802.11.
962 off_macpl_is_variable = 0;
966 label_stack_depth = 0;
976 off_nl = 0; /* XXX in reality, variable! */
977 off_nl_nosnap = 0; /* no 802.2 LLC */
980 case DLT_ARCNET_LINUX:
983 off_nl = 0; /* XXX in reality, variable! */
984 off_nl_nosnap = 0; /* no 802.2 LLC */
989 off_macpl = 14; /* Ethernet header length */
990 off_nl = 0; /* Ethernet II */
991 off_nl_nosnap = 3; /* 802.3+802.2 */
996 * SLIP doesn't have a link level type. The 16 byte
997 * header is hacked into our SLIP driver.
1002 off_nl_nosnap = 0; /* no 802.2 LLC */
1005 case DLT_SLIP_BSDOS:
1006 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1011 off_nl_nosnap = 0; /* no 802.2 LLC */
1019 off_nl_nosnap = 0; /* no 802.2 LLC */
1026 off_nl_nosnap = 0; /* no 802.2 LLC */
1031 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1032 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1036 off_nl_nosnap = 0; /* no 802.2 LLC */
1041 * This does no include the Ethernet header, and
1042 * only covers session state.
1047 off_nl_nosnap = 0; /* no 802.2 LLC */
1054 off_nl_nosnap = 0; /* no 802.2 LLC */
1059 * FDDI doesn't really have a link-level type field.
1060 * We set "off_linktype" to the offset of the LLC header.
1062 * To check for Ethernet types, we assume that SSAP = SNAP
1063 * is being used and pick out the encapsulated Ethernet type.
1064 * XXX - should we generate code to check for SNAP?
1068 off_linktype += pcap_fddipad;
1070 off_macpl = 13; /* FDDI MAC header length */
1072 off_macpl += pcap_fddipad;
1074 off_nl = 8; /* 802.2+SNAP */
1075 off_nl_nosnap = 3; /* 802.2 */
1080 * Token Ring doesn't really have a link-level type field.
1081 * We set "off_linktype" to the offset of the LLC header.
1083 * To check for Ethernet types, we assume that SSAP = SNAP
1084 * is being used and pick out the encapsulated Ethernet type.
1085 * XXX - should we generate code to check for SNAP?
1087 * XXX - the header is actually variable-length.
1088 * Some various Linux patched versions gave 38
1089 * as "off_linktype" and 40 as "off_nl"; however,
1090 * if a token ring packet has *no* routing
1091 * information, i.e. is not source-routed, the correct
1092 * values are 20 and 22, as they are in the vanilla code.
1094 * A packet is source-routed iff the uppermost bit
1095 * of the first byte of the source address, at an
1096 * offset of 8, has the uppermost bit set. If the
1097 * packet is source-routed, the total number of bytes
1098 * of routing information is 2 plus bits 0x1F00 of
1099 * the 16-bit value at an offset of 14 (shifted right
1100 * 8 - figure out which byte that is).
1103 off_macpl = 14; /* Token Ring MAC header length */
1104 off_nl = 8; /* 802.2+SNAP */
1105 off_nl_nosnap = 3; /* 802.2 */
1108 case DLT_IEEE802_11:
1109 case DLT_PRISM_HEADER:
1110 case DLT_IEEE802_11_RADIO_AVS:
1111 case DLT_IEEE802_11_RADIO:
1113 * 802.11 doesn't really have a link-level type field.
1114 * We set "off_linktype" to the offset of the LLC header.
1116 * To check for Ethernet types, we assume that SSAP = SNAP
1117 * is being used and pick out the encapsulated Ethernet type.
1118 * XXX - should we generate code to check for SNAP?
1120 * We also handle variable-length radio headers here.
1121 * The Prism header is in theory variable-length, but in
1122 * practice it's always 144 bytes long. However, some
1123 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1124 * sometimes or always supply an AVS header, so we
1125 * have to check whether the radio header is a Prism
1126 * header or an AVS header, so, in practice, it's
1130 off_macpl = 0; /* link-layer header is variable-length */
1131 off_macpl_is_variable = 1;
1132 off_nl = 8; /* 802.2+SNAP */
1133 off_nl_nosnap = 3; /* 802.2 */
1138 * At the moment we treat PPI the same way that we treat
1139 * normal Radiotap encoded packets. The difference is in
1140 * the function that generates the code at the beginning
1141 * to compute the header length. Since this code generator
1142 * of PPI supports bare 802.11 encapsulation only (i.e.
1143 * the encapsulated DLT should be DLT_IEEE802_11) we
1144 * generate code to check for this too.
1147 off_macpl = 0; /* link-layer header is variable-length */
1148 off_macpl_is_variable = 1;
1149 off_nl = 8; /* 802.2+SNAP */
1150 off_nl_nosnap = 3; /* 802.2 */
1153 case DLT_ATM_RFC1483:
1154 case DLT_ATM_CLIP: /* Linux ATM defines this */
1156 * assume routed, non-ISO PDUs
1157 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1159 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1160 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1161 * latter would presumably be treated the way PPPoE
1162 * should be, so you can do "pppoe and udp port 2049"
1163 * or "pppoa and tcp port 80" and have it check for
1164 * PPPo{A,E} and a PPP protocol of IP and....
1167 off_macpl = 0; /* packet begins with LLC header */
1168 off_nl = 8; /* 802.2+SNAP */
1169 off_nl_nosnap = 3; /* 802.2 */
1174 * Full Frontal ATM; you get AALn PDUs with an ATM
1178 off_vpi = SUNATM_VPI_POS;
1179 off_vci = SUNATM_VCI_POS;
1180 off_proto = PROTO_POS;
1181 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1182 off_payload = SUNATM_PKT_BEGIN_POS;
1183 off_linktype = off_payload;
1184 off_macpl = off_payload; /* if LLC-encapsulated */
1185 off_nl = 8; /* 802.2+SNAP */
1186 off_nl_nosnap = 3; /* 802.2 */
1195 off_nl_nosnap = 0; /* no 802.2 LLC */
1198 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1202 off_nl_nosnap = 0; /* no 802.2 LLC */
1207 * LocalTalk does have a 1-byte type field in the LLAP header,
1208 * but really it just indicates whether there is a "short" or
1209 * "long" DDP packet following.
1214 off_nl_nosnap = 0; /* no 802.2 LLC */
1217 case DLT_IP_OVER_FC:
1219 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1220 * link-level type field. We set "off_linktype" to the
1221 * offset of the LLC header.
1223 * To check for Ethernet types, we assume that SSAP = SNAP
1224 * is being used and pick out the encapsulated Ethernet type.
1225 * XXX - should we generate code to check for SNAP? RFC
1226 * 2625 says SNAP should be used.
1230 off_nl = 8; /* 802.2+SNAP */
1231 off_nl_nosnap = 3; /* 802.2 */
1236 * XXX - we should set this to handle SNAP-encapsulated
1237 * frames (NLPID of 0x80).
1242 off_nl_nosnap = 0; /* no 802.2 LLC */
1246 * the only BPF-interesting FRF.16 frames are non-control frames;
1247 * Frame Relay has a variable length link-layer
1248 * so lets start with offset 4 for now and increments later on (FIXME);
1254 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1257 case DLT_APPLE_IP_OVER_IEEE1394:
1261 off_nl_nosnap = 0; /* no 802.2 LLC */
1264 case DLT_SYMANTEC_FIREWALL:
1267 off_nl = 0; /* Ethernet II */
1268 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1271 #ifdef HAVE_NET_PFVAR_H
1274 off_macpl = PFLOG_HDRLEN;
1276 off_nl_nosnap = 0; /* no 802.2 LLC */
1280 case DLT_JUNIPER_MFR:
1281 case DLT_JUNIPER_MLFR:
1282 case DLT_JUNIPER_MLPPP:
1283 case DLT_JUNIPER_PPP:
1284 case DLT_JUNIPER_CHDLC:
1285 case DLT_JUNIPER_FRELAY:
1289 off_nl_nosnap = -1; /* no 802.2 LLC */
1292 case DLT_JUNIPER_ATM1:
1293 off_linktype = 4; /* in reality variable between 4-8 */
1294 off_macpl = 4; /* in reality variable between 4-8 */
1299 case DLT_JUNIPER_ATM2:
1300 off_linktype = 8; /* in reality variable between 8-12 */
1301 off_macpl = 8; /* in reality variable between 8-12 */
1306 /* frames captured on a Juniper PPPoE service PIC
1307 * contain raw ethernet frames */
1308 case DLT_JUNIPER_PPPOE:
1309 case DLT_JUNIPER_ETHER:
1312 off_nl = 18; /* Ethernet II */
1313 off_nl_nosnap = 21; /* 802.3+802.2 */
1316 case DLT_JUNIPER_PPPOE_ATM:
1320 off_nl_nosnap = -1; /* no 802.2 LLC */
1323 case DLT_JUNIPER_GGSN:
1327 off_nl_nosnap = -1; /* no 802.2 LLC */
1330 case DLT_JUNIPER_ES:
1332 off_macpl = -1; /* not really a network layer but raw IP addresses */
1333 off_nl = -1; /* not really a network layer but raw IP addresses */
1334 off_nl_nosnap = -1; /* no 802.2 LLC */
1337 case DLT_JUNIPER_MONITOR:
1340 off_nl = 0; /* raw IP/IP6 header */
1341 off_nl_nosnap = -1; /* no 802.2 LLC */
1344 case DLT_JUNIPER_SERVICES:
1346 off_macpl = -1; /* L3 proto location dep. on cookie type */
1347 off_nl = -1; /* L3 proto location dep. on cookie type */
1348 off_nl_nosnap = -1; /* no 802.2 LLC */
1351 case DLT_JUNIPER_VP:
1358 case DLT_JUNIPER_ST:
1365 case DLT_JUNIPER_ISM:
1372 case DLT_JUNIPER_VS:
1373 case DLT_JUNIPER_SRX_E2E:
1374 case DLT_JUNIPER_FIBRECHANNEL:
1375 case DLT_JUNIPER_ATM_CEMIC:
1394 case DLT_MTP2_WITH_PHDR:
1427 * Currently, only raw "link[N:M]" filtering is supported.
1429 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1431 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1432 off_nl_nosnap = -1; /* no 802.2 LLC */
1433 off_mac = 1; /* step over the kiss length byte */
1438 off_macpl = 24; /* ipnet header length */
1443 case DLT_NETANALYZER:
1444 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1445 off_linktype = 16; /* includes 4-byte pseudo-header */
1446 off_macpl = 18; /* pseudo-header+Ethernet header length */
1447 off_nl = 0; /* Ethernet II */
1448 off_nl_nosnap = 3; /* 802.3+802.2 */
1451 case DLT_NETANALYZER_TRANSPARENT:
1452 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1453 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1454 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1455 off_nl = 0; /* Ethernet II */
1456 off_nl_nosnap = 3; /* 802.3+802.2 */
1461 * For values in the range in which we've assigned new
1462 * DLT_ values, only raw "link[N:M]" filtering is supported.
1464 if (linktype >= DLT_MATCHING_MIN &&
1465 linktype <= DLT_MATCHING_MAX) {
1474 bpf_error("unknown data link type %d", linktype);
1479 * Load a value relative to the beginning of the link-layer header.
1480 * The link-layer header doesn't necessarily begin at the beginning
1481 * of the packet data; there might be a variable-length prefix containing
1482 * radio information.
1484 static struct slist *
1485 gen_load_llrel(offset, size)
1488 struct slist *s, *s2;
1490 s = gen_llprefixlen();
1493 * If "s" is non-null, it has code to arrange that the X register
1494 * contains the length of the prefix preceding the link-layer
1497 * Otherwise, the length of the prefix preceding the link-layer
1498 * header is "off_ll".
1502 * There's a variable-length prefix preceding the
1503 * link-layer header. "s" points to a list of statements
1504 * that put the length of that prefix into the X register.
1505 * do an indirect load, to use the X register as an offset.
1507 s2 = new_stmt(BPF_LD|BPF_IND|size);
1512 * There is no variable-length header preceding the
1513 * link-layer header; add in off_ll, which, if there's
1514 * a fixed-length header preceding the link-layer header,
1515 * is the length of that header.
1517 s = new_stmt(BPF_LD|BPF_ABS|size);
1518 s->s.k = offset + off_ll;
1524 * Load a value relative to the beginning of the MAC-layer payload.
1526 static struct slist *
1527 gen_load_macplrel(offset, size)
1530 struct slist *s, *s2;
1532 s = gen_off_macpl();
1535 * If s is non-null, the offset of the MAC-layer payload is
1536 * variable, and s points to a list of instructions that
1537 * arrange that the X register contains that offset.
1539 * Otherwise, the offset of the MAC-layer payload is constant,
1540 * and is in off_macpl.
1544 * The offset of the MAC-layer payload is in the X
1545 * register. Do an indirect load, to use the X register
1548 s2 = new_stmt(BPF_LD|BPF_IND|size);
1553 * The offset of the MAC-layer payload is constant,
1554 * and is in off_macpl; load the value at that offset
1555 * plus the specified offset.
1557 s = new_stmt(BPF_LD|BPF_ABS|size);
1558 s->s.k = off_macpl + offset;
1564 * Load a value relative to the beginning of the specified header.
1566 static struct slist *
1567 gen_load_a(offrel, offset, size)
1568 enum e_offrel offrel;
1571 struct slist *s, *s2;
1576 s = new_stmt(BPF_LD|BPF_ABS|size);
1581 s = gen_load_llrel(offset, size);
1585 s = gen_load_macplrel(offset, size);
1589 s = gen_load_macplrel(off_nl + offset, size);
1593 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1598 * Load the X register with the length of the IPv4 header
1599 * (plus the offset of the link-layer header, if it's
1600 * preceded by a variable-length header such as a radio
1601 * header), in bytes.
1603 s = gen_loadx_iphdrlen();
1606 * Load the item at {offset of the MAC-layer payload} +
1607 * {offset, relative to the start of the MAC-layer
1608 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1609 * {specified offset}.
1611 * (If the offset of the MAC-layer payload is variable,
1612 * it's included in the value in the X register, and
1615 s2 = new_stmt(BPF_LD|BPF_IND|size);
1616 s2->s.k = off_macpl + off_nl + offset;
1621 s = gen_load_macplrel(off_nl + 40 + offset, size);
1632 * Generate code to load into the X register the sum of the length of
1633 * the IPv4 header and any variable-length header preceding the link-layer
1636 static struct slist *
1637 gen_loadx_iphdrlen()
1639 struct slist *s, *s2;
1641 s = gen_off_macpl();
1644 * There's a variable-length prefix preceding the
1645 * link-layer header, or the link-layer header is itself
1646 * variable-length. "s" points to a list of statements
1647 * that put the offset of the MAC-layer payload into
1650 * The 4*([k]&0xf) addressing mode can't be used, as we
1651 * don't have a constant offset, so we have to load the
1652 * value in question into the A register and add to it
1653 * the value from the X register.
1655 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1658 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1661 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1666 * The A register now contains the length of the
1667 * IP header. We need to add to it the offset of
1668 * the MAC-layer payload, which is still in the X
1669 * register, and move the result into the X register.
1671 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1672 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1675 * There is no variable-length header preceding the
1676 * link-layer header, and the link-layer header is
1677 * fixed-length; load the length of the IPv4 header,
1678 * which is at an offset of off_nl from the beginning
1679 * of the MAC-layer payload, and thus at an offset
1680 * of off_mac_pl + off_nl from the beginning of the
1683 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1684 s->s.k = off_macpl + off_nl;
1689 static struct block *
1696 s = new_stmt(BPF_LD|BPF_IMM);
1698 b = new_block(JMP(BPF_JEQ));
1704 static inline struct block *
1707 return gen_uncond(1);
1710 static inline struct block *
1713 return gen_uncond(0);
1717 * Byte-swap a 32-bit number.
1718 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1719 * big-endian platforms.)
1721 #define SWAPLONG(y) \
1722 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1725 * Generate code to match a particular packet type.
1727 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1728 * value, if <= ETHERMTU. We use that to determine whether to
1729 * match the type/length field or to check the type/length field for
1730 * a value <= ETHERMTU to see whether it's a type field and then do
1731 * the appropriate test.
1733 static struct block *
1734 gen_ether_linktype(proto)
1737 struct block *b0, *b1;
1743 case LLCSAP_NETBEUI:
1745 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1746 * so we check the DSAP and SSAP.
1748 * LLCSAP_IP checks for IP-over-802.2, rather
1749 * than IP-over-Ethernet or IP-over-SNAP.
1751 * XXX - should we check both the DSAP and the
1752 * SSAP, like this, or should we check just the
1753 * DSAP, as we do for other types <= ETHERMTU
1754 * (i.e., other SAP values)?
1756 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1758 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1759 ((proto << 8) | proto));
1767 * Ethernet_II frames, which are Ethernet
1768 * frames with a frame type of ETHERTYPE_IPX;
1770 * Ethernet_802.3 frames, which are 802.3
1771 * frames (i.e., the type/length field is
1772 * a length field, <= ETHERMTU, rather than
1773 * a type field) with the first two bytes
1774 * after the Ethernet/802.3 header being
1777 * Ethernet_802.2 frames, which are 802.3
1778 * frames with an 802.2 LLC header and
1779 * with the IPX LSAP as the DSAP in the LLC
1782 * Ethernet_SNAP frames, which are 802.3
1783 * frames with an LLC header and a SNAP
1784 * header and with an OUI of 0x000000
1785 * (encapsulated Ethernet) and a protocol
1786 * ID of ETHERTYPE_IPX in the SNAP header.
1788 * XXX - should we generate the same code both
1789 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1793 * This generates code to check both for the
1794 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1796 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1797 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1801 * Now we add code to check for SNAP frames with
1802 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1804 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1808 * Now we generate code to check for 802.3
1809 * frames in general.
1811 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1815 * Now add the check for 802.3 frames before the
1816 * check for Ethernet_802.2 and Ethernet_802.3,
1817 * as those checks should only be done on 802.3
1818 * frames, not on Ethernet frames.
1823 * Now add the check for Ethernet_II frames, and
1824 * do that before checking for the other frame
1827 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1828 (bpf_int32)ETHERTYPE_IPX);
1832 case ETHERTYPE_ATALK:
1833 case ETHERTYPE_AARP:
1835 * EtherTalk (AppleTalk protocols on Ethernet link
1836 * layer) may use 802.2 encapsulation.
1840 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1841 * we check for an Ethernet type field less than
1842 * 1500, which means it's an 802.3 length field.
1844 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1848 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1849 * SNAP packets with an organization code of
1850 * 0x080007 (Apple, for Appletalk) and a protocol
1851 * type of ETHERTYPE_ATALK (Appletalk).
1853 * 802.2-encapsulated ETHERTYPE_AARP packets are
1854 * SNAP packets with an organization code of
1855 * 0x000000 (encapsulated Ethernet) and a protocol
1856 * type of ETHERTYPE_AARP (Appletalk ARP).
1858 if (proto == ETHERTYPE_ATALK)
1859 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1860 else /* proto == ETHERTYPE_AARP */
1861 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1865 * Check for Ethernet encapsulation (Ethertalk
1866 * phase 1?); we just check for the Ethernet
1869 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1875 if (proto <= ETHERMTU) {
1877 * This is an LLC SAP value, so the frames
1878 * that match would be 802.2 frames.
1879 * Check that the frame is an 802.2 frame
1880 * (i.e., that the length/type field is
1881 * a length field, <= ETHERMTU) and
1882 * then check the DSAP.
1884 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1886 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1892 * This is an Ethernet type, so compare
1893 * the length/type field with it (if
1894 * the frame is an 802.2 frame, the length
1895 * field will be <= ETHERMTU, and, as
1896 * "proto" is > ETHERMTU, this test
1897 * will fail and the frame won't match,
1898 * which is what we want).
1900 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1907 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1908 * or IPv6 then we have an error.
1910 static struct block *
1911 gen_ipnet_linktype(proto)
1917 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1918 (bpf_int32)IPH_AF_INET);
1921 case ETHERTYPE_IPV6:
1922 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1923 (bpf_int32)IPH_AF_INET6);
1934 * Generate code to match a particular packet type.
1936 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1937 * value, if <= ETHERMTU. We use that to determine whether to
1938 * match the type field or to check the type field for the special
1939 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1941 static struct block *
1942 gen_linux_sll_linktype(proto)
1945 struct block *b0, *b1;
1951 case LLCSAP_NETBEUI:
1953 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1954 * so we check the DSAP and SSAP.
1956 * LLCSAP_IP checks for IP-over-802.2, rather
1957 * than IP-over-Ethernet or IP-over-SNAP.
1959 * XXX - should we check both the DSAP and the
1960 * SSAP, like this, or should we check just the
1961 * DSAP, as we do for other types <= ETHERMTU
1962 * (i.e., other SAP values)?
1964 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1965 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1966 ((proto << 8) | proto));
1972 * Ethernet_II frames, which are Ethernet
1973 * frames with a frame type of ETHERTYPE_IPX;
1975 * Ethernet_802.3 frames, which have a frame
1976 * type of LINUX_SLL_P_802_3;
1978 * Ethernet_802.2 frames, which are 802.3
1979 * frames with an 802.2 LLC header (i.e, have
1980 * a frame type of LINUX_SLL_P_802_2) and
1981 * with the IPX LSAP as the DSAP in the LLC
1984 * Ethernet_SNAP frames, which are 802.3
1985 * frames with an LLC header and a SNAP
1986 * header and with an OUI of 0x000000
1987 * (encapsulated Ethernet) and a protocol
1988 * ID of ETHERTYPE_IPX in the SNAP header.
1990 * First, do the checks on LINUX_SLL_P_802_2
1991 * frames; generate the check for either
1992 * Ethernet_802.2 or Ethernet_SNAP frames, and
1993 * then put a check for LINUX_SLL_P_802_2 frames
1996 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1997 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
1999 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2003 * Now check for 802.3 frames and OR that with
2004 * the previous test.
2006 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
2010 * Now add the check for Ethernet_II frames, and
2011 * do that before checking for the other frame
2014 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2015 (bpf_int32)ETHERTYPE_IPX);
2019 case ETHERTYPE_ATALK:
2020 case ETHERTYPE_AARP:
2022 * EtherTalk (AppleTalk protocols on Ethernet link
2023 * layer) may use 802.2 encapsulation.
2027 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2028 * we check for the 802.2 protocol type in the
2029 * "Ethernet type" field.
2031 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2034 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2035 * SNAP packets with an organization code of
2036 * 0x080007 (Apple, for Appletalk) and a protocol
2037 * type of ETHERTYPE_ATALK (Appletalk).
2039 * 802.2-encapsulated ETHERTYPE_AARP packets are
2040 * SNAP packets with an organization code of
2041 * 0x000000 (encapsulated Ethernet) and a protocol
2042 * type of ETHERTYPE_AARP (Appletalk ARP).
2044 if (proto == ETHERTYPE_ATALK)
2045 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2046 else /* proto == ETHERTYPE_AARP */
2047 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2051 * Check for Ethernet encapsulation (Ethertalk
2052 * phase 1?); we just check for the Ethernet
2055 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2061 if (proto <= ETHERMTU) {
2063 * This is an LLC SAP value, so the frames
2064 * that match would be 802.2 frames.
2065 * Check for the 802.2 protocol type
2066 * in the "Ethernet type" field, and
2067 * then check the DSAP.
2069 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2071 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2077 * This is an Ethernet type, so compare
2078 * the length/type field with it (if
2079 * the frame is an 802.2 frame, the length
2080 * field will be <= ETHERMTU, and, as
2081 * "proto" is > ETHERMTU, this test
2082 * will fail and the frame won't match,
2083 * which is what we want).
2085 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2091 static struct slist *
2092 gen_load_prism_llprefixlen()
2094 struct slist *s1, *s2;
2095 struct slist *sjeq_avs_cookie;
2096 struct slist *sjcommon;
2099 * This code is not compatible with the optimizer, as
2100 * we are generating jmp instructions within a normal
2101 * slist of instructions
2106 * Generate code to load the length of the radio header into
2107 * the register assigned to hold that length, if one has been
2108 * assigned. (If one hasn't been assigned, no code we've
2109 * generated uses that prefix, so we don't need to generate any
2112 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2113 * or always use the AVS header rather than the Prism header.
2114 * We load a 4-byte big-endian value at the beginning of the
2115 * raw packet data, and see whether, when masked with 0xFFFFF000,
2116 * it's equal to 0x80211000. If so, that indicates that it's
2117 * an AVS header (the masked-out bits are the version number).
2118 * Otherwise, it's a Prism header.
2120 * XXX - the Prism header is also, in theory, variable-length,
2121 * but no known software generates headers that aren't 144
2124 if (reg_off_ll != -1) {
2128 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2132 * AND it with 0xFFFFF000.
2134 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2135 s2->s.k = 0xFFFFF000;
2139 * Compare with 0x80211000.
2141 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2142 sjeq_avs_cookie->s.k = 0x80211000;
2143 sappend(s1, sjeq_avs_cookie);
2148 * The 4 bytes at an offset of 4 from the beginning of
2149 * the AVS header are the length of the AVS header.
2150 * That field is big-endian.
2152 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2155 sjeq_avs_cookie->s.jt = s2;
2158 * Now jump to the code to allocate a register
2159 * into which to save the header length and
2160 * store the length there. (The "jump always"
2161 * instruction needs to have the k field set;
2162 * it's added to the PC, so, as we're jumping
2163 * over a single instruction, it should be 1.)
2165 sjcommon = new_stmt(JMP(BPF_JA));
2167 sappend(s1, sjcommon);
2170 * Now for the code that handles the Prism header.
2171 * Just load the length of the Prism header (144)
2172 * into the A register. Have the test for an AVS
2173 * header branch here if we don't have an AVS header.
2175 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2178 sjeq_avs_cookie->s.jf = s2;
2181 * Now allocate a register to hold that value and store
2182 * it. The code for the AVS header will jump here after
2183 * loading the length of the AVS header.
2185 s2 = new_stmt(BPF_ST);
2186 s2->s.k = reg_off_ll;
2188 sjcommon->s.jf = s2;
2191 * Now move it into the X register.
2193 s2 = new_stmt(BPF_MISC|BPF_TAX);
2201 static struct slist *
2202 gen_load_avs_llprefixlen()
2204 struct slist *s1, *s2;
2207 * Generate code to load the length of the AVS header into
2208 * the register assigned to hold that length, if one has been
2209 * assigned. (If one hasn't been assigned, no code we've
2210 * generated uses that prefix, so we don't need to generate any
2213 if (reg_off_ll != -1) {
2215 * The 4 bytes at an offset of 4 from the beginning of
2216 * the AVS header are the length of the AVS header.
2217 * That field is big-endian.
2219 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2223 * Now allocate a register to hold that value and store
2226 s2 = new_stmt(BPF_ST);
2227 s2->s.k = reg_off_ll;
2231 * Now move it into the X register.
2233 s2 = new_stmt(BPF_MISC|BPF_TAX);
2241 static struct slist *
2242 gen_load_radiotap_llprefixlen()
2244 struct slist *s1, *s2;
2247 * Generate code to load the length of the radiotap header into
2248 * the register assigned to hold that length, if one has been
2249 * assigned. (If one hasn't been assigned, no code we've
2250 * generated uses that prefix, so we don't need to generate any
2253 if (reg_off_ll != -1) {
2255 * The 2 bytes at offsets of 2 and 3 from the beginning
2256 * of the radiotap header are the length of the radiotap
2257 * header; unfortunately, it's little-endian, so we have
2258 * to load it a byte at a time and construct the value.
2262 * Load the high-order byte, at an offset of 3, shift it
2263 * left a byte, and put the result in the X register.
2265 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2267 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2270 s2 = new_stmt(BPF_MISC|BPF_TAX);
2274 * Load the next byte, at an offset of 2, and OR the
2275 * value from the X register into it.
2277 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2280 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2284 * Now allocate a register to hold that value and store
2287 s2 = new_stmt(BPF_ST);
2288 s2->s.k = reg_off_ll;
2292 * Now move it into the X register.
2294 s2 = new_stmt(BPF_MISC|BPF_TAX);
2303 * At the moment we treat PPI as normal Radiotap encoded
2304 * packets. The difference is in the function that generates
2305 * the code at the beginning to compute the header length.
2306 * Since this code generator of PPI supports bare 802.11
2307 * encapsulation only (i.e. the encapsulated DLT should be
2308 * DLT_IEEE802_11) we generate code to check for this too;
2309 * that's done in finish_parse().
2311 static struct slist *
2312 gen_load_ppi_llprefixlen()
2314 struct slist *s1, *s2;
2317 * Generate code to load the length of the radiotap header
2318 * into the register assigned to hold that length, if one has
2321 if (reg_off_ll != -1) {
2323 * The 2 bytes at offsets of 2 and 3 from the beginning
2324 * of the radiotap header are the length of the radiotap
2325 * header; unfortunately, it's little-endian, so we have
2326 * to load it a byte at a time and construct the value.
2330 * Load the high-order byte, at an offset of 3, shift it
2331 * left a byte, and put the result in the X register.
2333 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2335 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2338 s2 = new_stmt(BPF_MISC|BPF_TAX);
2342 * Load the next byte, at an offset of 2, and OR the
2343 * value from the X register into it.
2345 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2348 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2352 * Now allocate a register to hold that value and store
2355 s2 = new_stmt(BPF_ST);
2356 s2->s.k = reg_off_ll;
2360 * Now move it into the X register.
2362 s2 = new_stmt(BPF_MISC|BPF_TAX);
2371 * Load a value relative to the beginning of the link-layer header after the 802.11
2372 * header, i.e. LLC_SNAP.
2373 * The link-layer header doesn't necessarily begin at the beginning
2374 * of the packet data; there might be a variable-length prefix containing
2375 * radio information.
2377 static struct slist *
2378 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2381 struct slist *sjset_data_frame_1;
2382 struct slist *sjset_data_frame_2;
2383 struct slist *sjset_qos;
2384 struct slist *sjset_radiotap_flags;
2385 struct slist *sjset_radiotap_tsft;
2386 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2387 struct slist *s_roundup;
2389 if (reg_off_macpl == -1) {
2391 * No register has been assigned to the offset of
2392 * the MAC-layer payload, which means nobody needs
2393 * it; don't bother computing it - just return
2394 * what we already have.
2400 * This code is not compatible with the optimizer, as
2401 * we are generating jmp instructions within a normal
2402 * slist of instructions
2407 * If "s" is non-null, it has code to arrange that the X register
2408 * contains the length of the prefix preceding the link-layer
2411 * Otherwise, the length of the prefix preceding the link-layer
2412 * header is "off_ll".
2416 * There is no variable-length header preceding the
2417 * link-layer header.
2419 * Load the length of the fixed-length prefix preceding
2420 * the link-layer header (if any) into the X register,
2421 * and store it in the reg_off_macpl register.
2422 * That length is off_ll.
2424 s = new_stmt(BPF_LDX|BPF_IMM);
2429 * The X register contains the offset of the beginning of the
2430 * link-layer header; add 24, which is the minimum length
2431 * of the MAC header for a data frame, to that, and store it
2432 * in reg_off_macpl, and then load the Frame Control field,
2433 * which is at the offset in the X register, with an indexed load.
2435 s2 = new_stmt(BPF_MISC|BPF_TXA);
2437 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2440 s2 = new_stmt(BPF_ST);
2441 s2->s.k = reg_off_macpl;
2444 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2449 * Check the Frame Control field to see if this is a data frame;
2450 * a data frame has the 0x08 bit (b3) in that field set and the
2451 * 0x04 bit (b2) clear.
2453 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2454 sjset_data_frame_1->s.k = 0x08;
2455 sappend(s, sjset_data_frame_1);
2458 * If b3 is set, test b2, otherwise go to the first statement of
2459 * the rest of the program.
2461 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2462 sjset_data_frame_2->s.k = 0x04;
2463 sappend(s, sjset_data_frame_2);
2464 sjset_data_frame_1->s.jf = snext;
2467 * If b2 is not set, this is a data frame; test the QoS bit.
2468 * Otherwise, go to the first statement of the rest of the
2471 sjset_data_frame_2->s.jt = snext;
2472 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2473 sjset_qos->s.k = 0x80; /* QoS bit */
2474 sappend(s, sjset_qos);
2477 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2479 * Otherwise, go to the first statement of the rest of the
2482 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2483 s2->s.k = reg_off_macpl;
2485 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2488 s2 = new_stmt(BPF_ST);
2489 s2->s.k = reg_off_macpl;
2493 * If we have a radiotap header, look at it to see whether
2494 * there's Atheros padding between the MAC-layer header
2497 * Note: all of the fields in the radiotap header are
2498 * little-endian, so we byte-swap all of the values
2499 * we test against, as they will be loaded as big-endian
2502 if (linktype == DLT_IEEE802_11_RADIO) {
2504 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2505 * in the presence flag?
2507 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2511 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2512 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2513 sappend(s, sjset_radiotap_flags);
2516 * If not, skip all of this.
2518 sjset_radiotap_flags->s.jf = snext;
2521 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2523 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2524 new_stmt(JMP(BPF_JSET));
2525 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2526 sappend(s, sjset_radiotap_tsft);
2529 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2530 * at an offset of 16 from the beginning of the raw packet
2531 * data (8 bytes for the radiotap header and 8 bytes for
2534 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2537 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2541 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2542 sjset_tsft_datapad->s.k = 0x20;
2543 sappend(s, sjset_tsft_datapad);
2546 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2547 * at an offset of 8 from the beginning of the raw packet
2548 * data (8 bytes for the radiotap header).
2550 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2553 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2557 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2558 sjset_notsft_datapad->s.k = 0x20;
2559 sappend(s, sjset_notsft_datapad);
2562 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2563 * set, round the length of the 802.11 header to
2564 * a multiple of 4. Do that by adding 3 and then
2565 * dividing by and multiplying by 4, which we do by
2568 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2569 s_roundup->s.k = reg_off_macpl;
2570 sappend(s, s_roundup);
2571 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2574 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2577 s2 = new_stmt(BPF_ST);
2578 s2->s.k = reg_off_macpl;
2581 sjset_tsft_datapad->s.jt = s_roundup;
2582 sjset_tsft_datapad->s.jf = snext;
2583 sjset_notsft_datapad->s.jt = s_roundup;
2584 sjset_notsft_datapad->s.jf = snext;
2586 sjset_qos->s.jf = snext;
2592 insert_compute_vloffsets(b)
2598 * For link-layer types that have a variable-length header
2599 * preceding the link-layer header, generate code to load
2600 * the offset of the link-layer header into the register
2601 * assigned to that offset, if any.
2605 case DLT_PRISM_HEADER:
2606 s = gen_load_prism_llprefixlen();
2609 case DLT_IEEE802_11_RADIO_AVS:
2610 s = gen_load_avs_llprefixlen();
2613 case DLT_IEEE802_11_RADIO:
2614 s = gen_load_radiotap_llprefixlen();
2618 s = gen_load_ppi_llprefixlen();
2627 * For link-layer types that have a variable-length link-layer
2628 * header, generate code to load the offset of the MAC-layer
2629 * payload into the register assigned to that offset, if any.
2633 case DLT_IEEE802_11:
2634 case DLT_PRISM_HEADER:
2635 case DLT_IEEE802_11_RADIO_AVS:
2636 case DLT_IEEE802_11_RADIO:
2638 s = gen_load_802_11_header_len(s, b->stmts);
2643 * If we have any offset-loading code, append all the
2644 * existing statements in the block to those statements,
2645 * and make the resulting list the list of statements
2649 sappend(s, b->stmts);
2654 static struct block *
2655 gen_ppi_dlt_check(void)
2657 struct slist *s_load_dlt;
2660 if (linktype == DLT_PPI)
2662 /* Create the statements that check for the DLT
2664 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2665 s_load_dlt->s.k = 4;
2667 b = new_block(JMP(BPF_JEQ));
2669 b->stmts = s_load_dlt;
2670 b->s.k = SWAPLONG(DLT_IEEE802_11);
2680 static struct slist *
2681 gen_prism_llprefixlen(void)
2685 if (reg_off_ll == -1) {
2687 * We haven't yet assigned a register for the length
2688 * of the radio header; allocate one.
2690 reg_off_ll = alloc_reg();
2694 * Load the register containing the radio length
2695 * into the X register.
2697 s = new_stmt(BPF_LDX|BPF_MEM);
2698 s->s.k = reg_off_ll;
2702 static struct slist *
2703 gen_avs_llprefixlen(void)
2707 if (reg_off_ll == -1) {
2709 * We haven't yet assigned a register for the length
2710 * of the AVS header; allocate one.
2712 reg_off_ll = alloc_reg();
2716 * Load the register containing the AVS length
2717 * into the X register.
2719 s = new_stmt(BPF_LDX|BPF_MEM);
2720 s->s.k = reg_off_ll;
2724 static struct slist *
2725 gen_radiotap_llprefixlen(void)
2729 if (reg_off_ll == -1) {
2731 * We haven't yet assigned a register for the length
2732 * of the radiotap header; allocate one.
2734 reg_off_ll = alloc_reg();
2738 * Load the register containing the radiotap length
2739 * into the X register.
2741 s = new_stmt(BPF_LDX|BPF_MEM);
2742 s->s.k = reg_off_ll;
2747 * At the moment we treat PPI as normal Radiotap encoded
2748 * packets. The difference is in the function that generates
2749 * the code at the beginning to compute the header length.
2750 * Since this code generator of PPI supports bare 802.11
2751 * encapsulation only (i.e. the encapsulated DLT should be
2752 * DLT_IEEE802_11) we generate code to check for this too.
2754 static struct slist *
2755 gen_ppi_llprefixlen(void)
2759 if (reg_off_ll == -1) {
2761 * We haven't yet assigned a register for the length
2762 * of the radiotap header; allocate one.
2764 reg_off_ll = alloc_reg();
2768 * Load the register containing the PPI length
2769 * into the X register.
2771 s = new_stmt(BPF_LDX|BPF_MEM);
2772 s->s.k = reg_off_ll;
2777 * Generate code to compute the link-layer header length, if necessary,
2778 * putting it into the X register, and to return either a pointer to a
2779 * "struct slist" for the list of statements in that code, or NULL if
2780 * no code is necessary.
2782 static struct slist *
2783 gen_llprefixlen(void)
2787 case DLT_PRISM_HEADER:
2788 return gen_prism_llprefixlen();
2790 case DLT_IEEE802_11_RADIO_AVS:
2791 return gen_avs_llprefixlen();
2793 case DLT_IEEE802_11_RADIO:
2794 return gen_radiotap_llprefixlen();
2797 return gen_ppi_llprefixlen();
2805 * Generate code to load the register containing the offset of the
2806 * MAC-layer payload into the X register; if no register for that offset
2807 * has been allocated, allocate it first.
2809 static struct slist *
2814 if (off_macpl_is_variable) {
2815 if (reg_off_macpl == -1) {
2817 * We haven't yet assigned a register for the offset
2818 * of the MAC-layer payload; allocate one.
2820 reg_off_macpl = alloc_reg();
2824 * Load the register containing the offset of the MAC-layer
2825 * payload into the X register.
2827 s = new_stmt(BPF_LDX|BPF_MEM);
2828 s->s.k = reg_off_macpl;
2832 * That offset isn't variable, so we don't need to
2833 * generate any code.
2840 * Map an Ethernet type to the equivalent PPP type.
2843 ethertype_to_ppptype(proto)
2852 case ETHERTYPE_IPV6:
2860 case ETHERTYPE_ATALK:
2874 * I'm assuming the "Bridging PDU"s that go
2875 * over PPP are Spanning Tree Protocol
2889 * Generate code to match a particular packet type by matching the
2890 * link-layer type field or fields in the 802.2 LLC header.
2892 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2893 * value, if <= ETHERMTU.
2895 static struct block *
2899 struct block *b0, *b1, *b2;
2901 /* are we checking MPLS-encapsulated packets? */
2902 if (label_stack_depth > 0) {
2906 /* FIXME add other L3 proto IDs */
2907 return gen_mpls_linktype(Q_IP);
2909 case ETHERTYPE_IPV6:
2911 /* FIXME add other L3 proto IDs */
2912 return gen_mpls_linktype(Q_IPV6);
2915 bpf_error("unsupported protocol over mpls");
2921 * Are we testing PPPoE packets?
2925 * The PPPoE session header is part of the
2926 * MAC-layer payload, so all references
2927 * should be relative to the beginning of
2932 * We use Ethernet protocol types inside libpcap;
2933 * map them to the corresponding PPP protocol types.
2935 proto = ethertype_to_ppptype(proto);
2936 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2942 case DLT_NETANALYZER:
2943 case DLT_NETANALYZER_TRANSPARENT:
2944 return gen_ether_linktype(proto);
2952 proto = (proto << 8 | LLCSAP_ISONS);
2956 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2963 case DLT_IEEE802_11:
2964 case DLT_PRISM_HEADER:
2965 case DLT_IEEE802_11_RADIO_AVS:
2966 case DLT_IEEE802_11_RADIO:
2969 * Check that we have a data frame.
2971 b0 = gen_check_802_11_data_frame();
2974 * Now check for the specified link-layer type.
2976 b1 = gen_llc_linktype(proto);
2984 * XXX - check for asynchronous frames, as per RFC 1103.
2986 return gen_llc_linktype(proto);
2992 * XXX - check for LLC PDUs, as per IEEE 802.5.
2994 return gen_llc_linktype(proto);
2998 case DLT_ATM_RFC1483:
3000 case DLT_IP_OVER_FC:
3001 return gen_llc_linktype(proto);
3007 * If "is_lane" is set, check for a LANE-encapsulated
3008 * version of this protocol, otherwise check for an
3009 * LLC-encapsulated version of this protocol.
3011 * We assume LANE means Ethernet, not Token Ring.
3015 * Check that the packet doesn't begin with an
3016 * LE Control marker. (We've already generated
3019 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3024 * Now generate an Ethernet test.
3026 b1 = gen_ether_linktype(proto);
3031 * Check for LLC encapsulation and then check the
3034 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3035 b1 = gen_llc_linktype(proto);
3043 return gen_linux_sll_linktype(proto);
3048 case DLT_SLIP_BSDOS:
3051 * These types don't provide any type field; packets
3052 * are always IPv4 or IPv6.
3054 * XXX - for IPv4, check for a version number of 4, and,
3055 * for IPv6, check for a version number of 6?
3060 /* Check for a version number of 4. */
3061 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3063 case ETHERTYPE_IPV6:
3064 /* Check for a version number of 6. */
3065 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3068 return gen_false(); /* always false */
3075 * Raw IPv4, so no type field.
3077 if (proto == ETHERTYPE_IP)
3078 return gen_true(); /* always true */
3080 /* Checking for something other than IPv4; always false */
3087 * Raw IPv6, so no type field.
3089 if (proto == ETHERTYPE_IPV6)
3090 return gen_true(); /* always true */
3092 /* Checking for something other than IPv6; always false */
3099 case DLT_PPP_SERIAL:
3102 * We use Ethernet protocol types inside libpcap;
3103 * map them to the corresponding PPP protocol types.
3105 proto = ethertype_to_ppptype(proto);
3106 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3112 * We use Ethernet protocol types inside libpcap;
3113 * map them to the corresponding PPP protocol types.
3119 * Also check for Van Jacobson-compressed IP.
3120 * XXX - do this for other forms of PPP?
3122 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3123 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3125 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3130 proto = ethertype_to_ppptype(proto);
3131 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3141 * For DLT_NULL, the link-layer header is a 32-bit
3142 * word containing an AF_ value in *host* byte order,
3143 * and for DLT_ENC, the link-layer header begins
3144 * with a 32-bit work containing an AF_ value in
3147 * In addition, if we're reading a saved capture file,
3148 * the host byte order in the capture may not be the
3149 * same as the host byte order on this machine.
3151 * For DLT_LOOP, the link-layer header is a 32-bit
3152 * word containing an AF_ value in *network* byte order.
3154 * XXX - AF_ values may, unfortunately, be platform-
3155 * dependent; for example, FreeBSD's AF_INET6 is 24
3156 * whilst NetBSD's and OpenBSD's is 26.
3158 * This means that, when reading a capture file, just
3159 * checking for our AF_INET6 value won't work if the
3160 * capture file came from another OS.
3169 case ETHERTYPE_IPV6:
3176 * Not a type on which we support filtering.
3177 * XXX - support those that have AF_ values
3178 * #defined on this platform, at least?
3183 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3185 * The AF_ value is in host byte order, but
3186 * the BPF interpreter will convert it to
3187 * network byte order.
3189 * If this is a save file, and it's from a
3190 * machine with the opposite byte order to
3191 * ours, we byte-swap the AF_ value.
3193 * Then we run it through "htonl()", and
3194 * generate code to compare against the result.
3196 if (bpf_pcap->sf.rfile != NULL &&
3197 bpf_pcap->sf.swapped)
3198 proto = SWAPLONG(proto);
3199 proto = htonl(proto);
3201 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3203 #ifdef HAVE_NET_PFVAR_H
3206 * af field is host byte order in contrast to the rest of
3209 if (proto == ETHERTYPE_IP)
3210 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3211 BPF_B, (bpf_int32)AF_INET));
3212 else if (proto == ETHERTYPE_IPV6)
3213 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3214 BPF_B, (bpf_int32)AF_INET6));
3219 #endif /* HAVE_NET_PFVAR_H */
3222 case DLT_ARCNET_LINUX:
3224 * XXX should we check for first fragment if the protocol
3232 case ETHERTYPE_IPV6:
3233 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3234 (bpf_int32)ARCTYPE_INET6));
3237 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3238 (bpf_int32)ARCTYPE_IP);
3239 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3240 (bpf_int32)ARCTYPE_IP_OLD);
3245 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3246 (bpf_int32)ARCTYPE_ARP);
3247 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3248 (bpf_int32)ARCTYPE_ARP_OLD);
3252 case ETHERTYPE_REVARP:
3253 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3254 (bpf_int32)ARCTYPE_REVARP));
3256 case ETHERTYPE_ATALK:
3257 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3258 (bpf_int32)ARCTYPE_ATALK));
3265 case ETHERTYPE_ATALK:
3275 * XXX - assumes a 2-byte Frame Relay header with
3276 * DLCI and flags. What if the address is longer?
3282 * Check for the special NLPID for IP.
3284 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3286 case ETHERTYPE_IPV6:
3288 * Check for the special NLPID for IPv6.
3290 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3294 * Check for several OSI protocols.
3296 * Frame Relay packets typically have an OSI
3297 * NLPID at the beginning; we check for each
3300 * What we check for is the NLPID and a frame
3301 * control field of UI, i.e. 0x03 followed
3304 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3305 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3306 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3318 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3320 case DLT_JUNIPER_MFR:
3321 case DLT_JUNIPER_MLFR:
3322 case DLT_JUNIPER_MLPPP:
3323 case DLT_JUNIPER_ATM1:
3324 case DLT_JUNIPER_ATM2:
3325 case DLT_JUNIPER_PPPOE:
3326 case DLT_JUNIPER_PPPOE_ATM:
3327 case DLT_JUNIPER_GGSN:
3328 case DLT_JUNIPER_ES:
3329 case DLT_JUNIPER_MONITOR:
3330 case DLT_JUNIPER_SERVICES:
3331 case DLT_JUNIPER_ETHER:
3332 case DLT_JUNIPER_PPP:
3333 case DLT_JUNIPER_FRELAY:
3334 case DLT_JUNIPER_CHDLC:
3335 case DLT_JUNIPER_VP:
3336 case DLT_JUNIPER_ST:
3337 case DLT_JUNIPER_ISM:
3338 case DLT_JUNIPER_VS:
3339 case DLT_JUNIPER_SRX_E2E:
3340 case DLT_JUNIPER_FIBRECHANNEL:
3341 case DLT_JUNIPER_ATM_CEMIC:
3343 /* just lets verify the magic number for now -
3344 * on ATM we may have up to 6 different encapsulations on the wire
3345 * and need a lot of heuristics to figure out that the payload
3348 * FIXME encapsulation specific BPF_ filters
3350 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3353 return gen_ipnet_linktype(proto);
3355 case DLT_LINUX_IRDA:
3356 bpf_error("IrDA link-layer type filtering not implemented");
3359 bpf_error("DOCSIS link-layer type filtering not implemented");
3362 case DLT_MTP2_WITH_PHDR:
3363 bpf_error("MTP2 link-layer type filtering not implemented");
3366 bpf_error("ERF link-layer type filtering not implemented");
3369 bpf_error("PFSYNC link-layer type filtering not implemented");
3371 case DLT_LINUX_LAPD:
3372 bpf_error("LAPD link-layer type filtering not implemented");
3376 case DLT_USB_LINUX_MMAPPED:
3377 bpf_error("USB link-layer type filtering not implemented");
3379 case DLT_BLUETOOTH_HCI_H4:
3380 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3381 bpf_error("Bluetooth link-layer type filtering not implemented");
3384 case DLT_CAN_SOCKETCAN:
3385 bpf_error("CAN link-layer type filtering not implemented");
3387 case DLT_IEEE802_15_4:
3388 case DLT_IEEE802_15_4_LINUX:
3389 case DLT_IEEE802_15_4_NONASK_PHY:
3390 case DLT_IEEE802_15_4_NOFCS:
3391 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3393 case DLT_IEEE802_16_MAC_CPS_RADIO:
3394 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3397 bpf_error("SITA link-layer type filtering not implemented");
3400 bpf_error("RAIF1 link-layer type filtering not implemented");
3403 bpf_error("IPMB link-layer type filtering not implemented");
3406 bpf_error("AX.25 link-layer type filtering not implemented");
3410 * All the types that have no encapsulation should either be
3411 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3412 * all packets are IP packets, or should be handled in some
3413 * special case, if none of them are (if some are and some
3414 * aren't, the lack of encapsulation is a problem, as we'd
3415 * have to find some other way of determining the packet type).
3417 * Therefore, if "off_linktype" is -1, there's an error.
3419 if (off_linktype == (u_int)-1)
3423 * Any type not handled above should always have an Ethernet
3424 * type at an offset of "off_linktype".
3426 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3430 * Check for an LLC SNAP packet with a given organization code and
3431 * protocol type; we check the entire contents of the 802.2 LLC and
3432 * snap headers, checking for DSAP and SSAP of SNAP and a control
3433 * field of 0x03 in the LLC header, and for the specified organization
3434 * code and protocol type in the SNAP header.
3436 static struct block *
3437 gen_snap(orgcode, ptype)
3438 bpf_u_int32 orgcode;
3441 u_char snapblock[8];
3443 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3444 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3445 snapblock[2] = 0x03; /* control = UI */
3446 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3447 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3448 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3449 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3450 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3451 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3455 * Generate code to match a particular packet type, for link-layer types
3456 * using 802.2 LLC headers.
3458 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3459 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3461 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3462 * value, if <= ETHERMTU. We use that to determine whether to
3463 * match the DSAP or both DSAP and LSAP or to check the OUI and
3464 * protocol ID in a SNAP header.
3466 static struct block *
3467 gen_llc_linktype(proto)
3471 * XXX - handle token-ring variable-length header.
3477 case LLCSAP_NETBEUI:
3479 * XXX - should we check both the DSAP and the
3480 * SSAP, like this, or should we check just the
3481 * DSAP, as we do for other types <= ETHERMTU
3482 * (i.e., other SAP values)?
3484 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3485 ((proto << 8) | proto));
3489 * XXX - are there ever SNAP frames for IPX on
3490 * non-Ethernet 802.x networks?
3492 return gen_cmp(OR_MACPL, 0, BPF_B,
3493 (bpf_int32)LLCSAP_IPX);
3495 case ETHERTYPE_ATALK:
3497 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3498 * SNAP packets with an organization code of
3499 * 0x080007 (Apple, for Appletalk) and a protocol
3500 * type of ETHERTYPE_ATALK (Appletalk).
3502 * XXX - check for an organization code of
3503 * encapsulated Ethernet as well?
3505 return gen_snap(0x080007, ETHERTYPE_ATALK);
3509 * XXX - we don't have to check for IPX 802.3
3510 * here, but should we check for the IPX Ethertype?
3512 if (proto <= ETHERMTU) {
3514 * This is an LLC SAP value, so check
3517 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3520 * This is an Ethernet type; we assume that it's
3521 * unlikely that it'll appear in the right place
3522 * at random, and therefore check only the
3523 * location that would hold the Ethernet type
3524 * in a SNAP frame with an organization code of
3525 * 0x000000 (encapsulated Ethernet).
3527 * XXX - if we were to check for the SNAP DSAP and
3528 * LSAP, as per XXX, and were also to check for an
3529 * organization code of 0x000000 (encapsulated
3530 * Ethernet), we'd do
3532 * return gen_snap(0x000000, proto);
3534 * here; for now, we don't, as per the above.
3535 * I don't know whether it's worth the extra CPU
3536 * time to do the right check or not.
3538 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3543 static struct block *
3544 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3548 u_int src_off, dst_off;
3550 struct block *b0, *b1;
3564 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3565 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3571 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3572 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3579 b0 = gen_linktype(proto);
3580 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3586 static struct block *
3587 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3588 struct in6_addr *addr;
3589 struct in6_addr *mask;
3591 u_int src_off, dst_off;
3593 struct block *b0, *b1;
3608 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3609 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3615 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3616 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3623 /* this order is important */
3624 a = (u_int32_t *)addr;
3625 m = (u_int32_t *)mask;
3626 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3627 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3629 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3631 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3633 b0 = gen_linktype(proto);
3639 static struct block *
3640 gen_ehostop(eaddr, dir)
3641 register const u_char *eaddr;
3644 register struct block *b0, *b1;
3648 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3651 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3654 b0 = gen_ehostop(eaddr, Q_SRC);
3655 b1 = gen_ehostop(eaddr, Q_DST);
3661 b0 = gen_ehostop(eaddr, Q_SRC);
3662 b1 = gen_ehostop(eaddr, Q_DST);
3667 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3671 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3675 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3679 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3683 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3687 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3695 * Like gen_ehostop, but for DLT_FDDI
3697 static struct block *
3698 gen_fhostop(eaddr, dir)
3699 register const u_char *eaddr;
3702 struct block *b0, *b1;
3707 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3709 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3714 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3716 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3720 b0 = gen_fhostop(eaddr, Q_SRC);
3721 b1 = gen_fhostop(eaddr, Q_DST);
3727 b0 = gen_fhostop(eaddr, Q_SRC);
3728 b1 = gen_fhostop(eaddr, Q_DST);
3733 bpf_error("'addr1' is only supported on 802.11");
3737 bpf_error("'addr2' is only supported on 802.11");
3741 bpf_error("'addr3' is only supported on 802.11");
3745 bpf_error("'addr4' is only supported on 802.11");
3749 bpf_error("'ra' is only supported on 802.11");
3753 bpf_error("'ta' is only supported on 802.11");
3761 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3763 static struct block *
3764 gen_thostop(eaddr, dir)
3765 register const u_char *eaddr;
3768 register struct block *b0, *b1;
3772 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3775 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3778 b0 = gen_thostop(eaddr, Q_SRC);
3779 b1 = gen_thostop(eaddr, Q_DST);
3785 b0 = gen_thostop(eaddr, Q_SRC);
3786 b1 = gen_thostop(eaddr, Q_DST);
3791 bpf_error("'addr1' is only supported on 802.11");
3795 bpf_error("'addr2' is only supported on 802.11");
3799 bpf_error("'addr3' is only supported on 802.11");
3803 bpf_error("'addr4' is only supported on 802.11");
3807 bpf_error("'ra' is only supported on 802.11");
3811 bpf_error("'ta' is only supported on 802.11");
3819 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3820 * various 802.11 + radio headers.
3822 static struct block *
3823 gen_wlanhostop(eaddr, dir)
3824 register const u_char *eaddr;
3827 register struct block *b0, *b1, *b2;
3828 register struct slist *s;
3830 #ifdef ENABLE_WLAN_FILTERING_PATCH
3833 * We need to disable the optimizer because the optimizer is buggy
3834 * and wipes out some LD instructions generated by the below
3835 * code to validate the Frame Control bits
3838 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3845 * For control frames, there is no SA.
3847 * For management frames, SA is at an
3848 * offset of 10 from the beginning of
3851 * For data frames, SA is at an offset
3852 * of 10 from the beginning of the packet
3853 * if From DS is clear, at an offset of
3854 * 16 from the beginning of the packet
3855 * if From DS is set and To DS is clear,
3856 * and an offset of 24 from the beginning
3857 * of the packet if From DS is set and To DS
3862 * Generate the tests to be done for data frames
3865 * First, check for To DS set, i.e. check "link[1] & 0x01".
3867 s = gen_load_a(OR_LINK, 1, BPF_B);
3868 b1 = new_block(JMP(BPF_JSET));
3869 b1->s.k = 0x01; /* To DS */
3873 * If To DS is set, the SA is at 24.
3875 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3879 * Now, check for To DS not set, i.e. check
3880 * "!(link[1] & 0x01)".
3882 s = gen_load_a(OR_LINK, 1, BPF_B);
3883 b2 = new_block(JMP(BPF_JSET));
3884 b2->s.k = 0x01; /* To DS */
3889 * If To DS is not set, the SA is at 16.