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