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