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