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