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