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