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