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