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