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