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