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