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