Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / libpcap / gencode.c
blob696bab21e612de9dd9b9c923502d2df5880cbb62
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/pfvar.h>
96 #include <net/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 #ifdef HAVE_REMOTE
430 Check if:
431 - We are on an remote capture
432 - we do not want to capture RPCAP traffic
434 If so, we have to save the current filter, because we have to add some
435 piece of stuff later
437 if ( (p->rmt_clientside) && (p->rmt_flags & PCAP_OPENFLAG_NOCAPTURE_RPCAP) )
439 int bufferlen;
441 if (p->currentfilter)
442 free (p->currentfilter);
444 if (buf)
445 bufferlen= strlen(buf) + 1;
446 else
447 bufferlen= 1;
449 p->currentfilter= (char *) malloc( sizeof(char) * bufferlen);
451 strncpy(p->currentfilter, buf, bufferlen);
453 p->currentfilter[bufferlen - 1]= 0;
455 #endif /* HAVE_REMOTE */
457 no_optimize = 0;
458 n_errors = 0;
459 root = NULL;
460 bpf_pcap = p;
461 init_regs();
462 if (setjmp(top_ctx)) {
463 #ifdef INET6
464 if (ai != NULL) {
465 freeaddrinfo(ai);
466 ai = NULL;
468 #endif
469 lex_cleanup();
470 freechunks();
471 return (-1);
474 netmask = mask;
476 snaplen = pcap_snapshot(p);
477 if (snaplen == 0) {
478 snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
479 "snaplen of 0 rejects all packets");
480 return -1;
483 lex_init(xbuf ? xbuf : "");
484 init_linktype(p);
485 (void)pcap_parse();
487 if (n_errors)
488 syntax();
490 if (root == NULL)
491 root = gen_retblk(snaplen);
493 if (optimize && !no_optimize) {
494 bpf_optimize(&root);
495 if (root == NULL ||
496 (root->s.code == (BPF_RET|BPF_K) && root->s.k == 0))
497 bpf_error("expression rejects all packets");
499 program->bf_insns = icode_to_fcode(root, &len);
500 program->bf_len = len;
502 lex_cleanup();
503 freechunks();
504 return (0);
508 * entry point for using the compiler with no pcap open
509 * pass in all the stuff that is needed explicitly instead.
512 pcap_compile_nopcap(int snaplen_arg, int linktype_arg,
513 struct bpf_program *program,
514 const char *buf, int optimize, bpf_u_int32 mask)
516 pcap_t *p;
517 int ret;
519 p = pcap_open_dead(linktype_arg, snaplen_arg);
520 if (p == NULL)
521 return (-1);
522 ret = pcap_compile(p, program, buf, optimize, mask);
523 pcap_close(p);
524 return (ret);
528 * Clean up a "struct bpf_program" by freeing all the memory allocated
529 * in it.
531 void
532 pcap_freecode(struct bpf_program *program)
534 program->bf_len = 0;
535 if (program->bf_insns != NULL) {
536 free((char *)program->bf_insns);
537 program->bf_insns = NULL;
542 * Backpatch the blocks in 'list' to 'target'. The 'sense' field indicates
543 * which of the jt and jf fields has been resolved and which is a pointer
544 * back to another unresolved block (or nil). At least one of the fields
545 * in each block is already resolved.
547 static void
548 backpatch(list, target)
549 struct block *list, *target;
551 struct block *next;
553 while (list) {
554 if (!list->sense) {
555 next = JT(list);
556 JT(list) = target;
557 } else {
558 next = JF(list);
559 JF(list) = target;
561 list = next;
566 * Merge the lists in b0 and b1, using the 'sense' field to indicate
567 * which of jt and jf is the link.
569 static void
570 merge(b0, b1)
571 struct block *b0, *b1;
573 register struct block **p = &b0;
575 /* Find end of list. */
576 while (*p)
577 p = !((*p)->sense) ? &JT(*p) : &JF(*p);
579 /* Concatenate the lists. */
580 *p = b1;
583 void
584 finish_parse(p)
585 struct block *p;
587 struct block *ppi_dlt_check;
590 * Insert before the statements of the first (root) block any
591 * statements needed to load the lengths of any variable-length
592 * headers into registers.
594 * XXX - a fancier strategy would be to insert those before the
595 * statements of all blocks that use those lengths and that
596 * have no predecessors that use them, so that we only compute
597 * the lengths if we need them. There might be even better
598 * approaches than that.
600 * However, those strategies would be more complicated, and
601 * as we don't generate code to compute a length if the
602 * program has no tests that use the length, and as most
603 * tests will probably use those lengths, we would just
604 * postpone computing the lengths so that it's not done
605 * for tests that fail early, and it's not clear that's
606 * worth the effort.
608 insert_compute_vloffsets(p->head);
611 * For DLT_PPI captures, generate a check of the per-packet
612 * DLT value to make sure it's DLT_IEEE802_11.
614 ppi_dlt_check = gen_ppi_dlt_check();
615 if (ppi_dlt_check != NULL)
616 gen_and(ppi_dlt_check, p);
618 backpatch(p, gen_retblk(snaplen));
619 p->sense = !p->sense;
620 backpatch(p, gen_retblk(0));
621 root = p->head;
624 void
625 gen_and(b0, b1)
626 struct block *b0, *b1;
628 backpatch(b0, b1->head);
629 b0->sense = !b0->sense;
630 b1->sense = !b1->sense;
631 merge(b1, b0);
632 b1->sense = !b1->sense;
633 b1->head = b0->head;
636 void
637 gen_or(b0, b1)
638 struct block *b0, *b1;
640 b0->sense = !b0->sense;
641 backpatch(b0, b1->head);
642 b0->sense = !b0->sense;
643 merge(b1, b0);
644 b1->head = b0->head;
647 void
648 gen_not(b)
649 struct block *b;
651 b->sense = !b->sense;
654 static struct block *
655 gen_cmp(offrel, offset, size, v)
656 enum e_offrel offrel;
657 u_int offset, size;
658 bpf_int32 v;
660 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JEQ, 0, v);
663 static struct block *
664 gen_cmp_gt(offrel, offset, size, v)
665 enum e_offrel offrel;
666 u_int offset, size;
667 bpf_int32 v;
669 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 0, v);
672 static struct block *
673 gen_cmp_ge(offrel, offset, size, v)
674 enum e_offrel offrel;
675 u_int offset, size;
676 bpf_int32 v;
678 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 0, v);
681 static struct block *
682 gen_cmp_lt(offrel, offset, size, v)
683 enum e_offrel offrel;
684 u_int offset, size;
685 bpf_int32 v;
687 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGE, 1, v);
690 static struct block *
691 gen_cmp_le(offrel, offset, size, v)
692 enum e_offrel offrel;
693 u_int offset, size;
694 bpf_int32 v;
696 return gen_ncmp(offrel, offset, size, 0xffffffff, BPF_JGT, 1, v);
699 static struct block *
700 gen_mcmp(offrel, offset, size, v, mask)
701 enum e_offrel offrel;
702 u_int offset, size;
703 bpf_int32 v;
704 bpf_u_int32 mask;
706 return gen_ncmp(offrel, offset, size, mask, BPF_JEQ, 0, v);
709 static struct block *
710 gen_bcmp(offrel, offset, size, v)
711 enum e_offrel offrel;
712 register u_int offset, size;
713 register const u_char *v;
715 register struct block *b, *tmp;
717 b = NULL;
718 while (size >= 4) {
719 register const u_char *p = &v[size - 4];
720 bpf_int32 w = ((bpf_int32)p[0] << 24) |
721 ((bpf_int32)p[1] << 16) | ((bpf_int32)p[2] << 8) | p[3];
723 tmp = gen_cmp(offrel, offset + size - 4, BPF_W, w);
724 if (b != NULL)
725 gen_and(b, tmp);
726 b = tmp;
727 size -= 4;
729 while (size >= 2) {
730 register const u_char *p = &v[size - 2];
731 bpf_int32 w = ((bpf_int32)p[0] << 8) | p[1];
733 tmp = gen_cmp(offrel, offset + size - 2, BPF_H, w);
734 if (b != NULL)
735 gen_and(b, tmp);
736 b = tmp;
737 size -= 2;
739 if (size > 0) {
740 tmp = gen_cmp(offrel, offset, BPF_B, (bpf_int32)v[0]);
741 if (b != NULL)
742 gen_and(b, tmp);
743 b = tmp;
745 return b;
749 * AND the field of size "size" at offset "offset" relative to the header
750 * specified by "offrel" with "mask", and compare it with the value "v"
751 * with the test specified by "jtype"; if "reverse" is true, the test
752 * should test the opposite of "jtype".
754 static struct block *
755 gen_ncmp(offrel, offset, size, mask, jtype, reverse, v)
756 enum e_offrel offrel;
757 bpf_int32 v;
758 bpf_u_int32 offset, size, mask, jtype;
759 int reverse;
761 struct slist *s, *s2;
762 struct block *b;
764 s = gen_load_a(offrel, offset, size);
766 if (mask != 0xffffffff) {
767 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
768 s2->s.k = mask;
769 sappend(s, s2);
772 b = new_block(JMP(jtype));
773 b->stmts = s;
774 b->s.k = v;
775 if (reverse && (jtype == BPF_JGT || jtype == BPF_JGE))
776 gen_not(b);
777 return b;
781 * Various code constructs need to know the layout of the data link
782 * layer. These variables give the necessary offsets from the beginning
783 * of the packet data.
787 * This is the offset of the beginning of the link-layer header from
788 * the beginning of the raw packet data.
790 * It's usually 0, except for 802.11 with a fixed-length radio header.
791 * (For 802.11 with a variable-length radio header, we have to generate
792 * code to compute that offset; off_ll is 0 in that case.)
794 static u_int off_ll;
797 * If there's a variable-length header preceding the link-layer header,
798 * "reg_off_ll" is the register number for a register containing the
799 * length of that header, and therefore the offset of the link-layer
800 * header from the beginning of the raw packet data. Otherwise,
801 * "reg_off_ll" is -1.
803 static int reg_off_ll;
806 * This is the offset of the beginning of the MAC-layer header from
807 * the beginning of the link-layer header.
808 * It's usually 0, except for ATM LANE, where it's the offset, relative
809 * to the beginning of the raw packet data, of the Ethernet header, and
810 * for Ethernet with various additional information.
812 static u_int off_mac;
815 * This is the offset of the beginning of the MAC-layer payload,
816 * from the beginning of the raw packet data.
818 * I.e., it's the sum of the length of the link-layer header (without,
819 * for example, any 802.2 LLC header, so it's the MAC-layer
820 * portion of that header), plus any prefix preceding the
821 * link-layer header.
823 static u_int off_macpl;
826 * This is 1 if the offset of the beginning of the MAC-layer payload
827 * from the beginning of the link-layer header is variable-length.
829 static int off_macpl_is_variable;
832 * If the link layer has variable_length headers, "reg_off_macpl"
833 * is the register number for a register containing the length of the
834 * link-layer header plus the length of any variable-length header
835 * preceding the link-layer header. Otherwise, "reg_off_macpl"
836 * is -1.
838 static int reg_off_macpl;
841 * "off_linktype" is the offset to information in the link-layer header
842 * giving the packet type. This offset is relative to the beginning
843 * of the link-layer header (i.e., it doesn't include off_ll).
845 * For Ethernet, it's the offset of the Ethernet type field.
847 * For link-layer types that always use 802.2 headers, it's the
848 * offset of the LLC header.
850 * For PPP, it's the offset of the PPP type field.
852 * For Cisco HDLC, it's the offset of the CHDLC type field.
854 * For BSD loopback, it's the offset of the AF_ value.
856 * For Linux cooked sockets, it's the offset of the type field.
858 * It's set to -1 for no encapsulation, in which case, IP is assumed.
860 static u_int off_linktype;
863 * TRUE if "pppoes" appeared in the filter; it causes link-layer type
864 * checks to check the PPP header, assumed to follow a LAN-style link-
865 * layer header and a PPPoE session header.
867 static int is_pppoes = 0;
870 * TRUE if the link layer includes an ATM pseudo-header.
872 static int is_atm = 0;
875 * TRUE if "lane" appeared in the filter; it causes us to generate
876 * code that assumes LANE rather than LLC-encapsulated traffic in SunATM.
878 static int is_lane = 0;
881 * These are offsets for the ATM pseudo-header.
883 static u_int off_vpi;
884 static u_int off_vci;
885 static u_int off_proto;
888 * These are offsets for the MTP2 fields.
890 static u_int off_li;
893 * These are offsets for the MTP3 fields.
895 static u_int off_sio;
896 static u_int off_opc;
897 static u_int off_dpc;
898 static u_int off_sls;
901 * This is the offset of the first byte after the ATM pseudo_header,
902 * or -1 if there is no ATM pseudo-header.
904 static u_int off_payload;
907 * These are offsets to the beginning of the network-layer header.
908 * They are relative to the beginning of the MAC-layer payload (i.e.,
909 * they don't include off_ll or off_macpl).
911 * If the link layer never uses 802.2 LLC:
913 * "off_nl" and "off_nl_nosnap" are the same.
915 * If the link layer always uses 802.2 LLC:
917 * "off_nl" is the offset if there's a SNAP header following
918 * the 802.2 header;
920 * "off_nl_nosnap" is the offset if there's no SNAP header.
922 * If the link layer is Ethernet:
924 * "off_nl" is the offset if the packet is an Ethernet II packet
925 * (we assume no 802.3+802.2+SNAP);
927 * "off_nl_nosnap" is the offset if the packet is an 802.3 packet
928 * with an 802.2 header following it.
930 static u_int off_nl;
931 static u_int off_nl_nosnap;
933 static int linktype;
935 static void
936 init_linktype(p)
937 pcap_t *p;
939 linktype = pcap_datalink(p);
940 #ifdef PCAP_FDDIPAD
941 pcap_fddipad = p->fddipad;
942 #endif
945 * Assume it's not raw ATM with a pseudo-header, for now.
947 off_mac = 0;
948 is_atm = 0;
949 is_lane = 0;
950 off_vpi = -1;
951 off_vci = -1;
952 off_proto = -1;
953 off_payload = -1;
956 * And that we're not doing PPPoE.
958 is_pppoes = 0;
961 * And assume we're not doing SS7.
963 off_li = -1;
964 off_sio = -1;
965 off_opc = -1;
966 off_dpc = -1;
967 off_sls = -1;
970 * Also assume it's not 802.11.
972 off_ll = 0;
973 off_macpl = 0;
974 off_macpl_is_variable = 0;
976 orig_linktype = -1;
977 orig_nl = -1;
978 label_stack_depth = 0;
980 reg_off_ll = -1;
981 reg_off_macpl = -1;
983 switch (linktype) {
985 case DLT_ARCNET:
986 off_linktype = 2;
987 off_macpl = 6;
988 off_nl = 0; /* XXX in reality, variable! */
989 off_nl_nosnap = 0; /* no 802.2 LLC */
990 return;
992 case DLT_ARCNET_LINUX:
993 off_linktype = 4;
994 off_macpl = 8;
995 off_nl = 0; /* XXX in reality, variable! */
996 off_nl_nosnap = 0; /* no 802.2 LLC */
997 return;
999 case DLT_EN10MB:
1000 off_linktype = 12;
1001 off_macpl = 14; /* Ethernet header length */
1002 off_nl = 0; /* Ethernet II */
1003 off_nl_nosnap = 3; /* 802.3+802.2 */
1004 return;
1006 case DLT_SLIP:
1008 * SLIP doesn't have a link level type. The 16 byte
1009 * header is hacked into our SLIP driver.
1011 off_linktype = -1;
1012 off_macpl = 16;
1013 off_nl = 0;
1014 off_nl_nosnap = 0; /* no 802.2 LLC */
1015 return;
1017 case DLT_SLIP_BSDOS:
1018 /* XXX this may be the same as the DLT_PPP_BSDOS case */
1019 off_linktype = -1;
1020 /* XXX end */
1021 off_macpl = 24;
1022 off_nl = 0;
1023 off_nl_nosnap = 0; /* no 802.2 LLC */
1024 return;
1026 case DLT_NULL:
1027 case DLT_LOOP:
1028 off_linktype = 0;
1029 off_macpl = 4;
1030 off_nl = 0;
1031 off_nl_nosnap = 0; /* no 802.2 LLC */
1032 return;
1034 case DLT_ENC:
1035 off_linktype = 0;
1036 off_macpl = 12;
1037 off_nl = 0;
1038 off_nl_nosnap = 0; /* no 802.2 LLC */
1039 return;
1041 case DLT_PPP:
1042 case DLT_PPP_PPPD:
1043 case DLT_C_HDLC: /* BSD/OS Cisco HDLC */
1044 case DLT_PPP_SERIAL: /* NetBSD sync/async serial PPP */
1045 off_linktype = 2;
1046 off_macpl = 4;
1047 off_nl = 0;
1048 off_nl_nosnap = 0; /* no 802.2 LLC */
1049 return;
1051 case DLT_PPP_ETHER:
1053 * This does no include the Ethernet header, and
1054 * only covers session state.
1056 off_linktype = 6;
1057 off_macpl = 8;
1058 off_nl = 0;
1059 off_nl_nosnap = 0; /* no 802.2 LLC */
1060 return;
1062 case DLT_PPP_BSDOS:
1063 off_linktype = 5;
1064 off_macpl = 24;
1065 off_nl = 0;
1066 off_nl_nosnap = 0; /* no 802.2 LLC */
1067 return;
1069 case DLT_FDDI:
1071 * FDDI doesn't really have a link-level type field.
1072 * We set "off_linktype" to the offset of the LLC header.
1074 * To check for Ethernet types, we assume that SSAP = SNAP
1075 * is being used and pick out the encapsulated Ethernet type.
1076 * XXX - should we generate code to check for SNAP?
1078 off_linktype = 13;
1079 #ifdef PCAP_FDDIPAD
1080 off_linktype += pcap_fddipad;
1081 #endif
1082 off_macpl = 13; /* FDDI MAC header length */
1083 #ifdef PCAP_FDDIPAD
1084 off_macpl += pcap_fddipad;
1085 #endif
1086 off_nl = 8; /* 802.2+SNAP */
1087 off_nl_nosnap = 3; /* 802.2 */
1088 return;
1090 case DLT_IEEE802:
1092 * Token Ring doesn't really have a link-level type field.
1093 * We set "off_linktype" to the offset of the LLC header.
1095 * To check for Ethernet types, we assume that SSAP = SNAP
1096 * is being used and pick out the encapsulated Ethernet type.
1097 * XXX - should we generate code to check for SNAP?
1099 * XXX - the header is actually variable-length.
1100 * Some various Linux patched versions gave 38
1101 * as "off_linktype" and 40 as "off_nl"; however,
1102 * if a token ring packet has *no* routing
1103 * information, i.e. is not source-routed, the correct
1104 * values are 20 and 22, as they are in the vanilla code.
1106 * A packet is source-routed iff the uppermost bit
1107 * of the first byte of the source address, at an
1108 * offset of 8, has the uppermost bit set. If the
1109 * packet is source-routed, the total number of bytes
1110 * of routing information is 2 plus bits 0x1F00 of
1111 * the 16-bit value at an offset of 14 (shifted right
1112 * 8 - figure out which byte that is).
1114 off_linktype = 14;
1115 off_macpl = 14; /* Token Ring MAC header length */
1116 off_nl = 8; /* 802.2+SNAP */
1117 off_nl_nosnap = 3; /* 802.2 */
1118 return;
1120 case DLT_IEEE802_11:
1121 case DLT_PRISM_HEADER:
1122 case DLT_IEEE802_11_RADIO_AVS:
1123 case DLT_IEEE802_11_RADIO:
1125 * 802.11 doesn't really have a link-level type field.
1126 * We set "off_linktype" to the offset of the LLC header.
1128 * To check for Ethernet types, we assume that SSAP = SNAP
1129 * is being used and pick out the encapsulated Ethernet type.
1130 * XXX - should we generate code to check for SNAP?
1132 * We also handle variable-length radio headers here.
1133 * The Prism header is in theory variable-length, but in
1134 * practice it's always 144 bytes long. However, some
1135 * drivers on Linux use ARPHRD_IEEE80211_PRISM, but
1136 * sometimes or always supply an AVS header, so we
1137 * have to check whether the radio header is a Prism
1138 * header or an AVS header, so, in practice, it's
1139 * variable-length.
1141 off_linktype = 24;
1142 off_macpl = 0; /* link-layer header is variable-length */
1143 off_macpl_is_variable = 1;
1144 off_nl = 8; /* 802.2+SNAP */
1145 off_nl_nosnap = 3; /* 802.2 */
1146 return;
1148 case DLT_PPI:
1150 * At the moment we treat PPI the same way that we treat
1151 * normal Radiotap encoded packets. The difference is in
1152 * the function that generates the code at the beginning
1153 * to compute the header length. Since this code generator
1154 * of PPI supports bare 802.11 encapsulation only (i.e.
1155 * the encapsulated DLT should be DLT_IEEE802_11) we
1156 * generate code to check for this too.
1158 off_linktype = 24;
1159 off_macpl = 0; /* link-layer header is variable-length */
1160 off_macpl_is_variable = 1;
1161 off_nl = 8; /* 802.2+SNAP */
1162 off_nl_nosnap = 3; /* 802.2 */
1163 return;
1165 case DLT_ATM_RFC1483:
1166 case DLT_ATM_CLIP: /* Linux ATM defines this */
1168 * assume routed, non-ISO PDUs
1169 * (i.e., LLC = 0xAA-AA-03, OUT = 0x00-00-00)
1171 * XXX - what about ISO PDUs, e.g. CLNP, ISIS, ESIS,
1172 * or PPP with the PPP NLPID (e.g., PPPoA)? The
1173 * latter would presumably be treated the way PPPoE
1174 * should be, so you can do "pppoe and udp port 2049"
1175 * or "pppoa and tcp port 80" and have it check for
1176 * PPPo{A,E} and a PPP protocol of IP and....
1178 off_linktype = 0;
1179 off_macpl = 0; /* packet begins with LLC header */
1180 off_nl = 8; /* 802.2+SNAP */
1181 off_nl_nosnap = 3; /* 802.2 */
1182 return;
1184 case DLT_SUNATM:
1186 * Full Frontal ATM; you get AALn PDUs with an ATM
1187 * pseudo-header.
1189 is_atm = 1;
1190 off_vpi = SUNATM_VPI_POS;
1191 off_vci = SUNATM_VCI_POS;
1192 off_proto = PROTO_POS;
1193 off_mac = -1; /* assume LLC-encapsulated, so no MAC-layer header */
1194 off_payload = SUNATM_PKT_BEGIN_POS;
1195 off_linktype = off_payload;
1196 off_macpl = off_payload; /* if LLC-encapsulated */
1197 off_nl = 8; /* 802.2+SNAP */
1198 off_nl_nosnap = 3; /* 802.2 */
1199 return;
1201 case DLT_RAW:
1202 case DLT_IPV4:
1203 case DLT_IPV6:
1204 off_linktype = -1;
1205 off_macpl = 0;
1206 off_nl = 0;
1207 off_nl_nosnap = 0; /* no 802.2 LLC */
1208 return;
1210 case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
1211 off_linktype = 14;
1212 off_macpl = 16;
1213 off_nl = 0;
1214 off_nl_nosnap = 0; /* no 802.2 LLC */
1215 return;
1217 case DLT_LTALK:
1219 * LocalTalk does have a 1-byte type field in the LLAP header,
1220 * but really it just indicates whether there is a "short" or
1221 * "long" DDP packet following.
1223 off_linktype = -1;
1224 off_macpl = 0;
1225 off_nl = 0;
1226 off_nl_nosnap = 0; /* no 802.2 LLC */
1227 return;
1229 case DLT_IP_OVER_FC:
1231 * RFC 2625 IP-over-Fibre-Channel doesn't really have a
1232 * link-level type field. We set "off_linktype" to the
1233 * offset of the LLC header.
1235 * To check for Ethernet types, we assume that SSAP = SNAP
1236 * is being used and pick out the encapsulated Ethernet type.
1237 * XXX - should we generate code to check for SNAP? RFC
1238 * 2625 says SNAP should be used.
1240 off_linktype = 16;
1241 off_macpl = 16;
1242 off_nl = 8; /* 802.2+SNAP */
1243 off_nl_nosnap = 3; /* 802.2 */
1244 return;
1246 case DLT_FRELAY:
1248 * XXX - we should set this to handle SNAP-encapsulated
1249 * frames (NLPID of 0x80).
1251 off_linktype = -1;
1252 off_macpl = 0;
1253 off_nl = 0;
1254 off_nl_nosnap = 0; /* no 802.2 LLC */
1255 return;
1258 * the only BPF-interesting FRF.16 frames are non-control frames;
1259 * Frame Relay has a variable length link-layer
1260 * so lets start with offset 4 for now and increments later on (FIXME);
1262 case DLT_MFR:
1263 off_linktype = -1;
1264 off_macpl = 0;
1265 off_nl = 4;
1266 off_nl_nosnap = 0; /* XXX - for now -> no 802.2 LLC */
1267 return;
1269 case DLT_APPLE_IP_OVER_IEEE1394:
1270 off_linktype = 16;
1271 off_macpl = 18;
1272 off_nl = 0;
1273 off_nl_nosnap = 0; /* no 802.2 LLC */
1274 return;
1276 case DLT_SYMANTEC_FIREWALL:
1277 off_linktype = 6;
1278 off_macpl = 44;
1279 off_nl = 0; /* Ethernet II */
1280 off_nl_nosnap = 0; /* XXX - what does it do with 802.3 packets? */
1281 return;
1283 #ifdef HAVE_NET_PFVAR_H
1284 case DLT_PFLOG:
1285 off_linktype = 0;
1286 off_macpl = PFLOG_HDRLEN;
1287 off_nl = 0;
1288 off_nl_nosnap = 0; /* no 802.2 LLC */
1289 return;
1290 #endif
1292 case DLT_JUNIPER_MFR:
1293 case DLT_JUNIPER_MLFR:
1294 case DLT_JUNIPER_MLPPP:
1295 case DLT_JUNIPER_PPP:
1296 case DLT_JUNIPER_CHDLC:
1297 case DLT_JUNIPER_FRELAY:
1298 off_linktype = 4;
1299 off_macpl = 4;
1300 off_nl = 0;
1301 off_nl_nosnap = -1; /* no 802.2 LLC */
1302 return;
1304 case DLT_JUNIPER_ATM1:
1305 off_linktype = 4; /* in reality variable between 4-8 */
1306 off_macpl = 4; /* in reality variable between 4-8 */
1307 off_nl = 0;
1308 off_nl_nosnap = 10;
1309 return;
1311 case DLT_JUNIPER_ATM2:
1312 off_linktype = 8; /* in reality variable between 8-12 */
1313 off_macpl = 8; /* in reality variable between 8-12 */
1314 off_nl = 0;
1315 off_nl_nosnap = 10;
1316 return;
1318 /* frames captured on a Juniper PPPoE service PIC
1319 * contain raw ethernet frames */
1320 case DLT_JUNIPER_PPPOE:
1321 case DLT_JUNIPER_ETHER:
1322 off_macpl = 14;
1323 off_linktype = 16;
1324 off_nl = 18; /* Ethernet II */
1325 off_nl_nosnap = 21; /* 802.3+802.2 */
1326 return;
1328 case DLT_JUNIPER_PPPOE_ATM:
1329 off_linktype = 4;
1330 off_macpl = 6;
1331 off_nl = 0;
1332 off_nl_nosnap = -1; /* no 802.2 LLC */
1333 return;
1335 case DLT_JUNIPER_GGSN:
1336 off_linktype = 6;
1337 off_macpl = 12;
1338 off_nl = 0;
1339 off_nl_nosnap = -1; /* no 802.2 LLC */
1340 return;
1342 case DLT_JUNIPER_ES:
1343 off_linktype = 6;
1344 off_macpl = -1; /* not really a network layer but raw IP addresses */
1345 off_nl = -1; /* not really a network layer but raw IP addresses */
1346 off_nl_nosnap = -1; /* no 802.2 LLC */
1347 return;
1349 case DLT_JUNIPER_MONITOR:
1350 off_linktype = 12;
1351 off_macpl = 12;
1352 off_nl = 0; /* raw IP/IP6 header */
1353 off_nl_nosnap = -1; /* no 802.2 LLC */
1354 return;
1356 case DLT_JUNIPER_SERVICES:
1357 off_linktype = 12;
1358 off_macpl = -1; /* L3 proto location dep. on cookie type */
1359 off_nl = -1; /* L3 proto location dep. on cookie type */
1360 off_nl_nosnap = -1; /* no 802.2 LLC */
1361 return;
1363 case DLT_JUNIPER_VP:
1364 off_linktype = 18;
1365 off_macpl = -1;
1366 off_nl = -1;
1367 off_nl_nosnap = -1;
1368 return;
1370 case DLT_JUNIPER_ST:
1371 off_linktype = 18;
1372 off_macpl = -1;
1373 off_nl = -1;
1374 off_nl_nosnap = -1;
1375 return;
1377 case DLT_JUNIPER_ISM:
1378 off_linktype = 8;
1379 off_macpl = -1;
1380 off_nl = -1;
1381 off_nl_nosnap = -1;
1382 return;
1384 case DLT_JUNIPER_VS:
1385 case DLT_JUNIPER_SRX_E2E:
1386 case DLT_JUNIPER_FIBRECHANNEL:
1387 case DLT_JUNIPER_ATM_CEMIC:
1388 off_linktype = 8;
1389 off_macpl = -1;
1390 off_nl = -1;
1391 off_nl_nosnap = -1;
1392 return;
1394 case DLT_MTP2:
1395 off_li = 2;
1396 off_sio = 3;
1397 off_opc = 4;
1398 off_dpc = 4;
1399 off_sls = 7;
1400 off_linktype = -1;
1401 off_macpl = -1;
1402 off_nl = -1;
1403 off_nl_nosnap = -1;
1404 return;
1406 case DLT_MTP2_WITH_PHDR:
1407 off_li = 6;
1408 off_sio = 7;
1409 off_opc = 8;
1410 off_dpc = 8;
1411 off_sls = 11;
1412 off_linktype = -1;
1413 off_macpl = -1;
1414 off_nl = -1;
1415 off_nl_nosnap = -1;
1416 return;
1418 case DLT_ERF:
1419 off_li = 22;
1420 off_sio = 23;
1421 off_opc = 24;
1422 off_dpc = 24;
1423 off_sls = 27;
1424 off_linktype = -1;
1425 off_macpl = -1;
1426 off_nl = -1;
1427 off_nl_nosnap = -1;
1428 return;
1430 case DLT_PFSYNC:
1431 off_linktype = -1;
1432 off_macpl = 4;
1433 off_nl = 0;
1434 off_nl_nosnap = 0;
1435 return;
1437 case DLT_AX25_KISS:
1439 * Currently, only raw "link[N:M]" filtering is supported.
1441 off_linktype = -1; /* variable, min 15, max 71 steps of 7 */
1442 off_macpl = -1;
1443 off_nl = -1; /* variable, min 16, max 71 steps of 7 */
1444 off_nl_nosnap = -1; /* no 802.2 LLC */
1445 off_mac = 1; /* step over the kiss length byte */
1446 return;
1448 case DLT_IPNET:
1449 off_linktype = 1;
1450 off_macpl = 24; /* ipnet header length */
1451 off_nl = 0;
1452 off_nl_nosnap = -1;
1453 return;
1455 case DLT_NETANALYZER:
1456 off_mac = 4; /* MAC header is past 4-byte pseudo-header */
1457 off_linktype = 16; /* includes 4-byte pseudo-header */
1458 off_macpl = 18; /* pseudo-header+Ethernet header length */
1459 off_nl = 0; /* Ethernet II */
1460 off_nl_nosnap = 3; /* 802.3+802.2 */
1461 return;
1463 case DLT_NETANALYZER_TRANSPARENT:
1464 off_mac = 12; /* MAC header is past 4-byte pseudo-header, preamble, and SFD */
1465 off_linktype = 24; /* includes 4-byte pseudo-header+preamble+SFD */
1466 off_macpl = 26; /* pseudo-header+preamble+SFD+Ethernet header length */
1467 off_nl = 0; /* Ethernet II */
1468 off_nl_nosnap = 3; /* 802.3+802.2 */
1469 return;
1471 default:
1473 * For values in the range in which we've assigned new
1474 * DLT_ values, only raw "link[N:M]" filtering is supported.
1476 if (linktype >= DLT_MATCHING_MIN &&
1477 linktype <= DLT_MATCHING_MAX) {
1478 off_linktype = -1;
1479 off_macpl = -1;
1480 off_nl = -1;
1481 off_nl_nosnap = -1;
1482 return;
1486 bpf_error("unknown data link type %d", linktype);
1487 /* NOTREACHED */
1491 * Load a value relative to the beginning of the link-layer header.
1492 * The link-layer header doesn't necessarily begin at the beginning
1493 * of the packet data; there might be a variable-length prefix containing
1494 * radio information.
1496 static struct slist *
1497 gen_load_llrel(offset, size)
1498 u_int offset, size;
1500 struct slist *s, *s2;
1502 s = gen_llprefixlen();
1505 * If "s" is non-null, it has code to arrange that the X register
1506 * contains the length of the prefix preceding the link-layer
1507 * header.
1509 * Otherwise, the length of the prefix preceding the link-layer
1510 * header is "off_ll".
1512 if (s != NULL) {
1514 * There's a variable-length prefix preceding the
1515 * link-layer header. "s" points to a list of statements
1516 * that put the length of that prefix into the X register.
1517 * do an indirect load, to use the X register as an offset.
1519 s2 = new_stmt(BPF_LD|BPF_IND|size);
1520 s2->s.k = offset;
1521 sappend(s, s2);
1522 } else {
1524 * There is no variable-length header preceding the
1525 * link-layer header; add in off_ll, which, if there's
1526 * a fixed-length header preceding the link-layer header,
1527 * is the length of that header.
1529 s = new_stmt(BPF_LD|BPF_ABS|size);
1530 s->s.k = offset + off_ll;
1532 return s;
1536 * Load a value relative to the beginning of the MAC-layer payload.
1538 static struct slist *
1539 gen_load_macplrel(offset, size)
1540 u_int offset, size;
1542 struct slist *s, *s2;
1544 s = gen_off_macpl();
1547 * If s is non-null, the offset of the MAC-layer payload is
1548 * variable, and s points to a list of instructions that
1549 * arrange that the X register contains that offset.
1551 * Otherwise, the offset of the MAC-layer payload is constant,
1552 * and is in off_macpl.
1554 if (s != NULL) {
1556 * The offset of the MAC-layer payload is in the X
1557 * register. Do an indirect load, to use the X register
1558 * as an offset.
1560 s2 = new_stmt(BPF_LD|BPF_IND|size);
1561 s2->s.k = offset;
1562 sappend(s, s2);
1563 } else {
1565 * The offset of the MAC-layer payload is constant,
1566 * and is in off_macpl; load the value at that offset
1567 * plus the specified offset.
1569 s = new_stmt(BPF_LD|BPF_ABS|size);
1570 s->s.k = off_macpl + offset;
1572 return s;
1576 * Load a value relative to the beginning of the specified header.
1578 static struct slist *
1579 gen_load_a(offrel, offset, size)
1580 enum e_offrel offrel;
1581 u_int offset, size;
1583 struct slist *s, *s2;
1585 switch (offrel) {
1587 case OR_PACKET:
1588 s = new_stmt(BPF_LD|BPF_ABS|size);
1589 s->s.k = offset;
1590 break;
1592 case OR_LINK:
1593 s = gen_load_llrel(offset, size);
1594 break;
1596 case OR_MACPL:
1597 s = gen_load_macplrel(offset, size);
1598 break;
1600 case OR_NET:
1601 s = gen_load_macplrel(off_nl + offset, size);
1602 break;
1604 case OR_NET_NOSNAP:
1605 s = gen_load_macplrel(off_nl_nosnap + offset, size);
1606 break;
1608 case OR_TRAN_IPV4:
1610 * Load the X register with the length of the IPv4 header
1611 * (plus the offset of the link-layer header, if it's
1612 * preceded by a variable-length header such as a radio
1613 * header), in bytes.
1615 s = gen_loadx_iphdrlen();
1618 * Load the item at {offset of the MAC-layer payload} +
1619 * {offset, relative to the start of the MAC-layer
1620 * paylod, of the IPv4 header} + {length of the IPv4 header} +
1621 * {specified offset}.
1623 * (If the offset of the MAC-layer payload is variable,
1624 * it's included in the value in the X register, and
1625 * off_macpl is 0.)
1627 s2 = new_stmt(BPF_LD|BPF_IND|size);
1628 s2->s.k = off_macpl + off_nl + offset;
1629 sappend(s, s2);
1630 break;
1632 case OR_TRAN_IPV6:
1633 s = gen_load_macplrel(off_nl + 40 + offset, size);
1634 break;
1636 default:
1637 abort();
1638 return NULL;
1640 return s;
1644 * Generate code to load into the X register the sum of the length of
1645 * the IPv4 header and any variable-length header preceding the link-layer
1646 * header.
1648 static struct slist *
1649 gen_loadx_iphdrlen()
1651 struct slist *s, *s2;
1653 s = gen_off_macpl();
1654 if (s != NULL) {
1656 * There's a variable-length prefix preceding the
1657 * link-layer header, or the link-layer header is itself
1658 * variable-length. "s" points to a list of statements
1659 * that put the offset of the MAC-layer payload into
1660 * the X register.
1662 * The 4*([k]&0xf) addressing mode can't be used, as we
1663 * don't have a constant offset, so we have to load the
1664 * value in question into the A register and add to it
1665 * the value from the X register.
1667 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
1668 s2->s.k = off_nl;
1669 sappend(s, s2);
1670 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
1671 s2->s.k = 0xf;
1672 sappend(s, s2);
1673 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
1674 s2->s.k = 2;
1675 sappend(s, s2);
1678 * The A register now contains the length of the
1679 * IP header. We need to add to it the offset of
1680 * the MAC-layer payload, which is still in the X
1681 * register, and move the result into the X register.
1683 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
1684 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
1685 } else {
1687 * There is no variable-length header preceding the
1688 * link-layer header, and the link-layer header is
1689 * fixed-length; load the length of the IPv4 header,
1690 * which is at an offset of off_nl from the beginning
1691 * of the MAC-layer payload, and thus at an offset
1692 * of off_mac_pl + off_nl from the beginning of the
1693 * raw packet data.
1695 s = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
1696 s->s.k = off_macpl + off_nl;
1698 return s;
1701 static struct block *
1702 gen_uncond(rsense)
1703 int rsense;
1705 struct block *b;
1706 struct slist *s;
1708 s = new_stmt(BPF_LD|BPF_IMM);
1709 s->s.k = !rsense;
1710 b = new_block(JMP(BPF_JEQ));
1711 b->stmts = s;
1713 return b;
1716 static inline struct block *
1717 gen_true()
1719 return gen_uncond(1);
1722 static inline struct block *
1723 gen_false()
1725 return gen_uncond(0);
1729 * Byte-swap a 32-bit number.
1730 * ("htonl()" or "ntohl()" won't work - we want to byte-swap even on
1731 * big-endian platforms.)
1733 #define SWAPLONG(y) \
1734 ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
1737 * Generate code to match a particular packet type.
1739 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1740 * value, if <= ETHERMTU. We use that to determine whether to
1741 * match the type/length field or to check the type/length field for
1742 * a value <= ETHERMTU to see whether it's a type field and then do
1743 * the appropriate test.
1745 static struct block *
1746 gen_ether_linktype(proto)
1747 register int proto;
1749 struct block *b0, *b1;
1751 switch (proto) {
1753 case LLCSAP_ISONS:
1754 case LLCSAP_IP:
1755 case LLCSAP_NETBEUI:
1757 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1758 * so we check the DSAP and SSAP.
1760 * LLCSAP_IP checks for IP-over-802.2, rather
1761 * than IP-over-Ethernet or IP-over-SNAP.
1763 * XXX - should we check both the DSAP and the
1764 * SSAP, like this, or should we check just the
1765 * DSAP, as we do for other types <= ETHERMTU
1766 * (i.e., other SAP values)?
1768 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1769 gen_not(b0);
1770 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1771 ((proto << 8) | proto));
1772 gen_and(b0, b1);
1773 return b1;
1775 case LLCSAP_IPX:
1777 * Check for;
1779 * Ethernet_II frames, which are Ethernet
1780 * frames with a frame type of ETHERTYPE_IPX;
1782 * Ethernet_802.3 frames, which are 802.3
1783 * frames (i.e., the type/length field is
1784 * a length field, <= ETHERMTU, rather than
1785 * a type field) with the first two bytes
1786 * after the Ethernet/802.3 header being
1787 * 0xFFFF;
1789 * Ethernet_802.2 frames, which are 802.3
1790 * frames with an 802.2 LLC header and
1791 * with the IPX LSAP as the DSAP in the LLC
1792 * header;
1794 * Ethernet_SNAP frames, which are 802.3
1795 * frames with an LLC header and a SNAP
1796 * header and with an OUI of 0x000000
1797 * (encapsulated Ethernet) and a protocol
1798 * ID of ETHERTYPE_IPX in the SNAP header.
1800 * XXX - should we generate the same code both
1801 * for tests for LLCSAP_IPX and for ETHERTYPE_IPX?
1805 * This generates code to check both for the
1806 * IPX LSAP (Ethernet_802.2) and for Ethernet_802.3.
1808 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
1809 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)0xFFFF);
1810 gen_or(b0, b1);
1813 * Now we add code to check for SNAP frames with
1814 * ETHERTYPE_IPX, i.e. Ethernet_SNAP.
1816 b0 = gen_snap(0x000000, ETHERTYPE_IPX);
1817 gen_or(b0, b1);
1820 * Now we generate code to check for 802.3
1821 * frames in general.
1823 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1824 gen_not(b0);
1827 * Now add the check for 802.3 frames before the
1828 * check for Ethernet_802.2 and Ethernet_802.3,
1829 * as those checks should only be done on 802.3
1830 * frames, not on Ethernet frames.
1832 gen_and(b0, b1);
1835 * Now add the check for Ethernet_II frames, and
1836 * do that before checking for the other frame
1837 * types.
1839 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
1840 (bpf_int32)ETHERTYPE_IPX);
1841 gen_or(b0, b1);
1842 return b1;
1844 case ETHERTYPE_ATALK:
1845 case ETHERTYPE_AARP:
1847 * EtherTalk (AppleTalk protocols on Ethernet link
1848 * layer) may use 802.2 encapsulation.
1852 * Check for 802.2 encapsulation (EtherTalk phase 2?);
1853 * we check for an Ethernet type field less than
1854 * 1500, which means it's an 802.3 length field.
1856 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1857 gen_not(b0);
1860 * 802.2-encapsulated ETHERTYPE_ATALK packets are
1861 * SNAP packets with an organization code of
1862 * 0x080007 (Apple, for Appletalk) and a protocol
1863 * type of ETHERTYPE_ATALK (Appletalk).
1865 * 802.2-encapsulated ETHERTYPE_AARP packets are
1866 * SNAP packets with an organization code of
1867 * 0x000000 (encapsulated Ethernet) and a protocol
1868 * type of ETHERTYPE_AARP (Appletalk ARP).
1870 if (proto == ETHERTYPE_ATALK)
1871 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
1872 else /* proto == ETHERTYPE_AARP */
1873 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
1874 gen_and(b0, b1);
1877 * Check for Ethernet encapsulation (Ethertalk
1878 * phase 1?); we just check for the Ethernet
1879 * protocol type.
1881 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
1883 gen_or(b0, b1);
1884 return b1;
1886 default:
1887 if (proto <= ETHERMTU) {
1889 * This is an LLC SAP value, so the frames
1890 * that match would be 802.2 frames.
1891 * Check that the frame is an 802.2 frame
1892 * (i.e., that the length/type field is
1893 * a length field, <= ETHERMTU) and
1894 * then check the DSAP.
1896 b0 = gen_cmp_gt(OR_LINK, off_linktype, BPF_H, ETHERMTU);
1897 gen_not(b0);
1898 b1 = gen_cmp(OR_LINK, off_linktype + 2, BPF_B,
1899 (bpf_int32)proto);
1900 gen_and(b0, b1);
1901 return b1;
1902 } else {
1904 * This is an Ethernet type, so compare
1905 * the length/type field with it (if
1906 * the frame is an 802.2 frame, the length
1907 * field will be <= ETHERMTU, and, as
1908 * "proto" is > ETHERMTU, this test
1909 * will fail and the frame won't match,
1910 * which is what we want).
1912 return gen_cmp(OR_LINK, off_linktype, BPF_H,
1913 (bpf_int32)proto);
1919 * "proto" is an Ethernet type value and for IPNET, if it is not IPv4
1920 * or IPv6 then we have an error.
1922 static struct block *
1923 gen_ipnet_linktype(proto)
1924 register int proto;
1926 switch (proto) {
1928 case ETHERTYPE_IP:
1929 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1930 (bpf_int32)IPH_AF_INET);
1931 /* NOTREACHED */
1933 case ETHERTYPE_IPV6:
1934 return gen_cmp(OR_LINK, off_linktype, BPF_B,
1935 (bpf_int32)IPH_AF_INET6);
1936 /* NOTREACHED */
1938 default:
1939 break;
1942 return gen_false();
1946 * Generate code to match a particular packet type.
1948 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
1949 * value, if <= ETHERMTU. We use that to determine whether to
1950 * match the type field or to check the type field for the special
1951 * LINUX_SLL_P_802_2 value and then do the appropriate test.
1953 static struct block *
1954 gen_linux_sll_linktype(proto)
1955 register int proto;
1957 struct block *b0, *b1;
1959 switch (proto) {
1961 case LLCSAP_ISONS:
1962 case LLCSAP_IP:
1963 case LLCSAP_NETBEUI:
1965 * OSI protocols and NetBEUI always use 802.2 encapsulation,
1966 * so we check the DSAP and SSAP.
1968 * LLCSAP_IP checks for IP-over-802.2, rather
1969 * than IP-over-Ethernet or IP-over-SNAP.
1971 * XXX - should we check both the DSAP and the
1972 * SSAP, like this, or should we check just the
1973 * DSAP, as we do for other types <= ETHERMTU
1974 * (i.e., other SAP values)?
1976 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
1977 b1 = gen_cmp(OR_MACPL, 0, BPF_H, (bpf_int32)
1978 ((proto << 8) | proto));
1979 gen_and(b0, b1);
1980 return b1;
1982 case LLCSAP_IPX:
1984 * Ethernet_II frames, which are Ethernet
1985 * frames with a frame type of ETHERTYPE_IPX;
1987 * Ethernet_802.3 frames, which have a frame
1988 * type of LINUX_SLL_P_802_3;
1990 * Ethernet_802.2 frames, which are 802.3
1991 * frames with an 802.2 LLC header (i.e, have
1992 * a frame type of LINUX_SLL_P_802_2) and
1993 * with the IPX LSAP as the DSAP in the LLC
1994 * header;
1996 * Ethernet_SNAP frames, which are 802.3
1997 * frames with an LLC header and a SNAP
1998 * header and with an OUI of 0x000000
1999 * (encapsulated Ethernet) and a protocol
2000 * ID of ETHERTYPE_IPX in the SNAP header.
2002 * First, do the checks on LINUX_SLL_P_802_2
2003 * frames; generate the check for either
2004 * Ethernet_802.2 or Ethernet_SNAP frames, and
2005 * then put a check for LINUX_SLL_P_802_2 frames
2006 * before it.
2008 b0 = gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)LLCSAP_IPX);
2009 b1 = gen_snap(0x000000, ETHERTYPE_IPX);
2010 gen_or(b0, b1);
2011 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2012 gen_and(b0, b1);
2015 * Now check for 802.3 frames and OR that with
2016 * the previous test.
2018 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_3);
2019 gen_or(b0, b1);
2022 * Now add the check for Ethernet_II frames, and
2023 * do that before checking for the other frame
2024 * types.
2026 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2027 (bpf_int32)ETHERTYPE_IPX);
2028 gen_or(b0, b1);
2029 return b1;
2031 case ETHERTYPE_ATALK:
2032 case ETHERTYPE_AARP:
2034 * EtherTalk (AppleTalk protocols on Ethernet link
2035 * layer) may use 802.2 encapsulation.
2039 * Check for 802.2 encapsulation (EtherTalk phase 2?);
2040 * we check for the 802.2 protocol type in the
2041 * "Ethernet type" field.
2043 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, LINUX_SLL_P_802_2);
2046 * 802.2-encapsulated ETHERTYPE_ATALK packets are
2047 * SNAP packets with an organization code of
2048 * 0x080007 (Apple, for Appletalk) and a protocol
2049 * type of ETHERTYPE_ATALK (Appletalk).
2051 * 802.2-encapsulated ETHERTYPE_AARP packets are
2052 * SNAP packets with an organization code of
2053 * 0x000000 (encapsulated Ethernet) and a protocol
2054 * type of ETHERTYPE_AARP (Appletalk ARP).
2056 if (proto == ETHERTYPE_ATALK)
2057 b1 = gen_snap(0x080007, ETHERTYPE_ATALK);
2058 else /* proto == ETHERTYPE_AARP */
2059 b1 = gen_snap(0x000000, ETHERTYPE_AARP);
2060 gen_and(b0, b1);
2063 * Check for Ethernet encapsulation (Ethertalk
2064 * phase 1?); we just check for the Ethernet
2065 * protocol type.
2067 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
2069 gen_or(b0, b1);
2070 return b1;
2072 default:
2073 if (proto <= ETHERMTU) {
2075 * This is an LLC SAP value, so the frames
2076 * that match would be 802.2 frames.
2077 * Check for the 802.2 protocol type
2078 * in the "Ethernet type" field, and
2079 * then check the DSAP.
2081 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
2082 LINUX_SLL_P_802_2);
2083 b1 = gen_cmp(OR_LINK, off_macpl, BPF_B,
2084 (bpf_int32)proto);
2085 gen_and(b0, b1);
2086 return b1;
2087 } else {
2089 * This is an Ethernet type, so compare
2090 * the length/type field with it (if
2091 * the frame is an 802.2 frame, the length
2092 * field will be <= ETHERMTU, and, as
2093 * "proto" is > ETHERMTU, this test
2094 * will fail and the frame won't match,
2095 * which is what we want).
2097 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2098 (bpf_int32)proto);
2103 static struct slist *
2104 gen_load_prism_llprefixlen()
2106 struct slist *s1, *s2;
2107 struct slist *sjeq_avs_cookie;
2108 struct slist *sjcommon;
2111 * This code is not compatible with the optimizer, as
2112 * we are generating jmp instructions within a normal
2113 * slist of instructions
2115 no_optimize = 1;
2118 * Generate code to load the length of the radio header into
2119 * the register assigned to hold that length, if one has been
2120 * assigned. (If one hasn't been assigned, no code we've
2121 * generated uses that prefix, so we don't need to generate any
2122 * code to load it.)
2124 * Some Linux drivers use ARPHRD_IEEE80211_PRISM but sometimes
2125 * or always use the AVS header rather than the Prism header.
2126 * We load a 4-byte big-endian value at the beginning of the
2127 * raw packet data, and see whether, when masked with 0xFFFFF000,
2128 * it's equal to 0x80211000. If so, that indicates that it's
2129 * an AVS header (the masked-out bits are the version number).
2130 * Otherwise, it's a Prism header.
2132 * XXX - the Prism header is also, in theory, variable-length,
2133 * but no known software generates headers that aren't 144
2134 * bytes long.
2136 if (reg_off_ll != -1) {
2138 * Load the cookie.
2140 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2141 s1->s.k = 0;
2144 * AND it with 0xFFFFF000.
2146 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_K);
2147 s2->s.k = 0xFFFFF000;
2148 sappend(s1, s2);
2151 * Compare with 0x80211000.
2153 sjeq_avs_cookie = new_stmt(JMP(BPF_JEQ));
2154 sjeq_avs_cookie->s.k = 0x80211000;
2155 sappend(s1, sjeq_avs_cookie);
2158 * If it's AVS:
2160 * The 4 bytes at an offset of 4 from the beginning of
2161 * the AVS header are the length of the AVS header.
2162 * That field is big-endian.
2164 s2 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2165 s2->s.k = 4;
2166 sappend(s1, s2);
2167 sjeq_avs_cookie->s.jt = s2;
2170 * Now jump to the code to allocate a register
2171 * into which to save the header length and
2172 * store the length there. (The "jump always"
2173 * instruction needs to have the k field set;
2174 * it's added to the PC, so, as we're jumping
2175 * over a single instruction, it should be 1.)
2177 sjcommon = new_stmt(JMP(BPF_JA));
2178 sjcommon->s.k = 1;
2179 sappend(s1, sjcommon);
2182 * Now for the code that handles the Prism header.
2183 * Just load the length of the Prism header (144)
2184 * into the A register. Have the test for an AVS
2185 * header branch here if we don't have an AVS header.
2187 s2 = new_stmt(BPF_LD|BPF_W|BPF_IMM);
2188 s2->s.k = 144;
2189 sappend(s1, s2);
2190 sjeq_avs_cookie->s.jf = s2;
2193 * Now allocate a register to hold that value and store
2194 * it. The code for the AVS header will jump here after
2195 * loading the length of the AVS header.
2197 s2 = new_stmt(BPF_ST);
2198 s2->s.k = reg_off_ll;
2199 sappend(s1, s2);
2200 sjcommon->s.jf = s2;
2203 * Now move it into the X register.
2205 s2 = new_stmt(BPF_MISC|BPF_TAX);
2206 sappend(s1, s2);
2208 return (s1);
2209 } else
2210 return (NULL);
2213 static struct slist *
2214 gen_load_avs_llprefixlen()
2216 struct slist *s1, *s2;
2219 * Generate code to load the length of the AVS header into
2220 * the register assigned to hold that length, if one has been
2221 * assigned. (If one hasn't been assigned, no code we've
2222 * generated uses that prefix, so we don't need to generate any
2223 * code to load it.)
2225 if (reg_off_ll != -1) {
2227 * The 4 bytes at an offset of 4 from the beginning of
2228 * the AVS header are the length of the AVS header.
2229 * That field is big-endian.
2231 s1 = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2232 s1->s.k = 4;
2235 * Now allocate a register to hold that value and store
2236 * it.
2238 s2 = new_stmt(BPF_ST);
2239 s2->s.k = reg_off_ll;
2240 sappend(s1, s2);
2243 * Now move it into the X register.
2245 s2 = new_stmt(BPF_MISC|BPF_TAX);
2246 sappend(s1, s2);
2248 return (s1);
2249 } else
2250 return (NULL);
2253 static struct slist *
2254 gen_load_radiotap_llprefixlen()
2256 struct slist *s1, *s2;
2259 * Generate code to load the length of the radiotap header into
2260 * the register assigned to hold that length, if one has been
2261 * assigned. (If one hasn't been assigned, no code we've
2262 * generated uses that prefix, so we don't need to generate any
2263 * code to load it.)
2265 if (reg_off_ll != -1) {
2267 * The 2 bytes at offsets of 2 and 3 from the beginning
2268 * of the radiotap header are the length of the radiotap
2269 * header; unfortunately, it's little-endian, so we have
2270 * to load it a byte at a time and construct the value.
2274 * Load the high-order byte, at an offset of 3, shift it
2275 * left a byte, and put the result in the X register.
2277 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2278 s1->s.k = 3;
2279 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2280 sappend(s1, s2);
2281 s2->s.k = 8;
2282 s2 = new_stmt(BPF_MISC|BPF_TAX);
2283 sappend(s1, s2);
2286 * Load the next byte, at an offset of 2, and OR the
2287 * value from the X register into it.
2289 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2290 sappend(s1, s2);
2291 s2->s.k = 2;
2292 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2293 sappend(s1, s2);
2296 * Now allocate a register to hold that value and store
2297 * it.
2299 s2 = new_stmt(BPF_ST);
2300 s2->s.k = reg_off_ll;
2301 sappend(s1, s2);
2304 * Now move it into the X register.
2306 s2 = new_stmt(BPF_MISC|BPF_TAX);
2307 sappend(s1, s2);
2309 return (s1);
2310 } else
2311 return (NULL);
2315 * At the moment we treat PPI as normal Radiotap encoded
2316 * packets. The difference is in the function that generates
2317 * the code at the beginning to compute the header length.
2318 * Since this code generator of PPI supports bare 802.11
2319 * encapsulation only (i.e. the encapsulated DLT should be
2320 * DLT_IEEE802_11) we generate code to check for this too;
2321 * that's done in finish_parse().
2323 static struct slist *
2324 gen_load_ppi_llprefixlen()
2326 struct slist *s1, *s2;
2329 * Generate code to load the length of the radiotap header
2330 * into the register assigned to hold that length, if one has
2331 * been assigned.
2333 if (reg_off_ll != -1) {
2335 * The 2 bytes at offsets of 2 and 3 from the beginning
2336 * of the radiotap header are the length of the radiotap
2337 * header; unfortunately, it's little-endian, so we have
2338 * to load it a byte at a time and construct the value.
2342 * Load the high-order byte, at an offset of 3, shift it
2343 * left a byte, and put the result in the X register.
2345 s1 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2346 s1->s.k = 3;
2347 s2 = new_stmt(BPF_ALU|BPF_LSH|BPF_K);
2348 sappend(s1, s2);
2349 s2->s.k = 8;
2350 s2 = new_stmt(BPF_MISC|BPF_TAX);
2351 sappend(s1, s2);
2354 * Load the next byte, at an offset of 2, and OR the
2355 * value from the X register into it.
2357 s2 = new_stmt(BPF_LD|BPF_B|BPF_ABS);
2358 sappend(s1, s2);
2359 s2->s.k = 2;
2360 s2 = new_stmt(BPF_ALU|BPF_OR|BPF_X);
2361 sappend(s1, s2);
2364 * Now allocate a register to hold that value and store
2365 * it.
2367 s2 = new_stmt(BPF_ST);
2368 s2->s.k = reg_off_ll;
2369 sappend(s1, s2);
2372 * Now move it into the X register.
2374 s2 = new_stmt(BPF_MISC|BPF_TAX);
2375 sappend(s1, s2);
2377 return (s1);
2378 } else
2379 return (NULL);
2383 * Load a value relative to the beginning of the link-layer header after the 802.11
2384 * header, i.e. LLC_SNAP.
2385 * The link-layer header doesn't necessarily begin at the beginning
2386 * of the packet data; there might be a variable-length prefix containing
2387 * radio information.
2389 static struct slist *
2390 gen_load_802_11_header_len(struct slist *s, struct slist *snext)
2392 struct slist *s2;
2393 struct slist *sjset_data_frame_1;
2394 struct slist *sjset_data_frame_2;
2395 struct slist *sjset_qos;
2396 struct slist *sjset_radiotap_flags;
2397 struct slist *sjset_radiotap_tsft;
2398 struct slist *sjset_tsft_datapad, *sjset_notsft_datapad;
2399 struct slist *s_roundup;
2401 if (reg_off_macpl == -1) {
2403 * No register has been assigned to the offset of
2404 * the MAC-layer payload, which means nobody needs
2405 * it; don't bother computing it - just return
2406 * what we already have.
2408 return (s);
2412 * This code is not compatible with the optimizer, as
2413 * we are generating jmp instructions within a normal
2414 * slist of instructions
2416 no_optimize = 1;
2419 * If "s" is non-null, it has code to arrange that the X register
2420 * contains the length of the prefix preceding the link-layer
2421 * header.
2423 * Otherwise, the length of the prefix preceding the link-layer
2424 * header is "off_ll".
2426 if (s == NULL) {
2428 * There is no variable-length header preceding the
2429 * link-layer header.
2431 * Load the length of the fixed-length prefix preceding
2432 * the link-layer header (if any) into the X register,
2433 * and store it in the reg_off_macpl register.
2434 * That length is off_ll.
2436 s = new_stmt(BPF_LDX|BPF_IMM);
2437 s->s.k = off_ll;
2441 * The X register contains the offset of the beginning of the
2442 * link-layer header; add 24, which is the minimum length
2443 * of the MAC header for a data frame, to that, and store it
2444 * in reg_off_macpl, and then load the Frame Control field,
2445 * which is at the offset in the X register, with an indexed load.
2447 s2 = new_stmt(BPF_MISC|BPF_TXA);
2448 sappend(s, s2);
2449 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
2450 s2->s.k = 24;
2451 sappend(s, s2);
2452 s2 = new_stmt(BPF_ST);
2453 s2->s.k = reg_off_macpl;
2454 sappend(s, s2);
2456 s2 = new_stmt(BPF_LD|BPF_IND|BPF_B);
2457 s2->s.k = 0;
2458 sappend(s, s2);
2461 * Check the Frame Control field to see if this is a data frame;
2462 * a data frame has the 0x08 bit (b3) in that field set and the
2463 * 0x04 bit (b2) clear.
2465 sjset_data_frame_1 = new_stmt(JMP(BPF_JSET));
2466 sjset_data_frame_1->s.k = 0x08;
2467 sappend(s, sjset_data_frame_1);
2470 * If b3 is set, test b2, otherwise go to the first statement of
2471 * the rest of the program.
2473 sjset_data_frame_1->s.jt = sjset_data_frame_2 = new_stmt(JMP(BPF_JSET));
2474 sjset_data_frame_2->s.k = 0x04;
2475 sappend(s, sjset_data_frame_2);
2476 sjset_data_frame_1->s.jf = snext;
2479 * If b2 is not set, this is a data frame; test the QoS bit.
2480 * Otherwise, go to the first statement of the rest of the
2481 * program.
2483 sjset_data_frame_2->s.jt = snext;
2484 sjset_data_frame_2->s.jf = sjset_qos = new_stmt(JMP(BPF_JSET));
2485 sjset_qos->s.k = 0x80; /* QoS bit */
2486 sappend(s, sjset_qos);
2489 * If it's set, add 2 to reg_off_macpl, to skip the QoS
2490 * field.
2491 * Otherwise, go to the first statement of the rest of the
2492 * program.
2494 sjset_qos->s.jt = s2 = new_stmt(BPF_LD|BPF_MEM);
2495 s2->s.k = reg_off_macpl;
2496 sappend(s, s2);
2497 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2498 s2->s.k = 2;
2499 sappend(s, s2);
2500 s2 = new_stmt(BPF_ST);
2501 s2->s.k = reg_off_macpl;
2502 sappend(s, s2);
2505 * If we have a radiotap header, look at it to see whether
2506 * there's Atheros padding between the MAC-layer header
2507 * and the payload.
2509 * Note: all of the fields in the radiotap header are
2510 * little-endian, so we byte-swap all of the values
2511 * we test against, as they will be loaded as big-endian
2512 * values.
2514 if (linktype == DLT_IEEE802_11_RADIO) {
2516 * Is the IEEE80211_RADIOTAP_FLAGS bit (0x0000002) set
2517 * in the presence flag?
2519 sjset_qos->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_W);
2520 s2->s.k = 4;
2521 sappend(s, s2);
2523 sjset_radiotap_flags = new_stmt(JMP(BPF_JSET));
2524 sjset_radiotap_flags->s.k = SWAPLONG(0x00000002);
2525 sappend(s, sjset_radiotap_flags);
2528 * If not, skip all of this.
2530 sjset_radiotap_flags->s.jf = snext;
2533 * Otherwise, is the IEEE80211_RADIOTAP_TSFT bit set?
2535 sjset_radiotap_tsft = sjset_radiotap_flags->s.jt =
2536 new_stmt(JMP(BPF_JSET));
2537 sjset_radiotap_tsft->s.k = SWAPLONG(0x00000001);
2538 sappend(s, sjset_radiotap_tsft);
2541 * If IEEE80211_RADIOTAP_TSFT is set, the flags field is
2542 * at an offset of 16 from the beginning of the raw packet
2543 * data (8 bytes for the radiotap header and 8 bytes for
2544 * the TSFT field).
2546 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2547 * is set.
2549 sjset_radiotap_tsft->s.jt = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2550 s2->s.k = 16;
2551 sappend(s, s2);
2553 sjset_tsft_datapad = new_stmt(JMP(BPF_JSET));
2554 sjset_tsft_datapad->s.k = 0x20;
2555 sappend(s, sjset_tsft_datapad);
2558 * If IEEE80211_RADIOTAP_TSFT is not set, the flags field is
2559 * at an offset of 8 from the beginning of the raw packet
2560 * data (8 bytes for the radiotap header).
2562 * Test whether the IEEE80211_RADIOTAP_F_DATAPAD bit (0x20)
2563 * is set.
2565 sjset_radiotap_tsft->s.jf = s2 = new_stmt(BPF_LD|BPF_ABS|BPF_B);
2566 s2->s.k = 8;
2567 sappend(s, s2);
2569 sjset_notsft_datapad = new_stmt(JMP(BPF_JSET));
2570 sjset_notsft_datapad->s.k = 0x20;
2571 sappend(s, sjset_notsft_datapad);
2574 * In either case, if IEEE80211_RADIOTAP_F_DATAPAD is
2575 * set, round the length of the 802.11 header to
2576 * a multiple of 4. Do that by adding 3 and then
2577 * dividing by and multiplying by 4, which we do by
2578 * ANDing with ~3.
2580 s_roundup = new_stmt(BPF_LD|BPF_MEM);
2581 s_roundup->s.k = reg_off_macpl;
2582 sappend(s, s_roundup);
2583 s2 = new_stmt(BPF_ALU|BPF_ADD|BPF_IMM);
2584 s2->s.k = 3;
2585 sappend(s, s2);
2586 s2 = new_stmt(BPF_ALU|BPF_AND|BPF_IMM);
2587 s2->s.k = ~3;
2588 sappend(s, s2);
2589 s2 = new_stmt(BPF_ST);
2590 s2->s.k = reg_off_macpl;
2591 sappend(s, s2);
2593 sjset_tsft_datapad->s.jt = s_roundup;
2594 sjset_tsft_datapad->s.jf = snext;
2595 sjset_notsft_datapad->s.jt = s_roundup;
2596 sjset_notsft_datapad->s.jf = snext;
2597 } else
2598 sjset_qos->s.jf = snext;
2600 return s;
2603 static void
2604 insert_compute_vloffsets(b)
2605 struct block *b;
2607 struct slist *s;
2610 * For link-layer types that have a variable-length header
2611 * preceding the link-layer header, generate code to load
2612 * the offset of the link-layer header into the register
2613 * assigned to that offset, if any.
2615 switch (linktype) {
2617 case DLT_PRISM_HEADER:
2618 s = gen_load_prism_llprefixlen();
2619 break;
2621 case DLT_IEEE802_11_RADIO_AVS:
2622 s = gen_load_avs_llprefixlen();
2623 break;
2625 case DLT_IEEE802_11_RADIO:
2626 s = gen_load_radiotap_llprefixlen();
2627 break;
2629 case DLT_PPI:
2630 s = gen_load_ppi_llprefixlen();
2631 break;
2633 default:
2634 s = NULL;
2635 break;
2639 * For link-layer types that have a variable-length link-layer
2640 * header, generate code to load the offset of the MAC-layer
2641 * payload into the register assigned to that offset, if any.
2643 switch (linktype) {
2645 case DLT_IEEE802_11:
2646 case DLT_PRISM_HEADER:
2647 case DLT_IEEE802_11_RADIO_AVS:
2648 case DLT_IEEE802_11_RADIO:
2649 case DLT_PPI:
2650 s = gen_load_802_11_header_len(s, b->stmts);
2651 break;
2655 * If we have any offset-loading code, append all the
2656 * existing statements in the block to those statements,
2657 * and make the resulting list the list of statements
2658 * for the block.
2660 if (s != NULL) {
2661 sappend(s, b->stmts);
2662 b->stmts = s;
2666 static struct block *
2667 gen_ppi_dlt_check(void)
2669 struct slist *s_load_dlt;
2670 struct block *b;
2672 if (linktype == DLT_PPI)
2674 /* Create the statements that check for the DLT
2676 s_load_dlt = new_stmt(BPF_LD|BPF_W|BPF_ABS);
2677 s_load_dlt->s.k = 4;
2679 b = new_block(JMP(BPF_JEQ));
2681 b->stmts = s_load_dlt;
2682 b->s.k = SWAPLONG(DLT_IEEE802_11);
2684 else
2686 b = NULL;
2689 return b;
2692 static struct slist *
2693 gen_prism_llprefixlen(void)
2695 struct slist *s;
2697 if (reg_off_ll == -1) {
2699 * We haven't yet assigned a register for the length
2700 * of the radio header; allocate one.
2702 reg_off_ll = alloc_reg();
2706 * Load the register containing the radio length
2707 * into the X register.
2709 s = new_stmt(BPF_LDX|BPF_MEM);
2710 s->s.k = reg_off_ll;
2711 return s;
2714 static struct slist *
2715 gen_avs_llprefixlen(void)
2717 struct slist *s;
2719 if (reg_off_ll == -1) {
2721 * We haven't yet assigned a register for the length
2722 * of the AVS header; allocate one.
2724 reg_off_ll = alloc_reg();
2728 * Load the register containing the AVS length
2729 * into the X register.
2731 s = new_stmt(BPF_LDX|BPF_MEM);
2732 s->s.k = reg_off_ll;
2733 return s;
2736 static struct slist *
2737 gen_radiotap_llprefixlen(void)
2739 struct slist *s;
2741 if (reg_off_ll == -1) {
2743 * We haven't yet assigned a register for the length
2744 * of the radiotap header; allocate one.
2746 reg_off_ll = alloc_reg();
2750 * Load the register containing the radiotap length
2751 * into the X register.
2753 s = new_stmt(BPF_LDX|BPF_MEM);
2754 s->s.k = reg_off_ll;
2755 return s;
2759 * At the moment we treat PPI as normal Radiotap encoded
2760 * packets. The difference is in the function that generates
2761 * the code at the beginning to compute the header length.
2762 * Since this code generator of PPI supports bare 802.11
2763 * encapsulation only (i.e. the encapsulated DLT should be
2764 * DLT_IEEE802_11) we generate code to check for this too.
2766 static struct slist *
2767 gen_ppi_llprefixlen(void)
2769 struct slist *s;
2771 if (reg_off_ll == -1) {
2773 * We haven't yet assigned a register for the length
2774 * of the radiotap header; allocate one.
2776 reg_off_ll = alloc_reg();
2780 * Load the register containing the PPI length
2781 * into the X register.
2783 s = new_stmt(BPF_LDX|BPF_MEM);
2784 s->s.k = reg_off_ll;
2785 return s;
2789 * Generate code to compute the link-layer header length, if necessary,
2790 * putting it into the X register, and to return either a pointer to a
2791 * "struct slist" for the list of statements in that code, or NULL if
2792 * no code is necessary.
2794 static struct slist *
2795 gen_llprefixlen(void)
2797 switch (linktype) {
2799 case DLT_PRISM_HEADER:
2800 return gen_prism_llprefixlen();
2802 case DLT_IEEE802_11_RADIO_AVS:
2803 return gen_avs_llprefixlen();
2805 case DLT_IEEE802_11_RADIO:
2806 return gen_radiotap_llprefixlen();
2808 case DLT_PPI:
2809 return gen_ppi_llprefixlen();
2811 default:
2812 return NULL;
2817 * Generate code to load the register containing the offset of the
2818 * MAC-layer payload into the X register; if no register for that offset
2819 * has been allocated, allocate it first.
2821 static struct slist *
2822 gen_off_macpl(void)
2824 struct slist *s;
2826 if (off_macpl_is_variable) {
2827 if (reg_off_macpl == -1) {
2829 * We haven't yet assigned a register for the offset
2830 * of the MAC-layer payload; allocate one.
2832 reg_off_macpl = alloc_reg();
2836 * Load the register containing the offset of the MAC-layer
2837 * payload into the X register.
2839 s = new_stmt(BPF_LDX|BPF_MEM);
2840 s->s.k = reg_off_macpl;
2841 return s;
2842 } else {
2844 * That offset isn't variable, so we don't need to
2845 * generate any code.
2847 return NULL;
2852 * Map an Ethernet type to the equivalent PPP type.
2854 static int
2855 ethertype_to_ppptype(proto)
2856 int proto;
2858 switch (proto) {
2860 case ETHERTYPE_IP:
2861 proto = PPP_IP;
2862 break;
2864 #ifdef INET6
2865 case ETHERTYPE_IPV6:
2866 proto = PPP_IPV6;
2867 break;
2868 #endif
2870 case ETHERTYPE_DN:
2871 proto = PPP_DECNET;
2872 break;
2874 case ETHERTYPE_ATALK:
2875 proto = PPP_APPLE;
2876 break;
2878 case ETHERTYPE_NS:
2879 proto = PPP_NS;
2880 break;
2882 case LLCSAP_ISONS:
2883 proto = PPP_OSI;
2884 break;
2886 case LLCSAP_8021D:
2888 * I'm assuming the "Bridging PDU"s that go
2889 * over PPP are Spanning Tree Protocol
2890 * Bridging PDUs.
2892 proto = PPP_BRPDU;
2893 break;
2895 case LLCSAP_IPX:
2896 proto = PPP_IPX;
2897 break;
2899 return (proto);
2903 * Generate code to match a particular packet type by matching the
2904 * link-layer type field or fields in the 802.2 LLC header.
2906 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
2907 * value, if <= ETHERMTU.
2909 static struct block *
2910 gen_linktype(proto)
2911 register int proto;
2913 struct block *b0, *b1, *b2;
2915 /* are we checking MPLS-encapsulated packets? */
2916 if (label_stack_depth > 0) {
2917 switch (proto) {
2918 case ETHERTYPE_IP:
2919 case PPP_IP:
2920 /* FIXME add other L3 proto IDs */
2921 return gen_mpls_linktype(Q_IP);
2923 case ETHERTYPE_IPV6:
2924 case PPP_IPV6:
2925 /* FIXME add other L3 proto IDs */
2926 return gen_mpls_linktype(Q_IPV6);
2928 default:
2929 bpf_error("unsupported protocol over mpls");
2930 /* NOTREACHED */
2935 * Are we testing PPPoE packets?
2937 if (is_pppoes) {
2939 * The PPPoE session header is part of the
2940 * MAC-layer payload, so all references
2941 * should be relative to the beginning of
2942 * that payload.
2946 * We use Ethernet protocol types inside libpcap;
2947 * map them to the corresponding PPP protocol types.
2949 proto = ethertype_to_ppptype(proto);
2950 return gen_cmp(OR_MACPL, off_linktype, BPF_H, (bpf_int32)proto);
2953 switch (linktype) {
2955 case DLT_EN10MB:
2956 case DLT_NETANALYZER:
2957 case DLT_NETANALYZER_TRANSPARENT:
2958 return gen_ether_linktype(proto);
2959 /*NOTREACHED*/
2960 break;
2962 case DLT_C_HDLC:
2963 switch (proto) {
2965 case LLCSAP_ISONS:
2966 proto = (proto << 8 | LLCSAP_ISONS);
2967 /* fall through */
2969 default:
2970 return gen_cmp(OR_LINK, off_linktype, BPF_H,
2971 (bpf_int32)proto);
2972 /*NOTREACHED*/
2973 break;
2975 break;
2977 case DLT_IEEE802_11:
2978 case DLT_PRISM_HEADER:
2979 case DLT_IEEE802_11_RADIO_AVS:
2980 case DLT_IEEE802_11_RADIO:
2981 case DLT_PPI:
2983 * Check that we have a data frame.
2985 b0 = gen_check_802_11_data_frame();
2988 * Now check for the specified link-layer type.
2990 b1 = gen_llc_linktype(proto);
2991 gen_and(b0, b1);
2992 return b1;
2993 /*NOTREACHED*/
2994 break;
2996 case DLT_FDDI:
2998 * XXX - check for asynchronous frames, as per RFC 1103.
3000 return gen_llc_linktype(proto);
3001 /*NOTREACHED*/
3002 break;
3004 case DLT_IEEE802:
3006 * XXX - check for LLC PDUs, as per IEEE 802.5.
3008 return gen_llc_linktype(proto);
3009 /*NOTREACHED*/
3010 break;
3012 case DLT_ATM_RFC1483:
3013 case DLT_ATM_CLIP:
3014 case DLT_IP_OVER_FC:
3015 return gen_llc_linktype(proto);
3016 /*NOTREACHED*/
3017 break;
3019 case DLT_SUNATM:
3021 * If "is_lane" is set, check for a LANE-encapsulated
3022 * version of this protocol, otherwise check for an
3023 * LLC-encapsulated version of this protocol.
3025 * We assume LANE means Ethernet, not Token Ring.
3027 if (is_lane) {
3029 * Check that the packet doesn't begin with an
3030 * LE Control marker. (We've already generated
3031 * a test for LANE.)
3033 b0 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
3034 0xFF00);
3035 gen_not(b0);
3038 * Now generate an Ethernet test.
3040 b1 = gen_ether_linktype(proto);
3041 gen_and(b0, b1);
3042 return b1;
3043 } else {
3045 * Check for LLC encapsulation and then check the
3046 * protocol.
3048 b0 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
3049 b1 = gen_llc_linktype(proto);
3050 gen_and(b0, b1);
3051 return b1;
3053 /*NOTREACHED*/
3054 break;
3056 case DLT_LINUX_SLL:
3057 return gen_linux_sll_linktype(proto);
3058 /*NOTREACHED*/
3059 break;
3061 case DLT_SLIP:
3062 case DLT_SLIP_BSDOS:
3063 case DLT_RAW:
3065 * These types don't provide any type field; packets
3066 * are always IPv4 or IPv6.
3068 * XXX - for IPv4, check for a version number of 4, and,
3069 * for IPv6, check for a version number of 6?
3071 switch (proto) {
3073 case ETHERTYPE_IP:
3074 /* Check for a version number of 4. */
3075 return gen_mcmp(OR_LINK, 0, BPF_B, 0x40, 0xF0);
3076 #ifdef INET6
3077 case ETHERTYPE_IPV6:
3078 /* Check for a version number of 6. */
3079 return gen_mcmp(OR_LINK, 0, BPF_B, 0x60, 0xF0);
3080 #endif
3082 default:
3083 return gen_false(); /* always false */
3085 /*NOTREACHED*/
3086 break;
3088 case DLT_IPV4:
3090 * Raw IPv4, so no type field.
3092 if (proto == ETHERTYPE_IP)
3093 return gen_true(); /* always true */
3095 /* Checking for something other than IPv4; always false */
3096 return gen_false();
3097 /*NOTREACHED*/
3098 break;
3100 case DLT_IPV6:
3102 * Raw IPv6, so no type field.
3104 #ifdef INET6
3105 if (proto == ETHERTYPE_IPV6)
3106 return gen_true(); /* always true */
3107 #endif
3109 /* Checking for something other than IPv6; always false */
3110 return gen_false();
3111 /*NOTREACHED*/
3112 break;
3114 case DLT_PPP:
3115 case DLT_PPP_PPPD:
3116 case DLT_PPP_SERIAL:
3117 case DLT_PPP_ETHER:
3119 * We use Ethernet protocol types inside libpcap;
3120 * map them to the corresponding PPP protocol types.
3122 proto = ethertype_to_ppptype(proto);
3123 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3124 /*NOTREACHED*/
3125 break;
3127 case DLT_PPP_BSDOS:
3129 * We use Ethernet protocol types inside libpcap;
3130 * map them to the corresponding PPP protocol types.
3132 switch (proto) {
3134 case ETHERTYPE_IP:
3136 * Also check for Van Jacobson-compressed IP.
3137 * XXX - do this for other forms of PPP?
3139 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_IP);
3140 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJC);
3141 gen_or(b0, b1);
3142 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H, PPP_VJNC);
3143 gen_or(b1, b0);
3144 return b0;
3146 default:
3147 proto = ethertype_to_ppptype(proto);
3148 return gen_cmp(OR_LINK, off_linktype, BPF_H,
3149 (bpf_int32)proto);
3151 /*NOTREACHED*/
3152 break;
3154 case DLT_NULL:
3155 case DLT_LOOP:
3156 case DLT_ENC:
3158 * For DLT_NULL, the link-layer header is a 32-bit
3159 * word containing an AF_ value in *host* byte order,
3160 * and for DLT_ENC, the link-layer header begins
3161 * with a 32-bit work containing an AF_ value in
3162 * host byte order.
3164 * In addition, if we're reading a saved capture file,
3165 * the host byte order in the capture may not be the
3166 * same as the host byte order on this machine.
3168 * For DLT_LOOP, the link-layer header is a 32-bit
3169 * word containing an AF_ value in *network* byte order.
3171 * XXX - AF_ values may, unfortunately, be platform-
3172 * dependent; for example, FreeBSD's AF_INET6 is 24
3173 * whilst NetBSD's and OpenBSD's is 26.
3175 * This means that, when reading a capture file, just
3176 * checking for our AF_INET6 value won't work if the
3177 * capture file came from another OS.
3179 switch (proto) {
3181 case ETHERTYPE_IP:
3182 proto = AF_INET;
3183 break;
3185 #ifdef INET6
3186 case ETHERTYPE_IPV6:
3187 proto = AF_INET6;
3188 break;
3189 #endif
3191 default:
3193 * Not a type on which we support filtering.
3194 * XXX - support those that have AF_ values
3195 * #defined on this platform, at least?
3197 return gen_false();
3200 if (linktype == DLT_NULL || linktype == DLT_ENC) {
3202 * The AF_ value is in host byte order, but
3203 * the BPF interpreter will convert it to
3204 * network byte order.
3206 * If this is a save file, and it's from a
3207 * machine with the opposite byte order to
3208 * ours, we byte-swap the AF_ value.
3210 * Then we run it through "htonl()", and
3211 * generate code to compare against the result.
3213 if (bpf_pcap->sf.rfile != NULL &&
3214 bpf_pcap->sf.swapped)
3215 proto = SWAPLONG(proto);
3216 proto = htonl(proto);
3218 return (gen_cmp(OR_LINK, 0, BPF_W, (bpf_int32)proto));
3220 #ifdef HAVE_NET_PFVAR_H
3221 case DLT_PFLOG:
3223 * af field is host byte order in contrast to the rest of
3224 * the packet.
3226 if (proto == ETHERTYPE_IP)
3227 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3228 BPF_B, (bpf_int32)AF_INET));
3229 #ifdef INET6
3230 else if (proto == ETHERTYPE_IPV6)
3231 return (gen_cmp(OR_LINK, offsetof(struct pfloghdr, af),
3232 BPF_B, (bpf_int32)AF_INET6));
3233 #endif /* INET6 */
3234 else
3235 return gen_false();
3236 /*NOTREACHED*/
3237 break;
3238 #endif /* HAVE_NET_PFVAR_H */
3240 case DLT_ARCNET:
3241 case DLT_ARCNET_LINUX:
3243 * XXX should we check for first fragment if the protocol
3244 * uses PHDS?
3246 switch (proto) {
3248 default:
3249 return gen_false();
3251 #ifdef INET6
3252 case ETHERTYPE_IPV6:
3253 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3254 (bpf_int32)ARCTYPE_INET6));
3255 #endif /* INET6 */
3257 case ETHERTYPE_IP:
3258 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3259 (bpf_int32)ARCTYPE_IP);
3260 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3261 (bpf_int32)ARCTYPE_IP_OLD);
3262 gen_or(b0, b1);
3263 return (b1);
3265 case ETHERTYPE_ARP:
3266 b0 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3267 (bpf_int32)ARCTYPE_ARP);
3268 b1 = gen_cmp(OR_LINK, off_linktype, BPF_B,
3269 (bpf_int32)ARCTYPE_ARP_OLD);
3270 gen_or(b0, b1);
3271 return (b1);
3273 case ETHERTYPE_REVARP:
3274 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3275 (bpf_int32)ARCTYPE_REVARP));
3277 case ETHERTYPE_ATALK:
3278 return (gen_cmp(OR_LINK, off_linktype, BPF_B,
3279 (bpf_int32)ARCTYPE_ATALK));
3281 /*NOTREACHED*/
3282 break;
3284 case DLT_LTALK:
3285 switch (proto) {
3286 case ETHERTYPE_ATALK:
3287 return gen_true();
3288 default:
3289 return gen_false();
3291 /*NOTREACHED*/
3292 break;
3294 case DLT_FRELAY:
3296 * XXX - assumes a 2-byte Frame Relay header with
3297 * DLCI and flags. What if the address is longer?
3299 switch (proto) {
3301 case ETHERTYPE_IP:
3303 * Check for the special NLPID for IP.
3305 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0xcc);
3307 #ifdef INET6
3308 case ETHERTYPE_IPV6:
3310 * Check for the special NLPID for IPv6.
3312 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | 0x8e);
3313 #endif
3315 case LLCSAP_ISONS:
3317 * Check for several OSI protocols.
3319 * Frame Relay packets typically have an OSI
3320 * NLPID at the beginning; we check for each
3321 * of them.
3323 * What we check for is the NLPID and a frame
3324 * control field of UI, i.e. 0x03 followed
3325 * by the NLPID.
3327 b0 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO8473_CLNP);
3328 b1 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO9542_ESIS);
3329 b2 = gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | ISO10589_ISIS);
3330 gen_or(b1, b2);
3331 gen_or(b0, b2);
3332 return b2;
3334 default:
3335 return gen_false();
3337 /*NOTREACHED*/
3338 break;
3340 case DLT_MFR:
3341 bpf_error("Multi-link Frame Relay link-layer type filtering not implemented");
3343 case DLT_JUNIPER_MFR:
3344 case DLT_JUNIPER_MLFR:
3345 case DLT_JUNIPER_MLPPP:
3346 case DLT_JUNIPER_ATM1:
3347 case DLT_JUNIPER_ATM2:
3348 case DLT_JUNIPER_PPPOE:
3349 case DLT_JUNIPER_PPPOE_ATM:
3350 case DLT_JUNIPER_GGSN:
3351 case DLT_JUNIPER_ES:
3352 case DLT_JUNIPER_MONITOR:
3353 case DLT_JUNIPER_SERVICES:
3354 case DLT_JUNIPER_ETHER:
3355 case DLT_JUNIPER_PPP:
3356 case DLT_JUNIPER_FRELAY:
3357 case DLT_JUNIPER_CHDLC:
3358 case DLT_JUNIPER_VP:
3359 case DLT_JUNIPER_ST:
3360 case DLT_JUNIPER_ISM:
3361 case DLT_JUNIPER_VS:
3362 case DLT_JUNIPER_SRX_E2E:
3363 case DLT_JUNIPER_FIBRECHANNEL:
3364 case DLT_JUNIPER_ATM_CEMIC:
3366 /* just lets verify the magic number for now -
3367 * on ATM we may have up to 6 different encapsulations on the wire
3368 * and need a lot of heuristics to figure out that the payload
3369 * might be;
3371 * FIXME encapsulation specific BPF_ filters
3373 return gen_mcmp(OR_LINK, 0, BPF_W, 0x4d474300, 0xffffff00); /* compare the magic number */
3375 case DLT_IPNET:
3376 return gen_ipnet_linktype(proto);
3378 case DLT_LINUX_IRDA:
3379 bpf_error("IrDA link-layer type filtering not implemented");
3381 case DLT_DOCSIS:
3382 bpf_error("DOCSIS link-layer type filtering not implemented");
3384 case DLT_MTP2:
3385 case DLT_MTP2_WITH_PHDR:
3386 bpf_error("MTP2 link-layer type filtering not implemented");
3388 case DLT_ERF:
3389 bpf_error("ERF link-layer type filtering not implemented");
3391 case DLT_PFSYNC:
3392 bpf_error("PFSYNC link-layer type filtering not implemented");
3394 case DLT_LINUX_LAPD:
3395 bpf_error("LAPD link-layer type filtering not implemented");
3397 case DLT_USB:
3398 case DLT_USB_LINUX:
3399 case DLT_USB_LINUX_MMAPPED:
3400 bpf_error("USB link-layer type filtering not implemented");
3402 case DLT_BLUETOOTH_HCI_H4:
3403 case DLT_BLUETOOTH_HCI_H4_WITH_PHDR:
3404 bpf_error("Bluetooth link-layer type filtering not implemented");
3406 case DLT_CAN20B:
3407 case DLT_CAN_SOCKETCAN:
3408 bpf_error("CAN link-layer type filtering not implemented");
3410 case DLT_IEEE802_15_4:
3411 case DLT_IEEE802_15_4_LINUX:
3412 case DLT_IEEE802_15_4_NONASK_PHY:
3413 case DLT_IEEE802_15_4_NOFCS:
3414 bpf_error("IEEE 802.15.4 link-layer type filtering not implemented");
3416 case DLT_IEEE802_16_MAC_CPS_RADIO:
3417 bpf_error("IEEE 802.16 link-layer type filtering not implemented");
3419 case DLT_SITA:
3420 bpf_error("SITA link-layer type filtering not implemented");
3422 case DLT_RAIF1:
3423 bpf_error("RAIF1 link-layer type filtering not implemented");
3425 case DLT_IPMB:
3426 bpf_error("IPMB link-layer type filtering not implemented");
3428 case DLT_AX25_KISS:
3429 bpf_error("AX.25 link-layer type filtering not implemented");
3433 * All the types that have no encapsulation should either be
3434 * handled as DLT_SLIP, DLT_SLIP_BSDOS, and DLT_RAW are, if
3435 * all packets are IP packets, or should be handled in some
3436 * special case, if none of them are (if some are and some
3437 * aren't, the lack of encapsulation is a problem, as we'd
3438 * have to find some other way of determining the packet type).
3440 * Therefore, if "off_linktype" is -1, there's an error.
3442 if (off_linktype == (u_int)-1)
3443 abort();
3446 * Any type not handled above should always have an Ethernet
3447 * type at an offset of "off_linktype".
3449 return gen_cmp(OR_LINK, off_linktype, BPF_H, (bpf_int32)proto);
3453 * Check for an LLC SNAP packet with a given organization code and
3454 * protocol type; we check the entire contents of the 802.2 LLC and
3455 * snap headers, checking for DSAP and SSAP of SNAP and a control
3456 * field of 0x03 in the LLC header, and for the specified organization
3457 * code and protocol type in the SNAP header.
3459 static struct block *
3460 gen_snap(orgcode, ptype)
3461 bpf_u_int32 orgcode;
3462 bpf_u_int32 ptype;
3464 u_char snapblock[8];
3466 snapblock[0] = LLCSAP_SNAP; /* DSAP = SNAP */
3467 snapblock[1] = LLCSAP_SNAP; /* SSAP = SNAP */
3468 snapblock[2] = 0x03; /* control = UI */
3469 snapblock[3] = (orgcode >> 16); /* upper 8 bits of organization code */
3470 snapblock[4] = (orgcode >> 8); /* middle 8 bits of organization code */
3471 snapblock[5] = (orgcode >> 0); /* lower 8 bits of organization code */
3472 snapblock[6] = (ptype >> 8); /* upper 8 bits of protocol type */
3473 snapblock[7] = (ptype >> 0); /* lower 8 bits of protocol type */
3474 return gen_bcmp(OR_MACPL, 0, 8, snapblock);
3478 * Generate code to match a particular packet type, for link-layer types
3479 * using 802.2 LLC headers.
3481 * This is *NOT* used for Ethernet; "gen_ether_linktype()" is used
3482 * for that - it handles the D/I/X Ethernet vs. 802.3+802.2 issues.
3484 * "proto" is an Ethernet type value, if > ETHERMTU, or an LLC SAP
3485 * value, if <= ETHERMTU. We use that to determine whether to
3486 * match the DSAP or both DSAP and LSAP or to check the OUI and
3487 * protocol ID in a SNAP header.
3489 static struct block *
3490 gen_llc_linktype(proto)
3491 int proto;
3494 * XXX - handle token-ring variable-length header.
3496 switch (proto) {
3498 case LLCSAP_IP:
3499 case LLCSAP_ISONS:
3500 case LLCSAP_NETBEUI:
3502 * XXX - should we check both the DSAP and the
3503 * SSAP, like this, or should we check just the
3504 * DSAP, as we do for other types <= ETHERMTU
3505 * (i.e., other SAP values)?
3507 return gen_cmp(OR_MACPL, 0, BPF_H, (bpf_u_int32)
3508 ((proto << 8) | proto));
3510 case LLCSAP_IPX:
3512 * XXX - are there ever SNAP frames for IPX on
3513 * non-Ethernet 802.x networks?
3515 return gen_cmp(OR_MACPL, 0, BPF_B,
3516 (bpf_int32)LLCSAP_IPX);
3518 case ETHERTYPE_ATALK:
3520 * 802.2-encapsulated ETHERTYPE_ATALK packets are
3521 * SNAP packets with an organization code of
3522 * 0x080007 (Apple, for Appletalk) and a protocol
3523 * type of ETHERTYPE_ATALK (Appletalk).
3525 * XXX - check for an organization code of
3526 * encapsulated Ethernet as well?
3528 return gen_snap(0x080007, ETHERTYPE_ATALK);
3530 default:
3532 * XXX - we don't have to check for IPX 802.3
3533 * here, but should we check for the IPX Ethertype?
3535 if (proto <= ETHERMTU) {
3537 * This is an LLC SAP value, so check
3538 * the DSAP.
3540 return gen_cmp(OR_MACPL, 0, BPF_B, (bpf_int32)proto);
3541 } else {
3543 * This is an Ethernet type; we assume that it's
3544 * unlikely that it'll appear in the right place
3545 * at random, and therefore check only the
3546 * location that would hold the Ethernet type
3547 * in a SNAP frame with an organization code of
3548 * 0x000000 (encapsulated Ethernet).
3550 * XXX - if we were to check for the SNAP DSAP and
3551 * LSAP, as per XXX, and were also to check for an
3552 * organization code of 0x000000 (encapsulated
3553 * Ethernet), we'd do
3555 * return gen_snap(0x000000, proto);
3557 * here; for now, we don't, as per the above.
3558 * I don't know whether it's worth the extra CPU
3559 * time to do the right check or not.
3561 return gen_cmp(OR_MACPL, 6, BPF_H, (bpf_int32)proto);
3566 static struct block *
3567 gen_hostop(addr, mask, dir, proto, src_off, dst_off)
3568 bpf_u_int32 addr;
3569 bpf_u_int32 mask;
3570 int dir, proto;
3571 u_int src_off, dst_off;
3573 struct block *b0, *b1;
3574 u_int offset;
3576 switch (dir) {
3578 case Q_SRC:
3579 offset = src_off;
3580 break;
3582 case Q_DST:
3583 offset = dst_off;
3584 break;
3586 case Q_AND:
3587 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3588 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3589 gen_and(b0, b1);
3590 return b1;
3592 case Q_OR:
3593 case Q_DEFAULT:
3594 b0 = gen_hostop(addr, mask, Q_SRC, proto, src_off, dst_off);
3595 b1 = gen_hostop(addr, mask, Q_DST, proto, src_off, dst_off);
3596 gen_or(b0, b1);
3597 return b1;
3599 default:
3600 abort();
3602 b0 = gen_linktype(proto);
3603 b1 = gen_mcmp(OR_NET, offset, BPF_W, (bpf_int32)addr, mask);
3604 gen_and(b0, b1);
3605 return b1;
3608 #ifdef INET6
3609 static struct block *
3610 gen_hostop6(addr, mask, dir, proto, src_off, dst_off)
3611 struct in6_addr *addr;
3612 struct in6_addr *mask;
3613 int dir, proto;
3614 u_int src_off, dst_off;
3616 struct block *b0, *b1;
3617 u_int offset;
3618 u_int32_t *a, *m;
3620 switch (dir) {
3622 case Q_SRC:
3623 offset = src_off;
3624 break;
3626 case Q_DST:
3627 offset = dst_off;
3628 break;
3630 case Q_AND:
3631 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3632 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3633 gen_and(b0, b1);
3634 return b1;
3636 case Q_OR:
3637 case Q_DEFAULT:
3638 b0 = gen_hostop6(addr, mask, Q_SRC, proto, src_off, dst_off);
3639 b1 = gen_hostop6(addr, mask, Q_DST, proto, src_off, dst_off);
3640 gen_or(b0, b1);
3641 return b1;
3643 default:
3644 abort();
3646 /* this order is important */
3647 a = (u_int32_t *)addr;
3648 m = (u_int32_t *)mask;
3649 b1 = gen_mcmp(OR_NET, offset + 12, BPF_W, ntohl(a[3]), ntohl(m[3]));
3650 b0 = gen_mcmp(OR_NET, offset + 8, BPF_W, ntohl(a[2]), ntohl(m[2]));
3651 gen_and(b0, b1);
3652 b0 = gen_mcmp(OR_NET, offset + 4, BPF_W, ntohl(a[1]), ntohl(m[1]));
3653 gen_and(b0, b1);
3654 b0 = gen_mcmp(OR_NET, offset + 0, BPF_W, ntohl(a[0]), ntohl(m[0]));
3655 gen_and(b0, b1);
3656 b0 = gen_linktype(proto);
3657 gen_and(b0, b1);
3658 return b1;
3660 #endif /*INET6*/
3662 static struct block *
3663 gen_ehostop(eaddr, dir)
3664 register const u_char *eaddr;
3665 register int dir;
3667 register struct block *b0, *b1;
3669 switch (dir) {
3670 case Q_SRC:
3671 return gen_bcmp(OR_LINK, off_mac + 6, 6, eaddr);
3673 case Q_DST:
3674 return gen_bcmp(OR_LINK, off_mac + 0, 6, eaddr);
3676 case Q_AND:
3677 b0 = gen_ehostop(eaddr, Q_SRC);
3678 b1 = gen_ehostop(eaddr, Q_DST);
3679 gen_and(b0, b1);
3680 return b1;
3682 case Q_DEFAULT:
3683 case Q_OR:
3684 b0 = gen_ehostop(eaddr, Q_SRC);
3685 b1 = gen_ehostop(eaddr, Q_DST);
3686 gen_or(b0, b1);
3687 return b1;
3689 case Q_ADDR1:
3690 bpf_error("'addr1' is only supported on 802.11 with 802.11 headers");
3691 break;
3693 case Q_ADDR2:
3694 bpf_error("'addr2' is only supported on 802.11 with 802.11 headers");
3695 break;
3697 case Q_ADDR3:
3698 bpf_error("'addr3' is only supported on 802.11 with 802.11 headers");
3699 break;
3701 case Q_ADDR4:
3702 bpf_error("'addr4' is only supported on 802.11 with 802.11 headers");
3703 break;
3705 case Q_RA:
3706 bpf_error("'ra' is only supported on 802.11 with 802.11 headers");
3707 break;
3709 case Q_TA:
3710 bpf_error("'ta' is only supported on 802.11 with 802.11 headers");
3711 break;
3713 abort();
3714 /* NOTREACHED */
3718 * Like gen_ehostop, but for DLT_FDDI
3720 static struct block *
3721 gen_fhostop(eaddr, dir)
3722 register const u_char *eaddr;
3723 register int dir;
3725 struct block *b0, *b1;
3727 switch (dir) {
3728 case Q_SRC:
3729 #ifdef PCAP_FDDIPAD
3730 return gen_bcmp(OR_LINK, 6 + 1 + pcap_fddipad, 6, eaddr);
3731 #else
3732 return gen_bcmp(OR_LINK, 6 + 1, 6, eaddr);
3733 #endif
3735 case Q_DST:
3736 #ifdef PCAP_FDDIPAD
3737 return gen_bcmp(OR_LINK, 0 + 1 + pcap_fddipad, 6, eaddr);
3738 #else
3739 return gen_bcmp(OR_LINK, 0 + 1, 6, eaddr);
3740 #endif
3742 case Q_AND:
3743 b0 = gen_fhostop(eaddr, Q_SRC);
3744 b1 = gen_fhostop(eaddr, Q_DST);
3745 gen_and(b0, b1);
3746 return b1;
3748 case Q_DEFAULT:
3749 case Q_OR:
3750 b0 = gen_fhostop(eaddr, Q_SRC);
3751 b1 = gen_fhostop(eaddr, Q_DST);
3752 gen_or(b0, b1);
3753 return b1;
3755 case Q_ADDR1:
3756 bpf_error("'addr1' is only supported on 802.11");
3757 break;
3759 case Q_ADDR2:
3760 bpf_error("'addr2' is only supported on 802.11");
3761 break;
3763 case Q_ADDR3:
3764 bpf_error("'addr3' is only supported on 802.11");
3765 break;
3767 case Q_ADDR4:
3768 bpf_error("'addr4' is only supported on 802.11");
3769 break;
3771 case Q_RA:
3772 bpf_error("'ra' is only supported on 802.11");
3773 break;
3775 case Q_TA:
3776 bpf_error("'ta' is only supported on 802.11");
3777 break;
3779 abort();
3780 /* NOTREACHED */
3784 * Like gen_ehostop, but for DLT_IEEE802 (Token Ring)
3786 static struct block *
3787 gen_thostop(eaddr, dir)
3788 register const u_char *eaddr;
3789 register int dir;
3791 register struct block *b0, *b1;
3793 switch (dir) {
3794 case Q_SRC:
3795 return gen_bcmp(OR_LINK, 8, 6, eaddr);
3797 case Q_DST:
3798 return gen_bcmp(OR_LINK, 2, 6, eaddr);
3800 case Q_AND:
3801 b0 = gen_thostop(eaddr, Q_SRC);
3802 b1 = gen_thostop(eaddr, Q_DST);
3803 gen_and(b0, b1);
3804 return b1;
3806 case Q_DEFAULT:
3807 case Q_OR:
3808 b0 = gen_thostop(eaddr, Q_SRC);
3809 b1 = gen_thostop(eaddr, Q_DST);
3810 gen_or(b0, b1);
3811 return b1;
3813 case Q_ADDR1:
3814 bpf_error("'addr1' is only supported on 802.11");
3815 break;
3817 case Q_ADDR2:
3818 bpf_error("'addr2' is only supported on 802.11");
3819 break;
3821 case Q_ADDR3:
3822 bpf_error("'addr3' is only supported on 802.11");
3823 break;
3825 case Q_ADDR4:
3826 bpf_error("'addr4' is only supported on 802.11");
3827 break;
3829 case Q_RA:
3830 bpf_error("'ra' is only supported on 802.11");
3831 break;
3833 case Q_TA:
3834 bpf_error("'ta' is only supported on 802.11");
3835 break;
3837 abort();
3838 /* NOTREACHED */
3842 * Like gen_ehostop, but for DLT_IEEE802_11 (802.11 wireless LAN) and
3843 * various 802.11 + radio headers.
3845 static struct block *
3846 gen_wlanhostop(eaddr, dir)
3847 register const u_char *eaddr;
3848 register int dir;
3850 register struct block *b0, *b1, *b2;
3851 register struct slist *s;
3853 #ifdef ENABLE_WLAN_FILTERING_PATCH
3855 * TODO GV 20070613
3856 * We need to disable the optimizer because the optimizer is buggy
3857 * and wipes out some LD instructions generated by the below
3858 * code to validate the Frame Control bits
3860 no_optimize = 1;
3861 #endif /* ENABLE_WLAN_FILTERING_PATCH */
3863 switch (dir) {
3864 case Q_SRC:
3866 * Oh, yuk.
3868 * For control frames, there is no SA.
3870 * For management frames, SA is at an
3871 * offset of 10 from the beginning of
3872 * the packet.
3874 * For data frames, SA is at an offset
3875 * of 10 from the beginning of the packet
3876 * if From DS is clear, at an offset of
3877 * 16 from the beginning of the packet
3878 * if From DS is set and To DS is clear,
3879 * and an offset of 24 from the beginning
3880 * of the packet if From DS is set and To DS
3881 * is set.
3885 * Generate the tests to be done for data frames
3886 * with From DS set.
3888 * First, check for To DS set, i.e. check "link[1] & 0x01".
3890 s = gen_load_a(OR_LINK, 1, BPF_B);
3891 b1 = new_block(JMP(BPF_JSET));
3892 b1->s.k = 0x01; /* To DS */
3893 b1->stmts = s;
3896 * If To DS is set, the SA is at 24.
3898 b0 = gen_bcmp(OR_LINK, 24, 6, eaddr);
3899 gen_and(b1, b0);
3902 * Now, check for To DS not set, i.e. check
3903 * "!(link[1] & 0x01)".
3905 s = gen_load_a(OR_LINK, 1, BPF_B);
3906 b2 = new_block(JMP(BPF_JSET));
3907 b2->s.k = 0x01; /* To DS */
3908 b2->stmts = s;
3909 gen_not(b2);
3912 * If To DS is not set, the SA is at 16.
3914 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
3915 gen_and(b2, b1);
3918 * Now OR together the last two checks. That gives
3919 * the complete set of checks for data frames with
3920 * From DS set.
3922 gen_or(b1, b0);
3925 * Now check for From DS being set, and AND that with
3926 * the ORed-together checks.
3928 s = gen_load_a(OR_LINK, 1, BPF_B);
3929 b1 = new_block(JMP(BPF_JSET));
3930 b1->s.k = 0x02; /* From DS */
3931 b1->stmts = s;
3932 gen_and(b1, b0);
3935 * Now check for data frames with From DS not set.
3937 s = gen_load_a(OR_LINK, 1, BPF_B);
3938 b2 = new_block(JMP(BPF_JSET));
3939 b2->s.k = 0x02; /* From DS */
3940 b2->stmts = s;
3941 gen_not(b2);
3944 * If From DS isn't set, the SA is at 10.
3946 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3947 gen_and(b2, b1);
3950 * Now OR together the checks for data frames with
3951 * From DS not set and for data frames with From DS
3952 * set; that gives the checks done for data frames.
3954 gen_or(b1, b0);
3957 * Now check for a data frame.
3958 * I.e, check "link[0] & 0x08".
3960 s = gen_load_a(OR_LINK, 0, BPF_B);
3961 b1 = new_block(JMP(BPF_JSET));
3962 b1->s.k = 0x08;
3963 b1->stmts = s;
3966 * AND that with the checks done for data frames.
3968 gen_and(b1, b0);
3971 * If the high-order bit of the type value is 0, this
3972 * is a management frame.
3973 * I.e, check "!(link[0] & 0x08)".
3975 s = gen_load_a(OR_LINK, 0, BPF_B);
3976 b2 = new_block(JMP(BPF_JSET));
3977 b2->s.k = 0x08;
3978 b2->stmts = s;
3979 gen_not(b2);
3982 * For management frames, the SA is at 10.
3984 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
3985 gen_and(b2, b1);
3988 * OR that with the checks done for data frames.
3989 * That gives the checks done for management and
3990 * data frames.
3992 gen_or(b1, b0);
3995 * If the low-order bit of the type value is 1,
3996 * this is either a control frame or a frame
3997 * with a reserved type, and thus not a
3998 * frame with an SA.
4000 * I.e., check "!(link[0] & 0x04)".
4002 s = gen_load_a(OR_LINK, 0, BPF_B);
4003 b1 = new_block(JMP(BPF_JSET));
4004 b1->s.k = 0x04;
4005 b1->stmts = s;
4006 gen_not(b1);
4009 * AND that with the checks for data and management
4010 * frames.
4012 gen_and(b1, b0);
4013 return b0;
4015 case Q_DST:
4017 * Oh, yuk.
4019 * For control frames, there is no DA.
4021 * For management frames, DA is at an
4022 * offset of 4 from the beginning of
4023 * the packet.
4025 * For data frames, DA is at an offset
4026 * of 4 from the beginning of the packet
4027 * if To DS is clear and at an offset of
4028 * 16 from the beginning of the packet
4029 * if To DS is set.
4033 * Generate the tests to be done for data frames.
4035 * First, check for To DS set, i.e. "link[1] & 0x01".
4037 s = gen_load_a(OR_LINK, 1, BPF_B);
4038 b1 = new_block(JMP(BPF_JSET));
4039 b1->s.k = 0x01; /* To DS */
4040 b1->stmts = s;
4043 * If To DS is set, the DA is at 16.
4045 b0 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4046 gen_and(b1, b0);
4049 * Now, check for To DS not set, i.e. check
4050 * "!(link[1] & 0x01)".
4052 s = gen_load_a(OR_LINK, 1, BPF_B);
4053 b2 = new_block(JMP(BPF_JSET));
4054 b2->s.k = 0x01; /* To DS */
4055 b2->stmts = s;
4056 gen_not(b2);
4059 * If To DS is not set, the DA is at 4.
4061 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4062 gen_and(b2, b1);
4065 * Now OR together the last two checks. That gives
4066 * the complete set of checks for data frames.
4068 gen_or(b1, b0);
4071 * Now check for a data frame.
4072 * I.e, check "link[0] & 0x08".
4074 s = gen_load_a(OR_LINK, 0, BPF_B);
4075 b1 = new_block(JMP(BPF_JSET));
4076 b1->s.k = 0x08;
4077 b1->stmts = s;
4080 * AND that with the checks done for data frames.
4082 gen_and(b1, b0);
4085 * If the high-order bit of the type value is 0, this
4086 * is a management frame.
4087 * I.e, check "!(link[0] & 0x08)".
4089 s = gen_load_a(OR_LINK, 0, BPF_B);
4090 b2 = new_block(JMP(BPF_JSET));
4091 b2->s.k = 0x08;
4092 b2->stmts = s;
4093 gen_not(b2);
4096 * For management frames, the DA is at 4.
4098 b1 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4099 gen_and(b2, b1);
4102 * OR that with the checks done for data frames.
4103 * That gives the checks done for management and
4104 * data frames.
4106 gen_or(b1, b0);
4109 * If the low-order bit of the type value is 1,
4110 * this is either a control frame or a frame
4111 * with a reserved type, and thus not a
4112 * frame with an SA.
4114 * I.e., check "!(link[0] & 0x04)".
4116 s = gen_load_a(OR_LINK, 0, BPF_B);
4117 b1 = new_block(JMP(BPF_JSET));
4118 b1->s.k = 0x04;
4119 b1->stmts = s;
4120 gen_not(b1);
4123 * AND that with the checks for data and management
4124 * frames.
4126 gen_and(b1, b0);
4127 return b0;
4129 case Q_RA:
4131 * Not present in management frames; addr1 in other
4132 * frames.
4136 * If the high-order bit of the type value is 0, this
4137 * is a management frame.
4138 * I.e, check "(link[0] & 0x08)".
4140 s = gen_load_a(OR_LINK, 0, BPF_B);
4141 b1 = new_block(JMP(BPF_JSET));
4142 b1->s.k = 0x08;
4143 b1->stmts = s;
4146 * Check addr1.
4148 b0 = gen_bcmp(OR_LINK, 4, 6, eaddr);
4151 * AND that with the check of addr1.
4153 gen_and(b1, b0);
4154 return (b0);
4156 case Q_TA:
4158 * Not present in management frames; addr2, if present,
4159 * in other frames.
4163 * Not present in CTS or ACK control frames.
4165 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4166 IEEE80211_FC0_TYPE_MASK);
4167 gen_not(b0);
4168 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4169 IEEE80211_FC0_SUBTYPE_MASK);
4170 gen_not(b1);
4171 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4172 IEEE80211_FC0_SUBTYPE_MASK);
4173 gen_not(b2);
4174 gen_and(b1, b2);
4175 gen_or(b0, b2);
4178 * If the high-order bit of the type value is 0, this
4179 * is a management frame.
4180 * I.e, check "(link[0] & 0x08)".
4182 s = gen_load_a(OR_LINK, 0, BPF_B);
4183 b1 = new_block(JMP(BPF_JSET));
4184 b1->s.k = 0x08;
4185 b1->stmts = s;
4188 * AND that with the check for frames other than
4189 * CTS and ACK frames.
4191 gen_and(b1, b2);
4194 * Check addr2.
4196 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4197 gen_and(b2, b1);
4198 return b1;
4201 * XXX - add BSSID keyword?
4203 case Q_ADDR1:
4204 return (gen_bcmp(OR_LINK, 4, 6, eaddr));
4206 case Q_ADDR2:
4208 * Not present in CTS or ACK control frames.
4210 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4211 IEEE80211_FC0_TYPE_MASK);
4212 gen_not(b0);
4213 b1 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_CTS,
4214 IEEE80211_FC0_SUBTYPE_MASK);
4215 gen_not(b1);
4216 b2 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_SUBTYPE_ACK,
4217 IEEE80211_FC0_SUBTYPE_MASK);
4218 gen_not(b2);
4219 gen_and(b1, b2);
4220 gen_or(b0, b2);
4221 b1 = gen_bcmp(OR_LINK, 10, 6, eaddr);
4222 gen_and(b2, b1);
4223 return b1;
4225 case Q_ADDR3:
4227 * Not present in control frames.
4229 b0 = gen_mcmp(OR_LINK, 0, BPF_B, IEEE80211_FC0_TYPE_CTL,
4230 IEEE80211_FC0_TYPE_MASK);
4231 gen_not(b0);
4232 b1 = gen_bcmp(OR_LINK, 16, 6, eaddr);
4233 gen_and(b0, b1);
4234 return b1;
4236 case Q_ADDR4:
4238 * Present only if the direction mask has both "From DS"
4239 * and "To DS" set. Neither control frames nor management
4240 * frames should have both of those set, so we don't
4241 * check the frame type.
4243 b0 = gen_mcmp(OR_LINK, 1, BPF_B,
4244 IEEE80211_FC1_DIR_DSTODS, IEEE80211_FC1_DIR_MASK);
4245 b1 = gen_bcmp(OR_LINK, 24, 6, eaddr);
4246 gen_and(b0, b1);
4247 return b1;
4249 case Q_AND:
4250 b0 = gen_wlanhostop(eaddr, Q_SRC);
4251 b1 = gen_wlanhostop(eaddr, Q_DST);
4252 gen_and(b0, b1);
4253 return b1;
4255 case Q_DEFAULT:
4256 case Q_OR:
4257 b0 = gen_wlanhostop(eaddr, Q_SRC);
4258 b1 = gen_wlanhostop(eaddr, Q_DST);
4259 gen_or(b0, b1);
4260 return b1;
4262 abort();
4263 /* NOTREACHED */
4267 * Like gen_ehostop, but for RFC 2625 IP-over-Fibre-Channel.
4268 * (We assume that the addresses are IEEE 48-bit MAC addresses,
4269 * as the RFC states.)
4271 static struct block *
4272 gen_ipfchostop(eaddr, dir)
4273 register const u_char *eaddr;
4274 register int dir;
4276 register struct block *b0, *b1;
4278 switch (dir) {
4279 case Q_SRC:
4280 return gen_bcmp(OR_LINK, 10, 6, eaddr);
4282 case Q_DST:
4283 return gen_bcmp(OR_LINK, 2, 6, eaddr);
4285 case Q_AND:
4286 b0 = gen_ipfchostop(eaddr, Q_SRC);
4287 b1 = gen_ipfchostop(eaddr, Q_DST);
4288 gen_and(b0, b1);
4289 return b1;
4291 case Q_DEFAULT:
4292 case Q_OR:
4293 b0 = gen_ipfchostop(eaddr, Q_SRC);
4294 b1 = gen_ipfchostop(eaddr, Q_DST);
4295 gen_or(b0, b1);
4296 return b1;
4298 case Q_ADDR1:
4299 bpf_error("'addr1' is only supported on 802.11");
4300 break;
4302 case Q_ADDR2:
4303 bpf_error("'addr2' is only supported on 802.11");
4304 break;
4306 case Q_ADDR3:
4307 bpf_error("'addr3' is only supported on 802.11");
4308 break;
4310 case Q_ADDR4:
4311 bpf_error("'addr4' is only supported on 802.11");
4312 break;
4314 case Q_RA:
4315 bpf_error("'ra' is only supported on 802.11");
4316 break;
4318 case Q_TA:
4319 bpf_error("'ta' is only supported on 802.11");
4320 break;
4322 abort();
4323 /* NOTREACHED */
4327 * This is quite tricky because there may be pad bytes in front of the
4328 * DECNET header, and then there are two possible data packet formats that
4329 * carry both src and dst addresses, plus 5 packet types in a format that
4330 * carries only the src node, plus 2 types that use a different format and
4331 * also carry just the src node.
4333 * Yuck.
4335 * Instead of doing those all right, we just look for data packets with
4336 * 0 or 1 bytes of padding. If you want to look at other packets, that
4337 * will require a lot more hacking.
4339 * To add support for filtering on DECNET "areas" (network numbers)
4340 * one would want to add a "mask" argument to this routine. That would
4341 * make the filter even more inefficient, although one could be clever
4342 * and not generate masking instructions if the mask is 0xFFFF.
4344 static struct block *
4345 gen_dnhostop(addr, dir)
4346 bpf_u_int32 addr;
4347 int dir;
4349 struct block *b0, *b1, *b2, *tmp;
4350 u_int offset_lh; /* offset if long header is received */
4351 u_int offset_sh; /* offset if short header is received */
4353 switch (dir) {
4355 case Q_DST:
4356 offset_sh = 1; /* follows flags */
4357 offset_lh = 7; /* flgs,darea,dsubarea,HIORD */
4358 break;
4360 case Q_SRC:
4361 offset_sh = 3; /* follows flags, dstnode */
4362 offset_lh = 15; /* flgs,darea,dsubarea,did,sarea,ssub,HIORD */
4363 break;
4365 case Q_AND:
4366 /* Inefficient because we do our Calvinball dance twice */
4367 b0 = gen_dnhostop(addr, Q_SRC);
4368 b1 = gen_dnhostop(addr, Q_DST);
4369 gen_and(b0, b1);
4370 return b1;
4372 case Q_OR:
4373 case Q_DEFAULT:
4374 /* Inefficient because we do our Calvinball dance twice */
4375 b0 = gen_dnhostop(addr, Q_SRC);
4376 b1 = gen_dnhostop(addr, Q_DST);
4377 gen_or(b0, b1);
4378 return b1;
4380 case Q_ISO:
4381 bpf_error("ISO host filtering not implemented");
4383 default:
4384 abort();
4386 b0 = gen_linktype(ETHERTYPE_DN);
4387 /* Check for pad = 1, long header case */
4388 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4389 (bpf_int32)ntohs(0x0681), (bpf_int32)ntohs(0x07FF));
4390 b1 = gen_cmp(OR_NET, 2 + 1 + offset_lh,
4391 BPF_H, (bpf_int32)ntohs((u_short)addr));
4392 gen_and(tmp, b1);
4393 /* Check for pad = 0, long header case */
4394 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x06, (bpf_int32)0x7);
4395 b2 = gen_cmp(OR_NET, 2 + offset_lh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4396 gen_and(tmp, b2);
4397 gen_or(b2, b1);
4398 /* Check for pad = 1, short header case */
4399 tmp = gen_mcmp(OR_NET, 2, BPF_H,
4400 (bpf_int32)ntohs(0x0281), (bpf_int32)ntohs(0x07FF));
4401 b2 = gen_cmp(OR_NET, 2 + 1 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4402 gen_and(tmp, b2);
4403 gen_or(b2, b1);
4404 /* Check for pad = 0, short header case */
4405 tmp = gen_mcmp(OR_NET, 2, BPF_B, (bpf_int32)0x02, (bpf_int32)0x7);
4406 b2 = gen_cmp(OR_NET, 2 + offset_sh, BPF_H, (bpf_int32)ntohs((u_short)addr));
4407 gen_and(tmp, b2);
4408 gen_or(b2, b1);
4410 /* Combine with test for linktype */
4411 gen_and(b0, b1);
4412 return b1;
4416 * Generate a check for IPv4 or IPv6 for MPLS-encapsulated packets;
4417 * test the bottom-of-stack bit, and then check the version number
4418 * field in the IP header.
4420 static struct block *
4421 gen_mpls_linktype(proto)
4422 int proto;
4424 struct block *b0, *b1;
4426 switch (proto) {
4428 case Q_IP:
4429 /* match the bottom-of-stack bit */
4430 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4431 /* match the IPv4 version number */
4432 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x40, 0xf0);
4433 gen_and(b0, b1);
4434 return b1;
4436 case Q_IPV6:
4437 /* match the bottom-of-stack bit */
4438 b0 = gen_mcmp(OR_NET, -2, BPF_B, 0x01, 0x01);
4439 /* match the IPv4 version number */
4440 b1 = gen_mcmp(OR_NET, 0, BPF_B, 0x60, 0xf0);
4441 gen_and(b0, b1);
4442 return b1;
4444 default:
4445 abort();
4449 static struct block *
4450 gen_host(addr, mask, proto, dir, type)
4451 bpf_u_int32 addr;
4452 bpf_u_int32 mask;
4453 int proto;
4454 int dir;
4455 int type;
4457 struct block *b0, *b1;
4458 const char *typestr;
4460 if (type == Q_NET)
4461 typestr = "net";
4462 else
4463 typestr = "host";
4465 switch (proto) {
4467 case Q_DEFAULT:
4468 b0 = gen_host(addr, mask, Q_IP, dir, type);
4470 * Only check for non-IPv4 addresses if we're not
4471 * checking MPLS-encapsulated packets.
4473 if (label_stack_depth == 0) {
4474 b1 = gen_host(addr, mask, Q_ARP, dir, type);
4475 gen_or(b0, b1);
4476 b0 = gen_host(addr, mask, Q_RARP, dir, type);
4477 gen_or(b1, b0);
4479 return b0;
4481 case Q_IP:
4482 return gen_hostop(addr, mask, dir, ETHERTYPE_IP, 12, 16);
4484 case Q_RARP:
4485 return gen_hostop(addr, mask, dir, ETHERTYPE_REVARP, 14, 24);
4487 case Q_ARP:
4488 return gen_hostop(addr, mask, dir, ETHERTYPE_ARP, 14, 24);
4490 case Q_TCP:
4491 bpf_error("'tcp' modifier applied to %s", typestr);
4493 case Q_SCTP:
4494 bpf_error("'sctp' modifier applied to %s", typestr);
4496 case Q_UDP:
4497 bpf_error("'udp' modifier applied to %s", typestr);
4499 case Q_ICMP:
4500 bpf_error("'icmp' modifier applied to %s", typestr);
4502 case Q_IGMP:
4503 bpf_error("'igmp' modifier applied to %s", typestr);
4505 case Q_IGRP:
4506 bpf_error("'igrp' modifier applied to %s", typestr);
4508 case Q_PIM:
4509 bpf_error("'pim' modifier applied to %s", typestr);
4511 case Q_VRRP:
4512 bpf_error("'vrrp' modifier applied to %s", typestr);
4514 case Q_CARP:
4515 bpf_error("'carp' modifier applied to %s", typestr);
4517 case Q_ATALK:
4518 bpf_error("ATALK host filtering not implemented");
4520 case Q_AARP:
4521 bpf_error("AARP host filtering not implemented");
4523 case Q_DECNET:
4524 return gen_dnhostop(addr, dir);
4526 case Q_SCA:
4527 bpf_error("SCA host filtering not implemented");
4529 case Q_LAT:
4530 bpf_error("LAT host filtering not implemented");
4532 case Q_MOPDL:
4533 bpf_error("MOPDL host filtering not implemented");
4535 case Q_MOPRC:
4536 bpf_error("MOPRC host filtering not implemented");
4538 #ifdef INET6
4539 case Q_IPV6:
4540 bpf_error("'ip6' modifier applied to ip host");
4542 case Q_ICMPV6:
4543 bpf_error("'icmp6' modifier applied to %s", typestr);
4544 #endif /* INET6 */
4546 case Q_AH:
4547 bpf_error("'ah' modifier applied to %s", typestr);
4549 case Q_ESP:
4550 bpf_error("'esp' modifier applied to %s", typestr);
4552 case Q_ISO:
4553 bpf_error("ISO host filtering not implemented");
4555 case Q_ESIS:
4556 bpf_error("'esis' modifier applied to %s", typestr);
4558 case Q_ISIS:
4559 bpf_error("'isis' modifier applied to %s", typestr);
4561 case Q_CLNP:
4562 bpf_error("'clnp' modifier applied to %s", typestr);
4564 case Q_STP:
4565 bpf_error("'stp' modifier applied to %s", typestr);
4567 case Q_IPX:
4568 bpf_error("IPX host filtering not implemented");
4570 case Q_NETBEUI:
4571 bpf_error("'netbeui' modifier applied to %s", typestr);
4573 case Q_RADIO:
4574 bpf_error("'radio' modifier applied to %s", typestr);
4576 default:
4577 abort();
4579 /* NOTREACHED */
4582 #ifdef INET6
4583 static struct block *
4584 gen_host6(addr, mask, proto, dir, type)
4585 struct in6_addr *addr;
4586 struct in6_addr *mask;
4587 int proto;
4588 int dir;
4589 int type;
4591 const char *typestr;
4593 if (type == Q_NET)
4594 typestr = "net";
4595 else
4596 typestr = "host";
4598 switch (proto) {
4600 case Q_DEFAULT:
4601 return gen_host6(addr, mask, Q_IPV6, dir, type);
4603 case Q_IP:
4604 bpf_error("'ip' modifier applied to ip6 %s", typestr);
4606 case Q_RARP:
4607 bpf_error("'rarp' modifier applied to ip6 %s", typestr);
4609 case Q_ARP:
4610 bpf_error("'arp' modifier applied to ip6 %s", typestr);
4612 case Q_SCTP:
4613 bpf_error("'sctp' modifier applied to %s", typestr);
4615 case Q_TCP:
4616 bpf_error("'tcp' modifier applied to %s", typestr);
4618 case Q_UDP:
4619 bpf_error("'udp' modifier applied to %s", typestr);
4621 case Q_ICMP:
4622 bpf_error("'icmp' modifier applied to %s", typestr);
4624 case Q_IGMP:
4625 bpf_error("'igmp' modifier applied to %s", typestr);
4627 case Q_IGRP:
4628 bpf_error("'igrp' modifier applied to %s", typestr);
4630 case Q_PIM:
4631 bpf_error("'pim' modifier applied to %s", typestr);
4633 case Q_VRRP:
4634 bpf_error("'vrrp' modifier applied to %s", typestr);
4636 case Q_CARP:
4637 bpf_error("'carp' modifier applied to %s", typestr);
4639 case Q_ATALK:
4640 bpf_error("ATALK host filtering not implemented");
4642 case Q_AARP:
4643 bpf_error("AARP host filtering not implemented");
4645 case Q_DECNET:
4646 bpf_error("'decnet' modifier applied to ip6 %s", typestr);
4648 case Q_SCA:
4649 bpf_error("SCA host filtering not implemented");
4651 case Q_LAT:
4652 bpf_error("LAT host filtering not implemented");
4654 case Q_MOPDL:
4655 bpf_error("MOPDL host filtering not implemented");
4657 case Q_MOPRC:
4658 bpf_error("MOPRC host filtering not implemented");
4660 case Q_IPV6:
4661 return gen_hostop6(addr, mask, dir, ETHERTYPE_IPV6, 8, 24);
4663 case Q_ICMPV6:
4664 bpf_error("'icmp6' modifier applied to %s", typestr);
4666 case Q_AH:
4667 bpf_error("'ah' modifier applied to %s", typestr);
4669 case Q_ESP:
4670 bpf_error("'esp' modifier applied to %s", typestr);
4672 case Q_ISO:
4673 bpf_error("ISO host filtering not implemented");
4675 case Q_ESIS:
4676 bpf_error("'esis' modifier applied to %s", typestr);
4678 case Q_ISIS:
4679 bpf_error("'isis' modifier applied to %s", typestr);
4681 case Q_CLNP:
4682 bpf_error("'clnp' modifier applied to %s", typestr);
4684 case Q_STP:
4685 bpf_error("'stp' modifier applied to %s", typestr);
4687 case Q_IPX:
4688 bpf_error("IPX host filtering not implemented");
4690 case Q_NETBEUI:
4691 bpf_error("'netbeui' modifier applied to %s", typestr);
4693 case Q_RADIO:
4694 bpf_error("'radio' modifier applied to %s", typestr);
4696 default:
4697 abort();
4699 /* NOTREACHED */
4701 #endif /*INET6*/
4703 #ifndef INET6
4704 static struct block *
4705 gen_gateway(eaddr, alist, proto, dir)
4706 const u_char *eaddr;
4707 bpf_u_int32 **alist;
4708 int proto;
4709 int dir;
4711 struct block *b0, *b1, *tmp;
4713 if (dir != 0)
4714 bpf_error("direction applied to 'gateway'");
4716 switch (proto) {
4717 case Q_DEFAULT:
4718 case Q_IP:
4719 case Q_ARP:
4720 case Q_RARP:
4721 switch (linktype) {
4722 case DLT_EN10MB:
4723 case DLT_NETANALYZER:
4724 case DLT_NETANALYZER_TRANSPARENT:
4725 b0 = gen_ehostop(eaddr, Q_OR);
4726 break;
4727 case DLT_FDDI:
4728 b0 = gen_fhostop(eaddr, Q_OR);
4729 break;
4730 case DLT_IEEE802:
4731 b0 = gen_thostop(eaddr, Q_OR);
4732 break;
4733 case DLT_IEEE802_11:
4734 case DLT_PRISM_HEADER:
4735 case DLT_IEEE802_11_RADIO_AVS:
4736 case DLT_IEEE802_11_RADIO:
4737 case DLT_PPI:
4738 b0 = gen_wlanhostop(eaddr, Q_OR);
4739 break;
4740 case DLT_SUNATM:
4741 if (!is_lane)
4742 bpf_error(
4743 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4745 * Check that the packet doesn't begin with an
4746 * LE Control marker. (We've already generated
4747 * a test for LANE.)
4749 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
4750 BPF_H, 0xFF00);
4751 gen_not(b1);
4754 * Now check the MAC address.
4756 b0 = gen_ehostop(eaddr, Q_OR);
4757 gen_and(b1, b0);
4758 break;
4759 case DLT_IP_OVER_FC:
4760 b0 = gen_ipfchostop(eaddr, Q_OR);
4761 break;
4762 default:
4763 bpf_error(
4764 "'gateway' supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
4766 b1 = gen_host(**alist++, 0xffffffff, proto, Q_OR, Q_HOST);
4767 while (*alist) {
4768 tmp = gen_host(**alist++, 0xffffffff, proto, Q_OR,
4769 Q_HOST);
4770 gen_or(b1, tmp);
4771 b1 = tmp;
4773 gen_not(b1);
4774 gen_and(b0, b1);
4775 return b1;
4777 bpf_error("illegal modifier of 'gateway'");
4778 /* NOTREACHED */
4780 #endif
4782 struct block *
4783 gen_proto_abbrev(proto)
4784 int proto;
4786 struct block *b0;
4787 struct block *b1;
4789 switch (proto) {
4791 case Q_SCTP:
4792 b1 = gen_proto(IPPROTO_SCTP, Q_IP, Q_DEFAULT);
4793 #ifdef INET6
4794 b0 = gen_proto(IPPROTO_SCTP, Q_IPV6, Q_DEFAULT);
4795 gen_or(b0, b1);
4796 #endif
4797 break;
4799 case Q_TCP:
4800 b1 = gen_proto(IPPROTO_TCP, Q_IP, Q_DEFAULT);
4801 #ifdef INET6
4802 b0 = gen_proto(IPPROTO_TCP, Q_IPV6, Q_DEFAULT);
4803 gen_or(b0, b1);
4804 #endif
4805 break;
4807 case Q_UDP:
4808 b1 = gen_proto(IPPROTO_UDP, Q_IP, Q_DEFAULT);
4809 #ifdef INET6
4810 b0 = gen_proto(IPPROTO_UDP, Q_IPV6, Q_DEFAULT);
4811 gen_or(b0, b1);
4812 #endif
4813 break;
4815 case Q_ICMP:
4816 b1 = gen_proto(IPPROTO_ICMP, Q_IP, Q_DEFAULT);
4817 break;
4819 #ifndef IPPROTO_IGMP
4820 #define IPPROTO_IGMP 2
4821 #endif
4823 case Q_IGMP:
4824 b1 = gen_proto(IPPROTO_IGMP, Q_IP, Q_DEFAULT);
4825 break;
4827 #ifndef IPPROTO_IGRP
4828 #define IPPROTO_IGRP 9
4829 #endif
4830 case Q_IGRP:
4831 b1 = gen_proto(IPPROTO_IGRP, Q_IP, Q_DEFAULT);
4832 break;
4834 #ifndef IPPROTO_PIM
4835 #define IPPROTO_PIM 103
4836 #endif
4838 case Q_PIM:
4839 b1 = gen_proto(IPPROTO_PIM, Q_IP, Q_DEFAULT);
4840 #ifdef INET6
4841 b0 = gen_proto(IPPROTO_PIM, Q_IPV6, Q_DEFAULT);
4842 gen_or(b0, b1);
4843 #endif
4844 break;
4846 #ifndef IPPROTO_VRRP
4847 #define IPPROTO_VRRP 112
4848 #endif
4850 case Q_VRRP:
4851 b1 = gen_proto(IPPROTO_VRRP, Q_IP, Q_DEFAULT);
4852 break;
4854 #ifndef IPPROTO_CARP
4855 #define IPPROTO_CARP 112
4856 #endif
4858 case Q_CARP:
4859 b1 = gen_proto(IPPROTO_CARP, Q_IP, Q_DEFAULT);
4860 break;
4862 case Q_IP:
4863 b1 = gen_linktype(ETHERTYPE_IP);
4864 break;
4866 case Q_ARP:
4867 b1 = gen_linktype(ETHERTYPE_ARP);
4868 break;
4870 case Q_RARP:
4871 b1 = gen_linktype(ETHERTYPE_REVARP);
4872 break;
4874 case Q_LINK:
4875 bpf_error("link layer applied in wrong context");
4877 case Q_ATALK:
4878 b1 = gen_linktype(ETHERTYPE_ATALK);
4879 break;
4881 case Q_AARP:
4882 b1 = gen_linktype(ETHERTYPE_AARP);
4883 break;
4885 case Q_DECNET:
4886 b1 = gen_linktype(ETHERTYPE_DN);
4887 break;
4889 case Q_SCA:
4890 b1 = gen_linktype(ETHERTYPE_SCA);
4891 break;
4893 case Q_LAT:
4894 b1 = gen_linktype(ETHERTYPE_LAT);
4895 break;
4897 case Q_MOPDL:
4898 b1 = gen_linktype(ETHERTYPE_MOPDL);
4899 break;
4901 case Q_MOPRC:
4902 b1 = gen_linktype(ETHERTYPE_MOPRC);
4903 break;
4905 #ifdef INET6
4906 case Q_IPV6:
4907 b1 = gen_linktype(ETHERTYPE_IPV6);
4908 break;
4910 #ifndef IPPROTO_ICMPV6
4911 #define IPPROTO_ICMPV6 58
4912 #endif
4913 case Q_ICMPV6:
4914 b1 = gen_proto(IPPROTO_ICMPV6, Q_IPV6, Q_DEFAULT);
4915 break;
4916 #endif /* INET6 */
4918 #ifndef IPPROTO_AH
4919 #define IPPROTO_AH 51
4920 #endif
4921 case Q_AH:
4922 b1 = gen_proto(IPPROTO_AH, Q_IP, Q_DEFAULT);
4923 #ifdef INET6
4924 b0 = gen_proto(IPPROTO_AH, Q_IPV6, Q_DEFAULT);
4925 gen_or(b0, b1);
4926 #endif
4927 break;
4929 #ifndef IPPROTO_ESP
4930 #define IPPROTO_ESP 50
4931 #endif
4932 case Q_ESP:
4933 b1 = gen_proto(IPPROTO_ESP, Q_IP, Q_DEFAULT);
4934 #ifdef INET6
4935 b0 = gen_proto(IPPROTO_ESP, Q_IPV6, Q_DEFAULT);
4936 gen_or(b0, b1);
4937 #endif
4938 break;
4940 case Q_ISO:
4941 b1 = gen_linktype(LLCSAP_ISONS);
4942 break;
4944 case Q_ESIS:
4945 b1 = gen_proto(ISO9542_ESIS, Q_ISO, Q_DEFAULT);
4946 break;
4948 case Q_ISIS:
4949 b1 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
4950 break;
4952 case Q_ISIS_L1: /* all IS-IS Level1 PDU-Types */
4953 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4954 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4955 gen_or(b0, b1);
4956 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4957 gen_or(b0, b1);
4958 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4959 gen_or(b0, b1);
4960 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4961 gen_or(b0, b1);
4962 break;
4964 case Q_ISIS_L2: /* all IS-IS Level2 PDU-Types */
4965 b0 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4966 b1 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT); /* FIXME extract the circuit-type bits */
4967 gen_or(b0, b1);
4968 b0 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4969 gen_or(b0, b1);
4970 b0 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4971 gen_or(b0, b1);
4972 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4973 gen_or(b0, b1);
4974 break;
4976 case Q_ISIS_IIH: /* all IS-IS Hello PDU-Types */
4977 b0 = gen_proto(ISIS_L1_LAN_IIH, Q_ISIS, Q_DEFAULT);
4978 b1 = gen_proto(ISIS_L2_LAN_IIH, Q_ISIS, Q_DEFAULT);
4979 gen_or(b0, b1);
4980 b0 = gen_proto(ISIS_PTP_IIH, Q_ISIS, Q_DEFAULT);
4981 gen_or(b0, b1);
4982 break;
4984 case Q_ISIS_LSP:
4985 b0 = gen_proto(ISIS_L1_LSP, Q_ISIS, Q_DEFAULT);
4986 b1 = gen_proto(ISIS_L2_LSP, Q_ISIS, Q_DEFAULT);
4987 gen_or(b0, b1);
4988 break;
4990 case Q_ISIS_SNP:
4991 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
4992 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
4993 gen_or(b0, b1);
4994 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
4995 gen_or(b0, b1);
4996 b0 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
4997 gen_or(b0, b1);
4998 break;
5000 case Q_ISIS_CSNP:
5001 b0 = gen_proto(ISIS_L1_CSNP, Q_ISIS, Q_DEFAULT);
5002 b1 = gen_proto(ISIS_L2_CSNP, Q_ISIS, Q_DEFAULT);
5003 gen_or(b0, b1);
5004 break;
5006 case Q_ISIS_PSNP:
5007 b0 = gen_proto(ISIS_L1_PSNP, Q_ISIS, Q_DEFAULT);
5008 b1 = gen_proto(ISIS_L2_PSNP, Q_ISIS, Q_DEFAULT);
5009 gen_or(b0, b1);
5010 break;
5012 case Q_CLNP:
5013 b1 = gen_proto(ISO8473_CLNP, Q_ISO, Q_DEFAULT);
5014 break;
5016 case Q_STP:
5017 b1 = gen_linktype(LLCSAP_8021D);
5018 break;
5020 case Q_IPX:
5021 b1 = gen_linktype(LLCSAP_IPX);
5022 break;
5024 case Q_NETBEUI:
5025 b1 = gen_linktype(LLCSAP_NETBEUI);
5026 break;
5028 case Q_RADIO:
5029 bpf_error("'radio' is not a valid protocol type");
5031 default:
5032 abort();
5034 return b1;
5037 static struct block *
5038 gen_ipfrag()
5040 struct slist *s;
5041 struct block *b;
5043 /* not IPv4 frag other than the first frag */
5044 s = gen_load_a(OR_NET, 6, BPF_H);
5045 b = new_block(JMP(BPF_JSET));
5046 b->s.k = 0x1fff;
5047 b->stmts = s;
5048 gen_not(b);
5050 return b;
5054 * Generate a comparison to a port value in the transport-layer header
5055 * at the specified offset from the beginning of that header.
5057 * XXX - this handles a variable-length prefix preceding the link-layer
5058 * header, such as the radiotap or AVS radio prefix, but doesn't handle
5059 * variable-length link-layer headers (such as Token Ring or 802.11
5060 * headers).
5062 static struct block *
5063 gen_portatom(off, v)
5064 int off;
5065 bpf_int32 v;
5067 return gen_cmp(OR_TRAN_IPV4, off, BPF_H, v);
5070 #ifdef INET6
5071 static struct block *
5072 gen_portatom6(off, v)
5073 int off;
5074 bpf_int32 v;
5076 return gen_cmp(OR_TRAN_IPV6, off, BPF_H, v);
5078 #endif/*INET6*/
5080 struct block *
5081 gen_portop(port, proto, dir)
5082 int port, proto, dir;
5084 struct block *b0, *b1, *tmp;
5086 /* ip proto 'proto' and not a fragment other than the first fragment */
5087 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5088 b0 = gen_ipfrag();
5089 gen_and(tmp, b0);
5091 switch (dir) {
5092 case Q_SRC:
5093 b1 = gen_portatom(0, (bpf_int32)port);
5094 break;
5096 case Q_DST:
5097 b1 = gen_portatom(2, (bpf_int32)port);
5098 break;
5100 case Q_OR:
5101 case Q_DEFAULT:
5102 tmp = gen_portatom(0, (bpf_int32)port);
5103 b1 = gen_portatom(2, (bpf_int32)port);
5104 gen_or(tmp, b1);
5105 break;
5107 case Q_AND:
5108 tmp = gen_portatom(0, (bpf_int32)port);
5109 b1 = gen_portatom(2, (bpf_int32)port);
5110 gen_and(tmp, b1);
5111 break;
5113 default:
5114 abort();
5116 gen_and(b0, b1);
5118 return b1;
5121 static struct block *
5122 gen_port(port, ip_proto, dir)
5123 int port;
5124 int ip_proto;
5125 int dir;
5127 struct block *b0, *b1, *tmp;
5130 * ether proto ip
5132 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5133 * not LLC encapsulation with LLCSAP_IP.
5135 * For IEEE 802 networks - which includes 802.5 token ring
5136 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5137 * says that SNAP encapsulation is used, not LLC encapsulation
5138 * with LLCSAP_IP.
5140 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5141 * RFC 2225 say that SNAP encapsulation is used, not LLC
5142 * encapsulation with LLCSAP_IP.
5144 * So we always check for ETHERTYPE_IP.
5146 b0 = gen_linktype(ETHERTYPE_IP);
5148 switch (ip_proto) {
5149 case IPPROTO_UDP:
5150 case IPPROTO_TCP:
5151 case IPPROTO_SCTP:
5152 b1 = gen_portop(port, ip_proto, dir);
5153 break;
5155 case PROTO_UNDEF:
5156 tmp = gen_portop(port, IPPROTO_TCP, dir);
5157 b1 = gen_portop(port, IPPROTO_UDP, dir);
5158 gen_or(tmp, b1);
5159 tmp = gen_portop(port, IPPROTO_SCTP, dir);
5160 gen_or(tmp, b1);
5161 break;
5163 default:
5164 abort();
5166 gen_and(b0, b1);
5167 return b1;
5170 #ifdef INET6
5171 struct block *
5172 gen_portop6(port, proto, dir)
5173 int port, proto, dir;
5175 struct block *b0, *b1, *tmp;
5177 /* ip6 proto 'proto' */
5178 /* XXX - catch the first fragment of a fragmented packet? */
5179 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5181 switch (dir) {
5182 case Q_SRC:
5183 b1 = gen_portatom6(0, (bpf_int32)port);
5184 break;
5186 case Q_DST:
5187 b1 = gen_portatom6(2, (bpf_int32)port);
5188 break;
5190 case Q_OR:
5191 case Q_DEFAULT:
5192 tmp = gen_portatom6(0, (bpf_int32)port);
5193 b1 = gen_portatom6(2, (bpf_int32)port);
5194 gen_or(tmp, b1);
5195 break;
5197 case Q_AND:
5198 tmp = gen_portatom6(0, (bpf_int32)port);
5199 b1 = gen_portatom6(2, (bpf_int32)port);
5200 gen_and(tmp, b1);
5201 break;
5203 default:
5204 abort();
5206 gen_and(b0, b1);
5208 return b1;
5211 static struct block *
5212 gen_port6(port, ip_proto, dir)
5213 int port;
5214 int ip_proto;
5215 int dir;
5217 struct block *b0, *b1, *tmp;
5219 /* link proto ip6 */
5220 b0 = gen_linktype(ETHERTYPE_IPV6);
5222 switch (ip_proto) {
5223 case IPPROTO_UDP:
5224 case IPPROTO_TCP:
5225 case IPPROTO_SCTP:
5226 b1 = gen_portop6(port, ip_proto, dir);
5227 break;
5229 case PROTO_UNDEF:
5230 tmp = gen_portop6(port, IPPROTO_TCP, dir);
5231 b1 = gen_portop6(port, IPPROTO_UDP, dir);
5232 gen_or(tmp, b1);
5233 tmp = gen_portop6(port, IPPROTO_SCTP, dir);
5234 gen_or(tmp, b1);
5235 break;
5237 default:
5238 abort();
5240 gen_and(b0, b1);
5241 return b1;
5243 #endif /* INET6 */
5245 /* gen_portrange code */
5246 static struct block *
5247 gen_portrangeatom(off, v1, v2)
5248 int off;
5249 bpf_int32 v1, v2;
5251 struct block *b1, *b2;
5253 if (v1 > v2) {
5255 * Reverse the order of the ports, so v1 is the lower one.
5257 bpf_int32 vtemp;
5259 vtemp = v1;
5260 v1 = v2;
5261 v2 = vtemp;
5264 b1 = gen_cmp_ge(OR_TRAN_IPV4, off, BPF_H, v1);
5265 b2 = gen_cmp_le(OR_TRAN_IPV4, off, BPF_H, v2);
5267 gen_and(b1, b2);
5269 return b2;
5272 struct block *
5273 gen_portrangeop(port1, port2, proto, dir)
5274 int port1, port2;
5275 int proto;
5276 int dir;
5278 struct block *b0, *b1, *tmp;
5280 /* ip proto 'proto' and not a fragment other than the first fragment */
5281 tmp = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)proto);
5282 b0 = gen_ipfrag();
5283 gen_and(tmp, b0);
5285 switch (dir) {
5286 case Q_SRC:
5287 b1 = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5288 break;
5290 case Q_DST:
5291 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5292 break;
5294 case Q_OR:
5295 case Q_DEFAULT:
5296 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5297 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5298 gen_or(tmp, b1);
5299 break;
5301 case Q_AND:
5302 tmp = gen_portrangeatom(0, (bpf_int32)port1, (bpf_int32)port2);
5303 b1 = gen_portrangeatom(2, (bpf_int32)port1, (bpf_int32)port2);
5304 gen_and(tmp, b1);
5305 break;
5307 default:
5308 abort();
5310 gen_and(b0, b1);
5312 return b1;
5315 static struct block *
5316 gen_portrange(port1, port2, ip_proto, dir)
5317 int port1, port2;
5318 int ip_proto;
5319 int dir;
5321 struct block *b0, *b1, *tmp;
5323 /* link proto ip */
5324 b0 = gen_linktype(ETHERTYPE_IP);
5326 switch (ip_proto) {
5327 case IPPROTO_UDP:
5328 case IPPROTO_TCP:
5329 case IPPROTO_SCTP:
5330 b1 = gen_portrangeop(port1, port2, ip_proto, dir);
5331 break;
5333 case PROTO_UNDEF:
5334 tmp = gen_portrangeop(port1, port2, IPPROTO_TCP, dir);
5335 b1 = gen_portrangeop(port1, port2, IPPROTO_UDP, dir);
5336 gen_or(tmp, b1);
5337 tmp = gen_portrangeop(port1, port2, IPPROTO_SCTP, dir);
5338 gen_or(tmp, b1);
5339 break;
5341 default:
5342 abort();
5344 gen_and(b0, b1);
5345 return b1;
5348 #ifdef INET6
5349 static struct block *
5350 gen_portrangeatom6(off, v1, v2)
5351 int off;
5352 bpf_int32 v1, v2;
5354 struct block *b1, *b2;
5356 if (v1 > v2) {
5358 * Reverse the order of the ports, so v1 is the lower one.
5360 bpf_int32 vtemp;
5362 vtemp = v1;
5363 v1 = v2;
5364 v2 = vtemp;
5367 b1 = gen_cmp_ge(OR_TRAN_IPV6, off, BPF_H, v1);
5368 b2 = gen_cmp_le(OR_TRAN_IPV6, off, BPF_H, v2);
5370 gen_and(b1, b2);
5372 return b2;
5375 struct block *
5376 gen_portrangeop6(port1, port2, proto, dir)
5377 int port1, port2;
5378 int proto;
5379 int dir;
5381 struct block *b0, *b1, *tmp;
5383 /* ip6 proto 'proto' */
5384 /* XXX - catch the first fragment of a fragmented packet? */
5385 b0 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)proto);
5387 switch (dir) {
5388 case Q_SRC:
5389 b1 = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5390 break;
5392 case Q_DST:
5393 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5394 break;
5396 case Q_OR:
5397 case Q_DEFAULT:
5398 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5399 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5400 gen_or(tmp, b1);
5401 break;
5403 case Q_AND:
5404 tmp = gen_portrangeatom6(0, (bpf_int32)port1, (bpf_int32)port2);
5405 b1 = gen_portrangeatom6(2, (bpf_int32)port1, (bpf_int32)port2);
5406 gen_and(tmp, b1);
5407 break;
5409 default:
5410 abort();
5412 gen_and(b0, b1);
5414 return b1;
5417 static struct block *
5418 gen_portrange6(port1, port2, ip_proto, dir)
5419 int port1, port2;
5420 int ip_proto;
5421 int dir;
5423 struct block *b0, *b1, *tmp;
5425 /* link proto ip6 */
5426 b0 = gen_linktype(ETHERTYPE_IPV6);
5428 switch (ip_proto) {
5429 case IPPROTO_UDP:
5430 case IPPROTO_TCP:
5431 case IPPROTO_SCTP:
5432 b1 = gen_portrangeop6(port1, port2, ip_proto, dir);
5433 break;
5435 case PROTO_UNDEF:
5436 tmp = gen_portrangeop6(port1, port2, IPPROTO_TCP, dir);
5437 b1 = gen_portrangeop6(port1, port2, IPPROTO_UDP, dir);
5438 gen_or(tmp, b1);
5439 tmp = gen_portrangeop6(port1, port2, IPPROTO_SCTP, dir);
5440 gen_or(tmp, b1);
5441 break;
5443 default:
5444 abort();
5446 gen_and(b0, b1);
5447 return b1;
5449 #endif /* INET6 */
5451 static int
5452 lookup_proto(name, proto)
5453 register const char *name;
5454 register int proto;
5456 register int v;
5458 switch (proto) {
5460 case Q_DEFAULT:
5461 case Q_IP:
5462 case Q_IPV6:
5463 v = pcap_nametoproto(name);
5464 if (v == PROTO_UNDEF)
5465 bpf_error("unknown ip proto '%s'", name);
5466 break;
5468 case Q_LINK:
5469 /* XXX should look up h/w protocol type based on linktype */
5470 v = pcap_nametoeproto(name);
5471 if (v == PROTO_UNDEF) {
5472 v = pcap_nametollc(name);
5473 if (v == PROTO_UNDEF)
5474 bpf_error("unknown ether proto '%s'", name);
5476 break;
5478 case Q_ISO:
5479 if (strcmp(name, "esis") == 0)
5480 v = ISO9542_ESIS;
5481 else if (strcmp(name, "isis") == 0)
5482 v = ISO10589_ISIS;
5483 else if (strcmp(name, "clnp") == 0)
5484 v = ISO8473_CLNP;
5485 else
5486 bpf_error("unknown osi proto '%s'", name);
5487 break;
5489 default:
5490 v = PROTO_UNDEF;
5491 break;
5493 return v;
5496 #if 0
5497 struct stmt *
5498 gen_joinsp(s, n)
5499 struct stmt **s;
5500 int n;
5502 return NULL;
5504 #endif
5506 static struct block *
5507 gen_protochain(v, proto, dir)
5508 int v;
5509 int proto;
5510 int dir;
5512 #ifdef NO_PROTOCHAIN
5513 return gen_proto(v, proto, dir);
5514 #else
5515 struct block *b0, *b;
5516 struct slist *s[100];
5517 int fix2, fix3, fix4, fix5;
5518 int ahcheck, again, end;
5519 int i, max;
5520 int reg2 = alloc_reg();
5522 memset(s, 0, sizeof(s));
5523 fix2 = fix3 = fix4 = fix5 = 0;
5525 switch (proto) {
5526 case Q_IP:
5527 case Q_IPV6:
5528 break;
5529 case Q_DEFAULT:
5530 b0 = gen_protochain(v, Q_IP, dir);
5531 b = gen_protochain(v, Q_IPV6, dir);
5532 gen_or(b0, b);
5533 return b;
5534 default:
5535 bpf_error("bad protocol applied for 'protochain'");
5536 /*NOTREACHED*/
5540 * We don't handle variable-length prefixes before the link-layer
5541 * header, or variable-length link-layer headers, here yet.
5542 * We might want to add BPF instructions to do the protochain
5543 * work, to simplify that and, on platforms that have a BPF
5544 * interpreter with the new instructions, let the filtering
5545 * be done in the kernel. (We already require a modified BPF
5546 * engine to do the protochain stuff, to support backward
5547 * branches, and backward branch support is unlikely to appear
5548 * in kernel BPF engines.)
5550 switch (linktype) {
5552 case DLT_IEEE802_11:
5553 case DLT_PRISM_HEADER:
5554 case DLT_IEEE802_11_RADIO_AVS:
5555 case DLT_IEEE802_11_RADIO:
5556 case DLT_PPI:
5557 bpf_error("'protochain' not supported with 802.11");
5560 no_optimize = 1; /*this code is not compatible with optimzer yet */
5563 * s[0] is a dummy entry to protect other BPF insn from damage
5564 * by s[fix] = foo with uninitialized variable "fix". It is somewhat
5565 * hard to find interdependency made by jump table fixup.
5567 i = 0;
5568 s[i] = new_stmt(0); /*dummy*/
5569 i++;
5571 switch (proto) {
5572 case Q_IP:
5573 b0 = gen_linktype(ETHERTYPE_IP);
5575 /* A = ip->ip_p */
5576 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5577 s[i]->s.k = off_macpl + off_nl + 9;
5578 i++;
5579 /* X = ip->ip_hl << 2 */
5580 s[i] = new_stmt(BPF_LDX|BPF_MSH|BPF_B);
5581 s[i]->s.k = off_macpl + off_nl;
5582 i++;
5583 break;
5584 #ifdef INET6
5585 case Q_IPV6:
5586 b0 = gen_linktype(ETHERTYPE_IPV6);
5588 /* A = ip6->ip_nxt */
5589 s[i] = new_stmt(BPF_LD|BPF_ABS|BPF_B);
5590 s[i]->s.k = off_macpl + off_nl + 6;
5591 i++;
5592 /* X = sizeof(struct ip6_hdr) */
5593 s[i] = new_stmt(BPF_LDX|BPF_IMM);
5594 s[i]->s.k = 40;
5595 i++;
5596 break;
5597 #endif
5598 default:
5599 bpf_error("unsupported proto to gen_protochain");
5600 /*NOTREACHED*/
5603 /* again: if (A == v) goto end; else fall through; */
5604 again = i;
5605 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5606 s[i]->s.k = v;
5607 s[i]->s.jt = NULL; /*later*/
5608 s[i]->s.jf = NULL; /*update in next stmt*/
5609 fix5 = i;
5610 i++;
5612 #ifndef IPPROTO_NONE
5613 #define IPPROTO_NONE 59
5614 #endif
5615 /* if (A == IPPROTO_NONE) goto end */
5616 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5617 s[i]->s.jt = NULL; /*later*/
5618 s[i]->s.jf = NULL; /*update in next stmt*/
5619 s[i]->s.k = IPPROTO_NONE;
5620 s[fix5]->s.jf = s[i];
5621 fix2 = i;
5622 i++;
5624 #ifdef INET6
5625 if (proto == Q_IPV6) {
5626 int v6start, v6end, v6advance, j;
5628 v6start = i;
5629 /* if (A == IPPROTO_HOPOPTS) goto v6advance */
5630 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5631 s[i]->s.jt = NULL; /*later*/
5632 s[i]->s.jf = NULL; /*update in next stmt*/
5633 s[i]->s.k = IPPROTO_HOPOPTS;
5634 s[fix2]->s.jf = s[i];
5635 i++;
5636 /* if (A == IPPROTO_DSTOPTS) goto v6advance */
5637 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5638 s[i]->s.jt = NULL; /*later*/
5639 s[i]->s.jf = NULL; /*update in next stmt*/
5640 s[i]->s.k = IPPROTO_DSTOPTS;
5641 i++;
5642 /* if (A == IPPROTO_ROUTING) goto v6advance */
5643 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5644 s[i]->s.jt = NULL; /*later*/
5645 s[i]->s.jf = NULL; /*update in next stmt*/
5646 s[i]->s.k = IPPROTO_ROUTING;
5647 i++;
5648 /* if (A == IPPROTO_FRAGMENT) goto v6advance; else goto ahcheck; */
5649 s[i - 1]->s.jf = s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5650 s[i]->s.jt = NULL; /*later*/
5651 s[i]->s.jf = NULL; /*later*/
5652 s[i]->s.k = IPPROTO_FRAGMENT;
5653 fix3 = i;
5654 v6end = i;
5655 i++;
5657 /* v6advance: */
5658 v6advance = i;
5661 * in short,
5662 * A = P[X + packet head];
5663 * X = X + (P[X + packet head + 1] + 1) * 8;
5665 /* A = P[X + packet head] */
5666 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5667 s[i]->s.k = off_macpl + off_nl;
5668 i++;
5669 /* MEM[reg2] = A */
5670 s[i] = new_stmt(BPF_ST);
5671 s[i]->s.k = reg2;
5672 i++;
5673 /* A = P[X + packet head + 1]; */
5674 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5675 s[i]->s.k = off_macpl + off_nl + 1;
5676 i++;
5677 /* A += 1 */
5678 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5679 s[i]->s.k = 1;
5680 i++;
5681 /* A *= 8 */
5682 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5683 s[i]->s.k = 8;
5684 i++;
5685 /* A += X */
5686 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_X);
5687 s[i]->s.k = 0;
5688 i++;
5689 /* X = A; */
5690 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5691 i++;
5692 /* A = MEM[reg2] */
5693 s[i] = new_stmt(BPF_LD|BPF_MEM);
5694 s[i]->s.k = reg2;
5695 i++;
5697 /* goto again; (must use BPF_JA for backward jump) */
5698 s[i] = new_stmt(BPF_JMP|BPF_JA);
5699 s[i]->s.k = again - i - 1;
5700 s[i - 1]->s.jf = s[i];
5701 i++;
5703 /* fixup */
5704 for (j = v6start; j <= v6end; j++)
5705 s[j]->s.jt = s[v6advance];
5706 } else
5707 #endif
5709 /* nop */
5710 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5711 s[i]->s.k = 0;
5712 s[fix2]->s.jf = s[i];
5713 i++;
5716 /* ahcheck: */
5717 ahcheck = i;
5718 /* if (A == IPPROTO_AH) then fall through; else goto end; */
5719 s[i] = new_stmt(BPF_JMP|BPF_JEQ|BPF_K);
5720 s[i]->s.jt = NULL; /*later*/
5721 s[i]->s.jf = NULL; /*later*/
5722 s[i]->s.k = IPPROTO_AH;
5723 if (fix3)
5724 s[fix3]->s.jf = s[ahcheck];
5725 fix4 = i;
5726 i++;
5729 * in short,
5730 * A = P[X];
5731 * X = X + (P[X + 1] + 2) * 4;
5733 /* A = X */
5734 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5735 i++;
5736 /* A = P[X + packet head]; */
5737 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5738 s[i]->s.k = off_macpl + off_nl;
5739 i++;
5740 /* MEM[reg2] = A */
5741 s[i] = new_stmt(BPF_ST);
5742 s[i]->s.k = reg2;
5743 i++;
5744 /* A = X */
5745 s[i - 1]->s.jt = s[i] = new_stmt(BPF_MISC|BPF_TXA);
5746 i++;
5747 /* A += 1 */
5748 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5749 s[i]->s.k = 1;
5750 i++;
5751 /* X = A */
5752 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5753 i++;
5754 /* A = P[X + packet head] */
5755 s[i] = new_stmt(BPF_LD|BPF_IND|BPF_B);
5756 s[i]->s.k = off_macpl + off_nl;
5757 i++;
5758 /* A += 2 */
5759 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5760 s[i]->s.k = 2;
5761 i++;
5762 /* A *= 4 */
5763 s[i] = new_stmt(BPF_ALU|BPF_MUL|BPF_K);
5764 s[i]->s.k = 4;
5765 i++;
5766 /* X = A; */
5767 s[i] = new_stmt(BPF_MISC|BPF_TAX);
5768 i++;
5769 /* A = MEM[reg2] */
5770 s[i] = new_stmt(BPF_LD|BPF_MEM);
5771 s[i]->s.k = reg2;
5772 i++;
5774 /* goto again; (must use BPF_JA for backward jump) */
5775 s[i] = new_stmt(BPF_JMP|BPF_JA);
5776 s[i]->s.k = again - i - 1;
5777 i++;
5779 /* end: nop */
5780 end = i;
5781 s[i] = new_stmt(BPF_ALU|BPF_ADD|BPF_K);
5782 s[i]->s.k = 0;
5783 s[fix2]->s.jt = s[end];
5784 s[fix4]->s.jf = s[end];
5785 s[fix5]->s.jt = s[end];
5786 i++;
5789 * make slist chain
5791 max = i;
5792 for (i = 0; i < max - 1; i++)
5793 s[i]->next = s[i + 1];
5794 s[max - 1]->next = NULL;
5797 * emit final check
5799 b = new_block(JMP(BPF_JEQ));
5800 b->stmts = s[1]; /*remember, s[0] is dummy*/
5801 b->s.k = v;
5803 free_reg(reg2);
5805 gen_and(b0, b);
5806 return b;
5807 #endif
5810 static struct block *
5811 gen_check_802_11_data_frame()
5813 struct slist *s;
5814 struct block *b0, *b1;
5817 * A data frame has the 0x08 bit (b3) in the frame control field set
5818 * and the 0x04 bit (b2) clear.
5820 s = gen_load_a(OR_LINK, 0, BPF_B);
5821 b0 = new_block(JMP(BPF_JSET));
5822 b0->s.k = 0x08;
5823 b0->stmts = s;
5825 s = gen_load_a(OR_LINK, 0, BPF_B);
5826 b1 = new_block(JMP(BPF_JSET));
5827 b1->s.k = 0x04;
5828 b1->stmts = s;
5829 gen_not(b1);
5831 gen_and(b1, b0);
5833 return b0;
5837 * Generate code that checks whether the packet is a packet for protocol
5838 * <proto> and whether the type field in that protocol's header has
5839 * the value <v>, e.g. if <proto> is Q_IP, it checks whether it's an
5840 * IP packet and checks the protocol number in the IP header against <v>.
5842 * If <proto> is Q_DEFAULT, i.e. just "proto" was specified, it checks
5843 * against Q_IP and Q_IPV6.
5845 static struct block *
5846 gen_proto(v, proto, dir)
5847 int v;
5848 int proto;
5849 int dir;
5851 struct block *b0, *b1;
5852 #ifdef INET6
5853 #ifndef CHASE_CHAIN
5854 struct block *b2;
5855 #endif
5856 #endif
5858 if (dir != Q_DEFAULT)
5859 bpf_error("direction applied to 'proto'");
5861 switch (proto) {
5862 case Q_DEFAULT:
5863 #ifdef INET6
5864 b0 = gen_proto(v, Q_IP, dir);
5865 b1 = gen_proto(v, Q_IPV6, dir);
5866 gen_or(b0, b1);
5867 return b1;
5868 #else
5869 /*FALLTHROUGH*/
5870 #endif
5871 case Q_IP:
5873 * For FDDI, RFC 1188 says that SNAP encapsulation is used,
5874 * not LLC encapsulation with LLCSAP_IP.
5876 * For IEEE 802 networks - which includes 802.5 token ring
5877 * (which is what DLT_IEEE802 means) and 802.11 - RFC 1042
5878 * says that SNAP encapsulation is used, not LLC encapsulation
5879 * with LLCSAP_IP.
5881 * For LLC-encapsulated ATM/"Classical IP", RFC 1483 and
5882 * RFC 2225 say that SNAP encapsulation is used, not LLC
5883 * encapsulation with LLCSAP_IP.
5885 * So we always check for ETHERTYPE_IP.
5887 b0 = gen_linktype(ETHERTYPE_IP);
5888 #ifndef CHASE_CHAIN
5889 b1 = gen_cmp(OR_NET, 9, BPF_B, (bpf_int32)v);
5890 #else
5891 b1 = gen_protochain(v, Q_IP);
5892 #endif
5893 gen_and(b0, b1);
5894 return b1;
5896 case Q_ISO:
5897 switch (linktype) {
5899 case DLT_FRELAY:
5901 * Frame Relay packets typically have an OSI
5902 * NLPID at the beginning; "gen_linktype(LLCSAP_ISONS)"
5903 * generates code to check for all the OSI
5904 * NLPIDs, so calling it and then adding a check
5905 * for the particular NLPID for which we're
5906 * looking is bogus, as we can just check for
5907 * the NLPID.
5909 * What we check for is the NLPID and a frame
5910 * control field value of UI, i.e. 0x03 followed
5911 * by the NLPID.
5913 * XXX - assumes a 2-byte Frame Relay header with
5914 * DLCI and flags. What if the address is longer?
5916 * XXX - what about SNAP-encapsulated frames?
5918 return gen_cmp(OR_LINK, 2, BPF_H, (0x03<<8) | v);
5919 /*NOTREACHED*/
5920 break;
5922 case DLT_C_HDLC:
5924 * Cisco uses an Ethertype lookalike - for OSI,
5925 * it's 0xfefe.
5927 b0 = gen_linktype(LLCSAP_ISONS<<8 | LLCSAP_ISONS);
5928 /* OSI in C-HDLC is stuffed with a fudge byte */
5929 b1 = gen_cmp(OR_NET_NOSNAP, 1, BPF_B, (long)v);
5930 gen_and(b0, b1);
5931 return b1;
5933 default:
5934 b0 = gen_linktype(LLCSAP_ISONS);
5935 b1 = gen_cmp(OR_NET_NOSNAP, 0, BPF_B, (long)v);
5936 gen_and(b0, b1);
5937 return b1;
5940 case Q_ISIS:
5941 b0 = gen_proto(ISO10589_ISIS, Q_ISO, Q_DEFAULT);
5943 * 4 is the offset of the PDU type relative to the IS-IS
5944 * header.
5946 b1 = gen_cmp(OR_NET_NOSNAP, 4, BPF_B, (long)v);
5947 gen_and(b0, b1);
5948 return b1;
5950 case Q_ARP:
5951 bpf_error("arp does not encapsulate another protocol");
5952 /* NOTREACHED */
5954 case Q_RARP:
5955 bpf_error("rarp does not encapsulate another protocol");
5956 /* NOTREACHED */
5958 case Q_ATALK:
5959 bpf_error("atalk encapsulation is not specifiable");
5960 /* NOTREACHED */
5962 case Q_DECNET:
5963 bpf_error("decnet encapsulation is not specifiable");
5964 /* NOTREACHED */
5966 case Q_SCA:
5967 bpf_error("sca does not encapsulate another protocol");
5968 /* NOTREACHED */
5970 case Q_LAT:
5971 bpf_error("lat does not encapsulate another protocol");
5972 /* NOTREACHED */
5974 case Q_MOPRC:
5975 bpf_error("moprc does not encapsulate another protocol");
5976 /* NOTREACHED */
5978 case Q_MOPDL:
5979 bpf_error("mopdl does not encapsulate another protocol");
5980 /* NOTREACHED */
5982 case Q_LINK:
5983 return gen_linktype(v);
5985 case Q_UDP:
5986 bpf_error("'udp proto' is bogus");
5987 /* NOTREACHED */
5989 case Q_TCP:
5990 bpf_error("'tcp proto' is bogus");
5991 /* NOTREACHED */
5993 case Q_SCTP:
5994 bpf_error("'sctp proto' is bogus");
5995 /* NOTREACHED */
5997 case Q_ICMP:
5998 bpf_error("'icmp proto' is bogus");
5999 /* NOTREACHED */
6001 case Q_IGMP:
6002 bpf_error("'igmp proto' is bogus");
6003 /* NOTREACHED */
6005 case Q_IGRP:
6006 bpf_error("'igrp proto' is bogus");
6007 /* NOTREACHED */
6009 case Q_PIM:
6010 bpf_error("'pim proto' is bogus");
6011 /* NOTREACHED */
6013 case Q_VRRP:
6014 bpf_error("'vrrp proto' is bogus");
6015 /* NOTREACHED */
6017 case Q_CARP:
6018 bpf_error("'carp proto' is bogus");
6019 /* NOTREACHED */
6021 #ifdef INET6
6022 case Q_IPV6:
6023 b0 = gen_linktype(ETHERTYPE_IPV6);
6024 #ifndef CHASE_CHAIN
6026 * Also check for a fragment header before the final
6027 * header.
6029 b2 = gen_cmp(OR_NET, 6, BPF_B, IPPROTO_FRAGMENT);
6030 b1 = gen_cmp(OR_NET, 40, BPF_B, (bpf_int32)v);
6031 gen_and(b2, b1);
6032 b2 = gen_cmp(OR_NET, 6, BPF_B, (bpf_int32)v);
6033 gen_or(b2, b1);
6034 #else
6035 b1 = gen_protochain(v, Q_IPV6);
6036 #endif
6037 gen_and(b0, b1);
6038 return b1;
6040 case Q_ICMPV6:
6041 bpf_error("'icmp6 proto' is bogus");
6042 #endif /* INET6 */
6044 case Q_AH:
6045 bpf_error("'ah proto' is bogus");
6047 case Q_ESP:
6048 bpf_error("'ah proto' is bogus");
6050 case Q_STP:
6051 bpf_error("'stp proto' is bogus");
6053 case Q_IPX:
6054 bpf_error("'ipx proto' is bogus");
6056 case Q_NETBEUI:
6057 bpf_error("'netbeui proto' is bogus");
6059 case Q_RADIO:
6060 bpf_error("'radio proto' is bogus");
6062 default:
6063 abort();
6064 /* NOTREACHED */
6066 /* NOTREACHED */
6069 struct block *
6070 gen_scode(name, q)
6071 register const char *name;
6072 struct qual q;
6074 int proto = q.proto;
6075 int dir = q.dir;
6076 int tproto;
6077 u_char *eaddr;
6078 bpf_u_int32 mask, addr;
6079 #ifndef INET6
6080 bpf_u_int32 **alist;
6081 #else
6082 int tproto6;
6083 struct sockaddr_in *sin4;
6084 struct sockaddr_in6 *sin6;
6085 struct addrinfo *res, *res0;
6086 struct in6_addr mask128;
6087 #endif /*INET6*/
6088 struct block *b, *tmp;
6089 int port, real_proto;
6090 int port1, port2;
6092 switch (q.addr) {
6094 case Q_NET:
6095 addr = pcap_nametonetaddr(name);
6096 if (addr == 0)
6097 bpf_error("unknown network '%s'", name);
6098 /* Left justify network addr and calculate its network mask */
6099 mask = 0xffffffff;
6100 while (addr && (addr & 0xff000000) == 0) {
6101 addr <<= 8;
6102 mask <<= 8;
6104 return gen_host(addr, mask, proto, dir, q.addr);
6106 case Q_DEFAULT:
6107 case Q_HOST:
6108 if (proto == Q_LINK) {
6109 switch (linktype) {
6111 case DLT_EN10MB:
6112 case DLT_NETANALYZER:
6113 case DLT_NETANALYZER_TRANSPARENT:
6114 eaddr = pcap_ether_hostton(name);
6115 if (eaddr == NULL)
6116 bpf_error(
6117 "unknown ether host '%s'", name);
6118 b = gen_ehostop(eaddr, dir);
6119 free(eaddr);
6120 return b;
6122 case DLT_FDDI:
6123 eaddr = pcap_ether_hostton(name);
6124 if (eaddr == NULL)
6125 bpf_error(
6126 "unknown FDDI host '%s'", name);
6127 b = gen_fhostop(eaddr, dir);
6128 free(eaddr);
6129 return b;
6131 case DLT_IEEE802:
6132 eaddr = pcap_ether_hostton(name);
6133 if (eaddr == NULL)
6134 bpf_error(
6135 "unknown token ring host '%s'", name);
6136 b = gen_thostop(eaddr, dir);
6137 free(eaddr);
6138 return b;
6140 case DLT_IEEE802_11:
6141 case DLT_PRISM_HEADER:
6142 case DLT_IEEE802_11_RADIO_AVS:
6143 case DLT_IEEE802_11_RADIO:
6144 case DLT_PPI:
6145 eaddr = pcap_ether_hostton(name);
6146 if (eaddr == NULL)
6147 bpf_error(
6148 "unknown 802.11 host '%s'", name);
6149 b = gen_wlanhostop(eaddr, dir);
6150 free(eaddr);
6151 return b;
6153 case DLT_IP_OVER_FC:
6154 eaddr = pcap_ether_hostton(name);
6155 if (eaddr == NULL)
6156 bpf_error(
6157 "unknown Fibre Channel host '%s'", name);
6158 b = gen_ipfchostop(eaddr, dir);
6159 free(eaddr);
6160 return b;
6162 case DLT_SUNATM:
6163 if (!is_lane)
6164 break;
6167 * Check that the packet doesn't begin
6168 * with an LE Control marker. (We've
6169 * already generated a test for LANE.)
6171 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
6172 BPF_H, 0xFF00);
6173 gen_not(tmp);
6175 eaddr = pcap_ether_hostton(name);
6176 if (eaddr == NULL)
6177 bpf_error(
6178 "unknown ether host '%s'", name);
6179 b = gen_ehostop(eaddr, dir);
6180 gen_and(tmp, b);
6181 free(eaddr);
6182 return b;
6185 bpf_error("only ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel supports link-level host name");
6186 } else if (proto == Q_DECNET) {
6187 unsigned short dn_addr = __pcap_nametodnaddr(name);
6189 * I don't think DECNET hosts can be multihomed, so
6190 * there is no need to build up a list of addresses
6192 return (gen_host(dn_addr, 0, proto, dir, q.addr));
6193 } else {
6194 #ifndef INET6
6195 alist = pcap_nametoaddr(name);
6196 if (alist == NULL || *alist == NULL)
6197 bpf_error("unknown host '%s'", name);
6198 tproto = proto;
6199 if (off_linktype == (u_int)-1 && tproto == Q_DEFAULT)
6200 tproto = Q_IP;
6201 b = gen_host(**alist++, 0xffffffff, tproto, dir, q.addr);
6202 while (*alist) {
6203 tmp = gen_host(**alist++, 0xffffffff,
6204 tproto, dir, q.addr);
6205 gen_or(b, tmp);
6206 b = tmp;
6208 return b;
6209 #else
6210 memset(&mask128, 0xff, sizeof(mask128));
6211 res0 = res = pcap_nametoaddrinfo(name);
6212 if (res == NULL)
6213 bpf_error("unknown host '%s'", name);
6214 ai = res;
6215 b = tmp = NULL;
6216 tproto = tproto6 = proto;
6217 if (off_linktype == -1 && tproto == Q_DEFAULT) {
6218 tproto = Q_IP;
6219 tproto6 = Q_IPV6;
6221 for (res = res0; res; res = res->ai_next) {
6222 switch (res->ai_family) {
6223 case AF_INET:
6224 if (tproto == Q_IPV6)
6225 continue;
6227 sin4 = (struct sockaddr_in *)
6228 res->ai_addr;
6229 tmp = gen_host(ntohl(sin4->sin_addr.s_addr),
6230 0xffffffff, tproto, dir, q.addr);
6231 break;
6232 case AF_INET6:
6233 if (tproto6 == Q_IP)
6234 continue;
6236 sin6 = (struct sockaddr_in6 *)
6237 res->ai_addr;
6238 tmp = gen_host6(&sin6->sin6_addr,
6239 &mask128, tproto6, dir, q.addr);
6240 break;
6241 default:
6242 continue;
6244 if (b)
6245 gen_or(b, tmp);
6246 b = tmp;
6248 ai = NULL;
6249 freeaddrinfo(res0);
6250 if (b == NULL) {
6251 bpf_error("unknown host '%s'%s", name,
6252 (proto == Q_DEFAULT)
6253 ? ""
6254 : " for specified address family");
6256 return b;
6257 #endif /*INET6*/
6260 case Q_PORT:
6261 if (proto != Q_DEFAULT &&
6262 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6263 bpf_error("illegal qualifier of 'port'");
6264 if (pcap_nametoport(name, &port, &real_proto) == 0)
6265 bpf_error("unknown port '%s'", name);
6266 if (proto == Q_UDP) {
6267 if (real_proto == IPPROTO_TCP)
6268 bpf_error("port '%s' is tcp", name);
6269 else if (real_proto == IPPROTO_SCTP)
6270 bpf_error("port '%s' is sctp", name);
6271 else
6272 /* override PROTO_UNDEF */
6273 real_proto = IPPROTO_UDP;
6275 if (proto == Q_TCP) {
6276 if (real_proto == IPPROTO_UDP)
6277 bpf_error("port '%s' is udp", name);
6279 else if (real_proto == IPPROTO_SCTP)
6280 bpf_error("port '%s' is sctp", name);
6281 else
6282 /* override PROTO_UNDEF */
6283 real_proto = IPPROTO_TCP;
6285 if (proto == Q_SCTP) {
6286 if (real_proto == IPPROTO_UDP)
6287 bpf_error("port '%s' is udp", name);
6289 else if (real_proto == IPPROTO_TCP)
6290 bpf_error("port '%s' is tcp", name);
6291 else
6292 /* override PROTO_UNDEF */
6293 real_proto = IPPROTO_SCTP;
6295 if (port < 0)
6296 bpf_error("illegal port number %d < 0", port);
6297 if (port > 65535)
6298 bpf_error("illegal port number %d > 65535", port);
6299 #ifndef INET6
6300 return gen_port(port, real_proto, dir);
6301 #else
6302 b = gen_port(port, real_proto, dir);
6303 gen_or(gen_port6(port, real_proto, dir), b);
6304 return b;
6305 #endif /* INET6 */
6307 case Q_PORTRANGE:
6308 if (proto != Q_DEFAULT &&
6309 proto != Q_UDP && proto != Q_TCP && proto != Q_SCTP)
6310 bpf_error("illegal qualifier of 'portrange'");
6311 if (pcap_nametoportrange(name, &port1, &port2, &real_proto) == 0)
6312 bpf_error("unknown port in range '%s'", name);
6313 if (proto == Q_UDP) {
6314 if (real_proto == IPPROTO_TCP)
6315 bpf_error("port in range '%s' is tcp", name);
6316 else if (real_proto == IPPROTO_SCTP)
6317 bpf_error("port in range '%s' is sctp", name);
6318 else
6319 /* override PROTO_UNDEF */
6320 real_proto = IPPROTO_UDP;
6322 if (proto == Q_TCP) {
6323 if (real_proto == IPPROTO_UDP)
6324 bpf_error("port in range '%s' is udp", name);
6325 else if (real_proto == IPPROTO_SCTP)
6326 bpf_error("port in range '%s' is sctp", name);
6327 else
6328 /* override PROTO_UNDEF */
6329 real_proto = IPPROTO_TCP;
6331 if (proto == Q_SCTP) {
6332 if (real_proto == IPPROTO_UDP)
6333 bpf_error("port in range '%s' is udp", name);
6334 else if (real_proto == IPPROTO_TCP)
6335 bpf_error("port in range '%s' is tcp", name);
6336 else
6337 /* override PROTO_UNDEF */
6338 real_proto = IPPROTO_SCTP;
6340 if (port1 < 0)
6341 bpf_error("illegal port number %d < 0", port1);
6342 if (port1 > 65535)
6343 bpf_error("illegal port number %d > 65535", port1);
6344 if (port2 < 0)
6345 bpf_error("illegal port number %d < 0", port2);
6346 if (port2 > 65535)
6347 bpf_error("illegal port number %d > 65535", port2);
6349 #ifndef INET6
6350 return gen_portrange(port1, port2, real_proto, dir);
6351 #else
6352 b = gen_portrange(port1, port2, real_proto, dir);
6353 gen_or(gen_portrange6(port1, port2, real_proto, dir), b);
6354 return b;
6355 #endif /* INET6 */
6357 case Q_GATEWAY:
6358 #ifndef INET6
6359 eaddr = pcap_ether_hostton(name);
6360 if (eaddr == NULL)
6361 bpf_error("unknown ether host: %s", name);
6363 alist = pcap_nametoaddr(name);
6364 if (alist == NULL || *alist == NULL)
6365 bpf_error("unknown host '%s'", name);
6366 b = gen_gateway(eaddr, alist, proto, dir);
6367 free(eaddr);
6368 return b;
6369 #else
6370 bpf_error("'gateway' not supported in this configuration");
6371 #endif /*INET6*/
6373 case Q_PROTO:
6374 real_proto = lookup_proto(name, proto);
6375 if (real_proto >= 0)
6376 return gen_proto(real_proto, proto, dir);
6377 else
6378 bpf_error("unknown protocol: %s", name);
6380 case Q_PROTOCHAIN:
6381 real_proto = lookup_proto(name, proto);
6382 if (real_proto >= 0)
6383 return gen_protochain(real_proto, proto, dir);
6384 else
6385 bpf_error("unknown protocol: %s", name);
6387 case Q_UNDEF:
6388 syntax();
6389 /* NOTREACHED */
6391 abort();
6392 /* NOTREACHED */
6395 struct block *
6396 gen_mcode(s1, s2, masklen, q)
6397 register const char *s1, *s2;
6398 register int masklen;
6399 struct qual q;
6401 register int nlen, mlen;
6402 bpf_u_int32 n, m;
6404 nlen = __pcap_atoin(s1, &n);
6405 /* Promote short ipaddr */
6406 n <<= 32 - nlen;
6408 if (s2 != NULL) {
6409 mlen = __pcap_atoin(s2, &m);
6410 /* Promote short ipaddr */
6411 m <<= 32 - mlen;
6412 if ((n & ~m) != 0)
6413 bpf_error("non-network bits set in \"%s mask %s\"",
6414 s1, s2);
6415 } else {
6416 /* Convert mask len to mask */
6417 if (masklen > 32)
6418 bpf_error("mask length must be <= 32");
6419 if (masklen == 0) {
6421 * X << 32 is not guaranteed by C to be 0; it's
6422 * undefined.
6424 m = 0;
6425 } else
6426 m = 0xffffffff << (32 - masklen);
6427 if ((n & ~m) != 0)
6428 bpf_error("non-network bits set in \"%s/%d\"",
6429 s1, masklen);
6432 switch (q.addr) {
6434 case Q_NET:
6435 return gen_host(n, m, q.proto, q.dir, q.addr);
6437 default:
6438 bpf_error("Mask syntax for networks only");
6439 /* NOTREACHED */
6441 /* NOTREACHED */
6442 return NULL;
6445 struct block *
6446 gen_ncode(s, v, q)
6447 register const char *s;
6448 bpf_u_int32 v;
6449 struct qual q;
6451 bpf_u_int32 mask;
6452 int proto = q.proto;
6453 int dir = q.dir;
6454 register int vlen;
6456 if (s == NULL)
6457 vlen = 32;
6458 else if (q.proto == Q_DECNET)
6459 vlen = __pcap_atodn(s, &v);
6460 else
6461 vlen = __pcap_atoin(s, &v);
6463 switch (q.addr) {
6465 case Q_DEFAULT:
6466 case Q_HOST:
6467 case Q_NET:
6468 if (proto == Q_DECNET)
6469 return gen_host(v, 0, proto, dir, q.addr);
6470 else if (proto == Q_LINK) {
6471 bpf_error("illegal link layer address");
6472 } else {
6473 mask = 0xffffffff;
6474 if (s == NULL && q.addr == Q_NET) {
6475 /* Promote short net number */
6476 while (v && (v & 0xff000000) == 0) {
6477 v <<= 8;
6478 mask <<= 8;
6480 } else {
6481 /* Promote short ipaddr */
6482 v <<= 32 - vlen;
6483 mask <<= 32 - vlen;
6485 return gen_host(v, mask, proto, dir, q.addr);
6488 case Q_PORT:
6489 if (proto == Q_UDP)
6490 proto = IPPROTO_UDP;
6491 else if (proto == Q_TCP)
6492 proto = IPPROTO_TCP;
6493 else if (proto == Q_SCTP)
6494 proto = IPPROTO_SCTP;
6495 else if (proto == Q_DEFAULT)
6496 proto = PROTO_UNDEF;
6497 else
6498 bpf_error("illegal qualifier of 'port'");
6500 if (v > 65535)
6501 bpf_error("illegal port number %u > 65535", v);
6503 #ifndef INET6
6504 return gen_port((int)v, proto, dir);
6505 #else
6507 struct block *b;
6508 b = gen_port((int)v, proto, dir);
6509 gen_or(gen_port6((int)v, proto, dir), b);
6510 return b;
6512 #endif /* INET6 */
6514 case Q_PORTRANGE:
6515 if (proto == Q_UDP)
6516 proto = IPPROTO_UDP;
6517 else if (proto == Q_TCP)
6518 proto = IPPROTO_TCP;
6519 else if (proto == Q_SCTP)
6520 proto = IPPROTO_SCTP;
6521 else if (proto == Q_DEFAULT)
6522 proto = PROTO_UNDEF;
6523 else
6524 bpf_error("illegal qualifier of 'portrange'");
6526 if (v > 65535)
6527 bpf_error("illegal port number %u > 65535", v);
6529 #ifndef INET6
6530 return gen_portrange((int)v, (int)v, proto, dir);
6531 #else
6533 struct block *b;
6534 b = gen_portrange((int)v, (int)v, proto, dir);
6535 gen_or(gen_portrange6((int)v, (int)v, proto, dir), b);
6536 return b;
6538 #endif /* INET6 */
6540 case Q_GATEWAY:
6541 bpf_error("'gateway' requires a name");
6542 /* NOTREACHED */
6544 case Q_PROTO:
6545 return gen_proto((int)v, proto, dir);
6547 case Q_PROTOCHAIN:
6548 return gen_protochain((int)v, proto, dir);
6550 case Q_UNDEF:
6551 syntax();
6552 /* NOTREACHED */
6554 default:
6555 abort();
6556 /* NOTREACHED */
6558 /* NOTREACHED */
6561 #ifdef INET6
6562 struct block *
6563 gen_mcode6(s1, s2, masklen, q)
6564 register const char *s1, *s2;
6565 register int masklen;
6566 struct qual q;
6568 struct addrinfo *res;
6569 struct in6_addr *addr;
6570 struct in6_addr mask;
6571 struct block *b;
6572 u_int32_t *a, *m;
6574 if (s2)
6575 bpf_error("no mask %s supported", s2);
6577 res = pcap_nametoaddrinfo(s1);
6578 if (!res)
6579 bpf_error("invalid ip6 address %s", s1);
6580 ai = res;
6581 if (res->ai_next)
6582 bpf_error("%s resolved to multiple address", s1);
6583 addr = &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
6585 if (sizeof(mask) * 8 < masklen)
6586 bpf_error("mask length must be <= %u", (unsigned int)(sizeof(mask) * 8));
6587 memset(&mask, 0, sizeof(mask));
6588 memset(&mask, 0xff, masklen / 8);
6589 if (masklen % 8) {
6590 mask.s6_addr[masklen / 8] =
6591 (0xff << (8 - masklen % 8)) & 0xff;
6594 a = (u_int32_t *)addr;
6595 m = (u_int32_t *)&mask;
6596 if ((a[0] & ~m[0]) || (a[1] & ~m[1])
6597 || (a[2] & ~m[2]) || (a[3] & ~m[3])) {
6598 bpf_error("non-network bits set in \"%s/%d\"", s1, masklen);
6601 switch (q.addr) {
6603 case Q_DEFAULT:
6604 case Q_HOST:
6605 if (masklen != 128)
6606 bpf_error("Mask syntax for networks only");
6607 /* FALLTHROUGH */
6609 case Q_NET:
6610 b = gen_host6(addr, &mask, q.proto, q.dir, q.addr);
6611 ai = NULL;
6612 freeaddrinfo(res);
6613 return b;
6615 default:
6616 bpf_error("invalid qualifier against IPv6 address");
6617 /* NOTREACHED */
6619 return NULL;
6621 #endif /*INET6*/
6623 struct block *
6624 gen_ecode(eaddr, q)
6625 register const u_char *eaddr;
6626 struct qual q;
6628 struct block *b, *tmp;
6630 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) && q.proto == Q_LINK) {
6631 switch (linktype) {
6632 case DLT_EN10MB:
6633 case DLT_NETANALYZER:
6634 case DLT_NETANALYZER_TRANSPARENT:
6635 return gen_ehostop(eaddr, (int)q.dir);
6636 case DLT_FDDI:
6637 return gen_fhostop(eaddr, (int)q.dir);
6638 case DLT_IEEE802:
6639 return gen_thostop(eaddr, (int)q.dir);
6640 case DLT_IEEE802_11:
6641 case DLT_PRISM_HEADER:
6642 case DLT_IEEE802_11_RADIO_AVS:
6643 case DLT_IEEE802_11_RADIO:
6644 case DLT_PPI:
6645 return gen_wlanhostop(eaddr, (int)q.dir);
6646 case DLT_SUNATM:
6647 if (is_lane) {
6649 * Check that the packet doesn't begin with an
6650 * LE Control marker. (We've already generated
6651 * a test for LANE.)
6653 tmp = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS, BPF_H,
6654 0xFF00);
6655 gen_not(tmp);
6658 * Now check the MAC address.
6660 b = gen_ehostop(eaddr, (int)q.dir);
6661 gen_and(tmp, b);
6662 return b;
6664 break;
6665 case DLT_IP_OVER_FC:
6666 return gen_ipfchostop(eaddr, (int)q.dir);
6667 default:
6668 bpf_error("ethernet addresses supported only on ethernet/FDDI/token ring/802.11/ATM LANE/Fibre Channel");
6669 break;
6672 bpf_error("ethernet address used in non-ether expression");
6673 /* NOTREACHED */
6674 return NULL;
6677 void
6678 sappend(s0, s1)
6679 struct slist *s0, *s1;
6682 * This is definitely not the best way to do this, but the
6683 * lists will rarely get long.
6685 while (s0->next)
6686 s0 = s0->next;
6687 s0->next = s1;
6690 static struct slist *
6691 xfer_to_x(a)
6692 struct arth *a;
6694 struct slist *s;
6696 s = new_stmt(BPF_LDX|BPF_MEM);
6697 s->s.k = a->regno;
6698 return s;
6701 static struct slist *
6702 xfer_to_a(a)
6703 struct arth *a;
6705 struct slist *s;
6707 s = new_stmt(BPF_LD|BPF_MEM);
6708 s->s.k = a->regno;
6709 return s;
6713 * Modify "index" to use the value stored into its register as an
6714 * offset relative to the beginning of the header for the protocol
6715 * "proto", and allocate a register and put an item "size" bytes long
6716 * (1, 2, or 4) at that offset into that register, making it the register
6717 * for "index".
6719 struct arth *
6720 gen_load(proto, inst, size)
6721 int proto;
6722 struct arth *inst;
6723 int size;
6725 struct slist *s, *tmp;
6726 struct block *b;
6727 int regno = alloc_reg();
6729 free_reg(inst->regno);
6730 switch (size) {
6732 default:
6733 bpf_error("data size must be 1, 2, or 4");
6735 case 1:
6736 size = BPF_B;
6737 break;
6739 case 2:
6740 size = BPF_H;
6741 break;
6743 case 4:
6744 size = BPF_W;
6745 break;
6747 switch (proto) {
6748 default:
6749 bpf_error("unsupported index operation");
6751 case Q_RADIO:
6753 * The offset is relative to the beginning of the packet
6754 * data, if we have a radio header. (If we don't, this
6755 * is an error.)
6757 if (linktype != DLT_IEEE802_11_RADIO_AVS &&
6758 linktype != DLT_IEEE802_11_RADIO &&
6759 linktype != DLT_PRISM_HEADER)
6760 bpf_error("radio information not present in capture");
6763 * Load into the X register the offset computed into the
6764 * register specified by "index".
6766 s = xfer_to_x(inst);
6769 * Load the item at that offset.
6771 tmp = new_stmt(BPF_LD|BPF_IND|size);
6772 sappend(s, tmp);
6773 sappend(inst->s, s);
6774 break;
6776 case Q_LINK:
6778 * The offset is relative to the beginning of
6779 * the link-layer header.
6781 * XXX - what about ATM LANE? Should the index be
6782 * relative to the beginning of the AAL5 frame, so
6783 * that 0 refers to the beginning of the LE Control
6784 * field, or relative to the beginning of the LAN
6785 * frame, so that 0 refers, for Ethernet LANE, to
6786 * the beginning of the destination address?
6788 s = gen_llprefixlen();
6791 * If "s" is non-null, it has code to arrange that the
6792 * X register contains the length of the prefix preceding
6793 * the link-layer header. Add to it the offset computed
6794 * into the register specified by "index", and move that
6795 * into the X register. Otherwise, just load into the X
6796 * register the offset computed into the register specified
6797 * by "index".
6799 if (s != NULL) {
6800 sappend(s, xfer_to_a(inst));
6801 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6802 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6803 } else
6804 s = xfer_to_x(inst);
6807 * Load the item at the sum of the offset we've put in the
6808 * X register and the offset of the start of the link
6809 * layer header (which is 0 if the radio header is
6810 * variable-length; that header length is what we put
6811 * into the X register and then added to the index).
6813 tmp = new_stmt(BPF_LD|BPF_IND|size);
6814 tmp->s.k = off_ll;
6815 sappend(s, tmp);
6816 sappend(inst->s, s);
6817 break;
6819 case Q_IP:
6820 case Q_ARP:
6821 case Q_RARP:
6822 case Q_ATALK:
6823 case Q_DECNET:
6824 case Q_SCA:
6825 case Q_LAT:
6826 case Q_MOPRC:
6827 case Q_MOPDL:
6828 #ifdef INET6
6829 case Q_IPV6:
6830 #endif
6832 * The offset is relative to the beginning of
6833 * the network-layer header.
6834 * XXX - are there any cases where we want
6835 * off_nl_nosnap?
6837 s = gen_off_macpl();
6840 * If "s" is non-null, it has code to arrange that the
6841 * X register contains the offset of the MAC-layer
6842 * payload. Add to it the offset computed into the
6843 * register specified by "index", and move that into
6844 * the X register. Otherwise, just load into the X
6845 * register the offset computed into the register specified
6846 * by "index".
6848 if (s != NULL) {
6849 sappend(s, xfer_to_a(inst));
6850 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6851 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6852 } else
6853 s = xfer_to_x(inst);
6856 * Load the item at the sum of the offset we've put in the
6857 * X register, the offset of the start of the network
6858 * layer header from the beginning of the MAC-layer
6859 * payload, and the purported offset of the start of the
6860 * MAC-layer payload (which might be 0 if there's a
6861 * variable-length prefix before the link-layer header
6862 * or the link-layer header itself is variable-length;
6863 * the variable-length offset of the start of the
6864 * MAC-layer payload is what we put into the X register
6865 * and then added to the index).
6867 tmp = new_stmt(BPF_LD|BPF_IND|size);
6868 tmp->s.k = off_macpl + off_nl;
6869 sappend(s, tmp);
6870 sappend(inst->s, s);
6873 * Do the computation only if the packet contains
6874 * the protocol in question.
6876 b = gen_proto_abbrev(proto);
6877 if (inst->b)
6878 gen_and(inst->b, b);
6879 inst->b = b;
6880 break;
6882 case Q_SCTP:
6883 case Q_TCP:
6884 case Q_UDP:
6885 case Q_ICMP:
6886 case Q_IGMP:
6887 case Q_IGRP:
6888 case Q_PIM:
6889 case Q_VRRP:
6890 case Q_CARP:
6892 * The offset is relative to the beginning of
6893 * the transport-layer header.
6895 * Load the X register with the length of the IPv4 header
6896 * (plus the offset of the link-layer header, if it's
6897 * a variable-length header), in bytes.
6899 * XXX - are there any cases where we want
6900 * off_nl_nosnap?
6901 * XXX - we should, if we're built with
6902 * IPv6 support, generate code to load either
6903 * IPv4, IPv6, or both, as appropriate.
6905 s = gen_loadx_iphdrlen();
6908 * The X register now contains the sum of the length
6909 * of any variable-length header preceding the link-layer
6910 * header, any variable-length link-layer header, and the
6911 * length of the network-layer header.
6913 * Load into the A register the offset relative to
6914 * the beginning of the transport layer header,
6915 * add the X register to that, move that to the
6916 * X register, and load with an offset from the
6917 * X register equal to the offset of the network
6918 * layer header relative to the beginning of
6919 * the MAC-layer payload plus the fixed-length
6920 * portion of the offset of the MAC-layer payload
6921 * from the beginning of the raw packet data.
6923 sappend(s, xfer_to_a(inst));
6924 sappend(s, new_stmt(BPF_ALU|BPF_ADD|BPF_X));
6925 sappend(s, new_stmt(BPF_MISC|BPF_TAX));
6926 sappend(s, tmp = new_stmt(BPF_LD|BPF_IND|size));
6927 tmp->s.k = off_macpl + off_nl;
6928 sappend(inst->s, s);
6931 * Do the computation only if the packet contains
6932 * the protocol in question - which is true only
6933 * if this is an IP datagram and is the first or
6934 * only fragment of that datagram.
6936 gen_and(gen_proto_abbrev(proto), b = gen_ipfrag());
6937 if (inst->b)
6938 gen_and(inst->b, b);
6939 #ifdef INET6
6940 gen_and(gen_proto_abbrev(Q_IP), b);
6941 #endif
6942 inst->b = b;
6943 break;
6944 #ifdef INET6
6945 case Q_ICMPV6:
6946 bpf_error("IPv6 upper-layer protocol is not supported by proto[x]");
6947 /*NOTREACHED*/
6948 #endif
6950 inst->regno = regno;
6951 s = new_stmt(BPF_ST);
6952 s->s.k = regno;
6953 sappend(inst->s, s);
6955 return inst;
6958 struct block *
6959 gen_relation(code, a0, a1, reversed)
6960 int code;
6961 struct arth *a0, *a1;
6962 int reversed;
6964 struct slist *s0, *s1, *s2;
6965 struct block *b, *tmp;
6967 s0 = xfer_to_x(a1);
6968 s1 = xfer_to_a(a0);
6969 if (code == BPF_JEQ) {
6970 s2 = new_stmt(BPF_ALU|BPF_SUB|BPF_X);
6971 b = new_block(JMP(code));
6972 sappend(s1, s2);
6974 else
6975 b = new_block(BPF_JMP|code|BPF_X);
6976 if (reversed)
6977 gen_not(b);
6979 sappend(s0, s1);
6980 sappend(a1->s, s0);
6981 sappend(a0->s, a1->s);
6983 b->stmts = a0->s;
6985 free_reg(a0->regno);
6986 free_reg(a1->regno);
6988 /* 'and' together protocol checks */
6989 if (a0->b) {
6990 if (a1->b) {
6991 gen_and(a0->b, tmp = a1->b);
6993 else
6994 tmp = a0->b;
6995 } else
6996 tmp = a1->b;
6998 if (tmp)
6999 gen_and(tmp, b);
7001 return b;
7004 struct arth *
7005 gen_loadlen()
7007 int regno = alloc_reg();
7008 struct arth *a = (struct arth *)newchunk(sizeof(*a));
7009 struct slist *s;
7011 s = new_stmt(BPF_LD|BPF_LEN);
7012 s->next = new_stmt(BPF_ST);
7013 s->next->s.k = regno;
7014 a->s = s;
7015 a->regno = regno;
7017 return a;
7020 struct arth *
7021 gen_loadi(val)
7022 int val;
7024 struct arth *a;
7025 struct slist *s;
7026 int reg;
7028 a = (struct arth *)newchunk(sizeof(*a));
7030 reg = alloc_reg();
7032 s = new_stmt(BPF_LD|BPF_IMM);
7033 s->s.k = val;
7034 s->next = new_stmt(BPF_ST);
7035 s->next->s.k = reg;
7036 a->s = s;
7037 a->regno = reg;
7039 return a;
7042 struct arth *
7043 gen_neg(a)
7044 struct arth *a;
7046 struct slist *s;
7048 s = xfer_to_a(a);
7049 sappend(a->s, s);
7050 s = new_stmt(BPF_ALU|BPF_NEG);
7051 s->s.k = 0;
7052 sappend(a->s, s);
7053 s = new_stmt(BPF_ST);
7054 s->s.k = a->regno;
7055 sappend(a->s, s);
7057 return a;
7060 struct arth *
7061 gen_arth(code, a0, a1)
7062 int code;
7063 struct arth *a0, *a1;
7065 struct slist *s0, *s1, *s2;
7067 s0 = xfer_to_x(a1);
7068 s1 = xfer_to_a(a0);
7069 s2 = new_stmt(BPF_ALU|BPF_X|code);
7071 sappend(s1, s2);
7072 sappend(s0, s1);
7073 sappend(a1->s, s0);
7074 sappend(a0->s, a1->s);
7076 free_reg(a0->regno);
7077 free_reg(a1->regno);
7079 s0 = new_stmt(BPF_ST);
7080 a0->regno = s0->s.k = alloc_reg();
7081 sappend(a0->s, s0);
7083 return a0;
7087 * Here we handle simple allocation of the scratch registers.
7088 * If too many registers are alloc'd, the allocator punts.
7090 static int regused[BPF_MEMWORDS];
7091 static int curreg;
7094 * Initialize the table of used registers and the current register.
7096 static void
7097 init_regs()
7099 curreg = 0;
7100 memset(regused, 0, sizeof regused);
7104 * Return the next free register.
7106 static int
7107 alloc_reg()
7109 int n = BPF_MEMWORDS;
7111 while (--n >= 0) {
7112 if (regused[curreg])
7113 curreg = (curreg + 1) % BPF_MEMWORDS;
7114 else {
7115 regused[curreg] = 1;
7116 return curreg;
7119 bpf_error("too many registers needed to evaluate expression");
7120 /* NOTREACHED */
7121 return 0;
7125 * Return a register to the table so it can
7126 * be used later.
7128 static void
7129 free_reg(n)
7130 int n;
7132 regused[n] = 0;
7135 static struct block *
7136 gen_len(jmp, n)
7137 int jmp, n;
7139 struct slist *s;
7140 struct block *b;
7142 s = new_stmt(BPF_LD|BPF_LEN);
7143 b = new_block(JMP(jmp));
7144 b->stmts = s;
7145 b->s.k = n;
7147 return b;
7150 struct block *
7151 gen_greater(n)
7152 int n;
7154 return gen_len(BPF_JGE, n);
7158 * Actually, this is less than or equal.
7160 struct block *
7161 gen_less(n)
7162 int n;
7164 struct block *b;
7166 b = gen_len(BPF_JGT, n);
7167 gen_not(b);
7169 return b;
7173 * This is for "byte {idx} {op} {val}"; "idx" is treated as relative to
7174 * the beginning of the link-layer header.
7175 * XXX - that means you can't test values in the radiotap header, but
7176 * as that header is difficult if not impossible to parse generally
7177 * without a loop, that might not be a severe problem. A new keyword
7178 * "radio" could be added for that, although what you'd really want
7179 * would be a way of testing particular radio header values, which
7180 * would generate code appropriate to the radio header in question.
7182 struct block *
7183 gen_byteop(op, idx, val)
7184 int op, idx, val;
7186 struct block *b;
7187 struct slist *s;
7189 switch (op) {
7190 default:
7191 abort();
7193 case '=':
7194 return gen_cmp(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7196 case '<':
7197 b = gen_cmp_lt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7198 return b;
7200 case '>':
7201 b = gen_cmp_gt(OR_LINK, (u_int)idx, BPF_B, (bpf_int32)val);
7202 return b;
7204 case '|':
7205 s = new_stmt(BPF_ALU|BPF_OR|BPF_K);
7206 break;
7208 case '&':
7209 s = new_stmt(BPF_ALU|BPF_AND|BPF_K);
7210 break;
7212 s->s.k = val;
7213 b = new_block(JMP(BPF_JEQ));
7214 b->stmts = s;
7215 gen_not(b);
7217 return b;
7220 static u_char abroadcast[] = { 0x0 };
7222 struct block *
7223 gen_broadcast(proto)
7224 int proto;
7226 bpf_u_int32 hostmask;
7227 struct block *b0, *b1, *b2;
7228 static u_char ebroadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7230 switch (proto) {
7232 case Q_DEFAULT:
7233 case Q_LINK:
7234 switch (linktype) {
7235 case DLT_ARCNET:
7236 case DLT_ARCNET_LINUX:
7237 return gen_ahostop(abroadcast, Q_DST);
7238 case DLT_EN10MB:
7239 case DLT_NETANALYZER:
7240 case DLT_NETANALYZER_TRANSPARENT:
7241 return gen_ehostop(ebroadcast, Q_DST);
7242 case DLT_FDDI:
7243 return gen_fhostop(ebroadcast, Q_DST);
7244 case DLT_IEEE802:
7245 return gen_thostop(ebroadcast, Q_DST);
7246 case DLT_IEEE802_11:
7247 case DLT_PRISM_HEADER:
7248 case DLT_IEEE802_11_RADIO_AVS:
7249 case DLT_IEEE802_11_RADIO:
7250 case DLT_PPI:
7251 return gen_wlanhostop(ebroadcast, Q_DST);
7252 case DLT_IP_OVER_FC:
7253 return gen_ipfchostop(ebroadcast, Q_DST);
7254 case DLT_SUNATM:
7255 if (is_lane) {
7257 * Check that the packet doesn't begin with an
7258 * LE Control marker. (We've already generated
7259 * a test for LANE.)
7261 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7262 BPF_H, 0xFF00);
7263 gen_not(b1);
7266 * Now check the MAC address.
7268 b0 = gen_ehostop(ebroadcast, Q_DST);
7269 gen_and(b1, b0);
7270 return b0;
7272 break;
7273 default:
7274 bpf_error("not a broadcast link");
7276 break;
7278 case Q_IP:
7280 * We treat a netmask of PCAP_NETMASK_UNKNOWN (0xffffffff)
7281 * as an indication that we don't know the netmask, and fail
7282 * in that case.
7284 if (netmask == PCAP_NETMASK_UNKNOWN)
7285 bpf_error("netmask not known, so 'ip broadcast' not supported");
7286 b0 = gen_linktype(ETHERTYPE_IP);
7287 hostmask = ~netmask;
7288 b1 = gen_mcmp(OR_NET, 16, BPF_W, (bpf_int32)0, hostmask);
7289 b2 = gen_mcmp(OR_NET, 16, BPF_W,
7290 (bpf_int32)(~0 & hostmask), hostmask);
7291 gen_or(b1, b2);
7292 gen_and(b0, b2);
7293 return b2;
7295 bpf_error("only link-layer/IP broadcast filters supported");
7296 /* NOTREACHED */
7297 return NULL;
7301 * Generate code to test the low-order bit of a MAC address (that's
7302 * the bottom bit of the *first* byte).
7304 static struct block *
7305 gen_mac_multicast(offset)
7306 int offset;
7308 register struct block *b0;
7309 register struct slist *s;
7311 /* link[offset] & 1 != 0 */
7312 s = gen_load_a(OR_LINK, offset, BPF_B);
7313 b0 = new_block(JMP(BPF_JSET));
7314 b0->s.k = 1;
7315 b0->stmts = s;
7316 return b0;
7319 struct block *
7320 gen_multicast(proto)
7321 int proto;
7323 register struct block *b0, *b1, *b2;
7324 register struct slist *s;
7326 switch (proto) {
7328 case Q_DEFAULT:
7329 case Q_LINK:
7330 switch (linktype) {
7331 case DLT_ARCNET:
7332 case DLT_ARCNET_LINUX:
7333 /* all ARCnet multicasts use the same address */
7334 return gen_ahostop(abroadcast, Q_DST);
7335 case DLT_EN10MB:
7336 case DLT_NETANALYZER:
7337 case DLT_NETANALYZER_TRANSPARENT:
7338 /* ether[0] & 1 != 0 */
7339 return gen_mac_multicast(0);
7340 case DLT_FDDI:
7342 * XXX TEST THIS: MIGHT NOT PORT PROPERLY XXX
7344 * XXX - was that referring to bit-order issues?
7346 /* fddi[1] & 1 != 0 */
7347 return gen_mac_multicast(1);
7348 case DLT_IEEE802:
7349 /* tr[2] & 1 != 0 */
7350 return gen_mac_multicast(2);
7351 case DLT_IEEE802_11:
7352 case DLT_PRISM_HEADER:
7353 case DLT_IEEE802_11_RADIO_AVS:
7354 case DLT_IEEE802_11_RADIO:
7355 case DLT_PPI:
7357 * Oh, yuk.
7359 * For control frames, there is no DA.
7361 * For management frames, DA is at an
7362 * offset of 4 from the beginning of
7363 * the packet.
7365 * For data frames, DA is at an offset
7366 * of 4 from the beginning of the packet
7367 * if To DS is clear and at an offset of
7368 * 16 from the beginning of the packet
7369 * if To DS is set.
7373 * Generate the tests to be done for data frames.
7375 * First, check for To DS set, i.e. "link[1] & 0x01".
7377 s = gen_load_a(OR_LINK, 1, BPF_B);
7378 b1 = new_block(JMP(BPF_JSET));
7379 b1->s.k = 0x01; /* To DS */
7380 b1->stmts = s;
7383 * If To DS is set, the DA is at 16.
7385 b0 = gen_mac_multicast(16);
7386 gen_and(b1, b0);
7389 * Now, check for To DS not set, i.e. check
7390 * "!(link[1] & 0x01)".
7392 s = gen_load_a(OR_LINK, 1, BPF_B);
7393 b2 = new_block(JMP(BPF_JSET));
7394 b2->s.k = 0x01; /* To DS */
7395 b2->stmts = s;
7396 gen_not(b2);
7399 * If To DS is not set, the DA is at 4.
7401 b1 = gen_mac_multicast(4);
7402 gen_and(b2, b1);
7405 * Now OR together the last two checks. That gives
7406 * the complete set of checks for data frames.
7408 gen_or(b1, b0);
7411 * Now check for a data frame.
7412 * I.e, check "link[0] & 0x08".
7414 s = gen_load_a(OR_LINK, 0, BPF_B);
7415 b1 = new_block(JMP(BPF_JSET));
7416 b1->s.k = 0x08;
7417 b1->stmts = s;
7420 * AND that with the checks done for data frames.
7422 gen_and(b1, b0);
7425 * If the high-order bit of the type value is 0, this
7426 * is a management frame.
7427 * I.e, check "!(link[0] & 0x08)".
7429 s = gen_load_a(OR_LINK, 0, BPF_B);
7430 b2 = new_block(JMP(BPF_JSET));
7431 b2->s.k = 0x08;
7432 b2->stmts = s;
7433 gen_not(b2);
7436 * For management frames, the DA is at 4.
7438 b1 = gen_mac_multicast(4);
7439 gen_and(b2, b1);
7442 * OR that with the checks done for data frames.
7443 * That gives the checks done for management and
7444 * data frames.
7446 gen_or(b1, b0);
7449 * If the low-order bit of the type value is 1,
7450 * this is either a control frame or a frame
7451 * with a reserved type, and thus not a
7452 * frame with an SA.
7454 * I.e., check "!(link[0] & 0x04)".
7456 s = gen_load_a(OR_LINK, 0, BPF_B);
7457 b1 = new_block(JMP(BPF_JSET));
7458 b1->s.k = 0x04;
7459 b1->stmts = s;
7460 gen_not(b1);
7463 * AND that with the checks for data and management
7464 * frames.
7466 gen_and(b1, b0);
7467 return b0;
7468 case DLT_IP_OVER_FC:
7469 b0 = gen_mac_multicast(2);
7470 return b0;
7471 case DLT_SUNATM:
7472 if (is_lane) {
7474 * Check that the packet doesn't begin with an
7475 * LE Control marker. (We've already generated
7476 * a test for LANE.)
7478 b1 = gen_cmp(OR_LINK, SUNATM_PKT_BEGIN_POS,
7479 BPF_H, 0xFF00);
7480 gen_not(b1);
7482 /* ether[off_mac] & 1 != 0 */
7483 b0 = gen_mac_multicast(off_mac);
7484 gen_and(b1, b0);
7485 return b0;
7487 break;
7488 default:
7489 break;
7491 /* Link not known to support multicasts */
7492 break;
7494 case Q_IP:
7495 b0 = gen_linktype(ETHERTYPE_IP);
7496 b1 = gen_cmp_ge(OR_NET, 16, BPF_B, (bpf_int32)224);
7497 gen_and(b0, b1);
7498 return b1;
7500 #ifdef INET6
7501 case Q_IPV6:
7502 b0 = gen_linktype(ETHERTYPE_IPV6);
7503 b1 = gen_cmp(OR_NET, 24, BPF_B, (bpf_int32)255);
7504 gen_and(b0, b1);
7505 return b1;
7506 #endif /* INET6 */
7508 bpf_error("link-layer multicast filters supported only on ethernet/FDDI/token ring/ARCNET/802.11/ATM LANE/Fibre Channel");
7509 /* NOTREACHED */
7510 return NULL;
7514 * Filter on inbound (dir == 0) or outbound (dir == 1) traffic.
7515 * Outbound traffic is sent by this machine, while inbound traffic is
7516 * sent by a remote machine (and may include packets destined for a
7517 * unicast or multicast link-layer address we are not subscribing to).
7518 * These are the same definitions implemented by pcap_setdirection().
7519 * Capturing only unicast traffic destined for this host is probably
7520 * better accomplished using a higher-layer filter.
7522 struct block *
7523 gen_inbound(dir)
7524 int dir;
7526 register struct block *b0;
7529 * Only some data link types support inbound/outbound qualifiers.
7531 switch (linktype) {
7532 case DLT_SLIP:
7533 b0 = gen_relation(BPF_JEQ,
7534 gen_load(Q_LINK, gen_loadi(0), 1),
7535 gen_loadi(0),
7536 dir);
7537 break;
7539 case DLT_IPNET:
7540 if (dir) {
7541 /* match outgoing packets */
7542 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_OUTBOUND);
7543 } else {
7544 /* match incoming packets */
7545 b0 = gen_cmp(OR_LINK, 2, BPF_H, IPNET_INBOUND);
7547 break;
7549 case DLT_LINUX_SLL:
7550 /* match outgoing packets */
7551 b0 = gen_cmp(OR_LINK, 0, BPF_H, LINUX_SLL_OUTGOING);
7552 if (!dir) {
7553 /* to filter on inbound traffic, invert the match */
7554 gen_not(b0);
7556 break;
7558 #ifdef HAVE_NET_PFVAR_H
7559 case DLT_PFLOG:
7560 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, dir), BPF_B,
7561 (bpf_int32)((dir == 0) ? PF_IN : PF_OUT));
7562 break;
7563 #endif
7565 case DLT_PPP_PPPD:
7566 if (dir) {
7567 /* match outgoing packets */
7568 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_OUT);
7569 } else {
7570 /* match incoming packets */
7571 b0 = gen_cmp(OR_LINK, 0, BPF_B, PPP_PPPD_IN);
7573 break;
7575 case DLT_JUNIPER_MFR:
7576 case DLT_JUNIPER_MLFR:
7577 case DLT_JUNIPER_MLPPP:
7578 case DLT_JUNIPER_ATM1:
7579 case DLT_JUNIPER_ATM2:
7580 case DLT_JUNIPER_PPPOE:
7581 case DLT_JUNIPER_PPPOE_ATM:
7582 case DLT_JUNIPER_GGSN:
7583 case DLT_JUNIPER_ES:
7584 case DLT_JUNIPER_MONITOR:
7585 case DLT_JUNIPER_SERVICES:
7586 case DLT_JUNIPER_ETHER:
7587 case DLT_JUNIPER_PPP:
7588 case DLT_JUNIPER_FRELAY:
7589 case DLT_JUNIPER_CHDLC:
7590 case DLT_JUNIPER_VP:
7591 case DLT_JUNIPER_ST:
7592 case DLT_JUNIPER_ISM:
7593 case DLT_JUNIPER_VS:
7594 case DLT_JUNIPER_SRX_E2E:
7595 case DLT_JUNIPER_FIBRECHANNEL:
7596 case DLT_JUNIPER_ATM_CEMIC:
7598 /* juniper flags (including direction) are stored
7599 * the byte after the 3-byte magic number */
7600 if (dir) {
7601 /* match outgoing packets */
7602 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 0, 0x01);
7603 } else {
7604 /* match incoming packets */
7605 b0 = gen_mcmp(OR_LINK, 3, BPF_B, 1, 0x01);
7607 break;
7609 default:
7611 * If we have packet meta-data indicating a direction,
7612 * check it, otherwise give up as this link-layer type
7613 * has nothing in the packet data.
7615 #if defined(PF_PACKET) && defined(SO_ATTACH_FILTER)
7617 * We infer that this is Linux with PF_PACKET support.
7618 * If this is a *live* capture, we can look at
7619 * special meta-data in the filter expression;
7620 * if it's a savefile, we can't.
7622 if (bpf_pcap->sf.rfile != NULL) {
7623 /* We have a FILE *, so this is a savefile */
7624 bpf_error("inbound/outbound not supported on linktype %d when reading savefiles",
7625 linktype);
7626 b0 = NULL;
7627 /* NOTREACHED */
7629 /* match outgoing packets */
7630 b0 = gen_cmp(OR_LINK, SKF_AD_OFF + SKF_AD_PKTTYPE, BPF_H,
7631 PACKET_OUTGOING);
7632 if (!dir) {
7633 /* to filter on inbound traffic, invert the match */
7634 gen_not(b0);
7636 #else /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7637 bpf_error("inbound/outbound not supported on linktype %d",
7638 linktype);
7639 b0 = NULL;
7640 /* NOTREACHED */
7641 #endif /* defined(PF_PACKET) && defined(SO_ATTACH_FILTER) */
7643 return (b0);
7646 #ifdef HAVE_NET_PFVAR_H
7647 /* PF firewall log matched interface */
7648 struct block *
7649 gen_pf_ifname(const char *ifname)
7651 struct block *b0;
7652 u_int len, off;
7654 if (linktype != DLT_PFLOG) {
7655 bpf_error("ifname supported only on PF linktype");
7656 /* NOTREACHED */
7658 len = sizeof(((struct pfloghdr *)0)->ifname);
7659 off = offsetof(struct pfloghdr, ifname);
7660 if (strlen(ifname) >= len) {
7661 bpf_error("ifname interface names can only be %d characters",
7662 len-1);
7663 /* NOTREACHED */
7665 b0 = gen_bcmp(OR_LINK, off, strlen(ifname), (const u_char *)ifname);
7666 return (b0);
7669 /* PF firewall log ruleset name */
7670 struct block *
7671 gen_pf_ruleset(char *ruleset)
7673 struct block *b0;
7675 if (linktype != DLT_PFLOG) {
7676 bpf_error("ruleset supported only on PF linktype");
7677 /* NOTREACHED */
7680 if (strlen(ruleset) >= sizeof(((struct pfloghdr *)0)->ruleset)) {
7681 bpf_error("ruleset names can only be %ld characters",
7682 (long)(sizeof(((struct pfloghdr *)0)->ruleset) - 1));
7683 /* NOTREACHED */
7686 b0 = gen_bcmp(OR_LINK, offsetof(struct pfloghdr, ruleset),
7687 strlen(ruleset), (const u_char *)ruleset);
7688 return (b0);
7691 /* PF firewall log rule number */
7692 struct block *
7693 gen_pf_rnr(int rnr)
7695 struct block *b0;
7697 if (linktype != DLT_PFLOG) {
7698 bpf_error("rnr supported only on PF linktype");
7699 /* NOTREACHED */
7702 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, rulenr), BPF_W,
7703 (bpf_int32)rnr);
7704 return (b0);
7707 /* PF firewall log sub-rule number */
7708 struct block *
7709 gen_pf_srnr(int srnr)
7711 struct block *b0;
7713 if (linktype != DLT_PFLOG) {
7714 bpf_error("srnr supported only on PF linktype");
7715 /* NOTREACHED */
7718 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, subrulenr), BPF_W,
7719 (bpf_int32)srnr);
7720 return (b0);
7723 /* PF firewall log reason code */
7724 struct block *
7725 gen_pf_reason(int reason)
7727 struct block *b0;
7729 if (linktype != DLT_PFLOG) {
7730 bpf_error("reason supported only on PF linktype");
7731 /* NOTREACHED */
7734 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, reason), BPF_B,
7735 (bpf_int32)reason);
7736 return (b0);
7739 /* PF firewall log action */
7740 struct block *
7741 gen_pf_action(int action)
7743 struct block *b0;
7745 if (linktype != DLT_PFLOG) {
7746 bpf_error("action supported only on PF linktype");
7747 /* NOTREACHED */
7750 b0 = gen_cmp(OR_LINK, offsetof(struct pfloghdr, action), BPF_B,
7751 (bpf_int32)action);
7752 return (b0);
7754 #else /* !HAVE_NET_PFVAR_H */
7755 struct block *
7756 gen_pf_ifname(const char *ifname)
7758 bpf_error("libpcap was compiled without pf support");
7759 /* NOTREACHED */
7760 return (NULL);
7763 struct block *
7764 gen_pf_ruleset(char *ruleset)
7766 bpf_error("libpcap was compiled on a machine without pf support");
7767 /* NOTREACHED */
7768 return (NULL);
7771 struct block *
7772 gen_pf_rnr(int rnr)
7774 bpf_error("libpcap was compiled on a machine without pf support");
7775 /* NOTREACHED */
7776 return (NULL);
7779 struct block *
7780 gen_pf_srnr(int srnr)
7782 bpf_error("libpcap was compiled on a machine without pf support");
7783 /* NOTREACHED */
7784 return (NULL);
7787 struct block *
7788 gen_pf_reason(int reason)
7790 bpf_error("libpcap was compiled on a machine without pf support");
7791 /* NOTREACHED */
7792 return (NULL);
7795 struct block *
7796 gen_pf_action(int action)
7798 bpf_error("libpcap was compiled on a machine without pf support");
7799 /* NOTREACHED */
7800 return (NULL);
7802 #endif /* HAVE_NET_PFVAR_H */
7804 /* IEEE 802.11 wireless header */
7805 struct block *
7806 gen_p80211_type(int type, int mask)
7808 struct block *b0;
7810 switch (linktype) {
7812 case DLT_IEEE802_11:
7813 case DLT_PRISM_HEADER:
7814 case DLT_IEEE802_11_RADIO_AVS:
7815 case DLT_IEEE802_11_RADIO:
7816 b0 = gen_mcmp(OR_LINK, 0, BPF_B, (bpf_int32)type,
7817 (bpf_int32)mask);
7818 break;
7820 default:
7821 bpf_error("802.11 link-layer types supported only on 802.11");
7822 /* NOTREACHED */
7825 return (b0);
7828 struct block *
7829 gen_p80211_fcdir(int fcdir)
7831 struct block *b0;
7833 switch (linktype) {
7835 case DLT_IEEE802_11:
7836 case DLT_PRISM_HEADER:
7837 case DLT_IEEE802_11_RADIO_AVS:
7838 case DLT_IEEE802_11_RADIO:
7839 break;
7841 default:
7842 bpf_error("frame direction supported only with 802.11 headers");
7843 /* NOTREACHED */
7846 b0 = gen_mcmp(OR_LINK, 1, BPF_B, (bpf_int32)fcdir,
7847 (bpf_u_int32)IEEE80211_FC1_DIR_MASK);
7849 return (b0);
7852 struct block *
7853 gen_acode(eaddr, q)
7854 register const u_char *eaddr;
7855 struct qual q;
7857 switch (linktype) {
7859 case DLT_ARCNET:
7860 case DLT_ARCNET_LINUX:
7861 if ((q.addr == Q_HOST || q.addr == Q_DEFAULT) &&
7862 q.proto == Q_LINK)
7863 return (gen_ahostop(eaddr, (int)q.dir));
7864 else {
7865 bpf_error("ARCnet address used in non-arc expression");
7866 /* NOTREACHED */
7868 break;
7870 default:
7871 bpf_error("aid supported only on ARCnet");
7872 /* NOTREACHED */
7874 bpf_error("ARCnet address used in non-arc expression");
7875 /* NOTREACHED */
7876 return NULL;
7879 static struct block *
7880 gen_ahostop(eaddr, dir)
7881 register const u_char *eaddr;
7882 register int dir;
7884 register struct block *b0, *b1;
7886 switch (dir) {
7887 /* src comes first, different from Ethernet */
7888 case Q_SRC:
7889 return gen_bcmp(OR_LINK, 0, 1, eaddr);
7891 case Q_DST:
7892 return gen_bcmp(OR_LINK, 1, 1, eaddr);
7894 case Q_AND:
7895 b0 = gen_ahostop(eaddr, Q_SRC);
7896 b1 = gen_ahostop(eaddr, Q_DST);
7897 gen_and(b0, b1);
7898 return b1;
7900 case Q_DEFAULT:
7901 case Q_OR:
7902 b0 = gen_ahostop(eaddr, Q_SRC);
7903 b1 = gen_ahostop(eaddr, Q_DST);
7904 gen_or(b0, b1);
7905 return b1;
7907 case Q_ADDR1:
7908 bpf_error("'addr1' is only supported on 802.11");
7909 break;
7911 case Q_ADDR2:
7912 bpf_error("'addr2' is only supported on 802.11");
7913 break;
7915 case Q_ADDR3:
7916 bpf_error("'addr3' is only supported on 802.11");
7917 break;
7919 case Q_ADDR4:
7920 bpf_error("'addr4' is only supported on 802.11");
7921 break;
7923 case Q_RA:
7924 bpf_error("'ra' is only supported on 802.11");
7925 break;
7927 case Q_TA:
7928 bpf_error("'ta' is only supported on 802.11");
7929 break;
7931 abort();
7932 /* NOTREACHED */
7936 * support IEEE 802.1Q VLAN trunk over ethernet
7938 struct block *
7939 gen_vlan(vlan_num)
7940 int vlan_num;
7942 struct block *b0, *b1;
7944 /* can't check for VLAN-encapsulated packets inside MPLS */
7945 if (label_stack_depth > 0)
7946 bpf_error("no VLAN match after MPLS");
7949 * Check for a VLAN packet, and then change the offsets to point
7950 * to the type and data fields within the VLAN packet. Just
7951 * increment the offsets, so that we can support a hierarchy, e.g.
7952 * "vlan 300 && vlan 200" to capture VLAN 200 encapsulated within
7953 * VLAN 100.
7955 * XXX - this is a bit of a kludge. If we were to split the
7956 * compiler into a parser that parses an expression and
7957 * generates an expression tree, and a code generator that
7958 * takes an expression tree (which could come from our
7959 * parser or from some other parser) and generates BPF code,
7960 * we could perhaps make the offsets parameters of routines
7961 * and, in the handler for an "AND" node, pass to subnodes
7962 * other than the VLAN node the adjusted offsets.
7964 * This would mean that "vlan" would, instead of changing the
7965 * behavior of *all* tests after it, change only the behavior
7966 * of tests ANDed with it. That would change the documented
7967 * semantics of "vlan", which might break some expressions.
7968 * However, it would mean that "(vlan and ip) or ip" would check
7969 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
7970 * checking only for VLAN-encapsulated IP, so that could still
7971 * be considered worth doing; it wouldn't break expressions
7972 * that are of the form "vlan and ..." or "vlan N and ...",
7973 * which I suspect are the most common expressions involving
7974 * "vlan". "vlan or ..." doesn't necessarily do what the user
7975 * would really want, now, as all the "or ..." tests would
7976 * be done assuming a VLAN, even though the "or" could be viewed
7977 * as meaning "or, if this isn't a VLAN packet...".
7979 orig_nl = off_nl;
7981 switch (linktype) {
7983 case DLT_EN10MB:
7984 case DLT_NETANALYZER:
7985 case DLT_NETANALYZER_TRANSPARENT:
7986 /* check for VLAN, including QinQ */
7987 b0 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7988 (bpf_int32)ETHERTYPE_8021Q);
7989 b1 = gen_cmp(OR_LINK, off_linktype, BPF_H,
7990 (bpf_int32)ETHERTYPE_8021QINQ);
7991 gen_or(b0,b1);
7992 b0 = b1;
7994 /* If a specific VLAN is requested, check VLAN id */
7995 if (vlan_num >= 0) {
7996 b1 = gen_mcmp(OR_MACPL, 0, BPF_H,
7997 (bpf_int32)vlan_num, 0x0fff);
7998 gen_and(b0, b1);
7999 b0 = b1;
8002 off_macpl += 4;
8003 off_linktype += 4;
8004 #if 0
8005 off_nl_nosnap += 4;
8006 off_nl += 4;
8007 #endif
8008 break;
8010 default:
8011 bpf_error("no VLAN support for data link type %d",
8012 linktype);
8013 /*NOTREACHED*/
8016 return (b0);
8020 * support for MPLS
8022 struct block *
8023 gen_mpls(label_num)
8024 int label_num;
8026 struct block *b0,*b1;
8029 * Change the offsets to point to the type and data fields within
8030 * the MPLS packet. Just increment the offsets, so that we
8031 * can support a hierarchy, e.g. "mpls 100000 && mpls 1024" to
8032 * capture packets with an outer label of 100000 and an inner
8033 * label of 1024.
8035 * XXX - this is a bit of a kludge. See comments in gen_vlan().
8037 orig_nl = off_nl;
8039 if (label_stack_depth > 0) {
8040 /* just match the bottom-of-stack bit clear */
8041 b0 = gen_mcmp(OR_MACPL, orig_nl-2, BPF_B, 0, 0x01);
8042 } else {
8044 * Indicate that we're checking MPLS-encapsulated headers,
8045 * to make sure higher level code generators don't try to
8046 * match against IP-related protocols such as Q_ARP, Q_RARP
8047 * etc.
8049 switch (linktype) {
8051 case DLT_C_HDLC: /* fall through */
8052 case DLT_EN10MB:
8053 case DLT_NETANALYZER:
8054 case DLT_NETANALYZER_TRANSPARENT:
8055 b0 = gen_linktype(ETHERTYPE_MPLS);
8056 break;
8058 case DLT_PPP:
8059 b0 = gen_linktype(PPP_MPLS_UCAST);
8060 break;
8062 /* FIXME add other DLT_s ...
8063 * for Frame-Relay/and ATM this may get messy due to SNAP headers
8064 * leave it for now */
8066 default:
8067 bpf_error("no MPLS support for data link type %d",
8068 linktype);
8069 b0 = NULL;
8070 /*NOTREACHED*/
8071 break;
8075 /* If a specific MPLS label is requested, check it */
8076 if (label_num >= 0) {
8077 label_num = label_num << 12; /* label is shifted 12 bits on the wire */
8078 b1 = gen_mcmp(OR_MACPL, orig_nl, BPF_W, (bpf_int32)label_num,
8079 0xfffff000); /* only compare the first 20 bits */
8080 gen_and(b0, b1);
8081 b0 = b1;
8084 off_nl_nosnap += 4;
8085 off_nl += 4;
8086 label_stack_depth++;
8087 return (b0);
8091 * Support PPPOE discovery and session.
8093 struct block *
8094 gen_pppoed()
8096 /* check for PPPoE discovery */
8097 return gen_linktype((bpf_int32)ETHERTYPE_PPPOED);
8100 struct block *
8101 gen_pppoes()
8103 struct block *b0;
8106 * Test against the PPPoE session link-layer type.
8108 b0 = gen_linktype((bpf_int32)ETHERTYPE_PPPOES);
8111 * Change the offsets to point to the type and data fields within
8112 * the PPP packet, and note that this is PPPoE rather than
8113 * raw PPP.
8115 * XXX - this is a bit of a kludge. If we were to split the
8116 * compiler into a parser that parses an expression and
8117 * generates an expression tree, and a code generator that
8118 * takes an expression tree (which could come from our
8119 * parser or from some other parser) and generates BPF code,
8120 * we could perhaps make the offsets parameters of routines
8121 * and, in the handler for an "AND" node, pass to subnodes
8122 * other than the PPPoE node the adjusted offsets.
8124 * This would mean that "pppoes" would, instead of changing the
8125 * behavior of *all* tests after it, change only the behavior
8126 * of tests ANDed with it. That would change the documented
8127 * semantics of "pppoes", which might break some expressions.
8128 * However, it would mean that "(pppoes and ip) or ip" would check
8129 * both for VLAN-encapsulated IP and IP-over-Ethernet, rather than
8130 * checking only for VLAN-encapsulated IP, so that could still
8131 * be considered worth doing; it wouldn't break expressions
8132 * that are of the form "pppoes and ..." which I suspect are the
8133 * most common expressions involving "pppoes". "pppoes or ..."
8134 * doesn't necessarily do what the user would really want, now,
8135 * as all the "or ..." tests would be done assuming PPPoE, even
8136 * though the "or" could be viewed as meaning "or, if this isn't
8137 * a PPPoE packet...".
8139 orig_linktype = off_linktype; /* save original values */
8140 orig_nl = off_nl;
8141 is_pppoes = 1;
8144 * The "network-layer" protocol is PPPoE, which has a 6-byte
8145 * PPPoE header, followed by a PPP packet.
8147 * There is no HDLC encapsulation for the PPP packet (it's
8148 * encapsulated in PPPoES instead), so the link-layer type
8149 * starts at the first byte of the PPP packet. For PPPoE,
8150 * that offset is relative to the beginning of the total
8151 * link-layer payload, including any 802.2 LLC header, so
8152 * it's 6 bytes past off_nl.
8154 off_linktype = off_nl + 6;
8157 * The network-layer offsets are relative to the beginning
8158 * of the MAC-layer payload; that's past the 6-byte
8159 * PPPoE header and the 2-byte PPP header.
8161 off_nl = 6+2;
8162 off_nl_nosnap = 6+2;
8164 return b0;
8167 struct block *
8168 gen_atmfield_code(atmfield, jvalue, jtype, reverse)
8169 int atmfield;
8170 bpf_int32 jvalue;
8171 bpf_u_int32 jtype;
8172 int reverse;
8174 struct block *b0;
8176 switch (atmfield) {
8178 case A_VPI:
8179 if (!is_atm)
8180 bpf_error("'vpi' supported only on raw ATM");
8181 if (off_vpi == (u_int)-1)
8182 abort();
8183 b0 = gen_ncmp(OR_LINK, off_vpi, BPF_B, 0xffffffff, jtype,
8184 reverse, jvalue);
8185 break;
8187 case A_VCI:
8188 if (!is_atm)
8189 bpf_error("'vci' supported only on raw ATM");
8190 if (off_vci == (u_int)-1)
8191 abort();
8192 b0 = gen_ncmp(OR_LINK, off_vci, BPF_H, 0xffffffff, jtype,
8193 reverse, jvalue);
8194 break;
8196 case A_PROTOTYPE:
8197 if (off_proto == (u_int)-1)
8198 abort(); /* XXX - this isn't on FreeBSD */
8199 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0x0f, jtype,
8200 reverse, jvalue);
8201 break;
8203 case A_MSGTYPE:
8204 if (off_payload == (u_int)-1)
8205 abort();
8206 b0 = gen_ncmp(OR_LINK, off_payload + MSG_TYPE_POS, BPF_B,
8207 0xffffffff, jtype, reverse, jvalue);
8208 break;
8210 case A_CALLREFTYPE:
8211 if (!is_atm)
8212 bpf_error("'callref' supported only on raw ATM");
8213 if (off_proto == (u_int)-1)
8214 abort();
8215 b0 = gen_ncmp(OR_LINK, off_proto, BPF_B, 0xffffffff,
8216 jtype, reverse, jvalue);
8217 break;
8219 default:
8220 abort();
8222 return b0;
8225 struct block *
8226 gen_atmtype_abbrev(type)
8227 int type;
8229 struct block *b0, *b1;
8231 switch (type) {
8233 case A_METAC:
8234 /* Get all packets in Meta signalling Circuit */
8235 if (!is_atm)
8236 bpf_error("'metac' supported only on raw ATM");
8237 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8238 b1 = gen_atmfield_code(A_VCI, 1, BPF_JEQ, 0);
8239 gen_and(b0, b1);
8240 break;
8242 case A_BCC:
8243 /* Get all packets in Broadcast Circuit*/
8244 if (!is_atm)
8245 bpf_error("'bcc' supported only on raw ATM");
8246 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8247 b1 = gen_atmfield_code(A_VCI, 2, BPF_JEQ, 0);
8248 gen_and(b0, b1);
8249 break;
8251 case A_OAMF4SC:
8252 /* Get all cells in Segment OAM F4 circuit*/
8253 if (!is_atm)
8254 bpf_error("'oam4sc' supported only on raw ATM");
8255 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8256 b1 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8257 gen_and(b0, b1);
8258 break;
8260 case A_OAMF4EC:
8261 /* Get all cells in End-to-End OAM F4 Circuit*/
8262 if (!is_atm)
8263 bpf_error("'oam4ec' supported only on raw ATM");
8264 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8265 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8266 gen_and(b0, b1);
8267 break;
8269 case A_SC:
8270 /* Get all packets in connection Signalling Circuit */
8271 if (!is_atm)
8272 bpf_error("'sc' supported only on raw ATM");
8273 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8274 b1 = gen_atmfield_code(A_VCI, 5, BPF_JEQ, 0);
8275 gen_and(b0, b1);
8276 break;
8278 case A_ILMIC:
8279 /* Get all packets in ILMI Circuit */
8280 if (!is_atm)
8281 bpf_error("'ilmic' supported only on raw ATM");
8282 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8283 b1 = gen_atmfield_code(A_VCI, 16, BPF_JEQ, 0);
8284 gen_and(b0, b1);
8285 break;
8287 case A_LANE:
8288 /* Get all LANE packets */
8289 if (!is_atm)
8290 bpf_error("'lane' supported only on raw ATM");
8291 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LANE, BPF_JEQ, 0);
8294 * Arrange that all subsequent tests assume LANE
8295 * rather than LLC-encapsulated packets, and set
8296 * the offsets appropriately for LANE-encapsulated
8297 * Ethernet.
8299 * "off_mac" is the offset of the Ethernet header,
8300 * which is 2 bytes past the ATM pseudo-header
8301 * (skipping the pseudo-header and 2-byte LE Client
8302 * field). The other offsets are Ethernet offsets
8303 * relative to "off_mac".
8305 is_lane = 1;
8306 off_mac = off_payload + 2; /* MAC header */
8307 off_linktype = off_mac + 12;
8308 off_macpl = off_mac + 14; /* Ethernet */
8309 off_nl = 0; /* Ethernet II */
8310 off_nl_nosnap = 3; /* 802.3+802.2 */
8311 break;
8313 case A_LLC:
8314 /* Get all LLC-encapsulated packets */
8315 if (!is_atm)
8316 bpf_error("'llc' supported only on raw ATM");
8317 b1 = gen_atmfield_code(A_PROTOTYPE, PT_LLC, BPF_JEQ, 0);
8318 is_lane = 0;
8319 break;
8321 default:
8322 abort();
8324 return b1;
8328 * Filtering for MTP2 messages based on li value
8329 * FISU, length is null
8330 * LSSU, length is 1 or 2
8331 * MSU, length is 3 or more
8333 struct block *
8334 gen_mtp2type_abbrev(type)
8335 int type;
8337 struct block *b0, *b1;
8339 switch (type) {
8341 case M_FISU:
8342 if ( (linktype != DLT_MTP2) &&
8343 (linktype != DLT_ERF) &&
8344 (linktype != DLT_MTP2_WITH_PHDR) )
8345 bpf_error("'fisu' supported only on MTP2");
8346 /* gen_ncmp(offrel, offset, size, mask, jtype, reverse, value) */
8347 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JEQ, 0, 0);
8348 break;
8350 case M_LSSU:
8351 if ( (linktype != DLT_MTP2) &&
8352 (linktype != DLT_ERF) &&
8353 (linktype != DLT_MTP2_WITH_PHDR) )
8354 bpf_error("'lssu' supported only on MTP2");
8355 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 1, 2);
8356 b1 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 0);
8357 gen_and(b1, b0);
8358 break;
8360 case M_MSU:
8361 if ( (linktype != DLT_MTP2) &&
8362 (linktype != DLT_ERF) &&
8363 (linktype != DLT_MTP2_WITH_PHDR) )
8364 bpf_error("'msu' supported only on MTP2");
8365 b0 = gen_ncmp(OR_PACKET, off_li, BPF_B, 0x3f, BPF_JGT, 0, 2);
8366 break;
8368 default:
8369 abort();
8371 return b0;
8374 struct block *
8375 gen_mtp3field_code(mtp3field, jvalue, jtype, reverse)
8376 int mtp3field;
8377 bpf_u_int32 jvalue;
8378 bpf_u_int32 jtype;
8379 int reverse;
8381 struct block *b0;
8382 bpf_u_int32 val1 , val2 , val3;
8384 switch (mtp3field) {
8386 case M_SIO:
8387 if (off_sio == (u_int)-1)
8388 bpf_error("'sio' supported only on SS7");
8389 /* sio coded on 1 byte so max value 255 */
8390 if(jvalue > 255)
8391 bpf_error("sio value %u too big; max value = 255",
8392 jvalue);
8393 b0 = gen_ncmp(OR_PACKET, off_sio, BPF_B, 0xffffffff,
8394 (u_int)jtype, reverse, (u_int)jvalue);
8395 break;
8397 case M_OPC:
8398 if (off_opc == (u_int)-1)
8399 bpf_error("'opc' supported only on SS7");
8400 /* opc coded on 14 bits so max value 16383 */
8401 if (jvalue > 16383)
8402 bpf_error("opc value %u too big; max value = 16383",
8403 jvalue);
8404 /* the following instructions are made to convert jvalue
8405 * to the form used to write opc in an ss7 message*/
8406 val1 = jvalue & 0x00003c00;
8407 val1 = val1 >>10;
8408 val2 = jvalue & 0x000003fc;
8409 val2 = val2 <<6;
8410 val3 = jvalue & 0x00000003;
8411 val3 = val3 <<22;
8412 jvalue = val1 + val2 + val3;
8413 b0 = gen_ncmp(OR_PACKET, off_opc, BPF_W, 0x00c0ff0f,
8414 (u_int)jtype, reverse, (u_int)jvalue);
8415 break;
8417 case M_DPC:
8418 if (off_dpc == (u_int)-1)
8419 bpf_error("'dpc' supported only on SS7");
8420 /* dpc coded on 14 bits so max value 16383 */
8421 if (jvalue > 16383)
8422 bpf_error("dpc value %u too big; max value = 16383",
8423 jvalue);
8424 /* the following instructions are made to convert jvalue
8425 * to the forme used to write dpc in an ss7 message*/
8426 val1 = jvalue & 0x000000ff;
8427 val1 = val1 << 24;
8428 val2 = jvalue & 0x00003f00;
8429 val2 = val2 << 8;
8430 jvalue = val1 + val2;
8431 b0 = gen_ncmp(OR_PACKET, off_dpc, BPF_W, 0xff3f0000,
8432 (u_int)jtype, reverse, (u_int)jvalue);
8433 break;
8435 case M_SLS:
8436 if (off_sls == (u_int)-1)
8437 bpf_error("'sls' supported only on SS7");
8438 /* sls coded on 4 bits so max value 15 */
8439 if (jvalue > 15)
8440 bpf_error("sls value %u too big; max value = 15",
8441 jvalue);
8442 /* the following instruction is made to convert jvalue
8443 * to the forme used to write sls in an ss7 message*/
8444 jvalue = jvalue << 4;
8445 b0 = gen_ncmp(OR_PACKET, off_sls, BPF_B, 0xf0,
8446 (u_int)jtype,reverse, (u_int)jvalue);
8447 break;
8449 default:
8450 abort();
8452 return b0;
8455 static struct block *
8456 gen_msg_abbrev(type)
8457 int type;
8459 struct block *b1;
8462 * Q.2931 signalling protocol messages for handling virtual circuits
8463 * establishment and teardown
8465 switch (type) {
8467 case A_SETUP:
8468 b1 = gen_atmfield_code(A_MSGTYPE, SETUP, BPF_JEQ, 0);
8469 break;
8471 case A_CALLPROCEED:
8472 b1 = gen_atmfield_code(A_MSGTYPE, CALL_PROCEED, BPF_JEQ, 0);
8473 break;
8475 case A_CONNECT:
8476 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT, BPF_JEQ, 0);
8477 break;
8479 case A_CONNECTACK:
8480 b1 = gen_atmfield_code(A_MSGTYPE, CONNECT_ACK, BPF_JEQ, 0);
8481 break;
8483 case A_RELEASE:
8484 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE, BPF_JEQ, 0);
8485 break;
8487 case A_RELEASE_DONE:
8488 b1 = gen_atmfield_code(A_MSGTYPE, RELEASE_DONE, BPF_JEQ, 0);
8489 break;
8491 default:
8492 abort();
8494 return b1;
8497 struct block *
8498 gen_atmmulti_abbrev(type)
8499 int type;
8501 struct block *b0, *b1;
8503 switch (type) {
8505 case A_OAM:
8506 if (!is_atm)
8507 bpf_error("'oam' supported only on raw ATM");
8508 b1 = gen_atmmulti_abbrev(A_OAMF4);
8509 break;
8511 case A_OAMF4:
8512 if (!is_atm)
8513 bpf_error("'oamf4' supported only on raw ATM");
8514 /* OAM F4 type */
8515 b0 = gen_atmfield_code(A_VCI, 3, BPF_JEQ, 0);
8516 b1 = gen_atmfield_code(A_VCI, 4, BPF_JEQ, 0);
8517 gen_or(b0, b1);
8518 b0 = gen_atmfield_code(A_VPI, 0, BPF_JEQ, 0);
8519 gen_and(b0, b1);
8520 break;
8522 case A_CONNECTMSG:
8524 * Get Q.2931 signalling messages for switched
8525 * virtual connection
8527 if (!is_atm)
8528 bpf_error("'connectmsg' supported only on raw ATM");
8529 b0 = gen_msg_abbrev(A_SETUP);
8530 b1 = gen_msg_abbrev(A_CALLPROCEED);
8531 gen_or(b0, b1);
8532 b0 = gen_msg_abbrev(A_CONNECT);
8533 gen_or(b0, b1);
8534 b0 = gen_msg_abbrev(A_CONNECTACK);
8535 gen_or(b0, b1);
8536 b0 = gen_msg_abbrev(A_RELEASE);
8537 gen_or(b0, b1);
8538 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8539 gen_or(b0, b1);
8540 b0 = gen_atmtype_abbrev(A_SC);
8541 gen_and(b0, b1);
8542 break;
8544 case A_METACONNECT:
8545 if (!is_atm)
8546 bpf_error("'metaconnect' supported only on raw ATM");
8547 b0 = gen_msg_abbrev(A_SETUP);
8548 b1 = gen_msg_abbrev(A_CALLPROCEED);
8549 gen_or(b0, b1);
8550 b0 = gen_msg_abbrev(A_CONNECT);
8551 gen_or(b0, b1);
8552 b0 = gen_msg_abbrev(A_RELEASE);
8553 gen_or(b0, b1);
8554 b0 = gen_msg_abbrev(A_RELEASE_DONE);
8555 gen_or(b0, b1);
8556 b0 = gen_atmtype_abbrev(A_METAC);
8557 gen_and(b0, b1);
8558 break;
8560 default:
8561 abort();
8563 return b1;