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