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