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