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