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