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