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