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