Update files for libpcap-1.2.1 import.
[dragonfly.git] / contrib / libpcap / gencode.c
... / ...
CommitLineData
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_ =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.309 2008-12-23 20:13:29 guy Exp $ (LBL)";
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 */
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
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__
50#include "ip6_misc.h"
51#endif
52
53#ifndef WIN32
54
55#ifdef __NetBSD__
56#include <sys/param.h>
57#endif
58
59#include <netinet/in.h>
60#include <arpa/inet.h>
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"
80#include "ieee80211.h"
81#include "atmuni31.h"
82#include "sunatmpos.h"
83#include "ppp.h"
84#include "pcap/sll.h"
85#include "pcap/ipnet.h"
86#include "arcnet.h"
87#ifdef HAVE_NET_PFVAR_H
88#include <sys/socket.h>
89#include <net/if.h>
90#include <net/pf/pfvar.h>
91#include <net/pf/if_pflog.h>
92#endif
93#ifndef offsetof
94#define offsetof(s, e) ((size_t)&((s *)0)->e)
95#endif
96#ifdef INET6
97#ifndef WIN32
98#include <netdb.h> /* for "struct addrinfo" */
99#endif /* WIN32 */
100#endif /*INET6*/
101#include <pcap/namedb.h>
102
103#define ETHERMTU 1500
104
105#ifndef IPPROTO_SCTP
106#define IPPROTO_SCTP 132
107#endif
108
109#ifdef HAVE_OS_PROTO_H
110#include "os-proto.h"
111#endif
112
113#define JMP(c) ((c)|BPF_JMP|BPF_K)
114
115/* Locals */
116static jmp_buf top_ctx;
117static pcap_t *bpf_pcap;
118
119/* Hack for updating VLAN, MPLS, and PPPoE offsets. */
120#ifdef WIN32
121static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
122#else
123static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
124#endif
125
126/* XXX */
127#ifdef PCAP_FDDIPAD
128static int pcap_fddipad;
129#endif
130
131/* VARARGS */
132void
133bpf_error(const char *fmt, ...)
134{
135 va_list ap;
136
137 va_start(ap, fmt);
138 if (bpf_pcap != NULL)
139 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
140 fmt, ap);
141 va_end(ap);
142 longjmp(top_ctx, 1);
143 /* NOTREACHED */
144}
145
146static void init_linktype(pcap_t *);
147
148static void init_regs(void);
149static int alloc_reg(void);
150static void free_reg(int);
151
152static struct block *root;
153
154/*
155 * Value passed to gen_load_a() to indicate what the offset argument
156 * is relative to.
157 */
158enum e_offrel {
159 OR_PACKET, /* relative to the beginning of the packet */
160 OR_LINK, /* relative to the beginning of the link-layer header */
161 OR_MACPL, /* relative to the end of the MAC-layer header */
162 OR_NET, /* relative to the network-layer header */
163 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
164 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
165 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
166};
167
168#ifdef INET6
169/*
170 * As errors are handled by a longjmp, anything allocated must be freed
171 * in the longjmp handler, so it must be reachable from that handler.
172 * One thing that's allocated is the result of pcap_nametoaddrinfo();
173 * it must be freed with freeaddrinfo(). This variable points to any
174 * addrinfo structure that would need to be freed.
175 */
176static struct addrinfo *ai;
177#endif
178
179/*
180 * We divy out chunks of memory rather than call malloc each time so
181 * we don't have to worry about leaking memory. It's probably
182 * not a big deal if all this memory was wasted but if this ever
183 * goes into a library that would probably not be a good idea.
184 *
185 * XXX - this *is* in a library....
186 */
187#define NCHUNKS 16
188#define CHUNK0SIZE 1024
189struct chunk {
190 u_int n_left;
191 void *m;
192};
193
194static struct chunk chunks[NCHUNKS];
195static int cur_chunk;
196
197static void *newchunk(u_int);
198static void freechunks(void);
199static inline struct block *new_block(int);
200static inline struct slist *new_stmt(int);
201static struct block *gen_retblk(int);
202static inline void syntax(void);
203
204static void backpatch(struct block *, struct block *);
205static void merge(struct block *, struct block *);
206static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
207static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
208static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
209static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
210static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
211static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
212 bpf_u_int32);
213static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
214static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
215 bpf_u_int32, bpf_u_int32, int, bpf_int32);
216static struct slist *gen_load_llrel(u_int, u_int);
217static struct slist *gen_load_macplrel(u_int, u_int);
218static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
219static struct slist *gen_loadx_iphdrlen(void);
220static struct block *gen_uncond(int);
221static inline struct block *gen_true(void);
222static inline struct block *gen_false(void);
223static struct block *gen_ether_linktype(int);
224static struct block *gen_ipnet_linktype(int);
225static struct block *gen_linux_sll_linktype(int);
226static struct slist *gen_load_prism_llprefixlen(void);
227static struct slist *gen_load_avs_llprefixlen(void);
228static struct slist *gen_load_radiotap_llprefixlen(void);
229static struct slist *gen_load_ppi_llprefixlen(void);
230static void insert_compute_vloffsets(struct block *);
231static struct slist *gen_llprefixlen(void);
232static struct slist *gen_off_macpl(void);
233static int ethertype_to_ppptype(int);
234static struct block *gen_linktype(int);
235static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
236static struct block *gen_llc_linktype(int);
237static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
238#ifdef INET6
239static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
240#endif
241static struct block *gen_ahostop(const u_char *, int);
242static struct block *gen_ehostop(const u_char *, int);
243static struct block *gen_fhostop(const u_char *, int);
244static struct block *gen_thostop(const u_char *, int);
245static struct block *gen_wlanhostop(const u_char *, int);
246static struct block *gen_ipfchostop(const u_char *, int);
247static struct block *gen_dnhostop(bpf_u_int32, int);
248static struct block *gen_mpls_linktype(int);
249static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
250#ifdef INET6
251static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
252#endif
253#ifndef INET6
254static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
255#endif
256static struct block *gen_ipfrag(void);
257static struct block *gen_portatom(int, bpf_int32);
258static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
259#ifdef INET6
260static struct block *gen_portatom6(int, bpf_int32);
261static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
262#endif
263struct block *gen_portop(int, int, int);
264static struct block *gen_port(int, int, int);
265struct block *gen_portrangeop(int, int, int, int);
266static struct block *gen_portrange(int, int, int, int);
267#ifdef INET6
268struct block *gen_portop6(int, int, int);
269static struct block *gen_port6(int, int, int);
270struct block *gen_portrangeop6(int, int, int, int);
271static struct block *gen_portrange6(int, int, int, int);
272#endif
273static int lookup_proto(const char *, int);
274static struct block *gen_protochain(int, int, int);
275static struct block *gen_proto(int, int, int);
276static struct slist *xfer_to_x(struct arth *);
277static struct slist *xfer_to_a(struct arth *);
278static struct block *gen_mac_multicast(int);
279static struct block *gen_len(int, int);
280static struct block *gen_check_802_11_data_frame(void);
281
282static struct block *gen_ppi_dlt_check(void);
283static struct block *gen_msg_abbrev(int type);
284
285static void *
286newchunk(n)
287 u_int n;
288{
289 struct chunk *cp;
290 int k;
291 size_t size;
292
293#ifndef __NetBSD__
294 /* XXX Round up to nearest long. */
295 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
296#else
297 /* XXX Round up to structure boundary. */
298 n = ALIGN(n);
299#endif
300
301 cp = &chunks[cur_chunk];
302 if (n > cp->n_left) {
303 ++cp, k = ++cur_chunk;
304 if (k >= NCHUNKS)
305 bpf_error("out of memory");
306 size = CHUNK0SIZE << k;
307 cp->m = (void *)malloc(size);
308 if (cp->m == NULL)
309 bpf_error("out of memory");
310 memset((char *)cp->m, 0, size);
311 cp->n_left = size;
312 if (n > size)
313 bpf_error("out of memory");
314 }
315 cp->n_left -= n;
316 return (void *)((char *)cp->m + cp->n_left);
317}
318
319static void
320freechunks()
321{
322 int i;
323
324 cur_chunk = 0;
325 for (i = 0; i < NCHUNKS; ++i)
326 if (chunks[i].m != NULL) {
327 free(chunks[i].m);
328 chunks[i].m = NULL;
329 }
330}
331
332/*
333 * A strdup whose allocations are freed after code generation is over.
334 */
335char *
336sdup(s)
337 register const char *s;
338{
339 int n = strlen(s) + 1;
340 char *cp = newchunk(n);
341
342 strlcpy(cp, s, n);
343 return (cp);
344}
345
346static inline struct block *
347new_block(code)
348 int code;
349{
350 struct block *p;
351
352 p = (struct block *)newchunk(sizeof(*p));
353 p->s.code = code;
354 p->head = p;
355
356 return p;
357}
358
359static inline struct slist *
360new_stmt(code)
361 int code;
362{
363 struct slist *p;
364
365 p = (struct slist *)newchunk(sizeof(*p));
366 p->s.code = code;
367
368 return p;
369}
370
371static struct block *
372gen_retblk(v)
373 int v;
374{
375 struct block *b = new_block(BPF_RET|BPF_K);
376
377 b->s.k = v;
378 return b;
379}
380
381static inline void
382syntax()
383{
384 bpf_error("syntax error in filter expression");
385}
386
387static bpf_u_int32 netmask;
388static int snaplen;
389int no_optimize;
390#ifdef WIN32
391static int
392pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
393 const char *buf, int optimize, bpf_u_int32 mask);
394
395int
396pcap_compile(pcap_t *p, struct bpf_program *program,
397 const char *buf, int optimize, bpf_u_int32 mask)
398{
399 int result;
400
401 EnterCriticalSection(&g_PcapCompileCriticalSection);
402
403 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
404
405 LeaveCriticalSection(&g_PcapCompileCriticalSection);
406
407 return result;
408}
409
410static int
411pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
412 const char *buf, int optimize, bpf_u_int32 mask)
413#else /* WIN32 */
414int
415pcap_compile(pcap_t *p, struct bpf_program *program,
416 const char *buf, int optimize, bpf_u_int32 mask)
417#endif /* WIN32 */
418{
419 extern int n_errors;
420 const char * volatile xbuf = buf;
421 int len;
422
423 no_optimize = 0;
424 n_errors = 0;
425 root = NULL;
426 bpf_pcap = p;
427 init_regs();
428 if (setjmp(top_ctx)) {
429#ifdef INET6
430 if (ai != NULL) {
431 freeaddrinfo(ai);
432 ai = NULL;
433 }
434#endif
435 lex_cleanup();
436 freechunks();
437 return (-1);
438 }
439
440 netmask = mask;
441
442 snaplen = pcap_snapshot(p);
443 if (snaplen == 0) {
444 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
445 "snaplen of 0 rejects all packets");
446 return -1;
447 }
448
449 lex_init(xbuf ? xbuf : "");
450 init_linktype(p);
451 (void)pcap_parse();
452
453 if (n_errors)
454 syntax();
455
456 if (root == NULL)
457 root = gen_retblk(snaplen);
458
459 if (optimize && !no_optimize) {
460 bpf_optimize(&root);
461 if (root == NULL ||
462 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
463 bpf_error("expression rejects all packets");
464 }
465 program->bf_insns = icode_to_fcode(root, &len);
466 program->bf_len = len;
467
468 lex_cleanup();
469 freechunks();
470 return (0);
471}
472
473/*
474 * entry point for using the compiler with no pcap open
475 * pass in all the stuff that is needed explicitly instead.
476 */
477int
478pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
479 struct bpf_program *program,
480 const char *buf, int optimize, bpf_u_int32 mask)
481{
482 pcap_t *p;
483 int ret;
484
485 p = pcap_open_dead(linktype_arg, snaplen_arg);
486 if (p == NULL)
487 return (-1);
488 ret = pcap_compile(p, program, buf, optimize, mask);
489 pcap_close(p);
490 return (ret);
491}
492
493/*
494 * Clean up a "struct bpf_program" by freeing all the memory allocated
495 * in it.
496 */
497void
498pcap_freecode(struct bpf_program *program)
499{
500 program->bf_len = 0;
501 if (program->bf_insns != NULL) {
502 free((char *)program->bf_insns);
503 program->bf_insns = NULL;
504 }
505}
506
507/*
508 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
509 * which of the jt and jf fields has been resolved and which is a pointer
510 * back to another unresolved block (or nil). At least one of the fields
511 * in each block is already resolved.
512 */
513static void
514backpatch(list, target)
515 struct block *list, *target;
516{
517 struct block *next;
518
519 while (list) {
520 if (!list->sense) {
521 next = JT(list);
522 JT(list) = target;
523 } else {
524 next = JF(list);
525 JF(list) = target;
526 }
527 list = next;
528 }
529}
530
531/*
532 * Merge the lists in b0 and b1, using the 'sense' field to indicate
533 * which of jt and jf is the link.
534 */
535static void
536merge(b0, b1)
537 struct block *b0, *b1;
538{
539 register struct block **p = &b0;
540
541 /* Find end of list. */
542 while (*p)
543 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
544
545 /* Concatenate the lists. */
546 *p = b1;
547}
548
549void
550finish_parse(p)
551 struct block *p;
552{
553 struct block *ppi_dlt_check;
554
555 /*
556 * Insert before the statements of the first (root) block any
557 * statements needed to load the lengths of any variable-length
558 * headers into registers.
559 *
560 * XXX - a fancier strategy would be to insert those before the
561 * statements of all blocks that use those lengths and that
562 * have no predecessors that use them, so that we only compute
563 * the lengths if we need them. There might be even better
564 * approaches than that.
565 *
566 * However, those strategies would be more complicated, and
567 * as we don't generate code to compute a length if the
568 * program has no tests that use the length, and as most
569 * tests will probably use those lengths, we would just
570 * postpone computing the lengths so that it's not done
571 * for tests that fail early, and it's not clear that's
572 * worth the effort.
573 */
574 insert_compute_vloffsets(p->head);
575
576 /*
577 * For DLT_PPI captures, generate a check of the per-packet
578 * DLT value to make sure it's DLT_IEEE802_11.
579 */
580 ppi_dlt_check = gen_ppi_dlt_check();
581 if (ppi_dlt_check != NULL)
582 gen_and(ppi_dlt_check, p);
583
584 backpatch(p, gen_retblk(snaplen));
585 p->sense = !p->sense;
586 backpatch(p, gen_retblk(0));
587 root = p->head;
588}
589
590void
591gen_and(b0, b1)
592 struct block *b0, *b1;
593{
594 backpatch(b0, b1->head);
595 b0->sense = !b0->sense;
596 b1->sense = !b1->sense;
597 merge(b1, b0);
598 b1->sense = !b1->sense;
599 b1->head = b0->head;
600}
601
602void
603gen_or(b0, b1)
604 struct block *b0, *b1;
605{
606 b0->sense = !b0->sense;
607 backpatch(b0, b1->head);
608 b0->sense = !b0->sense;
609 merge(b1, b0);
610 b1->head = b0->head;
611}
612
613void
614gen_not(b)
615 struct block *b;
616{
617 b->sense = !b->sense;
618}
619
620static struct block *
621gen_cmp(offrel, offset, size, v)
622 enum e_offrel offrel;
623 u_int offset, size;
624 bpf_int32 v;
625{
626 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
627}
628
629static struct block *
630gen_cmp_gt(offrel, offset, size, v)
631 enum e_offrel offrel;
632 u_int offset, size;
633 bpf_int32 v;
634{
635 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
636}
637
638static struct block *
639gen_cmp_ge(offrel, offset, size, v)
640 enum e_offrel offrel;
641 u_int offset, size;
642 bpf_int32 v;
643{
644 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
645}
646
647static struct block *
648gen_cmp_lt(offrel, offset, size, v)
649 enum e_offrel offrel;
650 u_int offset, size;
651 bpf_int32 v;
652{
653 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
654}
655
656static struct block *
657gen_cmp_le(offrel, offset, size, v)
658 enum e_offrel offrel;
659 u_int offset, size;
660 bpf_int32 v;
661{
662 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
663}
664
665static struct block *
666gen_mcmp(offrel, offset, size, v, mask)
667 enum e_offrel offrel;
668 u_int offset, size;
669 bpf_int32 v;
670 bpf_u_int32 mask;
671{
672 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
673}
674
675static struct block *
676gen_bcmp(offrel, offset, size, v)
677 enum e_offrel offrel;
678 register u_int offset, size;
679 register const u_char *v;
680{
681 register struct block *b, *tmp;
682
683 b = NULL;
684 while (size >= 4) {
685 register const u_char *p = &v[size - 4];
686 bpf_int32 w = ((bpf_int32)p[0] << 24) |
687 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
688
689 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
690 if (b != NULL)
691 gen_and(b, tmp);
692 b = tmp;
693 size -= 4;
694 }
695 while (size >= 2) {
696 register const u_char *p = &v[size - 2];
697 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
698
699 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
700 if (b != NULL)
701 gen_and(b, tmp);
702 b = tmp;
703 size -= 2;
704 }
705 if (size > 0) {
706 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
707 if (b != NULL)
708 gen_and(b, tmp);
709 b = tmp;
710 }
711 return b;
712}
713
714/*
715 * AND the field of size "size" at offset "offset" relative to the header
716 * specified by "offrel" with "mask", and compare it with the value "v"
717 * with the test specified by "jtype"; if "reverse" is true, the test
718 * should test the opposite of "jtype".
719 */
720static struct block *
721gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
722 enum e_offrel offrel;
723 bpf_int32 v;
724 bpf_u_int32 offset, size, mask, jtype;
725 int reverse;
726{
727 struct slist *s, *s2;
728 struct block *b;
729
730 s = gen_load_a(offrel, offset, size);
731
732 if (mask != 0xffffffff) {
733 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
734 s2->s.k = mask;
735 sappend(s, s2);
736 }
737
738 b = new_block(JMP(jtype));
739 b->stmts = s;
740 b->s.k = v;
741 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
742 gen_not(b);
743 return b;
744}
745
746/*
747 * Various code constructs need to know the layout of the data link
748 * layer. These variables give the necessary offsets from the beginning
749 * of the packet data.
750 */
751
752/*
753 * This is the offset of the beginning of the link-layer header from
754 * the beginning of the raw packet data.
755 *
756 * It's usually 0, except for 802.11 with a fixed-length radio header.
757 * (For 802.11 with a variable-length radio header, we have to generate
758 * code to compute that offset; off_ll is 0 in that case.)
759 */
760static u_int off_ll;
761
762/*
763 * If there's a variable-length header preceding the link-layer header,
764 * "reg_off_ll" is the register number for a register containing the
765 * length of that header, and therefore the offset of the link-layer
766 * header from the beginning of the raw packet data. Otherwise,
767 * "reg_off_ll" is -1.
768 */
769static int reg_off_ll;
770
771/*
772 * This is the offset of the beginning of the MAC-layer header from
773 * the beginning of the link-layer header.
774 * It's usually 0, except for ATM LANE, where it's the offset, relative
775 * to the beginning of the raw packet data, of the Ethernet header, and
776 * for Ethernet with various additional information.
777 */
778static u_int off_mac;
779
780/*
781 * This is the offset of the beginning of the MAC-layer payload,
782 * from the beginning of the raw packet data.
783 *
784 * I.e., it's the sum of the length of the link-layer header (without,
785 * for example, any 802.2 LLC header, so it's the MAC-layer
786 * portion of that header), plus any prefix preceding the
787 * link-layer header.
788 */
789static u_int off_macpl;
790
791/*
792 * This is 1 if the offset of the beginning of the MAC-layer payload
793 * from the beginning of the link-layer header is variable-length.
794 */
795static int off_macpl_is_variable;
796
797/*
798 * If the link layer has variable_length headers, "reg_off_macpl"
799 * is the register number for a register containing the length of the
800 * link-layer header plus the length of any variable-length header
801 * preceding the link-layer header. Otherwise, "reg_off_macpl"
802 * is -1.
803 */
804static int reg_off_macpl;
805
806/*
807 * "off_linktype" is the offset to information in the link-layer header
808 * giving the packet type. This offset is relative to the beginning
809 * of the link-layer header (i.e., it doesn't include off_ll).
810 *
811 * For Ethernet, it's the offset of the Ethernet type field.
812 *
813 * For link-layer types that always use 802.2 headers, it's the
814 * offset of the LLC header.
815 *
816 * For PPP, it's the offset of the PPP type field.
817 *
818 * For Cisco HDLC, it's the offset of the CHDLC type field.
819 *
820 * For BSD loopback, it's the offset of the AF_ value.
821 *
822 * For Linux cooked sockets, it's the offset of the type field.
823 *
824 * It's set to -1 for no encapsulation, in which case, IP is assumed.
825 */
826static u_int off_linktype;
827
828/*
829 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
830 * checks to check the PPP header, assumed to follow a LAN-style link-
831 * layer header and a PPPoE session header.
832 */
833static int is_pppoes = 0;
834
835/*
836 * TRUE if the link layer includes an ATM pseudo-header.
837 */
838static int is_atm = 0;
839
840/*
841 * TRUE if "lane" appeared in the filter; it causes us to generate
842 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
843 */
844static int is_lane = 0;
845
846/*
847 * These are offsets for the ATM pseudo-header.
848 */
849static u_int off_vpi;
850static u_int off_vci;
851static u_int off_proto;
852
853/*
854 * These are offsets for the MTP2 fields.
855 */
856static u_int off_li;
857
858/*
859 * These are offsets for the MTP3 fields.
860 */
861static u_int off_sio;
862static u_int off_opc;
863static u_int off_dpc;
864static u_int off_sls;
865
866/*
867 * This is the offset of the first byte after the ATM pseudo_header,
868 * or -1 if there is no ATM pseudo-header.
869 */
870static u_int off_payload;
871
872/*
873 * These are offsets to the beginning of the network-layer header.
874 * They are relative to the beginning of the MAC-layer payload (i.e.,
875 * they don't include off_ll or off_macpl).
876 *
877 * If the link layer never uses 802.2 LLC:
878 *
879 * "off_nl" and "off_nl_nosnap" are the same.
880 *
881 * If the link layer always uses 802.2 LLC:
882 *
883 * "off_nl" is the offset if there's a SNAP header following
884 * the 802.2 header;
885 *
886 * "off_nl_nosnap" is the offset if there's no SNAP header.
887 *
888 * If the link layer is Ethernet:
889 *
890 * "off_nl" is the offset if the packet is an Ethernet II packet
891 * (we assume no 802.3+802.2+SNAP);
892 *
893 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
894 * with an 802.2 header following it.
895 */
896static u_int off_nl;
897static u_int off_nl_nosnap;
898
899static int linktype;
900
901static void
902init_linktype(p)
903 pcap_t *p;
904{
905 linktype = pcap_datalink(p);
906#ifdef PCAP_FDDIPAD
907 pcap_fddipad = p->fddipad;
908#endif
909
910 /*
911 * Assume it's not raw ATM with a pseudo-header, for now.
912 */
913 off_mac = 0;
914 is_atm = 0;
915 is_lane = 0;
916 off_vpi = -1;
917 off_vci = -1;
918 off_proto = -1;
919 off_payload = -1;
920
921 /*
922 * And that we're not doing PPPoE.
923 */
924 is_pppoes = 0;
925
926 /*
927 * And assume we're not doing SS7.
928 */
929 off_li = -1;
930 off_sio = -1;
931 off_opc = -1;
932 off_dpc = -1;
933 off_sls = -1;
934
935 /*
936 * Also assume it's not 802.11.
937 */
938 off_ll = 0;
939 off_macpl = 0;
940 off_macpl_is_variable = 0;
941
942 orig_linktype = -1;
943 orig_nl = -1;
944 label_stack_depth = 0;
945
946 reg_off_ll = -1;
947 reg_off_macpl = -1;
948
949 switch (linktype) {
950
951 case DLT_ARCNET:
952 off_linktype = 2;
953 off_macpl = 6;
954 off_nl = 0; /* XXX in reality, variable! */
955 off_nl_nosnap = 0; /* no 802.2 LLC */
956 return;
957
958 case DLT_ARCNET_LINUX:
959 off_linktype = 4;
960 off_macpl = 8;
961 off_nl = 0; /* XXX in reality, variable! */
962 off_nl_nosnap = 0; /* no 802.2 LLC */
963 return;
964
965 case DLT_EN10MB:
966 off_linktype = 12;
967 off_macpl = 14; /* Ethernet header length */
968 off_nl = 0; /* Ethernet II */
969 off_nl_nosnap = 3; /* 802.3+802.2 */
970 return;
971
972 case DLT_SLIP:
973 /*
974 * SLIP doesn't have a link level type. The 16 byte
975 * header is hacked into our SLIP driver.
976 */
977 off_linktype = -1;
978 off_macpl = 16;
979 off_nl = 0;
980 off_nl_nosnap = 0; /* no 802.2 LLC */
981 return;
982
983 case DLT_SLIP_BSDOS:
984 /* XXX this may be the same as the DLT_PPP_BSDOS case */
985 off_linktype = -1;
986 /* XXX end */
987 off_macpl = 24;
988 off_nl = 0;
989 off_nl_nosnap = 0; /* no 802.2 LLC */
990 return;
991
992 case DLT_NULL:
993 case DLT_LOOP:
994 off_linktype = 0;
995 off_macpl = 4;
996 off_nl = 0;
997 off_nl_nosnap = 0; /* no 802.2 LLC */
998 return;
999
1000 case DLT_ENC:
1001 off_linktype = 0;
1002 off_macpl = 12;
1003 off_nl = 0;
1004 off_nl_nosnap = 0; /* no 802.2 LLC */
1005 return;
1006
1007 case DLT_PPP:
1008 case DLT_PPP_PPPD:
1009 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1010 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1011 off_linktype = 2;
1012 off_macpl = 4;
1013 off_nl = 0;
1014 off_nl_nosnap = 0; /* no 802.2 LLC */
1015 return;
1016
1017 case DLT_PPP_ETHER:
1018 /*
1019 * This does no include the Ethernet header, and
1020 * only covers session state.
1021 */
1022 off_linktype = 6;
1023 off_macpl = 8;
1024 off_nl = 0;
1025 off_nl_nosnap = 0; /* no 802.2 LLC */
1026 return;
1027
1028 case DLT_PPP_BSDOS:
1029 off_linktype = 5;
1030 off_macpl = 24;
1031 off_nl = 0;
1032 off_nl_nosnap = 0; /* no 802.2 LLC */
1033 return;
1034
1035 case DLT_FDDI:
1036 /*
1037 * FDDI doesn't really have a link-level type field.
1038 * We set "off_linktype" to the offset of the LLC header.
1039 *
1040 * To check for Ethernet types, we assume that SSAP = SNAP
1041 * is being used and pick out the encapsulated Ethernet type.
1042 * XXX - should we generate code to check for SNAP?
1043 */
1044 off_linktype = 13;
1045#ifdef PCAP_FDDIPAD
1046 off_linktype += pcap_fddipad;
1047#endif
1048 off_macpl = 13; /* FDDI MAC header length */
1049#ifdef PCAP_FDDIPAD
1050 off_macpl += pcap_fddipad;
1051#endif
1052 off_nl = 8; /* 802.2+SNAP */
1053 off_nl_nosnap = 3; /* 802.2 */
1054 return;
1055
1056 case DLT_IEEE802:
1057 /*
1058 * Token Ring doesn't really have a link-level type field.
1059 * We set "off_linktype" to the offset of the LLC header.
1060 *
1061 * To check for Ethernet types, we assume that SSAP = SNAP
1062 * is being used and pick out the encapsulated Ethernet type.
1063 * XXX - should we generate code to check for SNAP?
1064 *
1065 * XXX - the header is actually variable-length.
1066 * Some various Linux patched versions gave 38
1067 * as "off_linktype" and 40 as "off_nl"; however,
1068 * if a token ring packet has *no* routing
1069 * information, i.e. is not source-routed, the correct
1070 * values are 20 and 22, as they are in the vanilla code.
1071 *
1072 * A packet is source-routed iff the uppermost bit
1073 * of the first byte of the source address, at an
1074 * offset of 8, has the uppermost bit set. If the
1075 * packet is source-routed, the total number of bytes
1076 * of routing information is 2 plus bits 0x1F00 of
1077 * the 16-bit value at an offset of 14 (shifted right
1078 * 8 - figure out which byte that is).
1079 */
1080 off_linktype = 14;
1081 off_macpl = 14; /* Token Ring MAC header length */
1082 off_nl = 8; /* 802.2+SNAP */
1083 off_nl_nosnap = 3; /* 802.2 */
1084 return;
1085
1086 case DLT_IEEE802_11:
1087 case DLT_PRISM_HEADER:
1088 case DLT_IEEE802_11_RADIO_AVS:
1089 case DLT_IEEE802_11_RADIO:
1090 /*
1091 * 802.11 doesn't really have a link-level type field.
1092 * We set "off_linktype" to the offset of the LLC header.
1093 *
1094 * To check for Ethernet types, we assume that SSAP = SNAP
1095 * is being used and pick out the encapsulated Ethernet type.
1096 * XXX - should we generate code to check for SNAP?
1097 *
1098 * We also handle variable-length radio headers here.
1099 * The Prism header is in theory variable-length, but in
1100 * practice it's always 144 bytes long. However, some
1101 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1102 * sometimes or always supply an AVS header, so we
1103 * have to check whether the radio header is a Prism
1104 * header or an AVS header, so, in practice, it's
1105 * variable-length.
1106 */
1107 off_linktype = 24;
1108 off_macpl = 0; /* link-layer header is variable-length */
1109 off_macpl_is_variable = 1;
1110 off_nl = 8; /* 802.2+SNAP */
1111 off_nl_nosnap = 3; /* 802.2 */
1112 return;
1113
1114 case DLT_PPI:
1115 /*
1116 * At the moment we treat PPI the same way that we treat
1117 * normal Radiotap encoded packets. The difference is in
1118 * the function that generates the code at the beginning
1119 * to compute the header length. Since this code generator
1120 * of PPI supports bare 802.11 encapsulation only (i.e.
1121 * the encapsulated DLT should be DLT_IEEE802_11) we
1122 * generate code to check for this too.
1123 */
1124 off_linktype = 24;
1125 off_macpl = 0; /* link-layer header is variable-length */
1126 off_macpl_is_variable = 1;
1127 off_nl = 8; /* 802.2+SNAP */
1128 off_nl_nosnap = 3; /* 802.2 */
1129 return;
1130
1131 case DLT_ATM_RFC1483:
1132 case DLT_ATM_CLIP: /* Linux ATM defines this */
1133 /*
1134 * assume routed, non-ISO PDUs
1135 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1136 *
1137 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1138 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1139 * latter would presumably be treated the way PPPoE
1140 * should be, so you can do "pppoe and udp port 2049"
1141 * or "pppoa and tcp port 80" and have it check for
1142 * PPPo{A,E} and a PPP protocol of IP and....
1143 */
1144 off_linktype = 0;
1145 off_macpl = 0; /* packet begins with LLC header */
1146 off_nl = 8; /* 802.2+SNAP */
1147 off_nl_nosnap = 3; /* 802.2 */
1148 return;
1149
1150 case DLT_SUNATM:
1151 /*
1152 * Full Frontal ATM; you get AALn PDUs with an ATM
1153 * pseudo-header.
1154 */
1155 is_atm = 1;
1156 off_vpi = SUNATM_VPI_POS;
1157 off_vci = SUNATM_VCI_POS;
1158 off_proto = PROTO_POS;
1159 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1160 off_payload = SUNATM_PKT_BEGIN_POS;
1161 off_linktype = off_payload;
1162 off_macpl = off_payload; /* if LLC-encapsulated */
1163 off_nl = 8; /* 802.2+SNAP */
1164 off_nl_nosnap = 3; /* 802.2 */
1165 return;
1166
1167 case DLT_RAW:
1168 case DLT_IPV4:
1169 case DLT_IPV6:
1170 off_linktype = -1;
1171 off_macpl = 0;
1172 off_nl = 0;
1173 off_nl_nosnap = 0; /* no 802.2 LLC */
1174 return;
1175
1176 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1177 off_linktype = 14;
1178 off_macpl = 16;
1179 off_nl = 0;
1180 off_nl_nosnap = 0; /* no 802.2 LLC */
1181 return;
1182
1183 case DLT_LTALK:
1184 /*
1185 * LocalTalk does have a 1-byte type field in the LLAP header,
1186 * but really it just indicates whether there is a "short" or
1187 * "long" DDP packet following.
1188 */
1189 off_linktype = -1;
1190 off_macpl = 0;
1191 off_nl = 0;
1192 off_nl_nosnap = 0; /* no 802.2 LLC */
1193 return;
1194
1195 case DLT_IP_OVER_FC:
1196 /*
1197 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1198 * link-level type field. We set "off_linktype" to the
1199 * offset of the LLC header.
1200 *
1201 * To check for Ethernet types, we assume that SSAP = SNAP
1202 * is being used and pick out the encapsulated Ethernet type.
1203 * XXX - should we generate code to check for SNAP? RFC
1204 * 2625 says SNAP should be used.
1205 */
1206 off_linktype = 16;
1207 off_macpl = 16;
1208 off_nl = 8; /* 802.2+SNAP */
1209 off_nl_nosnap = 3; /* 802.2 */
1210 return;
1211
1212 case DLT_FRELAY:
1213 /*
1214 * XXX - we should set this to handle SNAP-encapsulated
1215 * frames (NLPID of 0x80).
1216 */
1217 off_linktype = -1;
1218 off_macpl = 0;
1219 off_nl = 0;
1220 off_nl_nosnap = 0; /* no 802.2 LLC */
1221 return;
1222
1223 /*
1224 * the only BPF-interesting FRF.16 frames are non-control frames;
1225 * Frame Relay has a variable length link-layer
1226 * so lets start with offset 4 for now and increments later on (FIXME);
1227 */
1228 case DLT_MFR:
1229 off_linktype = -1;
1230 off_macpl = 0;
1231 off_nl = 4;
1232 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1233 return;
1234
1235 case DLT_APPLE_IP_OVER_IEEE1394:
1236 off_linktype = 16;
1237 off_macpl = 18;
1238 off_nl = 0;
1239 off_nl_nosnap = 0; /* no 802.2 LLC */
1240 return;
1241
1242 case DLT_SYMANTEC_FIREWALL:
1243 off_linktype = 6;
1244 off_macpl = 44;
1245 off_nl = 0; /* Ethernet II */
1246 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1247 return;
1248
1249#ifdef HAVE_NET_PFVAR_H
1250 case DLT_PFLOG:
1251 off_linktype = 0;
1252 off_macpl = PFLOG_HDRLEN;
1253 off_nl = 0;
1254 off_nl_nosnap = 0; /* no 802.2 LLC */
1255 return;
1256#endif
1257
1258 case DLT_JUNIPER_MFR:
1259 case DLT_JUNIPER_MLFR:
1260 case DLT_JUNIPER_MLPPP:
1261 case DLT_JUNIPER_PPP:
1262 case DLT_JUNIPER_CHDLC:
1263 case DLT_JUNIPER_FRELAY:
1264 off_linktype = 4;
1265 off_macpl = 4;
1266 off_nl = 0;
1267 off_nl_nosnap = -1; /* no 802.2 LLC */
1268 return;
1269
1270 case DLT_JUNIPER_ATM1:
1271 off_linktype = 4; /* in reality variable between 4-8 */
1272 off_macpl = 4; /* in reality variable between 4-8 */
1273 off_nl = 0;
1274 off_nl_nosnap = 10;
1275 return;
1276
1277 case DLT_JUNIPER_ATM2:
1278 off_linktype = 8; /* in reality variable between 8-12 */
1279 off_macpl = 8; /* in reality variable between 8-12 */
1280 off_nl = 0;
1281 off_nl_nosnap = 10;
1282 return;
1283
1284 /* frames captured on a Juniper PPPoE service PIC
1285 * contain raw ethernet frames */
1286 case DLT_JUNIPER_PPPOE:
1287 case DLT_JUNIPER_ETHER:
1288 off_macpl = 14;
1289 off_linktype = 16;
1290 off_nl = 18; /* Ethernet II */
1291 off_nl_nosnap = 21; /* 802.3+802.2 */
1292 return;
1293
1294 case DLT_JUNIPER_PPPOE_ATM:
1295 off_linktype = 4;
1296 off_macpl = 6;
1297 off_nl = 0;
1298 off_nl_nosnap = -1; /* no 802.2 LLC */
1299 return;
1300
1301 case DLT_JUNIPER_GGSN:
1302 off_linktype = 6;
1303 off_macpl = 12;
1304 off_nl = 0;
1305 off_nl_nosnap = -1; /* no 802.2 LLC */
1306 return;
1307
1308 case DLT_JUNIPER_ES:
1309 off_linktype = 6;
1310 off_macpl = -1; /* not really a network layer but raw IP addresses */
1311 off_nl = -1; /* not really a network layer but raw IP addresses */
1312 off_nl_nosnap = -1; /* no 802.2 LLC */
1313 return;
1314
1315 case DLT_JUNIPER_MONITOR:
1316 off_linktype = 12;
1317 off_macpl = 12;
1318 off_nl = 0; /* raw IP/IP6 header */
1319 off_nl_nosnap = -1; /* no 802.2 LLC */
1320 return;
1321
1322 case DLT_JUNIPER_SERVICES:
1323 off_linktype = 12;
1324 off_macpl = -1; /* L3 proto location dep. on cookie type */
1325 off_nl = -1; /* L3 proto location dep. on cookie type */
1326 off_nl_nosnap = -1; /* no 802.2 LLC */
1327 return;
1328
1329 case DLT_JUNIPER_VP:
1330 off_linktype = 18;
1331 off_macpl = -1;
1332 off_nl = -1;
1333 off_nl_nosnap = -1;
1334 return;
1335
1336 case DLT_JUNIPER_ST:
1337 off_linktype = 18;
1338 off_macpl = -1;
1339 off_nl = -1;
1340 off_nl_nosnap = -1;
1341 return;
1342
1343 case DLT_JUNIPER_ISM:
1344 off_linktype = 8;
1345 off_macpl = -1;
1346 off_nl = -1;
1347 off_nl_nosnap = -1;
1348 return;
1349
1350 case DLT_JUNIPER_VS:
1351 case DLT_JUNIPER_SRX_E2E:
1352 case DLT_JUNIPER_FIBRECHANNEL:
1353 case DLT_JUNIPER_ATM_CEMIC:
1354 off_linktype = 8;
1355 off_macpl = -1;
1356 off_nl = -1;
1357 off_nl_nosnap = -1;
1358 return;
1359
1360 case DLT_MTP2:
1361 off_li = 2;
1362 off_sio = 3;
1363 off_opc = 4;
1364 off_dpc = 4;
1365 off_sls = 7;
1366 off_linktype = -1;
1367 off_macpl = -1;
1368 off_nl = -1;
1369 off_nl_nosnap = -1;
1370 return;
1371
1372 case DLT_MTP2_WITH_PHDR:
1373 off_li = 6;
1374 off_sio = 7;
1375 off_opc = 8;
1376 off_dpc = 8;
1377 off_sls = 11;
1378 off_linktype = -1;
1379 off_macpl = -1;
1380 off_nl = -1;
1381 off_nl_nosnap = -1;
1382 return;
1383
1384 case DLT_ERF:
1385 off_li = 22;
1386 off_sio = 23;
1387 off_opc = 24;
1388 off_dpc = 24;
1389 off_sls = 27;
1390 off_linktype = -1;
1391 off_macpl = -1;
1392 off_nl = -1;
1393 off_nl_nosnap = -1;
1394 return;
1395
1396#ifdef DLT_PFSYNC
1397 case DLT_PFSYNC:
1398 off_linktype = -1;
1399 off_macpl = 4;
1400 off_nl = 0;
1401 off_nl_nosnap = 0;
1402 return;
1403#endif
1404
1405 case DLT_AX25_KISS:
1406 /*
1407 * Currently, only raw "link[N:M]" filtering is supported.
1408 */
1409 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1410 off_macpl = -1;
1411 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1412 off_nl_nosnap = -1; /* no 802.2 LLC */
1413 off_mac = 1; /* step over the kiss length byte */
1414 return;
1415
1416 case DLT_IPNET:
1417 off_linktype = 1;
1418 off_macpl = 24; /* ipnet header length */
1419 off_nl = 0;
1420 off_nl_nosnap = -1;
1421 return;
1422
1423 case DLT_NETANALYZER:
1424 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1425 off_linktype = 16; /* includes 4-byte pseudo-header */
1426 off_macpl = 18; /* pseudo-header+Ethernet header length */
1427 off_nl = 0; /* Ethernet II */
1428 off_nl_nosnap = 3; /* 802.3+802.2 */
1429 return;
1430
1431 case DLT_NETANALYZER_TRANSPARENT:
1432 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1433 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1434 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1435 off_nl = 0; /* Ethernet II */
1436 off_nl_nosnap = 3; /* 802.3+802.2 */
1437 return;
1438
1439 default:
1440 /*
1441 * For values in the range in which we've assigned new
1442 * DLT_ values, only raw "link[N:M]" filtering is supported.
1443 */
1444 if (linktype >= DLT_MATCHING_MIN &&
1445 linktype <= DLT_MATCHING_MAX) {
1446 off_linktype = -1;
1447 off_macpl = -1;
1448 off_nl = -1;
1449 off_nl_nosnap = -1;
1450 return;
1451 }
1452
1453 }
1454 bpf_error("unknown data link type %d", linktype);
1455 /* NOTREACHED */
1456}
1457
1458/*
1459 * Load a value relative to the beginning of the link-layer header.
1460 * The link-layer header doesn't necessarily begin at the beginning
1461 * of the packet data; there might be a variable-length prefix containing
1462 * radio information.
1463 */
1464static struct slist *
1465gen_load_llrel(offset, size)
1466 u_int offset, size;
1467{
1468 struct slist *s, *s2;
1469
1470 s = gen_llprefixlen();
1471
1472 /*
1473 * If "s" is non-null, it has code to arrange that the X register
1474 * contains the length of the prefix preceding the link-layer
1475 * header.
1476 *
1477 * Otherwise, the length of the prefix preceding the link-layer
1478 * header is "off_ll".
1479 */
1480 if (s != NULL) {
1481 /*
1482 * There's a variable-length prefix preceding the
1483 * link-layer header. "s" points to a list of statements
1484 * that put the length of that prefix into the X register.
1485 * do an indirect load, to use the X register as an offset.
1486 */
1487 s2 = new_stmt(BPF_LD|BPF_IND|size);
1488 s2->s.k = offset;
1489 sappend(s, s2);
1490 } else {
1491 /*
1492 * There is no variable-length header preceding the
1493 * link-layer header; add in off_ll, which, if there's
1494 * a fixed-length header preceding the link-layer header,
1495 * is the length of that header.
1496 */
1497 s = new_stmt(BPF_LD|BPF_ABS|size);
1498 s->s.k = offset + off_ll;
1499 }
1500 return s;
1501}
1502
1503/*
1504 * Load a value relative to the beginning of the MAC-layer payload.
1505 */
1506static struct slist *
1507gen_load_macplrel(offset, size)
1508 u_int offset, size;
1509{
1510 struct slist *s, *s2;
1511
1512 s = gen_off_macpl();
1513
1514 /*
1515 * If s is non-null, the offset of the MAC-layer payload is
1516 * variable, and s points to a list of instructions that
1517 * arrange that the X register contains that offset.
1518 *
1519 * Otherwise, the offset of the MAC-layer payload is constant,
1520 * and is in off_macpl.
1521 */
1522 if (s != NULL) {
1523 /*
1524 * The offset of the MAC-layer payload is in the X
1525 * register. Do an indirect load, to use the X register
1526 * as an offset.
1527 */
1528 s2 = new_stmt(BPF_LD|BPF_IND|size);
1529 s2->s.k = offset;
1530 sappend(s, s2);
1531 } else {
1532 /*
1533 * The offset of the MAC-layer payload is constant,
1534 * and is in off_macpl; load the value at that offset
1535 * plus the specified offset.
1536 */
1537 s = new_stmt(BPF_LD|BPF_ABS|size);
1538 s->s.k = off_macpl + offset;
1539 }
1540 return s;
1541}
1542
1543/*
1544 * Load a value relative to the beginning of the specified header.
1545 */
1546static struct slist *
1547gen_load_a(offrel, offset, size)
1548 enum e_offrel offrel;
1549 u_int offset, size;
1550{
1551 struct slist *s, *s2;
1552
1553 switch (offrel) {
1554
1555 case OR_PACKET:
1556 s = new_stmt(BPF_LD|BPF_ABS|size);
1557 s->s.k = offset;
1558 break;
1559
1560 case OR_LINK:
1561 s = gen_load_llrel(offset, size);
1562 break;
1563
1564 case OR_MACPL:
1565 s = gen_load_macplrel(offset, size);
1566 break;
1567
1568 case OR_NET:
1569 s = gen_load_macplrel(off_nl + offset, size);
1570 break;
1571
1572 case OR_NET_NOSNAP:
1573 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1574 break;
1575
1576 case OR_TRAN_IPV4:
1577 /*
1578 * Load the X register with the length of the IPv4 header
1579 * (plus the offset of the link-layer header, if it's
1580 * preceded by a variable-length header such as a radio
1581 * header), in bytes.
1582 */
1583 s = gen_loadx_iphdrlen();
1584
1585 /*
1586 * Load the item at {offset of the MAC-layer payload} +
1587 * {offset, relative to the start of the MAC-layer
1588 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1589 * {specified offset}.
1590 *
1591 * (If the offset of the MAC-layer payload is variable,
1592 * it's included in the value in the X register, and
1593 * off_macpl is 0.)
1594 */
1595 s2 = new_stmt(BPF_LD|BPF_IND|size);
1596 s2->s.k = off_macpl + off_nl + offset;
1597 sappend(s, s2);
1598 break;
1599
1600 case OR_TRAN_IPV6:
1601 s = gen_load_macplrel(off_nl + 40 + offset, size);
1602 break;
1603
1604 default:
1605 abort();
1606 return NULL;
1607 }
1608 return s;
1609}
1610
1611/*
1612 * Generate code to load into the X register the sum of the length of
1613 * the IPv4 header and any variable-length header preceding the link-layer
1614 * header.
1615 */
1616static struct slist *
1617gen_loadx_iphdrlen()
1618{
1619 struct slist *s, *s2;
1620
1621 s = gen_off_macpl();
1622 if (s != NULL) {
1623 /*
1624 * There's a variable-length prefix preceding the
1625 * link-layer header, or the link-layer header is itself
1626 * variable-length. "s" points to a list of statements
1627 * that put the offset of the MAC-layer payload into
1628 * the X register.
1629 *
1630 * The 4*([k]&0xf) addressing mode can't be used, as we
1631 * don't have a constant offset, so we have to load the
1632 * value in question into the A register and add to it
1633 * the value from the X register.
1634 */
1635 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1636 s2->s.k = off_nl;
1637 sappend(s, s2);
1638 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1639 s2->s.k = 0xf;
1640 sappend(s, s2);
1641 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1642 s2->s.k = 2;
1643 sappend(s, s2);
1644
1645 /*
1646 * The A register now contains the length of the
1647 * IP header. We need to add to it the offset of
1648 * the MAC-layer payload, which is still in the X
1649 * register, and move the result into the X register.
1650 */
1651 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1652 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1653 } else {
1654 /*
1655 * There is no variable-length header preceding the
1656 * link-layer header, and the link-layer header is
1657 * fixed-length; load the length of the IPv4 header,
1658 * which is at an offset of off_nl from the beginning
1659 * of the MAC-layer payload, and thus at an offset
1660 * of off_mac_pl + off_nl from the beginning of the
1661 * raw packet data.
1662 */
1663 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1664 s->s.k = off_macpl + off_nl;
1665 }
1666 return s;
1667}
1668
1669static struct block *
1670gen_uncond(rsense)
1671 int rsense;
1672{
1673 struct block *b;
1674 struct slist *s;
1675
1676 s = new_stmt(BPF_LD|BPF_IMM);
1677 s->s.k = !rsense;
1678 b = new_block(JMP(BPF_JEQ));
1679 b->stmts = s;
1680
1681 return b;
1682}
1683
1684static inline struct block *
1685gen_true()
1686{
1687 return gen_uncond(1);
1688}
1689
1690static inline struct block *
1691gen_false()
1692{
1693 return gen_uncond(0);
1694}
1695
1696/*
1697 * Byte-swap a 32-bit number.
1698 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1699 * big-endian platforms.)
1700 */
1701#define SWAPLONG(y) \
1702((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1703
1704/*
1705 * Generate code to match a particular packet type.
1706 *
1707 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1708 * value, if <= ETHERMTU. We use that to determine whether to
1709 * match the type/length field or to check the type/length field for
1710 * a value <= ETHERMTU to see whether it's a type field and then do
1711 * the appropriate test.
1712 */
1713static struct block *
1714gen_ether_linktype(proto)
1715 register int proto;
1716{
1717 struct block *b0, *b1;
1718
1719 switch (proto) {
1720
1721 case LLCSAP_ISONS:
1722 case LLCSAP_IP:
1723 case LLCSAP_NETBEUI:
1724 /*
1725 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1726 * so we check the DSAP and SSAP.
1727 *
1728 * LLCSAP_IP checks for IP-over-802.2, rather
1729 * than IP-over-Ethernet or IP-over-SNAP.
1730 *
1731 * XXX - should we check both the DSAP and the
1732 * SSAP, like this, or should we check just the
1733 * DSAP, as we do for other types <= ETHERMTU
1734 * (i.e., other SAP values)?
1735 */
1736 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1737 gen_not(b0);
1738 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1739 ((proto << 8) | proto));
1740 gen_and(b0, b1);
1741 return b1;
1742
1743 case LLCSAP_IPX:
1744 /*
1745 * Check for;
1746 *
1747 * Ethernet_II frames, which are Ethernet
1748 * frames with a frame type of ETHERTYPE_IPX;
1749 *
1750 * Ethernet_802.3 frames, which are 802.3
1751 * frames (i.e., the type/length field is
1752 * a length field, <= ETHERMTU, rather than
1753 * a type field) with the first two bytes
1754 * after the Ethernet/802.3 header being
1755 * 0xFFFF;
1756 *
1757 * Ethernet_802.2 frames, which are 802.3
1758 * frames with an 802.2 LLC header and
1759 * with the IPX LSAP as the DSAP in the LLC
1760 * header;
1761 *
1762 * Ethernet_SNAP frames, which are 802.3
1763 * frames with an LLC header and a SNAP
1764 * header and with an OUI of 0x000000
1765 * (encapsulated Ethernet) and a protocol
1766 * ID of ETHERTYPE_IPX in the SNAP header.
1767 *
1768 * XXX - should we generate the same code both
1769 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1770 */
1771
1772 /*
1773 * This generates code to check both for the
1774 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1775 */
1776 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1777 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1778 gen_or(b0, b1);
1779
1780 /*
1781 * Now we add code to check for SNAP frames with
1782 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1783 */
1784 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1785 gen_or(b0, b1);
1786
1787 /*
1788 * Now we generate code to check for 802.3
1789 * frames in general.
1790 */
1791 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1792 gen_not(b0);
1793
1794 /*
1795 * Now add the check for 802.3 frames before the
1796 * check for Ethernet_802.2 and Ethernet_802.3,
1797 * as those checks should only be done on 802.3
1798 * frames, not on Ethernet frames.
1799 */
1800 gen_and(b0, b1);
1801
1802 /*
1803 * Now add the check for Ethernet_II frames, and
1804 * do that before checking for the other frame
1805 * types.
1806 */
1807 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1808 (bpf_int32)ETHERTYPE_IPX);
1809 gen_or(b0, b1);
1810 return b1;
1811
1812 case ETHERTYPE_ATALK:
1813 case ETHERTYPE_AARP:
1814 /*
1815 * EtherTalk (AppleTalk protocols on Ethernet link
1816 * layer) may use 802.2 encapsulation.
1817 */
1818
1819 /*
1820 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1821 * we check for an Ethernet type field less than
1822 * 1500, which means it's an 802.3 length field.
1823 */
1824 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1825 gen_not(b0);
1826
1827 /*
1828 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1829 * SNAP packets with an organization code of
1830 * 0x080007 (Apple, for Appletalk) and a protocol
1831 * type of ETHERTYPE_ATALK (Appletalk).
1832 *
1833 * 802.2-encapsulated ETHERTYPE_AARP packets are
1834 * SNAP packets with an organization code of
1835 * 0x000000 (encapsulated Ethernet) and a protocol
1836 * type of ETHERTYPE_AARP (Appletalk ARP).
1837 */
1838 if (proto == ETHERTYPE_ATALK)
1839 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1840 else /* proto == ETHERTYPE_AARP */
1841 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1842 gen_and(b0, b1);
1843
1844 /*
1845 * Check for Ethernet encapsulation (Ethertalk
1846 * phase 1?); we just check for the Ethernet
1847 * protocol type.
1848 */
1849 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1850
1851 gen_or(b0, b1);
1852 return b1;
1853
1854 default:
1855 if (proto <= ETHERMTU) {
1856 /*
1857 * This is an LLC SAP value, so the frames
1858 * that match would be 802.2 frames.
1859 * Check that the frame is an 802.2 frame
1860 * (i.e., that the length/type field is
1861 * a length field, <= ETHERMTU) and
1862 * then check the DSAP.
1863 */
1864 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1865 gen_not(b0);
1866 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1867 (bpf_int32)proto);
1868 gen_and(b0, b1);
1869 return b1;
1870 } else {
1871 /*
1872 * This is an Ethernet type, so compare
1873 * the length/type field with it (if
1874 * the frame is an 802.2 frame, the length
1875 * field will be <= ETHERMTU, and, as
1876 * "proto" is > ETHERMTU, this test
1877 * will fail and the frame won't match,
1878 * which is what we want).
1879 */
1880 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1881 (bpf_int32)proto);
1882 }
1883 }
1884}
1885
1886/*
1887 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1888 * or IPv6 then we have an error.
1889 */
1890static struct block *
1891gen_ipnet_linktype(proto)
1892 register int proto;
1893{
1894 switch (proto) {
1895
1896 case ETHERTYPE_IP:
1897 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1898 (bpf_int32)IPH_AF_INET);
1899 /* NOTREACHED */
1900
1901 case ETHERTYPE_IPV6:
1902 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1903 (bpf_int32)IPH_AF_INET6);
1904 /* NOTREACHED */
1905
1906 default:
1907 break;
1908 }
1909
1910 return gen_false();
1911}
1912
1913/*
1914 * Generate code to match a particular packet type.
1915 *
1916 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1917 * value, if <= ETHERMTU. We use that to determine whether to
1918 * match the type field or to check the type field for the special
1919 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1920 */
1921static struct block *
1922gen_linux_sll_linktype(proto)
1923 register int proto;
1924{
1925 struct block *b0, *b1;
1926
1927 switch (proto) {
1928
1929 case LLCSAP_ISONS:
1930 case LLCSAP_IP:
1931 case LLCSAP_NETBEUI:
1932 /*
1933 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1934 * so we check the DSAP and SSAP.
1935 *
1936 * LLCSAP_IP checks for IP-over-802.2, rather
1937 * than IP-over-Ethernet or IP-over-SNAP.
1938 *
1939 * XXX - should we check both the DSAP and the
1940 * SSAP, like this, or should we check just the
1941 * DSAP, as we do for other types <= ETHERMTU
1942 * (i.e., other SAP values)?
1943 */
1944 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1945 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1946 ((proto << 8) | proto));
1947 gen_and(b0, b1);
1948 return b1;
1949
1950 case LLCSAP_IPX:
1951 /*
1952 * Ethernet_II frames, which are Ethernet
1953 * frames with a frame type of ETHERTYPE_IPX;
1954 *
1955 * Ethernet_802.3 frames, which have a frame
1956 * type of LINUX_SLL_P_802_3;
1957 *
1958 * Ethernet_802.2 frames, which are 802.3
1959 * frames with an 802.2 LLC header (i.e, have
1960 * a frame type of LINUX_SLL_P_802_2) and
1961 * with the IPX LSAP as the DSAP in the LLC
1962 * header;
1963 *
1964 * Ethernet_SNAP frames, which are 802.3
1965 * frames with an LLC header and a SNAP
1966 * header and with an OUI of 0x000000
1967 * (encapsulated Ethernet) and a protocol
1968 * ID of ETHERTYPE_IPX in the SNAP header.
1969 *
1970 * First, do the checks on LINUX_SLL_P_802_2
1971 * frames; generate the check for either
1972 * Ethernet_802.2 or Ethernet_SNAP frames, and
1973 * then put a check for LINUX_SLL_P_802_2 frames
1974 * before it.
1975 */
1976 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1977 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
1978 gen_or(b0, b1);
1979 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1980 gen_and(b0, b1);
1981
1982 /*
1983 * Now check for 802.3 frames and OR that with
1984 * the previous test.
1985 */
1986 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1987 gen_or(b0, b1);
1988
1989 /*
1990 * Now add the check for Ethernet_II frames, and
1991 * do that before checking for the other frame
1992 * types.
1993 */
1994 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1995 (bpf_int32)ETHERTYPE_IPX);
1996 gen_or(b0, b1);
1997 return b1;
1998
1999 case ETHERTYPE_ATALK:
2000 case ETHERTYPE_AARP:
2001 /*
2002 * EtherTalk (AppleTalk protocols on Ethernet link
2003 * layer) may use 802.2 encapsulation.
2004 */
2005
2006 /*
2007 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2008 * we check for the 802.2 protocol type in the
2009 * "Ethernet type" field.
2010 */
2011 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2012
2013 /*
2014 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2015 * SNAP packets with an organization code of
2016 * 0x080007 (Apple, for Appletalk) and a protocol
2017 * type of ETHERTYPE_ATALK (Appletalk).
2018 *
2019 * 802.2-encapsulated ETHERTYPE_AARP packets are
2020 * SNAP packets with an organization code of
2021 * 0x000000 (encapsulated Ethernet) and a protocol
2022 * type of ETHERTYPE_AARP (Appletalk ARP).
2023 */
2024 if (proto == ETHERTYPE_ATALK)
2025 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2026 else /* proto == ETHERTYPE_AARP */
2027 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2028 gen_and(b0, b1);
2029
2030 /*
2031 * Check for Ethernet encapsulation (Ethertalk
2032 * phase 1?); we just check for the Ethernet
2033 * protocol type.
2034 */
2035 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2036
2037 gen_or(b0, b1);
2038 return b1;
2039
2040 default:
2041 if (proto <= ETHERMTU) {
2042 /*
2043 * This is an LLC SAP value, so the frames
2044 * that match would be 802.2 frames.
2045 * Check for the 802.2 protocol type
2046 * in the "Ethernet type" field, and
2047 * then check the DSAP.
2048 */
2049 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2050 LINUX_SLL_P_802_2);
2051 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2052 (bpf_int32)proto);
2053 gen_and(b0, b1);
2054 return b1;
2055 } else {
2056 /*
2057 * This is an Ethernet type, so compare
2058 * the length/type field with it (if
2059 * the frame is an 802.2 frame, the length
2060 * field will be <= ETHERMTU, and, as
2061 * "proto" is > ETHERMTU, this test
2062 * will fail and the frame won't match,
2063 * which is what we want).
2064 */
2065 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2066 (bpf_int32)proto);
2067 }
2068 }
2069}
2070
2071static struct slist *
2072gen_load_prism_llprefixlen()
2073{
2074 struct slist *s1, *s2;
2075 struct slist *sjeq_avs_cookie;
2076 struct slist *sjcommon;
2077
2078 /*
2079 * This code is not compatible with the optimizer, as
2080 * we are generating jmp instructions within a normal
2081 * slist of instructions
2082 */
2083 no_optimize = 1;
2084
2085 /*
2086 * Generate code to load the length of the radio header into
2087 * the register assigned to hold that length, if one has been
2088 * assigned. (If one hasn't been assigned, no code we've
2089 * generated uses that prefix, so we don't need to generate any
2090 * code to load it.)
2091 *
2092 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2093 * or always use the AVS header rather than the Prism header.
2094 * We load a 4-byte big-endian value at the beginning of the
2095 * raw packet data, and see whether, when masked with 0xFFFFF000,
2096 * it's equal to 0x80211000. If so, that indicates that it's
2097 * an AVS header (the masked-out bits are the version number).
2098 * Otherwise, it's a Prism header.
2099 *
2100 * XXX - the Prism header is also, in theory, variable-length,
2101 * but no known software generates headers that aren't 144
2102 * bytes long.
2103 */
2104 if (reg_off_ll != -1) {
2105 /*
2106 * Load the cookie.
2107 */
2108 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2109 s1->s.k = 0;
2110
2111 /*
2112 * AND it with 0xFFFFF000.
2113 */
2114 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2115 s2->s.k = 0xFFFFF000;
2116 sappend(s1, s2);
2117
2118 /*
2119 * Compare with 0x80211000.
2120 */
2121 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2122 sjeq_avs_cookie->s.k = 0x80211000;
2123 sappend(s1, sjeq_avs_cookie);
2124
2125 /*
2126 * If it's AVS:
2127 *
2128 * The 4 bytes at an offset of 4 from the beginning of
2129 * the AVS header are the length of the AVS header.
2130 * That field is big-endian.
2131 */
2132 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2133 s2->s.k = 4;
2134 sappend(s1, s2);
2135 sjeq_avs_cookie->s.jt = s2;
2136
2137 /*
2138 * Now jump to the code to allocate a register
2139 * into which to save the header length and
2140 * store the length there. (The "jump always"
2141 * instruction needs to have the k field set;
2142 * it's added to the PC, so, as we're jumping
2143 * over a single instruction, it should be 1.)
2144 */
2145 sjcommon = new_stmt(JMP(BPF_JA));
2146 sjcommon->s.k = 1;
2147 sappend(s1, sjcommon);
2148
2149 /*
2150 * Now for the code that handles the Prism header.
2151 * Just load the length of the Prism header (144)
2152 * into the A register. Have the test for an AVS
2153 * header branch here if we don't have an AVS header.
2154 */
2155 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2156 s2->s.k = 144;
2157 sappend(s1, s2);
2158 sjeq_avs_cookie->s.jf = s2;
2159
2160 /*
2161 * Now allocate a register to hold that value and store
2162 * it. The code for the AVS header will jump here after
2163 * loading the length of the AVS header.
2164 */
2165 s2 = new_stmt(BPF_ST);
2166 s2->s.k = reg_off_ll;
2167 sappend(s1, s2);
2168 sjcommon->s.jf = s2;
2169
2170 /*
2171 * Now move it into the X register.
2172 */
2173 s2 = new_stmt(BPF_MISC|BPF_TAX);
2174 sappend(s1, s2);
2175
2176 return (s1);
2177 } else
2178 return (NULL);
2179}
2180
2181static struct slist *
2182gen_load_avs_llprefixlen()
2183{
2184 struct slist *s1, *s2;
2185
2186 /*
2187 * Generate code to load the length of the AVS header into
2188 * the register assigned to hold that length, if one has been
2189 * assigned. (If one hasn't been assigned, no code we've
2190 * generated uses that prefix, so we don't need to generate any
2191 * code to load it.)
2192 */
2193 if (reg_off_ll != -1) {
2194 /*
2195 * The 4 bytes at an offset of 4 from the beginning of
2196 * the AVS header are the length of the AVS header.
2197 * That field is big-endian.
2198 */
2199 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2200 s1->s.k = 4;
2201
2202 /*
2203 * Now allocate a register to hold that value and store
2204 * it.
2205 */
2206 s2 = new_stmt(BPF_ST);
2207 s2->s.k = reg_off_ll;
2208 sappend(s1, s2);
2209
2210 /*
2211 * Now move it into the X register.
2212 */
2213 s2 = new_stmt(BPF_MISC|BPF_TAX);
2214 sappend(s1, s2);
2215
2216 return (s1);
2217 } else
2218 return (NULL);
2219}
2220
2221static struct slist *
2222gen_load_radiotap_llprefixlen()
2223{
2224 struct slist *s1, *s2;
2225
2226 /*
2227 * Generate code to load the length of the radiotap header into
2228 * the register assigned to hold that length, if one has been
2229 * assigned. (If one hasn't been assigned, no code we've
2230 * generated uses that prefix, so we don't need to generate any
2231 * code to load it.)
2232 */
2233 if (reg_off_ll != -1) {
2234 /*
2235 * The 2 bytes at offsets of 2 and 3 from the beginning
2236 * of the radiotap header are the length of the radiotap
2237 * header; unfortunately, it's little-endian, so we have
2238 * to load it a byte at a time and construct the value.
2239 */
2240
2241 /*
2242 * Load the high-order byte, at an offset of 3, shift it
2243 * left a byte, and put the result in the X register.
2244 */
2245 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2246 s1->s.k = 3;
2247 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2248 sappend(s1, s2);
2249 s2->s.k = 8;
2250 s2 = new_stmt(BPF_MISC|BPF_TAX);
2251 sappend(s1, s2);
2252
2253 /*
2254 * Load the next byte, at an offset of 2, and OR the
2255 * value from the X register into it.
2256 */
2257 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2258 sappend(s1, s2);
2259 s2->s.k = 2;
2260 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2261 sappend(s1, s2);
2262
2263 /*
2264 * Now allocate a register to hold that value and store
2265 * it.
2266 */
2267 s2 = new_stmt(BPF_ST);
2268 s2->s.k = reg_off_ll;
2269 sappend(s1, s2);
2270
2271 /*
2272 * Now move it into the X register.
2273 */
2274 s2 = new_stmt(BPF_MISC|BPF_TAX);
2275 sappend(s1, s2);
2276
2277 return (s1);
2278 } else
2279 return (NULL);
2280}
2281
2282/*
2283 * At the moment we treat PPI as normal Radiotap encoded
2284 * packets. The difference is in the function that generates
2285 * the code at the beginning to compute the header length.
2286 * Since this code generator of PPI supports bare 802.11
2287 * encapsulation only (i.e. the encapsulated DLT should be
2288 * DLT_IEEE802_11) we generate code to check for this too;
2289 * that's done in finish_parse().
2290 */
2291static struct slist *
2292gen_load_ppi_llprefixlen()
2293{
2294 struct slist *s1, *s2;
2295
2296 /*
2297 * Generate code to load the length of the radiotap header
2298 * into the register assigned to hold that length, if one has
2299 * been assigned.
2300 */
2301 if (reg_off_ll != -1) {
2302 /*
2303 * The 2 bytes at offsets of 2 and 3 from the beginning
2304 * of the radiotap header are the length of the radiotap
2305 * header; unfortunately, it's little-endian, so we have
2306 * to load it a byte at a time and construct the value.
2307 */
2308
2309 /*
2310 * Load the high-order byte, at an offset of 3, shift it
2311 * left a byte, and put the result in the X register.
2312 */
2313 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2314 s1->s.k = 3;
2315 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2316 sappend(s1, s2);
2317 s2->s.k = 8;
2318 s2 = new_stmt(BPF_MISC|BPF_TAX);
2319 sappend(s1, s2);
2320
2321 /*
2322 * Load the next byte, at an offset of 2, and OR the
2323 * value from the X register into it.
2324 */
2325 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2326 sappend(s1, s2);
2327 s2->s.k = 2;
2328 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2329 sappend(s1, s2);
2330
2331 /*
2332 * Now allocate a register to hold that value and store
2333 * it.
2334 */
2335 s2 = new_stmt(BPF_ST);
2336 s2->s.k = reg_off_ll;
2337 sappend(s1, s2);
2338
2339 /*
2340 * Now move it into the X register.
2341 */
2342 s2 = new_stmt(BPF_MISC|BPF_TAX);
2343 sappend(s1, s2);
2344
2345 return (s1);
2346 } else
2347 return (NULL);
2348}
2349
2350/*
2351 * Load a value relative to the beginning of the link-layer header after the 802.11
2352 * header, i.e. LLC_SNAP.
2353 * The link-layer header doesn't necessarily begin at the beginning
2354 * of the packet data; there might be a variable-length prefix containing
2355 * radio information.
2356 */
2357static struct slist *
2358gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2359{
2360 struct slist *s2;
2361 struct slist *sjset_data_frame_1;
2362 struct slist *sjset_data_frame_2;
2363 struct slist *sjset_qos;
2364 struct slist *sjset_radiotap_flags;
2365 struct slist *sjset_radiotap_tsft;
2366 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2367 struct slist *s_roundup;
2368
2369 if (reg_off_macpl == -1) {
2370 /*
2371 * No register has been assigned to the offset of
2372 * the MAC-layer payload, which means nobody needs
2373 * it; don't bother computing it - just return
2374 * what we already have.
2375 */
2376 return (s);
2377 }
2378
2379 /*
2380 * This code is not compatible with the optimizer, as
2381 * we are generating jmp instructions within a normal
2382 * slist of instructions
2383 */
2384 no_optimize = 1;
2385
2386 /*
2387 * If "s" is non-null, it has code to arrange that the X register
2388 * contains the length of the prefix preceding the link-layer
2389 * header.
2390 *
2391 * Otherwise, the length of the prefix preceding the link-layer
2392 * header is "off_ll".
2393 */
2394 if (s == NULL) {
2395 /*
2396 * There is no variable-length header preceding the
2397 * link-layer header.
2398 *
2399 * Load the length of the fixed-length prefix preceding
2400 * the link-layer header (if any) into the X register,
2401 * and store it in the reg_off_macpl register.
2402 * That length is off_ll.
2403 */
2404 s = new_stmt(BPF_LDX|BPF_IMM);
2405 s->s.k = off_ll;
2406 }
2407
2408 /*
2409 * The X register contains the offset of the beginning of the
2410 * link-layer header; add 24, which is the minimum length
2411 * of the MAC header for a data frame, to that, and store it
2412 * in reg_off_macpl, and then load the Frame Control field,
2413 * which is at the offset in the X register, with an indexed load.
2414 */
2415 s2 = new_stmt(BPF_MISC|BPF_TXA);
2416 sappend(s, s2);
2417 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2418 s2->s.k = 24;
2419 sappend(s, s2);
2420 s2 = new_stmt(BPF_ST);
2421 s2->s.k = reg_off_macpl;
2422 sappend(s, s2);
2423
2424 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2425 s2->s.k = 0;
2426 sappend(s, s2);
2427
2428 /*
2429 * Check the Frame Control field to see if this is a data frame;
2430 * a data frame has the 0x08 bit (b3) in that field set and the
2431 * 0x04 bit (b2) clear.
2432 */
2433 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2434 sjset_data_frame_1->s.k = 0x08;
2435 sappend(s, sjset_data_frame_1);
2436
2437 /*
2438 * If b3 is set, test b2, otherwise go to the first statement of
2439 * the rest of the program.
2440 */
2441 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2442 sjset_data_frame_2->s.k = 0x04;
2443 sappend(s, sjset_data_frame_2);
2444 sjset_data_frame_1->s.jf = snext;
2445
2446 /*
2447 * If b2 is not set, this is a data frame; test the QoS bit.
2448 * Otherwise, go to the first statement of the rest of the
2449 * program.
2450 */
2451 sjset_data_frame_2->s.jt = snext;
2452 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2453 sjset_qos->s.k = 0x80; /* QoS bit */
2454 sappend(s, sjset_qos);
2455
2456 /*
2457 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2458 * field.
2459 * Otherwise, go to the first statement of the rest of the
2460 * program.
2461 */
2462 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2463 s2->s.k = reg_off_macpl;
2464 sappend(s, s2);
2465 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2466 s2->s.k = 2;
2467 sappend(s, s2);
2468 s2 = new_stmt(BPF_ST);
2469 s2->s.k = reg_off_macpl;
2470 sappend(s, s2);
2471
2472 /*
2473 * If we have a radiotap header, look at it to see whether
2474 * there's Atheros padding between the MAC-layer header
2475 * and the payload.
2476 *
2477 * Note: all of the fields in the radiotap header are
2478 * little-endian, so we byte-swap all of the values
2479 * we test against, as they will be loaded as big-endian
2480 * values.
2481 */
2482 if (linktype == DLT_IEEE802_11_RADIO) {
2483 /*
2484 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2485 * in the presence flag?
2486 */
2487 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2488 s2->s.k = 4;
2489 sappend(s, s2);
2490
2491 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2492 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2493 sappend(s, sjset_radiotap_flags);
2494
2495 /*
2496 * If not, skip all of this.
2497 */
2498 sjset_radiotap_flags->s.jf = snext;
2499
2500 /*
2501 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2502 */
2503 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2504 new_stmt(JMP(BPF_JSET));
2505 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2506 sappend(s, sjset_radiotap_tsft);
2507
2508 /*
2509 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2510 * at an offset of 16 from the beginning of the raw packet
2511 * data (8 bytes for the radiotap header and 8 bytes for
2512 * the TSFT field).
2513 *
2514 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2515 * is set.
2516 */
2517 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2518 s2->s.k = 16;
2519 sappend(s, s2);
2520
2521 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2522 sjset_tsft_datapad->s.k = 0x20;
2523 sappend(s, sjset_tsft_datapad);
2524
2525 /*
2526 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2527 * at an offset of 8 from the beginning of the raw packet
2528 * data (8 bytes for the radiotap header).
2529 *
2530 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2531 * is set.
2532 */
2533 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2534 s2->s.k = 8;
2535 sappend(s, s2);
2536
2537 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2538 sjset_notsft_datapad->s.k = 0x20;
2539 sappend(s, sjset_notsft_datapad);
2540
2541 /*
2542 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2543 * set, round the length of the 802.11 header to
2544 * a multiple of 4. Do that by adding 3 and then
2545 * dividing by and multiplying by 4, which we do by
2546 * ANDing with ~3.
2547 */
2548 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2549 s_roundup->s.k = reg_off_macpl;
2550 sappend(s, s_roundup);
2551 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2552 s2->s.k = 3;
2553 sappend(s, s2);
2554 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2555 s2->s.k = ~3;
2556 sappend(s, s2);
2557 s2 = new_stmt(BPF_ST);
2558 s2->s.k = reg_off_macpl;
2559 sappend(s, s2);
2560
2561 sjset_tsft_datapad->s.jt = s_roundup;
2562 sjset_tsft_datapad->s.jf = snext;
2563 sjset_notsft_datapad->s.jt = s_roundup;
2564 sjset_notsft_datapad->s.jf = snext;
2565 } else
2566 sjset_qos->s.jf = snext;
2567
2568 return s;
2569}
2570
2571static void
2572insert_compute_vloffsets(b)
2573 struct block *b;
2574{
2575 struct slist *s;
2576
2577 /*
2578 * For link-layer types that have a variable-length header
2579 * preceding the link-layer header, generate code to load
2580 * the offset of the link-layer header into the register
2581 * assigned to that offset, if any.
2582 */
2583 switch (linktype) {
2584
2585 case DLT_PRISM_HEADER:
2586 s = gen_load_prism_llprefixlen();
2587 break;
2588
2589 case DLT_IEEE802_11_RADIO_AVS:
2590 s = gen_load_avs_llprefixlen();
2591 break;
2592
2593 case DLT_IEEE802_11_RADIO:
2594 s = gen_load_radiotap_llprefixlen();
2595 break;
2596
2597 case DLT_PPI:
2598 s = gen_load_ppi_llprefixlen();
2599 break;
2600
2601 default:
2602 s = NULL;
2603 break;
2604 }
2605
2606 /*
2607 * For link-layer types that have a variable-length link-layer
2608 * header, generate code to load the offset of the MAC-layer
2609 * payload into the register assigned to that offset, if any.
2610 */
2611 switch (linktype) {
2612
2613 case DLT_IEEE802_11:
2614 case DLT_PRISM_HEADER:
2615 case DLT_IEEE802_11_RADIO_AVS:
2616 case DLT_IEEE802_11_RADIO:
2617 case DLT_PPI:
2618 s = gen_load_802_11_header_len(s, b->stmts);
2619 break;
2620 }
2621
2622 /*
2623 * If we have any offset-loading code, append all the
2624 * existing statements in the block to those statements,
2625 * and make the resulting list the list of statements
2626 * for the block.
2627 */
2628 if (s != NULL) {
2629 sappend(s, b->stmts);
2630 b->stmts = s;
2631 }
2632}
2633
2634static struct block *
2635gen_ppi_dlt_check(void)
2636{
2637 struct slist *s_load_dlt;
2638 struct block *b;
2639
2640 if (linktype == DLT_PPI)
2641 {
2642 /* Create the statements that check for the DLT
2643 */
2644 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2645 s_load_dlt->s.k = 4;
2646
2647 b = new_block(JMP(BPF_JEQ));
2648
2649 b->stmts = s_load_dlt;
2650 b->s.k = SWAPLONG(DLT_IEEE802_11);
2651 }
2652 else
2653 {
2654 b = NULL;
2655 }
2656
2657 return b;
2658}
2659
2660static struct slist *
2661gen_prism_llprefixlen(void)
2662{
2663 struct slist *s;
2664
2665 if (reg_off_ll == -1) {
2666 /*
2667 * We haven't yet assigned a register for the length
2668 * of the radio header; allocate one.
2669 */
2670 reg_off_ll = alloc_reg();
2671 }
2672
2673 /*
2674 * Load the register containing the radio length
2675 * into the X register.
2676 */
2677 s = new_stmt(BPF_LDX|BPF_MEM);
2678 s->s.k = reg_off_ll;
2679 return s;
2680}
2681
2682static struct slist *
2683gen_avs_llprefixlen(void)
2684{
2685 struct slist *s;
2686
2687 if (reg_off_ll == -1) {
2688 /*
2689 * We haven't yet assigned a register for the length
2690 * of the AVS header; allocate one.
2691 */
2692 reg_off_ll = alloc_reg();
2693 }
2694
2695 /*
2696 * Load the register containing the AVS length
2697 * into the X register.
2698 */
2699 s = new_stmt(BPF_LDX|BPF_MEM);
2700 s->s.k = reg_off_ll;
2701 return s;
2702}
2703
2704static struct slist *
2705gen_radiotap_llprefixlen(void)
2706{
2707 struct slist *s;
2708
2709 if (reg_off_ll == -1) {
2710 /*
2711 * We haven't yet assigned a register for the length
2712 * of the radiotap header; allocate one.
2713 */
2714 reg_off_ll = alloc_reg();
2715 }
2716
2717 /*
2718 * Load the register containing the radiotap length
2719 * into the X register.
2720 */
2721 s = new_stmt(BPF_LDX|BPF_MEM);
2722 s->s.k = reg_off_ll;
2723 return s;
2724}
2725
2726/*
2727 * At the moment we treat PPI as normal Radiotap encoded
2728 * packets. The difference is in the function that generates
2729 * the code at the beginning to compute the header length.
2730 * Since this code generator of PPI supports bare 802.11
2731 * encapsulation only (i.e. the encapsulated DLT should be
2732 * DLT_IEEE802_11) we generate code to check for this too.
2733 */
2734static struct slist *
2735gen_ppi_llprefixlen(void)
2736{
2737 struct slist *s;
2738
2739 if (reg_off_ll == -1) {
2740 /*
2741 * We haven't yet assigned a register for the length
2742 * of the radiotap header; allocate one.
2743 */
2744 reg_off_ll = alloc_reg();
2745 }
2746
2747 /*
2748 * Load the register containing the PPI length
2749 * into the X register.
2750 */
2751 s = new_stmt(BPF_LDX|BPF_MEM);
2752 s->s.k = reg_off_ll;
2753 return s;
2754}
2755
2756/*
2757 * Generate code to compute the link-layer header length, if necessary,
2758 * putting it into the X register, and to return either a pointer to a
2759 * "struct slist" for the list of statements in that code, or NULL if
2760 * no code is necessary.
2761 */
2762static struct slist *
2763gen_llprefixlen(void)
2764{
2765 switch (linktype) {
2766
2767 case DLT_PRISM_HEADER:
2768 return gen_prism_llprefixlen();
2769
2770 case DLT_IEEE802_11_RADIO_AVS:
2771 return gen_avs_llprefixlen();
2772
2773 case DLT_IEEE802_11_RADIO:
2774 return gen_radiotap_llprefixlen();
2775
2776 case DLT_PPI:
2777 return gen_ppi_llprefixlen();
2778
2779 default:
2780 return NULL;
2781 }
2782}
2783
2784/*
2785 * Generate code to load the register containing the offset of the
2786 * MAC-layer payload into the X register; if no register for that offset
2787 * has been allocated, allocate it first.
2788 */
2789static struct slist *
2790gen_off_macpl(void)
2791{
2792 struct slist *s;
2793
2794 if (off_macpl_is_variable) {
2795 if (reg_off_macpl == -1) {
2796 /*
2797 * We haven't yet assigned a register for the offset
2798 * of the MAC-layer payload; allocate one.
2799 */
2800 reg_off_macpl = alloc_reg();
2801 }
2802
2803 /*
2804 * Load the register containing the offset of the MAC-layer
2805 * payload into the X register.
2806 */
2807 s = new_stmt(BPF_LDX|BPF_MEM);
2808 s->s.k = reg_off_macpl;
2809 return s;
2810 } else {
2811 /*
2812 * That offset isn't variable, so we don't need to
2813 * generate any code.
2814 */
2815 return NULL;
2816 }
2817}
2818
2819/*
2820 * Map an Ethernet type to the equivalent PPP type.
2821 */
2822static int
2823ethertype_to_ppptype(proto)
2824 int proto;
2825{
2826 switch (proto) {
2827
2828 case ETHERTYPE_IP:
2829 proto = PPP_IP;
2830 break;
2831
2832#ifdef INET6
2833 case ETHERTYPE_IPV6:
2834 proto = PPP_IPV6;
2835 break;
2836#endif
2837
2838 case ETHERTYPE_DN:
2839 proto = PPP_DECNET;
2840 break;
2841
2842 case ETHERTYPE_ATALK:
2843 proto = PPP_APPLE;
2844 break;
2845
2846 case ETHERTYPE_NS:
2847 proto = PPP_NS;
2848 break;
2849
2850 case LLCSAP_ISONS:
2851 proto = PPP_OSI;
2852 break;
2853
2854 case LLCSAP_8021D:
2855 /*
2856 * I'm assuming the "Bridging PDU"s that go
2857 * over PPP are Spanning Tree Protocol
2858 * Bridging PDUs.
2859 */
2860 proto = PPP_BRPDU;
2861 break;
2862
2863 case LLCSAP_IPX:
2864 proto = PPP_IPX;
2865 break;
2866 }
2867 return (proto);
2868}
2869
2870/*
2871 * Generate code to match a particular packet type by matching the
2872 * link-layer type field or fields in the 802.2 LLC header.
2873 *
2874 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2875 * value, if <= ETHERMTU.
2876 */
2877static struct block *
2878gen_linktype(proto)
2879 register int proto;
2880{
2881 struct block *b0, *b1, *b2;
2882
2883 /* are we checking MPLS-encapsulated packets? */
2884 if (label_stack_depth > 0) {
2885 switch (proto) {
2886 case ETHERTYPE_IP:
2887 case PPP_IP:
2888 /* FIXME add other L3 proto IDs */
2889 return gen_mpls_linktype(Q_IP);
2890
2891 case ETHERTYPE_IPV6:
2892 case PPP_IPV6:
2893 /* FIXME add other L3 proto IDs */
2894 return gen_mpls_linktype(Q_IPV6);
2895
2896 default:
2897 bpf_error("unsupported protocol over mpls");
2898 /* NOTREACHED */
2899 }
2900 }
2901
2902 /*
2903 * Are we testing PPPoE packets?
2904 */
2905 if (is_pppoes) {
2906 /*
2907 * The PPPoE session header is part of the
2908 * MAC-layer payload, so all references
2909 * should be relative to the beginning of
2910 * that payload.
2911 */
2912
2913 /*
2914 * We use Ethernet protocol types inside libpcap;
2915 * map them to the corresponding PPP protocol types.
2916 */
2917 proto = ethertype_to_ppptype(proto);
2918 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2919 }
2920
2921 switch (linktype) {
2922
2923 case DLT_EN10MB:
2924 case DLT_NETANALYZER:
2925 case DLT_NETANALYZER_TRANSPARENT:
2926 return gen_ether_linktype(proto);
2927 /*NOTREACHED*/
2928 break;
2929
2930 case DLT_C_HDLC:
2931 switch (proto) {
2932
2933 case LLCSAP_ISONS:
2934 proto = (proto << 8 | LLCSAP_ISONS);
2935 /* fall through */
2936
2937 default:
2938 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2939 (bpf_int32)proto);
2940 /*NOTREACHED*/
2941 break;
2942 }
2943 break;
2944
2945 case DLT_IEEE802_11:
2946 case DLT_PRISM_HEADER:
2947 case DLT_IEEE802_11_RADIO_AVS:
2948 case DLT_IEEE802_11_RADIO:
2949 case DLT_PPI:
2950 /*
2951 * Check that we have a data frame.
2952 */
2953 b0 = gen_check_802_11_data_frame();
2954
2955 /*
2956 * Now check for the specified link-layer type.
2957 */
2958 b1 = gen_llc_linktype(proto);
2959 gen_and(b0, b1);
2960 return b1;
2961 /*NOTREACHED*/
2962 break;
2963
2964 case DLT_FDDI:
2965 /*
2966 * XXX - check for asynchronous frames, as per RFC 1103.
2967 */
2968 return gen_llc_linktype(proto);
2969 /*NOTREACHED*/
2970 break;
2971
2972 case DLT_IEEE802:
2973 /*
2974 * XXX - check for LLC PDUs, as per IEEE 802.5.
2975 */
2976 return gen_llc_linktype(proto);
2977 /*NOTREACHED*/
2978 break;
2979
2980 case DLT_ATM_RFC1483:
2981 case DLT_ATM_CLIP:
2982 case DLT_IP_OVER_FC:
2983 return gen_llc_linktype(proto);
2984 /*NOTREACHED*/
2985 break;
2986
2987 case DLT_SUNATM:
2988 /*
2989 * If "is_lane" is set, check for a LANE-encapsulated
2990 * version of this protocol, otherwise check for an
2991 * LLC-encapsulated version of this protocol.
2992 *
2993 * We assume LANE means Ethernet, not Token Ring.
2994 */
2995 if (is_lane) {
2996 /*
2997 * Check that the packet doesn't begin with an
2998 * LE Control marker. (We've already generated
2999 * a test for LANE.)
3000 */
3001 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3002 0xFF00);
3003 gen_not(b0);
3004
3005 /*
3006 * Now generate an Ethernet test.
3007 */
3008 b1 = gen_ether_linktype(proto);
3009 gen_and(b0, b1);
3010 return b1;
3011 } else {
3012 /*
3013 * Check for LLC encapsulation and then check the
3014 * protocol.
3015 */
3016 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3017 b1 = gen_llc_linktype(proto);
3018 gen_and(b0, b1);
3019 return b1;
3020 }
3021 /*NOTREACHED*/
3022 break;
3023
3024 case DLT_LINUX_SLL:
3025 return gen_linux_sll_linktype(proto);
3026 /*NOTREACHED*/
3027 break;
3028
3029 case DLT_SLIP:
3030 case DLT_SLIP_BSDOS:
3031 case DLT_RAW:
3032 /*
3033 * These types don't provide any type field; packets
3034 * are always IPv4 or IPv6.
3035 *
3036 * XXX - for IPv4, check for a version number of 4, and,
3037 * for IPv6, check for a version number of 6?
3038 */
3039 switch (proto) {
3040
3041 case ETHERTYPE_IP:
3042 /* Check for a version number of 4. */
3043 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3044#ifdef INET6
3045 case ETHERTYPE_IPV6:
3046 /* Check for a version number of 6. */
3047 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3048#endif
3049
3050 default:
3051 return gen_false(); /* always false */
3052 }
3053 /*NOTREACHED*/
3054 break;
3055
3056 case DLT_IPV4:
3057 /*
3058 * Raw IPv4, so no type field.
3059 */
3060 if (proto == ETHERTYPE_IP)
3061 return gen_true(); /* always true */
3062
3063 /* Checking for something other than IPv4; always false */
3064 return gen_false();
3065 /*NOTREACHED*/
3066 break;
3067
3068 case DLT_IPV6:
3069 /*
3070 * Raw IPv6, so no type field.
3071 */
3072#ifdef INET6
3073 if (proto == ETHERTYPE_IPV6)
3074 return gen_true(); /* always true */
3075#endif
3076
3077 /* Checking for something other than IPv6; always false */
3078 return gen_false();
3079 /*NOTREACHED*/
3080 break;
3081
3082 case DLT_PPP:
3083 case DLT_PPP_PPPD:
3084 case DLT_PPP_SERIAL:
3085 case DLT_PPP_ETHER:
3086 /*
3087 * We use Ethernet protocol types inside libpcap;
3088 * map them to the corresponding PPP protocol types.
3089 */
3090 proto = ethertype_to_ppptype(proto);
3091 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3092 /*NOTREACHED*/
3093 break;
3094
3095 case DLT_PPP_BSDOS:
3096 /*
3097 * We use Ethernet protocol types inside libpcap;
3098 * map them to the corresponding PPP protocol types.
3099 */
3100 switch (proto) {
3101
3102 case ETHERTYPE_IP:
3103 /*
3104 * Also check for Van Jacobson-compressed IP.
3105 * XXX - do this for other forms of PPP?
3106 */
3107 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3108 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3109 gen_or(b0, b1);
3110 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3111 gen_or(b1, b0);
3112 return b0;
3113
3114 default:
3115 proto = ethertype_to_ppptype(proto);
3116 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3117 (bpf_int32)proto);
3118 }
3119 /*NOTREACHED*/
3120 break;
3121
3122 case DLT_NULL:
3123 case DLT_LOOP:
3124 case DLT_ENC:
3125 /*
3126 * For DLT_NULL, the link-layer header is a 32-bit
3127 * word containing an AF_ value in *host* byte order,
3128 * and for DLT_ENC, the link-layer header begins
3129 * with a 32-bit work containing an AF_ value in
3130 * host byte order.
3131 *
3132 * In addition, if we're reading a saved capture file,
3133 * the host byte order in the capture may not be the
3134 * same as the host byte order on this machine.
3135 *
3136 * For DLT_LOOP, the link-layer header is a 32-bit
3137 * word containing an AF_ value in *network* byte order.
3138 *
3139 * XXX - AF_ values may, unfortunately, be platform-
3140 * dependent; for example, FreeBSD's AF_INET6 is 24
3141 * whilst NetBSD's and OpenBSD's is 26.
3142 *
3143 * This means that, when reading a capture file, just
3144 * checking for our AF_INET6 value won't work if the
3145 * capture file came from another OS.
3146 */
3147 switch (proto) {
3148
3149 case ETHERTYPE_IP:
3150 proto = AF_INET;
3151 break;
3152
3153#ifdef INET6
3154 case ETHERTYPE_IPV6:
3155 proto = AF_INET6;
3156 break;
3157#endif
3158
3159 default:
3160 /*
3161 * Not a type on which we support filtering.
3162 * XXX - support those that have AF_ values
3163 * #defined on this platform, at least?
3164 */
3165 return gen_false();
3166 }
3167
3168 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3169 /*
3170 * The AF_ value is in host byte order, but
3171 * the BPF interpreter will convert it to
3172 * network byte order.
3173 *
3174 * If this is a save file, and it's from a
3175 * machine with the opposite byte order to
3176 * ours, we byte-swap the AF_ value.
3177 *
3178 * Then we run it through "htonl()", and
3179 * generate code to compare against the result.
3180 */
3181 if (bpf_pcap->sf.rfile != NULL &&
3182 bpf_pcap->sf.swapped)
3183 proto = SWAPLONG(proto);
3184 proto = htonl(proto);
3185 }
3186 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3187
3188#ifdef HAVE_NET_PFVAR_H
3189 case DLT_PFLOG:
3190 /*
3191 * af field is host byte order in contrast to the rest of
3192 * the packet.
3193 */
3194 if (proto == ETHERTYPE_IP)
3195 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3196 BPF_B, (bpf_int32)AF_INET));
3197#ifdef INET6
3198 else if (proto == ETHERTYPE_IPV6)
3199 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3200 BPF_B, (bpf_int32)AF_INET6));
3201#endif /* INET6 */
3202 else
3203 return gen_false();
3204 /*NOTREACHED*/
3205 break;
3206#endif /* HAVE_NET_PFVAR_H */
3207
3208 case DLT_ARCNET:
3209 case DLT_ARCNET_LINUX:
3210 /*
3211 * XXX should we check for first fragment if the protocol
3212 * uses PHDS?
3213 */
3214 switch (proto) {
3215
3216 default:
3217 return gen_false();
3218
3219#ifdef INET6
3220 case ETHERTYPE_IPV6:
3221 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3222 (bpf_int32)ARCTYPE_INET6));
3223#endif /* INET6 */
3224
3225 case ETHERTYPE_IP:
3226 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3227 (bpf_int32)ARCTYPE_IP);
3228 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3229 (bpf_int32)ARCTYPE_IP_OLD);
3230 gen_or(b0, b1);
3231 return (b1);
3232
3233 case ETHERTYPE_ARP:
3234 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3235 (bpf_int32)ARCTYPE_ARP);
3236 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3237 (bpf_int32)ARCTYPE_ARP_OLD);
3238 gen_or(b0, b1);
3239 return (b1);
3240
3241 case ETHERTYPE_REVARP:
3242 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3243 (bpf_int32)ARCTYPE_REVARP));
3244
3245 case ETHERTYPE_ATALK:
3246 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3247 (bpf_int32)ARCTYPE_ATALK));
3248 }
3249 /*NOTREACHED*/
3250 break;
3251
3252 case DLT_LTALK:
3253 switch (proto) {
3254 case ETHERTYPE_ATALK:
3255 return gen_true();
3256 default:
3257 return gen_false();
3258 }
3259 /*NOTREACHED*/
3260 break;
3261
3262 case DLT_FRELAY:
3263 /*
3264 * XXX - assumes a 2-byte Frame Relay header with
3265 * DLCI and flags. What if the address is longer?
3266 */
3267 switch (proto) {
3268
3269 case ETHERTYPE_IP:
3270 /*
3271 * Check for the special NLPID for IP.
3272 */
3273 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3274
3275#ifdef INET6
3276 case ETHERTYPE_IPV6:
3277 /*
3278 * Check for the special NLPID for IPv6.
3279 */
3280 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3281#endif
3282
3283 case LLCSAP_ISONS:
3284 /*
3285 * Check for several OSI protocols.
3286 *
3287 * Frame Relay packets typically have an OSI
3288 * NLPID at the beginning; we check for each
3289 * of them.
3290 *
3291 * What we check for is the NLPID and a frame
3292 * control field of UI, i.e. 0x03 followed
3293 * by the NLPID.
3294 */
3295 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3296 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3297 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3298 gen_or(b1, b2);
3299 gen_or(b0, b2);
3300 return b2;
3301
3302 default:
3303 return gen_false();
3304 }
3305 /*NOTREACHED*/
3306 break;
3307
3308 case DLT_MFR:
3309 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3310
3311 case DLT_JUNIPER_MFR:
3312 case DLT_JUNIPER_MLFR:
3313 case DLT_JUNIPER_MLPPP:
3314 case DLT_JUNIPER_ATM1:
3315 case DLT_JUNIPER_ATM2:
3316 case DLT_JUNIPER_PPPOE:
3317 case DLT_JUNIPER_PPPOE_ATM:
3318 case DLT_JUNIPER_GGSN:
3319 case DLT_JUNIPER_ES:
3320 case DLT_JUNIPER_MONITOR:
3321 case DLT_JUNIPER_SERVICES:
3322 case DLT_JUNIPER_ETHER:
3323 case DLT_JUNIPER_PPP:
3324 case DLT_JUNIPER_FRELAY:
3325 case DLT_JUNIPER_CHDLC:
3326 case DLT_JUNIPER_VP:
3327 case DLT_JUNIPER_ST:
3328 case DLT_JUNIPER_ISM:
3329 case DLT_JUNIPER_VS:
3330 case DLT_JUNIPER_SRX_E2E:
3331 case DLT_JUNIPER_FIBRECHANNEL:
3332 case DLT_JUNIPER_ATM_CEMIC:
3333
3334 /* just lets verify the magic number for now -
3335 * on ATM we may have up to 6 different encapsulations on the wire
3336 * and need a lot of heuristics to figure out that the payload
3337 * might be;
3338 *
3339 * FIXME encapsulation specific BPF_ filters
3340 */
3341 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3342
3343 case DLT_IPNET:
3344 return gen_ipnet_linktype(proto);
3345
3346 case DLT_LINUX_IRDA:
3347 bpf_error("IrDA link-layer type filtering not implemented");
3348
3349 case DLT_DOCSIS:
3350 bpf_error("DOCSIS link-layer type filtering not implemented");
3351
3352 case DLT_MTP2:
3353 case DLT_MTP2_WITH_PHDR:
3354 bpf_error("MTP2 link-layer type filtering not implemented");
3355
3356 case DLT_ERF:
3357 bpf_error("ERF link-layer type filtering not implemented");
3358
3359#ifdef DLT_PFSYNC
3360 case DLT_PFSYNC:
3361 bpf_error("PFSYNC link-layer type filtering not implemented");
3362#endif
3363
3364 case DLT_LINUX_LAPD:
3365 bpf_error("LAPD link-layer type filtering not implemented");
3366
3367 case DLT_USB:
3368 case DLT_USB_LINUX:
3369 case DLT_USB_LINUX_MMAPPED:
3370 bpf_error("USB link-layer type filtering not implemented");
3371
3372 case DLT_BLUETOOTH_HCI_H4:
3373 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3374 bpf_error("Bluetooth link-layer type filtering not implemented");
3375
3376 case DLT_CAN20B:
3377 case DLT_CAN_SOCKETCAN:
3378 bpf_error("CAN link-layer type filtering not implemented");
3379
3380 case DLT_IEEE802_15_4:
3381 case DLT_IEEE802_15_4_LINUX:
3382 case DLT_IEEE802_15_4_NONASK_PHY:
3383 case DLT_IEEE802_15_4_NOFCS:
3384 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3385
3386 case DLT_IEEE802_16_MAC_CPS_RADIO:
3387 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3388
3389 case DLT_SITA:
3390 bpf_error("SITA link-layer type filtering not implemented");
3391
3392 case DLT_RAIF1:
3393 bpf_error("RAIF1 link-layer type filtering not implemented");
3394
3395 case DLT_IPMB:
3396 bpf_error("IPMB link-layer type filtering not implemented");
3397
3398 case DLT_AX25_KISS:
3399 bpf_error("AX.25 link-layer type filtering not implemented");
3400 }
3401
3402 /*
3403 * All the types that have no encapsulation should either be
3404 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3405 * all packets are IP packets, or should be handled in some
3406 * special case, if none of them are (if some are and some
3407 * aren't, the lack of encapsulation is a problem, as we'd
3408 * have to find some other way of determining the packet type).
3409 *
3410 * Therefore, if "off_linktype" is -1, there's an error.
3411 */
3412 if (off_linktype == (u_int)-1)
3413 abort();
3414
3415 /*
3416 * Any type not handled above should always have an Ethernet
3417 * type at an offset of "off_linktype".
3418 */
3419 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3420}
3421
3422/*
3423 * Check for an LLC SNAP packet with a given organization code and
3424 * protocol type; we check the entire contents of the 802.2 LLC and
3425 * snap headers, checking for DSAP and SSAP of SNAP and a control
3426 * field of 0x03 in the LLC header, and for the specified organization
3427 * code and protocol type in the SNAP header.
3428 */
3429static struct block *
3430gen_snap(orgcode, ptype)
3431 bpf_u_int32 orgcode;
3432 bpf_u_int32 ptype;
3433{
3434 u_char snapblock[8];
3435
3436 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3437 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3438 snapblock[2] = 0x03; /* control = UI */
3439 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3440 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3441 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3442 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3443 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3444 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3445}
3446
3447/*
3448 * Generate code to match a particular packet type, for link-layer types
3449 * using 802.2 LLC headers.
3450 *
3451 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3452 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3453 *
3454 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3455 * value, if <= ETHERMTU. We use that to determine whether to
3456 * match the DSAP or both DSAP and LSAP or to check the OUI and
3457 * protocol ID in a SNAP header.
3458 */
3459static struct block *
3460gen_llc_linktype(proto)
3461 int proto;
3462{
3463 /*
3464 * XXX - handle token-ring variable-length header.
3465 */
3466 switch (proto) {
3467
3468 case LLCSAP_IP:
3469 case LLCSAP_ISONS:
3470 case LLCSAP_NETBEUI:
3471 /*
3472 * XXX - should we check both the DSAP and the
3473 * SSAP, like this, or should we check just the
3474 * DSAP, as we do for other types <= ETHERMTU
3475 * (i.e., other SAP values)?
3476 */
3477 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3478 ((proto << 8) | proto));
3479
3480 case LLCSAP_IPX:
3481 /*
3482 * XXX - are there ever SNAP frames for IPX on
3483 * non-Ethernet 802.x networks?
3484 */
3485 return gen_cmp(OR_MACPL, 0, BPF_B,
3486 (bpf_int32)LLCSAP_IPX);
3487
3488 case ETHERTYPE_ATALK:
3489 /*
3490 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3491 * SNAP packets with an organization code of
3492 * 0x080007 (Apple, for Appletalk) and a protocol
3493 * type of ETHERTYPE_ATALK (Appletalk).
3494 *
3495 * XXX - check for an organization code of
3496 * encapsulated Ethernet as well?
3497 */
3498 return gen_snap(0x080007, ETHERTYPE_ATALK);
3499
3500 default:
3501 /*
3502 * XXX - we don't have to check for IPX 802.3
3503 * here, but should we check for the IPX Ethertype?
3504 */
3505 if (proto <= ETHERMTU) {
3506 /*
3507 * This is an LLC SAP value, so check
3508 * the DSAP.
3509 */
3510 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3511 } else {
3512 /*
3513 * This is an Ethernet type; we assume that it's
3514 * unlikely that it'll appear in the right place
3515 * at random, and therefore check only the
3516 * location that would hold the Ethernet type
3517 * in a SNAP frame with an organization code of
3518 * 0x000000 (encapsulated Ethernet).
3519 *
3520 * XXX - if we were to check for the SNAP DSAP and
3521 * LSAP, as per XXX, and were also to check for an
3522 * organization code of 0x000000 (encapsulated
3523 * Ethernet), we'd do
3524 *
3525 * return gen_snap(0x000000, proto);
3526 *
3527 * here; for now, we don't, as per the above.
3528 * I don't know whether it's worth the extra CPU
3529 * time to do the right check or not.
3530 */
3531 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3532 }
3533 }
3534}
3535
3536static struct block *
3537gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3538 bpf_u_int32 addr;
3539 bpf_u_int32 mask;
3540 int dir, proto;
3541 u_int src_off, dst_off;
3542{
3543 struct block *b0, *b1;
3544 u_int offset;
3545
3546 switch (dir) {
3547
3548 case Q_SRC:
3549 offset = src_off;
3550 break;
3551
3552 case Q_DST:
3553 offset = dst_off;
3554 break;
3555
3556 case Q_AND:
3557 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3558 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3559 gen_and(b0, b1);
3560 return b1;
3561
3562 case Q_OR:
3563 case Q_DEFAULT:
3564 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3565 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3566 gen_or(b0, b1);
3567 return b1;
3568
3569 default:
3570 abort();
3571 }
3572 b0 = gen_linktype(proto);
3573 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3574 gen_and(b0, b1);
3575 return b1;
3576}
3577
3578#ifdef INET6
3579static struct block *
3580gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3581 struct in6_addr *addr;
3582 struct in6_addr *mask;
3583 int dir, proto;
3584 u_int src_off, dst_off;
3585{
3586 struct block *b0, *b1;
3587 u_int offset;
3588 u_int32_t *a, *m;
3589
3590 switch (dir) {
3591
3592 case Q_SRC:
3593 offset = src_off;
3594 break;
3595
3596 case Q_DST:
3597 offset = dst_off;
3598 break;
3599
3600 case Q_AND:
3601 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3602 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3603 gen_and(b0, b1);
3604 return b1;
3605
3606 case Q_OR:
3607 case Q_DEFAULT:
3608 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3609 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3610 gen_or(b0, b1);
3611 return b1;
3612
3613 default:
3614 abort();
3615 }
3616 /* this order is important */
3617 a = (u_int32_t *)addr;
3618 m = (u_int32_t *)mask;
3619 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3620 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3621 gen_and(b0, b1);
3622 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3623 gen_and(b0, b1);
3624 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3625 gen_and(b0, b1);
3626 b0 = gen_linktype(proto);
3627 gen_and(b0, b1);
3628 return b1;
3629}
3630#endif /*INET6*/
3631
3632static struct block *
3633gen_ehostop(eaddr, dir)
3634 register const u_char *eaddr;
3635 register int dir;
3636{
3637 register struct block *b0, *b1;
3638
3639 switch (dir) {
3640 case Q_SRC:
3641 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3642
3643 case Q_DST:
3644 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3645
3646 case Q_AND:
3647 b0 = gen_ehostop(eaddr, Q_SRC);
3648 b1 = gen_ehostop(eaddr, Q_DST);
3649 gen_and(b0, b1);
3650 return b1;
3651
3652 case Q_DEFAULT:
3653 case Q_OR:
3654 b0 = gen_ehostop(eaddr, Q_SRC);
3655 b1 = gen_ehostop(eaddr, Q_DST);
3656 gen_or(b0, b1);
3657 return b1;
3658
3659 case Q_ADDR1:
3660 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3661 break;
3662
3663 case Q_ADDR2:
3664 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3665 break;
3666
3667 case Q_ADDR3:
3668 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3669 break;
3670
3671 case Q_ADDR4:
3672 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3673 break;
3674
3675 case Q_RA:
3676 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3677 break;
3678
3679 case Q_TA:
3680 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3681 break;
3682 }
3683 abort();
3684 /* NOTREACHED */
3685}
3686
3687/*
3688 * Like gen_ehostop, but for DLT_FDDI
3689 */
3690static struct block *
3691gen_fhostop(eaddr, dir)
3692 register const u_char *eaddr;
3693 register int dir;
3694{
3695 struct block *b0, *b1;
3696
3697 switch (dir) {
3698 case Q_SRC:
3699#ifdef PCAP_FDDIPAD
3700 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3701#else
3702 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3703#endif
3704
3705 case Q_DST:
3706#ifdef PCAP_FDDIPAD
3707 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3708#else
3709 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3710#endif
3711
3712 case Q_AND:
3713 b0 = gen_fhostop(eaddr, Q_SRC);
3714 b1 = gen_fhostop(eaddr, Q_DST);
3715 gen_and(b0, b1);
3716 return b1;
3717
3718 case Q_DEFAULT:
3719 case Q_OR:
3720 b0 = gen_fhostop(eaddr, Q_SRC);
3721 b1 = gen_fhostop(eaddr, Q_DST);
3722 gen_or(b0, b1);
3723 return b1;
3724
3725 case Q_ADDR1:
3726 bpf_error("'addr1' is only supported on 802.11");
3727 break;
3728
3729 case Q_ADDR2:
3730 bpf_error("'addr2' is only supported on 802.11");
3731 break;
3732
3733 case Q_ADDR3:
3734 bpf_error("'addr3' is only supported on 802.11");
3735 break;
3736
3737 case Q_ADDR4:
3738 bpf_error("'addr4' is only supported on 802.11");
3739 break;
3740
3741 case Q_RA:
3742 bpf_error("'ra' is only supported on 802.11");
3743 break;
3744
3745 case Q_TA:
3746 bpf_error("'ta' is only supported on 802.11");
3747 break;
3748 }
3749 abort();
3750 /* NOTREACHED */
3751}
3752
3753/*
3754 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3755 */
3756static struct block *
3757gen_thostop(eaddr, dir)
3758 register const u_char *eaddr;
3759 register int dir;
3760{
3761 register struct block *b0, *b1;
3762
3763 switch (dir) {
3764 case Q_SRC:
3765 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3766
3767 case Q_DST:
3768 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3769
3770 case Q_AND:
3771 b0 = gen_thostop(eaddr, Q_SRC);
3772 b1 = gen_thostop(eaddr, Q_DST);
3773 gen_and(b0, b1);
3774 return b1;
3775
3776 case Q_DEFAULT:
3777 case Q_OR:
3778 b0 = gen_thostop(eaddr, Q_SRC);
3779 b1 = gen_thostop(eaddr, Q_DST);
3780 gen_or(b0, b1);
3781 return b1;
3782
3783 case Q_ADDR1:
3784 bpf_error("'addr1' is only supported on 802.11");
3785 break;
3786
3787 case Q_ADDR2:
3788 bpf_error("'addr2' is only supported on 802.11");
3789 break;
3790
3791 case Q_ADDR3:
3792 bpf_error("'addr3' is only supported on 802.11");
3793 break;
3794
3795 case Q_ADDR4:
3796 bpf_error("'addr4' is only supported on 802.11");
3797 break;
3798
3799 case Q_RA:
3800 bpf_error("'ra' is only supported on 802.11");
3801 break;
3802
3803 case Q_TA:
3804 bpf_error("'ta' is only supported on 802.11");
3805 break;
3806 }
3807 abort();
3808 /* NOTREACHED */
3809}
3810
3811/*
3812 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3813 * various 802.11 + radio headers.
3814 */
3815static struct block *
3816gen_wlanhostop(eaddr, dir)
3817 register const u_char *eaddr;
3818 register int dir;
3819{
3820 register struct block *b0, *b1, *b2;
3821 register struct slist *s;
3822
3823#ifdef ENABLE_WLAN_FILTERING_PATCH
3824 /*
3825 * TODO GV 20070613
3826 * We need to disable the optimizer because the optimizer is buggy
3827 * and wipes out some LD instructions generated by the below
3828 * code to validate the Frame Control bits
3829 */
3830 no_optimize = 1;
3831#endif /* ENABLE_WLAN_FILTERING_PATCH */
3832
3833 switch (dir) {
3834 case Q_SRC:
3835 /*
3836 * Oh, yuk.
3837 *
3838 * For control frames, there is no SA.
3839 *
3840 * For management frames, SA is at an
3841 * offset of 10 from the beginning of
3842 * the packet.
3843 *
3844 * For data frames, SA is at an offset
3845 * of 10 from the beginning of the packet
3846 * if From DS is clear, at an offset of
3847 * 16 from the beginning of the packet
3848 * if From DS is set and To DS is clear,
3849 * and an offset of 24 from the beginning
3850 * of the packet if From DS is set and To DS
3851 * is set.
3852 */
3853
3854 /*
3855 * Generate the tests to be done for data frames
3856 * with From DS set.
3857 *
3858 * First, check for To DS set, i.e. check "link[1] & 0x01".
3859 */
3860 s = gen_load_a(OR_LINK, 1, BPF_B);
3861 b1 = new_block(JMP(BPF_JSET));
3862 b1->s.k = 0x01; /* To DS */
3863 b1->stmts = s;
3864
3865 /*
3866 * If To DS is set, the SA is at 24.
3867 */
3868 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3869 gen_and(b1, b0);
3870
3871 /*
3872 * Now, check for To DS not set, i.e. check
3873 * "!(link[1] & 0x01)".
3874 */
3875 s = gen_load_a(OR_LINK, 1, BPF_B);
3876 b2 = new_block(JMP(BPF_JSET));
3877 b2->s.k = 0x01; /* To DS */
3878 b2->stmts = s;
3879 gen_not(b2);
3880
3881 /*
3882 * If To DS is not set, the SA is at 16.
3883 */
3884 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3885 gen_and(b2, b1);
3886
3887 /*
3888 * Now OR together the last two checks. That gives
3889 * the complete set of checks for data frames with
3890 * From DS set.
3891 */
3892 gen_or(b1, b0);
3893
3894 /*
3895 * Now check for From DS being set, and AND that with
3896 * the ORed-together checks.
3897 */
3898 s = gen_load_a(OR_LINK, 1, BPF_B);
3899 b1 = new_block(JMP(BPF_JSET));
3900 b1->s.k = 0x02; /* From DS */
3901 b1->stmts = s;
3902 gen_and(b1, b0);
3903
3904 /*
3905 * Now check for data frames with From DS not set.
3906 */
3907 s = gen_load_a(OR_LINK, 1, BPF_B);
3908 b2 = new_block(JMP(BPF_JSET));
3909 b2->s.k = 0x02; /* From DS */
3910 b2->stmts = s;
3911 gen_not(b2);
3912
3913 /*
3914 * If From DS isn't set, the SA is at 10.
3915 */
3916 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3917 gen_and(b2, b1);
3918
3919 /*
3920 * Now OR together the checks for data frames with
3921 * From DS not set and for data frames with From DS
3922 * set; that gives the checks done for data frames.
3923 */
3924 gen_or(b1, b0);
3925
3926 /*
3927 * Now check for a data frame.
3928 * I.e, check "link[0] & 0x08".
3929 */
3930 s = gen_load_a(OR_LINK, 0, BPF_B);
3931 b1 = new_block(JMP(BPF_JSET));
3932 b1->s.k = 0x08;
3933 b1->stmts = s;
3934
3935 /*
3936 * AND that with the checks done for data frames.
3937 */
3938 gen_and(b1, b0);
3939
3940 /*
3941 * If the high-order bit of the type value is 0, this
3942 * is a management frame.
3943 * I.e, check "!(link[0] & 0x08)".
3944 */
3945 s = gen_load_a(OR_LINK, 0, BPF_B);
3946 b2 = new_block(JMP(BPF_JSET));
3947 b2->s.k = 0x08;
3948 b2->stmts = s;
3949 gen_not(b2);
3950
3951 /*
3952 * For management frames, the SA is at 10.
3953 */
3954 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3955 gen_and(b2, b1);
3956
3957 /*
3958 * OR that with the checks done for data frames.
3959 * That gives the checks done for management and
3960 * data frames.
3961 */
3962 gen_or(b1, b0);
3963
3964 /*
3965 * If the low-order bit of the type value is 1,
3966 * this is either a control frame or a frame
3967 * with a reserved type, and thus not a
3968 * frame with an SA.
3969 *
3970 * I.e., check "!(link[0] & 0x04)".
3971 */
3972 s = gen_load_a(OR_LINK, 0, BPF_B);
3973 b1 = new_block(JMP(BPF_JSET));
3974 b1->s.k = 0x04;
3975 b1->stmts = s;
3976 gen_not(b1);
3977
3978 /*
3979 * AND that with the checks for data and management
3980 * frames.
3981 */
3982 gen_and(b1, b0);
3983 return b0;
3984
3985 case Q_DST:
3986 /*
3987 * Oh, yuk.
3988 *
3989 * For control frames, there is no DA.
3990 *
3991 * For management frames, DA is at an
3992 * offset of 4 from the beginning of
3993 * the packet.
3994 *
3995 * For data frames, DA is at an offset
3996 * of 4 from the beginning of the packet
3997 * if To DS is clear and at an offset of
3998 * 16 from the beginning of the packet
3999 * if To DS is set.
4000 */
4001
4002 /*
4003 * Generate the tests to be done for data frames.
4004 *
4005 * First, check for To DS set, i.e. "link[1] & 0x01".
4006 */
4007 s = gen_load_a(OR_LINK, 1, BPF_B);
4008 b1 = new_block(JMP(BPF_JSET));
4009 b1->s.k = 0x01; /* To DS */
4010 b1->stmts = s;
4011
4012 /*
4013 * If To DS is set, the DA is at 16.
4014 */
4015 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4016 gen_and(b1, b0);
4017
4018 /*
4019 * Now, check for To DS not set, i.e. check
4020 * "!(link[1] & 0x01)".
4021 */
4022 s = gen_load_a(OR_LINK, 1, BPF_B);
4023 b2 = new_block(JMP(BPF_JSET));
4024 b2->s.k = 0x01; /* To DS */
4025 b2->stmts = s;
4026 gen_not(b2);
4027
4028 /*
4029 * If To DS is not set, the DA is at 4.
4030 */
4031 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4032 gen_and(b2, b1);
4033
4034 /*
4035 * Now OR together the last two checks. That gives
4036 * the complete set of checks for data frames.
4037 */
4038 gen_or(b1, b0);
4039
4040 /*
4041 * Now check for a data frame.
4042 * I.e, check "link[0] & 0x08".
4043 */
4044 s = gen_load_a(OR_LINK, 0, BPF_B);
4045 b1 = new_block(JMP(BPF_JSET));
4046 b1->s.k = 0x08;
4047 b1->stmts = s;
4048
4049 /*
4050 * AND that with the checks done for data frames.
4051 */
4052 gen_and(b1, b0);
4053
4054 /*
4055 * If the high-order bit of the type value is 0, this
4056 * is a management frame.
4057 * I.e, check "!(link[0] & 0x08)".
4058 */
4059 s = gen_load_a(OR_LINK, 0, BPF_B);
4060 b2 = new_block(JMP(BPF_JSET));
4061 b2->s.k = 0x08;
4062 b2->stmts = s;
4063 gen_not(b2);
4064
4065 /*
4066 * For management frames, the DA is at 4.
4067 */
4068 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4069 gen_and(b2, b1);
4070
4071 /*
4072 * OR that with the checks done for data frames.
4073 * That gives the checks done for management and
4074 * data frames.
4075 */
4076 gen_or(b1, b0);
4077
4078 /*
4079 * If the low-order bit of the type value is 1,
4080 * this is either a control frame or a frame
4081 * with a reserved type, and thus not a
4082 * frame with an SA.
4083 *
4084 * I.e., check "!(link[0] & 0x04)".
4085 */
4086 s = gen_load_a(OR_LINK, 0, BPF_B);
4087 b1 = new_block(JMP(BPF_JSET));
4088 b1->s.k = 0x04;
4089 b1->stmts = s;
4090 gen_not(b1);
4091
4092 /*
4093 * AND that with the checks for data and management
4094 * frames.
4095 */
4096 gen_and(b1, b0);
4097 return b0;
4098
4099 case Q_RA:
4100 /*
4101 * Not present in management frames; addr1 in other
4102 * frames.
4103 */
4104
4105 /*
4106 * If the high-order bit of the type value is 0, this
4107 * is a management frame.
4108 * I.e, check "(link[0] & 0x08)".
4109 */
4110 s = gen_load_a(OR_LINK, 0, BPF_B);
4111 b1 = new_block(JMP(BPF_JSET));
4112 b1->s.k = 0x08;
4113 b1->stmts = s;
4114
4115 /*
4116 * Check addr1.
4117 */
4118 b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4119
4120 /*
4121 * AND that with the check of addr1.
4122 */
4123 gen_and(b1, b0);
4124 return (b0);
4125
4126 case Q_TA:
4127 /*
4128 * Not present in management frames; addr2, if present,
4129 * in other frames.
4130 */
4131
4132 /*
4133 * Not present in CTS or ACK control frames.
4134 */
4135 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4136 IEEE80211_FC0_TYPE_MASK);
4137 gen_not(b0);
4138 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4139 IEEE80211_FC0_SUBTYPE_MASK);
4140 gen_not(b1);
4141 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4142 IEEE80211_FC0_SUBTYPE_MASK);
4143 gen_not(b2);
4144 gen_and(b1, b2);
4145 gen_or(b0, b2);
4146
4147 /*
4148 * If the high-order bit of the type value is 0, this
4149 * is a management frame.
4150 * I.e, check "(link[0] & 0x08)".
4151 */
4152 s = gen_load_a(OR_LINK, 0, BPF_B);
4153 b1 = new_block(JMP(BPF_JSET));
4154 b1->s.k = 0x08;
4155 b1->stmts = s;
4156
4157 /*
4158 * AND that with the check for frames other than
4159 * CTS and ACK frames.
4160 */
4161 gen_and(b1, b2);
4162
4163 /*
4164 * Check addr2.
4165 */
4166 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4167 gen_and(b2, b1);
4168 return b1;
4169
4170 /*
4171 * XXX - add BSSID keyword?
4172 */
4173 case Q_ADDR1:
4174 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4175
4176 case Q_ADDR2:
4177 /*
4178 * Not present in CTS or ACK control frames.
4179 */
4180 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4181 IEEE80211_FC0_TYPE_MASK);
4182 gen_not(b0);
4183 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4184 IEEE80211_FC0_SUBTYPE_MASK);
4185 gen_not(b1);
4186 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4187 IEEE80211_FC0_SUBTYPE_MASK);
4188 gen_not(b2);
4189 gen_and(b1, b2);
4190 gen_or(b0, b2);
4191 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4192 gen_and(b2, b1);
4193 return b1;
4194
4195 case Q_ADDR3:
4196 /*
4197 * Not present in control frames.
4198 */
4199 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4200 IEEE80211_FC0_TYPE_MASK);
4201 gen_not(b0);
4202 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4203 gen_and(b0, b1);
4204 return b1;
4205
4206 case Q_ADDR4:
4207 /*
4208 * Present only if the direction mask has both "From DS"
4209 * and "To DS" set. Neither control frames nor management
4210 * frames should have both of those set, so we don't
4211 * check the frame type.
4212 */
4213 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4214 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4215 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4216 gen_and(b0, b1);
4217 return b1;
4218
4219 case Q_AND:
4220 b0 = gen_wlanhostop(eaddr, Q_SRC);
4221 b1 = gen_wlanhostop(eaddr, Q_DST);
4222 gen_and(b0, b1);
4223 return b1;
4224
4225 case Q_DEFAULT:
4226 case Q_OR:
4227 b0 = gen_wlanhostop(eaddr, Q_SRC);
4228 b1 = gen_wlanhostop(eaddr, Q_DST);
4229 gen_or(b0, b1);
4230 return b1;
4231 }
4232 abort();
4233 /* NOTREACHED */
4234}
4235
4236/*
4237 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4238 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4239 * as the RFC states.)
4240 */
4241static struct block *
4242gen_ipfchostop(eaddr, dir)
4243 register const u_char *eaddr;
4244 register int dir;
4245{
4246 register struct block *b0, *b1;
4247
4248 switch (dir) {
4249 case Q_SRC:
4250 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4251
4252 case Q_DST:
4253 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4254
4255 case Q_AND:
4256 b0 = gen_ipfchostop(eaddr, Q_SRC);
4257 b1 = gen_ipfchostop(eaddr, Q_DST);
4258 gen_and(b0, b1);
4259 return b1;
4260
4261 case Q_DEFAULT:
4262 case Q_OR:
4263 b0 = gen_ipfchostop(eaddr, Q_SRC);
4264 b1 = gen_ipfchostop(eaddr, Q_DST);
4265 gen_or(b0, b1);
4266 return b1;
4267
4268 case Q_ADDR1:
4269 bpf_error("'addr1' is only supported on 802.11");
4270 break;
4271
4272 case Q_ADDR2:
4273 bpf_error("'addr2' is only supported on 802.11");
4274 break;
4275
4276 case Q_ADDR3:
4277 bpf_error("'addr3' is only supported on 802.11");
4278 break;
4279
4280 case Q_ADDR4:
4281 bpf_error("'addr4' is only supported on 802.11");
4282 break;
4283
4284 case Q_RA:
4285 bpf_error("'ra' is only supported on 802.11");
4286 break;
4287
4288 case Q_TA:
4289 bpf_error("'ta' is only supported on 802.11");
4290 break;
4291 }
4292 abort();
4293 /* NOTREACHED */
4294}
4295
4296/*
4297 * This is quite tricky because there may be pad bytes in front of the
4298 * DECNET header, and then there are two possible data packet formats that
4299 * carry both src and dst addresses, plus 5 packet types in a format that
4300 * carries only the src node, plus 2 types that use a different format and
4301 * also carry just the src node.
4302 *
4303 * Yuck.
4304 *
4305 * Instead of doing those all right, we just look for data packets with
4306 * 0 or 1 bytes of padding. If you want to look at other packets, that
4307 * will require a lot more hacking.
4308 *
4309 * To add support for filtering on DECNET "areas" (network numbers)
4310 * one would want to add a "mask" argument to this routine. That would
4311 * make the filter even more inefficient, although one could be clever
4312 * and not generate masking instructions if the mask is 0xFFFF.
4313 */
4314static struct block *
4315gen_dnhostop(addr, dir)
4316 bpf_u_int32 addr;
4317 int dir;
4318{
4319 struct block *b0, *b1, *b2, *tmp;
4320 u_int offset_lh; /* offset if long header is received */
4321 u_int offset_sh; /* offset if short header is received */
4322
4323 switch (dir) {
4324
4325 case Q_DST:
4326 offset_sh = 1; /* follows flags */
4327 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4328 break;
4329
4330 case Q_SRC:
4331 offset_sh = 3; /* follows flags, dstnode */
4332 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4333 break;
4334
4335 case Q_AND:
4336 /* Inefficient because we do our Calvinball dance twice */
4337 b0 = gen_dnhostop(addr, Q_SRC);
4338 b1 = gen_dnhostop(addr, Q_DST);
4339 gen_and(b0, b1);
4340 return b1;
4341
4342 case Q_OR:
4343 case Q_DEFAULT:
4344 /* Inefficient because we do our Calvinball dance twice */
4345 b0 = gen_dnhostop(addr, Q_SRC);
4346 b1 = gen_dnhostop(addr, Q_DST);
4347 gen_or(b0, b1);
4348 return b1;
4349
4350 case Q_ISO:
4351 bpf_error("ISO host filtering not implemented");
4352
4353 default:
4354 abort();
4355 }
4356 b0 = gen_linktype(ETHERTYPE_DN);
4357 /* Check for pad = 1, long header case */
4358 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4359 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4360 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4361 BPF_H, (bpf_int32)ntohs((u_short)addr));
4362 gen_and(tmp, b1);
4363 /* Check for pad = 0, long header case */
4364 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4365 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4366 gen_and(tmp, b2);
4367 gen_or(b2, b1);
4368 /* Check for pad = 1, short header case */
4369 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4370 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4371 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4372 gen_and(tmp, b2);
4373 gen_or(b2, b1);
4374 /* Check for pad = 0, short header case */
4375 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4376 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4377 gen_and(tmp, b2);
4378 gen_or(b2, b1);
4379
4380 /* Combine with test for linktype */
4381 gen_and(b0, b1);
4382 return b1;
4383}
4384
4385/*
4386 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4387 * test the bottom-of-stack bit, and then check the version number
4388 * field in the IP header.
4389 */
4390static struct block *
4391gen_mpls_linktype(proto)
4392 int proto;
4393{
4394 struct block *b0, *b1;
4395
4396 switch (proto) {
4397
4398 case Q_IP:
4399 /* match the bottom-of-stack bit */
4400 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4401 /* match the IPv4 version number */
4402 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4403 gen_and(b0, b1);
4404 return b1;
4405
4406 case Q_IPV6:
4407 /* match the bottom-of-stack bit */
4408 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4409 /* match the IPv4 version number */
4410 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4411 gen_and(b0, b1);
4412 return b1;
4413
4414 default:
4415 abort();
4416 }
4417}
4418
4419static struct block *
4420gen_host(addr, mask, proto, dir, type)
4421 bpf_u_int32 addr;
4422 bpf_u_int32 mask;
4423 int proto;
4424 int dir;
4425 int type;
4426{
4427 struct block *b0, *b1;
4428 const char *typestr;
4429
4430 if (type == Q_NET)
4431 typestr = "net";
4432 else
4433 typestr = "host";
4434
4435 switch (proto) {
4436
4437 case Q_DEFAULT:
4438 b0 = gen_host(addr, mask, Q_IP, dir, type);
4439 /*
4440 * Only check for non-IPv4 addresses if we're not
4441 * checking MPLS-encapsulated packets.
4442 */
4443 if (label_stack_depth == 0) {
4444 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4445 gen_or(b0, b1);
4446 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4447 gen_or(b1, b0);
4448 }
4449 return b0;
4450
4451 case Q_IP:
4452 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4453
4454 case Q_RARP:
4455 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4456
4457 case Q_ARP:
4458 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4459
4460 case Q_TCP:
4461 bpf_error("'tcp' modifier applied to %s", typestr);
4462
4463 case Q_SCTP:
4464 bpf_error("'sctp' modifier applied to %s", typestr);
4465
4466 case Q_UDP:
4467 bpf_error("'udp' modifier applied to %s", typestr);
4468
4469 case Q_ICMP:
4470 bpf_error("'icmp' modifier applied to %s", typestr);
4471
4472 case Q_IGMP:
4473 bpf_error("'igmp' modifier applied to %s", typestr);
4474
4475 case Q_IGRP:
4476 bpf_error("'igrp' modifier applied to %s", typestr);
4477
4478 case Q_PIM:
4479 bpf_error("'pim' modifier applied to %s", typestr);
4480
4481 case Q_VRRP:
4482 bpf_error("'vrrp' modifier applied to %s", typestr);
4483
4484 case Q_CARP:
4485 bpf_error("'carp' modifier applied to %s", typestr);
4486
4487 case Q_ATALK:
4488 bpf_error("ATALK host filtering not implemented");
4489
4490 case Q_AARP:
4491 bpf_error("AARP host filtering not implemented");
4492
4493 case Q_DECNET:
4494 return gen_dnhostop(addr, dir);
4495
4496 case Q_SCA:
4497 bpf_error("SCA host filtering not implemented");
4498
4499 case Q_LAT:
4500 bpf_error("LAT host filtering not implemented");
4501
4502 case Q_MOPDL:
4503 bpf_error("MOPDL host filtering not implemented");
4504
4505 case Q_MOPRC:
4506 bpf_error("MOPRC host filtering not implemented");
4507
4508#ifdef INET6
4509 case Q_IPV6:
4510 bpf_error("'ip6' modifier applied to ip host");
4511
4512 case Q_ICMPV6:
4513 bpf_error("'icmp6' modifier applied to %s", typestr);
4514#endif /* INET6 */
4515
4516 case Q_AH:
4517 bpf_error("'ah' modifier applied to %s", typestr);
4518
4519 case Q_ESP:
4520 bpf_error("'esp' modifier applied to %s", typestr);
4521
4522 case Q_ISO:
4523 bpf_error("ISO host filtering not implemented");
4524
4525 case Q_ESIS:
4526 bpf_error("'esis' modifier applied to %s", typestr);
4527
4528 case Q_ISIS:
4529 bpf_error("'isis' modifier applied to %s", typestr);
4530
4531 case Q_CLNP:
4532 bpf_error("'clnp' modifier applied to %s", typestr);
4533
4534 case Q_STP:
4535 bpf_error("'stp' modifier applied to %s", typestr);
4536
4537 case Q_IPX:
4538 bpf_error("IPX host filtering not implemented");
4539
4540 case Q_NETBEUI:
4541 bpf_error("'netbeui' modifier applied to %s", typestr);
4542
4543 case Q_RADIO:
4544 bpf_error("'radio' modifier applied to %s", typestr);
4545
4546 default:
4547 abort();
4548 }
4549 /* NOTREACHED */
4550}
4551
4552#ifdef INET6
4553static struct block *
4554gen_host6(addr, mask, proto, dir, type)
4555 struct in6_addr *addr;
4556 struct in6_addr *mask;
4557 int proto;
4558 int dir;
4559 int type;
4560{
4561 const char *typestr;
4562
4563 if (type == Q_NET)
4564 typestr = "net";
4565 else
4566 typestr = "host";
4567
4568 switch (proto) {
4569
4570 case Q_DEFAULT:
4571 return gen_host6(addr, mask, Q_IPV6, dir, type);
4572
4573 case Q_IP:
4574 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4575
4576 case Q_RARP:
4577 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4578
4579 case Q_ARP:
4580 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4581
4582 case Q_SCTP:
4583 bpf_error("'sctp' modifier applied to %s", typestr);
4584
4585 case Q_TCP:
4586 bpf_error("'tcp' modifier applied to %s", typestr);
4587
4588 case Q_UDP:
4589 bpf_error("'udp' modifier applied to %s", typestr);
4590
4591 case Q_ICMP:
4592 bpf_error("'icmp' modifier applied to %s", typestr);
4593
4594 case Q_IGMP:
4595 bpf_error("'igmp' modifier applied to %s", typestr);
4596
4597 case Q_IGRP:
4598 bpf_error("'igrp' modifier applied to %s", typestr);
4599
4600 case Q_PIM:
4601 bpf_error("'pim' modifier applied to %s", typestr);
4602
4603 case Q_VRRP:
4604 bpf_error("'vrrp' modifier applied to %s", typestr);
4605
4606 case Q_CARP:
4607 bpf_error("'carp' modifier applied to %s", typestr);
4608
4609 case Q_ATALK:
4610 bpf_error("ATALK host filtering not implemented");
4611
4612 case Q_AARP:
4613 bpf_error("AARP host filtering not implemented");
4614
4615 case Q_DECNET:
4616 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4617
4618 case Q_SCA:
4619 bpf_error("SCA host filtering not implemented");
4620
4621 case Q_LAT:
4622 bpf_error("LAT host filtering not implemented");
4623
4624 case Q_MOPDL:
4625 bpf_error("MOPDL host filtering not implemented");
4626
4627 case Q_MOPRC:
4628 bpf_error("MOPRC host filtering not implemented");
4629
4630 case Q_IPV6:
4631 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4632
4633 case Q_ICMPV6:
4634 bpf_error("'icmp6' modifier applied to %s", typestr);
4635
4636 case Q_AH:
4637 bpf_error("'ah' modifier applied to %s", typestr);
4638
4639 case Q_ESP:
4640 bpf_error("'esp' modifier applied to %s", typestr);
4641
4642 case Q_ISO:
4643 bpf_error("ISO host filtering not implemented");
4644
4645 case Q_ESIS:
4646 bpf_error("'esis' modifier applied to %s", typestr);
4647
4648 case Q_ISIS:
4649 bpf_error("'isis' modifier applied to %s", typestr);
4650
4651 case Q_CLNP:
4652 bpf_error("'clnp' modifier applied to %s", typestr);
4653
4654 case Q_STP:
4655 bpf_error("'stp' modifier applied to %s", typestr);
4656
4657 case Q_IPX:
4658 bpf_error("IPX host filtering not implemented");
4659
4660 case Q_NETBEUI:
4661 bpf_error("'netbeui' modifier applied to %s", typestr);
4662
4663 case Q_RADIO:
4664 bpf_error("'radio' modifier applied to %s", typestr);
4665
4666 default:
4667 abort();
4668 }
4669 /* NOTREACHED */
4670}
4671#endif /*INET6*/
4672
4673#ifndef INET6
4674static struct block *
4675gen_gateway(eaddr, alist, proto, dir)
4676 const u_char *eaddr;
4677 bpf_u_int32 **alist;
4678 int proto;
4679 int dir;
4680{
4681 struct block *b0, *b1, *tmp;
4682
4683 if (dir != 0)
4684 bpf_error("direction applied to 'gateway'");
4685
4686 switch (proto) {
4687 case Q_DEFAULT:
4688 case Q_IP:
4689 case Q_ARP:
4690 case Q_RARP:
4691 switch (linktype) {
4692 case DLT_EN10MB:
4693 case DLT_NETANALYZER:
4694 case DLT_NETANALYZER_TRANSPARENT:
4695 b0 = gen_ehostop(eaddr, Q_OR);
4696 break;
4697 case DLT_FDDI:
4698 b0 = gen_fhostop(eaddr, Q_OR);
4699 break;
4700 case DLT_IEEE802:
4701 b0 = gen_thostop(eaddr, Q_OR);
4702 break;
4703 case DLT_IEEE802_11:
4704 case DLT_PRISM_HEADER:
4705 case DLT_IEEE802_11_RADIO_AVS:
4706 case DLT_IEEE802_11_RADIO:
4707 case DLT_PPI:
4708 b0 = gen_wlanhostop(eaddr, Q_OR);
4709 break;
4710 case DLT_SUNATM:
4711 if (!is_lane)
4712 bpf_error(
4713 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4714 /*
4715 * Check that the packet doesn't begin with an
4716 * LE Control marker. (We've already generated
4717 * a test for LANE.)
4718 */
4719 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4720 BPF_H, 0xFF00);
4721 gen_not(b1);
4722
4723 /*
4724 * Now check the MAC address.
4725 */
4726 b0 = gen_ehostop(eaddr, Q_OR);
4727 gen_and(b1, b0);
4728 break;
4729 case DLT_IP_OVER_FC:
4730 b0 = gen_ipfchostop(eaddr, Q_OR);
4731 break;
4732 default:
4733 bpf_error(
4734 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4735 }
4736 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4737 while (*alist) {
4738 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4739 Q_HOST);
4740 gen_or(b1, tmp);
4741 b1 = tmp;
4742 }
4743 gen_not(b1);
4744 gen_and(b0, b1);
4745 return b1;
4746 }
4747 bpf_error("illegal modifier of 'gateway'");
4748 /* NOTREACHED */
4749}
4750#endif
4751
4752struct block *
4753gen_proto_abbrev(proto)
4754 int proto;
4755{
4756 struct block *b0;
4757 struct block *b1;
4758
4759 switch (proto) {
4760
4761 case Q_SCTP:
4762 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4763#ifdef INET6
4764 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4765 gen_or(b0, b1);
4766#endif
4767 break;
4768
4769 case Q_TCP:
4770 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4771#ifdef INET6
4772 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4773 gen_or(b0, b1);
4774#endif
4775 break;
4776
4777 case Q_UDP:
4778 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4779#ifdef INET6
4780 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4781 gen_or(b0, b1);
4782#endif
4783 break;
4784
4785 case Q_ICMP:
4786 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4787 break;
4788
4789#ifndef IPPROTO_IGMP
4790#define IPPROTO_IGMP 2
4791#endif
4792
4793 case Q_IGMP:
4794 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4795 break;
4796
4797#ifndef IPPROTO_IGRP
4798#define IPPROTO_IGRP 9
4799#endif
4800 case Q_IGRP:
4801 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4802 break;
4803
4804#ifndef IPPROTO_PIM
4805#define IPPROTO_PIM 103
4806#endif
4807
4808 case Q_PIM:
4809 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4810#ifdef INET6
4811 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4812 gen_or(b0, b1);
4813#endif
4814 break;
4815
4816#ifndef IPPROTO_VRRP
4817#define IPPROTO_VRRP 112
4818#endif
4819
4820 case Q_VRRP:
4821 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4822 break;
4823
4824#ifndef IPPROTO_CARP
4825#define IPPROTO_CARP 112
4826#endif
4827
4828 case Q_CARP:
4829 b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
4830 break;
4831
4832 case Q_IP:
4833 b1 = gen_linktype(ETHERTYPE_IP);
4834 break;
4835
4836 case Q_ARP:
4837 b1 = gen_linktype(ETHERTYPE_ARP);
4838 break;
4839
4840 case Q_RARP:
4841 b1 = gen_linktype(ETHERTYPE_REVARP);
4842 break;
4843
4844 case Q_LINK:
4845 bpf_error("link layer applied in wrong context");
4846
4847 case Q_ATALK:
4848 b1 = gen_linktype(ETHERTYPE_ATALK);
4849 break;
4850
4851 case Q_AARP:
4852 b1 = gen_linktype(ETHERTYPE_AARP);
4853 break;
4854
4855 case Q_DECNET:
4856 b1 = gen_linktype(ETHERTYPE_DN);
4857 break;
4858
4859 case Q_SCA:
4860 b1 = gen_linktype(ETHERTYPE_SCA);
4861 break;
4862
4863 case Q_LAT:
4864 b1 = gen_linktype(ETHERTYPE_LAT);
4865 break;
4866
4867 case Q_MOPDL:
4868 b1 = gen_linktype(ETHERTYPE_MOPDL);
4869 break;
4870
4871 case Q_MOPRC:
4872 b1 = gen_linktype(ETHERTYPE_MOPRC);
4873 break;
4874
4875#ifdef INET6
4876 case Q_IPV6:
4877 b1 = gen_linktype(ETHERTYPE_IPV6);
4878 break;
4879
4880#ifndef IPPROTO_ICMPV6
4881#define IPPROTO_ICMPV6 58
4882#endif
4883 case Q_ICMPV6:
4884 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4885 break;
4886#endif /* INET6 */
4887
4888#ifndef IPPROTO_AH
4889#define IPPROTO_AH 51
4890#endif
4891 case Q_AH:
4892 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4893#ifdef INET6
4894 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4895 gen_or(b0, b1);
4896#endif
4897 break;
4898
4899#ifndef IPPROTO_ESP
4900#define IPPROTO_ESP 50
4901#endif
4902 case Q_ESP:
4903 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4904#ifdef INET6
4905 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4906 gen_or(b0, b1);
4907#endif
4908 break;
4909
4910 case Q_ISO:
4911 b1 = gen_linktype(LLCSAP_ISONS);
4912 break;
4913
4914 case Q_ESIS:
4915 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4916 break;
4917
4918 case Q_ISIS:
4919 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4920 break;
4921
4922 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4923 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4924 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4925 gen_or(b0, b1);
4926 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4927 gen_or(b0, b1);
4928 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4929 gen_or(b0, b1);
4930 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4931 gen_or(b0, b1);
4932 break;
4933
4934 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4935 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4936 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4937 gen_or(b0, b1);
4938 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4939 gen_or(b0, b1);
4940 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4941 gen_or(b0, b1);
4942 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4943 gen_or(b0, b1);
4944 break;
4945
4946 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4947 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4948 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4949 gen_or(b0, b1);
4950 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4951 gen_or(b0, b1);
4952 break;
4953
4954 case Q_ISIS_LSP:
4955 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4956 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4957 gen_or(b0, b1);
4958 break;
4959
4960 case Q_ISIS_SNP:
4961 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4962 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4963 gen_or(b0, b1);
4964 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4965 gen_or(b0, b1);
4966 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4967 gen_or(b0, b1);
4968 break;
4969
4970 case Q_ISIS_CSNP:
4971 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4972 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4973 gen_or(b0, b1);
4974 break;
4975
4976 case Q_ISIS_PSNP:
4977 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4978 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4979 gen_or(b0, b1);
4980 break;
4981
4982 case Q_CLNP:
4983 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
4984 break;
4985
4986 case Q_STP:
4987 b1 = gen_linktype(LLCSAP_8021D);
4988 break;
4989
4990 case Q_IPX:
4991 b1 = gen_linktype(LLCSAP_IPX);
4992 break;
4993
4994 case Q_NETBEUI:
4995 b1 = gen_linktype(LLCSAP_NETBEUI);
4996 break;
4997
4998 case Q_RADIO:
4999 bpf_error("'radio' is not a valid protocol type");
5000
5001 default:
5002 abort();
5003 }
5004 return b1;
5005}
5006
5007static struct block *
5008gen_ipfrag()
5009{
5010 struct slist *s;
5011 struct block *b;
5012
5013 /* not IPv4 frag other than the first frag */
5014 s = gen_load_a(OR_NET, 6, BPF_H);
5015 b = new_block(JMP(BPF_JSET));
5016 b->s.k = 0x1fff;
5017 b->stmts = s;
5018 gen_not(b);
5019
5020 return b;
5021}
5022
5023/*
5024 * Generate a comparison to a port value in the transport-layer header
5025 * at the specified offset from the beginning of that header.
5026 *
5027 * XXX - this handles a variable-length prefix preceding the link-layer
5028 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5029 * variable-length link-layer headers (such as Token Ring or 802.11
5030 * headers).
5031 */
5032static struct block *
5033gen_portatom(off, v)
5034 int off;
5035 bpf_int32 v;
5036{
5037 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
5038}
5039
5040#ifdef INET6
5041static struct block *
5042gen_portatom6(off, v)
5043 int off;
5044 bpf_int32 v;
5045{
5046 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
5047}
5048#endif/*INET6*/
5049
5050struct block *
5051gen_portop(port, proto, dir)
5052 int port, proto, dir;
5053{
5054 struct block *b0, *b1, *tmp;
5055
5056 /* ip proto 'proto' and not a fragment other than the first fragment */
5057 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5058 b0 = gen_ipfrag();
5059 gen_and(tmp, b0);
5060
5061 switch (dir) {
5062 case Q_SRC:
5063 b1 = gen_portatom(0, (bpf_int32)port);
5064 break;
5065
5066 case Q_DST:
5067 b1 = gen_portatom(2, (bpf_int32)port);
5068 break;
5069
5070 case Q_OR:
5071 case Q_DEFAULT:
5072 tmp = gen_portatom(0, (bpf_int32)port);
5073 b1 = gen_portatom(2, (bpf_int32)port);
5074 gen_or(tmp, b1);
5075 break;
5076
5077 case Q_AND:
5078 tmp = gen_portatom(0, (bpf_int32)port);
5079 b1 = gen_portatom(2, (bpf_int32)port);
5080 gen_and(tmp, b1);
5081 break;
5082
5083 default:
5084 abort();
5085 }
5086 gen_and(b0, b1);
5087
5088 return b1;
5089}
5090
5091static struct block *
5092gen_port(port, ip_proto, dir)
5093 int port;
5094 int ip_proto;
5095 int dir;
5096{
5097 struct block *b0, *b1, *tmp;
5098
5099 /*
5100 * ether proto ip
5101 *
5102 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5103 * not LLC encapsulation with LLCSAP_IP.
5104 *
5105 * For IEEE 802 networks - which includes 802.5 token ring
5106 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5107 * says that SNAP encapsulation is used, not LLC encapsulation
5108 * with LLCSAP_IP.
5109 *
5110 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5111 * RFC 2225 say that SNAP encapsulation is used, not LLC
5112 * encapsulation with LLCSAP_IP.
5113 *
5114 * So we always check for ETHERTYPE_IP.
5115 */
5116 b0 = gen_linktype(ETHERTYPE_IP);
5117
5118 switch (ip_proto) {
5119 case IPPROTO_UDP:
5120 case IPPROTO_TCP:
5121 case IPPROTO_SCTP:
5122 b1 = gen_portop(port, ip_proto, dir);
5123 break;
5124
5125 case PROTO_UNDEF:
5126 tmp = gen_portop(port, IPPROTO_TCP, dir);
5127 b1 = gen_portop(port, IPPROTO_UDP, dir);
5128 gen_or(tmp, b1);
5129 tmp = gen_portop(port, IPPROTO_SCTP, dir);
5130 gen_or(tmp, b1);
5131 break;
5132
5133 default:
5134 abort();
5135 }
5136 gen_and(b0, b1);
5137 return b1;
5138}
5139
5140#ifdef INET6
5141struct block *
5142gen_portop6(port, proto, dir)
5143 int port, proto, dir;
5144{
5145 struct block *b0, *b1, *tmp;
5146
5147 /* ip6 proto 'proto' */
5148 /* XXX - catch the first fragment of a fragmented packet? */
5149 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5150
5151 switch (dir) {
5152 case Q_SRC:
5153 b1 = gen_portatom6(0, (bpf_int32)port);
5154 break;
5155
5156 case Q_DST:
5157 b1 = gen_portatom6(2, (bpf_int32)port);
5158 break;
5159
5160 case Q_OR:
5161 case Q_DEFAULT:
5162 tmp = gen_portatom6(0, (bpf_int32)port);
5163 b1 = gen_portatom6(2, (bpf_int32)port);
5164 gen_or(tmp, b1);
5165 break;
5166
5167 case Q_AND:
5168 tmp = gen_portatom6(0, (bpf_int32)port);
5169 b1 = gen_portatom6(2, (bpf_int32)port);
5170 gen_and(tmp, b1);
5171 break;
5172
5173 default:
5174 abort();
5175 }
5176 gen_and(b0, b1);
5177
5178 return b1;
5179}
5180
5181static struct block *
5182gen_port6(port, ip_proto, dir)
5183 int port;
5184 int ip_proto;
5185 int dir;
5186{
5187 struct block *b0, *b1, *tmp;
5188
5189 /* link proto ip6 */
5190 b0 = gen_linktype(ETHERTYPE_IPV6);
5191
5192 switch (ip_proto) {
5193 case IPPROTO_UDP:
5194 case IPPROTO_TCP:
5195 case IPPROTO_SCTP:
5196 b1 = gen_portop6(port, ip_proto, dir);
5197 break;
5198
5199 case PROTO_UNDEF:
5200 tmp = gen_portop6(port, IPPROTO_TCP, dir);
5201 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5202 gen_or(tmp, b1);
5203 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5204 gen_or(tmp, b1);
5205 break;
5206
5207 default:
5208 abort();
5209 }
5210 gen_and(b0, b1);
5211 return b1;
5212}
5213#endif /* INET6 */
5214
5215/* gen_portrange code */
5216static struct block *
5217gen_portrangeatom(off, v1, v2)
5218 int off;
5219 bpf_int32 v1, v2;
5220{
5221 struct block *b1, *b2;
5222
5223 if (v1 > v2) {
5224 /*
5225 * Reverse the order of the ports, so v1 is the lower one.
5226 */
5227 bpf_int32 vtemp;
5228
5229 vtemp = v1;
5230 v1 = v2;
5231 v2 = vtemp;
5232 }
5233
5234 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5235 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5236
5237 gen_and(b1, b2);
5238
5239 return b2;
5240}
5241
5242struct block *
5243gen_portrangeop(port1, port2, proto, dir)
5244 int port1, port2;
5245 int proto;
5246 int dir;
5247{
5248 struct block *b0, *b1, *tmp;
5249
5250 /* ip proto 'proto' and not a fragment other than the first fragment */
5251 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5252 b0 = gen_ipfrag();
5253 gen_and(tmp, b0);
5254
5255 switch (dir) {
5256 case Q_SRC:
5257 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5258 break;
5259
5260 case Q_DST:
5261 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5262 break;
5263
5264 case Q_OR:
5265 case Q_DEFAULT:
5266 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5267 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5268 gen_or(tmp, b1);
5269 break;
5270
5271 case Q_AND:
5272 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5273 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5274 gen_and(tmp, b1);
5275 break;
5276
5277 default:
5278 abort();
5279 }
5280 gen_and(b0, b1);
5281
5282 return b1;
5283}
5284
5285static struct block *
5286gen_portrange(port1, port2, ip_proto, dir)
5287 int port1, port2;
5288 int ip_proto;
5289 int dir;
5290{
5291 struct block *b0, *b1, *tmp;
5292
5293 /* link proto ip */
5294 b0 = gen_linktype(ETHERTYPE_IP);
5295
5296 switch (ip_proto) {
5297 case IPPROTO_UDP:
5298 case IPPROTO_TCP:
5299 case IPPROTO_SCTP:
5300 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5301 break;
5302
5303 case PROTO_UNDEF:
5304 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5305 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5306 gen_or(tmp, b1);
5307 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5308 gen_or(tmp, b1);
5309 break;
5310
5311 default:
5312 abort();
5313 }
5314 gen_and(b0, b1);
5315 return b1;
5316}
5317
5318#ifdef INET6
5319static struct block *
5320gen_portrangeatom6(off, v1, v2)
5321 int off;
5322 bpf_int32 v1, v2;
5323{
5324 struct block *b1, *b2;
5325
5326 if (v1 > v2) {
5327 /*
5328 * Reverse the order of the ports, so v1 is the lower one.
5329 */
5330 bpf_int32 vtemp;
5331
5332 vtemp = v1;
5333 v1 = v2;
5334 v2 = vtemp;
5335 }
5336
5337 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5338 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5339
5340 gen_and(b1, b2);
5341
5342 return b2;
5343}
5344
5345struct block *
5346gen_portrangeop6(port1, port2, proto, dir)
5347 int port1, port2;
5348 int proto;
5349 int dir;
5350{
5351 struct block *b0, *b1, *tmp;
5352
5353 /* ip6 proto 'proto' */
5354 /* XXX - catch the first fragment of a fragmented packet? */
5355 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5356
5357 switch (dir) {
5358 case Q_SRC:
5359 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5360 break;
5361
5362 case Q_DST:
5363 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5364 break;
5365
5366 case Q_OR:
5367 case Q_DEFAULT:
5368 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5369 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5370 gen_or(tmp, b1);
5371 break;
5372
5373 case Q_AND:
5374 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5375 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5376 gen_and(tmp, b1);
5377 break;
5378
5379 default:
5380 abort();
5381 }
5382 gen_and(b0, b1);
5383
5384 return b1;
5385}
5386
5387static struct block *
5388gen_portrange6(port1, port2, ip_proto, dir)
5389 int port1, port2;
5390 int ip_proto;
5391 int dir;
5392{
5393 struct block *b0, *b1, *tmp;
5394
5395 /* link proto ip6 */
5396 b0 = gen_linktype(ETHERTYPE_IPV6);
5397
5398 switch (ip_proto) {
5399 case IPPROTO_UDP:
5400 case IPPROTO_TCP:
5401 case IPPROTO_SCTP:
5402 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5403 break;
5404
5405 case PROTO_UNDEF:
5406 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5407 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5408 gen_or(tmp, b1);
5409 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5410 gen_or(tmp, b1);
5411 break;
5412
5413 default:
5414 abort();
5415 }
5416 gen_and(b0, b1);
5417 return b1;
5418}
5419#endif /* INET6 */
5420
5421static int
5422lookup_proto(name, proto)
5423 register const char *name;
5424 register int proto;
5425{
5426 register int v;
5427
5428 switch (proto) {
5429
5430 case Q_DEFAULT:
5431 case Q_IP:
5432 case Q_IPV6:
5433 v = pcap_nametoproto(name);
5434 if (v == PROTO_UNDEF)
5435 bpf_error("unknown ip proto '%s'", name);
5436 break;
5437
5438 case Q_LINK:
5439 /* XXX should look up h/w protocol type based on linktype */
5440 v = pcap_nametoeproto(name);
5441 if (v == PROTO_UNDEF) {
5442 v = pcap_nametollc(name);
5443 if (v == PROTO_UNDEF)
5444 bpf_error("unknown ether proto '%s'", name);
5445 }
5446 break;
5447
5448 case Q_ISO:
5449 if (strcmp(name, "esis") == 0)
5450 v = ISO9542_ESIS;
5451 else if (strcmp(name, "isis") == 0)
5452 v = ISO10589_ISIS;
5453 else if (strcmp(name, "clnp") == 0)
5454 v = ISO8473_CLNP;
5455 else
5456 bpf_error("unknown osi proto '%s'", name);
5457 break;
5458
5459 default:
5460 v = PROTO_UNDEF;
5461 break;
5462 }
5463 return v;
5464}
5465
5466#if 0
5467struct stmt *
5468gen_joinsp(s, n)
5469 struct stmt **s;
5470 int n;
5471{
5472 return NULL;
5473}
5474#endif
5475
5476static struct block *
5477gen_protochain(v, proto, dir)
5478 int v;
5479 int proto;
5480 int dir;
5481{
5482#ifdef NO_PROTOCHAIN
5483 return gen_proto(v, proto, dir);
5484#else
5485 struct block *b0, *b;
5486 struct slist *s[100];
5487 int fix2, fix3, fix4, fix5;
5488 int ahcheck, again, end;
5489 int i, max;
5490 int reg2 = alloc_reg();
5491
5492 memset(s, 0, sizeof(s));
5493 fix2 = fix3 = fix4 = fix5 = 0;
5494
5495 switch (proto) {
5496 case Q_IP:
5497 case Q_IPV6:
5498 break;
5499 case Q_DEFAULT:
5500 b0 = gen_protochain(v, Q_IP, dir);
5501 b = gen_protochain(v, Q_IPV6, dir);
5502 gen_or(b0, b);
5503 return b;
5504 default:
5505 bpf_error("bad protocol applied for 'protochain'");
5506 /*NOTREACHED*/
5507 }
5508
5509 /*
5510 * We don't handle variable-length prefixes before the link-layer
5511 * header, or variable-length link-layer headers, here yet.
5512 * We might want to add BPF instructions to do the protochain
5513 * work, to simplify that and, on platforms that have a BPF
5514 * interpreter with the new instructions, let the filtering
5515 * be done in the kernel. (We already require a modified BPF
5516 * engine to do the protochain stuff, to support backward
5517 * branches, and backward branch support is unlikely to appear
5518 * in kernel BPF engines.)
5519 */
5520 switch (linktype) {
5521
5522 case DLT_IEEE802_11:
5523 case DLT_PRISM_HEADER:
5524 case DLT_IEEE802_11_RADIO_AVS:
5525 case DLT_IEEE802_11_RADIO:
5526 case DLT_PPI:
5527 bpf_error("'protochain' not supported with 802.11");
5528 }
5529
5530 no_optimize = 1; /*this code is not compatible with optimzer yet */
5531
5532 /*
5533 * s[0] is a dummy entry to protect other BPF insn from damage
5534 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5535 * hard to find interdependency made by jump table fixup.
5536 */
5537 i = 0;
5538 s[i] = new_stmt(0); /*dummy*/
5539 i++;
5540
5541 switch (proto) {
5542 case Q_IP:
5543 b0 = gen_linktype(ETHERTYPE_IP);
5544
5545 /* A = ip->ip_p */
5546 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5547 s[i]->s.k = off_macpl + off_nl + 9;
5548 i++;
5549 /* X = ip->ip_hl << 2 */
5550 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5551 s[i]->s.k = off_macpl + off_nl;
5552 i++;
5553 break;
5554#ifdef INET6
5555 case Q_IPV6:
5556 b0 = gen_linktype(ETHERTYPE_IPV6);
5557
5558 /* A = ip6->ip_nxt */
5559 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5560 s[i]->s.k = off_macpl + off_nl + 6;
5561 i++;
5562 /* X = sizeof(struct ip6_hdr) */
5563 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5564 s[i]->s.k = 40;
5565 i++;
5566 break;
5567#endif
5568 default:
5569 bpf_error("unsupported proto to gen_protochain");
5570 /*NOTREACHED*/
5571 }
5572
5573 /* again: if (A == v) goto end; else fall through; */
5574 again = i;
5575 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5576 s[i]->s.k = v;
5577 s[i]->s.jt = NULL; /*later*/
5578 s[i]->s.jf = NULL; /*update in next stmt*/
5579 fix5 = i;
5580 i++;
5581
5582#ifndef IPPROTO_NONE
5583#define IPPROTO_NONE 59
5584#endif
5585 /* if (A == IPPROTO_NONE) goto end */
5586 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5587 s[i]->s.jt = NULL; /*later*/
5588 s[i]->s.jf = NULL; /*update in next stmt*/
5589 s[i]->s.k = IPPROTO_NONE;
5590 s[fix5]->s.jf = s[i];
5591 fix2 = i;
5592 i++;
5593
5594#ifdef INET6
5595 if (proto == Q_IPV6) {
5596 int v6start, v6end, v6advance, j;
5597
5598 v6start = i;
5599 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5600 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5601 s[i]->s.jt = NULL; /*later*/
5602 s[i]->s.jf = NULL; /*update in next stmt*/
5603 s[i]->s.k = IPPROTO_HOPOPTS;
5604 s[fix2]->s.jf = s[i];
5605 i++;
5606 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5607 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5608 s[i]->s.jt = NULL; /*later*/
5609 s[i]->s.jf = NULL; /*update in next stmt*/
5610 s[i]->s.k = IPPROTO_DSTOPTS;
5611 i++;
5612 /* if (A == IPPROTO_ROUTING) goto v6advance */
5613 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5614 s[i]->s.jt = NULL; /*later*/
5615 s[i]->s.jf = NULL; /*update in next stmt*/
5616 s[i]->s.k = IPPROTO_ROUTING;
5617 i++;
5618 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5619 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5620 s[i]->s.jt = NULL; /*later*/
5621 s[i]->s.jf = NULL; /*later*/
5622 s[i]->s.k = IPPROTO_FRAGMENT;
5623 fix3 = i;
5624 v6end = i;
5625 i++;
5626
5627 /* v6advance: */
5628 v6advance = i;
5629
5630 /*
5631 * in short,
5632 * A = P[X + packet head];
5633 * X = X + (P[X + packet head + 1] + 1) * 8;
5634 */
5635 /* A = P[X + packet head] */
5636 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5637 s[i]->s.k = off_macpl + off_nl;
5638 i++;
5639 /* MEM[reg2] = A */
5640 s[i] = new_stmt(BPF_ST);
5641 s[i]->s.k = reg2;
5642 i++;
5643 /* A = P[X + packet head + 1]; */
5644 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5645 s[i]->s.k = off_macpl + off_nl + 1;
5646 i++;
5647 /* A += 1 */
5648 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5649 s[i]->s.k = 1;
5650 i++;
5651 /* A *= 8 */
5652 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5653 s[i]->s.k = 8;
5654 i++;
5655 /* A += X */
5656 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
5657 s[i]->s.k = 0;
5658 i++;
5659 /* X = A; */
5660 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5661 i++;
5662 /* A = MEM[reg2] */
5663 s[i] = new_stmt(BPF_LD|BPF_MEM);
5664 s[i]->s.k = reg2;
5665 i++;
5666
5667 /* goto again; (must use BPF_JA for backward jump) */
5668 s[i] = new_stmt(BPF_JMP|BPF_JA);
5669 s[i]->s.k = again - i - 1;
5670 s[i - 1]->s.jf = s[i];
5671 i++;
5672
5673 /* fixup */
5674 for (j = v6start; j <= v6end; j++)
5675 s[j]->s.jt = s[v6advance];
5676 } else
5677#endif
5678 {
5679 /* nop */
5680 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5681 s[i]->s.k = 0;
5682 s[fix2]->s.jf = s[i];
5683 i++;
5684 }
5685
5686 /* ahcheck: */
5687 ahcheck = i;
5688 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5689 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5690 s[i]->s.jt = NULL; /*later*/
5691 s[i]->s.jf = NULL; /*later*/
5692 s[i]->s.k = IPPROTO_AH;
5693 if (fix3)
5694 s[fix3]->s.jf = s[ahcheck];
5695 fix4 = i;
5696 i++;
5697
5698 /*
5699 * in short,
5700 * A = P[X];
5701 * X = X + (P[X + 1] + 2) * 4;
5702 */
5703 /* A = X */
5704 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5705 i++;
5706 /* A = P[X + packet head]; */
5707 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5708 s[i]->s.k = off_macpl + off_nl;
5709 i++;
5710 /* MEM[reg2] = A */
5711 s[i] = new_stmt(BPF_ST);
5712 s[i]->s.k = reg2;
5713 i++;
5714 /* A = X */
5715 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5716 i++;
5717 /* A += 1 */
5718 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5719 s[i]->s.k = 1;
5720 i++;
5721 /* X = A */
5722 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5723 i++;
5724 /* A = P[X + packet head] */
5725 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5726 s[i]->s.k = off_macpl + off_nl;
5727 i++;
5728 /* A += 2 */
5729 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5730 s[i]->s.k = 2;
5731 i++;
5732 /* A *= 4 */
5733 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5734 s[i]->s.k = 4;
5735 i++;
5736 /* X = A; */
5737 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5738 i++;
5739 /* A = MEM[reg2] */
5740 s[i] = new_stmt(BPF_LD|BPF_MEM);
5741 s[i]->s.k = reg2;
5742 i++;
5743
5744 /* goto again; (must use BPF_JA for backward jump) */
5745 s[i] = new_stmt(BPF_JMP|BPF_JA);
5746 s[i]->s.k = again - i - 1;
5747 i++;
5748
5749 /* end: nop */
5750 end = i;
5751 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5752 s[i]->s.k = 0;
5753 s[fix2]->s.jt = s[end];
5754 s[fix4]->s.jf = s[end];
5755 s[fix5]->s.jt = s[end];
5756 i++;
5757
5758 /*
5759 * make slist chain
5760 */
5761 max = i;
5762 for (i = 0; i < max - 1; i++)
5763 s[i]->next = s[i + 1];
5764 s[max - 1]->next = NULL;
5765
5766 /*
5767 * emit final check
5768 */
5769 b = new_block(JMP(BPF_JEQ));
5770 b->stmts = s[1]; /*remember, s[0] is dummy*/
5771 b->s.k = v;
5772
5773 free_reg(reg2);
5774
5775 gen_and(b0, b);
5776 return b;
5777#endif
5778}
5779
5780static struct block *
5781gen_check_802_11_data_frame()
5782{
5783 struct slist *s;
5784 struct block *b0, *b1;
5785
5786 /*
5787 * A data frame has the 0x08 bit (b3) in the frame control field set
5788 * and the 0x04 bit (b2) clear.
5789 */
5790 s = gen_load_a(OR_LINK, 0, BPF_B);
5791 b0 = new_block(JMP(BPF_JSET));
5792 b0->s.k = 0x08;
5793 b0->stmts = s;
5794
5795 s = gen_load_a(OR_LINK, 0, BPF_B);
5796 b1 = new_block(JMP(BPF_JSET));
5797 b1->s.k = 0x04;
5798 b1->stmts = s;
5799 gen_not(b1);
5800
5801 gen_and(b1, b0);
5802
5803 return b0;
5804}
5805
5806/*
5807 * Generate code that checks whether the packet is a packet for protocol
5808 * <proto> and whether the type field in that protocol's header has
5809 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5810 * IP packet and checks the protocol number in the IP header against <v>.
5811 *
5812 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5813 * against Q_IP and Q_IPV6.
5814 */
5815static struct block *
5816gen_proto(v, proto, dir)
5817 int v;
5818 int proto;
5819 int dir;
5820{
5821 struct block *b0, *b1;
5822
5823 if (dir != Q_DEFAULT)
5824 bpf_error("direction applied to 'proto'");
5825
5826 switch (proto) {
5827 case Q_DEFAULT:
5828#ifdef INET6
5829 b0 = gen_proto(v, Q_IP, dir);
5830 b1 = gen_proto(v, Q_IPV6, dir);
5831 gen_or(b0, b1);
5832 return b1;
5833#else
5834 /*FALLTHROUGH*/
5835#endif
5836 case Q_IP:
5837 /*
5838 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5839 * not LLC encapsulation with LLCSAP_IP.
5840 *
5841 * For IEEE 802 networks - which includes 802.5 token ring
5842 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5843 * says that SNAP encapsulation is used, not LLC encapsulation
5844 * with LLCSAP_IP.
5845 *
5846 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5847 * RFC 2225 say that SNAP encapsulation is used, not LLC
5848 * encapsulation with LLCSAP_IP.
5849 *
5850 * So we always check for ETHERTYPE_IP.
5851 */
5852 b0 = gen_linktype(ETHERTYPE_IP);
5853#ifndef CHASE_CHAIN
5854 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5855#else
5856 b1 = gen_protochain(v, Q_IP);
5857#endif
5858 gen_and(b0, b1);
5859 return b1;
5860
5861 case Q_ISO:
5862 switch (linktype) {
5863
5864 case DLT_FRELAY:
5865 /*
5866 * Frame Relay packets typically have an OSI
5867 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5868 * generates code to check for all the OSI
5869 * NLPIDs, so calling it and then adding a check
5870 * for the particular NLPID for which we're
5871 * looking is bogus, as we can just check for
5872 * the NLPID.
5873 *
5874 * What we check for is the NLPID and a frame
5875 * control field value of UI, i.e. 0x03 followed
5876 * by the NLPID.
5877 *
5878 * XXX - assumes a 2-byte Frame Relay header with
5879 * DLCI and flags. What if the address is longer?
5880 *
5881 * XXX - what about SNAP-encapsulated frames?
5882 */
5883 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5884 /*NOTREACHED*/
5885 break;
5886
5887 case DLT_C_HDLC:
5888 /*
5889 * Cisco uses an Ethertype lookalike - for OSI,
5890 * it's 0xfefe.
5891 */
5892 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5893 /* OSI in C-HDLC is stuffed with a fudge byte */
5894 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5895 gen_and(b0, b1);
5896 return b1;
5897
5898 default:
5899 b0 = gen_linktype(LLCSAP_ISONS);
5900 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5901 gen_and(b0, b1);
5902 return b1;
5903 }
5904
5905 case Q_ISIS:
5906 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5907 /*
5908 * 4 is the offset of the PDU type relative to the IS-IS
5909 * header.
5910 */
5911 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5912 gen_and(b0, b1);
5913 return b1;
5914
5915 case Q_ARP:
5916 bpf_error("arp does not encapsulate another protocol");
5917 /* NOTREACHED */
5918
5919 case Q_RARP:
5920 bpf_error("rarp does not encapsulate another protocol");
5921 /* NOTREACHED */
5922
5923 case Q_ATALK:
5924 bpf_error("atalk encapsulation is not specifiable");
5925 /* NOTREACHED */
5926
5927 case Q_DECNET:
5928 bpf_error("decnet encapsulation is not specifiable");
5929 /* NOTREACHED */
5930
5931 case Q_SCA:
5932 bpf_error("sca does not encapsulate another protocol");
5933 /* NOTREACHED */
5934
5935 case Q_LAT:
5936 bpf_error("lat does not encapsulate another protocol");
5937 /* NOTREACHED */
5938
5939 case Q_MOPRC:
5940 bpf_error("moprc does not encapsulate another protocol");
5941 /* NOTREACHED */
5942
5943 case Q_MOPDL:
5944 bpf_error("mopdl does not encapsulate another protocol");
5945 /* NOTREACHED */
5946
5947 case Q_LINK:
5948 return gen_linktype(v);
5949
5950 case Q_UDP:
5951 bpf_error("'udp proto' is bogus");
5952 /* NOTREACHED */
5953
5954 case Q_TCP:
5955 bpf_error("'tcp proto' is bogus");
5956 /* NOTREACHED */
5957
5958 case Q_SCTP:
5959 bpf_error("'sctp proto' is bogus");
5960 /* NOTREACHED */
5961
5962 case Q_ICMP:
5963 bpf_error("'icmp proto' is bogus");
5964 /* NOTREACHED */
5965
5966 case Q_IGMP:
5967 bpf_error("'igmp proto' is bogus");
5968 /* NOTREACHED */
5969
5970 case Q_IGRP:
5971 bpf_error("'igrp proto' is bogus");
5972 /* NOTREACHED */
5973
5974 case Q_PIM:
5975 bpf_error("'pim proto' is bogus");
5976 /* NOTREACHED */
5977
5978 case Q_VRRP:
5979 bpf_error("'vrrp proto' is bogus");
5980 /* NOTREACHED */
5981
5982 case Q_CARP:
5983 bpf_error("'carp proto' is bogus");
5984 /* NOTREACHED */
5985
5986#ifdef INET6
5987 case Q_IPV6:
5988 b0 = gen_linktype(ETHERTYPE_IPV6);
5989#ifndef CHASE_CHAIN
5990 b1 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
5991#else
5992 b1 = gen_protochain(v, Q_IPV6);
5993#endif
5994 gen_and(b0, b1);
5995 return b1;
5996
5997 case Q_ICMPV6:
5998 bpf_error("'icmp6 proto' is bogus");
5999#endif /* INET6 */
6000
6001 case Q_AH:
6002 bpf_error("'ah proto' is bogus");
6003
6004 case Q_ESP:
6005 bpf_error("'ah proto' is bogus");
6006
6007 case Q_STP:
6008 bpf_error("'stp proto' is bogus");
6009
6010 case Q_IPX:
6011 bpf_error("'ipx proto' is bogus");
6012
6013 case Q_NETBEUI:
6014 bpf_error("'netbeui proto' is bogus");
6015
6016 case Q_RADIO:
6017 bpf_error("'radio proto' is bogus");
6018
6019 default:
6020 abort();
6021 /* NOTREACHED */
6022 }
6023 /* NOTREACHED */
6024}
6025
6026struct block *
6027gen_scode(name, q)
6028 register const char *name;
6029 struct qual q;
6030{
6031 int proto = q.proto;
6032 int dir = q.dir;
6033 int tproto;
6034 u_char *eaddr;
6035 bpf_u_int32 mask, addr;
6036#ifndef INET6
6037 bpf_u_int32 **alist;
6038#else
6039 int tproto6;
6040 struct sockaddr_in *sin4;
6041 struct sockaddr_in6 *sin6;
6042 struct addrinfo *res, *res0;
6043 struct in6_addr mask128;
6044#endif /*INET6*/
6045 struct block *b, *tmp;
6046 int port, real_proto;
6047 int port1, port2;
6048
6049 switch (q.addr) {
6050
6051 case Q_NET:
6052 addr = pcap_nametonetaddr(name);
6053 if (addr == 0)
6054 bpf_error("unknown network '%s'", name);
6055 /* Left justify network addr and calculate its network mask */
6056 mask = 0xffffffff;
6057 while (addr && (addr & 0xff000000) == 0) {
6058 addr <<= 8;
6059 mask <<= 8;
6060 }
6061 return gen_host(addr, mask, proto, dir, q.addr);
6062
6063 case Q_DEFAULT:
6064 case Q_HOST:
6065 if (proto == Q_LINK) {
6066 switch (linktype) {
6067
6068 case DLT_EN10MB:
6069 case DLT_NETANALYZER:
6070 case DLT_NETANALYZER_TRANSPARENT:
6071 eaddr = pcap_ether_hostton(name);
6072 if (eaddr == NULL)
6073 bpf_error(
6074 "unknown ether host '%s'", name);
6075 b = gen_ehostop(eaddr, dir);
6076 free(eaddr);
6077 return b;
6078
6079 case DLT_FDDI:
6080 eaddr = pcap_ether_hostton(name);
6081 if (eaddr == NULL)
6082 bpf_error(
6083 "unknown FDDI host '%s'", name);
6084 b = gen_fhostop(eaddr, dir);
6085 free(eaddr);
6086 return b;
6087
6088 case DLT_IEEE802:
6089 eaddr = pcap_ether_hostton(name);
6090 if (eaddr == NULL)
6091 bpf_error(
6092 "unknown token ring host '%s'", name);
6093 b = gen_thostop(eaddr, dir);
6094 free(eaddr);
6095 return b;
6096
6097 case DLT_IEEE802_11:
6098 case DLT_PRISM_HEADER:
6099 case DLT_IEEE802_11_RADIO_AVS:
6100 case DLT_IEEE802_11_RADIO:
6101 case DLT_PPI:
6102 eaddr = pcap_ether_hostton(name);
6103 if (eaddr == NULL)
6104 bpf_error(
6105 "unknown 802.11 host '%s'", name);
6106 b = gen_wlanhostop(eaddr, dir);
6107 free(eaddr);
6108 return b;
6109
6110 case DLT_IP_OVER_FC:
6111 eaddr = pcap_ether_hostton(name);
6112 if (eaddr == NULL)
6113 bpf_error(
6114 "unknown Fibre Channel host '%s'", name);
6115 b = gen_ipfchostop(eaddr, dir);
6116 free(eaddr);
6117 return b;
6118
6119 case DLT_SUNATM:
6120 if (!is_lane)
6121 break;
6122
6123 /*
6124 * Check that the packet doesn't begin
6125 * with an LE Control marker. (We've
6126 * already generated a test for LANE.)
6127 */
6128 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6129 BPF_H, 0xFF00);
6130 gen_not(tmp);
6131
6132 eaddr = pcap_ether_hostton(name);
6133 if (eaddr == NULL)
6134 bpf_error(
6135 "unknown ether host '%s'", name);
6136 b = gen_ehostop(eaddr, dir);
6137 gen_and(tmp, b);
6138 free(eaddr);
6139 return b;
6140 }
6141
6142 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6143 } else if (proto == Q_DECNET) {
6144 unsigned short dn_addr = __pcap_nametodnaddr(name);
6145 /*
6146 * I don't think DECNET hosts can be multihomed, so
6147 * there is no need to build up a list of addresses
6148 */
6149 return (gen_host(dn_addr, 0, proto, dir, q.addr));
6150 } else {
6151#ifndef INET6
6152 alist = pcap_nametoaddr(name);
6153 if (alist == NULL || *alist == NULL)
6154 bpf_error("unknown host '%s'", name);
6155 tproto = proto;
6156 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
6157 tproto = Q_IP;
6158 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
6159 while (*alist) {
6160 tmp = gen_host(**alist++, 0xffffffff,
6161 tproto, dir, q.addr);
6162 gen_or(b, tmp);
6163 b = tmp;
6164 }
6165 return b;
6166#else
6167 memset(&mask128, 0xff, sizeof(mask128));
6168 res0 = res = pcap_nametoaddrinfo(name);
6169 if (res == NULL)
6170 bpf_error("unknown host '%s'", name);
6171 ai = res;
6172 b = tmp = NULL;
6173 tproto = tproto6 = proto;
6174 if (off_linktype == -1 && tproto == Q_DEFAULT) {
6175 tproto = Q_IP;
6176 tproto6 = Q_IPV6;
6177 }
6178 for (res = res0; res; res = res->ai_next) {
6179 switch (res->ai_family) {
6180 case AF_INET:
6181 if (tproto == Q_IPV6)
6182 continue;
6183
6184 sin4 = (struct sockaddr_in *)
6185 res->ai_addr;
6186 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
6187 0xffffffff, tproto, dir, q.addr);
6188 break;
6189 case AF_INET6:
6190 if (tproto6 == Q_IP)
6191 continue;
6192
6193 sin6 = (struct sockaddr_in6 *)
6194 res->ai_addr;
6195 tmp = gen_host6(&sin6->sin6_addr,
6196 &mask128, tproto6, dir, q.addr);
6197 break;
6198 default:
6199 continue;
6200 }
6201 if (b)
6202 gen_or(b, tmp);
6203 b = tmp;
6204 }
6205 ai = NULL;
6206 freeaddrinfo(res0);
6207 if (b == NULL) {
6208 bpf_error("unknown host '%s'%s", name,
6209 (proto == Q_DEFAULT)
6210 ? ""
6211 : " for specified address family");
6212 }
6213 return b;
6214#endif /*INET6*/
6215 }
6216
6217 case Q_PORT:
6218 if (proto != Q_DEFAULT &&
6219 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6220 bpf_error("illegal qualifier of 'port'");
6221 if (pcap_nametoport(name, &port, &real_proto) == 0)
6222 bpf_error("unknown port '%s'", name);
6223 if (proto == Q_UDP) {
6224 if (real_proto == IPPROTO_TCP)
6225 bpf_error("port '%s' is tcp", name);
6226 else if (real_proto == IPPROTO_SCTP)
6227 bpf_error("port '%s' is sctp", name);
6228 else
6229 /* override PROTO_UNDEF */
6230 real_proto = IPPROTO_UDP;
6231 }
6232 if (proto == Q_TCP) {
6233 if (real_proto == IPPROTO_UDP)
6234 bpf_error("port '%s' is udp", name);
6235
6236 else if (real_proto == IPPROTO_SCTP)
6237 bpf_error("port '%s' is sctp", name);
6238 else
6239 /* override PROTO_UNDEF */
6240 real_proto = IPPROTO_TCP;
6241 }
6242 if (proto == Q_SCTP) {
6243 if (real_proto == IPPROTO_UDP)
6244 bpf_error("port '%s' is udp", name);
6245
6246 else if (real_proto == IPPROTO_TCP)
6247 bpf_error("port '%s' is tcp", name);
6248 else
6249 /* override PROTO_UNDEF */
6250 real_proto = IPPROTO_SCTP;
6251 }
6252 if (port < 0)
6253 bpf_error("illegal port number %d < 0", port);
6254 if (port > 65535)
6255 bpf_error("illegal port number %d > 65535", port);
6256#ifndef INET6
6257 return gen_port(port, real_proto, dir);
6258#else
6259 b = gen_port(port, real_proto, dir);
6260 gen_or(gen_port6(port, real_proto, dir), b);
6261 return b;
6262#endif /* INET6 */
6263
6264 case Q_PORTRANGE:
6265 if (proto != Q_DEFAULT &&
6266 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6267 bpf_error("illegal qualifier of 'portrange'");
6268 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6269 bpf_error("unknown port in range '%s'", name);
6270 if (proto == Q_UDP) {
6271 if (real_proto == IPPROTO_TCP)
6272 bpf_error("port in range '%s' is tcp", name);
6273 else if (real_proto == IPPROTO_SCTP)
6274 bpf_error("port in range '%s' is sctp", name);
6275 else
6276 /* override PROTO_UNDEF */
6277 real_proto = IPPROTO_UDP;
6278 }
6279 if (proto == Q_TCP) {
6280 if (real_proto == IPPROTO_UDP)
6281 bpf_error("port in range '%s' is udp", name);
6282 else if (real_proto == IPPROTO_SCTP)
6283 bpf_error("port in range '%s' is sctp", name);
6284 else
6285 /* override PROTO_UNDEF */
6286 real_proto = IPPROTO_TCP;
6287 }
6288 if (proto == Q_SCTP) {
6289 if (real_proto == IPPROTO_UDP)
6290 bpf_error("port in range '%s' is udp", name);
6291 else if (real_proto == IPPROTO_TCP)
6292 bpf_error("port in range '%s' is tcp", name);
6293 else
6294 /* override PROTO_UNDEF */
6295 real_proto = IPPROTO_SCTP;
6296 }
6297 if (port1 < 0)
6298 bpf_error("illegal port number %d < 0", port1);
6299 if (port1 > 65535)
6300 bpf_error("illegal port number %d > 65535", port1);
6301 if (port2 < 0)
6302 bpf_error("illegal port number %d < 0", port2);
6303 if (port2 > 65535)
6304 bpf_error("illegal port number %d > 65535", port2);
6305
6306#ifndef INET6
6307 return gen_portrange(port1, port2, real_proto, dir);
6308#else
6309 b = gen_portrange(port1, port2, real_proto, dir);
6310 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6311 return b;
6312#endif /* INET6 */
6313
6314 case Q_GATEWAY:
6315#ifndef INET6
6316 eaddr = pcap_ether_hostton(name);
6317 if (eaddr == NULL)
6318 bpf_error("unknown ether host: %s", name);
6319
6320 alist = pcap_nametoaddr(name);
6321 if (alist == NULL || *alist == NULL)
6322 bpf_error("unknown host '%s'", name);
6323 b = gen_gateway(eaddr, alist, proto, dir);
6324 free(eaddr);
6325 return b;
6326#else
6327 bpf_error("'gateway' not supported in this configuration");
6328#endif /*INET6*/
6329
6330 case Q_PROTO:
6331 real_proto = lookup_proto(name, proto);
6332 if (real_proto >= 0)
6333 return gen_proto(real_proto, proto, dir);
6334 else
6335 bpf_error("unknown protocol: %s", name);
6336
6337 case Q_PROTOCHAIN:
6338 real_proto = lookup_proto(name, proto);
6339 if (real_proto >= 0)
6340 return gen_protochain(real_proto, proto, dir);
6341 else
6342 bpf_error("unknown protocol: %s", name);
6343
6344 case Q_UNDEF:
6345 syntax();
6346 /* NOTREACHED */
6347 }
6348 abort();
6349 /* NOTREACHED */
6350}
6351
6352struct block *
6353gen_mcode(s1, s2, masklen, q)
6354 register const char *s1, *s2;
6355 register int masklen;
6356 struct qual q;
6357{
6358 register int nlen, mlen;
6359 bpf_u_int32 n, m;
6360
6361 nlen = __pcap_atoin(s1, &n);
6362 /* Promote short ipaddr */
6363 n <<= 32 - nlen;
6364
6365 if (s2 != NULL) {
6366 mlen = __pcap_atoin(s2, &m);
6367 /* Promote short ipaddr */
6368 m <<= 32 - mlen;
6369 if ((n & ~m) != 0)
6370 bpf_error("non-network bits set in \"%s mask %s\"",
6371 s1, s2);
6372 } else {
6373 /* Convert mask len to mask */
6374 if (masklen > 32)
6375 bpf_error("mask length must be <= 32");
6376 if (masklen == 0) {
6377 /*
6378 * X << 32 is not guaranteed by C to be 0; it's
6379 * undefined.
6380 */
6381 m = 0;
6382 } else
6383 m = 0xffffffff << (32 - masklen);
6384 if ((n & ~m) != 0)
6385 bpf_error("non-network bits set in \"%s/%d\"",
6386 s1, masklen);
6387 }
6388
6389 switch (q.addr) {
6390
6391 case Q_NET:
6392 return gen_host(n, m, q.proto, q.dir, q.addr);
6393
6394 default:
6395 bpf_error("Mask syntax for networks only");
6396 /* NOTREACHED */
6397 }
6398 /* NOTREACHED */
6399 return NULL;
6400}
6401
6402struct block *
6403gen_ncode(s, v, q)
6404 register const char *s;
6405 bpf_u_int32 v;
6406 struct qual q;
6407{
6408 bpf_u_int32 mask;
6409 int proto = q.proto;
6410 int dir = q.dir;
6411 register int vlen;
6412
6413 if (s == NULL)
6414 vlen = 32;
6415 else if (q.proto == Q_DECNET)
6416 vlen = __pcap_atodn(s, &v);
6417 else
6418 vlen = __pcap_atoin(s, &v);
6419
6420 switch (q.addr) {
6421
6422 case Q_DEFAULT:
6423 case Q_HOST:
6424 case Q_NET:
6425 if (proto == Q_DECNET)
6426 return gen_host(v, 0, proto, dir, q.addr);
6427 else if (proto == Q_LINK) {
6428 bpf_error("illegal link layer address");
6429 } else {
6430 mask = 0xffffffff;
6431 if (s == NULL && q.addr == Q_NET) {
6432 /* Promote short net number */
6433 while (v && (v & 0xff000000) == 0) {
6434 v <<= 8;
6435 mask <<= 8;
6436 }
6437 } else {
6438 /* Promote short ipaddr */
6439 v <<= 32 - vlen;
6440 mask <<= 32 - vlen;
6441 }
6442 return gen_host(v, mask, proto, dir, q.addr);
6443 }
6444
6445 case Q_PORT:
6446 if (proto == Q_UDP)
6447 proto = IPPROTO_UDP;
6448 else if (proto == Q_TCP)
6449 proto = IPPROTO_TCP;
6450 else if (proto == Q_SCTP)
6451 proto = IPPROTO_SCTP;
6452 else if (proto == Q_DEFAULT)
6453 proto = PROTO_UNDEF;
6454 else
6455 bpf_error("illegal qualifier of 'port'");
6456
6457 if (v > 65535)
6458 bpf_error("illegal port number %u > 65535", v);
6459
6460#ifndef INET6
6461 return gen_port((int)v, proto, dir);
6462#else
6463 {
6464 struct block *b;
6465 b = gen_port((int)v, proto, dir);
6466 gen_or(gen_port6((int)v, proto, dir), b);
6467 return b;
6468 }
6469#endif /* INET6 */
6470
6471 case Q_PORTRANGE:
6472 if (proto == Q_UDP)
6473 proto = IPPROTO_UDP;
6474 else if (proto == Q_TCP)
6475 proto = IPPROTO_TCP;
6476 else if (proto == Q_SCTP)
6477 proto = IPPROTO_SCTP;
6478 else if (proto == Q_DEFAULT)
6479 proto = PROTO_UNDEF;
6480 else
6481 bpf_error("illegal qualifier of 'portrange'");
6482
6483 if (v > 65535)
6484 bpf_error("illegal port number %u > 65535", v);
6485
6486#ifndef INET6
6487 return gen_portrange((int)v, (int)v, proto, dir);
6488#else
6489 {
6490 struct block *b;
6491 b = gen_portrange((int)v, (int)v, proto, dir);
6492 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6493 return b;
6494 }
6495#endif /* INET6 */
6496
6497 case Q_GATEWAY:
6498 bpf_error("'gateway' requires a name");
6499 /* NOTREACHED */
6500
6501 case Q_PROTO:
6502 return gen_proto((int)v, proto, dir);
6503
6504 case Q_PROTOCHAIN:
6505 return gen_protochain((int)v, proto, dir);
6506
6507 case Q_UNDEF:
6508 syntax();
6509 /* NOTREACHED */
6510
6511 default:
6512 abort();
6513 /* NOTREACHED */
6514 }
6515 /* NOTREACHED */
6516}
6517
6518#ifdef INET6
6519struct block *
6520gen_mcode6(s1, s2, masklen, q)
6521 register const char *s1, *s2;
6522 register int masklen;
6523 struct qual q;
6524{
6525 struct addrinfo *res;
6526 struct in6_addr *addr;
6527 struct in6_addr mask;
6528 struct block *b;
6529 u_int32_t *a, *m;
6530
6531 if (s2)
6532 bpf_error("no mask %s supported", s2);
6533
6534 res = pcap_nametoaddrinfo(s1);
6535 if (!res)
6536 bpf_error("invalid ip6 address %s", s1);
6537 ai = res;
6538 if (res->ai_next)
6539 bpf_error("%s resolved to multiple address", s1);
6540 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6541
6542 if (sizeof(mask) * 8 < masklen)
6543 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6544 memset(&mask, 0, sizeof(mask));
6545 memset(&mask, 0xff, masklen / 8);
6546 if (masklen % 8) {
6547 mask.s6_addr[masklen / 8] =
6548 (0xff << (8 - masklen % 8)) & 0xff;
6549 }
6550
6551 a = (u_int32_t *)addr;
6552 m = (u_int32_t *)&mask;
6553 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6554 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6555 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6556 }
6557
6558 switch (q.addr) {
6559
6560 case Q_DEFAULT:
6561 case Q_HOST:
6562 if (masklen != 128)
6563 bpf_error("Mask syntax for networks only");
6564 /* FALLTHROUGH */
6565
6566 case Q_NET:
6567 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6568 ai = NULL;
6569 freeaddrinfo(res);
6570 return b;
6571
6572 default:
6573 bpf_error("invalid qualifier against IPv6 address");
6574 /* NOTREACHED */
6575 }
6576 return NULL;
6577}
6578#endif /*INET6*/
6579
6580struct block *
6581gen_ecode(eaddr, q)
6582 register const u_char *eaddr;
6583 struct qual q;
6584{
6585 struct block *b, *tmp;
6586
6587 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6588 switch (linktype) {
6589 case DLT_EN10MB:
6590 case DLT_NETANALYZER:
6591 case DLT_NETANALYZER_TRANSPARENT:
6592 return gen_ehostop(eaddr, (int)q.dir);
6593 case DLT_FDDI:
6594 return gen_fhostop(eaddr, (int)q.dir);
6595 case DLT_IEEE802:
6596 return gen_thostop(eaddr, (int)q.dir);
6597 case DLT_IEEE802_11:
6598 case DLT_PRISM_HEADER:
6599 case DLT_IEEE802_11_RADIO_AVS:
6600 case DLT_IEEE802_11_RADIO:
6601 case DLT_PPI:
6602 return gen_wlanhostop(eaddr, (int)q.dir);
6603 case DLT_SUNATM:
6604 if (is_lane) {
6605 /*
6606 * Check that the packet doesn't begin with an
6607 * LE Control marker. (We've already generated
6608 * a test for LANE.)
6609 */
6610 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6611 0xFF00);
6612 gen_not(tmp);
6613
6614 /*
6615 * Now check the MAC address.
6616 */
6617 b = gen_ehostop(eaddr, (int)q.dir);
6618 gen_and(tmp, b);
6619 return b;
6620 }
6621 break;
6622 case DLT_IP_OVER_FC:
6623 return gen_ipfchostop(eaddr, (int)q.dir);
6624 default:
6625 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6626 break;
6627 }
6628 }
6629 bpf_error("ethernet address used in non-ether expression");
6630 /* NOTREACHED */
6631 return NULL;
6632}
6633
6634void
6635sappend(s0, s1)
6636 struct slist *s0, *s1;
6637{
6638 /*
6639 * This is definitely not the best way to do this, but the
6640 * lists will rarely get long.
6641 */
6642 while (s0->next)
6643 s0 = s0->next;
6644 s0->next = s1;
6645}
6646
6647static struct slist *
6648xfer_to_x(a)
6649 struct arth *a;
6650{
6651 struct slist *s;
6652
6653 s = new_stmt(BPF_LDX|BPF_MEM);
6654 s->s.k = a->regno;
6655 return s;
6656}
6657
6658static struct slist *
6659xfer_to_a(a)
6660 struct arth *a;
6661{
6662 struct slist *s;
6663
6664 s = new_stmt(BPF_LD|BPF_MEM);
6665 s->s.k = a->regno;
6666 return s;
6667}
6668
6669/*
6670 * Modify "index" to use the value stored into its register as an
6671 * offset relative to the beginning of the header for the protocol
6672 * "proto", and allocate a register and put an item "size" bytes long
6673 * (1, 2, or 4) at that offset into that register, making it the register
6674 * for "index".
6675 */
6676struct arth *
6677gen_load(proto, inst, size)
6678 int proto;
6679 struct arth *inst;
6680 int size;
6681{
6682 struct slist *s, *tmp;
6683 struct block *b;
6684 int regno = alloc_reg();
6685
6686 free_reg(inst->regno);
6687 switch (size) {
6688
6689 default:
6690 bpf_error("data size must be 1, 2, or 4");
6691
6692 case 1:
6693 size = BPF_B;
6694 break;
6695
6696 case 2:
6697 size = BPF_H;
6698 break;
6699
6700 case 4:
6701 size = BPF_W;
6702 break;
6703 }
6704 switch (proto) {
6705 default:
6706 bpf_error("unsupported index operation");
6707
6708 case Q_RADIO:
6709 /*
6710 * The offset is relative to the beginning of the packet
6711 * data, if we have a radio header. (If we don't, this
6712 * is an error.)
6713 */
6714 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6715 linktype != DLT_IEEE802_11_RADIO &&
6716 linktype != DLT_PRISM_HEADER)
6717 bpf_error("radio information not present in capture");
6718
6719 /*
6720 * Load into the X register the offset computed into the
6721 * register specified by "index".
6722 */
6723 s = xfer_to_x(inst);
6724
6725 /*
6726 * Load the item at that offset.
6727 */
6728 tmp = new_stmt(BPF_LD|BPF_IND|size);
6729 sappend(s, tmp);
6730 sappend(inst->s, s);
6731 break;
6732
6733 case Q_LINK:
6734 /*
6735 * The offset is relative to the beginning of
6736 * the link-layer header.
6737 *
6738 * XXX - what about ATM LANE? Should the index be
6739 * relative to the beginning of the AAL5 frame, so
6740 * that 0 refers to the beginning of the LE Control
6741 * field, or relative to the beginning of the LAN
6742 * frame, so that 0 refers, for Ethernet LANE, to
6743 * the beginning of the destination address?
6744 */
6745 s = gen_llprefixlen();
6746
6747 /*
6748 * If "s" is non-null, it has code to arrange that the
6749 * X register contains the length of the prefix preceding
6750 * the link-layer header. Add to it the offset computed
6751 * into the register specified by "index", and move that
6752 * into the X register. Otherwise, just load into the X
6753 * register the offset computed into the register specified
6754 * by "index".
6755 */
6756 if (s != NULL) {
6757 sappend(s, xfer_to_a(inst));
6758 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6759 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6760 } else
6761 s = xfer_to_x(inst);
6762
6763 /*
6764 * Load the item at the sum of the offset we've put in the
6765 * X register and the offset of the start of the link
6766 * layer header (which is 0 if the radio header is
6767 * variable-length; that header length is what we put
6768 * into the X register and then added to the index).
6769 */
6770 tmp = new_stmt(BPF_LD|BPF_IND|size);
6771 tmp->s.k = off_ll;
6772 sappend(s, tmp);
6773 sappend(inst->s, s);
6774 break;
6775
6776 case Q_IP:
6777 case Q_ARP:
6778 case Q_RARP:
6779 case Q_ATALK:
6780 case Q_DECNET:
6781 case Q_SCA:
6782 case Q_LAT:
6783 case Q_MOPRC:
6784 case Q_MOPDL:
6785#ifdef INET6
6786 case Q_IPV6:
6787#endif
6788 /*
6789 * The offset is relative to the beginning of
6790 * the network-layer header.
6791 * XXX - are there any cases where we want
6792 * off_nl_nosnap?
6793 */
6794 s = gen_off_macpl();
6795
6796 /*
6797 * If "s" is non-null, it has code to arrange that the
6798 * X register contains the offset of the MAC-layer
6799 * payload. Add to it the offset computed into the
6800 * register specified by "index", and move that into
6801 * the X register. Otherwise, just load into the X
6802 * register the offset computed into the register specified
6803 * by "index".
6804 */
6805 if (s != NULL) {
6806 sappend(s, xfer_to_a(inst));
6807 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6808 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6809 } else
6810 s = xfer_to_x(inst);
6811
6812 /*
6813 * Load the item at the sum of the offset we've put in the
6814 * X register, the offset of the start of the network
6815 * layer header from the beginning of the MAC-layer
6816 * payload, and the purported offset of the start of the
6817 * MAC-layer payload (which might be 0 if there's a
6818 * variable-length prefix before the link-layer header
6819 * or the link-layer header itself is variable-length;
6820 * the variable-length offset of the start of the
6821 * MAC-layer payload is what we put into the X register
6822 * and then added to the index).
6823 */
6824 tmp = new_stmt(BPF_LD|BPF_IND|size);
6825 tmp->s.k = off_macpl + off_nl;
6826 sappend(s, tmp);
6827 sappend(inst->s, s);
6828
6829 /*
6830 * Do the computation only if the packet contains
6831 * the protocol in question.
6832 */
6833 b = gen_proto_abbrev(proto);
6834 if (inst->b)
6835 gen_and(inst->b, b);
6836 inst->b = b;
6837 break;
6838
6839 case Q_SCTP:
6840 case Q_TCP:
6841 case Q_UDP:
6842 case Q_ICMP:
6843 case Q_IGMP:
6844 case Q_IGRP:
6845 case Q_PIM:
6846 case Q_VRRP:
6847 case Q_CARP:
6848 /*
6849 * The offset is relative to the beginning of
6850 * the transport-layer header.
6851 *
6852 * Load the X register with the length of the IPv4 header
6853 * (plus the offset of the link-layer header, if it's
6854 * a variable-length header), in bytes.
6855 *
6856 * XXX - are there any cases where we want
6857 * off_nl_nosnap?
6858 * XXX - we should, if we're built with
6859 * IPv6 support, generate code to load either
6860 * IPv4, IPv6, or both, as appropriate.
6861 */
6862 s = gen_loadx_iphdrlen();
6863
6864 /*
6865 * The X register now contains the sum of the length
6866 * of any variable-length header preceding the link-layer
6867 * header, any variable-length link-layer header, and the
6868 * length of the network-layer header.
6869 *
6870 * Load into the A register the offset relative to
6871 * the beginning of the transport layer header,
6872 * add the X register to that, move that to the
6873 * X register, and load with an offset from the
6874 * X register equal to the offset of the network
6875 * layer header relative to the beginning of
6876 * the MAC-layer payload plus the fixed-length
6877 * portion of the offset of the MAC-layer payload
6878 * from the beginning of the raw packet data.
6879 */
6880 sappend(s, xfer_to_a(inst));
6881 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6882 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6883 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6884 tmp->s.k = off_macpl + off_nl;
6885 sappend(inst->s, s);
6886
6887 /*
6888 * Do the computation only if the packet contains
6889 * the protocol in question - which is true only
6890 * if this is an IP datagram and is the first or
6891 * only fragment of that datagram.
6892 */
6893 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6894 if (inst->b)
6895 gen_and(inst->b, b);
6896#ifdef INET6
6897 gen_and(gen_proto_abbrev(Q_IP), b);
6898#endif
6899 inst->b = b;
6900 break;
6901#ifdef INET6
6902 case Q_ICMPV6:
6903 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6904 /*NOTREACHED*/
6905#endif
6906 }
6907 inst->regno = regno;
6908 s = new_stmt(BPF_ST);
6909 s->s.k = regno;
6910 sappend(inst->s, s);
6911
6912 return inst;
6913}
6914
6915struct block *
6916gen_relation(code, a0, a1, reversed)
6917 int code;
6918 struct arth *a0, *a1;
6919 int reversed;
6920{
6921 struct slist *s0, *s1, *s2;
6922 struct block *b, *tmp;
6923
6924 s0 = xfer_to_x(a1);
6925 s1 = xfer_to_a(a0);
6926 if (code == BPF_JEQ) {
6927 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6928 b = new_block(JMP(code));
6929 sappend(s1, s2);
6930 }
6931 else
6932 b = new_block(BPF_JMP|code|BPF_X);
6933 if (reversed)
6934 gen_not(b);
6935
6936 sappend(s0, s1);
6937 sappend(a1->s, s0);
6938 sappend(a0->s, a1->s);
6939
6940 b->stmts = a0->s;
6941
6942 free_reg(a0->regno);
6943 free_reg(a1->regno);
6944
6945 /* 'and' together protocol checks */
6946 if (a0->b) {
6947 if (a1->b) {
6948 gen_and(a0->b, tmp = a1->b);
6949 }
6950 else
6951 tmp = a0->b;
6952 } else
6953 tmp = a1->b;
6954
6955 if (tmp)
6956 gen_and(tmp, b);
6957
6958 return b;
6959}
6960
6961struct arth *
6962gen_loadlen()
6963{
6964 int regno = alloc_reg();
6965 struct arth *a = (struct arth *)newchunk(sizeof(*a));
6966 struct slist *s;
6967
6968 s = new_stmt(BPF_LD|BPF_LEN);
6969 s->next = new_stmt(BPF_ST);
6970 s->next->s.k = regno;
6971 a->s = s;
6972 a->regno = regno;
6973
6974 return a;
6975}
6976
6977struct arth *
6978gen_loadi(val)
6979 int val;
6980{
6981 struct arth *a;
6982 struct slist *s;
6983 int reg;
6984
6985 a = (struct arth *)newchunk(sizeof(*a));
6986
6987 reg = alloc_reg();
6988
6989 s = new_stmt(BPF_LD|BPF_IMM);
6990 s->s.k = val;
6991 s->next = new_stmt(BPF_ST);
6992 s->next->s.k = reg;
6993 a->s = s;
6994 a->regno = reg;
6995
6996 return a;
6997}
6998
6999struct arth *
7000gen_neg(a)
7001 struct arth *a;
7002{
7003 struct slist *s;
7004
7005 s = xfer_to_a(a);
7006 sappend(a->s, s);
7007 s = new_stmt(BPF_ALU|BPF_NEG);
7008 s->s.k = 0;
7009 sappend(a->s, s);
7010 s = new_stmt(BPF_ST);
7011 s->s.k = a->regno;
7012 sappend(a->s, s);
7013
7014 return a;
7015}
7016
7017struct arth *
7018gen_arth(code, a0, a1)
7019 int code;
7020 struct arth *a0, *a1;
7021{
7022 struct slist *s0, *s1, *s2;
7023
7024 s0 = xfer_to_x(a1);
7025 s1 = xfer_to_a(a0);
7026 s2 = new_stmt(BPF_ALU|BPF_X|code);
7027
7028 sappend(s1, s2);
7029 sappend(s0, s1);
7030 sappend(a1->s, s0);
7031 sappend(a0->s, a1->s);
7032
7033 free_reg(a0->regno);
7034 free_reg(a1->regno);
7035
7036 s0 = new_stmt(BPF_ST);
7037 a0->regno = s0->s.k = alloc_reg();
7038 sappend(a0->s, s0);
7039
7040 return a0;
7041}
7042
7043/*
7044 * Here we handle simple allocation of the scratch registers.
7045 * If too many registers are alloc'd, the allocator punts.
7046 */
7047static int regused[BPF_MEMWORDS];
7048static int curreg;
7049
7050/*
7051 * Initialize the table of used registers and the current register.
7052 */
7053static void
7054init_regs()
7055{
7056 curreg = 0;
7057 memset(regused, 0, sizeof regused);
7058}
7059
7060/*
7061 * Return the next free register.
7062 */
7063static int
7064alloc_reg()
7065{
7066 int n = BPF_MEMWORDS;
7067
7068 while (--n >= 0) {
7069 if (regused[curreg])
7070 curreg = (curreg + 1) % BPF_MEMWORDS;
7071 else {
7072 regused[curreg] = 1;
7073 return curreg;
7074 }
7075 }
7076 bpf_error("too many registers needed to evaluate expression");
7077 /* NOTREACHED */
7078 return 0;
7079}
7080
7081/*
7082 * Return a register to the table so it can
7083 * be used later.
7084 */
7085static void
7086free_reg(n)
7087 int n;
7088{
7089 regused[n] = 0;
7090}
7091
7092static struct block *
7093gen_len(jmp, n)
7094 int jmp, n;
7095{
7096 struct slist *s;
7097 struct block *b;
7098
7099 s = new_stmt(BPF_LD|BPF_LEN);
7100 b = new_block(JMP(jmp));
7101 b->stmts = s;
7102 b->s.k = n;
7103
7104 return b;
7105}
7106
7107struct block *
7108gen_greater(n)
7109 int n;
7110{
7111 return gen_len(BPF_JGE, n);
7112}
7113
7114/*
7115 * Actually, this is less than or equal.
7116 */
7117struct block *
7118gen_less(n)
7119 int n;
7120{
7121 struct block *b;
7122
7123 b = gen_len(BPF_JGT, n);
7124 gen_not(b);
7125
7126 return b;
7127}
7128
7129/*
7130 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7131 * the beginning of the link-layer header.
7132 * XXX - that means you can't test values in the radiotap header, but
7133 * as that header is difficult if not impossible to parse generally
7134 * without a loop, that might not be a severe problem. A new keyword
7135 * "radio" could be added for that, although what you'd really want
7136 * would be a way of testing particular radio header values, which
7137 * would generate code appropriate to the radio header in question.
7138 */
7139struct block *
7140gen_byteop(op, idx, val)
7141 int op, idx, val;
7142{
7143 struct block *b;
7144 struct slist *s;
7145
7146 switch (op) {
7147 default:
7148 abort();
7149
7150 case '=':
7151 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7152
7153 case '<':
7154 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7155 return b;
7156
7157 case '>':
7158 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7159 return b;
7160
7161 case '|':
7162 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
7163 break;
7164
7165 case '&':
7166 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
7167 break;
7168 }
7169 s->s.k = val;
7170 b = new_block(JMP(BPF_JEQ));
7171 b->stmts = s;
7172 gen_not(b);
7173
7174 return b;
7175}
7176
7177static u_char abroadcast[] = { 0x0 };
7178
7179struct block *
7180gen_broadcast(proto)
7181 int proto;
7182{
7183 bpf_u_int32 hostmask;
7184 struct block *b0, *b1, *b2;
7185 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7186
7187 switch (proto) {
7188
7189 case Q_DEFAULT:
7190 case Q_LINK:
7191 switch (linktype) {
7192 case DLT_ARCNET:
7193 case DLT_ARCNET_LINUX:
7194 return gen_ahostop(abroadcast, Q_DST);
7195 case DLT_EN10MB:
7196 case DLT_NETANALYZER:
7197 case DLT_NETANALYZER_TRANSPARENT:
7198 return gen_ehostop(ebroadcast, Q_DST);
7199 case DLT_FDDI:
7200 return gen_fhostop(ebroadcast, Q_DST);
7201 case DLT_IEEE802:
7202 return gen_thostop(ebroadcast, Q_DST);
7203 case DLT_IEEE802_11:
7204 case DLT_PRISM_HEADER:
7205 case DLT_IEEE802_11_RADIO_AVS:
7206 case DLT_IEEE802_11_RADIO:
7207 case DLT_PPI:
7208 return gen_wlanhostop(ebroadcast, Q_DST);
7209 case DLT_IP_OVER_FC:
7210 return gen_ipfchostop(ebroadcast, Q_DST);
7211 case DLT_SUNATM:
7212 if (is_lane) {
7213 /*
7214 * Check that the packet doesn't begin with an
7215 * LE Control marker. (We've already generated
7216 * a test for LANE.)
7217 */
7218 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7219 BPF_H, 0xFF00);
7220 gen_not(b1);
7221
7222 /*
7223 * Now check the MAC address.
7224 */
7225 b0 = gen_ehostop(ebroadcast, Q_DST);
7226 gen_and(b1, b0);
7227 return b0;
7228 }
7229 break;
7230 default:
7231 bpf_error("not a broadcast link");
7232 }
7233 break;
7234
7235 case Q_IP:
7236 /*
7237 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7238 * as an indication that we don't know the netmask, and fail
7239 * in that case.
7240 */
7241 if (netmask == PCAP_NETMASK_UNKNOWN)
7242 bpf_error("netmask not known, so 'ip broadcast' not supported");
7243 b0 = gen_linktype(ETHERTYPE_IP);
7244 hostmask = ~netmask;
7245 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7246 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7247 (bpf_int32)(~0 & hostmask), hostmask);
7248 gen_or(b1, b2);
7249 gen_and(b0, b2);
7250 return b2;
7251 }
7252 bpf_error("only link-layer/IP broadcast filters supported");
7253 /* NOTREACHED */
7254 return NULL;
7255}
7256
7257/*
7258 * Generate code to test the low-order bit of a MAC address (that's
7259 * the bottom bit of the *first* byte).
7260 */
7261static struct block *
7262gen_mac_multicast(offset)
7263 int offset;
7264{
7265 register struct block *b0;
7266 register struct slist *s;
7267
7268 /* link[offset] & 1 != 0 */
7269 s = gen_load_a(OR_LINK, offset, BPF_B);
7270 b0 = new_block(JMP(BPF_JSET));
7271 b0->s.k = 1;
7272 b0->stmts = s;
7273 return b0;
7274}
7275
7276struct block *
7277gen_multicast(proto)
7278 int proto;
7279{
7280 register struct block *b0, *b1, *b2;
7281 register struct slist *s;
7282
7283 switch (proto) {
7284
7285 case Q_DEFAULT:
7286 case Q_LINK:
7287 switch (linktype) {
7288 case DLT_ARCNET:
7289 case DLT_ARCNET_LINUX:
7290 /* all ARCnet multicasts use the same address */
7291 return gen_ahostop(abroadcast, Q_DST);
7292 case DLT_EN10MB:
7293 case DLT_NETANALYZER:
7294 case DLT_NETANALYZER_TRANSPARENT:
7295 /* ether[0] & 1 != 0 */
7296 return gen_mac_multicast(0);
7297 case DLT_FDDI:
7298 /*
7299 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7300 *
7301 * XXX - was that referring to bit-order issues?
7302 */
7303 /* fddi[1] & 1 != 0 */
7304 return gen_mac_multicast(1);
7305 case DLT_IEEE802:
7306 /* tr[2] & 1 != 0 */
7307 return gen_mac_multicast(2);
7308 case DLT_IEEE802_11:
7309 case DLT_PRISM_HEADER:
7310 case DLT_IEEE802_11_RADIO_AVS:
7311 case DLT_IEEE802_11_RADIO:
7312 case DLT_PPI:
7313 /*
7314 * Oh, yuk.
7315 *
7316 * For control frames, there is no DA.
7317 *
7318 * For management frames, DA is at an
7319 * offset of 4 from the beginning of
7320 * the packet.
7321 *
7322 * For data frames, DA is at an offset
7323 * of 4 from the beginning of the packet
7324 * if To DS is clear and at an offset of
7325 * 16 from the beginning of the packet
7326 * if To DS is set.
7327 */
7328
7329 /*
7330 * Generate the tests to be done for data frames.
7331 *
7332 * First, check for To DS set, i.e. "link[1] & 0x01".
7333 */
7334 s = gen_load_a(OR_LINK, 1, BPF_B);
7335 b1 = new_block(JMP(BPF_JSET));
7336 b1->s.k = 0x01; /* To DS */
7337 b1->stmts = s;
7338
7339 /*
7340 * If To DS is set, the DA is at 16.
7341 */
7342 b0 = gen_mac_multicast(16);
7343 gen_and(b1, b0);
7344
7345 /*
7346 * Now, check for To DS not set, i.e. check
7347 * "!(link[1] & 0x01)".
7348 */
7349 s = gen_load_a(OR_LINK, 1, BPF_B);
7350 b2 = new_block(JMP(BPF_JSET));
7351 b2->s.k = 0x01; /* To DS */
7352 b2->stmts = s;
7353 gen_not(b2);
7354
7355 /*
7356 * If To DS is not set, the DA is at 4.
7357 */
7358 b1 = gen_mac_multicast(4);
7359 gen_and(b2, b1);
7360
7361 /*
7362 * Now OR together the last two checks. That gives
7363 * the complete set of checks for data frames.
7364 */
7365 gen_or(b1, b0);
7366
7367 /*
7368 * Now check for a data frame.
7369 * I.e, check "link[0] & 0x08".
7370 */
7371 s = gen_load_a(OR_LINK, 0, BPF_B);
7372 b1 = new_block(JMP(BPF_JSET));
7373 b1->s.k = 0x08;
7374 b1->stmts = s;
7375
7376 /*
7377 * AND that with the checks done for data frames.
7378 */
7379 gen_and(b1, b0);
7380
7381 /*
7382 * If the high-order bit of the type value is 0, this
7383 * is a management frame.
7384 * I.e, check "!(link[0] & 0x08)".
7385 */
7386 s = gen_load_a(OR_LINK, 0, BPF_B);
7387 b2 = new_block(JMP(BPF_JSET));
7388 b2->s.k = 0x08;
7389 b2->stmts = s;
7390 gen_not(b2);
7391
7392 /*
7393 * For management frames, the DA is at 4.
7394 */
7395 b1 = gen_mac_multicast(4);
7396 gen_and(b2, b1);
7397
7398 /*
7399 * OR that with the checks done for data frames.
7400 * That gives the checks done for management and
7401 * data frames.
7402 */
7403 gen_or(b1, b0);
7404
7405 /*
7406 * If the low-order bit of the type value is 1,
7407 * this is either a control frame or a frame
7408 * with a reserved type, and thus not a
7409 * frame with an SA.
7410 *
7411 * I.e., check "!(link[0] & 0x04)".
7412 */
7413 s = gen_load_a(OR_LINK, 0, BPF_B);
7414 b1 = new_block(JMP(BPF_JSET));
7415 b1->s.k = 0x04;
7416 b1->stmts = s;
7417 gen_not(b1);
7418
7419 /*
7420 * AND that with the checks for data and management
7421 * frames.
7422 */
7423 gen_and(b1, b0);
7424 return b0;
7425 case DLT_IP_OVER_FC:
7426 b0 = gen_mac_multicast(2);
7427 return b0;
7428 case DLT_SUNATM:
7429 if (is_lane) {
7430 /*
7431 * Check that the packet doesn't begin with an
7432 * LE Control marker. (We've already generated
7433 * a test for LANE.)
7434 */
7435 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7436 BPF_H, 0xFF00);
7437 gen_not(b1);
7438
7439 /* ether[off_mac] & 1 != 0 */
7440 b0 = gen_mac_multicast(off_mac);
7441 gen_and(b1, b0);
7442 return b0;
7443 }
7444 break;
7445 default:
7446 break;
7447 }
7448 /* Link not known to support multicasts */
7449 break;
7450
7451 case Q_IP:
7452 b0 = gen_linktype(ETHERTYPE_IP);
7453 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7454 gen_and(b0, b1);
7455 return b1;
7456
7457#ifdef INET6
7458 case Q_IPV6:
7459 b0 = gen_linktype(ETHERTYPE_IPV6);
7460 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7461 gen_and(b0, b1);
7462 return b1;
7463#endif /* INET6 */
7464 }
7465 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7466 /* NOTREACHED */
7467 return NULL;
7468}
7469
7470/*
7471 * generate command for inbound/outbound. It's here so we can
7472 * make it link-type specific. 'dir' = 0 implies "inbound",
7473 * = 1 implies "outbound".
7474 */
7475struct block *
7476gen_inbound(dir)
7477 int dir;
7478{
7479 register struct block *b0;
7480
7481 /*
7482 * Only some data link types support inbound/outbound qualifiers.
7483 */
7484 switch (linktype) {
7485 case DLT_SLIP:
7486 b0 = gen_relation(BPF_JEQ,
7487 gen_load(Q_LINK, gen_loadi(0), 1),
7488 gen_loadi(0),
7489 dir);
7490 break;
7491
7492 case DLT_IPNET:
7493 if (dir) {
7494 /* match outgoing packets */
7495 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
7496 } else {
7497 /* match incoming packets */
7498 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
7499 }
7500 break;
7501
7502 case DLT_LINUX_SLL:
7503 if (dir) {
7504 /*
7505 * Match packets sent by this machine.
7506 */
7507 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7508 } else {
7509 /*
7510 * Match packets sent to this machine.
7511 * (No broadcast or multicast packets, or
7512 * packets sent to some other machine and
7513 * received promiscuously.)
7514 *
7515 * XXX - packets sent to other machines probably
7516 * shouldn't be matched, but what about broadcast
7517 * or multicast packets we received?
7518 */
7519 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_HOST);
7520 }
7521 break;
7522
7523#ifdef HAVE_NET_PFVAR_H
7524 case DLT_PFLOG:
7525 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7526 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7527 break;
7528#endif
7529
7530 case DLT_PPP_PPPD:
7531 if (dir) {
7532 /* match outgoing packets */
7533 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7534 } else {
7535 /* match incoming packets */
7536 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7537 }
7538 break;
7539
7540 case DLT_JUNIPER_MFR:
7541 case DLT_JUNIPER_MLFR:
7542 case DLT_JUNIPER_MLPPP:
7543 case DLT_JUNIPER_ATM1:
7544 case DLT_JUNIPER_ATM2:
7545 case DLT_JUNIPER_PPPOE:
7546 case DLT_JUNIPER_PPPOE_ATM:
7547 case DLT_JUNIPER_GGSN:
7548 case DLT_JUNIPER_ES:
7549 case DLT_JUNIPER_MONITOR:
7550 case DLT_JUNIPER_SERVICES:
7551 case DLT_JUNIPER_ETHER:
7552 case DLT_JUNIPER_PPP:
7553 case DLT_JUNIPER_FRELAY:
7554 case DLT_JUNIPER_CHDLC:
7555 case DLT_JUNIPER_VP:
7556 case DLT_JUNIPER_ST:
7557 case DLT_JUNIPER_ISM:
7558 case DLT_JUNIPER_VS:
7559 case DLT_JUNIPER_SRX_E2E:
7560 case DLT_JUNIPER_FIBRECHANNEL:
7561 case DLT_JUNIPER_ATM_CEMIC:
7562
7563 /* juniper flags (including direction) are stored
7564 * the byte after the 3-byte magic number */
7565 if (dir) {
7566 /* match outgoing packets */
7567 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7568 } else {
7569 /* match incoming packets */
7570 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7571 }
7572 break;
7573
7574 default:
7575 bpf_error("inbound/outbound not supported on linktype %d",
7576 linktype);
7577 b0 = NULL;
7578 /* NOTREACHED */
7579 }
7580 return (b0);
7581}
7582
7583#ifdef HAVE_NET_PFVAR_H
7584/* PF firewall log matched interface */
7585struct block *
7586gen_pf_ifname(const char *ifname)
7587{
7588 struct block *b0;
7589 u_int len, off;
7590
7591 if (linktype != DLT_PFLOG) {
7592 bpf_error("ifname supported only on PF linktype");
7593 /* NOTREACHED */
7594 }
7595 len = sizeof(((struct pfloghdr *)0)->ifname);
7596 off = offsetof(struct pfloghdr, ifname);
7597 if (strlen(ifname) >= len) {
7598 bpf_error("ifname interface names can only be %d characters",
7599 len-1);
7600 /* NOTREACHED */
7601 }
7602 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7603 return (b0);
7604}
7605
7606/* PF firewall log ruleset name */
7607struct block *
7608gen_pf_ruleset(char *ruleset)
7609{
7610 struct block *b0;
7611
7612 if (linktype != DLT_PFLOG) {
7613 bpf_error("ruleset supported only on PF linktype");
7614 /* NOTREACHED */
7615 }
7616
7617 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7618 bpf_error("ruleset names can only be %ld characters",
7619 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7620 /* NOTREACHED */
7621 }
7622
7623 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7624 strlen(ruleset), (const u_char *)ruleset);
7625 return (b0);
7626}
7627
7628/* PF firewall log rule number */
7629struct block *
7630gen_pf_rnr(int rnr)
7631{
7632 struct block *b0;
7633
7634 if (linktype != DLT_PFLOG) {
7635 bpf_error("rnr supported only on PF linktype");
7636 /* NOTREACHED */
7637 }
7638
7639 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7640 (bpf_int32)rnr);
7641 return (b0);
7642}
7643
7644/* PF firewall log sub-rule number */
7645struct block *
7646gen_pf_srnr(int srnr)
7647{
7648 struct block *b0;
7649
7650 if (linktype != DLT_PFLOG) {
7651 bpf_error("srnr supported only on PF linktype");
7652 /* NOTREACHED */
7653 }
7654
7655 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7656 (bpf_int32)srnr);
7657 return (b0);
7658}
7659
7660/* PF firewall log reason code */
7661struct block *
7662gen_pf_reason(int reason)
7663{
7664 struct block *b0;
7665
7666 if (linktype != DLT_PFLOG) {
7667 bpf_error("reason supported only on PF linktype");
7668 /* NOTREACHED */
7669 }
7670
7671 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7672 (bpf_int32)reason);
7673 return (b0);
7674}
7675
7676/* PF firewall log action */
7677struct block *
7678gen_pf_action(int action)
7679{
7680 struct block *b0;
7681
7682 if (linktype != DLT_PFLOG) {
7683 bpf_error("action supported only on PF linktype");
7684 /* NOTREACHED */
7685 }
7686
7687 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7688 (bpf_int32)action);
7689 return (b0);
7690}
7691#else /* !HAVE_NET_PFVAR_H */
7692struct block *
7693gen_pf_ifname(const char *ifname)
7694{
7695 bpf_error("libpcap was compiled without pf support");
7696 /* NOTREACHED */
7697 return (NULL);
7698}
7699
7700struct block *
7701gen_pf_ruleset(char *ruleset)
7702{
7703 bpf_error("libpcap was compiled on a machine without pf support");
7704 /* NOTREACHED */
7705 return (NULL);
7706}
7707
7708struct block *
7709gen_pf_rnr(int rnr)
7710{
7711 bpf_error("libpcap was compiled on a machine without pf support");
7712 /* NOTREACHED */
7713 return (NULL);
7714}
7715
7716struct block *
7717gen_pf_srnr(int srnr)
7718{
7719 bpf_error("libpcap was compiled on a machine without pf support");
7720 /* NOTREACHED */
7721 return (NULL);
7722}
7723
7724struct block *
7725gen_pf_reason(int reason)
7726{
7727 bpf_error("libpcap was compiled on a machine without pf support");
7728 /* NOTREACHED */
7729 return (NULL);
7730}
7731
7732struct block *
7733gen_pf_action(int action)
7734{
7735 bpf_error("libpcap was compiled on a machine without pf support");
7736 /* NOTREACHED */
7737 return (NULL);
7738}
7739#endif /* HAVE_NET_PFVAR_H */
7740
7741/* IEEE 802.11 wireless header */
7742struct block *
7743gen_p80211_type(int type, int mask)
7744{
7745 struct block *b0;
7746
7747 switch (linktype) {
7748
7749 case DLT_IEEE802_11:
7750 case DLT_PRISM_HEADER:
7751 case DLT_IEEE802_11_RADIO_AVS:
7752 case DLT_IEEE802_11_RADIO:
7753 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7754 (bpf_int32)mask);
7755 break;
7756
7757 default:
7758 bpf_error("802.11 link-layer types supported only on 802.11");
7759 /* NOTREACHED */
7760 }
7761
7762 return (b0);
7763}
7764
7765struct block *
7766gen_p80211_fcdir(int fcdir)
7767{
7768 struct block *b0;
7769
7770 switch (linktype) {
7771
7772 case DLT_IEEE802_11:
7773 case DLT_PRISM_HEADER:
7774 case DLT_IEEE802_11_RADIO_AVS:
7775 case DLT_IEEE802_11_RADIO:
7776 break;
7777
7778 default:
7779 bpf_error("frame direction supported only with 802.11 headers");
7780 /* NOTREACHED */
7781 }
7782
7783 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7784 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7785
7786 return (b0);
7787}
7788
7789struct block *
7790gen_acode(eaddr, q)
7791 register const u_char *eaddr;
7792 struct qual q;
7793{
7794 switch (linktype) {
7795
7796 case DLT_ARCNET:
7797 case DLT_ARCNET_LINUX:
7798 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7799 q.proto == Q_LINK)
7800 return (gen_ahostop(eaddr, (int)q.dir));
7801 else {
7802 bpf_error("ARCnet address used in non-arc expression");
7803 /* NOTREACHED */
7804 }
7805 break;
7806
7807 default:
7808 bpf_error("aid supported only on ARCnet");
7809 /* NOTREACHED */
7810 }
7811 bpf_error("ARCnet address used in non-arc expression");
7812 /* NOTREACHED */
7813 return NULL;
7814}
7815
7816static struct block *
7817gen_ahostop(eaddr, dir)
7818 register const u_char *eaddr;
7819 register int dir;
7820{
7821 register struct block *b0, *b1;
7822
7823 switch (dir) {
7824 /* src comes first, different from Ethernet */
7825 case Q_SRC:
7826 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7827
7828 case Q_DST:
7829 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7830
7831 case Q_AND:
7832 b0 = gen_ahostop(eaddr, Q_SRC);
7833 b1 = gen_ahostop(eaddr, Q_DST);
7834 gen_and(b0, b1);
7835 return b1;
7836
7837 case Q_DEFAULT:
7838 case Q_OR:
7839 b0 = gen_ahostop(eaddr, Q_SRC);
7840 b1 = gen_ahostop(eaddr, Q_DST);
7841 gen_or(b0, b1);
7842 return b1;
7843
7844 case Q_ADDR1:
7845 bpf_error("'addr1' is only supported on 802.11");
7846 break;
7847
7848 case Q_ADDR2:
7849 bpf_error("'addr2' is only supported on 802.11");
7850 break;
7851
7852 case Q_ADDR3:
7853 bpf_error("'addr3' is only supported on 802.11");
7854 break;
7855
7856 case Q_ADDR4:
7857 bpf_error("'addr4' is only supported on 802.11");
7858 break;
7859
7860 case Q_RA:
7861 bpf_error("'ra' is only supported on 802.11");
7862 break;
7863
7864 case Q_TA:
7865 bpf_error("'ta' is only supported on 802.11");
7866 break;
7867 }
7868 abort();
7869 /* NOTREACHED */
7870}
7871
7872/*
7873 * support IEEE 802.1Q VLAN trunk over ethernet
7874 */
7875struct block *
7876gen_vlan(vlan_num)
7877 int vlan_num;
7878{
7879 struct block *b0, *b1;
7880
7881 /* can't check for VLAN-encapsulated packets inside MPLS */
7882 if (label_stack_depth > 0)
7883 bpf_error("no VLAN match after MPLS");
7884
7885 /*
7886 * Check for a VLAN packet, and then change the offsets to point
7887 * to the type and data fields within the VLAN packet. Just
7888 * increment the offsets, so that we can support a hierarchy, e.g.
7889 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7890 * VLAN 100.
7891 *
7892 * XXX - this is a bit of a kludge. If we were to split the
7893 * compiler into a parser that parses an expression and
7894 * generates an expression tree, and a code generator that
7895 * takes an expression tree (which could come from our
7896 * parser or from some other parser) and generates BPF code,
7897 * we could perhaps make the offsets parameters of routines
7898 * and, in the handler for an "AND" node, pass to subnodes
7899 * other than the VLAN node the adjusted offsets.
7900 *
7901 * This would mean that "vlan" would, instead of changing the
7902 * behavior of *all* tests after it, change only the behavior
7903 * of tests ANDed with it. That would change the documented
7904 * semantics of "vlan", which might break some expressions.
7905 * However, it would mean that "(vlan and ip) or ip" would check
7906 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7907 * checking only for VLAN-encapsulated IP, so that could still
7908 * be considered worth doing; it wouldn't break expressions
7909 * that are of the form "vlan and ..." or "vlan N and ...",
7910 * which I suspect are the most common expressions involving
7911 * "vlan". "vlan or ..." doesn't necessarily do what the user
7912 * would really want, now, as all the "or ..." tests would
7913 * be done assuming a VLAN, even though the "or" could be viewed
7914 * as meaning "or, if this isn't a VLAN packet...".
7915 */
7916 orig_nl = off_nl;
7917
7918 switch (linktype) {
7919
7920 case DLT_EN10MB:
7921 case DLT_NETANALYZER:
7922 case DLT_NETANALYZER_TRANSPARENT:
7923 /* check for VLAN, including QinQ */
7924 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7925 (bpf_int32)ETHERTYPE_8021Q);
7926 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7927 (bpf_int32)ETHERTYPE_8021QINQ);
7928 gen_or(b0,b1);
7929 b0 = b1;
7930
7931 /* If a specific VLAN is requested, check VLAN id */
7932 if (vlan_num >= 0) {
7933 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7934 (bpf_int32)vlan_num, 0x0fff);
7935 gen_and(b0, b1);
7936 b0 = b1;
7937 }
7938
7939 off_macpl += 4;
7940 off_linktype += 4;
7941#if 0
7942 off_nl_nosnap += 4;
7943 off_nl += 4;
7944#endif
7945 break;
7946
7947 default:
7948 bpf_error("no VLAN support for data link type %d",
7949 linktype);
7950 /*NOTREACHED*/
7951 }
7952
7953 return (b0);
7954}
7955
7956/*
7957 * support for MPLS
7958 */
7959struct block *
7960gen_mpls(label_num)
7961 int label_num;
7962{
7963 struct block *b0,*b1;
7964
7965 /*
7966 * Change the offsets to point to the type and data fields within
7967 * the MPLS packet. Just increment the offsets, so that we
7968 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
7969 * capture packets with an outer label of 100000 and an inner
7970 * label of 1024.
7971 *
7972 * XXX - this is a bit of a kludge. See comments in gen_vlan().
7973 */
7974 orig_nl = off_nl;
7975
7976 if (label_stack_depth > 0) {
7977 /* just match the bottom-of-stack bit clear */
7978 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
7979 } else {
7980 /*
7981 * Indicate that we're checking MPLS-encapsulated headers,
7982 * to make sure higher level code generators don't try to
7983 * match against IP-related protocols such as Q_ARP, Q_RARP
7984 * etc.
7985 */
7986 switch (linktype) {
7987
7988 case DLT_C_HDLC: /* fall through */
7989 case DLT_EN10MB:
7990 case DLT_NETANALYZER:
7991 case DLT_NETANALYZER_TRANSPARENT:
7992 b0 = gen_linktype(ETHERTYPE_MPLS);
7993 break;
7994
7995 case DLT_PPP:
7996 b0 = gen_linktype(PPP_MPLS_UCAST);
7997 break;
7998
7999 /* FIXME add other DLT_s ...
8000 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8001 * leave it for now */
8002
8003 default:
8004 bpf_error("no MPLS support for data link type %d",
8005 linktype);
8006 b0 = NULL;
8007 /*NOTREACHED*/
8008 break;
8009 }
8010 }
8011
8012 /* If a specific MPLS label is requested, check it */
8013 if (label_num >= 0) {
8014 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
8015 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
8016 0xfffff000); /* only compare the first 20 bits */
8017 gen_and(b0, b1);
8018 b0 = b1;
8019 }
8020
8021 off_nl_nosnap += 4;
8022 off_nl += 4;
8023 label_stack_depth++;
8024 return (b0);
8025}
8026
8027/*
8028 * Support PPPOE discovery and session.
8029 */
8030struct block *
8031gen_pppoed()
8032{
8033 /* check for PPPoE discovery */
8034 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
8035}
8036
8037struct block *
8038gen_pppoes()
8039{
8040 struct block *b0;
8041
8042 /*
8043 * Test against the PPPoE session link-layer type.
8044 */
8045 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
8046
8047 /*
8048 * Change the offsets to point to the type and data fields within
8049 * the PPP packet, and note that this is PPPoE rather than
8050 * raw PPP.
8051 *
8052 * XXX - this is a bit of a kludge. If we were to split the
8053 * compiler into a parser that parses an expression and
8054 * generates an expression tree, and a code generator that
8055 * takes an expression tree (which could come from our
8056 * parser or from some other parser) and generates BPF code,
8057 * we could perhaps make the offsets parameters of routines
8058 * and, in the handler for an "AND" node, pass to subnodes
8059 * other than the PPPoE node the adjusted offsets.
8060 *
8061 * This would mean that "pppoes" would, instead of changing the
8062 * behavior of *all* tests after it, change only the behavior
8063 * of tests ANDed with it. That would change the documented
8064 * semantics of "pppoes", which might break some expressions.
8065 * However, it would mean that "(pppoes and ip) or ip" would check
8066 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8067 * checking only for VLAN-encapsulated IP, so that could still
8068 * be considered worth doing; it wouldn't break expressions
8069 * that are of the form "pppoes and ..." which I suspect are the
8070 * most common expressions involving "pppoes". "pppoes or ..."
8071 * doesn't necessarily do what the user would really want, now,
8072 * as all the "or ..." tests would be done assuming PPPoE, even
8073 * though the "or" could be viewed as meaning "or, if this isn't
8074 * a PPPoE packet...".
8075 */
8076 orig_linktype = off_linktype; /* save original values */
8077 orig_nl = off_nl;
8078 is_pppoes = 1;
8079
8080 /*
8081 * The "network-layer" protocol is PPPoE, which has a 6-byte
8082 * PPPoE header, followed by a PPP packet.
8083 *
8084 * There is no HDLC encapsulation for the PPP packet (it's
8085 * encapsulated in PPPoES instead), so the link-layer type
8086 * starts at the first byte of the PPP packet. For PPPoE,
8087 * that offset is relative to the beginning of the total
8088 * link-layer payload, including any 802.2 LLC header, so
8089 * it's 6 bytes past off_nl.
8090 */
8091 off_linktype = off_nl + 6;
8092
8093 /*
8094 * The network-layer offsets are relative to the beginning
8095 * of the MAC-layer payload; that's past the 6-byte
8096 * PPPoE header and the 2-byte PPP header.
8097 */
8098 off_nl = 6+2;
8099 off_nl_nosnap = 6+2;
8100
8101 return b0;
8102}
8103
8104struct block *
8105gen_atmfield_code(atmfield, jvalue, jtype, reverse)
8106 int atmfield;
8107 bpf_int32 jvalue;
8108 bpf_u_int32 jtype;
8109 int reverse;
8110{
8111 struct block *b0;
8112
8113 switch (atmfield) {
8114
8115 case A_VPI:
8116 if (!is_atm)
8117 bpf_error("'vpi' supported only on raw ATM");
8118 if (off_vpi == (u_int)-1)
8119 abort();
8120 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
8121 reverse, jvalue);
8122 break;
8123
8124 case A_VCI:
8125 if (!is_atm)
8126 bpf_error("'vci' supported only on raw ATM");
8127 if (off_vci == (u_int)-1)
8128 abort();
8129 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
8130 reverse, jvalue);
8131 break;
8132
8133 case A_PROTOTYPE:
8134 if (off_proto == (u_int)-1)
8135 abort(); /* XXX - this isn't on FreeBSD */
8136 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
8137 reverse, jvalue);
8138 break;
8139
8140 case A_MSGTYPE:
8141 if (off_payload == (u_int)-1)
8142 abort();
8143 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
8144 0xffffffff, jtype, reverse, jvalue);
8145 break;
8146
8147 case A_CALLREFTYPE:
8148 if (!is_atm)
8149 bpf_error("'callref' supported only on raw ATM");
8150 if (off_proto == (u_int)-1)
8151 abort();
8152 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
8153 jtype, reverse, jvalue);
8154 break;
8155
8156 default:
8157 abort();
8158 }
8159 return b0;
8160}
8161
8162struct block *
8163gen_atmtype_abbrev(type)
8164 int type;
8165{
8166 struct block *b0, *b1;
8167
8168 switch (type) {
8169
8170 case A_METAC:
8171 /* Get all packets in Meta signalling Circuit */
8172 if (!is_atm)
8173 bpf_error("'metac' supported only on raw ATM");
8174 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8175 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
8176 gen_and(b0, b1);
8177 break;
8178
8179 case A_BCC:
8180 /* Get all packets in Broadcast Circuit*/
8181 if (!is_atm)
8182 bpf_error("'bcc' supported only on raw ATM");
8183 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8184 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
8185 gen_and(b0, b1);
8186 break;
8187
8188 case A_OAMF4SC:
8189 /* Get all cells in Segment OAM F4 circuit*/
8190 if (!is_atm)
8191 bpf_error("'oam4sc' supported only on raw ATM");
8192 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8193 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8194 gen_and(b0, b1);
8195 break;
8196
8197 case A_OAMF4EC:
8198 /* Get all cells in End-to-End OAM F4 Circuit*/
8199 if (!is_atm)
8200 bpf_error("'oam4ec' supported only on raw ATM");
8201 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8202 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8203 gen_and(b0, b1);
8204 break;
8205
8206 case A_SC:
8207 /* Get all packets in connection Signalling Circuit */
8208 if (!is_atm)
8209 bpf_error("'sc' supported only on raw ATM");
8210 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8211 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
8212 gen_and(b0, b1);
8213 break;
8214
8215 case A_ILMIC:
8216 /* Get all packets in ILMI Circuit */
8217 if (!is_atm)
8218 bpf_error("'ilmic' supported only on raw ATM");
8219 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8220 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
8221 gen_and(b0, b1);
8222 break;
8223
8224 case A_LANE:
8225 /* Get all LANE packets */
8226 if (!is_atm)
8227 bpf_error("'lane' supported only on raw ATM");
8228 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8229
8230 /*
8231 * Arrange that all subsequent tests assume LANE
8232 * rather than LLC-encapsulated packets, and set
8233 * the offsets appropriately for LANE-encapsulated
8234 * Ethernet.
8235 *
8236 * "off_mac" is the offset of the Ethernet header,
8237 * which is 2 bytes past the ATM pseudo-header
8238 * (skipping the pseudo-header and 2-byte LE Client
8239 * field). The other offsets are Ethernet offsets
8240 * relative to "off_mac".
8241 */
8242 is_lane = 1;
8243 off_mac = off_payload + 2; /* MAC header */
8244 off_linktype = off_mac + 12;
8245 off_macpl = off_mac + 14; /* Ethernet */
8246 off_nl = 0; /* Ethernet II */
8247 off_nl_nosnap = 3; /* 802.3+802.2 */
8248 break;
8249
8250 case A_LLC:
8251 /* Get all LLC-encapsulated packets */
8252 if (!is_atm)
8253 bpf_error("'llc' supported only on raw ATM");
8254 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8255 is_lane = 0;
8256 break;
8257
8258 default:
8259 abort();
8260 }
8261 return b1;
8262}
8263
8264/*
8265 * Filtering for MTP2 messages based on li value
8266 * FISU, length is null
8267 * LSSU, length is 1 or 2
8268 * MSU, length is 3 or more
8269 */
8270struct block *
8271gen_mtp2type_abbrev(type)
8272 int type;
8273{
8274 struct block *b0, *b1;
8275
8276 switch (type) {
8277
8278 case M_FISU:
8279 if ( (linktype != DLT_MTP2) &&
8280 (linktype != DLT_ERF) &&
8281 (linktype != DLT_MTP2_WITH_PHDR) )
8282 bpf_error("'fisu' supported only on MTP2");
8283 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8284 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8285 break;
8286
8287 case M_LSSU:
8288 if ( (linktype != DLT_MTP2) &&
8289 (linktype != DLT_ERF) &&
8290 (linktype != DLT_MTP2_WITH_PHDR) )
8291 bpf_error("'lssu' supported only on MTP2");
8292 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8293 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8294 gen_and(b1, b0);
8295 break;
8296
8297 case M_MSU:
8298 if ( (linktype != DLT_MTP2) &&
8299 (linktype != DLT_ERF) &&
8300 (linktype != DLT_MTP2_WITH_PHDR) )
8301 bpf_error("'msu' supported only on MTP2");
8302 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8303 break;
8304
8305 default:
8306 abort();
8307 }
8308 return b0;
8309}
8310
8311struct block *
8312gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8313 int mtp3field;
8314 bpf_u_int32 jvalue;
8315 bpf_u_int32 jtype;
8316 int reverse;
8317{
8318 struct block *b0;
8319 bpf_u_int32 val1 , val2 , val3;
8320
8321 switch (mtp3field) {
8322
8323 case M_SIO:
8324 if (off_sio == (u_int)-1)
8325 bpf_error("'sio' supported only on SS7");
8326 /* sio coded on 1 byte so max value 255 */
8327 if(jvalue > 255)
8328 bpf_error("sio value %u too big; max value = 255",
8329 jvalue);
8330 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8331 (u_int)jtype, reverse, (u_int)jvalue);
8332 break;
8333
8334 case M_OPC:
8335 if (off_opc == (u_int)-1)
8336 bpf_error("'opc' supported only on SS7");
8337 /* opc coded on 14 bits so max value 16383 */
8338 if (jvalue > 16383)
8339 bpf_error("opc value %u too big; max value = 16383",
8340 jvalue);
8341 /* the following instructions are made to convert jvalue
8342 * to the form used to write opc in an ss7 message*/
8343 val1 = jvalue & 0x00003c00;
8344 val1 = val1 >>10;
8345 val2 = jvalue & 0x000003fc;
8346 val2 = val2 <<6;
8347 val3 = jvalue & 0x00000003;
8348 val3 = val3 <<22;
8349 jvalue = val1 + val2 + val3;
8350 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8351 (u_int)jtype, reverse, (u_int)jvalue);
8352 break;
8353
8354 case M_DPC:
8355 if (off_dpc == (u_int)-1)
8356 bpf_error("'dpc' supported only on SS7");
8357 /* dpc coded on 14 bits so max value 16383 */
8358 if (jvalue > 16383)
8359 bpf_error("dpc value %u too big; max value = 16383",
8360 jvalue);
8361 /* the following instructions are made to convert jvalue
8362 * to the forme used to write dpc in an ss7 message*/
8363 val1 = jvalue & 0x000000ff;
8364 val1 = val1 << 24;
8365 val2 = jvalue & 0x00003f00;
8366 val2 = val2 << 8;
8367 jvalue = val1 + val2;
8368 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8369 (u_int)jtype, reverse, (u_int)jvalue);
8370 break;
8371
8372 case M_SLS:
8373 if (off_sls == (u_int)-1)
8374 bpf_error("'sls' supported only on SS7");
8375 /* sls coded on 4 bits so max value 15 */
8376 if (jvalue > 15)
8377 bpf_error("sls value %u too big; max value = 15",
8378 jvalue);
8379 /* the following instruction is made to convert jvalue
8380 * to the forme used to write sls in an ss7 message*/
8381 jvalue = jvalue << 4;
8382 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8383 (u_int)jtype,reverse, (u_int)jvalue);
8384 break;
8385
8386 default:
8387 abort();
8388 }
8389 return b0;
8390}
8391
8392static struct block *
8393gen_msg_abbrev(type)
8394 int type;
8395{
8396 struct block *b1;
8397
8398 /*
8399 * Q.2931 signalling protocol messages for handling virtual circuits
8400 * establishment and teardown
8401 */
8402 switch (type) {
8403
8404 case A_SETUP:
8405 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8406 break;
8407
8408 case A_CALLPROCEED:
8409 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8410 break;
8411
8412 case A_CONNECT:
8413 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8414 break;
8415
8416 case A_CONNECTACK:
8417 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8418 break;
8419
8420 case A_RELEASE:
8421 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8422 break;
8423
8424 case A_RELEASE_DONE:
8425 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8426 break;
8427
8428 default:
8429 abort();
8430 }
8431 return b1;
8432}
8433
8434struct block *
8435gen_atmmulti_abbrev(type)
8436 int type;
8437{
8438 struct block *b0, *b1;
8439
8440 switch (type) {
8441
8442 case A_OAM:
8443 if (!is_atm)
8444 bpf_error("'oam' supported only on raw ATM");
8445 b1 = gen_atmmulti_abbrev(A_OAMF4);
8446 break;
8447
8448 case A_OAMF4:
8449 if (!is_atm)
8450 bpf_error("'oamf4' supported only on raw ATM");
8451 /* OAM F4 type */
8452 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8453 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8454 gen_or(b0, b1);
8455 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8456 gen_and(b0, b1);
8457 break;
8458
8459 case A_CONNECTMSG:
8460 /*
8461 * Get Q.2931 signalling messages for switched
8462 * virtual connection
8463 */
8464 if (!is_atm)
8465 bpf_error("'connectmsg' supported only on raw ATM");
8466 b0 = gen_msg_abbrev(A_SETUP);
8467 b1 = gen_msg_abbrev(A_CALLPROCEED);
8468 gen_or(b0, b1);
8469 b0 = gen_msg_abbrev(A_CONNECT);
8470 gen_or(b0, b1);
8471 b0 = gen_msg_abbrev(A_CONNECTACK);
8472 gen_or(b0, b1);
8473 b0 = gen_msg_abbrev(A_RELEASE);
8474 gen_or(b0, b1);
8475 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8476 gen_or(b0, b1);
8477 b0 = gen_atmtype_abbrev(A_SC);
8478 gen_and(b0, b1);
8479 break;
8480
8481 case A_METACONNECT:
8482 if (!is_atm)
8483 bpf_error("'metaconnect' supported only on raw ATM");
8484 b0 = gen_msg_abbrev(A_SETUP);
8485 b1 = gen_msg_abbrev(A_CALLPROCEED);
8486 gen_or(b0, b1);
8487 b0 = gen_msg_abbrev(A_CONNECT);
8488 gen_or(b0, b1);
8489 b0 = gen_msg_abbrev(A_RELEASE);
8490 gen_or(b0, b1);
8491 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8492 gen_or(b0, b1);
8493 b0 = gen_atmtype_abbrev(A_METAC);
8494 gen_and(b0, b1);
8495 break;
8496
8497 default:
8498 abort();
8499 }
8500 return b1;
8501}