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