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