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