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