]> The Tcpdump Group git mirrors - libpcap/blob - gencode.c
2750df65f4f3ed7481658777e5e61b1ea233443a
[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[] =
24 "@(#) $Header: /tcpdump/master/libpcap/gencode.c,v 1.120 2000-10-06 04:52:53 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/time.h>
34 #ifdef __NetBSD__
35 #include <sys/param.h>
36 #endif
37
38 struct mbuf;
39 struct rtentry;
40 #include <net/if.h>
41
42 #include <netinet/in.h>
43
44 #include <stdlib.h>
45 #include <memory.h>
46 #include <setjmp.h>
47 #include <stdarg.h>
48
49 #include "pcap-int.h"
50
51 #include "ethertype.h"
52 #include "gencode.h"
53 #include "ppp.h"
54 #include <pcap-namedb.h>
55 #ifdef INET6
56 #include <netdb.h>
57 #include <sys/socket.h>
58 #endif /*INET6*/
59
60 #ifdef HAVE_OS_PROTO_H
61 #include "os-proto.h"
62 #endif
63
64 #define JMP(c) ((c)|BPF_JMP|BPF_K)
65
66 /* Locals */
67 static jmp_buf top_ctx;
68 static pcap_t *bpf_pcap;
69
70 /* XXX */
71 #ifdef PCAP_FDDIPAD
72 int pcap_fddipad = PCAP_FDDIPAD;
73 #else
74 int pcap_fddipad;
75 #endif
76
77 /* VARARGS */
78 void
79 bpf_error(const char *fmt, ...)
80
81 {
82 va_list ap;
83
84 va_start(ap, fmt);
85 if (bpf_pcap != NULL)
86 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
87 fmt, ap);
88 va_end(ap);
89 longjmp(top_ctx, 1);
90 /* NOTREACHED */
91 }
92
93 static void init_linktype(int);
94
95 static int alloc_reg(void);
96 static void free_reg(int);
97
98 static struct block *root;
99
100 /*
101 * We divy out chunks of memory rather than call malloc each time so
102 * we don't have to worry about leaking memory. It's probably
103 * not a big deal if all this memory was wasted but it this ever
104 * goes into a library that would probably not be a good idea.
105 */
106 #define NCHUNKS 16
107 #define CHUNK0SIZE 1024
108 struct chunk {
109 u_int n_left;
110 void *m;
111 };
112
113 static struct chunk chunks[NCHUNKS];
114 static int cur_chunk;
115
116 static void *newchunk(u_int);
117 static void freechunks(void);
118 static inline struct block *new_block(int);
119 static inline struct slist *new_stmt(int);
120 static struct block *gen_retblk(int);
121 static inline void syntax(void);
122
123 static void backpatch(struct block *, struct block *);
124 static void merge(struct block *, struct block *);
125 static struct block *gen_cmp(u_int, u_int, bpf_int32);
126 static struct block *gen_mcmp(u_int, u_int, bpf_int32, bpf_u_int32);
127 static struct block *gen_bcmp(u_int, u_int, const u_char *);
128 static struct block *gen_uncond(int);
129 static inline struct block *gen_true(void);
130 static inline struct block *gen_false(void);
131 static struct block *gen_linktype(int);
132 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
133 #ifdef INET6
134 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
135 #endif
136 static struct block *gen_ehostop(const u_char *, int);
137 static struct block *gen_fhostop(const u_char *, int);
138 static struct block *gen_thostop(const u_char *, int);
139 static struct block *gen_dnhostop(bpf_u_int32, int, u_int);
140 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
141 #ifdef INET6
142 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
143 #endif
144 #ifndef INET6
145 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
146 #endif
147 static struct block *gen_ipfrag(void);
148 static struct block *gen_portatom(int, bpf_int32);
149 #ifdef INET6
150 static struct block *gen_portatom6(int, bpf_int32);
151 #endif
152 struct block *gen_portop(int, int, int);
153 static struct block *gen_port(int, int, int);
154 #ifdef INET6
155 struct block *gen_portop6(int, int, int);
156 static struct block *gen_port6(int, int, int);
157 #endif
158 static int lookup_proto(const char *, int);
159 static struct block *gen_protochain(int, int, int);
160 static struct block *gen_proto(int, int, int);
161 static struct slist *xfer_to_x(struct arth *);
162 static struct slist *xfer_to_a(struct arth *);
163 static struct block *gen_len(int, int);
164
165 static void *
166 newchunk(n)
167 u_int n;
168 {
169 struct chunk *cp;
170 int k, size;
171
172 #ifndef __NetBSD__
173 /* XXX Round up to nearest long. */
174 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
175 #else
176 /* XXX Round up to structure boundary. */
177 n = ALIGN(n);
178 #endif
179
180 cp = &chunks[cur_chunk];
181 if (n > cp->n_left) {
182 ++cp, k = ++cur_chunk;
183 if (k >= NCHUNKS)
184 bpf_error("out of memory");
185 size = CHUNK0SIZE << k;
186 cp->m = (void *)malloc(size);
187 memset((char *)cp->m, 0, size);
188 cp->n_left = size;
189 if (n > size)
190 bpf_error("out of memory");
191 }
192 cp->n_left -= n;
193 return (void *)((char *)cp->m + cp->n_left);
194 }
195
196 static void
197 freechunks()
198 {
199 int i;
200
201 cur_chunk = 0;
202 for (i = 0; i < NCHUNKS; ++i)
203 if (chunks[i].m != NULL) {
204 free(chunks[i].m);
205 chunks[i].m = NULL;
206 }
207 }
208
209 /*
210 * A strdup whose allocations are freed after code generation is over.
211 */
212 char *
213 sdup(s)
214 register const char *s;
215 {
216 int n = strlen(s) + 1;
217 char *cp = newchunk(n);
218
219 strlcpy(cp, s, n);
220 return (cp);
221 }
222
223 static inline struct block *
224 new_block(code)
225 int code;
226 {
227 struct block *p;
228
229 p = (struct block *)newchunk(sizeof(*p));
230 p->s.code = code;
231 p->head = p;
232
233 return p;
234 }
235
236 static inline struct slist *
237 new_stmt(code)
238 int code;
239 {
240 struct slist *p;
241
242 p = (struct slist *)newchunk(sizeof(*p));
243 p->s.code = code;
244
245 return p;
246 }
247
248 static struct block *
249 gen_retblk(v)
250 int v;
251 {
252 struct block *b = new_block(BPF_RET|BPF_K);
253
254 b->s.k = v;
255 return b;
256 }
257
258 static inline void
259 syntax()
260 {
261 bpf_error("syntax error in filter expression");
262 }
263
264 static bpf_u_int32 netmask;
265 static int snaplen;
266 int no_optimize;
267
268 int
269 pcap_compile(pcap_t *p, struct bpf_program *program,
270 char *buf, int optimize, bpf_u_int32 mask)
271 {
272 extern int n_errors;
273 int len;
274
275 no_optimize = 0;
276 n_errors = 0;
277 root = NULL;
278 bpf_pcap = p;
279 if (setjmp(top_ctx)) {
280 lex_cleanup();
281 freechunks();
282 return (-1);
283 }
284
285 netmask = mask;
286
287 /* On Linux we do not use the BPF filter to truncate the packet
288 * since the kernel provides other ways for that. In fact if we
289 * are using the packet filter for that duty we will be unable
290 * to acquire the original packet size. -- Torsten */
291 #ifndef linux
292 snaplen = pcap_snapshot(p);
293 #else
294 snaplen = 0xffff;
295 #endif
296 if (snaplen == 0) {
297 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
298 "snaplen of 0 rejects all packets");
299 return -1;
300 }
301
302 lex_init(buf ? buf : "");
303 init_linktype(pcap_datalink(p));
304 (void)pcap_parse();
305
306 if (n_errors)
307 syntax();
308
309 if (root == NULL)
310 root = gen_retblk(snaplen);
311
312 if (optimize && !no_optimize) {
313 bpf_optimize(&root);
314 if (root == NULL ||
315 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
316 bpf_error("expression rejects all packets");
317 }
318 program->bf_insns = icode_to_fcode(root, &len);
319 program->bf_len = len;
320
321 freechunks();
322 return (0);
323 }
324
325 /*
326 * entry point for using the compiler with no pcap open
327 * pass in all the stuff that is needed explicitly instead.
328 */
329 int
330 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
331 struct bpf_program *program,
332 char *buf, int optimize, bpf_u_int32 mask)
333 {
334 extern int n_errors;
335 int len;
336
337 n_errors = 0;
338 root = NULL;
339 bpf_pcap = NULL;
340 if (setjmp(top_ctx)) {
341 freechunks();
342 return (-1);
343 }
344
345 netmask = mask;
346
347 /* XXX needed? I don't grok the use of globals here. */
348 snaplen = snaplen_arg;
349
350 lex_init(buf ? buf : "");
351 init_linktype(linktype_arg);
352 (void)pcap_parse();
353
354 if (n_errors)
355 syntax();
356
357 if (root == NULL)
358 root = gen_retblk(snaplen_arg);
359
360 if (optimize) {
361 bpf_optimize(&root);
362 if (root == NULL ||
363 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
364 bpf_error("expression rejects all packets");
365 }
366 program->bf_insns = icode_to_fcode(root, &len);
367 program->bf_len = len;
368
369 freechunks();
370 return (0);
371 }
372
373 /*
374 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
375 * which of the jt and jf fields has been resolved and which is a pointer
376 * back to another unresolved block (or nil). At least one of the fields
377 * in each block is already resolved.
378 */
379 static void
380 backpatch(list, target)
381 struct block *list, *target;
382 {
383 struct block *next;
384
385 while (list) {
386 if (!list->sense) {
387 next = JT(list);
388 JT(list) = target;
389 } else {
390 next = JF(list);
391 JF(list) = target;
392 }
393 list = next;
394 }
395 }
396
397 /*
398 * Merge the lists in b0 and b1, using the 'sense' field to indicate
399 * which of jt and jf is the link.
400 */
401 static void
402 merge(b0, b1)
403 struct block *b0, *b1;
404 {
405 register struct block **p = &b0;
406
407 /* Find end of list. */
408 while (*p)
409 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
410
411 /* Concatenate the lists. */
412 *p = b1;
413 }
414
415 void
416 finish_parse(p)
417 struct block *p;
418 {
419 backpatch(p, gen_retblk(snaplen));
420 p->sense = !p->sense;
421 backpatch(p, gen_retblk(0));
422 root = p->head;
423 }
424
425 void
426 gen_and(b0, b1)
427 struct block *b0, *b1;
428 {
429 backpatch(b0, b1->head);
430 b0->sense = !b0->sense;
431 b1->sense = !b1->sense;
432 merge(b1, b0);
433 b1->sense = !b1->sense;
434 b1->head = b0->head;
435 }
436
437 void
438 gen_or(b0, b1)
439 struct block *b0, *b1;
440 {
441 b0->sense = !b0->sense;
442 backpatch(b0, b1->head);
443 b0->sense = !b0->sense;
444 merge(b1, b0);
445 b1->head = b0->head;
446 }
447
448 void
449 gen_not(b)
450 struct block *b;
451 {
452 b->sense = !b->sense;
453 }
454
455 static struct block *
456 gen_cmp(offset, size, v)
457 u_int offset, size;
458 bpf_int32 v;
459 {
460 struct slist *s;
461 struct block *b;
462
463 s = new_stmt(BPF_LD|BPF_ABS|size);
464 s->s.k = offset;
465
466 b = new_block(JMP(BPF_JEQ));
467 b->stmts = s;
468 b->s.k = v;
469
470 return b;
471 }
472
473 static struct block *
474 gen_mcmp(offset, size, v, mask)
475 u_int offset, size;
476 bpf_int32 v;
477 bpf_u_int32 mask;
478 {
479 struct block *b = gen_cmp(offset, size, v);
480 struct slist *s;
481
482 if (mask != 0xffffffff) {
483 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
484 s->s.k = mask;
485 b->stmts->next = s;
486 }
487 return b;
488 }
489
490 static struct block *
491 gen_bcmp(offset, size, v)
492 register u_int offset, size;
493 register const u_char *v;
494 {
495 register struct block *b, *tmp;
496
497 b = NULL;
498 while (size >= 4) {
499 register const u_char *p = &v[size - 4];
500 bpf_int32 w = ((bpf_int32)p[0] << 24) |
501 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
502
503 tmp = gen_cmp(offset + size - 4, BPF_W, w);
504 if (b != NULL)
505 gen_and(b, tmp);
506 b = tmp;
507 size -= 4;
508 }
509 while (size >= 2) {
510 register const u_char *p = &v[size - 2];
511 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
512
513 tmp = gen_cmp(offset + size - 2, BPF_H, w);
514 if (b != NULL)
515 gen_and(b, tmp);
516 b = tmp;
517 size -= 2;
518 }
519 if (size > 0) {
520 tmp = gen_cmp(offset, BPF_B, (bpf_int32)v[0]);
521 if (b != NULL)
522 gen_and(b, tmp);
523 b = tmp;
524 }
525 return b;
526 }
527
528 /*
529 * Various code constructs need to know the layout of the data link
530 * layer. These variables give the necessary offsets. off_linktype
531 * is set to -1 for no encapsulation, in which case, IP is assumed.
532 */
533 static u_int off_linktype;
534 static u_int off_nl;
535 static int linktype;
536
537 static void
538 init_linktype(type)
539 int type;
540 {
541 /*
542 * Map DLT_ codes that don't have the same value as the
543 * equivalent PCAP_ENCAP_ codes to the corresponding PCAP_ENCAP_
544 * code.
545 *
546 * Even though "pcap_open_live()" in "pcap-bpf.c" does a
547 * similar mapping, we do that mapping here as well, to
548 * handle filters constructed for savefiles.
549 *
550 * XXX - should we do this mapping in "savefile.c"? Doing so
551 * might cause programs that read one or more capture files
552 * and write another capture file with the same type as
553 * the input file(s) to use PCAP_ENCAP_ values that aren't
554 * supported by the libpcap on the system that wrote the original
555 * capture file, so we might not want to do that.
556 */
557 switch (type) {
558
559 #ifdef DLT_ATM_RFC1483
560 case DLT_ATM_RFC1483:
561 linktype = PCAP_ENCAP_ATM_RFC1483;
562 break;
563 #endif
564
565 #ifdef DLT_RAW
566 case DLT_RAW:
567 linktype = PCAP_ENCAP_RAW;
568 break;
569 #endif
570
571 #ifdef DLT_SLIP_BSDOS
572 case DLT_SLIP_BSDOS:
573 linktype = PCAP_ENCAP_SLIP_BSDOS;
574 break;
575 #endif
576
577 #ifdef DLT_PPP_BSDOS
578 case DLT_PPP_BSDOS:
579 linktype = PCAP_ENCAP_PPP_BSDOS;
580 break;
581 #endif
582
583 #ifdef DLT_CIP
584 case DLT_CIP:
585 linktype = PCAP_ENCAP_ATM_CLIP;
586 break;
587 #endif
588
589 #ifdef DLT_ATM_CLIP
590 case DLT_ATM_CLIP:
591 linktype = PCAP_ENCAP_ATM_CLIP;
592 break;
593 #endif
594
595 #ifdef DLT_PPP_SERIAL
596 case DLT_PPP_SERIAL:
597 linktype = PCAP_ENCAP_PPP_HDLC;
598 break;
599 #endif
600
601 default:
602 linktype = type;
603 break;
604 }
605
606 switch (linktype) {
607
608 case PCAP_ENCAP_ETHERNET:
609 off_linktype = 12;
610 off_nl = 14;
611 return;
612
613 case PCAP_ENCAP_SLIP:
614 /*
615 * SLIP doesn't have a link level type. The 16 byte
616 * header is hacked into our SLIP driver.
617 */
618 off_linktype = -1;
619 off_nl = 16;
620 return;
621
622 case PCAP_ENCAP_SLIP_BSDOS:
623 /* XXX this may be the same as the PCAP_ENCAP_PPP_BSDOS case */
624 off_linktype = -1;
625 /* XXX end */
626 off_nl = 24;
627 return;
628
629 case PCAP_ENCAP_NULL:
630 off_linktype = 0;
631 off_nl = 4;
632 return;
633
634 case PCAP_ENCAP_PPP:
635 case PCAP_ENCAP_C_HDLC:
636 case PCAP_ENCAP_PPP_HDLC:
637 off_linktype = 2;
638 off_nl = 4;
639 return;
640
641 case PCAP_ENCAP_PPP_BSDOS:
642 off_linktype = 5;
643 off_nl = 24;
644 return;
645
646 case PCAP_ENCAP_FDDI:
647 /*
648 * FDDI doesn't really have a link-level type field.
649 * We assume that SSAP = SNAP is being used and pick
650 * out the encapsulated Ethernet type.
651 *
652 * XXX - should we generate code to check for SNAP?
653 */
654 off_linktype = 19;
655 #ifdef PCAP_FDDIPAD
656 off_linktype += pcap_fddipad;
657 #endif
658 off_nl = 21;
659 #ifdef PCAP_FDDIPAD
660 off_nl += pcap_fddipad;
661 #endif
662 return;
663
664 case PCAP_ENCAP_TOKEN_RING:
665 /*
666 * Token Ring doesn't really have a link-level type field.
667 * We assume that SSAP = SNAP is being used and pick
668 * out the encapsulated Ethernet type.
669 *
670 * XXX - should we generate code to check for SNAP?
671 *
672 * XXX - the header is actually variable-length.
673 * Some various Linux patched versions gave 38
674 * as "off_linktype" and 40 as "off_nl"; however,
675 * if a token ring packet has *no* routing
676 * information, i.e. is not source-routed, the correct
677 * values are 20 and 22, as they are in the vanilla code.
678 *
679 * A packet is source-routed iff the uppermost bit
680 * of the first byte of the source address, at an
681 * offset of 8, has the uppermost bit set. If the
682 * packet is source-routed, the total number of bytes
683 * of routing information is 2 plus bits 0x1F00 of
684 * the 16-bit value at an offset of 14 (shifted right
685 * 8 - figure out which byte that is).
686 */
687 off_linktype = 20;
688 off_nl = 22;
689 return;
690
691 case PCAP_ENCAP_ATM_RFC1483:
692 /*
693 * assume routed, non-ISO PDUs
694 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
695 */
696 off_linktype = 6;
697 off_nl = 8;
698 return;
699
700 case PCAP_ENCAP_RAW:
701 off_linktype = -1;
702 off_nl = 0;
703 return;
704
705 case PCAP_ENCAP_ATM_CLIP:
706 off_linktype = 6;
707 off_nl = 8;
708 return;
709 }
710 bpf_error("unknown data link type 0x%x", linktype);
711 /* NOTREACHED */
712 }
713
714 static struct block *
715 gen_uncond(rsense)
716 int rsense;
717 {
718 struct block *b;
719 struct slist *s;
720
721 s = new_stmt(BPF_LD|BPF_IMM);
722 s->s.k = !rsense;
723 b = new_block(JMP(BPF_JEQ));
724 b->stmts = s;
725
726 return b;
727 }
728
729 static inline struct block *
730 gen_true()
731 {
732 return gen_uncond(1);
733 }
734
735 static inline struct block *
736 gen_false()
737 {
738 return gen_uncond(0);
739 }
740
741 static struct block *
742 gen_linktype(proto)
743 register int proto;
744 {
745 struct block *b0, *b1;
746
747 /* If we're not using encapsulation, we're done */
748 if (off_linktype == -1)
749 return gen_true();
750
751 switch (linktype) {
752
753 case PCAP_ENCAP_SLIP:
754 return gen_false();
755
756 case PCAP_ENCAP_PPP:
757 case PCAP_ENCAP_PPP_HDLC:
758 if (proto == ETHERTYPE_IP)
759 proto = PPP_IP; /* XXX was 0x21 */
760 #ifdef INET6
761 else if (proto == ETHERTYPE_IPV6)
762 proto = PPP_IPV6;
763 #endif
764 break;
765
766 case PCAP_ENCAP_PPP_BSDOS:
767 switch (proto) {
768
769 case ETHERTYPE_IP:
770 b0 = gen_cmp(off_linktype, BPF_H, PPP_IP);
771 b1 = gen_cmp(off_linktype, BPF_H, PPP_VJC);
772 gen_or(b0, b1);
773 b0 = gen_cmp(off_linktype, BPF_H, PPP_VJNC);
774 gen_or(b1, b0);
775 return b0;
776
777 #ifdef INET6
778 case ETHERTYPE_IPV6:
779 proto = PPP_IPV6;
780 /* more to go? */
781 break;
782 #endif
783
784 case ETHERTYPE_DN:
785 proto = PPP_DECNET;
786 break;
787
788 case ETHERTYPE_ATALK:
789 proto = PPP_APPLE;
790 break;
791
792 case ETHERTYPE_NS:
793 proto = PPP_NS;
794 break;
795 }
796 break;
797
798 case PCAP_ENCAP_NULL:
799 /* XXX */
800 if (proto == ETHERTYPE_IP)
801 return (gen_cmp(0, BPF_W, (bpf_int32)htonl(AF_INET)));
802 #ifdef INET6
803 else if (proto == ETHERTYPE_IPV6)
804 return (gen_cmp(0, BPF_W, (bpf_int32)htonl(AF_INET6)));
805 #endif
806 else
807 return gen_false();
808 }
809 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
810 }
811
812 static struct block *
813 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
814 bpf_u_int32 addr;
815 bpf_u_int32 mask;
816 int dir, proto;
817 u_int src_off, dst_off;
818 {
819 struct block *b0, *b1;
820 u_int offset;
821
822 switch (dir) {
823
824 case Q_SRC:
825 offset = src_off;
826 break;
827
828 case Q_DST:
829 offset = dst_off;
830 break;
831
832 case Q_AND:
833 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
834 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
835 gen_and(b0, b1);
836 return b1;
837
838 case Q_OR:
839 case Q_DEFAULT:
840 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
841 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
842 gen_or(b0, b1);
843 return b1;
844
845 default:
846 abort();
847 }
848 b0 = gen_linktype(proto);
849 b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask);
850 gen_and(b0, b1);
851 return b1;
852 }
853
854 #ifdef INET6
855 static struct block *
856 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
857 struct in6_addr *addr;
858 struct in6_addr *mask;
859 int dir, proto;
860 u_int src_off, dst_off;
861 {
862 struct block *b0, *b1;
863 u_int offset;
864 u_int32_t *a, *m;
865
866 switch (dir) {
867
868 case Q_SRC:
869 offset = src_off;
870 break;
871
872 case Q_DST:
873 offset = dst_off;
874 break;
875
876 case Q_AND:
877 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
878 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
879 gen_and(b0, b1);
880 return b1;
881
882 case Q_OR:
883 case Q_DEFAULT:
884 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
885 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
886 gen_or(b0, b1);
887 return b1;
888
889 default:
890 abort();
891 }
892 /* this order is important */
893 a = (u_int32_t *)addr;
894 m = (u_int32_t *)mask;
895 b1 = gen_mcmp(offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
896 b0 = gen_mcmp(offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
897 gen_and(b0, b1);
898 b0 = gen_mcmp(offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
899 gen_and(b0, b1);
900 b0 = gen_mcmp(offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
901 gen_and(b0, b1);
902 b0 = gen_linktype(proto);
903 gen_and(b0, b1);
904 return b1;
905 }
906 #endif /*INET6*/
907
908 static struct block *
909 gen_ehostop(eaddr, dir)
910 register const u_char *eaddr;
911 register int dir;
912 {
913 register struct block *b0, *b1;
914
915 switch (dir) {
916 case Q_SRC:
917 return gen_bcmp(6, 6, eaddr);
918
919 case Q_DST:
920 return gen_bcmp(0, 6, eaddr);
921
922 case Q_AND:
923 b0 = gen_ehostop(eaddr, Q_SRC);
924 b1 = gen_ehostop(eaddr, Q_DST);
925 gen_and(b0, b1);
926 return b1;
927
928 case Q_DEFAULT:
929 case Q_OR:
930 b0 = gen_ehostop(eaddr, Q_SRC);
931 b1 = gen_ehostop(eaddr, Q_DST);
932 gen_or(b0, b1);
933 return b1;
934 }
935 abort();
936 /* NOTREACHED */
937 }
938
939 /*
940 * Like gen_ehostop, but for PCAP_ENCAP_FDDI
941 */
942 static struct block *
943 gen_fhostop(eaddr, dir)
944 register const u_char *eaddr;
945 register int dir;
946 {
947 struct block *b0, *b1;
948
949 switch (dir) {
950 case Q_SRC:
951 #ifdef PCAP_FDDIPAD
952 return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr);
953 #else
954 return gen_bcmp(6 + 1, 6, eaddr);
955 #endif
956
957 case Q_DST:
958 #ifdef PCAP_FDDIPAD
959 return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr);
960 #else
961 return gen_bcmp(0 + 1, 6, eaddr);
962 #endif
963
964 case Q_AND:
965 b0 = gen_fhostop(eaddr, Q_SRC);
966 b1 = gen_fhostop(eaddr, Q_DST);
967 gen_and(b0, b1);
968 return b1;
969
970 case Q_DEFAULT:
971 case Q_OR:
972 b0 = gen_fhostop(eaddr, Q_SRC);
973 b1 = gen_fhostop(eaddr, Q_DST);
974 gen_or(b0, b1);
975 return b1;
976 }
977 abort();
978 /* NOTREACHED */
979 }
980
981 /*
982 * Like gen_ehostop, but for PCAP_ENCAP_TOKEN_RING
983 */
984 static struct block *
985 gen_thostop(eaddr, dir)
986 register const u_char *eaddr;
987 register int dir;
988 {
989 register struct block *b0, *b1;
990
991 switch (dir) {
992 case Q_SRC:
993 return gen_bcmp(8, 6, eaddr);
994
995 case Q_DST:
996 return gen_bcmp(2, 6, eaddr);
997
998 case Q_AND:
999 b0 = gen_thostop(eaddr, Q_SRC);
1000 b1 = gen_thostop(eaddr, Q_DST);
1001 gen_and(b0, b1);
1002 return b1;
1003
1004 case Q_DEFAULT:
1005 case Q_OR:
1006 b0 = gen_thostop(eaddr, Q_SRC);
1007 b1 = gen_thostop(eaddr, Q_DST);
1008 gen_or(b0, b1);
1009 return b1;
1010 }
1011 abort();
1012 /* NOTREACHED */
1013 }
1014
1015 /*
1016 * This is quite tricky because there may be pad bytes in front of the
1017 * DECNET header, and then there are two possible data packet formats that
1018 * carry both src and dst addresses, plus 5 packet types in a format that
1019 * carries only the src node, plus 2 types that use a different format and
1020 * also carry just the src node.
1021 *
1022 * Yuck.
1023 *
1024 * Instead of doing those all right, we just look for data packets with
1025 * 0 or 1 bytes of padding. If you want to look at other packets, that
1026 * will require a lot more hacking.
1027 *
1028 * To add support for filtering on DECNET "areas" (network numbers)
1029 * one would want to add a "mask" argument to this routine. That would
1030 * make the filter even more inefficient, although one could be clever
1031 * and not generate masking instructions if the mask is 0xFFFF.
1032 */
1033 static struct block *
1034 gen_dnhostop(addr, dir, base_off)
1035 bpf_u_int32 addr;
1036 int dir;
1037 u_int base_off;
1038 {
1039 struct block *b0, *b1, *b2, *tmp;
1040 u_int offset_lh; /* offset if long header is received */
1041 u_int offset_sh; /* offset if short header is received */
1042
1043 switch (dir) {
1044
1045 case Q_DST:
1046 offset_sh = 1; /* follows flags */
1047 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
1048 break;
1049
1050 case Q_SRC:
1051 offset_sh = 3; /* follows flags, dstnode */
1052 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
1053 break;
1054
1055 case Q_AND:
1056 /* Inefficient because we do our Calvinball dance twice */
1057 b0 = gen_dnhostop(addr, Q_SRC, base_off);
1058 b1 = gen_dnhostop(addr, Q_DST, base_off);
1059 gen_and(b0, b1);
1060 return b1;
1061
1062 case Q_OR:
1063 case Q_DEFAULT:
1064 /* Inefficient because we do our Calvinball dance twice */
1065 b0 = gen_dnhostop(addr, Q_SRC, base_off);
1066 b1 = gen_dnhostop(addr, Q_DST, base_off);
1067 gen_or(b0, b1);
1068 return b1;
1069
1070 default:
1071 abort();
1072 }
1073 b0 = gen_linktype(ETHERTYPE_DN);
1074 /* Check for pad = 1, long header case */
1075 tmp = gen_mcmp(base_off + 2, BPF_H,
1076 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
1077 b1 = gen_cmp(base_off + 2 + 1 + offset_lh,
1078 BPF_H, (bpf_int32)ntohs(addr));
1079 gen_and(tmp, b1);
1080 /* Check for pad = 0, long header case */
1081 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
1082 b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
1083 gen_and(tmp, b2);
1084 gen_or(b2, b1);
1085 /* Check for pad = 1, short header case */
1086 tmp = gen_mcmp(base_off + 2, BPF_H,
1087 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
1088 b2 = gen_cmp(base_off + 2 + 1 + offset_sh,
1089 BPF_H, (bpf_int32)ntohs(addr));
1090 gen_and(tmp, b2);
1091 gen_or(b2, b1);
1092 /* Check for pad = 0, short header case */
1093 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
1094 b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
1095 gen_and(tmp, b2);
1096 gen_or(b2, b1);
1097
1098 /* Combine with test for linktype */
1099 gen_and(b0, b1);
1100 return b1;
1101 }
1102
1103 static struct block *
1104 gen_host(addr, mask, proto, dir)
1105 bpf_u_int32 addr;
1106 bpf_u_int32 mask;
1107 int proto;
1108 int dir;
1109 {
1110 struct block *b0, *b1;
1111
1112 switch (proto) {
1113
1114 case Q_DEFAULT:
1115 b0 = gen_host(addr, mask, Q_IP, dir);
1116 if (off_linktype != -1) {
1117 b1 = gen_host(addr, mask, Q_ARP, dir);
1118 gen_or(b0, b1);
1119 b0 = gen_host(addr, mask, Q_RARP, dir);
1120 gen_or(b1, b0);
1121 }
1122 return b0;
1123
1124 case Q_IP:
1125 return gen_hostop(addr, mask, dir, ETHERTYPE_IP,
1126 off_nl + 12, off_nl + 16);
1127
1128 case Q_RARP:
1129 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP,
1130 off_nl + 14, off_nl + 24);
1131
1132 case Q_ARP:
1133 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP,
1134 off_nl + 14, off_nl + 24);
1135
1136 case Q_TCP:
1137 bpf_error("'tcp' modifier applied to host");
1138
1139 case Q_UDP:
1140 bpf_error("'udp' modifier applied to host");
1141
1142 case Q_ICMP:
1143 bpf_error("'icmp' modifier applied to host");
1144
1145 case Q_IGMP:
1146 bpf_error("'igmp' modifier applied to host");
1147
1148 case Q_IGRP:
1149 bpf_error("'igrp' modifier applied to host");
1150
1151 case Q_PIM:
1152 bpf_error("'pim' modifier applied to host");
1153
1154 case Q_ATALK:
1155 bpf_error("ATALK host filtering not implemented");
1156
1157 case Q_DECNET:
1158 return gen_dnhostop(addr, dir, off_nl);
1159
1160 case Q_SCA:
1161 bpf_error("SCA host filtering not implemented");
1162
1163 case Q_LAT:
1164 bpf_error("LAT host filtering not implemented");
1165
1166 case Q_MOPDL:
1167 bpf_error("MOPDL host filtering not implemented");
1168
1169 case Q_MOPRC:
1170 bpf_error("MOPRC host filtering not implemented");
1171
1172 #ifdef INET6
1173 case Q_IPV6:
1174 bpf_error("'ip6' modifier applied to ip host");
1175
1176 case Q_ICMPV6:
1177 bpf_error("'icmp6' modifier applied to host");
1178 #endif /* INET6 */
1179
1180 case Q_AH:
1181 bpf_error("'ah' modifier applied to host");
1182
1183 case Q_ESP:
1184 bpf_error("'esp' modifier applied to host");
1185
1186 default:
1187 abort();
1188 }
1189 /* NOTREACHED */
1190 }
1191
1192 #ifdef INET6
1193 static struct block *
1194 gen_host6(addr, mask, proto, dir)
1195 struct in6_addr *addr;
1196 struct in6_addr *mask;
1197 int proto;
1198 int dir;
1199 {
1200 switch (proto) {
1201
1202 case Q_DEFAULT:
1203 return gen_host6(addr, mask, Q_IPV6, dir);
1204
1205 case Q_IP:
1206 bpf_error("'ip' modifier applied to ip6 host");
1207
1208 case Q_RARP:
1209 bpf_error("'rarp' modifier applied to ip6 host");
1210
1211 case Q_ARP:
1212 bpf_error("'arp' modifier applied to ip6 host");
1213
1214 case Q_TCP:
1215 bpf_error("'tcp' modifier applied to host");
1216
1217 case Q_UDP:
1218 bpf_error("'udp' modifier applied to host");
1219
1220 case Q_ICMP:
1221 bpf_error("'icmp' modifier applied to host");
1222
1223 case Q_IGMP:
1224 bpf_error("'igmp' modifier applied to host");
1225
1226 case Q_IGRP:
1227 bpf_error("'igrp' modifier applied to host");
1228
1229 case Q_PIM:
1230 bpf_error("'pim' modifier applied to host");
1231
1232 case Q_ATALK:
1233 bpf_error("ATALK host filtering not implemented");
1234
1235 case Q_DECNET:
1236 bpf_error("'decnet' modifier applied to ip6 host");
1237
1238 case Q_SCA:
1239 bpf_error("SCA host filtering not implemented");
1240
1241 case Q_LAT:
1242 bpf_error("LAT host filtering not implemented");
1243
1244 case Q_MOPDL:
1245 bpf_error("MOPDL host filtering not implemented");
1246
1247 case Q_MOPRC:
1248 bpf_error("MOPRC host filtering not implemented");
1249
1250 case Q_IPV6:
1251 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6,
1252 off_nl + 8, off_nl + 24);
1253
1254 case Q_ICMPV6:
1255 bpf_error("'icmp6' modifier applied to host");
1256
1257 case Q_AH:
1258 bpf_error("'ah' modifier applied to host");
1259
1260 case Q_ESP:
1261 bpf_error("'esp' modifier applied to host");
1262
1263 default:
1264 abort();
1265 }
1266 /* NOTREACHED */
1267 }
1268 #endif /*INET6*/
1269
1270 #ifndef INET6
1271 static struct block *
1272 gen_gateway(eaddr, alist, proto, dir)
1273 const u_char *eaddr;
1274 bpf_u_int32 **alist;
1275 int proto;
1276 int dir;
1277 {
1278 struct block *b0, *b1, *tmp;
1279
1280 if (dir != 0)
1281 bpf_error("direction applied to 'gateway'");
1282
1283 switch (proto) {
1284 case Q_DEFAULT:
1285 case Q_IP:
1286 case Q_ARP:
1287 case Q_RARP:
1288 if (linktype == PCAP_ENCAP_ETHERNET)
1289 b0 = gen_ehostop(eaddr, Q_OR);
1290 else if (linktype == PCAP_ENCAP_FDDI)
1291 b0 = gen_fhostop(eaddr, Q_OR);
1292 else if (linktype == PCAP_ENCAP_TOKEN_RING)
1293 b0 = gen_thostop(eaddr, Q_OR);
1294 else
1295 bpf_error(
1296 "'gateway' supported only on ethernet, FDDI or token ring");
1297
1298 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
1299 while (*alist) {
1300 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
1301 gen_or(b1, tmp);
1302 b1 = tmp;
1303 }
1304 gen_not(b1);
1305 gen_and(b0, b1);
1306 return b1;
1307 }
1308 bpf_error("illegal modifier of 'gateway'");
1309 /* NOTREACHED */
1310 }
1311 #endif
1312
1313 struct block *
1314 gen_proto_abbrev(proto)
1315 int proto;
1316 {
1317 #ifdef INET6
1318 struct block *b0;
1319 #endif
1320 struct block *b1;
1321
1322 switch (proto) {
1323
1324 case Q_TCP:
1325 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
1326 #ifdef INET6
1327 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
1328 gen_or(b0, b1);
1329 #endif
1330 break;
1331
1332 case Q_UDP:
1333 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
1334 #ifdef INET6
1335 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
1336 gen_or(b0, b1);
1337 #endif
1338 break;
1339
1340 case Q_ICMP:
1341 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
1342 break;
1343
1344 #ifndef IPPROTO_IGMP
1345 #define IPPROTO_IGMP 2
1346 #endif
1347
1348 case Q_IGMP:
1349 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
1350 break;
1351
1352 #ifndef IPPROTO_IGRP
1353 #define IPPROTO_IGRP 9
1354 #endif
1355 case Q_IGRP:
1356 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
1357 break;
1358
1359 #ifndef IPPROTO_PIM
1360 #define IPPROTO_PIM 103
1361 #endif
1362
1363 case Q_PIM:
1364 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
1365 #ifdef INET6
1366 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
1367 gen_or(b0, b1);
1368 #endif
1369 break;
1370
1371 case Q_IP:
1372 b1 = gen_linktype(ETHERTYPE_IP);
1373 break;
1374
1375 case Q_ARP:
1376 b1 = gen_linktype(ETHERTYPE_ARP);
1377 break;
1378
1379 case Q_RARP:
1380 b1 = gen_linktype(ETHERTYPE_REVARP);
1381 break;
1382
1383 case Q_LINK:
1384 bpf_error("link layer applied in wrong context");
1385
1386 case Q_ATALK:
1387 b1 = gen_linktype(ETHERTYPE_ATALK);
1388 break;
1389
1390 case Q_DECNET:
1391 b1 = gen_linktype(ETHERTYPE_DN);
1392 break;
1393
1394 case Q_SCA:
1395 b1 = gen_linktype(ETHERTYPE_SCA);
1396 break;
1397
1398 case Q_LAT:
1399 b1 = gen_linktype(ETHERTYPE_LAT);
1400 break;
1401
1402 case Q_MOPDL:
1403 b1 = gen_linktype(ETHERTYPE_MOPDL);
1404 break;
1405
1406 case Q_MOPRC:
1407 b1 = gen_linktype(ETHERTYPE_MOPRC);
1408 break;
1409
1410 #ifdef INET6
1411 case Q_IPV6:
1412 b1 = gen_linktype(ETHERTYPE_IPV6);
1413 break;
1414
1415 #ifndef IPPROTO_ICMPV6
1416 #define IPPROTO_ICMPV6 58
1417 #endif
1418 case Q_ICMPV6:
1419 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
1420 break;
1421 #endif /* INET6 */
1422
1423 #ifndef IPPROTO_AH
1424 #define IPPROTO_AH 51
1425 #endif
1426 case Q_AH:
1427 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
1428 #ifdef INET6
1429 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
1430 gen_or(b0, b1);
1431 #endif
1432 break;
1433
1434 #ifndef IPPROTO_ESP
1435 #define IPPROTO_ESP 50
1436 #endif
1437 case Q_ESP:
1438 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
1439 #ifdef INET6
1440 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
1441 gen_or(b0, b1);
1442 #endif
1443 break;
1444
1445 default:
1446 abort();
1447 }
1448 return b1;
1449 }
1450
1451 static struct block *
1452 gen_ipfrag()
1453 {
1454 struct slist *s;
1455 struct block *b;
1456
1457 /* not ip frag */
1458 s = new_stmt(BPF_LD|BPF_H|BPF_ABS);
1459 s->s.k = off_nl + 6;
1460 b = new_block(JMP(BPF_JSET));
1461 b->s.k = 0x1fff;
1462 b->stmts = s;
1463 gen_not(b);
1464
1465 return b;
1466 }
1467
1468 static struct block *
1469 gen_portatom(off, v)
1470 int off;
1471 bpf_int32 v;
1472 {
1473 struct slist *s;
1474 struct block *b;
1475
1476 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1477 s->s.k = off_nl;
1478
1479 s->next = new_stmt(BPF_LD|BPF_IND|BPF_H);
1480 s->next->s.k = off_nl + off;
1481
1482 b = new_block(JMP(BPF_JEQ));
1483 b->stmts = s;
1484 b->s.k = v;
1485
1486 return b;
1487 }
1488
1489 #ifdef INET6
1490 static struct block *
1491 gen_portatom6(off, v)
1492 int off;
1493 bpf_int32 v;
1494 {
1495 return gen_cmp(off_nl + 40 + off, BPF_H, v);
1496 }
1497 #endif/*INET6*/
1498
1499 struct block *
1500 gen_portop(port, proto, dir)
1501 int port, proto, dir;
1502 {
1503 struct block *b0, *b1, *tmp;
1504
1505 /* ip proto 'proto' */
1506 tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto);
1507 b0 = gen_ipfrag();
1508 gen_and(tmp, b0);
1509
1510 switch (dir) {
1511 case Q_SRC:
1512 b1 = gen_portatom(0, (bpf_int32)port);
1513 break;
1514
1515 case Q_DST:
1516 b1 = gen_portatom(2, (bpf_int32)port);
1517 break;
1518
1519 case Q_OR:
1520 case Q_DEFAULT:
1521 tmp = gen_portatom(0, (bpf_int32)port);
1522 b1 = gen_portatom(2, (bpf_int32)port);
1523 gen_or(tmp, b1);
1524 break;
1525
1526 case Q_AND:
1527 tmp = gen_portatom(0, (bpf_int32)port);
1528 b1 = gen_portatom(2, (bpf_int32)port);
1529 gen_and(tmp, b1);
1530 break;
1531
1532 default:
1533 abort();
1534 }
1535 gen_and(b0, b1);
1536
1537 return b1;
1538 }
1539
1540 static struct block *
1541 gen_port(port, ip_proto, dir)
1542 int port;
1543 int ip_proto;
1544 int dir;
1545 {
1546 struct block *b0, *b1, *tmp;
1547
1548 /* ether proto ip */
1549 b0 = gen_linktype(ETHERTYPE_IP);
1550
1551 switch (ip_proto) {
1552 case IPPROTO_UDP:
1553 case IPPROTO_TCP:
1554 b1 = gen_portop(port, ip_proto, dir);
1555 break;
1556
1557 case PROTO_UNDEF:
1558 tmp = gen_portop(port, IPPROTO_TCP, dir);
1559 b1 = gen_portop(port, IPPROTO_UDP, dir);
1560 gen_or(tmp, b1);
1561 break;
1562
1563 default:
1564 abort();
1565 }
1566 gen_and(b0, b1);
1567 return b1;
1568 }
1569
1570 #ifdef INET6
1571 struct block *
1572 gen_portop6(port, proto, dir)
1573 int port, proto, dir;
1574 {
1575 struct block *b0, *b1, *tmp;
1576
1577 /* ip proto 'proto' */
1578 b0 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)proto);
1579
1580 switch (dir) {
1581 case Q_SRC:
1582 b1 = gen_portatom6(0, (bpf_int32)port);
1583 break;
1584
1585 case Q_DST:
1586 b1 = gen_portatom6(2, (bpf_int32)port);
1587 break;
1588
1589 case Q_OR:
1590 case Q_DEFAULT:
1591 tmp = gen_portatom6(0, (bpf_int32)port);
1592 b1 = gen_portatom6(2, (bpf_int32)port);
1593 gen_or(tmp, b1);
1594 break;
1595
1596 case Q_AND:
1597 tmp = gen_portatom6(0, (bpf_int32)port);
1598 b1 = gen_portatom6(2, (bpf_int32)port);
1599 gen_and(tmp, b1);
1600 break;
1601
1602 default:
1603 abort();
1604 }
1605 gen_and(b0, b1);
1606
1607 return b1;
1608 }
1609
1610 static struct block *
1611 gen_port6(port, ip_proto, dir)
1612 int port;
1613 int ip_proto;
1614 int dir;
1615 {
1616 struct block *b0, *b1, *tmp;
1617
1618 /* ether proto ip */
1619 b0 = gen_linktype(ETHERTYPE_IPV6);
1620
1621 switch (ip_proto) {
1622 case IPPROTO_UDP:
1623 case IPPROTO_TCP:
1624 b1 = gen_portop6(port, ip_proto, dir);
1625 break;
1626
1627 case PROTO_UNDEF:
1628 tmp = gen_portop6(port, IPPROTO_TCP, dir);
1629 b1 = gen_portop6(port, IPPROTO_UDP, dir);
1630 gen_or(tmp, b1);
1631 break;
1632
1633 default:
1634 abort();
1635 }
1636 gen_and(b0, b1);
1637 return b1;
1638 }
1639 #endif /* INET6 */
1640
1641 static int
1642 lookup_proto(name, proto)
1643 register const char *name;
1644 register int proto;
1645 {
1646 register int v;
1647
1648 switch (proto) {
1649
1650 case Q_DEFAULT:
1651 case Q_IP:
1652 v = pcap_nametoproto(name);
1653 if (v == PROTO_UNDEF)
1654 bpf_error("unknown ip proto '%s'", name);
1655 break;
1656
1657 case Q_LINK:
1658 /* XXX should look up h/w protocol type based on linktype */
1659 v = pcap_nametoeproto(name);
1660 if (v == PROTO_UNDEF)
1661 bpf_error("unknown ether proto '%s'", name);
1662 break;
1663
1664 default:
1665 v = PROTO_UNDEF;
1666 break;
1667 }
1668 return v;
1669 }
1670
1671 #if 0
1672 struct stmt *
1673 gen_joinsp(s, n)
1674 struct stmt **s;
1675 int n;
1676 {
1677 return NULL;
1678 }
1679 #endif
1680
1681 static struct block *
1682 gen_protochain(v, proto, dir)
1683 int v;
1684 int proto;
1685 int dir;
1686 {
1687 #ifdef NO_PROTOCHAIN
1688 return gen_proto(v, proto, dir);
1689 #else
1690 struct block *b0, *b;
1691 struct slist *s[100];
1692 int fix2, fix3, fix4, fix5;
1693 int ahcheck, again, end;
1694 int i, max;
1695 int reg1 = alloc_reg();
1696 int reg2 = alloc_reg();
1697
1698 memset(s, 0, sizeof(s));
1699 fix2 = fix3 = fix4 = fix5 = 0;
1700
1701 switch (proto) {
1702 case Q_IP:
1703 case Q_IPV6:
1704 break;
1705 case Q_DEFAULT:
1706 b0 = gen_protochain(v, Q_IP, dir);
1707 b = gen_protochain(v, Q_IPV6, dir);
1708 gen_or(b0, b);
1709 return b;
1710 default:
1711 bpf_error("bad protocol applied for 'protochain'");
1712 /*NOTREACHED*/
1713 }
1714
1715 no_optimize = 1; /*this code is not compatible with optimzer yet */
1716
1717 /*
1718 * s[0] is a dummy entry to protect other BPF insn from damaged
1719 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
1720 * hard to find interdependency made by jump table fixup.
1721 */
1722 i = 0;
1723 s[i] = new_stmt(0); /*dummy*/
1724 i++;
1725
1726 switch (proto) {
1727 case Q_IP:
1728 b0 = gen_linktype(ETHERTYPE_IP);
1729
1730 /* A = ip->ip_p */
1731 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
1732 s[i]->s.k = off_nl + 9;
1733 i++;
1734 /* X = ip->ip_hl << 2 */
1735 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1736 s[i]->s.k = off_nl;
1737 i++;
1738 break;
1739 #ifdef INET6
1740 case Q_IPV6:
1741 b0 = gen_linktype(ETHERTYPE_IPV6);
1742
1743 /* A = ip6->ip_nxt */
1744 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
1745 s[i]->s.k = off_nl + 6;
1746 i++;
1747 /* X = sizeof(struct ip6_hdr) */
1748 s[i] = new_stmt(BPF_LDX|BPF_IMM);
1749 s[i]->s.k = 40;
1750 i++;
1751 break;
1752 #endif
1753 default:
1754 bpf_error("unsupported proto to gen_protochain");
1755 /*NOTREACHED*/
1756 }
1757
1758 /* again: if (A == v) goto end; else fall through; */
1759 again = i;
1760 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1761 s[i]->s.k = v;
1762 s[i]->s.jt = NULL; /*later*/
1763 s[i]->s.jf = NULL; /*update in next stmt*/
1764 fix5 = i;
1765 i++;
1766
1767 #ifndef IPPROTO_NONE
1768 #define IPPROTO_NONE 59
1769 #endif
1770 /* if (A == IPPROTO_NONE) goto end */
1771 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1772 s[i]->s.jt = NULL; /*later*/
1773 s[i]->s.jf = NULL; /*update in next stmt*/
1774 s[i]->s.k = IPPROTO_NONE;
1775 s[fix5]->s.jf = s[i];
1776 fix2 = i;
1777 i++;
1778
1779 #ifdef INET6
1780 if (proto == Q_IPV6) {
1781 int v6start, v6end, v6advance, j;
1782
1783 v6start = i;
1784 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
1785 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1786 s[i]->s.jt = NULL; /*later*/
1787 s[i]->s.jf = NULL; /*update in next stmt*/
1788 s[i]->s.k = IPPROTO_HOPOPTS;
1789 s[fix2]->s.jf = s[i];
1790 i++;
1791 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
1792 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1793 s[i]->s.jt = NULL; /*later*/
1794 s[i]->s.jf = NULL; /*update in next stmt*/
1795 s[i]->s.k = IPPROTO_DSTOPTS;
1796 i++;
1797 /* if (A == IPPROTO_ROUTING) goto v6advance */
1798 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1799 s[i]->s.jt = NULL; /*later*/
1800 s[i]->s.jf = NULL; /*update in next stmt*/
1801 s[i]->s.k = IPPROTO_ROUTING;
1802 i++;
1803 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
1804 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1805 s[i]->s.jt = NULL; /*later*/
1806 s[i]->s.jf = NULL; /*later*/
1807 s[i]->s.k = IPPROTO_FRAGMENT;
1808 fix3 = i;
1809 v6end = i;
1810 i++;
1811
1812 /* v6advance: */
1813 v6advance = i;
1814
1815 /*
1816 * in short,
1817 * A = P[X + 1];
1818 * X = X + (P[X] + 1) * 8;
1819 */
1820 /* A = X */
1821 s[i] = new_stmt(BPF_MISC|BPF_TXA);
1822 i++;
1823 /* MEM[reg1] = A */
1824 s[i] = new_stmt(BPF_ST);
1825 s[i]->s.k = reg1;
1826 i++;
1827 /* A += 1 */
1828 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1829 s[i]->s.k = 1;
1830 i++;
1831 /* X = A */
1832 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1833 i++;
1834 /* A = P[X + packet head]; */
1835 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1836 s[i]->s.k = off_nl;
1837 i++;
1838 /* MEM[reg2] = A */
1839 s[i] = new_stmt(BPF_ST);
1840 s[i]->s.k = reg2;
1841 i++;
1842 /* X = MEM[reg1] */
1843 s[i] = new_stmt(BPF_LDX|BPF_MEM);
1844 s[i]->s.k = reg1;
1845 i++;
1846 /* A = P[X + packet head] */
1847 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1848 s[i]->s.k = off_nl;
1849 i++;
1850 /* A += 1 */
1851 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1852 s[i]->s.k = 1;
1853 i++;
1854 /* A *= 8 */
1855 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
1856 s[i]->s.k = 8;
1857 i++;
1858 /* X = A; */
1859 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1860 i++;
1861 /* A = MEM[reg2] */
1862 s[i] = new_stmt(BPF_LD|BPF_MEM);
1863 s[i]->s.k = reg2;
1864 i++;
1865
1866 /* goto again; (must use BPF_JA for backward jump) */
1867 s[i] = new_stmt(BPF_JMP|BPF_JA);
1868 s[i]->s.k = again - i - 1;
1869 s[i - 1]->s.jf = s[i];
1870 i++;
1871
1872 /* fixup */
1873 for (j = v6start; j <= v6end; j++)
1874 s[j]->s.jt = s[v6advance];
1875 } else
1876 #endif
1877 {
1878 /* nop */
1879 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1880 s[i]->s.k = 0;
1881 s[fix2]->s.jf = s[i];
1882 i++;
1883 }
1884
1885 /* ahcheck: */
1886 ahcheck = i;
1887 /* if (A == IPPROTO_AH) then fall through; else goto end; */
1888 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
1889 s[i]->s.jt = NULL; /*later*/
1890 s[i]->s.jf = NULL; /*later*/
1891 s[i]->s.k = IPPROTO_AH;
1892 if (fix3)
1893 s[fix3]->s.jf = s[ahcheck];
1894 fix4 = i;
1895 i++;
1896
1897 /*
1898 * in short,
1899 * A = P[X + 1];
1900 * X = X + (P[X] + 2) * 4;
1901 */
1902 /* A = X */
1903 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
1904 i++;
1905 /* MEM[reg1] = A */
1906 s[i] = new_stmt(BPF_ST);
1907 s[i]->s.k = reg1;
1908 i++;
1909 /* A += 1 */
1910 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1911 s[i]->s.k = 1;
1912 i++;
1913 /* X = A */
1914 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1915 i++;
1916 /* A = P[X + packet head]; */
1917 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1918 s[i]->s.k = off_nl;
1919 i++;
1920 /* MEM[reg2] = A */
1921 s[i] = new_stmt(BPF_ST);
1922 s[i]->s.k = reg2;
1923 i++;
1924 /* X = MEM[reg1] */
1925 s[i] = new_stmt(BPF_LDX|BPF_MEM);
1926 s[i]->s.k = reg1;
1927 i++;
1928 /* A = P[X + packet head] */
1929 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
1930 s[i]->s.k = off_nl;
1931 i++;
1932 /* A += 2 */
1933 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1934 s[i]->s.k = 2;
1935 i++;
1936 /* A *= 4 */
1937 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
1938 s[i]->s.k = 4;
1939 i++;
1940 /* X = A; */
1941 s[i] = new_stmt(BPF_MISC|BPF_TAX);
1942 i++;
1943 /* A = MEM[reg2] */
1944 s[i] = new_stmt(BPF_LD|BPF_MEM);
1945 s[i]->s.k = reg2;
1946 i++;
1947
1948 /* goto again; (must use BPF_JA for backward jump) */
1949 s[i] = new_stmt(BPF_JMP|BPF_JA);
1950 s[i]->s.k = again - i - 1;
1951 i++;
1952
1953 /* end: nop */
1954 end = i;
1955 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
1956 s[i]->s.k = 0;
1957 s[fix2]->s.jt = s[end];
1958 s[fix4]->s.jf = s[end];
1959 s[fix5]->s.jt = s[end];
1960 i++;
1961
1962 /*
1963 * make slist chain
1964 */
1965 max = i;
1966 for (i = 0; i < max - 1; i++)
1967 s[i]->next = s[i + 1];
1968 s[max - 1]->next = NULL;
1969
1970 /*
1971 * emit final check
1972 */
1973 b = new_block(JMP(BPF_JEQ));
1974 b->stmts = s[1]; /*remember, s[0] is dummy*/
1975 b->s.k = v;
1976
1977 free_reg(reg1);
1978 free_reg(reg2);
1979
1980 gen_and(b0, b);
1981 return b;
1982 #endif
1983 }
1984
1985 static struct block *
1986 gen_proto(v, proto, dir)
1987 int v;
1988 int proto;
1989 int dir;
1990 {
1991 struct block *b0, *b1;
1992
1993 if (dir != Q_DEFAULT)
1994 bpf_error("direction applied to 'proto'");
1995
1996 switch (proto) {
1997 case Q_DEFAULT:
1998 #ifdef INET6
1999 b0 = gen_proto(v, Q_IP, dir);
2000 b1 = gen_proto(v, Q_IPV6, dir);
2001 gen_or(b0, b1);
2002 return b1;
2003 #else
2004 /*FALLTHROUGH*/
2005 #endif
2006 case Q_IP:
2007 b0 = gen_linktype(ETHERTYPE_IP);
2008 #ifndef CHASE_CHAIN
2009 b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v);
2010 #else
2011 b1 = gen_protochain(v, Q_IP);
2012 #endif
2013 gen_and(b0, b1);
2014 return b1;
2015
2016 case Q_ARP:
2017 bpf_error("arp does not encapsulate another protocol");
2018 /* NOTREACHED */
2019
2020 case Q_RARP:
2021 bpf_error("rarp does not encapsulate another protocol");
2022 /* NOTREACHED */
2023
2024 case Q_ATALK:
2025 bpf_error("atalk encapsulation is not specifiable");
2026 /* NOTREACHED */
2027
2028 case Q_DECNET:
2029 bpf_error("decnet encapsulation is not specifiable");
2030 /* NOTREACHED */
2031
2032 case Q_SCA:
2033 bpf_error("sca does not encapsulate another protocol");
2034 /* NOTREACHED */
2035
2036 case Q_LAT:
2037 bpf_error("lat does not encapsulate another protocol");
2038 /* NOTREACHED */
2039
2040 case Q_MOPRC:
2041 bpf_error("moprc does not encapsulate another protocol");
2042 /* NOTREACHED */
2043
2044 case Q_MOPDL:
2045 bpf_error("mopdl does not encapsulate another protocol");
2046 /* NOTREACHED */
2047
2048 case Q_LINK:
2049 return gen_linktype(v);
2050
2051 case Q_UDP:
2052 bpf_error("'udp proto' is bogus");
2053 /* NOTREACHED */
2054
2055 case Q_TCP:
2056 bpf_error("'tcp proto' is bogus");
2057 /* NOTREACHED */
2058
2059 case Q_ICMP:
2060 bpf_error("'icmp proto' is bogus");
2061 /* NOTREACHED */
2062
2063 case Q_IGMP:
2064 bpf_error("'igmp proto' is bogus");
2065 /* NOTREACHED */
2066
2067 case Q_IGRP:
2068 bpf_error("'igrp proto' is bogus");
2069 /* NOTREACHED */
2070
2071 case Q_PIM:
2072 bpf_error("'pim proto' is bogus");
2073 /* NOTREACHED */
2074
2075 #ifdef INET6
2076 case Q_IPV6:
2077 b0 = gen_linktype(ETHERTYPE_IPV6);
2078 #ifndef CHASE_CHAIN
2079 b1 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)v);
2080 #else
2081 b1 = gen_protochain(v, Q_IPV6);
2082 #endif
2083 gen_and(b0, b1);
2084 return b1;
2085
2086 case Q_ICMPV6:
2087 bpf_error("'icmp6 proto' is bogus");
2088 #endif /* INET6 */
2089
2090 case Q_AH:
2091 bpf_error("'ah proto' is bogus");
2092
2093 case Q_ESP:
2094 bpf_error("'ah proto' is bogus");
2095
2096 default:
2097 abort();
2098 /* NOTREACHED */
2099 }
2100 /* NOTREACHED */
2101 }
2102
2103 struct block *
2104 gen_scode(name, q)
2105 register const char *name;
2106 struct qual q;
2107 {
2108 int proto = q.proto;
2109 int dir = q.dir;
2110 int tproto;
2111 u_char *eaddr;
2112 bpf_u_int32 mask, addr;
2113 #ifndef INET6
2114 bpf_u_int32 **alist;
2115 #else
2116 int tproto6;
2117 struct sockaddr_in *sin;
2118 struct sockaddr_in6 *sin6;
2119 struct addrinfo *res, *res0;
2120 struct in6_addr mask128;
2121 #endif /*INET6*/
2122 struct block *b, *tmp;
2123 int port, real_proto;
2124
2125 switch (q.addr) {
2126
2127 case Q_NET:
2128 addr = pcap_nametonetaddr(name);
2129 if (addr == 0)
2130 bpf_error("unknown network '%s'", name);
2131 /* Left justify network addr and calculate its network mask */
2132 mask = 0xffffffff;
2133 while (addr && (addr & 0xff000000) == 0) {
2134 addr <<= 8;
2135 mask <<= 8;
2136 }
2137 return gen_host(addr, mask, proto, dir);
2138
2139 case Q_DEFAULT:
2140 case Q_HOST:
2141 if (proto == Q_LINK) {
2142 switch (linktype) {
2143
2144 case PCAP_ENCAP_ETHERNET:
2145 eaddr = pcap_ether_hostton(name);
2146 if (eaddr == NULL)
2147 bpf_error(
2148 "unknown ether host '%s'", name);
2149 return gen_ehostop(eaddr, dir);
2150
2151 case PCAP_ENCAP_FDDI:
2152 eaddr = pcap_ether_hostton(name);
2153 if (eaddr == NULL)
2154 bpf_error(
2155 "unknown FDDI host '%s'", name);
2156 return gen_fhostop(eaddr, dir);
2157
2158 case PCAP_ENCAP_TOKEN_RING:
2159 eaddr = pcap_ether_hostton(name);
2160 if (eaddr == NULL)
2161 bpf_error(
2162 "unknown token ring host '%s'", name);
2163 return gen_thostop(eaddr, dir);
2164
2165 default:
2166 bpf_error(
2167 "only ethernet/FDDI/token ring supports link-level host name");
2168 break;
2169 }
2170 } else if (proto == Q_DECNET) {
2171 unsigned short dn_addr = __pcap_nametodnaddr(name);
2172 /*
2173 * I don't think DECNET hosts can be multihomed, so
2174 * there is no need to build up a list of addresses
2175 */
2176 return (gen_host(dn_addr, 0, proto, dir));
2177 } else {
2178 #ifndef INET6
2179 alist = pcap_nametoaddr(name);
2180 if (alist == NULL || *alist == NULL)
2181 bpf_error("unknown host '%s'", name);
2182 tproto = proto;
2183 if (off_linktype == -1 && tproto == Q_DEFAULT)
2184 tproto = Q_IP;
2185 b = gen_host(**alist++, 0xffffffff, tproto, dir);
2186 while (*alist) {
2187 tmp = gen_host(**alist++, 0xffffffff,
2188 tproto, dir);
2189 gen_or(b, tmp);
2190 b = tmp;
2191 }
2192 return b;
2193 #else
2194 memset(&mask128, 0xff, sizeof(mask128));
2195 res0 = res = pcap_nametoaddrinfo(name);
2196 if (res == NULL)
2197 bpf_error("unknown host '%s'", name);
2198 b = tmp = NULL;
2199 tproto = tproto6 = proto;
2200 if (off_linktype == -1 && tproto == Q_DEFAULT) {
2201 tproto = Q_IP;
2202 tproto6 = Q_IPV6;
2203 }
2204 for (res = res0; res; res = res->ai_next) {
2205 switch (res->ai_family) {
2206 case AF_INET:
2207 if (tproto == Q_IPV6)
2208 continue;
2209
2210 sin = (struct sockaddr_in *)
2211 res->ai_addr;
2212 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
2213 0xffffffff, tproto, dir);
2214 break;
2215 case AF_INET6:
2216 if (tproto6 == Q_IP)
2217 continue;
2218
2219 sin6 = (struct sockaddr_in6 *)
2220 res->ai_addr;
2221 tmp = gen_host6(&sin6->sin6_addr,
2222 &mask128, tproto6, dir);
2223 break;
2224 }
2225 if (b)
2226 gen_or(b, tmp);
2227 b = tmp;
2228 }
2229 freeaddrinfo(res0);
2230 if (b == NULL) {
2231 bpf_error("unknown host '%s'%s", name,
2232 (proto == Q_DEFAULT)
2233 ? ""
2234 : " for specified address family");
2235 }
2236 return b;
2237 #endif /*INET6*/
2238 }
2239
2240 case Q_PORT:
2241 if (proto != Q_DEFAULT && proto != Q_UDP && proto != Q_TCP)
2242 bpf_error("illegal qualifier of 'port'");
2243 if (pcap_nametoport(name, &port, &real_proto) == 0)
2244 bpf_error("unknown port '%s'", name);
2245 if (proto == Q_UDP) {
2246 if (real_proto == IPPROTO_TCP)
2247 bpf_error("port '%s' is tcp", name);
2248 else
2249 /* override PROTO_UNDEF */
2250 real_proto = IPPROTO_UDP;
2251 }
2252 if (proto == Q_TCP) {
2253 if (real_proto == IPPROTO_UDP)
2254 bpf_error("port '%s' is udp", name);
2255 else
2256 /* override PROTO_UNDEF */
2257 real_proto = IPPROTO_TCP;
2258 }
2259 #ifndef INET6
2260 return gen_port(port, real_proto, dir);
2261 #else
2262 {
2263 struct block *b;
2264 b = gen_port(port, real_proto, dir);
2265 gen_or(gen_port6(port, real_proto, dir), b);
2266 return b;
2267 }
2268 #endif /* INET6 */
2269
2270 case Q_GATEWAY:
2271 #ifndef INET6
2272 eaddr = pcap_ether_hostton(name);
2273 if (eaddr == NULL)
2274 bpf_error("unknown ether host: %s", name);
2275
2276 alist = pcap_nametoaddr(name);
2277 if (alist == NULL || *alist == NULL)
2278 bpf_error("unknown host '%s'", name);
2279 return gen_gateway(eaddr, alist, proto, dir);
2280 #else
2281 bpf_error("'gateway' not supported in this configuration");
2282 #endif /*INET6*/
2283
2284 case Q_PROTO:
2285 real_proto = lookup_proto(name, proto);
2286 if (real_proto >= 0)
2287 return gen_proto(real_proto, proto, dir);
2288 else
2289 bpf_error("unknown protocol: %s", name);
2290
2291 case Q_PROTOCHAIN:
2292 real_proto = lookup_proto(name, proto);
2293 if (real_proto >= 0)
2294 return gen_protochain(real_proto, proto, dir);
2295 else
2296 bpf_error("unknown protocol: %s", name);
2297
2298
2299 case Q_UNDEF:
2300 syntax();
2301 /* NOTREACHED */
2302 }
2303 abort();
2304 /* NOTREACHED */
2305 }
2306
2307 struct block *
2308 gen_mcode(s1, s2, masklen, q)
2309 register const char *s1, *s2;
2310 register int masklen;
2311 struct qual q;
2312 {
2313 register int nlen, mlen;
2314 bpf_u_int32 n, m;
2315
2316 nlen = __pcap_atoin(s1, &n);
2317 /* Promote short ipaddr */
2318 n <<= 32 - nlen;
2319
2320 if (s2 != NULL) {
2321 mlen = __pcap_atoin(s2, &m);
2322 /* Promote short ipaddr */
2323 m <<= 32 - mlen;
2324 if ((n & ~m) != 0)
2325 bpf_error("non-network bits set in \"%s mask %s\"",
2326 s1, s2);
2327 } else {
2328 /* Convert mask len to mask */
2329 if (masklen > 32)
2330 bpf_error("mask length must be <= 32");
2331 m = 0xffffffff << (32 - masklen);
2332 if ((n & ~m) != 0)
2333 bpf_error("non-network bits set in \"%s/%d\"",
2334 s1, masklen);
2335 }
2336
2337 switch (q.addr) {
2338
2339 case Q_NET:
2340 return gen_host(n, m, q.proto, q.dir);
2341
2342 default:
2343 bpf_error("Mask syntax for networks only");
2344 /* NOTREACHED */
2345 }
2346 }
2347
2348 struct block *
2349 gen_ncode(s, v, q)
2350 register const char *s;
2351 bpf_u_int32 v;
2352 struct qual q;
2353 {
2354 bpf_u_int32 mask;
2355 int proto = q.proto;
2356 int dir = q.dir;
2357 register int vlen;
2358
2359 if (s == NULL)
2360 vlen = 32;
2361 else if (q.proto == Q_DECNET)
2362 vlen = __pcap_atodn(s, &v);
2363 else
2364 vlen = __pcap_atoin(s, &v);
2365
2366 switch (q.addr) {
2367
2368 case Q_DEFAULT:
2369 case Q_HOST:
2370 case Q_NET:
2371 if (proto == Q_DECNET)
2372 return gen_host(v, 0, proto, dir);
2373 else if (proto == Q_LINK) {
2374 bpf_error("illegal link layer address");
2375 } else {
2376 mask = 0xffffffff;
2377 if (s == NULL && q.addr == Q_NET) {
2378 /* Promote short net number */
2379 while (v && (v & 0xff000000) == 0) {
2380 v <<= 8;
2381 mask <<= 8;
2382 }
2383 } else {
2384 /* Promote short ipaddr */
2385 v <<= 32 - vlen;
2386 mask <<= 32 - vlen;
2387 }
2388 return gen_host(v, mask, proto, dir);
2389 }
2390
2391 case Q_PORT:
2392 if (proto == Q_UDP)
2393 proto = IPPROTO_UDP;
2394 else if (proto == Q_TCP)
2395 proto = IPPROTO_TCP;
2396 else if (proto == Q_DEFAULT)
2397 proto = PROTO_UNDEF;
2398 else
2399 bpf_error("illegal qualifier of 'port'");
2400
2401 #ifndef INET6
2402 return gen_port((int)v, proto, dir);
2403 #else
2404 {
2405 struct block *b;
2406 b = gen_port((int)v, proto, dir);
2407 gen_or(gen_port6((int)v, proto, dir), b);
2408 return b;
2409 }
2410 #endif /* INET6 */
2411
2412 case Q_GATEWAY:
2413 bpf_error("'gateway' requires a name");
2414 /* NOTREACHED */
2415
2416 case Q_PROTO:
2417 return gen_proto((int)v, proto, dir);
2418
2419 case Q_PROTOCHAIN:
2420 return gen_protochain((int)v, proto, dir);
2421
2422 case Q_UNDEF:
2423 syntax();
2424 /* NOTREACHED */
2425
2426 default:
2427 abort();
2428 /* NOTREACHED */
2429 }
2430 /* NOTREACHED */
2431 }
2432
2433 #ifdef INET6
2434 struct block *
2435 gen_mcode6(s1, s2, masklen, q)
2436 register const char *s1, *s2;
2437 register int masklen;
2438 struct qual q;
2439 {
2440 struct addrinfo *res;
2441 struct in6_addr *addr;
2442 struct in6_addr mask;
2443 struct block *b;
2444 u_int32_t *a, *m;
2445
2446 if (s2)
2447 bpf_error("no mask %s supported", s2);
2448
2449 res = pcap_nametoaddrinfo(s1);
2450 if (!res)
2451 bpf_error("invalid ip6 address %s", s1);
2452 if (res->ai_next)
2453 bpf_error("%s resolved to multiple address", s1);
2454 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
2455
2456 if (sizeof(mask) * 8 < masklen)
2457 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
2458 memset(&mask, 0xff, masklen / 8);
2459 if (masklen % 8) {
2460 mask.s6_addr[masklen / 8] =
2461 (0xff << (8 - masklen % 8)) & 0xff;
2462 }
2463
2464 a = (u_int32_t *)addr;
2465 m = (u_int32_t *)&mask;
2466 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
2467 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
2468 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
2469 }
2470
2471 switch (q.addr) {
2472
2473 case Q_DEFAULT:
2474 case Q_HOST:
2475 if (masklen != 128)
2476 bpf_error("Mask syntax for networks only");
2477 /* FALLTHROUGH */
2478
2479 case Q_NET:
2480 b = gen_host6(addr, &mask, q.proto, q.dir);
2481 freeaddrinfo(res);
2482 return b;
2483
2484 default:
2485 bpf_error("invalid qualifier against IPv6 address");
2486 /* NOTREACHED */
2487 }
2488 }
2489 #endif /*INET6*/
2490
2491 struct block *
2492 gen_ecode(eaddr, q)
2493 register const u_char *eaddr;
2494 struct qual q;
2495 {
2496 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
2497 if (linktype == PCAP_ENCAP_ETHERNET)
2498 return gen_ehostop(eaddr, (int)q.dir);
2499 if (linktype == PCAP_ENCAP_FDDI)
2500 return gen_fhostop(eaddr, (int)q.dir);
2501 if (linktype == PCAP_ENCAP_TOKEN_RING)
2502 return gen_thostop(eaddr, (int)q.dir);
2503 }
2504 bpf_error("ethernet address used in non-ether expression");
2505 /* NOTREACHED */
2506 }
2507
2508 void
2509 sappend(s0, s1)
2510 struct slist *s0, *s1;
2511 {
2512 /*
2513 * This is definitely not the best way to do this, but the
2514 * lists will rarely get long.
2515 */
2516 while (s0->next)
2517 s0 = s0->next;
2518 s0->next = s1;
2519 }
2520
2521 static struct slist *
2522 xfer_to_x(a)
2523 struct arth *a;
2524 {
2525 struct slist *s;
2526
2527 s = new_stmt(BPF_LDX|BPF_MEM);
2528 s->s.k = a->regno;
2529 return s;
2530 }
2531
2532 static struct slist *
2533 xfer_to_a(a)
2534 struct arth *a;
2535 {
2536 struct slist *s;
2537
2538 s = new_stmt(BPF_LD|BPF_MEM);
2539 s->s.k = a->regno;
2540 return s;
2541 }
2542
2543 struct arth *
2544 gen_load(proto, index, size)
2545 int proto;
2546 struct arth *index;
2547 int size;
2548 {
2549 struct slist *s, *tmp;
2550 struct block *b;
2551 int regno = alloc_reg();
2552
2553 free_reg(index->regno);
2554 switch (size) {
2555
2556 default:
2557 bpf_error("data size must be 1, 2, or 4");
2558
2559 case 1:
2560 size = BPF_B;
2561 break;
2562
2563 case 2:
2564 size = BPF_H;
2565 break;
2566
2567 case 4:
2568 size = BPF_W;
2569 break;
2570 }
2571 switch (proto) {
2572 default:
2573 bpf_error("unsupported index operation");
2574
2575 case Q_LINK:
2576 s = xfer_to_x(index);
2577 tmp = new_stmt(BPF_LD|BPF_IND|size);
2578 sappend(s, tmp);
2579 sappend(index->s, s);
2580 break;
2581
2582 case Q_IP:
2583 case Q_ARP:
2584 case Q_RARP:
2585 case Q_ATALK:
2586 case Q_DECNET:
2587 case Q_SCA:
2588 case Q_LAT:
2589 case Q_MOPRC:
2590 case Q_MOPDL:
2591 #ifdef INET6
2592 case Q_IPV6:
2593 #endif
2594 /* XXX Note that we assume a fixed link header here. */
2595 s = xfer_to_x(index);
2596 tmp = new_stmt(BPF_LD|BPF_IND|size);
2597 tmp->s.k = off_nl;
2598 sappend(s, tmp);
2599 sappend(index->s, s);
2600
2601 b = gen_proto_abbrev(proto);
2602 if (index->b)
2603 gen_and(index->b, b);
2604 index->b = b;
2605 break;
2606
2607 case Q_TCP:
2608 case Q_UDP:
2609 case Q_ICMP:
2610 case Q_IGMP:
2611 case Q_IGRP:
2612 case Q_PIM:
2613 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
2614 s->s.k = off_nl;
2615 sappend(s, xfer_to_a(index));
2616 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
2617 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
2618 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
2619 tmp->s.k = off_nl;
2620 sappend(index->s, s);
2621
2622 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
2623 if (index->b)
2624 gen_and(index->b, b);
2625 #ifdef INET6
2626 gen_and(gen_proto_abbrev(Q_IP), b);
2627 #endif
2628 index->b = b;
2629 break;
2630 #ifdef INET6
2631 case Q_ICMPV6:
2632 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
2633 /*NOTREACHED*/
2634 #endif
2635 }
2636 index->regno = regno;
2637 s = new_stmt(BPF_ST);
2638 s->s.k = regno;
2639 sappend(index->s, s);
2640
2641 return index;
2642 }
2643
2644 struct block *
2645 gen_relation(code, a0, a1, reversed)
2646 int code;
2647 struct arth *a0, *a1;
2648 int reversed;
2649 {
2650 struct slist *s0, *s1, *s2;
2651 struct block *b, *tmp;
2652
2653 s0 = xfer_to_x(a1);
2654 s1 = xfer_to_a(a0);
2655 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
2656 b = new_block(JMP(code));
2657 if (code == BPF_JGT || code == BPF_JGE) {
2658 reversed = !reversed;
2659 b->s.k = 0x80000000;
2660 }
2661 if (reversed)
2662 gen_not(b);
2663
2664 sappend(s1, s2);
2665 sappend(s0, s1);
2666 sappend(a1->s, s0);
2667 sappend(a0->s, a1->s);
2668
2669 b->stmts = a0->s;
2670
2671 free_reg(a0->regno);
2672 free_reg(a1->regno);
2673
2674 /* 'and' together protocol checks */
2675 if (a0->b) {
2676 if (a1->b) {
2677 gen_and(a0->b, tmp = a1->b);
2678 }
2679 else
2680 tmp = a0->b;
2681 } else
2682 tmp = a1->b;
2683
2684 if (tmp)
2685 gen_and(tmp, b);
2686
2687 return b;
2688 }
2689
2690 struct arth *
2691 gen_loadlen()
2692 {
2693 int regno = alloc_reg();
2694 struct arth *a = (struct arth *)newchunk(sizeof(*a));
2695 struct slist *s;
2696
2697 s = new_stmt(BPF_LD|BPF_LEN);
2698 s->next = new_stmt(BPF_ST);
2699 s->next->s.k = regno;
2700 a->s = s;
2701 a->regno = regno;
2702
2703 return a;
2704 }
2705
2706 struct arth *
2707 gen_loadi(val)
2708 int val;
2709 {
2710 struct arth *a;
2711 struct slist *s;
2712 int reg;
2713
2714 a = (struct arth *)newchunk(sizeof(*a));
2715
2716 reg = alloc_reg();
2717
2718 s = new_stmt(BPF_LD|BPF_IMM);
2719 s->s.k = val;
2720 s->next = new_stmt(BPF_ST);
2721 s->next->s.k = reg;
2722 a->s = s;
2723 a->regno = reg;
2724
2725 return a;
2726 }
2727
2728 struct arth *
2729 gen_neg(a)
2730 struct arth *a;
2731 {
2732 struct slist *s;
2733
2734 s = xfer_to_a(a);
2735 sappend(a->s, s);
2736 s = new_stmt(BPF_ALU|BPF_NEG);
2737 s->s.k = 0;
2738 sappend(a->s, s);
2739 s = new_stmt(BPF_ST);
2740 s->s.k = a->regno;
2741 sappend(a->s, s);
2742
2743 return a;
2744 }
2745
2746 struct arth *
2747 gen_arth(code, a0, a1)
2748 int code;
2749 struct arth *a0, *a1;
2750 {
2751 struct slist *s0, *s1, *s2;
2752
2753 s0 = xfer_to_x(a1);
2754 s1 = xfer_to_a(a0);
2755 s2 = new_stmt(BPF_ALU|BPF_X|code);
2756
2757 sappend(s1, s2);
2758 sappend(s0, s1);
2759 sappend(a1->s, s0);
2760 sappend(a0->s, a1->s);
2761
2762 free_reg(a1->regno);
2763
2764 s0 = new_stmt(BPF_ST);
2765 a0->regno = s0->s.k = alloc_reg();
2766 sappend(a0->s, s0);
2767
2768 return a0;
2769 }
2770
2771 /*
2772 * Here we handle simple allocation of the scratch registers.
2773 * If too many registers are alloc'd, the allocator punts.
2774 */
2775 static int regused[BPF_MEMWORDS];
2776 static int curreg;
2777
2778 /*
2779 * Return the next free register.
2780 */
2781 static int
2782 alloc_reg()
2783 {
2784 int n = BPF_MEMWORDS;
2785
2786 while (--n >= 0) {
2787 if (regused[curreg])
2788 curreg = (curreg + 1) % BPF_MEMWORDS;
2789 else {
2790 regused[curreg] = 1;
2791 return curreg;
2792 }
2793 }
2794 bpf_error("too many registers needed to evaluate expression");
2795 /* NOTREACHED */
2796 }
2797
2798 /*
2799 * Return a register to the table so it can
2800 * be used later.
2801 */
2802 static void
2803 free_reg(n)
2804 int n;
2805 {
2806 regused[n] = 0;
2807 }
2808
2809 static struct block *
2810 gen_len(jmp, n)
2811 int jmp, n;
2812 {
2813 struct slist *s;
2814 struct block *b;
2815
2816 s = new_stmt(BPF_LD|BPF_LEN);
2817 b = new_block(JMP(jmp));
2818 b->stmts = s;
2819 b->s.k = n;
2820
2821 return b;
2822 }
2823
2824 struct block *
2825 gen_greater(n)
2826 int n;
2827 {
2828 return gen_len(BPF_JGE, n);
2829 }
2830
2831 struct block *
2832 gen_less(n)
2833 int n;
2834 {
2835 struct block *b;
2836
2837 b = gen_len(BPF_JGT, n);
2838 gen_not(b);
2839
2840 return b;
2841 }
2842
2843 struct block *
2844 gen_byteop(op, idx, val)
2845 int op, idx, val;
2846 {
2847 struct block *b;
2848 struct slist *s;
2849
2850 switch (op) {
2851 default:
2852 abort();
2853
2854 case '=':
2855 return gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
2856
2857 case '<':
2858 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
2859 b->s.code = JMP(BPF_JGE);
2860 gen_not(b);
2861 return b;
2862
2863 case '>':
2864 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
2865 b->s.code = JMP(BPF_JGT);
2866 return b;
2867
2868 case '|':
2869 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
2870 break;
2871
2872 case '&':
2873 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2874 break;
2875 }
2876 s->s.k = val;
2877 b = new_block(JMP(BPF_JEQ));
2878 b->stmts = s;
2879 gen_not(b);
2880
2881 return b;
2882 }
2883
2884 struct block *
2885 gen_broadcast(proto)
2886 int proto;
2887 {
2888 bpf_u_int32 hostmask;
2889 struct block *b0, *b1, *b2;
2890 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
2891
2892 switch (proto) {
2893
2894 case Q_DEFAULT:
2895 case Q_LINK:
2896 if (linktype == PCAP_ENCAP_ETHERNET)
2897 return gen_ehostop(ebroadcast, Q_DST);
2898 if (linktype == PCAP_ENCAP_FDDI)
2899 return gen_fhostop(ebroadcast, Q_DST);
2900 if (linktype == PCAP_ENCAP_TOKEN_RING)
2901 return gen_thostop(ebroadcast, Q_DST);
2902 bpf_error("not a broadcast link");
2903 break;
2904
2905 case Q_IP:
2906 b0 = gen_linktype(ETHERTYPE_IP);
2907 hostmask = ~netmask;
2908 b1 = gen_mcmp(off_nl + 16, BPF_W, (bpf_int32)0, hostmask);
2909 b2 = gen_mcmp(off_nl + 16, BPF_W,
2910 (bpf_int32)(~0 & hostmask), hostmask);
2911 gen_or(b1, b2);
2912 gen_and(b0, b2);
2913 return b2;
2914 }
2915 bpf_error("only ether/ip broadcast filters supported");
2916 }
2917
2918 struct block *
2919 gen_multicast(proto)
2920 int proto;
2921 {
2922 register struct block *b0, *b1;
2923 register struct slist *s;
2924
2925 switch (proto) {
2926
2927 case Q_DEFAULT:
2928 case Q_LINK:
2929 if (linktype == PCAP_ENCAP_ETHERNET) {
2930 /* ether[0] & 1 != 0 */
2931 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2932 s->s.k = 0;
2933 b0 = new_block(JMP(BPF_JSET));
2934 b0->s.k = 1;
2935 b0->stmts = s;
2936 return b0;
2937 }
2938
2939 if (linktype == PCAP_ENCAP_FDDI) {
2940 /* XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX */
2941 /* fddi[1] & 1 != 0 */
2942 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2943 s->s.k = 1;
2944 b0 = new_block(JMP(BPF_JSET));
2945 b0->s.k = 1;
2946 b0->stmts = s;
2947 return b0;
2948 }
2949
2950 /* TODO - check how token ring handles multicast */
2951 /* if (linktype == PCAP_ENCAP_TOKEN_RING) ... */
2952
2953 /* Link not known to support multicasts */
2954 break;
2955
2956 case Q_IP:
2957 b0 = gen_linktype(ETHERTYPE_IP);
2958 b1 = gen_cmp(off_nl + 16, BPF_B, (bpf_int32)224);
2959 b1->s.code = JMP(BPF_JGE);
2960 gen_and(b0, b1);
2961 return b1;
2962
2963 #ifdef INET6
2964 case Q_IPV6:
2965 b0 = gen_linktype(ETHERTYPE_IPV6);
2966 b1 = gen_cmp(off_nl + 24, BPF_B, (bpf_int32)255);
2967 gen_and(b0, b1);
2968 return b1;
2969 #endif /* INET6 */
2970 }
2971 bpf_error("only IP multicast filters supported on ethernet/FDDI");
2972 }
2973
2974 /*
2975 * generate command for inbound/outbound. It's here so we can
2976 * make it link-type specific. 'dir' = 0 implies "inbound",
2977 * = 1 implies "outbound".
2978 */
2979 struct block *
2980 gen_inbound(dir)
2981 int dir;
2982 {
2983 register struct block *b0;
2984
2985 b0 = gen_relation(BPF_JEQ,
2986 gen_load(Q_LINK, gen_loadi(0), 1),
2987 gen_loadi(0),
2988 dir);
2989 return (b0);
2990 }