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