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