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