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