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