Import libpcap-1.3.0.
[dragonfly.git] / contrib / libpcap / gencode.c
CommitLineData
23fa89e6
PA
1/*#define CHASE_CHAIN*/
2/*
3 * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998
4 * The Regents of the University of California. All rights reserved.
5 *
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
17 * written permission.
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.
21 */
22#ifndef lint
23static const char rcsid[] _U_ =
a85e14b0 24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
23fa89e6
PA
25#endif
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#ifdef WIN32
32#include <pcap-stdinc.h>
33#else /* WIN32 */
a85e14b0
PA
34#if HAVE_INTTYPES_H
35#include <inttypes.h>
36#elif HAVE_STDINT_H
37#include <stdint.h>
38#endif
39#ifdef HAVE_SYS_BITYPES_H
40#include <sys/bitypes.h>
41#endif
23fa89e6
PA
42#include <sys/types.h>
43#include <sys/socket.h>
44#endif /* WIN32 */
45
46/*
47 * XXX - why was this included even on UNIX?
48 */
49#ifdef __MINGW32__
a85e14b0 50#include "ip6_misc.h"
23fa89e6
PA
51#endif
52
53#ifndef WIN32
54
55#ifdef __NetBSD__
56#include <sys/param.h>
57#endif
58
59#include <netinet/in.h>
a85e14b0 60#include <arpa/inet.h>
23fa89e6
PA
61
62#endif /* WIN32 */
63
64#include <stdlib.h>
65#include <string.h>
66#include <memory.h>
67#include <setjmp.h>
68#include <stdarg.h>
69
70#ifdef MSDOS
71#include "pcap-dos.h"
72#endif
73
74#include "pcap-int.h"
75
76#include "ethertype.h"
77#include "nlpid.h"
78#include "llc.h"
79#include "gencode.h"
de0d3203 80#include "ieee80211.h"
23fa89e6
PA
81#include "atmuni31.h"
82#include "sunatmpos.h"
83#include "ppp.h"
de0d3203 84#include "pcap/sll.h"
a85e14b0 85#include "pcap/ipnet.h"
23fa89e6 86#include "arcnet.h"
0e1eae1f
PA
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>
91#endif
0cb9e74f
PA
92#ifdef HAVE_NET_PFVAR_H
93#include <sys/socket.h>
94#include <net/if.h>
95#include <net/pfvar.h>
96#include <net/if_pflog.h>
97#endif
23fa89e6
PA
98#ifndef offsetof
99#define offsetof(s, e) ((size_t)&((s *)0)->e)
100#endif
101#ifdef INET6
102#ifndef WIN32
103#include <netdb.h> /* for "struct addrinfo" */
104#endif /* WIN32 */
105#endif /*INET6*/
de0d3203 106#include <pcap/namedb.h>
23fa89e6
PA
107
108#define ETHERMTU 1500
109
110#ifndef IPPROTO_SCTP
111#define IPPROTO_SCTP 132
112#endif
113
114#ifdef HAVE_OS_PROTO_H
115#include "os-proto.h"
116#endif
117
118#define JMP(c) ((c)|BPF_JMP|BPF_K)
119
120/* Locals */
121static jmp_buf top_ctx;
122static pcap_t *bpf_pcap;
123
124/* Hack for updating VLAN, MPLS, and PPPoE offsets. */
de0d3203 125#ifdef WIN32
dfcad8d8
PA
126static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
127#else
23fa89e6 128static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
dfcad8d8 129#endif
23fa89e6
PA
130
131/* XXX */
132#ifdef PCAP_FDDIPAD
133static int pcap_fddipad;
134#endif
135
136/* VARARGS */
137void
138bpf_error(const char *fmt, ...)
139{
140 va_list ap;
141
142 va_start(ap, fmt);
143 if (bpf_pcap != NULL)
144 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
145 fmt, ap);
146 va_end(ap);
147 longjmp(top_ctx, 1);
148 /* NOTREACHED */
149}
150
151static void init_linktype(pcap_t *);
152
de0d3203 153static void init_regs(void);
23fa89e6
PA
154static int alloc_reg(void);
155static void free_reg(int);
156
157static struct block *root;
158
159/*
160 * Value passed to gen_load_a() to indicate what the offset argument
161 * is relative to.
162 */
163enum e_offrel {
164 OR_PACKET, /* relative to the beginning of the packet */
de0d3203
PA
165 OR_LINK, /* relative to the beginning of the link-layer header */
166 OR_MACPL, /* relative to the end of the MAC-layer header */
23fa89e6
PA
167 OR_NET, /* relative to the network-layer header */
168 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
169 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
170 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
171};
172
a85e14b0
PA
173#ifdef INET6
174/*
175 * As errors are handled by a longjmp, anything allocated must be freed
176 * in the longjmp handler, so it must be reachable from that handler.
177 * One thing that's allocated is the result of pcap_nametoaddrinfo();
178 * it must be freed with freeaddrinfo(). This variable points to any
179 * addrinfo structure that would need to be freed.
180 */
181static struct addrinfo *ai;
182#endif
183
23fa89e6
PA
184/*
185 * We divy out chunks of memory rather than call malloc each time so
186 * we don't have to worry about leaking memory. It's probably
187 * not a big deal if all this memory was wasted but if this ever
188 * goes into a library that would probably not be a good idea.
189 *
190 * XXX - this *is* in a library....
191 */
192#define NCHUNKS 16
193#define CHUNK0SIZE 1024
194struct chunk {
195 u_int n_left;
196 void *m;
197};
198
199static struct chunk chunks[NCHUNKS];
200static int cur_chunk;
201
202static void *newchunk(u_int);
203static void freechunks(void);
204static inline struct block *new_block(int);
205static inline struct slist *new_stmt(int);
206static struct block *gen_retblk(int);
207static inline void syntax(void);
208
209static void backpatch(struct block *, struct block *);
210static void merge(struct block *, struct block *);
211static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
212static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
213static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
214static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
215static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
216static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
217 bpf_u_int32);
218static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
219static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
220 bpf_u_int32, bpf_u_int32, int, bpf_int32);
221static struct slist *gen_load_llrel(u_int, u_int);
de0d3203 222static struct slist *gen_load_macplrel(u_int, u_int);
23fa89e6
PA
223static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
224static struct slist *gen_loadx_iphdrlen(void);
225static struct block *gen_uncond(int);
226static inline struct block *gen_true(void);
227static inline struct block *gen_false(void);
228static struct block *gen_ether_linktype(int);
a85e14b0 229static struct block *gen_ipnet_linktype(int);
23fa89e6 230static struct block *gen_linux_sll_linktype(int);
de0d3203
PA
231static struct slist *gen_load_prism_llprefixlen(void);
232static struct slist *gen_load_avs_llprefixlen(void);
233static struct slist *gen_load_radiotap_llprefixlen(void);
234static struct slist *gen_load_ppi_llprefixlen(void);
235static void insert_compute_vloffsets(struct block *);
23fa89e6 236static struct slist *gen_llprefixlen(void);
de0d3203
PA
237static struct slist *gen_off_macpl(void);
238static int ethertype_to_ppptype(int);
23fa89e6 239static struct block *gen_linktype(int);
de0d3203 240static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
23fa89e6
PA
241static struct block *gen_llc_linktype(int);
242static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
243#ifdef INET6
244static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
245#endif
246static struct block *gen_ahostop(const u_char *, int);
247static struct block *gen_ehostop(const u_char *, int);
248static struct block *gen_fhostop(const u_char *, int);
249static struct block *gen_thostop(const u_char *, int);
250static struct block *gen_wlanhostop(const u_char *, int);
251static struct block *gen_ipfchostop(const u_char *, int);
252static struct block *gen_dnhostop(bpf_u_int32, int);
253static struct block *gen_mpls_linktype(int);
254static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
255#ifdef INET6
256static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
257#endif
258#ifndef INET6
259static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
260#endif
261static struct block *gen_ipfrag(void);
262static struct block *gen_portatom(int, bpf_int32);
263static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
264#ifdef INET6
265static struct block *gen_portatom6(int, bpf_int32);
266static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
267#endif
268struct block *gen_portop(int, int, int);
269static struct block *gen_port(int, int, int);
270struct block *gen_portrangeop(int, int, int, int);
271static struct block *gen_portrange(int, int, int, int);
272#ifdef INET6
273struct block *gen_portop6(int, int, int);
274static struct block *gen_port6(int, int, int);
275struct block *gen_portrangeop6(int, int, int, int);
276static struct block *gen_portrange6(int, int, int, int);
277#endif
278static int lookup_proto(const char *, int);
279static struct block *gen_protochain(int, int, int);
280static struct block *gen_proto(int, int, int);
281static struct slist *xfer_to_x(struct arth *);
282static struct slist *xfer_to_a(struct arth *);
283static struct block *gen_mac_multicast(int);
284static struct block *gen_len(int, int);
de0d3203 285static struct block *gen_check_802_11_data_frame(void);
23fa89e6 286
dfcad8d8 287static struct block *gen_ppi_dlt_check(void);
23fa89e6
PA
288static struct block *gen_msg_abbrev(int type);
289
290static void *
291newchunk(n)
292 u_int n;
293{
294 struct chunk *cp;
295 int k;
296 size_t size;
297
298#ifndef __NetBSD__
299 /* XXX Round up to nearest long. */
300 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
301#else
302 /* XXX Round up to structure boundary. */
303 n = ALIGN(n);
304#endif
305
306 cp = &chunks[cur_chunk];
307 if (n > cp->n_left) {
308 ++cp, k = ++cur_chunk;
309 if (k >= NCHUNKS)
310 bpf_error("out of memory");
311 size = CHUNK0SIZE << k;
312 cp->m = (void *)malloc(size);
313 if (cp->m == NULL)
314 bpf_error("out of memory");
315 memset((char *)cp->m, 0, size);
316 cp->n_left = size;
317 if (n > size)
318 bpf_error("out of memory");
319 }
320 cp->n_left -= n;
321 return (void *)((char *)cp->m + cp->n_left);
322}
323
324static void
325freechunks()
326{
327 int i;
328
329 cur_chunk = 0;
330 for (i = 0; i < NCHUNKS; ++i)
331 if (chunks[i].m != NULL) {
332 free(chunks[i].m);
333 chunks[i].m = NULL;
334 }
335}
336
337/*
338 * A strdup whose allocations are freed after code generation is over.
339 */
340char *
341sdup(s)
342 register const char *s;
343{
344 int n = strlen(s) + 1;
345 char *cp = newchunk(n);
346
347 strlcpy(cp, s, n);
348 return (cp);
349}
350
351static inline struct block *
352new_block(code)
353 int code;
354{
355 struct block *p;
356
357 p = (struct block *)newchunk(sizeof(*p));
358 p->s.code = code;
359 p->head = p;
360
361 return p;
362}
363
364static inline struct slist *
365new_stmt(code)
366 int code;
367{
368 struct slist *p;
369
370 p = (struct slist *)newchunk(sizeof(*p));
371 p->s.code = code;
372
373 return p;
374}
375
376static struct block *
377gen_retblk(v)
378 int v;
379{
380 struct block *b = new_block(BPF_RET|BPF_K);
381
382 b->s.k = v;
383 return b;
384}
385
386static inline void
387syntax()
388{
389 bpf_error("syntax error in filter expression");
390}
391
392static bpf_u_int32 netmask;
393static int snaplen;
394int no_optimize;
a85e14b0
PA
395#ifdef WIN32
396static int
397pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
398 const char *buf, int optimize, bpf_u_int32 mask);
23fa89e6
PA
399
400int
401pcap_compile(pcap_t *p, struct bpf_program *program,
dfcad8d8 402 const char *buf, int optimize, bpf_u_int32 mask)
a85e14b0
PA
403{
404 int result;
405
406 EnterCriticalSection(&g_PcapCompileCriticalSection);
407
408 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
409
410 LeaveCriticalSection(&g_PcapCompileCriticalSection);
411
412 return result;
413}
414
415static int
416pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
417 const char *buf, int optimize, bpf_u_int32 mask)
418#else /* WIN32 */
419int
420pcap_compile(pcap_t *p, struct bpf_program *program,
421 const char *buf, int optimize, bpf_u_int32 mask)
422#endif /* WIN32 */
23fa89e6
PA
423{
424 extern int n_errors;
dfcad8d8 425 const char * volatile xbuf = buf;
0e1eae1f 426 u_int len;
23fa89e6
PA
427
428 no_optimize = 0;
429 n_errors = 0;
430 root = NULL;
431 bpf_pcap = p;
de0d3203 432 init_regs();
23fa89e6 433 if (setjmp(top_ctx)) {
a85e14b0
PA
434#ifdef INET6
435 if (ai != NULL) {
436 freeaddrinfo(ai);
437 ai = NULL;
438 }
439#endif
23fa89e6
PA
440 lex_cleanup();
441 freechunks();
442 return (-1);
443 }
444
445 netmask = mask;
446
447 snaplen = pcap_snapshot(p);
448 if (snaplen == 0) {
449 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
450 "snaplen of 0 rejects all packets");
451 return -1;
452 }
453
dfcad8d8 454 lex_init(xbuf ? xbuf : "");
23fa89e6
PA
455 init_linktype(p);
456 (void)pcap_parse();
457
458 if (n_errors)
459 syntax();
460
461 if (root == NULL)
462 root = gen_retblk(snaplen);
463
464 if (optimize && !no_optimize) {
465 bpf_optimize(&root);
466 if (root == NULL ||
467 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
468 bpf_error("expression rejects all packets");
469 }
470 program->bf_insns = icode_to_fcode(root, &len);
471 program->bf_len = len;
472
473 lex_cleanup();
474 freechunks();
475 return (0);
476}
477
478/*
479 * entry point for using the compiler with no pcap open
480 * pass in all the stuff that is needed explicitly instead.
481 */
482int
483pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
484 struct bpf_program *program,
dfcad8d8 485 const char *buf, int optimize, bpf_u_int32 mask)
23fa89e6
PA
486{
487 pcap_t *p;
488 int ret;
489
490 p = pcap_open_dead(linktype_arg, snaplen_arg);
491 if (p == NULL)
492 return (-1);
493 ret = pcap_compile(p, program, buf, optimize, mask);
494 pcap_close(p);
495 return (ret);
496}
497
498/*
499 * Clean up a "struct bpf_program" by freeing all the memory allocated
500 * in it.
501 */
502void
503pcap_freecode(struct bpf_program *program)
504{
505 program->bf_len = 0;
506 if (program->bf_insns != NULL) {
507 free((char *)program->bf_insns);
508 program->bf_insns = NULL;
509 }
510}
511
512/*
513 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
514 * which of the jt and jf fields has been resolved and which is a pointer
515 * back to another unresolved block (or nil). At least one of the fields
516 * in each block is already resolved.
517 */
518static void
519backpatch(list, target)
520 struct block *list, *target;
521{
522 struct block *next;
523
524 while (list) {
525 if (!list->sense) {
526 next = JT(list);
527 JT(list) = target;
528 } else {
529 next = JF(list);
530 JF(list) = target;
531 }
532 list = next;
533 }
534}
535
536/*
537 * Merge the lists in b0 and b1, using the 'sense' field to indicate
538 * which of jt and jf is the link.
539 */
540static void
541merge(b0, b1)
542 struct block *b0, *b1;
543{
544 register struct block **p = &b0;
545
546 /* Find end of list. */
547 while (*p)
548 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
549
550 /* Concatenate the lists. */
551 *p = b1;
552}
553
554void
555finish_parse(p)
556 struct block *p;
557{
dfcad8d8 558 struct block *ppi_dlt_check;
23fa89e6
PA
559
560 /*
561 * Insert before the statements of the first (root) block any
562 * statements needed to load the lengths of any variable-length
563 * headers into registers.
564 *
565 * XXX - a fancier strategy would be to insert those before the
566 * statements of all blocks that use those lengths and that
567 * have no predecessors that use them, so that we only compute
568 * the lengths if we need them. There might be even better
de0d3203
PA
569 * approaches than that.
570 *
571 * However, those strategies would be more complicated, and
572 * as we don't generate code to compute a length if the
573 * program has no tests that use the length, and as most
574 * tests will probably use those lengths, we would just
575 * postpone computing the lengths so that it's not done
576 * for tests that fail early, and it's not clear that's
577 * worth the effort.
23fa89e6 578 */
de0d3203
PA
579 insert_compute_vloffsets(p->head);
580
581 /*
582 * For DLT_PPI captures, generate a check of the per-packet
583 * DLT value to make sure it's DLT_IEEE802_11.
584 */
585 ppi_dlt_check = gen_ppi_dlt_check();
586 if (ppi_dlt_check != NULL)
587 gen_and(ppi_dlt_check, p);
dfcad8d8 588
de0d3203
PA
589 backpatch(p, gen_retblk(snaplen));
590 p->sense = !p->sense;
591 backpatch(p, gen_retblk(0));
592 root = p->head;
23fa89e6
PA
593}
594
595void
596gen_and(b0, b1)
597 struct block *b0, *b1;
598{
599 backpatch(b0, b1->head);
600 b0->sense = !b0->sense;
601 b1->sense = !b1->sense;
602 merge(b1, b0);
603 b1->sense = !b1->sense;
604 b1->head = b0->head;
605}
606
607void
608gen_or(b0, b1)
609 struct block *b0, *b1;
610{
611 b0->sense = !b0->sense;
612 backpatch(b0, b1->head);
613 b0->sense = !b0->sense;
614 merge(b1, b0);
615 b1->head = b0->head;
616}
617
618void
619gen_not(b)
620 struct block *b;
621{
622 b->sense = !b->sense;
623}
624
625static struct block *
626gen_cmp(offrel, offset, size, v)
627 enum e_offrel offrel;
628 u_int offset, size;
629 bpf_int32 v;
630{
631 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
632}
633
634static struct block *
635gen_cmp_gt(offrel, offset, size, v)
636 enum e_offrel offrel;
637 u_int offset, size;
638 bpf_int32 v;
639{
640 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
641}
642
643static struct block *
644gen_cmp_ge(offrel, offset, size, v)
645 enum e_offrel offrel;
646 u_int offset, size;
647 bpf_int32 v;
648{
649 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
650}
651
652static struct block *
653gen_cmp_lt(offrel, offset, size, v)
654 enum e_offrel offrel;
655 u_int offset, size;
656 bpf_int32 v;
657{
658 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
659}
660
661static struct block *
662gen_cmp_le(offrel, offset, size, v)
663 enum e_offrel offrel;
664 u_int offset, size;
665 bpf_int32 v;
666{
667 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
668}
669
670static struct block *
671gen_mcmp(offrel, offset, size, v, mask)
672 enum e_offrel offrel;
673 u_int offset, size;
674 bpf_int32 v;
675 bpf_u_int32 mask;
676{
677 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
678}
679
680static struct block *
681gen_bcmp(offrel, offset, size, v)
682 enum e_offrel offrel;
683 register u_int offset, size;
684 register const u_char *v;
685{
686 register struct block *b, *tmp;
687
688 b = NULL;
689 while (size >= 4) {
690 register const u_char *p = &v[size - 4];
691 bpf_int32 w = ((bpf_int32)p[0] << 24) |
692 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
693
694 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
695 if (b != NULL)
696 gen_and(b, tmp);
697 b = tmp;
698 size -= 4;
699 }
700 while (size >= 2) {
701 register const u_char *p = &v[size - 2];
702 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
703
704 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
705 if (b != NULL)
706 gen_and(b, tmp);
707 b = tmp;
708 size -= 2;
709 }
710 if (size > 0) {
711 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
712 if (b != NULL)
713 gen_and(b, tmp);
714 b = tmp;
715 }
716 return b;
717}
718
719/*
720 * AND the field of size "size" at offset "offset" relative to the header
721 * specified by "offrel" with "mask", and compare it with the value "v"
722 * with the test specified by "jtype"; if "reverse" is true, the test
723 * should test the opposite of "jtype".
724 */
725static struct block *
726gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
727 enum e_offrel offrel;
728 bpf_int32 v;
729 bpf_u_int32 offset, size, mask, jtype;
730 int reverse;
731{
732 struct slist *s, *s2;
733 struct block *b;
734
735 s = gen_load_a(offrel, offset, size);
736
737 if (mask != 0xffffffff) {
738 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
739 s2->s.k = mask;
740 sappend(s, s2);
741 }
742
743 b = new_block(JMP(jtype));
744 b->stmts = s;
745 b->s.k = v;
746 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
747 gen_not(b);
748 return b;
749}
750
751/*
752 * Various code constructs need to know the layout of the data link
753 * layer. These variables give the necessary offsets from the beginning
754 * of the packet data.
23fa89e6 755 */
23fa89e6
PA
756
757/*
758 * This is the offset of the beginning of the link-layer header from
759 * the beginning of the raw packet data.
760 *
761 * It's usually 0, except for 802.11 with a fixed-length radio header.
762 * (For 802.11 with a variable-length radio header, we have to generate
763 * code to compute that offset; off_ll is 0 in that case.)
764 */
765static u_int off_ll;
766
767/*
de0d3203
PA
768 * If there's a variable-length header preceding the link-layer header,
769 * "reg_off_ll" is the register number for a register containing the
770 * length of that header, and therefore the offset of the link-layer
771 * header from the beginning of the raw packet data. Otherwise,
772 * "reg_off_ll" is -1.
773 */
774static int reg_off_ll;
775
776/*
777 * This is the offset of the beginning of the MAC-layer header from
778 * the beginning of the link-layer header.
23fa89e6 779 * It's usually 0, except for ATM LANE, where it's the offset, relative
a85e14b0
PA
780 * to the beginning of the raw packet data, of the Ethernet header, and
781 * for Ethernet with various additional information.
23fa89e6
PA
782 */
783static u_int off_mac;
784
de0d3203
PA
785/*
786 * This is the offset of the beginning of the MAC-layer payload,
787 * from the beginning of the raw packet data.
788 *
789 * I.e., it's the sum of the length of the link-layer header (without,
790 * for example, any 802.2 LLC header, so it's the MAC-layer
791 * portion of that header), plus any prefix preceding the
792 * link-layer header.
793 */
794static u_int off_macpl;
795
796/*
797 * This is 1 if the offset of the beginning of the MAC-layer payload
798 * from the beginning of the link-layer header is variable-length.
799 */
800static int off_macpl_is_variable;
801
802/*
803 * If the link layer has variable_length headers, "reg_off_macpl"
804 * is the register number for a register containing the length of the
805 * link-layer header plus the length of any variable-length header
806 * preceding the link-layer header. Otherwise, "reg_off_macpl"
807 * is -1.
808 */
809static int reg_off_macpl;
810
23fa89e6
PA
811/*
812 * "off_linktype" is the offset to information in the link-layer header
813 * giving the packet type. This offset is relative to the beginning
814 * of the link-layer header (i.e., it doesn't include off_ll).
815 *
816 * For Ethernet, it's the offset of the Ethernet type field.
817 *
818 * For link-layer types that always use 802.2 headers, it's the
819 * offset of the LLC header.
820 *
821 * For PPP, it's the offset of the PPP type field.
822 *
823 * For Cisco HDLC, it's the offset of the CHDLC type field.
824 *
825 * For BSD loopback, it's the offset of the AF_ value.
826 *
827 * For Linux cooked sockets, it's the offset of the type field.
828 *
829 * It's set to -1 for no encapsulation, in which case, IP is assumed.
830 */
831static u_int off_linktype;
832
de0d3203
PA
833/*
834 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
835 * checks to check the PPP header, assumed to follow a LAN-style link-
836 * layer header and a PPPoE session header.
837 */
838static int is_pppoes = 0;
839
23fa89e6
PA
840/*
841 * TRUE if the link layer includes an ATM pseudo-header.
842 */
843static int is_atm = 0;
844
845/*
846 * TRUE if "lane" appeared in the filter; it causes us to generate
847 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
848 */
849static int is_lane = 0;
850
851/*
852 * These are offsets for the ATM pseudo-header.
853 */
854static u_int off_vpi;
855static u_int off_vci;
856static u_int off_proto;
857
dfcad8d8
PA
858/*
859 * These are offsets for the MTP2 fields.
860 */
861static u_int off_li;
862
23fa89e6
PA
863/*
864 * These are offsets for the MTP3 fields.
865 */
866static u_int off_sio;
867static u_int off_opc;
868static u_int off_dpc;
869static u_int off_sls;
870
871/*
872 * This is the offset of the first byte after the ATM pseudo_header,
873 * or -1 if there is no ATM pseudo-header.
874 */
875static u_int off_payload;
876
877/*
878 * These are offsets to the beginning of the network-layer header.
de0d3203
PA
879 * They are relative to the beginning of the MAC-layer payload (i.e.,
880 * they don't include off_ll or off_macpl).
23fa89e6
PA
881 *
882 * If the link layer never uses 802.2 LLC:
883 *
884 * "off_nl" and "off_nl_nosnap" are the same.
885 *
886 * If the link layer always uses 802.2 LLC:
887 *
888 * "off_nl" is the offset if there's a SNAP header following
889 * the 802.2 header;
890 *
891 * "off_nl_nosnap" is the offset if there's no SNAP header.
892 *
893 * If the link layer is Ethernet:
894 *
895 * "off_nl" is the offset if the packet is an Ethernet II packet
896 * (we assume no 802.3+802.2+SNAP);
897 *
898 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
899 * with an 802.2 header following it.
900 */
901static u_int off_nl;
902static u_int off_nl_nosnap;
903
904static int linktype;
905
906static void
907init_linktype(p)
908 pcap_t *p;
909{
910 linktype = pcap_datalink(p);
911#ifdef PCAP_FDDIPAD
912 pcap_fddipad = p->fddipad;
913#endif
914
915 /*
916 * Assume it's not raw ATM with a pseudo-header, for now.
917 */
918 off_mac = 0;
919 is_atm = 0;
920 is_lane = 0;
921 off_vpi = -1;
922 off_vci = -1;
923 off_proto = -1;
924 off_payload = -1;
925
de0d3203
PA
926 /*
927 * And that we're not doing PPPoE.
928 */
929 is_pppoes = 0;
930
23fa89e6
PA
931 /*
932 * And assume we're not doing SS7.
933 */
dfcad8d8 934 off_li = -1;
23fa89e6
PA
935 off_sio = -1;
936 off_opc = -1;
937 off_dpc = -1;
938 off_sls = -1;
939
940 /*
de0d3203 941 * Also assume it's not 802.11.
23fa89e6
PA
942 */
943 off_ll = 0;
de0d3203
PA
944 off_macpl = 0;
945 off_macpl_is_variable = 0;
23fa89e6
PA
946
947 orig_linktype = -1;
948 orig_nl = -1;
949 label_stack_depth = 0;
950
de0d3203
PA
951 reg_off_ll = -1;
952 reg_off_macpl = -1;
23fa89e6
PA
953
954 switch (linktype) {
955
956 case DLT_ARCNET:
957 off_linktype = 2;
de0d3203
PA
958 off_macpl = 6;
959 off_nl = 0; /* XXX in reality, variable! */
960 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
961 return;
962
963 case DLT_ARCNET_LINUX:
964 off_linktype = 4;
de0d3203
PA
965 off_macpl = 8;
966 off_nl = 0; /* XXX in reality, variable! */
967 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
968 return;
969
970 case DLT_EN10MB:
971 off_linktype = 12;
de0d3203
PA
972 off_macpl = 14; /* Ethernet header length */
973 off_nl = 0; /* Ethernet II */
974 off_nl_nosnap = 3; /* 802.3+802.2 */
23fa89e6
PA
975 return;
976
977 case DLT_SLIP:
978 /*
979 * SLIP doesn't have a link level type. The 16 byte
980 * header is hacked into our SLIP driver.
981 */
982 off_linktype = -1;
de0d3203
PA
983 off_macpl = 16;
984 off_nl = 0;
985 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
986 return;
987
988 case DLT_SLIP_BSDOS:
989 /* XXX this may be the same as the DLT_PPP_BSDOS case */
990 off_linktype = -1;
991 /* XXX end */
de0d3203
PA
992 off_macpl = 24;
993 off_nl = 0;
994 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
995 return;
996
997 case DLT_NULL:
998 case DLT_LOOP:
999 off_linktype = 0;
de0d3203
PA
1000 off_macpl = 4;
1001 off_nl = 0;
1002 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1003 return;
1004
1005 case DLT_ENC:
1006 off_linktype = 0;
de0d3203
PA
1007 off_macpl = 12;
1008 off_nl = 0;
1009 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1010 return;
1011
1012 case DLT_PPP:
1013 case DLT_PPP_PPPD:
1014 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1015 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1016 off_linktype = 2;
de0d3203
PA
1017 off_macpl = 4;
1018 off_nl = 0;
1019 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1020 return;
1021
1022 case DLT_PPP_ETHER:
1023 /*
1024 * This does no include the Ethernet header, and
1025 * only covers session state.
1026 */
1027 off_linktype = 6;
de0d3203
PA
1028 off_macpl = 8;
1029 off_nl = 0;
1030 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1031 return;
1032
1033 case DLT_PPP_BSDOS:
1034 off_linktype = 5;
de0d3203
PA
1035 off_macpl = 24;
1036 off_nl = 0;
1037 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1038 return;
1039
1040 case DLT_FDDI:
1041 /*
1042 * FDDI doesn't really have a link-level type field.
1043 * We set "off_linktype" to the offset of the LLC header.
1044 *
1045 * To check for Ethernet types, we assume that SSAP = SNAP
1046 * is being used and pick out the encapsulated Ethernet type.
1047 * XXX - should we generate code to check for SNAP?
1048 */
1049 off_linktype = 13;
1050#ifdef PCAP_FDDIPAD
1051 off_linktype += pcap_fddipad;
1052#endif
de0d3203 1053 off_macpl = 13; /* FDDI MAC header length */
23fa89e6 1054#ifdef PCAP_FDDIPAD
de0d3203 1055 off_macpl += pcap_fddipad;
23fa89e6 1056#endif
de0d3203
PA
1057 off_nl = 8; /* 802.2+SNAP */
1058 off_nl_nosnap = 3; /* 802.2 */
23fa89e6
PA
1059 return;
1060
1061 case DLT_IEEE802:
1062 /*
1063 * Token Ring doesn't really have a link-level type field.
1064 * We set "off_linktype" to the offset of the LLC header.
1065 *
1066 * To check for Ethernet types, we assume that SSAP = SNAP
1067 * is being used and pick out the encapsulated Ethernet type.
1068 * XXX - should we generate code to check for SNAP?
1069 *
1070 * XXX - the header is actually variable-length.
1071 * Some various Linux patched versions gave 38
1072 * as "off_linktype" and 40 as "off_nl"; however,
1073 * if a token ring packet has *no* routing
1074 * information, i.e. is not source-routed, the correct
1075 * values are 20 and 22, as they are in the vanilla code.
1076 *
1077 * A packet is source-routed iff the uppermost bit
1078 * of the first byte of the source address, at an
1079 * offset of 8, has the uppermost bit set. If the
1080 * packet is source-routed, the total number of bytes
1081 * of routing information is 2 plus bits 0x1F00 of
1082 * the 16-bit value at an offset of 14 (shifted right
1083 * 8 - figure out which byte that is).
1084 */
1085 off_linktype = 14;
de0d3203
PA
1086 off_macpl = 14; /* Token Ring MAC header length */
1087 off_nl = 8; /* 802.2+SNAP */
1088 off_nl_nosnap = 3; /* 802.2 */
23fa89e6
PA
1089 return;
1090
1091 case DLT_IEEE802_11:
de0d3203
PA
1092 case DLT_PRISM_HEADER:
1093 case DLT_IEEE802_11_RADIO_AVS:
1094 case DLT_IEEE802_11_RADIO:
23fa89e6
PA
1095 /*
1096 * 802.11 doesn't really have a link-level type field.
1097 * We set "off_linktype" to the offset of the LLC header.
1098 *
1099 * To check for Ethernet types, we assume that SSAP = SNAP
1100 * is being used and pick out the encapsulated Ethernet type.
1101 * XXX - should we generate code to check for SNAP?
1102 *
de0d3203
PA
1103 * We also handle variable-length radio headers here.
1104 * The Prism header is in theory variable-length, but in
1105 * practice it's always 144 bytes long. However, some
1106 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1107 * sometimes or always supply an AVS header, so we
1108 * have to check whether the radio header is a Prism
1109 * header or an AVS header, so, in practice, it's
1110 * variable-length.
23fa89e6 1111 */
23fa89e6 1112 off_linktype = 24;
de0d3203
PA
1113 off_macpl = 0; /* link-layer header is variable-length */
1114 off_macpl_is_variable = 1;
1115 off_nl = 8; /* 802.2+SNAP */
1116 off_nl_nosnap = 3; /* 802.2 */
23fa89e6
PA
1117 return;
1118
dfcad8d8 1119 case DLT_PPI:
de0d3203
PA
1120 /*
1121 * At the moment we treat PPI the same way that we treat
1122 * normal Radiotap encoded packets. The difference is in
1123 * the function that generates the code at the beginning
1124 * to compute the header length. Since this code generator
1125 * of PPI supports bare 802.11 encapsulation only (i.e.
1126 * the encapsulated DLT should be DLT_IEEE802_11) we
1127 * generate code to check for this too.
23fa89e6
PA
1128 */
1129 off_linktype = 24;
de0d3203
PA
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 */
23fa89e6
PA
1134 return;
1135
1136 case DLT_ATM_RFC1483:
1137 case DLT_ATM_CLIP: /* Linux ATM defines this */
1138 /*
1139 * assume routed, non-ISO PDUs
1140 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1141 *
1142 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1143 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1144 * latter would presumably be treated the way PPPoE
1145 * should be, so you can do "pppoe and udp port 2049"
1146 * or "pppoa and tcp port 80" and have it check for
1147 * PPPo{A,E} and a PPP protocol of IP and....
1148 */
1149 off_linktype = 0;
de0d3203 1150 off_macpl = 0; /* packet begins with LLC header */
23fa89e6
PA
1151 off_nl = 8; /* 802.2+SNAP */
1152 off_nl_nosnap = 3; /* 802.2 */
1153 return;
1154
1155 case DLT_SUNATM:
1156 /*
1157 * Full Frontal ATM; you get AALn PDUs with an ATM
1158 * pseudo-header.
1159 */
1160 is_atm = 1;
1161 off_vpi = SUNATM_VPI_POS;
1162 off_vci = SUNATM_VCI_POS;
1163 off_proto = PROTO_POS;
de0d3203 1164 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
23fa89e6
PA
1165 off_payload = SUNATM_PKT_BEGIN_POS;
1166 off_linktype = off_payload;
de0d3203
PA
1167 off_macpl = off_payload; /* if LLC-encapsulated */
1168 off_nl = 8; /* 802.2+SNAP */
1169 off_nl_nosnap = 3; /* 802.2 */
23fa89e6
PA
1170 return;
1171
1172 case DLT_RAW:
a85e14b0
PA
1173 case DLT_IPV4:
1174 case DLT_IPV6:
23fa89e6 1175 off_linktype = -1;
de0d3203 1176 off_macpl = 0;
23fa89e6
PA
1177 off_nl = 0;
1178 off_nl_nosnap = 0; /* no 802.2 LLC */
1179 return;
1180
1181 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1182 off_linktype = 14;
de0d3203
PA
1183 off_macpl = 16;
1184 off_nl = 0;
1185 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1186 return;
1187
1188 case DLT_LTALK:
1189 /*
1190 * LocalTalk does have a 1-byte type field in the LLAP header,
1191 * but really it just indicates whether there is a "short" or
1192 * "long" DDP packet following.
1193 */
1194 off_linktype = -1;
de0d3203 1195 off_macpl = 0;
23fa89e6
PA
1196 off_nl = 0;
1197 off_nl_nosnap = 0; /* no 802.2 LLC */
1198 return;
1199
1200 case DLT_IP_OVER_FC:
1201 /*
1202 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1203 * link-level type field. We set "off_linktype" to the
1204 * offset of the LLC header.
1205 *
1206 * To check for Ethernet types, we assume that SSAP = SNAP
1207 * is being used and pick out the encapsulated Ethernet type.
1208 * XXX - should we generate code to check for SNAP? RFC
1209 * 2625 says SNAP should be used.
1210 */
1211 off_linktype = 16;
de0d3203
PA
1212 off_macpl = 16;
1213 off_nl = 8; /* 802.2+SNAP */
1214 off_nl_nosnap = 3; /* 802.2 */
23fa89e6
PA
1215 return;
1216
1217 case DLT_FRELAY:
1218 /*
1219 * XXX - we should set this to handle SNAP-encapsulated
1220 * frames (NLPID of 0x80).
1221 */
1222 off_linktype = -1;
de0d3203 1223 off_macpl = 0;
23fa89e6
PA
1224 off_nl = 0;
1225 off_nl_nosnap = 0; /* no 802.2 LLC */
1226 return;
1227
1228 /*
1229 * the only BPF-interesting FRF.16 frames are non-control frames;
1230 * Frame Relay has a variable length link-layer
1231 * so lets start with offset 4 for now and increments later on (FIXME);
1232 */
1233 case DLT_MFR:
1234 off_linktype = -1;
de0d3203 1235 off_macpl = 0;
23fa89e6
PA
1236 off_nl = 4;
1237 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1238 return;
1239
1240 case DLT_APPLE_IP_OVER_IEEE1394:
1241 off_linktype = 16;
de0d3203
PA
1242 off_macpl = 18;
1243 off_nl = 0;
1244 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6
PA
1245 return;
1246
23fa89e6
PA
1247 case DLT_SYMANTEC_FIREWALL:
1248 off_linktype = 6;
de0d3203
PA
1249 off_macpl = 44;
1250 off_nl = 0; /* Ethernet II */
1251 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
23fa89e6
PA
1252 return;
1253
0cb9e74f 1254#ifdef HAVE_NET_PFVAR_H
23fa89e6
PA
1255 case DLT_PFLOG:
1256 off_linktype = 0;
de0d3203
PA
1257 off_macpl = PFLOG_HDRLEN;
1258 off_nl = 0;
1259 off_nl_nosnap = 0; /* no 802.2 LLC */
23fa89e6 1260 return;
0cb9e74f 1261#endif
23fa89e6
PA
1262
1263 case DLT_JUNIPER_MFR:
1264 case DLT_JUNIPER_MLFR:
1265 case DLT_JUNIPER_MLPPP:
1266 case DLT_JUNIPER_PPP:
1267 case DLT_JUNIPER_CHDLC:
1268 case DLT_JUNIPER_FRELAY:
1269 off_linktype = 4;
de0d3203
PA
1270 off_macpl = 4;
1271 off_nl = 0;
23fa89e6
PA
1272 off_nl_nosnap = -1; /* no 802.2 LLC */
1273 return;
1274
1275 case DLT_JUNIPER_ATM1:
de0d3203
PA
1276 off_linktype = 4; /* in reality variable between 4-8 */
1277 off_macpl = 4; /* in reality variable between 4-8 */
1278 off_nl = 0;
1279 off_nl_nosnap = 10;
23fa89e6
PA
1280 return;
1281
1282 case DLT_JUNIPER_ATM2:
de0d3203
PA
1283 off_linktype = 8; /* in reality variable between 8-12 */
1284 off_macpl = 8; /* in reality variable between 8-12 */
1285 off_nl = 0;
1286 off_nl_nosnap = 10;
23fa89e6
PA
1287 return;
1288
1289 /* frames captured on a Juniper PPPoE service PIC
1290 * contain raw ethernet frames */
1291 case DLT_JUNIPER_PPPOE:
1292 case DLT_JUNIPER_ETHER:
de0d3203 1293 off_macpl = 14;
23fa89e6
PA
1294 off_linktype = 16;
1295 off_nl = 18; /* Ethernet II */
1296 off_nl_nosnap = 21; /* 802.3+802.2 */
1297 return;
1298
1299 case DLT_JUNIPER_PPPOE_ATM:
1300 off_linktype = 4;
de0d3203
PA
1301 off_macpl = 6;
1302 off_nl = 0;
1303 off_nl_nosnap = -1; /* no 802.2 LLC */
23fa89e6
PA
1304 return;
1305
1306 case DLT_JUNIPER_GGSN:
1307 off_linktype = 6;
de0d3203
PA
1308 off_macpl = 12;
1309 off_nl = 0;
1310 off_nl_nosnap = -1; /* no 802.2 LLC */
23fa89e6
PA
1311 return;
1312
1313 case DLT_JUNIPER_ES:
1314 off_linktype = 6;
de0d3203
PA
1315 off_macpl = -1; /* not really a network layer but raw IP addresses */
1316 off_nl = -1; /* not really a network layer but raw IP addresses */
23fa89e6
PA
1317 off_nl_nosnap = -1; /* no 802.2 LLC */
1318 return;
1319
1320 case DLT_JUNIPER_MONITOR:
1321 off_linktype = 12;
de0d3203
PA
1322 off_macpl = 12;
1323 off_nl = 0; /* raw IP/IP6 header */
23fa89e6
PA
1324 off_nl_nosnap = -1; /* no 802.2 LLC */
1325 return;
1326
1327 case DLT_JUNIPER_SERVICES:
1328 off_linktype = 12;
de0d3203 1329 off_macpl = -1; /* L3 proto location dep. on cookie type */
23fa89e6
PA
1330 off_nl = -1; /* L3 proto location dep. on cookie type */
1331 off_nl_nosnap = -1; /* no 802.2 LLC */
1332 return;
1333
dfcad8d8
PA
1334 case DLT_JUNIPER_VP:
1335 off_linktype = 18;
de0d3203
PA
1336 off_macpl = -1;
1337 off_nl = -1;
1338 off_nl_nosnap = -1;
1339 return;
1340
1341 case DLT_JUNIPER_ST:
1342 off_linktype = 18;
1343 off_macpl = -1;
1344 off_nl = -1;
1345 off_nl_nosnap = -1;
1346 return;
1347
1348 case DLT_JUNIPER_ISM:
1349 off_linktype = 8;
1350 off_macpl = -1;
dfcad8d8
PA
1351 off_nl = -1;
1352 off_nl_nosnap = -1;
1353 return;
1354
a85e14b0
PA
1355 case DLT_JUNIPER_VS:
1356 case DLT_JUNIPER_SRX_E2E:
1357 case DLT_JUNIPER_FIBRECHANNEL:
1358 case DLT_JUNIPER_ATM_CEMIC:
1359 off_linktype = 8;
1360 off_macpl = -1;
1361 off_nl = -1;
1362 off_nl_nosnap = -1;
1363 return;
1364
23fa89e6 1365 case DLT_MTP2:
dfcad8d8 1366 off_li = 2;
23fa89e6
PA
1367 off_sio = 3;
1368 off_opc = 4;
1369 off_dpc = 4;
1370 off_sls = 7;
1371 off_linktype = -1;
de0d3203 1372 off_macpl = -1;
23fa89e6
PA
1373 off_nl = -1;
1374 off_nl_nosnap = -1;
1375 return;
1376
dfcad8d8
PA
1377 case DLT_MTP2_WITH_PHDR:
1378 off_li = 6;
1379 off_sio = 7;
1380 off_opc = 8;
1381 off_dpc = 8;
1382 off_sls = 11;
1383 off_linktype = -1;
de0d3203
PA
1384 off_macpl = -1;
1385 off_nl = -1;
1386 off_nl_nosnap = -1;
1387 return;
1388
1389 case DLT_ERF:
1390 off_li = 22;
1391 off_sio = 23;
1392 off_opc = 24;
1393 off_dpc = 24;
1394 off_sls = 27;
1395 off_linktype = -1;
1396 off_macpl = -1;
dfcad8d8
PA
1397 off_nl = -1;
1398 off_nl_nosnap = -1;
1399 return;
1400
23fa89e6
PA
1401 case DLT_PFSYNC:
1402 off_linktype = -1;
de0d3203
PA
1403 off_macpl = 4;
1404 off_nl = 0;
1405 off_nl_nosnap = 0;
23fa89e6 1406 return;
23fa89e6 1407
a85e14b0 1408 case DLT_AX25_KISS:
de0d3203
PA
1409 /*
1410 * Currently, only raw "link[N:M]" filtering is supported.
1411 */
a85e14b0 1412 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
de0d3203 1413 off_macpl = -1;
a85e14b0
PA
1414 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1415 off_nl_nosnap = -1; /* no 802.2 LLC */
1416 off_mac = 1; /* step over the kiss length byte */
de0d3203
PA
1417 return;
1418
a85e14b0
PA
1419 case DLT_IPNET:
1420 off_linktype = 1;
1421 off_macpl = 24; /* ipnet header length */
1422 off_nl = 0;
de0d3203
PA
1423 off_nl_nosnap = -1;
1424 return;
1425
a85e14b0
PA
1426 case DLT_NETANALYZER:
1427 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1428 off_linktype = 16; /* includes 4-byte pseudo-header */
1429 off_macpl = 18; /* pseudo-header+Ethernet header length */
1430 off_nl = 0; /* Ethernet II */
1431 off_nl_nosnap = 3; /* 802.3+802.2 */
de0d3203
PA
1432 return;
1433
a85e14b0
PA
1434 case DLT_NETANALYZER_TRANSPARENT:
1435 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1436 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1437 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1438 off_nl = 0; /* Ethernet II */
1439 off_nl_nosnap = 3; /* 802.3+802.2 */
de0d3203
PA
1440 return;
1441
a85e14b0 1442 default:
de0d3203 1443 /*
a85e14b0
PA
1444 * For values in the range in which we've assigned new
1445 * DLT_ values, only raw "link[N:M]" filtering is supported.
de0d3203 1446 */
a85e14b0
PA
1447 if (linktype >= DLT_MATCHING_MIN &&
1448 linktype <= DLT_MATCHING_MAX) {
1449 off_linktype = -1;
1450 off_macpl = -1;
1451 off_nl = -1;
1452 off_nl_nosnap = -1;
1453 return;
1454 }
de0d3203 1455
23fa89e6
PA
1456 }
1457 bpf_error("unknown data link type %d", linktype);
1458 /* NOTREACHED */
1459}
1460
1461/*
1462 * Load a value relative to the beginning of the link-layer header.
1463 * The link-layer header doesn't necessarily begin at the beginning
1464 * of the packet data; there might be a variable-length prefix containing
1465 * radio information.
1466 */
1467static struct slist *
1468gen_load_llrel(offset, size)
1469 u_int offset, size;
1470{
1471 struct slist *s, *s2;
1472
1473 s = gen_llprefixlen();
1474
1475 /*
1476 * If "s" is non-null, it has code to arrange that the X register
1477 * contains the length of the prefix preceding the link-layer
1478 * header.
1479 *
1480 * Otherwise, the length of the prefix preceding the link-layer
1481 * header is "off_ll".
1482 */
1483 if (s != NULL) {
1484 /*
1485 * There's a variable-length prefix preceding the
1486 * link-layer header. "s" points to a list of statements
1487 * that put the length of that prefix into the X register.
1488 * do an indirect load, to use the X register as an offset.
1489 */
1490 s2 = new_stmt(BPF_LD|BPF_IND|size);
1491 s2->s.k = offset;
1492 sappend(s, s2);
1493 } else {
1494 /*
1495 * There is no variable-length header preceding the
1496 * link-layer header; add in off_ll, which, if there's
1497 * a fixed-length header preceding the link-layer header,
1498 * is the length of that header.
1499 */
1500 s = new_stmt(BPF_LD|BPF_ABS|size);
1501 s->s.k = offset + off_ll;
1502 }
1503 return s;
1504}
1505
de0d3203
PA
1506/*
1507 * Load a value relative to the beginning of the MAC-layer payload.
1508 */
1509static struct slist *
1510gen_load_macplrel(offset, size)
1511 u_int offset, size;
1512{
1513 struct slist *s, *s2;
1514
1515 s = gen_off_macpl();
1516
1517 /*
1518 * If s is non-null, the offset of the MAC-layer payload is
1519 * variable, and s points to a list of instructions that
1520 * arrange that the X register contains that offset.
1521 *
1522 * Otherwise, the offset of the MAC-layer payload is constant,
1523 * and is in off_macpl.
1524 */
1525 if (s != NULL) {
1526 /*
1527 * The offset of the MAC-layer payload is in the X
1528 * register. Do an indirect load, to use the X register
1529 * as an offset.
1530 */
1531 s2 = new_stmt(BPF_LD|BPF_IND|size);
1532 s2->s.k = offset;
1533 sappend(s, s2);
1534 } else {
1535 /*
1536 * The offset of the MAC-layer payload is constant,
1537 * and is in off_macpl; load the value at that offset
1538 * plus the specified offset.
1539 */
1540 s = new_stmt(BPF_LD|BPF_ABS|size);
1541 s->s.k = off_macpl + offset;
1542 }
1543 return s;
1544}
dfcad8d8 1545
23fa89e6
PA
1546/*
1547 * Load a value relative to the beginning of the specified header.
1548 */
1549static struct slist *
1550gen_load_a(offrel, offset, size)
1551 enum e_offrel offrel;
1552 u_int offset, size;
1553{
1554 struct slist *s, *s2;
1555
1556 switch (offrel) {
1557
1558 case OR_PACKET:
1559 s = new_stmt(BPF_LD|BPF_ABS|size);
1560 s->s.k = offset;
1561 break;
1562
1563 case OR_LINK:
1564 s = gen_load_llrel(offset, size);
1565 break;
1566
de0d3203
PA
1567 case OR_MACPL:
1568 s = gen_load_macplrel(offset, size);
1569 break;
1570
23fa89e6 1571 case OR_NET:
de0d3203 1572 s = gen_load_macplrel(off_nl + offset, size);
23fa89e6
PA
1573 break;
1574
1575 case OR_NET_NOSNAP:
de0d3203 1576 s = gen_load_macplrel(off_nl_nosnap + offset, size);
23fa89e6
PA
1577 break;
1578
1579 case OR_TRAN_IPV4:
1580 /*
1581 * Load the X register with the length of the IPv4 header
1582 * (plus the offset of the link-layer header, if it's
1583 * preceded by a variable-length header such as a radio
1584 * header), in bytes.
1585 */
1586 s = gen_loadx_iphdrlen();
1587
1588 /*
de0d3203
PA
1589 * Load the item at {offset of the MAC-layer payload} +
1590 * {offset, relative to the start of the MAC-layer
1591 * paylod, of the IPv4 header} + {length of the IPv4 header} +
23fa89e6
PA
1592 * {specified offset}.
1593 *
de0d3203
PA
1594 * (If the offset of the MAC-layer payload is variable,
1595 * it's included in the value in the X register, and
1596 * off_macpl is 0.)
23fa89e6
PA
1597 */
1598 s2 = new_stmt(BPF_LD|BPF_IND|size);
de0d3203 1599 s2->s.k = off_macpl + off_nl + offset;
23fa89e6
PA
1600 sappend(s, s2);
1601 break;
1602
1603 case OR_TRAN_IPV6:
de0d3203 1604 s = gen_load_macplrel(off_nl + 40 + offset, size);
23fa89e6
PA
1605 break;
1606
1607 default:
1608 abort();
1609 return NULL;
1610 }
1611 return s;
1612}
1613
1614/*
1615 * Generate code to load into the X register the sum of the length of
1616 * the IPv4 header and any variable-length header preceding the link-layer
1617 * header.
1618 */
1619static struct slist *
1620gen_loadx_iphdrlen()
1621{
1622 struct slist *s, *s2;
1623
de0d3203 1624 s = gen_off_macpl();
23fa89e6
PA
1625 if (s != NULL) {
1626 /*
1627 * There's a variable-length prefix preceding the
de0d3203
PA
1628 * link-layer header, or the link-layer header is itself
1629 * variable-length. "s" points to a list of statements
1630 * that put the offset of the MAC-layer payload into
1631 * the X register.
1632 *
23fa89e6
PA
1633 * The 4*([k]&0xf) addressing mode can't be used, as we
1634 * don't have a constant offset, so we have to load the
1635 * value in question into the A register and add to it
1636 * the value from the X register.
1637 */
1638 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1639 s2->s.k = off_nl;
1640 sappend(s, s2);
1641 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1642 s2->s.k = 0xf;
1643 sappend(s, s2);
1644 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1645 s2->s.k = 2;
1646 sappend(s, s2);
1647
1648 /*
1649 * The A register now contains the length of the
de0d3203
PA
1650 * IP header. We need to add to it the offset of
1651 * the MAC-layer payload, which is still in the X
1652 * register, and move the result into the X register.
23fa89e6
PA
1653 */
1654 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1655 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1656 } else {
1657 /*
1658 * There is no variable-length header preceding the
de0d3203
PA
1659 * link-layer header, and the link-layer header is
1660 * fixed-length; load the length of the IPv4 header,
1661 * which is at an offset of off_nl from the beginning
1662 * of the MAC-layer payload, and thus at an offset
1663 * of off_mac_pl + off_nl from the beginning of the
1664 * raw packet data.
23fa89e6
PA
1665 */
1666 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
de0d3203 1667 s->s.k = off_macpl + off_nl;
23fa89e6
PA
1668 }
1669 return s;
1670}
1671
1672static struct block *
1673gen_uncond(rsense)
1674 int rsense;
1675{
1676 struct block *b;
1677 struct slist *s;
1678
1679 s = new_stmt(BPF_LD|BPF_IMM);
1680 s->s.k = !rsense;
1681 b = new_block(JMP(BPF_JEQ));
1682 b->stmts = s;
1683
1684 return b;
1685}
1686
1687static inline struct block *
1688gen_true()
1689{
1690 return gen_uncond(1);
1691}
1692
1693static inline struct block *
1694gen_false()
1695{
1696 return gen_uncond(0);
1697}
1698
1699/*
1700 * Byte-swap a 32-bit number.
1701 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1702 * big-endian platforms.)
1703 */
1704#define SWAPLONG(y) \
1705((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1706
1707/*
1708 * Generate code to match a particular packet type.
1709 *
1710 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1711 * value, if <= ETHERMTU. We use that to determine whether to
1712 * match the type/length field or to check the type/length field for
1713 * a value <= ETHERMTU to see whether it's a type field and then do
1714 * the appropriate test.
1715 */
1716static struct block *
1717gen_ether_linktype(proto)
1718 register int proto;
1719{
1720 struct block *b0, *b1;
1721
1722 switch (proto) {
1723
1724 case LLCSAP_ISONS:
1725 case LLCSAP_IP:
1726 case LLCSAP_NETBEUI:
1727 /*
1728 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1729 * so we check the DSAP and SSAP.
1730 *
1731 * LLCSAP_IP checks for IP-over-802.2, rather
1732 * than IP-over-Ethernet or IP-over-SNAP.
1733 *
1734 * XXX - should we check both the DSAP and the
1735 * SSAP, like this, or should we check just the
1736 * DSAP, as we do for other types <= ETHERMTU
1737 * (i.e., other SAP values)?
1738 */
1739 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1740 gen_not(b0);
de0d3203 1741 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
23fa89e6
PA
1742 ((proto << 8) | proto));
1743 gen_and(b0, b1);
1744 return b1;
1745
1746 case LLCSAP_IPX:
1747 /*
1748 * Check for;
1749 *
1750 * Ethernet_II frames, which are Ethernet
1751 * frames with a frame type of ETHERTYPE_IPX;
1752 *
1753 * Ethernet_802.3 frames, which are 802.3
1754 * frames (i.e., the type/length field is
1755 * a length field, <= ETHERMTU, rather than
1756 * a type field) with the first two bytes
1757 * after the Ethernet/802.3 header being
1758 * 0xFFFF;
1759 *
1760 * Ethernet_802.2 frames, which are 802.3
1761 * frames with an 802.2 LLC header and
1762 * with the IPX LSAP as the DSAP in the LLC
1763 * header;
1764 *
1765 * Ethernet_SNAP frames, which are 802.3
1766 * frames with an LLC header and a SNAP
1767 * header and with an OUI of 0x000000
1768 * (encapsulated Ethernet) and a protocol
1769 * ID of ETHERTYPE_IPX in the SNAP header.
1770 *
1771 * XXX - should we generate the same code both
1772 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1773 */
1774
1775 /*
1776 * This generates code to check both for the
1777 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1778 */
de0d3203
PA
1779 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1780 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
23fa89e6
PA
1781 gen_or(b0, b1);
1782
1783 /*
1784 * Now we add code to check for SNAP frames with
1785 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1786 */
de0d3203 1787 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
23fa89e6
PA
1788 gen_or(b0, b1);
1789
1790 /*
1791 * Now we generate code to check for 802.3
1792 * frames in general.
1793 */
1794 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1795 gen_not(b0);
1796
1797 /*
1798 * Now add the check for 802.3 frames before the
1799 * check for Ethernet_802.2 and Ethernet_802.3,
1800 * as those checks should only be done on 802.3
1801 * frames, not on Ethernet frames.
1802 */
1803 gen_and(b0, b1);
1804
1805 /*
1806 * Now add the check for Ethernet_II frames, and
1807 * do that before checking for the other frame
1808 * types.
1809 */
1810 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1811 (bpf_int32)ETHERTYPE_IPX);
1812 gen_or(b0, b1);
1813 return b1;
1814
1815 case ETHERTYPE_ATALK:
1816 case ETHERTYPE_AARP:
1817 /*
1818 * EtherTalk (AppleTalk protocols on Ethernet link
1819 * layer) may use 802.2 encapsulation.
1820 */
1821
1822 /*
1823 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1824 * we check for an Ethernet type field less than
1825 * 1500, which means it's an 802.3 length field.
1826 */
1827 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1828 gen_not(b0);
1829
1830 /*
1831 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1832 * SNAP packets with an organization code of
1833 * 0x080007 (Apple, for Appletalk) and a protocol
1834 * type of ETHERTYPE_ATALK (Appletalk).
1835 *
1836 * 802.2-encapsulated ETHERTYPE_AARP packets are
1837 * SNAP packets with an organization code of
1838 * 0x000000 (encapsulated Ethernet) and a protocol
1839 * type of ETHERTYPE_AARP (Appletalk ARP).
1840 */
1841 if (proto == ETHERTYPE_ATALK)
de0d3203 1842 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
23fa89e6 1843 else /* proto == ETHERTYPE_AARP */
de0d3203 1844 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
23fa89e6
PA
1845 gen_and(b0, b1);
1846
1847 /*
1848 * Check for Ethernet encapsulation (Ethertalk
1849 * phase 1?); we just check for the Ethernet
1850 * protocol type.
1851 */
1852 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1853
1854 gen_or(b0, b1);
1855 return b1;
1856
1857 default:
1858 if (proto <= ETHERMTU) {
1859 /*
1860 * This is an LLC SAP value, so the frames
1861 * that match would be 802.2 frames.
1862 * Check that the frame is an 802.2 frame
1863 * (i.e., that the length/type field is
1864 * a length field, <= ETHERMTU) and
1865 * then check the DSAP.
1866 */
1867 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1868 gen_not(b0);
1869 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1870 (bpf_int32)proto);
1871 gen_and(b0, b1);
1872 return b1;
1873 } else {
1874 /*
1875 * This is an Ethernet type, so compare
1876 * the length/type field with it (if
1877 * the frame is an 802.2 frame, the length
1878 * field will be <= ETHERMTU, and, as
1879 * "proto" is > ETHERMTU, this test
1880 * will fail and the frame won't match,
1881 * which is what we want).
1882 */
1883 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1884 (bpf_int32)proto);
1885 }
1886 }
1887}
1888
a85e14b0
PA
1889/*
1890 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1891 * or IPv6 then we have an error.
1892 */
1893static struct block *
1894gen_ipnet_linktype(proto)
1895 register int proto;
1896{
1897 switch (proto) {
1898
1899 case ETHERTYPE_IP:
1900 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1901 (bpf_int32)IPH_AF_INET);
1902 /* NOTREACHED */
1903
1904 case ETHERTYPE_IPV6:
1905 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1906 (bpf_int32)IPH_AF_INET6);
1907 /* NOTREACHED */
1908
1909 default:
1910 break;
1911 }
1912
1913 return gen_false();
1914}
1915
23fa89e6
PA
1916/*
1917 * Generate code to match a particular packet type.
1918 *
1919 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1920 * value, if <= ETHERMTU. We use that to determine whether to
1921 * match the type field or to check the type field for the special
1922 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1923 */
1924static struct block *
1925gen_linux_sll_linktype(proto)
1926 register int proto;
1927{
1928 struct block *b0, *b1;
1929
1930 switch (proto) {
1931
1932 case LLCSAP_ISONS:
1933 case LLCSAP_IP:
1934 case LLCSAP_NETBEUI:
1935 /*
1936 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1937 * so we check the DSAP and SSAP.
1938 *
1939 * LLCSAP_IP checks for IP-over-802.2, rather
1940 * than IP-over-Ethernet or IP-over-SNAP.
1941 *
1942 * XXX - should we check both the DSAP and the
1943 * SSAP, like this, or should we check just the
1944 * DSAP, as we do for other types <= ETHERMTU
1945 * (i.e., other SAP values)?
1946 */
1947 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
de0d3203 1948 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
23fa89e6
PA
1949 ((proto << 8) | proto));
1950 gen_and(b0, b1);
1951 return b1;
1952
1953 case LLCSAP_IPX:
1954 /*
1955 * Ethernet_II frames, which are Ethernet
1956 * frames with a frame type of ETHERTYPE_IPX;
1957 *
1958 * Ethernet_802.3 frames, which have a frame
1959 * type of LINUX_SLL_P_802_3;
1960 *
1961 * Ethernet_802.2 frames, which are 802.3
1962 * frames with an 802.2 LLC header (i.e, have
1963 * a frame type of LINUX_SLL_P_802_2) and
1964 * with the IPX LSAP as the DSAP in the LLC
1965 * header;
1966 *
1967 * Ethernet_SNAP frames, which are 802.3
1968 * frames with an LLC header and a SNAP
1969 * header and with an OUI of 0x000000
1970 * (encapsulated Ethernet) and a protocol
1971 * ID of ETHERTYPE_IPX in the SNAP header.
1972 *
1973 * First, do the checks on LINUX_SLL_P_802_2
1974 * frames; generate the check for either
1975 * Ethernet_802.2 or Ethernet_SNAP frames, and
1976 * then put a check for LINUX_SLL_P_802_2 frames
1977 * before it.
1978 */
de0d3203
PA
1979 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1980 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
23fa89e6
PA
1981 gen_or(b0, b1);
1982 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1983 gen_and(b0, b1);
1984
1985 /*
1986 * Now check for 802.3 frames and OR that with
1987 * the previous test.
1988 */
1989 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1990 gen_or(b0, b1);
1991
1992 /*
1993 * Now add the check for Ethernet_II frames, and
1994 * do that before checking for the other frame
1995 * types.
1996 */
1997 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1998 (bpf_int32)ETHERTYPE_IPX);
1999 gen_or(b0, b1);
2000 return b1;
2001
2002 case ETHERTYPE_ATALK:
2003 case ETHERTYPE_AARP:
2004 /*
2005 * EtherTalk (AppleTalk protocols on Ethernet link
2006 * layer) may use 802.2 encapsulation.
2007 */
2008
2009 /*
2010 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2011 * we check for the 802.2 protocol type in the
2012 * "Ethernet type" field.
2013 */
2014 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2015
2016 /*
2017 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2018 * SNAP packets with an organization code of
2019 * 0x080007 (Apple, for Appletalk) and a protocol
2020 * type of ETHERTYPE_ATALK (Appletalk).
2021 *
2022 * 802.2-encapsulated ETHERTYPE_AARP packets are
2023 * SNAP packets with an organization code of
2024 * 0x000000 (encapsulated Ethernet) and a protocol
2025 * type of ETHERTYPE_AARP (Appletalk ARP).
2026 */
2027 if (proto == ETHERTYPE_ATALK)
de0d3203 2028 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
23fa89e6 2029 else /* proto == ETHERTYPE_AARP */
de0d3203 2030 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
23fa89e6
PA
2031 gen_and(b0, b1);
2032
2033 /*
2034 * Check for Ethernet encapsulation (Ethertalk
2035 * phase 1?); we just check for the Ethernet
2036 * protocol type.
2037 */
2038 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2039
2040 gen_or(b0, b1);
2041 return b1;
2042
2043 default:
2044 if (proto <= ETHERMTU) {
2045 /*
2046 * This is an LLC SAP value, so the frames
2047 * that match would be 802.2 frames.
2048 * Check for the 802.2 protocol type
2049 * in the "Ethernet type" field, and
2050 * then check the DSAP.
2051 */
2052 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2053 LINUX_SLL_P_802_2);
de0d3203 2054 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
23fa89e6
PA
2055 (bpf_int32)proto);
2056 gen_and(b0, b1);
2057 return b1;
2058 } else {
2059 /*
2060 * This is an Ethernet type, so compare
2061 * the length/type field with it (if
2062 * the frame is an 802.2 frame, the length
2063 * field will be <= ETHERMTU, and, as
2064 * "proto" is > ETHERMTU, this test
2065 * will fail and the frame won't match,
2066 * which is what we want).
2067 */
2068 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2069 (bpf_int32)proto);
2070 }
2071 }
2072}
2073
de0d3203
PA
2074static struct slist *
2075gen_load_prism_llprefixlen()
23fa89e6
PA
2076{
2077 struct slist *s1, *s2;
de0d3203
PA
2078 struct slist *sjeq_avs_cookie;
2079 struct slist *sjcommon;
2080
2081 /*
2082 * This code is not compatible with the optimizer, as
2083 * we are generating jmp instructions within a normal
2084 * slist of instructions
2085 */
2086 no_optimize = 1;
23fa89e6
PA
2087
2088 /*
de0d3203
PA
2089 * Generate code to load the length of the radio header into
2090 * the register assigned to hold that length, if one has been
2091 * assigned. (If one hasn't been assigned, no code we've
2092 * generated uses that prefix, so we don't need to generate any
2093 * code to load it.)
2094 *
2095 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2096 * or always use the AVS header rather than the Prism header.
2097 * We load a 4-byte big-endian value at the beginning of the
2098 * raw packet data, and see whether, when masked with 0xFFFFF000,
2099 * it's equal to 0x80211000. If so, that indicates that it's
2100 * an AVS header (the masked-out bits are the version number).
2101 * Otherwise, it's a Prism header.
2102 *
2103 * XXX - the Prism header is also, in theory, variable-length,
2104 * but no known software generates headers that aren't 144
2105 * bytes long.
23fa89e6 2106 */
de0d3203
PA
2107 if (reg_off_ll != -1) {
2108 /*
2109 * Load the cookie.
2110 */
2111 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2112 s1->s.k = 0;
2113
2114 /*
2115 * AND it with 0xFFFFF000.
2116 */
2117 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2118 s2->s.k = 0xFFFFF000;
2119 sappend(s1, s2);
2120
2121 /*
2122 * Compare with 0x80211000.
2123 */
2124 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2125 sjeq_avs_cookie->s.k = 0x80211000;
2126 sappend(s1, sjeq_avs_cookie);
2127
2128 /*
2129 * If it's AVS:
2130 *
2131 * The 4 bytes at an offset of 4 from the beginning of
2132 * the AVS header are the length of the AVS header.
2133 * That field is big-endian.
2134 */
2135 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2136 s2->s.k = 4;
2137 sappend(s1, s2);
2138 sjeq_avs_cookie->s.jt = s2;
2139
2140 /*
2141 * Now jump to the code to allocate a register
2142 * into which to save the header length and
2143 * store the length there. (The "jump always"
2144 * instruction needs to have the k field set;
2145 * it's added to the PC, so, as we're jumping
2146 * over a single instruction, it should be 1.)
2147 */
2148 sjcommon = new_stmt(JMP(BPF_JA));
2149 sjcommon->s.k = 1;
2150 sappend(s1, sjcommon);
2151
2152 /*
2153 * Now for the code that handles the Prism header.
2154 * Just load the length of the Prism header (144)
2155 * into the A register. Have the test for an AVS
2156 * header branch here if we don't have an AVS header.
2157 */
2158 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2159 s2->s.k = 144;
2160 sappend(s1, s2);
2161 sjeq_avs_cookie->s.jf = s2;
2162
2163 /*
2164 * Now allocate a register to hold that value and store
2165 * it. The code for the AVS header will jump here after
2166 * loading the length of the AVS header.
2167 */
2168 s2 = new_stmt(BPF_ST);
2169 s2->s.k = reg_off_ll;
2170 sappend(s1, s2);
2171 sjcommon->s.jf = s2;
2172
2173 /*
2174 * Now move it into the X register.
2175 */
2176 s2 = new_stmt(BPF_MISC|BPF_TAX);
2177 sappend(s1, s2);
2178
2179 return (s1);
2180 } else
2181 return (NULL);
2182}
2183
2184static struct slist *
2185gen_load_avs_llprefixlen()
2186{
2187 struct slist *s1, *s2;
2188
2189 /*
2190 * Generate code to load the length of the AVS header into
2191 * the register assigned to hold that length, if one has been
2192 * assigned. (If one hasn't been assigned, no code we've
2193 * generated uses that prefix, so we don't need to generate any
2194 * code to load it.)
2195 */
2196 if (reg_off_ll != -1) {
2197 /*
2198 * The 4 bytes at an offset of 4 from the beginning of
2199 * the AVS header are the length of the AVS header.
2200 * That field is big-endian.
2201 */
2202 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2203 s1->s.k = 4;
2204
2205 /*
2206 * Now allocate a register to hold that value and store
2207 * it.
2208 */
2209 s2 = new_stmt(BPF_ST);
2210 s2->s.k = reg_off_ll;
2211 sappend(s1, s2);
2212
2213 /*
2214 * Now move it into the X register.
2215 */
2216 s2 = new_stmt(BPF_MISC|BPF_TAX);
2217 sappend(s1, s2);
2218
2219 return (s1);
2220 } else
2221 return (NULL);
2222}
2223
2224static struct slist *
2225gen_load_radiotap_llprefixlen()
2226{
2227 struct slist *s1, *s2;
2228
2229 /*
2230 * Generate code to load the length of the radiotap header into
2231 * the register assigned to hold that length, if one has been
2232 * assigned. (If one hasn't been assigned, no code we've
2233 * generated uses that prefix, so we don't need to generate any
2234 * code to load it.)
2235 */
2236 if (reg_off_ll != -1) {
23fa89e6
PA
2237 /*
2238 * The 2 bytes at offsets of 2 and 3 from the beginning
2239 * of the radiotap header are the length of the radiotap
2240 * header; unfortunately, it's little-endian, so we have
2241 * to load it a byte at a time and construct the value.
2242 */
2243
2244 /*
2245 * Load the high-order byte, at an offset of 3, shift it
2246 * left a byte, and put the result in the X register.
2247 */
2248 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2249 s1->s.k = 3;
2250 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2251 sappend(s1, s2);
2252 s2->s.k = 8;
2253 s2 = new_stmt(BPF_MISC|BPF_TAX);
2254 sappend(s1, s2);
2255
2256 /*
2257 * Load the next byte, at an offset of 2, and OR the
2258 * value from the X register into it.
2259 */
2260 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2261 sappend(s1, s2);
2262 s2->s.k = 2;
2263 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2264 sappend(s1, s2);
2265
2266 /*
2267 * Now allocate a register to hold that value and store
2268 * it.
2269 */
2270 s2 = new_stmt(BPF_ST);
de0d3203 2271 s2->s.k = reg_off_ll;
23fa89e6
PA
2272 sappend(s1, s2);
2273
2274 /*
2275 * Now move it into the X register.
2276 */
2277 s2 = new_stmt(BPF_MISC|BPF_TAX);
2278 sappend(s1, s2);
2279
de0d3203
PA
2280 return (s1);
2281 } else
2282 return (NULL);
23fa89e6
PA
2283}
2284
dfcad8d8
PA
2285/*
2286 * At the moment we treat PPI as normal Radiotap encoded
2287 * packets. The difference is in the function that generates
2288 * the code at the beginning to compute the header length.
2289 * Since this code generator of PPI supports bare 802.11
2290 * encapsulation only (i.e. the encapsulated DLT should be
de0d3203
PA
2291 * DLT_IEEE802_11) we generate code to check for this too;
2292 * that's done in finish_parse().
dfcad8d8 2293 */
de0d3203
PA
2294static struct slist *
2295gen_load_ppi_llprefixlen()
dfcad8d8
PA
2296{
2297 struct slist *s1, *s2;
2298
2299 /*
de0d3203
PA
2300 * Generate code to load the length of the radiotap header
2301 * into the register assigned to hold that length, if one has
2302 * been assigned.
dfcad8d8 2303 */
de0d3203
PA
2304 if (reg_off_ll != -1) {
2305 /*
dfcad8d8
PA
2306 * The 2 bytes at offsets of 2 and 3 from the beginning
2307 * of the radiotap header are the length of the radiotap
2308 * header; unfortunately, it's little-endian, so we have
2309 * to load it a byte at a time and construct the value.
2310 */
2311
2312 /*
2313 * Load the high-order byte, at an offset of 3, shift it
2314 * left a byte, and put the result in the X register.
2315 */
2316 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2317 s1->s.k = 3;
2318 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2319 sappend(s1, s2);
2320 s2->s.k = 8;
2321 s2 = new_stmt(BPF_MISC|BPF_TAX);
2322 sappend(s1, s2);
2323
2324 /*
2325 * Load the next byte, at an offset of 2, and OR the
2326 * value from the X register into it.
2327 */
2328 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2329 sappend(s1, s2);
2330 s2->s.k = 2;
2331 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2332 sappend(s1, s2);
2333
2334 /*
2335 * Now allocate a register to hold that value and store
2336 * it.
2337 */
2338 s2 = new_stmt(BPF_ST);
de0d3203 2339 s2->s.k = reg_off_ll;
dfcad8d8
PA
2340 sappend(s1, s2);
2341
2342 /*
2343 * Now move it into the X register.
2344 */
2345 s2 = new_stmt(BPF_MISC|BPF_TAX);
2346 sappend(s1, s2);
2347
de0d3203
PA
2348 return (s1);
2349 } else
2350 return (NULL);
2351}
2352
2353/*
2354 * Load a value relative to the beginning of the link-layer header after the 802.11
2355 * header, i.e. LLC_SNAP.
2356 * The link-layer header doesn't necessarily begin at the beginning
2357 * of the packet data; there might be a variable-length prefix containing
2358 * radio information.
2359 */
2360static struct slist *
2361gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2362{
2363 struct slist *s2;
2364 struct slist *sjset_data_frame_1;
2365 struct slist *sjset_data_frame_2;
2366 struct slist *sjset_qos;
2367 struct slist *sjset_radiotap_flags;
2368 struct slist *sjset_radiotap_tsft;
2369 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2370 struct slist *s_roundup;
2371
2372 if (reg_off_macpl == -1) {
2373 /*
2374 * No register has been assigned to the offset of
2375 * the MAC-layer payload, which means nobody needs
2376 * it; don't bother computing it - just return
2377 * what we already have.
2378 */
2379 return (s);
2380 }
2381
2382 /*
2383 * This code is not compatible with the optimizer, as
2384 * we are generating jmp instructions within a normal
2385 * slist of instructions
2386 */
2387 no_optimize = 1;
2388
2389 /*
2390 * If "s" is non-null, it has code to arrange that the X register
2391 * contains the length of the prefix preceding the link-layer
2392 * header.
2393 *
2394 * Otherwise, the length of the prefix preceding the link-layer
2395 * header is "off_ll".
2396 */
2397 if (s == NULL) {
2398 /*
2399 * There is no variable-length header preceding the
2400 * link-layer header.
2401 *
2402 * Load the length of the fixed-length prefix preceding
2403 * the link-layer header (if any) into the X register,
2404 * and store it in the reg_off_macpl register.
2405 * That length is off_ll.
2406 */
2407 s = new_stmt(BPF_LDX|BPF_IMM);
2408 s->s.k = off_ll;
2409 }
2410
2411 /*
2412 * The X register contains the offset of the beginning of the
2413 * link-layer header; add 24, which is the minimum length
2414 * of the MAC header for a data frame, to that, and store it
2415 * in reg_off_macpl, and then load the Frame Control field,
2416 * which is at the offset in the X register, with an indexed load.
2417 */
2418 s2 = new_stmt(BPF_MISC|BPF_TXA);
2419 sappend(s, s2);
2420 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2421 s2->s.k = 24;
2422 sappend(s, s2);
2423 s2 = new_stmt(BPF_ST);
2424 s2->s.k = reg_off_macpl;
2425 sappend(s, s2);
2426
2427 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2428 s2->s.k = 0;
2429 sappend(s, s2);
2430
2431 /*
2432 * Check the Frame Control field to see if this is a data frame;
2433 * a data frame has the 0x08 bit (b3) in that field set and the
2434 * 0x04 bit (b2) clear.
2435 */
2436 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2437 sjset_data_frame_1->s.k = 0x08;
2438 sappend(s, sjset_data_frame_1);
2439
2440 /*
2441 * If b3 is set, test b2, otherwise go to the first statement of
2442 * the rest of the program.
2443 */
2444 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2445 sjset_data_frame_2->s.k = 0x04;
2446 sappend(s, sjset_data_frame_2);
2447 sjset_data_frame_1->s.jf = snext;
2448
2449 /*
2450 * If b2 is not set, this is a data frame; test the QoS bit.
2451 * Otherwise, go to the first statement of the rest of the
2452 * program.
2453 */
2454 sjset_data_frame_2->s.jt = snext;
2455 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2456 sjset_qos->s.k = 0x80; /* QoS bit */
2457 sappend(s, sjset_qos);
2458
2459 /*
2460 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2461 * field.
2462 * Otherwise, go to the first statement of the rest of the
2463 * program.
2464 */
2465 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2466 s2->s.k = reg_off_macpl;
2467 sappend(s, s2);
2468 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2469 s2->s.k = 2;
2470 sappend(s, s2);
2471 s2 = new_stmt(BPF_ST);
2472 s2->s.k = reg_off_macpl;
2473 sappend(s, s2);
2474
2475 /*
2476 * If we have a radiotap header, look at it to see whether
2477 * there's Atheros padding between the MAC-layer header
2478 * and the payload.
2479 *
2480 * Note: all of the fields in the radiotap header are
2481 * little-endian, so we byte-swap all of the values
2482 * we test against, as they will be loaded as big-endian
2483 * values.
2484 */
2485 if (linktype == DLT_IEEE802_11_RADIO) {
dfcad8d8 2486 /*
de0d3203
PA
2487 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2488 * in the presence flag?
dfcad8d8 2489 */
de0d3203
PA
2490 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2491 s2->s.k = 4;
2492 sappend(s, s2);
2493
2494 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2495 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2496 sappend(s, sjset_radiotap_flags);
dfcad8d8 2497
de0d3203
PA
2498 /*
2499 * If not, skip all of this.
2500 */
2501 sjset_radiotap_flags->s.jf = snext;
2502
2503 /*
2504 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2505 */
2506 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2507 new_stmt(JMP(BPF_JSET));
2508 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2509 sappend(s, sjset_radiotap_tsft);
2510
2511 /*
2512 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2513 * at an offset of 16 from the beginning of the raw packet
2514 * data (8 bytes for the radiotap header and 8 bytes for
2515 * the TSFT field).
2516 *
2517 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2518 * is set.
2519 */
2520 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2521 s2->s.k = 16;
2522 sappend(s, s2);
2523
2524 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2525 sjset_tsft_datapad->s.k = 0x20;
2526 sappend(s, sjset_tsft_datapad);
2527
2528 /*
2529 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2530 * at an offset of 8 from the beginning of the raw packet
2531 * data (8 bytes for the radiotap header).
2532 *
2533 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2534 * is set.
2535 */
2536 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2537 s2->s.k = 8;
2538 sappend(s, s2);
2539
2540 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2541 sjset_notsft_datapad->s.k = 0x20;
2542 sappend(s, sjset_notsft_datapad);
2543
2544 /*
2545 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2546 * set, round the length of the 802.11 header to
2547 * a multiple of 4. Do that by adding 3 and then
2548 * dividing by and multiplying by 4, which we do by
2549 * ANDing with ~3.
2550 */
2551 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2552 s_roundup->s.k = reg_off_macpl;
2553 sappend(s, s_roundup);
2554 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2555 s2->s.k = 3;
2556 sappend(s, s2);
2557 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2558 s2->s.k = ~3;
2559 sappend(s, s2);
2560 s2 = new_stmt(BPF_ST);
2561 s2->s.k = reg_off_macpl;
2562 sappend(s, s2);
2563
2564 sjset_tsft_datapad->s.jt = s_roundup;
2565 sjset_tsft_datapad->s.jf = snext;
2566 sjset_notsft_datapad->s.jt = s_roundup;
2567 sjset_notsft_datapad->s.jf = snext;
2568 } else
2569 sjset_qos->s.jf = snext;
2570
2571 return s;
2572}
2573
2574static void
2575insert_compute_vloffsets(b)
2576 struct block *b;
2577{
2578 struct slist *s;
2579
2580 /*
2581 * For link-layer types that have a variable-length header
2582 * preceding the link-layer header, generate code to load
2583 * the offset of the link-layer header into the register
2584 * assigned to that offset, if any.
2585 */
2586 switch (linktype) {
2587
2588 case DLT_PRISM_HEADER:
2589 s = gen_load_prism_llprefixlen();
2590 break;
2591
2592 case DLT_IEEE802_11_RADIO_AVS:
2593 s = gen_load_avs_llprefixlen();
2594 break;
2595
2596 case DLT_IEEE802_11_RADIO:
2597 s = gen_load_radiotap_llprefixlen();
2598 break;
2599
2600 case DLT_PPI:
2601 s = gen_load_ppi_llprefixlen();
2602 break;
2603
2604 default:
2605 s = NULL;
2606 break;
2607 }
2608
2609 /*
2610 * For link-layer types that have a variable-length link-layer
2611 * header, generate code to load the offset of the MAC-layer
2612 * payload into the register assigned to that offset, if any.
2613 */
2614 switch (linktype) {
2615
2616 case DLT_IEEE802_11:
2617 case DLT_PRISM_HEADER:
2618 case DLT_IEEE802_11_RADIO_AVS:
2619 case DLT_IEEE802_11_RADIO:
2620 case DLT_PPI:
2621 s = gen_load_802_11_header_len(s, b->stmts);
2622 break;
2623 }
2624
2625 /*
2626 * If we have any offset-loading code, append all the
2627 * existing statements in the block to those statements,
2628 * and make the resulting list the list of statements
2629 * for the block.
2630 */
2631 if (s != NULL) {
2632 sappend(s, b->stmts);
2633 b->stmts = s;
dfcad8d8
PA
2634 }
2635}
de0d3203 2636
dfcad8d8
PA
2637static struct block *
2638gen_ppi_dlt_check(void)
2639{
2640 struct slist *s_load_dlt;
2641 struct block *b;
2642
2643 if (linktype == DLT_PPI)
2644 {
2645 /* Create the statements that check for the DLT
2646 */
2647 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2648 s_load_dlt->s.k = 4;
2649
2650 b = new_block(JMP(BPF_JEQ));
2651
2652 b->stmts = s_load_dlt;
2653 b->s.k = SWAPLONG(DLT_IEEE802_11);
2654 }
2655 else
2656 {
2657 b = NULL;
2658 }
2659
2660 return b;
2661}
23fa89e6 2662
de0d3203
PA
2663static struct slist *
2664gen_prism_llprefixlen(void)
23fa89e6 2665{
de0d3203
PA
2666 struct slist *s;
2667
2668 if (reg_off_ll == -1) {
2669 /*
2670 * We haven't yet assigned a register for the length
2671 * of the radio header; allocate one.
2672 */
2673 reg_off_ll = alloc_reg();
2674 }
23fa89e6 2675
de0d3203
PA
2676 /*
2677 * Load the register containing the radio length
2678 * into the X register.
dfcad8d8 2679 */
de0d3203
PA
2680 s = new_stmt(BPF_LDX|BPF_MEM);
2681 s->s.k = reg_off_ll;
2682 return s;
2683}
dfcad8d8 2684
de0d3203
PA
2685static struct slist *
2686gen_avs_llprefixlen(void)
2687{
2688 struct slist *s;
2689
2690 if (reg_off_ll == -1) {
2691 /*
2692 * We haven't yet assigned a register for the length
2693 * of the AVS header; allocate one.
2694 */
2695 reg_off_ll = alloc_reg();
23fa89e6 2696 }
23fa89e6 2697
de0d3203
PA
2698 /*
2699 * Load the register containing the AVS length
2700 * into the X register.
2701 */
2702 s = new_stmt(BPF_LDX|BPF_MEM);
2703 s->s.k = reg_off_ll;
2704 return s;
2705}
23fa89e6
PA
2706
2707static struct slist *
2708gen_radiotap_llprefixlen(void)
2709{
2710 struct slist *s;
2711
de0d3203 2712 if (reg_off_ll == -1) {
23fa89e6
PA
2713 /*
2714 * We haven't yet assigned a register for the length
2715 * of the radiotap header; allocate one.
2716 */
de0d3203 2717 reg_off_ll = alloc_reg();
23fa89e6
PA
2718 }
2719
2720 /*
2721 * Load the register containing the radiotap length
2722 * into the X register.
2723 */
2724 s = new_stmt(BPF_LDX|BPF_MEM);
de0d3203 2725 s->s.k = reg_off_ll;
23fa89e6
PA
2726 return s;
2727}
2728
dfcad8d8
PA
2729/*
2730 * At the moment we treat PPI as normal Radiotap encoded
2731 * packets. The difference is in the function that generates
2732 * the code at the beginning to compute the header length.
2733 * Since this code generator of PPI supports bare 802.11
2734 * encapsulation only (i.e. the encapsulated DLT should be
2735 * DLT_IEEE802_11) we generate code to check for this too.
2736 */
2737static struct slist *
2738gen_ppi_llprefixlen(void)
2739{
2740 struct slist *s;
2741
de0d3203 2742 if (reg_off_ll == -1) {
dfcad8d8
PA
2743 /*
2744 * We haven't yet assigned a register for the length
2745 * of the radiotap header; allocate one.
2746 */
de0d3203 2747 reg_off_ll = alloc_reg();
dfcad8d8
PA
2748 }
2749
2750 /*
de0d3203 2751 * Load the register containing the PPI length
dfcad8d8
PA
2752 * into the X register.
2753 */
2754 s = new_stmt(BPF_LDX|BPF_MEM);
de0d3203 2755 s->s.k = reg_off_ll;
dfcad8d8
PA
2756 return s;
2757}
2758
23fa89e6
PA
2759/*
2760 * Generate code to compute the link-layer header length, if necessary,
2761 * putting it into the X register, and to return either a pointer to a
2762 * "struct slist" for the list of statements in that code, or NULL if
2763 * no code is necessary.
2764 */
2765static struct slist *
2766gen_llprefixlen(void)
2767{
2768 switch (linktype) {
2769
de0d3203
PA
2770 case DLT_PRISM_HEADER:
2771 return gen_prism_llprefixlen();
2772
2773 case DLT_IEEE802_11_RADIO_AVS:
2774 return gen_avs_llprefixlen();
dfcad8d8 2775
23fa89e6
PA
2776 case DLT_IEEE802_11_RADIO:
2777 return gen_radiotap_llprefixlen();
2778
de0d3203
PA
2779 case DLT_PPI:
2780 return gen_ppi_llprefixlen();
2781
23fa89e6
PA
2782 default:
2783 return NULL;
2784 }
2785}
2786
de0d3203
PA
2787/*
2788 * Generate code to load the register containing the offset of the
2789 * MAC-layer payload into the X register; if no register for that offset
2790 * has been allocated, allocate it first.
2791 */
2792static struct slist *
2793gen_off_macpl(void)
2794{
2795 struct slist *s;
2796
2797 if (off_macpl_is_variable) {
2798 if (reg_off_macpl == -1) {
2799 /*
2800 * We haven't yet assigned a register for the offset
2801 * of the MAC-layer payload; allocate one.
2802 */
2803 reg_off_macpl = alloc_reg();
2804 }
2805
2806 /*
2807 * Load the register containing the offset of the MAC-layer
2808 * payload into the X register.
2809 */
2810 s = new_stmt(BPF_LDX|BPF_MEM);
2811 s->s.k = reg_off_macpl;
2812 return s;
2813 } else {
2814 /*
2815 * That offset isn't variable, so we don't need to
2816 * generate any code.
2817 */
2818 return NULL;
2819 }
2820}
2821
2822/*
2823 * Map an Ethernet type to the equivalent PPP type.
2824 */
2825static int
2826ethertype_to_ppptype(proto)
2827 int proto;
2828{
2829 switch (proto) {
2830
2831 case ETHERTYPE_IP:
2832 proto = PPP_IP;
2833 break;
2834
2835#ifdef INET6
2836 case ETHERTYPE_IPV6:
2837 proto = PPP_IPV6;
2838 break;
2839#endif
2840
2841 case ETHERTYPE_DN:
2842 proto = PPP_DECNET;
2843 break;
2844
2845 case ETHERTYPE_ATALK:
2846 proto = PPP_APPLE;
2847 break;
2848
2849 case ETHERTYPE_NS:
2850 proto = PPP_NS;
2851 break;
2852
2853 case LLCSAP_ISONS:
2854 proto = PPP_OSI;
2855 break;
2856
2857 case LLCSAP_8021D:
2858 /*
2859 * I'm assuming the "Bridging PDU"s that go
2860 * over PPP are Spanning Tree Protocol
2861 * Bridging PDUs.
2862 */
2863 proto = PPP_BRPDU;
2864 break;
2865
2866 case LLCSAP_IPX:
2867 proto = PPP_IPX;
2868 break;
2869 }
2870 return (proto);
2871}
2872
23fa89e6
PA
2873/*
2874 * Generate code to match a particular packet type by matching the
2875 * link-layer type field or fields in the 802.2 LLC header.
2876 *
2877 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2878 * value, if <= ETHERMTU.
2879 */
2880static struct block *
2881gen_linktype(proto)
2882 register int proto;
2883{
2884 struct block *b0, *b1, *b2;
2885
2886 /* are we checking MPLS-encapsulated packets? */
2887 if (label_stack_depth > 0) {
2888 switch (proto) {
2889 case ETHERTYPE_IP:
2890 case PPP_IP:
de0d3203 2891 /* FIXME add other L3 proto IDs */
23fa89e6
PA
2892 return gen_mpls_linktype(Q_IP);
2893
2894 case ETHERTYPE_IPV6:
2895 case PPP_IPV6:
de0d3203 2896 /* FIXME add other L3 proto IDs */
23fa89e6
PA
2897 return gen_mpls_linktype(Q_IPV6);
2898
2899 default:
2900 bpf_error("unsupported protocol over mpls");
2901 /* NOTREACHED */
2902 }
2903 }
2904
de0d3203
PA
2905 /*
2906 * Are we testing PPPoE packets?
2907 */
2908 if (is_pppoes) {
2909 /*
2910 * The PPPoE session header is part of the
2911 * MAC-layer payload, so all references
2912 * should be relative to the beginning of
2913 * that payload.
2914 */
2915
2916 /*
2917 * We use Ethernet protocol types inside libpcap;
2918 * map them to the corresponding PPP protocol types.
2919 */
2920 proto = ethertype_to_ppptype(proto);
2921 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2922 }
2923
23fa89e6
PA
2924 switch (linktype) {
2925
2926 case DLT_EN10MB:
a85e14b0
PA
2927 case DLT_NETANALYZER:
2928 case DLT_NETANALYZER_TRANSPARENT:
23fa89e6
PA
2929 return gen_ether_linktype(proto);
2930 /*NOTREACHED*/
2931 break;
2932
2933 case DLT_C_HDLC:
2934 switch (proto) {
2935
2936 case LLCSAP_ISONS:
2937 proto = (proto << 8 | LLCSAP_ISONS);
2938 /* fall through */
2939
2940 default:
2941 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2942 (bpf_int32)proto);
2943 /*NOTREACHED*/
2944 break;
2945 }
2946 break;
2947
23fa89e6 2948 case DLT_IEEE802_11:
de0d3203 2949 case DLT_PRISM_HEADER:
23fa89e6
PA
2950 case DLT_IEEE802_11_RADIO_AVS:
2951 case DLT_IEEE802_11_RADIO:
de0d3203
PA
2952 case DLT_PPI:
2953 /*
2954 * Check that we have a data frame.
2955 */
2956 b0 = gen_check_802_11_data_frame();
2957
2958 /*
2959 * Now check for the specified link-layer type.
2960 */
2961 b1 = gen_llc_linktype(proto);
2962 gen_and(b0, b1);
2963 return b1;
2964 /*NOTREACHED*/
2965 break;
2966
2967 case DLT_FDDI:
2968 /*
2969 * XXX - check for asynchronous frames, as per RFC 1103.
2970 */
2971 return gen_llc_linktype(proto);
2972 /*NOTREACHED*/
2973 break;
2974
2975 case DLT_IEEE802:
2976 /*
2977 * XXX - check for LLC PDUs, as per IEEE 802.5.
2978 */
2979 return gen_llc_linktype(proto);
2980 /*NOTREACHED*/
2981 break;
2982
23fa89e6
PA
2983 case DLT_ATM_RFC1483:
2984 case DLT_ATM_CLIP:
2985 case DLT_IP_OVER_FC:
2986 return gen_llc_linktype(proto);
2987 /*NOTREACHED*/
2988 break;
2989
2990 case DLT_SUNATM:
2991 /*
2992 * If "is_lane" is set, check for a LANE-encapsulated
2993 * version of this protocol, otherwise check for an
2994 * LLC-encapsulated version of this protocol.
2995 *
2996 * We assume LANE means Ethernet, not Token Ring.
2997 */
2998 if (is_lane) {
2999 /*
3000 * Check that the packet doesn't begin with an
3001 * LE Control marker. (We've already generated
3002 * a test for LANE.)
3003 */
3004 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3005 0xFF00);
3006 gen_not(b0);
3007
3008 /*
3009 * Now generate an Ethernet test.
3010 */
3011 b1 = gen_ether_linktype(proto);
3012 gen_and(b0, b1);
3013 return b1;
3014 } else {
3015 /*
3016 * Check for LLC encapsulation and then check the
3017 * protocol.
3018 */
3019 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3020 b1 = gen_llc_linktype(proto);
3021 gen_and(b0, b1);
3022 return b1;
3023 }
3024 /*NOTREACHED*/
3025 break;
3026
3027 case DLT_LINUX_SLL:
3028 return gen_linux_sll_linktype(proto);
3029 /*NOTREACHED*/
3030 break;
3031
3032 case DLT_SLIP:
3033 case DLT_SLIP_BSDOS:
3034 case DLT_RAW:
3035 /*
3036 * These types don't provide any type field; packets
dfcad8d8 3037 * are always IPv4 or IPv6.
23fa89e6
PA
3038 *
3039 * XXX - for IPv4, check for a version number of 4, and,
3040 * for IPv6, check for a version number of 6?
3041 */
3042 switch (proto) {
3043
3044 case ETHERTYPE_IP:
dfcad8d8
PA
3045 /* Check for a version number of 4. */
3046 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
23fa89e6
PA
3047#ifdef INET6
3048 case ETHERTYPE_IPV6:
dfcad8d8
PA
3049 /* Check for a version number of 6. */
3050 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
23fa89e6 3051#endif
23fa89e6
PA
3052
3053 default:
3054 return gen_false(); /* always false */
3055 }
3056 /*NOTREACHED*/
3057 break;
3058
a85e14b0
PA
3059 case DLT_IPV4:
3060 /*
3061 * Raw IPv4, so no type field.
3062 */
3063 if (proto == ETHERTYPE_IP)
3064 return gen_true(); /* always true */
3065
3066 /* Checking for something other than IPv4; always false */
3067 return gen_false();
3068 /*NOTREACHED*/
3069 break;
3070
3071 case DLT_IPV6:
3072 /*
3073 * Raw IPv6, so no type field.
3074 */
3075#ifdef INET6
3076 if (proto == ETHERTYPE_IPV6)
3077 return gen_true(); /* always true */
3078#endif
3079
3080 /* Checking for something other than IPv6; always false */
3081 return gen_false();
3082 /*NOTREACHED*/
3083 break;
3084
23fa89e6
PA
3085 case DLT_PPP:
3086 case DLT_PPP_PPPD:
3087 case DLT_PPP_SERIAL:
3088 case DLT_PPP_ETHER:
3089 /*
3090 * We use Ethernet protocol types inside libpcap;
3091 * map them to the corresponding PPP protocol types.
3092 */
de0d3203
PA
3093 proto = ethertype_to_ppptype(proto);
3094 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3095 /*NOTREACHED*/
23fa89e6
PA
3096 break;
3097
3098 case DLT_PPP_BSDOS:
3099 /*
3100 * We use Ethernet protocol types inside libpcap;
3101 * map them to the corresponding PPP protocol types.
3102 */
3103 switch (proto) {
3104
3105 case ETHERTYPE_IP:
de0d3203
PA
3106 /*
3107 * Also check for Van Jacobson-compressed IP.
3108 * XXX - do this for other forms of PPP?
3109 */
23fa89e6
PA
3110 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3111 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3112 gen_or(b0, b1);
3113 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3114 gen_or(b1, b0);
3115 return b0;
3116
de0d3203
PA
3117 default:
3118 proto = ethertype_to_ppptype(proto);
3119 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3120 (bpf_int32)proto);
23fa89e6 3121 }
de0d3203 3122 /*NOTREACHED*/
23fa89e6
PA
3123 break;
3124
3125 case DLT_NULL:
3126 case DLT_LOOP:
3127 case DLT_ENC:
3128 /*
3129 * For DLT_NULL, the link-layer header is a 32-bit
3130 * word containing an AF_ value in *host* byte order,
3131 * and for DLT_ENC, the link-layer header begins
3132 * with a 32-bit work containing an AF_ value in
3133 * host byte order.
3134 *
3135 * In addition, if we're reading a saved capture file,
3136 * the host byte order in the capture may not be the
3137 * same as the host byte order on this machine.
3138 *
3139 * For DLT_LOOP, the link-layer header is a 32-bit
3140 * word containing an AF_ value in *network* byte order.
3141 *
3142 * XXX - AF_ values may, unfortunately, be platform-
3143 * dependent; for example, FreeBSD's AF_INET6 is 24
3144 * whilst NetBSD's and OpenBSD's is 26.
3145 *
3146 * This means that, when reading a capture file, just
3147 * checking for our AF_INET6 value won't work if the
3148 * capture file came from another OS.
3149 */
3150 switch (proto) {
3151
3152 case ETHERTYPE_IP:
3153 proto = AF_INET;
3154 break;
3155
3156#ifdef INET6
3157 case ETHERTYPE_IPV6:
3158 proto = AF_INET6;
3159 break;
3160#endif
3161
3162 default:
3163 /*
3164 * Not a type on which we support filtering.
3165 * XXX - support those that have AF_ values
3166 * #defined on this platform, at least?
3167 */
3168 return gen_false();
3169 }
3170
3171 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3172 /*
3173 * The AF_ value is in host byte order, but
3174 * the BPF interpreter will convert it to
3175 * network byte order.
3176 *
3177 * If this is a save file, and it's from a
3178 * machine with the opposite byte order to
3179 * ours, we byte-swap the AF_ value.
3180 *
3181 * Then we run it through "htonl()", and
3182 * generate code to compare against the result.
3183 */
3184 if (bpf_pcap->sf.rfile != NULL &&
3185 bpf_pcap->sf.swapped)
3186 proto = SWAPLONG(proto);
3187 proto = htonl(proto);
3188 }
3189 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3190
0cb9e74f 3191#ifdef HAVE_NET_PFVAR_H
23fa89e6
PA
3192 case DLT_PFLOG:
3193 /*
3194 * af field is host byte order in contrast to the rest of
3195 * the packet.
3196 */
3197 if (proto == ETHERTYPE_IP)
3198 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3199 BPF_B, (bpf_int32)AF_INET));
3200#ifdef INET6
3201 else if (proto == ETHERTYPE_IPV6)
3202 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3203 BPF_B, (bpf_int32)AF_INET6));
3204#endif /* INET6 */
3205 else
3206 return gen_false();
3207 /*NOTREACHED*/
3208 break;
0cb9e74f 3209#endif /* HAVE_NET_PFVAR_H */
23fa89e6
PA
3210
3211 case DLT_ARCNET:
3212 case DLT_ARCNET_LINUX:
3213 /*
3214 * XXX should we check for first fragment if the protocol
3215 * uses PHDS?
3216 */
3217 switch (proto) {
3218
3219 default:
3220 return gen_false();
3221
3222#ifdef INET6
3223 case ETHERTYPE_IPV6:
3224 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3225 (bpf_int32)ARCTYPE_INET6));
3226#endif /* INET6 */
3227
3228 case ETHERTYPE_IP:
3229 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3230 (bpf_int32)ARCTYPE_IP);
3231 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3232 (bpf_int32)ARCTYPE_IP_OLD);
3233 gen_or(b0, b1);
3234 return (b1);
3235
3236 case ETHERTYPE_ARP:
3237 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3238 (bpf_int32)ARCTYPE_ARP);
3239 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3240 (bpf_int32)ARCTYPE_ARP_OLD);
3241 gen_or(b0, b1);
3242 return (b1);
3243
3244 case ETHERTYPE_REVARP:
3245 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3246 (bpf_int32)ARCTYPE_REVARP));
3247
3248 case ETHERTYPE_ATALK:
3249 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3250 (bpf_int32)ARCTYPE_ATALK));
3251 }
3252 /*NOTREACHED*/
3253 break;
3254
3255 case DLT_LTALK:
3256 switch (proto) {
3257 case ETHERTYPE_ATALK:
3258 return gen_true();
3259 default:
3260 return gen_false();
3261 }
3262 /*NOTREACHED*/
3263 break;
3264
3265 case DLT_FRELAY:
3266 /*
3267 * XXX - assumes a 2-byte Frame Relay header with
3268 * DLCI and flags. What if the address is longer?
3269 */
3270 switch (proto) {
3271
3272 case ETHERTYPE_IP:
3273 /*
3274 * Check for the special NLPID for IP.
3275 */
3276 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3277
3278#ifdef INET6
3279 case ETHERTYPE_IPV6:
3280 /*
3281 * Check for the special NLPID for IPv6.
3282 */
3283 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3284#endif
3285
3286 case LLCSAP_ISONS:
3287 /*
3288 * Check for several OSI protocols.
3289 *
3290 * Frame Relay packets typically have an OSI
3291 * NLPID at the beginning; we check for each
3292 * of them.
3293 *
3294 * What we check for is the NLPID and a frame
3295 * control field of UI, i.e. 0x03 followed
3296 * by the NLPID.
3297 */
3298 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3299 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3300 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3301 gen_or(b1, b2);
3302 gen_or(b0, b2);
3303 return b2;
3304
3305 default:
3306 return gen_false();
3307 }
3308 /*NOTREACHED*/
3309 break;
3310
de0d3203
PA
3311 case DLT_MFR:
3312 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3313
23fa89e6
PA
3314 case DLT_JUNIPER_MFR:
3315 case DLT_JUNIPER_MLFR:
3316 case DLT_JUNIPER_MLPPP:
3317 case DLT_JUNIPER_ATM1:
3318 case DLT_JUNIPER_ATM2:
3319 case DLT_JUNIPER_PPPOE:
3320 case DLT_JUNIPER_PPPOE_ATM:
3321 case DLT_JUNIPER_GGSN:
3322 case DLT_JUNIPER_ES:
3323 case DLT_JUNIPER_MONITOR:
3324 case DLT_JUNIPER_SERVICES:
3325 case DLT_JUNIPER_ETHER:
3326 case DLT_JUNIPER_PPP:
3327 case DLT_JUNIPER_FRELAY:
3328 case DLT_JUNIPER_CHDLC:
dfcad8d8 3329 case DLT_JUNIPER_VP:
de0d3203
PA
3330 case DLT_JUNIPER_ST:
3331 case DLT_JUNIPER_ISM:
a85e14b0
PA
3332 case DLT_JUNIPER_VS:
3333 case DLT_JUNIPER_SRX_E2E:
3334 case DLT_JUNIPER_FIBRECHANNEL:
3335 case DLT_JUNIPER_ATM_CEMIC:
3336
23fa89e6
PA
3337 /* just lets verify the magic number for now -
3338 * on ATM we may have up to 6 different encapsulations on the wire
3339 * and need a lot of heuristics to figure out that the payload
3340 * might be;
3341 *
3342 * FIXME encapsulation specific BPF_ filters
3343 */
3344 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3345
a85e14b0
PA
3346 case DLT_IPNET:
3347 return gen_ipnet_linktype(proto);
3348
23fa89e6
PA
3349 case DLT_LINUX_IRDA:
3350 bpf_error("IrDA link-layer type filtering not implemented");
3351
3352 case DLT_DOCSIS:
3353 bpf_error("DOCSIS link-layer type filtering not implemented");
3354
de0d3203
PA
3355 case DLT_MTP2:
3356 case DLT_MTP2_WITH_PHDR:
3357 bpf_error("MTP2 link-layer type filtering not implemented");
3358
3359 case DLT_ERF:
3360 bpf_error("ERF link-layer type filtering not implemented");
3361
de0d3203
PA
3362 case DLT_PFSYNC:
3363 bpf_error("PFSYNC link-layer type filtering not implemented");
de0d3203 3364
23fa89e6
PA
3365 case DLT_LINUX_LAPD:
3366 bpf_error("LAPD link-layer type filtering not implemented");
de0d3203
PA
3367
3368 case DLT_USB:
3369 case DLT_USB_LINUX:
a85e14b0 3370 case DLT_USB_LINUX_MMAPPED:
de0d3203
PA
3371 bpf_error("USB link-layer type filtering not implemented");
3372
3373 case DLT_BLUETOOTH_HCI_H4:
3374 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3375 bpf_error("Bluetooth link-layer type filtering not implemented");
3376
3377 case DLT_CAN20B:
a85e14b0
PA
3378 case DLT_CAN_SOCKETCAN:
3379 bpf_error("CAN link-layer type filtering not implemented");
de0d3203
PA
3380
3381 case DLT_IEEE802_15_4:
3382 case DLT_IEEE802_15_4_LINUX:
3383 case DLT_IEEE802_15_4_NONASK_PHY:
a85e14b0 3384 case DLT_IEEE802_15_4_NOFCS:
de0d3203
PA
3385 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3386
3387 case DLT_IEEE802_16_MAC_CPS_RADIO:
3388 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3389
3390 case DLT_SITA:
3391 bpf_error("SITA link-layer type filtering not implemented");
3392
3393 case DLT_RAIF1:
3394 bpf_error("RAIF1 link-layer type filtering not implemented");
3395
3396 case DLT_IPMB:
3397 bpf_error("IPMB link-layer type filtering not implemented");
3398
3399 case DLT_AX25_KISS:
3400 bpf_error("AX.25 link-layer type filtering not implemented");
23fa89e6
PA
3401 }
3402
3403 /*
3404 * All the types that have no encapsulation should either be
3405 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3406 * all packets are IP packets, or should be handled in some
3407 * special case, if none of them are (if some are and some
3408 * aren't, the lack of encapsulation is a problem, as we'd
3409 * have to find some other way of determining the packet type).
3410 *
3411 * Therefore, if "off_linktype" is -1, there's an error.
3412 */
3413 if (off_linktype == (u_int)-1)
3414 abort();
3415
3416 /*
3417 * Any type not handled above should always have an Ethernet
de0d3203 3418 * type at an offset of "off_linktype".
23fa89e6
PA
3419 */
3420 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3421}
3422
3423/*
3424 * Check for an LLC SNAP packet with a given organization code and
3425 * protocol type; we check the entire contents of the 802.2 LLC and
3426 * snap headers, checking for DSAP and SSAP of SNAP and a control
3427 * field of 0x03 in the LLC header, and for the specified organization
3428 * code and protocol type in the SNAP header.
3429 */
3430static struct block *
de0d3203 3431gen_snap(orgcode, ptype)
23fa89e6
PA
3432 bpf_u_int32 orgcode;
3433 bpf_u_int32 ptype;
23fa89e6
PA
3434{
3435 u_char snapblock[8];
3436
3437 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3438 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3439 snapblock[2] = 0x03; /* control = UI */
3440 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3441 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3442 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3443 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3444 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
de0d3203 3445 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
23fa89e6
PA
3446}
3447
3448/*
3449 * Generate code to match a particular packet type, for link-layer types
3450 * using 802.2 LLC headers.
3451 *
3452 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3453 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3454 *
3455 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3456 * value, if <= ETHERMTU. We use that to determine whether to
3457 * match the DSAP or both DSAP and LSAP or to check the OUI and
3458 * protocol ID in a SNAP header.
3459 */
3460static struct block *
3461gen_llc_linktype(proto)
3462 int proto;
3463{
3464 /*
3465 * XXX - handle token-ring variable-length header.
3466 */
3467 switch (proto) {
3468
3469 case LLCSAP_IP:
3470 case LLCSAP_ISONS:
3471 case LLCSAP_NETBEUI:
3472 /*
3473 * XXX - should we check both the DSAP and the
3474 * SSAP, like this, or should we check just the
3475 * DSAP, as we do for other types <= ETHERMTU
3476 * (i.e., other SAP values)?
3477 */
de0d3203 3478 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
23fa89e6
PA
3479 ((proto << 8) | proto));
3480
3481 case LLCSAP_IPX:
3482 /*
3483 * XXX - are there ever SNAP frames for IPX on
3484 * non-Ethernet 802.x networks?
3485 */
de0d3203 3486 return gen_cmp(OR_MACPL, 0, BPF_B,
23fa89e6
PA
3487 (bpf_int32)LLCSAP_IPX);
3488
3489 case ETHERTYPE_ATALK:
3490 /*
3491 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3492 * SNAP packets with an organization code of
3493 * 0x080007 (Apple, for Appletalk) and a protocol
3494 * type of ETHERTYPE_ATALK (Appletalk).
3495 *
3496 * XXX - check for an organization code of
3497 * encapsulated Ethernet as well?
3498 */
de0d3203 3499 return gen_snap(0x080007, ETHERTYPE_ATALK);
23fa89e6
PA
3500
3501 default:
3502 /*
3503 * XXX - we don't have to check for IPX 802.3
3504 * here, but should we check for the IPX Ethertype?
3505 */
3506 if (proto <= ETHERMTU) {
3507 /*
3508 * This is an LLC SAP value, so check
3509 * the DSAP.
3510 */
de0d3203 3511 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
23fa89e6
PA
3512 } else {
3513 /*
3514 * This is an Ethernet type; we assume that it's
3515 * unlikely that it'll appear in the right place
3516 * at random, and therefore check only the
3517 * location that would hold the Ethernet type
3518 * in a SNAP frame with an organization code of
3519 * 0x000000 (encapsulated Ethernet).
3520 *
3521 * XXX - if we were to check for the SNAP DSAP and
3522 * LSAP, as per XXX, and were also to check for an
3523 * organization code of 0x000000 (encapsulated
3524 * Ethernet), we'd do
3525 *
de0d3203 3526 * return gen_snap(0x000000, proto);
23fa89e6
PA
3527 *
3528 * here; for now, we don't, as per the above.
3529 * I don't know whether it's worth the extra CPU
3530 * time to do the right check or not.
3531 */
de0d3203 3532 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
23fa89e6
PA
3533 }
3534 }
3535}
3536
3537static struct block *
3538gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3539 bpf_u_int32 addr;
3540 bpf_u_int32 mask;
3541 int dir, proto;
3542 u_int src_off, dst_off;
3543{
3544 struct block *b0, *b1;
3545 u_int offset;
3546
3547 switch (dir) {
3548
3549 case Q_SRC:
3550 offset = src_off;
3551 break;
3552
3553 case Q_DST:
3554 offset = dst_off;
3555 break;
3556
3557 case Q_AND:
3558 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3559 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3560 gen_and(b0, b1);
3561 return b1;
3562
3563 case Q_OR:
3564 case Q_DEFAULT:
3565 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3566 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3567 gen_or(b0, b1);
3568 return b1;
3569
3570 default:
3571 abort();
3572 }
3573 b0 = gen_linktype(proto);
3574 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3575 gen_and(b0, b1);
3576 return b1;
3577}
3578
3579#ifdef INET6
3580static struct block *
3581gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3582 struct in6_addr *addr;
3583 struct in6_addr *mask;
3584 int dir, proto;
3585 u_int src_off, dst_off;
3586{
3587 struct block *b0, *b1;
3588 u_int offset;
3589 u_int32_t *a, *m;
3590
3591 switch (dir) {
3592
3593 case Q_SRC:
3594 offset = src_off;
3595 break;
3596
3597 case Q_DST:
3598 offset = dst_off;
3599 break;
3600
3601 case Q_AND:
3602 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3603 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3604 gen_and(b0, b1);
3605 return b1;
3606
3607 case Q_OR:
3608 case Q_DEFAULT:
3609 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3610 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3611 gen_or(b0, b1);
3612 return b1;
3613
3614 default:
3615 abort();
3616 }
3617 /* this order is important */
3618 a = (u_int32_t *)addr;
3619 m = (u_int32_t *)mask;
3620 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3621 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3622 gen_and(b0, b1);
3623 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3624 gen_and(b0, b1);
3625 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3626 gen_and(b0, b1);
3627 b0 = gen_linktype(proto);
3628 gen_and(b0, b1);
3629 return b1;
3630}
3631#endif /*INET6*/
3632
3633static struct block *
3634gen_ehostop(eaddr, dir)
3635 register const u_char *eaddr;
3636 register int dir;
3637{
3638 register struct block *b0, *b1;
3639
3640 switch (dir) {
3641 case Q_SRC:
3642 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3643
3644 case Q_DST:
3645 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3646
3647 case Q_AND:
3648 b0 = gen_ehostop(eaddr, Q_SRC);
3649 b1 = gen_ehostop(eaddr, Q_DST);
3650 gen_and(b0, b1);
3651 return b1;
3652
3653 case Q_DEFAULT:
3654 case Q_OR:
3655 b0 = gen_ehostop(eaddr, Q_SRC);
3656 b1 = gen_ehostop(eaddr, Q_DST);
3657 gen_or(b0, b1);
3658 return b1;
a85e14b0
PA
3659
3660 case Q_ADDR1:
3661 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3662 break;
3663
3664 case Q_ADDR2:
3665 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3666 break;
3667
3668 case Q_ADDR3:
3669 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3670 break;
3671
3672 case Q_ADDR4:
3673 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3674 break;
3675
3676 case Q_RA:
3677 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3678 break;
3679
3680 case Q_TA:
3681 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3682 break;
23fa89e6
PA
3683 }
3684 abort();
3685 /* NOTREACHED */
3686}
3687
3688/*
3689 * Like gen_ehostop, but for DLT_FDDI
3690 */
3691static struct block *
3692gen_fhostop(eaddr, dir)
3693 register const u_char *eaddr;
3694 register int dir;
3695{
3696 struct block *b0, *b1;
3697
3698 switch (dir) {
3699 case Q_SRC:
3700#ifdef PCAP_FDDIPAD
3701 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3702#else
3703 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3704#endif
3705
3706 case Q_DST:
3707#ifdef PCAP_FDDIPAD
3708 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3709#else
3710 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3711#endif
3712
3713 case Q_AND:
3714 b0 = gen_fhostop(eaddr, Q_SRC);
3715 b1 = gen_fhostop(eaddr, Q_DST);
3716 gen_and(b0, b1);
3717 return b1;
3718
3719 case Q_DEFAULT:
3720 case Q_OR:
3721 b0 = gen_fhostop(eaddr, Q_SRC);
3722 b1 = gen_fhostop(eaddr, Q_DST);
3723 gen_or(b0, b1);
3724 return b1;
a85e14b0
PA
3725
3726 case Q_ADDR1:
3727 bpf_error("'addr1' is only supported on 802.11");
3728 break;
3729
3730 case Q_ADDR2:
3731 bpf_error("'addr2' is only supported on 802.11");
3732 break;
3733
3734 case Q_ADDR3:
3735 bpf_error("'addr3' is only supported on 802.11");
3736 break;
3737
3738 case Q_ADDR4:
3739 bpf_error("'addr4' is only supported on 802.11");
3740 break;
3741
3742 case Q_RA:
3743 bpf_error("'ra' is only supported on 802.11");
3744 break;
3745
3746 case Q_TA:
3747 bpf_error("'ta' is only supported on 802.11");
3748 break;
23fa89e6
PA
3749 }
3750 abort();
3751 /* NOTREACHED */
3752}
3753
3754/*
3755 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3756 */
3757static struct block *
3758gen_thostop(eaddr, dir)
3759 register const u_char *eaddr;
3760 register int dir;
3761{
3762 register struct block *b0, *b1;
3763
3764 switch (dir) {
3765 case Q_SRC:
3766 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3767
3768 case Q_DST:
3769 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3770
3771 case Q_AND:
3772 b0 = gen_thostop(eaddr, Q_SRC);
3773 b1 = gen_thostop(eaddr, Q_DST);
3774 gen_and(b0, b1);
3775 return b1;
3776
3777 case Q_DEFAULT:
3778 case Q_OR:
3779 b0 = gen_thostop(eaddr, Q_SRC);
3780 b1 = gen_thostop(eaddr, Q_DST);
3781 gen_or(b0, b1);
3782 return b1;
a85e14b0
PA
3783
3784 case Q_ADDR1:
3785 bpf_error("'addr1' is only supported on 802.11");
3786 break;
3787
3788 case Q_ADDR2:
3789 bpf_error("'addr2' is only supported on 802.11");
3790 break;
3791
3792 case Q_ADDR3:
3793 bpf_error("'addr3' is only supported on 802.11");
3794 break;
3795
3796 case Q_ADDR4:
3797 bpf_error("'addr4' is only supported on 802.11");
3798 break;
3799
3800 case Q_RA:
3801 bpf_error("'ra' is only supported on 802.11");
3802 break;
3803
3804 case Q_TA:
3805 bpf_error("'ta' is only supported on 802.11");
3806 break;
23fa89e6
PA
3807 }
3808 abort();
3809 /* NOTREACHED */
3810}
3811
3812/*
de0d3203
PA
3813 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3814 * various 802.11 + radio headers.
23fa89e6
PA
3815 */
3816static struct block *
3817gen_wlanhostop(eaddr, dir)
3818 register const u_char *eaddr;
3819 register int dir;
3820{
3821 register struct block *b0, *b1, *b2;
3822 register struct slist *s;
3823
de0d3203
PA
3824#ifdef ENABLE_WLAN_FILTERING_PATCH
3825 /*
3826 * TODO GV 20070613
3827 * We need to disable the optimizer because the optimizer is buggy
3828 * and wipes out some LD instructions generated by the below
3829 * code to validate the Frame Control bits
3830 */
3831 no_optimize = 1;
3832#endif /* ENABLE_WLAN_FILTERING_PATCH */
3833
23fa89e6
PA
3834 switch (dir) {
3835 case Q_SRC:
3836 /*
3837 * Oh, yuk.
3838 *
3839 * For control frames, there is no SA.
3840 *
3841 * For management frames, SA is at an
3842 * offset of 10 from the beginning of
3843 * the packet.
3844 *
3845 * For data frames, SA is at an offset
3846 * of 10 from the beginning of the packet
3847 * if From DS is clear, at an offset of
3848 * 16 from the beginning of the packet
3849 * if From DS is set and To DS is clear,
3850 * and an offset of 24 from the beginning
3851 * of the packet if From DS is set and To DS
3852 * is set.
3853 */
3854
3855 /*
3856 * Generate the tests to be done for data frames
3857 * with From DS set.
3858 *
3859 * First, check for To DS set, i.e. check "link[1] & 0x01".
3860 */
3861 s = gen_load_a(OR_LINK, 1, BPF_B);
3862 b1 = new_block(JMP(BPF_JSET));
3863 b1->s.k = 0x01; /* To DS */
3864 b1->stmts = s;
3865
3866 /*
3867 * If To DS is set, the SA is at 24.
3868 */
3869 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3870 gen_and(b1, b0);
3871
3872 /*
3873 * Now, check for To DS not set, i.e. check
3874 * "!(link[1] & 0x01)".
3875 */
3876 s = gen_load_a(OR_LINK, 1, BPF_B);
3877 b2 = new_block(JMP(BPF_JSET));
3878 b2->s.k = 0x01; /* To DS */
3879 b2->stmts = s;
3880 gen_not(b2);
3881
3882 /*
3883 * If To DS is not set, the SA is at 16.
3884 */
3885 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3886 gen_and(b2, b1);
3887
3888 /*
3889 * Now OR together the last two checks. That gives
3890 * the complete set of checks for data frames with
3891 * From DS set.
3892 */
3893 gen_or(b1, b0);
3894
3895 /*
3896 * Now check for From DS being set, and AND that with
3897 * the ORed-together checks.
3898 */
3899 s = gen_load_a(OR_LINK, 1, BPF_B);
3900 b1 = new_block(JMP(BPF_JSET));
3901 b1->s.k = 0x02; /* From DS */
3902 b1->stmts = s;
3903 gen_and(b1, b0);
3904
3905 /*
3906 * Now check for data frames with From DS not set.
3907 */
3908 s = gen_load_a(OR_LINK, 1, BPF_B);
3909 b2 = new_block(JMP(BPF_JSET));
3910 b2->s.k = 0x02; /* From DS */
3911 b2->stmts = s;
3912 gen_not(b2);
3913
3914 /*
3915 * If From DS isn't set, the SA is at 10.
3916 */
3917 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3918 gen_and(b2, b1);
3919
3920 /*
3921 * Now OR together the checks for data frames with
3922 * From DS not set and for data frames with From DS
3923 * set; that gives the checks done for data frames.
3924 */
3925 gen_or(b1, b0);
3926
3927 /*
3928 * Now check for a data frame.
3929 * I.e, check "link[0] & 0x08".
3930 */
de0d3203 3931 s = gen_load_a(OR_LINK, 0, BPF_B);
23fa89e6
PA
3932 b1 = new_block(JMP(BPF_JSET));
3933 b1->s.k = 0x08;
3934 b1->stmts = s;
3935
3936 /*
3937 * AND that with the checks done for data frames.
3938 */
3939 gen_and(b1, b0);
3940
3941 /*
3942 * If the high-order bit of the type value is 0, this
3943 * is a management frame.
3944 * I.e, check "!(link[0] & 0x08)".
3945 */
3946 s = gen_load_a(OR_LINK, 0, BPF_B);
3947 b2 = new_block(JMP(BPF_JSET));
3948 b2->s.k = 0x08;
3949 b2->stmts = s;
3950 gen_not(b2);
3951
3952 /*