]> The Tcpdump Group git mirrors - libpcap/blob - gencode.c
We have to include <net/bpf.h> on Digital UNIX to get various ioctls
[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.190 2003-03-28 08:09:48 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 #include <sys/time.h>
37 #endif /* WIN32 */
38
39 /*
40 * XXX - why was this included even on UNIX?
41 */
42 #ifdef __MINGW32__
43 #include "IP6_misc.h"
44 #endif
45
46 #ifndef WIN32
47
48 #ifdef __NetBSD__
49 #include <sys/param.h>
50 #endif
51
52 #include <netinet/in.h>
53
54 #endif /* WIN32 */
55
56 #include <stdlib.h>
57 #include <string.h>
58 #include <memory.h>
59 #include <setjmp.h>
60 #include <stdarg.h>
61
62 #include "pcap-int.h"
63
64 #include "ethertype.h"
65 #include "nlpid.h"
66 #include "llc.h"
67 #include "gencode.h"
68 #include "atmuni31.h"
69 #include "sunatmpos.h"
70 #include "ppp.h"
71 #include "sll.h"
72 #include "arcnet.h"
73 #include "pf.h"
74 #ifdef INET6
75 #ifndef WIN32
76 #include <netdb.h> /* for "struct addrinfo" */
77 #endif /* WIN32 */
78 #endif /*INET6*/
79 #include <pcap-namedb.h>
80
81 #define ETHERMTU 1500
82
83 #ifndef IPPROTO_SCTP
84 #define IPPROTO_SCTP 132
85 #endif
86
87 #ifdef HAVE_OS_PROTO_H
88 #include "os-proto.h"
89 #endif
90
91 #define JMP(c) ((c)|BPF_JMP|BPF_K)
92
93 /* Locals */
94 static jmp_buf top_ctx;
95 static pcap_t *bpf_pcap;
96
97 /* Hack for updating VLAN offsets. */
98 static u_int orig_linktype = -1, orig_nl = -1, orig_nl_nosnap = -1;
99
100 /* XXX */
101 #ifdef PCAP_FDDIPAD
102 int pcap_fddipad = PCAP_FDDIPAD;
103 #else
104 int pcap_fddipad;
105 #endif
106
107 /* VARARGS */
108 void
109 bpf_error(const char *fmt, ...)
110
111 {
112 va_list ap;
113
114 va_start(ap, fmt);
115 if (bpf_pcap != NULL)
116 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
117 fmt, ap);
118 va_end(ap);
119 longjmp(top_ctx, 1);
120 /* NOTREACHED */
121 }
122
123 static void init_linktype(int);
124
125 static int alloc_reg(void);
126 static void free_reg(int);
127
128 static struct block *root;
129
130 /*
131 * We divy out chunks of memory rather than call malloc each time so
132 * we don't have to worry about leaking memory. It's probably
133 * not a big deal if all this memory was wasted but it this ever
134 * goes into a library that would probably not be a good idea.
135 */
136 #define NCHUNKS 16
137 #define CHUNK0SIZE 1024
138 struct chunk {
139 u_int n_left;
140 void *m;
141 };
142
143 static struct chunk chunks[NCHUNKS];
144 static int cur_chunk;
145
146 static void *newchunk(u_int);
147 static void freechunks(void);
148 static inline struct block *new_block(int);
149 static inline struct slist *new_stmt(int);
150 static struct block *gen_retblk(int);
151 static inline void syntax(void);
152
153 static void backpatch(struct block *, struct block *);
154 static void merge(struct block *, struct block *);
155 static struct block *gen_cmp(u_int, u_int, bpf_int32);
156 static struct block *gen_cmp_gt(u_int, u_int, bpf_int32);
157 static struct block *gen_mcmp(u_int, u_int, bpf_int32, bpf_u_int32);
158 static struct block *gen_bcmp(u_int, u_int, const u_char *);
159 static struct block *gen_ncmp(bpf_u_int32, bpf_u_int32, bpf_u_int32,
160 bpf_u_int32, bpf_u_int32, int);
161 static struct block *gen_uncond(int);
162 static inline struct block *gen_true(void);
163 static inline struct block *gen_false(void);
164 static struct block *gen_ether_linktype(int);
165 static struct block *gen_linktype(int);
166 static struct block *gen_snap(bpf_u_int32, bpf_u_int32, u_int);
167 static struct block *gen_llc(int);
168 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
169 #ifdef INET6
170 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
171 #endif
172 static struct block *gen_ahostop(const u_char *, int);
173 static struct block *gen_ehostop(const u_char *, int);
174 static struct block *gen_fhostop(const u_char *, int);
175 static struct block *gen_thostop(const u_char *, int);
176 static struct block *gen_wlanhostop(const u_char *, int);
177 static struct block *gen_ipfchostop(const u_char *, int);
178 static struct block *gen_dnhostop(bpf_u_int32, int, u_int);
179 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int);
180 #ifdef INET6
181 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int);
182 #endif
183 #ifndef INET6
184 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
185 #endif
186 static struct block *gen_ipfrag(void);
187 static struct block *gen_portatom(int, bpf_int32);
188 #ifdef INET6
189 static struct block *gen_portatom6(int, bpf_int32);
190 #endif
191 struct block *gen_portop(int, int, int);
192 static struct block *gen_port(int, int, int);
193 #ifdef INET6
194 struct block *gen_portop6(int, int, int);
195 static struct block *gen_port6(int, int, int);
196 #endif
197 static int lookup_proto(const char *, int);
198 static struct block *gen_protochain(int, int, int);
199 static struct block *gen_proto(int, int, int);
200 static struct slist *xfer_to_x(struct arth *);
201 static struct slist *xfer_to_a(struct arth *);
202 static struct block *gen_mac_multicast(int);
203 static struct block *gen_len(int, int);
204
205 static struct block *gen_msg_abbrev(int type);
206
207 static void *
208 newchunk(n)
209 u_int n;
210 {
211 struct chunk *cp;
212 int k, size;
213
214 #ifndef __NetBSD__
215 /* XXX Round up to nearest long. */
216 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
217 #else
218 /* XXX Round up to structure boundary. */
219 n = ALIGN(n);
220 #endif
221
222 cp = &chunks[cur_chunk];
223 if (n > cp->n_left) {
224 ++cp, k = ++cur_chunk;
225 if (k >= NCHUNKS)
226 bpf_error("out of memory");
227 size = CHUNK0SIZE << k;
228 cp->m = (void *)malloc(size);
229 if (cp->m == NULL)
230 bpf_error("out of memory");
231 memset((char *)cp->m, 0, size);
232 cp->n_left = size;
233 if (n > size)
234 bpf_error("out of memory");
235 }
236 cp->n_left -= n;
237 return (void *)((char *)cp->m + cp->n_left);
238 }
239
240 static void
241 freechunks()
242 {
243 int i;
244
245 cur_chunk = 0;
246 for (i = 0; i < NCHUNKS; ++i)
247 if (chunks[i].m != NULL) {
248 free(chunks[i].m);
249 chunks[i].m = NULL;
250 }
251 }
252
253 /*
254 * A strdup whose allocations are freed after code generation is over.
255 */
256 char *
257 sdup(s)
258 register const char *s;
259 {
260 int n = strlen(s) + 1;
261 char *cp = newchunk(n);
262
263 strlcpy(cp, s, n);
264 return (cp);
265 }
266
267 static inline struct block *
268 new_block(code)
269 int code;
270 {
271 struct block *p;
272
273 p = (struct block *)newchunk(sizeof(*p));
274 p->s.code = code;
275 p->head = p;
276
277 return p;
278 }
279
280 static inline struct slist *
281 new_stmt(code)
282 int code;
283 {
284 struct slist *p;
285
286 p = (struct slist *)newchunk(sizeof(*p));
287 p->s.code = code;
288
289 return p;
290 }
291
292 static struct block *
293 gen_retblk(v)
294 int v;
295 {
296 struct block *b = new_block(BPF_RET|BPF_K);
297
298 b->s.k = v;
299 return b;
300 }
301
302 static inline void
303 syntax()
304 {
305 bpf_error("syntax error in filter expression");
306 }
307
308 static bpf_u_int32 netmask;
309 static int snaplen;
310 int no_optimize;
311
312 int
313 pcap_compile(pcap_t *p, struct bpf_program *program,
314 char *buf, int optimize, bpf_u_int32 mask)
315 {
316 extern int n_errors;
317 int len;
318
319 no_optimize = 0;
320 n_errors = 0;
321 root = NULL;
322 bpf_pcap = p;
323 if (setjmp(top_ctx)) {
324 lex_cleanup();
325 freechunks();
326 return (-1);
327 }
328
329 netmask = mask;
330
331 snaplen = pcap_snapshot(p);
332 if (snaplen == 0) {
333 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
334 "snaplen of 0 rejects all packets");
335 return -1;
336 }
337
338 lex_init(buf ? buf : "");
339 init_linktype(pcap_datalink(p));
340 (void)pcap_parse();
341
342 if (n_errors)
343 syntax();
344
345 if (root == NULL)
346 root = gen_retblk(snaplen);
347
348 if (optimize && !no_optimize) {
349 bpf_optimize(&root);
350 if (root == NULL ||
351 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
352 bpf_error("expression rejects all packets");
353 }
354 program->bf_insns = icode_to_fcode(root, &len);
355 program->bf_len = len;
356
357 lex_cleanup();
358 freechunks();
359 return (0);
360 }
361
362 /*
363 * entry point for using the compiler with no pcap open
364 * pass in all the stuff that is needed explicitly instead.
365 */
366 int
367 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
368 struct bpf_program *program,
369 char *buf, int optimize, bpf_u_int32 mask)
370 {
371 pcap_t *p;
372 int ret;
373
374 p = pcap_open_dead(linktype_arg, snaplen_arg);
375 if (p == NULL)
376 return (-1);
377 ret = pcap_compile(p, program, buf, optimize, mask);
378 pcap_close(p);
379 return (ret);
380 }
381
382 /*
383 * Clean up a "struct bpf_program" by freeing all the memory allocated
384 * in it.
385 */
386 void
387 pcap_freecode(struct bpf_program *program)
388 {
389 program->bf_len = 0;
390 if (program->bf_insns != NULL) {
391 free((char *)program->bf_insns);
392 program->bf_insns = NULL;
393 }
394 }
395
396 /*
397 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
398 * which of the jt and jf fields has been resolved and which is a pointer
399 * back to another unresolved block (or nil). At least one of the fields
400 * in each block is already resolved.
401 */
402 static void
403 backpatch(list, target)
404 struct block *list, *target;
405 {
406 struct block *next;
407
408 while (list) {
409 if (!list->sense) {
410 next = JT(list);
411 JT(list) = target;
412 } else {
413 next = JF(list);
414 JF(list) = target;
415 }
416 list = next;
417 }
418 }
419
420 /*
421 * Merge the lists in b0 and b1, using the 'sense' field to indicate
422 * which of jt and jf is the link.
423 */
424 static void
425 merge(b0, b1)
426 struct block *b0, *b1;
427 {
428 register struct block **p = &b0;
429
430 /* Find end of list. */
431 while (*p)
432 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
433
434 /* Concatenate the lists. */
435 *p = b1;
436 }
437
438 void
439 finish_parse(p)
440 struct block *p;
441 {
442 backpatch(p, gen_retblk(snaplen));
443 p->sense = !p->sense;
444 backpatch(p, gen_retblk(0));
445 root = p->head;
446 }
447
448 void
449 gen_and(b0, b1)
450 struct block *b0, *b1;
451 {
452 backpatch(b0, b1->head);
453 b0->sense = !b0->sense;
454 b1->sense = !b1->sense;
455 merge(b1, b0);
456 b1->sense = !b1->sense;
457 b1->head = b0->head;
458 }
459
460 void
461 gen_or(b0, b1)
462 struct block *b0, *b1;
463 {
464 b0->sense = !b0->sense;
465 backpatch(b0, b1->head);
466 b0->sense = !b0->sense;
467 merge(b1, b0);
468 b1->head = b0->head;
469 }
470
471 void
472 gen_not(b)
473 struct block *b;
474 {
475 b->sense = !b->sense;
476 }
477
478 static struct block *
479 gen_cmp(offset, size, v)
480 u_int offset, size;
481 bpf_int32 v;
482 {
483 struct slist *s;
484 struct block *b;
485
486 s = new_stmt(BPF_LD|BPF_ABS|size);
487 s->s.k = offset;
488
489 b = new_block(JMP(BPF_JEQ));
490 b->stmts = s;
491 b->s.k = v;
492
493 return b;
494 }
495
496 static struct block *
497 gen_cmp_gt(offset, size, v)
498 u_int offset, size;
499 bpf_int32 v;
500 {
501 struct slist *s;
502 struct block *b;
503
504 s = new_stmt(BPF_LD|BPF_ABS|size);
505 s->s.k = offset;
506
507 b = new_block(JMP(BPF_JGT));
508 b->stmts = s;
509 b->s.k = v;
510
511 return b;
512 }
513
514 static struct block *
515 gen_mcmp(offset, size, v, mask)
516 u_int offset, size;
517 bpf_int32 v;
518 bpf_u_int32 mask;
519 {
520 struct block *b = gen_cmp(offset, size, v);
521 struct slist *s;
522
523 if (mask != 0xffffffff) {
524 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
525 s->s.k = mask;
526 b->stmts->next = s;
527 }
528 return b;
529 }
530
531 static struct block *
532 gen_bcmp(offset, size, v)
533 register u_int offset, size;
534 register const u_char *v;
535 {
536 register struct block *b, *tmp;
537
538 b = NULL;
539 while (size >= 4) {
540 register const u_char *p = &v[size - 4];
541 bpf_int32 w = ((bpf_int32)p[0] << 24) |
542 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
543
544 tmp = gen_cmp(offset + size - 4, BPF_W, w);
545 if (b != NULL)
546 gen_and(b, tmp);
547 b = tmp;
548 size -= 4;
549 }
550 while (size >= 2) {
551 register const u_char *p = &v[size - 2];
552 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
553
554 tmp = gen_cmp(offset + size - 2, BPF_H, w);
555 if (b != NULL)
556 gen_and(b, tmp);
557 b = tmp;
558 size -= 2;
559 }
560 if (size > 0) {
561 tmp = gen_cmp(offset, BPF_B, (bpf_int32)v[0]);
562 if (b != NULL)
563 gen_and(b, tmp);
564 b = tmp;
565 }
566 return b;
567 }
568
569 static struct block *
570 gen_ncmp(datasize, offset, mask, jtype, jvalue, reverse)
571 bpf_u_int32 datasize, offset, mask, jtype, jvalue;
572 int reverse;
573 {
574 struct slist *s;
575 struct block *b;
576
577 s = new_stmt(BPF_LD|datasize|BPF_ABS);
578 s->s.k = offset;
579
580 if (mask != 0xffffffff) {
581 s->next = new_stmt(BPF_ALU|BPF_AND|BPF_K);
582 s->next->s.k = mask;
583 }
584
585 b = new_block(JMP(jtype));
586 b->stmts = s;
587 b->s.k = jvalue;
588 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
589 gen_not(b);
590 return b;
591 }
592
593 /*
594 * Various code constructs need to know the layout of the data link
595 * layer. These variables give the necessary offsets.
596 */
597
598 /*
599 * This is the offset of the beginning of the MAC-layer header.
600 * It's usually 0, except for ATM LANE.
601 */
602 static u_int off_mac;
603
604 /*
605 * "off_linktype" is the offset to information in the link-layer header
606 * giving the packet type.
607 *
608 * For Ethernet, it's the offset of the Ethernet type field.
609 *
610 * For link-layer types that always use 802.2 headers, it's the
611 * offset of the LLC header.
612 *
613 * For PPP, it's the offset of the PPP type field.
614 *
615 * For Cisco HDLC, it's the offset of the CHDLC type field.
616 *
617 * For BSD loopback, it's the offset of the AF_ value.
618 *
619 * For Linux cooked sockets, it's the offset of the type field.
620 *
621 * It's set to -1 for no encapsulation, in which case, IP is assumed.
622 */
623 static u_int off_linktype;
624
625 /*
626 * TRUE if the link layer includes an ATM pseudo-header.
627 */
628 static int is_atm = 0;
629
630 /*
631 * TRUE if "lane" appeared in the filter; it causes us to generate
632 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
633 */
634 static int is_lane = 0;
635
636 /*
637 * These are offsets for the ATM pseudo-header.
638 */
639 static u_int off_vpi;
640 static u_int off_vci;
641 static u_int off_proto;
642
643 /*
644 * This is the offset of the first byte after the ATM pseudo_header,
645 * or -1 if there is no ATM pseudo-header.
646 */
647 static u_int off_payload;
648
649 /*
650 * These are offsets to the beginning of the network-layer header.
651 *
652 * If the link layer never uses 802.2 LLC:
653 *
654 * "off_nl" and "off_nl_nosnap" are the same.
655 *
656 * If the link layer always uses 802.2 LLC:
657 *
658 * "off_nl" is the offset if there's a SNAP header following
659 * the 802.2 header;
660 *
661 * "off_nl_nosnap" is the offset if there's no SNAP header.
662 *
663 * If the link layer is Ethernet:
664 *
665 * "off_nl" is the offset if the packet is an Ethernet II packet
666 * (we assume no 802.3+802.2+SNAP);
667 *
668 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
669 * with an 802.2 header following it.
670 */
671 static u_int off_nl;
672 static u_int off_nl_nosnap;
673
674 static int linktype;
675
676 static void
677 init_linktype(type)
678 int type;
679 {
680 linktype = type;
681
682 /*
683 * Assume it's not raw ATM with a pseudo-header, for now.
684 */
685 off_mac = 0;
686 is_atm = 0;
687 is_lane = 0;
688 off_vpi = -1;
689 off_vci = -1;
690 off_proto = -1;
691 off_payload = -1;
692
693 orig_linktype = -1;
694 orig_nl = -1;
695 orig_nl_nosnap = -1;
696
697 switch (type) {
698
699 case DLT_ARCNET:
700 off_linktype = 2;
701 off_nl = 6; /* XXX in reality, variable! */
702 off_nl_nosnap = 6; /* no 802.2 LLC */
703 return;
704
705 case DLT_ARCNET_LINUX:
706 off_linktype = 4;
707 off_nl = 8; /* XXX in reality, variable! */
708 off_nl_nosnap = 8; /* no 802.2 LLC */
709 return;
710
711 case DLT_EN10MB:
712 off_linktype = 12;
713 off_nl = 14; /* Ethernet II */
714 off_nl_nosnap = 17; /* 802.3+802.2 */
715 return;
716
717 case DLT_SLIP:
718 /*
719 * SLIP doesn't have a link level type. The 16 byte
720 * header is hacked into our SLIP driver.
721 */
722 off_linktype = -1;
723 off_nl = 16;
724 off_nl_nosnap = 16; /* no 802.2 LLC */
725 return;
726
727 case DLT_SLIP_BSDOS:
728 /* XXX this may be the same as the DLT_PPP_BSDOS case */
729 off_linktype = -1;
730 /* XXX end */
731 off_nl = 24;
732 off_nl_nosnap = 24; /* no 802.2 LLC */
733 return;
734
735 case DLT_NULL:
736 case DLT_LOOP:
737 off_linktype = 0;
738 off_nl = 4;
739 off_nl_nosnap = 4; /* no 802.2 LLC */
740 return;
741
742 case DLT_ENC:
743 off_linktype = 0;
744 off_nl = 12;
745 off_nl_nosnap = 12; /* no 802.2 LLC */
746 return;
747
748 case DLT_PFLOG:
749 off_linktype = 0;
750 off_nl = 28;
751 off_nl_nosnap = 28; /* no 802.2 LLC */
752 return;
753
754 case DLT_PPP:
755 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
756 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
757 off_linktype = 2;
758 off_nl = 4;
759 off_nl_nosnap = 4; /* no 802.2 LLC */
760 return;
761
762 case DLT_PPP_ETHER:
763 /*
764 * This does no include the Ethernet header, and
765 * only covers session state.
766 */
767 off_linktype = 6;
768 off_nl = 8;
769 off_nl_nosnap = 8; /* no 802.2 LLC */
770 return;
771
772 case DLT_PPP_BSDOS:
773 off_linktype = 5;
774 off_nl = 24;
775 off_nl_nosnap = 24; /* no 802.2 LLC */
776 return;
777
778 case DLT_FDDI:
779 /*
780 * FDDI doesn't really have a link-level type field.
781 * We set "off_linktype" to the offset of the LLC header.
782 *
783 * To check for Ethernet types, we assume that SSAP = SNAP
784 * is being used and pick out the encapsulated Ethernet type.
785 * XXX - should we generate code to check for SNAP?
786 */
787 off_linktype = 13;
788 #ifdef PCAP_FDDIPAD
789 off_linktype += pcap_fddipad;
790 #endif
791 off_nl = 21; /* FDDI+802.2+SNAP */
792 off_nl_nosnap = 16; /* FDDI+802.2 */
793 #ifdef PCAP_FDDIPAD
794 off_nl += pcap_fddipad;
795 off_nl_nosnap += pcap_fddipad;
796 #endif
797 return;
798
799 case DLT_IEEE802:
800 /*
801 * Token Ring doesn't really have a link-level type field.
802 * We set "off_linktype" to the offset of the LLC header.
803 *
804 * To check for Ethernet types, we assume that SSAP = SNAP
805 * is being used and pick out the encapsulated Ethernet type.
806 * XXX - should we generate code to check for SNAP?
807 *
808 * XXX - the header is actually variable-length.
809 * Some various Linux patched versions gave 38
810 * as "off_linktype" and 40 as "off_nl"; however,
811 * if a token ring packet has *no* routing
812 * information, i.e. is not source-routed, the correct
813 * values are 20 and 22, as they are in the vanilla code.
814 *
815 * A packet is source-routed iff the uppermost bit
816 * of the first byte of the source address, at an
817 * offset of 8, has the uppermost bit set. If the
818 * packet is source-routed, the total number of bytes
819 * of routing information is 2 plus bits 0x1F00 of
820 * the 16-bit value at an offset of 14 (shifted right
821 * 8 - figure out which byte that is).
822 */
823 off_linktype = 14;
824 off_nl = 22; /* Token Ring+802.2+SNAP */
825 off_nl_nosnap = 17; /* Token Ring+802.2 */
826 return;
827
828 case DLT_IEEE802_11:
829 /*
830 * 802.11 doesn't really have a link-level type field.
831 * We set "off_linktype" to the offset of the LLC header.
832 *
833 * To check for Ethernet types, we assume that SSAP = SNAP
834 * is being used and pick out the encapsulated Ethernet type.
835 * XXX - should we generate code to check for SNAP?
836 *
837 * XXX - the header is actually variable-length. We
838 * assume a 24-byte link-layer header, as appears in
839 * data frames in networks with no bridges.
840 */
841 off_linktype = 24;
842 off_nl = 32; /* 802.11+802.2+SNAP */
843 off_nl_nosnap = 27; /* 802.11+802.2 */
844 return;
845
846 case DLT_PRISM_HEADER:
847 /*
848 * Same as 802.11, but with an additional header before
849 * the 802.11 header, containing a bunch of additional
850 * information including radio-level information.
851 *
852 * The header is 144 bytes long.
853 *
854 * XXX - same variable-length header problem; at least
855 * the Prism header is fixed-length.
856 */
857 off_linktype = 144+24;
858 off_nl = 144+32; /* Prism+802.11+802.2+SNAP */
859 off_nl_nosnap = 144+27; /* Prism+802.11+802.2 */
860 return;
861
862 case DLT_IEEE802_11_RADIO:
863 /*
864 * Same as 802.11, but with an additional header before
865 * the 802.11 header, containing a bunch of additional
866 * information including radio-level information.
867 *
868 * The header is 64 bytes long.
869 *
870 * XXX - same variable-length header problem, only
871 * more so; this header is also variable-length,
872 * with the length being the 32-bit big-endian
873 * number at an offset of 4 from the beginning
874 * of the radio header.
875 */
876 off_linktype = 64+24;
877 off_nl = 64+32; /* Radio+802.11+802.2+SNAP */
878 off_nl_nosnap = 64+27; /* Radio+802.11+802.2 */
879 return;
880
881 case DLT_ATM_RFC1483:
882 case DLT_ATM_CLIP: /* Linux ATM defines this */
883 /*
884 * assume routed, non-ISO PDUs
885 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
886 */
887 off_linktype = 0;
888 off_nl = 8; /* 802.2+SNAP */
889 off_nl_nosnap = 3; /* 802.2 */
890 return;
891
892 case DLT_SUNATM:
893 /*
894 * Full Frontal ATM; you get AALn PDUs with an ATM
895 * pseudo-header.
896 */
897 is_atm = 1;
898 off_vpi = SUNATM_VPI_POS;
899 off_vci = SUNATM_VCI_POS;
900 off_proto = PROTO_POS;
901 off_mac = -1; /* LLC-encapsulated, so no MAC-layer header */
902 off_payload = SUNATM_PKT_BEGIN_POS;
903 off_linktype = off_payload;
904 off_nl = off_payload+8; /* 802.2+SNAP */
905 off_nl_nosnap = off_payload+3; /* 802.2 */
906 return;
907
908 case DLT_RAW:
909 off_linktype = -1;
910 off_nl = 0;
911 off_nl_nosnap = 0; /* no 802.2 LLC */
912 return;
913
914 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
915 off_linktype = 14;
916 off_nl = 16;
917 off_nl_nosnap = 16; /* no 802.2 LLC */
918 return;
919
920 case DLT_LTALK:
921 /*
922 * LocalTalk does have a 1-byte type field in the LLAP header,
923 * but really it just indicates whether there is a "short" or
924 * "long" DDP packet following.
925 */
926 off_linktype = -1;
927 off_nl = 0;
928 off_nl_nosnap = 0; /* no 802.2 LLC */
929 return;
930
931 case DLT_IP_OVER_FC:
932 /*
933 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
934 * link-level type field. We set "off_linktype" to the
935 * offset of the LLC header.
936 *
937 * To check for Ethernet types, we assume that SSAP = SNAP
938 * is being used and pick out the encapsulated Ethernet type.
939 * XXX - should we generate code to check for SNAP? RFC
940 * 2625 says SNAP should be used.
941 */
942 off_linktype = 16;
943 off_nl = 24; /* IPFC+802.2+SNAP */
944 off_nl_nosnap = 19; /* IPFC+802.2 */
945 return;
946
947 case DLT_FRELAY:
948 /*
949 * XXX - we should set this to handle SNAP-encapsulated
950 * frames (NLPID of 0x80).
951 */
952 off_linktype = -1;
953 off_nl = 0;
954 off_nl_nosnap = 0; /* no 802.2 LLC */
955 return;
956 }
957 bpf_error("unknown data link type %d", linktype);
958 /* NOTREACHED */
959 }
960
961 static struct block *
962 gen_uncond(rsense)
963 int rsense;
964 {
965 struct block *b;
966 struct slist *s;
967
968 s = new_stmt(BPF_LD|BPF_IMM);
969 s->s.k = !rsense;
970 b = new_block(JMP(BPF_JEQ));
971 b->stmts = s;
972
973 return b;
974 }
975
976 static inline struct block *
977 gen_true()
978 {
979 return gen_uncond(1);
980 }
981
982 static inline struct block *
983 gen_false()
984 {
985 return gen_uncond(0);
986 }
987
988 /*
989 * Byte-swap a 32-bit number.
990 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
991 * big-endian platforms.)
992 */
993 #define SWAPLONG(y) \
994 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
995
996 static struct block *
997 gen_ether_linktype(proto)
998 register int proto;
999 {
1000 struct block *b0, *b1;
1001
1002 switch (proto) {
1003
1004 case LLCSAP_ISONS:
1005 /*
1006 * OSI protocols always use 802.2 encapsulation.
1007 * XXX - should we check both the DSAP and the
1008 * SSAP, like this, or should we check just the
1009 * DSAP?
1010 */
1011 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1012 gen_not(b0);
1013 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1014 ((LLCSAP_ISONS << 8) | LLCSAP_ISONS));
1015 gen_and(b0, b1);
1016 return b1;
1017
1018 case LLCSAP_IP:
1019 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1020 gen_not(b0);
1021 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1022 ((LLCSAP_IP << 8) | LLCSAP_IP));
1023 gen_and(b0, b1);
1024 return b1;
1025
1026 case LLCSAP_NETBEUI:
1027 /*
1028 * NetBEUI always uses 802.2 encapsulation.
1029 * XXX - should we check both the DSAP and the
1030 * SSAP, like this, or should we check just the
1031 * DSAP?
1032 */
1033 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1034 gen_not(b0);
1035 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1036 ((LLCSAP_NETBEUI << 8) | LLCSAP_NETBEUI));
1037 gen_and(b0, b1);
1038 return b1;
1039
1040 case LLCSAP_IPX:
1041 /*
1042 * Check for;
1043 *
1044 * Ethernet_II frames, which are Ethernet
1045 * frames with a frame type of ETHERTYPE_IPX;
1046 *
1047 * Ethernet_802.3 frames, which are 802.3
1048 * frames (i.e., the type/length field is
1049 * a length field, <= ETHERMTU, rather than
1050 * a type field) with the first two bytes
1051 * after the Ethernet/802.3 header being
1052 * 0xFFFF;
1053 *
1054 * Ethernet_802.2 frames, which are 802.3
1055 * frames with an 802.2 LLC header and
1056 * with the IPX LSAP as the DSAP in the LLC
1057 * header;
1058 *
1059 * Ethernet_SNAP frames, which are 802.3
1060 * frames with an LLC header and a SNAP
1061 * header and with an OUI of 0x000000
1062 * (encapsulated Ethernet) and a protocol
1063 * ID of ETHERTYPE_IPX in the SNAP header.
1064 *
1065 * XXX - should we generate the same code both
1066 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1067 */
1068
1069 /*
1070 * This generates code to check both for the
1071 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1072 */
1073 b0 = gen_cmp(off_linktype + 2, BPF_B, (bpf_int32)LLCSAP_IPX);
1074 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)0xFFFF);
1075 gen_or(b0, b1);
1076
1077 /*
1078 * Now we add code to check for SNAP frames with
1079 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1080 */
1081 b0 = gen_snap(0x000000, ETHERTYPE_IPX, 14);
1082 gen_or(b0, b1);
1083
1084 /*
1085 * Now we generate code to check for 802.3
1086 * frames in general.
1087 */
1088 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1089 gen_not(b0);
1090
1091 /*
1092 * Now add the check for 802.3 frames before the
1093 * check for Ethernet_802.2 and Ethernet_802.3,
1094 * as those checks should only be done on 802.3
1095 * frames, not on Ethernet frames.
1096 */
1097 gen_and(b0, b1);
1098
1099 /*
1100 * Now add the check for Ethernet_II frames, and
1101 * do that before checking for the other frame
1102 * types.
1103 */
1104 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)ETHERTYPE_IPX);
1105 gen_or(b0, b1);
1106 return b1;
1107
1108 case ETHERTYPE_ATALK:
1109 case ETHERTYPE_AARP:
1110 /*
1111 * EtherTalk (AppleTalk protocols on Ethernet link
1112 * layer) may use 802.2 encapsulation.
1113 */
1114
1115 /*
1116 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1117 * we check for an Ethernet type field less than
1118 * 1500, which means it's an 802.3 length field.
1119 */
1120 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1121 gen_not(b0);
1122
1123 /*
1124 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1125 * SNAP packets with an organization code of
1126 * 0x080007 (Apple, for Appletalk) and a protocol
1127 * type of ETHERTYPE_ATALK (Appletalk).
1128 *
1129 * 802.2-encapsulated ETHERTYPE_AARP packets are
1130 * SNAP packets with an organization code of
1131 * 0x000000 (encapsulated Ethernet) and a protocol
1132 * type of ETHERTYPE_AARP (Appletalk ARP).
1133 */
1134 if (proto == ETHERTYPE_ATALK)
1135 b1 = gen_snap(0x080007, ETHERTYPE_ATALK, 14);
1136 else /* proto == ETHERTYPE_AARP */
1137 b1 = gen_snap(0x000000, ETHERTYPE_AARP, 14);
1138 gen_and(b0, b1);
1139
1140 /*
1141 * Check for Ethernet encapsulation (Ethertalk
1142 * phase 1?); we just check for the Ethernet
1143 * protocol type.
1144 */
1145 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1146
1147 gen_or(b0, b1);
1148 return b1;
1149
1150 default:
1151 if (proto <= ETHERMTU) {
1152 /*
1153 * This is an LLC SAP value, so the frames
1154 * that match would be 802.2 frames.
1155 * Check that the frame is an 802.2 frame
1156 * (i.e., that the length/type field is
1157 * a length field, <= ETHERMTU) and
1158 * then check the DSAP.
1159 */
1160 b0 = gen_cmp_gt(off_linktype, BPF_H, ETHERMTU);
1161 gen_not(b0);
1162 b1 = gen_cmp(off_linktype + 2, BPF_B, (bpf_int32)proto);
1163 gen_and(b0, b1);
1164 return b1;
1165 } else {
1166 /*
1167 * This is an Ethernet type, so compare
1168 * the length/type field with it (if
1169 * the frame is an 802.2 frame, the length
1170 * field will be <= ETHERMTU, and, as
1171 * "proto" is > ETHERMTU, this test
1172 * will fail and the frame won't match,
1173 * which is what we want).
1174 */
1175 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1176 }
1177 }
1178 }
1179
1180 static struct block *
1181 gen_linktype(proto)
1182 register int proto;
1183 {
1184 struct block *b0, *b1, *b2;
1185
1186 switch (linktype) {
1187
1188 case DLT_EN10MB:
1189 return gen_ether_linktype(proto);
1190 break;
1191
1192 case DLT_C_HDLC:
1193 switch (proto) {
1194
1195 case LLCSAP_ISONS:
1196 proto = (proto << 8 | LLCSAP_ISONS);
1197 /* fall through */
1198
1199 default:
1200 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1201 break;
1202 }
1203 break;
1204
1205 case DLT_IEEE802_11:
1206 case DLT_PRISM_HEADER:
1207 case DLT_IEEE802_11_RADIO:
1208 case DLT_FDDI:
1209 case DLT_IEEE802:
1210 case DLT_ATM_RFC1483:
1211 case DLT_ATM_CLIP:
1212 case DLT_IP_OVER_FC:
1213 return gen_llc(proto);
1214 break;
1215
1216 case DLT_SUNATM:
1217 /*
1218 * If "is_lane" is set, check for a LANE-encapsulated
1219 * version of this protocol, otherwise check for an
1220 * LLC-encapsulated version of this protocol.
1221 *
1222 * We assume LANE means Ethernet, not Token Ring.
1223 */
1224 if (is_lane) {
1225 /*
1226 * Check that the packet doesn't begin with an
1227 * LE Control marker. (We've already generated
1228 * a test for LANE.)
1229 */
1230 b0 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
1231 gen_not(b0);
1232
1233 /*
1234 * Now generate an Ethernet test.
1235 */
1236 b1 = gen_ether_linktype(proto);
1237 gen_and(b0, b1);
1238 return b1;
1239 } else {
1240 /*
1241 * Check for LLC encapsulation and then check the
1242 * protocol.
1243 */
1244 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
1245 b1 = gen_llc(proto);
1246 gen_and(b0, b1);
1247 return b1;
1248 }
1249
1250 case DLT_LINUX_SLL:
1251 switch (proto) {
1252
1253 case LLCSAP_IP:
1254 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1255 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1256 ((LLCSAP_IP << 8) | LLCSAP_IP));
1257 gen_and(b0, b1);
1258 return b1;
1259
1260 case LLCSAP_ISONS:
1261 /*
1262 * OSI protocols always use 802.2 encapsulation.
1263 * XXX - should we check both the DSAP and the
1264 * LSAP, like this, or should we check just the
1265 * DSAP?
1266 */
1267 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1268 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1269 ((LLCSAP_ISONS << 8) | LLCSAP_ISONS));
1270 gen_and(b0, b1);
1271 return b1;
1272
1273 case LLCSAP_NETBEUI:
1274 /*
1275 * NetBEUI always uses 802.2 encapsulation.
1276 * XXX - should we check both the DSAP and the
1277 * LSAP, like this, or should we check just the
1278 * DSAP?
1279 */
1280 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1281 b1 = gen_cmp(off_linktype + 2, BPF_H, (bpf_int32)
1282 ((LLCSAP_NETBEUI << 8) | LLCSAP_NETBEUI));
1283 gen_and(b0, b1);
1284 return b1;
1285
1286 case LLCSAP_IPX:
1287 /*
1288 * Ethernet_II frames, which are Ethernet
1289 * frames with a frame type of ETHERTYPE_IPX;
1290 *
1291 * Ethernet_802.3 frames, which have a frame
1292 * type of LINUX_SLL_P_802_3;
1293 *
1294 * Ethernet_802.2 frames, which are 802.3
1295 * frames with an 802.2 LLC header (i.e, have
1296 * a frame type of LINUX_SLL_P_802_2) and
1297 * with the IPX LSAP as the DSAP in the LLC
1298 * header;
1299 *
1300 * Ethernet_SNAP frames, which are 802.3
1301 * frames with an LLC header and a SNAP
1302 * header and with an OUI of 0x000000
1303 * (encapsulated Ethernet) and a protocol
1304 * ID of ETHERTYPE_IPX in the SNAP header.
1305 *
1306 * First, do the checks on LINUX_SLL_P_802_2
1307 * frames; generate the check for either
1308 * Ethernet_802.2 or Ethernet_SNAP frames, and
1309 * then put a check for LINUX_SLL_P_802_2 frames
1310 * before it.
1311 */
1312 b0 = gen_cmp(off_linktype + 2, BPF_B,
1313 (bpf_int32)LLCSAP_IPX);
1314 b1 = gen_snap(0x000000, ETHERTYPE_IPX,
1315 off_linktype + 2);
1316 gen_or(b0, b1);
1317 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1318 gen_and(b0, b1);
1319
1320 /*
1321 * Now check for 802.3 frames and OR that with
1322 * the previous test.
1323 */
1324 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_3);
1325 gen_or(b0, b1);
1326
1327 /*
1328 * Now add the check for Ethernet_II frames, and
1329 * do that before checking for the other frame
1330 * types.
1331 */
1332 b0 = gen_cmp(off_linktype, BPF_H,
1333 (bpf_int32)ETHERTYPE_IPX);
1334 gen_or(b0, b1);
1335 return b1;
1336
1337 case ETHERTYPE_ATALK:
1338 case ETHERTYPE_AARP:
1339 /*
1340 * EtherTalk (AppleTalk protocols on Ethernet link
1341 * layer) may use 802.2 encapsulation.
1342 */
1343
1344 /*
1345 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1346 * we check for the 802.2 protocol type in the
1347 * "Ethernet type" field.
1348 */
1349 b0 = gen_cmp(off_linktype, BPF_H, LINUX_SLL_P_802_2);
1350
1351 /*
1352 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1353 * SNAP packets with an organization code of
1354 * 0x080007 (Apple, for Appletalk) and a protocol
1355 * type of ETHERTYPE_ATALK (Appletalk).
1356 *
1357 * 802.2-encapsulated ETHERTYPE_AARP packets are
1358 * SNAP packets with an organization code of
1359 * 0x000000 (encapsulated Ethernet) and a protocol
1360 * type of ETHERTYPE_AARP (Appletalk ARP).
1361 */
1362 if (proto == ETHERTYPE_ATALK)
1363 b1 = gen_snap(0x080007, ETHERTYPE_ATALK,
1364 off_linktype + 2);
1365 else /* proto == ETHERTYPE_AARP */
1366 b1 = gen_snap(0x000000, ETHERTYPE_AARP,
1367 off_linktype + 2);
1368 gen_and(b0, b1);
1369
1370 /*
1371 * Check for Ethernet encapsulation (Ethertalk
1372 * phase 1?); we just check for the Ethernet
1373 * protocol type.
1374 */
1375 b0 = gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1376
1377 gen_or(b0, b1);
1378 return b1;
1379
1380 default:
1381 if (proto <= ETHERMTU) {
1382 /*
1383 * This is an LLC SAP value, so the frames
1384 * that match would be 802.2 frames.
1385 * Check for the 802.2 protocol type
1386 * in the "Ethernet type" field, and
1387 * then check the DSAP.
1388 */
1389 b0 = gen_cmp(off_linktype, BPF_H,
1390 LINUX_SLL_P_802_2);
1391 b1 = gen_cmp(off_linktype + 2, BPF_B,
1392 (bpf_int32)proto);
1393 gen_and(b0, b1);
1394 return b1;
1395 } else {
1396 /*
1397 * This is an Ethernet type, so compare
1398 * the length/type field with it (if
1399 * the frame is an 802.2 frame, the length
1400 * field will be <= ETHERMTU, and, as
1401 * "proto" is > ETHERMTU, this test
1402 * will fail and the frame won't match,
1403 * which is what we want).
1404 */
1405 return gen_cmp(off_linktype, BPF_H,
1406 (bpf_int32)proto);
1407 }
1408 }
1409 break;
1410
1411 case DLT_SLIP:
1412 case DLT_SLIP_BSDOS:
1413 case DLT_RAW:
1414 /*
1415 * These types don't provide any type field; packets
1416 * are always IP.
1417 *
1418 * XXX - for IPv4, check for a version number of 4, and,
1419 * for IPv6, check for a version number of 6?
1420 */
1421 switch (proto) {
1422
1423 case ETHERTYPE_IP:
1424 #ifdef INET6
1425 case ETHERTYPE_IPV6:
1426 #endif
1427 return gen_true(); /* always true */
1428
1429 default:
1430 return gen_false(); /* always false */
1431 }
1432 break;
1433
1434 case DLT_PPP:
1435 case DLT_PPP_SERIAL:
1436 case DLT_PPP_ETHER:
1437 /*
1438 * We use Ethernet protocol types inside libpcap;
1439 * map them to the corresponding PPP protocol types.
1440 */
1441 switch (proto) {
1442
1443 case ETHERTYPE_IP:
1444 proto = PPP_IP;
1445 break;
1446
1447 #ifdef INET6
1448 case ETHERTYPE_IPV6:
1449 proto = PPP_IPV6;
1450 break;
1451 #endif
1452
1453 case ETHERTYPE_DN:
1454 proto = PPP_DECNET;
1455 break;
1456
1457 case ETHERTYPE_ATALK:
1458 proto = PPP_APPLE;
1459 break;
1460
1461 case ETHERTYPE_NS:
1462 proto = PPP_NS;
1463 break;
1464
1465 case LLCSAP_ISONS:
1466 proto = PPP_OSI;
1467 break;
1468
1469 case LLCSAP_8021D:
1470 /*
1471 * I'm assuming the "Bridging PDU"s that go
1472 * over PPP are Spanning Tree Protocol
1473 * Bridging PDUs.
1474 */
1475 proto = PPP_BRPDU;
1476 break;
1477
1478 case LLCSAP_IPX:
1479 proto = PPP_IPX;
1480 break;
1481 }
1482 break;
1483
1484 case DLT_PPP_BSDOS:
1485 /*
1486 * We use Ethernet protocol types inside libpcap;
1487 * map them to the corresponding PPP protocol types.
1488 */
1489 switch (proto) {
1490
1491 case ETHERTYPE_IP:
1492 b0 = gen_cmp(off_linktype, BPF_H, PPP_IP);
1493 b1 = gen_cmp(off_linktype, BPF_H, PPP_VJC);
1494 gen_or(b0, b1);
1495 b0 = gen_cmp(off_linktype, BPF_H, PPP_VJNC);
1496 gen_or(b1, b0);
1497 return b0;
1498
1499 #ifdef INET6
1500 case ETHERTYPE_IPV6:
1501 proto = PPP_IPV6;
1502 /* more to go? */
1503 break;
1504 #endif
1505
1506 case ETHERTYPE_DN:
1507 proto = PPP_DECNET;
1508 break;
1509
1510 case ETHERTYPE_ATALK:
1511 proto = PPP_APPLE;
1512 break;
1513
1514 case ETHERTYPE_NS:
1515 proto = PPP_NS;
1516 break;
1517
1518 case LLCSAP_ISONS:
1519 proto = PPP_OSI;
1520 break;
1521
1522 case LLCSAP_8021D:
1523 /*
1524 * I'm assuming the "Bridging PDU"s that go
1525 * over PPP are Spanning Tree Protocol
1526 * Bridging PDUs.
1527 */
1528 proto = PPP_BRPDU;
1529 break;
1530
1531 case LLCSAP_IPX:
1532 proto = PPP_IPX;
1533 break;
1534 }
1535 break;
1536
1537 case DLT_NULL:
1538 case DLT_LOOP:
1539 case DLT_ENC:
1540 case DLT_PFLOG:
1541 /*
1542 * For DLT_NULL, the link-layer header is a 32-bit
1543 * word containing an AF_ value in *host* byte order,
1544 * and for DLT_ENC, the link-layer header begins
1545 * with a 32-bit work containing an AF_ value in
1546 * host byte order.
1547 *
1548 * In addition, if we're reading a saved capture file,
1549 * the host byte order in the capture may not be the
1550 * same as the host byte order on this machine.
1551 *
1552 * For DLT_LOOP, the link-layer header is a 32-bit
1553 * word containing an AF_ value in *network* byte order.
1554 *
1555 * XXX - AF_ values may, unfortunately, be platform-
1556 * dependent; for example, FreeBSD's AF_INET6 is 24
1557 * whilst NetBSD's and OpenBSD's is 26.
1558 *
1559 * This means that, when reading a capture file, just
1560 * checking for our AF_INET6 value won't work if the
1561 * capture file came from another OS.
1562 *
1563 * XXX - what's the byte order for DLT_PFLOG?
1564 */
1565 switch (proto) {
1566
1567 case ETHERTYPE_IP:
1568 proto = AF_INET;
1569 break;
1570
1571 #ifdef INET6
1572 case ETHERTYPE_IPV6:
1573 proto = AF_INET6;
1574 break;
1575 #endif
1576
1577 default:
1578 /*
1579 * Not a type on which we support filtering.
1580 * XXX - support those that have AF_ values
1581 * #defined on this platform, at least?
1582 */
1583 return gen_false();
1584 }
1585
1586 if (linktype == DLT_NULL || linktype == DLT_ENC) {
1587 /*
1588 * The AF_ value is in host byte order, but
1589 * the BPF interpreter will convert it to
1590 * network byte order.
1591 *
1592 * If this is a save file, and it's from a
1593 * machine with the opposite byte order to
1594 * ours, we byte-swap the AF_ value.
1595 *
1596 * Then we run it through "htonl()", and
1597 * generate code to compare against the result.
1598 */
1599 if (bpf_pcap->sf.rfile != NULL &&
1600 bpf_pcap->sf.swapped)
1601 proto = SWAPLONG(proto);
1602 proto = htonl(proto);
1603 }
1604 return (gen_cmp(0, BPF_W, (bpf_int32)proto));
1605
1606 case DLT_ARCNET:
1607 case DLT_ARCNET_LINUX:
1608 /*
1609 * XXX should we check for first fragment if the protocol
1610 * uses PHDS?
1611 */
1612 switch (proto) {
1613
1614 default:
1615 return gen_false();
1616
1617 #ifdef INET6
1618 case ETHERTYPE_IPV6:
1619 return (gen_cmp(off_linktype, BPF_B,
1620 (bpf_int32)ARCTYPE_INET6));
1621 #endif /* INET6 */
1622
1623 case ETHERTYPE_IP:
1624 b0 = gen_cmp(off_linktype, BPF_B,
1625 (bpf_int32)ARCTYPE_IP);
1626 b1 = gen_cmp(off_linktype, BPF_B,
1627 (bpf_int32)ARCTYPE_IP_OLD);
1628 gen_or(b0, b1);
1629 return (b1);
1630
1631 case ETHERTYPE_ARP:
1632 b0 = gen_cmp(off_linktype, BPF_B,
1633 (bpf_int32)ARCTYPE_ARP);
1634 b1 = gen_cmp(off_linktype, BPF_B,
1635 (bpf_int32)ARCTYPE_ARP_OLD);
1636 gen_or(b0, b1);
1637 return (b1);
1638
1639 case ETHERTYPE_REVARP:
1640 return (gen_cmp(off_linktype, BPF_B,
1641 (bpf_int32)ARCTYPE_REVARP));
1642
1643 case ETHERTYPE_ATALK:
1644 return (gen_cmp(off_linktype, BPF_B,
1645 (bpf_int32)ARCTYPE_ATALK));
1646 }
1647 break;
1648
1649 case DLT_LTALK:
1650 switch (proto) {
1651 case ETHERTYPE_ATALK:
1652 return gen_true();
1653 default:
1654 return gen_false();
1655 }
1656 break;
1657
1658 case DLT_FRELAY:
1659 /*
1660 * XXX - assumes a 2-byte Frame Relay header with
1661 * DLCI and flags. What if the address is longer?
1662 */
1663 switch (proto) {
1664
1665 case ETHERTYPE_IP:
1666 /*
1667 * Check for the special NLPID for IP.
1668 */
1669 return gen_cmp(2, BPF_H, (0x03<<8) | 0xcc);
1670
1671 #ifdef INET6
1672 case ETHERTYPE_IPV6:
1673 /*
1674 * Check for the special NLPID for IPv6.
1675 */
1676 return gen_cmp(2, BPF_H, (0x03<<8) | 0x8e);
1677 #endif
1678
1679 case LLCSAP_ISONS:
1680 /*
1681 * Check for several OSI protocols.
1682 *
1683 * Frame Relay packets typically have an OSI
1684 * NLPID at the beginning; we check for each
1685 * of them.
1686 *
1687 * What we check for is the NLPID and a frame
1688 * control field of UI, i.e. 0x03 followed
1689 * by the NLPID.
1690 */
1691 b0 = gen_cmp(2, BPF_H, (0x03<<8) | ISO8473_CLNP);
1692 b1 = gen_cmp(2, BPF_H, (0x03<<8) | ISO9542_ESIS);
1693 b2 = gen_cmp(2, BPF_H, (0x03<<8) | ISO10589_ISIS);
1694 gen_or(b1, b2);
1695 gen_or(b0, b2);
1696 return b2;
1697
1698 default:
1699 return gen_false();
1700 }
1701 break;
1702 }
1703
1704 /*
1705 * All the types that have no encapsulation should either be
1706 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
1707 * all packets are IP packets, or should be handled in some
1708 * special case, if none of them are (if some are and some
1709 * aren't, the lack of encapsulation is a problem, as we'd
1710 * have to find some other way of determining the packet type).
1711 *
1712 * Therefore, if "off_linktype" is -1, there's an error.
1713 */
1714 if (off_linktype == -1)
1715 abort();
1716
1717 /*
1718 * Any type not handled above should always have an Ethernet
1719 * type at an offset of "off_linktype". (PPP is partially
1720 * handled above - the protocol type is mapped from the
1721 * Ethernet and LLC types we use internally to the corresponding
1722 * PPP type - but the PPP type is always specified by a value
1723 * at "off_linktype", so we don't have to do the code generation
1724 * above.)
1725 */
1726 return gen_cmp(off_linktype, BPF_H, (bpf_int32)proto);
1727 }
1728
1729 /*
1730 * Check for an LLC SNAP packet with a given organization code and
1731 * protocol type; we check the entire contents of the 802.2 LLC and
1732 * snap headers, checking for DSAP and SSAP of SNAP and a control
1733 * field of 0x03 in the LLC header, and for the specified organization
1734 * code and protocol type in the SNAP header.
1735 */
1736 static struct block *
1737 gen_snap(orgcode, ptype, offset)
1738 bpf_u_int32 orgcode;
1739 bpf_u_int32 ptype;
1740 u_int offset;
1741 {
1742 u_char snapblock[8];
1743
1744 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
1745 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
1746 snapblock[2] = 0x03; /* control = UI */
1747 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
1748 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
1749 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
1750 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
1751 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
1752 return gen_bcmp(offset, 8, snapblock);
1753 }
1754
1755 /*
1756 * Check for a given protocol value assuming an 802.2 LLC header.
1757 */
1758 static struct block *
1759 gen_llc(proto)
1760 int proto;
1761 {
1762 /*
1763 * XXX - handle token-ring variable-length header.
1764 */
1765 switch (proto) {
1766
1767 case LLCSAP_IP:
1768 return gen_cmp(off_linktype, BPF_H, (long)
1769 ((LLCSAP_IP << 8) | LLCSAP_IP));
1770
1771 case LLCSAP_ISONS:
1772 return gen_cmp(off_linktype, BPF_H, (long)
1773 ((LLCSAP_ISONS << 8) | LLCSAP_ISONS));
1774
1775 case LLCSAP_NETBEUI:
1776 return gen_cmp(off_linktype, BPF_H, (long)
1777 ((LLCSAP_NETBEUI << 8) | LLCSAP_NETBEUI));
1778
1779 case LLCSAP_IPX:
1780 /*
1781 * XXX - are there ever SNAP frames for IPX on
1782 * non-Ethernet 802.x networks?
1783 */
1784 return gen_cmp(off_linktype, BPF_B, (bpf_int32)LLCSAP_IPX);
1785
1786 case ETHERTYPE_ATALK:
1787 /*
1788 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1789 * SNAP packets with an organization code of
1790 * 0x080007 (Apple, for Appletalk) and a protocol
1791 * type of ETHERTYPE_ATALK (Appletalk).
1792 *
1793 * XXX - check for an organization code of
1794 * encapsulated Ethernet as well?
1795 */
1796 return gen_snap(0x080007, ETHERTYPE_ATALK, off_linktype);
1797
1798 default:
1799 /*
1800 * XXX - we don't have to check for IPX 802.3
1801 * here, but should we check for the IPX Ethertype?
1802 */
1803 if (proto <= ETHERMTU) {
1804 /*
1805 * This is an LLC SAP value, so check
1806 * the DSAP.
1807 */
1808 return gen_cmp(off_linktype, BPF_B, (bpf_int32)proto);
1809 } else {
1810 /*
1811 * This is an Ethernet type; we assume that it's
1812 * unlikely that it'll appear in the right place
1813 * at random, and therefore check only the
1814 * location that would hold the Ethernet type
1815 * in a SNAP frame with an organization code of
1816 * 0x000000 (encapsulated Ethernet).
1817 *
1818 * XXX - if we were to check for the SNAP DSAP and
1819 * LSAP, as per XXX, and were also to check for an
1820 * organization code of 0x000000 (encapsulated
1821 * Ethernet), we'd do
1822 *
1823 * return gen_snap(0x000000, proto,
1824 * off_linktype);
1825 *
1826 * here; for now, we don't, as per the above.
1827 * I don't know whether it's worth the extra CPU
1828 * time to do the right check or not.
1829 */
1830 return gen_cmp(off_linktype+6, BPF_H, (bpf_int32)proto);
1831 }
1832 }
1833 }
1834
1835 static struct block *
1836 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
1837 bpf_u_int32 addr;
1838 bpf_u_int32 mask;
1839 int dir, proto;
1840 u_int src_off, dst_off;
1841 {
1842 struct block *b0, *b1;
1843 u_int offset;
1844
1845 switch (dir) {
1846
1847 case Q_SRC:
1848 offset = src_off;
1849 break;
1850
1851 case Q_DST:
1852 offset = dst_off;
1853 break;
1854
1855 case Q_AND:
1856 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
1857 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
1858 gen_and(b0, b1);
1859 return b1;
1860
1861 case Q_OR:
1862 case Q_DEFAULT:
1863 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
1864 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
1865 gen_or(b0, b1);
1866 return b1;
1867
1868 default:
1869 abort();
1870 }
1871 b0 = gen_linktype(proto);
1872 b1 = gen_mcmp(offset, BPF_W, (bpf_int32)addr, mask);
1873 gen_and(b0, b1);
1874 return b1;
1875 }
1876
1877 #ifdef INET6
1878 static struct block *
1879 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
1880 struct in6_addr *addr;
1881 struct in6_addr *mask;
1882 int dir, proto;
1883 u_int src_off, dst_off;
1884 {
1885 struct block *b0, *b1;
1886 u_int offset;
1887 u_int32_t *a, *m;
1888
1889 switch (dir) {
1890
1891 case Q_SRC:
1892 offset = src_off;
1893 break;
1894
1895 case Q_DST:
1896 offset = dst_off;
1897 break;
1898
1899 case Q_AND:
1900 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
1901 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
1902 gen_and(b0, b1);
1903 return b1;
1904
1905 case Q_OR:
1906 case Q_DEFAULT:
1907 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
1908 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
1909 gen_or(b0, b1);
1910 return b1;
1911
1912 default:
1913 abort();
1914 }
1915 /* this order is important */
1916 a = (u_int32_t *)addr;
1917 m = (u_int32_t *)mask;
1918 b1 = gen_mcmp(offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
1919 b0 = gen_mcmp(offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
1920 gen_and(b0, b1);
1921 b0 = gen_mcmp(offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
1922 gen_and(b0, b1);
1923 b0 = gen_mcmp(offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
1924 gen_and(b0, b1);
1925 b0 = gen_linktype(proto);
1926 gen_and(b0, b1);
1927 return b1;
1928 }
1929 #endif /*INET6*/
1930
1931 static struct block *
1932 gen_ehostop(eaddr, dir)
1933 register const u_char *eaddr;
1934 register int dir;
1935 {
1936 register struct block *b0, *b1;
1937
1938 switch (dir) {
1939 case Q_SRC:
1940 return gen_bcmp(off_mac + 6, 6, eaddr);
1941
1942 case Q_DST:
1943 return gen_bcmp(off_mac + 0, 6, eaddr);
1944
1945 case Q_AND:
1946 b0 = gen_ehostop(eaddr, Q_SRC);
1947 b1 = gen_ehostop(eaddr, Q_DST);
1948 gen_and(b0, b1);
1949 return b1;
1950
1951 case Q_DEFAULT:
1952 case Q_OR:
1953 b0 = gen_ehostop(eaddr, Q_SRC);
1954 b1 = gen_ehostop(eaddr, Q_DST);
1955 gen_or(b0, b1);
1956 return b1;
1957 }
1958 abort();
1959 /* NOTREACHED */
1960 }
1961
1962 /*
1963 * Like gen_ehostop, but for DLT_FDDI
1964 */
1965 static struct block *
1966 gen_fhostop(eaddr, dir)
1967 register const u_char *eaddr;
1968 register int dir;
1969 {
1970 struct block *b0, *b1;
1971
1972 switch (dir) {
1973 case Q_SRC:
1974 #ifdef PCAP_FDDIPAD
1975 return gen_bcmp(6 + 1 + pcap_fddipad, 6, eaddr);
1976 #else
1977 return gen_bcmp(6 + 1, 6, eaddr);
1978 #endif
1979
1980 case Q_DST:
1981 #ifdef PCAP_FDDIPAD
1982 return gen_bcmp(0 + 1 + pcap_fddipad, 6, eaddr);
1983 #else
1984 return gen_bcmp(0 + 1, 6, eaddr);
1985 #endif
1986
1987 case Q_AND:
1988 b0 = gen_fhostop(eaddr, Q_SRC);
1989 b1 = gen_fhostop(eaddr, Q_DST);
1990 gen_and(b0, b1);
1991 return b1;
1992
1993 case Q_DEFAULT:
1994 case Q_OR:
1995 b0 = gen_fhostop(eaddr, Q_SRC);
1996 b1 = gen_fhostop(eaddr, Q_DST);
1997 gen_or(b0, b1);
1998 return b1;
1999 }
2000 abort();
2001 /* NOTREACHED */
2002 }
2003
2004 /*
2005 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
2006 */
2007 static struct block *
2008 gen_thostop(eaddr, dir)
2009 register const u_char *eaddr;
2010 register int dir;
2011 {
2012 register struct block *b0, *b1;
2013
2014 switch (dir) {
2015 case Q_SRC:
2016 return gen_bcmp(8, 6, eaddr);
2017
2018 case Q_DST:
2019 return gen_bcmp(2, 6, eaddr);
2020
2021 case Q_AND:
2022 b0 = gen_thostop(eaddr, Q_SRC);
2023 b1 = gen_thostop(eaddr, Q_DST);
2024 gen_and(b0, b1);
2025 return b1;
2026
2027 case Q_DEFAULT:
2028 case Q_OR:
2029 b0 = gen_thostop(eaddr, Q_SRC);
2030 b1 = gen_thostop(eaddr, Q_DST);
2031 gen_or(b0, b1);
2032 return b1;
2033 }
2034 abort();
2035 /* NOTREACHED */
2036 }
2037
2038 /*
2039 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN)
2040 */
2041 static struct block *
2042 gen_wlanhostop(eaddr, dir)
2043 register const u_char *eaddr;
2044 register int dir;
2045 {
2046 register struct block *b0, *b1, *b2;
2047 register struct slist *s;
2048
2049 switch (dir) {
2050 case Q_SRC:
2051 /*
2052 * Oh, yuk.
2053 *
2054 * For control frames, there is no SA.
2055 *
2056 * For management frames, SA is at an
2057 * offset of 10 from the beginning of
2058 * the packet.
2059 *
2060 * For data frames, SA is at an offset
2061 * of 10 from the beginning of the packet
2062 * if From DS is clear, at an offset of
2063 * 16 from the beginning of the packet
2064 * if From DS is set and To DS is clear,
2065 * and an offset of 24 from the beginning
2066 * of the packet if From DS is set and To DS
2067 * is set.
2068 */
2069
2070 /*
2071 * Generate the tests to be done for data frames
2072 * with From DS set.
2073 *
2074 * First, check for To DS set, i.e. check "link[1] & 0x01".
2075 */
2076 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2077 s->s.k = 1;
2078 b1 = new_block(JMP(BPF_JSET));
2079 b1->s.k = 0x01; /* To DS */
2080 b1->stmts = s;
2081
2082 /*
2083 * If To DS is set, the SA is at 24.
2084 */
2085 b0 = gen_bcmp(24, 6, eaddr);
2086 gen_and(b1, b0);
2087
2088 /*
2089 * Now, check for To DS not set, i.e. check
2090 * "!(link[1] & 0x01)".
2091 */
2092 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2093 s->s.k = 1;
2094 b2 = new_block(JMP(BPF_JSET));
2095 b2->s.k = 0x01; /* To DS */
2096 b2->stmts = s;
2097 gen_not(b2);
2098
2099 /*
2100 * If To DS is not set, the SA is at 16.
2101 */
2102 b1 = gen_bcmp(16, 6, eaddr);
2103 gen_and(b2, b1);
2104
2105 /*
2106 * Now OR together the last two checks. That gives
2107 * the complete set of checks for data frames with
2108 * From DS set.
2109 */
2110 gen_or(b1, b0);
2111
2112 /*
2113 * Now check for From DS being set, and AND that with
2114 * the ORed-together checks.
2115 */
2116 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2117 s->s.k = 1;
2118 b1 = new_block(JMP(BPF_JSET));
2119 b1->s.k = 0x02; /* From DS */
2120 b1->stmts = s;
2121 gen_and(b1, b0);
2122
2123 /*
2124 * Now check for data frames with From DS not set.
2125 */
2126 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2127 s->s.k = 1;
2128 b2 = new_block(JMP(BPF_JSET));
2129 b2->s.k = 0x02; /* From DS */
2130 b2->stmts = s;
2131 gen_not(b2);
2132
2133 /*
2134 * If From DS isn't set, the SA is at 10.
2135 */
2136 b1 = gen_bcmp(10, 6, eaddr);
2137 gen_and(b2, b1);
2138
2139 /*
2140 * Now OR together the checks for data frames with
2141 * From DS not set and for data frames with From DS
2142 * set; that gives the checks done for data frames.
2143 */
2144 gen_or(b1, b0);
2145
2146 /*
2147 * Now check for a data frame.
2148 * I.e, check "link[0] & 0x08".
2149 */
2150 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2151 s->s.k = 0;
2152 b1 = new_block(JMP(BPF_JSET));
2153 b1->s.k = 0x08;
2154 b1->stmts = s;
2155
2156 /*
2157 * AND that with the checks done for data frames.
2158 */
2159 gen_and(b1, b0);
2160
2161 /*
2162 * If the high-order bit of the type value is 0, this
2163 * is a management frame.
2164 * I.e, check "!(link[0] & 0x08)".
2165 */
2166 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2167 s->s.k = 0;
2168 b2 = new_block(JMP(BPF_JSET));
2169 b2->s.k = 0x08;
2170 b2->stmts = s;
2171 gen_not(b2);
2172
2173 /*
2174 * For management frames, the SA is at 10.
2175 */
2176 b1 = gen_bcmp(10, 6, eaddr);
2177 gen_and(b2, b1);
2178
2179 /*
2180 * OR that with the checks done for data frames.
2181 * That gives the checks done for management and
2182 * data frames.
2183 */
2184 gen_or(b1, b0);
2185
2186 /*
2187 * If the low-order bit of the type value is 1,
2188 * this is either a control frame or a frame
2189 * with a reserved type, and thus not a
2190 * frame with an SA.
2191 *
2192 * I.e., check "!(link[0] & 0x04)".
2193 */
2194 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2195 s->s.k = 0;
2196 b1 = new_block(JMP(BPF_JSET));
2197 b1->s.k = 0x04;
2198 b1->stmts = s;
2199 gen_not(b1);
2200
2201 /*
2202 * AND that with the checks for data and management
2203 * frames.
2204 */
2205 gen_and(b1, b0);
2206 return b0;
2207
2208 case Q_DST:
2209 /*
2210 * Oh, yuk.
2211 *
2212 * For control frames, there is no DA.
2213 *
2214 * For management frames, DA is at an
2215 * offset of 4 from the beginning of
2216 * the packet.
2217 *
2218 * For data frames, DA is at an offset
2219 * of 4 from the beginning of the packet
2220 * if To DS is clear and at an offset of
2221 * 16 from the beginning of the packet
2222 * if To DS is set.
2223 */
2224
2225 /*
2226 * Generate the tests to be done for data frames.
2227 *
2228 * First, check for To DS set, i.e. "link[1] & 0x01".
2229 */
2230 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2231 s->s.k = 1;
2232 b1 = new_block(JMP(BPF_JSET));
2233 b1->s.k = 0x01; /* To DS */
2234 b1->stmts = s;
2235
2236 /*
2237 * If To DS is set, the DA is at 16.
2238 */
2239 b0 = gen_bcmp(16, 6, eaddr);
2240 gen_and(b1, b0);
2241
2242 /*
2243 * Now, check for To DS not set, i.e. check
2244 * "!(link[1] & 0x01)".
2245 */
2246 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2247 s->s.k = 1;
2248 b2 = new_block(JMP(BPF_JSET));
2249 b2->s.k = 0x01; /* To DS */
2250 b2->stmts = s;
2251 gen_not(b2);
2252
2253 /*
2254 * If To DS is not set, the DA is at 4.
2255 */
2256 b1 = gen_bcmp(4, 6, eaddr);
2257 gen_and(b2, b1);
2258
2259 /*
2260 * Now OR together the last two checks. That gives
2261 * the complete set of checks for data frames.
2262 */
2263 gen_or(b1, b0);
2264
2265 /*
2266 * Now check for a data frame.
2267 * I.e, check "link[0] & 0x08".
2268 */
2269 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2270 s->s.k = 0;
2271 b1 = new_block(JMP(BPF_JSET));
2272 b1->s.k = 0x08;
2273 b1->stmts = s;
2274
2275 /*
2276 * AND that with the checks done for data frames.
2277 */
2278 gen_and(b1, b0);
2279
2280 /*
2281 * If the high-order bit of the type value is 0, this
2282 * is a management frame.
2283 * I.e, check "!(link[0] & 0x08)".
2284 */
2285 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2286 s->s.k = 0;
2287 b2 = new_block(JMP(BPF_JSET));
2288 b2->s.k = 0x08;
2289 b2->stmts = s;
2290 gen_not(b2);
2291
2292 /*
2293 * For management frames, the DA is at 4.
2294 */
2295 b1 = gen_bcmp(4, 6, eaddr);
2296 gen_and(b2, b1);
2297
2298 /*
2299 * OR that with the checks done for data frames.
2300 * That gives the checks done for management and
2301 * data frames.
2302 */
2303 gen_or(b1, b0);
2304
2305 /*
2306 * If the low-order bit of the type value is 1,
2307 * this is either a control frame or a frame
2308 * with a reserved type, and thus not a
2309 * frame with an SA.
2310 *
2311 * I.e., check "!(link[0] & 0x04)".
2312 */
2313 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2314 s->s.k = 0;
2315 b1 = new_block(JMP(BPF_JSET));
2316 b1->s.k = 0x04;
2317 b1->stmts = s;
2318 gen_not(b1);
2319
2320 /*
2321 * AND that with the checks for data and management
2322 * frames.
2323 */
2324 gen_and(b1, b0);
2325 return b0;
2326
2327 case Q_AND:
2328 b0 = gen_wlanhostop(eaddr, Q_SRC);
2329 b1 = gen_wlanhostop(eaddr, Q_DST);
2330 gen_and(b0, b1);
2331 return b1;
2332
2333 case Q_DEFAULT:
2334 case Q_OR:
2335 b0 = gen_wlanhostop(eaddr, Q_SRC);
2336 b1 = gen_wlanhostop(eaddr, Q_DST);
2337 gen_or(b0, b1);
2338 return b1;
2339 }
2340 abort();
2341 /* NOTREACHED */
2342 }
2343
2344 /*
2345 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
2346 * (We assume that the addresses are IEEE 48-bit MAC addresses,
2347 * as the RFC states.)
2348 */
2349 static struct block *
2350 gen_ipfchostop(eaddr, dir)
2351 register const u_char *eaddr;
2352 register int dir;
2353 {
2354 register struct block *b0, *b1;
2355
2356 switch (dir) {
2357 case Q_SRC:
2358 return gen_bcmp(10, 6, eaddr);
2359
2360 case Q_DST:
2361 return gen_bcmp(2, 6, eaddr);
2362
2363 case Q_AND:
2364 b0 = gen_ipfchostop(eaddr, Q_SRC);
2365 b1 = gen_ipfchostop(eaddr, Q_DST);
2366 gen_and(b0, b1);
2367 return b1;
2368
2369 case Q_DEFAULT:
2370 case Q_OR:
2371 b0 = gen_ipfchostop(eaddr, Q_SRC);
2372 b1 = gen_ipfchostop(eaddr, Q_DST);
2373 gen_or(b0, b1);
2374 return b1;
2375 }
2376 abort();
2377 /* NOTREACHED */
2378 }
2379
2380 /*
2381 * This is quite tricky because there may be pad bytes in front of the
2382 * DECNET header, and then there are two possible data packet formats that
2383 * carry both src and dst addresses, plus 5 packet types in a format that
2384 * carries only the src node, plus 2 types that use a different format and
2385 * also carry just the src node.
2386 *
2387 * Yuck.
2388 *
2389 * Instead of doing those all right, we just look for data packets with
2390 * 0 or 1 bytes of padding. If you want to look at other packets, that
2391 * will require a lot more hacking.
2392 *
2393 * To add support for filtering on DECNET "areas" (network numbers)
2394 * one would want to add a "mask" argument to this routine. That would
2395 * make the filter even more inefficient, although one could be clever
2396 * and not generate masking instructions if the mask is 0xFFFF.
2397 */
2398 static struct block *
2399 gen_dnhostop(addr, dir, base_off)
2400 bpf_u_int32 addr;
2401 int dir;
2402 u_int base_off;
2403 {
2404 struct block *b0, *b1, *b2, *tmp;
2405 u_int offset_lh; /* offset if long header is received */
2406 u_int offset_sh; /* offset if short header is received */
2407
2408 switch (dir) {
2409
2410 case Q_DST:
2411 offset_sh = 1; /* follows flags */
2412 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
2413 break;
2414
2415 case Q_SRC:
2416 offset_sh = 3; /* follows flags, dstnode */
2417 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
2418 break;
2419
2420 case Q_AND:
2421 /* Inefficient because we do our Calvinball dance twice */
2422 b0 = gen_dnhostop(addr, Q_SRC, base_off);
2423 b1 = gen_dnhostop(addr, Q_DST, base_off);
2424 gen_and(b0, b1);
2425 return b1;
2426
2427 case Q_OR:
2428 case Q_DEFAULT:
2429 /* Inefficient because we do our Calvinball dance twice */
2430 b0 = gen_dnhostop(addr, Q_SRC, base_off);
2431 b1 = gen_dnhostop(addr, Q_DST, base_off);
2432 gen_or(b0, b1);
2433 return b1;
2434
2435 case Q_ISO:
2436 bpf_error("ISO host filtering not implemented");
2437
2438 default:
2439 abort();
2440 }
2441 b0 = gen_linktype(ETHERTYPE_DN);
2442 /* Check for pad = 1, long header case */
2443 tmp = gen_mcmp(base_off + 2, BPF_H,
2444 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
2445 b1 = gen_cmp(base_off + 2 + 1 + offset_lh,
2446 BPF_H, (bpf_int32)ntohs(addr));
2447 gen_and(tmp, b1);
2448 /* Check for pad = 0, long header case */
2449 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
2450 b2 = gen_cmp(base_off + 2 + offset_lh, BPF_H, (bpf_int32)ntohs(addr));
2451 gen_and(tmp, b2);
2452 gen_or(b2, b1);
2453 /* Check for pad = 1, short header case */
2454 tmp = gen_mcmp(base_off + 2, BPF_H,
2455 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
2456 b2 = gen_cmp(base_off + 2 + 1 + offset_sh,
2457 BPF_H, (bpf_int32)ntohs(addr));
2458 gen_and(tmp, b2);
2459 gen_or(b2, b1);
2460 /* Check for pad = 0, short header case */
2461 tmp = gen_mcmp(base_off + 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
2462 b2 = gen_cmp(base_off + 2 + offset_sh, BPF_H, (bpf_int32)ntohs(addr));
2463 gen_and(tmp, b2);
2464 gen_or(b2, b1);
2465
2466 /* Combine with test for linktype */
2467 gen_and(b0, b1);
2468 return b1;
2469 }
2470
2471 static struct block *
2472 gen_host(addr, mask, proto, dir)
2473 bpf_u_int32 addr;
2474 bpf_u_int32 mask;
2475 int proto;
2476 int dir;
2477 {
2478 struct block *b0, *b1;
2479
2480 switch (proto) {
2481
2482 case Q_DEFAULT:
2483 b0 = gen_host(addr, mask, Q_IP, dir);
2484 if (off_linktype != -1) {
2485 b1 = gen_host(addr, mask, Q_ARP, dir);
2486 gen_or(b0, b1);
2487 b0 = gen_host(addr, mask, Q_RARP, dir);
2488 gen_or(b1, b0);
2489 }
2490 return b0;
2491
2492 case Q_IP:
2493 return gen_hostop(addr, mask, dir, ETHERTYPE_IP,
2494 off_nl + 12, off_nl + 16);
2495
2496 case Q_RARP:
2497 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP,
2498 off_nl + 14, off_nl + 24);
2499
2500 case Q_ARP:
2501 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP,
2502 off_nl + 14, off_nl + 24);
2503
2504 case Q_TCP:
2505 bpf_error("'tcp' modifier applied to host");
2506
2507 case Q_SCTP:
2508 bpf_error("'sctp' modifier applied to host");
2509
2510 case Q_UDP:
2511 bpf_error("'udp' modifier applied to host");
2512
2513 case Q_ICMP:
2514 bpf_error("'icmp' modifier applied to host");
2515
2516 case Q_IGMP:
2517 bpf_error("'igmp' modifier applied to host");
2518
2519 case Q_IGRP:
2520 bpf_error("'igrp' modifier applied to host");
2521
2522 case Q_PIM:
2523 bpf_error("'pim' modifier applied to host");
2524
2525 case Q_VRRP:
2526 bpf_error("'vrrp' modifier applied to host");
2527
2528 case Q_ATALK:
2529 bpf_error("ATALK host filtering not implemented");
2530
2531 case Q_AARP:
2532 bpf_error("AARP host filtering not implemented");
2533
2534 case Q_DECNET:
2535 return gen_dnhostop(addr, dir, off_nl);
2536
2537 case Q_SCA:
2538 bpf_error("SCA host filtering not implemented");
2539
2540 case Q_LAT:
2541 bpf_error("LAT host filtering not implemented");
2542
2543 case Q_MOPDL:
2544 bpf_error("MOPDL host filtering not implemented");
2545
2546 case Q_MOPRC:
2547 bpf_error("MOPRC host filtering not implemented");
2548
2549 #ifdef INET6
2550 case Q_IPV6:
2551 bpf_error("'ip6' modifier applied to ip host");
2552
2553 case Q_ICMPV6:
2554 bpf_error("'icmp6' modifier applied to host");
2555 #endif /* INET6 */
2556
2557 case Q_AH:
2558 bpf_error("'ah' modifier applied to host");
2559
2560 case Q_ESP:
2561 bpf_error("'esp' modifier applied to host");
2562
2563 case Q_ISO:
2564 bpf_error("ISO host filtering not implemented");
2565
2566 case Q_ESIS:
2567 bpf_error("'esis' modifier applied to host");
2568
2569 case Q_ISIS:
2570 bpf_error("'isis' modifier applied to host");
2571
2572 case Q_CLNP:
2573 bpf_error("'clnp' modifier applied to host");
2574
2575 case Q_STP:
2576 bpf_error("'stp' modifier applied to host");
2577
2578 case Q_IPX:
2579 bpf_error("IPX host filtering not implemented");
2580
2581 case Q_NETBEUI:
2582 bpf_error("'netbeui' modifier applied to host");
2583
2584 default:
2585 abort();
2586 }
2587 /* NOTREACHED */
2588 }
2589
2590 #ifdef INET6
2591 static struct block *
2592 gen_host6(addr, mask, proto, dir)
2593 struct in6_addr *addr;
2594 struct in6_addr *mask;
2595 int proto;
2596 int dir;
2597 {
2598 switch (proto) {
2599
2600 case Q_DEFAULT:
2601 return gen_host6(addr, mask, Q_IPV6, dir);
2602
2603 case Q_IP:
2604 bpf_error("'ip' modifier applied to ip6 host");
2605
2606 case Q_RARP:
2607 bpf_error("'rarp' modifier applied to ip6 host");
2608
2609 case Q_ARP:
2610 bpf_error("'arp' modifier applied to ip6 host");
2611
2612 case Q_SCTP:
2613 bpf_error("'sctp' modifier applied to host");
2614
2615 case Q_TCP:
2616 bpf_error("'tcp' modifier applied to host");
2617
2618 case Q_UDP:
2619 bpf_error("'udp' modifier applied to host");
2620
2621 case Q_ICMP:
2622 bpf_error("'icmp' modifier applied to host");
2623
2624 case Q_IGMP:
2625 bpf_error("'igmp' modifier applied to host");
2626
2627 case Q_IGRP:
2628 bpf_error("'igrp' modifier applied to host");
2629
2630 case Q_PIM:
2631 bpf_error("'pim' modifier applied to host");
2632
2633 case Q_VRRP:
2634 bpf_error("'vrrp' modifier applied to host");
2635
2636 case Q_ATALK:
2637 bpf_error("ATALK host filtering not implemented");
2638
2639 case Q_AARP:
2640 bpf_error("AARP host filtering not implemented");
2641
2642 case Q_DECNET:
2643 bpf_error("'decnet' modifier applied to ip6 host");
2644
2645 case Q_SCA:
2646 bpf_error("SCA host filtering not implemented");
2647
2648 case Q_LAT:
2649 bpf_error("LAT host filtering not implemented");
2650
2651 case Q_MOPDL:
2652 bpf_error("MOPDL host filtering not implemented");
2653
2654 case Q_MOPRC:
2655 bpf_error("MOPRC host filtering not implemented");
2656
2657 case Q_IPV6:
2658 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6,
2659 off_nl + 8, off_nl + 24);
2660
2661 case Q_ICMPV6:
2662 bpf_error("'icmp6' modifier applied to host");
2663
2664 case Q_AH:
2665 bpf_error("'ah' modifier applied to host");
2666
2667 case Q_ESP:
2668 bpf_error("'esp' modifier applied to host");
2669
2670 case Q_ISO:
2671 bpf_error("ISO host filtering not implemented");
2672
2673 case Q_ESIS:
2674 bpf_error("'esis' modifier applied to host");
2675
2676 case Q_ISIS:
2677 bpf_error("'isis' modifier applied to host");
2678
2679 case Q_CLNP:
2680 bpf_error("'clnp' modifier applied to host");
2681
2682 case Q_STP:
2683 bpf_error("'stp' modifier applied to host");
2684
2685 case Q_IPX:
2686 bpf_error("IPX host filtering not implemented");
2687
2688 case Q_NETBEUI:
2689 bpf_error("'netbeui' modifier applied to host");
2690
2691 default:
2692 abort();
2693 }
2694 /* NOTREACHED */
2695 }
2696 #endif /*INET6*/
2697
2698 #ifndef INET6
2699 static struct block *
2700 gen_gateway(eaddr, alist, proto, dir)
2701 const u_char *eaddr;
2702 bpf_u_int32 **alist;
2703 int proto;
2704 int dir;
2705 {
2706 struct block *b0, *b1, *tmp;
2707
2708 if (dir != 0)
2709 bpf_error("direction applied to 'gateway'");
2710
2711 switch (proto) {
2712 case Q_DEFAULT:
2713 case Q_IP:
2714 case Q_ARP:
2715 case Q_RARP:
2716 if (linktype == DLT_EN10MB)
2717 b0 = gen_ehostop(eaddr, Q_OR);
2718 else if (linktype == DLT_FDDI)
2719 b0 = gen_fhostop(eaddr, Q_OR);
2720 else if (linktype == DLT_IEEE802)
2721 b0 = gen_thostop(eaddr, Q_OR);
2722 else if (linktype == DLT_IEEE802_11)
2723 b0 = gen_wlanhostop(eaddr, Q_OR);
2724 else if (linktype == DLT_SUNATM && is_lane) {
2725 /*
2726 * Check that the packet doesn't begin with an
2727 * LE Control marker. (We've already generated
2728 * a test for LANE.)
2729 */
2730 b1 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
2731 gen_not(b1);
2732
2733 /*
2734 * Now check the MAC address.
2735 */
2736 b0 = gen_ehostop(eaddr, Q_OR);
2737 gen_and(b1, b0);
2738 } else if (linktype == DLT_IP_OVER_FC)
2739 b0 = gen_ipfchostop(eaddr, Q_OR);
2740 else
2741 bpf_error(
2742 "'gateway' supported only on ethernet/FDDI/token ring/802.11/Fibre Channel");
2743
2744 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR);
2745 while (*alist) {
2746 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR);
2747 gen_or(b1, tmp);
2748 b1 = tmp;
2749 }
2750 gen_not(b1);
2751 gen_and(b0, b1);
2752 return b1;
2753 }
2754 bpf_error("illegal modifier of 'gateway'");
2755 /* NOTREACHED */
2756 }
2757 #endif
2758
2759 struct block *
2760 gen_proto_abbrev(proto)
2761 int proto;
2762 {
2763 struct block *b0;
2764 struct block *b1;
2765
2766 switch (proto) {
2767
2768 case Q_SCTP:
2769 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
2770 #ifdef INET6
2771 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
2772 gen_or(b0, b1);
2773 #endif
2774 break;
2775
2776 case Q_TCP:
2777 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
2778 #ifdef INET6
2779 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
2780 gen_or(b0, b1);
2781 #endif
2782 break;
2783
2784 case Q_UDP:
2785 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
2786 #ifdef INET6
2787 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
2788 gen_or(b0, b1);
2789 #endif
2790 break;
2791
2792 case Q_ICMP:
2793 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
2794 break;
2795
2796 #ifndef IPPROTO_IGMP
2797 #define IPPROTO_IGMP 2
2798 #endif
2799
2800 case Q_IGMP:
2801 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
2802 break;
2803
2804 #ifndef IPPROTO_IGRP
2805 #define IPPROTO_IGRP 9
2806 #endif
2807 case Q_IGRP:
2808 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
2809 break;
2810
2811 #ifndef IPPROTO_PIM
2812 #define IPPROTO_PIM 103
2813 #endif
2814
2815 case Q_PIM:
2816 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
2817 #ifdef INET6
2818 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
2819 gen_or(b0, b1);
2820 #endif
2821 break;
2822
2823 #ifndef IPPROTO_VRRP
2824 #define IPPROTO_VRRP 112
2825 #endif
2826
2827 case Q_VRRP:
2828 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
2829 break;
2830
2831 case Q_IP:
2832 b1 = gen_linktype(ETHERTYPE_IP);
2833 break;
2834
2835 case Q_ARP:
2836 b1 = gen_linktype(ETHERTYPE_ARP);
2837 break;
2838
2839 case Q_RARP:
2840 b1 = gen_linktype(ETHERTYPE_REVARP);
2841 break;
2842
2843 case Q_LINK:
2844 bpf_error("link layer applied in wrong context");
2845
2846 case Q_ATALK:
2847 b1 = gen_linktype(ETHERTYPE_ATALK);
2848 break;
2849
2850 case Q_AARP:
2851 b1 = gen_linktype(ETHERTYPE_AARP);
2852 break;
2853
2854 case Q_DECNET:
2855 b1 = gen_linktype(ETHERTYPE_DN);
2856 break;
2857
2858 case Q_SCA:
2859 b1 = gen_linktype(ETHERTYPE_SCA);
2860 break;
2861
2862 case Q_LAT:
2863 b1 = gen_linktype(ETHERTYPE_LAT);
2864 break;
2865
2866 case Q_MOPDL:
2867 b1 = gen_linktype(ETHERTYPE_MOPDL);
2868 break;
2869
2870 case Q_MOPRC:
2871 b1 = gen_linktype(ETHERTYPE_MOPRC);
2872 break;
2873
2874 #ifdef INET6
2875 case Q_IPV6:
2876 b1 = gen_linktype(ETHERTYPE_IPV6);
2877 break;
2878
2879 #ifndef IPPROTO_ICMPV6
2880 #define IPPROTO_ICMPV6 58
2881 #endif
2882 case Q_ICMPV6:
2883 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
2884 break;
2885 #endif /* INET6 */
2886
2887 #ifndef IPPROTO_AH
2888 #define IPPROTO_AH 51
2889 #endif
2890 case Q_AH:
2891 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
2892 #ifdef INET6
2893 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
2894 gen_or(b0, b1);
2895 #endif
2896 break;
2897
2898 #ifndef IPPROTO_ESP
2899 #define IPPROTO_ESP 50
2900 #endif
2901 case Q_ESP:
2902 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
2903 #ifdef INET6
2904 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
2905 gen_or(b0, b1);
2906 #endif
2907 break;
2908
2909 case Q_ISO:
2910 b1 = gen_linktype(LLCSAP_ISONS);
2911 break;
2912
2913 case Q_ESIS:
2914 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
2915 break;
2916
2917 case Q_ISIS:
2918 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
2919 break;
2920
2921 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
2922 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
2923 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
2924 gen_or(b0, b1);
2925 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
2926 gen_or(b0, b1);
2927 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
2928 gen_or(b0, b1);
2929 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
2930 gen_or(b0, b1);
2931 break;
2932
2933 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
2934 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
2935 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
2936 gen_or(b0, b1);
2937 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
2938 gen_or(b0, b1);
2939 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
2940 gen_or(b0, b1);
2941 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
2942 gen_or(b0, b1);
2943 break;
2944
2945 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
2946 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
2947 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
2948 gen_or(b0, b1);
2949 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
2950 gen_or(b0, b1);
2951 break;
2952
2953 case Q_ISIS_LSP:
2954 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
2955 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
2956 gen_or(b0, b1);
2957 break;
2958
2959 case Q_ISIS_SNP:
2960 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
2961 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
2962 gen_or(b0, b1);
2963 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
2964 gen_or(b0, b1);
2965 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
2966 gen_or(b0, b1);
2967 break;
2968
2969 case Q_ISIS_CSNP:
2970 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
2971 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
2972 gen_or(b0, b1);
2973 break;
2974
2975 case Q_ISIS_PSNP:
2976 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
2977 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
2978 gen_or(b0, b1);
2979 break;
2980
2981 case Q_CLNP:
2982 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
2983 break;
2984
2985 case Q_STP:
2986 b1 = gen_linktype(LLCSAP_8021D);
2987 break;
2988
2989 case Q_IPX:
2990 b1 = gen_linktype(LLCSAP_IPX);
2991 break;
2992
2993 case Q_NETBEUI:
2994 b1 = gen_linktype(LLCSAP_NETBEUI);
2995 break;
2996
2997 default:
2998 abort();
2999 }
3000 return b1;
3001 }
3002
3003 static struct block *
3004 gen_ipfrag()
3005 {
3006 struct slist *s;
3007 struct block *b;
3008
3009 /* not ip frag */
3010 s = new_stmt(BPF_LD|BPF_H|BPF_ABS);
3011 s->s.k = off_nl + 6;
3012 b = new_block(JMP(BPF_JSET));
3013 b->s.k = 0x1fff;
3014 b->stmts = s;
3015 gen_not(b);
3016
3017 return b;
3018 }
3019
3020 static struct block *
3021 gen_portatom(off, v)
3022 int off;
3023 bpf_int32 v;
3024 {
3025 struct slist *s;
3026 struct block *b;
3027
3028 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
3029 s->s.k = off_nl;
3030
3031 s->next = new_stmt(BPF_LD|BPF_IND|BPF_H);
3032 s->next->s.k = off_nl + off;
3033
3034 b = new_block(JMP(BPF_JEQ));
3035 b->stmts = s;
3036 b->s.k = v;
3037
3038 return b;
3039 }
3040
3041 #ifdef INET6
3042 static struct block *
3043 gen_portatom6(off, v)
3044 int off;
3045 bpf_int32 v;
3046 {
3047 return gen_cmp(off_nl + 40 + off, BPF_H, v);
3048 }
3049 #endif/*INET6*/
3050
3051 struct block *
3052 gen_portop(port, proto, dir)
3053 int port, proto, dir;
3054 {
3055 struct block *b0, *b1, *tmp;
3056
3057 /* ip proto 'proto' */
3058 tmp = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)proto);
3059 b0 = gen_ipfrag();
3060 gen_and(tmp, b0);
3061
3062 switch (dir) {
3063 case Q_SRC:
3064 b1 = gen_portatom(0, (bpf_int32)port);
3065 break;
3066
3067 case Q_DST:
3068 b1 = gen_portatom(2, (bpf_int32)port);
3069 break;
3070
3071 case Q_OR:
3072 case Q_DEFAULT:
3073 tmp = gen_portatom(0, (bpf_int32)port);
3074 b1 = gen_portatom(2, (bpf_int32)port);
3075 gen_or(tmp, b1);
3076 break;
3077
3078 case Q_AND:
3079 tmp = gen_portatom(0, (bpf_int32)port);
3080 b1 = gen_portatom(2, (bpf_int32)port);
3081 gen_and(tmp, b1);
3082 break;
3083
3084 default:
3085 abort();
3086 }
3087 gen_and(b0, b1);
3088
3089 return b1;
3090 }
3091
3092 static struct block *
3093 gen_port(port, ip_proto, dir)
3094 int port;
3095 int ip_proto;
3096 int dir;
3097 {
3098 struct block *b0, *b1, *tmp;
3099
3100 /*
3101 * ether proto ip
3102 *
3103 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3104 * not LLC encapsulation with LLCSAP_IP.
3105 *
3106 * For IEEE 802 networks - which includes 802.5 token ring
3107 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3108 * says that SNAP encapsulation is used, not LLC encapsulation
3109 * with LLCSAP_IP.
3110 *
3111 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3112 * RFC 2225 say that SNAP encapsulation is used, not LLC
3113 * encapsulation with LLCSAP_IP.
3114 *
3115 * So we always check for ETHERTYPE_IP.
3116 */
3117 b0 = gen_linktype(ETHERTYPE_IP);
3118
3119 switch (ip_proto) {
3120 case IPPROTO_UDP:
3121 case IPPROTO_TCP:
3122 case IPPROTO_SCTP:
3123 b1 = gen_portop(port, ip_proto, dir);
3124 break;
3125
3126 case PROTO_UNDEF:
3127 tmp = gen_portop(port, IPPROTO_TCP, dir);
3128 b1 = gen_portop(port, IPPROTO_UDP, dir);
3129 gen_or(tmp, b1);
3130 tmp = gen_portop(port, IPPROTO_SCTP, dir);
3131 gen_or(tmp, b1);
3132 break;
3133
3134 default:
3135 abort();
3136 }
3137 gen_and(b0, b1);
3138 return b1;
3139 }
3140
3141 #ifdef INET6
3142 struct block *
3143 gen_portop6(port, proto, dir)
3144 int port, proto, dir;
3145 {
3146 struct block *b0, *b1, *tmp;
3147
3148 /* ip proto 'proto' */
3149 b0 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)proto);
3150
3151 switch (dir) {
3152 case Q_SRC:
3153 b1 = gen_portatom6(0, (bpf_int32)port);
3154 break;
3155
3156 case Q_DST:
3157 b1 = gen_portatom6(2, (bpf_int32)port);
3158 break;
3159
3160 case Q_OR:
3161 case Q_DEFAULT:
3162 tmp = gen_portatom6(0, (bpf_int32)port);
3163 b1 = gen_portatom6(2, (bpf_int32)port);
3164 gen_or(tmp, b1);
3165 break;
3166
3167 case Q_AND:
3168 tmp = gen_portatom6(0, (bpf_int32)port);
3169 b1 = gen_portatom6(2, (bpf_int32)port);
3170 gen_and(tmp, b1);
3171 break;
3172
3173 default:
3174 abort();
3175 }
3176 gen_and(b0, b1);
3177
3178 return b1;
3179 }
3180
3181 static struct block *
3182 gen_port6(port, ip_proto, dir)
3183 int port;
3184 int ip_proto;
3185 int dir;
3186 {
3187 struct block *b0, *b1, *tmp;
3188
3189 /* ether proto ip */
3190 b0 = gen_linktype(ETHERTYPE_IPV6);
3191
3192 switch (ip_proto) {
3193 case IPPROTO_UDP:
3194 case IPPROTO_TCP:
3195 case IPPROTO_SCTP:
3196 b1 = gen_portop6(port, ip_proto, dir);
3197 break;
3198
3199 case PROTO_UNDEF:
3200 tmp = gen_portop6(port, IPPROTO_TCP, dir);
3201 b1 = gen_portop6(port, IPPROTO_UDP, dir);
3202 gen_or(tmp, b1);
3203 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
3204 gen_or(tmp, b1);
3205 break;
3206
3207 default:
3208 abort();
3209 }
3210 gen_and(b0, b1);
3211 return b1;
3212 }
3213 #endif /* INET6 */
3214
3215 static int
3216 lookup_proto(name, proto)
3217 register const char *name;
3218 register int proto;
3219 {
3220 register int v;
3221
3222 switch (proto) {
3223
3224 case Q_DEFAULT:
3225 case Q_IP:
3226 case Q_IPV6:
3227 v = pcap_nametoproto(name);
3228 if (v == PROTO_UNDEF)
3229 bpf_error("unknown ip proto '%s'", name);
3230 break;
3231
3232 case Q_LINK:
3233 /* XXX should look up h/w protocol type based on linktype */
3234 v = pcap_nametoeproto(name);
3235 if (v == PROTO_UNDEF)
3236 bpf_error("unknown ether proto '%s'", name);
3237 break;
3238
3239 case Q_ISO:
3240 if (strcmp(name, "esis") == 0)
3241 v = ISO9542_ESIS;
3242 else if (strcmp(name, "isis") == 0)
3243 v = ISO10589_ISIS;
3244 else if (strcmp(name, "clnp") == 0)
3245 v = ISO8473_CLNP;
3246 else
3247 bpf_error("unknown osi proto '%s'", name);
3248 break;
3249
3250 default:
3251 v = PROTO_UNDEF;
3252 break;
3253 }
3254 return v;
3255 }
3256
3257 #if 0
3258 struct stmt *
3259 gen_joinsp(s, n)
3260 struct stmt **s;
3261 int n;
3262 {
3263 return NULL;
3264 }
3265 #endif
3266
3267 static struct block *
3268 gen_protochain(v, proto, dir)
3269 int v;
3270 int proto;
3271 int dir;
3272 {
3273 #ifdef NO_PROTOCHAIN
3274 return gen_proto(v, proto, dir);
3275 #else
3276 struct block *b0, *b;
3277 struct slist *s[100];
3278 int fix2, fix3, fix4, fix5;
3279 int ahcheck, again, end;
3280 int i, max;
3281 int reg2 = alloc_reg();
3282
3283 memset(s, 0, sizeof(s));
3284 fix2 = fix3 = fix4 = fix5 = 0;
3285
3286 switch (proto) {
3287 case Q_IP:
3288 case Q_IPV6:
3289 break;
3290 case Q_DEFAULT:
3291 b0 = gen_protochain(v, Q_IP, dir);
3292 b = gen_protochain(v, Q_IPV6, dir);
3293 gen_or(b0, b);
3294 return b;
3295 default:
3296 bpf_error("bad protocol applied for 'protochain'");
3297 /*NOTREACHED*/
3298 }
3299
3300 no_optimize = 1; /*this code is not compatible with optimzer yet */
3301
3302 /*
3303 * s[0] is a dummy entry to protect other BPF insn from damaged
3304 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
3305 * hard to find interdependency made by jump table fixup.
3306 */
3307 i = 0;
3308 s[i] = new_stmt(0); /*dummy*/
3309 i++;
3310
3311 switch (proto) {
3312 case Q_IP:
3313 b0 = gen_linktype(ETHERTYPE_IP);
3314
3315 /* A = ip->ip_p */
3316 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
3317 s[i]->s.k = off_nl + 9;
3318 i++;
3319 /* X = ip->ip_hl << 2 */
3320 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
3321 s[i]->s.k = off_nl;
3322 i++;
3323 break;
3324 #ifdef INET6
3325 case Q_IPV6:
3326 b0 = gen_linktype(ETHERTYPE_IPV6);
3327
3328 /* A = ip6->ip_nxt */
3329 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
3330 s[i]->s.k = off_nl + 6;
3331 i++;
3332 /* X = sizeof(struct ip6_hdr) */
3333 s[i] = new_stmt(BPF_LDX|BPF_IMM);
3334 s[i]->s.k = 40;
3335 i++;
3336 break;
3337 #endif
3338 default:
3339 bpf_error("unsupported proto to gen_protochain");
3340 /*NOTREACHED*/
3341 }
3342
3343 /* again: if (A == v) goto end; else fall through; */
3344 again = i;
3345 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3346 s[i]->s.k = v;
3347 s[i]->s.jt = NULL; /*later*/
3348 s[i]->s.jf = NULL; /*update in next stmt*/
3349 fix5 = i;
3350 i++;
3351
3352 #ifndef IPPROTO_NONE
3353 #define IPPROTO_NONE 59
3354 #endif
3355 /* if (A == IPPROTO_NONE) goto end */
3356 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3357 s[i]->s.jt = NULL; /*later*/
3358 s[i]->s.jf = NULL; /*update in next stmt*/
3359 s[i]->s.k = IPPROTO_NONE;
3360 s[fix5]->s.jf = s[i];
3361 fix2 = i;
3362 i++;
3363
3364 #ifdef INET6
3365 if (proto == Q_IPV6) {
3366 int v6start, v6end, v6advance, j;
3367
3368 v6start = i;
3369 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
3370 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3371 s[i]->s.jt = NULL; /*later*/
3372 s[i]->s.jf = NULL; /*update in next stmt*/
3373 s[i]->s.k = IPPROTO_HOPOPTS;
3374 s[fix2]->s.jf = s[i];
3375 i++;
3376 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
3377 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3378 s[i]->s.jt = NULL; /*later*/
3379 s[i]->s.jf = NULL; /*update in next stmt*/
3380 s[i]->s.k = IPPROTO_DSTOPTS;
3381 i++;
3382 /* if (A == IPPROTO_ROUTING) goto v6advance */
3383 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3384 s[i]->s.jt = NULL; /*later*/
3385 s[i]->s.jf = NULL; /*update in next stmt*/
3386 s[i]->s.k = IPPROTO_ROUTING;
3387 i++;
3388 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
3389 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3390 s[i]->s.jt = NULL; /*later*/
3391 s[i]->s.jf = NULL; /*later*/
3392 s[i]->s.k = IPPROTO_FRAGMENT;
3393 fix3 = i;
3394 v6end = i;
3395 i++;
3396
3397 /* v6advance: */
3398 v6advance = i;
3399
3400 /*
3401 * in short,
3402 * A = P[X];
3403 * X = X + (P[X + 1] + 1) * 8;
3404 */
3405 /* A = X */
3406 s[i] = new_stmt(BPF_MISC|BPF_TXA);
3407 i++;
3408 /* A = P[X + packet head] */
3409 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3410 s[i]->s.k = off_nl;
3411 i++;
3412 /* MEM[reg2] = A */
3413 s[i] = new_stmt(BPF_ST);
3414 s[i]->s.k = reg2;
3415 i++;
3416 /* A = X */
3417 s[i] = new_stmt(BPF_MISC|BPF_TXA);
3418 i++;
3419 /* A += 1 */
3420 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3421 s[i]->s.k = 1;
3422 i++;
3423 /* X = A */
3424 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3425 i++;
3426 /* A = P[X + packet head]; */
3427 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3428 s[i]->s.k = off_nl;
3429 i++;
3430 /* A += 1 */
3431 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3432 s[i]->s.k = 1;
3433 i++;
3434 /* A *= 8 */
3435 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
3436 s[i]->s.k = 8;
3437 i++;
3438 /* X = A; */
3439 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3440 i++;
3441 /* A = MEM[reg2] */
3442 s[i] = new_stmt(BPF_LD|BPF_MEM);
3443 s[i]->s.k = reg2;
3444 i++;
3445
3446 /* goto again; (must use BPF_JA for backward jump) */
3447 s[i] = new_stmt(BPF_JMP|BPF_JA);
3448 s[i]->s.k = again - i - 1;
3449 s[i - 1]->s.jf = s[i];
3450 i++;
3451
3452 /* fixup */
3453 for (j = v6start; j <= v6end; j++)
3454 s[j]->s.jt = s[v6advance];
3455 } else
3456 #endif
3457 {
3458 /* nop */
3459 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3460 s[i]->s.k = 0;
3461 s[fix2]->s.jf = s[i];
3462 i++;
3463 }
3464
3465 /* ahcheck: */
3466 ahcheck = i;
3467 /* if (A == IPPROTO_AH) then fall through; else goto end; */
3468 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
3469 s[i]->s.jt = NULL; /*later*/
3470 s[i]->s.jf = NULL; /*later*/
3471 s[i]->s.k = IPPROTO_AH;
3472 if (fix3)
3473 s[fix3]->s.jf = s[ahcheck];
3474 fix4 = i;
3475 i++;
3476
3477 /*
3478 * in short,
3479 * A = P[X];
3480 * X = X + (P[X + 1] + 2) * 4;
3481 */
3482 /* A = X */
3483 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
3484 i++;
3485 /* A = P[X + packet head]; */
3486 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3487 s[i]->s.k = off_nl;
3488 i++;
3489 /* MEM[reg2] = A */
3490 s[i] = new_stmt(BPF_ST);
3491 s[i]->s.k = reg2;
3492 i++;
3493 /* A = X */
3494 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
3495 i++;
3496 /* A += 1 */
3497 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3498 s[i]->s.k = 1;
3499 i++;
3500 /* X = A */
3501 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3502 i++;
3503 /* A = P[X + packet head] */
3504 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
3505 s[i]->s.k = off_nl;
3506 i++;
3507 /* A += 2 */
3508 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3509 s[i]->s.k = 2;
3510 i++;
3511 /* A *= 4 */
3512 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
3513 s[i]->s.k = 4;
3514 i++;
3515 /* X = A; */
3516 s[i] = new_stmt(BPF_MISC|BPF_TAX);
3517 i++;
3518 /* A = MEM[reg2] */
3519 s[i] = new_stmt(BPF_LD|BPF_MEM);
3520 s[i]->s.k = reg2;
3521 i++;
3522
3523 /* goto again; (must use BPF_JA for backward jump) */
3524 s[i] = new_stmt(BPF_JMP|BPF_JA);
3525 s[i]->s.k = again - i - 1;
3526 i++;
3527
3528 /* end: nop */
3529 end = i;
3530 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
3531 s[i]->s.k = 0;
3532 s[fix2]->s.jt = s[end];
3533 s[fix4]->s.jf = s[end];
3534 s[fix5]->s.jt = s[end];
3535 i++;
3536
3537 /*
3538 * make slist chain
3539 */
3540 max = i;
3541 for (i = 0; i < max - 1; i++)
3542 s[i]->next = s[i + 1];
3543 s[max - 1]->next = NULL;
3544
3545 /*
3546 * emit final check
3547 */
3548 b = new_block(JMP(BPF_JEQ));
3549 b->stmts = s[1]; /*remember, s[0] is dummy*/
3550 b->s.k = v;
3551
3552 free_reg(reg2);
3553
3554 gen_and(b0, b);
3555 return b;
3556 #endif
3557 }
3558
3559 static struct block *
3560 gen_proto(v, proto, dir)
3561 int v;
3562 int proto;
3563 int dir;
3564 {
3565 struct block *b0, *b1;
3566
3567 if (dir != Q_DEFAULT)
3568 bpf_error("direction applied to 'proto'");
3569
3570 switch (proto) {
3571 case Q_DEFAULT:
3572 #ifdef INET6
3573 b0 = gen_proto(v, Q_IP, dir);
3574 b1 = gen_proto(v, Q_IPV6, dir);
3575 gen_or(b0, b1);
3576 return b1;
3577 #else
3578 /*FALLTHROUGH*/
3579 #endif
3580 case Q_IP:
3581 /*
3582 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
3583 * not LLC encapsulation with LLCSAP_IP.
3584 *
3585 * For IEEE 802 networks - which includes 802.5 token ring
3586 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
3587 * says that SNAP encapsulation is used, not LLC encapsulation
3588 * with LLCSAP_IP.
3589 *
3590 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
3591 * RFC 2225 say that SNAP encapsulation is used, not LLC
3592 * encapsulation with LLCSAP_IP.
3593 *
3594 * So we always check for ETHERTYPE_IP.
3595 */
3596 b0 = gen_linktype(ETHERTYPE_IP);
3597 #ifndef CHASE_CHAIN
3598 b1 = gen_cmp(off_nl + 9, BPF_B, (bpf_int32)v);
3599 #else
3600 b1 = gen_protochain(v, Q_IP);
3601 #endif
3602 gen_and(b0, b1);
3603 return b1;
3604
3605 case Q_ISO:
3606 switch (linktype) {
3607
3608 case DLT_FRELAY:
3609 /*
3610 * Frame Relay packets typically have an OSI
3611 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
3612 * generates code to check for all the OSI
3613 * NLPIDs, so calling it and then adding a check
3614 * for the particular NLPID for which we're
3615 * looking is bogus, as we can just check for
3616 * the NLPID.
3617 *
3618 * What we check for is the NLPID and a frame
3619 * control field value of UI, i.e. 0x03 followed
3620 * by the NLPID.
3621 *
3622 * XXX - assumes a 2-byte Frame Relay header with
3623 * DLCI and flags. What if the address is longer?
3624 *
3625 * XXX - what about SNAP-encapsulated frames?
3626 */
3627 return gen_cmp(2, BPF_H, (0x03<<8) | v);
3628 break;
3629
3630 case DLT_C_HDLC:
3631 /*
3632 * Cisco uses an Ethertype lookalike - for OSI,
3633 * it's 0xfefe.
3634 */
3635 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
3636 /* OSI in C-HDLC is stuffed with a fudge byte */
3637 b1 = gen_cmp(off_nl_nosnap+1, BPF_B, (long)v);
3638 gen_and(b0, b1);
3639 return b1;
3640
3641 default:
3642 b0 = gen_linktype(LLCSAP_ISONS);
3643 b1 = gen_cmp(off_nl_nosnap, BPF_B, (long)v);
3644 gen_and(b0, b1);
3645 return b1;
3646 }
3647
3648 case Q_ISIS:
3649 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
3650 /*
3651 * 4 is the offset of the PDU type relative to the IS-IS
3652 * header.
3653 */
3654 b1 = gen_cmp(off_nl_nosnap+4, BPF_B, (long)v);
3655 gen_and(b0, b1);
3656 return b1;
3657
3658 case Q_ARP:
3659 bpf_error("arp does not encapsulate another protocol");
3660 /* NOTREACHED */
3661
3662 case Q_RARP:
3663 bpf_error("rarp does not encapsulate another protocol");
3664 /* NOTREACHED */
3665
3666 case Q_ATALK:
3667 bpf_error("atalk encapsulation is not specifiable");
3668 /* NOTREACHED */
3669
3670 case Q_DECNET:
3671 bpf_error("decnet encapsulation is not specifiable");
3672 /* NOTREACHED */
3673
3674 case Q_SCA:
3675 bpf_error("sca does not encapsulate another protocol");
3676 /* NOTREACHED */
3677
3678 case Q_LAT:
3679 bpf_error("lat does not encapsulate another protocol");
3680 /* NOTREACHED */
3681
3682 case Q_MOPRC:
3683 bpf_error("moprc does not encapsulate another protocol");
3684 /* NOTREACHED */
3685
3686 case Q_MOPDL:
3687 bpf_error("mopdl does not encapsulate another protocol");
3688 /* NOTREACHED */
3689
3690 case Q_LINK:
3691 return gen_linktype(v);
3692
3693 case Q_UDP:
3694 bpf_error("'udp proto' is bogus");
3695 /* NOTREACHED */
3696
3697 case Q_TCP:
3698 bpf_error("'tcp proto' is bogus");
3699 /* NOTREACHED */
3700
3701 case Q_SCTP:
3702 bpf_error("'sctp proto' is bogus");
3703 /* NOTREACHED */
3704
3705 case Q_ICMP:
3706 bpf_error("'icmp proto' is bogus");
3707 /* NOTREACHED */
3708
3709 case Q_IGMP:
3710 bpf_error("'igmp proto' is bogus");
3711 /* NOTREACHED */
3712
3713 case Q_IGRP:
3714 bpf_error("'igrp proto' is bogus");
3715 /* NOTREACHED */
3716
3717 case Q_PIM:
3718 bpf_error("'pim proto' is bogus");
3719 /* NOTREACHED */
3720
3721 case Q_VRRP:
3722 bpf_error("'vrrp proto' is bogus");
3723 /* NOTREACHED */
3724
3725 #ifdef INET6
3726 case Q_IPV6:
3727 b0 = gen_linktype(ETHERTYPE_IPV6);
3728 #ifndef CHASE_CHAIN
3729 b1 = gen_cmp(off_nl + 6, BPF_B, (bpf_int32)v);
3730 #else
3731 b1 = gen_protochain(v, Q_IPV6);
3732 #endif
3733 gen_and(b0, b1);
3734 return b1;
3735
3736 case Q_ICMPV6:
3737 bpf_error("'icmp6 proto' is bogus");
3738 #endif /* INET6 */
3739
3740 case Q_AH:
3741 bpf_error("'ah proto' is bogus");
3742
3743 case Q_ESP:
3744 bpf_error("'ah proto' is bogus");
3745
3746 case Q_STP:
3747 bpf_error("'stp proto' is bogus");
3748
3749 case Q_IPX:
3750 bpf_error("'ipx proto' is bogus");
3751
3752 case Q_NETBEUI:
3753 bpf_error("'netbeui proto' is bogus");
3754
3755 default:
3756 abort();
3757 /* NOTREACHED */
3758 }
3759 /* NOTREACHED */
3760 }
3761
3762 struct block *
3763 gen_scode(name, q)
3764 register const char *name;
3765 struct qual q;
3766 {
3767 int proto = q.proto;
3768 int dir = q.dir;
3769 int tproto;
3770 u_char *eaddr;
3771 bpf_u_int32 mask, addr;
3772 #ifndef INET6
3773 bpf_u_int32 **alist;
3774 #else
3775 int tproto6;
3776 struct sockaddr_in *sin;
3777 struct sockaddr_in6 *sin6;
3778 struct addrinfo *res, *res0;
3779 struct in6_addr mask128;
3780 #endif /*INET6*/
3781 struct block *b, *tmp;
3782 int port, real_proto;
3783
3784 switch (q.addr) {
3785
3786 case Q_NET:
3787 addr = pcap_nametonetaddr(name);
3788 if (addr == 0)
3789 bpf_error("unknown network '%s'", name);
3790 /* Left justify network addr and calculate its network mask */
3791 mask = 0xffffffff;
3792 while (addr && (addr & 0xff000000) == 0) {
3793 addr <<= 8;
3794 mask <<= 8;
3795 }
3796 return gen_host(addr, mask, proto, dir);
3797
3798 case Q_DEFAULT:
3799 case Q_HOST:
3800 if (proto == Q_LINK) {
3801 switch (linktype) {
3802
3803 case DLT_EN10MB:
3804 eaddr = pcap_ether_hostton(name);
3805 if (eaddr == NULL)
3806 bpf_error(
3807 "unknown ether host '%s'", name);
3808 b = gen_ehostop(eaddr, dir);
3809 free(eaddr);
3810 return b;
3811
3812 case DLT_FDDI:
3813 eaddr = pcap_ether_hostton(name);
3814 if (eaddr == NULL)
3815 bpf_error(
3816 "unknown FDDI host '%s'", name);
3817 b = gen_fhostop(eaddr, dir);
3818 free(eaddr);
3819 return b;
3820
3821 case DLT_IEEE802:
3822 eaddr = pcap_ether_hostton(name);
3823 if (eaddr == NULL)
3824 bpf_error(
3825 "unknown token ring host '%s'", name);
3826 b = gen_thostop(eaddr, dir);
3827 free(eaddr);
3828 return b;
3829
3830 case DLT_IEEE802_11:
3831 eaddr = pcap_ether_hostton(name);
3832 if (eaddr == NULL)
3833 bpf_error(
3834 "unknown 802.11 host '%s'", name);
3835 b = gen_wlanhostop(eaddr, dir);
3836 free(eaddr);
3837 return b;
3838
3839 case DLT_IP_OVER_FC:
3840 eaddr = pcap_ether_hostton(name);
3841 if (eaddr == NULL)
3842 bpf_error(
3843 "unknown Fibre Channel host '%s'", name);
3844 b = gen_ipfchostop(eaddr, dir);
3845 free(eaddr);
3846 return b;
3847
3848 case DLT_SUNATM:
3849 if (!is_lane)
3850 break;
3851
3852 /*
3853 * Check that the packet doesn't begin
3854 * with an LE Control marker. (We've
3855 * already generated a test for LANE.)
3856 */
3857 tmp = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H,
3858 0xFF00);
3859 gen_not(tmp);
3860
3861 eaddr = pcap_ether_hostton(name);
3862 if (eaddr == NULL)
3863 bpf_error(
3864 "unknown ether host '%s'", name);
3865 b = gen_ehostop(eaddr, dir);
3866 gen_and(tmp, b);
3867 free(eaddr);
3868 return b;
3869 }
3870
3871 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
3872 } else if (proto == Q_DECNET) {
3873 unsigned short dn_addr = __pcap_nametodnaddr(name);
3874 /*
3875 * I don't think DECNET hosts can be multihomed, so
3876 * there is no need to build up a list of addresses
3877 */
3878 return (gen_host(dn_addr, 0, proto, dir));
3879 } else {
3880 #ifndef INET6
3881 alist = pcap_nametoaddr(name);
3882 if (alist == NULL || *alist == NULL)
3883 bpf_error("unknown host '%s'", name);
3884 tproto = proto;
3885 if (off_linktype == -1 && tproto == Q_DEFAULT)
3886 tproto = Q_IP;
3887 b = gen_host(**alist++, 0xffffffff, tproto, dir);
3888 while (*alist) {
3889 tmp = gen_host(**alist++, 0xffffffff,
3890 tproto, dir);
3891 gen_or(b, tmp);
3892 b = tmp;
3893 }
3894 return b;
3895 #else
3896 memset(&mask128, 0xff, sizeof(mask128));
3897 res0 = res = pcap_nametoaddrinfo(name);
3898 if (res == NULL)
3899 bpf_error("unknown host '%s'", name);
3900 b = tmp = NULL;
3901 tproto = tproto6 = proto;
3902 if (off_linktype == -1 && tproto == Q_DEFAULT) {
3903 tproto = Q_IP;
3904 tproto6 = Q_IPV6;
3905 }
3906 for (res = res0; res; res = res->ai_next) {
3907 switch (res->ai_family) {
3908 case AF_INET:
3909 if (tproto == Q_IPV6)
3910 continue;
3911
3912 sin = (struct sockaddr_in *)
3913 res->ai_addr;
3914 tmp = gen_host(ntohl(sin->sin_addr.s_addr),
3915 0xffffffff, tproto, dir);
3916 break;
3917 case AF_INET6:
3918 if (tproto6 == Q_IP)
3919 continue;
3920
3921 sin6 = (struct sockaddr_in6 *)
3922 res->ai_addr;
3923 tmp = gen_host6(&sin6->sin6_addr,
3924 &mask128, tproto6, dir);
3925 break;
3926 default:
3927 continue;
3928 }
3929 if (b)
3930 gen_or(b, tmp);
3931 b = tmp;
3932 }
3933 freeaddrinfo(res0);
3934 if (b == NULL) {
3935 bpf_error("unknown host '%s'%s", name,
3936 (proto == Q_DEFAULT)
3937 ? ""
3938 : " for specified address family");
3939 }
3940 return b;
3941 #endif /*INET6*/
3942 }
3943
3944 case Q_PORT:
3945 if (proto != Q_DEFAULT &&
3946 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
3947 bpf_error("illegal qualifier of 'port'");
3948 if (pcap_nametoport(name, &port, &real_proto) == 0)
3949 bpf_error("unknown port '%s'", name);
3950 if (proto == Q_UDP) {
3951 if (real_proto == IPPROTO_TCP)
3952 bpf_error("port '%s' is tcp", name);
3953 else if (real_proto == IPPROTO_SCTP)
3954 bpf_error("port '%s' is sctp", name);
3955 else
3956 /* override PROTO_UNDEF */
3957 real_proto = IPPROTO_UDP;
3958 }
3959 if (proto == Q_TCP) {
3960 if (real_proto == IPPROTO_UDP)
3961 bpf_error("port '%s' is udp", name);
3962
3963 else if (real_proto == IPPROTO_SCTP)
3964 bpf_error("port '%s' is sctp", name);
3965 else
3966 /* override PROTO_UNDEF */
3967 real_proto = IPPROTO_TCP;
3968 }
3969 if (proto == Q_SCTP) {
3970 if (real_proto == IPPROTO_UDP)
3971 bpf_error("port '%s' is udp", name);
3972
3973 else if (real_proto == IPPROTO_TCP)
3974 bpf_error("port '%s' is tcp", name);
3975 else
3976 /* override PROTO_UNDEF */
3977 real_proto = IPPROTO_SCTP;
3978 }
3979 #ifndef INET6
3980 return gen_port(port, real_proto, dir);
3981 #else
3982 {
3983 struct block *b;
3984 b = gen_port(port, real_proto, dir);
3985 gen_or(gen_port6(port, real_proto, dir), b);
3986 return b;
3987 }
3988 #endif /* INET6 */
3989
3990 case Q_GATEWAY:
3991 #ifndef INET6
3992 eaddr = pcap_ether_hostton(name);
3993 if (eaddr == NULL)
3994 bpf_error("unknown ether host: %s", name);
3995
3996 alist = pcap_nametoaddr(name);
3997 if (alist == NULL || *alist == NULL)
3998 bpf_error("unknown host '%s'", name);
3999 b = gen_gateway(eaddr, alist, proto, dir);
4000 free(eaddr);
4001 return b;
4002 #else
4003 bpf_error("'gateway' not supported in this configuration");
4004 #endif /*INET6*/
4005
4006 case Q_PROTO:
4007 real_proto = lookup_proto(name, proto);
4008 if (real_proto >= 0)
4009 return gen_proto(real_proto, proto, dir);
4010 else
4011 bpf_error("unknown protocol: %s", name);
4012
4013 case Q_PROTOCHAIN:
4014 real_proto = lookup_proto(name, proto);
4015 if (real_proto >= 0)
4016 return gen_protochain(real_proto, proto, dir);
4017 else
4018 bpf_error("unknown protocol: %s", name);
4019
4020
4021 case Q_UNDEF:
4022 syntax();
4023 /* NOTREACHED */
4024 }
4025 abort();
4026 /* NOTREACHED */
4027 }
4028
4029 struct block *
4030 gen_mcode(s1, s2, masklen, q)
4031 register const char *s1, *s2;
4032 register int masklen;
4033 struct qual q;
4034 {
4035 register int nlen, mlen;
4036 bpf_u_int32 n, m;
4037
4038 nlen = __pcap_atoin(s1, &n);
4039 /* Promote short ipaddr */
4040 n <<= 32 - nlen;
4041
4042 if (s2 != NULL) {
4043 mlen = __pcap_atoin(s2, &m);
4044 /* Promote short ipaddr */
4045 m <<= 32 - mlen;
4046 if ((n & ~m) != 0)
4047 bpf_error("non-network bits set in \"%s mask %s\"",
4048 s1, s2);
4049 } else {
4050 /* Convert mask len to mask */
4051 if (masklen > 32)
4052 bpf_error("mask length must be <= 32");
4053 m = 0xffffffff << (32 - masklen);
4054 if ((n & ~m) != 0)
4055 bpf_error("non-network bits set in \"%s/%d\"",
4056 s1, masklen);
4057 }
4058
4059 switch (q.addr) {
4060
4061 case Q_NET:
4062 return gen_host(n, m, q.proto, q.dir);
4063
4064 default:
4065 bpf_error("Mask syntax for networks only");
4066 /* NOTREACHED */
4067 }
4068 }
4069
4070 struct block *
4071 gen_ncode(s, v, q)
4072 register const char *s;
4073 bpf_u_int32 v;
4074 struct qual q;
4075 {
4076 bpf_u_int32 mask;
4077 int proto = q.proto;
4078 int dir = q.dir;
4079 register int vlen;
4080
4081 if (s == NULL)
4082 vlen = 32;
4083 else if (q.proto == Q_DECNET)
4084 vlen = __pcap_atodn(s, &v);
4085 else
4086 vlen = __pcap_atoin(s, &v);
4087
4088 switch (q.addr) {
4089
4090 case Q_DEFAULT:
4091 case Q_HOST:
4092 case Q_NET:
4093 if (proto == Q_DECNET)
4094 return gen_host(v, 0, proto, dir);
4095 else if (proto == Q_LINK) {
4096 bpf_error("illegal link layer address");
4097 } else {
4098 mask = 0xffffffff;
4099 if (s == NULL && q.addr == Q_NET) {
4100 /* Promote short net number */
4101 while (v && (v & 0xff000000) == 0) {
4102 v <<= 8;
4103 mask <<= 8;
4104 }
4105 } else {
4106 /* Promote short ipaddr */
4107 v <<= 32 - vlen;
4108 mask <<= 32 - vlen;
4109 }
4110 return gen_host(v, mask, proto, dir);
4111 }
4112
4113 case Q_PORT:
4114 if (proto == Q_UDP)
4115 proto = IPPROTO_UDP;
4116 else if (proto == Q_TCP)
4117 proto = IPPROTO_TCP;
4118 else if (proto == Q_SCTP)
4119 proto = IPPROTO_SCTP;
4120 else if (proto == Q_DEFAULT)
4121 proto = PROTO_UNDEF;
4122 else
4123 bpf_error("illegal qualifier of 'port'");
4124
4125 #ifndef INET6
4126 return gen_port((int)v, proto, dir);
4127 #else
4128 {
4129 struct block *b;
4130 b = gen_port((int)v, proto, dir);
4131 gen_or(gen_port6((int)v, proto, dir), b);
4132 return b;
4133 }
4134 #endif /* INET6 */
4135
4136 case Q_GATEWAY:
4137 bpf_error("'gateway' requires a name");
4138 /* NOTREACHED */
4139
4140 case Q_PROTO:
4141 return gen_proto((int)v, proto, dir);
4142
4143 case Q_PROTOCHAIN:
4144 return gen_protochain((int)v, proto, dir);
4145
4146 case Q_UNDEF:
4147 syntax();
4148 /* NOTREACHED */
4149
4150 default:
4151 abort();
4152 /* NOTREACHED */
4153 }
4154 /* NOTREACHED */
4155 }
4156
4157 #ifdef INET6
4158 struct block *
4159 gen_mcode6(s1, s2, masklen, q)
4160 register const char *s1, *s2;
4161 register int masklen;
4162 struct qual q;
4163 {
4164 struct addrinfo *res;
4165 struct in6_addr *addr;
4166 struct in6_addr mask;
4167 struct block *b;
4168 u_int32_t *a, *m;
4169
4170 if (s2)
4171 bpf_error("no mask %s supported", s2);
4172
4173 res = pcap_nametoaddrinfo(s1);
4174 if (!res)
4175 bpf_error("invalid ip6 address %s", s1);
4176 if (res->ai_next)
4177 bpf_error("%s resolved to multiple address", s1);
4178 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
4179
4180 if (sizeof(mask) * 8 < masklen)
4181 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
4182 memset(&mask, 0, sizeof(mask));
4183 memset(&mask, 0xff, masklen / 8);
4184 if (masklen % 8) {
4185 mask.s6_addr[masklen / 8] =
4186 (0xff << (8 - masklen % 8)) & 0xff;
4187 }
4188
4189 a = (u_int32_t *)addr;
4190 m = (u_int32_t *)&mask;
4191 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
4192 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
4193 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
4194 }
4195
4196 switch (q.addr) {
4197
4198 case Q_DEFAULT:
4199 case Q_HOST:
4200 if (masklen != 128)
4201 bpf_error("Mask syntax for networks only");
4202 /* FALLTHROUGH */
4203
4204 case Q_NET:
4205 b = gen_host6(addr, &mask, q.proto, q.dir);
4206 freeaddrinfo(res);
4207 return b;
4208
4209 default:
4210 bpf_error("invalid qualifier against IPv6 address");
4211 /* NOTREACHED */
4212 }
4213 }
4214 #endif /*INET6*/
4215
4216 struct block *
4217 gen_ecode(eaddr, q)
4218 register const u_char *eaddr;
4219 struct qual q;
4220 {
4221 struct block *b, *tmp;
4222
4223 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
4224 if (linktype == DLT_EN10MB)
4225 return gen_ehostop(eaddr, (int)q.dir);
4226 if (linktype == DLT_FDDI)
4227 return gen_fhostop(eaddr, (int)q.dir);
4228 if (linktype == DLT_IEEE802)
4229 return gen_thostop(eaddr, (int)q.dir);
4230 if (linktype == DLT_IEEE802_11)
4231 return gen_wlanhostop(eaddr, (int)q.dir);
4232 if (linktype == DLT_SUNATM && is_lane) {
4233 /*
4234 * Check that the packet doesn't begin with an
4235 * LE Control marker. (We've already generated
4236 * a test for LANE.)
4237 */
4238 tmp = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
4239 gen_not(tmp);
4240
4241 /*
4242 * Now check the MAC address.
4243 */
4244 b = gen_ehostop(eaddr, (int)q.dir);
4245 gen_and(tmp, b);
4246 return b;
4247 }
4248 if (linktype == DLT_IP_OVER_FC)
4249 return gen_ipfchostop(eaddr, (int)q.dir);
4250 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4251 }
4252 bpf_error("ethernet address used in non-ether expression");
4253 /* NOTREACHED */
4254 }
4255
4256 void
4257 sappend(s0, s1)
4258 struct slist *s0, *s1;
4259 {
4260 /*
4261 * This is definitely not the best way to do this, but the
4262 * lists will rarely get long.
4263 */
4264 while (s0->next)
4265 s0 = s0->next;
4266 s0->next = s1;
4267 }
4268
4269 static struct slist *
4270 xfer_to_x(a)
4271 struct arth *a;
4272 {
4273 struct slist *s;
4274
4275 s = new_stmt(BPF_LDX|BPF_MEM);
4276 s->s.k = a->regno;
4277 return s;
4278 }
4279
4280 static struct slist *
4281 xfer_to_a(a)
4282 struct arth *a;
4283 {
4284 struct slist *s;
4285
4286 s = new_stmt(BPF_LD|BPF_MEM);
4287 s->s.k = a->regno;
4288 return s;
4289 }
4290
4291 struct arth *
4292 gen_load(proto, index, size)
4293 int proto;
4294 struct arth *index;
4295 int size;
4296 {
4297 struct slist *s, *tmp;
4298 struct block *b;
4299 int regno = alloc_reg();
4300
4301 free_reg(index->regno);
4302 switch (size) {
4303
4304 default:
4305 bpf_error("data size must be 1, 2, or 4");
4306
4307 case 1:
4308 size = BPF_B;
4309 break;
4310
4311 case 2:
4312 size = BPF_H;
4313 break;
4314
4315 case 4:
4316 size = BPF_W;
4317 break;
4318 }
4319 switch (proto) {
4320 default:
4321 bpf_error("unsupported index operation");
4322
4323 case Q_LINK:
4324 /*
4325 * XXX - what about ATM LANE? Should the index be
4326 * relative to the beginning of the AAL5 frame, so
4327 * that 0 refers to the beginning of the LE Control
4328 * field, or relative to the beginning of the LAN
4329 * frame, so that 0 refers, for Ethernet LANE, to
4330 * the beginning of the destination address?
4331 */
4332 s = xfer_to_x(index);
4333 tmp = new_stmt(BPF_LD|BPF_IND|size);
4334 sappend(s, tmp);
4335 sappend(index->s, s);
4336 break;
4337
4338 case Q_IP:
4339 case Q_ARP:
4340 case Q_RARP:
4341 case Q_ATALK:
4342 case Q_DECNET:
4343 case Q_SCA:
4344 case Q_LAT:
4345 case Q_MOPRC:
4346 case Q_MOPDL:
4347 #ifdef INET6
4348 case Q_IPV6:
4349 #endif
4350 /* XXX Note that we assume a fixed link header here. */
4351 s = xfer_to_x(index);
4352 tmp = new_stmt(BPF_LD|BPF_IND|size);
4353 tmp->s.k = off_nl;
4354 sappend(s, tmp);
4355 sappend(index->s, s);
4356
4357 b = gen_proto_abbrev(proto);
4358 if (index->b)
4359 gen_and(index->b, b);
4360 index->b = b;
4361 break;
4362
4363 case Q_SCTP:
4364 case Q_TCP:
4365 case Q_UDP:
4366 case Q_ICMP:
4367 case Q_IGMP:
4368 case Q_IGRP:
4369 case Q_PIM:
4370 case Q_VRRP:
4371 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
4372 s->s.k = off_nl;
4373 sappend(s, xfer_to_a(index));
4374 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
4375 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
4376 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
4377 tmp->s.k = off_nl;
4378 sappend(index->s, s);
4379
4380 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
4381 if (index->b)
4382 gen_and(index->b, b);
4383 #ifdef INET6
4384 gen_and(gen_proto_abbrev(Q_IP), b);
4385 #endif
4386 index->b = b;
4387 break;
4388 #ifdef INET6
4389 case Q_ICMPV6:
4390 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
4391 /*NOTREACHED*/
4392 #endif
4393 }
4394 index->regno = regno;
4395 s = new_stmt(BPF_ST);
4396 s->s.k = regno;
4397 sappend(index->s, s);
4398
4399 return index;
4400 }
4401
4402 struct block *
4403 gen_relation(code, a0, a1, reversed)
4404 int code;
4405 struct arth *a0, *a1;
4406 int reversed;
4407 {
4408 struct slist *s0, *s1, *s2;
4409 struct block *b, *tmp;
4410
4411 s0 = xfer_to_x(a1);
4412 s1 = xfer_to_a(a0);
4413 if (code == BPF_JEQ) {
4414 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
4415 b = new_block(JMP(code));
4416 sappend(s1, s2);
4417 }
4418 else
4419 b = new_block(BPF_JMP|code|BPF_X);
4420 if (reversed)
4421 gen_not(b);
4422
4423 sappend(s0, s1);
4424 sappend(a1->s, s0);
4425 sappend(a0->s, a1->s);
4426
4427 b->stmts = a0->s;
4428
4429 free_reg(a0->regno);
4430 free_reg(a1->regno);
4431
4432 /* 'and' together protocol checks */
4433 if (a0->b) {
4434 if (a1->b) {
4435 gen_and(a0->b, tmp = a1->b);
4436 }
4437 else
4438 tmp = a0->b;
4439 } else
4440 tmp = a1->b;
4441
4442 if (tmp)
4443 gen_and(tmp, b);
4444
4445 return b;
4446 }
4447
4448 struct arth *
4449 gen_loadlen()
4450 {
4451 int regno = alloc_reg();
4452 struct arth *a = (struct arth *)newchunk(sizeof(*a));
4453 struct slist *s;
4454
4455 s = new_stmt(BPF_LD|BPF_LEN);
4456 s->next = new_stmt(BPF_ST);
4457 s->next->s.k = regno;
4458 a->s = s;
4459 a->regno = regno;
4460
4461 return a;
4462 }
4463
4464 struct arth *
4465 gen_loadi(val)
4466 int val;
4467 {
4468 struct arth *a;
4469 struct slist *s;
4470 int reg;
4471
4472 a = (struct arth *)newchunk(sizeof(*a));
4473
4474 reg = alloc_reg();
4475
4476 s = new_stmt(BPF_LD|BPF_IMM);
4477 s->s.k = val;
4478 s->next = new_stmt(BPF_ST);
4479 s->next->s.k = reg;
4480 a->s = s;
4481 a->regno = reg;
4482
4483 return a;
4484 }
4485
4486 struct arth *
4487 gen_neg(a)
4488 struct arth *a;
4489 {
4490 struct slist *s;
4491
4492 s = xfer_to_a(a);
4493 sappend(a->s, s);
4494 s = new_stmt(BPF_ALU|BPF_NEG);
4495 s->s.k = 0;
4496 sappend(a->s, s);
4497 s = new_stmt(BPF_ST);
4498 s->s.k = a->regno;
4499 sappend(a->s, s);
4500
4501 return a;
4502 }
4503
4504 struct arth *
4505 gen_arth(code, a0, a1)
4506 int code;
4507 struct arth *a0, *a1;
4508 {
4509 struct slist *s0, *s1, *s2;
4510
4511 s0 = xfer_to_x(a1);
4512 s1 = xfer_to_a(a0);
4513 s2 = new_stmt(BPF_ALU|BPF_X|code);
4514
4515 sappend(s1, s2);
4516 sappend(s0, s1);
4517 sappend(a1->s, s0);
4518 sappend(a0->s, a1->s);
4519
4520 free_reg(a0->regno);
4521 free_reg(a1->regno);
4522
4523 s0 = new_stmt(BPF_ST);
4524 a0->regno = s0->s.k = alloc_reg();
4525 sappend(a0->s, s0);
4526
4527 return a0;
4528 }
4529
4530 /*
4531 * Here we handle simple allocation of the scratch registers.
4532 * If too many registers are alloc'd, the allocator punts.
4533 */
4534 static int regused[BPF_MEMWORDS];
4535 static int curreg;
4536
4537 /*
4538 * Return the next free register.
4539 */
4540 static int
4541 alloc_reg()
4542 {
4543 int n = BPF_MEMWORDS;
4544
4545 while (--n >= 0) {
4546 if (regused[curreg])
4547 curreg = (curreg + 1) % BPF_MEMWORDS;
4548 else {
4549 regused[curreg] = 1;
4550 return curreg;
4551 }
4552 }
4553 bpf_error("too many registers needed to evaluate expression");
4554 /* NOTREACHED */
4555 }
4556
4557 /*
4558 * Return a register to the table so it can
4559 * be used later.
4560 */
4561 static void
4562 free_reg(n)
4563 int n;
4564 {
4565 regused[n] = 0;
4566 }
4567
4568 static struct block *
4569 gen_len(jmp, n)
4570 int jmp, n;
4571 {
4572 struct slist *s;
4573 struct block *b;
4574
4575 s = new_stmt(BPF_LD|BPF_LEN);
4576 b = new_block(JMP(jmp));
4577 b->stmts = s;
4578 b->s.k = n;
4579
4580 return b;
4581 }
4582
4583 struct block *
4584 gen_greater(n)
4585 int n;
4586 {
4587 return gen_len(BPF_JGE, n);
4588 }
4589
4590 /*
4591 * Actually, this is less than or equal.
4592 */
4593 struct block *
4594 gen_less(n)
4595 int n;
4596 {
4597 struct block *b;
4598
4599 b = gen_len(BPF_JGT, n);
4600 gen_not(b);
4601
4602 return b;
4603 }
4604
4605 struct block *
4606 gen_byteop(op, idx, val)
4607 int op, idx, val;
4608 {
4609 struct block *b;
4610 struct slist *s;
4611
4612 switch (op) {
4613 default:
4614 abort();
4615
4616 case '=':
4617 return gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
4618
4619 case '<':
4620 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
4621 b->s.code = JMP(BPF_JGE);
4622 gen_not(b);
4623 return b;
4624
4625 case '>':
4626 b = gen_cmp((u_int)idx, BPF_B, (bpf_int32)val);
4627 b->s.code = JMP(BPF_JGT);
4628 return b;
4629
4630 case '|':
4631 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
4632 break;
4633
4634 case '&':
4635 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
4636 break;
4637 }
4638 s->s.k = val;
4639 b = new_block(JMP(BPF_JEQ));
4640 b->stmts = s;
4641 gen_not(b);
4642
4643 return b;
4644 }
4645
4646 static u_char abroadcast[] = { 0x0 };
4647
4648 struct block *
4649 gen_broadcast(proto)
4650 int proto;
4651 {
4652 bpf_u_int32 hostmask;
4653 struct block *b0, *b1, *b2;
4654 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
4655
4656 switch (proto) {
4657
4658 case Q_DEFAULT:
4659 case Q_LINK:
4660 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
4661 return gen_ahostop(abroadcast, Q_DST);
4662 if (linktype == DLT_EN10MB)
4663 return gen_ehostop(ebroadcast, Q_DST);
4664 if (linktype == DLT_FDDI)
4665 return gen_fhostop(ebroadcast, Q_DST);
4666 if (linktype == DLT_IEEE802)
4667 return gen_thostop(ebroadcast, Q_DST);
4668 if (linktype == DLT_IEEE802_11)
4669 return gen_wlanhostop(ebroadcast, Q_DST);
4670 if (linktype == DLT_IP_OVER_FC)
4671 return gen_ipfchostop(ebroadcast, Q_DST);
4672 if (linktype == DLT_SUNATM && is_lane) {
4673 /*
4674 * Check that the packet doesn't begin with an
4675 * LE Control marker. (We've already generated
4676 * a test for LANE.)
4677 */
4678 b1 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
4679 gen_not(b1);
4680
4681 /*
4682 * Now check the MAC address.
4683 */
4684 b0 = gen_ehostop(ebroadcast, Q_DST);
4685 gen_and(b1, b0);
4686 return b0;
4687 }
4688 bpf_error("not a broadcast link");
4689 break;
4690
4691 case Q_IP:
4692 b0 = gen_linktype(ETHERTYPE_IP);
4693 hostmask = ~netmask;
4694 b1 = gen_mcmp(off_nl + 16, BPF_W, (bpf_int32)0, hostmask);
4695 b2 = gen_mcmp(off_nl + 16, BPF_W,
4696 (bpf_int32)(~0 & hostmask), hostmask);
4697 gen_or(b1, b2);
4698 gen_and(b0, b2);
4699 return b2;
4700 }
4701 bpf_error("only link-layer/IP broadcast filters supported");
4702 }
4703
4704 /*
4705 * Generate code to test the low-order bit of a MAC address (that's
4706 * the bottom bit of the *first* byte).
4707 */
4708 static struct block *
4709 gen_mac_multicast(offset)
4710 int offset;
4711 {
4712 register struct block *b0;
4713 register struct slist *s;
4714
4715 /* link[offset] & 1 != 0 */
4716 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4717 s->s.k = offset;
4718 b0 = new_block(JMP(BPF_JSET));
4719 b0->s.k = 1;
4720 b0->stmts = s;
4721 return b0;
4722 }
4723
4724 struct block *
4725 gen_multicast(proto)
4726 int proto;
4727 {
4728 register struct block *b0, *b1, *b2;
4729 register struct slist *s;
4730
4731 switch (proto) {
4732
4733 case Q_DEFAULT:
4734 case Q_LINK:
4735 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
4736 /* all ARCnet multicasts use the same address */
4737 return gen_ahostop(abroadcast, Q_DST);
4738
4739 if (linktype == DLT_EN10MB) {
4740 /* ether[0] & 1 != 0 */
4741 return gen_mac_multicast(0);
4742 }
4743
4744 if (linktype == DLT_FDDI) {
4745 /*
4746 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
4747 *
4748 * XXX - was that referring to bit-order issues?
4749 */
4750 /* fddi[1] & 1 != 0 */
4751 return gen_mac_multicast(1);
4752 }
4753
4754 if (linktype == DLT_IEEE802) {
4755 /* tr[2] & 1 != 0 */
4756 return gen_mac_multicast(2);
4757 }
4758
4759 if (linktype == DLT_IEEE802_11) {
4760 /*
4761 * Oh, yuk.
4762 *
4763 * For control frames, there is no DA.
4764 *
4765 * For management frames, DA is at an
4766 * offset of 4 from the beginning of
4767 * the packet.
4768 *
4769 * For data frames, DA is at an offset
4770 * of 4 from the beginning of the packet
4771 * if To DS is clear and at an offset of
4772 * 16 from the beginning of the packet
4773 * if To DS is set.
4774 */
4775
4776 /*
4777 * Generate the tests to be done for data frames.
4778 *
4779 * First, check for To DS set, i.e. "link[1] & 0x01".
4780 */
4781 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4782 s->s.k = 1;
4783 b1 = new_block(JMP(BPF_JSET));
4784 b1->s.k = 0x01; /* To DS */
4785 b1->stmts = s;
4786
4787 /*
4788 * If To DS is set, the DA is at 16.
4789 */
4790 b0 = gen_mac_multicast(16);
4791 gen_and(b1, b0);
4792
4793 /*
4794 * Now, check for To DS not set, i.e. check
4795 * "!(link[1] & 0x01)".
4796 */
4797 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4798 s->s.k = 1;
4799 b2 = new_block(JMP(BPF_JSET));
4800 b2->s.k = 0x01; /* To DS */
4801 b2->stmts = s;
4802 gen_not(b2);
4803
4804 /*
4805 * If To DS is not set, the DA is at 4.
4806 */
4807 b1 = gen_mac_multicast(4);
4808 gen_and(b2, b1);
4809
4810 /*
4811 * Now OR together the last two checks. That gives
4812 * the complete set of checks for data frames.
4813 */
4814 gen_or(b1, b0);
4815
4816 /*
4817 * Now check for a data frame.
4818 * I.e, check "link[0] & 0x08".
4819 */
4820 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4821 s->s.k = 0;
4822 b1 = new_block(JMP(BPF_JSET));
4823 b1->s.k = 0x08;
4824 b1->stmts = s;
4825
4826 /*
4827 * AND that with the checks done for data frames.
4828 */
4829 gen_and(b1, b0);
4830
4831 /*
4832 * If the high-order bit of the type value is 0, this
4833 * is a management frame.
4834 * I.e, check "!(link[0] & 0x08)".
4835 */
4836 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4837 s->s.k = 0;
4838 b2 = new_block(JMP(BPF_JSET));
4839 b2->s.k = 0x08;
4840 b2->stmts = s;
4841 gen_not(b2);
4842
4843 /*
4844 * For management frames, the DA is at 4.
4845 */
4846 b1 = gen_mac_multicast(4);
4847 gen_and(b2, b1);
4848
4849 /*
4850 * OR that with the checks done for data frames.
4851 * That gives the checks done for management and
4852 * data frames.
4853 */
4854 gen_or(b1, b0);
4855
4856 /*
4857 * If the low-order bit of the type value is 1,
4858 * this is either a control frame or a frame
4859 * with a reserved type, and thus not a
4860 * frame with an SA.
4861 *
4862 * I.e., check "!(link[0] & 0x04)".
4863 */
4864 s = new_stmt(BPF_LD|BPF_B|BPF_ABS);
4865 s->s.k = 0;
4866 b1 = new_block(JMP(BPF_JSET));
4867 b1->s.k = 0x04;
4868 b1->stmts = s;
4869 gen_not(b1);
4870
4871 /*
4872 * AND that with the checks for data and management
4873 * frames.
4874 */
4875 gen_and(b1, b0);
4876 return b0;
4877 }
4878
4879 if (linktype == DLT_IP_OVER_FC) {
4880 b0 = gen_mac_multicast(2);
4881 return b0;
4882 }
4883
4884 if (linktype == DLT_SUNATM && is_lane) {
4885 /*
4886 * Check that the packet doesn't begin with an
4887 * LE Control marker. (We've already generated
4888 * a test for LANE.)
4889 */
4890 b1 = gen_cmp(SUNATM_PKT_BEGIN_POS, BPF_H, 0xFF00);
4891 gen_not(b1);
4892
4893 /* ether[off_mac] & 1 != 0 */
4894 b0 = gen_mac_multicast(off_mac);
4895 gen_and(b1, b0);
4896 return b0;
4897 }
4898
4899 /* Link not known to support multicasts */
4900 break;
4901
4902 case Q_IP:
4903 b0 = gen_linktype(ETHERTYPE_IP);
4904 b1 = gen_cmp(off_nl + 16, BPF_B, (bpf_int32)224);
4905 b1->s.code = JMP(BPF_JGE);
4906 gen_and(b0, b1);
4907 return b1;
4908
4909 #ifdef INET6
4910 case Q_IPV6:
4911 b0 = gen_linktype(ETHERTYPE_IPV6);
4912 b1 = gen_cmp(off_nl + 24, BPF_B, (bpf_int32)255);
4913 gen_and(b0, b1);
4914 return b1;
4915 #endif /* INET6 */
4916 }
4917 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
4918 }
4919
4920 /*
4921 * generate command for inbound/outbound. It's here so we can
4922 * make it link-type specific. 'dir' = 0 implies "inbound",
4923 * = 1 implies "outbound".
4924 */
4925 struct block *
4926 gen_inbound(dir)
4927 int dir;
4928 {
4929 register struct block *b0;
4930
4931 /*
4932 * Only some data link types support inbound/outbound qualifiers.
4933 */
4934 switch (linktype) {
4935 case DLT_SLIP:
4936 b0 = gen_relation(BPF_JEQ,
4937 gen_load(Q_LINK, gen_loadi(0), 1),
4938 gen_loadi(0),
4939 dir);
4940 break;
4941
4942 case DLT_LINUX_SLL:
4943 if (dir) {
4944 /*
4945 * Match packets sent by this machine.
4946 */
4947 b0 = gen_cmp(0, BPF_H, LINUX_SLL_OUTGOING);
4948 } else {
4949 /*
4950 * Match packets sent to this machine.
4951 * (No broadcast or multicast packets, or
4952 * packets sent to some other machine and
4953 * received promiscuously.)
4954 *
4955 * XXX - packets sent to other machines probably
4956 * shouldn't be matched, but what about broadcast
4957 * or multicast packets we received?
4958 */
4959 b0 = gen_cmp(0, BPF_H, LINUX_SLL_HOST);
4960 }
4961 break;
4962
4963 case DLT_PFLOG:
4964 b0 = gen_cmp(26, BPF_H,
4965 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
4966 break;
4967
4968 default:
4969 bpf_error("inbound/outbound not supported on linktype %d",
4970 linktype);
4971 b0 = NULL;
4972 /* NOTREACHED */
4973 }
4974 return (b0);
4975 }
4976
4977 /* PF firewall log matched interface */
4978 struct block *
4979 gen_pf_ifname(char *ifname)
4980 {
4981 if (linktype != DLT_PFLOG) {
4982 bpf_error("ifname supported only for DLT_PFLOG");
4983 /* NOTREACHED */
4984 }
4985 if (strlen(ifname) >= 16) {
4986 bpf_error("ifname interface names can't be larger than 16 characters");
4987 /* NOTREACHED */
4988 }
4989 return (gen_bcmp(4, strlen(ifname), ifname));
4990 }
4991
4992
4993 /* PF firewall log rule number */
4994 struct block *
4995 gen_pf_rnr(int rnr)
4996 {
4997 if (linktype != DLT_PFLOG) {
4998 bpf_error("rnr supported only for DLT_PFLOG");
4999 /* NOTREACHED */
5000 }
5001
5002 return (gen_cmp(20, BPF_H, (bpf_int32)rnr));
5003 }
5004
5005 /* PF firewall log reason code */
5006 struct block *
5007 gen_pf_reason(int reason)
5008 {
5009 if (linktype != DLT_PFLOG) {
5010 bpf_error("reason supported only for DLT_PFLOG");
5011 /* NOTREACHED */
5012 }
5013
5014 return (gen_cmp(22, BPF_H, (bpf_int32)reason));
5015 }
5016
5017 /* PF firewall log action */
5018 struct block *
5019 gen_pf_action(int action)
5020 {
5021 if (linktype != DLT_PFLOG) {
5022 bpf_error("action supported only for DLT_PFLOG");
5023 /* NOTREACHED */
5024 }
5025
5026 return (gen_cmp(24, BPF_H, (bpf_int32)action));
5027 }
5028
5029 struct block *
5030 gen_acode(eaddr, q)
5031 register const u_char *eaddr;
5032 struct qual q;
5033 {
5034 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
5035 if (linktype == DLT_ARCNET || linktype == DLT_ARCNET_LINUX)
5036 return gen_ahostop(eaddr, (int)q.dir);
5037 }
5038 bpf_error("ARCnet address used in non-arc expression");
5039 /* NOTREACHED */
5040 }
5041
5042 static struct block *
5043 gen_ahostop(eaddr, dir)
5044 register const u_char *eaddr;
5045 register int dir;
5046 {
5047 register struct block *b0, *b1;
5048
5049 switch (dir) {
5050 /* src comes first, different from Ethernet */
5051 case Q_SRC:
5052 return gen_bcmp(0, 1, eaddr);
5053
5054 case Q_DST:
5055 return gen_bcmp(1, 1, eaddr);
5056
5057 case Q_AND:
5058 b0 = gen_ahostop(eaddr, Q_SRC);
5059 b1 = gen_ahostop(eaddr, Q_DST);
5060 gen_and(b0, b1);
5061 return b1;
5062
5063 case Q_DEFAULT:
5064 case Q_OR:
5065 b0 = gen_ahostop(eaddr, Q_SRC);
5066 b1 = gen_ahostop(eaddr, Q_DST);
5067 gen_or(b0, b1);
5068 return b1;
5069 }
5070 abort();
5071 /* NOTREACHED */
5072 }
5073
5074 /*
5075 * support IEEE 802.1Q VLAN trunk over ethernet
5076 */
5077 struct block *
5078 gen_vlan(vlan_num)
5079 int vlan_num;
5080 {
5081 struct block *b0;
5082
5083 /*
5084 * Change the offsets to point to the type and data fields within
5085 * the VLAN packet. This is somewhat of a kludge.
5086 */
5087 if (orig_nl == (u_int)-1) {
5088 orig_linktype = off_linktype; /* save original values */
5089 orig_nl = off_nl;
5090 orig_nl_nosnap = off_nl_nosnap;
5091
5092 switch (linktype) {
5093
5094 case DLT_EN10MB:
5095 off_linktype = 16;
5096 off_nl_nosnap = 18;
5097 off_nl = 18;
5098 break;
5099
5100 default:
5101 bpf_error("no VLAN support for data link type %d",
5102 linktype);
5103 /*NOTREACHED*/
5104 }
5105 }
5106
5107 /* check for VLAN */
5108 b0 = gen_cmp(orig_linktype, BPF_H, (bpf_int32)ETHERTYPE_8021Q);
5109
5110 /* If a specific VLAN is requested, check VLAN id */
5111 if (vlan_num >= 0) {
5112 struct block *b1;
5113
5114 b1 = gen_cmp(orig_nl, BPF_H, (bpf_int32)vlan_num);
5115 gen_and(b0, b1);
5116 b0 = b1;
5117 }
5118
5119 return (b0);
5120 }
5121
5122 struct block *
5123 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
5124 int atmfield;
5125 bpf_u_int32 jvalue;
5126 bpf_u_int32 jtype;
5127 int reverse;
5128 {
5129 struct block *b0;
5130
5131 switch (atmfield) {
5132
5133 case A_VPI:
5134 if (!is_atm)
5135 bpf_error("'vpi' supported only on raw ATM");
5136 if (off_vpi == -1)
5137 abort();
5138 b0 = gen_ncmp(BPF_B, off_vpi, 0xffffffff, (u_int)jtype,
5139 (u_int)jvalue, reverse);
5140 break;
5141
5142 case A_VCI:
5143 if (!is_atm)
5144 bpf_error("'vci' supported only on raw ATM");
5145 if (off_vci == -1)
5146 abort();
5147 b0 = gen_ncmp(BPF_H, off_vci, 0xffffffff, (u_int)jtype,
5148 (u_int)jvalue, reverse);
5149 break;
5150
5151 case A_PROTOTYPE:
5152 if (off_proto == -1)
5153 abort(); /* XXX - this isn't on FreeBSD */
5154 b0 = gen_ncmp(BPF_B, off_proto, 0x0f, (u_int)jtype,
5155 (u_int)jvalue, reverse);
5156 break;
5157
5158 case A_MSGTYPE:
5159 if (off_payload == -1)
5160 abort();
5161 b0 = gen_ncmp(BPF_B, off_payload + MSG_TYPE_POS, 0xffffffff,
5162 (u_int)jtype, (u_int)jvalue, reverse);
5163 break;
5164
5165 case A_CALLREFTYPE:
5166 if (!is_atm)
5167 bpf_error("'callref' supported only on raw ATM");
5168 if (off_proto == -1)
5169 abort();
5170 b0 = gen_ncmp(BPF_B, off_proto, 0xffffffff, (u_int)jtype,
5171 (u_int)jvalue, reverse);
5172 break;
5173
5174 default:
5175 abort();
5176 }
5177 return b0;
5178 }
5179
5180 struct block *
5181 gen_atmtype_abbrev(type)
5182 int type;
5183 {
5184 struct block *b0, *b1;
5185
5186 switch (type) {
5187
5188 case A_METAC:
5189 /* Get all packets in Meta signalling Circuit */
5190 if (!is_atm)
5191 bpf_error("'metac' supported only on raw ATM");
5192 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5193 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
5194 gen_and(b0, b1);
5195 break;
5196
5197 case A_BCC:
5198 /* Get all packets in Broadcast Circuit*/
5199 if (!is_atm)
5200 bpf_error("'bcc' supported only on raw ATM");
5201 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5202 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
5203 gen_and(b0, b1);
5204 break;
5205
5206 case A_OAMF4SC:
5207 /* Get all cells in Segment OAM F4 circuit*/
5208 if (!is_atm)
5209 bpf_error("'oam4sc' supported only on raw ATM");
5210 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5211 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
5212 gen_and(b0, b1);
5213 break;
5214
5215 case A_OAMF4EC:
5216 /* Get all cells in End-to-End OAM F4 Circuit*/
5217 if (!is_atm)
5218 bpf_error("'oam4ec' supported only on raw ATM");
5219 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5220 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
5221 gen_and(b0, b1);
5222 break;
5223
5224 case A_SC:
5225 /* Get all packets in connection Signalling Circuit */
5226 if (!is_atm)
5227 bpf_error("'sc' supported only on raw ATM");
5228 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5229 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
5230 gen_and(b0, b1);
5231 break;
5232
5233 case A_ILMIC:
5234 /* Get all packets in ILMI Circuit */
5235 if (!is_atm)
5236 bpf_error("'ilmic' supported only on raw ATM");
5237 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5238 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
5239 gen_and(b0, b1);
5240 break;
5241
5242 case A_LANE:
5243 /* Get all LANE packets */
5244 if (!is_atm)
5245 bpf_error("'lane' supported only on raw ATM");
5246 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
5247
5248 /*
5249 * Arrange that all subsequent tests assume LANE
5250 * rather than LLC-encapsulated packets, and set
5251 * the offsets appropriately for LANE-encapsulated
5252 * Ethernet.
5253 *
5254 * "off_mac" is the offset of the Ethernet header,
5255 * which is 2 bytes past the ATM pseudo-header
5256 * (skipping the pseudo-header and 2-byte LE Client
5257 * field). The other offsets are Ethernet offsets
5258 * relative to "off_mac".
5259 */
5260 is_lane = 1;
5261 off_mac = off_payload + 2; /* MAC header */
5262 off_linktype = off_mac + 12;
5263 off_nl = off_mac + 14; /* Ethernet II */
5264 off_nl_nosnap = off_mac + 17; /* 802.3+802.2 */
5265 break;
5266
5267 case A_LLC:
5268 /* Get all LLC-encapsulated packets */
5269 if (!is_atm)
5270 bpf_error("'llc' supported only on raw ATM");
5271 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
5272 is_lane = 0;
5273 break;
5274
5275 default:
5276 abort();
5277 }
5278 return b1;
5279 }
5280
5281
5282 static struct block *
5283 gen_msg_abbrev(type)
5284 int type;
5285 {
5286 struct block *b1;
5287
5288 /*
5289 * Q.2931 signalling protocol messages for handling virtual circuits
5290 * establishment and teardown
5291 */
5292 switch (type) {
5293
5294 case A_SETUP:
5295 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
5296 break;
5297
5298 case A_CALLPROCEED:
5299 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
5300 break;
5301
5302 case A_CONNECT:
5303 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
5304 break;
5305
5306 case A_CONNECTACK:
5307 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
5308 break;
5309
5310 case A_RELEASE:
5311 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
5312 break;
5313
5314 case A_RELEASE_DONE:
5315 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
5316 break;
5317
5318 default:
5319 abort();
5320 }
5321 return b1;
5322 }
5323
5324 struct block *
5325 gen_atmmulti_abbrev(type)
5326 int type;
5327 {
5328 struct block *b0, *b1;
5329
5330 switch (type) {
5331
5332 case A_OAM:
5333 if (!is_atm)
5334 bpf_error("'oam' supported only on raw ATM");
5335 b1 = gen_atmmulti_abbrev(A_OAMF4);
5336 break;
5337
5338 case A_OAMF4:
5339 if (!is_atm)
5340 bpf_error("'oamf4' supported only on raw ATM");
5341 /* OAM F4 type */
5342 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
5343 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
5344 gen_or(b0, b1);
5345 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
5346 gen_and(b0, b1);
5347 break;
5348
5349 case A_CONNECTMSG:
5350 /*
5351 * Get Q.2931 signalling messages for switched
5352 * virtual connection
5353 */
5354 if (!is_atm)
5355 bpf_error("'connectmsg' supported only on raw ATM");
5356 b0 = gen_msg_abbrev(A_SETUP);
5357 b1 = gen_msg_abbrev(A_CALLPROCEED);
5358 gen_or(b0, b1);
5359 b0 = gen_msg_abbrev(A_CONNECT);
5360 gen_or(b0, b1);
5361 b0 = gen_msg_abbrev(A_CONNECTACK);
5362 gen_or(b0, b1);
5363 b0 = gen_msg_abbrev(A_RELEASE);
5364 gen_or(b0, b1);
5365 b0 = gen_msg_abbrev(A_RELEASE_DONE);
5366 gen_or(b0, b1);
5367 b0 = gen_atmtype_abbrev(A_SC);
5368 gen_and(b0, b1);
5369 break;
5370
5371 case A_METACONNECT:
5372 if (!is_atm)
5373 bpf_error("'metaconnect' supported only on raw ATM");
5374 b0 = gen_msg_abbrev(A_SETUP);
5375 b1 = gen_msg_abbrev(A_CALLPROCEED);
5376 gen_or(b0, b1);
5377 b0 = gen_msg_abbrev(A_CONNECT);
5378 gen_or(b0, b1);
5379 b0 = gen_msg_abbrev(A_RELEASE);
5380 gen_or(b0, b1);
5381 b0 = gen_msg_abbrev(A_RELEASE_DONE);
5382 gen_or(b0, b1);
5383 b0 = gen_atmtype_abbrev(A_METAC);
5384 gen_and(b0, b1);
5385 break;
5386
5387 default:
5388 abort();
5389 }
5390 return b1;
5391 }