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