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