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