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