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