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