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