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