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