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