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