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