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