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