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