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