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