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