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