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