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