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