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