Merge branch 'vendor/OPENPAM'
[dragonfly.git] / contrib / libpcap / gencode.c
blob27473837332a96820beb61fb66cdf247ef599b57
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.
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.
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
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
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 */
47 * XXX - why was this included even on UNIX?
49 #ifdef __MINGW32__
50 #include "ip6_misc.h"
51 #endif
53 #ifndef WIN32
55 #ifdef __NetBSD__
56 #include <sys/param.h>
57 #endif
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
62 #endif /* WIN32 */
64 #include <stdlib.h>
65 #include <string.h>
66 #include <memory.h>
67 #include <setjmp.h>
68 #include <stdarg.h>
70 #ifdef MSDOS
71 #include "pcap-dos.h"
72 #endif
74 #include "pcap-int.h"
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 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
88 #include <linux/types.h>
89 #include <linux/if_packet.h>
90 #include <linux/filter.h>
91 #endif
92 #ifdef HAVE_NET_PFVAR_H
93 #include <sys/socket.h>
94 #include <net/if.h>
95 #include <net/pf/pfvar.h>
96 #include <net/pf/if_pflog.h>
97 #endif
98 #ifndef offsetof
99 #define offsetof(s, e) ((size_t)&((s *)0)->e)
100 #endif
101 #ifdef INET6
102 #ifndef WIN32
103 #include <netdb.h> /* for "struct addrinfo" */
104 #endif /* WIN32 */
105 #endif /*INET6*/
106 #include <pcap/namedb.h>
108 #define ETHERMTU 1500
110 #ifndef IPPROTO_SCTP
111 #define IPPROTO_SCTP 132
112 #endif
114 #ifdef HAVE_OS_PROTO_H
115 #include "os-proto.h"
116 #endif
118 #define JMP(c) ((c)|BPF_JMP|BPF_K)
120 /* Locals */
121 static jmp_buf top_ctx;
122 static pcap_t *bpf_pcap;
124 /* Hack for updating VLAN, MPLS, and PPPoE offsets. */
125 #ifdef WIN32
126 static u_int orig_linktype = (u_int)-1, orig_nl = (u_int)-1, label_stack_depth = (u_int)-1;
127 #else
128 static u_int orig_linktype = -1U, orig_nl = -1U, label_stack_depth = -1U;
129 #endif
131 /* XXX */
132 #ifdef PCAP_FDDIPAD
133 static int pcap_fddipad;
134 #endif
136 /* VARARGS */
137 void
138 bpf_error(const char *fmt, ...)
140 va_list ap;
142 va_start(ap, fmt);
143 if (bpf_pcap != NULL)
144 (void)vsnprintf(pcap_geterr(bpf_pcap), PCAP_ERRBUF_SIZE,
145 fmt, ap);
146 va_end(ap);
147 longjmp(top_ctx, 1);
148 /* NOTREACHED */
151 static void init_linktype(pcap_t *);
153 static void init_regs(void);
154 static int alloc_reg(void);
155 static void free_reg(int);
157 static struct block *root;
160 * Value passed to gen_load_a() to indicate what the offset argument
161 * is relative to.
163 enum e_offrel {
164 OR_PACKET, /* relative to the beginning of the packet */
165 OR_LINK, /* relative to the beginning of the link-layer header */
166 OR_MACPL, /* relative to the end of the MAC-layer header */
167 OR_NET, /* relative to the network-layer header */
168 OR_NET_NOSNAP, /* relative to the network-layer header, with no SNAP header at the link layer */
169 OR_TRAN_IPV4, /* relative to the transport-layer header, with IPv4 network layer */
170 OR_TRAN_IPV6 /* relative to the transport-layer header, with IPv6 network layer */
173 #ifdef INET6
175 * As errors are handled by a longjmp, anything allocated must be freed
176 * in the longjmp handler, so it must be reachable from that handler.
177 * One thing that's allocated is the result of pcap_nametoaddrinfo();
178 * it must be freed with freeaddrinfo(). This variable points to any
179 * addrinfo structure that would need to be freed.
181 static struct addrinfo *ai;
182 #endif
185 * We divy out chunks of memory rather than call malloc each time so
186 * we don't have to worry about leaking memory. It's probably
187 * not a big deal if all this memory was wasted but if this ever
188 * goes into a library that would probably not be a good idea.
190 * XXX - this *is* in a library....
192 #define NCHUNKS 16
193 #define CHUNK0SIZE 1024
194 struct chunk {
195 u_int n_left;
196 void *m;
199 static struct chunk chunks[NCHUNKS];
200 static int cur_chunk;
202 static void *newchunk(u_int);
203 static void freechunks(void);
204 static inline struct block *new_block(int);
205 static inline struct slist *new_stmt(int);
206 static struct block *gen_retblk(int);
207 static inline void syntax(void);
209 static void backpatch(struct block *, struct block *);
210 static void merge(struct block *, struct block *);
211 static struct block *gen_cmp(enum e_offrel, u_int, u_int, bpf_int32);
212 static struct block *gen_cmp_gt(enum e_offrel, u_int, u_int, bpf_int32);
213 static struct block *gen_cmp_ge(enum e_offrel, u_int, u_int, bpf_int32);
214 static struct block *gen_cmp_lt(enum e_offrel, u_int, u_int, bpf_int32);
215 static struct block *gen_cmp_le(enum e_offrel, u_int, u_int, bpf_int32);
216 static struct block *gen_mcmp(enum e_offrel, u_int, u_int, bpf_int32,
217 bpf_u_int32);
218 static struct block *gen_bcmp(enum e_offrel, u_int, u_int, const u_char *);
219 static struct block *gen_ncmp(enum e_offrel, bpf_u_int32, bpf_u_int32,
220 bpf_u_int32, bpf_u_int32, int, bpf_int32);
221 static struct slist *gen_load_llrel(u_int, u_int);
222 static struct slist *gen_load_macplrel(u_int, u_int);
223 static struct slist *gen_load_a(enum e_offrel, u_int, u_int);
224 static struct slist *gen_loadx_iphdrlen(void);
225 static struct block *gen_uncond(int);
226 static inline struct block *gen_true(void);
227 static inline struct block *gen_false(void);
228 static struct block *gen_ether_linktype(int);
229 static struct block *gen_ipnet_linktype(int);
230 static struct block *gen_linux_sll_linktype(int);
231 static struct slist *gen_load_prism_llprefixlen(void);
232 static struct slist *gen_load_avs_llprefixlen(void);
233 static struct slist *gen_load_radiotap_llprefixlen(void);
234 static struct slist *gen_load_ppi_llprefixlen(void);
235 static void insert_compute_vloffsets(struct block *);
236 static struct slist *gen_llprefixlen(void);
237 static struct slist *gen_off_macpl(void);
238 static int ethertype_to_ppptype(int);
239 static struct block *gen_linktype(int);
240 static struct block *gen_snap(bpf_u_int32, bpf_u_int32);
241 static struct block *gen_llc_linktype(int);
242 static struct block *gen_hostop(bpf_u_int32, bpf_u_int32, int, int, u_int, u_int);
243 #ifdef INET6
244 static struct block *gen_hostop6(struct in6_addr *, struct in6_addr *, int, int, u_int, u_int);
245 #endif
246 static struct block *gen_ahostop(const u_char *, int);
247 static struct block *gen_ehostop(const u_char *, int);
248 static struct block *gen_fhostop(const u_char *, int);
249 static struct block *gen_thostop(const u_char *, int);
250 static struct block *gen_wlanhostop(const u_char *, int);
251 static struct block *gen_ipfchostop(const u_char *, int);
252 static struct block *gen_dnhostop(bpf_u_int32, int);
253 static struct block *gen_mpls_linktype(int);
254 static struct block *gen_host(bpf_u_int32, bpf_u_int32, int, int, int);
255 #ifdef INET6
256 static struct block *gen_host6(struct in6_addr *, struct in6_addr *, int, int, int);
257 #endif
258 #ifndef INET6
259 static struct block *gen_gateway(const u_char *, bpf_u_int32 **, int, int);
260 #endif
261 static struct block *gen_ipfrag(void);
262 static struct block *gen_portatom(int, bpf_int32);
263 static struct block *gen_portrangeatom(int, bpf_int32, bpf_int32);
264 #ifdef INET6
265 static struct block *gen_portatom6(int, bpf_int32);
266 static struct block *gen_portrangeatom6(int, bpf_int32, bpf_int32);
267 #endif
268 struct block *gen_portop(int, int, int);
269 static struct block *gen_port(int, int, int);
270 struct block *gen_portrangeop(int, int, int, int);
271 static struct block *gen_portrange(int, int, int, int);
272 #ifdef INET6
273 struct block *gen_portop6(int, int, int);
274 static struct block *gen_port6(int, int, int);
275 struct block *gen_portrangeop6(int, int, int, int);
276 static struct block *gen_portrange6(int, int, int, int);
277 #endif
278 static int lookup_proto(const char *, int);
279 static struct block *gen_protochain(int, int, int);
280 static struct block *gen_proto(int, int, int);
281 static struct slist *xfer_to_x(struct arth *);
282 static struct slist *xfer_to_a(struct arth *);
283 static struct block *gen_mac_multicast(int);
284 static struct block *gen_len(int, int);
285 static struct block *gen_check_802_11_data_frame(void);
287 static struct block *gen_ppi_dlt_check(void);
288 static struct block *gen_msg_abbrev(int type);
290 static void *
291 newchunk(n)
292 u_int n;
294 struct chunk *cp;
295 int k;
296 size_t size;
298 #ifndef __NetBSD__
299 /* XXX Round up to nearest long. */
300 n = (n + sizeof(long) - 1) & ~(sizeof(long) - 1);
301 #else
302 /* XXX Round up to structure boundary. */
303 n = ALIGN(n);
304 #endif
306 cp = &chunks[cur_chunk];
307 if (n > cp->n_left) {
308 ++cp, k = ++cur_chunk;
309 if (k >= NCHUNKS)
310 bpf_error("out of memory");
311 size = CHUNK0SIZE << k;
312 cp->m = (void *)malloc(size);
313 if (cp->m == NULL)
314 bpf_error("out of memory");
315 memset((char *)cp->m, 0, size);
316 cp->n_left = size;
317 if (n > size)
318 bpf_error("out of memory");
320 cp->n_left -= n;
321 return (void *)((char *)cp->m + cp->n_left);
324 static void
325 freechunks()
327 int i;
329 cur_chunk = 0;
330 for (i = 0; i < NCHUNKS; ++i)
331 if (chunks[i].m != NULL) {
332 free(chunks[i].m);
333 chunks[i].m = NULL;
338 * A strdup whose allocations are freed after code generation is over.
340 char *
341 sdup(s)
342 register const char *s;
344 int n = strlen(s) + 1;
345 char *cp = newchunk(n);
347 strlcpy(cp, s, n);
348 return (cp);
351 static inline struct block *
352 new_block(code)
353 int code;
355 struct block *p;
357 p = (struct block *)newchunk(sizeof(*p));
358 p->s.code = code;
359 p->head = p;
361 return p;
364 static inline struct slist *
365 new_stmt(code)
366 int code;
368 struct slist *p;
370 p = (struct slist *)newchunk(sizeof(*p));
371 p->s.code = code;
373 return p;
376 static struct block *
377 gen_retblk(v)
378 int v;
380 struct block *b = new_block(BPF_RET|BPF_K);
382 b->s.k = v;
383 return b;
386 static inline void
387 syntax()
389 bpf_error("syntax error in filter expression");
392 static bpf_u_int32 netmask;
393 static int snaplen;
394 int no_optimize;
395 #ifdef WIN32
396 static int
397 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
398 const char *buf, int optimize, bpf_u_int32 mask);
401 pcap_compile(pcap_t *p, struct bpf_program *program,
402 const char *buf, int optimize, bpf_u_int32 mask)
404 int result;
406 EnterCriticalSection(&g_PcapCompileCriticalSection);
408 result = pcap_compile_unsafe(p, program, buf, optimize, mask);
410 LeaveCriticalSection(&g_PcapCompileCriticalSection);
412 return result;
415 static int
416 pcap_compile_unsafe(pcap_t *p, struct bpf_program *program,
417 const char *buf, int optimize, bpf_u_int32 mask)
418 #else /* WIN32 */
420 pcap_compile(pcap_t *p, struct bpf_program *program,
421 const char *buf, int optimize, bpf_u_int32 mask)
422 #endif /* WIN32 */
424 extern int n_errors;
425 const char * volatile xbuf = buf;
426 u_int len;
428 no_optimize = 0;
429 n_errors = 0;
430 root = NULL;
431 bpf_pcap = p;
432 init_regs();
433 if (setjmp(top_ctx)) {
434 #ifdef INET6
435 if (ai != NULL) {
436 freeaddrinfo(ai);
437 ai = NULL;
439 #endif
440 lex_cleanup();
441 freechunks();
442 return (-1);
445 netmask = mask;
447 snaplen = pcap_snapshot(p);
448 if (snaplen == 0) {
449 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
450 "snaplen of 0 rejects all packets");
451 return -1;
454 lex_init(xbuf ? xbuf : "");
455 init_linktype(p);
456 (void)pcap_parse();
458 if (n_errors)
459 syntax();
461 if (root == NULL)
462 root = gen_retblk(snaplen);
464 if (optimize && !no_optimize) {
465 bpf_optimize(&root);
466 if (root == NULL ||
467 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
468 bpf_error("expression rejects all packets");
470 program->bf_insns = icode_to_fcode(root, &len);
471 program->bf_len = len;
473 lex_cleanup();
474 freechunks();
475 return (0);
479 * entry point for using the compiler with no pcap open
480 * pass in all the stuff that is needed explicitly instead.
483 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
484 struct bpf_program *program,
485 const char *buf, int optimize, bpf_u_int32 mask)
487 pcap_t *p;
488 int ret;
490 p = pcap_open_dead(linktype_arg, snaplen_arg);
491 if (p == NULL)
492 return (-1);
493 ret = pcap_compile(p, program, buf, optimize, mask);
494 pcap_close(p);
495 return (ret);
499 * Clean up a "struct bpf_program" by freeing all the memory allocated
500 * in it.
502 void
503 pcap_freecode(struct bpf_program *program)
505 program->bf_len = 0;
506 if (program->bf_insns != NULL) {
507 free((char *)program->bf_insns);
508 program->bf_insns = NULL;
513 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
514 * which of the jt and jf fields has been resolved and which is a pointer
515 * back to another unresolved block (or nil). At least one of the fields
516 * in each block is already resolved.
518 static void
519 backpatch(list, target)
520 struct block *list, *target;
522 struct block *next;
524 while (list) {
525 if (!list->sense) {
526 next = JT(list);
527 JT(list) = target;
528 } else {
529 next = JF(list);
530 JF(list) = target;
532 list = next;
537 * Merge the lists in b0 and b1, using the 'sense' field to indicate
538 * which of jt and jf is the link.
540 static void
541 merge(b0, b1)
542 struct block *b0, *b1;
544 register struct block **p = &b0;
546 /* Find end of list. */
547 while (*p)
548 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
550 /* Concatenate the lists. */
551 *p = b1;
554 void
555 finish_parse(p)
556 struct block *p;
558 struct block *ppi_dlt_check;
561 * Insert before the statements of the first (root) block any
562 * statements needed to load the lengths of any variable-length
563 * headers into registers.
565 * XXX - a fancier strategy would be to insert those before the
566 * statements of all blocks that use those lengths and that
567 * have no predecessors that use them, so that we only compute
568 * the lengths if we need them. There might be even better
569 * approaches than that.
571 * However, those strategies would be more complicated, and
572 * as we don't generate code to compute a length if the
573 * program has no tests that use the length, and as most
574 * tests will probably use those lengths, we would just
575 * postpone computing the lengths so that it's not done
576 * for tests that fail early, and it's not clear that's
577 * worth the effort.
579 insert_compute_vloffsets(p->head);
582 * For DLT_PPI captures, generate a check of the per-packet
583 * DLT value to make sure it's DLT_IEEE802_11.
585 ppi_dlt_check = gen_ppi_dlt_check();
586 if (ppi_dlt_check != NULL)
587 gen_and(ppi_dlt_check, p);
589 backpatch(p, gen_retblk(snaplen));
590 p->sense = !p->sense;
591 backpatch(p, gen_retblk(0));
592 root = p->head;
595 void
596 gen_and(b0, b1)
597 struct block *b0, *b1;
599 backpatch(b0, b1->head);
600 b0->sense = !b0->sense;
601 b1->sense = !b1->sense;
602 merge(b1, b0);
603 b1->sense = !b1->sense;
604 b1->head = b0->head;
607 void
608 gen_or(b0, b1)
609 struct block *b0, *b1;
611 b0->sense = !b0->sense;
612 backpatch(b0, b1->head);
613 b0->sense = !b0->sense;
614 merge(b1, b0);
615 b1->head = b0->head;
618 void
619 gen_not(b)
620 struct block *b;
622 b->sense = !b->sense;
625 static struct block *
626 gen_cmp(offrel, offset, size, v)
627 enum e_offrel offrel;
628 u_int offset, size;
629 bpf_int32 v;
631 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
634 static struct block *
635 gen_cmp_gt(offrel, offset, size, v)
636 enum e_offrel offrel;
637 u_int offset, size;
638 bpf_int32 v;
640 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
643 static struct block *
644 gen_cmp_ge(offrel, offset, size, v)
645 enum e_offrel offrel;
646 u_int offset, size;
647 bpf_int32 v;
649 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
652 static struct block *
653 gen_cmp_lt(offrel, offset, size, v)
654 enum e_offrel offrel;
655 u_int offset, size;
656 bpf_int32 v;
658 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
661 static struct block *
662 gen_cmp_le(offrel, offset, size, v)
663 enum e_offrel offrel;
664 u_int offset, size;
665 bpf_int32 v;
667 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
670 static struct block *
671 gen_mcmp(offrel, offset, size, v, mask)
672 enum e_offrel offrel;
673 u_int offset, size;
674 bpf_int32 v;
675 bpf_u_int32 mask;
677 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
680 static struct block *
681 gen_bcmp(offrel, offset, size, v)
682 enum e_offrel offrel;
683 register u_int offset, size;
684 register const u_char *v;
686 register struct block *b, *tmp;
688 b = NULL;
689 while (size >= 4) {
690 register const u_char *p = &v[size - 4];
691 bpf_int32 w = ((bpf_int32)p[0] << 24) |
692 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
694 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
695 if (b != NULL)
696 gen_and(b, tmp);
697 b = tmp;
698 size -= 4;
700 while (size >= 2) {
701 register const u_char *p = &v[size - 2];
702 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
704 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
705 if (b != NULL)
706 gen_and(b, tmp);
707 b = tmp;
708 size -= 2;
710 if (size > 0) {
711 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
712 if (b != NULL)
713 gen_and(b, tmp);
714 b = tmp;
716 return b;
720 * AND the field of size "size" at offset "offset" relative to the header
721 * specified by "offrel" with "mask", and compare it with the value "v"
722 * with the test specified by "jtype"; if "reverse" is true, the test
723 * should test the opposite of "jtype".
725 static struct block *
726 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
727 enum e_offrel offrel;
728 bpf_int32 v;
729 bpf_u_int32 offset, size, mask, jtype;
730 int reverse;
732 struct slist *s, *s2;
733 struct block *b;
735 s = gen_load_a(offrel, offset, size);
737 if (mask != 0xffffffff) {
738 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
739 s2->s.k = mask;
740 sappend(s, s2);
743 b = new_block(JMP(jtype));
744 b->stmts = s;
745 b->s.k = v;
746 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
747 gen_not(b);
748 return b;
752 * Various code constructs need to know the layout of the data link
753 * layer. These variables give the necessary offsets from the beginning
754 * of the packet data.
758 * This is the offset of the beginning of the link-layer header from
759 * the beginning of the raw packet data.
761 * It's usually 0, except for 802.11 with a fixed-length radio header.
762 * (For 802.11 with a variable-length radio header, we have to generate
763 * code to compute that offset; off_ll is 0 in that case.)
765 static u_int off_ll;
768 * If there's a variable-length header preceding the link-layer header,
769 * "reg_off_ll" is the register number for a register containing the
770 * length of that header, and therefore the offset of the link-layer
771 * header from the beginning of the raw packet data. Otherwise,
772 * "reg_off_ll" is -1.
774 static int reg_off_ll;
777 * This is the offset of the beginning of the MAC-layer header from
778 * the beginning of the link-layer header.
779 * It's usually 0, except for ATM LANE, where it's the offset, relative
780 * to the beginning of the raw packet data, of the Ethernet header, and
781 * for Ethernet with various additional information.
783 static u_int off_mac;
786 * This is the offset of the beginning of the MAC-layer payload,
787 * from the beginning of the raw packet data.
789 * I.e., it's the sum of the length of the link-layer header (without,
790 * for example, any 802.2 LLC header, so it's the MAC-layer
791 * portion of that header), plus any prefix preceding the
792 * link-layer header.
794 static u_int off_macpl;
797 * This is 1 if the offset of the beginning of the MAC-layer payload
798 * from the beginning of the link-layer header is variable-length.
800 static int off_macpl_is_variable;
803 * If the link layer has variable_length headers, "reg_off_macpl"
804 * is the register number for a register containing the length of the
805 * link-layer header plus the length of any variable-length header
806 * preceding the link-layer header. Otherwise, "reg_off_macpl"
807 * is -1.
809 static int reg_off_macpl;
812 * "off_linktype" is the offset to information in the link-layer header
813 * giving the packet type. This offset is relative to the beginning
814 * of the link-layer header (i.e., it doesn't include off_ll).
816 * For Ethernet, it's the offset of the Ethernet type field.
818 * For link-layer types that always use 802.2 headers, it's the
819 * offset of the LLC header.
821 * For PPP, it's the offset of the PPP type field.
823 * For Cisco HDLC, it's the offset of the CHDLC type field.
825 * For BSD loopback, it's the offset of the AF_ value.
827 * For Linux cooked sockets, it's the offset of the type field.
829 * It's set to -1 for no encapsulation, in which case, IP is assumed.
831 static u_int off_linktype;
834 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
835 * checks to check the PPP header, assumed to follow a LAN-style link-
836 * layer header and a PPPoE session header.
838 static int is_pppoes = 0;
841 * TRUE if the link layer includes an ATM pseudo-header.
843 static int is_atm = 0;
846 * TRUE if "lane" appeared in the filter; it causes us to generate
847 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
849 static int is_lane = 0;
852 * These are offsets for the ATM pseudo-header.
854 static u_int off_vpi;
855 static u_int off_vci;
856 static u_int off_proto;
859 * These are offsets for the MTP2 fields.
861 static u_int off_li;
864 * These are offsets for the MTP3 fields.
866 static u_int off_sio;
867 static u_int off_opc;
868 static u_int off_dpc;
869 static u_int off_sls;
872 * This is the offset of the first byte after the ATM pseudo_header,
873 * or -1 if there is no ATM pseudo-header.
875 static u_int off_payload;
878 * These are offsets to the beginning of the network-layer header.
879 * They are relative to the beginning of the MAC-layer payload (i.e.,
880 * they don't include off_ll or off_macpl).
882 * If the link layer never uses 802.2 LLC:
884 * "off_nl" and "off_nl_nosnap" are the same.
886 * If the link layer always uses 802.2 LLC:
888 * "off_nl" is the offset if there's a SNAP header following
889 * the 802.2 header;
891 * "off_nl_nosnap" is the offset if there's no SNAP header.
893 * If the link layer is Ethernet:
895 * "off_nl" is the offset if the packet is an Ethernet II packet
896 * (we assume no 802.3+802.2+SNAP);
898 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
899 * with an 802.2 header following it.
901 static u_int off_nl;
902 static u_int off_nl_nosnap;
904 static int linktype;
906 static void
907 init_linktype(p)
908 pcap_t *p;
910 linktype = pcap_datalink(p);
911 #ifdef PCAP_FDDIPAD
912 pcap_fddipad = p->fddipad;
913 #endif
916 * Assume it's not raw ATM with a pseudo-header, for now.
918 off_mac = 0;
919 is_atm = 0;
920 is_lane = 0;
921 off_vpi = -1;
922 off_vci = -1;
923 off_proto = -1;
924 off_payload = -1;
927 * And that we're not doing PPPoE.
929 is_pppoes = 0;
932 * And assume we're not doing SS7.
934 off_li = -1;
935 off_sio = -1;
936 off_opc = -1;
937 off_dpc = -1;
938 off_sls = -1;
941 * Also assume it's not 802.11.
943 off_ll = 0;
944 off_macpl = 0;
945 off_macpl_is_variable = 0;
947 orig_linktype = -1;
948 orig_nl = -1;
949 label_stack_depth = 0;
951 reg_off_ll = -1;
952 reg_off_macpl = -1;
954 switch (linktype) {
956 case DLT_ARCNET:
957 off_linktype = 2;
958 off_macpl = 6;
959 off_nl = 0; /* XXX in reality, variable! */
960 off_nl_nosnap = 0; /* no 802.2 LLC */
961 return;
963 case DLT_ARCNET_LINUX:
964 off_linktype = 4;
965 off_macpl = 8;
966 off_nl = 0; /* XXX in reality, variable! */
967 off_nl_nosnap = 0; /* no 802.2 LLC */
968 return;
970 case DLT_EN10MB:
971 off_linktype = 12;
972 off_macpl = 14; /* Ethernet header length */
973 off_nl = 0; /* Ethernet II */
974 off_nl_nosnap = 3; /* 802.3+802.2 */
975 return;
977 case DLT_SLIP:
979 * SLIP doesn't have a link level type. The 16 byte
980 * header is hacked into our SLIP driver.
982 off_linktype = -1;
983 off_macpl = 16;
984 off_nl = 0;
985 off_nl_nosnap = 0; /* no 802.2 LLC */
986 return;
988 case DLT_SLIP_BSDOS:
989 /* XXX this may be the same as the DLT_PPP_BSDOS case */
990 off_linktype = -1;
991 /* XXX end */
992 off_macpl = 24;
993 off_nl = 0;
994 off_nl_nosnap = 0; /* no 802.2 LLC */
995 return;
997 case DLT_NULL:
998 case DLT_LOOP:
999 off_linktype = 0;
1000 off_macpl = 4;
1001 off_nl = 0;
1002 off_nl_nosnap = 0; /* no 802.2 LLC */
1003 return;
1005 case DLT_ENC:
1006 off_linktype = 0;
1007 off_macpl = 12;
1008 off_nl = 0;
1009 off_nl_nosnap = 0; /* no 802.2 LLC */
1010 return;
1012 case DLT_PPP:
1013 case DLT_PPP_PPPD:
1014 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1015 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1016 off_linktype = 2;
1017 off_macpl = 4;
1018 off_nl = 0;
1019 off_nl_nosnap = 0; /* no 802.2 LLC */
1020 return;
1022 case DLT_PPP_ETHER:
1024 * This does no include the Ethernet header, and
1025 * only covers session state.
1027 off_linktype = 6;
1028 off_macpl = 8;
1029 off_nl = 0;
1030 off_nl_nosnap = 0; /* no 802.2 LLC */
1031 return;
1033 case DLT_PPP_BSDOS:
1034 off_linktype = 5;
1035 off_macpl = 24;
1036 off_nl = 0;
1037 off_nl_nosnap = 0; /* no 802.2 LLC */
1038 return;
1040 case DLT_FDDI:
1042 * FDDI doesn't really have a link-level type field.
1043 * We set "off_linktype" to the offset of the LLC header.
1045 * To check for Ethernet types, we assume that SSAP = SNAP
1046 * is being used and pick out the encapsulated Ethernet type.
1047 * XXX - should we generate code to check for SNAP?
1049 off_linktype = 13;
1050 #ifdef PCAP_FDDIPAD
1051 off_linktype += pcap_fddipad;
1052 #endif
1053 off_macpl = 13; /* FDDI MAC header length */
1054 #ifdef PCAP_FDDIPAD
1055 off_macpl += pcap_fddipad;
1056 #endif
1057 off_nl = 8; /* 802.2+SNAP */
1058 off_nl_nosnap = 3; /* 802.2 */
1059 return;
1061 case DLT_IEEE802:
1063 * Token Ring doesn't really have a link-level type field.
1064 * We set "off_linktype" to the offset of the LLC header.
1066 * To check for Ethernet types, we assume that SSAP = SNAP
1067 * is being used and pick out the encapsulated Ethernet type.
1068 * XXX - should we generate code to check for SNAP?
1070 * XXX - the header is actually variable-length.
1071 * Some various Linux patched versions gave 38
1072 * as "off_linktype" and 40 as "off_nl"; however,
1073 * if a token ring packet has *no* routing
1074 * information, i.e. is not source-routed, the correct
1075 * values are 20 and 22, as they are in the vanilla code.
1077 * A packet is source-routed iff the uppermost bit
1078 * of the first byte of the source address, at an
1079 * offset of 8, has the uppermost bit set. If the
1080 * packet is source-routed, the total number of bytes
1081 * of routing information is 2 plus bits 0x1F00 of
1082 * the 16-bit value at an offset of 14 (shifted right
1083 * 8 - figure out which byte that is).
1085 off_linktype = 14;
1086 off_macpl = 14; /* Token Ring MAC header length */
1087 off_nl = 8; /* 802.2+SNAP */
1088 off_nl_nosnap = 3; /* 802.2 */
1089 return;
1091 case DLT_IEEE802_11:
1092 case DLT_PRISM_HEADER:
1093 case DLT_IEEE802_11_RADIO_AVS:
1094 case DLT_IEEE802_11_RADIO:
1096 * 802.11 doesn't really have a link-level type field.
1097 * We set "off_linktype" to the offset of the LLC header.
1099 * To check for Ethernet types, we assume that SSAP = SNAP
1100 * is being used and pick out the encapsulated Ethernet type.
1101 * XXX - should we generate code to check for SNAP?
1103 * We also handle variable-length radio headers here.
1104 * The Prism header is in theory variable-length, but in
1105 * practice it's always 144 bytes long. However, some
1106 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1107 * sometimes or always supply an AVS header, so we
1108 * have to check whether the radio header is a Prism
1109 * header or an AVS header, so, in practice, it's
1110 * variable-length.
1112 off_linktype = 24;
1113 off_macpl = 0; /* link-layer header is variable-length */
1114 off_macpl_is_variable = 1;
1115 off_nl = 8; /* 802.2+SNAP */
1116 off_nl_nosnap = 3; /* 802.2 */
1117 return;
1119 case DLT_PPI:
1121 * At the moment we treat PPI the same way that we treat
1122 * normal Radiotap encoded packets. The difference is in
1123 * the function that generates the code at the beginning
1124 * to compute the header length. Since this code generator
1125 * of PPI supports bare 802.11 encapsulation only (i.e.
1126 * the encapsulated DLT should be DLT_IEEE802_11) we
1127 * generate code to check for this too.
1129 off_linktype = 24;
1130 off_macpl = 0; /* link-layer header is variable-length */
1131 off_macpl_is_variable = 1;
1132 off_nl = 8; /* 802.2+SNAP */
1133 off_nl_nosnap = 3; /* 802.2 */
1134 return;
1136 case DLT_ATM_RFC1483:
1137 case DLT_ATM_CLIP: /* Linux ATM defines this */
1139 * assume routed, non-ISO PDUs
1140 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1142 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1143 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1144 * latter would presumably be treated the way PPPoE
1145 * should be, so you can do "pppoe and udp port 2049"
1146 * or "pppoa and tcp port 80" and have it check for
1147 * PPPo{A,E} and a PPP protocol of IP and....
1149 off_linktype = 0;
1150 off_macpl = 0; /* packet begins with LLC header */
1151 off_nl = 8; /* 802.2+SNAP */
1152 off_nl_nosnap = 3; /* 802.2 */
1153 return;
1155 case DLT_SUNATM:
1157 * Full Frontal ATM; you get AALn PDUs with an ATM
1158 * pseudo-header.
1160 is_atm = 1;
1161 off_vpi = SUNATM_VPI_POS;
1162 off_vci = SUNATM_VCI_POS;
1163 off_proto = PROTO_POS;
1164 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1165 off_payload = SUNATM_PKT_BEGIN_POS;
1166 off_linktype = off_payload;
1167 off_macpl = off_payload; /* if LLC-encapsulated */
1168 off_nl = 8; /* 802.2+SNAP */
1169 off_nl_nosnap = 3; /* 802.2 */
1170 return;
1172 case DLT_RAW:
1173 case DLT_IPV4:
1174 case DLT_IPV6:
1175 off_linktype = -1;
1176 off_macpl = 0;
1177 off_nl = 0;
1178 off_nl_nosnap = 0; /* no 802.2 LLC */
1179 return;
1181 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1182 off_linktype = 14;
1183 off_macpl = 16;
1184 off_nl = 0;
1185 off_nl_nosnap = 0; /* no 802.2 LLC */
1186 return;
1188 case DLT_LTALK:
1190 * LocalTalk does have a 1-byte type field in the LLAP header,
1191 * but really it just indicates whether there is a "short" or
1192 * "long" DDP packet following.
1194 off_linktype = -1;
1195 off_macpl = 0;
1196 off_nl = 0;
1197 off_nl_nosnap = 0; /* no 802.2 LLC */
1198 return;
1200 case DLT_IP_OVER_FC:
1202 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1203 * link-level type field. We set "off_linktype" to the
1204 * offset of the LLC header.
1206 * To check for Ethernet types, we assume that SSAP = SNAP
1207 * is being used and pick out the encapsulated Ethernet type.
1208 * XXX - should we generate code to check for SNAP? RFC
1209 * 2625 says SNAP should be used.
1211 off_linktype = 16;
1212 off_macpl = 16;
1213 off_nl = 8; /* 802.2+SNAP */
1214 off_nl_nosnap = 3; /* 802.2 */
1215 return;
1217 case DLT_FRELAY:
1219 * XXX - we should set this to handle SNAP-encapsulated
1220 * frames (NLPID of 0x80).
1222 off_linktype = -1;
1223 off_macpl = 0;
1224 off_nl = 0;
1225 off_nl_nosnap = 0; /* no 802.2 LLC */
1226 return;
1229 * the only BPF-interesting FRF.16 frames are non-control frames;
1230 * Frame Relay has a variable length link-layer
1231 * so lets start with offset 4 for now and increments later on (FIXME);
1233 case DLT_MFR:
1234 off_linktype = -1;
1235 off_macpl = 0;
1236 off_nl = 4;
1237 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1238 return;
1240 case DLT_APPLE_IP_OVER_IEEE1394:
1241 off_linktype = 16;
1242 off_macpl = 18;
1243 off_nl = 0;
1244 off_nl_nosnap = 0; /* no 802.2 LLC */
1245 return;
1247 case DLT_SYMANTEC_FIREWALL:
1248 off_linktype = 6;
1249 off_macpl = 44;
1250 off_nl = 0; /* Ethernet II */
1251 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1252 return;
1254 #ifdef HAVE_NET_PFVAR_H
1255 case DLT_PFLOG:
1256 off_linktype = 0;
1257 off_macpl = PFLOG_HDRLEN;
1258 off_nl = 0;
1259 off_nl_nosnap = 0; /* no 802.2 LLC */
1260 return;
1261 #endif
1263 case DLT_JUNIPER_MFR:
1264 case DLT_JUNIPER_MLFR:
1265 case DLT_JUNIPER_MLPPP:
1266 case DLT_JUNIPER_PPP:
1267 case DLT_JUNIPER_CHDLC:
1268 case DLT_JUNIPER_FRELAY:
1269 off_linktype = 4;
1270 off_macpl = 4;
1271 off_nl = 0;
1272 off_nl_nosnap = -1; /* no 802.2 LLC */
1273 return;
1275 case DLT_JUNIPER_ATM1:
1276 off_linktype = 4; /* in reality variable between 4-8 */
1277 off_macpl = 4; /* in reality variable between 4-8 */
1278 off_nl = 0;
1279 off_nl_nosnap = 10;
1280 return;
1282 case DLT_JUNIPER_ATM2:
1283 off_linktype = 8; /* in reality variable between 8-12 */
1284 off_macpl = 8; /* in reality variable between 8-12 */
1285 off_nl = 0;
1286 off_nl_nosnap = 10;
1287 return;
1289 /* frames captured on a Juniper PPPoE service PIC
1290 * contain raw ethernet frames */
1291 case DLT_JUNIPER_PPPOE:
1292 case DLT_JUNIPER_ETHER:
1293 off_macpl = 14;
1294 off_linktype = 16;
1295 off_nl = 18; /* Ethernet II */
1296 off_nl_nosnap = 21; /* 802.3+802.2 */
1297 return;
1299 case DLT_JUNIPER_PPPOE_ATM:
1300 off_linktype = 4;
1301 off_macpl = 6;
1302 off_nl = 0;
1303 off_nl_nosnap = -1; /* no 802.2 LLC */
1304 return;
1306 case DLT_JUNIPER_GGSN:
1307 off_linktype = 6;
1308 off_macpl = 12;
1309 off_nl = 0;
1310 off_nl_nosnap = -1; /* no 802.2 LLC */
1311 return;
1313 case DLT_JUNIPER_ES:
1314 off_linktype = 6;
1315 off_macpl = -1; /* not really a network layer but raw IP addresses */
1316 off_nl = -1; /* not really a network layer but raw IP addresses */
1317 off_nl_nosnap = -1; /* no 802.2 LLC */
1318 return;
1320 case DLT_JUNIPER_MONITOR:
1321 off_linktype = 12;
1322 off_macpl = 12;
1323 off_nl = 0; /* raw IP/IP6 header */
1324 off_nl_nosnap = -1; /* no 802.2 LLC */
1325 return;
1327 case DLT_JUNIPER_SERVICES:
1328 off_linktype = 12;
1329 off_macpl = -1; /* L3 proto location dep. on cookie type */
1330 off_nl = -1; /* L3 proto location dep. on cookie type */
1331 off_nl_nosnap = -1; /* no 802.2 LLC */
1332 return;
1334 case DLT_JUNIPER_VP:
1335 off_linktype = 18;
1336 off_macpl = -1;
1337 off_nl = -1;
1338 off_nl_nosnap = -1;
1339 return;
1341 case DLT_JUNIPER_ST:
1342 off_linktype = 18;
1343 off_macpl = -1;
1344 off_nl = -1;
1345 off_nl_nosnap = -1;
1346 return;
1348 case DLT_JUNIPER_ISM:
1349 off_linktype = 8;
1350 off_macpl = -1;
1351 off_nl = -1;
1352 off_nl_nosnap = -1;
1353 return;
1355 case DLT_JUNIPER_VS:
1356 case DLT_JUNIPER_SRX_E2E:
1357 case DLT_JUNIPER_FIBRECHANNEL:
1358 case DLT_JUNIPER_ATM_CEMIC:
1359 off_linktype = 8;
1360 off_macpl = -1;
1361 off_nl = -1;
1362 off_nl_nosnap = -1;
1363 return;
1365 case DLT_MTP2:
1366 off_li = 2;
1367 off_sio = 3;
1368 off_opc = 4;
1369 off_dpc = 4;
1370 off_sls = 7;
1371 off_linktype = -1;
1372 off_macpl = -1;
1373 off_nl = -1;
1374 off_nl_nosnap = -1;
1375 return;
1377 case DLT_MTP2_WITH_PHDR:
1378 off_li = 6;
1379 off_sio = 7;
1380 off_opc = 8;
1381 off_dpc = 8;
1382 off_sls = 11;
1383 off_linktype = -1;
1384 off_macpl = -1;
1385 off_nl = -1;
1386 off_nl_nosnap = -1;
1387 return;
1389 case DLT_ERF:
1390 off_li = 22;
1391 off_sio = 23;
1392 off_opc = 24;
1393 off_dpc = 24;
1394 off_sls = 27;
1395 off_linktype = -1;
1396 off_macpl = -1;
1397 off_nl = -1;
1398 off_nl_nosnap = -1;
1399 return;
1401 case DLT_PFSYNC:
1402 off_linktype = -1;
1403 off_macpl = 4;
1404 off_nl = 0;
1405 off_nl_nosnap = 0;
1406 return;
1408 case DLT_AX25_KISS:
1410 * Currently, only raw "link[N:M]" filtering is supported.
1412 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1413 off_macpl = -1;
1414 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1415 off_nl_nosnap = -1; /* no 802.2 LLC */
1416 off_mac = 1; /* step over the kiss length byte */
1417 return;
1419 case DLT_IPNET:
1420 off_linktype = 1;
1421 off_macpl = 24; /* ipnet header length */
1422 off_nl = 0;
1423 off_nl_nosnap = -1;
1424 return;
1426 case DLT_NETANALYZER:
1427 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1428 off_linktype = 16; /* includes 4-byte pseudo-header */
1429 off_macpl = 18; /* pseudo-header+Ethernet header length */
1430 off_nl = 0; /* Ethernet II */
1431 off_nl_nosnap = 3; /* 802.3+802.2 */
1432 return;
1434 case DLT_NETANALYZER_TRANSPARENT:
1435 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1436 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1437 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1438 off_nl = 0; /* Ethernet II */
1439 off_nl_nosnap = 3; /* 802.3+802.2 */
1440 return;
1442 default:
1444 * For values in the range in which we've assigned new
1445 * DLT_ values, only raw "link[N:M]" filtering is supported.
1447 if (linktype >= DLT_MATCHING_MIN &&
1448 linktype <= DLT_MATCHING_MAX) {
1449 off_linktype = -1;
1450 off_macpl = -1;
1451 off_nl = -1;
1452 off_nl_nosnap = -1;
1453 return;
1457 bpf_error("unknown data link type %d", linktype);
1458 /* NOTREACHED */
1462 * Load a value relative to the beginning of the link-layer header.
1463 * The link-layer header doesn't necessarily begin at the beginning
1464 * of the packet data; there might be a variable-length prefix containing
1465 * radio information.
1467 static struct slist *
1468 gen_load_llrel(offset, size)
1469 u_int offset, size;
1471 struct slist *s, *s2;
1473 s = gen_llprefixlen();
1476 * If "s" is non-null, it has code to arrange that the X register
1477 * contains the length of the prefix preceding the link-layer
1478 * header.
1480 * Otherwise, the length of the prefix preceding the link-layer
1481 * header is "off_ll".
1483 if (s != NULL) {
1485 * There's a variable-length prefix preceding the
1486 * link-layer header. "s" points to a list of statements
1487 * that put the length of that prefix into the X register.
1488 * do an indirect load, to use the X register as an offset.
1490 s2 = new_stmt(BPF_LD|BPF_IND|size);
1491 s2->s.k = offset;
1492 sappend(s, s2);
1493 } else {
1495 * There is no variable-length header preceding the
1496 * link-layer header; add in off_ll, which, if there's
1497 * a fixed-length header preceding the link-layer header,
1498 * is the length of that header.
1500 s = new_stmt(BPF_LD|BPF_ABS|size);
1501 s->s.k = offset + off_ll;
1503 return s;
1507 * Load a value relative to the beginning of the MAC-layer payload.
1509 static struct slist *
1510 gen_load_macplrel(offset, size)
1511 u_int offset, size;
1513 struct slist *s, *s2;
1515 s = gen_off_macpl();
1518 * If s is non-null, the offset of the MAC-layer payload is
1519 * variable, and s points to a list of instructions that
1520 * arrange that the X register contains that offset.
1522 * Otherwise, the offset of the MAC-layer payload is constant,
1523 * and is in off_macpl.
1525 if (s != NULL) {
1527 * The offset of the MAC-layer payload is in the X
1528 * register. Do an indirect load, to use the X register
1529 * as an offset.
1531 s2 = new_stmt(BPF_LD|BPF_IND|size);
1532 s2->s.k = offset;
1533 sappend(s, s2);
1534 } else {
1536 * The offset of the MAC-layer payload is constant,
1537 * and is in off_macpl; load the value at that offset
1538 * plus the specified offset.
1540 s = new_stmt(BPF_LD|BPF_ABS|size);
1541 s->s.k = off_macpl + offset;
1543 return s;
1547 * Load a value relative to the beginning of the specified header.
1549 static struct slist *
1550 gen_load_a(offrel, offset, size)
1551 enum e_offrel offrel;
1552 u_int offset, size;
1554 struct slist *s, *s2;
1556 switch (offrel) {
1558 case OR_PACKET:
1559 s = new_stmt(BPF_LD|BPF_ABS|size);
1560 s->s.k = offset;
1561 break;
1563 case OR_LINK:
1564 s = gen_load_llrel(offset, size);
1565 break;
1567 case OR_MACPL:
1568 s = gen_load_macplrel(offset, size);
1569 break;
1571 case OR_NET:
1572 s = gen_load_macplrel(off_nl + offset, size);
1573 break;
1575 case OR_NET_NOSNAP:
1576 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1577 break;
1579 case OR_TRAN_IPV4:
1581 * Load the X register with the length of the IPv4 header
1582 * (plus the offset of the link-layer header, if it's
1583 * preceded by a variable-length header such as a radio
1584 * header), in bytes.
1586 s = gen_loadx_iphdrlen();
1589 * Load the item at {offset of the MAC-layer payload} +
1590 * {offset, relative to the start of the MAC-layer
1591 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1592 * {specified offset}.
1594 * (If the offset of the MAC-layer payload is variable,
1595 * it's included in the value in the X register, and
1596 * off_macpl is 0.)
1598 s2 = new_stmt(BPF_LD|BPF_IND|size);
1599 s2->s.k = off_macpl + off_nl + offset;
1600 sappend(s, s2);
1601 break;
1603 case OR_TRAN_IPV6:
1604 s = gen_load_macplrel(off_nl + 40 + offset, size);
1605 break;
1607 default:
1608 abort();
1609 return NULL;
1611 return s;
1615 * Generate code to load into the X register the sum of the length of
1616 * the IPv4 header and any variable-length header preceding the link-layer
1617 * header.
1619 static struct slist *
1620 gen_loadx_iphdrlen()
1622 struct slist *s, *s2;
1624 s = gen_off_macpl();
1625 if (s != NULL) {
1627 * There's a variable-length prefix preceding the
1628 * link-layer header, or the link-layer header is itself
1629 * variable-length. "s" points to a list of statements
1630 * that put the offset of the MAC-layer payload into
1631 * the X register.
1633 * The 4*([k]&0xf) addressing mode can't be used, as we
1634 * don't have a constant offset, so we have to load the
1635 * value in question into the A register and add to it
1636 * the value from the X register.
1638 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1639 s2->s.k = off_nl;
1640 sappend(s, s2);
1641 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1642 s2->s.k = 0xf;
1643 sappend(s, s2);
1644 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1645 s2->s.k = 2;
1646 sappend(s, s2);
1649 * The A register now contains the length of the
1650 * IP header. We need to add to it the offset of
1651 * the MAC-layer payload, which is still in the X
1652 * register, and move the result into the X register.
1654 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1655 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1656 } else {
1658 * There is no variable-length header preceding the
1659 * link-layer header, and the link-layer header is
1660 * fixed-length; load the length of the IPv4 header,
1661 * which is at an offset of off_nl from the beginning
1662 * of the MAC-layer payload, and thus at an offset
1663 * of off_mac_pl + off_nl from the beginning of the
1664 * raw packet data.
1666 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1667 s->s.k = off_macpl + off_nl;
1669 return s;
1672 static struct block *
1673 gen_uncond(rsense)
1674 int rsense;
1676 struct block *b;
1677 struct slist *s;
1679 s = new_stmt(BPF_LD|BPF_IMM);
1680 s->s.k = !rsense;
1681 b = new_block(JMP(BPF_JEQ));
1682 b->stmts = s;
1684 return b;
1687 static inline struct block *
1688 gen_true()
1690 return gen_uncond(1);
1693 static inline struct block *
1694 gen_false()
1696 return gen_uncond(0);
1700 * Byte-swap a 32-bit number.
1701 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1702 * big-endian platforms.)
1704 #define SWAPLONG(y) \
1705 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1708 * Generate code to match a particular packet type.
1710 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1711 * value, if <= ETHERMTU. We use that to determine whether to
1712 * match the type/length field or to check the type/length field for
1713 * a value <= ETHERMTU to see whether it's a type field and then do
1714 * the appropriate test.
1716 static struct block *
1717 gen_ether_linktype(proto)
1718 register int proto;
1720 struct block *b0, *b1;
1722 switch (proto) {
1724 case LLCSAP_ISONS:
1725 case LLCSAP_IP:
1726 case LLCSAP_NETBEUI:
1728 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1729 * so we check the DSAP and SSAP.
1731 * LLCSAP_IP checks for IP-over-802.2, rather
1732 * than IP-over-Ethernet or IP-over-SNAP.
1734 * XXX - should we check both the DSAP and the
1735 * SSAP, like this, or should we check just the
1736 * DSAP, as we do for other types <= ETHERMTU
1737 * (i.e., other SAP values)?
1739 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1740 gen_not(b0);
1741 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1742 ((proto << 8) | proto));
1743 gen_and(b0, b1);
1744 return b1;
1746 case LLCSAP_IPX:
1748 * Check for;
1750 * Ethernet_II frames, which are Ethernet
1751 * frames with a frame type of ETHERTYPE_IPX;
1753 * Ethernet_802.3 frames, which are 802.3
1754 * frames (i.e., the type/length field is
1755 * a length field, <= ETHERMTU, rather than
1756 * a type field) with the first two bytes
1757 * after the Ethernet/802.3 header being
1758 * 0xFFFF;
1760 * Ethernet_802.2 frames, which are 802.3
1761 * frames with an 802.2 LLC header and
1762 * with the IPX LSAP as the DSAP in the LLC
1763 * header;
1765 * Ethernet_SNAP frames, which are 802.3
1766 * frames with an LLC header and a SNAP
1767 * header and with an OUI of 0x000000
1768 * (encapsulated Ethernet) and a protocol
1769 * ID of ETHERTYPE_IPX in the SNAP header.
1771 * XXX - should we generate the same code both
1772 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1776 * This generates code to check both for the
1777 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1779 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1780 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1781 gen_or(b0, b1);
1784 * Now we add code to check for SNAP frames with
1785 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1787 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1788 gen_or(b0, b1);
1791 * Now we generate code to check for 802.3
1792 * frames in general.
1794 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1795 gen_not(b0);
1798 * Now add the check for 802.3 frames before the
1799 * check for Ethernet_802.2 and Ethernet_802.3,
1800 * as those checks should only be done on 802.3
1801 * frames, not on Ethernet frames.
1803 gen_and(b0, b1);
1806 * Now add the check for Ethernet_II frames, and
1807 * do that before checking for the other frame
1808 * types.
1810 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1811 (bpf_int32)ETHERTYPE_IPX);
1812 gen_or(b0, b1);
1813 return b1;
1815 case ETHERTYPE_ATALK:
1816 case ETHERTYPE_AARP:
1818 * EtherTalk (AppleTalk protocols on Ethernet link
1819 * layer) may use 802.2 encapsulation.
1823 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1824 * we check for an Ethernet type field less than
1825 * 1500, which means it's an 802.3 length field.
1827 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1828 gen_not(b0);
1831 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1832 * SNAP packets with an organization code of
1833 * 0x080007 (Apple, for Appletalk) and a protocol
1834 * type of ETHERTYPE_ATALK (Appletalk).
1836 * 802.2-encapsulated ETHERTYPE_AARP packets are
1837 * SNAP packets with an organization code of
1838 * 0x000000 (encapsulated Ethernet) and a protocol
1839 * type of ETHERTYPE_AARP (Appletalk ARP).
1841 if (proto == ETHERTYPE_ATALK)
1842 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1843 else /* proto == ETHERTYPE_AARP */
1844 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1845 gen_and(b0, b1);
1848 * Check for Ethernet encapsulation (Ethertalk
1849 * phase 1?); we just check for the Ethernet
1850 * protocol type.
1852 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1854 gen_or(b0, b1);
1855 return b1;
1857 default:
1858 if (proto <= ETHERMTU) {
1860 * This is an LLC SAP value, so the frames
1861 * that match would be 802.2 frames.
1862 * Check that the frame is an 802.2 frame
1863 * (i.e., that the length/type field is
1864 * a length field, <= ETHERMTU) and
1865 * then check the DSAP.
1867 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1868 gen_not(b0);
1869 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1870 (bpf_int32)proto);
1871 gen_and(b0, b1);
1872 return b1;
1873 } else {
1875 * This is an Ethernet type, so compare
1876 * the length/type field with it (if
1877 * the frame is an 802.2 frame, the length
1878 * field will be <= ETHERMTU, and, as
1879 * "proto" is > ETHERMTU, this test
1880 * will fail and the frame won't match,
1881 * which is what we want).
1883 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1884 (bpf_int32)proto);
1890 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1891 * or IPv6 then we have an error.
1893 static struct block *
1894 gen_ipnet_linktype(proto)
1895 register int proto;
1897 switch (proto) {
1899 case ETHERTYPE_IP:
1900 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1901 (bpf_int32)IPH_AF_INET);
1902 /* NOTREACHED */
1904 case ETHERTYPE_IPV6:
1905 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1906 (bpf_int32)IPH_AF_INET6);
1907 /* NOTREACHED */
1909 default:
1910 break;
1913 return gen_false();
1917 * Generate code to match a particular packet type.
1919 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1920 * value, if <= ETHERMTU. We use that to determine whether to
1921 * match the type field or to check the type field for the special
1922 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1924 static struct block *
1925 gen_linux_sll_linktype(proto)
1926 register int proto;
1928 struct block *b0, *b1;
1930 switch (proto) {
1932 case LLCSAP_ISONS:
1933 case LLCSAP_IP:
1934 case LLCSAP_NETBEUI:
1936 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1937 * so we check the DSAP and SSAP.
1939 * LLCSAP_IP checks for IP-over-802.2, rather
1940 * than IP-over-Ethernet or IP-over-SNAP.
1942 * XXX - should we check both the DSAP and the
1943 * SSAP, like this, or should we check just the
1944 * DSAP, as we do for other types <= ETHERMTU
1945 * (i.e., other SAP values)?
1947 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1948 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1949 ((proto << 8) | proto));
1950 gen_and(b0, b1);
1951 return b1;
1953 case LLCSAP_IPX:
1955 * Ethernet_II frames, which are Ethernet
1956 * frames with a frame type of ETHERTYPE_IPX;
1958 * Ethernet_802.3 frames, which have a frame
1959 * type of LINUX_SLL_P_802_3;
1961 * Ethernet_802.2 frames, which are 802.3
1962 * frames with an 802.2 LLC header (i.e, have
1963 * a frame type of LINUX_SLL_P_802_2) and
1964 * with the IPX LSAP as the DSAP in the LLC
1965 * header;
1967 * Ethernet_SNAP frames, which are 802.3
1968 * frames with an LLC header and a SNAP
1969 * header and with an OUI of 0x000000
1970 * (encapsulated Ethernet) and a protocol
1971 * ID of ETHERTYPE_IPX in the SNAP header.
1973 * First, do the checks on LINUX_SLL_P_802_2
1974 * frames; generate the check for either
1975 * Ethernet_802.2 or Ethernet_SNAP frames, and
1976 * then put a check for LINUX_SLL_P_802_2 frames
1977 * before it.
1979 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1980 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
1981 gen_or(b0, b1);
1982 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1983 gen_and(b0, b1);
1986 * Now check for 802.3 frames and OR that with
1987 * the previous test.
1989 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
1990 gen_or(b0, b1);
1993 * Now add the check for Ethernet_II frames, and
1994 * do that before checking for the other frame
1995 * types.
1997 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1998 (bpf_int32)ETHERTYPE_IPX);
1999 gen_or(b0, b1);
2000 return b1;
2002 case ETHERTYPE_ATALK:
2003 case ETHERTYPE_AARP:
2005 * EtherTalk (AppleTalk protocols on Ethernet link
2006 * layer) may use 802.2 encapsulation.
2010 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2011 * we check for the 802.2 protocol type in the
2012 * "Ethernet type" field.
2014 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2017 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2018 * SNAP packets with an organization code of
2019 * 0x080007 (Apple, for Appletalk) and a protocol
2020 * type of ETHERTYPE_ATALK (Appletalk).
2022 * 802.2-encapsulated ETHERTYPE_AARP packets are
2023 * SNAP packets with an organization code of
2024 * 0x000000 (encapsulated Ethernet) and a protocol
2025 * type of ETHERTYPE_AARP (Appletalk ARP).
2027 if (proto == ETHERTYPE_ATALK)
2028 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2029 else /* proto == ETHERTYPE_AARP */
2030 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2031 gen_and(b0, b1);
2034 * Check for Ethernet encapsulation (Ethertalk
2035 * phase 1?); we just check for the Ethernet
2036 * protocol type.
2038 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2040 gen_or(b0, b1);
2041 return b1;
2043 default:
2044 if (proto <= ETHERMTU) {
2046 * This is an LLC SAP value, so the frames
2047 * that match would be 802.2 frames.
2048 * Check for the 802.2 protocol type
2049 * in the "Ethernet type" field, and
2050 * then check the DSAP.
2052 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2053 LINUX_SLL_P_802_2);
2054 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2055 (bpf_int32)proto);
2056 gen_and(b0, b1);
2057 return b1;
2058 } else {
2060 * This is an Ethernet type, so compare
2061 * the length/type field with it (if
2062 * the frame is an 802.2 frame, the length
2063 * field will be <= ETHERMTU, and, as
2064 * "proto" is > ETHERMTU, this test
2065 * will fail and the frame won't match,
2066 * which is what we want).
2068 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2069 (bpf_int32)proto);
2074 static struct slist *
2075 gen_load_prism_llprefixlen()
2077 struct slist *s1, *s2;
2078 struct slist *sjeq_avs_cookie;
2079 struct slist *sjcommon;
2082 * This code is not compatible with the optimizer, as
2083 * we are generating jmp instructions within a normal
2084 * slist of instructions
2086 no_optimize = 1;
2089 * Generate code to load the length of the radio header into
2090 * the register assigned to hold that length, if one has been
2091 * assigned. (If one hasn't been assigned, no code we've
2092 * generated uses that prefix, so we don't need to generate any
2093 * code to load it.)
2095 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2096 * or always use the AVS header rather than the Prism header.
2097 * We load a 4-byte big-endian value at the beginning of the
2098 * raw packet data, and see whether, when masked with 0xFFFFF000,
2099 * it's equal to 0x80211000. If so, that indicates that it's
2100 * an AVS header (the masked-out bits are the version number).
2101 * Otherwise, it's a Prism header.
2103 * XXX - the Prism header is also, in theory, variable-length,
2104 * but no known software generates headers that aren't 144
2105 * bytes long.
2107 if (reg_off_ll != -1) {
2109 * Load the cookie.
2111 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2112 s1->s.k = 0;
2115 * AND it with 0xFFFFF000.
2117 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2118 s2->s.k = 0xFFFFF000;
2119 sappend(s1, s2);
2122 * Compare with 0x80211000.
2124 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2125 sjeq_avs_cookie->s.k = 0x80211000;
2126 sappend(s1, sjeq_avs_cookie);
2129 * If it's AVS:
2131 * The 4 bytes at an offset of 4 from the beginning of
2132 * the AVS header are the length of the AVS header.
2133 * That field is big-endian.
2135 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2136 s2->s.k = 4;
2137 sappend(s1, s2);
2138 sjeq_avs_cookie->s.jt = s2;
2141 * Now jump to the code to allocate a register
2142 * into which to save the header length and
2143 * store the length there. (The "jump always"
2144 * instruction needs to have the k field set;
2145 * it's added to the PC, so, as we're jumping
2146 * over a single instruction, it should be 1.)
2148 sjcommon = new_stmt(JMP(BPF_JA));
2149 sjcommon->s.k = 1;
2150 sappend(s1, sjcommon);
2153 * Now for the code that handles the Prism header.
2154 * Just load the length of the Prism header (144)
2155 * into the A register. Have the test for an AVS
2156 * header branch here if we don't have an AVS header.
2158 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2159 s2->s.k = 144;
2160 sappend(s1, s2);
2161 sjeq_avs_cookie->s.jf = s2;
2164 * Now allocate a register to hold that value and store
2165 * it. The code for the AVS header will jump here after
2166 * loading the length of the AVS header.
2168 s2 = new_stmt(BPF_ST);
2169 s2->s.k = reg_off_ll;
2170 sappend(s1, s2);
2171 sjcommon->s.jf = s2;
2174 * Now move it into the X register.
2176 s2 = new_stmt(BPF_MISC|BPF_TAX);
2177 sappend(s1, s2);
2179 return (s1);
2180 } else
2181 return (NULL);
2184 static struct slist *
2185 gen_load_avs_llprefixlen()
2187 struct slist *s1, *s2;
2190 * Generate code to load the length of the AVS header into
2191 * the register assigned to hold that length, if one has been
2192 * assigned. (If one hasn't been assigned, no code we've
2193 * generated uses that prefix, so we don't need to generate any
2194 * code to load it.)
2196 if (reg_off_ll != -1) {
2198 * The 4 bytes at an offset of 4 from the beginning of
2199 * the AVS header are the length of the AVS header.
2200 * That field is big-endian.
2202 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2203 s1->s.k = 4;
2206 * Now allocate a register to hold that value and store
2207 * it.
2209 s2 = new_stmt(BPF_ST);
2210 s2->s.k = reg_off_ll;
2211 sappend(s1, s2);
2214 * Now move it into the X register.
2216 s2 = new_stmt(BPF_MISC|BPF_TAX);
2217 sappend(s1, s2);
2219 return (s1);
2220 } else
2221 return (NULL);
2224 static struct slist *
2225 gen_load_radiotap_llprefixlen()
2227 struct slist *s1, *s2;
2230 * Generate code to load the length of the radiotap header into
2231 * the register assigned to hold that length, if one has been
2232 * assigned. (If one hasn't been assigned, no code we've
2233 * generated uses that prefix, so we don't need to generate any
2234 * code to load it.)
2236 if (reg_off_ll != -1) {
2238 * The 2 bytes at offsets of 2 and 3 from the beginning
2239 * of the radiotap header are the length of the radiotap
2240 * header; unfortunately, it's little-endian, so we have
2241 * to load it a byte at a time and construct the value.
2245 * Load the high-order byte, at an offset of 3, shift it
2246 * left a byte, and put the result in the X register.
2248 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2249 s1->s.k = 3;
2250 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2251 sappend(s1, s2);
2252 s2->s.k = 8;
2253 s2 = new_stmt(BPF_MISC|BPF_TAX);
2254 sappend(s1, s2);
2257 * Load the next byte, at an offset of 2, and OR the
2258 * value from the X register into it.
2260 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2261 sappend(s1, s2);
2262 s2->s.k = 2;
2263 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2264 sappend(s1, s2);
2267 * Now allocate a register to hold that value and store
2268 * it.
2270 s2 = new_stmt(BPF_ST);
2271 s2->s.k = reg_off_ll;
2272 sappend(s1, s2);
2275 * Now move it into the X register.
2277 s2 = new_stmt(BPF_MISC|BPF_TAX);
2278 sappend(s1, s2);
2280 return (s1);
2281 } else
2282 return (NULL);
2286 * At the moment we treat PPI as normal Radiotap encoded
2287 * packets. The difference is in the function that generates
2288 * the code at the beginning to compute the header length.
2289 * Since this code generator of PPI supports bare 802.11
2290 * encapsulation only (i.e. the encapsulated DLT should be
2291 * DLT_IEEE802_11) we generate code to check for this too;
2292 * that's done in finish_parse().
2294 static struct slist *
2295 gen_load_ppi_llprefixlen()
2297 struct slist *s1, *s2;
2300 * Generate code to load the length of the radiotap header
2301 * into the register assigned to hold that length, if one has
2302 * been assigned.
2304 if (reg_off_ll != -1) {
2306 * The 2 bytes at offsets of 2 and 3 from the beginning
2307 * of the radiotap header are the length of the radiotap
2308 * header; unfortunately, it's little-endian, so we have
2309 * to load it a byte at a time and construct the value.
2313 * Load the high-order byte, at an offset of 3, shift it
2314 * left a byte, and put the result in the X register.
2316 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2317 s1->s.k = 3;
2318 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2319 sappend(s1, s2);
2320 s2->s.k = 8;
2321 s2 = new_stmt(BPF_MISC|BPF_TAX);
2322 sappend(s1, s2);
2325 * Load the next byte, at an offset of 2, and OR the
2326 * value from the X register into it.
2328 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2329 sappend(s1, s2);
2330 s2->s.k = 2;
2331 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2332 sappend(s1, s2);
2335 * Now allocate a register to hold that value and store
2336 * it.
2338 s2 = new_stmt(BPF_ST);
2339 s2->s.k = reg_off_ll;
2340 sappend(s1, s2);
2343 * Now move it into the X register.
2345 s2 = new_stmt(BPF_MISC|BPF_TAX);
2346 sappend(s1, s2);
2348 return (s1);
2349 } else
2350 return (NULL);
2354 * Load a value relative to the beginning of the link-layer header after the 802.11
2355 * header, i.e. LLC_SNAP.
2356 * The link-layer header doesn't necessarily begin at the beginning
2357 * of the packet data; there might be a variable-length prefix containing
2358 * radio information.
2360 static struct slist *
2361 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2363 struct slist *s2;
2364 struct slist *sjset_data_frame_1;
2365 struct slist *sjset_data_frame_2;
2366 struct slist *sjset_qos;
2367 struct slist *sjset_radiotap_flags;
2368 struct slist *sjset_radiotap_tsft;
2369 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2370 struct slist *s_roundup;
2372 if (reg_off_macpl == -1) {
2374 * No register has been assigned to the offset of
2375 * the MAC-layer payload, which means nobody needs
2376 * it; don't bother computing it - just return
2377 * what we already have.
2379 return (s);
2383 * This code is not compatible with the optimizer, as
2384 * we are generating jmp instructions within a normal
2385 * slist of instructions
2387 no_optimize = 1;
2390 * If "s" is non-null, it has code to arrange that the X register
2391 * contains the length of the prefix preceding the link-layer
2392 * header.
2394 * Otherwise, the length of the prefix preceding the link-layer
2395 * header is "off_ll".
2397 if (s == NULL) {
2399 * There is no variable-length header preceding the
2400 * link-layer header.
2402 * Load the length of the fixed-length prefix preceding
2403 * the link-layer header (if any) into the X register,
2404 * and store it in the reg_off_macpl register.
2405 * That length is off_ll.
2407 s = new_stmt(BPF_LDX|BPF_IMM);
2408 s->s.k = off_ll;
2412 * The X register contains the offset of the beginning of the
2413 * link-layer header; add 24, which is the minimum length
2414 * of the MAC header for a data frame, to that, and store it
2415 * in reg_off_macpl, and then load the Frame Control field,
2416 * which is at the offset in the X register, with an indexed load.
2418 s2 = new_stmt(BPF_MISC|BPF_TXA);
2419 sappend(s, s2);
2420 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2421 s2->s.k = 24;
2422 sappend(s, s2);
2423 s2 = new_stmt(BPF_ST);
2424 s2->s.k = reg_off_macpl;
2425 sappend(s, s2);
2427 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2428 s2->s.k = 0;
2429 sappend(s, s2);
2432 * Check the Frame Control field to see if this is a data frame;
2433 * a data frame has the 0x08 bit (b3) in that field set and the
2434 * 0x04 bit (b2) clear.
2436 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2437 sjset_data_frame_1->s.k = 0x08;
2438 sappend(s, sjset_data_frame_1);
2441 * If b3 is set, test b2, otherwise go to the first statement of
2442 * the rest of the program.
2444 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2445 sjset_data_frame_2->s.k = 0x04;
2446 sappend(s, sjset_data_frame_2);
2447 sjset_data_frame_1->s.jf = snext;
2450 * If b2 is not set, this is a data frame; test the QoS bit.
2451 * Otherwise, go to the first statement of the rest of the
2452 * program.
2454 sjset_data_frame_2->s.jt = snext;
2455 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2456 sjset_qos->s.k = 0x80; /* QoS bit */
2457 sappend(s, sjset_qos);
2460 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2461 * field.
2462 * Otherwise, go to the first statement of the rest of the
2463 * program.
2465 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2466 s2->s.k = reg_off_macpl;
2467 sappend(s, s2);
2468 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2469 s2->s.k = 2;
2470 sappend(s, s2);
2471 s2 = new_stmt(BPF_ST);
2472 s2->s.k = reg_off_macpl;
2473 sappend(s, s2);
2476 * If we have a radiotap header, look at it to see whether
2477 * there's Atheros padding between the MAC-layer header
2478 * and the payload.
2480 * Note: all of the fields in the radiotap header are
2481 * little-endian, so we byte-swap all of the values
2482 * we test against, as they will be loaded as big-endian
2483 * values.
2485 if (linktype == DLT_IEEE802_11_RADIO) {
2487 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2488 * in the presence flag?
2490 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2491 s2->s.k = 4;
2492 sappend(s, s2);
2494 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2495 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2496 sappend(s, sjset_radiotap_flags);
2499 * If not, skip all of this.
2501 sjset_radiotap_flags->s.jf = snext;
2504 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2506 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2507 new_stmt(JMP(BPF_JSET));
2508 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2509 sappend(s, sjset_radiotap_tsft);
2512 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2513 * at an offset of 16 from the beginning of the raw packet
2514 * data (8 bytes for the radiotap header and 8 bytes for
2515 * the TSFT field).
2517 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2518 * is set.
2520 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2521 s2->s.k = 16;
2522 sappend(s, s2);
2524 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2525 sjset_tsft_datapad->s.k = 0x20;
2526 sappend(s, sjset_tsft_datapad);
2529 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2530 * at an offset of 8 from the beginning of the raw packet
2531 * data (8 bytes for the radiotap header).
2533 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2534 * is set.
2536 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2537 s2->s.k = 8;
2538 sappend(s, s2);
2540 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2541 sjset_notsft_datapad->s.k = 0x20;
2542 sappend(s, sjset_notsft_datapad);
2545 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2546 * set, round the length of the 802.11 header to
2547 * a multiple of 4. Do that by adding 3 and then
2548 * dividing by and multiplying by 4, which we do by
2549 * ANDing with ~3.
2551 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2552 s_roundup->s.k = reg_off_macpl;
2553 sappend(s, s_roundup);
2554 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2555 s2->s.k = 3;
2556 sappend(s, s2);
2557 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2558 s2->s.k = ~3;
2559 sappend(s, s2);
2560 s2 = new_stmt(BPF_ST);
2561 s2->s.k = reg_off_macpl;
2562 sappend(s, s2);
2564 sjset_tsft_datapad->s.jt = s_roundup;
2565 sjset_tsft_datapad->s.jf = snext;
2566 sjset_notsft_datapad->s.jt = s_roundup;
2567 sjset_notsft_datapad->s.jf = snext;
2568 } else
2569 sjset_qos->s.jf = snext;
2571 return s;
2574 static void
2575 insert_compute_vloffsets(b)
2576 struct block *b;
2578 struct slist *s;
2581 * For link-layer types that have a variable-length header
2582 * preceding the link-layer header, generate code to load
2583 * the offset of the link-layer header into the register
2584 * assigned to that offset, if any.
2586 switch (linktype) {
2588 case DLT_PRISM_HEADER:
2589 s = gen_load_prism_llprefixlen();
2590 break;
2592 case DLT_IEEE802_11_RADIO_AVS:
2593 s = gen_load_avs_llprefixlen();
2594 break;
2596 case DLT_IEEE802_11_RADIO:
2597 s = gen_load_radiotap_llprefixlen();
2598 break;
2600 case DLT_PPI:
2601 s = gen_load_ppi_llprefixlen();
2602 break;
2604 default:
2605 s = NULL;
2606 break;
2610 * For link-layer types that have a variable-length link-layer
2611 * header, generate code to load the offset of the MAC-layer
2612 * payload into the register assigned to that offset, if any.
2614 switch (linktype) {
2616 case DLT_IEEE802_11:
2617 case DLT_PRISM_HEADER:
2618 case DLT_IEEE802_11_RADIO_AVS:
2619 case DLT_IEEE802_11_RADIO:
2620 case DLT_PPI:
2621 s = gen_load_802_11_header_len(s, b->stmts);
2622 break;
2626 * If we have any offset-loading code, append all the
2627 * existing statements in the block to those statements,
2628 * and make the resulting list the list of statements
2629 * for the block.
2631 if (s != NULL) {
2632 sappend(s, b->stmts);
2633 b->stmts = s;
2637 static struct block *
2638 gen_ppi_dlt_check(void)
2640 struct slist *s_load_dlt;
2641 struct block *b;
2643 if (linktype == DLT_PPI)
2645 /* Create the statements that check for the DLT
2647 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2648 s_load_dlt->s.k = 4;
2650 b = new_block(JMP(BPF_JEQ));
2652 b->stmts = s_load_dlt;
2653 b->s.k = SWAPLONG(DLT_IEEE802_11);
2655 else
2657 b = NULL;
2660 return b;
2663 static struct slist *
2664 gen_prism_llprefixlen(void)
2666 struct slist *s;
2668 if (reg_off_ll == -1) {
2670 * We haven't yet assigned a register for the length
2671 * of the radio header; allocate one.
2673 reg_off_ll = alloc_reg();
2677 * Load the register containing the radio length
2678 * into the X register.
2680 s = new_stmt(BPF_LDX|BPF_MEM);
2681 s->s.k = reg_off_ll;
2682 return s;
2685 static struct slist *
2686 gen_avs_llprefixlen(void)
2688 struct slist *s;
2690 if (reg_off_ll == -1) {
2692 * We haven't yet assigned a register for the length
2693 * of the AVS header; allocate one.
2695 reg_off_ll = alloc_reg();
2699 * Load the register containing the AVS length
2700 * into the X register.
2702 s = new_stmt(BPF_LDX|BPF_MEM);
2703 s->s.k = reg_off_ll;
2704 return s;
2707 static struct slist *
2708 gen_radiotap_llprefixlen(void)
2710 struct slist *s;
2712 if (reg_off_ll == -1) {
2714 * We haven't yet assigned a register for the length
2715 * of the radiotap header; allocate one.
2717 reg_off_ll = alloc_reg();
2721 * Load the register containing the radiotap length
2722 * into the X register.
2724 s = new_stmt(BPF_LDX|BPF_MEM);
2725 s->s.k = reg_off_ll;
2726 return s;
2730 * At the moment we treat PPI as normal Radiotap encoded
2731 * packets. The difference is in the function that generates
2732 * the code at the beginning to compute the header length.
2733 * Since this code generator of PPI supports bare 802.11
2734 * encapsulation only (i.e. the encapsulated DLT should be
2735 * DLT_IEEE802_11) we generate code to check for this too.
2737 static struct slist *
2738 gen_ppi_llprefixlen(void)
2740 struct slist *s;
2742 if (reg_off_ll == -1) {
2744 * We haven't yet assigned a register for the length
2745 * of the radiotap header; allocate one.
2747 reg_off_ll = alloc_reg();
2751 * Load the register containing the PPI length
2752 * into the X register.
2754 s = new_stmt(BPF_LDX|BPF_MEM);
2755 s->s.k = reg_off_ll;
2756 return s;
2760 * Generate code to compute the link-layer header length, if necessary,
2761 * putting it into the X register, and to return either a pointer to a
2762 * "struct slist" for the list of statements in that code, or NULL if
2763 * no code is necessary.
2765 static struct slist *
2766 gen_llprefixlen(void)
2768 switch (linktype) {
2770 case DLT_PRISM_HEADER:
2771 return gen_prism_llprefixlen();
2773 case DLT_IEEE802_11_RADIO_AVS:
2774 return gen_avs_llprefixlen();
2776 case DLT_IEEE802_11_RADIO:
2777 return gen_radiotap_llprefixlen();
2779 case DLT_PPI:
2780 return gen_ppi_llprefixlen();
2782 default:
2783 return NULL;
2788 * Generate code to load the register containing the offset of the
2789 * MAC-layer payload into the X register; if no register for that offset
2790 * has been allocated, allocate it first.
2792 static struct slist *
2793 gen_off_macpl(void)
2795 struct slist *s;
2797 if (off_macpl_is_variable) {
2798 if (reg_off_macpl == -1) {
2800 * We haven't yet assigned a register for the offset
2801 * of the MAC-layer payload; allocate one.
2803 reg_off_macpl = alloc_reg();
2807 * Load the register containing the offset of the MAC-layer
2808 * payload into the X register.
2810 s = new_stmt(BPF_LDX|BPF_MEM);
2811 s->s.k = reg_off_macpl;
2812 return s;
2813 } else {
2815 * That offset isn't variable, so we don't need to
2816 * generate any code.
2818 return NULL;
2823 * Map an Ethernet type to the equivalent PPP type.
2825 static int
2826 ethertype_to_ppptype(proto)
2827 int proto;
2829 switch (proto) {
2831 case ETHERTYPE_IP:
2832 proto = PPP_IP;
2833 break;
2835 #ifdef INET6
2836 case ETHERTYPE_IPV6:
2837 proto = PPP_IPV6;
2838 break;
2839 #endif
2841 case ETHERTYPE_DN:
2842 proto = PPP_DECNET;
2843 break;
2845 case ETHERTYPE_ATALK:
2846 proto = PPP_APPLE;
2847 break;
2849 case ETHERTYPE_NS:
2850 proto = PPP_NS;
2851 break;
2853 case LLCSAP_ISONS:
2854 proto = PPP_OSI;
2855 break;
2857 case LLCSAP_8021D:
2859 * I'm assuming the "Bridging PDU"s that go
2860 * over PPP are Spanning Tree Protocol
2861 * Bridging PDUs.
2863 proto = PPP_BRPDU;
2864 break;
2866 case LLCSAP_IPX:
2867 proto = PPP_IPX;
2868 break;
2870 return (proto);
2874 * Generate code to match a particular packet type by matching the
2875 * link-layer type field or fields in the 802.2 LLC header.
2877 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2878 * value, if <= ETHERMTU.
2880 static struct block *
2881 gen_linktype(proto)
2882 register int proto;
2884 struct block *b0, *b1, *b2;
2886 /* are we checking MPLS-encapsulated packets? */
2887 if (label_stack_depth > 0) {
2888 switch (proto) {
2889 case ETHERTYPE_IP:
2890 case PPP_IP:
2891 /* FIXME add other L3 proto IDs */
2892 return gen_mpls_linktype(Q_IP);
2894 case ETHERTYPE_IPV6:
2895 case PPP_IPV6:
2896 /* FIXME add other L3 proto IDs */
2897 return gen_mpls_linktype(Q_IPV6);
2899 default:
2900 bpf_error("unsupported protocol over mpls");
2901 /* NOTREACHED */
2906 * Are we testing PPPoE packets?
2908 if (is_pppoes) {
2910 * The PPPoE session header is part of the
2911 * MAC-layer payload, so all references
2912 * should be relative to the beginning of
2913 * that payload.
2917 * We use Ethernet protocol types inside libpcap;
2918 * map them to the corresponding PPP protocol types.
2920 proto = ethertype_to_ppptype(proto);
2921 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2924 switch (linktype) {
2926 case DLT_EN10MB:
2927 case DLT_NETANALYZER:
2928 case DLT_NETANALYZER_TRANSPARENT:
2929 return gen_ether_linktype(proto);
2930 /*NOTREACHED*/
2931 break;
2933 case DLT_C_HDLC:
2934 switch (proto) {
2936 case LLCSAP_ISONS:
2937 proto = (proto << 8 | LLCSAP_ISONS);
2938 /* fall through */
2940 default:
2941 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2942 (bpf_int32)proto);
2943 /*NOTREACHED*/
2944 break;
2946 break;
2948 case DLT_IEEE802_11:
2949 case DLT_PRISM_HEADER:
2950 case DLT_IEEE802_11_RADIO_AVS:
2951 case DLT_IEEE802_11_RADIO:
2952 case DLT_PPI:
2954 * Check that we have a data frame.
2956 b0 = gen_check_802_11_data_frame();
2959 * Now check for the specified link-layer type.
2961 b1 = gen_llc_linktype(proto);
2962 gen_and(b0, b1);
2963 return b1;
2964 /*NOTREACHED*/
2965 break;
2967 case DLT_FDDI:
2969 * XXX - check for asynchronous frames, as per RFC 1103.
2971 return gen_llc_linktype(proto);
2972 /*NOTREACHED*/
2973 break;
2975 case DLT_IEEE802:
2977 * XXX - check for LLC PDUs, as per IEEE 802.5.
2979 return gen_llc_linktype(proto);
2980 /*NOTREACHED*/
2981 break;
2983 case DLT_ATM_RFC1483:
2984 case DLT_ATM_CLIP:
2985 case DLT_IP_OVER_FC:
2986 return gen_llc_linktype(proto);
2987 /*NOTREACHED*/
2988 break;
2990 case DLT_SUNATM:
2992 * If "is_lane" is set, check for a LANE-encapsulated
2993 * version of this protocol, otherwise check for an
2994 * LLC-encapsulated version of this protocol.
2996 * We assume LANE means Ethernet, not Token Ring.
2998 if (is_lane) {
3000 * Check that the packet doesn't begin with an
3001 * LE Control marker. (We've already generated
3002 * a test for LANE.)
3004 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3005 0xFF00);
3006 gen_not(b0);
3009 * Now generate an Ethernet test.
3011 b1 = gen_ether_linktype(proto);
3012 gen_and(b0, b1);
3013 return b1;
3014 } else {
3016 * Check for LLC encapsulation and then check the
3017 * protocol.
3019 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3020 b1 = gen_llc_linktype(proto);
3021 gen_and(b0, b1);
3022 return b1;
3024 /*NOTREACHED*/
3025 break;
3027 case DLT_LINUX_SLL:
3028 return gen_linux_sll_linktype(proto);
3029 /*NOTREACHED*/
3030 break;
3032 case DLT_SLIP:
3033 case DLT_SLIP_BSDOS:
3034 case DLT_RAW:
3036 * These types don't provide any type field; packets
3037 * are always IPv4 or IPv6.
3039 * XXX - for IPv4, check for a version number of 4, and,
3040 * for IPv6, check for a version number of 6?
3042 switch (proto) {
3044 case ETHERTYPE_IP:
3045 /* Check for a version number of 4. */
3046 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3047 #ifdef INET6
3048 case ETHERTYPE_IPV6:
3049 /* Check for a version number of 6. */
3050 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3051 #endif
3053 default:
3054 return gen_false(); /* always false */
3056 /*NOTREACHED*/
3057 break;
3059 case DLT_IPV4:
3061 * Raw IPv4, so no type field.
3063 if (proto == ETHERTYPE_IP)
3064 return gen_true(); /* always true */
3066 /* Checking for something other than IPv4; always false */
3067 return gen_false();
3068 /*NOTREACHED*/
3069 break;
3071 case DLT_IPV6:
3073 * Raw IPv6, so no type field.
3075 #ifdef INET6
3076 if (proto == ETHERTYPE_IPV6)
3077 return gen_true(); /* always true */
3078 #endif
3080 /* Checking for something other than IPv6; always false */
3081 return gen_false();
3082 /*NOTREACHED*/
3083 break;
3085 case DLT_PPP:
3086 case DLT_PPP_PPPD:
3087 case DLT_PPP_SERIAL:
3088 case DLT_PPP_ETHER:
3090 * We use Ethernet protocol types inside libpcap;
3091 * map them to the corresponding PPP protocol types.
3093 proto = ethertype_to_ppptype(proto);
3094 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3095 /*NOTREACHED*/
3096 break;
3098 case DLT_PPP_BSDOS:
3100 * We use Ethernet protocol types inside libpcap;
3101 * map them to the corresponding PPP protocol types.
3103 switch (proto) {
3105 case ETHERTYPE_IP:
3107 * Also check for Van Jacobson-compressed IP.
3108 * XXX - do this for other forms of PPP?
3110 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3111 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3112 gen_or(b0, b1);
3113 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3114 gen_or(b1, b0);
3115 return b0;
3117 default:
3118 proto = ethertype_to_ppptype(proto);
3119 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3120 (bpf_int32)proto);
3122 /*NOTREACHED*/
3123 break;
3125 case DLT_NULL:
3126 case DLT_LOOP:
3127 case DLT_ENC:
3129 * For DLT_NULL, the link-layer header is a 32-bit
3130 * word containing an AF_ value in *host* byte order,
3131 * and for DLT_ENC, the link-layer header begins
3132 * with a 32-bit work containing an AF_ value in
3133 * host byte order.
3135 * In addition, if we're reading a saved capture file,
3136 * the host byte order in the capture may not be the
3137 * same as the host byte order on this machine.
3139 * For DLT_LOOP, the link-layer header is a 32-bit
3140 * word containing an AF_ value in *network* byte order.
3142 * XXX - AF_ values may, unfortunately, be platform-
3143 * dependent; for example, FreeBSD's AF_INET6 is 24
3144 * whilst NetBSD's and OpenBSD's is 26.
3146 * This means that, when reading a capture file, just
3147 * checking for our AF_INET6 value won't work if the
3148 * capture file came from another OS.
3150 switch (proto) {
3152 case ETHERTYPE_IP:
3153 proto = AF_INET;
3154 break;
3156 #ifdef INET6
3157 case ETHERTYPE_IPV6:
3158 proto = AF_INET6;
3159 break;
3160 #endif
3162 default:
3164 * Not a type on which we support filtering.
3165 * XXX - support those that have AF_ values
3166 * #defined on this platform, at least?
3168 return gen_false();
3171 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3173 * The AF_ value is in host byte order, but
3174 * the BPF interpreter will convert it to
3175 * network byte order.
3177 * If this is a save file, and it's from a
3178 * machine with the opposite byte order to
3179 * ours, we byte-swap the AF_ value.
3181 * Then we run it through "htonl()", and
3182 * generate code to compare against the result.
3184 if (bpf_pcap->sf.rfile != NULL &&
3185 bpf_pcap->sf.swapped)
3186 proto = SWAPLONG(proto);
3187 proto = htonl(proto);
3189 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3191 #ifdef HAVE_NET_PFVAR_H
3192 case DLT_PFLOG:
3194 * af field is host byte order in contrast to the rest of
3195 * the packet.
3197 if (proto == ETHERTYPE_IP)
3198 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3199 BPF_B, (bpf_int32)AF_INET));
3200 #ifdef INET6
3201 else if (proto == ETHERTYPE_IPV6)
3202 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3203 BPF_B, (bpf_int32)AF_INET6));
3204 #endif /* INET6 */
3205 else
3206 return gen_false();
3207 /*NOTREACHED*/
3208 break;
3209 #endif /* HAVE_NET_PFVAR_H */
3211 case DLT_ARCNET:
3212 case DLT_ARCNET_LINUX:
3214 * XXX should we check for first fragment if the protocol
3215 * uses PHDS?
3217 switch (proto) {
3219 default:
3220 return gen_false();
3222 #ifdef INET6
3223 case ETHERTYPE_IPV6:
3224 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3225 (bpf_int32)ARCTYPE_INET6));
3226 #endif /* INET6 */
3228 case ETHERTYPE_IP:
3229 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3230 (bpf_int32)ARCTYPE_IP);
3231 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3232 (bpf_int32)ARCTYPE_IP_OLD);
3233 gen_or(b0, b1);
3234 return (b1);
3236 case ETHERTYPE_ARP:
3237 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3238 (bpf_int32)ARCTYPE_ARP);
3239 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3240 (bpf_int32)ARCTYPE_ARP_OLD);
3241 gen_or(b0, b1);
3242 return (b1);
3244 case ETHERTYPE_REVARP:
3245 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3246 (bpf_int32)ARCTYPE_REVARP));
3248 case ETHERTYPE_ATALK:
3249 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3250 (bpf_int32)ARCTYPE_ATALK));
3252 /*NOTREACHED*/
3253 break;
3255 case DLT_LTALK:
3256 switch (proto) {
3257 case ETHERTYPE_ATALK:
3258 return gen_true();
3259 default:
3260 return gen_false();
3262 /*NOTREACHED*/
3263 break;
3265 case DLT_FRELAY:
3267 * XXX - assumes a 2-byte Frame Relay header with
3268 * DLCI and flags. What if the address is longer?
3270 switch (proto) {
3272 case ETHERTYPE_IP:
3274 * Check for the special NLPID for IP.
3276 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3278 #ifdef INET6
3279 case ETHERTYPE_IPV6:
3281 * Check for the special NLPID for IPv6.
3283 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3284 #endif
3286 case LLCSAP_ISONS:
3288 * Check for several OSI protocols.
3290 * Frame Relay packets typically have an OSI
3291 * NLPID at the beginning; we check for each
3292 * of them.
3294 * What we check for is the NLPID and a frame
3295 * control field of UI, i.e. 0x03 followed
3296 * by the NLPID.
3298 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3299 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3300 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3301 gen_or(b1, b2);
3302 gen_or(b0, b2);
3303 return b2;
3305 default:
3306 return gen_false();
3308 /*NOTREACHED*/
3309 break;
3311 case DLT_MFR:
3312 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3314 case DLT_JUNIPER_MFR:
3315 case DLT_JUNIPER_MLFR:
3316 case DLT_JUNIPER_MLPPP:
3317 case DLT_JUNIPER_ATM1:
3318 case DLT_JUNIPER_ATM2:
3319 case DLT_JUNIPER_PPPOE:
3320 case DLT_JUNIPER_PPPOE_ATM:
3321 case DLT_JUNIPER_GGSN:
3322 case DLT_JUNIPER_ES:
3323 case DLT_JUNIPER_MONITOR:
3324 case DLT_JUNIPER_SERVICES:
3325 case DLT_JUNIPER_ETHER:
3326 case DLT_JUNIPER_PPP:
3327 case DLT_JUNIPER_FRELAY:
3328 case DLT_JUNIPER_CHDLC:
3329 case DLT_JUNIPER_VP:
3330 case DLT_JUNIPER_ST:
3331 case DLT_JUNIPER_ISM:
3332 case DLT_JUNIPER_VS:
3333 case DLT_JUNIPER_SRX_E2E:
3334 case DLT_JUNIPER_FIBRECHANNEL:
3335 case DLT_JUNIPER_ATM_CEMIC:
3337 /* just lets verify the magic number for now -
3338 * on ATM we may have up to 6 different encapsulations on the wire
3339 * and need a lot of heuristics to figure out that the payload
3340 * might be;
3342 * FIXME encapsulation specific BPF_ filters
3344 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3346 case DLT_IPNET:
3347 return gen_ipnet_linktype(proto);
3349 case DLT_LINUX_IRDA:
3350 bpf_error("IrDA link-layer type filtering not implemented");
3352 case DLT_DOCSIS:
3353 bpf_error("DOCSIS link-layer type filtering not implemented");
3355 case DLT_MTP2:
3356 case DLT_MTP2_WITH_PHDR:
3357 bpf_error("MTP2 link-layer type filtering not implemented");
3359 case DLT_ERF:
3360 bpf_error("ERF link-layer type filtering not implemented");
3362 case DLT_PFSYNC:
3363 bpf_error("PFSYNC link-layer type filtering not implemented");
3365 case DLT_LINUX_LAPD:
3366 bpf_error("LAPD link-layer type filtering not implemented");
3368 case DLT_USB:
3369 case DLT_USB_LINUX:
3370 case DLT_USB_LINUX_MMAPPED:
3371 bpf_error("USB link-layer type filtering not implemented");
3373 case DLT_BLUETOOTH_HCI_H4:
3374 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3375 bpf_error("Bluetooth link-layer type filtering not implemented");
3377 case DLT_CAN20B:
3378 case DLT_CAN_SOCKETCAN:
3379 bpf_error("CAN link-layer type filtering not implemented");
3381 case DLT_IEEE802_15_4:
3382 case DLT_IEEE802_15_4_LINUX:
3383 case DLT_IEEE802_15_4_NONASK_PHY:
3384 case DLT_IEEE802_15_4_NOFCS:
3385 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3387 case DLT_IEEE802_16_MAC_CPS_RADIO:
3388 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3390 case DLT_SITA:
3391 bpf_error("SITA link-layer type filtering not implemented");
3393 case DLT_RAIF1:
3394 bpf_error("RAIF1 link-layer type filtering not implemented");
3396 case DLT_IPMB:
3397 bpf_error("IPMB link-layer type filtering not implemented");
3399 case DLT_AX25_KISS:
3400 bpf_error("AX.25 link-layer type filtering not implemented");
3404 * All the types that have no encapsulation should either be
3405 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3406 * all packets are IP packets, or should be handled in some
3407 * special case, if none of them are (if some are and some
3408 * aren't, the lack of encapsulation is a problem, as we'd
3409 * have to find some other way of determining the packet type).
3411 * Therefore, if "off_linktype" is -1, there's an error.
3413 if (off_linktype == (u_int)-1)
3414 abort();
3417 * Any type not handled above should always have an Ethernet
3418 * type at an offset of "off_linktype".
3420 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3424 * Check for an LLC SNAP packet with a given organization code and
3425 * protocol type; we check the entire contents of the 802.2 LLC and
3426 * snap headers, checking for DSAP and SSAP of SNAP and a control
3427 * field of 0x03 in the LLC header, and for the specified organization
3428 * code and protocol type in the SNAP header.
3430 static struct block *
3431 gen_snap(orgcode, ptype)
3432 bpf_u_int32 orgcode;
3433 bpf_u_int32 ptype;
3435 u_char snapblock[8];
3437 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3438 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3439 snapblock[2] = 0x03; /* control = UI */
3440 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3441 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3442 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3443 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3444 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3445 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3449 * Generate code to match a particular packet type, for link-layer types
3450 * using 802.2 LLC headers.
3452 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3453 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3455 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3456 * value, if <= ETHERMTU. We use that to determine whether to
3457 * match the DSAP or both DSAP and LSAP or to check the OUI and
3458 * protocol ID in a SNAP header.
3460 static struct block *
3461 gen_llc_linktype(proto)
3462 int proto;
3465 * XXX - handle token-ring variable-length header.
3467 switch (proto) {
3469 case LLCSAP_IP:
3470 case LLCSAP_ISONS:
3471 case LLCSAP_NETBEUI:
3473 * XXX - should we check both the DSAP and the
3474 * SSAP, like this, or should we check just the
3475 * DSAP, as we do for other types <= ETHERMTU
3476 * (i.e., other SAP values)?
3478 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3479 ((proto << 8) | proto));
3481 case LLCSAP_IPX:
3483 * XXX - are there ever SNAP frames for IPX on
3484 * non-Ethernet 802.x networks?
3486 return gen_cmp(OR_MACPL, 0, BPF_B,
3487 (bpf_int32)LLCSAP_IPX);
3489 case ETHERTYPE_ATALK:
3491 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3492 * SNAP packets with an organization code of
3493 * 0x080007 (Apple, for Appletalk) and a protocol
3494 * type of ETHERTYPE_ATALK (Appletalk).
3496 * XXX - check for an organization code of
3497 * encapsulated Ethernet as well?
3499 return gen_snap(0x080007, ETHERTYPE_ATALK);
3501 default:
3503 * XXX - we don't have to check for IPX 802.3
3504 * here, but should we check for the IPX Ethertype?
3506 if (proto <= ETHERMTU) {
3508 * This is an LLC SAP value, so check
3509 * the DSAP.
3511 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3512 } else {
3514 * This is an Ethernet type; we assume that it's
3515 * unlikely that it'll appear in the right place
3516 * at random, and therefore check only the
3517 * location that would hold the Ethernet type
3518 * in a SNAP frame with an organization code of
3519 * 0x000000 (encapsulated Ethernet).
3521 * XXX - if we were to check for the SNAP DSAP and
3522 * LSAP, as per XXX, and were also to check for an
3523 * organization code of 0x000000 (encapsulated
3524 * Ethernet), we'd do
3526 * return gen_snap(0x000000, proto);
3528 * here; for now, we don't, as per the above.
3529 * I don't know whether it's worth the extra CPU
3530 * time to do the right check or not.
3532 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3537 static struct block *
3538 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3539 bpf_u_int32 addr;
3540 bpf_u_int32 mask;
3541 int dir, proto;
3542 u_int src_off, dst_off;
3544 struct block *b0, *b1;
3545 u_int offset;
3547 switch (dir) {
3549 case Q_SRC:
3550 offset = src_off;
3551 break;
3553 case Q_DST:
3554 offset = dst_off;
3555 break;
3557 case Q_AND:
3558 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3559 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3560 gen_and(b0, b1);
3561 return b1;
3563 case Q_OR:
3564 case Q_DEFAULT:
3565 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3566 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3567 gen_or(b0, b1);
3568 return b1;
3570 default:
3571 abort();
3573 b0 = gen_linktype(proto);
3574 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3575 gen_and(b0, b1);
3576 return b1;
3579 #ifdef INET6
3580 static struct block *
3581 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3582 struct in6_addr *addr;
3583 struct in6_addr *mask;
3584 int dir, proto;
3585 u_int src_off, dst_off;
3587 struct block *b0, *b1;
3588 u_int offset;
3589 u_int32_t *a, *m;
3591 switch (dir) {
3593 case Q_SRC:
3594 offset = src_off;
3595 break;
3597 case Q_DST:
3598 offset = dst_off;
3599 break;
3601 case Q_AND:
3602 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3603 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3604 gen_and(b0, b1);
3605 return b1;
3607 case Q_OR:
3608 case Q_DEFAULT:
3609 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3610 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3611 gen_or(b0, b1);
3612 return b1;
3614 default:
3615 abort();
3617 /* this order is important */
3618 a = (u_int32_t *)addr;
3619 m = (u_int32_t *)mask;
3620 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3621 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3622 gen_and(b0, b1);
3623 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3624 gen_and(b0, b1);
3625 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3626 gen_and(b0, b1);
3627 b0 = gen_linktype(proto);
3628 gen_and(b0, b1);
3629 return b1;
3631 #endif /*INET6*/
3633 static struct block *
3634 gen_ehostop(eaddr, dir)
3635 register const u_char *eaddr;
3636 register int dir;
3638 register struct block *b0, *b1;
3640 switch (dir) {
3641 case Q_SRC:
3642 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3644 case Q_DST:
3645 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3647 case Q_AND:
3648 b0 = gen_ehostop(eaddr, Q_SRC);
3649 b1 = gen_ehostop(eaddr, Q_DST);
3650 gen_and(b0, b1);
3651 return b1;
3653 case Q_DEFAULT:
3654 case Q_OR:
3655 b0 = gen_ehostop(eaddr, Q_SRC);
3656 b1 = gen_ehostop(eaddr, Q_DST);
3657 gen_or(b0, b1);
3658 return b1;
3660 case Q_ADDR1:
3661 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3662 break;
3664 case Q_ADDR2:
3665 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3666 break;
3668 case Q_ADDR3:
3669 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3670 break;
3672 case Q_ADDR4:
3673 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3674 break;
3676 case Q_RA:
3677 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3678 break;
3680 case Q_TA:
3681 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3682 break;
3684 abort();
3685 /* NOTREACHED */
3689 * Like gen_ehostop, but for DLT_FDDI
3691 static struct block *
3692 gen_fhostop(eaddr, dir)
3693 register const u_char *eaddr;
3694 register int dir;
3696 struct block *b0, *b1;
3698 switch (dir) {
3699 case Q_SRC:
3700 #ifdef PCAP_FDDIPAD
3701 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3702 #else
3703 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3704 #endif
3706 case Q_DST:
3707 #ifdef PCAP_FDDIPAD
3708 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3709 #else
3710 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3711 #endif
3713 case Q_AND:
3714 b0 = gen_fhostop(eaddr, Q_SRC);
3715 b1 = gen_fhostop(eaddr, Q_DST);
3716 gen_and(b0, b1);
3717 return b1;
3719 case Q_DEFAULT:
3720 case Q_OR:
3721 b0 = gen_fhostop(eaddr, Q_SRC);
3722 b1 = gen_fhostop(eaddr, Q_DST);
3723 gen_or(b0, b1);
3724 return b1;
3726 case Q_ADDR1:
3727 bpf_error("'addr1' is only supported on 802.11");
3728 break;
3730 case Q_ADDR2:
3731 bpf_error("'addr2' is only supported on 802.11");
3732 break;
3734 case Q_ADDR3:
3735 bpf_error("'addr3' is only supported on 802.11");
3736 break;
3738 case Q_ADDR4:
3739 bpf_error("'addr4' is only supported on 802.11");
3740 break;
3742 case Q_RA:
3743 bpf_error("'ra' is only supported on 802.11");
3744 break;
3746 case Q_TA:
3747 bpf_error("'ta' is only supported on 802.11");
3748 break;
3750 abort();
3751 /* NOTREACHED */
3755 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3757 static struct block *
3758 gen_thostop(eaddr, dir)
3759 register const u_char *eaddr;
3760 register int dir;
3762 register struct block *b0, *b1;
3764 switch (dir) {
3765 case Q_SRC:
3766 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3768 case Q_DST:
3769 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3771 case Q_AND:
3772 b0 = gen_thostop(eaddr, Q_SRC);
3773 b1 = gen_thostop(eaddr, Q_DST);
3774 gen_and(b0, b1);
3775 return b1;
3777 case Q_DEFAULT:
3778 case Q_OR:
3779 b0 = gen_thostop(eaddr, Q_SRC);
3780 b1 = gen_thostop(eaddr, Q_DST);
3781 gen_or(b0, b1);
3782 return b1;
3784 case Q_ADDR1:
3785 bpf_error("'addr1' is only supported on 802.11");
3786 break;
3788 case Q_ADDR2:
3789 bpf_error("'addr2' is only supported on 802.11");
3790 break;
3792 case Q_ADDR3:
3793 bpf_error("'addr3' is only supported on 802.11");
3794 break;
3796 case Q_ADDR4:
3797 bpf_error("'addr4' is only supported on 802.11");
3798 break;
3800 case Q_RA:
3801 bpf_error("'ra' is only supported on 802.11");
3802 break;
3804 case Q_TA:
3805 bpf_error("'ta' is only supported on 802.11");
3806 break;
3808 abort();
3809 /* NOTREACHED */
3813 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3814 * various 802.11 + radio headers.
3816 static struct block *
3817 gen_wlanhostop(eaddr, dir)
3818 register const u_char *eaddr;
3819 register int dir;
3821 register struct block *b0, *b1, *b2;
3822 register struct slist *s;
3824 #ifdef ENABLE_WLAN_FILTERING_PATCH
3826 * TODO GV 20070613
3827 * We need to disable the optimizer because the optimizer is buggy
3828 * and wipes out some LD instructions generated by the below
3829 * code to validate the Frame Control bits
3831 no_optimize = 1;
3832 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3834 switch (dir) {
3835 case Q_SRC:
3837 * Oh, yuk.
3839 * For control frames, there is no SA.
3841 * For management frames, SA is at an
3842 * offset of 10 from the beginning of
3843 * the packet.
3845 * For data frames, SA is at an offset
3846 * of 10 from the beginning of the packet
3847 * if From DS is clear, at an offset of
3848 * 16 from the beginning of the packet
3849 * if From DS is set and To DS is clear,
3850 * and an offset of 24 from the beginning
3851 * of the packet if From DS is set and To DS
3852 * is set.
3856 * Generate the tests to be done for data frames
3857 * with From DS set.
3859 * First, check for To DS set, i.e. check "link[1] & 0x01".
3861 s = gen_load_a(OR_LINK, 1, BPF_B);
3862 b1 = new_block(JMP(BPF_JSET));
3863 b1->s.k = 0x01; /* To DS */
3864 b1->stmts = s;
3867 * If To DS is set, the SA is at 24.
3869 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3870 gen_and(b1, b0);
3873 * Now, check for To DS not set, i.e. check
3874 * "!(link[1] & 0x01)".
3876 s = gen_load_a(OR_LINK, 1, BPF_B);
3877 b2 = new_block(JMP(BPF_JSET));
3878 b2->s.k = 0x01; /* To DS */
3879 b2->stmts = s;
3880 gen_not(b2);
3883 * If To DS is not set, the SA is at 16.
3885 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3886 gen_and(b2, b1);
3889 * Now OR together the last two checks. That gives
3890 * the complete set of checks for data frames with
3891 * From DS set.
3893 gen_or(b1, b0);
3896 * Now check for From DS being set, and AND that with
3897 * the ORed-together checks.
3899 s = gen_load_a(OR_LINK, 1, BPF_B);
3900 b1 = new_block(JMP(BPF_JSET));
3901 b1->s.k = 0x02; /* From DS */
3902 b1->stmts = s;
3903 gen_and(b1, b0);
3906 * Now check for data frames with From DS not set.
3908 s = gen_load_a(OR_LINK, 1, BPF_B);
3909 b2 = new_block(JMP(BPF_JSET));
3910 b2->s.k = 0x02; /* From DS */
3911 b2->stmts = s;
3912 gen_not(b2);
3915 * If From DS isn't set, the SA is at 10.
3917 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3918 gen_and(b2, b1);
3921 * Now OR together the checks for data frames with
3922 * From DS not set and for data frames with From DS
3923 * set; that gives the checks done for data frames.
3925 gen_or(b1, b0);
3928 * Now check for a data frame.
3929 * I.e, check "link[0] & 0x08".
3931 s = gen_load_a(OR_LINK, 0, BPF_B);
3932 b1 = new_block(JMP(BPF_JSET));
3933 b1->s.k = 0x08;
3934 b1->stmts = s;
3937 * AND that with the checks done for data frames.
3939 gen_and(b1, b0);
3942 * If the high-order bit of the type value is 0, this
3943 * is a management frame.
3944 * I.e, check "!(link[0] & 0x08)".
3946 s = gen_load_a(OR_LINK, 0, BPF_B);
3947 b2 = new_block(JMP(BPF_JSET));
3948 b2->s.k = 0x08;
3949 b2->stmts = s;
3950 gen_not(b2);
3953 * For management frames, the SA is at 10.
3955 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3956 gen_and(b2, b1);
3959 * OR that with the checks done for data frames.
3960 * That gives the checks done for management and
3961 * data frames.
3963 gen_or(b1, b0);
3966 * If the low-order bit of the type value is 1,
3967 * this is either a control frame or a frame
3968 * with a reserved type, and thus not a
3969 * frame with an SA.
3971 * I.e., check "!(link[0] & 0x04)".
3973 s = gen_load_a(OR_LINK, 0, BPF_B);
3974 b1 = new_block(JMP(BPF_JSET));
3975 b1->s.k = 0x04;
3976 b1->stmts = s;
3977 gen_not(b1);
3980 * AND that with the checks for data and management
3981 * frames.
3983 gen_and(b1, b0);
3984 return b0;
3986 case Q_DST:
3988 * Oh, yuk.
3990 * For control frames, there is no DA.
3992 * For management frames, DA is at an
3993 * offset of 4 from the beginning of
3994 * the packet.
3996 * For data frames, DA is at an offset
3997 * of 4 from the beginning of the packet
3998 * if To DS is clear and at an offset of
3999 * 16 from the beginning of the packet
4000 * if To DS is set.
4004 * Generate the tests to be done for data frames.
4006 * First, check for To DS set, i.e. "link[1] & 0x01".
4008 s = gen_load_a(OR_LINK, 1, BPF_B);
4009 b1 = new_block(JMP(BPF_JSET));
4010 b1->s.k = 0x01; /* To DS */
4011 b1->stmts = s;
4014 * If To DS is set, the DA is at 16.
4016 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4017 gen_and(b1, b0);
4020 * Now, check for To DS not set, i.e. check
4021 * "!(link[1] & 0x01)".
4023 s = gen_load_a(OR_LINK, 1, BPF_B);
4024 b2 = new_block(JMP(BPF_JSET));
4025 b2->s.k = 0x01; /* To DS */
4026 b2->stmts = s;
4027 gen_not(b2);
4030 * If To DS is not set, the DA is at 4.
4032 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4033 gen_and(b2, b1);
4036 * Now OR together the last two checks. That gives
4037 * the complete set of checks for data frames.
4039 gen_or(b1, b0);
4042 * Now check for a data frame.
4043 * I.e, check "link[0] & 0x08".
4045 s = gen_load_a(OR_LINK, 0, BPF_B);
4046 b1 = new_block(JMP(BPF_JSET));
4047 b1->s.k = 0x08;
4048 b1->stmts = s;
4051 * AND that with the checks done for data frames.
4053 gen_and(b1, b0);
4056 * If the high-order bit of the type value is 0, this
4057 * is a management frame.
4058 * I.e, check "!(link[0] & 0x08)".
4060 s = gen_load_a(OR_LINK, 0, BPF_B);
4061 b2 = new_block(JMP(BPF_JSET));
4062 b2->s.k = 0x08;
4063 b2->stmts = s;
4064 gen_not(b2);
4067 * For management frames, the DA is at 4.
4069 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4070 gen_and(b2, b1);
4073 * OR that with the checks done for data frames.
4074 * That gives the checks done for management and
4075 * data frames.
4077 gen_or(b1, b0);
4080 * If the low-order bit of the type value is 1,
4081 * this is either a control frame or a frame
4082 * with a reserved type, and thus not a
4083 * frame with an SA.
4085 * I.e., check "!(link[0] & 0x04)".
4087 s = gen_load_a(OR_LINK, 0, BPF_B);
4088 b1 = new_block(JMP(BPF_JSET));
4089 b1->s.k = 0x04;
4090 b1->stmts = s;
4091 gen_not(b1);
4094 * AND that with the checks for data and management
4095 * frames.
4097 gen_and(b1, b0);
4098 return b0;
4100 case Q_RA:
4102 * Not present in management frames; addr1 in other
4103 * frames.
4107 * If the high-order bit of the type value is 0, this
4108 * is a management frame.
4109 * I.e, check "(link[0] & 0x08)".
4111 s = gen_load_a(OR_LINK, 0, BPF_B);
4112 b1 = new_block(JMP(BPF_JSET));
4113 b1->s.k = 0x08;
4114 b1->stmts = s;
4117 * Check addr1.
4119 b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4122 * AND that with the check of addr1.
4124 gen_and(b1, b0);
4125 return (b0);
4127 case Q_TA:
4129 * Not present in management frames; addr2, if present,
4130 * in other frames.
4134 * Not present in CTS or ACK control frames.
4136 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4137 IEEE80211_FC0_TYPE_MASK);
4138 gen_not(b0);
4139 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4140 IEEE80211_FC0_SUBTYPE_MASK);
4141 gen_not(b1);
4142 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4143 IEEE80211_FC0_SUBTYPE_MASK);
4144 gen_not(b2);
4145 gen_and(b1, b2);
4146 gen_or(b0, b2);
4149 * If the high-order bit of the type value is 0, this
4150 * is a management frame.
4151 * I.e, check "(link[0] & 0x08)".
4153 s = gen_load_a(OR_LINK, 0, BPF_B);
4154 b1 = new_block(JMP(BPF_JSET));
4155 b1->s.k = 0x08;
4156 b1->stmts = s;
4159 * AND that with the check for frames other than
4160 * CTS and ACK frames.
4162 gen_and(b1, b2);
4165 * Check addr2.
4167 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4168 gen_and(b2, b1);
4169 return b1;
4172 * XXX - add BSSID keyword?
4174 case Q_ADDR1:
4175 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4177 case Q_ADDR2:
4179 * Not present in CTS or ACK control frames.
4181 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4182 IEEE80211_FC0_TYPE_MASK);
4183 gen_not(b0);
4184 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4185 IEEE80211_FC0_SUBTYPE_MASK);
4186 gen_not(b1);
4187 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4188 IEEE80211_FC0_SUBTYPE_MASK);
4189 gen_not(b2);
4190 gen_and(b1, b2);
4191 gen_or(b0, b2);
4192 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4193 gen_and(b2, b1);
4194 return b1;
4196 case Q_ADDR3:
4198 * Not present in control frames.
4200 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4201 IEEE80211_FC0_TYPE_MASK);
4202 gen_not(b0);
4203 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4204 gen_and(b0, b1);
4205 return b1;
4207 case Q_ADDR4:
4209 * Present only if the direction mask has both "From DS"
4210 * and "To DS" set. Neither control frames nor management
4211 * frames should have both of those set, so we don't
4212 * check the frame type.
4214 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4215 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4216 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4217 gen_and(b0, b1);
4218 return b1;
4220 case Q_AND:
4221 b0 = gen_wlanhostop(eaddr, Q_SRC);
4222 b1 = gen_wlanhostop(eaddr, Q_DST);
4223 gen_and(b0, b1);
4224 return b1;
4226 case Q_DEFAULT:
4227 case Q_OR:
4228 b0 = gen_wlanhostop(eaddr, Q_SRC);
4229 b1 = gen_wlanhostop(eaddr, Q_DST);
4230 gen_or(b0, b1);
4231 return b1;
4233 abort();
4234 /* NOTREACHED */
4238 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4239 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4240 * as the RFC states.)
4242 static struct block *
4243 gen_ipfchostop(eaddr, dir)
4244 register const u_char *eaddr;
4245 register int dir;
4247 register struct block *b0, *b1;
4249 switch (dir) {
4250 case Q_SRC:
4251 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4253 case Q_DST:
4254 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4256 case Q_AND:
4257 b0 = gen_ipfchostop(eaddr, Q_SRC);
4258 b1 = gen_ipfchostop(eaddr, Q_DST);
4259 gen_and(b0, b1);
4260 return b1;
4262 case Q_DEFAULT:
4263 case Q_OR:
4264 b0 = gen_ipfchostop(eaddr, Q_SRC);
4265 b1 = gen_ipfchostop(eaddr, Q_DST);
4266 gen_or(b0, b1);
4267 return b1;
4269 case Q_ADDR1:
4270 bpf_error("'addr1' is only supported on 802.11");
4271 break;
4273 case Q_ADDR2:
4274 bpf_error("'addr2' is only supported on 802.11");
4275 break;
4277 case Q_ADDR3:
4278 bpf_error("'addr3' is only supported on 802.11");
4279 break;
4281 case Q_ADDR4:
4282 bpf_error("'addr4' is only supported on 802.11");
4283 break;
4285 case Q_RA:
4286 bpf_error("'ra' is only supported on 802.11");
4287 break;
4289 case Q_TA:
4290 bpf_error("'ta' is only supported on 802.11");
4291 break;
4293 abort();
4294 /* NOTREACHED */
4298 * This is quite tricky because there may be pad bytes in front of the
4299 * DECNET header, and then there are two possible data packet formats that
4300 * carry both src and dst addresses, plus 5 packet types in a format that
4301 * carries only the src node, plus 2 types that use a different format and
4302 * also carry just the src node.
4304 * Yuck.
4306 * Instead of doing those all right, we just look for data packets with
4307 * 0 or 1 bytes of padding. If you want to look at other packets, that
4308 * will require a lot more hacking.
4310 * To add support for filtering on DECNET "areas" (network numbers)
4311 * one would want to add a "mask" argument to this routine. That would
4312 * make the filter even more inefficient, although one could be clever
4313 * and not generate masking instructions if the mask is 0xFFFF.
4315 static struct block *
4316 gen_dnhostop(addr, dir)
4317 bpf_u_int32 addr;
4318 int dir;
4320 struct block *b0, *b1, *b2, *tmp;
4321 u_int offset_lh; /* offset if long header is received */
4322 u_int offset_sh; /* offset if short header is received */
4324 switch (dir) {
4326 case Q_DST:
4327 offset_sh = 1; /* follows flags */
4328 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4329 break;
4331 case Q_SRC:
4332 offset_sh = 3; /* follows flags, dstnode */
4333 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4334 break;
4336 case Q_AND:
4337 /* Inefficient because we do our Calvinball dance twice */
4338 b0 = gen_dnhostop(addr, Q_SRC);
4339 b1 = gen_dnhostop(addr, Q_DST);
4340 gen_and(b0, b1);
4341 return b1;
4343 case Q_OR:
4344 case Q_DEFAULT:
4345 /* Inefficient because we do our Calvinball dance twice */
4346 b0 = gen_dnhostop(addr, Q_SRC);
4347 b1 = gen_dnhostop(addr, Q_DST);
4348 gen_or(b0, b1);
4349 return b1;
4351 case Q_ISO:
4352 bpf_error("ISO host filtering not implemented");
4354 default:
4355 abort();
4357 b0 = gen_linktype(ETHERTYPE_DN);
4358 /* Check for pad = 1, long header case */
4359 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4360 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4361 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4362 BPF_H, (bpf_int32)ntohs((u_short)addr));
4363 gen_and(tmp, b1);
4364 /* Check for pad = 0, long header case */
4365 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4366 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4367 gen_and(tmp, b2);
4368 gen_or(b2, b1);
4369 /* Check for pad = 1, short header case */
4370 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4371 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4372 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4373 gen_and(tmp, b2);
4374 gen_or(b2, b1);
4375 /* Check for pad = 0, short header case */
4376 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4377 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4378 gen_and(tmp, b2);
4379 gen_or(b2, b1);
4381 /* Combine with test for linktype */
4382 gen_and(b0, b1);
4383 return b1;
4387 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4388 * test the bottom-of-stack bit, and then check the version number
4389 * field in the IP header.
4391 static struct block *
4392 gen_mpls_linktype(proto)
4393 int proto;
4395 struct block *b0, *b1;
4397 switch (proto) {
4399 case Q_IP:
4400 /* match the bottom-of-stack bit */
4401 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4402 /* match the IPv4 version number */
4403 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4404 gen_and(b0, b1);
4405 return b1;
4407 case Q_IPV6:
4408 /* match the bottom-of-stack bit */
4409 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4410 /* match the IPv4 version number */
4411 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4412 gen_and(b0, b1);
4413 return b1;
4415 default:
4416 abort();
4420 static struct block *
4421 gen_host(addr, mask, proto, dir, type)
4422 bpf_u_int32 addr;
4423 bpf_u_int32 mask;
4424 int proto;
4425 int dir;
4426 int type;
4428 struct block *b0, *b1;
4429 const char *typestr;
4431 if (type == Q_NET)
4432 typestr = "net";
4433 else
4434 typestr = "host";
4436 switch (proto) {
4438 case Q_DEFAULT:
4439 b0 = gen_host(addr, mask, Q_IP, dir, type);
4441 * Only check for non-IPv4 addresses if we're not
4442 * checking MPLS-encapsulated packets.
4444 if (label_stack_depth == 0) {
4445 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4446 gen_or(b0, b1);
4447 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4448 gen_or(b1, b0);
4450 return b0;
4452 case Q_IP:
4453 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4455 case Q_RARP:
4456 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4458 case Q_ARP:
4459 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4461 case Q_TCP:
4462 bpf_error("'tcp' modifier applied to %s", typestr);
4464 case Q_SCTP:
4465 bpf_error("'sctp' modifier applied to %s", typestr);
4467 case Q_UDP:
4468 bpf_error("'udp' modifier applied to %s", typestr);
4470 case Q_ICMP:
4471 bpf_error("'icmp' modifier applied to %s", typestr);
4473 case Q_IGMP:
4474 bpf_error("'igmp' modifier applied to %s", typestr);
4476 case Q_IGRP:
4477 bpf_error("'igrp' modifier applied to %s", typestr);
4479 case Q_PIM:
4480 bpf_error("'pim' modifier applied to %s", typestr);
4482 case Q_VRRP:
4483 bpf_error("'vrrp' modifier applied to %s", typestr);
4485 case Q_CARP:
4486 bpf_error("'carp' modifier applied to %s", typestr);
4488 case Q_ATALK:
4489 bpf_error("ATALK host filtering not implemented");
4491 case Q_AARP:
4492 bpf_error("AARP host filtering not implemented");
4494 case Q_DECNET:
4495 return gen_dnhostop(addr, dir);
4497 case Q_SCA:
4498 bpf_error("SCA host filtering not implemented");
4500 case Q_LAT:
4501 bpf_error("LAT host filtering not implemented");
4503 case Q_MOPDL:
4504 bpf_error("MOPDL host filtering not implemented");
4506 case Q_MOPRC:
4507 bpf_error("MOPRC host filtering not implemented");
4509 #ifdef INET6
4510 case Q_IPV6:
4511 bpf_error("'ip6' modifier applied to ip host");
4513 case Q_ICMPV6:
4514 bpf_error("'icmp6' modifier applied to %s", typestr);
4515 #endif /* INET6 */
4517 case Q_AH:
4518 bpf_error("'ah' modifier applied to %s", typestr);
4520 case Q_ESP:
4521 bpf_error("'esp' modifier applied to %s", typestr);
4523 case Q_ISO:
4524 bpf_error("ISO host filtering not implemented");
4526 case Q_ESIS:
4527 bpf_error("'esis' modifier applied to %s", typestr);
4529 case Q_ISIS:
4530 bpf_error("'isis' modifier applied to %s", typestr);
4532 case Q_CLNP:
4533 bpf_error("'clnp' modifier applied to %s", typestr);
4535 case Q_STP:
4536 bpf_error("'stp' modifier applied to %s", typestr);
4538 case Q_IPX:
4539 bpf_error("IPX host filtering not implemented");
4541 case Q_NETBEUI:
4542 bpf_error("'netbeui' modifier applied to %s", typestr);
4544 case Q_RADIO:
4545 bpf_error("'radio' modifier applied to %s", typestr);
4547 default:
4548 abort();
4550 /* NOTREACHED */
4553 #ifdef INET6
4554 static struct block *
4555 gen_host6(addr, mask, proto, dir, type)
4556 struct in6_addr *addr;
4557 struct in6_addr *mask;
4558 int proto;
4559 int dir;
4560 int type;
4562 const char *typestr;
4564 if (type == Q_NET)
4565 typestr = "net";
4566 else
4567 typestr = "host";
4569 switch (proto) {
4571 case Q_DEFAULT:
4572 return gen_host6(addr, mask, Q_IPV6, dir, type);
4574 case Q_IP:
4575 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4577 case Q_RARP:
4578 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4580 case Q_ARP:
4581 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4583 case Q_SCTP:
4584 bpf_error("'sctp' modifier applied to %s", typestr);
4586 case Q_TCP:
4587 bpf_error("'tcp' modifier applied to %s", typestr);
4589 case Q_UDP:
4590 bpf_error("'udp' modifier applied to %s", typestr);
4592 case Q_ICMP:
4593 bpf_error("'icmp' modifier applied to %s", typestr);
4595 case Q_IGMP:
4596 bpf_error("'igmp' modifier applied to %s", typestr);
4598 case Q_IGRP:
4599 bpf_error("'igrp' modifier applied to %s", typestr);
4601 case Q_PIM:
4602 bpf_error("'pim' modifier applied to %s", typestr);
4604 case Q_VRRP:
4605 bpf_error("'vrrp' modifier applied to %s", typestr);
4607 case Q_CARP:
4608 bpf_error("'carp' modifier applied to %s", typestr);
4610 case Q_ATALK:
4611 bpf_error("ATALK host filtering not implemented");
4613 case Q_AARP:
4614 bpf_error("AARP host filtering not implemented");
4616 case Q_DECNET:
4617 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4619 case Q_SCA:
4620 bpf_error("SCA host filtering not implemented");
4622 case Q_LAT:
4623 bpf_error("LAT host filtering not implemented");
4625 case Q_MOPDL:
4626 bpf_error("MOPDL host filtering not implemented");
4628 case Q_MOPRC:
4629 bpf_error("MOPRC host filtering not implemented");
4631 case Q_IPV6:
4632 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4634 case Q_ICMPV6:
4635 bpf_error("'icmp6' modifier applied to %s", typestr);
4637 case Q_AH:
4638 bpf_error("'ah' modifier applied to %s", typestr);
4640 case Q_ESP:
4641 bpf_error("'esp' modifier applied to %s", typestr);
4643 case Q_ISO:
4644 bpf_error("ISO host filtering not implemented");
4646 case Q_ESIS:
4647 bpf_error("'esis' modifier applied to %s", typestr);
4649 case Q_ISIS:
4650 bpf_error("'isis' modifier applied to %s", typestr);
4652 case Q_CLNP:
4653 bpf_error("'clnp' modifier applied to %s", typestr);
4655 case Q_STP:
4656 bpf_error("'stp' modifier applied to %s", typestr);
4658 case Q_IPX:
4659 bpf_error("IPX host filtering not implemented");
4661 case Q_NETBEUI:
4662 bpf_error("'netbeui' modifier applied to %s", typestr);
4664 case Q_RADIO:
4665 bpf_error("'radio' modifier applied to %s", typestr);
4667 default:
4668 abort();
4670 /* NOTREACHED */
4672 #endif /*INET6*/
4674 #ifndef INET6
4675 static struct block *
4676 gen_gateway(eaddr, alist, proto, dir)
4677 const u_char *eaddr;
4678 bpf_u_int32 **alist;
4679 int proto;
4680 int dir;
4682 struct block *b0, *b1, *tmp;
4684 if (dir != 0)
4685 bpf_error("direction applied to 'gateway'");
4687 switch (proto) {
4688 case Q_DEFAULT:
4689 case Q_IP:
4690 case Q_ARP:
4691 case Q_RARP:
4692 switch (linktype) {
4693 case DLT_EN10MB:
4694 case DLT_NETANALYZER:
4695 case DLT_NETANALYZER_TRANSPARENT:
4696 b0 = gen_ehostop(eaddr, Q_OR);
4697 break;
4698 case DLT_FDDI:
4699 b0 = gen_fhostop(eaddr, Q_OR);
4700 break;
4701 case DLT_IEEE802:
4702 b0 = gen_thostop(eaddr, Q_OR);
4703 break;
4704 case DLT_IEEE802_11:
4705 case DLT_PRISM_HEADER:
4706 case DLT_IEEE802_11_RADIO_AVS:
4707 case DLT_IEEE802_11_RADIO:
4708 case DLT_PPI:
4709 b0 = gen_wlanhostop(eaddr, Q_OR);
4710 break;
4711 case DLT_SUNATM:
4712 if (!is_lane)
4713 bpf_error(
4714 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4716 * Check that the packet doesn't begin with an
4717 * LE Control marker. (We've already generated
4718 * a test for LANE.)
4720 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4721 BPF_H, 0xFF00);
4722 gen_not(b1);
4725 * Now check the MAC address.
4727 b0 = gen_ehostop(eaddr, Q_OR);
4728 gen_and(b1, b0);
4729 break;
4730 case DLT_IP_OVER_FC:
4731 b0 = gen_ipfchostop(eaddr, Q_OR);
4732 break;
4733 default:
4734 bpf_error(
4735 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4737 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4738 while (*alist) {
4739 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4740 Q_HOST);
4741 gen_or(b1, tmp);
4742 b1 = tmp;
4744 gen_not(b1);
4745 gen_and(b0, b1);
4746 return b1;
4748 bpf_error("illegal modifier of 'gateway'");
4749 /* NOTREACHED */
4751 #endif
4753 struct block *
4754 gen_proto_abbrev(proto)
4755 int proto;
4757 struct block *b0;
4758 struct block *b1;
4760 switch (proto) {
4762 case Q_SCTP:
4763 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4764 #ifdef INET6
4765 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4766 gen_or(b0, b1);
4767 #endif
4768 break;
4770 case Q_TCP:
4771 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4772 #ifdef INET6
4773 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4774 gen_or(b0, b1);
4775 #endif
4776 break;
4778 case Q_UDP:
4779 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4780 #ifdef INET6
4781 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4782 gen_or(b0, b1);
4783 #endif
4784 break;
4786 case Q_ICMP:
4787 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4788 break;
4790 #ifndef IPPROTO_IGMP
4791 #define IPPROTO_IGMP 2
4792 #endif
4794 case Q_IGMP:
4795 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4796 break;
4798 #ifndef IPPROTO_IGRP
4799 #define IPPROTO_IGRP 9
4800 #endif
4801 case Q_IGRP:
4802 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4803 break;
4805 #ifndef IPPROTO_PIM
4806 #define IPPROTO_PIM 103
4807 #endif
4809 case Q_PIM:
4810 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4811 #ifdef INET6
4812 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4813 gen_or(b0, b1);
4814 #endif
4815 break;
4817 #ifndef IPPROTO_VRRP
4818 #define IPPROTO_VRRP 112
4819 #endif
4821 case Q_VRRP:
4822 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4823 break;
4825 #ifndef IPPROTO_CARP
4826 #define IPPROTO_CARP 112
4827 #endif
4829 case Q_CARP:
4830 b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
4831 break;
4833 case Q_IP:
4834 b1 = gen_linktype(ETHERTYPE_IP);
4835 break;
4837 case Q_ARP:
4838 b1 = gen_linktype(ETHERTYPE_ARP);
4839 break;
4841 case Q_RARP:
4842 b1 = gen_linktype(ETHERTYPE_REVARP);
4843 break;
4845 case Q_LINK:
4846 bpf_error("link layer applied in wrong context");
4848 case Q_ATALK:
4849 b1 = gen_linktype(ETHERTYPE_ATALK);
4850 break;
4852 case Q_AARP:
4853 b1 = gen_linktype(ETHERTYPE_AARP);
4854 break;
4856 case Q_DECNET:
4857 b1 = gen_linktype(ETHERTYPE_DN);
4858 break;
4860 case Q_SCA:
4861 b1 = gen_linktype(ETHERTYPE_SCA);
4862 break;
4864 case Q_LAT:
4865 b1 = gen_linktype(ETHERTYPE_LAT);
4866 break;
4868 case Q_MOPDL:
4869 b1 = gen_linktype(ETHERTYPE_MOPDL);
4870 break;
4872 case Q_MOPRC:
4873 b1 = gen_linktype(ETHERTYPE_MOPRC);
4874 break;
4876 #ifdef INET6
4877 case Q_IPV6:
4878 b1 = gen_linktype(ETHERTYPE_IPV6);
4879 break;
4881 #ifndef IPPROTO_ICMPV6
4882 #define IPPROTO_ICMPV6 58
4883 #endif
4884 case Q_ICMPV6:
4885 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4886 break;
4887 #endif /* INET6 */
4889 #ifndef IPPROTO_AH
4890 #define IPPROTO_AH 51
4891 #endif
4892 case Q_AH:
4893 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4894 #ifdef INET6
4895 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4896 gen_or(b0, b1);
4897 #endif
4898 break;
4900 #ifndef IPPROTO_ESP
4901 #define IPPROTO_ESP 50
4902 #endif
4903 case Q_ESP:
4904 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4905 #ifdef INET6
4906 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4907 gen_or(b0, b1);
4908 #endif
4909 break;
4911 case Q_ISO:
4912 b1 = gen_linktype(LLCSAP_ISONS);
4913 break;
4915 case Q_ESIS:
4916 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4917 break;
4919 case Q_ISIS:
4920 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4921 break;
4923 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4924 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4925 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4926 gen_or(b0, b1);
4927 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4928 gen_or(b0, b1);
4929 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4930 gen_or(b0, b1);
4931 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4932 gen_or(b0, b1);
4933 break;
4935 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4936 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4937 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4938 gen_or(b0, b1);
4939 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4940 gen_or(b0, b1);
4941 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4942 gen_or(b0, b1);
4943 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4944 gen_or(b0, b1);
4945 break;
4947 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4948 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4949 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4950 gen_or(b0, b1);
4951 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4952 gen_or(b0, b1);
4953 break;
4955 case Q_ISIS_LSP:
4956 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4957 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4958 gen_or(b0, b1);
4959 break;
4961 case Q_ISIS_SNP:
4962 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4963 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4964 gen_or(b0, b1);
4965 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4966 gen_or(b0, b1);
4967 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4968 gen_or(b0, b1);
4969 break;
4971 case Q_ISIS_CSNP:
4972 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4973 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4974 gen_or(b0, b1);
4975 break;
4977 case Q_ISIS_PSNP:
4978 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4979 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4980 gen_or(b0, b1);
4981 break;
4983 case Q_CLNP:
4984 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
4985 break;
4987 case Q_STP:
4988 b1 = gen_linktype(LLCSAP_8021D);
4989 break;
4991 case Q_IPX:
4992 b1 = gen_linktype(LLCSAP_IPX);
4993 break;
4995 case Q_NETBEUI:
4996 b1 = gen_linktype(LLCSAP_NETBEUI);
4997 break;
4999 case Q_RADIO:
5000 bpf_error("'radio' is not a valid protocol type");
5002 default:
5003 abort();
5005 return b1;
5008 static struct block *
5009 gen_ipfrag()
5011 struct slist *s;
5012 struct block *b;
5014 /* not IPv4 frag other than the first frag */
5015 s = gen_load_a(OR_NET, 6, BPF_H);
5016 b = new_block(JMP(BPF_JSET));
5017 b->s.k = 0x1fff;
5018 b->stmts = s;
5019 gen_not(b);
5021 return b;
5025 * Generate a comparison to a port value in the transport-layer header
5026 * at the specified offset from the beginning of that header.
5028 * XXX - this handles a variable-length prefix preceding the link-layer
5029 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5030 * variable-length link-layer headers (such as Token Ring or 802.11
5031 * headers).
5033 static struct block *
5034 gen_portatom(off, v)
5035 int off;
5036 bpf_int32 v;
5038 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
5041 #ifdef INET6
5042 static struct block *
5043 gen_portatom6(off, v)
5044 int off;
5045 bpf_int32 v;
5047 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
5049 #endif/*INET6*/
5051 struct block *
5052 gen_portop(port, proto, dir)
5053 int port, proto, dir;
5055 struct block *b0, *b1, *tmp;
5057 /* ip proto 'proto' and not a fragment other than the first fragment */
5058 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5059 b0 = gen_ipfrag();
5060 gen_and(tmp, b0);
5062 switch (dir) {
5063 case Q_SRC:
5064 b1 = gen_portatom(0, (bpf_int32)port);
5065 break;
5067 case Q_DST:
5068 b1 = gen_portatom(2, (bpf_int32)port);
5069 break;
5071 case Q_OR:
5072 case Q_DEFAULT:
5073 tmp = gen_portatom(0, (bpf_int32)port);
5074 b1 = gen_portatom(2, (bpf_int32)port);
5075 gen_or(tmp, b1);
5076 break;
5078 case Q_AND:
5079 tmp = gen_portatom(0, (bpf_int32)port);
5080 b1 = gen_portatom(2, (bpf_int32)port);
5081 gen_and(tmp, b1);
5082 break;
5084 default:
5085 abort();
5087 gen_and(b0, b1);
5089 return b1;
5092 static struct block *
5093 gen_port(port, ip_proto, dir)
5094 int port;
5095 int ip_proto;
5096 int dir;
5098 struct block *b0, *b1, *tmp;
5101 * ether proto ip
5103 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5104 * not LLC encapsulation with LLCSAP_IP.
5106 * For IEEE 802 networks - which includes 802.5 token ring
5107 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5108 * says that SNAP encapsulation is used, not LLC encapsulation
5109 * with LLCSAP_IP.
5111 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5112 * RFC 2225 say that SNAP encapsulation is used, not LLC
5113 * encapsulation with LLCSAP_IP.
5115 * So we always check for ETHERTYPE_IP.
5117 b0 = gen_linktype(ETHERTYPE_IP);
5119 switch (ip_proto) {
5120 case IPPROTO_UDP:
5121 case IPPROTO_TCP:
5122 case IPPROTO_SCTP:
5123 b1 = gen_portop(port, ip_proto, dir);
5124 break;
5126 case PROTO_UNDEF:
5127 tmp = gen_portop(port, IPPROTO_TCP, dir);
5128 b1 = gen_portop(port, IPPROTO_UDP, dir);
5129 gen_or(tmp, b1);
5130 tmp = gen_portop(port, IPPROTO_SCTP, dir);
5131 gen_or(tmp, b1);
5132 break;
5134 default:
5135 abort();
5137 gen_and(b0, b1);
5138 return b1;
5141 #ifdef INET6
5142 struct block *
5143 gen_portop6(port, proto, dir)
5144 int port, proto, dir;
5146 struct block *b0, *b1, *tmp;
5148 /* ip6 proto 'proto' */
5149 /* XXX - catch the first fragment of a fragmented packet? */
5150 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5152 switch (dir) {
5153 case Q_SRC:
5154 b1 = gen_portatom6(0, (bpf_int32)port);
5155 break;
5157 case Q_DST:
5158 b1 = gen_portatom6(2, (bpf_int32)port);
5159 break;
5161 case Q_OR:
5162 case Q_DEFAULT:
5163 tmp = gen_portatom6(0, (bpf_int32)port);
5164 b1 = gen_portatom6(2, (bpf_int32)port);
5165 gen_or(tmp, b1);
5166 break;
5168 case Q_AND:
5169 tmp = gen_portatom6(0, (bpf_int32)port);
5170 b1 = gen_portatom6(2, (bpf_int32)port);
5171 gen_and(tmp, b1);
5172 break;
5174 default:
5175 abort();
5177 gen_and(b0, b1);
5179 return b1;
5182 static struct block *
5183 gen_port6(port, ip_proto, dir)
5184 int port;
5185 int ip_proto;
5186 int dir;
5188 struct block *b0, *b1, *tmp;
5190 /* link proto ip6 */
5191 b0 = gen_linktype(ETHERTYPE_IPV6);
5193 switch (ip_proto) {
5194 case IPPROTO_UDP:
5195 case IPPROTO_TCP:
5196 case IPPROTO_SCTP:
5197 b1 = gen_portop6(port, ip_proto, dir);
5198 break;
5200 case PROTO_UNDEF:
5201 tmp = gen_portop6(port, IPPROTO_TCP, dir);
5202 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5203 gen_or(tmp, b1);
5204 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5205 gen_or(tmp, b1);
5206 break;
5208 default:
5209 abort();
5211 gen_and(b0, b1);
5212 return b1;
5214 #endif /* INET6 */
5216 /* gen_portrange code */
5217 static struct block *
5218 gen_portrangeatom(off, v1, v2)
5219 int off;
5220 bpf_int32 v1, v2;
5222 struct block *b1, *b2;
5224 if (v1 > v2) {
5226 * Reverse the order of the ports, so v1 is the lower one.
5228 bpf_int32 vtemp;
5230 vtemp = v1;
5231 v1 = v2;
5232 v2 = vtemp;
5235 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5236 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5238 gen_and(b1, b2);
5240 return b2;
5243 struct block *
5244 gen_portrangeop(port1, port2, proto, dir)
5245 int port1, port2;
5246 int proto;
5247 int dir;
5249 struct block *b0, *b1, *tmp;
5251 /* ip proto 'proto' and not a fragment other than the first fragment */
5252 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5253 b0 = gen_ipfrag();
5254 gen_and(tmp, b0);
5256 switch (dir) {
5257 case Q_SRC:
5258 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5259 break;
5261 case Q_DST:
5262 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5263 break;
5265 case Q_OR:
5266 case Q_DEFAULT:
5267 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5268 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5269 gen_or(tmp, b1);
5270 break;
5272 case Q_AND:
5273 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5274 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5275 gen_and(tmp, b1);
5276 break;
5278 default:
5279 abort();
5281 gen_and(b0, b1);
5283 return b1;
5286 static struct block *
5287 gen_portrange(port1, port2, ip_proto, dir)
5288 int port1, port2;
5289 int ip_proto;
5290 int dir;
5292 struct block *b0, *b1, *tmp;
5294 /* link proto ip */
5295 b0 = gen_linktype(ETHERTYPE_IP);
5297 switch (ip_proto) {
5298 case IPPROTO_UDP:
5299 case IPPROTO_TCP:
5300 case IPPROTO_SCTP:
5301 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5302 break;
5304 case PROTO_UNDEF:
5305 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5306 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5307 gen_or(tmp, b1);
5308 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5309 gen_or(tmp, b1);
5310 break;
5312 default:
5313 abort();
5315 gen_and(b0, b1);
5316 return b1;
5319 #ifdef INET6
5320 static struct block *
5321 gen_portrangeatom6(off, v1, v2)
5322 int off;
5323 bpf_int32 v1, v2;
5325 struct block *b1, *b2;
5327 if (v1 > v2) {
5329 * Reverse the order of the ports, so v1 is the lower one.
5331 bpf_int32 vtemp;
5333 vtemp = v1;
5334 v1 = v2;
5335 v2 = vtemp;
5338 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5339 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5341 gen_and(b1, b2);
5343 return b2;
5346 struct block *
5347 gen_portrangeop6(port1, port2, proto, dir)
5348 int port1, port2;
5349 int proto;
5350 int dir;
5352 struct block *b0, *b1, *tmp;
5354 /* ip6 proto 'proto' */
5355 /* XXX - catch the first fragment of a fragmented packet? */
5356 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5358 switch (dir) {
5359 case Q_SRC:
5360 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5361 break;
5363 case Q_DST:
5364 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5365 break;
5367 case Q_OR:
5368 case Q_DEFAULT:
5369 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5370 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5371 gen_or(tmp, b1);
5372 break;
5374 case Q_AND:
5375 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5376 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5377 gen_and(tmp, b1);
5378 break;
5380 default:
5381 abort();
5383 gen_and(b0, b1);
5385 return b1;
5388 static struct block *
5389 gen_portrange6(port1, port2, ip_proto, dir)
5390 int port1, port2;
5391 int ip_proto;
5392 int dir;
5394 struct block *b0, *b1, *tmp;
5396 /* link proto ip6 */
5397 b0 = gen_linktype(ETHERTYPE_IPV6);
5399 switch (ip_proto) {
5400 case IPPROTO_UDP:
5401 case IPPROTO_TCP:
5402 case IPPROTO_SCTP:
5403 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5404 break;
5406 case PROTO_UNDEF:
5407 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5408 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5409 gen_or(tmp, b1);
5410 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5411 gen_or(tmp, b1);
5412 break;
5414 default:
5415 abort();
5417 gen_and(b0, b1);
5418 return b1;
5420 #endif /* INET6 */
5422 static int
5423 lookup_proto(name, proto)
5424 register const char *name;
5425 register int proto;
5427 register int v;
5429 switch (proto) {
5431 case Q_DEFAULT:
5432 case Q_IP:
5433 case Q_IPV6:
5434 v = pcap_nametoproto(name);
5435 if (v == PROTO_UNDEF)
5436 bpf_error("unknown ip proto '%s'", name);
5437 break;
5439 case Q_LINK:
5440 /* XXX should look up h/w protocol type based on linktype */
5441 v = pcap_nametoeproto(name);
5442 if (v == PROTO_UNDEF) {
5443 v = pcap_nametollc(name);
5444 if (v == PROTO_UNDEF)
5445 bpf_error("unknown ether proto '%s'", name);
5447 break;
5449 case Q_ISO:
5450 if (strcmp(name, "esis") == 0)
5451 v = ISO9542_ESIS;
5452 else if (strcmp(name, "isis") == 0)
5453 v = ISO10589_ISIS;
5454 else if (strcmp(name, "clnp") == 0)
5455 v = ISO8473_CLNP;
5456 else
5457 bpf_error("unknown osi proto '%s'", name);
5458 break;
5460 default:
5461 v = PROTO_UNDEF;
5462 break;
5464 return v;
5467 #if 0
5468 struct stmt *
5469 gen_joinsp(s, n)
5470 struct stmt **s;
5471 int n;
5473 return NULL;
5475 #endif
5477 static struct block *
5478 gen_protochain(v, proto, dir)
5479 int v;
5480 int proto;
5481 int dir;
5483 #ifdef NO_PROTOCHAIN
5484 return gen_proto(v, proto, dir);
5485 #else
5486 struct block *b0, *b;
5487 struct slist *s[100];
5488 int fix2, fix3, fix4, fix5;
5489 int ahcheck, again, end;
5490 int i, max;
5491 int reg2 = alloc_reg();
5493 memset(s, 0, sizeof(s));
5494 fix2 = fix3 = fix4 = fix5 = 0;
5496 switch (proto) {
5497 case Q_IP:
5498 case Q_IPV6:
5499 break;
5500 case Q_DEFAULT:
5501 b0 = gen_protochain(v, Q_IP, dir);
5502 b = gen_protochain(v, Q_IPV6, dir);
5503 gen_or(b0, b);
5504 return b;
5505 default:
5506 bpf_error("bad protocol applied for 'protochain'");
5507 /*NOTREACHED*/
5511 * We don't handle variable-length prefixes before the link-layer
5512 * header, or variable-length link-layer headers, here yet.
5513 * We might want to add BPF instructions to do the protochain
5514 * work, to simplify that and, on platforms that have a BPF
5515 * interpreter with the new instructions, let the filtering
5516 * be done in the kernel. (We already require a modified BPF
5517 * engine to do the protochain stuff, to support backward
5518 * branches, and backward branch support is unlikely to appear
5519 * in kernel BPF engines.)
5521 switch (linktype) {
5523 case DLT_IEEE802_11:
5524 case DLT_PRISM_HEADER:
5525 case DLT_IEEE802_11_RADIO_AVS:
5526 case DLT_IEEE802_11_RADIO:
5527 case DLT_PPI:
5528 bpf_error("'protochain' not supported with 802.11");
5531 no_optimize = 1; /*this code is not compatible with optimzer yet */
5534 * s[0] is a dummy entry to protect other BPF insn from damage
5535 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5536 * hard to find interdependency made by jump table fixup.
5538 i = 0;
5539 s[i] = new_stmt(0); /*dummy*/
5540 i++;
5542 switch (proto) {
5543 case Q_IP:
5544 b0 = gen_linktype(ETHERTYPE_IP);
5546 /* A = ip->ip_p */
5547 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5548 s[i]->s.k = off_macpl + off_nl + 9;
5549 i++;
5550 /* X = ip->ip_hl << 2 */
5551 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5552 s[i]->s.k = off_macpl + off_nl;
5553 i++;
5554 break;
5555 #ifdef INET6
5556 case Q_IPV6:
5557 b0 = gen_linktype(ETHERTYPE_IPV6);
5559 /* A = ip6->ip_nxt */
5560 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5561 s[i]->s.k = off_macpl + off_nl + 6;
5562 i++;
5563 /* X = sizeof(struct ip6_hdr) */
5564 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5565 s[i]->s.k = 40;
5566 i++;
5567 break;
5568 #endif
5569 default:
5570 bpf_error("unsupported proto to gen_protochain");
5571 /*NOTREACHED*/
5574 /* again: if (A == v) goto end; else fall through; */
5575 again = i;
5576 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5577 s[i]->s.k = v;
5578 s[i]->s.jt = NULL; /*later*/
5579 s[i]->s.jf = NULL; /*update in next stmt*/
5580 fix5 = i;
5581 i++;
5583 #ifndef IPPROTO_NONE
5584 #define IPPROTO_NONE 59
5585 #endif
5586 /* if (A == IPPROTO_NONE) goto end */
5587 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5588 s[i]->s.jt = NULL; /*later*/
5589 s[i]->s.jf = NULL; /*update in next stmt*/
5590 s[i]->s.k = IPPROTO_NONE;
5591 s[fix5]->s.jf = s[i];
5592 fix2 = i;
5593 i++;
5595 #ifdef INET6
5596 if (proto == Q_IPV6) {
5597 int v6start, v6end, v6advance, j;
5599 v6start = i;
5600 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5601 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5602 s[i]->s.jt = NULL; /*later*/
5603 s[i]->s.jf = NULL; /*update in next stmt*/
5604 s[i]->s.k = IPPROTO_HOPOPTS;
5605 s[fix2]->s.jf = s[i];
5606 i++;
5607 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5608 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5609 s[i]->s.jt = NULL; /*later*/
5610 s[i]->s.jf = NULL; /*update in next stmt*/
5611 s[i]->s.k = IPPROTO_DSTOPTS;
5612 i++;
5613 /* if (A == IPPROTO_ROUTING) goto v6advance */
5614 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5615 s[i]->s.jt = NULL; /*later*/
5616 s[i]->s.jf = NULL; /*update in next stmt*/
5617 s[i]->s.k = IPPROTO_ROUTING;
5618 i++;
5619 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5620 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5621 s[i]->s.jt = NULL; /*later*/
5622 s[i]->s.jf = NULL; /*later*/
5623 s[i]->s.k = IPPROTO_FRAGMENT;
5624 fix3 = i;
5625 v6end = i;
5626 i++;
5628 /* v6advance: */
5629 v6advance = i;
5632 * in short,
5633 * A = P[X + packet head];
5634 * X = X + (P[X + packet head + 1] + 1) * 8;
5636 /* A = P[X + packet head] */
5637 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5638 s[i]->s.k = off_macpl + off_nl;
5639 i++;
5640 /* MEM[reg2] = A */
5641 s[i] = new_stmt(BPF_ST);
5642 s[i]->s.k = reg2;
5643 i++;
5644 /* A = P[X + packet head + 1]; */
5645 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5646 s[i]->s.k = off_macpl + off_nl + 1;
5647 i++;
5648 /* A += 1 */
5649 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5650 s[i]->s.k = 1;
5651 i++;
5652 /* A *= 8 */
5653 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5654 s[i]->s.k = 8;
5655 i++;
5656 /* A += X */
5657 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
5658 s[i]->s.k = 0;
5659 i++;
5660 /* X = A; */
5661 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5662 i++;
5663 /* A = MEM[reg2] */
5664 s[i] = new_stmt(BPF_LD|BPF_MEM);
5665 s[i]->s.k = reg2;
5666 i++;
5668 /* goto again; (must use BPF_JA for backward jump) */
5669 s[i] = new_stmt(BPF_JMP|BPF_JA);
5670 s[i]->s.k = again - i - 1;
5671 s[i - 1]->s.jf = s[i];
5672 i++;
5674 /* fixup */
5675 for (j = v6start; j <= v6end; j++)
5676 s[j]->s.jt = s[v6advance];
5677 } else
5678 #endif
5680 /* nop */
5681 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5682 s[i]->s.k = 0;
5683 s[fix2]->s.jf = s[i];
5684 i++;
5687 /* ahcheck: */
5688 ahcheck = i;
5689 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5690 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5691 s[i]->s.jt = NULL; /*later*/
5692 s[i]->s.jf = NULL; /*later*/
5693 s[i]->s.k = IPPROTO_AH;
5694 if (fix3)
5695 s[fix3]->s.jf = s[ahcheck];
5696 fix4 = i;
5697 i++;
5700 * in short,
5701 * A = P[X];
5702 * X = X + (P[X + 1] + 2) * 4;
5704 /* A = X */
5705 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5706 i++;
5707 /* A = P[X + packet head]; */
5708 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5709 s[i]->s.k = off_macpl + off_nl;
5710 i++;
5711 /* MEM[reg2] = A */
5712 s[i] = new_stmt(BPF_ST);
5713 s[i]->s.k = reg2;
5714 i++;
5715 /* A = X */
5716 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5717 i++;
5718 /* A += 1 */
5719 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5720 s[i]->s.k = 1;
5721 i++;
5722 /* X = A */
5723 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5724 i++;
5725 /* A = P[X + packet head] */
5726 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5727 s[i]->s.k = off_macpl + off_nl;
5728 i++;
5729 /* A += 2 */
5730 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5731 s[i]->s.k = 2;
5732 i++;
5733 /* A *= 4 */
5734 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5735 s[i]->s.k = 4;
5736 i++;
5737 /* X = A; */
5738 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5739 i++;
5740 /* A = MEM[reg2] */
5741 s[i] = new_stmt(BPF_LD|BPF_MEM);
5742 s[i]->s.k = reg2;
5743 i++;
5745 /* goto again; (must use BPF_JA for backward jump) */
5746 s[i] = new_stmt(BPF_JMP|BPF_JA);
5747 s[i]->s.k = again - i - 1;
5748 i++;
5750 /* end: nop */
5751 end = i;
5752 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5753 s[i]->s.k = 0;
5754 s[fix2]->s.jt = s[end];
5755 s[fix4]->s.jf = s[end];
5756 s[fix5]->s.jt = s[end];
5757 i++;
5760 * make slist chain
5762 max = i;
5763 for (i = 0; i < max - 1; i++)
5764 s[i]->next = s[i + 1];
5765 s[max - 1]->next = NULL;
5768 * emit final check
5770 b = new_block(JMP(BPF_JEQ));
5771 b->stmts = s[1]; /*remember, s[0] is dummy*/
5772 b->s.k = v;
5774 free_reg(reg2);
5776 gen_and(b0, b);
5777 return b;
5778 #endif
5781 static struct block *
5782 gen_check_802_11_data_frame()
5784 struct slist *s;
5785 struct block *b0, *b1;
5788 * A data frame has the 0x08 bit (b3) in the frame control field set
5789 * and the 0x04 bit (b2) clear.
5791 s = gen_load_a(OR_LINK, 0, BPF_B);
5792 b0 = new_block(JMP(BPF_JSET));
5793 b0->s.k = 0x08;
5794 b0->stmts = s;
5796 s = gen_load_a(OR_LINK, 0, BPF_B);
5797 b1 = new_block(JMP(BPF_JSET));
5798 b1->s.k = 0x04;
5799 b1->stmts = s;
5800 gen_not(b1);
5802 gen_and(b1, b0);
5804 return b0;
5808 * Generate code that checks whether the packet is a packet for protocol
5809 * <proto> and whether the type field in that protocol's header has
5810 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5811 * IP packet and checks the protocol number in the IP header against <v>.
5813 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5814 * against Q_IP and Q_IPV6.
5816 static struct block *
5817 gen_proto(v, proto, dir)
5818 int v;
5819 int proto;
5820 int dir;
5822 struct block *b0, *b1;
5823 #ifdef INET6
5824 #ifndef CHASE_CHAIN
5825 struct block *b2;
5826 #endif
5827 #endif
5829 if (dir != Q_DEFAULT)
5830 bpf_error("direction applied to 'proto'");
5832 switch (proto) {
5833 case Q_DEFAULT:
5834 #ifdef INET6
5835 b0 = gen_proto(v, Q_IP, dir);
5836 b1 = gen_proto(v, Q_IPV6, dir);
5837 gen_or(b0, b1);
5838 return b1;
5839 #else
5840 /*FALLTHROUGH*/
5841 #endif
5842 case Q_IP:
5844 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5845 * not LLC encapsulation with LLCSAP_IP.
5847 * For IEEE 802 networks - which includes 802.5 token ring
5848 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5849 * says that SNAP encapsulation is used, not LLC encapsulation
5850 * with LLCSAP_IP.
5852 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5853 * RFC 2225 say that SNAP encapsulation is used, not LLC
5854 * encapsulation with LLCSAP_IP.
5856 * So we always check for ETHERTYPE_IP.
5858 b0 = gen_linktype(ETHERTYPE_IP);
5859 #ifndef CHASE_CHAIN
5860 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5861 #else
5862 b1 = gen_protochain(v, Q_IP);
5863 #endif
5864 gen_and(b0, b1);
5865 return b1;
5867 case Q_ISO:
5868 switch (linktype) {
5870 case DLT_FRELAY:
5872 * Frame Relay packets typically have an OSI
5873 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5874 * generates code to check for all the OSI
5875 * NLPIDs, so calling it and then adding a check
5876 * for the particular NLPID for which we're
5877 * looking is bogus, as we can just check for
5878 * the NLPID.
5880 * What we check for is the NLPID and a frame
5881 * control field value of UI, i.e. 0x03 followed
5882 * by the NLPID.
5884 * XXX - assumes a 2-byte Frame Relay header with
5885 * DLCI and flags. What if the address is longer?
5887 * XXX - what about SNAP-encapsulated frames?
5889 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5890 /*NOTREACHED*/
5891 break;
5893 case DLT_C_HDLC:
5895 * Cisco uses an Ethertype lookalike - for OSI,
5896 * it's 0xfefe.
5898 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5899 /* OSI in C-HDLC is stuffed with a fudge byte */
5900 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5901 gen_and(b0, b1);
5902 return b1;
5904 default:
5905 b0 = gen_linktype(LLCSAP_ISONS);
5906 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5907 gen_and(b0, b1);
5908 return b1;
5911 case Q_ISIS:
5912 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5914 * 4 is the offset of the PDU type relative to the IS-IS
5915 * header.
5917 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5918 gen_and(b0, b1);
5919 return b1;
5921 case Q_ARP:
5922 bpf_error("arp does not encapsulate another protocol");
5923 /* NOTREACHED */
5925 case Q_RARP:
5926 bpf_error("rarp does not encapsulate another protocol");
5927 /* NOTREACHED */
5929 case Q_ATALK:
5930 bpf_error("atalk encapsulation is not specifiable");
5931 /* NOTREACHED */
5933 case Q_DECNET:
5934 bpf_error("decnet encapsulation is not specifiable");
5935 /* NOTREACHED */
5937 case Q_SCA:
5938 bpf_error("sca does not encapsulate another protocol");
5939 /* NOTREACHED */
5941 case Q_LAT:
5942 bpf_error("lat does not encapsulate another protocol");
5943 /* NOTREACHED */
5945 case Q_MOPRC:
5946 bpf_error("moprc does not encapsulate another protocol");
5947 /* NOTREACHED */
5949 case Q_MOPDL:
5950 bpf_error("mopdl does not encapsulate another protocol");
5951 /* NOTREACHED */
5953 case Q_LINK:
5954 return gen_linktype(v);
5956 case Q_UDP:
5957 bpf_error("'udp proto' is bogus");
5958 /* NOTREACHED */
5960 case Q_TCP:
5961 bpf_error("'tcp proto' is bogus");
5962 /* NOTREACHED */
5964 case Q_SCTP:
5965 bpf_error("'sctp proto' is bogus");
5966 /* NOTREACHED */
5968 case Q_ICMP:
5969 bpf_error("'icmp proto' is bogus");
5970 /* NOTREACHED */
5972 case Q_IGMP:
5973 bpf_error("'igmp proto' is bogus");
5974 /* NOTREACHED */
5976 case Q_IGRP:
5977 bpf_error("'igrp proto' is bogus");
5978 /* NOTREACHED */
5980 case Q_PIM:
5981 bpf_error("'pim proto' is bogus");
5982 /* NOTREACHED */
5984 case Q_VRRP:
5985 bpf_error("'vrrp proto' is bogus");
5986 /* NOTREACHED */
5988 case Q_CARP:
5989 bpf_error("'carp proto' is bogus");
5990 /* NOTREACHED */
5992 #ifdef INET6
5993 case Q_IPV6:
5994 b0 = gen_linktype(ETHERTYPE_IPV6);
5995 #ifndef CHASE_CHAIN
5997 * Also check for a fragment header before the final
5998 * header.
6000 b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT);
6001 b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v);
6002 gen_and(b2, b1);
6003 b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
6004 gen_or(b2, b1);
6005 #else
6006 b1 = gen_protochain(v, Q_IPV6);
6007 #endif
6008 gen_and(b0, b1);
6009 return b1;
6011 case Q_ICMPV6:
6012 bpf_error("'icmp6 proto' is bogus");
6013 #endif /* INET6 */
6015 case Q_AH:
6016 bpf_error("'ah proto' is bogus");
6018 case Q_ESP:
6019 bpf_error("'ah proto' is bogus");
6021 case Q_STP:
6022 bpf_error("'stp proto' is bogus");
6024 case Q_IPX:
6025 bpf_error("'ipx proto' is bogus");
6027 case Q_NETBEUI:
6028 bpf_error("'netbeui proto' is bogus");
6030 case Q_RADIO:
6031 bpf_error("'radio proto' is bogus");
6033 default:
6034 abort();
6035 /* NOTREACHED */
6037 /* NOTREACHED */
6040 struct block *
6041 gen_scode(name, q)
6042 register const char *name;
6043 struct qual q;
6045 int proto = q.proto;
6046 int dir = q.dir;
6047 int tproto;
6048 u_char *eaddr;
6049 bpf_u_int32 mask, addr;
6050 #ifndef INET6
6051 bpf_u_int32 **alist;
6052 #else
6053 int tproto6;
6054 struct sockaddr_in *sin4;
6055 struct sockaddr_in6 *sin6;
6056 struct addrinfo *res, *res0;
6057 struct in6_addr mask128;
6058 #endif /*INET6*/
6059 struct block *b, *tmp;
6060 int port, real_proto;
6061 int port1, port2;
6063 switch (q.addr) {
6065 case Q_NET:
6066 addr = pcap_nametonetaddr(name);
6067 if (addr == 0)
6068 bpf_error("unknown network '%s'", name);
6069 /* Left justify network addr and calculate its network mask */
6070 mask = 0xffffffff;
6071 while (addr && (addr & 0xff000000) == 0) {
6072 addr <<= 8;
6073 mask <<= 8;
6075 return gen_host(addr, mask, proto, dir, q.addr);
6077 case Q_DEFAULT:
6078 case Q_HOST:
6079 if (proto == Q_LINK) {
6080 switch (linktype) {
6082 case DLT_EN10MB:
6083 case DLT_NETANALYZER:
6084 case DLT_NETANALYZER_TRANSPARENT:
6085 eaddr = pcap_ether_hostton(name);
6086 if (eaddr == NULL)
6087 bpf_error(
6088 "unknown ether host '%s'", name);
6089 b = gen_ehostop(eaddr, dir);
6090 free(eaddr);
6091 return b;
6093 case DLT_FDDI:
6094 eaddr = pcap_ether_hostton(name);
6095 if (eaddr == NULL)
6096 bpf_error(
6097 "unknown FDDI host '%s'", name);
6098 b = gen_fhostop(eaddr, dir);
6099 free(eaddr);
6100 return b;
6102 case DLT_IEEE802:
6103 eaddr = pcap_ether_hostton(name);
6104 if (eaddr == NULL)
6105 bpf_error(
6106 "unknown token ring host '%s'", name);
6107 b = gen_thostop(eaddr, dir);
6108 free(eaddr);
6109 return b;
6111 case DLT_IEEE802_11:
6112 case DLT_PRISM_HEADER:
6113 case DLT_IEEE802_11_RADIO_AVS:
6114 case DLT_IEEE802_11_RADIO:
6115 case DLT_PPI:
6116 eaddr = pcap_ether_hostton(name);
6117 if (eaddr == NULL)
6118 bpf_error(
6119 "unknown 802.11 host '%s'", name);
6120 b = gen_wlanhostop(eaddr, dir);
6121 free(eaddr);
6122 return b;
6124 case DLT_IP_OVER_FC:
6125 eaddr = pcap_ether_hostton(name);
6126 if (eaddr == NULL)
6127 bpf_error(
6128 "unknown Fibre Channel host '%s'", name);
6129 b = gen_ipfchostop(eaddr, dir);
6130 free(eaddr);
6131 return b;
6133 case DLT_SUNATM:
6134 if (!is_lane)
6135 break;
6138 * Check that the packet doesn't begin
6139 * with an LE Control marker. (We've
6140 * already generated a test for LANE.)
6142 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6143 BPF_H, 0xFF00);
6144 gen_not(tmp);
6146 eaddr = pcap_ether_hostton(name);
6147 if (eaddr == NULL)
6148 bpf_error(
6149 "unknown ether host '%s'", name);
6150 b = gen_ehostop(eaddr, dir);
6151 gen_and(tmp, b);
6152 free(eaddr);
6153 return b;
6156 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6157 } else if (proto == Q_DECNET) {
6158 unsigned short dn_addr = __pcap_nametodnaddr(name);
6160 * I don't think DECNET hosts can be multihomed, so
6161 * there is no need to build up a list of addresses
6163 return (gen_host(dn_addr, 0, proto, dir, q.addr));
6164 } else {
6165 #ifndef INET6
6166 alist = pcap_nametoaddr(name);
6167 if (alist == NULL || *alist == NULL)
6168 bpf_error("unknown host '%s'", name);
6169 tproto = proto;
6170 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
6171 tproto = Q_IP;
6172 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
6173 while (*alist) {
6174 tmp = gen_host(**alist++, 0xffffffff,
6175 tproto, dir, q.addr);
6176 gen_or(b, tmp);
6177 b = tmp;
6179 return b;
6180 #else
6181 memset(&mask128, 0xff, sizeof(mask128));
6182 res0 = res = pcap_nametoaddrinfo(name);
6183 if (res == NULL)
6184 bpf_error("unknown host '%s'", name);
6185 ai = res;
6186 b = tmp = NULL;
6187 tproto = tproto6 = proto;
6188 if (off_linktype == -1 && tproto == Q_DEFAULT) {
6189 tproto = Q_IP;
6190 tproto6 = Q_IPV6;
6192 for (res = res0; res; res = res->ai_next) {
6193 switch (res->ai_family) {
6194 case AF_INET:
6195 if (tproto == Q_IPV6)
6196 continue;
6198 sin4 = (struct sockaddr_in *)
6199 res->ai_addr;
6200 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
6201 0xffffffff, tproto, dir, q.addr);
6202 break;
6203 case AF_INET6:
6204 if (tproto6 == Q_IP)
6205 continue;
6207 sin6 = (struct sockaddr_in6 *)
6208 res->ai_addr;
6209 tmp = gen_host6(&sin6->sin6_addr,
6210 &mask128, tproto6, dir, q.addr);
6211 break;
6212 default:
6213 continue;
6215 if (b)
6216 gen_or(b, tmp);
6217 b = tmp;
6219 ai = NULL;
6220 freeaddrinfo(res0);
6221 if (b == NULL) {
6222 bpf_error("unknown host '%s'%s", name,
6223 (proto == Q_DEFAULT)
6224 ? ""
6225 : " for specified address family");
6227 return b;
6228 #endif /*INET6*/
6231 case Q_PORT:
6232 if (proto != Q_DEFAULT &&
6233 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6234 bpf_error("illegal qualifier of 'port'");
6235 if (pcap_nametoport(name, &port, &real_proto) == 0)
6236 bpf_error("unknown port '%s'", name);
6237 if (proto == Q_UDP) {
6238 if (real_proto == IPPROTO_TCP)
6239 bpf_error("port '%s' is tcp", name);
6240 else if (real_proto == IPPROTO_SCTP)
6241 bpf_error("port '%s' is sctp", name);
6242 else
6243 /* override PROTO_UNDEF */
6244 real_proto = IPPROTO_UDP;
6246 if (proto == Q_TCP) {
6247 if (real_proto == IPPROTO_UDP)
6248 bpf_error("port '%s' is udp", name);
6250 else if (real_proto == IPPROTO_SCTP)
6251 bpf_error("port '%s' is sctp", name);
6252 else
6253 /* override PROTO_UNDEF */
6254 real_proto = IPPROTO_TCP;
6256 if (proto == Q_SCTP) {
6257 if (real_proto == IPPROTO_UDP)
6258 bpf_error("port '%s' is udp", name);
6260 else if (real_proto == IPPROTO_TCP)
6261 bpf_error("port '%s' is tcp", name);
6262 else
6263 /* override PROTO_UNDEF */
6264 real_proto = IPPROTO_SCTP;
6266 if (port < 0)
6267 bpf_error("illegal port number %d < 0", port);
6268 if (port > 65535)
6269 bpf_error("illegal port number %d > 65535", port);
6270 #ifndef INET6
6271 return gen_port(port, real_proto, dir);
6272 #else
6273 b = gen_port(port, real_proto, dir);
6274 gen_or(gen_port6(port, real_proto, dir), b);
6275 return b;
6276 #endif /* INET6 */
6278 case Q_PORTRANGE:
6279 if (proto != Q_DEFAULT &&
6280 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6281 bpf_error("illegal qualifier of 'portrange'");
6282 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6283 bpf_error("unknown port in range '%s'", name);
6284 if (proto == Q_UDP) {
6285 if (real_proto == IPPROTO_TCP)
6286 bpf_error("port in range '%s' is tcp", name);
6287 else if (real_proto == IPPROTO_SCTP)
6288 bpf_error("port in range '%s' is sctp", name);
6289 else
6290 /* override PROTO_UNDEF */
6291 real_proto = IPPROTO_UDP;
6293 if (proto == Q_TCP) {
6294 if (real_proto == IPPROTO_UDP)
6295 bpf_error("port in range '%s' is udp", name);
6296 else if (real_proto == IPPROTO_SCTP)
6297 bpf_error("port in range '%s' is sctp", name);
6298 else
6299 /* override PROTO_UNDEF */
6300 real_proto = IPPROTO_TCP;
6302 if (proto == Q_SCTP) {
6303 if (real_proto == IPPROTO_UDP)
6304 bpf_error("port in range '%s' is udp", name);
6305 else if (real_proto == IPPROTO_TCP)
6306 bpf_error("port in range '%s' is tcp", name);
6307 else
6308 /* override PROTO_UNDEF */
6309 real_proto = IPPROTO_SCTP;
6311 if (port1 < 0)
6312 bpf_error("illegal port number %d < 0", port1);
6313 if (port1 > 65535)
6314 bpf_error("illegal port number %d > 65535", port1);
6315 if (port2 < 0)
6316 bpf_error("illegal port number %d < 0", port2);
6317 if (port2 > 65535)
6318 bpf_error("illegal port number %d > 65535", port2);
6320 #ifndef INET6
6321 return gen_portrange(port1, port2, real_proto, dir);
6322 #else
6323 b = gen_portrange(port1, port2, real_proto, dir);
6324 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6325 return b;
6326 #endif /* INET6 */
6328 case Q_GATEWAY:
6329 #ifndef INET6
6330 eaddr = pcap_ether_hostton(name);
6331 if (eaddr == NULL)
6332 bpf_error("unknown ether host: %s", name);
6334 alist = pcap_nametoaddr(name);
6335 if (alist == NULL || *alist == NULL)
6336 bpf_error("unknown host '%s'", name);
6337 b = gen_gateway(eaddr, alist, proto, dir);
6338 free(eaddr);
6339 return b;
6340 #else
6341 bpf_error("'gateway' not supported in this configuration");
6342 #endif /*INET6*/
6344 case Q_PROTO:
6345 real_proto = lookup_proto(name, proto);
6346 if (real_proto >= 0)
6347 return gen_proto(real_proto, proto, dir);
6348 else
6349 bpf_error("unknown protocol: %s", name);
6351 case Q_PROTOCHAIN:
6352 real_proto = lookup_proto(name, proto);
6353 if (real_proto >= 0)
6354 return gen_protochain(real_proto, proto, dir);
6355 else
6356 bpf_error("unknown protocol: %s", name);
6358 case Q_UNDEF:
6359 syntax();
6360 /* NOTREACHED */
6362 abort();
6363 /* NOTREACHED */
6366 struct block *
6367 gen_mcode(s1, s2, masklen, q)
6368 register const char *s1, *s2;
6369 register int masklen;
6370 struct qual q;
6372 register int nlen, mlen;
6373 bpf_u_int32 n, m;
6375 nlen = __pcap_atoin(s1, &n);
6376 /* Promote short ipaddr */
6377 n <<= 32 - nlen;
6379 if (s2 != NULL) {
6380 mlen = __pcap_atoin(s2, &m);
6381 /* Promote short ipaddr */
6382 m <<= 32 - mlen;
6383 if ((n & ~m) != 0)
6384 bpf_error("non-network bits set in \"%s mask %s\"",
6385 s1, s2);
6386 } else {
6387 /* Convert mask len to mask */
6388 if (masklen > 32)
6389 bpf_error("mask length must be <= 32");
6390 if (masklen == 0) {
6392 * X << 32 is not guaranteed by C to be 0; it's
6393 * undefined.
6395 m = 0;
6396 } else
6397 m = 0xffffffff << (32 - masklen);
6398 if ((n & ~m) != 0)
6399 bpf_error("non-network bits set in \"%s/%d\"",
6400 s1, masklen);
6403 switch (q.addr) {
6405 case Q_NET:
6406 return gen_host(n, m, q.proto, q.dir, q.addr);
6408 default:
6409 bpf_error("Mask syntax for networks only");
6410 /* NOTREACHED */
6412 /* NOTREACHED */
6413 return NULL;
6416 struct block *
6417 gen_ncode(s, v, q)
6418 register const char *s;
6419 bpf_u_int32 v;
6420 struct qual q;
6422 bpf_u_int32 mask;
6423 int proto = q.proto;
6424 int dir = q.dir;
6425 register int vlen;
6427 if (s == NULL)
6428 vlen = 32;
6429 else if (q.proto == Q_DECNET)
6430 vlen = __pcap_atodn(s, &v);
6431 else
6432 vlen = __pcap_atoin(s, &v);
6434 switch (q.addr) {
6436 case Q_DEFAULT:
6437 case Q_HOST:
6438 case Q_NET:
6439 if (proto == Q_DECNET)
6440 return gen_host(v, 0, proto, dir, q.addr);
6441 else if (proto == Q_LINK) {
6442 bpf_error("illegal link layer address");
6443 } else {
6444 mask = 0xffffffff;
6445 if (s == NULL && q.addr == Q_NET) {
6446 /* Promote short net number */
6447 while (v && (v & 0xff000000) == 0) {
6448 v <<= 8;
6449 mask <<= 8;
6451 } else {
6452 /* Promote short ipaddr */
6453 v <<= 32 - vlen;
6454 mask <<= 32 - vlen;
6456 return gen_host(v, mask, proto, dir, q.addr);
6459 case Q_PORT:
6460 if (proto == Q_UDP)
6461 proto = IPPROTO_UDP;
6462 else if (proto == Q_TCP)
6463 proto = IPPROTO_TCP;
6464 else if (proto == Q_SCTP)
6465 proto = IPPROTO_SCTP;
6466 else if (proto == Q_DEFAULT)
6467 proto = PROTO_UNDEF;
6468 else
6469 bpf_error("illegal qualifier of 'port'");
6471 if (v > 65535)
6472 bpf_error("illegal port number %u > 65535", v);
6474 #ifndef INET6
6475 return gen_port((int)v, proto, dir);
6476 #else
6478 struct block *b;
6479 b = gen_port((int)v, proto, dir);
6480 gen_or(gen_port6((int)v, proto, dir), b);
6481 return b;
6483 #endif /* INET6 */
6485 case Q_PORTRANGE:
6486 if (proto == Q_UDP)
6487 proto = IPPROTO_UDP;
6488 else if (proto == Q_TCP)
6489 proto = IPPROTO_TCP;
6490 else if (proto == Q_SCTP)
6491 proto = IPPROTO_SCTP;
6492 else if (proto == Q_DEFAULT)
6493 proto = PROTO_UNDEF;
6494 else
6495 bpf_error("illegal qualifier of 'portrange'");
6497 if (v > 65535)
6498 bpf_error("illegal port number %u > 65535", v);
6500 #ifndef INET6
6501 return gen_portrange((int)v, (int)v, proto, dir);
6502 #else
6504 struct block *b;
6505 b = gen_portrange((int)v, (int)v, proto, dir);
6506 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6507 return b;
6509 #endif /* INET6 */
6511 case Q_GATEWAY:
6512 bpf_error("'gateway' requires a name");
6513 /* NOTREACHED */
6515 case Q_PROTO:
6516 return gen_proto((int)v, proto, dir);
6518 case Q_PROTOCHAIN:
6519 return gen_protochain((int)v, proto, dir);
6521 case Q_UNDEF:
6522 syntax();
6523 /* NOTREACHED */
6525 default:
6526 abort();
6527 /* NOTREACHED */
6529 /* NOTREACHED */
6532 #ifdef INET6
6533 struct block *
6534 gen_mcode6(s1, s2, masklen, q)
6535 register const char *s1, *s2;
6536 register int masklen;
6537 struct qual q;
6539 struct addrinfo *res;
6540 struct in6_addr *addr;
6541 struct in6_addr mask;
6542 struct block *b;
6543 u_int32_t *a, *m;
6545 if (s2)
6546 bpf_error("no mask %s supported", s2);
6548 res = pcap_nametoaddrinfo(s1);
6549 if (!res)
6550 bpf_error("invalid ip6 address %s", s1);
6551 ai = res;
6552 if (res->ai_next)
6553 bpf_error("%s resolved to multiple address", s1);
6554 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6556 if (sizeof(mask) * 8 < masklen)
6557 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6558 memset(&mask, 0, sizeof(mask));
6559 memset(&mask, 0xff, masklen / 8);
6560 if (masklen % 8) {
6561 mask.s6_addr[masklen / 8] =
6562 (0xff << (8 - masklen % 8)) & 0xff;
6565 a = (u_int32_t *)addr;
6566 m = (u_int32_t *)&mask;
6567 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6568 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6569 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6572 switch (q.addr) {
6574 case Q_DEFAULT:
6575 case Q_HOST:
6576 if (masklen != 128)
6577 bpf_error("Mask syntax for networks only");
6578 /* FALLTHROUGH */
6580 case Q_NET:
6581 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6582 ai = NULL;
6583 freeaddrinfo(res);
6584 return b;
6586 default:
6587 bpf_error("invalid qualifier against IPv6 address");
6588 /* NOTREACHED */
6590 return NULL;
6592 #endif /*INET6*/
6594 struct block *
6595 gen_ecode(eaddr, q)
6596 register const u_char *eaddr;
6597 struct qual q;
6599 struct block *b, *tmp;
6601 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6602 switch (linktype) {
6603 case DLT_EN10MB:
6604 case DLT_NETANALYZER:
6605 case DLT_NETANALYZER_TRANSPARENT:
6606 return gen_ehostop(eaddr, (int)q.dir);
6607 case DLT_FDDI:
6608 return gen_fhostop(eaddr, (int)q.dir);
6609 case DLT_IEEE802:
6610 return gen_thostop(eaddr, (int)q.dir);
6611 case DLT_IEEE802_11:
6612 case DLT_PRISM_HEADER:
6613 case DLT_IEEE802_11_RADIO_AVS:
6614 case DLT_IEEE802_11_RADIO:
6615 case DLT_PPI:
6616 return gen_wlanhostop(eaddr, (int)q.dir);
6617 case DLT_SUNATM:
6618 if (is_lane) {
6620 * Check that the packet doesn't begin with an
6621 * LE Control marker. (We've already generated
6622 * a test for LANE.)
6624 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6625 0xFF00);
6626 gen_not(tmp);
6629 * Now check the MAC address.
6631 b = gen_ehostop(eaddr, (int)q.dir);
6632 gen_and(tmp, b);
6633 return b;
6635 break;
6636 case DLT_IP_OVER_FC:
6637 return gen_ipfchostop(eaddr, (int)q.dir);
6638 default:
6639 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6640 break;
6643 bpf_error("ethernet address used in non-ether expression");
6644 /* NOTREACHED */
6645 return NULL;
6648 void
6649 sappend(s0, s1)
6650 struct slist *s0, *s1;
6653 * This is definitely not the best way to do this, but the
6654 * lists will rarely get long.
6656 while (s0->next)
6657 s0 = s0->next;
6658 s0->next = s1;
6661 static struct slist *
6662 xfer_to_x(a)
6663 struct arth *a;
6665 struct slist *s;
6667 s = new_stmt(BPF_LDX|BPF_MEM);
6668 s->s.k = a->regno;
6669 return s;
6672 static struct slist *
6673 xfer_to_a(a)
6674 struct arth *a;
6676 struct slist *s;
6678 s = new_stmt(BPF_LD|BPF_MEM);
6679 s->s.k = a->regno;
6680 return s;
6684 * Modify "index" to use the value stored into its register as an
6685 * offset relative to the beginning of the header for the protocol
6686 * "proto", and allocate a register and put an item "size" bytes long
6687 * (1, 2, or 4) at that offset into that register, making it the register
6688 * for "index".
6690 struct arth *
6691 gen_load(proto, inst, size)
6692 int proto;
6693 struct arth *inst;
6694 int size;
6696 struct slist *s, *tmp;
6697 struct block *b;
6698 int regno = alloc_reg();
6700 free_reg(inst->regno);
6701 switch (size) {
6703 default:
6704 bpf_error("data size must be 1, 2, or 4");
6706 case 1:
6707 size = BPF_B;
6708 break;
6710 case 2:
6711 size = BPF_H;
6712 break;
6714 case 4:
6715 size = BPF_W;
6716 break;
6718 switch (proto) {
6719 default:
6720 bpf_error("unsupported index operation");
6722 case Q_RADIO:
6724 * The offset is relative to the beginning of the packet
6725 * data, if we have a radio header. (If we don't, this
6726 * is an error.)
6728 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6729 linktype != DLT_IEEE802_11_RADIO &&
6730 linktype != DLT_PRISM_HEADER)
6731 bpf_error("radio information not present in capture");
6734 * Load into the X register the offset computed into the
6735 * register specified by "index".
6737 s = xfer_to_x(inst);
6740 * Load the item at that offset.
6742 tmp = new_stmt(BPF_LD|BPF_IND|size);
6743 sappend(s, tmp);
6744 sappend(inst->s, s);
6745 break;
6747 case Q_LINK:
6749 * The offset is relative to the beginning of
6750 * the link-layer header.
6752 * XXX - what about ATM LANE? Should the index be
6753 * relative to the beginning of the AAL5 frame, so
6754 * that 0 refers to the beginning of the LE Control
6755 * field, or relative to the beginning of the LAN
6756 * frame, so that 0 refers, for Ethernet LANE, to
6757 * the beginning of the destination address?
6759 s = gen_llprefixlen();
6762 * If "s" is non-null, it has code to arrange that the
6763 * X register contains the length of the prefix preceding
6764 * the link-layer header. Add to it the offset computed
6765 * into the register specified by "index", and move that
6766 * into the X register. Otherwise, just load into the X
6767 * register the offset computed into the register specified
6768 * by "index".
6770 if (s != NULL) {
6771 sappend(s, xfer_to_a(inst));
6772 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6773 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6774 } else
6775 s = xfer_to_x(inst);
6778 * Load the item at the sum of the offset we've put in the
6779 * X register and the offset of the start of the link
6780 * layer header (which is 0 if the radio header is
6781 * variable-length; that header length is what we put
6782 * into the X register and then added to the index).
6784 tmp = new_stmt(BPF_LD|BPF_IND|size);
6785 tmp->s.k = off_ll;
6786 sappend(s, tmp);
6787 sappend(inst->s, s);
6788 break;
6790 case Q_IP:
6791 case Q_ARP:
6792 case Q_RARP:
6793 case Q_ATALK:
6794 case Q_DECNET:
6795 case Q_SCA:
6796 case Q_LAT:
6797 case Q_MOPRC:
6798 case Q_MOPDL:
6799 #ifdef INET6
6800 case Q_IPV6:
6801 #endif
6803 * The offset is relative to the beginning of
6804 * the network-layer header.
6805 * XXX - are there any cases where we want
6806 * off_nl_nosnap?
6808 s = gen_off_macpl();
6811 * If "s" is non-null, it has code to arrange that the
6812 * X register contains the offset of the MAC-layer
6813 * payload. Add to it the offset computed into the
6814 * register specified by "index", and move that into
6815 * the X register. Otherwise, just load into the X
6816 * register the offset computed into the register specified
6817 * by "index".
6819 if (s != NULL) {
6820 sappend(s, xfer_to_a(inst));
6821 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6822 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6823 } else
6824 s = xfer_to_x(inst);
6827 * Load the item at the sum of the offset we've put in the
6828 * X register, the offset of the start of the network
6829 * layer header from the beginning of the MAC-layer
6830 * payload, and the purported offset of the start of the
6831 * MAC-layer payload (which might be 0 if there's a
6832 * variable-length prefix before the link-layer header
6833 * or the link-layer header itself is variable-length;
6834 * the variable-length offset of the start of the
6835 * MAC-layer payload is what we put into the X register
6836 * and then added to the index).
6838 tmp = new_stmt(BPF_LD|BPF_IND|size);
6839 tmp->s.k = off_macpl + off_nl;
6840 sappend(s, tmp);
6841 sappend(inst->s, s);
6844 * Do the computation only if the packet contains
6845 * the protocol in question.
6847 b = gen_proto_abbrev(proto);
6848 if (inst->b)
6849 gen_and(inst->b, b);
6850 inst->b = b;
6851 break;
6853 case Q_SCTP:
6854 case Q_TCP:
6855 case Q_UDP:
6856 case Q_ICMP:
6857 case Q_IGMP:
6858 case Q_IGRP:
6859 case Q_PIM:
6860 case Q_VRRP:
6861 case Q_CARP:
6863 * The offset is relative to the beginning of
6864 * the transport-layer header.
6866 * Load the X register with the length of the IPv4 header
6867 * (plus the offset of the link-layer header, if it's
6868 * a variable-length header), in bytes.
6870 * XXX - are there any cases where we want
6871 * off_nl_nosnap?
6872 * XXX - we should, if we're built with
6873 * IPv6 support, generate code to load either
6874 * IPv4, IPv6, or both, as appropriate.
6876 s = gen_loadx_iphdrlen();
6879 * The X register now contains the sum of the length
6880 * of any variable-length header preceding the link-layer
6881 * header, any variable-length link-layer header, and the
6882 * length of the network-layer header.
6884 * Load into the A register the offset relative to
6885 * the beginning of the transport layer header,
6886 * add the X register to that, move that to the
6887 * X register, and load with an offset from the
6888 * X register equal to the offset of the network
6889 * layer header relative to the beginning of
6890 * the MAC-layer payload plus the fixed-length
6891 * portion of the offset of the MAC-layer payload
6892 * from the beginning of the raw packet data.
6894 sappend(s, xfer_to_a(inst));
6895 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6896 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6897 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6898 tmp->s.k = off_macpl + off_nl;
6899 sappend(inst->s, s);
6902 * Do the computation only if the packet contains
6903 * the protocol in question - which is true only
6904 * if this is an IP datagram and is the first or
6905 * only fragment of that datagram.
6907 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6908 if (inst->b)
6909 gen_and(inst->b, b);
6910 #ifdef INET6
6911 gen_and(gen_proto_abbrev(Q_IP), b);
6912 #endif
6913 inst->b = b;
6914 break;
6915 #ifdef INET6
6916 case Q_ICMPV6:
6917 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6918 /*NOTREACHED*/
6919 #endif
6921 inst->regno = regno;
6922 s = new_stmt(BPF_ST);
6923 s->s.k = regno;
6924 sappend(inst->s, s);
6926 return inst;
6929 struct block *
6930 gen_relation(code, a0, a1, reversed)
6931 int code;
6932 struct arth *a0, *a1;
6933 int reversed;
6935 struct slist *s0, *s1, *s2;
6936 struct block *b, *tmp;
6938 s0 = xfer_to_x(a1);
6939 s1 = xfer_to_a(a0);
6940 if (code == BPF_JEQ) {
6941 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6942 b = new_block(JMP(code));
6943 sappend(s1, s2);
6945 else
6946 b = new_block(BPF_JMP|code|BPF_X);
6947 if (reversed)
6948 gen_not(b);
6950 sappend(s0, s1);
6951 sappend(a1->s, s0);
6952 sappend(a0->s, a1->s);
6954 b->stmts = a0->s;
6956 free_reg(a0->regno);
6957 free_reg(a1->regno);
6959 /* 'and' together protocol checks */
6960 if (a0->b) {
6961 if (a1->b) {
6962 gen_and(a0->b, tmp = a1->b);
6964 else
6965 tmp = a0->b;
6966 } else
6967 tmp = a1->b;
6969 if (tmp)
6970 gen_and(tmp, b);
6972 return b;
6975 struct arth *
6976 gen_loadlen()
6978 int regno = alloc_reg();
6979 struct arth *a = (struct arth *)newchunk(sizeof(*a));
6980 struct slist *s;
6982 s = new_stmt(BPF_LD|BPF_LEN);
6983 s->next = new_stmt(BPF_ST);
6984 s->next->s.k = regno;
6985 a->s = s;
6986 a->regno = regno;
6988 return a;
6991 struct arth *
6992 gen_loadi(val)
6993 int val;
6995 struct arth *a;
6996 struct slist *s;
6997 int reg;
6999 a = (struct arth *)newchunk(sizeof(*a));
7001 reg = alloc_reg();
7003 s = new_stmt(BPF_LD|BPF_IMM);
7004 s->s.k = val;
7005 s->next = new_stmt(BPF_ST);
7006 s->next->s.k = reg;
7007 a->s = s;
7008 a->regno = reg;
7010 return a;
7013 struct arth *
7014 gen_neg(a)
7015 struct arth *a;
7017 struct slist *s;
7019 s = xfer_to_a(a);
7020 sappend(a->s, s);
7021 s = new_stmt(BPF_ALU|BPF_NEG);
7022 s->s.k = 0;
7023 sappend(a->s, s);
7024 s = new_stmt(BPF_ST);
7025 s->s.k = a->regno;
7026 sappend(a->s, s);
7028 return a;
7031 struct arth *
7032 gen_arth(code, a0, a1)
7033 int code;
7034 struct arth *a0, *a1;
7036 struct slist *s0, *s1, *s2;
7038 s0 = xfer_to_x(a1);
7039 s1 = xfer_to_a(a0);
7040 s2 = new_stmt(BPF_ALU|BPF_X|code);
7042 sappend(s1, s2);
7043 sappend(s0, s1);
7044 sappend(a1->s, s0);
7045 sappend(a0->s, a1->s);
7047 free_reg(a0->regno);
7048 free_reg(a1->regno);
7050 s0 = new_stmt(BPF_ST);
7051 a0->regno = s0->s.k = alloc_reg();
7052 sappend(a0->s, s0);
7054 return a0;
7058 * Here we handle simple allocation of the scratch registers.
7059 * If too many registers are alloc'd, the allocator punts.
7061 static int regused[BPF_MEMWORDS];
7062 static int curreg;
7065 * Initialize the table of used registers and the current register.
7067 static void
7068 init_regs()
7070 curreg = 0;
7071 memset(regused, 0, sizeof regused);
7075 * Return the next free register.
7077 static int
7078 alloc_reg()
7080 int n = BPF_MEMWORDS;
7082 while (--n >= 0) {
7083 if (regused[curreg])
7084 curreg = (curreg + 1) % BPF_MEMWORDS;
7085 else {
7086 regused[curreg] = 1;
7087 return curreg;
7090 bpf_error("too many registers needed to evaluate expression");
7091 /* NOTREACHED */
7092 return 0;
7096 * Return a register to the table so it can
7097 * be used later.
7099 static void
7100 free_reg(n)
7101 int n;
7103 regused[n] = 0;
7106 static struct block *
7107 gen_len(jmp, n)
7108 int jmp, n;
7110 struct slist *s;
7111 struct block *b;
7113 s = new_stmt(BPF_LD|BPF_LEN);
7114 b = new_block(JMP(jmp));
7115 b->stmts = s;
7116 b->s.k = n;
7118 return b;
7121 struct block *
7122 gen_greater(n)
7123 int n;
7125 return gen_len(BPF_JGE, n);
7129 * Actually, this is less than or equal.
7131 struct block *
7132 gen_less(n)
7133 int n;
7135 struct block *b;
7137 b = gen_len(BPF_JGT, n);
7138 gen_not(b);
7140 return b;
7144 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7145 * the beginning of the link-layer header.
7146 * XXX - that means you can't test values in the radiotap header, but
7147 * as that header is difficult if not impossible to parse generally
7148 * without a loop, that might not be a severe problem. A new keyword
7149 * "radio" could be added for that, although what you'd really want
7150 * would be a way of testing particular radio header values, which
7151 * would generate code appropriate to the radio header in question.
7153 struct block *
7154 gen_byteop(op, idx, val)
7155 int op, idx, val;
7157 struct block *b;
7158 struct slist *s;
7160 switch (op) {
7161 default:
7162 abort();
7164 case '=':
7165 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7167 case '<':
7168 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7169 return b;
7171 case '>':
7172 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7173 return b;
7175 case '|':
7176 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
7177 break;
7179 case '&':
7180 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
7181 break;
7183 s->s.k = val;
7184 b = new_block(JMP(BPF_JEQ));
7185 b->stmts = s;
7186 gen_not(b);
7188 return b;
7191 static u_char abroadcast[] = { 0x0 };
7193 struct block *
7194 gen_broadcast(proto)
7195 int proto;
7197 bpf_u_int32 hostmask;
7198 struct block *b0, *b1, *b2;
7199 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7201 switch (proto) {
7203 case Q_DEFAULT:
7204 case Q_LINK:
7205 switch (linktype) {
7206 case DLT_ARCNET:
7207 case DLT_ARCNET_LINUX:
7208 return gen_ahostop(abroadcast, Q_DST);
7209 case DLT_EN10MB:
7210 case DLT_NETANALYZER:
7211 case DLT_NETANALYZER_TRANSPARENT:
7212 return gen_ehostop(ebroadcast, Q_DST);
7213 case DLT_FDDI:
7214 return gen_fhostop(ebroadcast, Q_DST);
7215 case DLT_IEEE802:
7216 return gen_thostop(ebroadcast, Q_DST);
7217 case DLT_IEEE802_11:
7218 case DLT_PRISM_HEADER:
7219 case DLT_IEEE802_11_RADIO_AVS:
7220 case DLT_IEEE802_11_RADIO:
7221 case DLT_PPI:
7222 return gen_wlanhostop(ebroadcast, Q_DST);
7223 case DLT_IP_OVER_FC:
7224 return gen_ipfchostop(ebroadcast, Q_DST);
7225 case DLT_SUNATM:
7226 if (is_lane) {
7228 * Check that the packet doesn't begin with an
7229 * LE Control marker. (We've already generated
7230 * a test for LANE.)
7232 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7233 BPF_H, 0xFF00);
7234 gen_not(b1);
7237 * Now check the MAC address.
7239 b0 = gen_ehostop(ebroadcast, Q_DST);
7240 gen_and(b1, b0);
7241 return b0;
7243 break;
7244 default:
7245 bpf_error("not a broadcast link");
7247 break;
7249 case Q_IP:
7251 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7252 * as an indication that we don't know the netmask, and fail
7253 * in that case.
7255 if (netmask == PCAP_NETMASK_UNKNOWN)
7256 bpf_error("netmask not known, so 'ip broadcast' not supported");
7257 b0 = gen_linktype(ETHERTYPE_IP);
7258 hostmask = ~netmask;
7259 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7260 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7261 (bpf_int32)(~0 & hostmask), hostmask);
7262 gen_or(b1, b2);
7263 gen_and(b0, b2);
7264 return b2;
7266 bpf_error("only link-layer/IP broadcast filters supported");
7267 /* NOTREACHED */
7268 return NULL;
7272 * Generate code to test the low-order bit of a MAC address (that's
7273 * the bottom bit of the *first* byte).
7275 static struct block *
7276 gen_mac_multicast(offset)
7277 int offset;
7279 register struct block *b0;
7280 register struct slist *s;
7282 /* link[offset] & 1 != 0 */
7283 s = gen_load_a(OR_LINK, offset, BPF_B);
7284 b0 = new_block(JMP(BPF_JSET));
7285 b0->s.k = 1;
7286 b0->stmts = s;
7287 return b0;
7290 struct block *
7291 gen_multicast(proto)
7292 int proto;
7294 register struct block *b0, *b1, *b2;
7295 register struct slist *s;
7297 switch (proto) {
7299 case Q_DEFAULT:
7300 case Q_LINK:
7301 switch (linktype) {
7302 case DLT_ARCNET:
7303 case DLT_ARCNET_LINUX:
7304 /* all ARCnet multicasts use the same address */
7305 return gen_ahostop(abroadcast, Q_DST);
7306 case DLT_EN10MB:
7307 case DLT_NETANALYZER:
7308 case DLT_NETANALYZER_TRANSPARENT:
7309 /* ether[0] & 1 != 0 */
7310 return gen_mac_multicast(0);
7311 case DLT_FDDI:
7313 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7315 * XXX - was that referring to bit-order issues?
7317 /* fddi[1] & 1 != 0 */
7318 return gen_mac_multicast(1);
7319 case DLT_IEEE802:
7320 /* tr[2] & 1 != 0 */
7321 return gen_mac_multicast(2);
7322 case DLT_IEEE802_11:
7323 case DLT_PRISM_HEADER:
7324 case DLT_IEEE802_11_RADIO_AVS:
7325 case DLT_IEEE802_11_RADIO:
7326 case DLT_PPI:
7328 * Oh, yuk.
7330 * For control frames, there is no DA.
7332 * For management frames, DA is at an
7333 * offset of 4 from the beginning of
7334 * the packet.
7336 * For data frames, DA is at an offset
7337 * of 4 from the beginning of the packet
7338 * if To DS is clear and at an offset of
7339 * 16 from the beginning of the packet
7340 * if To DS is set.
7344 * Generate the tests to be done for data frames.
7346 * First, check for To DS set, i.e. "link[1] & 0x01".
7348 s = gen_load_a(OR_LINK, 1, BPF_B);
7349 b1 = new_block(JMP(BPF_JSET));
7350 b1->s.k = 0x01; /* To DS */
7351 b1->stmts = s;
7354 * If To DS is set, the DA is at 16.
7356 b0 = gen_mac_multicast(16);
7357 gen_and(b1, b0);
7360 * Now, check for To DS not set, i.e. check
7361 * "!(link[1] & 0x01)".
7363 s = gen_load_a(OR_LINK, 1, BPF_B);
7364 b2 = new_block(JMP(BPF_JSET));
7365 b2->s.k = 0x01; /* To DS */
7366 b2->stmts = s;
7367 gen_not(b2);
7370 * If To DS is not set, the DA is at 4.
7372 b1 = gen_mac_multicast(4);
7373 gen_and(b2, b1);
7376 * Now OR together the last two checks. That gives
7377 * the complete set of checks for data frames.
7379 gen_or(b1, b0);
7382 * Now check for a data frame.
7383 * I.e, check "link[0] & 0x08".
7385 s = gen_load_a(OR_LINK, 0, BPF_B);
7386 b1 = new_block(JMP(BPF_JSET));
7387 b1->s.k = 0x08;
7388 b1->stmts = s;
7391 * AND that with the checks done for data frames.
7393 gen_and(b1, b0);
7396 * If the high-order bit of the type value is 0, this
7397 * is a management frame.
7398 * I.e, check "!(link[0] & 0x08)".
7400 s = gen_load_a(OR_LINK, 0, BPF_B);
7401 b2 = new_block(JMP(BPF_JSET));
7402 b2->s.k = 0x08;
7403 b2->stmts = s;
7404 gen_not(b2);
7407 * For management frames, the DA is at 4.
7409 b1 = gen_mac_multicast(4);
7410 gen_and(b2, b1);
7413 * OR that with the checks done for data frames.
7414 * That gives the checks done for management and
7415 * data frames.
7417 gen_or(b1, b0);
7420 * If the low-order bit of the type value is 1,
7421 * this is either a control frame or a frame
7422 * with a reserved type, and thus not a
7423 * frame with an SA.
7425 * I.e., check "!(link[0] & 0x04)".
7427 s = gen_load_a(OR_LINK, 0, BPF_B);
7428 b1 = new_block(JMP(BPF_JSET));
7429 b1->s.k = 0x04;
7430 b1->stmts = s;
7431 gen_not(b1);
7434 * AND that with the checks for data and management
7435 * frames.
7437 gen_and(b1, b0);
7438 return b0;
7439 case DLT_IP_OVER_FC:
7440 b0 = gen_mac_multicast(2);
7441 return b0;
7442 case DLT_SUNATM:
7443 if (is_lane) {
7445 * Check that the packet doesn't begin with an
7446 * LE Control marker. (We've already generated
7447 * a test for LANE.)
7449 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7450 BPF_H, 0xFF00);
7451 gen_not(b1);
7453 /* ether[off_mac] & 1 != 0 */
7454 b0 = gen_mac_multicast(off_mac);
7455 gen_and(b1, b0);
7456 return b0;
7458 break;
7459 default:
7460 break;
7462 /* Link not known to support multicasts */
7463 break;
7465 case Q_IP:
7466 b0 = gen_linktype(ETHERTYPE_IP);
7467 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7468 gen_and(b0, b1);
7469 return b1;
7471 #ifdef INET6
7472 case Q_IPV6:
7473 b0 = gen_linktype(ETHERTYPE_IPV6);
7474 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7475 gen_and(b0, b1);
7476 return b1;
7477 #endif /* INET6 */
7479 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7480 /* NOTREACHED */
7481 return NULL;
7485 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7486 * Outbound traffic is sent by this machine, while inbound traffic is
7487 * sent by a remote machine (and may include packets destined for a
7488 * unicast or multicast link-layer address we are not subscribing to).
7489 * These are the same definitions implemented by pcap_setdirection().
7490 * Capturing only unicast traffic destined for this host is probably
7491 * better accomplished using a higher-layer filter.
7493 struct block *
7494 gen_inbound(dir)
7495 int dir;
7497 register struct block *b0;
7500 * Only some data link types support inbound/outbound qualifiers.
7502 switch (linktype) {
7503 case DLT_SLIP:
7504 b0 = gen_relation(BPF_JEQ,
7505 gen_load(Q_LINK, gen_loadi(0), 1),
7506 gen_loadi(0),
7507 dir);
7508 break;
7510 case DLT_IPNET:
7511 if (dir) {
7512 /* match outgoing packets */
7513 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
7514 } else {
7515 /* match incoming packets */
7516 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
7518 break;
7520 case DLT_LINUX_SLL:
7521 /* match outgoing packets */
7522 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7523 if (!dir) {
7524 /* to filter on inbound traffic, invert the match */
7525 gen_not(b0);
7527 break;
7529 #ifdef HAVE_NET_PFVAR_H
7530 case DLT_PFLOG:
7531 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7532 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7533 break;
7534 #endif
7536 case DLT_PPP_PPPD:
7537 if (dir) {
7538 /* match outgoing packets */
7539 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7540 } else {
7541 /* match incoming packets */
7542 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7544 break;
7546 case DLT_JUNIPER_MFR:
7547 case DLT_JUNIPER_MLFR:
7548 case DLT_JUNIPER_MLPPP:
7549 case DLT_JUNIPER_ATM1:
7550 case DLT_JUNIPER_ATM2:
7551 case DLT_JUNIPER_PPPOE:
7552 case DLT_JUNIPER_PPPOE_ATM:
7553 case DLT_JUNIPER_GGSN:
7554 case DLT_JUNIPER_ES:
7555 case DLT_JUNIPER_MONITOR:
7556 case DLT_JUNIPER_SERVICES:
7557 case DLT_JUNIPER_ETHER:
7558 case DLT_JUNIPER_PPP:
7559 case DLT_JUNIPER_FRELAY:
7560 case DLT_JUNIPER_CHDLC:
7561 case DLT_JUNIPER_VP:
7562 case DLT_JUNIPER_ST:
7563 case DLT_JUNIPER_ISM:
7564 case DLT_JUNIPER_VS:
7565 case DLT_JUNIPER_SRX_E2E:
7566 case DLT_JUNIPER_FIBRECHANNEL:
7567 case DLT_JUNIPER_ATM_CEMIC:
7569 /* juniper flags (including direction) are stored
7570 * the byte after the 3-byte magic number */
7571 if (dir) {
7572 /* match outgoing packets */
7573 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7574 } else {
7575 /* match incoming packets */
7576 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7578 break;
7580 default:
7582 * If we have packet meta-data indicating a direction,
7583 * check it, otherwise give up as this link-layer type
7584 * has nothing in the packet data.
7586 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7588 * We infer that this is Linux with PF_PACKET support.
7589 * If this is a *live* capture, we can look at
7590 * special meta-data in the filter expression;
7591 * if it's a savefile, we can't.
7593 if (bpf_pcap->sf.rfile != NULL) {
7594 /* We have a FILE *, so this is a savefile */
7595 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7596 linktype);
7597 b0 = NULL;
7598 /* NOTREACHED */
7600 /* match outgoing packets */
7601 b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
7602 PACKET_OUTGOING);
7603 if (!dir) {
7604 /* to filter on inbound traffic, invert the match */
7605 gen_not(b0);
7607 #else /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7608 bpf_error("inbound/outbound not supported on linktype %d",
7609 linktype);
7610 b0 = NULL;
7611 /* NOTREACHED */
7612 #endif /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7614 return (b0);
7617 #ifdef HAVE_NET_PFVAR_H
7618 /* PF firewall log matched interface */
7619 struct block *
7620 gen_pf_ifname(const char *ifname)
7622 struct block *b0;
7623 u_int len, off;
7625 if (linktype != DLT_PFLOG) {
7626 bpf_error("ifname supported only on PF linktype");
7627 /* NOTREACHED */
7629 len = sizeof(((struct pfloghdr *)0)->ifname);
7630 off = offsetof(struct pfloghdr, ifname);
7631 if (strlen(ifname) >= len) {
7632 bpf_error("ifname interface names can only be %d characters",
7633 len-1);
7634 /* NOTREACHED */
7636 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7637 return (b0);
7640 /* PF firewall log ruleset name */
7641 struct block *
7642 gen_pf_ruleset(char *ruleset)
7644 struct block *b0;
7646 if (linktype != DLT_PFLOG) {
7647 bpf_error("ruleset supported only on PF linktype");
7648 /* NOTREACHED */
7651 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7652 bpf_error("ruleset names can only be %ld characters",
7653 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7654 /* NOTREACHED */
7657 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7658 strlen(ruleset), (const u_char *)ruleset);
7659 return (b0);
7662 /* PF firewall log rule number */
7663 struct block *
7664 gen_pf_rnr(int rnr)
7666 struct block *b0;
7668 if (linktype != DLT_PFLOG) {
7669 bpf_error("rnr supported only on PF linktype");
7670 /* NOTREACHED */
7673 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7674 (bpf_int32)rnr);
7675 return (b0);
7678 /* PF firewall log sub-rule number */
7679 struct block *
7680 gen_pf_srnr(int srnr)
7682 struct block *b0;
7684 if (linktype != DLT_PFLOG) {
7685 bpf_error("srnr supported only on PF linktype");
7686 /* NOTREACHED */
7689 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7690 (bpf_int32)srnr);
7691 return (b0);
7694 /* PF firewall log reason code */
7695 struct block *
7696 gen_pf_reason(int reason)
7698 struct block *b0;
7700 if (linktype != DLT_PFLOG) {
7701 bpf_error("reason supported only on PF linktype");
7702 /* NOTREACHED */
7705 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7706 (bpf_int32)reason);
7707 return (b0);
7710 /* PF firewall log action */
7711 struct block *
7712 gen_pf_action(int action)
7714 struct block *b0;
7716 if (linktype != DLT_PFLOG) {
7717 bpf_error("action supported only on PF linktype");
7718 /* NOTREACHED */
7721 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7722 (bpf_int32)action);
7723 return (b0);
7725 #else /* !HAVE_NET_PFVAR_H */
7726 struct block *
7727 gen_pf_ifname(const char *ifname)
7729 bpf_error("libpcap was compiled without pf support");
7730 /* NOTREACHED */
7731 return (NULL);
7734 struct block *
7735 gen_pf_ruleset(char *ruleset)
7737 bpf_error("libpcap was compiled on a machine without pf support");
7738 /* NOTREACHED */
7739 return (NULL);
7742 struct block *
7743 gen_pf_rnr(int rnr)
7745 bpf_error("libpcap was compiled on a machine without pf support");
7746 /* NOTREACHED */
7747 return (NULL);
7750 struct block *
7751 gen_pf_srnr(int srnr)
7753 bpf_error("libpcap was compiled on a machine without pf support");
7754 /* NOTREACHED */
7755 return (NULL);
7758 struct block *
7759 gen_pf_reason(int reason)
7761 bpf_error("libpcap was compiled on a machine without pf support");
7762 /* NOTREACHED */
7763 return (NULL);
7766 struct block *
7767 gen_pf_action(int action)
7769 bpf_error("libpcap was compiled on a machine without pf support");
7770 /* NOTREACHED */
7771 return (NULL);
7773 #endif /* HAVE_NET_PFVAR_H */
7775 /* IEEE 802.11 wireless header */
7776 struct block *
7777 gen_p80211_type(int type, int mask)
7779 struct block *b0;
7781 switch (linktype) {
7783 case DLT_IEEE802_11:
7784 case DLT_PRISM_HEADER:
7785 case DLT_IEEE802_11_RADIO_AVS:
7786 case DLT_IEEE802_11_RADIO:
7787 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7788 (bpf_int32)mask);
7789 break;
7791 default:
7792 bpf_error("802.11 link-layer types supported only on 802.11");
7793 /* NOTREACHED */
7796 return (b0);
7799 struct block *
7800 gen_p80211_fcdir(int fcdir)
7802 struct block *b0;
7804 switch (linktype) {
7806 case DLT_IEEE802_11:
7807 case DLT_PRISM_HEADER:
7808 case DLT_IEEE802_11_RADIO_AVS:
7809 case DLT_IEEE802_11_RADIO:
7810 break;
7812 default:
7813 bpf_error("frame direction supported only with 802.11 headers");
7814 /* NOTREACHED */
7817 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7818 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7820 return (b0);
7823 struct block *
7824 gen_acode(eaddr, q)
7825 register const u_char *eaddr;
7826 struct qual q;
7828 switch (linktype) {
7830 case DLT_ARCNET:
7831 case DLT_ARCNET_LINUX:
7832 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7833 q.proto == Q_LINK)
7834 return (gen_ahostop(eaddr, (int)q.dir));
7835 else {
7836 bpf_error("ARCnet address used in non-arc expression");
7837 /* NOTREACHED */
7839 break;
7841 default:
7842 bpf_error("aid supported only on ARCnet");
7843 /* NOTREACHED */
7845 bpf_error("ARCnet address used in non-arc expression");
7846 /* NOTREACHED */
7847 return NULL;
7850 static struct block *
7851 gen_ahostop(eaddr, dir)
7852 register const u_char *eaddr;
7853 register int dir;
7855 register struct block *b0, *b1;
7857 switch (dir) {
7858 /* src comes first, different from Ethernet */
7859 case Q_SRC:
7860 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7862 case Q_DST:
7863 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7865 case Q_AND:
7866 b0 = gen_ahostop(eaddr, Q_SRC);
7867 b1 = gen_ahostop(eaddr, Q_DST);
7868 gen_and(b0, b1);
7869 return b1;
7871 case Q_DEFAULT:
7872 case Q_OR:
7873 b0 = gen_ahostop(eaddr, Q_SRC);
7874 b1 = gen_ahostop(eaddr, Q_DST);
7875 gen_or(b0, b1);
7876 return b1;
7878 case Q_ADDR1:
7879 bpf_error("'addr1' is only supported on 802.11");
7880 break;
7882 case Q_ADDR2:
7883 bpf_error("'addr2' is only supported on 802.11");
7884 break;
7886 case Q_ADDR3:
7887 bpf_error("'addr3' is only supported on 802.11");
7888 break;
7890 case Q_ADDR4:
7891 bpf_error("'addr4' is only supported on 802.11");
7892 break;
7894 case Q_RA:
7895 bpf_error("'ra' is only supported on 802.11");
7896 break;
7898 case Q_TA:
7899 bpf_error("'ta' is only supported on 802.11");
7900 break;
7902 abort();
7903 /* NOTREACHED */
7907 * support IEEE 802.1Q VLAN trunk over ethernet
7909 struct block *
7910 gen_vlan(vlan_num)
7911 int vlan_num;
7913 struct block *b0, *b1;
7915 /* can't check for VLAN-encapsulated packets inside MPLS */
7916 if (label_stack_depth > 0)
7917 bpf_error("no VLAN match after MPLS");
7920 * Check for a VLAN packet, and then change the offsets to point
7921 * to the type and data fields within the VLAN packet. Just
7922 * increment the offsets, so that we can support a hierarchy, e.g.
7923 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7924 * VLAN 100.
7926 * XXX - this is a bit of a kludge. If we were to split the
7927 * compiler into a parser that parses an expression and
7928 * generates an expression tree, and a code generator that
7929 * takes an expression tree (which could come from our
7930 * parser or from some other parser) and generates BPF code,
7931 * we could perhaps make the offsets parameters of routines
7932 * and, in the handler for an "AND" node, pass to subnodes
7933 * other than the VLAN node the adjusted offsets.
7935 * This would mean that "vlan" would, instead of changing the
7936 * behavior of *all* tests after it, change only the behavior
7937 * of tests ANDed with it. That would change the documented
7938 * semantics of "vlan", which might break some expressions.
7939 * However, it would mean that "(vlan and ip) or ip" would check
7940 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7941 * checking only for VLAN-encapsulated IP, so that could still
7942 * be considered worth doing; it wouldn't break expressions
7943 * that are of the form "vlan and ..." or "vlan N and ...",
7944 * which I suspect are the most common expressions involving
7945 * "vlan". "vlan or ..." doesn't necessarily do what the user
7946 * would really want, now, as all the "or ..." tests would
7947 * be done assuming a VLAN, even though the "or" could be viewed
7948 * as meaning "or, if this isn't a VLAN packet...".
7950 orig_nl = off_nl;
7952 switch (linktype) {
7954 case DLT_EN10MB:
7955 case DLT_NETANALYZER:
7956 case DLT_NETANALYZER_TRANSPARENT:
7957 /* check for VLAN, including QinQ */
7958 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7959 (bpf_int32)ETHERTYPE_8021Q);
7960 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7961 (bpf_int32)ETHERTYPE_8021QINQ);
7962 gen_or(b0,b1);
7963 b0 = b1;
7965 /* If a specific VLAN is requested, check VLAN id */
7966 if (vlan_num >= 0) {
7967 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7968 (bpf_int32)vlan_num, 0x0fff);
7969 gen_and(b0, b1);
7970 b0 = b1;
7973 off_macpl += 4;
7974 off_linktype += 4;
7975 #if 0
7976 off_nl_nosnap += 4;
7977 off_nl += 4;
7978 #endif
7979 break;
7981 default:
7982 bpf_error("no VLAN support for data link type %d",
7983 linktype);
7984 /*NOTREACHED*/
7987 return (b0);
7991 * support for MPLS
7993 struct block *
7994 gen_mpls(label_num)
7995 int label_num;
7997 struct block *b0,*b1;
8000 * Change the offsets to point to the type and data fields within
8001 * the MPLS packet. Just increment the offsets, so that we
8002 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8003 * capture packets with an outer label of 100000 and an inner
8004 * label of 1024.
8006 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8008 orig_nl = off_nl;
8010 if (label_stack_depth > 0) {
8011 /* just match the bottom-of-stack bit clear */
8012 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
8013 } else {
8015 * Indicate that we're checking MPLS-encapsulated headers,
8016 * to make sure higher level code generators don't try to
8017 * match against IP-related protocols such as Q_ARP, Q_RARP
8018 * etc.
8020 switch (linktype) {
8022 case DLT_C_HDLC: /* fall through */
8023 case DLT_EN10MB:
8024 case DLT_NETANALYZER:
8025 case DLT_NETANALYZER_TRANSPARENT:
8026 b0 = gen_linktype(ETHERTYPE_MPLS);
8027 break;
8029 case DLT_PPP:
8030 b0 = gen_linktype(PPP_MPLS_UCAST);
8031 break;
8033 /* FIXME add other DLT_s ...
8034 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8035 * leave it for now */
8037 default:
8038 bpf_error("no MPLS support for data link type %d",
8039 linktype);
8040 b0 = NULL;
8041 /*NOTREACHED*/
8042 break;
8046 /* If a specific MPLS label is requested, check it */
8047 if (label_num >= 0) {
8048 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
8049 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
8050 0xfffff000); /* only compare the first 20 bits */
8051 gen_and(b0, b1);
8052 b0 = b1;
8055 off_nl_nosnap += 4;
8056 off_nl += 4;
8057 label_stack_depth++;
8058 return (b0);
8062 * Support PPPOE discovery and session.
8064 struct block *
8065 gen_pppoed()
8067 /* check for PPPoE discovery */
8068 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
8071 struct block *
8072 gen_pppoes()
8074 struct block *b0;
8077 * Test against the PPPoE session link-layer type.
8079 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
8082 * Change the offsets to point to the type and data fields within
8083 * the PPP packet, and note that this is PPPoE rather than
8084 * raw PPP.
8086 * XXX - this is a bit of a kludge. If we were to split the
8087 * compiler into a parser that parses an expression and
8088 * generates an expression tree, and a code generator that
8089 * takes an expression tree (which could come from our
8090 * parser or from some other parser) and generates BPF code,
8091 * we could perhaps make the offsets parameters of routines
8092 * and, in the handler for an "AND" node, pass to subnodes
8093 * other than the PPPoE node the adjusted offsets.
8095 * This would mean that "pppoes" would, instead of changing the
8096 * behavior of *all* tests after it, change only the behavior
8097 * of tests ANDed with it. That would change the documented
8098 * semantics of "pppoes", which might break some expressions.
8099 * However, it would mean that "(pppoes and ip) or ip" would check
8100 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8101 * checking only for VLAN-encapsulated IP, so that could still
8102 * be considered worth doing; it wouldn't break expressions
8103 * that are of the form "pppoes and ..." which I suspect are the
8104 * most common expressions involving "pppoes". "pppoes or ..."
8105 * doesn't necessarily do what the user would really want, now,
8106 * as all the "or ..." tests would be done assuming PPPoE, even
8107 * though the "or" could be viewed as meaning "or, if this isn't
8108 * a PPPoE packet...".
8110 orig_linktype = off_linktype; /* save original values */
8111 orig_nl = off_nl;
8112 is_pppoes = 1;
8115 * The "network-layer" protocol is PPPoE, which has a 6-byte
8116 * PPPoE header, followed by a PPP packet.
8118 * There is no HDLC encapsulation for the PPP packet (it's
8119 * encapsulated in PPPoES instead), so the link-layer type
8120 * starts at the first byte of the PPP packet. For PPPoE,
8121 * that offset is relative to the beginning of the total
8122 * link-layer payload, including any 802.2 LLC header, so
8123 * it's 6 bytes past off_nl.
8125 off_linktype = off_nl + 6;
8128 * The network-layer offsets are relative to the beginning
8129 * of the MAC-layer payload; that's past the 6-byte
8130 * PPPoE header and the 2-byte PPP header.
8132 off_nl = 6+2;
8133 off_nl_nosnap = 6+2;
8135 return b0;
8138 struct block *
8139 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
8140 int atmfield;
8141 bpf_int32 jvalue;
8142 bpf_u_int32 jtype;
8143 int reverse;
8145 struct block *b0;
8147 switch (atmfield) {
8149 case A_VPI:
8150 if (!is_atm)
8151 bpf_error("'vpi' supported only on raw ATM");
8152 if (off_vpi == (u_int)-1)
8153 abort();
8154 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
8155 reverse, jvalue);
8156 break;
8158 case A_VCI:
8159 if (!is_atm)
8160 bpf_error("'vci' supported only on raw ATM");
8161 if (off_vci == (u_int)-1)
8162 abort();
8163 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
8164 reverse, jvalue);
8165 break;
8167 case A_PROTOTYPE:
8168 if (off_proto == (u_int)-1)
8169 abort(); /* XXX - this isn't on FreeBSD */
8170 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
8171 reverse, jvalue);
8172 break;
8174 case A_MSGTYPE:
8175 if (off_payload == (u_int)-1)
8176 abort();
8177 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
8178 0xffffffff, jtype, reverse, jvalue);
8179 break;
8181 case A_CALLREFTYPE:
8182 if (!is_atm)
8183 bpf_error("'callref' supported only on raw ATM");
8184 if (off_proto == (u_int)-1)
8185 abort();
8186 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
8187 jtype, reverse, jvalue);
8188 break;
8190 default:
8191 abort();
8193 return b0;
8196 struct block *
8197 gen_atmtype_abbrev(type)
8198 int type;
8200 struct block *b0, *b1;
8202 switch (type) {
8204 case A_METAC:
8205 /* Get all packets in Meta signalling Circuit */
8206 if (!is_atm)
8207 bpf_error("'metac' supported only on raw ATM");
8208 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8209 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
8210 gen_and(b0, b1);
8211 break;
8213 case A_BCC:
8214 /* Get all packets in Broadcast Circuit*/
8215 if (!is_atm)
8216 bpf_error("'bcc' supported only on raw ATM");
8217 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8218 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
8219 gen_and(b0, b1);
8220 break;
8222 case A_OAMF4SC:
8223 /* Get all cells in Segment OAM F4 circuit*/
8224 if (!is_atm)
8225 bpf_error("'oam4sc' supported only on raw ATM");
8226 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8227 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8228 gen_and(b0, b1);
8229 break;
8231 case A_OAMF4EC:
8232 /* Get all cells in End-to-End OAM F4 Circuit*/
8233 if (!is_atm)
8234 bpf_error("'oam4ec' supported only on raw ATM");
8235 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8236 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8237 gen_and(b0, b1);
8238 break;
8240 case A_SC:
8241 /* Get all packets in connection Signalling Circuit */
8242 if (!is_atm)
8243 bpf_error("'sc' supported only on raw ATM");
8244 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8245 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
8246 gen_and(b0, b1);
8247 break;
8249 case A_ILMIC:
8250 /* Get all packets in ILMI Circuit */
8251 if (!is_atm)
8252 bpf_error("'ilmic' supported only on raw ATM");
8253 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8254 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
8255 gen_and(b0, b1);
8256 break;
8258 case A_LANE:
8259 /* Get all LANE packets */
8260 if (!is_atm)
8261 bpf_error("'lane' supported only on raw ATM");
8262 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8265 * Arrange that all subsequent tests assume LANE
8266 * rather than LLC-encapsulated packets, and set
8267 * the offsets appropriately for LANE-encapsulated
8268 * Ethernet.
8270 * "off_mac" is the offset of the Ethernet header,
8271 * which is 2 bytes past the ATM pseudo-header
8272 * (skipping the pseudo-header and 2-byte LE Client
8273 * field). The other offsets are Ethernet offsets
8274 * relative to "off_mac".
8276 is_lane = 1;
8277 off_mac = off_payload + 2; /* MAC header */
8278 off_linktype = off_mac + 12;
8279 off_macpl = off_mac + 14; /* Ethernet */
8280 off_nl = 0; /* Ethernet II */
8281 off_nl_nosnap = 3; /* 802.3+802.2 */
8282 break;
8284 case A_LLC:
8285 /* Get all LLC-encapsulated packets */
8286 if (!is_atm)
8287 bpf_error("'llc' supported only on raw ATM");
8288 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8289 is_lane = 0;
8290 break;
8292 default:
8293 abort();
8295 return b1;
8299 * Filtering for MTP2 messages based on li value
8300 * FISU, length is null
8301 * LSSU, length is 1 or 2
8302 * MSU, length is 3 or more
8304 struct block *
8305 gen_mtp2type_abbrev(type)
8306 int type;
8308 struct block *b0, *b1;
8310 switch (type) {
8312 case M_FISU:
8313 if ( (linktype != DLT_MTP2) &&
8314 (linktype != DLT_ERF) &&
8315 (linktype != DLT_MTP2_WITH_PHDR) )
8316 bpf_error("'fisu' supported only on MTP2");
8317 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8318 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8319 break;
8321 case M_LSSU:
8322 if ( (linktype != DLT_MTP2) &&
8323 (linktype != DLT_ERF) &&
8324 (linktype != DLT_MTP2_WITH_PHDR) )
8325 bpf_error("'lssu' supported only on MTP2");
8326 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8327 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8328 gen_and(b1, b0);
8329 break;
8331 case M_MSU:
8332 if ( (linktype != DLT_MTP2) &&
8333 (linktype != DLT_ERF) &&
8334 (linktype != DLT_MTP2_WITH_PHDR) )
8335 bpf_error("'msu' supported only on MTP2");
8336 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8337 break;
8339 default:
8340 abort();
8342 return b0;
8345 struct block *
8346 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8347 int mtp3field;
8348 bpf_u_int32 jvalue;
8349 bpf_u_int32 jtype;
8350 int reverse;
8352 struct block *b0;
8353 bpf_u_int32 val1 , val2 , val3;
8355 switch (mtp3field) {
8357 case M_SIO:
8358 if (off_sio == (u_int)-1)
8359 bpf_error("'sio' supported only on SS7");
8360 /* sio coded on 1 byte so max value 255 */
8361 if(jvalue > 255)
8362 bpf_error("sio value %u too big; max value = 255",
8363 jvalue);
8364 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8365 (u_int)jtype, reverse, (u_int)jvalue);
8366 break;
8368 case M_OPC:
8369 if (off_opc == (u_int)-1)
8370 bpf_error("'opc' supported only on SS7");
8371 /* opc coded on 14 bits so max value 16383 */
8372 if (jvalue > 16383)
8373 bpf_error("opc value %u too big; max value = 16383",
8374 jvalue);
8375 /* the following instructions are made to convert jvalue
8376 * to the form used to write opc in an ss7 message*/
8377 val1 = jvalue & 0x00003c00;
8378 val1 = val1 >>10;
8379 val2 = jvalue & 0x000003fc;
8380 val2 = val2 <<6;
8381 val3 = jvalue & 0x00000003;
8382 val3 = val3 <<22;
8383 jvalue = val1 + val2 + val3;
8384 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8385 (u_int)jtype, reverse, (u_int)jvalue);
8386 break;
8388 case M_DPC:
8389 if (off_dpc == (u_int)-1)
8390 bpf_error("'dpc' supported only on SS7");
8391 /* dpc coded on 14 bits so max value 16383 */
8392 if (jvalue > 16383)
8393 bpf_error("dpc value %u too big; max value = 16383",
8394 jvalue);
8395 /* the following instructions are made to convert jvalue
8396 * to the forme used to write dpc in an ss7 message*/
8397 val1 = jvalue & 0x000000ff;
8398 val1 = val1 << 24;
8399 val2 = jvalue & 0x00003f00;
8400 val2 = val2 << 8;
8401 jvalue = val1 + val2;
8402 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8403 (u_int)jtype, reverse, (u_int)jvalue);
8404 break;
8406 case M_SLS:
8407 if (off_sls == (u_int)-1)
8408 bpf_error("'sls' supported only on SS7");
8409 /* sls coded on 4 bits so max value 15 */
8410 if (jvalue > 15)
8411 bpf_error("sls value %u too big; max value = 15",
8412 jvalue);
8413 /* the following instruction is made to convert jvalue
8414 * to the forme used to write sls in an ss7 message*/
8415 jvalue = jvalue << 4;
8416 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8417 (u_int)jtype,reverse, (u_int)jvalue);
8418 break;
8420 default:
8421 abort();
8423 return b0;
8426 static struct block *
8427 gen_msg_abbrev(type)
8428 int type;
8430 struct block *b1;
8433 * Q.2931 signalling protocol messages for handling virtual circuits
8434 * establishment and teardown
8436 switch (type) {
8438 case A_SETUP:
8439 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8440 break;
8442 case A_CALLPROCEED:
8443 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8444 break;
8446 case A_CONNECT:
8447 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8448 break;
8450 case A_CONNECTACK:
8451 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8452 break;
8454 case A_RELEASE:
8455 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8456 break;
8458 case A_RELEASE_DONE:
8459 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8460 break;
8462 default:
8463 abort();
8465 return b1;
8468 struct block *
8469 gen_atmmulti_abbrev(type)
8470 int type;
8472 struct block *b0, *b1;
8474 switch (type) {
8476 case A_OAM:
8477 if (!is_atm)
8478 bpf_error("'oam' supported only on raw ATM");
8479 b1 = gen_atmmulti_abbrev(A_OAMF4);
8480 break;
8482 case A_OAMF4:
8483 if (!is_atm)
8484 bpf_error("'oamf4' supported only on raw ATM");
8485 /* OAM F4 type */
8486 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8487 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8488 gen_or(b0, b1);
8489 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8490 gen_and(b0, b1);
8491 break;
8493 case A_CONNECTMSG:
8495 * Get Q.2931 signalling messages for switched
8496 * virtual connection
8498 if (!is_atm)
8499 bpf_error("'connectmsg' supported only on raw ATM");
8500 b0 = gen_msg_abbrev(A_SETUP);
8501 b1 = gen_msg_abbrev(A_CALLPROCEED);
8502 gen_or(b0, b1);
8503 b0 = gen_msg_abbrev(A_CONNECT);
8504 gen_or(b0, b1);
8505 b0 = gen_msg_abbrev(A_CONNECTACK);
8506 gen_or(b0, b1);
8507 b0 = gen_msg_abbrev(A_RELEASE);
8508 gen_or(b0, b1);
8509 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8510 gen_or(b0, b1);
8511 b0 = gen_atmtype_abbrev(A_SC);
8512 gen_and(b0, b1);
8513 break;
8515 case A_METACONNECT:
8516 if (!is_atm)
8517 bpf_error("'metaconnect' supported only on raw ATM");
8518 b0 = gen_msg_abbrev(A_SETUP);
8519 b1 = gen_msg_abbrev(A_CALLPROCEED);
8520 gen_or(b0, b1);
8521 b0 = gen_msg_abbrev(A_CONNECT);
8522 gen_or(b0, b1);
8523 b0 = gen_msg_abbrev(A_RELEASE);
8524 gen_or(b0, b1);
8525 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8526 gen_or(b0, b1);
8527 b0 = gen_atmtype_abbrev(A_METAC);
8528 gen_and(b0, b1);
8529 break;
8531 default:
8532 abort();
8534 return b1;