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